blob: dad2c3f34e9363b37cf693b18da44b69ff282da0 [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
San Mehat8da6bcb2010-01-09 12:24:05 -080024#include <sys/types.h>
25#include <sys/stat.h>
Olivier Bailly37dcda62010-11-16 10:41:53 -080026#include <sys/ioctl.h>
San Mehat8da6bcb2010-01-09 12:24:05 -080027
San Mehatd9a4e352010-03-12 13:32:47 -080028#include <linux/kdev_t.h>
29
San Mehata19b2502010-01-06 10:33:53 -080030#define LOG_TAG "Vold"
31
32#include <cutils/log.h>
33
San Mehatd9a4e352010-03-12 13:32:47 -080034#include <sysutils/SocketClient.h>
San Mehata19b2502010-01-06 10:33:53 -080035#include "Loop.h"
36
San Mehatd9a4e352010-03-12 13:32:47 -080037int Loop::dumpState(SocketClient *c) {
38 int i;
39 int fd;
40 char filename[256];
41
42 for (i = 0; i < LOOP_MAX; i++) {
Kenny Root508c0e12010-07-12 09:59:49 -070043 struct loop_info64 li;
San Mehatd9a4e352010-03-12 13:32:47 -080044 int rc;
45
46 sprintf(filename, "/dev/block/loop%d", i);
47
48 if ((fd = open(filename, O_RDWR)) < 0) {
49 if (errno != ENOENT) {
San Mehat97ac40e2010-03-24 10:24:19 -070050 SLOGE("Unable to open %s (%s)", filename, strerror(errno));
San Mehatd9a4e352010-03-12 13:32:47 -080051 } else {
52 continue;
53 }
54 return -1;
55 }
56
Kenny Root508c0e12010-07-12 09:59:49 -070057 rc = ioctl(fd, LOOP_GET_STATUS64, &li);
San Mehatd9a4e352010-03-12 13:32:47 -080058 close(fd);
59 if (rc < 0 && errno == ENXIO) {
60 continue;
61 }
62
63 if (rc < 0) {
San Mehat97ac40e2010-03-24 10:24:19 -070064 SLOGE("Unable to get loop status for %s (%s)", filename,
San Mehatd9a4e352010-03-12 13:32:47 -080065 strerror(errno));
66 return -1;
67 }
68 char *tmp = NULL;
Kenny Root508c0e12010-07-12 09:59:49 -070069 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 -080070 MAJOR(li.lo_device), MINOR(li.lo_device), li.lo_inode, MAJOR(li.lo_rdevice),
Kenny Root508c0e12010-07-12 09:59:49 -070071 MINOR(li.lo_rdevice), li.lo_offset, li.lo_flags, li.lo_crypt_name,
72 li.lo_file_name);
San Mehatd9a4e352010-03-12 13:32:47 -080073 c->sendMsg(0, tmp, false);
74 free(tmp);
75 }
76 return 0;
77}
78
79int Loop::lookupActive(const char *id, char *buffer, size_t len) {
San Mehata19b2502010-01-06 10:33:53 -080080 int i;
81 int fd;
82 char filename[256];
83
84 memset(buffer, 0, len);
85
86 for (i = 0; i < LOOP_MAX; i++) {
Kenny Root508c0e12010-07-12 09:59:49 -070087 struct loop_info64 li;
San Mehata19b2502010-01-06 10:33:53 -080088 int rc;
89
90 sprintf(filename, "/dev/block/loop%d", i);
91
92 if ((fd = open(filename, O_RDWR)) < 0) {
San Mehatb78a32c2010-01-10 13:02:12 -080093 if (errno != ENOENT) {
San Mehat97ac40e2010-03-24 10:24:19 -070094 SLOGE("Unable to open %s (%s)", filename, strerror(errno));
San Mehatd9a4e352010-03-12 13:32:47 -080095 } else {
96 continue;
San Mehatb78a32c2010-01-10 13:02:12 -080097 }
San Mehata19b2502010-01-06 10:33:53 -080098 return -1;
99 }
100
Kenny Root508c0e12010-07-12 09:59:49 -0700101 rc = ioctl(fd, LOOP_GET_STATUS64, &li);
San Mehata19b2502010-01-06 10:33:53 -0800102 close(fd);
103 if (rc < 0 && errno == ENXIO) {
104 continue;
San Mehata19b2502010-01-06 10:33:53 -0800105 }
106
107 if (rc < 0) {
San Mehat97ac40e2010-03-24 10:24:19 -0700108 SLOGE("Unable to get loop status for %s (%s)", filename,
San Mehata19b2502010-01-06 10:33:53 -0800109 strerror(errno));
110 return -1;
111 }
Kenny Root508c0e12010-07-12 09:59:49 -0700112 if (!strncmp((const char*) li.lo_crypt_name, id, LO_NAME_SIZE)) {
San Mehata19b2502010-01-06 10:33:53 -0800113 break;
114 }
115 }
116
117 if (i == LOOP_MAX) {
118 errno = ENOENT;
119 return -1;
120 }
121 strncpy(buffer, filename, len -1);
122 return 0;
123}
124
San Mehatd9a4e352010-03-12 13:32:47 -0800125int Loop::create(const char *id, const char *loopFile, char *loopDeviceBuffer, size_t len) {
San Mehata19b2502010-01-06 10:33:53 -0800126 int i;
127 int fd;
128 char filename[256];
129
San Mehata19b2502010-01-06 10:33:53 -0800130 for (i = 0; i < LOOP_MAX; i++) {
Kenny Root7c165022011-02-01 15:58:25 -0800131 struct loop_info64 li;
San Mehata19b2502010-01-06 10:33:53 -0800132 int rc;
133
134 sprintf(filename, "/dev/block/loop%d", i);
135
San Mehat8da6bcb2010-01-09 12:24:05 -0800136 /*
137 * The kernel starts us off with 8 loop nodes, but more
138 * are created on-demand if needed.
139 */
140 mode_t mode = 0660 | S_IFBLK;
San Mehatb78a32c2010-01-10 13:02:12 -0800141 unsigned int dev = (0xff & i) | ((i << 12) & 0xfff00000) | (7 << 8);
San Mehat8da6bcb2010-01-09 12:24:05 -0800142 if (mknod(filename, mode, dev) < 0) {
143 if (errno != EEXIST) {
San Mehat97ac40e2010-03-24 10:24:19 -0700144 SLOGE("Error creating loop device node (%s)", strerror(errno));
San Mehat8da6bcb2010-01-09 12:24:05 -0800145 return -1;
146 }
147 }
148
San Mehata19b2502010-01-06 10:33:53 -0800149 if ((fd = open(filename, O_RDWR)) < 0) {
San Mehat97ac40e2010-03-24 10:24:19 -0700150 SLOGE("Unable to open %s (%s)", filename, strerror(errno));
San Mehata19b2502010-01-06 10:33:53 -0800151 return -1;
152 }
153
Kenny Root7c165022011-02-01 15:58:25 -0800154 rc = ioctl(fd, LOOP_GET_STATUS64, &li);
San Mehata19b2502010-01-06 10:33:53 -0800155 if (rc < 0 && errno == ENXIO)
156 break;
157
San Mehat8da6bcb2010-01-09 12:24:05 -0800158 close(fd);
159
San Mehata19b2502010-01-06 10:33:53 -0800160 if (rc < 0) {
San Mehat97ac40e2010-03-24 10:24:19 -0700161 SLOGE("Unable to get loop status for %s (%s)", filename,
San Mehata19b2502010-01-06 10:33:53 -0800162 strerror(errno));
163 return -1;
164 }
165 }
166
167 if (i == LOOP_MAX) {
San Mehat97ac40e2010-03-24 10:24:19 -0700168 SLOGE("Exhausted all loop devices");
San Mehata19b2502010-01-06 10:33:53 -0800169 errno = ENOSPC;
170 return -1;
171 }
San Mehata19b2502010-01-06 10:33:53 -0800172
San Mehat8da6bcb2010-01-09 12:24:05 -0800173 strncpy(loopDeviceBuffer, filename, len -1);
174
San Mehata19b2502010-01-06 10:33:53 -0800175 int file_fd;
176
San Mehata19b2502010-01-06 10:33:53 -0800177 if ((file_fd = open(loopFile, O_RDWR)) < 0) {
San Mehat97ac40e2010-03-24 10:24:19 -0700178 SLOGE("Unable to open %s (%s)", loopFile, strerror(errno));
San Mehata19b2502010-01-06 10:33:53 -0800179 close(fd);
180 return -1;
181 }
182
183 if (ioctl(fd, LOOP_SET_FD, file_fd) < 0) {
San Mehat97ac40e2010-03-24 10:24:19 -0700184 SLOGE("Error setting up loopback interface (%s)", strerror(errno));
San Mehata19b2502010-01-06 10:33:53 -0800185 close(file_fd);
186 close(fd);
187 return -1;
188 }
189
Kenny Root508c0e12010-07-12 09:59:49 -0700190 struct loop_info64 li;
San Mehata19b2502010-01-06 10:33:53 -0800191
192 memset(&li, 0, sizeof(li));
Peter Bohm092aa1c2011-04-01 12:35:25 +0200193 strlcpy((char*) li.lo_crypt_name, id, LO_NAME_SIZE);
194 strlcpy((char*) li.lo_file_name, loopFile, LO_NAME_SIZE);
San Mehata19b2502010-01-06 10:33:53 -0800195
Kenny Root508c0e12010-07-12 09:59:49 -0700196 if (ioctl(fd, LOOP_SET_STATUS64, &li) < 0) {
San Mehat97ac40e2010-03-24 10:24:19 -0700197 SLOGE("Error setting loopback status (%s)", strerror(errno));
San Mehata19b2502010-01-06 10:33:53 -0800198 close(file_fd);
199 close(fd);
200 return -1;
201 }
202
203 close(fd);
204 close(file_fd);
205
206 return 0;
207}
208
209int Loop::destroyByDevice(const char *loopDevice) {
210 int device_fd;
211
212 device_fd = open(loopDevice, O_RDONLY);
213 if (device_fd < 0) {
San Mehat97ac40e2010-03-24 10:24:19 -0700214 SLOGE("Failed to open loop (%d)", errno);
San Mehata19b2502010-01-06 10:33:53 -0800215 return -1;
216 }
217
218 if (ioctl(device_fd, LOOP_CLR_FD, 0) < 0) {
San Mehat97ac40e2010-03-24 10:24:19 -0700219 SLOGE("Failed to destroy loop (%d)", errno);
San Mehata19b2502010-01-06 10:33:53 -0800220 close(device_fd);
221 return -1;
222 }
223
224 close(device_fd);
225 return 0;
226}
227
228int Loop::destroyByFile(const char *loopFile) {
229 errno = ENOSYS;
230 return -1;
231}
232
San Mehat8b8f71b2010-01-11 09:17:25 -0800233int Loop::createImageFile(const char *file, unsigned int numSectors) {
San Mehata19b2502010-01-06 10:33:53 -0800234 int fd;
235
San Mehata19b2502010-01-06 10:33:53 -0800236 if ((fd = creat(file, 0600)) < 0) {
San Mehat97ac40e2010-03-24 10:24:19 -0700237 SLOGE("Error creating imagefile (%s)", strerror(errno));
San Mehata19b2502010-01-06 10:33:53 -0800238 return -1;
239 }
240
San Mehat8b8f71b2010-01-11 09:17:25 -0800241 if (ftruncate(fd, numSectors * 512) < 0) {
San Mehat97ac40e2010-03-24 10:24:19 -0700242 SLOGE("Error truncating imagefile (%s)", strerror(errno));
San Mehata19b2502010-01-06 10:33:53 -0800243 close(fd);
244 return -1;
245 }
246 close(fd);
247 return 0;
248}