• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 <android-base/logging.h>
35 #include <android-base/stringprintf.h>
36 #include <android-base/strings.h>
37 #include <android-base/unique_fd.h>
38 #include <utils/Trace.h>
39 
40 #include "Loop.h"
41 #include "VoldUtil.h"
42 #include "sehandle.h"
43 
44 using android::base::StringPrintf;
45 using android::base::unique_fd;
46 
47 static const char* kVoldPrefix = "vold:";
48 static constexpr size_t kLoopDeviceRetryAttempts = 3u;
49 
create(const std::string & target,std::string & out_device)50 int Loop::create(const std::string& target, std::string& out_device) {
51     unique_fd ctl_fd(open("/dev/loop-control", O_RDWR | O_CLOEXEC));
52     if (ctl_fd.get() == -1) {
53         PLOG(ERROR) << "Failed to open loop-control";
54         return -errno;
55     }
56 
57     int num = ioctl(ctl_fd.get(), LOOP_CTL_GET_FREE);
58     if (num == -1) {
59         PLOG(ERROR) << "Failed LOOP_CTL_GET_FREE";
60         return -errno;
61     }
62 
63     out_device = StringPrintf("/dev/block/loop%d", num);
64 
65     unique_fd target_fd;
66     for (size_t i = 0; i != kLoopDeviceRetryAttempts; ++i) {
67         target_fd.reset(open(target.c_str(), O_RDWR | O_CLOEXEC));
68         if (target_fd.get() != -1) {
69             break;
70         }
71         usleep(50000);
72     }
73     if (target_fd.get() == -1) {
74         PLOG(ERROR) << "Failed to open " << target;
75         return -errno;
76     }
77     unique_fd device_fd(open(out_device.c_str(), O_RDWR | O_CLOEXEC));
78     if (device_fd.get() == -1) {
79         PLOG(ERROR) << "Failed to open " << out_device;
80         return -errno;
81     }
82 
83     if (ioctl(device_fd.get(), LOOP_SET_FD, target_fd.get()) == -1) {
84         PLOG(ERROR) << "Failed to LOOP_SET_FD";
85         return -errno;
86     }
87 
88     struct loop_info64 li;
89     memset(&li, 0, sizeof(li));
90     strlcpy((char*)li.lo_crypt_name, kVoldPrefix, LO_NAME_SIZE);
91     if (ioctl(device_fd.get(), LOOP_SET_STATUS64, &li) == -1) {
92         PLOG(ERROR) << "Failed to LOOP_SET_STATUS64";
93         return -errno;
94     }
95 
96     return 0;
97 }
98 
destroyByDevice(const char * loopDevice)99 int Loop::destroyByDevice(const char* loopDevice) {
100     int device_fd;
101 
102     device_fd = open(loopDevice, O_RDONLY | O_CLOEXEC);
103     if (device_fd < 0) {
104         PLOG(ERROR) << "Failed to open " << loopDevice;
105         return -1;
106     }
107 
108     if (ioctl(device_fd, LOOP_CLR_FD, 0) < 0) {
109         PLOG(ERROR) << "Failed to destroy " << loopDevice;
110         close(device_fd);
111         return -1;
112     }
113 
114     close(device_fd);
115     return 0;
116 }
117 
destroyAll()118 int Loop::destroyAll() {
119     ATRACE_NAME("Loop::destroyAll");
120 
121     std::string root = "/dev/block/";
122     auto dirp = std::unique_ptr<DIR, int (*)(DIR*)>(opendir(root.c_str()), closedir);
123     if (!dirp) {
124         PLOG(ERROR) << "Failed to opendir";
125         return -1;
126     }
127 
128     // Poke through all devices looking for loops
129     struct dirent* de;
130     while ((de = readdir(dirp.get()))) {
131         auto test = std::string(de->d_name);
132         if (!android::base::StartsWith(test, "loop")) continue;
133 
134         auto path = root + de->d_name;
135         unique_fd fd(open(path.c_str(), O_RDWR | O_CLOEXEC));
136         if (fd.get() == -1) {
137             if (errno != ENOENT) {
138                 PLOG(WARNING) << "Failed to open " << path;
139             }
140             continue;
141         }
142 
143         struct loop_info64 li;
144         if (ioctl(fd.get(), LOOP_GET_STATUS64, &li) < 0) {
145             PLOG(WARNING) << "Failed to LOOP_GET_STATUS64 " << path;
146             continue;
147         }
148 
149         auto id = std::string((char*)li.lo_crypt_name);
150         if (android::base::StartsWith(id, kVoldPrefix)) {
151             LOG(DEBUG) << "Tearing down stale loop device at " << path << " named " << id;
152 
153             if (ioctl(fd.get(), LOOP_CLR_FD, 0) < 0) {
154                 PLOG(WARNING) << "Failed to LOOP_CLR_FD " << path;
155             }
156         } else {
157             LOG(DEBUG) << "Found unmanaged loop device at " << path << " named " << id;
158         }
159     }
160 
161     return 0;
162 }
163 
createImageFile(const char * file,unsigned long numSectors)164 int Loop::createImageFile(const char* file, unsigned long numSectors) {
165     unique_fd fd(open(file, O_CREAT | O_WRONLY | O_TRUNC | O_CLOEXEC, 0600));
166     if (fd.get() == -1) {
167         PLOG(ERROR) << "Failed to create image " << file;
168         return -errno;
169     }
170     if (fallocate(fd.get(), 0, 0, numSectors * 512) == -1) {
171         PLOG(WARNING) << "Failed to fallocate; falling back to ftruncate";
172         if (ftruncate(fd, numSectors * 512) == -1) {
173             PLOG(ERROR) << "Failed to ftruncate";
174             return -errno;
175         }
176     }
177     return 0;
178 }
179 
resizeImageFile(const char * file,unsigned long numSectors)180 int Loop::resizeImageFile(const char* file, unsigned long numSectors) {
181     int fd;
182 
183     if ((fd = open(file, O_RDWR | O_CLOEXEC)) < 0) {
184         PLOG(ERROR) << "Failed to open " << file;
185         return -1;
186     }
187 
188     LOG(DEBUG) << "Attempting to increase " << file << " to " << numSectors;
189 
190     if (fallocate(fd, 0, 0, numSectors * 512)) {
191         if (errno == ENOSYS || errno == ENOTSUP) {
192             PLOG(WARNING) << "fallocate not found. Falling back to ftruncate.";
193             if (ftruncate(fd, numSectors * 512) < 0) {
194                 PLOG(ERROR) << "Failed to ftruncate";
195                 close(fd);
196                 return -1;
197             }
198         } else {
199             PLOG(ERROR) << "Failed to fallocate";
200             close(fd);
201             return -1;
202         }
203     }
204     close(fd);
205     return 0;
206 }
207