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 <base/stringprintf.h>
24 #include <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>
35 using android::base::StringPrintf;
40 static const char* kFusePath = "/system/bin/sdcard";
42 static const char* kAsecPath = "/mnt/secure/asec";
44 PublicVolume::PublicVolume(dev_t device) :
45 VolumeBase(Type::kPublic), mDevice(device), mFusePid(0) {
46 setId(StringPrintf("public:%u,%u", major(device), minor(device)));
47 mDevPath = StringPrintf("/dev/block/vold/%s", getId().c_str());
50 PublicVolume::~PublicVolume() {
53 status_t PublicVolume::readMetadata() {
54 status_t res = ReadMetadataUntrusted(mDevPath, mFsType, mFsUuid, mFsLabel);
55 notifyEvent(ResponseCode::VolumeFsTypeChanged, mFsType);
56 notifyEvent(ResponseCode::VolumeFsUuidChanged, mFsUuid);
57 notifyEvent(ResponseCode::VolumeFsLabelChanged, mFsLabel);
61 status_t PublicVolume::initAsecStage() {
62 std::string legacyPath(mRawPath + "/android_secure");
63 std::string securePath(mRawPath + "/.android_secure");
65 // Recover legacy secure path
66 if (!access(legacyPath.c_str(), R_OK | X_OK)
67 && access(securePath.c_str(), R_OK | X_OK)) {
68 if (rename(legacyPath.c_str(), securePath.c_str())) {
69 PLOG(WARNING) << getId() << " failed to rename legacy ASEC dir";
73 if (TEMP_FAILURE_RETRY(mkdir(securePath.c_str(), 0700))) {
74 if (errno != EEXIST) {
75 PLOG(WARNING) << getId() << " creating ASEC stage failed";
80 BindMount(securePath, kAsecPath);
85 status_t PublicVolume::doCreate() {
86 return CreateDeviceNode(mDevPath, mDevice);
89 status_t PublicVolume::doDestroy() {
90 return DestroyDeviceNode(mDevPath);
93 status_t PublicVolume::doMount() {
94 // TODO: expand to support mounting other filesystems
97 if (mFsType != "vfat") {
98 LOG(ERROR) << getId() << " unsupported filesystem " << mFsType;
102 if (vfat::Check(mDevPath)) {
103 LOG(ERROR) << getId() << " failed filesystem check";
107 // Use UUID as stable name, if available
108 std::string stableName = getId();
109 if (!mFsUuid.empty()) {
110 stableName = mFsUuid;
113 mRawPath = StringPrintf("/mnt/media_rw/%s", stableName.c_str());
115 mFuseDefault = StringPrintf("/mnt/runtime_default/%s", stableName.c_str());
116 mFuseRead = StringPrintf("/mnt/runtime_read/%s", stableName.c_str());
117 mFuseWrite = StringPrintf("/mnt/runtime_write/%s", stableName.c_str());
119 setInternalPath(mRawPath);
120 setPath(StringPrintf("/storage/%s", stableName.c_str()));
122 if (fs_prepare_dir(mRawPath.c_str(), 0700, AID_ROOT, AID_ROOT) ||
123 fs_prepare_dir(mFuseDefault.c_str(), 0700, AID_ROOT, AID_ROOT) ||
124 fs_prepare_dir(mFuseRead.c_str(), 0700, AID_ROOT, AID_ROOT) ||
125 fs_prepare_dir(mFuseWrite.c_str(), 0700, AID_ROOT, AID_ROOT)) {
126 PLOG(ERROR) << getId() << " failed to create mount points";
130 if (vfat::Mount(mDevPath, mRawPath, false, false, false,
131 AID_MEDIA_RW, AID_MEDIA_RW, 0007, true)) {
132 PLOG(ERROR) << getId() << " failed to mount " << mDevPath;
136 if (getMountFlags() & MountFlags::kPrimary) {
140 if (!(getMountFlags() & MountFlags::kVisible)) {
141 // Not visible to apps, so no need to spin up FUSE
145 dev_t before = GetDevice(mFuseWrite);
147 if (!(mFusePid = fork())) {
148 if (getMountFlags() & MountFlags::kPrimary) {
149 if (execl(kFusePath, kFusePath,
150 "-u", "1023", // AID_MEDIA_RW
151 "-g", "1023", // AID_MEDIA_RW
156 PLOG(ERROR) << "Failed to exec";
159 if (execl(kFusePath, kFusePath,
160 "-u", "1023", // AID_MEDIA_RW
161 "-g", "1023", // AID_MEDIA_RW
165 PLOG(ERROR) << "Failed to exec";
169 LOG(ERROR) << "FUSE exiting";
173 if (mFusePid == -1) {
174 PLOG(ERROR) << getId() << " failed to fork";
178 while (before == GetDevice(mFuseWrite)) {
179 LOG(VERBOSE) << "Waiting for FUSE to spin up...";
180 usleep(50000); // 50ms
186 status_t PublicVolume::doUnmount() {
188 kill(mFusePid, SIGTERM);
189 TEMP_FAILURE_RETRY(waitpid(mFusePid, nullptr, 0));
193 ForceUnmount(kAsecPath);
195 ForceUnmount(mFuseDefault);
196 ForceUnmount(mFuseRead);
197 ForceUnmount(mFuseWrite);
198 ForceUnmount(mRawPath);
200 rmdir(mFuseDefault.c_str());
201 rmdir(mFuseRead.c_str());
202 rmdir(mFuseWrite.c_str());
203 rmdir(mRawPath.c_str());
205 mFuseDefault.clear();
213 status_t PublicVolume::doFormat(const std::string& fsType) {
214 if (fsType == "vfat" || fsType == "auto") {
215 if (WipeBlockDevice(mDevPath) != OK) {
216 LOG(WARNING) << getId() << " failed to wipe";
218 if (vfat::Format(mDevPath, 0)) {
219 LOG(ERROR) << getId() << " failed to format";
223 LOG(ERROR) << "Unsupported filesystem " << fsType;
231 } // namespace android