Loop.cpp 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  1. /*
  2. * Copyright (C) 2008 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. #define ATRACE_TAG ATRACE_TAG_PACKAGE_MANAGER
  17. #include <dirent.h>
  18. #include <errno.h>
  19. #include <fcntl.h>
  20. #include <stdio.h>
  21. #include <stdlib.h>
  22. #include <string.h>
  23. #include <unistd.h>
  24. #include <sys/ioctl.h>
  25. #include <sys/mount.h>
  26. #include <sys/stat.h>
  27. #include <sys/types.h>
  28. #include <linux/kdev_t.h>
  29. #include <android-base/logging.h>
  30. #include <android-base/stringprintf.h>
  31. #include <android-base/strings.h>
  32. #include <android-base/unique_fd.h>
  33. #include <utils/Trace.h>
  34. #include "Loop.h"
  35. #include "VoldUtil.h"
  36. #include "sehandle.h"
  37. using android::base::StringPrintf;
  38. using android::base::unique_fd;
  39. static const char* kVoldPrefix = "vold:";
  40. static constexpr size_t kLoopDeviceRetryAttempts = 3u;
  41. int Loop::create(const std::string& target, std::string& out_device) {
  42. unique_fd ctl_fd(open("/dev/loop-control", O_RDWR | O_CLOEXEC));
  43. if (ctl_fd.get() == -1) {
  44. PLOG(ERROR) << "Failed to open loop-control";
  45. return -errno;
  46. }
  47. int num = ioctl(ctl_fd.get(), LOOP_CTL_GET_FREE);
  48. if (num == -1) {
  49. PLOG(ERROR) << "Failed LOOP_CTL_GET_FREE";
  50. return -errno;
  51. }
  52. out_device = StringPrintf("/dev/block/loop%d", num);
  53. unique_fd target_fd;
  54. for (size_t i = 0; i != kLoopDeviceRetryAttempts; ++i) {
  55. target_fd.reset(open(target.c_str(), O_RDWR | O_CLOEXEC));
  56. if (target_fd.get() != -1) {
  57. break;
  58. }
  59. usleep(50000);
  60. }
  61. if (target_fd.get() == -1) {
  62. PLOG(ERROR) << "Failed to open " << target;
  63. return -errno;
  64. }
  65. unique_fd device_fd(open(out_device.c_str(), O_RDWR | O_CLOEXEC));
  66. if (device_fd.get() == -1) {
  67. PLOG(ERROR) << "Failed to open " << out_device;
  68. return -errno;
  69. }
  70. if (ioctl(device_fd.get(), LOOP_SET_FD, target_fd.get()) == -1) {
  71. PLOG(ERROR) << "Failed to LOOP_SET_FD";
  72. return -errno;
  73. }
  74. struct loop_info64 li;
  75. memset(&li, 0, sizeof(li));
  76. strlcpy((char*)li.lo_crypt_name, kVoldPrefix, LO_NAME_SIZE);
  77. if (ioctl(device_fd.get(), LOOP_SET_STATUS64, &li) == -1) {
  78. PLOG(ERROR) << "Failed to LOOP_SET_STATUS64";
  79. return -errno;
  80. }
  81. return 0;
  82. }
  83. int Loop::destroyByDevice(const char* loopDevice) {
  84. int device_fd;
  85. device_fd = open(loopDevice, O_RDONLY | O_CLOEXEC);
  86. if (device_fd < 0) {
  87. PLOG(ERROR) << "Failed to open " << loopDevice;
  88. return -1;
  89. }
  90. if (ioctl(device_fd, LOOP_CLR_FD, 0) < 0) {
  91. PLOG(ERROR) << "Failed to destroy " << loopDevice;
  92. close(device_fd);
  93. return -1;
  94. }
  95. close(device_fd);
  96. return 0;
  97. }
  98. int Loop::destroyAll() {
  99. ATRACE_NAME("Loop::destroyAll");
  100. std::string root = "/dev/block/";
  101. auto dirp = std::unique_ptr<DIR, int (*)(DIR*)>(opendir(root.c_str()), closedir);
  102. if (!dirp) {
  103. PLOG(ERROR) << "Failed to opendir";
  104. return -1;
  105. }
  106. // Poke through all devices looking for loops
  107. struct dirent* de;
  108. while ((de = readdir(dirp.get()))) {
  109. auto test = std::string(de->d_name);
  110. if (!android::base::StartsWith(test, "loop")) continue;
  111. auto path = root + de->d_name;
  112. unique_fd fd(open(path.c_str(), O_RDWR | O_CLOEXEC));
  113. if (fd.get() == -1) {
  114. if (errno != ENOENT) {
  115. PLOG(WARNING) << "Failed to open " << path;
  116. }
  117. continue;
  118. }
  119. struct loop_info64 li;
  120. if (ioctl(fd.get(), LOOP_GET_STATUS64, &li) < 0) {
  121. PLOG(WARNING) << "Failed to LOOP_GET_STATUS64 " << path;
  122. continue;
  123. }
  124. auto id = std::string((char*)li.lo_crypt_name);
  125. if (android::base::StartsWith(id, kVoldPrefix)) {
  126. LOG(DEBUG) << "Tearing down stale loop device at " << path << " named " << id;
  127. if (ioctl(fd.get(), LOOP_CLR_FD, 0) < 0) {
  128. PLOG(WARNING) << "Failed to LOOP_CLR_FD " << path;
  129. }
  130. } else {
  131. LOG(DEBUG) << "Found unmanaged loop device at " << path << " named " << id;
  132. }
  133. }
  134. return 0;
  135. }
  136. int Loop::createImageFile(const char* file, unsigned long numSectors) {
  137. unique_fd fd(open(file, O_CREAT | O_WRONLY | O_TRUNC | O_CLOEXEC, 0600));
  138. if (fd.get() == -1) {
  139. PLOG(ERROR) << "Failed to create image " << file;
  140. return -errno;
  141. }
  142. if (fallocate(fd.get(), 0, 0, numSectors * 512) == -1) {
  143. PLOG(WARNING) << "Failed to fallocate; falling back to ftruncate";
  144. if (ftruncate(fd, numSectors * 512) == -1) {
  145. PLOG(ERROR) << "Failed to ftruncate";
  146. return -errno;
  147. }
  148. }
  149. return 0;
  150. }
  151. int Loop::resizeImageFile(const char* file, unsigned long numSectors) {
  152. int fd;
  153. if ((fd = open(file, O_RDWR | O_CLOEXEC)) < 0) {
  154. PLOG(ERROR) << "Failed to open " << file;
  155. return -1;
  156. }
  157. LOG(DEBUG) << "Attempting to increase " << file << " to " << numSectors;
  158. if (fallocate(fd, 0, 0, numSectors * 512)) {
  159. if (errno == ENOSYS || errno == ENOTSUP) {
  160. PLOG(WARNING) << "fallocate not found. Falling back to ftruncate.";
  161. if (ftruncate(fd, numSectors * 512) < 0) {
  162. PLOG(ERROR) << "Failed to ftruncate";
  163. close(fd);
  164. return -1;
  165. }
  166. } else {
  167. PLOG(ERROR) << "Failed to fallocate";
  168. close(fd);
  169. return -1;
  170. }
  171. }
  172. close(fd);
  173. return 0;
  174. }