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.
23 #include <sys/types.h>
26 #include <linux/kdev_t.h>
28 #define LOG_TAG "Vold"
30 #include <cutils/log.h>
32 #include <sysutils/SocketClient.h>
35 int Loop::dumpState(SocketClient *c) {
40 for (i = 0; i < LOOP_MAX; i++) {
41 struct loop_info64 li;
44 sprintf(filename, "/dev/block/loop%d", i);
46 if ((fd = open(filename, O_RDWR)) < 0) {
47 if (errno != ENOENT) {
48 SLOGE("Unable to open %s (%s)", filename, strerror(errno));
55 rc = ioctl(fd, LOOP_GET_STATUS64, &li);
57 if (rc < 0 && errno == ENXIO) {
62 SLOGE("Unable to get loop status for %s (%s)", filename,
67 asprintf(&tmp, "%s %d %lld:%lld %llu %lld:%lld %lld 0x%x {%s} {%s}", filename, li.lo_number,
68 MAJOR(li.lo_device), MINOR(li.lo_device), li.lo_inode, MAJOR(li.lo_rdevice),
69 MINOR(li.lo_rdevice), li.lo_offset, li.lo_flags, li.lo_crypt_name,
71 c->sendMsg(0, tmp, false);
77 int Loop::lookupActive(const char *id, char *buffer, size_t len) {
82 memset(buffer, 0, len);
84 for (i = 0; i < LOOP_MAX; i++) {
85 struct loop_info64 li;
88 sprintf(filename, "/dev/block/loop%d", i);
90 if ((fd = open(filename, O_RDWR)) < 0) {
91 if (errno != ENOENT) {
92 SLOGE("Unable to open %s (%s)", filename, strerror(errno));
99 rc = ioctl(fd, LOOP_GET_STATUS64, &li);
101 if (rc < 0 && errno == ENXIO) {
106 SLOGE("Unable to get loop status for %s (%s)", filename,
110 if (!strncmp((const char*) li.lo_crypt_name, id, LO_NAME_SIZE)) {
119 strncpy(buffer, filename, len -1);
123 int Loop::create(const char *id, const char *loopFile, char *loopDeviceBuffer, size_t len) {
128 for (i = 0; i < LOOP_MAX; i++) {
132 sprintf(filename, "/dev/block/loop%d", i);
135 * The kernel starts us off with 8 loop nodes, but more
136 * are created on-demand if needed.
138 mode_t mode = 0660 | S_IFBLK;
139 unsigned int dev = (0xff & i) | ((i << 12) & 0xfff00000) | (7 << 8);
140 if (mknod(filename, mode, dev) < 0) {
141 if (errno != EEXIST) {
142 SLOGE("Error creating loop device node (%s)", strerror(errno));
147 if ((fd = open(filename, O_RDWR)) < 0) {
148 SLOGE("Unable to open %s (%s)", filename, strerror(errno));
152 rc = ioctl(fd, LOOP_GET_STATUS, &li);
153 if (rc < 0 && errno == ENXIO)
159 SLOGE("Unable to get loop status for %s (%s)", filename,
166 SLOGE("Exhausted all loop devices");
171 strncpy(loopDeviceBuffer, filename, len -1);
175 if ((file_fd = open(loopFile, O_RDWR)) < 0) {
176 SLOGE("Unable to open %s (%s)", loopFile, strerror(errno));
181 if (ioctl(fd, LOOP_SET_FD, file_fd) < 0) {
182 SLOGE("Error setting up loopback interface (%s)", strerror(errno));
188 struct loop_info64 li;
190 memset(&li, 0, sizeof(li));
191 strncpy((char*) li.lo_crypt_name, id, LO_NAME_SIZE);
192 strncpy((char*) li.lo_file_name, loopFile, LO_NAME_SIZE);
194 if (ioctl(fd, LOOP_SET_STATUS64, &li) < 0) {
195 SLOGE("Error setting loopback status (%s)", strerror(errno));
207 int Loop::destroyByDevice(const char *loopDevice) {
210 device_fd = open(loopDevice, O_RDONLY);
212 SLOGE("Failed to open loop (%d)", errno);
216 if (ioctl(device_fd, LOOP_CLR_FD, 0) < 0) {
217 SLOGE("Failed to destroy loop (%d)", errno);
226 int Loop::destroyByFile(const char *loopFile) {
231 int Loop::createImageFile(const char *file, unsigned int numSectors) {
234 if ((fd = creat(file, 0600)) < 0) {
235 SLOGE("Error creating imagefile (%s)", strerror(errno));
239 if (ftruncate(fd, numSectors * 512) < 0) {
240 SLOGE("Error truncating imagefile (%s)", strerror(errno));