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.
18 #include "PublicVolume.h"
20 #include "VolumeManager.h"
21 #include "ResponseCode.h"
23 #include <android-base/stringprintf.h>
24 #include <android-base/logging.h>
25 #include <cutils/fs.h>
26 #include <private/android_filesystem_config.h>
30 #include <sys/mount.h>
32 #include <sys/types.h>
33 #include <sys/sysmacros.h>
36 using android::base::StringPrintf;
41 static const char* kFusePath = "/system/bin/sdcard";
43 static const char* kAsecPath = "/mnt/secure/asec";
45 PublicVolume::PublicVolume(dev_t device) :
46 VolumeBase(Type::kPublic), mDevice(device), mFusePid(0) {
47 setId(StringPrintf("public:%u,%u", major(device), minor(device)));
48 mDevPath = StringPrintf("/dev/block/vold/%s", getId().c_str());
51 PublicVolume::~PublicVolume() {
54 status_t PublicVolume::readMetadata() {
55 status_t res = ReadMetadataUntrusted(mDevPath, mFsType, mFsUuid, mFsLabel);
56 notifyEvent(ResponseCode::VolumeFsTypeChanged, mFsType);
57 notifyEvent(ResponseCode::VolumeFsUuidChanged, mFsUuid);
58 notifyEvent(ResponseCode::VolumeFsLabelChanged, mFsLabel);
62 status_t PublicVolume::initAsecStage() {
63 std::string legacyPath(mRawPath + "/android_secure");
64 std::string securePath(mRawPath + "/.android_secure");
66 // Recover legacy secure path
67 if (!access(legacyPath.c_str(), R_OK | X_OK)
68 && access(securePath.c_str(), R_OK | X_OK)) {
69 if (rename(legacyPath.c_str(), securePath.c_str())) {
70 PLOG(WARNING) << getId() << " failed to rename legacy ASEC dir";
74 if (TEMP_FAILURE_RETRY(mkdir(securePath.c_str(), 0700))) {
75 if (errno != EEXIST) {
76 PLOG(WARNING) << getId() << " creating ASEC stage failed";
81 BindMount(securePath, kAsecPath);
86 status_t PublicVolume::doCreate() {
87 return CreateDeviceNode(mDevPath, mDevice);
90 status_t PublicVolume::doDestroy() {
91 return DestroyDeviceNode(mDevPath);
94 status_t PublicVolume::doMount() {
95 // TODO: expand to support mounting other filesystems
98 if (mFsType != "vfat") {
99 LOG(ERROR) << getId() << " unsupported filesystem " << mFsType;
103 if (vfat::Check(mDevPath)) {
104 LOG(ERROR) << getId() << " failed filesystem check";
108 // Use UUID as stable name, if available
109 std::string stableName = getId();
110 if (!mFsUuid.empty()) {
111 stableName = mFsUuid;
114 mRawPath = StringPrintf("/mnt/media_rw/%s", stableName.c_str());
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());
120 setInternalPath(mRawPath);
121 if (getMountFlags() & MountFlags::kVisible) {
122 setPath(StringPrintf("/storage/%s", stableName.c_str()));
127 if (fs_prepare_dir(mRawPath.c_str(), 0700, AID_ROOT, AID_ROOT)) {
128 PLOG(ERROR) << getId() << " failed to create mount points";
132 if (vfat::Mount(mDevPath, mRawPath, false, false, false,
133 AID_MEDIA_RW, AID_MEDIA_RW, 0007, true)) {
134 PLOG(ERROR) << getId() << " failed to mount " << mDevPath;
138 if (getMountFlags() & MountFlags::kPrimary) {
142 if (!(getMountFlags() & MountFlags::kVisible)) {
143 // Not visible to apps, so no need to spin up FUSE
147 if (fs_prepare_dir(mFuseDefault.c_str(), 0700, AID_ROOT, AID_ROOT) ||
148 fs_prepare_dir(mFuseRead.c_str(), 0700, AID_ROOT, AID_ROOT) ||
149 fs_prepare_dir(mFuseWrite.c_str(), 0700, AID_ROOT, AID_ROOT)) {
150 PLOG(ERROR) << getId() << " failed to create FUSE mount points";
154 dev_t before = GetDevice(mFuseWrite);
156 if (!(mFusePid = fork())) {
157 if (getMountFlags() & MountFlags::kPrimary) {
158 if (execl(kFusePath, kFusePath,
159 "-u", "1023", // AID_MEDIA_RW
160 "-g", "1023", // AID_MEDIA_RW
161 "-U", std::to_string(getMountUserId()).c_str(),
166 PLOG(ERROR) << "Failed to exec";
169 if (execl(kFusePath, kFusePath,
170 "-u", "1023", // AID_MEDIA_RW
171 "-g", "1023", // AID_MEDIA_RW
172 "-U", std::to_string(getMountUserId()).c_str(),
176 PLOG(ERROR) << "Failed to exec";
180 LOG(ERROR) << "FUSE exiting";
184 if (mFusePid == -1) {
185 PLOG(ERROR) << getId() << " failed to fork";
189 while (before == GetDevice(mFuseWrite)) {
190 LOG(VERBOSE) << "Waiting for FUSE to spin up...";
191 usleep(50000); // 50ms
197 status_t PublicVolume::doUnmount() {
198 // Unmount the storage before we kill the FUSE process. If we kill
199 // the FUSE process first, most file system operations will return
200 // ENOTCONN until the unmount completes. This is an exotic and unusual
201 // error code and might cause broken behaviour in applications.
202 KillProcessesUsingPath(getPath());
204 ForceUnmount(kAsecPath);
206 ForceUnmount(mFuseDefault);
207 ForceUnmount(mFuseRead);
208 ForceUnmount(mFuseWrite);
209 ForceUnmount(mRawPath);
212 kill(mFusePid, SIGTERM);
213 TEMP_FAILURE_RETRY(waitpid(mFusePid, nullptr, 0));
217 rmdir(mFuseDefault.c_str());
218 rmdir(mFuseRead.c_str());
219 rmdir(mFuseWrite.c_str());
220 rmdir(mRawPath.c_str());
222 mFuseDefault.clear();
230 status_t PublicVolume::doFormat(const std::string& fsType) {
231 if (fsType == "vfat" || fsType == "auto") {
232 if (WipeBlockDevice(mDevPath) != OK) {
233 LOG(WARNING) << getId() << " failed to wipe";
235 if (vfat::Format(mDevPath, 0)) {
236 LOG(ERROR) << getId() << " failed to format";
240 LOG(ERROR) << "Unsupported filesystem " << fsType;
248 } // namespace android