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/types.h>
26 #include <sys/ioctl.h>
28 #include <linux/kdev_t.h>
30 #define LOG_TAG "Vold"
32 #include <cutils/log.h>
34 #include <sysutils/SocketClient.h>
37 int Loop::dumpState(SocketClient *c) {
42 for (i = 0; i < LOOP_MAX; i++) {
43 struct loop_info64 li;
46 sprintf(filename, "/dev/block/loop%d", i);
48 if ((fd = open(filename, O_RDWR)) < 0) {
49 if (errno != ENOENT) {
50 SLOGE("Unable to open %s (%s)", filename, strerror(errno));
57 rc = ioctl(fd, LOOP_GET_STATUS64, &li);
59 if (rc < 0 && errno == ENXIO) {
64 SLOGE("Unable to get loop status for %s (%s)", filename,
69 asprintf(&tmp, "%s %d %lld:%lld %llu %lld:%lld %lld 0x%x {%s} {%s}", filename, li.lo_number,
70 MAJOR(li.lo_device), MINOR(li.lo_device), li.lo_inode, MAJOR(li.lo_rdevice),
71 MINOR(li.lo_rdevice), li.lo_offset, li.lo_flags, li.lo_crypt_name,
73 c->sendMsg(0, tmp, false);
79 int Loop::lookupActive(const char *id, char *buffer, size_t len) {
84 memset(buffer, 0, len);
86 for (i = 0; i < LOOP_MAX; i++) {
87 struct loop_info64 li;
90 sprintf(filename, "/dev/block/loop%d", i);
92 if ((fd = open(filename, O_RDWR)) < 0) {
93 if (errno != ENOENT) {
94 SLOGE("Unable to open %s (%s)", filename, strerror(errno));
101 rc = ioctl(fd, LOOP_GET_STATUS64, &li);
103 if (rc < 0 && errno == ENXIO) {
108 SLOGE("Unable to get loop status for %s (%s)", filename,
112 if (!strncmp((const char*) li.lo_crypt_name, id, LO_NAME_SIZE)) {
121 strncpy(buffer, filename, len -1);
125 int Loop::create(const char *id, const char *loopFile, char *loopDeviceBuffer, size_t len) {
130 for (i = 0; i < LOOP_MAX; i++) {
131 struct loop_info64 li;
134 sprintf(filename, "/dev/block/loop%d", i);
137 * The kernel starts us off with 8 loop nodes, but more
138 * are created on-demand if needed.
140 mode_t mode = 0660 | S_IFBLK;
141 unsigned int dev = (0xff & i) | ((i << 12) & 0xfff00000) | (7 << 8);
142 if (mknod(filename, mode, dev) < 0) {
143 if (errno != EEXIST) {
144 SLOGE("Error creating loop device node (%s)", strerror(errno));
149 if ((fd = open(filename, O_RDWR)) < 0) {
150 SLOGE("Unable to open %s (%s)", filename, strerror(errno));
154 rc = ioctl(fd, LOOP_GET_STATUS64, &li);
155 if (rc < 0 && errno == ENXIO)
161 SLOGE("Unable to get loop status for %s (%s)", filename,
168 SLOGE("Exhausted all loop devices");
173 strncpy(loopDeviceBuffer, filename, len -1);
177 if ((file_fd = open(loopFile, O_RDWR)) < 0) {
178 SLOGE("Unable to open %s (%s)", loopFile, strerror(errno));
183 if (ioctl(fd, LOOP_SET_FD, file_fd) < 0) {
184 SLOGE("Error setting up loopback interface (%s)", strerror(errno));
190 struct loop_info64 li;
192 memset(&li, 0, sizeof(li));
193 strlcpy((char*) li.lo_crypt_name, id, LO_NAME_SIZE);
194 strlcpy((char*) li.lo_file_name, loopFile, LO_NAME_SIZE);
196 if (ioctl(fd, LOOP_SET_STATUS64, &li) < 0) {
197 SLOGE("Error setting loopback status (%s)", strerror(errno));
209 int Loop::destroyByDevice(const char *loopDevice) {
212 device_fd = open(loopDevice, O_RDONLY);
214 SLOGE("Failed to open loop (%d)", errno);
218 if (ioctl(device_fd, LOOP_CLR_FD, 0) < 0) {
219 SLOGE("Failed to destroy loop (%d)", errno);
228 int Loop::destroyByFile(const char *loopFile) {
233 int Loop::createImageFile(const char *file, unsigned int numSectors) {
236 if ((fd = creat(file, 0600)) < 0) {
237 SLOGE("Error creating imagefile (%s)", strerror(errno));
241 if (ftruncate(fd, numSectors * 512) < 0) {
242 SLOGE("Error truncating imagefile (%s)", strerror(errno));