blob: 3f0ee1e3bef6f86dd4ccd5731f8d946682307169 [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
252int Loop::lookupInfo(const char *loopDevice, struct asec_superblock *sb, unsigned int *nr_sec) {
253 int fd;
254 struct asec_superblock buffer;
255
256 if ((fd = open(loopDevice, O_RDONLY)) < 0) {
257 SLOGE("Failed to open loopdevice (%s)", strerror(errno));
258 destroyByDevice(loopDevice);
259 return -1;
260 }
261
262 if (ioctl(fd, BLKGETSIZE, nr_sec)) {
263 SLOGE("Failed to get loop size (%s)", strerror(errno));
264 destroyByDevice(loopDevice);
265 close(fd);
266 return -1;
267 }
268
269 /*
270 * Try to read superblock.
271 */
272 memset(&buffer, 0, sizeof(struct asec_superblock));
273 if (lseek(fd, ((*nr_sec - 1) * 512), SEEK_SET) < 0) {
274 SLOGE("lseek failed (%s)", strerror(errno));
275 close(fd);
276 destroyByDevice(loopDevice);
277 return -1;
278 }
279 if (read(fd, &buffer, sizeof(struct asec_superblock)) != sizeof(struct asec_superblock)) {
280 SLOGE("superblock read failed (%s)", strerror(errno));
281 close(fd);
282 destroyByDevice(loopDevice);
283 return -1;
284 }
285 close(fd);
286
287 /*
288 * Superblock successfully read. Copy to caller's struct.
289 */
290 memcpy(sb, &buffer, sizeof(struct asec_superblock));
291 return 0;
292}