blob: fa8f8ba2b660774603b693f3df0ffec045e68261 [file] [log] [blame]
San Mehata19b2502010-01-06 10:33:53 -08001/*
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 Sharkey67b8c492017-09-21 17:08:43 -060017#define ATRACE_TAG ATRACE_TAG_PACKAGE_MANAGER
18
Paul Crowley14c8c072018-09-18 13:30:21 -070019#include <dirent.h>
20#include <errno.h>
21#include <fcntl.h>
San Mehata19b2502010-01-06 10:33:53 -080022#include <stdio.h>
Olivier Bailly37dcda62010-11-16 10:41:53 -080023#include <stdlib.h>
San Mehata19b2502010-01-06 10:33:53 -080024#include <string.h>
Paul Crowley14c8c072018-09-18 13:30:21 -070025#include <unistd.h>
San Mehata19b2502010-01-06 10:33:53 -080026
Olivier Bailly37dcda62010-11-16 10:41:53 -080027#include <sys/ioctl.h>
Paul Crowley14c8c072018-09-18 13:30:21 -070028#include <sys/mount.h>
29#include <sys/stat.h>
30#include <sys/types.h>
San Mehat8da6bcb2010-01-09 12:24:05 -080031
San Mehatd9a4e352010-03-12 13:32:47 -080032#include <linux/kdev_t.h>
33
Jeff Sharkeyfa1c6772017-03-25 22:49:13 -060034#include <android-base/logging.h>
35#include <android-base/stringprintf.h>
Paul Crowley14c8c072018-09-18 13:30:21 -070036#include <android-base/strings.h>
Jeff Sharkeyfa1c6772017-03-25 22:49:13 -060037#include <android-base/unique_fd.h>
Jeff Sharkey67b8c492017-09-21 17:08:43 -060038#include <utils/Trace.h>
Jeff Sharkeyfa1c6772017-03-25 22:49:13 -060039
San Mehata19b2502010-01-06 10:33:53 -080040#include "Loop.h"
Hiroaki Miyazawa14eab552015-02-04 13:29:15 +090041#include "VoldUtil.h"
Stephen Smalley684e6622014-09-30 10:29:24 -040042#include "sehandle.h"
San Mehata19b2502010-01-06 10:33:53 -080043
Jeff Sharkeyfa1c6772017-03-25 22:49:13 -060044using android::base::StringPrintf;
45using android::base::unique_fd;
46
Jeff Sharkey11c2d382017-09-11 10:32:01 -060047static const char* kVoldPrefix = "vold:";
Daniel Rosenberg4538cb22019-04-01 16:09:28 -070048static constexpr size_t kLoopDeviceRetryAttempts = 3u;
Jeff Sharkey11c2d382017-09-11 10:32:01 -060049
Jeff Sharkeyfa1c6772017-03-25 22:49:13 -060050int Loop::create(const std::string& target, std::string& out_device) {
Jeff Sharkeyfd3dc3c2017-03-27 10:49:21 -060051 unique_fd ctl_fd(open("/dev/loop-control", O_RDWR | O_CLOEXEC));
Jeff Sharkeyfa1c6772017-03-25 22:49:13 -060052 if (ctl_fd.get() == -1) {
53 PLOG(ERROR) << "Failed to open loop-control";
54 return -errno;
55 }
56
57 int num = ioctl(ctl_fd.get(), LOOP_CTL_GET_FREE);
58 if (num == -1) {
59 PLOG(ERROR) << "Failed LOOP_CTL_GET_FREE";
60 return -errno;
61 }
62
63 out_device = StringPrintf("/dev/block/loop%d", num);
64
Daniel Rosenberg4538cb22019-04-01 16:09:28 -070065 unique_fd target_fd;
66 for (size_t i = 0; i != kLoopDeviceRetryAttempts; ++i) {
67 target_fd.reset(open(target.c_str(), O_RDWR | O_CLOEXEC));
68 if (target_fd.get() != -1) {
69 break;
70 }
71 usleep(50000);
72 }
Jeff Sharkeyfa1c6772017-03-25 22:49:13 -060073 if (target_fd.get() == -1) {
74 PLOG(ERROR) << "Failed to open " << target;
75 return -errno;
76 }
Jeff Sharkeyfd3dc3c2017-03-27 10:49:21 -060077 unique_fd device_fd(open(out_device.c_str(), O_RDWR | O_CLOEXEC));
Jeff Sharkeyfa1c6772017-03-25 22:49:13 -060078 if (device_fd.get() == -1) {
79 PLOG(ERROR) << "Failed to open " << out_device;
80 return -errno;
81 }
82
83 if (ioctl(device_fd.get(), LOOP_SET_FD, target_fd.get()) == -1) {
84 PLOG(ERROR) << "Failed to LOOP_SET_FD";
85 return -errno;
86 }
87
Jeff Sharkey11c2d382017-09-11 10:32:01 -060088 struct loop_info64 li;
89 memset(&li, 0, sizeof(li));
Paul Crowley14c8c072018-09-18 13:30:21 -070090 strlcpy((char*)li.lo_crypt_name, kVoldPrefix, LO_NAME_SIZE);
Jeff Sharkey11c2d382017-09-11 10:32:01 -060091 if (ioctl(device_fd.get(), LOOP_SET_STATUS64, &li) == -1) {
92 PLOG(ERROR) << "Failed to LOOP_SET_STATUS64";
93 return -errno;
94 }
95
Jeff Sharkeyfa1c6772017-03-25 22:49:13 -060096 return 0;
97}
98
Paul Crowley14c8c072018-09-18 13:30:21 -070099int Loop::destroyByDevice(const char* loopDevice) {
San Mehata19b2502010-01-06 10:33:53 -0800100 int device_fd;
101
Jeff Sharkeyce6a9132015-04-08 21:07:21 -0700102 device_fd = open(loopDevice, O_RDONLY | O_CLOEXEC);
San Mehata19b2502010-01-06 10:33:53 -0800103 if (device_fd < 0) {
Jeff Sharkey3472e522017-10-06 18:02:53 -0600104 PLOG(ERROR) << "Failed to open " << loopDevice;
San Mehata19b2502010-01-06 10:33:53 -0800105 return -1;
106 }
107
108 if (ioctl(device_fd, LOOP_CLR_FD, 0) < 0) {
Jeff Sharkey3472e522017-10-06 18:02:53 -0600109 PLOG(ERROR) << "Failed to destroy " << loopDevice;
San Mehata19b2502010-01-06 10:33:53 -0800110 close(device_fd);
111 return -1;
112 }
113
114 close(device_fd);
115 return 0;
116}
117
Jeff Sharkey11c2d382017-09-11 10:32:01 -0600118int Loop::destroyAll() {
Jeff Sharkey67b8c492017-09-21 17:08:43 -0600119 ATRACE_NAME("Loop::destroyAll");
Jeff Sharkey11c2d382017-09-11 10:32:01 -0600120
Jeff Sharkey67b8c492017-09-21 17:08:43 -0600121 std::string root = "/dev/block/";
Jeff Sharkey5540b442018-02-24 18:09:21 -0700122 auto dirp = std::unique_ptr<DIR, int (*)(DIR*)>(opendir(root.c_str()), closedir);
123 if (!dirp) {
Jeff Sharkey67b8c492017-09-21 17:08:43 -0600124 PLOG(ERROR) << "Failed to opendir";
125 return -1;
126 }
127
128 // Poke through all devices looking for loops
Jeff Sharkey5540b442018-02-24 18:09:21 -0700129 struct dirent* de;
130 while ((de = readdir(dirp.get()))) {
Jeff Sharkey3472e522017-10-06 18:02:53 -0600131 auto test = std::string(de->d_name);
132 if (!android::base::StartsWith(test, "loop")) continue;
Jeff Sharkey67b8c492017-09-21 17:08:43 -0600133
134 auto path = root + de->d_name;
Jeff Sharkey11c2d382017-09-11 10:32:01 -0600135 unique_fd fd(open(path.c_str(), O_RDWR | O_CLOEXEC));
136 if (fd.get() == -1) {
137 if (errno != ENOENT) {
138 PLOG(WARNING) << "Failed to open " << path;
139 }
140 continue;
141 }
142
143 struct loop_info64 li;
144 if (ioctl(fd.get(), LOOP_GET_STATUS64, &li) < 0) {
145 PLOG(WARNING) << "Failed to LOOP_GET_STATUS64 " << path;
146 continue;
147 }
148
Paul Crowley14c8c072018-09-18 13:30:21 -0700149 auto id = std::string((char*)li.lo_crypt_name);
Jeff Sharkey3472e522017-10-06 18:02:53 -0600150 if (android::base::StartsWith(id, kVoldPrefix)) {
Jeff Sharkey11c2d382017-09-11 10:32:01 -0600151 LOG(DEBUG) << "Tearing down stale loop device at " << path << " named " << id;
152
153 if (ioctl(fd.get(), LOOP_CLR_FD, 0) < 0) {
154 PLOG(WARNING) << "Failed to LOOP_CLR_FD " << path;
155 }
156 } else {
Sudheer Shanka4b6ca4e2018-09-21 10:54:54 -0700157 LOG(DEBUG) << "Found unmanaged loop device at " << path << " named " << id;
Jeff Sharkey11c2d382017-09-11 10:32:01 -0600158 }
159 }
Jeff Sharkey67b8c492017-09-21 17:08:43 -0600160
Jeff Sharkey11c2d382017-09-11 10:32:01 -0600161 return 0;
San Mehata19b2502010-01-06 10:33:53 -0800162}
163
Paul Crowley14c8c072018-09-18 13:30:21 -0700164int Loop::createImageFile(const char* file, unsigned long numSectors) {
Jeff Sharkeyfd3dc3c2017-03-27 10:49:21 -0600165 unique_fd fd(open(file, O_CREAT | O_WRONLY | O_TRUNC | O_CLOEXEC, 0600));
Jeff Sharkeyfa1c6772017-03-25 22:49:13 -0600166 if (fd.get() == -1) {
167 PLOG(ERROR) << "Failed to create image " << file;
Jeff Sharkeyfd3dc3c2017-03-27 10:49:21 -0600168 return -errno;
San Mehata19b2502010-01-06 10:33:53 -0800169 }
Jeff Sharkeyfa1c6772017-03-25 22:49:13 -0600170 if (fallocate(fd.get(), 0, 0, numSectors * 512) == -1) {
171 PLOG(WARNING) << "Failed to fallocate; falling back to ftruncate";
172 if (ftruncate(fd, numSectors * 512) == -1) {
173 PLOG(ERROR) << "Failed to ftruncate";
Jeff Sharkeyfd3dc3c2017-03-27 10:49:21 -0600174 return -errno;
Jeff Sharkeyfa1c6772017-03-25 22:49:13 -0600175 }
176 }
Jeff Sharkeyfd3dc3c2017-03-27 10:49:21 -0600177 return 0;
San Mehata19b2502010-01-06 10:33:53 -0800178}
Kenny Root344ca102012-04-03 17:23:01 -0700179
Paul Crowley14c8c072018-09-18 13:30:21 -0700180int Loop::resizeImageFile(const char* file, unsigned long numSectors) {
Daniel Rosenbergfcd34a02014-05-22 11:23:56 -0700181 int fd;
182
Jeff Sharkeyce6a9132015-04-08 21:07:21 -0700183 if ((fd = open(file, O_RDWR | O_CLOEXEC)) < 0) {
Jeff Sharkey3472e522017-10-06 18:02:53 -0600184 PLOG(ERROR) << "Failed to open " << file;
Daniel Rosenbergfcd34a02014-05-22 11:23:56 -0700185 return -1;
186 }
187
Jeff Sharkey3472e522017-10-06 18:02:53 -0600188 LOG(DEBUG) << "Attempting to increase " << file << " to " << numSectors;
Daniel Rosenbergfcd34a02014-05-22 11:23:56 -0700189
190 if (fallocate(fd, 0, 0, numSectors * 512)) {
Jeff Sharkey43ed1232014-08-22 12:29:05 -0700191 if (errno == ENOSYS || errno == ENOTSUP) {
Jeff Sharkey3472e522017-10-06 18:02:53 -0600192 PLOG(WARNING) << "fallocate not found. Falling back to ftruncate.";
Daniel Rosenbergfcd34a02014-05-22 11:23:56 -0700193 if (ftruncate(fd, numSectors * 512) < 0) {
Jeff Sharkey3472e522017-10-06 18:02:53 -0600194 PLOG(ERROR) << "Failed to ftruncate";
Daniel Rosenbergfcd34a02014-05-22 11:23:56 -0700195 close(fd);
196 return -1;
197 }
198 } else {
Jeff Sharkey3472e522017-10-06 18:02:53 -0600199 PLOG(ERROR) << "Failed to fallocate";
Daniel Rosenbergfcd34a02014-05-22 11:23:56 -0700200 close(fd);
201 return -1;
202 }
203 }
204 close(fd);
205 return 0;
206}