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
17 #define ATRACE_TAG ATRACE_TAG_PACKAGE_MANAGER
18
19 #include <dirent.h>
20 #include <errno.h>
21 #include <fcntl.h>
22 #include <stdio.h>
23 #include <stdlib.h>
24 #include <string.h>
25 #include <unistd.h>
26
27 #include <sys/ioctl.h>
28 #include <sys/mount.h>
29 #include <sys/stat.h>
30 #include <sys/types.h>
31
32 #include <linux/kdev_t.h>
33
34 #include <chrono>
35
36 #include <android-base/logging.h>
37 #include <android-base/stringprintf.h>
38 #include <android-base/strings.h>
39 #include <android-base/unique_fd.h>
40 #include <fs_mgr/file_wait.h>
41 #include <utils/Trace.h>
42
43 #include "Loop.h"
44 #include "VoldUtil.h"
45 #include "sehandle.h"
46
47 using namespace std::literals;
48 using android::base::StringPrintf;
49 using android::base::unique_fd;
50
51 static const char* kVoldPrefix = "vold:";
52 static constexpr size_t kLoopDeviceRetryAttempts = 3u;
53
create(const std::string & target,std::string & out_device)54 int Loop::create(const std::string& target, std::string& out_device) {
55 unique_fd ctl_fd(open("/dev/loop-control", O_RDWR | O_CLOEXEC));
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
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 }
77 if (target_fd.get() == -1) {
78 PLOG(ERROR) << "Failed to open " << target;
79 return -errno;
80 }
81 if (!android::fs_mgr::WaitForFile(out_device, 2s)) {
82 LOG(ERROR) << "Failed to find " << out_device;
83 return -ENOENT;
84 }
85 unique_fd device_fd(open(out_device.c_str(), O_RDWR | O_CLOEXEC));
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
96 struct loop_info64 li;
97 memset(&li, 0, sizeof(li));
98 strlcpy((char*)li.lo_crypt_name, kVoldPrefix, LO_NAME_SIZE);
99 if (ioctl(device_fd.get(), LOOP_SET_STATUS64, &li) == -1) {
100 PLOG(ERROR) << "Failed to LOOP_SET_STATUS64";
101 return -errno;
102 }
103
104 return 0;
105 }
106
destroyByDevice(const char * loopDevice)107 int Loop::destroyByDevice(const char* loopDevice) {
108 int device_fd;
109
110 device_fd = open(loopDevice, O_RDONLY | O_CLOEXEC);
111 if (device_fd < 0) {
112 PLOG(ERROR) << "Failed to open " << loopDevice;
113 return -1;
114 }
115
116 if (ioctl(device_fd, LOOP_CLR_FD, 0) < 0) {
117 PLOG(ERROR) << "Failed to destroy " << loopDevice;
118 close(device_fd);
119 return -1;
120 }
121
122 close(device_fd);
123 return 0;
124 }
125
destroyAll()126 int Loop::destroyAll() {
127 ATRACE_NAME("Loop::destroyAll");
128
129 std::string root = "/dev/block/";
130 auto dirp = std::unique_ptr<DIR, int (*)(DIR*)>(opendir(root.c_str()), closedir);
131 if (!dirp) {
132 PLOG(ERROR) << "Failed to opendir";
133 return -1;
134 }
135
136 // Poke through all devices looking for loops
137 struct dirent* de;
138 while ((de = readdir(dirp.get()))) {
139 auto test = std::string(de->d_name);
140 if (!android::base::StartsWith(test, "loop")) continue;
141
142 auto path = root + de->d_name;
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) {
153 if (errno != ENXIO) {
154 PLOG(WARNING) << "Failed to LOOP_GET_STATUS64 " << path;
155 }
156 continue;
157 }
158
159 auto id = std::string((char*)li.lo_crypt_name);
160 if (android::base::StartsWith(id, kVoldPrefix)) {
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 {
167 LOG(DEBUG) << "Found unmanaged loop device at " << path << " named " << id;
168 }
169 }
170
171 return 0;
172 }
173
createImageFile(const char * file,unsigned long numSectors)174 int Loop::createImageFile(const char* file, unsigned long numSectors) {
175 unique_fd fd(open(file, O_CREAT | O_WRONLY | O_TRUNC | O_CLOEXEC, 0600));
176 if (fd.get() == -1) {
177 PLOG(ERROR) << "Failed to create image " << file;
178 return -errno;
179 }
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";
184 return -errno;
185 }
186 }
187 return 0;
188 }
189
resizeImageFile(const char * file,unsigned long numSectors)190 int Loop::resizeImageFile(const char* file, unsigned long numSectors) {
191 int fd;
192
193 if ((fd = open(file, O_RDWR | O_CLOEXEC)) < 0) {
194 PLOG(ERROR) << "Failed to open " << file;
195 return -1;
196 }
197
198 LOG(DEBUG) << "Attempting to increase " << file << " to " << numSectors;
199
200 if (fallocate(fd, 0, 0, numSectors * 512)) {
201 if (errno == ENOSYS || errno == ENOTSUP) {
202 PLOG(WARNING) << "fallocate not found. Falling back to ftruncate.";
203 if (ftruncate(fd, numSectors * 512) < 0) {
204 PLOG(ERROR) << "Failed to ftruncate";
205 close(fd);
206 return -1;
207 }
208 } else {
209 PLOG(ERROR) << "Failed to fallocate";
210 close(fd);
211 return -1;
212 }
213 }
214 close(fd);
215 return 0;
216 }
217