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>
41 int Loop::dumpState(SocketClient *c) {
46 for (i = 0; i < LOOP_MAX; i++) {
47 struct loop_info64 li;
50 sprintf(filename, "/dev/block/loop%d", i);
52 if ((fd = open(filename, O_RDWR | O_CLOEXEC)) < 0) {
53 if (errno != ENOENT) {
54 SLOGE("Unable to open %s (%s)", filename, strerror(errno));
61 rc = ioctl(fd, LOOP_GET_STATUS64, &li);
63 if (rc < 0 && errno == ENXIO) {
68 SLOGE("Unable to get loop status for %s (%s)", filename,
73 asprintf(&tmp, "%s %d %lld:%lld %llu %lld:%lld %lld 0x%x {%s} {%s}", filename, li.lo_number,
74 MAJOR(li.lo_device), MINOR(li.lo_device), li.lo_inode, MAJOR(li.lo_rdevice),
75 MINOR(li.lo_rdevice), li.lo_offset, li.lo_flags, li.lo_crypt_name,
77 c->sendMsg(0, tmp, false);
83 int Loop::lookupActive(const char *id, char *buffer, size_t len) {
88 memset(buffer, 0, len);
90 for (i = 0; i < LOOP_MAX; i++) {
91 struct loop_info64 li;
94 sprintf(filename, "/dev/block/loop%d", i);
96 if ((fd = open(filename, O_RDWR | O_CLOEXEC)) < 0) {
97 if (errno != ENOENT) {
98 SLOGE("Unable to open %s (%s)", filename, strerror(errno));
105 rc = ioctl(fd, LOOP_GET_STATUS64, &li);
107 if (rc < 0 && errno == ENXIO) {
112 SLOGE("Unable to get loop status for %s (%s)", filename,
116 if (!strncmp((const char*) li.lo_crypt_name, id, LO_NAME_SIZE)) {
125 strlcpy(buffer, filename, len);
129 int Loop::create(const char *id, const char *loopFile, char *loopDeviceBuffer, size_t len) {
134 for (i = 0; i < LOOP_MAX; i++) {
135 struct loop_info64 li;
137 char *secontext = NULL;
139 sprintf(filename, "/dev/block/loop%d", i);
142 * The kernel starts us off with 8 loop nodes, but more
143 * are created on-demand if needed.
145 mode_t mode = 0660 | S_IFBLK;
146 unsigned int dev = (0xff & i) | ((i << 12) & 0xfff00000) | (7 << 8);
149 rc = selabel_lookup(sehandle, &secontext, filename, S_IFBLK);
151 setfscreatecon(secontext);
154 if (mknod(filename, mode, dev) < 0) {
155 if (errno != EEXIST) {
157 SLOGE("Error creating loop device node (%s)", strerror(errno));
160 setfscreatecon(NULL);
168 setfscreatecon(NULL);
171 if ((fd = open(filename, O_RDWR | O_CLOEXEC)) < 0) {
172 SLOGE("Unable to open %s (%s)", filename, strerror(errno));
176 rc = ioctl(fd, LOOP_GET_STATUS64, &li);
177 if (rc < 0 && errno == ENXIO)
183 SLOGE("Unable to get loop status for %s (%s)", filename,
190 SLOGE("Exhausted all loop devices");
195 strlcpy(loopDeviceBuffer, filename, len);
199 if ((file_fd = open(loopFile, O_RDWR | O_CLOEXEC)) < 0) {
200 SLOGE("Unable to open %s (%s)", loopFile, strerror(errno));
205 if (ioctl(fd, LOOP_SET_FD, file_fd) < 0) {
206 SLOGE("Error setting up loopback interface (%s)", strerror(errno));
212 struct loop_info64 li;
214 memset(&li, 0, sizeof(li));
215 strlcpy((char*) li.lo_crypt_name, id, LO_NAME_SIZE);
216 strlcpy((char*) li.lo_file_name, loopFile, LO_NAME_SIZE);
218 if (ioctl(fd, LOOP_SET_STATUS64, &li) < 0) {
219 SLOGE("Error setting loopback status (%s)", strerror(errno));
231 int Loop::destroyByDevice(const char *loopDevice) {
234 device_fd = open(loopDevice, O_RDONLY | O_CLOEXEC);
236 SLOGE("Failed to open loop (%d)", errno);
240 if (ioctl(device_fd, LOOP_CLR_FD, 0) < 0) {
241 SLOGE("Failed to destroy loop (%d)", errno);
250 int Loop::destroyByFile(const char * /*loopFile*/) {
255 int Loop::createImageFile(const char *file, unsigned int numSectors) {
258 if ((fd = creat(file, 0600)) < 0) {
259 SLOGE("Error creating imagefile (%s)", strerror(errno));
263 if (ftruncate(fd, numSectors * 512) < 0) {
264 SLOGE("Error truncating imagefile (%s)", strerror(errno));
272 int Loop::resizeImageFile(const char *file, unsigned int numSectors) {
275 if ((fd = open(file, O_RDWR | O_CLOEXEC)) < 0) {
276 SLOGE("Error opening imagefile (%s)", strerror(errno));
280 SLOGD("Attempting to increase size of %s to %d sectors.", file, numSectors);
282 if (fallocate(fd, 0, 0, numSectors * 512)) {
283 if (errno == ENOSYS || errno == ENOTSUP) {
284 SLOGW("fallocate not found. Falling back to ftruncate.");
285 if (ftruncate(fd, numSectors * 512) < 0) {
286 SLOGE("Error truncating imagefile (%s)", strerror(errno));
291 SLOGE("Error allocating space (%s)", strerror(errno));
300 int Loop::lookupInfo(const char *loopDevice, struct asec_superblock *sb, unsigned long *nr_sec) {
302 struct asec_superblock buffer;
304 if ((fd = open(loopDevice, O_RDONLY | O_CLOEXEC)) < 0) {
305 SLOGE("Failed to open loopdevice (%s)", strerror(errno));
306 destroyByDevice(loopDevice);
310 get_blkdev_size(fd, nr_sec);
312 SLOGE("Failed to get loop size (%s)", strerror(errno));
313 destroyByDevice(loopDevice);
319 * Try to read superblock.
321 memset(&buffer, 0, sizeof(struct asec_superblock));
322 if (lseek(fd, ((*nr_sec - 1) * 512), SEEK_SET) < 0) {
323 SLOGE("lseek failed (%s)", strerror(errno));
325 destroyByDevice(loopDevice);
328 if (read(fd, &buffer, sizeof(struct asec_superblock)) != sizeof(struct asec_superblock)) {
329 SLOGE("superblock read failed (%s)", strerror(errno));
331 destroyByDevice(loopDevice);
337 * Superblock successfully read. Copy to caller's struct.
339 memcpy(sb, &buffer, sizeof(struct asec_superblock));