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.
|
|
|
|
*/
|
|
|
|
|
2017-09-22 01:08:43 +02:00
|
|
|
#define ATRACE_TAG ATRACE_TAG_PACKAGE_MANAGER
|
|
|
|
|
|
|
|
#include <dirent.h>
|
2010-01-06 19:33:53 +01:00
|
|
|
#include <errno.h>
|
2018-09-18 22:30:21 +02:00
|
|
|
#include <fcntl.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
2010-01-06 19:33:53 +01:00
|
|
|
#include <string.h>
|
2018-09-18 22:30:21 +02:00
|
|
|
#include <unistd.h>
|
2010-01-06 19:33:53 +01:00
|
|
|
|
2018-09-18 22:30:21 +02:00
|
|
|
#include <sys/ioctl.h>
|
2012-04-04 02:23:01 +02:00
|
|
|
#include <sys/mount.h>
|
2010-01-09 21:24:05 +01:00
|
|
|
#include <sys/stat.h>
|
2018-09-18 22:30:21 +02:00
|
|
|
#include <sys/types.h>
|
2010-01-09 21:24:05 +01:00
|
|
|
|
2010-03-12 22:32:47 +01:00
|
|
|
#include <linux/kdev_t.h>
|
|
|
|
|
2017-03-26 06:49:13 +02:00
|
|
|
#include <android-base/logging.h>
|
|
|
|
#include <android-base/stringprintf.h>
|
2018-09-18 22:30:21 +02:00
|
|
|
#include <android-base/strings.h>
|
2017-03-26 06:49:13 +02:00
|
|
|
#include <android-base/unique_fd.h>
|
2017-09-22 01:08:43 +02:00
|
|
|
#include <utils/Trace.h>
|
2017-03-26 06:49:13 +02:00
|
|
|
|
2010-01-06 19:33:53 +01:00
|
|
|
#include "Loop.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
|
|
|
|
2017-03-26 06:49:13 +02:00
|
|
|
using android::base::StringPrintf;
|
|
|
|
using android::base::unique_fd;
|
|
|
|
|
2017-09-11 18:32:01 +02:00
|
|
|
static const char* kVoldPrefix = "vold:";
|
2019-04-02 01:09:28 +02:00
|
|
|
static constexpr size_t kLoopDeviceRetryAttempts = 3u;
|
2017-09-11 18:32:01 +02:00
|
|
|
|
2017-03-26 06:49:13 +02:00
|
|
|
int Loop::create(const std::string& target, std::string& out_device) {
|
2017-03-27 18:49:21 +02:00
|
|
|
unique_fd ctl_fd(open("/dev/loop-control", O_RDWR | O_CLOEXEC));
|
2017-03-26 06:49:13 +02:00
|
|
|
if (ctl_fd.get() == -1) {
|
|
|
|
PLOG(ERROR) << "Failed to open loop-control";
|
|
|
|
return -errno;
|
|
|
|
}
|
|
|
|
|
|
|
|
int num = ioctl(ctl_fd.get(), LOOP_CTL_GET_FREE);
|
|
|
|
if (num == -1) {
|
|
|
|
PLOG(ERROR) << "Failed LOOP_CTL_GET_FREE";
|
|
|
|
return -errno;
|
|
|
|
}
|
|
|
|
|
|
|
|
out_device = StringPrintf("/dev/block/loop%d", num);
|
|
|
|
|
2019-04-02 01:09:28 +02:00
|
|
|
unique_fd target_fd;
|
|
|
|
for (size_t i = 0; i != kLoopDeviceRetryAttempts; ++i) {
|
|
|
|
target_fd.reset(open(target.c_str(), O_RDWR | O_CLOEXEC));
|
|
|
|
if (target_fd.get() != -1) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
usleep(50000);
|
|
|
|
}
|
2017-03-26 06:49:13 +02:00
|
|
|
if (target_fd.get() == -1) {
|
|
|
|
PLOG(ERROR) << "Failed to open " << target;
|
|
|
|
return -errno;
|
|
|
|
}
|
2017-03-27 18:49:21 +02:00
|
|
|
unique_fd device_fd(open(out_device.c_str(), O_RDWR | O_CLOEXEC));
|
2017-03-26 06:49:13 +02:00
|
|
|
if (device_fd.get() == -1) {
|
|
|
|
PLOG(ERROR) << "Failed to open " << out_device;
|
|
|
|
return -errno;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (ioctl(device_fd.get(), LOOP_SET_FD, target_fd.get()) == -1) {
|
|
|
|
PLOG(ERROR) << "Failed to LOOP_SET_FD";
|
|
|
|
return -errno;
|
|
|
|
}
|
|
|
|
|
2017-09-11 18:32:01 +02:00
|
|
|
struct loop_info64 li;
|
|
|
|
memset(&li, 0, sizeof(li));
|
2018-09-18 22:30:21 +02:00
|
|
|
strlcpy((char*)li.lo_crypt_name, kVoldPrefix, LO_NAME_SIZE);
|
2017-09-11 18:32:01 +02:00
|
|
|
if (ioctl(device_fd.get(), LOOP_SET_STATUS64, &li) == -1) {
|
|
|
|
PLOG(ERROR) << "Failed to LOOP_SET_STATUS64";
|
|
|
|
return -errno;
|
|
|
|
}
|
|
|
|
|
2017-03-26 06:49:13 +02:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2018-09-18 22:30:21 +02:00
|
|
|
int Loop::destroyByDevice(const char* loopDevice) {
|
2010-01-06 19:33:53 +01:00
|
|
|
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) {
|
2017-10-07 02:02:53 +02:00
|
|
|
PLOG(ERROR) << "Failed to open " << loopDevice;
|
2010-01-06 19:33:53 +01:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (ioctl(device_fd, LOOP_CLR_FD, 0) < 0) {
|
2017-10-07 02:02:53 +02:00
|
|
|
PLOG(ERROR) << "Failed to destroy " << loopDevice;
|
2010-01-06 19:33:53 +01:00
|
|
|
close(device_fd);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
close(device_fd);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2017-09-11 18:32:01 +02:00
|
|
|
int Loop::destroyAll() {
|
2017-09-22 01:08:43 +02:00
|
|
|
ATRACE_NAME("Loop::destroyAll");
|
|
|
|
|
|
|
|
std::string root = "/dev/block/";
|
2018-02-25 02:09:21 +01:00
|
|
|
auto dirp = std::unique_ptr<DIR, int (*)(DIR*)>(opendir(root.c_str()), closedir);
|
|
|
|
if (!dirp) {
|
2017-09-22 01:08:43 +02:00
|
|
|
PLOG(ERROR) << "Failed to opendir";
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Poke through all devices looking for loops
|
2018-02-25 02:09:21 +01:00
|
|
|
struct dirent* de;
|
|
|
|
while ((de = readdir(dirp.get()))) {
|
2017-10-07 02:02:53 +02:00
|
|
|
auto test = std::string(de->d_name);
|
|
|
|
if (!android::base::StartsWith(test, "loop")) continue;
|
2017-09-22 01:08:43 +02:00
|
|
|
|
|
|
|
auto path = root + de->d_name;
|
2017-09-11 18:32:01 +02:00
|
|
|
unique_fd fd(open(path.c_str(), O_RDWR | O_CLOEXEC));
|
|
|
|
if (fd.get() == -1) {
|
|
|
|
if (errno != ENOENT) {
|
|
|
|
PLOG(WARNING) << "Failed to open " << path;
|
|
|
|
}
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
struct loop_info64 li;
|
|
|
|
if (ioctl(fd.get(), LOOP_GET_STATUS64, &li) < 0) {
|
|
|
|
PLOG(WARNING) << "Failed to LOOP_GET_STATUS64 " << path;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2018-09-18 22:30:21 +02:00
|
|
|
auto id = std::string((char*)li.lo_crypt_name);
|
2017-10-07 02:02:53 +02:00
|
|
|
if (android::base::StartsWith(id, kVoldPrefix)) {
|
2017-09-11 18:32:01 +02:00
|
|
|
LOG(DEBUG) << "Tearing down stale loop device at " << path << " named " << id;
|
|
|
|
|
|
|
|
if (ioctl(fd.get(), LOOP_CLR_FD, 0) < 0) {
|
|
|
|
PLOG(WARNING) << "Failed to LOOP_CLR_FD " << path;
|
|
|
|
}
|
|
|
|
} else {
|
2018-09-21 19:54:54 +02:00
|
|
|
LOG(DEBUG) << "Found unmanaged loop device at " << path << " named " << id;
|
2017-09-11 18:32:01 +02:00
|
|
|
}
|
|
|
|
}
|
2017-09-22 01:08:43 +02:00
|
|
|
|
2017-09-11 18:32:01 +02:00
|
|
|
return 0;
|
2010-01-06 19:33:53 +01:00
|
|
|
}
|
|
|
|
|
2018-09-18 22:30:21 +02:00
|
|
|
int Loop::createImageFile(const char* file, unsigned long numSectors) {
|
2017-03-27 18:49:21 +02:00
|
|
|
unique_fd fd(open(file, O_CREAT | O_WRONLY | O_TRUNC | O_CLOEXEC, 0600));
|
2017-03-26 06:49:13 +02:00
|
|
|
if (fd.get() == -1) {
|
|
|
|
PLOG(ERROR) << "Failed to create image " << file;
|
2017-03-27 18:49:21 +02:00
|
|
|
return -errno;
|
2010-01-06 19:33:53 +01:00
|
|
|
}
|
2017-03-26 06:49:13 +02:00
|
|
|
if (fallocate(fd.get(), 0, 0, numSectors * 512) == -1) {
|
|
|
|
PLOG(WARNING) << "Failed to fallocate; falling back to ftruncate";
|
|
|
|
if (ftruncate(fd, numSectors * 512) == -1) {
|
|
|
|
PLOG(ERROR) << "Failed to ftruncate";
|
2017-03-27 18:49:21 +02:00
|
|
|
return -errno;
|
2017-03-26 06:49:13 +02:00
|
|
|
}
|
|
|
|
}
|
2017-03-27 18:49:21 +02:00
|
|
|
return 0;
|
2010-01-06 19:33:53 +01:00
|
|
|
}
|
2012-04-04 02:23:01 +02:00
|
|
|
|
2018-09-18 22:30:21 +02:00
|
|
|
int Loop::resizeImageFile(const char* file, unsigned long numSectors) {
|
2014-05-22 20:23:56 +02:00
|
|
|
int fd;
|
|
|
|
|
2015-04-09 06:07:21 +02:00
|
|
|
if ((fd = open(file, O_RDWR | O_CLOEXEC)) < 0) {
|
2017-10-07 02:02:53 +02:00
|
|
|
PLOG(ERROR) << "Failed to open " << file;
|
2014-05-22 20:23:56 +02:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2017-10-07 02:02:53 +02:00
|
|
|
LOG(DEBUG) << "Attempting to increase " << file << " to " << numSectors;
|
2014-05-22 20:23:56 +02:00
|
|
|
|
|
|
|
if (fallocate(fd, 0, 0, numSectors * 512)) {
|
2014-08-22 21:29:05 +02:00
|
|
|
if (errno == ENOSYS || errno == ENOTSUP) {
|
2017-10-07 02:02:53 +02:00
|
|
|
PLOG(WARNING) << "fallocate not found. Falling back to ftruncate.";
|
2014-05-22 20:23:56 +02:00
|
|
|
if (ftruncate(fd, numSectors * 512) < 0) {
|
2017-10-07 02:02:53 +02:00
|
|
|
PLOG(ERROR) << "Failed to ftruncate";
|
2014-05-22 20:23:56 +02:00
|
|
|
close(fd);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
} else {
|
2017-10-07 02:02:53 +02:00
|
|
|
PLOG(ERROR) << "Failed to fallocate";
|
2014-05-22 20:23:56 +02:00
|
|
|
close(fd);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
close(fd);
|
|
|
|
return 0;
|
|
|
|
}
|