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 <android-base/stringprintf.h>
28 #include <android-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;
44 static const char* kFusePath = "/system/bin/sdcard";
46 static const char* kAsecPath = "/mnt/secure/asec";
48 PublicVolume::PublicVolume(dev_t device, const std::string& fstype /* = "" */,
49 const std::string& mntopts /* = "" */) :
50 VolumeBase(Type::kPublic), mDevice(device), mFusePid(0),
51 mFsType(fstype), mMntOpts(mntopts) {
52 setId(StringPrintf("public:%u,%u", major(device), minor(device)));
53 mDevPath = StringPrintf("/dev/block/vold/%s", getId().c_str());
56 PublicVolume::~PublicVolume() {
59 status_t PublicVolume::readMetadata() {
60 status_t res = ReadMetadataUntrusted(mDevPath, mFsType, mFsUuid, mFsLabel);
61 notifyEvent(ResponseCode::VolumeFsTypeChanged, mFsType);
62 notifyEvent(ResponseCode::VolumeFsUuidChanged, mFsUuid);
63 notifyEvent(ResponseCode::VolumeFsLabelChanged, mFsLabel);
67 status_t PublicVolume::initAsecStage() {
68 std::string legacyPath(mRawPath + "/android_secure");
69 std::string securePath(mRawPath + "/.android_secure");
71 // Recover legacy secure path
72 if (!access(legacyPath.c_str(), R_OK | X_OK)
73 && access(securePath.c_str(), R_OK | X_OK)) {
74 if (rename(legacyPath.c_str(), securePath.c_str())) {
75 PLOG(WARNING) << getId() << " failed to rename legacy ASEC dir";
79 if (TEMP_FAILURE_RETRY(mkdir(securePath.c_str(), 0700))) {
80 if (errno != EEXIST) {
81 PLOG(WARNING) << getId() << " creating ASEC stage failed";
86 BindMount(securePath, kAsecPath);
91 status_t PublicVolume::doCreate() {
92 return CreateDeviceNode(mDevPath, mDevice);
95 status_t PublicVolume::doDestroy() {
96 return DestroyDeviceNode(mDevPath);
99 status_t PublicVolume::doMount() {
100 // TODO: expand to support mounting other filesystems
103 if (!IsFilesystemSupported(mFsType)) {
104 LOG(ERROR) << getId() << " unsupported filesystem " << mFsType;
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";
133 if (mFsType == "exfat") {
134 ret = exfat::Check(mDevPath);
135 } else if (mFsType == "ext4") {
136 ret = ext4::Check(mDevPath, mRawPath, false);
137 } else if (mFsType == "f2fs") {
138 ret = f2fs::Check(mDevPath, false);
139 } else if (mFsType == "ntfs") {
140 ret = ntfs::Check(mDevPath);
141 } else if (mFsType == "vfat") {
142 ret = vfat::Check(mDevPath);
144 LOG(WARNING) << getId() << " unsupported filesystem check, skipping";
147 LOG(ERROR) << getId() << " failed filesystem check";
151 if (mFsType == "exfat") {
152 ret = exfat::Mount(mDevPath, mRawPath, false, false, false,
153 AID_MEDIA_RW, AID_MEDIA_RW, 0007);
154 } else if (mFsType == "ext4") {
155 ret = ext4::Mount(mDevPath, mRawPath, false, false, true, mMntOpts, true);
156 } else if (mFsType == "f2fs") {
157 ret = f2fs::Mount(mDevPath, mRawPath, mMntOpts, true);
158 } else if (mFsType == "ntfs") {
159 ret = ntfs::Mount(mDevPath, mRawPath, false, false, false,
160 AID_MEDIA_RW, AID_MEDIA_RW, 0007, true);
161 } else if (mFsType == "vfat") {
162 ret = vfat::Mount(mDevPath, mRawPath, false, false, false,
163 AID_MEDIA_RW, AID_MEDIA_RW, 0007, true);
165 ret = ::mount(mDevPath.c_str(), mRawPath.c_str(), mFsType.c_str(), 0, NULL);
168 PLOG(ERROR) << getId() << " failed to mount " << mDevPath;
172 if (getMountFlags() & MountFlags::kPrimary) {
176 if (!(getMountFlags() & MountFlags::kVisible)) {
177 // Not visible to apps, so no need to spin up FUSE
181 if (fs_prepare_dir(mFuseDefault.c_str(), 0700, AID_ROOT, AID_ROOT) ||
182 fs_prepare_dir(mFuseRead.c_str(), 0700, AID_ROOT, AID_ROOT) ||
183 fs_prepare_dir(mFuseWrite.c_str(), 0700, AID_ROOT, AID_ROOT)) {
184 PLOG(ERROR) << getId() << " failed to create FUSE mount points";
188 dev_t before = GetDevice(mFuseWrite);
190 if (!(mFusePid = fork())) {
191 if (getMountFlags() & MountFlags::kPrimary) {
192 if (execl(kFusePath, kFusePath,
193 "-u", "1023", // AID_MEDIA_RW
194 "-g", "1023", // AID_MEDIA_RW
195 "-U", std::to_string(getMountUserId()).c_str(),
200 PLOG(ERROR) << "Failed to exec";
203 if (execl(kFusePath, kFusePath,
204 "-u", "1023", // AID_MEDIA_RW
205 "-g", "1023", // AID_MEDIA_RW
206 "-U", std::to_string(getMountUserId()).c_str(),
210 PLOG(ERROR) << "Failed to exec";
214 LOG(ERROR) << "FUSE exiting";
218 if (mFusePid == -1) {
219 PLOG(ERROR) << getId() << " failed to fork";
223 while (before == GetDevice(mFuseWrite)) {
224 LOG(VERBOSE) << "Waiting for FUSE to spin up...";
225 usleep(50000); // 50ms
231 status_t PublicVolume::doUnmount() {
232 // Unmount the storage before we kill the FUSE process. If we kill
233 // the FUSE process first, most file system operations will return
234 // ENOTCONN until the unmount completes. This is an exotic and unusual
235 // error code and might cause broken behaviour in applications.
236 KillProcessesUsingPath(getPath());
238 ForceUnmount(kAsecPath);
240 ForceUnmount(mFuseDefault);
241 ForceUnmount(mFuseRead);
242 ForceUnmount(mFuseWrite);
243 ForceUnmount(mRawPath);
246 kill(mFusePid, SIGTERM);
247 TEMP_FAILURE_RETRY(waitpid(mFusePid, nullptr, 0));
251 rmdir(mFuseDefault.c_str());
252 rmdir(mFuseRead.c_str());
253 rmdir(mFuseWrite.c_str());
254 rmdir(mRawPath.c_str());
256 mFuseDefault.clear();
264 status_t PublicVolume::doFormat(const std::string& fsType) {
265 // "auto" is used for newly partitioned disks (see Disk::partition*)
266 // and thus is restricted to external/removable storage.
267 if (!(IsFilesystemSupported(fsType) || fsType == "auto")) {
268 LOG(ERROR) << "Unsupported filesystem " << fsType;
272 if (WipeBlockDevice(mDevPath) != OK) {
273 LOG(WARNING) << getId() << " failed to wipe";
277 if (fsType == "auto") {
278 ret = vfat::Format(mDevPath, 0);
279 } else if (fsType == "exfat") {
280 ret = exfat::Format(mDevPath);
281 } else if (fsType == "ext4") {
282 ret = ext4::Format(mDevPath, 0, mRawPath);
283 } else if (fsType == "f2fs") {
284 ret = f2fs::Format(mDevPath);
285 } else if (fsType == "ntfs") {
286 ret = ntfs::Format(mDevPath, 0);
287 } else if (fsType == "vfat") {
288 ret = vfat::Format(mDevPath, 0);
290 LOG(ERROR) << getId() << " unrecognized filesystem " << fsType;
296 LOG(ERROR) << getId() << " failed to format";
304 } // namespace android