blob: ca2609340432347221e83448359d8a75bb5c2001 [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"
Stephen Smalley684e6622014-09-30 10:29:24 -040038#include "sehandle.h"
San Mehata19b2502010-01-06 10:33:53 -080039
San Mehatd9a4e352010-03-12 13:32:47 -080040int Loop::dumpState(SocketClient *c) {
41 int i;
42 int fd;
43 char filename[256];
44
45 for (i = 0; i < LOOP_MAX; i++) {
Kenny Root508c0e12010-07-12 09:59:49 -070046 struct loop_info64 li;
San Mehatd9a4e352010-03-12 13:32:47 -080047 int rc;
48
49 sprintf(filename, "/dev/block/loop%d", i);
50
51 if ((fd = open(filename, O_RDWR)) < 0) {
52 if (errno != ENOENT) {
San Mehat97ac40e2010-03-24 10:24:19 -070053 SLOGE("Unable to open %s (%s)", filename, strerror(errno));
San Mehatd9a4e352010-03-12 13:32:47 -080054 } else {
55 continue;
56 }
57 return -1;
58 }
59
Kenny Root508c0e12010-07-12 09:59:49 -070060 rc = ioctl(fd, LOOP_GET_STATUS64, &li);
San Mehatd9a4e352010-03-12 13:32:47 -080061 close(fd);
62 if (rc < 0 && errno == ENXIO) {
63 continue;
64 }
65
66 if (rc < 0) {
San Mehat97ac40e2010-03-24 10:24:19 -070067 SLOGE("Unable to get loop status for %s (%s)", filename,
San Mehatd9a4e352010-03-12 13:32:47 -080068 strerror(errno));
69 return -1;
70 }
71 char *tmp = NULL;
Kenny Root508c0e12010-07-12 09:59:49 -070072 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 -080073 MAJOR(li.lo_device), MINOR(li.lo_device), li.lo_inode, MAJOR(li.lo_rdevice),
Kenny Root508c0e12010-07-12 09:59:49 -070074 MINOR(li.lo_rdevice), li.lo_offset, li.lo_flags, li.lo_crypt_name,
75 li.lo_file_name);
San Mehatd9a4e352010-03-12 13:32:47 -080076 c->sendMsg(0, tmp, false);
77 free(tmp);
78 }
79 return 0;
80}
81
82int Loop::lookupActive(const char *id, char *buffer, size_t len) {
San Mehata19b2502010-01-06 10:33:53 -080083 int i;
84 int fd;
85 char filename[256];
86
87 memset(buffer, 0, len);
88
89 for (i = 0; i < LOOP_MAX; i++) {
Kenny Root508c0e12010-07-12 09:59:49 -070090 struct loop_info64 li;
San Mehata19b2502010-01-06 10:33:53 -080091 int rc;
92
93 sprintf(filename, "/dev/block/loop%d", i);
94
95 if ((fd = open(filename, O_RDWR)) < 0) {
San Mehatb78a32c2010-01-10 13:02:12 -080096 if (errno != ENOENT) {
San Mehat97ac40e2010-03-24 10:24:19 -070097 SLOGE("Unable to open %s (%s)", filename, strerror(errno));
San Mehatd9a4e352010-03-12 13:32:47 -080098 } else {
99 continue;
San Mehatb78a32c2010-01-10 13:02:12 -0800100 }
San Mehata19b2502010-01-06 10:33:53 -0800101 return -1;
102 }
103
Kenny Root508c0e12010-07-12 09:59:49 -0700104 rc = ioctl(fd, LOOP_GET_STATUS64, &li);
San Mehata19b2502010-01-06 10:33:53 -0800105 close(fd);
106 if (rc < 0 && errno == ENXIO) {
107 continue;
San Mehata19b2502010-01-06 10:33:53 -0800108 }
109
110 if (rc < 0) {
San Mehat97ac40e2010-03-24 10:24:19 -0700111 SLOGE("Unable to get loop status for %s (%s)", filename,
San Mehata19b2502010-01-06 10:33:53 -0800112 strerror(errno));
113 return -1;
114 }
Kenny Root508c0e12010-07-12 09:59:49 -0700115 if (!strncmp((const char*) li.lo_crypt_name, id, LO_NAME_SIZE)) {
San Mehata19b2502010-01-06 10:33:53 -0800116 break;
117 }
118 }
119
120 if (i == LOOP_MAX) {
121 errno = ENOENT;
122 return -1;
123 }
124 strncpy(buffer, filename, len -1);
125 return 0;
126}
127
San Mehatd9a4e352010-03-12 13:32:47 -0800128int Loop::create(const char *id, const char *loopFile, char *loopDeviceBuffer, size_t len) {
San Mehata19b2502010-01-06 10:33:53 -0800129 int i;
130 int fd;
131 char filename[256];
132
San Mehata19b2502010-01-06 10:33:53 -0800133 for (i = 0; i < LOOP_MAX; i++) {
Kenny Root7c165022011-02-01 15:58:25 -0800134 struct loop_info64 li;
San Mehata19b2502010-01-06 10:33:53 -0800135 int rc;
Stephen Smalley684e6622014-09-30 10:29:24 -0400136 char *secontext = NULL;
San Mehata19b2502010-01-06 10:33:53 -0800137
138 sprintf(filename, "/dev/block/loop%d", i);
139
San Mehat8da6bcb2010-01-09 12:24:05 -0800140 /*
141 * The kernel starts us off with 8 loop nodes, but more
142 * are created on-demand if needed.
143 */
144 mode_t mode = 0660 | S_IFBLK;
San Mehatb78a32c2010-01-10 13:02:12 -0800145 unsigned int dev = (0xff & i) | ((i << 12) & 0xfff00000) | (7 << 8);
Stephen Smalley684e6622014-09-30 10:29:24 -0400146
147 if (sehandle) {
148 rc = selabel_lookup(sehandle, &secontext, filename, S_IFBLK);
149 if (rc == 0)
150 setfscreatecon(secontext);
151 }
152
San Mehat8da6bcb2010-01-09 12:24:05 -0800153 if (mknod(filename, mode, dev) < 0) {
154 if (errno != EEXIST) {
Stephen Smalley684e6622014-09-30 10:29:24 -0400155 int sverrno = errno;
San Mehat97ac40e2010-03-24 10:24:19 -0700156 SLOGE("Error creating loop device node (%s)", strerror(errno));
Stephen Smalley684e6622014-09-30 10:29:24 -0400157 if (secontext) {
158 freecon(secontext);
159 setfscreatecon(NULL);
160 }
161 errno = sverrno;
San Mehat8da6bcb2010-01-09 12:24:05 -0800162 return -1;
163 }
164 }
Stephen Smalley684e6622014-09-30 10:29:24 -0400165 if (secontext) {
166 freecon(secontext);
167 setfscreatecon(NULL);
168 }
San Mehat8da6bcb2010-01-09 12:24:05 -0800169
San Mehata19b2502010-01-06 10:33:53 -0800170 if ((fd = open(filename, O_RDWR)) < 0) {
San Mehat97ac40e2010-03-24 10:24:19 -0700171 SLOGE("Unable to open %s (%s)", filename, strerror(errno));
San Mehata19b2502010-01-06 10:33:53 -0800172 return -1;
173 }
174
Kenny Root7c165022011-02-01 15:58:25 -0800175 rc = ioctl(fd, LOOP_GET_STATUS64, &li);
San Mehata19b2502010-01-06 10:33:53 -0800176 if (rc < 0 && errno == ENXIO)
177 break;
178
San Mehat8da6bcb2010-01-09 12:24:05 -0800179 close(fd);
180
San Mehata19b2502010-01-06 10:33:53 -0800181 if (rc < 0) {
San Mehat97ac40e2010-03-24 10:24:19 -0700182 SLOGE("Unable to get loop status for %s (%s)", filename,
San Mehata19b2502010-01-06 10:33:53 -0800183 strerror(errno));
184 return -1;
185 }
186 }
187
188 if (i == LOOP_MAX) {
San Mehat97ac40e2010-03-24 10:24:19 -0700189 SLOGE("Exhausted all loop devices");
San Mehata19b2502010-01-06 10:33:53 -0800190 errno = ENOSPC;
191 return -1;
192 }
San Mehata19b2502010-01-06 10:33:53 -0800193
San Mehat8da6bcb2010-01-09 12:24:05 -0800194 strncpy(loopDeviceBuffer, filename, len -1);
195
San Mehata19b2502010-01-06 10:33:53 -0800196 int file_fd;
197
San Mehata19b2502010-01-06 10:33:53 -0800198 if ((file_fd = open(loopFile, O_RDWR)) < 0) {
San Mehat97ac40e2010-03-24 10:24:19 -0700199 SLOGE("Unable to open %s (%s)", loopFile, strerror(errno));
San Mehata19b2502010-01-06 10:33:53 -0800200 close(fd);
201 return -1;
202 }
203
204 if (ioctl(fd, LOOP_SET_FD, file_fd) < 0) {
San Mehat97ac40e2010-03-24 10:24:19 -0700205 SLOGE("Error setting up loopback interface (%s)", strerror(errno));
San Mehata19b2502010-01-06 10:33:53 -0800206 close(file_fd);
207 close(fd);
208 return -1;
209 }
210
Kenny Root508c0e12010-07-12 09:59:49 -0700211 struct loop_info64 li;
San Mehata19b2502010-01-06 10:33:53 -0800212
213 memset(&li, 0, sizeof(li));
Peter Bohm092aa1c2011-04-01 12:35:25 +0200214 strlcpy((char*) li.lo_crypt_name, id, LO_NAME_SIZE);
215 strlcpy((char*) li.lo_file_name, loopFile, LO_NAME_SIZE);
San Mehata19b2502010-01-06 10:33:53 -0800216
Kenny Root508c0e12010-07-12 09:59:49 -0700217 if (ioctl(fd, LOOP_SET_STATUS64, &li) < 0) {
San Mehat97ac40e2010-03-24 10:24:19 -0700218 SLOGE("Error setting loopback status (%s)", strerror(errno));
San Mehata19b2502010-01-06 10:33:53 -0800219 close(file_fd);
220 close(fd);
221 return -1;
222 }
223
224 close(fd);
225 close(file_fd);
226
227 return 0;
228}
229
230int Loop::destroyByDevice(const char *loopDevice) {
231 int device_fd;
232
233 device_fd = open(loopDevice, O_RDONLY);
234 if (device_fd < 0) {
San Mehat97ac40e2010-03-24 10:24:19 -0700235 SLOGE("Failed to open loop (%d)", errno);
San Mehata19b2502010-01-06 10:33:53 -0800236 return -1;
237 }
238
239 if (ioctl(device_fd, LOOP_CLR_FD, 0) < 0) {
San Mehat97ac40e2010-03-24 10:24:19 -0700240 SLOGE("Failed to destroy loop (%d)", errno);
San Mehata19b2502010-01-06 10:33:53 -0800241 close(device_fd);
242 return -1;
243 }
244
245 close(device_fd);
246 return 0;
247}
248
Mark Salyzyn3e971272014-01-21 13:27:04 -0800249int Loop::destroyByFile(const char * /*loopFile*/) {
San Mehata19b2502010-01-06 10:33:53 -0800250 errno = ENOSYS;
251 return -1;
252}
253
San Mehat8b8f71b2010-01-11 09:17:25 -0800254int Loop::createImageFile(const char *file, unsigned int numSectors) {
San Mehata19b2502010-01-06 10:33:53 -0800255 int fd;
256
San Mehata19b2502010-01-06 10:33:53 -0800257 if ((fd = creat(file, 0600)) < 0) {
San Mehat97ac40e2010-03-24 10:24:19 -0700258 SLOGE("Error creating imagefile (%s)", strerror(errno));
San Mehata19b2502010-01-06 10:33:53 -0800259 return -1;
260 }
261
San Mehat8b8f71b2010-01-11 09:17:25 -0800262 if (ftruncate(fd, numSectors * 512) < 0) {
San Mehat97ac40e2010-03-24 10:24:19 -0700263 SLOGE("Error truncating imagefile (%s)", strerror(errno));
San Mehata19b2502010-01-06 10:33:53 -0800264 close(fd);
265 return -1;
266 }
267 close(fd);
268 return 0;
269}
Kenny Root344ca102012-04-03 17:23:01 -0700270
Daniel Rosenbergfcd34a02014-05-22 11:23:56 -0700271int Loop::resizeImageFile(const char *file, unsigned int numSectors) {
272 int fd;
273
274 if ((fd = open(file, O_RDWR)) < 0) {
275 SLOGE("Error opening imagefile (%s)", strerror(errno));
276 return -1;
277 }
278
279 SLOGD("Attempting to increase size of %s to %d sectors.", file, numSectors);
280
281 if (fallocate(fd, 0, 0, numSectors * 512)) {
Jeff Sharkey43ed1232014-08-22 12:29:05 -0700282 if (errno == ENOSYS || errno == ENOTSUP) {
Daniel Rosenbergfcd34a02014-05-22 11:23:56 -0700283 SLOGW("fallocate not found. Falling back to ftruncate.");
284 if (ftruncate(fd, numSectors * 512) < 0) {
285 SLOGE("Error truncating imagefile (%s)", strerror(errno));
286 close(fd);
287 return -1;
288 }
289 } else {
290 SLOGE("Error allocating space (%s)", strerror(errno));
291 close(fd);
292 return -1;
293 }
294 }
295 close(fd);
296 return 0;
297}
298
Kenny Root344ca102012-04-03 17:23:01 -0700299int Loop::lookupInfo(const char *loopDevice, struct asec_superblock *sb, unsigned int *nr_sec) {
300 int fd;
301 struct asec_superblock buffer;
302
303 if ((fd = open(loopDevice, O_RDONLY)) < 0) {
304 SLOGE("Failed to open loopdevice (%s)", strerror(errno));
305 destroyByDevice(loopDevice);
306 return -1;
307 }
308
309 if (ioctl(fd, BLKGETSIZE, nr_sec)) {
310 SLOGE("Failed to get loop size (%s)", strerror(errno));
311 destroyByDevice(loopDevice);
312 close(fd);
313 return -1;
314 }
315
316 /*
317 * Try to read superblock.
318 */
319 memset(&buffer, 0, sizeof(struct asec_superblock));
320 if (lseek(fd, ((*nr_sec - 1) * 512), SEEK_SET) < 0) {
321 SLOGE("lseek failed (%s)", strerror(errno));
322 close(fd);
323 destroyByDevice(loopDevice);
324 return -1;
325 }
326 if (read(fd, &buffer, sizeof(struct asec_superblock)) != sizeof(struct asec_superblock)) {
327 SLOGE("superblock read failed (%s)", strerror(errno));
328 close(fd);
329 destroyByDevice(loopDevice);
330 return -1;
331 }
332 close(fd);
333
334 /*
335 * Superblock successfully read. Copy to caller's struct.
336 */
337 memcpy(sb, &buffer, sizeof(struct asec_superblock));
338 return 0;
339}