blob: 11c114fe5170b54901d7a69c16a17515dfb32d94 [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"
San Mehata19b2502010-01-06 10:33:53 -080038
San Mehatd9a4e352010-03-12 13:32:47 -080039int Loop::dumpState(SocketClient *c) {
40 int i;
41 int fd;
42 char filename[256];
43
44 for (i = 0; i < LOOP_MAX; i++) {
Kenny Root508c0e12010-07-12 09:59:49 -070045 struct loop_info64 li;
San Mehatd9a4e352010-03-12 13:32:47 -080046 int rc;
47
48 sprintf(filename, "/dev/block/loop%d", i);
49
50 if ((fd = open(filename, O_RDWR)) < 0) {
51 if (errno != ENOENT) {
San Mehat97ac40e2010-03-24 10:24:19 -070052 SLOGE("Unable to open %s (%s)", filename, strerror(errno));
San Mehatd9a4e352010-03-12 13:32:47 -080053 } else {
54 continue;
55 }
56 return -1;
57 }
58
Kenny Root508c0e12010-07-12 09:59:49 -070059 rc = ioctl(fd, LOOP_GET_STATUS64, &li);
San Mehatd9a4e352010-03-12 13:32:47 -080060 close(fd);
61 if (rc < 0 && errno == ENXIO) {
62 continue;
63 }
64
65 if (rc < 0) {
San Mehat97ac40e2010-03-24 10:24:19 -070066 SLOGE("Unable to get loop status for %s (%s)", filename,
San Mehatd9a4e352010-03-12 13:32:47 -080067 strerror(errno));
68 return -1;
69 }
70 char *tmp = NULL;
Kenny Root508c0e12010-07-12 09:59:49 -070071 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 -080072 MAJOR(li.lo_device), MINOR(li.lo_device), li.lo_inode, MAJOR(li.lo_rdevice),
Kenny Root508c0e12010-07-12 09:59:49 -070073 MINOR(li.lo_rdevice), li.lo_offset, li.lo_flags, li.lo_crypt_name,
74 li.lo_file_name);
San Mehatd9a4e352010-03-12 13:32:47 -080075 c->sendMsg(0, tmp, false);
76 free(tmp);
77 }
78 return 0;
79}
80
81int Loop::lookupActive(const char *id, char *buffer, size_t len) {
San Mehata19b2502010-01-06 10:33:53 -080082 int i;
83 int fd;
84 char filename[256];
85
86 memset(buffer, 0, len);
87
88 for (i = 0; i < LOOP_MAX; i++) {
Kenny Root508c0e12010-07-12 09:59:49 -070089 struct loop_info64 li;
San Mehata19b2502010-01-06 10:33:53 -080090 int rc;
91
92 sprintf(filename, "/dev/block/loop%d", i);
93
94 if ((fd = open(filename, O_RDWR)) < 0) {
San Mehatb78a32c2010-01-10 13:02:12 -080095 if (errno != ENOENT) {
San Mehat97ac40e2010-03-24 10:24:19 -070096 SLOGE("Unable to open %s (%s)", filename, strerror(errno));
San Mehatd9a4e352010-03-12 13:32:47 -080097 } else {
98 continue;
San Mehatb78a32c2010-01-10 13:02:12 -080099 }
San Mehata19b2502010-01-06 10:33:53 -0800100 return -1;
101 }
102
Kenny Root508c0e12010-07-12 09:59:49 -0700103 rc = ioctl(fd, LOOP_GET_STATUS64, &li);
San Mehata19b2502010-01-06 10:33:53 -0800104 close(fd);
105 if (rc < 0 && errno == ENXIO) {
106 continue;
San Mehata19b2502010-01-06 10:33:53 -0800107 }
108
109 if (rc < 0) {
San Mehat97ac40e2010-03-24 10:24:19 -0700110 SLOGE("Unable to get loop status for %s (%s)", filename,
San Mehata19b2502010-01-06 10:33:53 -0800111 strerror(errno));
112 return -1;
113 }
Kenny Root508c0e12010-07-12 09:59:49 -0700114 if (!strncmp((const char*) li.lo_crypt_name, id, LO_NAME_SIZE)) {
San Mehata19b2502010-01-06 10:33:53 -0800115 break;
116 }
117 }
118
119 if (i == LOOP_MAX) {
120 errno = ENOENT;
121 return -1;
122 }
123 strncpy(buffer, filename, len -1);
124 return 0;
125}
126
San Mehatd9a4e352010-03-12 13:32:47 -0800127int Loop::create(const char *id, const char *loopFile, char *loopDeviceBuffer, size_t len) {
San Mehata19b2502010-01-06 10:33:53 -0800128 int i;
129 int fd;
130 char filename[256];
131
San Mehata19b2502010-01-06 10:33:53 -0800132 for (i = 0; i < LOOP_MAX; i++) {
Kenny Root7c165022011-02-01 15:58:25 -0800133 struct loop_info64 li;
San Mehata19b2502010-01-06 10:33:53 -0800134 int rc;
135
136 sprintf(filename, "/dev/block/loop%d", i);
137
San Mehat8da6bcb2010-01-09 12:24:05 -0800138 /*
139 * The kernel starts us off with 8 loop nodes, but more
140 * are created on-demand if needed.
141 */
142 mode_t mode = 0660 | S_IFBLK;
San Mehatb78a32c2010-01-10 13:02:12 -0800143 unsigned int dev = (0xff & i) | ((i << 12) & 0xfff00000) | (7 << 8);
San Mehat8da6bcb2010-01-09 12:24:05 -0800144 if (mknod(filename, mode, dev) < 0) {
145 if (errno != EEXIST) {
San Mehat97ac40e2010-03-24 10:24:19 -0700146 SLOGE("Error creating loop device node (%s)", strerror(errno));
San Mehat8da6bcb2010-01-09 12:24:05 -0800147 return -1;
148 }
149 }
150
San Mehata19b2502010-01-06 10:33:53 -0800151 if ((fd = open(filename, O_RDWR)) < 0) {
San Mehat97ac40e2010-03-24 10:24:19 -0700152 SLOGE("Unable to open %s (%s)", filename, strerror(errno));
San Mehata19b2502010-01-06 10:33:53 -0800153 return -1;
154 }
155
Kenny Root7c165022011-02-01 15:58:25 -0800156 rc = ioctl(fd, LOOP_GET_STATUS64, &li);
San Mehata19b2502010-01-06 10:33:53 -0800157 if (rc < 0 && errno == ENXIO)
158 break;
159
San Mehat8da6bcb2010-01-09 12:24:05 -0800160 close(fd);
161
San Mehata19b2502010-01-06 10:33:53 -0800162 if (rc < 0) {
San Mehat97ac40e2010-03-24 10:24:19 -0700163 SLOGE("Unable to get loop status for %s (%s)", filename,
San Mehata19b2502010-01-06 10:33:53 -0800164 strerror(errno));
165 return -1;
166 }
167 }
168
169 if (i == LOOP_MAX) {
San Mehat97ac40e2010-03-24 10:24:19 -0700170 SLOGE("Exhausted all loop devices");
San Mehata19b2502010-01-06 10:33:53 -0800171 errno = ENOSPC;
172 return -1;
173 }
San Mehata19b2502010-01-06 10:33:53 -0800174
San Mehat8da6bcb2010-01-09 12:24:05 -0800175 strncpy(loopDeviceBuffer, filename, len -1);
176
San Mehata19b2502010-01-06 10:33:53 -0800177 int file_fd;
178
San Mehata19b2502010-01-06 10:33:53 -0800179 if ((file_fd = open(loopFile, O_RDWR)) < 0) {
San Mehat97ac40e2010-03-24 10:24:19 -0700180 SLOGE("Unable to open %s (%s)", loopFile, strerror(errno));
San Mehata19b2502010-01-06 10:33:53 -0800181 close(fd);
182 return -1;
183 }
184
185 if (ioctl(fd, LOOP_SET_FD, file_fd) < 0) {
San Mehat97ac40e2010-03-24 10:24:19 -0700186 SLOGE("Error setting up loopback interface (%s)", strerror(errno));
San Mehata19b2502010-01-06 10:33:53 -0800187 close(file_fd);
188 close(fd);
189 return -1;
190 }
191
Kenny Root508c0e12010-07-12 09:59:49 -0700192 struct loop_info64 li;
San Mehata19b2502010-01-06 10:33:53 -0800193
194 memset(&li, 0, sizeof(li));
Peter Bohm092aa1c2011-04-01 12:35:25 +0200195 strlcpy((char*) li.lo_crypt_name, id, LO_NAME_SIZE);
196 strlcpy((char*) li.lo_file_name, loopFile, LO_NAME_SIZE);
San Mehata19b2502010-01-06 10:33:53 -0800197
Kenny Root508c0e12010-07-12 09:59:49 -0700198 if (ioctl(fd, LOOP_SET_STATUS64, &li) < 0) {
San Mehat97ac40e2010-03-24 10:24:19 -0700199 SLOGE("Error setting loopback status (%s)", strerror(errno));
San Mehata19b2502010-01-06 10:33:53 -0800200 close(file_fd);
201 close(fd);
202 return -1;
203 }
204
205 close(fd);
206 close(file_fd);
207
208 return 0;
209}
210
211int Loop::destroyByDevice(const char *loopDevice) {
212 int device_fd;
213
214 device_fd = open(loopDevice, O_RDONLY);
215 if (device_fd < 0) {
San Mehat97ac40e2010-03-24 10:24:19 -0700216 SLOGE("Failed to open loop (%d)", errno);
San Mehata19b2502010-01-06 10:33:53 -0800217 return -1;
218 }
219
220 if (ioctl(device_fd, LOOP_CLR_FD, 0) < 0) {
San Mehat97ac40e2010-03-24 10:24:19 -0700221 SLOGE("Failed to destroy loop (%d)", errno);
San Mehata19b2502010-01-06 10:33:53 -0800222 close(device_fd);
223 return -1;
224 }
225
226 close(device_fd);
227 return 0;
228}
229
Mark Salyzyn3e971272014-01-21 13:27:04 -0800230int Loop::destroyByFile(const char * /*loopFile*/) {
San Mehata19b2502010-01-06 10:33:53 -0800231 errno = ENOSYS;
232 return -1;
233}
234
San Mehat8b8f71b2010-01-11 09:17:25 -0800235int Loop::createImageFile(const char *file, unsigned int numSectors) {
San Mehata19b2502010-01-06 10:33:53 -0800236 int fd;
237
San Mehata19b2502010-01-06 10:33:53 -0800238 if ((fd = creat(file, 0600)) < 0) {
San Mehat97ac40e2010-03-24 10:24:19 -0700239 SLOGE("Error creating imagefile (%s)", strerror(errno));
San Mehata19b2502010-01-06 10:33:53 -0800240 return -1;
241 }
242
San Mehat8b8f71b2010-01-11 09:17:25 -0800243 if (ftruncate(fd, numSectors * 512) < 0) {
San Mehat97ac40e2010-03-24 10:24:19 -0700244 SLOGE("Error truncating imagefile (%s)", strerror(errno));
San Mehata19b2502010-01-06 10:33:53 -0800245 close(fd);
246 return -1;
247 }
248 close(fd);
249 return 0;
250}
Kenny Root344ca102012-04-03 17:23:01 -0700251
Daniel Rosenbergfcd34a02014-05-22 11:23:56 -0700252int Loop::resizeImageFile(const char *file, unsigned int numSectors) {
253 int fd;
254
255 if ((fd = open(file, O_RDWR)) < 0) {
256 SLOGE("Error opening imagefile (%s)", strerror(errno));
257 return -1;
258 }
259
260 SLOGD("Attempting to increase size of %s to %d sectors.", file, numSectors);
261
262 if (fallocate(fd, 0, 0, numSectors * 512)) {
Jeff Sharkey43ed1232014-08-22 12:29:05 -0700263 if (errno == ENOSYS || errno == ENOTSUP) {
Daniel Rosenbergfcd34a02014-05-22 11:23:56 -0700264 SLOGW("fallocate not found. Falling back to ftruncate.");
265 if (ftruncate(fd, numSectors * 512) < 0) {
266 SLOGE("Error truncating imagefile (%s)", strerror(errno));
267 close(fd);
268 return -1;
269 }
270 } else {
271 SLOGE("Error allocating space (%s)", strerror(errno));
272 close(fd);
273 return -1;
274 }
275 }
276 close(fd);
277 return 0;
278}
279
Kenny Root344ca102012-04-03 17:23:01 -0700280int Loop::lookupInfo(const char *loopDevice, struct asec_superblock *sb, unsigned int *nr_sec) {
281 int fd;
282 struct asec_superblock buffer;
283
284 if ((fd = open(loopDevice, O_RDONLY)) < 0) {
285 SLOGE("Failed to open loopdevice (%s)", strerror(errno));
286 destroyByDevice(loopDevice);
287 return -1;
288 }
289
290 if (ioctl(fd, BLKGETSIZE, nr_sec)) {
291 SLOGE("Failed to get loop size (%s)", strerror(errno));
292 destroyByDevice(loopDevice);
293 close(fd);
294 return -1;
295 }
296
297 /*
298 * Try to read superblock.
299 */
300 memset(&buffer, 0, sizeof(struct asec_superblock));
301 if (lseek(fd, ((*nr_sec - 1) * 512), SEEK_SET) < 0) {
302 SLOGE("lseek failed (%s)", strerror(errno));
303 close(fd);
304 destroyByDevice(loopDevice);
305 return -1;
306 }
307 if (read(fd, &buffer, sizeof(struct asec_superblock)) != sizeof(struct asec_superblock)) {
308 SLOGE("superblock read failed (%s)", strerror(errno));
309 close(fd);
310 destroyByDevice(loopDevice);
311 return -1;
312 }
313 close(fd);
314
315 /*
316 * Superblock successfully read. Copy to caller's struct.
317 */
318 memcpy(sb, &buffer, sizeof(struct asec_superblock));
319 return 0;
320}