blob: 1eb986541879e041b56880f527cda094b884347f [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
San Mehata19b2502010-01-06 10:33:53 -080034#define LOG_TAG "Vold"
35
36#include <cutils/log.h>
37
Jeff Sharkeyfa1c6772017-03-25 22:49:13 -060038#include <android-base/logging.h>
39#include <android-base/stringprintf.h>
40#include <android-base/unique_fd.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
Jeff Sharkeyfa1c6772017-03-25 22:49:13 -060047using android::base::StringPrintf;
48using android::base::unique_fd;
49
Jeff Sharkey11c2d382017-09-11 10:32:01 -060050static const char* kVoldPrefix = "vold:";
51
Jeff Sharkey11c2d382017-09-11 10:32:01 -060052int Loop::lookupActive(const char *id_raw, char *buffer, size_t len) {
53 auto id_string = StringPrintf("%s%s", kVoldPrefix, id_raw);
54 const char* id = id_string.c_str();
55
San Mehata19b2502010-01-06 10:33:53 -080056 int i;
57 int fd;
58 char filename[256];
59
60 memset(buffer, 0, len);
61
62 for (i = 0; i < LOOP_MAX; i++) {
Kenny Root508c0e12010-07-12 09:59:49 -070063 struct loop_info64 li;
San Mehata19b2502010-01-06 10:33:53 -080064 int rc;
65
George Burgess IV605d7ae2016-02-29 13:39:17 -080066 snprintf(filename, sizeof(filename), "/dev/block/loop%d", i);
San Mehata19b2502010-01-06 10:33:53 -080067
Jeff Sharkeyce6a9132015-04-08 21:07:21 -070068 if ((fd = open(filename, O_RDWR | O_CLOEXEC)) < 0) {
San Mehatb78a32c2010-01-10 13:02:12 -080069 if (errno != ENOENT) {
San Mehat97ac40e2010-03-24 10:24:19 -070070 SLOGE("Unable to open %s (%s)", filename, strerror(errno));
San Mehatd9a4e352010-03-12 13:32:47 -080071 } else {
72 continue;
San Mehatb78a32c2010-01-10 13:02:12 -080073 }
San Mehata19b2502010-01-06 10:33:53 -080074 return -1;
75 }
76
Kenny Root508c0e12010-07-12 09:59:49 -070077 rc = ioctl(fd, LOOP_GET_STATUS64, &li);
San Mehata19b2502010-01-06 10:33:53 -080078 if (rc < 0 && errno == ENXIO) {
tao.peia1038a92015-08-17 20:18:49 +080079 close(fd);
San Mehata19b2502010-01-06 10:33:53 -080080 continue;
San Mehata19b2502010-01-06 10:33:53 -080081 }
tao.peia1038a92015-08-17 20:18:49 +080082 close(fd);
San Mehata19b2502010-01-06 10:33:53 -080083
84 if (rc < 0) {
San Mehat97ac40e2010-03-24 10:24:19 -070085 SLOGE("Unable to get loop status for %s (%s)", filename,
San Mehata19b2502010-01-06 10:33:53 -080086 strerror(errno));
87 return -1;
88 }
Kenny Root508c0e12010-07-12 09:59:49 -070089 if (!strncmp((const char*) li.lo_crypt_name, id, LO_NAME_SIZE)) {
San Mehata19b2502010-01-06 10:33:53 -080090 break;
91 }
92 }
93
94 if (i == LOOP_MAX) {
95 errno = ENOENT;
96 return -1;
97 }
Henrik Baard21522662015-02-06 09:24:14 +010098 strlcpy(buffer, filename, len);
San Mehata19b2502010-01-06 10:33:53 -080099 return 0;
100}
101
Jeff Sharkey11c2d382017-09-11 10:32:01 -0600102int Loop::create(const char *id_raw, const char *loopFile, char *loopDeviceBuffer, size_t len) {
103 auto id_string = StringPrintf("%s%s", kVoldPrefix, id_raw);
104 const char* id = id_string.c_str();
105
San Mehata19b2502010-01-06 10:33:53 -0800106 int i;
107 int fd;
108 char filename[256];
109
San Mehata19b2502010-01-06 10:33:53 -0800110 for (i = 0; i < LOOP_MAX; i++) {
Kenny Root7c165022011-02-01 15:58:25 -0800111 struct loop_info64 li;
San Mehata19b2502010-01-06 10:33:53 -0800112 int rc;
Stephen Smalley684e6622014-09-30 10:29:24 -0400113 char *secontext = NULL;
San Mehata19b2502010-01-06 10:33:53 -0800114
George Burgess IV605d7ae2016-02-29 13:39:17 -0800115 snprintf(filename, sizeof(filename), "/dev/block/loop%d", i);
San Mehata19b2502010-01-06 10:33:53 -0800116
San Mehat8da6bcb2010-01-09 12:24:05 -0800117 /*
118 * The kernel starts us off with 8 loop nodes, but more
119 * are created on-demand if needed.
120 */
121 mode_t mode = 0660 | S_IFBLK;
San Mehatb78a32c2010-01-10 13:02:12 -0800122 unsigned int dev = (0xff & i) | ((i << 12) & 0xfff00000) | (7 << 8);
Stephen Smalley684e6622014-09-30 10:29:24 -0400123
124 if (sehandle) {
125 rc = selabel_lookup(sehandle, &secontext, filename, S_IFBLK);
126 if (rc == 0)
127 setfscreatecon(secontext);
128 }
129
San Mehat8da6bcb2010-01-09 12:24:05 -0800130 if (mknod(filename, mode, dev) < 0) {
131 if (errno != EEXIST) {
Stephen Smalley684e6622014-09-30 10:29:24 -0400132 int sverrno = errno;
San Mehat97ac40e2010-03-24 10:24:19 -0700133 SLOGE("Error creating loop device node (%s)", strerror(errno));
Stephen Smalley684e6622014-09-30 10:29:24 -0400134 if (secontext) {
135 freecon(secontext);
136 setfscreatecon(NULL);
137 }
138 errno = sverrno;
San Mehat8da6bcb2010-01-09 12:24:05 -0800139 return -1;
140 }
141 }
Stephen Smalley684e6622014-09-30 10:29:24 -0400142 if (secontext) {
143 freecon(secontext);
144 setfscreatecon(NULL);
145 }
San Mehat8da6bcb2010-01-09 12:24:05 -0800146
Jeff Sharkeyce6a9132015-04-08 21:07:21 -0700147 if ((fd = open(filename, O_RDWR | O_CLOEXEC)) < 0) {
San Mehat97ac40e2010-03-24 10:24:19 -0700148 SLOGE("Unable to open %s (%s)", filename, strerror(errno));
San Mehata19b2502010-01-06 10:33:53 -0800149 return -1;
150 }
151
Kenny Root7c165022011-02-01 15:58:25 -0800152 rc = ioctl(fd, LOOP_GET_STATUS64, &li);
San Mehata19b2502010-01-06 10:33:53 -0800153 if (rc < 0 && errno == ENXIO)
154 break;
155
San Mehat8da6bcb2010-01-09 12:24:05 -0800156 close(fd);
157
San Mehata19b2502010-01-06 10:33:53 -0800158 if (rc < 0) {
San Mehat97ac40e2010-03-24 10:24:19 -0700159 SLOGE("Unable to get loop status for %s (%s)", filename,
San Mehata19b2502010-01-06 10:33:53 -0800160 strerror(errno));
161 return -1;
162 }
163 }
164
165 if (i == LOOP_MAX) {
San Mehat97ac40e2010-03-24 10:24:19 -0700166 SLOGE("Exhausted all loop devices");
San Mehata19b2502010-01-06 10:33:53 -0800167 errno = ENOSPC;
168 return -1;
169 }
San Mehata19b2502010-01-06 10:33:53 -0800170
Henrik Baard21522662015-02-06 09:24:14 +0100171 strlcpy(loopDeviceBuffer, filename, len);
San Mehat8da6bcb2010-01-09 12:24:05 -0800172
San Mehata19b2502010-01-06 10:33:53 -0800173 int file_fd;
174
Jeff Sharkeyce6a9132015-04-08 21:07:21 -0700175 if ((file_fd = open(loopFile, O_RDWR | O_CLOEXEC)) < 0) {
San Mehat97ac40e2010-03-24 10:24:19 -0700176 SLOGE("Unable to open %s (%s)", loopFile, strerror(errno));
San Mehata19b2502010-01-06 10:33:53 -0800177 close(fd);
178 return -1;
179 }
180
181 if (ioctl(fd, LOOP_SET_FD, file_fd) < 0) {
San Mehat97ac40e2010-03-24 10:24:19 -0700182 SLOGE("Error setting up loopback interface (%s)", strerror(errno));
San Mehata19b2502010-01-06 10:33:53 -0800183 close(file_fd);
184 close(fd);
185 return -1;
186 }
187
Kenny Root508c0e12010-07-12 09:59:49 -0700188 struct loop_info64 li;
San Mehata19b2502010-01-06 10:33:53 -0800189
190 memset(&li, 0, sizeof(li));
Peter Bohm092aa1c2011-04-01 12:35:25 +0200191 strlcpy((char*) li.lo_crypt_name, id, LO_NAME_SIZE);
192 strlcpy((char*) li.lo_file_name, loopFile, LO_NAME_SIZE);
San Mehata19b2502010-01-06 10:33:53 -0800193
Kenny Root508c0e12010-07-12 09:59:49 -0700194 if (ioctl(fd, LOOP_SET_STATUS64, &li) < 0) {
San Mehat97ac40e2010-03-24 10:24:19 -0700195 SLOGE("Error setting loopback status (%s)", strerror(errno));
San Mehata19b2502010-01-06 10:33:53 -0800196 close(file_fd);
197 close(fd);
198 return -1;
199 }
200
201 close(fd);
202 close(file_fd);
203
204 return 0;
205}
206
Jeff Sharkeyfa1c6772017-03-25 22:49:13 -0600207int Loop::create(const std::string& target, std::string& out_device) {
Jeff Sharkeyfd3dc3c2017-03-27 10:49:21 -0600208 unique_fd ctl_fd(open("/dev/loop-control", O_RDWR | O_CLOEXEC));
Jeff Sharkeyfa1c6772017-03-25 22:49:13 -0600209 if (ctl_fd.get() == -1) {
210 PLOG(ERROR) << "Failed to open loop-control";
211 return -errno;
212 }
213
214 int num = ioctl(ctl_fd.get(), LOOP_CTL_GET_FREE);
215 if (num == -1) {
216 PLOG(ERROR) << "Failed LOOP_CTL_GET_FREE";
217 return -errno;
218 }
219
220 out_device = StringPrintf("/dev/block/loop%d", num);
221
Jeff Sharkeyfd3dc3c2017-03-27 10:49:21 -0600222 unique_fd target_fd(open(target.c_str(), O_RDWR | O_CLOEXEC));
Jeff Sharkeyfa1c6772017-03-25 22:49:13 -0600223 if (target_fd.get() == -1) {
224 PLOG(ERROR) << "Failed to open " << target;
225 return -errno;
226 }
Jeff Sharkeyfd3dc3c2017-03-27 10:49:21 -0600227 unique_fd device_fd(open(out_device.c_str(), O_RDWR | O_CLOEXEC));
Jeff Sharkeyfa1c6772017-03-25 22:49:13 -0600228 if (device_fd.get() == -1) {
229 PLOG(ERROR) << "Failed to open " << out_device;
230 return -errno;
231 }
232
233 if (ioctl(device_fd.get(), LOOP_SET_FD, target_fd.get()) == -1) {
234 PLOG(ERROR) << "Failed to LOOP_SET_FD";
235 return -errno;
236 }
237
Jeff Sharkey11c2d382017-09-11 10:32:01 -0600238 struct loop_info64 li;
239 memset(&li, 0, sizeof(li));
240 strlcpy((char*) li.lo_crypt_name, kVoldPrefix, LO_NAME_SIZE);
241 if (ioctl(device_fd.get(), LOOP_SET_STATUS64, &li) == -1) {
242 PLOG(ERROR) << "Failed to LOOP_SET_STATUS64";
243 return -errno;
244 }
245
Jeff Sharkeyfa1c6772017-03-25 22:49:13 -0600246 return 0;
247}
248
San Mehata19b2502010-01-06 10:33:53 -0800249int Loop::destroyByDevice(const char *loopDevice) {
250 int device_fd;
251
Jeff Sharkeyce6a9132015-04-08 21:07:21 -0700252 device_fd = open(loopDevice, O_RDONLY | O_CLOEXEC);
San Mehata19b2502010-01-06 10:33:53 -0800253 if (device_fd < 0) {
San Mehat97ac40e2010-03-24 10:24:19 -0700254 SLOGE("Failed to open loop (%d)", errno);
San Mehata19b2502010-01-06 10:33:53 -0800255 return -1;
256 }
257
258 if (ioctl(device_fd, LOOP_CLR_FD, 0) < 0) {
San Mehat97ac40e2010-03-24 10:24:19 -0700259 SLOGE("Failed to destroy loop (%d)", errno);
San Mehata19b2502010-01-06 10:33:53 -0800260 close(device_fd);
261 return -1;
262 }
263
264 close(device_fd);
265 return 0;
266}
267
Jeff Sharkey11c2d382017-09-11 10:32:01 -0600268int Loop::destroyAll() {
Jeff Sharkey67b8c492017-09-21 17:08:43 -0600269 ATRACE_NAME("Loop::destroyAll");
Jeff Sharkey11c2d382017-09-11 10:32:01 -0600270
Jeff Sharkey67b8c492017-09-21 17:08:43 -0600271 DIR* dir;
272 struct dirent* de;
273
274 std::string root = "/dev/block/";
275 if (!(dir = opendir(root.c_str()))) {
276 PLOG(ERROR) << "Failed to opendir";
277 return -1;
278 }
279
280 // Poke through all devices looking for loops
281 while ((de = readdir(dir))) {
282 if (strncmp(de->d_name, "loop", 4) != 0) continue;
283
284 auto path = root + de->d_name;
Jeff Sharkey11c2d382017-09-11 10:32:01 -0600285 unique_fd fd(open(path.c_str(), O_RDWR | O_CLOEXEC));
286 if (fd.get() == -1) {
287 if (errno != ENOENT) {
288 PLOG(WARNING) << "Failed to open " << path;
289 }
290 continue;
291 }
292
293 struct loop_info64 li;
294 if (ioctl(fd.get(), LOOP_GET_STATUS64, &li) < 0) {
295 PLOG(WARNING) << "Failed to LOOP_GET_STATUS64 " << path;
296 continue;
297 }
298
299 char* id = (char*) li.lo_crypt_name;
300 if (strncmp(id, kVoldPrefix, strlen(kVoldPrefix)) == 0) {
301 LOG(DEBUG) << "Tearing down stale loop device at " << path << " named " << id;
302
303 if (ioctl(fd.get(), LOOP_CLR_FD, 0) < 0) {
304 PLOG(WARNING) << "Failed to LOOP_CLR_FD " << path;
305 }
306 } else {
307 LOG(VERBOSE) << "Found unmanaged loop device at " << path << " named " << id;
308 }
309 }
Jeff Sharkey67b8c492017-09-21 17:08:43 -0600310
311 closedir(dir);
Jeff Sharkey11c2d382017-09-11 10:32:01 -0600312 return 0;
San Mehata19b2502010-01-06 10:33:53 -0800313}
314
Mateusz Nowaka4f48d02015-08-03 18:06:39 +0200315int Loop::createImageFile(const char *file, unsigned long numSectors) {
Jeff Sharkeyfd3dc3c2017-03-27 10:49:21 -0600316 unique_fd fd(open(file, O_CREAT | O_WRONLY | O_TRUNC | O_CLOEXEC, 0600));
Jeff Sharkeyfa1c6772017-03-25 22:49:13 -0600317 if (fd.get() == -1) {
318 PLOG(ERROR) << "Failed to create image " << file;
Jeff Sharkeyfd3dc3c2017-03-27 10:49:21 -0600319 return -errno;
San Mehata19b2502010-01-06 10:33:53 -0800320 }
Jeff Sharkeyfa1c6772017-03-25 22:49:13 -0600321 if (fallocate(fd.get(), 0, 0, numSectors * 512) == -1) {
322 PLOG(WARNING) << "Failed to fallocate; falling back to ftruncate";
323 if (ftruncate(fd, numSectors * 512) == -1) {
324 PLOG(ERROR) << "Failed to ftruncate";
Jeff Sharkeyfd3dc3c2017-03-27 10:49:21 -0600325 return -errno;
Jeff Sharkeyfa1c6772017-03-25 22:49:13 -0600326 }
327 }
Jeff Sharkeyfd3dc3c2017-03-27 10:49:21 -0600328 return 0;
San Mehata19b2502010-01-06 10:33:53 -0800329}
Kenny Root344ca102012-04-03 17:23:01 -0700330
Mateusz Nowaka4f48d02015-08-03 18:06:39 +0200331int Loop::resizeImageFile(const char *file, unsigned long numSectors) {
Daniel Rosenbergfcd34a02014-05-22 11:23:56 -0700332 int fd;
333
Jeff Sharkeyce6a9132015-04-08 21:07:21 -0700334 if ((fd = open(file, O_RDWR | O_CLOEXEC)) < 0) {
Daniel Rosenbergfcd34a02014-05-22 11:23:56 -0700335 SLOGE("Error opening imagefile (%s)", strerror(errno));
336 return -1;
337 }
338
Mateusz Nowaka4f48d02015-08-03 18:06:39 +0200339 SLOGD("Attempting to increase size of %s to %lu sectors.", file, numSectors);
Daniel Rosenbergfcd34a02014-05-22 11:23:56 -0700340
341 if (fallocate(fd, 0, 0, numSectors * 512)) {
Jeff Sharkey43ed1232014-08-22 12:29:05 -0700342 if (errno == ENOSYS || errno == ENOTSUP) {
Daniel Rosenbergfcd34a02014-05-22 11:23:56 -0700343 SLOGW("fallocate not found. Falling back to ftruncate.");
344 if (ftruncate(fd, numSectors * 512) < 0) {
345 SLOGE("Error truncating imagefile (%s)", strerror(errno));
346 close(fd);
347 return -1;
348 }
349 } else {
350 SLOGE("Error allocating space (%s)", strerror(errno));
351 close(fd);
352 return -1;
353 }
354 }
355 close(fd);
356 return 0;
357}