San Mehat | a19b250 | 2010-01-06 10:33:53 -0800 | [diff] [blame] | 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 | |
Jeff Sharkey | 67b8c49 | 2017-09-21 17:08:43 -0600 | [diff] [blame] | 17 | #define ATRACE_TAG ATRACE_TAG_PACKAGE_MANAGER |
| 18 | |
Paul Crowley | 14c8c07 | 2018-09-18 13:30:21 -0700 | [diff] [blame] | 19 | #include <dirent.h> |
| 20 | #include <errno.h> |
| 21 | #include <fcntl.h> |
San Mehat | a19b250 | 2010-01-06 10:33:53 -0800 | [diff] [blame] | 22 | #include <stdio.h> |
Olivier Bailly | 37dcda6 | 2010-11-16 10:41:53 -0800 | [diff] [blame] | 23 | #include <stdlib.h> |
San Mehat | a19b250 | 2010-01-06 10:33:53 -0800 | [diff] [blame] | 24 | #include <string.h> |
Paul Crowley | 14c8c07 | 2018-09-18 13:30:21 -0700 | [diff] [blame] | 25 | #include <unistd.h> |
San Mehat | a19b250 | 2010-01-06 10:33:53 -0800 | [diff] [blame] | 26 | |
Olivier Bailly | 37dcda6 | 2010-11-16 10:41:53 -0800 | [diff] [blame] | 27 | #include <sys/ioctl.h> |
Paul Crowley | 14c8c07 | 2018-09-18 13:30:21 -0700 | [diff] [blame] | 28 | #include <sys/mount.h> |
| 29 | #include <sys/stat.h> |
| 30 | #include <sys/types.h> |
San Mehat | 8da6bcb | 2010-01-09 12:24:05 -0800 | [diff] [blame] | 31 | |
San Mehat | d9a4e35 | 2010-03-12 13:32:47 -0800 | [diff] [blame] | 32 | #include <linux/kdev_t.h> |
| 33 | |
David Anderson | 1dd5c4f | 2019-06-11 14:51:25 -0700 | [diff] [blame] | 34 | #include <chrono> |
| 35 | |
Jeff Sharkey | fa1c677 | 2017-03-25 22:49:13 -0600 | [diff] [blame] | 36 | #include <android-base/logging.h> |
| 37 | #include <android-base/stringprintf.h> |
Paul Crowley | 14c8c07 | 2018-09-18 13:30:21 -0700 | [diff] [blame] | 38 | #include <android-base/strings.h> |
Jeff Sharkey | fa1c677 | 2017-03-25 22:49:13 -0600 | [diff] [blame] | 39 | #include <android-base/unique_fd.h> |
David Anderson | 1dd5c4f | 2019-06-11 14:51:25 -0700 | [diff] [blame] | 40 | #include <fs_mgr/file_wait.h> |
Jeff Sharkey | 67b8c49 | 2017-09-21 17:08:43 -0600 | [diff] [blame] | 41 | #include <utils/Trace.h> |
Jeff Sharkey | fa1c677 | 2017-03-25 22:49:13 -0600 | [diff] [blame] | 42 | |
San Mehat | a19b250 | 2010-01-06 10:33:53 -0800 | [diff] [blame] | 43 | #include "Loop.h" |
Hiroaki Miyazawa | 14eab55 | 2015-02-04 13:29:15 +0900 | [diff] [blame] | 44 | #include "VoldUtil.h" |
Stephen Smalley | 684e662 | 2014-09-30 10:29:24 -0400 | [diff] [blame] | 45 | #include "sehandle.h" |
San Mehat | a19b250 | 2010-01-06 10:33:53 -0800 | [diff] [blame] | 46 | |
David Anderson | 1dd5c4f | 2019-06-11 14:51:25 -0700 | [diff] [blame] | 47 | using namespace std::literals; |
Jeff Sharkey | fa1c677 | 2017-03-25 22:49:13 -0600 | [diff] [blame] | 48 | using android::base::StringPrintf; |
| 49 | using android::base::unique_fd; |
| 50 | |
Jeff Sharkey | 11c2d38 | 2017-09-11 10:32:01 -0600 | [diff] [blame] | 51 | static const char* kVoldPrefix = "vold:"; |
Daniel Rosenberg | 4538cb2 | 2019-04-01 16:09:28 -0700 | [diff] [blame] | 52 | static constexpr size_t kLoopDeviceRetryAttempts = 3u; |
Jeff Sharkey | 11c2d38 | 2017-09-11 10:32:01 -0600 | [diff] [blame] | 53 | |
Jeff Sharkey | fa1c677 | 2017-03-25 22:49:13 -0600 | [diff] [blame] | 54 | int Loop::create(const std::string& target, std::string& out_device) { |
Jeff Sharkey | fd3dc3c | 2017-03-27 10:49:21 -0600 | [diff] [blame] | 55 | unique_fd ctl_fd(open("/dev/loop-control", O_RDWR | O_CLOEXEC)); |
Jeff Sharkey | fa1c677 | 2017-03-25 22:49:13 -0600 | [diff] [blame] | 56 | if (ctl_fd.get() == -1) { |
| 57 | PLOG(ERROR) << "Failed to open loop-control"; |
| 58 | return -errno; |
| 59 | } |
| 60 | |
| 61 | int num = ioctl(ctl_fd.get(), LOOP_CTL_GET_FREE); |
| 62 | if (num == -1) { |
| 63 | PLOG(ERROR) << "Failed LOOP_CTL_GET_FREE"; |
| 64 | return -errno; |
| 65 | } |
| 66 | |
| 67 | out_device = StringPrintf("/dev/block/loop%d", num); |
| 68 | |
Daniel Rosenberg | 4538cb2 | 2019-04-01 16:09:28 -0700 | [diff] [blame] | 69 | unique_fd target_fd; |
| 70 | for (size_t i = 0; i != kLoopDeviceRetryAttempts; ++i) { |
| 71 | target_fd.reset(open(target.c_str(), O_RDWR | O_CLOEXEC)); |
| 72 | if (target_fd.get() != -1) { |
| 73 | break; |
| 74 | } |
| 75 | usleep(50000); |
| 76 | } |
Jeff Sharkey | fa1c677 | 2017-03-25 22:49:13 -0600 | [diff] [blame] | 77 | if (target_fd.get() == -1) { |
| 78 | PLOG(ERROR) << "Failed to open " << target; |
| 79 | return -errno; |
| 80 | } |
David Anderson | 1dd5c4f | 2019-06-11 14:51:25 -0700 | [diff] [blame] | 81 | if (!android::fs_mgr::WaitForFile(out_device, 2s)) { |
| 82 | LOG(ERROR) << "Failed to find " << out_device; |
| 83 | return -ENOENT; |
| 84 | } |
Jeff Sharkey | fd3dc3c | 2017-03-27 10:49:21 -0600 | [diff] [blame] | 85 | unique_fd device_fd(open(out_device.c_str(), O_RDWR | O_CLOEXEC)); |
Jeff Sharkey | fa1c677 | 2017-03-25 22:49:13 -0600 | [diff] [blame] | 86 | if (device_fd.get() == -1) { |
| 87 | PLOG(ERROR) << "Failed to open " << out_device; |
| 88 | return -errno; |
| 89 | } |
| 90 | |
| 91 | if (ioctl(device_fd.get(), LOOP_SET_FD, target_fd.get()) == -1) { |
| 92 | PLOG(ERROR) << "Failed to LOOP_SET_FD"; |
| 93 | return -errno; |
| 94 | } |
| 95 | |
Jeff Sharkey | 11c2d38 | 2017-09-11 10:32:01 -0600 | [diff] [blame] | 96 | struct loop_info64 li; |
| 97 | memset(&li, 0, sizeof(li)); |
Paul Crowley | 14c8c07 | 2018-09-18 13:30:21 -0700 | [diff] [blame] | 98 | strlcpy((char*)li.lo_crypt_name, kVoldPrefix, LO_NAME_SIZE); |
Jeff Sharkey | 11c2d38 | 2017-09-11 10:32:01 -0600 | [diff] [blame] | 99 | if (ioctl(device_fd.get(), LOOP_SET_STATUS64, &li) == -1) { |
| 100 | PLOG(ERROR) << "Failed to LOOP_SET_STATUS64"; |
| 101 | return -errno; |
| 102 | } |
| 103 | |
Jeff Sharkey | fa1c677 | 2017-03-25 22:49:13 -0600 | [diff] [blame] | 104 | return 0; |
| 105 | } |
| 106 | |
Paul Crowley | 14c8c07 | 2018-09-18 13:30:21 -0700 | [diff] [blame] | 107 | int Loop::destroyByDevice(const char* loopDevice) { |
San Mehat | a19b250 | 2010-01-06 10:33:53 -0800 | [diff] [blame] | 108 | int device_fd; |
| 109 | |
Jeff Sharkey | ce6a913 | 2015-04-08 21:07:21 -0700 | [diff] [blame] | 110 | device_fd = open(loopDevice, O_RDONLY | O_CLOEXEC); |
San Mehat | a19b250 | 2010-01-06 10:33:53 -0800 | [diff] [blame] | 111 | if (device_fd < 0) { |
Jeff Sharkey | 3472e52 | 2017-10-06 18:02:53 -0600 | [diff] [blame] | 112 | PLOG(ERROR) << "Failed to open " << loopDevice; |
San Mehat | a19b250 | 2010-01-06 10:33:53 -0800 | [diff] [blame] | 113 | return -1; |
| 114 | } |
| 115 | |
| 116 | if (ioctl(device_fd, LOOP_CLR_FD, 0) < 0) { |
Jeff Sharkey | 3472e52 | 2017-10-06 18:02:53 -0600 | [diff] [blame] | 117 | PLOG(ERROR) << "Failed to destroy " << loopDevice; |
San Mehat | a19b250 | 2010-01-06 10:33:53 -0800 | [diff] [blame] | 118 | close(device_fd); |
| 119 | return -1; |
| 120 | } |
| 121 | |
| 122 | close(device_fd); |
| 123 | return 0; |
| 124 | } |
| 125 | |
Jeff Sharkey | 11c2d38 | 2017-09-11 10:32:01 -0600 | [diff] [blame] | 126 | int Loop::destroyAll() { |
Jeff Sharkey | 67b8c49 | 2017-09-21 17:08:43 -0600 | [diff] [blame] | 127 | ATRACE_NAME("Loop::destroyAll"); |
Jeff Sharkey | 11c2d38 | 2017-09-11 10:32:01 -0600 | [diff] [blame] | 128 | |
Jeff Sharkey | 67b8c49 | 2017-09-21 17:08:43 -0600 | [diff] [blame] | 129 | std::string root = "/dev/block/"; |
Jeff Sharkey | 5540b44 | 2018-02-24 18:09:21 -0700 | [diff] [blame] | 130 | auto dirp = std::unique_ptr<DIR, int (*)(DIR*)>(opendir(root.c_str()), closedir); |
| 131 | if (!dirp) { |
Jeff Sharkey | 67b8c49 | 2017-09-21 17:08:43 -0600 | [diff] [blame] | 132 | PLOG(ERROR) << "Failed to opendir"; |
| 133 | return -1; |
| 134 | } |
| 135 | |
| 136 | // Poke through all devices looking for loops |
Jeff Sharkey | 5540b44 | 2018-02-24 18:09:21 -0700 | [diff] [blame] | 137 | struct dirent* de; |
| 138 | while ((de = readdir(dirp.get()))) { |
Jeff Sharkey | 3472e52 | 2017-10-06 18:02:53 -0600 | [diff] [blame] | 139 | auto test = std::string(de->d_name); |
| 140 | if (!android::base::StartsWith(test, "loop")) continue; |
Jeff Sharkey | 67b8c49 | 2017-09-21 17:08:43 -0600 | [diff] [blame] | 141 | |
| 142 | auto path = root + de->d_name; |
Jeff Sharkey | 11c2d38 | 2017-09-11 10:32:01 -0600 | [diff] [blame] | 143 | unique_fd fd(open(path.c_str(), O_RDWR | O_CLOEXEC)); |
| 144 | if (fd.get() == -1) { |
| 145 | if (errno != ENOENT) { |
| 146 | PLOG(WARNING) << "Failed to open " << path; |
| 147 | } |
| 148 | continue; |
| 149 | } |
| 150 | |
| 151 | struct loop_info64 li; |
| 152 | if (ioctl(fd.get(), LOOP_GET_STATUS64, &li) < 0) { |
Eric Biggers | 10724d9 | 2020-10-15 14:52:54 -0700 | [diff] [blame] | 153 | if (errno != ENXIO) { |
| 154 | PLOG(WARNING) << "Failed to LOOP_GET_STATUS64 " << path; |
| 155 | } |
Jeff Sharkey | 11c2d38 | 2017-09-11 10:32:01 -0600 | [diff] [blame] | 156 | continue; |
| 157 | } |
| 158 | |
Paul Crowley | 14c8c07 | 2018-09-18 13:30:21 -0700 | [diff] [blame] | 159 | auto id = std::string((char*)li.lo_crypt_name); |
Jeff Sharkey | 3472e52 | 2017-10-06 18:02:53 -0600 | [diff] [blame] | 160 | if (android::base::StartsWith(id, kVoldPrefix)) { |
Jeff Sharkey | 11c2d38 | 2017-09-11 10:32:01 -0600 | [diff] [blame] | 161 | LOG(DEBUG) << "Tearing down stale loop device at " << path << " named " << id; |
| 162 | |
| 163 | if (ioctl(fd.get(), LOOP_CLR_FD, 0) < 0) { |
| 164 | PLOG(WARNING) << "Failed to LOOP_CLR_FD " << path; |
| 165 | } |
| 166 | } else { |
Sudheer Shanka | 4b6ca4e | 2018-09-21 10:54:54 -0700 | [diff] [blame] | 167 | LOG(DEBUG) << "Found unmanaged loop device at " << path << " named " << id; |
Jeff Sharkey | 11c2d38 | 2017-09-11 10:32:01 -0600 | [diff] [blame] | 168 | } |
| 169 | } |
Jeff Sharkey | 67b8c49 | 2017-09-21 17:08:43 -0600 | [diff] [blame] | 170 | |
Jeff Sharkey | 11c2d38 | 2017-09-11 10:32:01 -0600 | [diff] [blame] | 171 | return 0; |
San Mehat | a19b250 | 2010-01-06 10:33:53 -0800 | [diff] [blame] | 172 | } |
| 173 | |
Paul Crowley | 14c8c07 | 2018-09-18 13:30:21 -0700 | [diff] [blame] | 174 | int Loop::createImageFile(const char* file, unsigned long numSectors) { |
Jeff Sharkey | fd3dc3c | 2017-03-27 10:49:21 -0600 | [diff] [blame] | 175 | unique_fd fd(open(file, O_CREAT | O_WRONLY | O_TRUNC | O_CLOEXEC, 0600)); |
Jeff Sharkey | fa1c677 | 2017-03-25 22:49:13 -0600 | [diff] [blame] | 176 | if (fd.get() == -1) { |
| 177 | PLOG(ERROR) << "Failed to create image " << file; |
Jeff Sharkey | fd3dc3c | 2017-03-27 10:49:21 -0600 | [diff] [blame] | 178 | return -errno; |
San Mehat | a19b250 | 2010-01-06 10:33:53 -0800 | [diff] [blame] | 179 | } |
Jeff Sharkey | fa1c677 | 2017-03-25 22:49:13 -0600 | [diff] [blame] | 180 | if (fallocate(fd.get(), 0, 0, numSectors * 512) == -1) { |
| 181 | PLOG(WARNING) << "Failed to fallocate; falling back to ftruncate"; |
| 182 | if (ftruncate(fd, numSectors * 512) == -1) { |
| 183 | PLOG(ERROR) << "Failed to ftruncate"; |
Jeff Sharkey | fd3dc3c | 2017-03-27 10:49:21 -0600 | [diff] [blame] | 184 | return -errno; |
Jeff Sharkey | fa1c677 | 2017-03-25 22:49:13 -0600 | [diff] [blame] | 185 | } |
| 186 | } |
Jeff Sharkey | fd3dc3c | 2017-03-27 10:49:21 -0600 | [diff] [blame] | 187 | return 0; |
San Mehat | a19b250 | 2010-01-06 10:33:53 -0800 | [diff] [blame] | 188 | } |
Kenny Root | 344ca10 | 2012-04-03 17:23:01 -0700 | [diff] [blame] | 189 | |
Paul Crowley | 14c8c07 | 2018-09-18 13:30:21 -0700 | [diff] [blame] | 190 | int Loop::resizeImageFile(const char* file, unsigned long numSectors) { |
Daniel Rosenberg | fcd34a0 | 2014-05-22 11:23:56 -0700 | [diff] [blame] | 191 | int fd; |
| 192 | |
Jeff Sharkey | ce6a913 | 2015-04-08 21:07:21 -0700 | [diff] [blame] | 193 | if ((fd = open(file, O_RDWR | O_CLOEXEC)) < 0) { |
Jeff Sharkey | 3472e52 | 2017-10-06 18:02:53 -0600 | [diff] [blame] | 194 | PLOG(ERROR) << "Failed to open " << file; |
Daniel Rosenberg | fcd34a0 | 2014-05-22 11:23:56 -0700 | [diff] [blame] | 195 | return -1; |
| 196 | } |
| 197 | |
Jeff Sharkey | 3472e52 | 2017-10-06 18:02:53 -0600 | [diff] [blame] | 198 | LOG(DEBUG) << "Attempting to increase " << file << " to " << numSectors; |
Daniel Rosenberg | fcd34a0 | 2014-05-22 11:23:56 -0700 | [diff] [blame] | 199 | |
| 200 | if (fallocate(fd, 0, 0, numSectors * 512)) { |
Jeff Sharkey | 43ed123 | 2014-08-22 12:29:05 -0700 | [diff] [blame] | 201 | if (errno == ENOSYS || errno == ENOTSUP) { |
Jeff Sharkey | 3472e52 | 2017-10-06 18:02:53 -0600 | [diff] [blame] | 202 | PLOG(WARNING) << "fallocate not found. Falling back to ftruncate."; |
Daniel Rosenberg | fcd34a0 | 2014-05-22 11:23:56 -0700 | [diff] [blame] | 203 | if (ftruncate(fd, numSectors * 512) < 0) { |
Jeff Sharkey | 3472e52 | 2017-10-06 18:02:53 -0600 | [diff] [blame] | 204 | PLOG(ERROR) << "Failed to ftruncate"; |
Daniel Rosenberg | fcd34a0 | 2014-05-22 11:23:56 -0700 | [diff] [blame] | 205 | close(fd); |
| 206 | return -1; |
| 207 | } |
| 208 | } else { |
Jeff Sharkey | 3472e52 | 2017-10-06 18:02:53 -0600 | [diff] [blame] | 209 | PLOG(ERROR) << "Failed to fallocate"; |
Daniel Rosenberg | fcd34a0 | 2014-05-22 11:23:56 -0700 | [diff] [blame] | 210 | close(fd); |
| 211 | return -1; |
| 212 | } |
| 213 | } |
| 214 | close(fd); |
| 215 | return 0; |
| 216 | } |