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.
22 #include "PublicVolume.h"
24 #include "VolumeManager.h"
25 #include "ResponseCode.h"
27 #include <base/stringprintf.h>
28 #include <base/logging.h>
29 #include <cutils/fs.h>
30 #include <private/android_filesystem_config.h>
34 #include <sys/mount.h>
36 #include <sys/types.h>
39 using android::base::StringPrintf;
45 static const char* kFusePath = "/sbin/sdcard";
47 static const char* kFusePath = "/system/bin/sdcard";
50 static const char* kAsecPath = "/mnt/secure/asec";
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());
61 PublicVolume::~PublicVolume() {
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);
72 status_t PublicVolume::initAsecStage() {
73 std::string legacyPath(mRawPath + "/android_secure");
74 std::string securePath(mRawPath + "/.android_secure");
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";
84 if (TEMP_FAILURE_RETRY(mkdir(securePath.c_str(), 0700))) {
85 if (errno != EEXIST) {
86 PLOG(WARNING) << getId() << " creating ASEC stage failed";
91 BindMount(securePath, kAsecPath);
96 status_t PublicVolume::doCreate() {
97 if (mFsLabel.size() > 0) {
98 notifyEvent(ResponseCode::VolumeFsLabelChanged, mFsLabel);
100 return CreateDeviceNode(mDevPath, mDevice);
103 status_t PublicVolume::doDestroy() {
104 return DestroyDeviceNode(mDevPath);
107 status_t PublicVolume::doMount() {
108 // TODO: expand to support mounting other filesystems
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;
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());
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());
135 setInternalPath(mRawPath);
136 if (getMountFlags() & MountFlags::kVisible) {
137 setPath(StringPrintf("/storage/%s", stableName.c_str()));
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";
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);
162 LOG(WARNING) << getId() << " unsupported filesystem check, skipping";
165 LOG(ERROR) << getId() << " failed filesystem check";
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,
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);
184 ret = ::mount(mDevPath.c_str(), mRawPath.c_str(), mFsType.c_str(), 0, NULL);
187 PLOG(ERROR) << getId() << " failed to mount " << mDevPath;
192 // In recovery, don't setup ASEC or FUSE
196 if (getMountFlags() & MountFlags::kPrimary) {
200 if (!(getMountFlags() & MountFlags::kVisible)) {
201 // Not visible to apps, so no need to spin up FUSE
205 dev_t before = GetDevice(mFuseWrite);
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(),
217 PLOG(ERROR) << "Failed to exec";
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(),
227 PLOG(ERROR) << "Failed to exec";
231 LOG(ERROR) << "FUSE exiting";
235 if (mFusePid == -1) {
236 PLOG(ERROR) << getId() << " failed to fork";
240 while (before == GetDevice(mFuseWrite)) {
241 LOG(VERBOSE) << "Waiting for FUSE to spin up...";
242 usleep(50000); // 50ms
248 status_t PublicVolume::doUnmount(bool detach /* = false */) {
250 kill(mFusePid, SIGTERM);
251 TEMP_FAILURE_RETRY(waitpid(mFusePid, nullptr, 0));
256 ForceUnmount(kAsecPath);
258 ForceUnmount(mFuseDefault);
259 ForceUnmount(mFuseRead);
260 ForceUnmount(mFuseWrite);
263 ForceUnmount(mRawPath, detach);
265 rmdir(mFuseDefault.c_str());
266 rmdir(mFuseRead.c_str());
267 rmdir(mFuseWrite.c_str());
268 rmdir(mRawPath.c_str());
270 mFuseDefault.clear();
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;
286 if (WipeBlockDevice(mDevPath) != OK) {
287 LOG(WARNING) << getId() << " failed to wipe";
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);
304 LOG(ERROR) << getId() << " unrecognized filesystem " << fsType;
310 LOG(ERROR) << getId() << " failed to format";
318 } // namespace android