blob: 8f8c87a249f74d17b0497a365dee4ed680f04ff0 [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
Martijn Coenenadcc8452019-12-09 14:18:01 +010043static const char* kSdcardFsPath = "/system/bin/sdcard";
Jeff Sharkeydeb24052015-03-02 21:01:40 -080044
Zima438b242019-09-25 14:37:38 +010045EmulatedVolume::EmulatedVolume(const std::string& rawPath, int userId)
Martijn Coenenadcc8452019-12-09 14:18:01 +010046 : VolumeBase(Type::kEmulated) {
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";
Martijn Coenenfd7362d2019-12-11 14:57:59 +010050 mFuseMounted = false;
Martijn Coenen86f21a22020-01-06 09:48:14 +010051 mUseSdcardFs = IsFilesystemSupported("sdcardfs");
Jeff Sharkey3161fb32015-04-12 16:03:33 -070052}
53
Zima438b242019-09-25 14:37:38 +010054EmulatedVolume::EmulatedVolume(const std::string& rawPath, dev_t device, const std::string& fsUuid,
55 int userId)
Martijn Coenenadcc8452019-12-09 14:18:01 +010056 : VolumeBase(Type::kEmulated) {
Zima438b242019-09-25 14:37:38 +010057 setId(StringPrintf("emulated:%u,%u;%u", major(device), minor(device), userId));
Jeff Sharkey3161fb32015-04-12 16:03:33 -070058 mRawPath = rawPath;
Jeff Sharkey66270a22015-06-24 11:49:24 -070059 mLabel = fsUuid;
Greg Kaiser5298ccc2019-12-12 05:41:46 -080060 mFuseMounted = false;
Martijn Coenen86f21a22020-01-06 09:48:14 +010061 mUseSdcardFs = IsFilesystemSupported("sdcardfs");
Jeff Sharkeydeb24052015-03-02 21:01:40 -080062}
63
Paul Crowleyedf7a4e2018-09-18 15:14:18 -070064EmulatedVolume::~EmulatedVolume() {}
Jeff Sharkeydeb24052015-03-02 21:01:40 -080065
Martijn Coenen6f5802e2019-11-28 11:53:53 +010066std::string EmulatedVolume::getLabel() {
Jeff Sharkey81f55c62015-07-07 14:37:03 -070067 // We could have migrated storage to an adopted private volume, so always
68 // call primary storage "emulated" to avoid media rescans.
Jeff Sharkey81f55c62015-07-07 14:37:03 -070069 if (getMountFlags() & MountFlags::kPrimary) {
Martijn Coenen6f5802e2019-11-28 11:53:53 +010070 return "emulated";
71 } else {
72 return mLabel;
Jeff Sharkey81f55c62015-07-07 14:37:03 -070073 }
Martijn Coenen6f5802e2019-11-28 11:53:53 +010074}
75
Martijn Coenen62a4b272020-01-31 15:23:09 +010076// Creates a bind mount from source to target
Martijn Coenen3a2dbfe2020-01-11 19:38:37 +010077static status_t doFuseBindMount(const std::string& source, const std::string& target) {
Martijn Coenen3a2dbfe2020-01-11 19:38:37 +010078 LOG(INFO) << "Bind mounting " << source << " on " << target;
79 auto status = BindMount(source, target);
80 if (status != OK) {
81 return status;
82 }
83 LOG(INFO) << "Bind mounted " << source << " on " << target;
84 return OK;
85}
86
Martijn Coenen86f21a22020-01-06 09:48:14 +010087status_t EmulatedVolume::mountFuseBindMounts() {
88 std::string androidSource;
89 std::string label = getLabel();
90 int userId = getMountUserId();
91
92 if (mUseSdcardFs) {
93 androidSource = StringPrintf("/mnt/runtime/default/%s/%d/Android", label.c_str(), userId);
94 } else {
95 androidSource = StringPrintf("/%s/%d/Android", mRawPath.c_str(), userId);
96 }
Martijn Coenen57002612019-11-28 11:56:13 +010097 std::string androidTarget(
98 StringPrintf("/mnt/user/%d/%s/%d/Android", userId, label.c_str(), userId));
99
Martijn Coenen3a2dbfe2020-01-11 19:38:37 +0100100 auto status = doFuseBindMount(androidSource, androidTarget);
Martijn Coenen57002612019-11-28 11:56:13 +0100101 if (status != OK) {
102 return status;
103 }
Martijn Coenen57002612019-11-28 11:56:13 +0100104
Martijn Coenen3a2dbfe2020-01-11 19:38:37 +0100105 // Installers get the same view as all other apps, with the sole exception that the
106 // OBB dirs (Android/obb) are writable to them. On sdcardfs devices, this requires
107 // a special bind mount, since app-private and OBB dirs share the same GID, but we
108 // only want to give access to the latter.
109 if (!mUseSdcardFs) {
110 return OK;
111 }
112 std::string installerSource(
113 StringPrintf("/mnt/runtime/write/%s/%d/Android/obb", label.c_str(), userId));
114 std::string installerTarget(
115 StringPrintf("/mnt/installer/%d/%s/%d/Android/obb", userId, label.c_str(), userId));
116
117 status = doFuseBindMount(installerSource, installerTarget);
118 if (status != OK) {
119 return status;
120 }
Martijn Coenen57002612019-11-28 11:56:13 +0100121 return OK;
122}
123
Martijn Coenen86f21a22020-01-06 09:48:14 +0100124status_t EmulatedVolume::unmountFuseBindMounts() {
125 std::string label = getLabel();
126 int userId = getMountUserId();
127
Martijn Coenen3a2dbfe2020-01-11 19:38:37 +0100128 if (mUseSdcardFs) {
129 std::string installerTarget(
130 StringPrintf("/mnt/installer/%d/%s/%d/Android/obb", userId, label.c_str(), userId));
131 LOG(INFO) << "Unmounting " << installerTarget;
132 auto status = UnmountTree(installerTarget);
133 if (status != OK) {
134 LOG(ERROR) << "Failed to unmount " << installerTarget;
135 // Intentional continue to try to unmount the other bind mount
136 }
137 }
138
Martijn Coenen57002612019-11-28 11:56:13 +0100139 std::string androidTarget(
140 StringPrintf("/mnt/user/%d/%s/%d/Android", userId, label.c_str(), userId));
141
142 LOG(INFO) << "Unmounting " << androidTarget;
143 auto status = UnmountTree(androidTarget);
144 if (status != OK) {
145 return status;
146 }
147 LOG(INFO) << "Unmounted " << androidTarget;
148
149 return OK;
150}
151
Martijn Coenen6f5802e2019-11-28 11:53:53 +0100152status_t EmulatedVolume::doMount() {
153 std::string label = getLabel();
154 bool isVisible = getMountFlags() & MountFlags::kVisible;
Jeff Sharkey81f55c62015-07-07 14:37:03 -0700155
Martijn Coenenadcc8452019-12-09 14:18:01 +0100156 mSdcardFsDefault = StringPrintf("/mnt/runtime/default/%s", label.c_str());
157 mSdcardFsRead = StringPrintf("/mnt/runtime/read/%s", label.c_str());
158 mSdcardFsWrite = StringPrintf("/mnt/runtime/write/%s", label.c_str());
159 mSdcardFsFull = StringPrintf("/mnt/runtime/full/%s", label.c_str());
Jeff Sharkey66270a22015-06-24 11:49:24 -0700160
161 setInternalPath(mRawPath);
Jeff Sharkey81f55c62015-07-07 14:37:03 -0700162 setPath(StringPrintf("/storage/%s", label.c_str()));
Jeff Sharkey66270a22015-06-24 11:49:24 -0700163
Martijn Coenenadcc8452019-12-09 14:18:01 +0100164 if (fs_prepare_dir(mSdcardFsDefault.c_str(), 0700, AID_ROOT, AID_ROOT) ||
165 fs_prepare_dir(mSdcardFsRead.c_str(), 0700, AID_ROOT, AID_ROOT) ||
166 fs_prepare_dir(mSdcardFsWrite.c_str(), 0700, AID_ROOT, AID_ROOT) ||
167 fs_prepare_dir(mSdcardFsFull.c_str(), 0700, AID_ROOT, AID_ROOT)) {
Jeff Sharkey66270a22015-06-24 11:49:24 -0700168 PLOG(ERROR) << getId() << " failed to create mount points";
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800169 return -errno;
170 }
171
Martijn Coenenadcc8452019-12-09 14:18:01 +0100172 dev_t before = GetDevice(mSdcardFsFull);
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700173
Abhijeet Kaur01fa0e02019-12-13 10:26:32 +0000174 bool isFuse = base::GetBoolProperty(kPropFuse, false);
Zim3623a212019-07-19 16:46:53 +0100175
Martijn Coenen6f5802e2019-11-28 11:53:53 +0100176 // Mount sdcardfs regardless of FUSE, since we need it to bind-mount on top of the
177 // FUSE volume for various reasons.
Martijn Coenen86f21a22020-01-06 09:48:14 +0100178 if (mUseSdcardFs && getMountUserId() == 0) {
Martijn Coenen6f5802e2019-11-28 11:53:53 +0100179 LOG(INFO) << "Executing sdcardfs";
180 int sdcardFsPid;
181 if (!(sdcardFsPid = fork())) {
182 // clang-format off
183 if (execl(kSdcardFsPath, kSdcardFsPath,
184 "-u", "1023", // AID_MEDIA_RW
185 "-g", "1023", // AID_MEDIA_RW
186 "-m",
187 "-w",
188 "-G",
189 "-i",
190 "-o",
191 mRawPath.c_str(),
192 label.c_str(),
193 NULL)) {
194 // clang-format on
195 PLOG(ERROR) << "Failed to exec";
196 }
197
198 LOG(ERROR) << "sdcardfs exiting";
199 _exit(1);
200 }
201
202 if (sdcardFsPid == -1) {
203 PLOG(ERROR) << getId() << " failed to fork";
204 return -errno;
205 }
206
207 nsecs_t start = systemTime(SYSTEM_TIME_BOOTTIME);
208 while (before == GetDevice(mSdcardFsFull)) {
209 LOG(DEBUG) << "Waiting for sdcardfs to spin up...";
210 usleep(50000); // 50ms
211
212 nsecs_t now = systemTime(SYSTEM_TIME_BOOTTIME);
213 if (nanoseconds_to_milliseconds(now - start) > 5000) {
214 LOG(WARNING) << "Timed out while waiting for sdcardfs to spin up";
215 return -ETIMEDOUT;
216 }
217 }
218 /* sdcardfs will have exited already. The filesystem will still be running */
219 TEMP_FAILURE_RETRY(waitpid(sdcardFsPid, nullptr, 0));
220 sdcardFsPid = 0;
221 }
222 if (isFuse && isVisible) {
Zim3623a212019-07-19 16:46:53 +0100223 LOG(INFO) << "Mounting emulated fuse volume";
Nandana Dutta914cc72019-08-29 15:22:42 +0100224 android::base::unique_fd fd;
Zim981222f2019-09-09 10:24:44 +0100225 int user_id = getMountUserId();
Martijn Coenen62a4b272020-01-31 15:23:09 +0100226 auto volumeRoot = getRootPath();
Zim981222f2019-09-09 10:24:44 +0100227
Martijn Coenen62a4b272020-01-31 15:23:09 +0100228 // Make sure Android/ dirs exist for bind mounting
229 status_t res = PrepareAndroidDirs(volumeRoot);
230 if (res != OK) {
231 LOG(ERROR) << "Failed to prepare Android/ directories";
232 return res;
233 }
234
235 res = MountUserFuse(user_id, getInternalPath(), label, &fd);
236 if (res != 0) {
Zim3623a212019-07-19 16:46:53 +0100237 PLOG(ERROR) << "Failed to mount emulated fuse volume";
Martijn Coenen62a4b272020-01-31 15:23:09 +0100238 return res;
Zim3623a212019-07-19 16:46:53 +0100239 }
Zim5048b4b2019-11-19 09:16:03 +0000240
Martijn Coenen6f5802e2019-11-28 11:53:53 +0100241 mFuseMounted = true;
Zim5048b4b2019-11-19 09:16:03 +0000242 auto callback = getMountCallback();
243 if (callback) {
244 bool is_ready = false;
245 callback->onVolumeChecking(std::move(fd), getPath(), getInternalPath(), &is_ready);
246 if (!is_ready) {
Zimdf073f52020-01-15 15:00:07 +0000247 fd.reset();
248 doUnmount();
Zim5048b4b2019-11-19 09:16:03 +0000249 return -EIO;
250 }
251 }
Martijn Coenen57002612019-11-28 11:56:13 +0100252
253 // Only do the bind-mounts when we know for sure the FUSE daemon can resolve the path.
Martijn Coenen62a4b272020-01-31 15:23:09 +0100254 res = mountFuseBindMounts();
Zimdf073f52020-01-15 15:00:07 +0000255 if (res != OK) {
256 fd.reset();
257 doUnmount();
258 }
259 return res;
Zim3623a212019-07-19 16:46:53 +0100260 }
261
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800262 return OK;
263}
264
265status_t EmulatedVolume::doUnmount() {
Martijn Coenen8f1e7f22019-11-29 15:38:55 +0100266 int userId = getMountUserId();
267
268 // Kill all processes using the filesystem before we unmount it. If we
269 // unmount the filesystem first, most file system operations will return
Narayan Kamathea243a32016-01-21 12:26:05 +0000270 // ENOTCONN until the unmount completes. This is an exotic and unusual
271 // error code and might cause broken behaviour in applications.
Martijn Coenen8f1e7f22019-11-29 15:38:55 +0100272 if (mFuseMounted) {
273 // For FUSE specifically, we have an emulated volume per user, so only kill
274 // processes using files from this particular user.
275 std::string user_path(StringPrintf("%s/%d", getPath().c_str(), getMountUserId()));
276 LOG(INFO) << "Killing all processes referencing " << user_path;
277 KillProcessesUsingPath(user_path);
278 } else {
279 KillProcessesUsingPath(getPath());
280 }
Zim3623a212019-07-19 16:46:53 +0100281
Martijn Coenen6f5802e2019-11-28 11:53:53 +0100282 if (mFuseMounted) {
283 std::string label = getLabel();
Martijn Coenen57002612019-11-28 11:56:13 +0100284 // Ignoring unmount return status because we do want to try to unmount
285 // the rest cleanly.
Zima438b242019-09-25 14:37:38 +0100286
Martijn Coenen86f21a22020-01-06 09:48:14 +0100287 unmountFuseBindMounts();
Martijn Coenen57002612019-11-28 11:56:13 +0100288 if (UnmountUserFuse(userId, getInternalPath(), label) != OK) {
Zima438b242019-09-25 14:37:38 +0100289 PLOG(INFO) << "UnmountUserFuse failed on emulated fuse volume";
290 return -errno;
Zim3623a212019-07-19 16:46:53 +0100291 }
292
Martijn Coenen6f5802e2019-11-28 11:53:53 +0100293 mFuseMounted = false;
294 }
Martijn Coenen86f21a22020-01-06 09:48:14 +0100295 if (getMountUserId() != 0 || !mUseSdcardFs) {
Zim2d45d9b2019-11-14 16:19:05 +0000296 // For sdcardfs, only unmount for user 0, since user 0 will always be running
297 // and the paths don't change for different users.
298 return OK;
Zim3623a212019-07-19 16:46:53 +0100299 }
300
Martijn Coenenadcc8452019-12-09 14:18:01 +0100301 ForceUnmount(mSdcardFsDefault);
302 ForceUnmount(mSdcardFsRead);
303 ForceUnmount(mSdcardFsWrite);
304 ForceUnmount(mSdcardFsFull);
Narayan Kamathea243a32016-01-21 12:26:05 +0000305
Martijn Coenenadcc8452019-12-09 14:18:01 +0100306 rmdir(mSdcardFsDefault.c_str());
307 rmdir(mSdcardFsRead.c_str());
308 rmdir(mSdcardFsWrite.c_str());
309 rmdir(mSdcardFsFull.c_str());
Jeff Sharkey66270a22015-06-24 11:49:24 -0700310
Martijn Coenenadcc8452019-12-09 14:18:01 +0100311 mSdcardFsDefault.clear();
312 mSdcardFsRead.clear();
313 mSdcardFsWrite.clear();
314 mSdcardFsFull.clear();
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800315
316 return OK;
317}
318
Martijn Coenen62a4b272020-01-31 15:23:09 +0100319std::string EmulatedVolume::getRootPath() const {
320 int user_id = getMountUserId();
321 std::string volumeRoot = StringPrintf("%s/%d", getInternalPath().c_str(), user_id);
322
323 return volumeRoot;
324}
325
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800326} // namespace vold
327} // namespace android