blob: 9fa876ce0df7c5986636e088344d583ad4066571 [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
David Anderson1dd5c4f2019-06-11 14:51:25 -070034#include <chrono>
35
Jeff Sharkeyfa1c6772017-03-25 22:49:13 -060036#include <android-base/logging.h>
37#include <android-base/stringprintf.h>
Paul Crowley14c8c072018-09-18 13:30:21 -070038#include <android-base/strings.h>
Jeff Sharkeyfa1c6772017-03-25 22:49:13 -060039#include <android-base/unique_fd.h>
David Anderson1dd5c4f2019-06-11 14:51:25 -070040#include <fs_mgr/file_wait.h>
Jeff Sharkey67b8c492017-09-21 17:08:43 -060041#include <utils/Trace.h>
Jeff Sharkeyfa1c6772017-03-25 22:49:13 -060042
San Mehata19b2502010-01-06 10:33:53 -080043#include "Loop.h"
Hiroaki Miyazawa14eab552015-02-04 13:29:15 +090044#include "VoldUtil.h"
Stephen Smalley684e6622014-09-30 10:29:24 -040045#include "sehandle.h"
San Mehata19b2502010-01-06 10:33:53 -080046
David Anderson1dd5c4f2019-06-11 14:51:25 -070047using namespace std::literals;
Jeff Sharkeyfa1c6772017-03-25 22:49:13 -060048using android::base::StringPrintf;
49using android::base::unique_fd;
50
Jeff Sharkey11c2d382017-09-11 10:32:01 -060051static const char* kVoldPrefix = "vold:";
Daniel Rosenberg4538cb22019-04-01 16:09:28 -070052static constexpr size_t kLoopDeviceRetryAttempts = 3u;
Jeff Sharkey11c2d382017-09-11 10:32:01 -060053
Jeff Sharkeyfa1c6772017-03-25 22:49:13 -060054int Loop::create(const std::string& target, std::string& out_device) {
Jeff Sharkeyfd3dc3c2017-03-27 10:49:21 -060055 unique_fd ctl_fd(open("/dev/loop-control", O_RDWR | O_CLOEXEC));
Jeff Sharkeyfa1c6772017-03-25 22:49:13 -060056 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 Rosenberg4538cb22019-04-01 16:09:28 -070069 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 Sharkeyfa1c6772017-03-25 22:49:13 -060077 if (target_fd.get() == -1) {
78 PLOG(ERROR) << "Failed to open " << target;
79 return -errno;
80 }
David Anderson1dd5c4f2019-06-11 14:51:25 -070081 if (!android::fs_mgr::WaitForFile(out_device, 2s)) {
82 LOG(ERROR) << "Failed to find " << out_device;
83 return -ENOENT;
84 }
Jeff Sharkeyfd3dc3c2017-03-27 10:49:21 -060085 unique_fd device_fd(open(out_device.c_str(), O_RDWR | O_CLOEXEC));
Jeff Sharkeyfa1c6772017-03-25 22:49:13 -060086 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 Sharkey11c2d382017-09-11 10:32:01 -060096 struct loop_info64 li;
97 memset(&li, 0, sizeof(li));
Paul Crowley14c8c072018-09-18 13:30:21 -070098 strlcpy((char*)li.lo_crypt_name, kVoldPrefix, LO_NAME_SIZE);
Jeff Sharkey11c2d382017-09-11 10:32:01 -060099 if (ioctl(device_fd.get(), LOOP_SET_STATUS64, &li) == -1) {
100 PLOG(ERROR) << "Failed to LOOP_SET_STATUS64";
101 return -errno;
102 }
103
Jeff Sharkeyfa1c6772017-03-25 22:49:13 -0600104 return 0;
105}
106
Paul Crowley14c8c072018-09-18 13:30:21 -0700107int Loop::destroyByDevice(const char* loopDevice) {
San Mehata19b2502010-01-06 10:33:53 -0800108 int device_fd;
109
Jeff Sharkeyce6a9132015-04-08 21:07:21 -0700110 device_fd = open(loopDevice, O_RDONLY | O_CLOEXEC);
San Mehata19b2502010-01-06 10:33:53 -0800111 if (device_fd < 0) {
Jeff Sharkey3472e522017-10-06 18:02:53 -0600112 PLOG(ERROR) << "Failed to open " << loopDevice;
San Mehata19b2502010-01-06 10:33:53 -0800113 return -1;
114 }
115
116 if (ioctl(device_fd, LOOP_CLR_FD, 0) < 0) {
Jeff Sharkey3472e522017-10-06 18:02:53 -0600117 PLOG(ERROR) << "Failed to destroy " << loopDevice;
San Mehata19b2502010-01-06 10:33:53 -0800118 close(device_fd);
119 return -1;
120 }
121
122 close(device_fd);
123 return 0;
124}
125
Jeff Sharkey11c2d382017-09-11 10:32:01 -0600126int Loop::destroyAll() {
Jeff Sharkey67b8c492017-09-21 17:08:43 -0600127 ATRACE_NAME("Loop::destroyAll");
Jeff Sharkey11c2d382017-09-11 10:32:01 -0600128
Jeff Sharkey67b8c492017-09-21 17:08:43 -0600129 std::string root = "/dev/block/";
Jeff Sharkey5540b442018-02-24 18:09:21 -0700130 auto dirp = std::unique_ptr<DIR, int (*)(DIR*)>(opendir(root.c_str()), closedir);
131 if (!dirp) {
Jeff Sharkey67b8c492017-09-21 17:08:43 -0600132 PLOG(ERROR) << "Failed to opendir";
133 return -1;
134 }
135
136 // Poke through all devices looking for loops
Jeff Sharkey5540b442018-02-24 18:09:21 -0700137 struct dirent* de;
138 while ((de = readdir(dirp.get()))) {
Jeff Sharkey3472e522017-10-06 18:02:53 -0600139 auto test = std::string(de->d_name);
140 if (!android::base::StartsWith(test, "loop")) continue;
Jeff Sharkey67b8c492017-09-21 17:08:43 -0600141
142 auto path = root + de->d_name;
Jeff Sharkey11c2d382017-09-11 10:32:01 -0600143 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) {
153 PLOG(WARNING) << "Failed to LOOP_GET_STATUS64 " << path;
154 continue;
155 }
156
Paul Crowley14c8c072018-09-18 13:30:21 -0700157 auto id = std::string((char*)li.lo_crypt_name);
Jeff Sharkey3472e522017-10-06 18:02:53 -0600158 if (android::base::StartsWith(id, kVoldPrefix)) {
Jeff Sharkey11c2d382017-09-11 10:32:01 -0600159 LOG(DEBUG) << "Tearing down stale loop device at " << path << " named " << id;
160
161 if (ioctl(fd.get(), LOOP_CLR_FD, 0) < 0) {
162 PLOG(WARNING) << "Failed to LOOP_CLR_FD " << path;
163 }
164 } else {
Sudheer Shanka4b6ca4e2018-09-21 10:54:54 -0700165 LOG(DEBUG) << "Found unmanaged loop device at " << path << " named " << id;
Jeff Sharkey11c2d382017-09-11 10:32:01 -0600166 }
167 }
Jeff Sharkey67b8c492017-09-21 17:08:43 -0600168
Jeff Sharkey11c2d382017-09-11 10:32:01 -0600169 return 0;
San Mehata19b2502010-01-06 10:33:53 -0800170}
171
Paul Crowley14c8c072018-09-18 13:30:21 -0700172int Loop::createImageFile(const char* file, unsigned long numSectors) {
Jeff Sharkeyfd3dc3c2017-03-27 10:49:21 -0600173 unique_fd fd(open(file, O_CREAT | O_WRONLY | O_TRUNC | O_CLOEXEC, 0600));
Jeff Sharkeyfa1c6772017-03-25 22:49:13 -0600174 if (fd.get() == -1) {
175 PLOG(ERROR) << "Failed to create image " << file;
Jeff Sharkeyfd3dc3c2017-03-27 10:49:21 -0600176 return -errno;
San Mehata19b2502010-01-06 10:33:53 -0800177 }
Jeff Sharkeyfa1c6772017-03-25 22:49:13 -0600178 if (fallocate(fd.get(), 0, 0, numSectors * 512) == -1) {
179 PLOG(WARNING) << "Failed to fallocate; falling back to ftruncate";
180 if (ftruncate(fd, numSectors * 512) == -1) {
181 PLOG(ERROR) << "Failed to ftruncate";
Jeff Sharkeyfd3dc3c2017-03-27 10:49:21 -0600182 return -errno;
Jeff Sharkeyfa1c6772017-03-25 22:49:13 -0600183 }
184 }
Jeff Sharkeyfd3dc3c2017-03-27 10:49:21 -0600185 return 0;
San Mehata19b2502010-01-06 10:33:53 -0800186}
Kenny Root344ca102012-04-03 17:23:01 -0700187
Paul Crowley14c8c072018-09-18 13:30:21 -0700188int Loop::resizeImageFile(const char* file, unsigned long numSectors) {
Daniel Rosenbergfcd34a02014-05-22 11:23:56 -0700189 int fd;
190
Jeff Sharkeyce6a9132015-04-08 21:07:21 -0700191 if ((fd = open(file, O_RDWR | O_CLOEXEC)) < 0) {
Jeff Sharkey3472e522017-10-06 18:02:53 -0600192 PLOG(ERROR) << "Failed to open " << file;
Daniel Rosenbergfcd34a02014-05-22 11:23:56 -0700193 return -1;
194 }
195
Jeff Sharkey3472e522017-10-06 18:02:53 -0600196 LOG(DEBUG) << "Attempting to increase " << file << " to " << numSectors;
Daniel Rosenbergfcd34a02014-05-22 11:23:56 -0700197
198 if (fallocate(fd, 0, 0, numSectors * 512)) {
Jeff Sharkey43ed1232014-08-22 12:29:05 -0700199 if (errno == ENOSYS || errno == ENOTSUP) {
Jeff Sharkey3472e522017-10-06 18:02:53 -0600200 PLOG(WARNING) << "fallocate not found. Falling back to ftruncate.";
Daniel Rosenbergfcd34a02014-05-22 11:23:56 -0700201 if (ftruncate(fd, numSectors * 512) < 0) {
Jeff Sharkey3472e522017-10-06 18:02:53 -0600202 PLOG(ERROR) << "Failed to ftruncate";
Daniel Rosenbergfcd34a02014-05-22 11:23:56 -0700203 close(fd);
204 return -1;
205 }
206 } else {
Jeff Sharkey3472e522017-10-06 18:02:53 -0600207 PLOG(ERROR) << "Failed to fallocate";
Daniel Rosenbergfcd34a02014-05-22 11:23:56 -0700208 close(fd);
209 return -1;
210 }
211 }
212 close(fd);
213 return 0;
214}