blob: 8d9ac74ebde30be2391b7c11ca410827d5643576 [file] [log] [blame]
Jeff Sharkeydeb24052015-03-02 21:01:40 -08001/*
2 * Copyright (C) 2015 The Android Open Source Project
3 *
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
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
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.
15 */
16
Jeff Sharkeydeb24052015-03-02 21:01:40 -080017#include "EmulatedVolume.h"
18#include "Utils.h"
19
Elliott Hughes7e128fb2015-12-04 15:50:53 -080020#include <android-base/stringprintf.h>
21#include <android-base/logging.h>
Jeff Sharkeydeb24052015-03-02 21:01:40 -080022#include <cutils/fs.h>
Jeff Sharkeydeb24052015-03-02 21:01:40 -080023#include <private/android_filesystem_config.h>
Jeff Sharkey7bdf4d52017-09-18 14:47:10 -060024#include <utils/Timers.h>
Jeff Sharkeydeb24052015-03-02 21:01:40 -080025
26#include <fcntl.h>
27#include <stdlib.h>
28#include <sys/mount.h>
29#include <sys/stat.h>
Elliott Hughes0e08e842017-05-18 09:08:24 -070030#include <sys/sysmacros.h>
Paul Crowley8915d622018-09-18 15:14:18 -070031#include <sys/types.h>
Jeff Sharkeydeb24052015-03-02 21:01:40 -080032#include <sys/wait.h>
33
Dan Albertae9e8902015-03-16 10:35:17 -070034using android::base::StringPrintf;
35
Jeff Sharkeydeb24052015-03-02 21:01:40 -080036namespace android {
37namespace vold {
38
39static const char* kFusePath = "/system/bin/sdcard";
40
Paul Crowley8915d622018-09-18 15:14:18 -070041EmulatedVolume::EmulatedVolume(const std::string& rawPath)
42 : VolumeBase(Type::kEmulated), mFusePid(0) {
Jeff Sharkey3161fb32015-04-12 16:03:33 -070043 setId("emulated");
Jeff Sharkeydeb24052015-03-02 21:01:40 -080044 mRawPath = rawPath;
Jeff Sharkey66270a22015-06-24 11:49:24 -070045 mLabel = "emulated";
Jeff Sharkey3161fb32015-04-12 16:03:33 -070046}
47
Paul Crowley8915d622018-09-18 15:14:18 -070048EmulatedVolume::EmulatedVolume(const std::string& rawPath, dev_t device, const std::string& fsUuid)
49 : VolumeBase(Type::kEmulated), mFusePid(0) {
Jeff Sharkey3161fb32015-04-12 16:03:33 -070050 setId(StringPrintf("emulated:%u,%u", major(device), minor(device)));
Jeff Sharkey3161fb32015-04-12 16:03:33 -070051 mRawPath = rawPath;
Jeff Sharkey66270a22015-06-24 11:49:24 -070052 mLabel = fsUuid;
Jeff Sharkeydeb24052015-03-02 21:01:40 -080053}
54
Paul Crowley8915d622018-09-18 15:14:18 -070055EmulatedVolume::~EmulatedVolume() {}
Jeff Sharkeydeb24052015-03-02 21:01:40 -080056
57status_t EmulatedVolume::doMount() {
Jeff Sharkey81f55c62015-07-07 14:37:03 -070058 // We could have migrated storage to an adopted private volume, so always
59 // call primary storage "emulated" to avoid media rescans.
60 std::string label = mLabel;
61 if (getMountFlags() & MountFlags::kPrimary) {
62 label = "emulated";
63 }
64
Jeff Sharkey1bd078f2015-08-06 11:40:00 -070065 mFuseDefault = StringPrintf("/mnt/runtime/default/%s", label.c_str());
66 mFuseRead = StringPrintf("/mnt/runtime/read/%s", label.c_str());
67 mFuseWrite = StringPrintf("/mnt/runtime/write/%s", label.c_str());
Jeff Sharkey66270a22015-06-24 11:49:24 -070068
69 setInternalPath(mRawPath);
Jeff Sharkey81f55c62015-07-07 14:37:03 -070070 setPath(StringPrintf("/storage/%s", label.c_str()));
Jeff Sharkey66270a22015-06-24 11:49:24 -070071
72 if (fs_prepare_dir(mFuseDefault.c_str(), 0700, AID_ROOT, AID_ROOT) ||
Paul Crowley8915d622018-09-18 15:14:18 -070073 fs_prepare_dir(mFuseRead.c_str(), 0700, AID_ROOT, AID_ROOT) ||
74 fs_prepare_dir(mFuseWrite.c_str(), 0700, AID_ROOT, AID_ROOT)) {
Jeff Sharkey66270a22015-06-24 11:49:24 -070075 PLOG(ERROR) << getId() << " failed to create mount points";
Jeff Sharkeydeb24052015-03-02 21:01:40 -080076 return -errno;
77 }
78
Jeff Sharkey66270a22015-06-24 11:49:24 -070079 dev_t before = GetDevice(mFuseWrite);
Jeff Sharkey36801cc2015-03-13 16:09:20 -070080
Jeff Sharkeydeb24052015-03-02 21:01:40 -080081 if (!(mFusePid = fork())) {
Paul Crowley8915d622018-09-18 15:14:18 -070082 // clang-format off
Jeff Sharkey36801cc2015-03-13 16:09:20 -070083 if (execl(kFusePath, kFusePath,
Jeff Sharkeydeb24052015-03-02 21:01:40 -080084 "-u", "1023", // AID_MEDIA_RW
85 "-g", "1023", // AID_MEDIA_RW
Jeff Sharkey66270a22015-06-24 11:49:24 -070086 "-m",
87 "-w",
Rom Lemarchand958c2162017-09-15 18:48:01 +000088 "-G",
Jeff Sharkeyd7e51762018-01-08 11:48:07 -070089 "-i",
Jeff Sharkeydeb24052015-03-02 21:01:40 -080090 mRawPath.c_str(),
Jeff Sharkey81f55c62015-07-07 14:37:03 -070091 label.c_str(),
Jeff Sharkey36801cc2015-03-13 16:09:20 -070092 NULL)) {
Paul Crowley8915d622018-09-18 15:14:18 -070093 // clang-format on
Jeff Sharkey36801cc2015-03-13 16:09:20 -070094 PLOG(ERROR) << "Failed to exec";
Jeff Sharkeydeb24052015-03-02 21:01:40 -080095 }
Jeff Sharkey36801cc2015-03-13 16:09:20 -070096
Jeff Sharkeyc7b5b572015-06-30 15:54:17 -070097 LOG(ERROR) << "FUSE exiting";
Jeff Sharkeydeb24052015-03-02 21:01:40 -080098 _exit(1);
99 }
100
101 if (mFusePid == -1) {
Jeff Sharkey9c484982015-03-31 10:35:33 -0700102 PLOG(ERROR) << getId() << " failed to fork";
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800103 return -errno;
104 }
105
Jeff Sharkey7bdf4d52017-09-18 14:47:10 -0600106 nsecs_t start = systemTime(SYSTEM_TIME_BOOTTIME);
Jeff Sharkey66270a22015-06-24 11:49:24 -0700107 while (before == GetDevice(mFuseWrite)) {
Sudheer Shanka4b6ca4e2018-09-21 10:54:54 -0700108 LOG(DEBUG) << "Waiting for FUSE to spin up...";
Paul Crowley8915d622018-09-18 15:14:18 -0700109 usleep(50000); // 50ms
Jeff Sharkey7bdf4d52017-09-18 14:47:10 -0600110
111 nsecs_t now = systemTime(SYSTEM_TIME_BOOTTIME);
112 if (nanoseconds_to_milliseconds(now - start) > 5000) {
113 LOG(WARNING) << "Timed out while waiting for FUSE to spin up";
114 return -ETIMEDOUT;
115 }
Jeff Sharkey66270a22015-06-24 11:49:24 -0700116 }
Daniel Rosenberg1d79d102017-07-11 17:59:55 -0700117 /* sdcardfs will have exited already. FUSE will still be running */
Daniel Rosenberg8b9a5b32018-03-12 15:47:23 -0700118 TEMP_FAILURE_RETRY(waitpid(mFusePid, nullptr, 0));
119 mFusePid = 0;
Jeff Sharkey66270a22015-06-24 11:49:24 -0700120
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800121 return OK;
122}
123
124status_t EmulatedVolume::doUnmount() {
Narayan Kamathea243a32016-01-21 12:26:05 +0000125 // Unmount the storage before we kill the FUSE process. If we kill
126 // the FUSE process first, most file system operations will return
127 // ENOTCONN until the unmount completes. This is an exotic and unusual
128 // error code and might cause broken behaviour in applications.
129 KillProcessesUsingPath(getPath());
130 ForceUnmount(mFuseDefault);
131 ForceUnmount(mFuseRead);
132 ForceUnmount(mFuseWrite);
133
Jeff Sharkey66270a22015-06-24 11:49:24 -0700134 rmdir(mFuseDefault.c_str());
135 rmdir(mFuseRead.c_str());
136 rmdir(mFuseWrite.c_str());
137
138 mFuseDefault.clear();
139 mFuseRead.clear();
140 mFuseWrite.clear();
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800141
142 return OK;
143}
144
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800145} // namespace vold
146} // namespace android