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.
17 #define ATRACE_TAG ATRACE_TAG_PACKAGE_MANAGER
27 #include <sys/ioctl.h>
28 #include <sys/mount.h>
30 #include <sys/types.h>
32 #include <linux/kdev_t.h>
36 #include <android-base/logging.h>
37 #include <android-base/stringprintf.h>
38 #include <android-base/strings.h>
39 #include <android-base/unique_fd.h>
40 #include <fs_mgr/file_wait.h>
41 #include <utils/Trace.h>
47 using namespace std::literals;
48 using android::base::StringPrintf;
49 using android::base::unique_fd;
51 static const char* kVoldPrefix = "vold:";
52 static constexpr size_t kLoopDeviceRetryAttempts = 3u;
54 int Loop::create(const std::string& target, std::string& out_device) {
55 unique_fd ctl_fd(open("/dev/loop-control", O_RDWR | O_CLOEXEC));
56 if (ctl_fd.get() == -1) {
57 PLOG(ERROR) << "Failed to open loop-control";
61 int num = ioctl(ctl_fd.get(), LOOP_CTL_GET_FREE);
63 PLOG(ERROR) << "Failed LOOP_CTL_GET_FREE";
67 out_device = StringPrintf("/dev/block/loop%d", num);
70 for (size_t i = 0; i != kLoopDeviceRetryAttempts; ++i) {
71 target_fd.reset(open(target.c_str(), O_RDWR | O_CLOEXEC));
72 if (target_fd.get() != -1) {
77 if (target_fd.get() == -1) {
78 PLOG(ERROR) << "Failed to open " << target;
81 if (!android::fs_mgr::WaitForFile(out_device, 2s)) {
82 LOG(ERROR) << "Failed to find " << out_device;
85 unique_fd device_fd(open(out_device.c_str(), O_RDWR | O_CLOEXEC));
86 if (device_fd.get() == -1) {
87 PLOG(ERROR) << "Failed to open " << out_device;
91 if (ioctl(device_fd.get(), LOOP_SET_FD, target_fd.get()) == -1) {
92 PLOG(ERROR) << "Failed to LOOP_SET_FD";
96 struct loop_info64 li;
97 memset(&li, 0, sizeof(li));
98 strlcpy((char*)li.lo_crypt_name, kVoldPrefix, LO_NAME_SIZE);
99 if (ioctl(device_fd.get(), LOOP_SET_STATUS64, &li) == -1) {
100 PLOG(ERROR) << "Failed to LOOP_SET_STATUS64";
107 int Loop::destroyByDevice(const char* loopDevice) {
110 device_fd = open(loopDevice, O_RDONLY | O_CLOEXEC);
112 PLOG(ERROR) << "Failed to open " << loopDevice;
116 if (ioctl(device_fd, LOOP_CLR_FD, 0) < 0) {
117 PLOG(ERROR) << "Failed to destroy " << loopDevice;
126 int Loop::destroyAll() {
127 ATRACE_NAME("Loop::destroyAll");
129 std::string root = "/dev/block/";
130 auto dirp = std::unique_ptr<DIR, int (*)(DIR*)>(opendir(root.c_str()), closedir);
132 PLOG(ERROR) << "Failed to opendir";
136 // Poke through all devices looking for loops
138 while ((de = readdir(dirp.get()))) {
139 auto test = std::string(de->d_name);
140 if (!android::base::StartsWith(test, "loop")) continue;
142 auto path = root + de->d_name;
143 unique_fd fd(open(path.c_str(), O_RDWR | O_CLOEXEC));
144 if (fd.get() == -1) {
145 if (errno != ENOENT) {
146 PLOG(WARNING) << "Failed to open " << path;
151 struct loop_info64 li;
152 if (ioctl(fd.get(), LOOP_GET_STATUS64, &li) < 0) {
153 if (errno != ENXIO) {
154 PLOG(WARNING) << "Failed to LOOP_GET_STATUS64 " << path;
159 auto id = std::string((char*)li.lo_crypt_name);
160 if (android::base::StartsWith(id, kVoldPrefix)) {
161 LOG(DEBUG) << "Tearing down stale loop device at " << path << " named " << id;
163 if (ioctl(fd.get(), LOOP_CLR_FD, 0) < 0) {
164 PLOG(WARNING) << "Failed to LOOP_CLR_FD " << path;
167 LOG(DEBUG) << "Found unmanaged loop device at " << path << " named " << id;
174 int Loop::createImageFile(const char* file, unsigned long numSectors) {
175 unique_fd fd(open(file, O_CREAT | O_WRONLY | O_TRUNC | O_CLOEXEC, 0600));
176 if (fd.get() == -1) {
177 PLOG(ERROR) << "Failed to create image " << file;
180 if (fallocate(fd.get(), 0, 0, numSectors * 512) == -1) {
181 PLOG(WARNING) << "Failed to fallocate; falling back to ftruncate";
182 if (ftruncate(fd, numSectors * 512) == -1) {
183 PLOG(ERROR) << "Failed to ftruncate";
190 int Loop::resizeImageFile(const char* file, unsigned long numSectors) {
193 if ((fd = open(file, O_RDWR | O_CLOEXEC)) < 0) {
194 PLOG(ERROR) << "Failed to open " << file;
198 LOG(DEBUG) << "Attempting to increase " << file << " to " << numSectors;
200 if (fallocate(fd, 0, 0, numSectors * 512)) {
201 if (errno == ENOSYS || errno == ENOTSUP) {
202 PLOG(WARNING) << "fallocate not found. Falling back to ftruncate.";
203 if (ftruncate(fd, numSectors * 512) < 0) {
204 PLOG(ERROR) << "Failed to ftruncate";
209 PLOG(ERROR) << "Failed to fallocate";