2 * Copyright (C) 2008 The Android Open Source Project
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
24 #include <sys/mount.h>
25 #include <sys/types.h>
27 #include <sys/ioctl.h>
29 #include <linux/kdev_t.h>
31 #define LOG_TAG "Vold"
33 #include <cutils/log.h>
35 #include <sysutils/SocketClient.h>
39 int Loop::dumpState(SocketClient *c) {
44 for (i = 0; i < LOOP_MAX; i++) {
45 struct loop_info64 li;
48 sprintf(filename, "/dev/block/loop%d", i);
50 if ((fd = open(filename, O_RDWR)) < 0) {
51 if (errno != ENOENT) {
52 SLOGE("Unable to open %s (%s)", filename, strerror(errno));
59 rc = ioctl(fd, LOOP_GET_STATUS64, &li);
61 if (rc < 0 && errno == ENXIO) {
66 SLOGE("Unable to get loop status for %s (%s)", filename,
71 asprintf(&tmp, "%s %d %lld:%lld %llu %lld:%lld %lld 0x%x {%s} {%s}", filename, li.lo_number,
72 MAJOR(li.lo_device), MINOR(li.lo_device), li.lo_inode, MAJOR(li.lo_rdevice),
73 MINOR(li.lo_rdevice), li.lo_offset, li.lo_flags, li.lo_crypt_name,
75 c->sendMsg(0, tmp, false);
81 int Loop::lookupActive(const char *id, char *buffer, size_t len) {
86 memset(buffer, 0, len);
88 for (i = 0; i < LOOP_MAX; i++) {
89 struct loop_info64 li;
92 sprintf(filename, "/dev/block/loop%d", i);
94 if ((fd = open(filename, O_RDWR)) < 0) {
95 if (errno != ENOENT) {
96 SLOGE("Unable to open %s (%s)", filename, strerror(errno));
103 rc = ioctl(fd, LOOP_GET_STATUS64, &li);
105 if (rc < 0 && errno == ENXIO) {
110 SLOGE("Unable to get loop status for %s (%s)", filename,
114 if (!strncmp((const char*) li.lo_crypt_name, id, LO_NAME_SIZE)) {
123 strncpy(buffer, filename, len -1);
127 int Loop::create(const char *id, const char *loopFile, char *loopDeviceBuffer, size_t len) {
132 for (i = 0; i < LOOP_MAX; i++) {
133 struct loop_info64 li;
136 sprintf(filename, "/dev/block/loop%d", i);
139 * The kernel starts us off with 8 loop nodes, but more
140 * are created on-demand if needed.
142 mode_t mode = 0660 | S_IFBLK;
143 unsigned int dev = (0xff & i) | ((i << 12) & 0xfff00000) | (7 << 8);
144 if (mknod(filename, mode, dev) < 0) {
145 if (errno != EEXIST) {
146 SLOGE("Error creating loop device node (%s)", strerror(errno));
151 if ((fd = open(filename, O_RDWR)) < 0) {
152 SLOGE("Unable to open %s (%s)", filename, strerror(errno));
156 rc = ioctl(fd, LOOP_GET_STATUS64, &li);
157 if (rc < 0 && errno == ENXIO)
163 SLOGE("Unable to get loop status for %s (%s)", filename,
170 SLOGE("Exhausted all loop devices");
175 strncpy(loopDeviceBuffer, filename, len -1);
179 if ((file_fd = open(loopFile, O_RDWR)) < 0) {
180 SLOGE("Unable to open %s (%s)", loopFile, strerror(errno));
185 if (ioctl(fd, LOOP_SET_FD, file_fd) < 0) {
186 SLOGE("Error setting up loopback interface (%s)", strerror(errno));
192 struct loop_info64 li;
194 memset(&li, 0, sizeof(li));
195 strlcpy((char*) li.lo_crypt_name, id, LO_NAME_SIZE);
196 strlcpy((char*) li.lo_file_name, loopFile, LO_NAME_SIZE);
198 if (ioctl(fd, LOOP_SET_STATUS64, &li) < 0) {
199 SLOGE("Error setting loopback status (%s)", strerror(errno));
211 int Loop::destroyByDevice(const char *loopDevice) {
214 device_fd = open(loopDevice, O_RDONLY);
216 SLOGE("Failed to open loop (%d)", errno);
220 if (ioctl(device_fd, LOOP_CLR_FD, 0) < 0) {
221 SLOGE("Failed to destroy loop (%d)", errno);
230 int Loop::destroyByFile(const char *loopFile) {
235 int Loop::createImageFile(const char *file, unsigned int numSectors) {
238 if ((fd = creat(file, 0600)) < 0) {
239 SLOGE("Error creating imagefile (%s)", strerror(errno));
243 if (ftruncate(fd, numSectors * 512) < 0) {
244 SLOGE("Error truncating imagefile (%s)", strerror(errno));
252 int Loop::lookupInfo(const char *loopDevice, struct asec_superblock *sb, unsigned int *nr_sec) {
254 struct asec_superblock buffer;
256 if ((fd = open(loopDevice, O_RDONLY)) < 0) {
257 SLOGE("Failed to open loopdevice (%s)", strerror(errno));
258 destroyByDevice(loopDevice);
262 if (ioctl(fd, BLKGETSIZE, nr_sec)) {
263 SLOGE("Failed to get loop size (%s)", strerror(errno));
264 destroyByDevice(loopDevice);
270 * Try to read superblock.
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));
276 destroyByDevice(loopDevice);
279 if (read(fd, &buffer, sizeof(struct asec_superblock)) != sizeof(struct asec_superblock)) {
280 SLOGE("superblock read failed (%s)", strerror(errno));
282 destroyByDevice(loopDevice);
288 * Superblock successfully read. Copy to caller's struct.
290 memcpy(sb, &buffer, sizeof(struct asec_superblock));