blob: c84fbb7e751705fd30c5339042808af12fb4c94c [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"
Zim3623a212019-07-19 16:46:53 +010018
19#include "AppFuseUtil.h"
Jeff Sharkeydeb24052015-03-02 21:01:40 -080020#include "Utils.h"
Sudheer Shanka40ab6742018-09-18 13:07:45 -070021#include "VolumeManager.h"
Jeff Sharkeydeb24052015-03-02 21:01:40 -080022
Elliott Hughes7e128fb2015-12-04 15:50:53 -080023#include <android-base/logging.h>
Zim3623a212019-07-19 16:46:53 +010024#include <android-base/properties.h>
Sudheer Shanka53947a32018-08-01 10:24:13 -070025#include <android-base/stringprintf.h>
Jeff Sharkeydeb24052015-03-02 21:01:40 -080026#include <cutils/fs.h>
Jeff Sharkeydeb24052015-03-02 21:01:40 -080027#include <private/android_filesystem_config.h>
Jeff Sharkey7bdf4d52017-09-18 14:47:10 -060028#include <utils/Timers.h>
Jeff Sharkeydeb24052015-03-02 21:01:40 -080029
30#include <fcntl.h>
31#include <stdlib.h>
32#include <sys/mount.h>
33#include <sys/stat.h>
Elliott Hughes0e08e842017-05-18 09:08:24 -070034#include <sys/sysmacros.h>
Paul Crowleyedf7a4e2018-09-18 15:14:18 -070035#include <sys/types.h>
Jeff Sharkeydeb24052015-03-02 21:01:40 -080036#include <sys/wait.h>
37
Dan Albertae9e8902015-03-16 10:35:17 -070038using android::base::StringPrintf;
39
Jeff Sharkeydeb24052015-03-02 21:01:40 -080040namespace android {
41namespace vold {
42
43static const char* kFusePath = "/system/bin/sdcard";
44
Paul Crowleyedf7a4e2018-09-18 15:14:18 -070045EmulatedVolume::EmulatedVolume(const std::string& rawPath)
46 : VolumeBase(Type::kEmulated), mFusePid(0) {
Jeff Sharkey3161fb32015-04-12 16:03:33 -070047 setId("emulated");
Jeff Sharkeydeb24052015-03-02 21:01:40 -080048 mRawPath = rawPath;
Jeff Sharkey66270a22015-06-24 11:49:24 -070049 mLabel = "emulated";
Jeff Sharkey3161fb32015-04-12 16:03:33 -070050}
51
Paul Crowleyedf7a4e2018-09-18 15:14:18 -070052EmulatedVolume::EmulatedVolume(const std::string& rawPath, dev_t device, const std::string& fsUuid)
53 : VolumeBase(Type::kEmulated), mFusePid(0) {
Jeff Sharkey3161fb32015-04-12 16:03:33 -070054 setId(StringPrintf("emulated:%u,%u", major(device), minor(device)));
Jeff Sharkey3161fb32015-04-12 16:03:33 -070055 mRawPath = rawPath;
Jeff Sharkey66270a22015-06-24 11:49:24 -070056 mLabel = fsUuid;
Jeff Sharkeydeb24052015-03-02 21:01:40 -080057}
58
Paul Crowleyedf7a4e2018-09-18 15:14:18 -070059EmulatedVolume::~EmulatedVolume() {}
Jeff Sharkeydeb24052015-03-02 21:01:40 -080060
61status_t EmulatedVolume::doMount() {
Jeff Sharkey81f55c62015-07-07 14:37:03 -070062 // We could have migrated storage to an adopted private volume, so always
63 // call primary storage "emulated" to avoid media rescans.
64 std::string label = mLabel;
65 if (getMountFlags() & MountFlags::kPrimary) {
66 label = "emulated";
67 }
68
Jeff Sharkey1bd078f2015-08-06 11:40:00 -070069 mFuseDefault = StringPrintf("/mnt/runtime/default/%s", label.c_str());
70 mFuseRead = StringPrintf("/mnt/runtime/read/%s", label.c_str());
71 mFuseWrite = StringPrintf("/mnt/runtime/write/%s", label.c_str());
Sudheer Shankadd4bb172019-01-16 23:35:49 -080072 mFuseFull = StringPrintf("/mnt/runtime/full/%s", label.c_str());
Jeff Sharkey66270a22015-06-24 11:49:24 -070073
74 setInternalPath(mRawPath);
Jeff Sharkey81f55c62015-07-07 14:37:03 -070075 setPath(StringPrintf("/storage/%s", label.c_str()));
Jeff Sharkey66270a22015-06-24 11:49:24 -070076
77 if (fs_prepare_dir(mFuseDefault.c_str(), 0700, AID_ROOT, AID_ROOT) ||
Paul Crowleyedf7a4e2018-09-18 15:14:18 -070078 fs_prepare_dir(mFuseRead.c_str(), 0700, AID_ROOT, AID_ROOT) ||
Sudheer Shankadd4bb172019-01-16 23:35:49 -080079 fs_prepare_dir(mFuseWrite.c_str(), 0700, AID_ROOT, AID_ROOT) ||
80 fs_prepare_dir(mFuseFull.c_str(), 0700, AID_ROOT, AID_ROOT)) {
Jeff Sharkey66270a22015-06-24 11:49:24 -070081 PLOG(ERROR) << getId() << " failed to create mount points";
Jeff Sharkeydeb24052015-03-02 21:01:40 -080082 return -errno;
83 }
84
Sudheer Shankadd4bb172019-01-16 23:35:49 -080085 dev_t before = GetDevice(mFuseFull);
Jeff Sharkey36801cc2015-03-13 16:09:20 -070086
shafik5cf32b52019-09-25 13:56:01 +010087 bool isFuse = base::GetBoolProperty(kPropFuseSnapshot, false);
Zim3623a212019-07-19 16:46:53 +010088
89 if (isFuse) {
90 LOG(INFO) << "Mounting emulated fuse volume";
Nandana Dutta914cc72019-08-29 15:22:42 +010091 android::base::unique_fd fd;
Zim981222f2019-09-09 10:24:44 +010092 int user_id = getMountUserId();
93 int result = MountUserFuse(user_id, label, &fd);
94
Zim3623a212019-07-19 16:46:53 +010095 if (result != 0) {
96 PLOG(ERROR) << "Failed to mount emulated fuse volume";
97 return -result;
98 }
Nandana Dutta914cc72019-08-29 15:22:42 +010099 setFuseFd(std::move(fd));
Zim981222f2019-09-09 10:24:44 +0100100
101 std::string pass_through_path(StringPrintf("/mnt/pass_through/%d/%s",
102 user_id, label.c_str()));
103 return BindMount(getInternalPath(), pass_through_path);
Zim3623a212019-07-19 16:46:53 +0100104 }
105
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800106 if (!(mFusePid = fork())) {
Zim3623a212019-07-19 16:46:53 +0100107 LOG(INFO) << "Executing sdcardfs";
Paul Crowleyedf7a4e2018-09-18 15:14:18 -0700108 // clang-format off
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700109 if (execl(kFusePath, kFusePath,
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800110 "-u", "1023", // AID_MEDIA_RW
111 "-g", "1023", // AID_MEDIA_RW
Jeff Sharkey66270a22015-06-24 11:49:24 -0700112 "-m",
113 "-w",
Rom Lemarchand958c2162017-09-15 18:48:01 +0000114 "-G",
Jeff Sharkeyd7e51762018-01-08 11:48:07 -0700115 "-i",
Sudheer Shanka8cad97b2019-03-05 10:31:27 -0800116 "-o",
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800117 mRawPath.c_str(),
Jeff Sharkey81f55c62015-07-07 14:37:03 -0700118 label.c_str(),
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700119 NULL)) {
Paul Crowleyedf7a4e2018-09-18 15:14:18 -0700120 // clang-format on
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700121 PLOG(ERROR) << "Failed to exec";
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800122 }
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700123
Jeff Sharkeyc7b5b572015-06-30 15:54:17 -0700124 LOG(ERROR) << "FUSE exiting";
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800125 _exit(1);
126 }
127
128 if (mFusePid == -1) {
Jeff Sharkey9c484982015-03-31 10:35:33 -0700129 PLOG(ERROR) << getId() << " failed to fork";
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800130 return -errno;
131 }
132
Jeff Sharkey7bdf4d52017-09-18 14:47:10 -0600133 nsecs_t start = systemTime(SYSTEM_TIME_BOOTTIME);
Sudheer Shankadd4bb172019-01-16 23:35:49 -0800134 while (before == GetDevice(mFuseFull)) {
Sudheer Shanka4b6ca4e2018-09-21 10:54:54 -0700135 LOG(DEBUG) << "Waiting for FUSE to spin up...";
Paul Crowleyedf7a4e2018-09-18 15:14:18 -0700136 usleep(50000); // 50ms
Jeff Sharkey7bdf4d52017-09-18 14:47:10 -0600137
138 nsecs_t now = systemTime(SYSTEM_TIME_BOOTTIME);
139 if (nanoseconds_to_milliseconds(now - start) > 5000) {
140 LOG(WARNING) << "Timed out while waiting for FUSE to spin up";
141 return -ETIMEDOUT;
142 }
Jeff Sharkey66270a22015-06-24 11:49:24 -0700143 }
Daniel Rosenberg1d79d102017-07-11 17:59:55 -0700144 /* sdcardfs will have exited already. FUSE will still be running */
Daniel Rosenberg8b9a5b32018-03-12 15:47:23 -0700145 TEMP_FAILURE_RETRY(waitpid(mFusePid, nullptr, 0));
146 mFusePid = 0;
Jeff Sharkey66270a22015-06-24 11:49:24 -0700147
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800148 return OK;
149}
150
151status_t EmulatedVolume::doUnmount() {
Narayan Kamathea243a32016-01-21 12:26:05 +0000152 // Unmount the storage before we kill the FUSE process. If we kill
153 // the FUSE process first, most file system operations will return
154 // ENOTCONN until the unmount completes. This is an exotic and unusual
155 // error code and might cause broken behaviour in applications.
156 KillProcessesUsingPath(getPath());
Zim3623a212019-07-19 16:46:53 +0100157
shafik5cf32b52019-09-25 13:56:01 +0100158 bool isFuse = base::GetBoolProperty(kPropFuseSnapshot, false);
Zim3623a212019-07-19 16:46:53 +0100159 if (isFuse) {
160 // We could have migrated storage to an adopted private volume, so always
161 // call primary storage "emulated" to avoid media rescans.
162 std::string label = mLabel;
163 if (getMountFlags() & MountFlags::kPrimary) {
164 label = "emulated";
165 }
166 std::string path(StringPrintf("/mnt/user/%d/%s", getMountUserId(), label.c_str()));
167 status_t result = ForceUnmount(path);
168 if (result != OK) {
169 // TODO(135341433): MNT_DETACH is needed for fuse because umount2 can fail with EBUSY.
170 // Figure out why we get EBUSY and remove this special casing if possible.
171 if (!umount2(path.c_str(), UMOUNT_NOFOLLOW | MNT_DETACH) || errno == EINVAL ||
172 errno == ENOENT) {
173 PLOG(INFO) << "ForceUnmount failed on emulated fuse volume";
174 }
175 }
176
177 rmdir(path.c_str());
Nandana Dutta914cc72019-08-29 15:22:42 +0100178 setFuseFd(android::base::unique_fd());
Zim3623a212019-07-19 16:46:53 +0100179 return OK;
180 }
181
Narayan Kamathea243a32016-01-21 12:26:05 +0000182 ForceUnmount(mFuseDefault);
183 ForceUnmount(mFuseRead);
184 ForceUnmount(mFuseWrite);
Sudheer Shankadd4bb172019-01-16 23:35:49 -0800185 ForceUnmount(mFuseFull);
Narayan Kamathea243a32016-01-21 12:26:05 +0000186
Jeff Sharkey66270a22015-06-24 11:49:24 -0700187 rmdir(mFuseDefault.c_str());
188 rmdir(mFuseRead.c_str());
189 rmdir(mFuseWrite.c_str());
Sudheer Shankadd4bb172019-01-16 23:35:49 -0800190 rmdir(mFuseFull.c_str());
Jeff Sharkey66270a22015-06-24 11:49:24 -0700191
192 mFuseDefault.clear();
193 mFuseRead.clear();
194 mFuseWrite.clear();
Sudheer Shankadd4bb172019-01-16 23:35:49 -0800195 mFuseFull.clear();
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800196
197 return OK;
198}
199
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800200} // namespace vold
201} // namespace android