1 /*
2  * Copyright (C) 2008 The Android Open Source Project
3  *
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
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  */
16 
17 #define ATRACE_TAG ATRACE_TAG_PACKAGE_MANAGER
18 
19 #include <dirent.h>
20 #include <errno.h>
21 #include <fcntl.h>
22 #include <stdio.h>
23 #include <stdlib.h>
24 #include <string.h>
25 #include <unistd.h>
26 
27 #include <sys/ioctl.h>
28 #include <sys/mount.h>
29 #include <sys/stat.h>
30 #include <sys/types.h>
31 
32 #include <linux/kdev_t.h>
33 
34 #include <chrono>
35 
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>
42 
43 #include "Loop.h"
44 #include "VoldUtil.h"
45 #include "sehandle.h"
46 
47 using namespace std::literals;
48 using android::base::StringPrintf;
49 using android::base::unique_fd;
50 
51 static const char* kVoldPrefix = "vold:";
52 static constexpr size_t kLoopDeviceRetryAttempts = 3u;
53 
create(const std::string & target,std::string & out_device)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";
58         return -errno;
59     }
60 
61     int num = ioctl(ctl_fd.get(), LOOP_CTL_GET_FREE);
62     if (num == -1) {
63         PLOG(ERROR) << "Failed LOOP_CTL_GET_FREE";
64         return -errno;
65     }
66 
67     out_device = StringPrintf("/dev/block/loop%d", num);
68 
69     unique_fd target_fd;
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) {
73             break;
74         }
75         usleep(50000);
76     }
77     if (target_fd.get() == -1) {
78         PLOG(ERROR) << "Failed to open " << target;
79         return -errno;
80     }
81     if (!android::fs_mgr::WaitForFile(out_device, 2s)) {
82         LOG(ERROR) << "Failed to find " << out_device;
83         return -ENOENT;
84     }
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;
88         return -errno;
89     }
90 
91     if (ioctl(device_fd.get(), LOOP_SET_FD, target_fd.get()) == -1) {
92         PLOG(ERROR) << "Failed to LOOP_SET_FD";
93         return -errno;
94     }
95 
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";
101         return -errno;
102     }
103 
104     return 0;
105 }
106 
destroyByDevice(const char * loopDevice)107 int Loop::destroyByDevice(const char* loopDevice) {
108     int device_fd;
109 
110     device_fd = open(loopDevice, O_RDONLY | O_CLOEXEC);
111     if (device_fd < 0) {
112         PLOG(ERROR) << "Failed to open " << loopDevice;
113         return -1;
114     }
115 
116     if (ioctl(device_fd, LOOP_CLR_FD, 0) < 0) {
117         PLOG(ERROR) << "Failed to destroy " << loopDevice;
118         close(device_fd);
119         return -1;
120     }
121 
122     close(device_fd);
123     return 0;
124 }
125 
destroyAll()126 int Loop::destroyAll() {
127     ATRACE_NAME("Loop::destroyAll");
128 
129     std::string root = "/dev/block/";
130     auto dirp = std::unique_ptr<DIR, int (*)(DIR*)>(opendir(root.c_str()), closedir);
131     if (!dirp) {
132         PLOG(ERROR) << "Failed to opendir";
133         return -1;
134     }
135 
136     // Poke through all devices looking for loops
137     struct dirent* de;
138     while ((de = readdir(dirp.get()))) {
139         auto test = std::string(de->d_name);
140         if (!android::base::StartsWith(test, "loop")) continue;
141 
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;
147             }
148             continue;
149         }
150 
151         struct loop_info64 li;
152         if (ioctl(fd.get(), LOOP_GET_STATUS64, &li) < 0) {
153             PLOG(WARNING) << "Failed to LOOP_GET_STATUS64 " << path;
154             continue;
155         }
156 
157         auto id = std::string((char*)li.lo_crypt_name);
158         if (android::base::StartsWith(id, kVoldPrefix)) {
159             LOG(DEBUG) << "Tearing down stale loop device at " << path << " named " << id;
160 
161             if (ioctl(fd.get(), LOOP_CLR_FD, 0) < 0) {
162                 PLOG(WARNING) << "Failed to LOOP_CLR_FD " << path;
163             }
164         } else {
165             LOG(DEBUG) << "Found unmanaged loop device at " << path << " named " << id;
166         }
167     }
168 
169     return 0;
170 }
171 
createImageFile(const char * file,unsigned long numSectors)172 int Loop::createImageFile(const char* file, unsigned long numSectors) {
173     unique_fd fd(open(file, O_CREAT | O_WRONLY | O_TRUNC | O_CLOEXEC, 0600));
174     if (fd.get() == -1) {
175         PLOG(ERROR) << "Failed to create image " << file;
176         return -errno;
177     }
178     if (fallocate(fd.get(), 0, 0, numSectors * 512) == -1) {
179         PLOG(WARNING) << "Failed to fallocate; falling back to ftruncate";
180         if (ftruncate(fd, numSectors * 512) == -1) {
181             PLOG(ERROR) << "Failed to ftruncate";
182             return -errno;
183         }
184     }
185     return 0;
186 }
187 
resizeImageFile(const char * file,unsigned long numSectors)188 int Loop::resizeImageFile(const char* file, unsigned long numSectors) {
189     int fd;
190 
191     if ((fd = open(file, O_RDWR | O_CLOEXEC)) < 0) {
192         PLOG(ERROR) << "Failed to open " << file;
193         return -1;
194     }
195 
196     LOG(DEBUG) << "Attempting to increase " << file << " to " << numSectors;
197 
198     if (fallocate(fd, 0, 0, numSectors * 512)) {
199         if (errno == ENOSYS || errno == ENOTSUP) {
200             PLOG(WARNING) << "fallocate not found. Falling back to ftruncate.";
201             if (ftruncate(fd, numSectors * 512) < 0) {
202                 PLOG(ERROR) << "Failed to ftruncate";
203                 close(fd);
204                 return -1;
205             }
206         } else {
207             PLOG(ERROR) << "Failed to fallocate";
208             close(fd);
209             return -1;
210         }
211     }
212     close(fd);
213     return 0;
214 }
215