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