OSDN Git Service

e6fda8fb4635ad79aabcd27cadfeeadfd5fe5a5f
[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 <base/stringprintf.h>
28 #include <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 #ifdef MINIVOLD
45 static const char* kFusePath = "/sbin/sdcard";
46 #else
47 static const char* kFusePath = "/system/bin/sdcard";
48 #endif
49
50 static const char* kAsecPath = "/mnt/secure/asec";
51
52 PublicVolume::PublicVolume(dev_t device, const std::string& nickname,
53                 const std::string& fstype /* = "" */,
54                 const std::string& mntopts /* = "" */) :
55         VolumeBase(Type::kPublic), mDevice(device), mFusePid(0),
56         mFsType(fstype), mFsLabel(nickname), mMntOpts(mntopts) {
57     setId(StringPrintf("public:%u_%u", major(device), minor(device)));
58     mDevPath = StringPrintf("/dev/block/vold/%s", getId().c_str());
59 }
60
61 PublicVolume::~PublicVolume() {
62 }
63
64 status_t PublicVolume::readMetadata() {
65     status_t res = ReadMetadataUntrusted(mDevPath, mFsType, mFsUuid, mFsLabel);
66     notifyEvent(ResponseCode::VolumeFsTypeChanged, mFsType);
67     notifyEvent(ResponseCode::VolumeFsUuidChanged, mFsUuid);
68     notifyEvent(ResponseCode::VolumeFsLabelChanged, mFsLabel);
69     return res;
70 }
71
72 status_t PublicVolume::initAsecStage() {
73     std::string legacyPath(mRawPath + "/android_secure");
74     std::string securePath(mRawPath + "/.android_secure");
75
76     // Recover legacy secure path
77     if (!access(legacyPath.c_str(), R_OK | X_OK)
78             && access(securePath.c_str(), R_OK | X_OK)) {
79         if (rename(legacyPath.c_str(), securePath.c_str())) {
80             PLOG(WARNING) << getId() << " failed to rename legacy ASEC dir";
81         }
82     }
83
84     if (TEMP_FAILURE_RETRY(mkdir(securePath.c_str(), 0700))) {
85         if (errno != EEXIST) {
86             PLOG(WARNING) << getId() << " creating ASEC stage failed";
87             return -errno;
88         }
89     }
90
91     BindMount(securePath, kAsecPath);
92
93     return OK;
94 }
95
96 status_t PublicVolume::doCreate() {
97     if (mFsLabel.size() > 0) {
98         notifyEvent(ResponseCode::VolumeFsLabelChanged, mFsLabel);
99     }
100     return CreateDeviceNode(mDevPath, mDevice);
101 }
102
103 status_t PublicVolume::doDestroy() {
104     return DestroyDeviceNode(mDevPath);
105 }
106
107 status_t PublicVolume::doMount() {
108     // TODO: expand to support mounting other filesystems
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 #ifdef MINIVOLD
123     // In recovery, directly mount to /storage/* since we have no fuse daemon
124     mRawPath = StringPrintf("/storage/%s", stableName.c_str());
125     mFuseDefault = StringPrintf("/storage/%s", stableName.c_str());
126     mFuseRead = StringPrintf("/storage/%s", stableName.c_str());
127     mFuseWrite = StringPrintf("/storage/%s", stableName.c_str());
128 #else
129     mRawPath = StringPrintf("/mnt/media_rw/%s", stableName.c_str());
130     mFuseDefault = StringPrintf("/mnt/runtime/default/%s", stableName.c_str());
131     mFuseRead = StringPrintf("/mnt/runtime/read/%s", stableName.c_str());
132     mFuseWrite = StringPrintf("/mnt/runtime/write/%s", stableName.c_str());
133 #endif
134
135     setInternalPath(mRawPath);
136     if (getMountFlags() & MountFlags::kVisible) {
137         setPath(StringPrintf("/storage/%s", stableName.c_str()));
138     } else {
139         setPath(mRawPath);
140     }
141
142     if (fs_prepare_dir(mRawPath.c_str(), 0700, AID_ROOT, AID_ROOT) ||
143             fs_prepare_dir(mFuseDefault.c_str(), 0700, AID_ROOT, AID_ROOT) ||
144             fs_prepare_dir(mFuseRead.c_str(), 0700, AID_ROOT, AID_ROOT) ||
145             fs_prepare_dir(mFuseWrite.c_str(), 0700, AID_ROOT, AID_ROOT)) {
146         PLOG(ERROR) << getId() << " failed to create mount points";
147         return -errno;
148     }
149
150     int ret = 0;
151     if (mFsType == "exfat") {
152         ret = exfat::Check(mDevPath);
153     } else if (mFsType == "ext4") {
154         ret = ext4::Check(mDevPath, mRawPath, false);
155     } else if (mFsType == "f2fs") {
156         ret = f2fs::Check(mDevPath, false);
157     } else if (mFsType == "ntfs") {
158         ret = ntfs::Check(mDevPath);
159     } else if (mFsType == "vfat") {
160         ret = vfat::Check(mDevPath);
161     } else {
162         LOG(WARNING) << getId() << " unsupported filesystem check, skipping";
163     }
164     if (ret) {
165         LOG(ERROR) << getId() << " failed filesystem check";
166         return -EIO;
167     }
168
169     if (mFsType == "exfat") {
170         ret = exfat::Mount(mDevPath, mRawPath, false, false, false,
171                 AID_MEDIA_RW, AID_MEDIA_RW, 0007);
172     } else if (mFsType == "ext4") {
173         ret = ext4::Mount(mDevPath, mRawPath, false, false, true, mMntOpts,
174                 false);
175     } else if (mFsType == "f2fs") {
176         ret = f2fs::Mount(mDevPath, mRawPath, mMntOpts, false);
177     } else if (mFsType == "ntfs") {
178         ret = ntfs::Mount(mDevPath, mRawPath, false, false, false,
179                 AID_MEDIA_RW, AID_MEDIA_RW, 0007, true);
180     } else if (mFsType == "vfat") {
181         ret = vfat::Mount(mDevPath, mRawPath, false, false, false,
182                 AID_MEDIA_RW, AID_MEDIA_RW, 0007, true);
183     } else {
184         ret = ::mount(mDevPath.c_str(), mRawPath.c_str(), mFsType.c_str(), 0, NULL);
185     }
186     if (ret) {
187         PLOG(ERROR) << getId() << " failed to mount " << mDevPath;
188         return -EIO;
189     }
190
191 #ifdef MINIVOLD
192     // In recovery, don't setup ASEC or FUSE
193     return OK;
194 #endif
195
196     if (getMountFlags() & MountFlags::kPrimary) {
197         initAsecStage();
198     }
199
200     if (!(getMountFlags() & MountFlags::kVisible)) {
201         // Not visible to apps, so no need to spin up FUSE
202         return OK;
203     }
204
205     dev_t before = GetDevice(mFuseWrite);
206
207     if (!(mFusePid = fork())) {
208         if (getMountFlags() & MountFlags::kPrimary) {
209             if (execl(kFusePath, kFusePath,
210                     "-u", "1023", // AID_MEDIA_RW
211                     "-g", "1023", // AID_MEDIA_RW
212                     "-U", std::to_string(getMountUserId()).c_str(),
213                     "-w",
214                     mRawPath.c_str(),
215                     stableName.c_str(),
216                     NULL)) {
217                 PLOG(ERROR) << "Failed to exec";
218             }
219         } else {
220             if (execl(kFusePath, kFusePath,
221                     "-u", "1023", // AID_MEDIA_RW
222                     "-g", "1023", // AID_MEDIA_RW
223                     "-U", std::to_string(getMountUserId()).c_str(),
224                     mRawPath.c_str(),
225                     stableName.c_str(),
226                     NULL)) {
227                 PLOG(ERROR) << "Failed to exec";
228             }
229         }
230
231         LOG(ERROR) << "FUSE exiting";
232         _exit(1);
233     }
234
235     if (mFusePid == -1) {
236         PLOG(ERROR) << getId() << " failed to fork";
237         return -errno;
238     }
239
240     while (before == GetDevice(mFuseWrite)) {
241         LOG(VERBOSE) << "Waiting for FUSE to spin up...";
242         usleep(50000); // 50ms
243     }
244
245     return OK;
246 }
247
248 status_t PublicVolume::doUnmount(bool detach /* = false */) {
249     if (mFusePid > 0) {
250         kill(mFusePid, SIGTERM);
251         TEMP_FAILURE_RETRY(waitpid(mFusePid, nullptr, 0));
252         mFusePid = 0;
253     }
254
255 #ifndef MINIVOLD
256     ForceUnmount(kAsecPath);
257
258     ForceUnmount(mFuseDefault);
259     ForceUnmount(mFuseRead);
260     ForceUnmount(mFuseWrite);
261 #endif
262
263     ForceUnmount(mRawPath, detach);
264
265     rmdir(mFuseDefault.c_str());
266     rmdir(mFuseRead.c_str());
267     rmdir(mFuseWrite.c_str());
268     rmdir(mRawPath.c_str());
269
270     mFuseDefault.clear();
271     mFuseRead.clear();
272     mFuseWrite.clear();
273     mRawPath.clear();
274
275     return OK;
276 }
277
278 status_t PublicVolume::doFormat(const std::string& fsType) {
279     // "auto" is used for newly partitioned disks (see Disk::partition*)
280     // and thus is restricted to external/removable storage.
281     if (!(IsFilesystemSupported(fsType) || fsType == "auto")) {
282         LOG(ERROR) << "Unsupported filesystem " << fsType;
283         return -EINVAL;
284     }
285
286     if (WipeBlockDevice(mDevPath) != OK) {
287         LOG(WARNING) << getId() << " failed to wipe";
288     }
289
290     int ret = 0;
291     if (fsType == "auto") {
292         ret = vfat::Format(mDevPath, 0);
293     } else if (fsType == "exfat") {
294         ret = exfat::Format(mDevPath);
295     } else if (fsType == "ext4") {
296         ret = ext4::Format(mDevPath, 0, mRawPath);
297     } else if (fsType == "f2fs") {
298         ret = f2fs::Format(mDevPath);
299     } else if (fsType == "ntfs") {
300         ret = ntfs::Format(mDevPath, 0);
301     } else if (fsType == "vfat") {
302         ret = vfat::Format(mDevPath, 0);
303     } else {
304         LOG(ERROR) << getId() << " unrecognized filesystem " << fsType;
305         ret = -1;
306         errno = EIO;
307     }
308
309     if (ret) {
310         LOG(ERROR) << getId() << " failed to format";
311         return -errno;
312     }
313
314     return OK;
315 }
316
317 }  // namespace vold
318 }  // namespace android