blob: 335ca13c8ed681b51e24916c7dc253aa09c2c2c8 [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
San Mehata19b2502010-01-06 10:33:53 -080019#include <stdio.h>
Olivier Bailly37dcda62010-11-16 10:41:53 -080020#include <stdlib.h>
Jeff Sharkey67b8c492017-09-21 17:08:43 -060021#include <dirent.h>
San Mehata19b2502010-01-06 10:33:53 -080022#include <fcntl.h>
23#include <unistd.h>
24#include <errno.h>
25#include <string.h>
26
Kenny Root344ca102012-04-03 17:23:01 -070027#include <sys/mount.h>
San Mehat8da6bcb2010-01-09 12:24:05 -080028#include <sys/types.h>
29#include <sys/stat.h>
Olivier Bailly37dcda62010-11-16 10:41:53 -080030#include <sys/ioctl.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>
Jeff Sharkey3472e522017-10-06 18:02:53 -060035#include <android-base/strings.h>
Jeff Sharkeyfa1c6772017-03-25 22:49:13 -060036#include <android-base/stringprintf.h>
37#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:";
48
Jeff Sharkeyfa1c6772017-03-25 22:49:13 -060049int Loop::create(const std::string& target, std::string& out_device) {
Jeff Sharkeyfd3dc3c2017-03-27 10:49:21 -060050 unique_fd ctl_fd(open("/dev/loop-control", O_RDWR | O_CLOEXEC));
Jeff Sharkeyfa1c6772017-03-25 22:49:13 -060051 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 Sharkeyfd3dc3c2017-03-27 10:49:21 -060064 unique_fd target_fd(open(target.c_str(), O_RDWR | O_CLOEXEC));
Jeff Sharkeyfa1c6772017-03-25 22:49:13 -060065 if (target_fd.get() == -1) {
66 PLOG(ERROR) << "Failed to open " << target;
67 return -errno;
68 }
Jeff Sharkeyfd3dc3c2017-03-27 10:49:21 -060069 unique_fd device_fd(open(out_device.c_str(), O_RDWR | O_CLOEXEC));
Jeff Sharkeyfa1c6772017-03-25 22:49:13 -060070 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 Sharkey11c2d382017-09-11 10:32:01 -060080 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 Sharkeyfa1c6772017-03-25 22:49:13 -060088 return 0;
89}
90
San Mehata19b2502010-01-06 10:33:53 -080091int Loop::destroyByDevice(const char *loopDevice) {
92 int device_fd;
93
Jeff Sharkeyce6a9132015-04-08 21:07:21 -070094 device_fd = open(loopDevice, O_RDONLY | O_CLOEXEC);
San Mehata19b2502010-01-06 10:33:53 -080095 if (device_fd < 0) {
Jeff Sharkey3472e522017-10-06 18:02:53 -060096 PLOG(ERROR) << "Failed to open " << loopDevice;
San Mehata19b2502010-01-06 10:33:53 -080097 return -1;
98 }
99
100 if (ioctl(device_fd, LOOP_CLR_FD, 0) < 0) {
Jeff Sharkey3472e522017-10-06 18:02:53 -0600101 PLOG(ERROR) << "Failed to destroy " << loopDevice;
San Mehata19b2502010-01-06 10:33:53 -0800102 close(device_fd);
103 return -1;
104 }
105
106 close(device_fd);
107 return 0;
108}
109
Jeff Sharkey11c2d382017-09-11 10:32:01 -0600110int Loop::destroyAll() {
Jeff Sharkey67b8c492017-09-21 17:08:43 -0600111 ATRACE_NAME("Loop::destroyAll");
Jeff Sharkey11c2d382017-09-11 10:32:01 -0600112
Jeff Sharkey67b8c492017-09-21 17:08:43 -0600113 std::string root = "/dev/block/";
Jeff Sharkey5540b442018-02-24 18:09:21 -0700114 auto dirp = std::unique_ptr<DIR, int (*)(DIR*)>(opendir(root.c_str()), closedir);
115 if (!dirp) {
Jeff Sharkey67b8c492017-09-21 17:08:43 -0600116 PLOG(ERROR) << "Failed to opendir";
117 return -1;
118 }
119
120 // Poke through all devices looking for loops
Jeff Sharkey5540b442018-02-24 18:09:21 -0700121 struct dirent* de;
122 while ((de = readdir(dirp.get()))) {
Jeff Sharkey3472e522017-10-06 18:02:53 -0600123 auto test = std::string(de->d_name);
124 if (!android::base::StartsWith(test, "loop")) continue;
Jeff Sharkey67b8c492017-09-21 17:08:43 -0600125
126 auto path = root + de->d_name;
Jeff Sharkey11c2d382017-09-11 10:32:01 -0600127 unique_fd fd(open(path.c_str(), O_RDWR | O_CLOEXEC));
128 if (fd.get() == -1) {
129 if (errno != ENOENT) {
130 PLOG(WARNING) << "Failed to open " << path;
131 }
132 continue;
133 }
134
135 struct loop_info64 li;
136 if (ioctl(fd.get(), LOOP_GET_STATUS64, &li) < 0) {
137 PLOG(WARNING) << "Failed to LOOP_GET_STATUS64 " << path;
138 continue;
139 }
140
Jeff Sharkey3472e522017-10-06 18:02:53 -0600141 auto id = std::string((char*) li.lo_crypt_name);
142 if (android::base::StartsWith(id, kVoldPrefix)) {
Jeff Sharkey11c2d382017-09-11 10:32:01 -0600143 LOG(DEBUG) << "Tearing down stale loop device at " << path << " named " << id;
144
145 if (ioctl(fd.get(), LOOP_CLR_FD, 0) < 0) {
146 PLOG(WARNING) << "Failed to LOOP_CLR_FD " << path;
147 }
148 } else {
149 LOG(VERBOSE) << "Found unmanaged loop device at " << path << " named " << id;
150 }
151 }
Jeff Sharkey67b8c492017-09-21 17:08:43 -0600152
Jeff Sharkey11c2d382017-09-11 10:32:01 -0600153 return 0;
San Mehata19b2502010-01-06 10:33:53 -0800154}
155
Mateusz Nowaka4f48d02015-08-03 18:06:39 +0200156int Loop::createImageFile(const char *file, unsigned long numSectors) {
Jeff Sharkeyfd3dc3c2017-03-27 10:49:21 -0600157 unique_fd fd(open(file, O_CREAT | O_WRONLY | O_TRUNC | O_CLOEXEC, 0600));
Jeff Sharkeyfa1c6772017-03-25 22:49:13 -0600158 if (fd.get() == -1) {
159 PLOG(ERROR) << "Failed to create image " << file;
Jeff Sharkeyfd3dc3c2017-03-27 10:49:21 -0600160 return -errno;
San Mehata19b2502010-01-06 10:33:53 -0800161 }
Jeff Sharkeyfa1c6772017-03-25 22:49:13 -0600162 if (fallocate(fd.get(), 0, 0, numSectors * 512) == -1) {
163 PLOG(WARNING) << "Failed to fallocate; falling back to ftruncate";
164 if (ftruncate(fd, numSectors * 512) == -1) {
165 PLOG(ERROR) << "Failed to ftruncate";
Jeff Sharkeyfd3dc3c2017-03-27 10:49:21 -0600166 return -errno;
Jeff Sharkeyfa1c6772017-03-25 22:49:13 -0600167 }
168 }
Jeff Sharkeyfd3dc3c2017-03-27 10:49:21 -0600169 return 0;
San Mehata19b2502010-01-06 10:33:53 -0800170}
Kenny Root344ca102012-04-03 17:23:01 -0700171
Mateusz Nowaka4f48d02015-08-03 18:06:39 +0200172int Loop::resizeImageFile(const char *file, unsigned long numSectors) {
Daniel Rosenbergfcd34a02014-05-22 11:23:56 -0700173 int fd;
174
Jeff Sharkeyce6a9132015-04-08 21:07:21 -0700175 if ((fd = open(file, O_RDWR | O_CLOEXEC)) < 0) {
Jeff Sharkey3472e522017-10-06 18:02:53 -0600176 PLOG(ERROR) << "Failed to open " << file;
Daniel Rosenbergfcd34a02014-05-22 11:23:56 -0700177 return -1;
178 }
179
Jeff Sharkey3472e522017-10-06 18:02:53 -0600180 LOG(DEBUG) << "Attempting to increase " << file << " to " << numSectors;
Daniel Rosenbergfcd34a02014-05-22 11:23:56 -0700181
182 if (fallocate(fd, 0, 0, numSectors * 512)) {
Jeff Sharkey43ed1232014-08-22 12:29:05 -0700183 if (errno == ENOSYS || errno == ENOTSUP) {
Jeff Sharkey3472e522017-10-06 18:02:53 -0600184 PLOG(WARNING) << "fallocate not found. Falling back to ftruncate.";
Daniel Rosenbergfcd34a02014-05-22 11:23:56 -0700185 if (ftruncate(fd, numSectors * 512) < 0) {
Jeff Sharkey3472e522017-10-06 18:02:53 -0600186 PLOG(ERROR) << "Failed to ftruncate";
Daniel Rosenbergfcd34a02014-05-22 11:23:56 -0700187 close(fd);
188 return -1;
189 }
190 } else {
Jeff Sharkey3472e522017-10-06 18:02:53 -0600191 PLOG(ERROR) << "Failed to fallocate";
Daniel Rosenbergfcd34a02014-05-22 11:23:56 -0700192 close(fd);
193 return -1;
194 }
195 }
196 close(fd);
197 return 0;
198}