OSDN Git Service

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