blob: eb6b8a34b7a7ed90e58685f00bb2916f758d7fed [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
Zima438b242019-09-25 14:37:38 +010045EmulatedVolume::EmulatedVolume(const std::string& rawPath, int userId)
Paul Crowleyedf7a4e2018-09-18 15:14:18 -070046 : VolumeBase(Type::kEmulated), mFusePid(0) {
Zima438b242019-09-25 14:37:38 +010047 setId(StringPrintf("emulated;%u", userId));
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
Zima438b242019-09-25 14:37:38 +010052EmulatedVolume::EmulatedVolume(const std::string& rawPath, dev_t device, const std::string& fsUuid,
53 int userId)
Paul Crowleyedf7a4e2018-09-18 15:14:18 -070054 : VolumeBase(Type::kEmulated), mFusePid(0) {
Zima438b242019-09-25 14:37:38 +010055 setId(StringPrintf("emulated:%u,%u;%u", major(device), minor(device), userId));
Jeff Sharkey3161fb32015-04-12 16:03:33 -070056 mRawPath = rawPath;
Jeff Sharkey66270a22015-06-24 11:49:24 -070057 mLabel = fsUuid;
Jeff Sharkeydeb24052015-03-02 21:01:40 -080058}
59
Paul Crowleyedf7a4e2018-09-18 15:14:18 -070060EmulatedVolume::~EmulatedVolume() {}
Jeff Sharkeydeb24052015-03-02 21:01:40 -080061
62status_t EmulatedVolume::doMount() {
Jeff Sharkey81f55c62015-07-07 14:37:03 -070063 // We could have migrated storage to an adopted private volume, so always
64 // call primary storage "emulated" to avoid media rescans.
65 std::string label = mLabel;
66 if (getMountFlags() & MountFlags::kPrimary) {
67 label = "emulated";
68 }
69
Jeff Sharkey1bd078f2015-08-06 11:40:00 -070070 mFuseDefault = StringPrintf("/mnt/runtime/default/%s", label.c_str());
71 mFuseRead = StringPrintf("/mnt/runtime/read/%s", label.c_str());
72 mFuseWrite = StringPrintf("/mnt/runtime/write/%s", label.c_str());
Sudheer Shankadd4bb172019-01-16 23:35:49 -080073 mFuseFull = StringPrintf("/mnt/runtime/full/%s", label.c_str());
Jeff Sharkey66270a22015-06-24 11:49:24 -070074
75 setInternalPath(mRawPath);
Jeff Sharkey81f55c62015-07-07 14:37:03 -070076 setPath(StringPrintf("/storage/%s", label.c_str()));
Jeff Sharkey66270a22015-06-24 11:49:24 -070077
78 if (fs_prepare_dir(mFuseDefault.c_str(), 0700, AID_ROOT, AID_ROOT) ||
Paul Crowleyedf7a4e2018-09-18 15:14:18 -070079 fs_prepare_dir(mFuseRead.c_str(), 0700, AID_ROOT, AID_ROOT) ||
Sudheer Shankadd4bb172019-01-16 23:35:49 -080080 fs_prepare_dir(mFuseWrite.c_str(), 0700, AID_ROOT, AID_ROOT) ||
81 fs_prepare_dir(mFuseFull.c_str(), 0700, AID_ROOT, AID_ROOT)) {
Jeff Sharkey66270a22015-06-24 11:49:24 -070082 PLOG(ERROR) << getId() << " failed to create mount points";
Jeff Sharkeydeb24052015-03-02 21:01:40 -080083 return -errno;
84 }
85
Sudheer Shankadd4bb172019-01-16 23:35:49 -080086 dev_t before = GetDevice(mFuseFull);
Jeff Sharkey36801cc2015-03-13 16:09:20 -070087
shafik5cf32b52019-09-25 13:56:01 +010088 bool isFuse = base::GetBoolProperty(kPropFuseSnapshot, false);
Zim3623a212019-07-19 16:46:53 +010089
90 if (isFuse) {
91 LOG(INFO) << "Mounting emulated fuse volume";
Nandana Dutta914cc72019-08-29 15:22:42 +010092 android::base::unique_fd fd;
Zim981222f2019-09-09 10:24:44 +010093 int user_id = getMountUserId();
Zima438b242019-09-25 14:37:38 +010094 int result = MountUserFuse(user_id, getInternalPath(), label, &fd);
Zim981222f2019-09-09 10:24:44 +010095
Zim3623a212019-07-19 16:46:53 +010096 if (result != 0) {
97 PLOG(ERROR) << "Failed to mount emulated fuse volume";
98 return -result;
99 }
Zim5048b4b2019-11-19 09:16:03 +0000100
101 auto callback = getMountCallback();
102 if (callback) {
103 bool is_ready = false;
104 callback->onVolumeChecking(std::move(fd), getPath(), getInternalPath(), &is_ready);
105 if (!is_ready) {
106 return -EIO;
107 }
108 }
109
110 return OK;
Zim2d45d9b2019-11-14 16:19:05 +0000111 } else if (getMountUserId() != 0) {
112 // For sdcardfs, only mount for user 0, since user 0 will always be running
113 // and the paths don't change for different users. Trying to double mount
114 // will cause sepolicy to scream since sdcardfs prevents 'mounton'
115 return OK;
Zim3623a212019-07-19 16:46:53 +0100116 }
117
Martijn Coenen10b122b2019-12-04 15:48:42 +0100118 LOG(INFO) << "Executing sdcardfs";
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800119 if (!(mFusePid = fork())) {
Paul Crowleyedf7a4e2018-09-18 15:14:18 -0700120 // clang-format off
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700121 if (execl(kFusePath, kFusePath,
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800122 "-u", "1023", // AID_MEDIA_RW
123 "-g", "1023", // AID_MEDIA_RW
Jeff Sharkey66270a22015-06-24 11:49:24 -0700124 "-m",
125 "-w",
Rom Lemarchand958c2162017-09-15 18:48:01 +0000126 "-G",
Jeff Sharkeyd7e51762018-01-08 11:48:07 -0700127 "-i",
Sudheer Shanka8cad97b2019-03-05 10:31:27 -0800128 "-o",
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800129 mRawPath.c_str(),
Jeff Sharkey81f55c62015-07-07 14:37:03 -0700130 label.c_str(),
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700131 NULL)) {
Paul Crowleyedf7a4e2018-09-18 15:14:18 -0700132 // clang-format on
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700133 PLOG(ERROR) << "Failed to exec";
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800134 }
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700135
Jeff Sharkeyc7b5b572015-06-30 15:54:17 -0700136 LOG(ERROR) << "FUSE exiting";
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800137 _exit(1);
138 }
139
140 if (mFusePid == -1) {
Jeff Sharkey9c484982015-03-31 10:35:33 -0700141 PLOG(ERROR) << getId() << " failed to fork";
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800142 return -errno;
143 }
144
Jeff Sharkey7bdf4d52017-09-18 14:47:10 -0600145 nsecs_t start = systemTime(SYSTEM_TIME_BOOTTIME);
Sudheer Shankadd4bb172019-01-16 23:35:49 -0800146 while (before == GetDevice(mFuseFull)) {
Sudheer Shanka4b6ca4e2018-09-21 10:54:54 -0700147 LOG(DEBUG) << "Waiting for FUSE to spin up...";
Paul Crowleyedf7a4e2018-09-18 15:14:18 -0700148 usleep(50000); // 50ms
Jeff Sharkey7bdf4d52017-09-18 14:47:10 -0600149
150 nsecs_t now = systemTime(SYSTEM_TIME_BOOTTIME);
151 if (nanoseconds_to_milliseconds(now - start) > 5000) {
152 LOG(WARNING) << "Timed out while waiting for FUSE to spin up";
153 return -ETIMEDOUT;
154 }
Jeff Sharkey66270a22015-06-24 11:49:24 -0700155 }
Daniel Rosenberg1d79d102017-07-11 17:59:55 -0700156 /* sdcardfs will have exited already. FUSE will still be running */
Daniel Rosenberg8b9a5b32018-03-12 15:47:23 -0700157 TEMP_FAILURE_RETRY(waitpid(mFusePid, nullptr, 0));
158 mFusePid = 0;
Jeff Sharkey66270a22015-06-24 11:49:24 -0700159
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800160 return OK;
161}
162
163status_t EmulatedVolume::doUnmount() {
Narayan Kamathea243a32016-01-21 12:26:05 +0000164 // Unmount the storage before we kill the FUSE process. If we kill
165 // the FUSE process first, most file system operations will return
166 // ENOTCONN until the unmount completes. This is an exotic and unusual
167 // error code and might cause broken behaviour in applications.
168 KillProcessesUsingPath(getPath());
Zim3623a212019-07-19 16:46:53 +0100169
shafik5cf32b52019-09-25 13:56:01 +0100170 bool isFuse = base::GetBoolProperty(kPropFuseSnapshot, false);
Zim3623a212019-07-19 16:46:53 +0100171 if (isFuse) {
172 // We could have migrated storage to an adopted private volume, so always
173 // call primary storage "emulated" to avoid media rescans.
174 std::string label = mLabel;
175 if (getMountFlags() & MountFlags::kPrimary) {
176 label = "emulated";
177 }
Zima438b242019-09-25 14:37:38 +0100178
179 std::string fuse_path(StringPrintf("/mnt/user/%d/%s", getMountUserId(), label.c_str()));
180 std::string pass_through_path(
181 StringPrintf("/mnt/pass_through/%d/%s", getMountUserId(), label.c_str()));
182 if (UnmountUserFuse(pass_through_path, fuse_path) != OK) {
183 PLOG(INFO) << "UnmountUserFuse failed on emulated fuse volume";
184 return -errno;
Zim3623a212019-07-19 16:46:53 +0100185 }
186
Zima438b242019-09-25 14:37:38 +0100187 rmdir(fuse_path.c_str());
188 rmdir(pass_through_path.c_str());
Zim3623a212019-07-19 16:46:53 +0100189 return OK;
Zim2d45d9b2019-11-14 16:19:05 +0000190 } else if (getMountUserId() != 0) {
191 // For sdcardfs, only unmount for user 0, since user 0 will always be running
192 // and the paths don't change for different users.
193 return OK;
Zim3623a212019-07-19 16:46:53 +0100194 }
195
Narayan Kamathea243a32016-01-21 12:26:05 +0000196 ForceUnmount(mFuseDefault);
197 ForceUnmount(mFuseRead);
198 ForceUnmount(mFuseWrite);
Sudheer Shankadd4bb172019-01-16 23:35:49 -0800199 ForceUnmount(mFuseFull);
Narayan Kamathea243a32016-01-21 12:26:05 +0000200
Jeff Sharkey66270a22015-06-24 11:49:24 -0700201 rmdir(mFuseDefault.c_str());
202 rmdir(mFuseRead.c_str());
203 rmdir(mFuseWrite.c_str());
Sudheer Shankadd4bb172019-01-16 23:35:49 -0800204 rmdir(mFuseFull.c_str());
Jeff Sharkey66270a22015-06-24 11:49:24 -0700205
206 mFuseDefault.clear();
207 mFuseRead.clear();
208 mFuseWrite.clear();
Sudheer Shankadd4bb172019-01-16 23:35:49 -0800209 mFuseFull.clear();
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800210
211 return OK;
212}
213
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800214} // namespace vold
215} // namespace android