loop_control.cpp 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. /*
  2. * Copyright (C) 2018 The Android Open Source Project
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. #include "libdm/loop_control.h"
  17. #include <fcntl.h>
  18. #include <linux/loop.h>
  19. #include <stdint.h>
  20. #include <sys/ioctl.h>
  21. #include <sys/types.h>
  22. #include <unistd.h>
  23. #include <android-base/logging.h>
  24. #include <android-base/stringprintf.h>
  25. #include <android-base/unique_fd.h>
  26. namespace android {
  27. namespace dm {
  28. LoopControl::LoopControl() : control_fd_(-1) {
  29. control_fd_.reset(TEMP_FAILURE_RETRY(open(kLoopControlDevice, O_RDWR | O_CLOEXEC)));
  30. if (control_fd_ < 0) {
  31. PLOG(ERROR) << "Failed to open loop-control";
  32. }
  33. }
  34. bool LoopControl::Attach(int file_fd, std::string* loopdev) const {
  35. if (!FindFreeLoopDevice(loopdev)) {
  36. LOG(ERROR) << "Failed to attach, no free loop devices";
  37. return false;
  38. }
  39. android::base::unique_fd loop_fd(TEMP_FAILURE_RETRY(open(loopdev->c_str(), O_RDWR | O_CLOEXEC)));
  40. if (loop_fd < 0) {
  41. PLOG(ERROR) << "Failed to open: " << *loopdev;
  42. return false;
  43. }
  44. int rc = ioctl(loop_fd, LOOP_SET_FD, file_fd);
  45. if (rc < 0) {
  46. PLOG(ERROR) << "Failed LOOP_SET_FD";
  47. return false;
  48. }
  49. return true;
  50. }
  51. bool LoopControl::Detach(const std::string& loopdev) const {
  52. if (loopdev.empty()) {
  53. LOG(ERROR) << "Must provide a loop device";
  54. return false;
  55. }
  56. android::base::unique_fd loop_fd(TEMP_FAILURE_RETRY(open(loopdev.c_str(), O_RDWR | O_CLOEXEC)));
  57. if (loop_fd < 0) {
  58. PLOG(ERROR) << "Failed to open: " << loopdev;
  59. return false;
  60. }
  61. int rc = ioctl(loop_fd, LOOP_CLR_FD, 0);
  62. if (rc) {
  63. PLOG(ERROR) << "Failed LOOP_CLR_FD for '" << loopdev << "'";
  64. return false;
  65. }
  66. return true;
  67. }
  68. bool LoopControl::FindFreeLoopDevice(std::string* loopdev) const {
  69. int rc = ioctl(control_fd_, LOOP_CTL_GET_FREE);
  70. if (rc < 0) {
  71. PLOG(ERROR) << "Failed to get free loop device";
  72. return false;
  73. }
  74. // Ueventd on android creates all loop devices as /dev/block/loopX
  75. // The total number of available devices is determined by 'loop.max_part'
  76. // kernel command line argument.
  77. *loopdev = ::android::base::StringPrintf("/dev/block/loop%d", rc);
  78. return true;
  79. }
  80. LoopDevice::LoopDevice(int fd, bool auto_close) : fd_(fd), owns_fd_(auto_close) {
  81. Init();
  82. }
  83. LoopDevice::LoopDevice(const std::string& path) : fd_(-1), owns_fd_(true) {
  84. fd_.reset(open(path.c_str(), O_RDWR | O_CLOEXEC));
  85. if (fd_ < -1) {
  86. PLOG(ERROR) << "open failed for " << path;
  87. return;
  88. }
  89. Init();
  90. }
  91. LoopDevice::~LoopDevice() {
  92. if (valid()) {
  93. control_.Detach(device_);
  94. }
  95. if (!owns_fd_) {
  96. (void)fd_.release();
  97. }
  98. }
  99. void LoopDevice::Init() {
  100. control_.Attach(fd_, &device_);
  101. }
  102. } // namespace dm
  103. } // namespace android