2 * Copyright (C) 2015 The Android Open Source Project
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 #include "PublicVolume.h"
19 #include "VolumeManager.h"
23 #include "fs/Iso9660.h"
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>
35 #include <sys/mount.h>
37 #include <sys/types.h>
38 #include <sys/sysmacros.h>
41 using android::base::StringPrintf;
46 static const char* kFusePath = "/system/bin/sdcard";
48 static const char* kAsecPath = "/mnt/secure/asec";
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());
58 PublicVolume::~PublicVolume() {
61 status_t PublicVolume::readMetadata() {
62 status_t res = ReadMetadataUntrusted(mDevPath, &mFsType, &mFsUuid, &mFsLabel);
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(), ' ', '_');
70 auto listener = getListener();
71 if (listener) listener->onVolumeMetadataChanged(getId(), mFsType, mFsUuid, mFsLabel);
76 status_t PublicVolume::initAsecStage() {
77 std::string legacyPath(mRawPath + "/android_secure");
78 std::string securePath(mRawPath + "/.android_secure");
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";
88 if (TEMP_FAILURE_RETRY(mkdir(securePath.c_str(), 0700))) {
89 if (errno != EEXIST) {
90 PLOG(WARNING) << getId() << " creating ASEC stage failed";
95 BindMount(securePath, kAsecPath);
100 status_t PublicVolume::doCreate() {
101 return CreateDeviceNode(mDevPath, mDevice);
104 status_t PublicVolume::doDestroy() {
105 return DestroyDeviceNode(mDevPath);
108 status_t PublicVolume::doMount() {
111 if (!IsFilesystemSupported(mFsType)) {
112 LOG(ERROR) << getId() << " unsupported filesystem " << mFsType;
116 // Use UUID as stable name, if available
117 std::string stableName = getId();
118 if (!mFsUuid.empty()) {
119 stableName = mFsUuid;
122 mRawPath = StringPrintf("/mnt/media_rw/%s", stableName.c_str());
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());
128 setInternalPath(mRawPath);
129 if (getMountFlags() & MountFlags::kVisible) {
130 setPath(StringPrintf("/storage/%s", stableName.c_str()));
135 if (fs_prepare_dir(mRawPath.c_str(), 0700, AID_ROOT, AID_ROOT)) {
136 PLOG(ERROR) << getId() << " failed to create mount points";
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";
155 LOG(ERROR) << getId() << " failed filesystem check";
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,
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);
174 ret = ::mount(mDevPath.c_str(), mRawPath.c_str(), mFsType.c_str(), 0, NULL);
177 PLOG(ERROR) << getId() << " failed to mount " << mDevPath;
181 if (getMountFlags() & MountFlags::kPrimary) {
185 if (!(getMountFlags() & MountFlags::kVisible)) {
186 // Not visible to apps, so no need to spin up FUSE
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";
197 dev_t before = GetDevice(mFuseWrite);
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(),
209 PLOG(ERROR) << "Failed to exec";
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(),
219 PLOG(ERROR) << "Failed to exec";
223 LOG(ERROR) << "FUSE exiting";
227 if (mFusePid == -1) {
228 PLOG(ERROR) << getId() << " failed to fork";
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
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";
243 /* sdcardfs will have exited already. FUSE will still be running */
244 if (TEMP_FAILURE_RETRY(waitpid(mFusePid, nullptr, WNOHANG)) == mFusePid)
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());
257 ForceUnmount(kAsecPath);
259 ForceUnmount(mFuseDefault);
260 ForceUnmount(mFuseRead);
261 ForceUnmount(mFuseWrite);
262 ForceUnmount(mRawPath);
265 kill(mFusePid, SIGTERM);
266 TEMP_FAILURE_RETRY(waitpid(mFusePid, nullptr, 0));
270 rmdir(mFuseDefault.c_str());
271 rmdir(mFuseRead.c_str());
272 rmdir(mFuseWrite.c_str());
273 rmdir(mRawPath.c_str());
275 mFuseDefault.clear();
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;
291 if (WipeBlockDevice(mDevPath) != OK) {
292 LOG(WARNING) << getId() << " failed to wipe";
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);
309 LOG(ERROR) << getId() << " unrecognized filesystem " << fsType;
315 LOG(ERROR) << getId() << " failed to format";
323 } // namespace android