blob: 98015e2d52045117ebe46fd4cbe638840232d24a [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>
18#include <fcntl.h>
19#include <unistd.h>
20#include <errno.h>
21#include <string.h>
22
San Mehat8da6bcb2010-01-09 12:24:05 -080023#include <sys/types.h>
24#include <sys/stat.h>
25
San Mehatd9a4e352010-03-12 13:32:47 -080026#include <linux/kdev_t.h>
27
San Mehata19b2502010-01-06 10:33:53 -080028#define LOG_TAG "Vold"
29
30#include <cutils/log.h>
31
San Mehatd9a4e352010-03-12 13:32:47 -080032#include <sysutils/SocketClient.h>
San Mehata19b2502010-01-06 10:33:53 -080033#include "Loop.h"
34
San Mehatd9a4e352010-03-12 13:32:47 -080035int Loop::dumpState(SocketClient *c) {
36 int i;
37 int fd;
38 char filename[256];
39
40 for (i = 0; i < LOOP_MAX; i++) {
Kenny Root508c0e12010-07-12 09:59:49 -070041 struct loop_info64 li;
San Mehatd9a4e352010-03-12 13:32:47 -080042 int rc;
43
44 sprintf(filename, "/dev/block/loop%d", i);
45
46 if ((fd = open(filename, O_RDWR)) < 0) {
47 if (errno != ENOENT) {
San Mehat97ac40e2010-03-24 10:24:19 -070048 SLOGE("Unable to open %s (%s)", filename, strerror(errno));
San Mehatd9a4e352010-03-12 13:32:47 -080049 } else {
50 continue;
51 }
52 return -1;
53 }
54
Kenny Root508c0e12010-07-12 09:59:49 -070055 rc = ioctl(fd, LOOP_GET_STATUS64, &li);
San Mehatd9a4e352010-03-12 13:32:47 -080056 close(fd);
57 if (rc < 0 && errno == ENXIO) {
58 continue;
59 }
60
61 if (rc < 0) {
San Mehat97ac40e2010-03-24 10:24:19 -070062 SLOGE("Unable to get loop status for %s (%s)", filename,
San Mehatd9a4e352010-03-12 13:32:47 -080063 strerror(errno));
64 return -1;
65 }
66 char *tmp = NULL;
Kenny Root508c0e12010-07-12 09:59:49 -070067 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 -080068 MAJOR(li.lo_device), MINOR(li.lo_device), li.lo_inode, MAJOR(li.lo_rdevice),
Kenny Root508c0e12010-07-12 09:59:49 -070069 MINOR(li.lo_rdevice), li.lo_offset, li.lo_flags, li.lo_crypt_name,
70 li.lo_file_name);
San Mehatd9a4e352010-03-12 13:32:47 -080071 c->sendMsg(0, tmp, false);
72 free(tmp);
73 }
74 return 0;
75}
76
77int Loop::lookupActive(const char *id, char *buffer, size_t len) {
San Mehata19b2502010-01-06 10:33:53 -080078 int i;
79 int fd;
80 char filename[256];
81
82 memset(buffer, 0, len);
83
84 for (i = 0; i < LOOP_MAX; i++) {
Kenny Root508c0e12010-07-12 09:59:49 -070085 struct loop_info64 li;
San Mehata19b2502010-01-06 10:33:53 -080086 int rc;
87
88 sprintf(filename, "/dev/block/loop%d", i);
89
90 if ((fd = open(filename, O_RDWR)) < 0) {
San Mehatb78a32c2010-01-10 13:02:12 -080091 if (errno != ENOENT) {
San Mehat97ac40e2010-03-24 10:24:19 -070092 SLOGE("Unable to open %s (%s)", filename, strerror(errno));
San Mehatd9a4e352010-03-12 13:32:47 -080093 } else {
94 continue;
San Mehatb78a32c2010-01-10 13:02:12 -080095 }
San Mehata19b2502010-01-06 10:33:53 -080096 return -1;
97 }
98
Kenny Root508c0e12010-07-12 09:59:49 -070099 rc = ioctl(fd, LOOP_GET_STATUS64, &li);
San Mehata19b2502010-01-06 10:33:53 -0800100 close(fd);
101 if (rc < 0 && errno == ENXIO) {
102 continue;
San Mehata19b2502010-01-06 10:33:53 -0800103 }
104
105 if (rc < 0) {
San Mehat97ac40e2010-03-24 10:24:19 -0700106 SLOGE("Unable to get loop status for %s (%s)", filename,
San Mehata19b2502010-01-06 10:33:53 -0800107 strerror(errno));
108 return -1;
109 }
Kenny Root508c0e12010-07-12 09:59:49 -0700110 if (!strncmp((const char*) li.lo_crypt_name, id, LO_NAME_SIZE)) {
San Mehata19b2502010-01-06 10:33:53 -0800111 break;
112 }
113 }
114
115 if (i == LOOP_MAX) {
116 errno = ENOENT;
117 return -1;
118 }
119 strncpy(buffer, filename, len -1);
120 return 0;
121}
122
San Mehatd9a4e352010-03-12 13:32:47 -0800123int Loop::create(const char *id, const char *loopFile, char *loopDeviceBuffer, size_t len) {
San Mehata19b2502010-01-06 10:33:53 -0800124 int i;
125 int fd;
126 char filename[256];
127
San Mehata19b2502010-01-06 10:33:53 -0800128 for (i = 0; i < LOOP_MAX; i++) {
129 struct loop_info li;
130 int rc;
131
132 sprintf(filename, "/dev/block/loop%d", i);
133
San Mehat8da6bcb2010-01-09 12:24:05 -0800134 /*
135 * The kernel starts us off with 8 loop nodes, but more
136 * are created on-demand if needed.
137 */
138 mode_t mode = 0660 | S_IFBLK;
San Mehatb78a32c2010-01-10 13:02:12 -0800139 unsigned int dev = (0xff & i) | ((i << 12) & 0xfff00000) | (7 << 8);
San Mehat8da6bcb2010-01-09 12:24:05 -0800140 if (mknod(filename, mode, dev) < 0) {
141 if (errno != EEXIST) {
San Mehat97ac40e2010-03-24 10:24:19 -0700142 SLOGE("Error creating loop device node (%s)", strerror(errno));
San Mehat8da6bcb2010-01-09 12:24:05 -0800143 return -1;
144 }
145 }
146
San Mehata19b2502010-01-06 10:33:53 -0800147 if ((fd = open(filename, O_RDWR)) < 0) {
San Mehat97ac40e2010-03-24 10:24:19 -0700148 SLOGE("Unable to open %s (%s)", filename, strerror(errno));
San Mehata19b2502010-01-06 10:33:53 -0800149 return -1;
150 }
151
Kenny Roote17e91f2010-07-16 15:04:55 -0700152 rc = ioctl(fd, LOOP_GET_STATUS, &li);
San Mehata19b2502010-01-06 10:33:53 -0800153 if (rc < 0 && errno == ENXIO)
154 break;
155
San Mehat8da6bcb2010-01-09 12:24:05 -0800156 close(fd);
157
San Mehata19b2502010-01-06 10:33:53 -0800158 if (rc < 0) {
San Mehat97ac40e2010-03-24 10:24:19 -0700159 SLOGE("Unable to get loop status for %s (%s)", filename,
San Mehata19b2502010-01-06 10:33:53 -0800160 strerror(errno));
161 return -1;
162 }
163 }
164
165 if (i == LOOP_MAX) {
San Mehat97ac40e2010-03-24 10:24:19 -0700166 SLOGE("Exhausted all loop devices");
San Mehata19b2502010-01-06 10:33:53 -0800167 errno = ENOSPC;
168 return -1;
169 }
San Mehata19b2502010-01-06 10:33:53 -0800170
San Mehat8da6bcb2010-01-09 12:24:05 -0800171 strncpy(loopDeviceBuffer, filename, len -1);
172
San Mehata19b2502010-01-06 10:33:53 -0800173 int file_fd;
174
San Mehata19b2502010-01-06 10:33:53 -0800175 if ((file_fd = open(loopFile, O_RDWR)) < 0) {
San Mehat97ac40e2010-03-24 10:24:19 -0700176 SLOGE("Unable to open %s (%s)", loopFile, strerror(errno));
San Mehata19b2502010-01-06 10:33:53 -0800177 close(fd);
178 return -1;
179 }
180
181 if (ioctl(fd, LOOP_SET_FD, file_fd) < 0) {
San Mehat97ac40e2010-03-24 10:24:19 -0700182 SLOGE("Error setting up loopback interface (%s)", strerror(errno));
San Mehata19b2502010-01-06 10:33:53 -0800183 close(file_fd);
184 close(fd);
185 return -1;
186 }
187
Kenny Root508c0e12010-07-12 09:59:49 -0700188 struct loop_info64 li;
San Mehata19b2502010-01-06 10:33:53 -0800189
190 memset(&li, 0, sizeof(li));
Kenny Root508c0e12010-07-12 09:59:49 -0700191 strncpy((char*) li.lo_crypt_name, id, LO_NAME_SIZE);
192 strncpy((char*) li.lo_file_name, loopFile, LO_NAME_SIZE);
San Mehata19b2502010-01-06 10:33:53 -0800193
Kenny Root508c0e12010-07-12 09:59:49 -0700194 if (ioctl(fd, LOOP_SET_STATUS64, &li) < 0) {
San Mehat97ac40e2010-03-24 10:24:19 -0700195 SLOGE("Error setting loopback status (%s)", strerror(errno));
San Mehata19b2502010-01-06 10:33:53 -0800196 close(file_fd);
197 close(fd);
198 return -1;
199 }
200
201 close(fd);
202 close(file_fd);
203
204 return 0;
205}
206
207int Loop::destroyByDevice(const char *loopDevice) {
208 int device_fd;
209
210 device_fd = open(loopDevice, O_RDONLY);
211 if (device_fd < 0) {
San Mehat97ac40e2010-03-24 10:24:19 -0700212 SLOGE("Failed to open loop (%d)", errno);
San Mehata19b2502010-01-06 10:33:53 -0800213 return -1;
214 }
215
216 if (ioctl(device_fd, LOOP_CLR_FD, 0) < 0) {
San Mehat97ac40e2010-03-24 10:24:19 -0700217 SLOGE("Failed to destroy loop (%d)", errno);
San Mehata19b2502010-01-06 10:33:53 -0800218 close(device_fd);
219 return -1;
220 }
221
222 close(device_fd);
223 return 0;
224}
225
226int Loop::destroyByFile(const char *loopFile) {
227 errno = ENOSYS;
228 return -1;
229}
230
San Mehat8b8f71b2010-01-11 09:17:25 -0800231int Loop::createImageFile(const char *file, unsigned int numSectors) {
San Mehata19b2502010-01-06 10:33:53 -0800232 int fd;
233
San Mehata19b2502010-01-06 10:33:53 -0800234 if ((fd = creat(file, 0600)) < 0) {
San Mehat97ac40e2010-03-24 10:24:19 -0700235 SLOGE("Error creating imagefile (%s)", strerror(errno));
San Mehata19b2502010-01-06 10:33:53 -0800236 return -1;
237 }
238
San Mehat8b8f71b2010-01-11 09:17:25 -0800239 if (ftruncate(fd, numSectors * 512) < 0) {
San Mehat97ac40e2010-03-24 10:24:19 -0700240 SLOGE("Error truncating imagefile (%s)", strerror(errno));
San Mehata19b2502010-01-06 10:33:53 -0800241 close(fd);
242 return -1;
243 }
244 close(fd);
245 return 0;
246}