OSDN Git Service

5c911758cb3522f94685d3ef7483942ecc4c1096
[android-x86/system-vold.git] / model / PublicVolume.cpp
1 /*
2  * Copyright (C) 2015 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 #include "PublicVolume.h"
18 #include "Utils.h"
19 #include "VolumeManager.h"
20 #include "fs/Exfat.h"
21 #include "fs/Ext4.h"
22 #include "fs/F2fs.h"
23 #include "fs/Iso9660.h"
24 #include "fs/Ntfs.h"
25 #include "fs/Vfat.h"
26
27 #include <android-base/stringprintf.h>
28 #include <android-base/logging.h>
29 #include <cutils/fs.h>
30 #include <private/android_filesystem_config.h>
31 #include <utils/Timers.h>
32
33 #include <fcntl.h>
34 #include <stdlib.h>
35 #include <sys/mount.h>
36 #include <sys/stat.h>
37 #include <sys/types.h>
38 #include <sys/sysmacros.h>
39 #include <sys/wait.h>
40
41 using android::base::StringPrintf;
42
43 namespace android {
44 namespace vold {
45
46 static const char* kFusePath = "/system/bin/sdcard";
47
48 static const char* kAsecPath = "/mnt/secure/asec";
49
50 PublicVolume::PublicVolume(dev_t device, const std::string& fstype /* = "" */,
51                 const std::string& mntopts /* = "" */) :
52         VolumeBase(Type::kPublic), mDevice(device), mFusePid(0),
53         mFsType(fstype), mMntOpts(mntopts) {
54     setId(StringPrintf("public:%u,%u", major(device), minor(device)));
55     mDevPath = StringPrintf("/dev/block/vold/%s", getId().c_str());
56 }
57
58 PublicVolume::~PublicVolume() {
59 }
60
61 status_t PublicVolume::readMetadata() {
62     status_t res = ReadMetadataUntrusted(mDevPath, &mFsType, &mFsUuid, &mFsLabel);
63
64     // iso9660 has no UUID, we use label as UUID
65     if (mFsType == "iso9660" && mFsUuid.empty() && !mFsLabel.empty()) {
66         std::replace(mFsLabel.begin(), mFsLabel.end(), ' ', '_');
67         mFsUuid = mFsLabel;
68     }
69
70     auto listener = getListener();
71     if (listener) listener->onVolumeMetadataChanged(getId(), mFsType, mFsUuid, mFsLabel);
72
73     return res;
74 }
75
76 status_t PublicVolume::initAsecStage() {
77     std::string legacyPath(mRawPath + "/android_secure");
78     std::string securePath(mRawPath + "/.android_secure");
79
80     // Recover legacy secure path
81     if (!access(legacyPath.c_str(), R_OK | X_OK)
82             && access(securePath.c_str(), R_OK | X_OK)) {
83         if (rename(legacyPath.c_str(), securePath.c_str())) {
84             PLOG(WARNING) << getId() << " failed to rename legacy ASEC dir";
85         }
86     }
87
88     if (TEMP_FAILURE_RETRY(mkdir(securePath.c_str(), 0700))) {
89         if (errno != EEXIST) {
90             PLOG(WARNING) << getId() << " creating ASEC stage failed";
91             return -errno;
92         }
93     }
94
95     BindMount(securePath, kAsecPath);
96
97     return OK;
98 }
99
100 status_t PublicVolume::doCreate() {
101     return CreateDeviceNode(mDevPath, mDevice);
102 }
103
104 status_t PublicVolume::doDestroy() {
105     return DestroyDeviceNode(mDevPath);
106 }
107
108 status_t PublicVolume::doMount() {
109     readMetadata();
110
111     if (!IsFilesystemSupported(mFsType)) {
112         LOG(ERROR) << getId() << " unsupported filesystem " << mFsType;
113         return -EIO;
114     }
115
116     // Use UUID as stable name, if available
117     std::string stableName = getId();
118     if (!mFsUuid.empty()) {
119         stableName = mFsUuid;
120     }
121
122     mRawPath = StringPrintf("/mnt/media_rw/%s", stableName.c_str());
123
124     mFuseDefault = StringPrintf("/mnt/runtime/default/%s", stableName.c_str());
125     mFuseRead = StringPrintf("/mnt/runtime/read/%s", stableName.c_str());
126     mFuseWrite = StringPrintf("/mnt/runtime/write/%s", stableName.c_str());
127
128     setInternalPath(mRawPath);
129     if (getMountFlags() & MountFlags::kVisible) {
130         setPath(StringPrintf("/storage/%s", stableName.c_str()));
131     } else {
132         setPath(mRawPath);
133     }
134
135     if (fs_prepare_dir(mRawPath.c_str(), 0700, AID_ROOT, AID_ROOT)) {
136         PLOG(ERROR) << getId() << " failed to create mount points";
137         return -errno;
138     }
139
140     int ret = 0;
141     if (mFsType == "exfat") {
142         ret = exfat::Check(mDevPath);
143     } else if (mFsType == "ext4") {
144         ret = ext4::Check(mDevPath, mRawPath, false);
145     } else if (mFsType == "f2fs") {
146         ret = f2fs::Check(mDevPath, false);
147     } else if (mFsType == "ntfs") {
148         ret = ntfs::Check(mDevPath);
149     } else if (mFsType == "vfat") {
150         ret = vfat::Check(mDevPath);
151     } else if (mFsType != "iso9660") {
152         LOG(WARNING) << getId() << " unsupported filesystem check, skipping";
153     }
154     if (ret) {
155         LOG(ERROR) << getId() << " failed filesystem check";
156         return -EIO;
157     }
158
159     if (mFsType == "exfat") {
160         ret = exfat::Mount(mDevPath, mRawPath, AID_MEDIA_RW, AID_MEDIA_RW, 0007);
161     } else if (mFsType == "ext4") {
162         ret = ext4::Mount(mDevPath, mRawPath, false, false, true, mMntOpts,
163                 false, true);
164     } else if (mFsType == "f2fs") {
165         ret = f2fs::Mount(mDevPath, mRawPath, mMntOpts, false, true);
166     } else if (mFsType == "iso9660") {
167         ret = iso9660::Mount(mDevPath, mRawPath, AID_MEDIA_RW, AID_MEDIA_RW);
168     } else if (mFsType == "ntfs") {
169         ret = ntfs::Mount(mDevPath, mRawPath, AID_MEDIA_RW, AID_MEDIA_RW, 0007);
170     } else if (mFsType == "vfat") {
171         ret = vfat::Mount(mDevPath, mRawPath, false, false, false,
172                 AID_MEDIA_RW, AID_MEDIA_RW, 0007, true);
173     } else {
174         ret = ::mount(mDevPath.c_str(), mRawPath.c_str(), mFsType.c_str(), 0, NULL);
175     }
176     if (ret) {
177         PLOG(ERROR) << getId() << " failed to mount " << mDevPath;
178         return -EIO;
179     }
180
181     if (getMountFlags() & MountFlags::kPrimary) {
182         initAsecStage();
183     }
184
185     if (!(getMountFlags() & MountFlags::kVisible)) {
186         // Not visible to apps, so no need to spin up FUSE
187         return OK;
188     }
189
190     if (fs_prepare_dir(mFuseDefault.c_str(), 0700, AID_ROOT, AID_ROOT) ||
191             fs_prepare_dir(mFuseRead.c_str(), 0700, AID_ROOT, AID_ROOT) ||
192             fs_prepare_dir(mFuseWrite.c_str(), 0700, AID_ROOT, AID_ROOT)) {
193         PLOG(ERROR) << getId() << " failed to create FUSE mount points";
194         return -errno;
195     }
196
197     dev_t before = GetDevice(mFuseWrite);
198
199     if (!(mFusePid = fork())) {
200         if (getMountFlags() & MountFlags::kPrimary) {
201             if (execl(kFusePath, kFusePath,
202                     "-u", "1023", // AID_MEDIA_RW
203                     "-g", "1023", // AID_MEDIA_RW
204                     "-U", std::to_string(getMountUserId()).c_str(),
205                     "-w",
206                     mRawPath.c_str(),
207                     stableName.c_str(),
208                     NULL)) {
209                 PLOG(ERROR) << "Failed to exec";
210             }
211         } else {
212             if (execl(kFusePath, kFusePath,
213                     "-u", "1023", // AID_MEDIA_RW
214                     "-g", "1023", // AID_MEDIA_RW
215                     "-U", std::to_string(getMountUserId()).c_str(),
216                     mRawPath.c_str(),
217                     stableName.c_str(),
218                     NULL)) {
219                 PLOG(ERROR) << "Failed to exec";
220             }
221         }
222
223         LOG(ERROR) << "FUSE exiting";
224         _exit(1);
225     }
226
227     if (mFusePid == -1) {
228         PLOG(ERROR) << getId() << " failed to fork";
229         return -errno;
230     }
231
232     nsecs_t start = systemTime(SYSTEM_TIME_BOOTTIME);
233     while (before == GetDevice(mFuseWrite)) {
234         LOG(VERBOSE) << "Waiting for FUSE to spin up...";
235         usleep(50000); // 50ms
236
237         nsecs_t now = systemTime(SYSTEM_TIME_BOOTTIME);
238         if (nanoseconds_to_milliseconds(now - start) > 5000) {
239             LOG(WARNING) << "Timed out while waiting for FUSE to spin up";
240             return -ETIMEDOUT;
241         }
242     }
243     /* sdcardfs will have exited already. FUSE will still be running */
244     if (TEMP_FAILURE_RETRY(waitpid(mFusePid, nullptr, WNOHANG)) == mFusePid)
245         mFusePid = 0;
246
247     return OK;
248 }
249
250 status_t PublicVolume::doUnmount() {
251     // Unmount the storage before we kill the FUSE process. If we kill
252     // the FUSE process first, most file system operations will return
253     // ENOTCONN until the unmount completes. This is an exotic and unusual
254     // error code and might cause broken behaviour in applications.
255     KillProcessesUsingPath(getPath());
256
257     ForceUnmount(kAsecPath);
258
259     ForceUnmount(mFuseDefault);
260     ForceUnmount(mFuseRead);
261     ForceUnmount(mFuseWrite);
262     ForceUnmount(mRawPath);
263
264     if (mFusePid > 0) {
265         kill(mFusePid, SIGTERM);
266         TEMP_FAILURE_RETRY(waitpid(mFusePid, nullptr, 0));
267         mFusePid = 0;
268     }
269
270     rmdir(mFuseDefault.c_str());
271     rmdir(mFuseRead.c_str());
272     rmdir(mFuseWrite.c_str());
273     rmdir(mRawPath.c_str());
274
275     mFuseDefault.clear();
276     mFuseRead.clear();
277     mFuseWrite.clear();
278     mRawPath.clear();
279
280     return OK;
281 }
282
283 status_t PublicVolume::doFormat(const std::string& fsType) {
284     // "auto" is used for newly partitioned disks (see Disk::partition*)
285     // and thus is restricted to external/removable storage.
286     if (!(IsFilesystemSupported(fsType) || fsType == "auto")) {
287         LOG(ERROR) << "Unsupported filesystem " << fsType;
288         return -EINVAL;
289     }
290
291     if (WipeBlockDevice(mDevPath) != OK) {
292         LOG(WARNING) << getId() << " failed to wipe";
293     }
294
295     int ret = 0;
296     if (fsType == "auto") {
297         ret = vfat::Format(mDevPath, 0);
298     } else if (fsType == "exfat") {
299         ret = exfat::Format(mDevPath);
300     } else if (fsType == "ext4") {
301         ret = ext4::Format(mDevPath, 0, mRawPath);
302     } else if (fsType == "f2fs") {
303         ret = f2fs::Format(mDevPath);
304     } else if (fsType == "ntfs") {
305         ret = ntfs::Format(mDevPath);
306     } else if (fsType == "vfat") {
307         ret = vfat::Format(mDevPath, 0);
308     } else {
309         LOG(ERROR) << getId() << " unrecognized filesystem " << fsType;
310         ret = -1;
311         errno = EIO;
312     }
313
314     if (ret) {
315         LOG(ERROR) << getId() << " failed to format";
316         return -errno;
317     }
318
319     return OK;
320 }
321
322 }  // namespace vold
323 }  // namespace android