blob: fc3909b5d2d362b111f77665b76cbbf9c26ff259 [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
17#include <stdio.h>
Olivier Bailly37dcda62010-11-16 10:41:53 -080018#include <stdlib.h>
San Mehata19b2502010-01-06 10:33:53 -080019#include <fcntl.h>
20#include <unistd.h>
21#include <errno.h>
22#include <string.h>
23
Kenny Root344ca102012-04-03 17:23:01 -070024#include <sys/mount.h>
San Mehat8da6bcb2010-01-09 12:24:05 -080025#include <sys/types.h>
26#include <sys/stat.h>
Olivier Bailly37dcda62010-11-16 10:41:53 -080027#include <sys/ioctl.h>
San Mehat8da6bcb2010-01-09 12:24:05 -080028
San Mehatd9a4e352010-03-12 13:32:47 -080029#include <linux/kdev_t.h>
30
San Mehata19b2502010-01-06 10:33:53 -080031#define LOG_TAG "Vold"
32
33#include <cutils/log.h>
34
Jeff Sharkeyfa1c6772017-03-25 22:49:13 -060035#include <android-base/logging.h>
36#include <android-base/stringprintf.h>
37#include <android-base/unique_fd.h>
38
San Mehata19b2502010-01-06 10:33:53 -080039#include "Loop.h"
Hiroaki Miyazawa14eab552015-02-04 13:29:15 +090040#include "VoldUtil.h"
Stephen Smalley684e6622014-09-30 10:29:24 -040041#include "sehandle.h"
San Mehata19b2502010-01-06 10:33:53 -080042
Jeff Sharkeyfa1c6772017-03-25 22:49:13 -060043using android::base::StringPrintf;
44using android::base::unique_fd;
45
Jeff Sharkey11c2d382017-09-11 10:32:01 -060046static const char* kVoldPrefix = "vold:";
47
Jeff Sharkey11c2d382017-09-11 10:32:01 -060048int Loop::lookupActive(const char *id_raw, char *buffer, size_t len) {
49 auto id_string = StringPrintf("%s%s", kVoldPrefix, id_raw);
50 const char* id = id_string.c_str();
51
San Mehata19b2502010-01-06 10:33:53 -080052 int i;
53 int fd;
54 char filename[256];
55
56 memset(buffer, 0, len);
57
58 for (i = 0; i < LOOP_MAX; i++) {
Kenny Root508c0e12010-07-12 09:59:49 -070059 struct loop_info64 li;
San Mehata19b2502010-01-06 10:33:53 -080060 int rc;
61
George Burgess IV605d7ae2016-02-29 13:39:17 -080062 snprintf(filename, sizeof(filename), "/dev/block/loop%d", i);
San Mehata19b2502010-01-06 10:33:53 -080063
Jeff Sharkeyce6a9132015-04-08 21:07:21 -070064 if ((fd = open(filename, O_RDWR | O_CLOEXEC)) < 0) {
San Mehatb78a32c2010-01-10 13:02:12 -080065 if (errno != ENOENT) {
San Mehat97ac40e2010-03-24 10:24:19 -070066 SLOGE("Unable to open %s (%s)", filename, strerror(errno));
San Mehatd9a4e352010-03-12 13:32:47 -080067 } else {
68 continue;
San Mehatb78a32c2010-01-10 13:02:12 -080069 }
San Mehata19b2502010-01-06 10:33:53 -080070 return -1;
71 }
72
Kenny Root508c0e12010-07-12 09:59:49 -070073 rc = ioctl(fd, LOOP_GET_STATUS64, &li);
San Mehata19b2502010-01-06 10:33:53 -080074 if (rc < 0 && errno == ENXIO) {
tao.peia1038a92015-08-17 20:18:49 +080075 close(fd);
San Mehata19b2502010-01-06 10:33:53 -080076 continue;
San Mehata19b2502010-01-06 10:33:53 -080077 }
tao.peia1038a92015-08-17 20:18:49 +080078 close(fd);
San Mehata19b2502010-01-06 10:33:53 -080079
80 if (rc < 0) {
San Mehat97ac40e2010-03-24 10:24:19 -070081 SLOGE("Unable to get loop status for %s (%s)", filename,
San Mehata19b2502010-01-06 10:33:53 -080082 strerror(errno));
83 return -1;
84 }
Kenny Root508c0e12010-07-12 09:59:49 -070085 if (!strncmp((const char*) li.lo_crypt_name, id, LO_NAME_SIZE)) {
San Mehata19b2502010-01-06 10:33:53 -080086 break;
87 }
88 }
89
90 if (i == LOOP_MAX) {
91 errno = ENOENT;
92 return -1;
93 }
Henrik Baard21522662015-02-06 09:24:14 +010094 strlcpy(buffer, filename, len);
San Mehata19b2502010-01-06 10:33:53 -080095 return 0;
96}
97
Jeff Sharkey11c2d382017-09-11 10:32:01 -060098int Loop::create(const char *id_raw, const char *loopFile, char *loopDeviceBuffer, size_t len) {
99 auto id_string = StringPrintf("%s%s", kVoldPrefix, id_raw);
100 const char* id = id_string.c_str();
101
San Mehata19b2502010-01-06 10:33:53 -0800102 int i;
103 int fd;
104 char filename[256];
105
San Mehata19b2502010-01-06 10:33:53 -0800106 for (i = 0; i < LOOP_MAX; i++) {
Kenny Root7c165022011-02-01 15:58:25 -0800107 struct loop_info64 li;
San Mehata19b2502010-01-06 10:33:53 -0800108 int rc;
Stephen Smalley684e6622014-09-30 10:29:24 -0400109 char *secontext = NULL;
San Mehata19b2502010-01-06 10:33:53 -0800110
George Burgess IV605d7ae2016-02-29 13:39:17 -0800111 snprintf(filename, sizeof(filename), "/dev/block/loop%d", i);
San Mehata19b2502010-01-06 10:33:53 -0800112
San Mehat8da6bcb2010-01-09 12:24:05 -0800113 /*
114 * The kernel starts us off with 8 loop nodes, but more
115 * are created on-demand if needed.
116 */
117 mode_t mode = 0660 | S_IFBLK;
San Mehatb78a32c2010-01-10 13:02:12 -0800118 unsigned int dev = (0xff & i) | ((i << 12) & 0xfff00000) | (7 << 8);
Stephen Smalley684e6622014-09-30 10:29:24 -0400119
120 if (sehandle) {
121 rc = selabel_lookup(sehandle, &secontext, filename, S_IFBLK);
122 if (rc == 0)
123 setfscreatecon(secontext);
124 }
125
San Mehat8da6bcb2010-01-09 12:24:05 -0800126 if (mknod(filename, mode, dev) < 0) {
127 if (errno != EEXIST) {
Stephen Smalley684e6622014-09-30 10:29:24 -0400128 int sverrno = errno;
San Mehat97ac40e2010-03-24 10:24:19 -0700129 SLOGE("Error creating loop device node (%s)", strerror(errno));
Stephen Smalley684e6622014-09-30 10:29:24 -0400130 if (secontext) {
131 freecon(secontext);
132 setfscreatecon(NULL);
133 }
134 errno = sverrno;
San Mehat8da6bcb2010-01-09 12:24:05 -0800135 return -1;
136 }
137 }
Stephen Smalley684e6622014-09-30 10:29:24 -0400138 if (secontext) {
139 freecon(secontext);
140 setfscreatecon(NULL);
141 }
San Mehat8da6bcb2010-01-09 12:24:05 -0800142
Jeff Sharkeyce6a9132015-04-08 21:07:21 -0700143 if ((fd = open(filename, O_RDWR | O_CLOEXEC)) < 0) {
San Mehat97ac40e2010-03-24 10:24:19 -0700144 SLOGE("Unable to open %s (%s)", filename, strerror(errno));
San Mehata19b2502010-01-06 10:33:53 -0800145 return -1;
146 }
147
Kenny Root7c165022011-02-01 15:58:25 -0800148 rc = ioctl(fd, LOOP_GET_STATUS64, &li);
San Mehata19b2502010-01-06 10:33:53 -0800149 if (rc < 0 && errno == ENXIO)
150 break;
151
San Mehat8da6bcb2010-01-09 12:24:05 -0800152 close(fd);
153
San Mehata19b2502010-01-06 10:33:53 -0800154 if (rc < 0) {
San Mehat97ac40e2010-03-24 10:24:19 -0700155 SLOGE("Unable to get loop status for %s (%s)", filename,
San Mehata19b2502010-01-06 10:33:53 -0800156 strerror(errno));
157 return -1;
158 }
159 }
160
161 if (i == LOOP_MAX) {
San Mehat97ac40e2010-03-24 10:24:19 -0700162 SLOGE("Exhausted all loop devices");
San Mehata19b2502010-01-06 10:33:53 -0800163 errno = ENOSPC;
164 return -1;
165 }
San Mehata19b2502010-01-06 10:33:53 -0800166
Henrik Baard21522662015-02-06 09:24:14 +0100167 strlcpy(loopDeviceBuffer, filename, len);
San Mehat8da6bcb2010-01-09 12:24:05 -0800168
San Mehata19b2502010-01-06 10:33:53 -0800169 int file_fd;
170
Jeff Sharkeyce6a9132015-04-08 21:07:21 -0700171 if ((file_fd = open(loopFile, O_RDWR | O_CLOEXEC)) < 0) {
San Mehat97ac40e2010-03-24 10:24:19 -0700172 SLOGE("Unable to open %s (%s)", loopFile, strerror(errno));
San Mehata19b2502010-01-06 10:33:53 -0800173 close(fd);
174 return -1;
175 }
176
177 if (ioctl(fd, LOOP_SET_FD, file_fd) < 0) {
San Mehat97ac40e2010-03-24 10:24:19 -0700178 SLOGE("Error setting up loopback interface (%s)", strerror(errno));
San Mehata19b2502010-01-06 10:33:53 -0800179 close(file_fd);
180 close(fd);
181 return -1;
182 }
183
Kenny Root508c0e12010-07-12 09:59:49 -0700184 struct loop_info64 li;
San Mehata19b2502010-01-06 10:33:53 -0800185
186 memset(&li, 0, sizeof(li));
Peter Bohm092aa1c2011-04-01 12:35:25 +0200187 strlcpy((char*) li.lo_crypt_name, id, LO_NAME_SIZE);
188 strlcpy((char*) li.lo_file_name, loopFile, LO_NAME_SIZE);
San Mehata19b2502010-01-06 10:33:53 -0800189
Kenny Root508c0e12010-07-12 09:59:49 -0700190 if (ioctl(fd, LOOP_SET_STATUS64, &li) < 0) {
San Mehat97ac40e2010-03-24 10:24:19 -0700191 SLOGE("Error setting loopback status (%s)", strerror(errno));
San Mehata19b2502010-01-06 10:33:53 -0800192 close(file_fd);
193 close(fd);
194 return -1;
195 }
196
197 close(fd);
198 close(file_fd);
199
200 return 0;
201}
202
Jeff Sharkeyfa1c6772017-03-25 22:49:13 -0600203int Loop::create(const std::string& target, std::string& out_device) {
Jeff Sharkeyfd3dc3c2017-03-27 10:49:21 -0600204 unique_fd ctl_fd(open("/dev/loop-control", O_RDWR | O_CLOEXEC));
Jeff Sharkeyfa1c6772017-03-25 22:49:13 -0600205 if (ctl_fd.get() == -1) {
206 PLOG(ERROR) << "Failed to open loop-control";
207 return -errno;
208 }
209
210 int num = ioctl(ctl_fd.get(), LOOP_CTL_GET_FREE);
211 if (num == -1) {
212 PLOG(ERROR) << "Failed LOOP_CTL_GET_FREE";
213 return -errno;
214 }
215
216 out_device = StringPrintf("/dev/block/loop%d", num);
217
Jeff Sharkeyfd3dc3c2017-03-27 10:49:21 -0600218 unique_fd target_fd(open(target.c_str(), O_RDWR | O_CLOEXEC));
Jeff Sharkeyfa1c6772017-03-25 22:49:13 -0600219 if (target_fd.get() == -1) {
220 PLOG(ERROR) << "Failed to open " << target;
221 return -errno;
222 }
Jeff Sharkeyfd3dc3c2017-03-27 10:49:21 -0600223 unique_fd device_fd(open(out_device.c_str(), O_RDWR | O_CLOEXEC));
Jeff Sharkeyfa1c6772017-03-25 22:49:13 -0600224 if (device_fd.get() == -1) {
225 PLOG(ERROR) << "Failed to open " << out_device;
226 return -errno;
227 }
228
229 if (ioctl(device_fd.get(), LOOP_SET_FD, target_fd.get()) == -1) {
230 PLOG(ERROR) << "Failed to LOOP_SET_FD";
231 return -errno;
232 }
233
Jeff Sharkey11c2d382017-09-11 10:32:01 -0600234 struct loop_info64 li;
235 memset(&li, 0, sizeof(li));
236 strlcpy((char*) li.lo_crypt_name, kVoldPrefix, LO_NAME_SIZE);
237 if (ioctl(device_fd.get(), LOOP_SET_STATUS64, &li) == -1) {
238 PLOG(ERROR) << "Failed to LOOP_SET_STATUS64";
239 return -errno;
240 }
241
Jeff Sharkeyfa1c6772017-03-25 22:49:13 -0600242 return 0;
243}
244
San Mehata19b2502010-01-06 10:33:53 -0800245int Loop::destroyByDevice(const char *loopDevice) {
246 int device_fd;
247
Jeff Sharkeyce6a9132015-04-08 21:07:21 -0700248 device_fd = open(loopDevice, O_RDONLY | O_CLOEXEC);
San Mehata19b2502010-01-06 10:33:53 -0800249 if (device_fd < 0) {
San Mehat97ac40e2010-03-24 10:24:19 -0700250 SLOGE("Failed to open loop (%d)", errno);
San Mehata19b2502010-01-06 10:33:53 -0800251 return -1;
252 }
253
254 if (ioctl(device_fd, LOOP_CLR_FD, 0) < 0) {
San Mehat97ac40e2010-03-24 10:24:19 -0700255 SLOGE("Failed to destroy loop (%d)", errno);
San Mehata19b2502010-01-06 10:33:53 -0800256 close(device_fd);
257 return -1;
258 }
259
260 close(device_fd);
261 return 0;
262}
263
Jeff Sharkey11c2d382017-09-11 10:32:01 -0600264int Loop::destroyAll() {
265 for (int i = 0; i < LOOP_MAX; i++) {
266 auto path = StringPrintf("/dev/block/loop%d", i);
267
268 unique_fd fd(open(path.c_str(), O_RDWR | O_CLOEXEC));
269 if (fd.get() == -1) {
270 if (errno != ENOENT) {
271 PLOG(WARNING) << "Failed to open " << path;
272 }
273 continue;
274 }
275
276 struct loop_info64 li;
277 if (ioctl(fd.get(), LOOP_GET_STATUS64, &li) < 0) {
278 PLOG(WARNING) << "Failed to LOOP_GET_STATUS64 " << path;
279 continue;
280 }
281
282 char* id = (char*) li.lo_crypt_name;
283 if (strncmp(id, kVoldPrefix, strlen(kVoldPrefix)) == 0) {
284 LOG(DEBUG) << "Tearing down stale loop device at " << path << " named " << id;
285
286 if (ioctl(fd.get(), LOOP_CLR_FD, 0) < 0) {
287 PLOG(WARNING) << "Failed to LOOP_CLR_FD " << path;
288 }
289 } else {
290 LOG(VERBOSE) << "Found unmanaged loop device at " << path << " named " << id;
291 }
292 }
293 return 0;
San Mehata19b2502010-01-06 10:33:53 -0800294}
295
Mateusz Nowaka4f48d02015-08-03 18:06:39 +0200296int Loop::createImageFile(const char *file, unsigned long numSectors) {
Jeff Sharkeyfd3dc3c2017-03-27 10:49:21 -0600297 unique_fd fd(open(file, O_CREAT | O_WRONLY | O_TRUNC | O_CLOEXEC, 0600));
Jeff Sharkeyfa1c6772017-03-25 22:49:13 -0600298 if (fd.get() == -1) {
299 PLOG(ERROR) << "Failed to create image " << file;
Jeff Sharkeyfd3dc3c2017-03-27 10:49:21 -0600300 return -errno;
San Mehata19b2502010-01-06 10:33:53 -0800301 }
Jeff Sharkeyfa1c6772017-03-25 22:49:13 -0600302 if (fallocate(fd.get(), 0, 0, numSectors * 512) == -1) {
303 PLOG(WARNING) << "Failed to fallocate; falling back to ftruncate";
304 if (ftruncate(fd, numSectors * 512) == -1) {
305 PLOG(ERROR) << "Failed to ftruncate";
Jeff Sharkeyfd3dc3c2017-03-27 10:49:21 -0600306 return -errno;
Jeff Sharkeyfa1c6772017-03-25 22:49:13 -0600307 }
308 }
Jeff Sharkeyfd3dc3c2017-03-27 10:49:21 -0600309 return 0;
San Mehata19b2502010-01-06 10:33:53 -0800310}
Kenny Root344ca102012-04-03 17:23:01 -0700311
Mateusz Nowaka4f48d02015-08-03 18:06:39 +0200312int Loop::resizeImageFile(const char *file, unsigned long numSectors) {
Daniel Rosenbergfcd34a02014-05-22 11:23:56 -0700313 int fd;
314
Jeff Sharkeyce6a9132015-04-08 21:07:21 -0700315 if ((fd = open(file, O_RDWR | O_CLOEXEC)) < 0) {
Daniel Rosenbergfcd34a02014-05-22 11:23:56 -0700316 SLOGE("Error opening imagefile (%s)", strerror(errno));
317 return -1;
318 }
319
Mateusz Nowaka4f48d02015-08-03 18:06:39 +0200320 SLOGD("Attempting to increase size of %s to %lu sectors.", file, numSectors);
Daniel Rosenbergfcd34a02014-05-22 11:23:56 -0700321
322 if (fallocate(fd, 0, 0, numSectors * 512)) {
Jeff Sharkey43ed1232014-08-22 12:29:05 -0700323 if (errno == ENOSYS || errno == ENOTSUP) {
Daniel Rosenbergfcd34a02014-05-22 11:23:56 -0700324 SLOGW("fallocate not found. Falling back to ftruncate.");
325 if (ftruncate(fd, numSectors * 512) < 0) {
326 SLOGE("Error truncating imagefile (%s)", strerror(errno));
327 close(fd);
328 return -1;
329 }
330 } else {
331 SLOGE("Error allocating space (%s)", strerror(errno));
332 close(fd);
333 return -1;
334 }
335 }
336 close(fd);
337 return 0;
338}