blob: 7e243de1e34dd2d1bcd446fd97090c9f622a2f78 [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 Mehatd9a4e352010-03-12 13:32:47 -080039#include <sysutils/SocketClient.h>
San Mehata19b2502010-01-06 10:33:53 -080040#include "Loop.h"
Kenny Root344ca102012-04-03 17:23:01 -070041#include "Asec.h"
Hiroaki Miyazawa14eab552015-02-04 13:29:15 +090042#include "VoldUtil.h"
Stephen Smalley684e6622014-09-30 10:29:24 -040043#include "sehandle.h"
San Mehata19b2502010-01-06 10:33:53 -080044
Jeff Sharkeyfa1c6772017-03-25 22:49:13 -060045using android::base::StringPrintf;
46using android::base::unique_fd;
47
San Mehatd9a4e352010-03-12 13:32:47 -080048int Loop::dumpState(SocketClient *c) {
49 int i;
50 int fd;
51 char filename[256];
52
53 for (i = 0; i < LOOP_MAX; i++) {
Kenny Root508c0e12010-07-12 09:59:49 -070054 struct loop_info64 li;
San Mehatd9a4e352010-03-12 13:32:47 -080055 int rc;
56
George Burgess IV605d7ae2016-02-29 13:39:17 -080057 snprintf(filename, sizeof(filename), "/dev/block/loop%d", i);
San Mehatd9a4e352010-03-12 13:32:47 -080058
Jeff Sharkeyce6a9132015-04-08 21:07:21 -070059 if ((fd = open(filename, O_RDWR | O_CLOEXEC)) < 0) {
San Mehatd9a4e352010-03-12 13:32:47 -080060 if (errno != ENOENT) {
San Mehat97ac40e2010-03-24 10:24:19 -070061 SLOGE("Unable to open %s (%s)", filename, strerror(errno));
San Mehatd9a4e352010-03-12 13:32:47 -080062 } else {
63 continue;
64 }
65 return -1;
66 }
67
Kenny Root508c0e12010-07-12 09:59:49 -070068 rc = ioctl(fd, LOOP_GET_STATUS64, &li);
San Mehatd9a4e352010-03-12 13:32:47 -080069 close(fd);
70 if (rc < 0 && errno == ENXIO) {
71 continue;
72 }
73
74 if (rc < 0) {
San Mehat97ac40e2010-03-24 10:24:19 -070075 SLOGE("Unable to get loop status for %s (%s)", filename,
San Mehatd9a4e352010-03-12 13:32:47 -080076 strerror(errno));
77 return -1;
78 }
79 char *tmp = NULL;
Kenny Root508c0e12010-07-12 09:59:49 -070080 asprintf(&tmp, "%s %d %lld:%lld %llu %lld:%lld %lld 0x%x {%s} {%s}", filename, li.lo_number,
San Mehatd9a4e352010-03-12 13:32:47 -080081 MAJOR(li.lo_device), MINOR(li.lo_device), li.lo_inode, MAJOR(li.lo_rdevice),
Kenny Root508c0e12010-07-12 09:59:49 -070082 MINOR(li.lo_rdevice), li.lo_offset, li.lo_flags, li.lo_crypt_name,
83 li.lo_file_name);
San Mehatd9a4e352010-03-12 13:32:47 -080084 c->sendMsg(0, tmp, false);
85 free(tmp);
86 }
87 return 0;
88}
89
90int Loop::lookupActive(const char *id, char *buffer, size_t len) {
San Mehata19b2502010-01-06 10:33:53 -080091 int i;
92 int fd;
93 char filename[256];
94
95 memset(buffer, 0, len);
96
97 for (i = 0; i < LOOP_MAX; i++) {
Kenny Root508c0e12010-07-12 09:59:49 -070098 struct loop_info64 li;
San Mehata19b2502010-01-06 10:33:53 -080099 int rc;
100
George Burgess IV605d7ae2016-02-29 13:39:17 -0800101 snprintf(filename, sizeof(filename), "/dev/block/loop%d", i);
San Mehata19b2502010-01-06 10:33:53 -0800102
Jeff Sharkeyce6a9132015-04-08 21:07:21 -0700103 if ((fd = open(filename, O_RDWR | O_CLOEXEC)) < 0) {
San Mehatb78a32c2010-01-10 13:02:12 -0800104 if (errno != ENOENT) {
San Mehat97ac40e2010-03-24 10:24:19 -0700105 SLOGE("Unable to open %s (%s)", filename, strerror(errno));
San Mehatd9a4e352010-03-12 13:32:47 -0800106 } else {
107 continue;
San Mehatb78a32c2010-01-10 13:02:12 -0800108 }
San Mehata19b2502010-01-06 10:33:53 -0800109 return -1;
110 }
111
Kenny Root508c0e12010-07-12 09:59:49 -0700112 rc = ioctl(fd, LOOP_GET_STATUS64, &li);
San Mehata19b2502010-01-06 10:33:53 -0800113 if (rc < 0 && errno == ENXIO) {
tao.peia1038a92015-08-17 20:18:49 +0800114 close(fd);
San Mehata19b2502010-01-06 10:33:53 -0800115 continue;
San Mehata19b2502010-01-06 10:33:53 -0800116 }
tao.peia1038a92015-08-17 20:18:49 +0800117 close(fd);
San Mehata19b2502010-01-06 10:33:53 -0800118
119 if (rc < 0) {
San Mehat97ac40e2010-03-24 10:24:19 -0700120 SLOGE("Unable to get loop status for %s (%s)", filename,
San Mehata19b2502010-01-06 10:33:53 -0800121 strerror(errno));
122 return -1;
123 }
Kenny Root508c0e12010-07-12 09:59:49 -0700124 if (!strncmp((const char*) li.lo_crypt_name, id, LO_NAME_SIZE)) {
San Mehata19b2502010-01-06 10:33:53 -0800125 break;
126 }
127 }
128
129 if (i == LOOP_MAX) {
130 errno = ENOENT;
131 return -1;
132 }
Henrik Baard21522662015-02-06 09:24:14 +0100133 strlcpy(buffer, filename, len);
San Mehata19b2502010-01-06 10:33:53 -0800134 return 0;
135}
136
San Mehatd9a4e352010-03-12 13:32:47 -0800137int Loop::create(const char *id, const char *loopFile, char *loopDeviceBuffer, size_t len) {
San Mehata19b2502010-01-06 10:33:53 -0800138 int i;
139 int fd;
140 char filename[256];
141
San Mehata19b2502010-01-06 10:33:53 -0800142 for (i = 0; i < LOOP_MAX; i++) {
Kenny Root7c165022011-02-01 15:58:25 -0800143 struct loop_info64 li;
San Mehata19b2502010-01-06 10:33:53 -0800144 int rc;
Stephen Smalley684e6622014-09-30 10:29:24 -0400145 char *secontext = NULL;
San Mehata19b2502010-01-06 10:33:53 -0800146
George Burgess IV605d7ae2016-02-29 13:39:17 -0800147 snprintf(filename, sizeof(filename), "/dev/block/loop%d", i);
San Mehata19b2502010-01-06 10:33:53 -0800148
San Mehat8da6bcb2010-01-09 12:24:05 -0800149 /*
150 * The kernel starts us off with 8 loop nodes, but more
151 * are created on-demand if needed.
152 */
153 mode_t mode = 0660 | S_IFBLK;
San Mehatb78a32c2010-01-10 13:02:12 -0800154 unsigned int dev = (0xff & i) | ((i << 12) & 0xfff00000) | (7 << 8);
Stephen Smalley684e6622014-09-30 10:29:24 -0400155
156 if (sehandle) {
157 rc = selabel_lookup(sehandle, &secontext, filename, S_IFBLK);
158 if (rc == 0)
159 setfscreatecon(secontext);
160 }
161
San Mehat8da6bcb2010-01-09 12:24:05 -0800162 if (mknod(filename, mode, dev) < 0) {
163 if (errno != EEXIST) {
Stephen Smalley684e6622014-09-30 10:29:24 -0400164 int sverrno = errno;
San Mehat97ac40e2010-03-24 10:24:19 -0700165 SLOGE("Error creating loop device node (%s)", strerror(errno));
Stephen Smalley684e6622014-09-30 10:29:24 -0400166 if (secontext) {
167 freecon(secontext);
168 setfscreatecon(NULL);
169 }
170 errno = sverrno;
San Mehat8da6bcb2010-01-09 12:24:05 -0800171 return -1;
172 }
173 }
Stephen Smalley684e6622014-09-30 10:29:24 -0400174 if (secontext) {
175 freecon(secontext);
176 setfscreatecon(NULL);
177 }
San Mehat8da6bcb2010-01-09 12:24:05 -0800178
Jeff Sharkeyce6a9132015-04-08 21:07:21 -0700179 if ((fd = open(filename, O_RDWR | O_CLOEXEC)) < 0) {
San Mehat97ac40e2010-03-24 10:24:19 -0700180 SLOGE("Unable to open %s (%s)", filename, strerror(errno));
San Mehata19b2502010-01-06 10:33:53 -0800181 return -1;
182 }
183
Kenny Root7c165022011-02-01 15:58:25 -0800184 rc = ioctl(fd, LOOP_GET_STATUS64, &li);
San Mehata19b2502010-01-06 10:33:53 -0800185 if (rc < 0 && errno == ENXIO)
186 break;
187
San Mehat8da6bcb2010-01-09 12:24:05 -0800188 close(fd);
189
San Mehata19b2502010-01-06 10:33:53 -0800190 if (rc < 0) {
San Mehat97ac40e2010-03-24 10:24:19 -0700191 SLOGE("Unable to get loop status for %s (%s)", filename,
San Mehata19b2502010-01-06 10:33:53 -0800192 strerror(errno));
193 return -1;
194 }
195 }
196
197 if (i == LOOP_MAX) {
San Mehat97ac40e2010-03-24 10:24:19 -0700198 SLOGE("Exhausted all loop devices");
San Mehata19b2502010-01-06 10:33:53 -0800199 errno = ENOSPC;
200 return -1;
201 }
San Mehata19b2502010-01-06 10:33:53 -0800202
Henrik Baard21522662015-02-06 09:24:14 +0100203 strlcpy(loopDeviceBuffer, filename, len);
San Mehat8da6bcb2010-01-09 12:24:05 -0800204
San Mehata19b2502010-01-06 10:33:53 -0800205 int file_fd;
206
Jeff Sharkeyce6a9132015-04-08 21:07:21 -0700207 if ((file_fd = open(loopFile, O_RDWR | O_CLOEXEC)) < 0) {
San Mehat97ac40e2010-03-24 10:24:19 -0700208 SLOGE("Unable to open %s (%s)", loopFile, strerror(errno));
San Mehata19b2502010-01-06 10:33:53 -0800209 close(fd);
210 return -1;
211 }
212
213 if (ioctl(fd, LOOP_SET_FD, file_fd) < 0) {
San Mehat97ac40e2010-03-24 10:24:19 -0700214 SLOGE("Error setting up loopback interface (%s)", strerror(errno));
San Mehata19b2502010-01-06 10:33:53 -0800215 close(file_fd);
216 close(fd);
217 return -1;
218 }
219
Kenny Root508c0e12010-07-12 09:59:49 -0700220 struct loop_info64 li;
San Mehata19b2502010-01-06 10:33:53 -0800221
222 memset(&li, 0, sizeof(li));
Peter Bohm092aa1c2011-04-01 12:35:25 +0200223 strlcpy((char*) li.lo_crypt_name, id, LO_NAME_SIZE);
224 strlcpy((char*) li.lo_file_name, loopFile, LO_NAME_SIZE);
San Mehata19b2502010-01-06 10:33:53 -0800225
Kenny Root508c0e12010-07-12 09:59:49 -0700226 if (ioctl(fd, LOOP_SET_STATUS64, &li) < 0) {
San Mehat97ac40e2010-03-24 10:24:19 -0700227 SLOGE("Error setting loopback status (%s)", strerror(errno));
San Mehata19b2502010-01-06 10:33:53 -0800228 close(file_fd);
229 close(fd);
230 return -1;
231 }
232
233 close(fd);
234 close(file_fd);
235
236 return 0;
237}
238
Jeff Sharkeyfa1c6772017-03-25 22:49:13 -0600239int Loop::create(const std::string& target, std::string& out_device) {
240 unique_fd ctl_fd(open("/dev/loop-control", O_RDWR));
241 if (ctl_fd.get() == -1) {
242 PLOG(ERROR) << "Failed to open loop-control";
243 return -errno;
244 }
245
246 int num = ioctl(ctl_fd.get(), LOOP_CTL_GET_FREE);
247 if (num == -1) {
248 PLOG(ERROR) << "Failed LOOP_CTL_GET_FREE";
249 return -errno;
250 }
251
252 out_device = StringPrintf("/dev/block/loop%d", num);
253
254 unique_fd target_fd(open(target.c_str(), O_RDWR));
255 if (target_fd.get() == -1) {
256 PLOG(ERROR) << "Failed to open " << target;
257 return -errno;
258 }
259 unique_fd device_fd(open(out_device.c_str(), O_RDWR));
260 if (device_fd.get() == -1) {
261 PLOG(ERROR) << "Failed to open " << out_device;
262 return -errno;
263 }
264
265 if (ioctl(device_fd.get(), LOOP_SET_FD, target_fd.get()) == -1) {
266 PLOG(ERROR) << "Failed to LOOP_SET_FD";
267 return -errno;
268 }
269
270 return 0;
271}
272
San Mehata19b2502010-01-06 10:33:53 -0800273int Loop::destroyByDevice(const char *loopDevice) {
274 int device_fd;
275
Jeff Sharkeyce6a9132015-04-08 21:07:21 -0700276 device_fd = open(loopDevice, O_RDONLY | O_CLOEXEC);
San Mehata19b2502010-01-06 10:33:53 -0800277 if (device_fd < 0) {
San Mehat97ac40e2010-03-24 10:24:19 -0700278 SLOGE("Failed to open loop (%d)", errno);
San Mehata19b2502010-01-06 10:33:53 -0800279 return -1;
280 }
281
282 if (ioctl(device_fd, LOOP_CLR_FD, 0) < 0) {
San Mehat97ac40e2010-03-24 10:24:19 -0700283 SLOGE("Failed to destroy loop (%d)", errno);
San Mehata19b2502010-01-06 10:33:53 -0800284 close(device_fd);
285 return -1;
286 }
287
288 close(device_fd);
289 return 0;
290}
291
Mark Salyzyn3e971272014-01-21 13:27:04 -0800292int Loop::destroyByFile(const char * /*loopFile*/) {
San Mehata19b2502010-01-06 10:33:53 -0800293 errno = ENOSYS;
294 return -1;
295}
296
Mateusz Nowaka4f48d02015-08-03 18:06:39 +0200297int Loop::createImageFile(const char *file, unsigned long numSectors) {
Jeff Sharkeyfa1c6772017-03-25 22:49:13 -0600298 int res = 0;
San Mehata19b2502010-01-06 10:33:53 -0800299
Jeff Sharkeyfa1c6772017-03-25 22:49:13 -0600300 char* secontext = nullptr;
301 if (sehandle) {
302 if (!selabel_lookup(sehandle, &secontext, file, S_IFREG)) {
303 setfscreatecon(secontext);
304 }
San Mehata19b2502010-01-06 10:33:53 -0800305 }
306
Jeff Sharkeyfa1c6772017-03-25 22:49:13 -0600307 unique_fd fd(creat(file, 0600));
308 if (fd.get() == -1) {
309 PLOG(ERROR) << "Failed to create image " << file;
310 res = -errno;
311 goto done;
San Mehata19b2502010-01-06 10:33:53 -0800312 }
Jeff Sharkeyfa1c6772017-03-25 22:49:13 -0600313
314 if (fallocate(fd.get(), 0, 0, numSectors * 512) == -1) {
315 PLOG(WARNING) << "Failed to fallocate; falling back to ftruncate";
316 if (ftruncate(fd, numSectors * 512) == -1) {
317 PLOG(ERROR) << "Failed to ftruncate";
318 res = -errno;
319 }
320 }
321
322done:
323 if (secontext) {
324 setfscreatecon(nullptr);
325 freecon(secontext);
326 }
327
328 return res;
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}
358
Hiroaki Miyazawa14eab552015-02-04 13:29:15 +0900359int Loop::lookupInfo(const char *loopDevice, struct asec_superblock *sb, unsigned long *nr_sec) {
Kenny Root344ca102012-04-03 17:23:01 -0700360 int fd;
361 struct asec_superblock buffer;
362
Jeff Sharkeyce6a9132015-04-08 21:07:21 -0700363 if ((fd = open(loopDevice, O_RDONLY | O_CLOEXEC)) < 0) {
Kenny Root344ca102012-04-03 17:23:01 -0700364 SLOGE("Failed to open loopdevice (%s)", strerror(errno));
365 destroyByDevice(loopDevice);
366 return -1;
367 }
368
Hiroaki Miyazawa14eab552015-02-04 13:29:15 +0900369 get_blkdev_size(fd, nr_sec);
370 if (*nr_sec == 0) {
Kenny Root344ca102012-04-03 17:23:01 -0700371 SLOGE("Failed to get loop size (%s)", strerror(errno));
372 destroyByDevice(loopDevice);
373 close(fd);
374 return -1;
375 }
376
377 /*
378 * Try to read superblock.
379 */
380 memset(&buffer, 0, sizeof(struct asec_superblock));
381 if (lseek(fd, ((*nr_sec - 1) * 512), SEEK_SET) < 0) {
382 SLOGE("lseek failed (%s)", strerror(errno));
383 close(fd);
384 destroyByDevice(loopDevice);
385 return -1;
386 }
387 if (read(fd, &buffer, sizeof(struct asec_superblock)) != sizeof(struct asec_superblock)) {
388 SLOGE("superblock read failed (%s)", strerror(errno));
389 close(fd);
390 destroyByDevice(loopDevice);
391 return -1;
392 }
393 close(fd);
394
395 /*
396 * Superblock successfully read. Copy to caller's struct.
397 */
398 memcpy(sb, &buffer, sizeof(struct asec_superblock));
399 return 0;
400}