2010-01-06 19:33:53 +01:00
|
|
|
/*
|
|
|
|
* Copyright (C) 2008 The Android Open Source Project
|
|
|
|
*
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <stdio.h>
|
2010-11-16 19:41:53 +01:00
|
|
|
#include <stdlib.h>
|
2010-01-06 19:33:53 +01:00
|
|
|
#include <fcntl.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <errno.h>
|
|
|
|
#include <string.h>
|
|
|
|
|
2012-04-04 02:23:01 +02:00
|
|
|
#include <sys/mount.h>
|
2010-01-09 21:24:05 +01:00
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/stat.h>
|
2010-11-16 19:41:53 +01:00
|
|
|
#include <sys/ioctl.h>
|
2010-01-09 21:24:05 +01:00
|
|
|
|
2010-03-12 22:32:47 +01:00
|
|
|
#include <linux/kdev_t.h>
|
|
|
|
|
2010-01-06 19:33:53 +01:00
|
|
|
#define LOG_TAG "Vold"
|
|
|
|
|
|
|
|
#include <cutils/log.h>
|
|
|
|
|
2010-03-12 22:32:47 +01:00
|
|
|
#include <sysutils/SocketClient.h>
|
2010-01-06 19:33:53 +01:00
|
|
|
#include "Loop.h"
|
2012-04-04 02:23:01 +02:00
|
|
|
#include "Asec.h"
|
2015-02-04 05:29:15 +01:00
|
|
|
#include "VoldUtil.h"
|
2014-09-30 16:29:24 +02:00
|
|
|
#include "sehandle.h"
|
2010-01-06 19:33:53 +01:00
|
|
|
|
2010-03-12 22:32:47 +01:00
|
|
|
int Loop::dumpState(SocketClient *c) {
|
|
|
|
int i;
|
|
|
|
int fd;
|
|
|
|
char filename[256];
|
|
|
|
|
|
|
|
for (i = 0; i < LOOP_MAX; i++) {
|
2010-07-12 18:59:49 +02:00
|
|
|
struct loop_info64 li;
|
2010-03-12 22:32:47 +01:00
|
|
|
int rc;
|
|
|
|
|
|
|
|
sprintf(filename, "/dev/block/loop%d", i);
|
|
|
|
|
2015-04-09 06:07:21 +02:00
|
|
|
if ((fd = open(filename, O_RDWR | O_CLOEXEC)) < 0) {
|
2010-03-12 22:32:47 +01:00
|
|
|
if (errno != ENOENT) {
|
2010-03-24 18:24:19 +01:00
|
|
|
SLOGE("Unable to open %s (%s)", filename, strerror(errno));
|
2010-03-12 22:32:47 +01:00
|
|
|
} else {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2010-07-12 18:59:49 +02:00
|
|
|
rc = ioctl(fd, LOOP_GET_STATUS64, &li);
|
2010-03-12 22:32:47 +01:00
|
|
|
close(fd);
|
|
|
|
if (rc < 0 && errno == ENXIO) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (rc < 0) {
|
2010-03-24 18:24:19 +01:00
|
|
|
SLOGE("Unable to get loop status for %s (%s)", filename,
|
2010-03-12 22:32:47 +01:00
|
|
|
strerror(errno));
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
char *tmp = NULL;
|
2010-07-12 18:59:49 +02:00
|
|
|
asprintf(&tmp, "%s %d %lld:%lld %llu %lld:%lld %lld 0x%x {%s} {%s}", filename, li.lo_number,
|
2010-03-12 22:32:47 +01:00
|
|
|
MAJOR(li.lo_device), MINOR(li.lo_device), li.lo_inode, MAJOR(li.lo_rdevice),
|
2010-07-12 18:59:49 +02:00
|
|
|
MINOR(li.lo_rdevice), li.lo_offset, li.lo_flags, li.lo_crypt_name,
|
|
|
|
li.lo_file_name);
|
2010-03-12 22:32:47 +01:00
|
|
|
c->sendMsg(0, tmp, false);
|
|
|
|
free(tmp);
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int Loop::lookupActive(const char *id, char *buffer, size_t len) {
|
2010-01-06 19:33:53 +01:00
|
|
|
int i;
|
|
|
|
int fd;
|
|
|
|
char filename[256];
|
|
|
|
|
|
|
|
memset(buffer, 0, len);
|
|
|
|
|
|
|
|
for (i = 0; i < LOOP_MAX; i++) {
|
2010-07-12 18:59:49 +02:00
|
|
|
struct loop_info64 li;
|
2010-01-06 19:33:53 +01:00
|
|
|
int rc;
|
|
|
|
|
|
|
|
sprintf(filename, "/dev/block/loop%d", i);
|
|
|
|
|
2015-04-09 06:07:21 +02:00
|
|
|
if ((fd = open(filename, O_RDWR | O_CLOEXEC)) < 0) {
|
2010-01-10 22:02:12 +01:00
|
|
|
if (errno != ENOENT) {
|
2010-03-24 18:24:19 +01:00
|
|
|
SLOGE("Unable to open %s (%s)", filename, strerror(errno));
|
2010-03-12 22:32:47 +01:00
|
|
|
} else {
|
|
|
|
continue;
|
2010-01-10 22:02:12 +01:00
|
|
|
}
|
2010-01-06 19:33:53 +01:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2010-07-12 18:59:49 +02:00
|
|
|
rc = ioctl(fd, LOOP_GET_STATUS64, &li);
|
2010-01-06 19:33:53 +01:00
|
|
|
close(fd);
|
|
|
|
if (rc < 0 && errno == ENXIO) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (rc < 0) {
|
2010-03-24 18:24:19 +01:00
|
|
|
SLOGE("Unable to get loop status for %s (%s)", filename,
|
2010-01-06 19:33:53 +01:00
|
|
|
strerror(errno));
|
|
|
|
return -1;
|
|
|
|
}
|
2010-07-12 18:59:49 +02:00
|
|
|
if (!strncmp((const char*) li.lo_crypt_name, id, LO_NAME_SIZE)) {
|
2010-01-06 19:33:53 +01:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (i == LOOP_MAX) {
|
|
|
|
errno = ENOENT;
|
|
|
|
return -1;
|
|
|
|
}
|
2015-02-06 09:24:14 +01:00
|
|
|
strlcpy(buffer, filename, len);
|
2010-01-06 19:33:53 +01:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2010-03-12 22:32:47 +01:00
|
|
|
int Loop::create(const char *id, const char *loopFile, char *loopDeviceBuffer, size_t len) {
|
2010-01-06 19:33:53 +01:00
|
|
|
int i;
|
|
|
|
int fd;
|
|
|
|
char filename[256];
|
|
|
|
|
|
|
|
for (i = 0; i < LOOP_MAX; i++) {
|
2011-02-02 00:58:25 +01:00
|
|
|
struct loop_info64 li;
|
2010-01-06 19:33:53 +01:00
|
|
|
int rc;
|
2014-09-30 16:29:24 +02:00
|
|
|
char *secontext = NULL;
|
2010-01-06 19:33:53 +01:00
|
|
|
|
|
|
|
sprintf(filename, "/dev/block/loop%d", i);
|
|
|
|
|
2010-01-09 21:24:05 +01:00
|
|
|
/*
|
|
|
|
* The kernel starts us off with 8 loop nodes, but more
|
|
|
|
* are created on-demand if needed.
|
|
|
|
*/
|
|
|
|
mode_t mode = 0660 | S_IFBLK;
|
2010-01-10 22:02:12 +01:00
|
|
|
unsigned int dev = (0xff & i) | ((i << 12) & 0xfff00000) | (7 << 8);
|
2014-09-30 16:29:24 +02:00
|
|
|
|
|
|
|
if (sehandle) {
|
|
|
|
rc = selabel_lookup(sehandle, &secontext, filename, S_IFBLK);
|
|
|
|
if (rc == 0)
|
|
|
|
setfscreatecon(secontext);
|
|
|
|
}
|
|
|
|
|
2010-01-09 21:24:05 +01:00
|
|
|
if (mknod(filename, mode, dev) < 0) {
|
|
|
|
if (errno != EEXIST) {
|
2014-09-30 16:29:24 +02:00
|
|
|
int sverrno = errno;
|
2010-03-24 18:24:19 +01:00
|
|
|
SLOGE("Error creating loop device node (%s)", strerror(errno));
|
2014-09-30 16:29:24 +02:00
|
|
|
if (secontext) {
|
|
|
|
freecon(secontext);
|
|
|
|
setfscreatecon(NULL);
|
|
|
|
}
|
|
|
|
errno = sverrno;
|
2010-01-09 21:24:05 +01:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
}
|
2014-09-30 16:29:24 +02:00
|
|
|
if (secontext) {
|
|
|
|
freecon(secontext);
|
|
|
|
setfscreatecon(NULL);
|
|
|
|
}
|
2010-01-09 21:24:05 +01:00
|
|
|
|
2015-04-09 06:07:21 +02:00
|
|
|
if ((fd = open(filename, O_RDWR | O_CLOEXEC)) < 0) {
|
2010-03-24 18:24:19 +01:00
|
|
|
SLOGE("Unable to open %s (%s)", filename, strerror(errno));
|
2010-01-06 19:33:53 +01:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2011-02-02 00:58:25 +01:00
|
|
|
rc = ioctl(fd, LOOP_GET_STATUS64, &li);
|
2010-01-06 19:33:53 +01:00
|
|
|
if (rc < 0 && errno == ENXIO)
|
|
|
|
break;
|
|
|
|
|
2010-01-09 21:24:05 +01:00
|
|
|
close(fd);
|
|
|
|
|
2010-01-06 19:33:53 +01:00
|
|
|
if (rc < 0) {
|
2010-03-24 18:24:19 +01:00
|
|
|
SLOGE("Unable to get loop status for %s (%s)", filename,
|
2010-01-06 19:33:53 +01:00
|
|
|
strerror(errno));
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (i == LOOP_MAX) {
|
2010-03-24 18:24:19 +01:00
|
|
|
SLOGE("Exhausted all loop devices");
|
2010-01-06 19:33:53 +01:00
|
|
|
errno = ENOSPC;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2015-02-06 09:24:14 +01:00
|
|
|
strlcpy(loopDeviceBuffer, filename, len);
|
2010-01-06 19:33:53 +01:00
|
|
|
|
2010-01-09 21:24:05 +01:00
|
|
|
int file_fd;
|
2010-01-06 19:33:53 +01:00
|
|
|
|
2015-04-09 06:07:21 +02:00
|
|
|
if ((file_fd = open(loopFile, O_RDWR | O_CLOEXEC)) < 0) {
|
2010-03-24 18:24:19 +01:00
|
|
|
SLOGE("Unable to open %s (%s)", loopFile, strerror(errno));
|
2010-01-06 19:33:53 +01:00
|
|
|
close(fd);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (ioctl(fd, LOOP_SET_FD, file_fd) < 0) {
|
2010-03-24 18:24:19 +01:00
|
|
|
SLOGE("Error setting up loopback interface (%s)", strerror(errno));
|
2010-01-06 19:33:53 +01:00
|
|
|
close(file_fd);
|
|
|
|
close(fd);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2010-07-12 18:59:49 +02:00
|
|
|
struct loop_info64 li;
|
2010-01-06 19:33:53 +01:00
|
|
|
|
|
|
|
memset(&li, 0, sizeof(li));
|
2011-04-01 12:35:25 +02:00
|
|
|
strlcpy((char*) li.lo_crypt_name, id, LO_NAME_SIZE);
|
|
|
|
strlcpy((char*) li.lo_file_name, loopFile, LO_NAME_SIZE);
|
2010-01-06 19:33:53 +01:00
|
|
|
|
2010-07-12 18:59:49 +02:00
|
|
|
if (ioctl(fd, LOOP_SET_STATUS64, &li) < 0) {
|
2010-03-24 18:24:19 +01:00
|
|
|
SLOGE("Error setting loopback status (%s)", strerror(errno));
|
2010-01-06 19:33:53 +01:00
|
|
|
close(file_fd);
|
|
|
|
close(fd);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
close(fd);
|
|
|
|
close(file_fd);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int Loop::destroyByDevice(const char *loopDevice) {
|
|
|
|
int device_fd;
|
|
|
|
|
2015-04-09 06:07:21 +02:00
|
|
|
device_fd = open(loopDevice, O_RDONLY | O_CLOEXEC);
|
2010-01-06 19:33:53 +01:00
|
|
|
if (device_fd < 0) {
|
2010-03-24 18:24:19 +01:00
|
|
|
SLOGE("Failed to open loop (%d)", errno);
|
2010-01-06 19:33:53 +01:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (ioctl(device_fd, LOOP_CLR_FD, 0) < 0) {
|
2010-03-24 18:24:19 +01:00
|
|
|
SLOGE("Failed to destroy loop (%d)", errno);
|
2010-01-06 19:33:53 +01:00
|
|
|
close(device_fd);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
close(device_fd);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2014-01-21 22:27:04 +01:00
|
|
|
int Loop::destroyByFile(const char * /*loopFile*/) {
|
2010-01-06 19:33:53 +01:00
|
|
|
errno = ENOSYS;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2010-01-11 18:17:25 +01:00
|
|
|
int Loop::createImageFile(const char *file, unsigned int numSectors) {
|
2010-01-06 19:33:53 +01:00
|
|
|
int fd;
|
|
|
|
|
|
|
|
if ((fd = creat(file, 0600)) < 0) {
|
2010-03-24 18:24:19 +01:00
|
|
|
SLOGE("Error creating imagefile (%s)", strerror(errno));
|
2010-01-06 19:33:53 +01:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2010-01-11 18:17:25 +01:00
|
|
|
if (ftruncate(fd, numSectors * 512) < 0) {
|
2010-03-24 18:24:19 +01:00
|
|
|
SLOGE("Error truncating imagefile (%s)", strerror(errno));
|
2010-01-06 19:33:53 +01:00
|
|
|
close(fd);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
close(fd);
|
|
|
|
return 0;
|
|
|
|
}
|
2012-04-04 02:23:01 +02:00
|
|
|
|
2014-05-22 20:23:56 +02:00
|
|
|
int Loop::resizeImageFile(const char *file, unsigned int numSectors) {
|
|
|
|
int fd;
|
|
|
|
|
2015-04-09 06:07:21 +02:00
|
|
|
if ((fd = open(file, O_RDWR | O_CLOEXEC)) < 0) {
|
2014-05-22 20:23:56 +02:00
|
|
|
SLOGE("Error opening imagefile (%s)", strerror(errno));
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
SLOGD("Attempting to increase size of %s to %d sectors.", file, numSectors);
|
|
|
|
|
|
|
|
if (fallocate(fd, 0, 0, numSectors * 512)) {
|
2014-08-22 21:29:05 +02:00
|
|
|
if (errno == ENOSYS || errno == ENOTSUP) {
|
2014-05-22 20:23:56 +02:00
|
|
|
SLOGW("fallocate not found. Falling back to ftruncate.");
|
|
|
|
if (ftruncate(fd, numSectors * 512) < 0) {
|
|
|
|
SLOGE("Error truncating imagefile (%s)", strerror(errno));
|
|
|
|
close(fd);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
SLOGE("Error allocating space (%s)", strerror(errno));
|
|
|
|
close(fd);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
close(fd);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2015-02-04 05:29:15 +01:00
|
|
|
int Loop::lookupInfo(const char *loopDevice, struct asec_superblock *sb, unsigned long *nr_sec) {
|
2012-04-04 02:23:01 +02:00
|
|
|
int fd;
|
|
|
|
struct asec_superblock buffer;
|
|
|
|
|
2015-04-09 06:07:21 +02:00
|
|
|
if ((fd = open(loopDevice, O_RDONLY | O_CLOEXEC)) < 0) {
|
2012-04-04 02:23:01 +02:00
|
|
|
SLOGE("Failed to open loopdevice (%s)", strerror(errno));
|
|
|
|
destroyByDevice(loopDevice);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2015-02-04 05:29:15 +01:00
|
|
|
get_blkdev_size(fd, nr_sec);
|
|
|
|
if (*nr_sec == 0) {
|
2012-04-04 02:23:01 +02:00
|
|
|
SLOGE("Failed to get loop size (%s)", strerror(errno));
|
|
|
|
destroyByDevice(loopDevice);
|
|
|
|
close(fd);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Try to read superblock.
|
|
|
|
*/
|
|
|
|
memset(&buffer, 0, sizeof(struct asec_superblock));
|
|
|
|
if (lseek(fd, ((*nr_sec - 1) * 512), SEEK_SET) < 0) {
|
|
|
|
SLOGE("lseek failed (%s)", strerror(errno));
|
|
|
|
close(fd);
|
|
|
|
destroyByDevice(loopDevice);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
if (read(fd, &buffer, sizeof(struct asec_superblock)) != sizeof(struct asec_superblock)) {
|
|
|
|
SLOGE("superblock read failed (%s)", strerror(errno));
|
|
|
|
close(fd);
|
|
|
|
destroyByDevice(loopDevice);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
close(fd);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Superblock successfully read. Copy to caller's struct.
|
|
|
|
*/
|
|
|
|
memcpy(sb, &buffer, sizeof(struct asec_superblock));
|
|
|
|
return 0;
|
|
|
|
}
|