blob: b1e9f6a4cd11eae383d8e02f94f1607e74de8b4b [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
San Mehatd9a4e352010-03-12 13:32:47 -080035#include <sysutils/SocketClient.h>
San Mehata19b2502010-01-06 10:33:53 -080036#include "Loop.h"
Kenny Root344ca102012-04-03 17:23:01 -070037#include "Asec.h"
Hiroaki Miyazawa14eab552015-02-04 13:29:15 +090038#include "VoldUtil.h"
Stephen Smalley684e6622014-09-30 10:29:24 -040039#include "sehandle.h"
San Mehata19b2502010-01-06 10:33:53 -080040
San Mehatd9a4e352010-03-12 13:32:47 -080041int Loop::dumpState(SocketClient *c) {
42 int i;
43 int fd;
44 char filename[256];
45
46 for (i = 0; i < LOOP_MAX; i++) {
Kenny Root508c0e12010-07-12 09:59:49 -070047 struct loop_info64 li;
San Mehatd9a4e352010-03-12 13:32:47 -080048 int rc;
49
50 sprintf(filename, "/dev/block/loop%d", i);
51
52 if ((fd = open(filename, O_RDWR)) < 0) {
53 if (errno != ENOENT) {
San Mehat97ac40e2010-03-24 10:24:19 -070054 SLOGE("Unable to open %s (%s)", filename, strerror(errno));
San Mehatd9a4e352010-03-12 13:32:47 -080055 } else {
56 continue;
57 }
58 return -1;
59 }
60
Kenny Root508c0e12010-07-12 09:59:49 -070061 rc = ioctl(fd, LOOP_GET_STATUS64, &li);
San Mehatd9a4e352010-03-12 13:32:47 -080062 close(fd);
63 if (rc < 0 && errno == ENXIO) {
64 continue;
65 }
66
67 if (rc < 0) {
San Mehat97ac40e2010-03-24 10:24:19 -070068 SLOGE("Unable to get loop status for %s (%s)", filename,
San Mehatd9a4e352010-03-12 13:32:47 -080069 strerror(errno));
70 return -1;
71 }
72 char *tmp = NULL;
Kenny Root508c0e12010-07-12 09:59:49 -070073 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 -080074 MAJOR(li.lo_device), MINOR(li.lo_device), li.lo_inode, MAJOR(li.lo_rdevice),
Kenny Root508c0e12010-07-12 09:59:49 -070075 MINOR(li.lo_rdevice), li.lo_offset, li.lo_flags, li.lo_crypt_name,
76 li.lo_file_name);
San Mehatd9a4e352010-03-12 13:32:47 -080077 c->sendMsg(0, tmp, false);
78 free(tmp);
79 }
80 return 0;
81}
82
83int Loop::lookupActive(const char *id, char *buffer, size_t len) {
San Mehata19b2502010-01-06 10:33:53 -080084 int i;
85 int fd;
86 char filename[256];
87
88 memset(buffer, 0, len);
89
90 for (i = 0; i < LOOP_MAX; i++) {
Kenny Root508c0e12010-07-12 09:59:49 -070091 struct loop_info64 li;
San Mehata19b2502010-01-06 10:33:53 -080092 int rc;
93
94 sprintf(filename, "/dev/block/loop%d", i);
95
96 if ((fd = open(filename, O_RDWR)) < 0) {
San Mehatb78a32c2010-01-10 13:02:12 -080097 if (errno != ENOENT) {
San Mehat97ac40e2010-03-24 10:24:19 -070098 SLOGE("Unable to open %s (%s)", filename, strerror(errno));
San Mehatd9a4e352010-03-12 13:32:47 -080099 } else {
100 continue;
San Mehatb78a32c2010-01-10 13:02:12 -0800101 }
San Mehata19b2502010-01-06 10:33:53 -0800102 return -1;
103 }
104
Kenny Root508c0e12010-07-12 09:59:49 -0700105 rc = ioctl(fd, LOOP_GET_STATUS64, &li);
San Mehata19b2502010-01-06 10:33:53 -0800106 close(fd);
107 if (rc < 0 && errno == ENXIO) {
108 continue;
San Mehata19b2502010-01-06 10:33:53 -0800109 }
110
111 if (rc < 0) {
San Mehat97ac40e2010-03-24 10:24:19 -0700112 SLOGE("Unable to get loop status for %s (%s)", filename,
San Mehata19b2502010-01-06 10:33:53 -0800113 strerror(errno));
114 return -1;
115 }
Kenny Root508c0e12010-07-12 09:59:49 -0700116 if (!strncmp((const char*) li.lo_crypt_name, id, LO_NAME_SIZE)) {
San Mehata19b2502010-01-06 10:33:53 -0800117 break;
118 }
119 }
120
121 if (i == LOOP_MAX) {
122 errno = ENOENT;
123 return -1;
124 }
Henrik Baard21522662015-02-06 09:24:14 +0100125 strlcpy(buffer, filename, len);
San Mehata19b2502010-01-06 10:33:53 -0800126 return 0;
127}
128
San Mehatd9a4e352010-03-12 13:32:47 -0800129int Loop::create(const char *id, const char *loopFile, char *loopDeviceBuffer, size_t len) {
San Mehata19b2502010-01-06 10:33:53 -0800130 int i;
131 int fd;
132 char filename[256];
133
San Mehata19b2502010-01-06 10:33:53 -0800134 for (i = 0; i < LOOP_MAX; i++) {
Kenny Root7c165022011-02-01 15:58:25 -0800135 struct loop_info64 li;
San Mehata19b2502010-01-06 10:33:53 -0800136 int rc;
Stephen Smalley684e6622014-09-30 10:29:24 -0400137 char *secontext = NULL;
San Mehata19b2502010-01-06 10:33:53 -0800138
139 sprintf(filename, "/dev/block/loop%d", i);
140
San Mehat8da6bcb2010-01-09 12:24:05 -0800141 /*
142 * The kernel starts us off with 8 loop nodes, but more
143 * are created on-demand if needed.
144 */
145 mode_t mode = 0660 | S_IFBLK;
San Mehatb78a32c2010-01-10 13:02:12 -0800146 unsigned int dev = (0xff & i) | ((i << 12) & 0xfff00000) | (7 << 8);
Stephen Smalley684e6622014-09-30 10:29:24 -0400147
148 if (sehandle) {
149 rc = selabel_lookup(sehandle, &secontext, filename, S_IFBLK);
150 if (rc == 0)
151 setfscreatecon(secontext);
152 }
153
San Mehat8da6bcb2010-01-09 12:24:05 -0800154 if (mknod(filename, mode, dev) < 0) {
155 if (errno != EEXIST) {
Stephen Smalley684e6622014-09-30 10:29:24 -0400156 int sverrno = errno;
San Mehat97ac40e2010-03-24 10:24:19 -0700157 SLOGE("Error creating loop device node (%s)", strerror(errno));
Stephen Smalley684e6622014-09-30 10:29:24 -0400158 if (secontext) {
159 freecon(secontext);
160 setfscreatecon(NULL);
161 }
162 errno = sverrno;
San Mehat8da6bcb2010-01-09 12:24:05 -0800163 return -1;
164 }
165 }
Stephen Smalley684e6622014-09-30 10:29:24 -0400166 if (secontext) {
167 freecon(secontext);
168 setfscreatecon(NULL);
169 }
San Mehat8da6bcb2010-01-09 12:24:05 -0800170
San Mehata19b2502010-01-06 10:33:53 -0800171 if ((fd = open(filename, O_RDWR)) < 0) {
San Mehat97ac40e2010-03-24 10:24:19 -0700172 SLOGE("Unable to open %s (%s)", filename, strerror(errno));
San Mehata19b2502010-01-06 10:33:53 -0800173 return -1;
174 }
175
Kenny Root7c165022011-02-01 15:58:25 -0800176 rc = ioctl(fd, LOOP_GET_STATUS64, &li);
San Mehata19b2502010-01-06 10:33:53 -0800177 if (rc < 0 && errno == ENXIO)
178 break;
179
San Mehat8da6bcb2010-01-09 12:24:05 -0800180 close(fd);
181
San Mehata19b2502010-01-06 10:33:53 -0800182 if (rc < 0) {
San Mehat97ac40e2010-03-24 10:24:19 -0700183 SLOGE("Unable to get loop status for %s (%s)", filename,
San Mehata19b2502010-01-06 10:33:53 -0800184 strerror(errno));
185 return -1;
186 }
187 }
188
189 if (i == LOOP_MAX) {
San Mehat97ac40e2010-03-24 10:24:19 -0700190 SLOGE("Exhausted all loop devices");
San Mehata19b2502010-01-06 10:33:53 -0800191 errno = ENOSPC;
192 return -1;
193 }
San Mehata19b2502010-01-06 10:33:53 -0800194
Henrik Baard21522662015-02-06 09:24:14 +0100195 strlcpy(loopDeviceBuffer, filename, len);
San Mehat8da6bcb2010-01-09 12:24:05 -0800196
San Mehata19b2502010-01-06 10:33:53 -0800197 int file_fd;
198
San Mehata19b2502010-01-06 10:33:53 -0800199 if ((file_fd = open(loopFile, O_RDWR)) < 0) {
San Mehat97ac40e2010-03-24 10:24:19 -0700200 SLOGE("Unable to open %s (%s)", loopFile, strerror(errno));
San Mehata19b2502010-01-06 10:33:53 -0800201 close(fd);
202 return -1;
203 }
204
205 if (ioctl(fd, LOOP_SET_FD, file_fd) < 0) {
San Mehat97ac40e2010-03-24 10:24:19 -0700206 SLOGE("Error setting up loopback interface (%s)", strerror(errno));
San Mehata19b2502010-01-06 10:33:53 -0800207 close(file_fd);
208 close(fd);
209 return -1;
210 }
211
Kenny Root508c0e12010-07-12 09:59:49 -0700212 struct loop_info64 li;
San Mehata19b2502010-01-06 10:33:53 -0800213
214 memset(&li, 0, sizeof(li));
Peter Bohm092aa1c2011-04-01 12:35:25 +0200215 strlcpy((char*) li.lo_crypt_name, id, LO_NAME_SIZE);
216 strlcpy((char*) li.lo_file_name, loopFile, LO_NAME_SIZE);
San Mehata19b2502010-01-06 10:33:53 -0800217
Kenny Root508c0e12010-07-12 09:59:49 -0700218 if (ioctl(fd, LOOP_SET_STATUS64, &li) < 0) {
San Mehat97ac40e2010-03-24 10:24:19 -0700219 SLOGE("Error setting loopback status (%s)", strerror(errno));
San Mehata19b2502010-01-06 10:33:53 -0800220 close(file_fd);
221 close(fd);
222 return -1;
223 }
224
225 close(fd);
226 close(file_fd);
227
228 return 0;
229}
230
231int Loop::destroyByDevice(const char *loopDevice) {
232 int device_fd;
233
234 device_fd = open(loopDevice, O_RDONLY);
235 if (device_fd < 0) {
San Mehat97ac40e2010-03-24 10:24:19 -0700236 SLOGE("Failed to open loop (%d)", errno);
San Mehata19b2502010-01-06 10:33:53 -0800237 return -1;
238 }
239
240 if (ioctl(device_fd, LOOP_CLR_FD, 0) < 0) {
San Mehat97ac40e2010-03-24 10:24:19 -0700241 SLOGE("Failed to destroy loop (%d)", errno);
San Mehata19b2502010-01-06 10:33:53 -0800242 close(device_fd);
243 return -1;
244 }
245
246 close(device_fd);
247 return 0;
248}
249
Mark Salyzyn3e971272014-01-21 13:27:04 -0800250int Loop::destroyByFile(const char * /*loopFile*/) {
San Mehata19b2502010-01-06 10:33:53 -0800251 errno = ENOSYS;
252 return -1;
253}
254
San Mehat8b8f71b2010-01-11 09:17:25 -0800255int Loop::createImageFile(const char *file, unsigned int numSectors) {
San Mehata19b2502010-01-06 10:33:53 -0800256 int fd;
257
San Mehata19b2502010-01-06 10:33:53 -0800258 if ((fd = creat(file, 0600)) < 0) {
San Mehat97ac40e2010-03-24 10:24:19 -0700259 SLOGE("Error creating imagefile (%s)", strerror(errno));
San Mehata19b2502010-01-06 10:33:53 -0800260 return -1;
261 }
262
San Mehat8b8f71b2010-01-11 09:17:25 -0800263 if (ftruncate(fd, numSectors * 512) < 0) {
San Mehat97ac40e2010-03-24 10:24:19 -0700264 SLOGE("Error truncating imagefile (%s)", strerror(errno));
San Mehata19b2502010-01-06 10:33:53 -0800265 close(fd);
266 return -1;
267 }
268 close(fd);
269 return 0;
270}
Kenny Root344ca102012-04-03 17:23:01 -0700271
Daniel Rosenbergfcd34a02014-05-22 11:23:56 -0700272int Loop::resizeImageFile(const char *file, unsigned int numSectors) {
273 int fd;
274
275 if ((fd = open(file, O_RDWR)) < 0) {
276 SLOGE("Error opening imagefile (%s)", strerror(errno));
277 return -1;
278 }
279
280 SLOGD("Attempting to increase size of %s to %d sectors.", file, numSectors);
281
282 if (fallocate(fd, 0, 0, numSectors * 512)) {
Jeff Sharkey43ed1232014-08-22 12:29:05 -0700283 if (errno == ENOSYS || errno == ENOTSUP) {
Daniel Rosenbergfcd34a02014-05-22 11:23:56 -0700284 SLOGW("fallocate not found. Falling back to ftruncate.");
285 if (ftruncate(fd, numSectors * 512) < 0) {
286 SLOGE("Error truncating imagefile (%s)", strerror(errno));
287 close(fd);
288 return -1;
289 }
290 } else {
291 SLOGE("Error allocating space (%s)", strerror(errno));
292 close(fd);
293 return -1;
294 }
295 }
296 close(fd);
297 return 0;
298}
299
Hiroaki Miyazawa14eab552015-02-04 13:29:15 +0900300int Loop::lookupInfo(const char *loopDevice, struct asec_superblock *sb, unsigned long *nr_sec) {
Kenny Root344ca102012-04-03 17:23:01 -0700301 int fd;
302 struct asec_superblock buffer;
303
304 if ((fd = open(loopDevice, O_RDONLY)) < 0) {
305 SLOGE("Failed to open loopdevice (%s)", strerror(errno));
306 destroyByDevice(loopDevice);
307 return -1;
308 }
309
Hiroaki Miyazawa14eab552015-02-04 13:29:15 +0900310 get_blkdev_size(fd, nr_sec);
311 if (*nr_sec == 0) {
Kenny Root344ca102012-04-03 17:23:01 -0700312 SLOGE("Failed to get loop size (%s)", strerror(errno));
313 destroyByDevice(loopDevice);
314 close(fd);
315 return -1;
316 }
317
318 /*
319 * Try to read superblock.
320 */
321 memset(&buffer, 0, sizeof(struct asec_superblock));
322 if (lseek(fd, ((*nr_sec - 1) * 512), SEEK_SET) < 0) {
323 SLOGE("lseek failed (%s)", strerror(errno));
324 close(fd);
325 destroyByDevice(loopDevice);
326 return -1;
327 }
328 if (read(fd, &buffer, sizeof(struct asec_superblock)) != sizeof(struct asec_superblock)) {
329 SLOGE("superblock read failed (%s)", strerror(errno));
330 close(fd);
331 destroyByDevice(loopDevice);
332 return -1;
333 }
334 close(fd);
335
336 /*
337 * Superblock successfully read. Copy to caller's struct.
338 */
339 memcpy(sb, &buffer, sizeof(struct asec_superblock));
340 return 0;
341}