blob: f99a369a3ec4f272c40a742dfc0285c40d17e0e0 [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"
Martijn Coenen73e30102022-07-12 08:11:02 +000021#include "VolumeBase.h"
Sudheer Shanka40ab6742018-09-18 13:07:45 -070022#include "VolumeManager.h"
Jeff Sharkeydeb24052015-03-02 21:01:40 -080023
Elliott Hughes7e128fb2015-12-04 15:50:53 -080024#include <android-base/logging.h>
Zim3623a212019-07-19 16:46:53 +010025#include <android-base/properties.h>
Martijn Coenen449a7d82020-03-16 14:37:33 +010026#include <android-base/scopeguard.h>
Sudheer Shanka53947a32018-08-01 10:24:13 -070027#include <android-base/stringprintf.h>
Jeff Sharkeydeb24052015-03-02 21:01:40 -080028#include <cutils/fs.h>
Jeff Sharkeydeb24052015-03-02 21:01:40 -080029#include <private/android_filesystem_config.h>
Jeff Sharkey7bdf4d52017-09-18 14:47:10 -060030#include <utils/Timers.h>
Jeff Sharkeydeb24052015-03-02 21:01:40 -080031
32#include <fcntl.h>
33#include <stdlib.h>
34#include <sys/mount.h>
35#include <sys/stat.h>
Elliott Hughes0e08e842017-05-18 09:08:24 -070036#include <sys/sysmacros.h>
Paul Crowleyedf7a4e2018-09-18 15:14:18 -070037#include <sys/types.h>
Jeff Sharkeydeb24052015-03-02 21:01:40 -080038#include <sys/wait.h>
39
Dan Albertae9e8902015-03-16 10:35:17 -070040using android::base::StringPrintf;
41
Jeff Sharkeydeb24052015-03-02 21:01:40 -080042namespace android {
43namespace vold {
44
Martijn Coenenadcc8452019-12-09 14:18:01 +010045static const char* kSdcardFsPath = "/system/bin/sdcard";
Jeff Sharkeydeb24052015-03-02 21:01:40 -080046
Zima438b242019-09-25 14:37:38 +010047EmulatedVolume::EmulatedVolume(const std::string& rawPath, int userId)
Martijn Coenenadcc8452019-12-09 14:18:01 +010048 : VolumeBase(Type::kEmulated) {
Zima438b242019-09-25 14:37:38 +010049 setId(StringPrintf("emulated;%u", userId));
Jeff Sharkeydeb24052015-03-02 21:01:40 -080050 mRawPath = rawPath;
Jeff Sharkey66270a22015-06-24 11:49:24 -070051 mLabel = "emulated";
Martijn Coenenfd7362d2019-12-11 14:57:59 +010052 mFuseMounted = false;
Alessio Balsini583ae3e2022-03-23 18:27:59 +000053 mFuseBpfEnabled = IsFuseBpfEnabled();
Daniel Rosenbergf36bddd2020-05-11 22:58:42 -070054 mUseSdcardFs = IsSdcardfsUsed();
Ricky Wai07e64a42020-02-11 14:31:24 +000055 mAppDataIsolationEnabled = base::GetBoolProperty(kVoldAppDataIsolationEnabled, false);
Jeff Sharkey3161fb32015-04-12 16:03:33 -070056}
57
Zima438b242019-09-25 14:37:38 +010058EmulatedVolume::EmulatedVolume(const std::string& rawPath, dev_t device, const std::string& fsUuid,
59 int userId)
Martijn Coenenadcc8452019-12-09 14:18:01 +010060 : VolumeBase(Type::kEmulated) {
Zima438b242019-09-25 14:37:38 +010061 setId(StringPrintf("emulated:%u,%u;%u", major(device), minor(device), userId));
Jeff Sharkey3161fb32015-04-12 16:03:33 -070062 mRawPath = rawPath;
Jeff Sharkey66270a22015-06-24 11:49:24 -070063 mLabel = fsUuid;
Greg Kaiser5298ccc2019-12-12 05:41:46 -080064 mFuseMounted = false;
Alessio Balsini583ae3e2022-03-23 18:27:59 +000065 mFuseBpfEnabled = IsFuseBpfEnabled();
Daniel Rosenbergf36bddd2020-05-11 22:58:42 -070066 mUseSdcardFs = IsSdcardfsUsed();
Ricky Wai07e64a42020-02-11 14:31:24 +000067 mAppDataIsolationEnabled = base::GetBoolProperty(kVoldAppDataIsolationEnabled, false);
Jeff Sharkeydeb24052015-03-02 21:01:40 -080068}
69
Paul Crowleyedf7a4e2018-09-18 15:14:18 -070070EmulatedVolume::~EmulatedVolume() {}
Jeff Sharkeydeb24052015-03-02 21:01:40 -080071
Martijn Coenen73e30102022-07-12 08:11:02 +000072std::string EmulatedVolume::getLabel() const {
Jeff Sharkey81f55c62015-07-07 14:37:03 -070073 // We could have migrated storage to an adopted private volume, so always
74 // call primary storage "emulated" to avoid media rescans.
Jeff Sharkey81f55c62015-07-07 14:37:03 -070075 if (getMountFlags() & MountFlags::kPrimary) {
Martijn Coenen6f5802e2019-11-28 11:53:53 +010076 return "emulated";
77 } else {
78 return mLabel;
Jeff Sharkey81f55c62015-07-07 14:37:03 -070079 }
Martijn Coenen6f5802e2019-11-28 11:53:53 +010080}
81
Martijn Coenen62a4b272020-01-31 15:23:09 +010082// Creates a bind mount from source to target
Martijn Coenen449a7d82020-03-16 14:37:33 +010083static status_t doFuseBindMount(const std::string& source, const std::string& target,
84 std::list<std::string>& pathsToUnmount) {
Martijn Coenen3a2dbfe2020-01-11 19:38:37 +010085 LOG(INFO) << "Bind mounting " << source << " on " << target;
86 auto status = BindMount(source, target);
87 if (status != OK) {
88 return status;
89 }
90 LOG(INFO) << "Bind mounted " << source << " on " << target;
Martijn Coenen449a7d82020-03-16 14:37:33 +010091 pathsToUnmount.push_front(target);
Martijn Coenen3a2dbfe2020-01-11 19:38:37 +010092 return OK;
93}
94
Martijn Coenen73e30102022-07-12 08:11:02 +000095// Bind mounts the volume 'volume' onto this volume.
96status_t EmulatedVolume::bindMountVolume(const EmulatedVolume& volume,
97 std::list<std::string>& pathsToUnmount) {
98 int myUserId = getMountUserId();
99 int volumeUserId = volume.getMountUserId();
100 std::string label = volume.getLabel();
101
102 // eg /mnt/user/10/emulated/10
103 std::string srcUserPath = GetFuseMountPathForUser(volumeUserId, label);
104 std::string srcPath = StringPrintf("%s/%d", srcUserPath.c_str(), volumeUserId);
105 // eg /mnt/user/0/emulated/10
106 std::string dstUserPath = GetFuseMountPathForUser(myUserId, label);
107 std::string dstPath = StringPrintf("%s/%d", dstUserPath.c_str(), volumeUserId);
108
109 auto status = doFuseBindMount(srcPath, dstPath, pathsToUnmount);
110 if (status == OK) {
111 // Store the mount path, so we can unmount it when this volume goes away
112 mSharedStorageMountPath = dstPath;
113 }
114
115 return status;
116}
117
Martijn Coenen86f21a22020-01-06 09:48:14 +0100118status_t EmulatedVolume::mountFuseBindMounts() {
119 std::string androidSource;
120 std::string label = getLabel();
121 int userId = getMountUserId();
Martijn Coenen449a7d82020-03-16 14:37:33 +0100122 std::list<std::string> pathsToUnmount;
123
124 auto unmounter = [&]() {
125 LOG(INFO) << "mountFuseBindMounts() unmount scope_guard running";
126 for (const auto& path : pathsToUnmount) {
127 LOG(INFO) << "Unmounting " << path;
128 auto status = UnmountTree(path);
129 if (status != OK) {
130 LOG(INFO) << "Failed to unmount " << path;
131 } else {
132 LOG(INFO) << "Unmounted " << path;
133 }
134 }
135 };
136 auto unmount_guard = android::base::make_scope_guard(unmounter);
Martijn Coenen86f21a22020-01-06 09:48:14 +0100137
138 if (mUseSdcardFs) {
139 androidSource = StringPrintf("/mnt/runtime/default/%s/%d/Android", label.c_str(), userId);
140 } else {
141 androidSource = StringPrintf("/%s/%d/Android", mRawPath.c_str(), userId);
142 }
Martijn Coenen57002612019-11-28 11:56:13 +0100143
Ricky Wai07e64a42020-02-11 14:31:24 +0000144 status_t status = OK;
Ricky Wai259a49a2021-03-19 15:35:49 +0000145 // Zygote will unmount these dirs if app data isolation is enabled, so apps
146 // cannot access these dirs directly.
147 std::string androidDataSource = StringPrintf("%s/data", androidSource.c_str());
148 std::string androidDataTarget(
149 StringPrintf("/mnt/user/%d/%s/%d/Android/data", userId, label.c_str(), userId));
150 status = doFuseBindMount(androidDataSource, androidDataTarget, pathsToUnmount);
151 if (status != OK) {
152 return status;
153 }
Ricky Wai07e64a42020-02-11 14:31:24 +0000154
Ricky Wai259a49a2021-03-19 15:35:49 +0000155 std::string androidObbSource = StringPrintf("%s/obb", androidSource.c_str());
156 std::string androidObbTarget(
157 StringPrintf("/mnt/user/%d/%s/%d/Android/obb", userId, label.c_str(), userId));
158 status = doFuseBindMount(androidObbSource, androidObbTarget, pathsToUnmount);
159 if (status != OK) {
160 return status;
Martijn Coenen57002612019-11-28 11:56:13 +0100161 }
Zimb6488f32020-03-17 15:15:42 +0000162
Martijn Coenen3a2dbfe2020-01-11 19:38:37 +0100163 // Installers get the same view as all other apps, with the sole exception that the
164 // OBB dirs (Android/obb) are writable to them. On sdcardfs devices, this requires
165 // a special bind mount, since app-private and OBB dirs share the same GID, but we
166 // only want to give access to the latter.
Martijn Coenen449a7d82020-03-16 14:37:33 +0100167 if (mUseSdcardFs) {
Ricky Waief639212020-04-07 13:43:20 +0100168 std::string obbSource(StringPrintf("/mnt/runtime/write/%s/%d/Android/obb",
169 label.c_str(), userId));
170 std::string obbInstallerTarget(StringPrintf("/mnt/installer/%d/%s/%d/Android/obb",
171 userId, label.c_str(), userId));
Martijn Coenen3a2dbfe2020-01-11 19:38:37 +0100172
Ricky Waief639212020-04-07 13:43:20 +0100173 status = doFuseBindMount(obbSource, obbInstallerTarget, pathsToUnmount);
174 if (status != OK) {
175 return status;
176 }
Ricky Waief639212020-04-07 13:43:20 +0100177 }
178
Martijn Coenen73e30102022-07-12 08:11:02 +0000179 // For users that share their volume with another user (eg a clone
180 // profile), the current mount setup can cause page cache inconsistency
181 // issues. Let's say this is user 10, and the user it shares storage with
182 // is user 0.
183 // Then:
184 // * The FUSE daemon for user 0 serves /mnt/user/0
185 // * The FUSE daemon for user 10 serves /mnt/user/10
186 // The emulated volume for user 10 would be located at two paths:
187 // /mnt/user/0/emulated/10
188 // /mnt/user/10/emulated/10
189 // Since these paths refer to the same files but are served by different FUSE
190 // daemons, this can result in page cache inconsistency issues. To prevent this,
191 // bind mount the relevant paths for the involved users:
192 // 1. /mnt/user/10/emulated/10 =B=> /mnt/user/0/emulated/10
193 // 2. /mnt/user/0/emulated/0 =B=> /mnt/user/10/emulated/0
194 //
195 // This will ensure that any access to the volume for a specific user always
196 // goes through a single FUSE daemon.
197 userid_t sharedStorageUserId = VolumeManager::Instance()->getSharedStorageUser(userId);
198 if (sharedStorageUserId != USER_UNKNOWN) {
199 auto filter_fn = [&](const VolumeBase& vol) {
200 if (vol.getState() != VolumeBase::State::kMounted) {
201 // The volume must be mounted
202 return false;
203 }
204 if (vol.getType() != VolumeBase::Type::kEmulated) {
205 return false;
206 }
207 if (vol.getMountUserId() != sharedStorageUserId) {
208 return false;
209 }
210 if ((vol.getMountFlags() & MountFlags::kPrimary) == 0) {
211 // We only care about the primary emulated volume, so not a private
212 // volume with an emulated volume stacked on top.
213 return false;
214 }
215 return true;
216 };
217 auto vol = VolumeManager::Instance()->findVolumeWithFilter(filter_fn);
218 if (vol != nullptr) {
219 auto sharedVol = static_cast<EmulatedVolume*>(vol.get());
220 // Bind mount this volume in the other user's primary volume
221 status = sharedVol->bindMountVolume(*this, pathsToUnmount);
222 if (status != OK) {
223 return status;
224 }
225 // And vice-versa
226 status = bindMountVolume(*sharedVol, pathsToUnmount);
227 if (status != OK) {
228 return status;
229 }
230 }
231 }
Martijn Coenen449a7d82020-03-16 14:37:33 +0100232 unmount_guard.Disable();
Martijn Coenen57002612019-11-28 11:56:13 +0100233 return OK;
234}
235
Martijn Coenen86f21a22020-01-06 09:48:14 +0100236status_t EmulatedVolume::unmountFuseBindMounts() {
237 std::string label = getLabel();
238 int userId = getMountUserId();
239
Martijn Coenen73e30102022-07-12 08:11:02 +0000240 if (!mSharedStorageMountPath.empty()) {
241 LOG(INFO) << "Unmounting " << mSharedStorageMountPath;
242 auto status = UnmountTree(mSharedStorageMountPath);
243 if (status != OK) {
244 LOG(ERROR) << "Failed to unmount " << mSharedStorageMountPath;
245 }
246 mSharedStorageMountPath = "";
247 }
Ricky Waief639212020-04-07 13:43:20 +0100248 if (mUseSdcardFs || mAppDataIsolationEnabled) {
Martijn Coenen3a2dbfe2020-01-11 19:38:37 +0100249 std::string installerTarget(
250 StringPrintf("/mnt/installer/%d/%s/%d/Android/obb", userId, label.c_str(), userId));
251 LOG(INFO) << "Unmounting " << installerTarget;
252 auto status = UnmountTree(installerTarget);
253 if (status != OK) {
254 LOG(ERROR) << "Failed to unmount " << installerTarget;
255 // Intentional continue to try to unmount the other bind mount
256 }
257 }
Ricky Waief639212020-04-07 13:43:20 +0100258 if (mAppDataIsolationEnabled) {
259 std::string obbTarget( StringPrintf("/mnt/androidwritable/%d/%s/%d/Android/obb",
260 userId, label.c_str(), userId));
261 LOG(INFO) << "Unmounting " << obbTarget;
262 auto status = UnmountTree(obbTarget);
263 if (status != OK) {
264 LOG(ERROR) << "Failed to unmount " << obbTarget;
265 // Intentional continue to try to unmount the other bind mount
266 }
267 std::string dataTarget(StringPrintf("/mnt/androidwritable/%d/%s/%d/Android/data",
268 userId, label.c_str(), userId));
269 LOG(INFO) << "Unmounting " << dataTarget;
270 status = UnmountTree(dataTarget);
271 if (status != OK) {
272 LOG(ERROR) << "Failed to unmount " << dataTarget;
273 // Intentional continue to try to unmount the other bind mount
274 }
275 }
276
Ricky Wai07e64a42020-02-11 14:31:24 +0000277 // When app data isolation is enabled, kill all apps that obb/ is mounted, otherwise we should
278 // umount the whole Android/ dir.
279 if (mAppDataIsolationEnabled) {
280 std::string appObbDir(StringPrintf("%s/%d/Android/obb", getPath().c_str(), userId));
Ricky Wai23356372021-04-30 09:53:07 +0100281 // Here we assume obb/data dirs is mounted as tmpfs, then it must be caused by
282 // app data isolation.
283 KillProcessesWithTmpfsMountPrefix(appObbDir);
Martijn Coenen57002612019-11-28 11:56:13 +0100284 }
Ricky Wai5f2a9fe2021-05-05 14:43:45 +0000285
286 // Always unmount data and obb dirs as they are mounted to lowerfs for speeding up access.
287 std::string androidDataTarget(
288 StringPrintf("/mnt/user/%d/%s/%d/Android/data", userId, label.c_str(), userId));
289
290 LOG(INFO) << "Unmounting " << androidDataTarget;
291 auto status = UnmountTree(androidDataTarget);
292 if (status != OK) {
293 return status;
294 }
295 LOG(INFO) << "Unmounted " << androidDataTarget;
296
297 std::string androidObbTarget(
298 StringPrintf("/mnt/user/%d/%s/%d/Android/obb", userId, label.c_str(), userId));
299
300 LOG(INFO) << "Unmounting " << androidObbTarget;
301 status = UnmountTree(androidObbTarget);
302 if (status != OK) {
303 return status;
304 }
305 LOG(INFO) << "Unmounted " << androidObbTarget;
Martijn Coenen57002612019-11-28 11:56:13 +0100306 return OK;
307}
308
Martijn Coenen449a7d82020-03-16 14:37:33 +0100309status_t EmulatedVolume::unmountSdcardFs() {
310 if (!mUseSdcardFs || getMountUserId() != 0) {
311 // For sdcardfs, only unmount for user 0, since user 0 will always be running
312 // and the paths don't change for different users.
313 return OK;
314 }
315
316 ForceUnmount(mSdcardFsDefault);
317 ForceUnmount(mSdcardFsRead);
318 ForceUnmount(mSdcardFsWrite);
319 ForceUnmount(mSdcardFsFull);
320
321 rmdir(mSdcardFsDefault.c_str());
322 rmdir(mSdcardFsRead.c_str());
323 rmdir(mSdcardFsWrite.c_str());
324 rmdir(mSdcardFsFull.c_str());
325
326 mSdcardFsDefault.clear();
327 mSdcardFsRead.clear();
328 mSdcardFsWrite.clear();
329 mSdcardFsFull.clear();
330
331 return OK;
332}
333
Martijn Coenen6f5802e2019-11-28 11:53:53 +0100334status_t EmulatedVolume::doMount() {
335 std::string label = getLabel();
Youkichi Hosoi2991cbe2021-11-11 11:23:01 +0900336 bool isVisible = isVisibleForWrite();
Jeff Sharkey81f55c62015-07-07 14:37:03 -0700337
Martijn Coenenadcc8452019-12-09 14:18:01 +0100338 mSdcardFsDefault = StringPrintf("/mnt/runtime/default/%s", label.c_str());
339 mSdcardFsRead = StringPrintf("/mnt/runtime/read/%s", label.c_str());
340 mSdcardFsWrite = StringPrintf("/mnt/runtime/write/%s", label.c_str());
341 mSdcardFsFull = StringPrintf("/mnt/runtime/full/%s", label.c_str());
Jeff Sharkey66270a22015-06-24 11:49:24 -0700342
343 setInternalPath(mRawPath);
Jeff Sharkey81f55c62015-07-07 14:37:03 -0700344 setPath(StringPrintf("/storage/%s", label.c_str()));
Jeff Sharkey66270a22015-06-24 11:49:24 -0700345
Martijn Coenenadcc8452019-12-09 14:18:01 +0100346 if (fs_prepare_dir(mSdcardFsDefault.c_str(), 0700, AID_ROOT, AID_ROOT) ||
347 fs_prepare_dir(mSdcardFsRead.c_str(), 0700, AID_ROOT, AID_ROOT) ||
348 fs_prepare_dir(mSdcardFsWrite.c_str(), 0700, AID_ROOT, AID_ROOT) ||
349 fs_prepare_dir(mSdcardFsFull.c_str(), 0700, AID_ROOT, AID_ROOT)) {
Jeff Sharkey66270a22015-06-24 11:49:24 -0700350 PLOG(ERROR) << getId() << " failed to create mount points";
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800351 return -errno;
352 }
353
Martijn Coenenadcc8452019-12-09 14:18:01 +0100354 dev_t before = GetDevice(mSdcardFsFull);
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700355
Martijn Coenen6f5802e2019-11-28 11:53:53 +0100356 // Mount sdcardfs regardless of FUSE, since we need it to bind-mount on top of the
357 // FUSE volume for various reasons.
Martijn Coenen86f21a22020-01-06 09:48:14 +0100358 if (mUseSdcardFs && getMountUserId() == 0) {
Martijn Coenen6f5802e2019-11-28 11:53:53 +0100359 LOG(INFO) << "Executing sdcardfs";
360 int sdcardFsPid;
361 if (!(sdcardFsPid = fork())) {
362 // clang-format off
363 if (execl(kSdcardFsPath, kSdcardFsPath,
364 "-u", "1023", // AID_MEDIA_RW
365 "-g", "1023", // AID_MEDIA_RW
366 "-m",
367 "-w",
368 "-G",
369 "-i",
370 "-o",
371 mRawPath.c_str(),
372 label.c_str(),
373 NULL)) {
374 // clang-format on
375 PLOG(ERROR) << "Failed to exec";
376 }
377
378 LOG(ERROR) << "sdcardfs exiting";
379 _exit(1);
380 }
381
382 if (sdcardFsPid == -1) {
383 PLOG(ERROR) << getId() << " failed to fork";
384 return -errno;
385 }
386
387 nsecs_t start = systemTime(SYSTEM_TIME_BOOTTIME);
388 while (before == GetDevice(mSdcardFsFull)) {
389 LOG(DEBUG) << "Waiting for sdcardfs to spin up...";
390 usleep(50000); // 50ms
391
392 nsecs_t now = systemTime(SYSTEM_TIME_BOOTTIME);
393 if (nanoseconds_to_milliseconds(now - start) > 5000) {
394 LOG(WARNING) << "Timed out while waiting for sdcardfs to spin up";
395 return -ETIMEDOUT;
396 }
397 }
398 /* sdcardfs will have exited already. The filesystem will still be running */
399 TEMP_FAILURE_RETRY(waitpid(sdcardFsPid, nullptr, 0));
400 sdcardFsPid = 0;
401 }
Martijn Coenen449a7d82020-03-16 14:37:33 +0100402
Ricky Waie78c78c2021-01-14 15:51:54 +0000403 if (isVisible) {
Martijn Coenen449a7d82020-03-16 14:37:33 +0100404 // Make sure we unmount sdcardfs if we bail out with an error below
405 auto sdcardfs_unmounter = [&]() {
406 LOG(INFO) << "sdcardfs_unmounter scope_guard running";
407 unmountSdcardFs();
408 };
409 auto sdcardfs_guard = android::base::make_scope_guard(sdcardfs_unmounter);
410
Zim3623a212019-07-19 16:46:53 +0100411 LOG(INFO) << "Mounting emulated fuse volume";
Nandana Dutta914cc72019-08-29 15:22:42 +0100412 android::base::unique_fd fd;
Zim981222f2019-09-09 10:24:44 +0100413 int user_id = getMountUserId();
Martijn Coenen62a4b272020-01-31 15:23:09 +0100414 auto volumeRoot = getRootPath();
Zim981222f2019-09-09 10:24:44 +0100415
Martijn Coenen62a4b272020-01-31 15:23:09 +0100416 // Make sure Android/ dirs exist for bind mounting
417 status_t res = PrepareAndroidDirs(volumeRoot);
418 if (res != OK) {
419 LOG(ERROR) << "Failed to prepare Android/ directories";
420 return res;
421 }
422
423 res = MountUserFuse(user_id, getInternalPath(), label, &fd);
424 if (res != 0) {
Zim3623a212019-07-19 16:46:53 +0100425 PLOG(ERROR) << "Failed to mount emulated fuse volume";
Martijn Coenen62a4b272020-01-31 15:23:09 +0100426 return res;
Zim3623a212019-07-19 16:46:53 +0100427 }
Zim5048b4b2019-11-19 09:16:03 +0000428
Martijn Coenen6f5802e2019-11-28 11:53:53 +0100429 mFuseMounted = true;
Martijn Coenen449a7d82020-03-16 14:37:33 +0100430 auto fuse_unmounter = [&]() {
431 LOG(INFO) << "fuse_unmounter scope_guard running";
432 fd.reset();
433 if (UnmountUserFuse(user_id, getInternalPath(), label) != OK) {
434 PLOG(INFO) << "UnmountUserFuse failed on emulated fuse volume";
435 }
436 mFuseMounted = false;
437 };
438 auto fuse_guard = android::base::make_scope_guard(fuse_unmounter);
439
Zim5048b4b2019-11-19 09:16:03 +0000440 auto callback = getMountCallback();
441 if (callback) {
442 bool is_ready = false;
443 callback->onVolumeChecking(std::move(fd), getPath(), getInternalPath(), &is_ready);
444 if (!is_ready) {
445 return -EIO;
446 }
447 }
Martijn Coenen57002612019-11-28 11:56:13 +0100448
Alessio Balsinidd1e91f2021-10-13 12:03:18 +0100449 if (!mFuseBpfEnabled) {
450 // Only do the bind-mounts when we know for sure the FUSE daemon can resolve the path.
451 res = mountFuseBindMounts();
452 if (res != OK) {
453 return res;
454 }
Zimdf073f52020-01-15 15:00:07 +0000455 }
Martijn Coenen449a7d82020-03-16 14:37:33 +0100456
Nikita Ioffedcee5c12020-06-12 12:59:45 +0100457 ConfigureReadAheadForFuse(GetFuseMountPathForUser(user_id, label), 256u);
458
Martijn Coenena4850062020-06-29 11:53:34 +0200459 // By default, FUSE has a max_dirty ratio of 1%. This means that out of
460 // all dirty pages in the system, only 1% is allowed to belong to any
461 // FUSE filesystem. The reason this is in place is that FUSE
462 // filesystems shouldn't be trusted by default; a FUSE filesystem could
463 // take up say 100% of dirty pages, and subsequently refuse to write
464 // them back to storage. The kernel will then apply rate-limiting, and
465 // block other tasks from writing. For this particular FUSE filesystem
466 // however, we trust the implementation, because it is a part of the
467 // Android platform. So use the default ratio of 100%.
468 //
469 // The reason we're setting this is that there's a suspicion that the
470 // kernel starts rate-limiting the FUSE filesystem under extreme
471 // memory pressure scenarios. While the kernel will only rate limit if
472 // the writeback can't keep up with the write rate, under extreme
473 // memory pressure the write rate may dip as well, in which case FUSE
474 // writes to a 1% max_ratio filesystem are throttled to an extreme amount.
475 //
476 // To prevent this, just give FUSE 40% max_ratio, meaning it can take
477 // up to 40% of all dirty pages in the system.
478 ConfigureMaxDirtyRatioForFuse(GetFuseMountPathForUser(user_id, label), 40u);
479
Martijn Coenen449a7d82020-03-16 14:37:33 +0100480 // All mounts where successful, disable scope guards
481 sdcardfs_guard.Disable();
482 fuse_guard.Disable();
Zim3623a212019-07-19 16:46:53 +0100483 }
484
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800485 return OK;
486}
487
488status_t EmulatedVolume::doUnmount() {
Martijn Coenen8f1e7f22019-11-29 15:38:55 +0100489 int userId = getMountUserId();
490
491 // Kill all processes using the filesystem before we unmount it. If we
492 // unmount the filesystem first, most file system operations will return
Narayan Kamathea243a32016-01-21 12:26:05 +0000493 // ENOTCONN until the unmount completes. This is an exotic and unusual
494 // error code and might cause broken behaviour in applications.
Martijn Coenen8f1e7f22019-11-29 15:38:55 +0100495 if (mFuseMounted) {
496 // For FUSE specifically, we have an emulated volume per user, so only kill
497 // processes using files from this particular user.
498 std::string user_path(StringPrintf("%s/%d", getPath().c_str(), getMountUserId()));
499 LOG(INFO) << "Killing all processes referencing " << user_path;
500 KillProcessesUsingPath(user_path);
501 } else {
502 KillProcessesUsingPath(getPath());
503 }
Zim3623a212019-07-19 16:46:53 +0100504
Martijn Coenen6f5802e2019-11-28 11:53:53 +0100505 if (mFuseMounted) {
506 std::string label = getLabel();
Ricky Wai07e64a42020-02-11 14:31:24 +0000507
Alessio Balsinidd1e91f2021-10-13 12:03:18 +0100508 if (!mFuseBpfEnabled) {
509 // Ignoring unmount return status because we do want to try to
510 // unmount the rest cleanly.
511 unmountFuseBindMounts();
512 }
Martijn Coenen449a7d82020-03-16 14:37:33 +0100513
Martijn Coenen57002612019-11-28 11:56:13 +0100514 if (UnmountUserFuse(userId, getInternalPath(), label) != OK) {
Zima438b242019-09-25 14:37:38 +0100515 PLOG(INFO) << "UnmountUserFuse failed on emulated fuse volume";
516 return -errno;
Zim3623a212019-07-19 16:46:53 +0100517 }
518
Martijn Coenen6f5802e2019-11-28 11:53:53 +0100519 mFuseMounted = false;
520 }
Zim3623a212019-07-19 16:46:53 +0100521
Martijn Coenen449a7d82020-03-16 14:37:33 +0100522 return unmountSdcardFs();
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800523}
524
Martijn Coenen62a4b272020-01-31 15:23:09 +0100525std::string EmulatedVolume::getRootPath() const {
526 int user_id = getMountUserId();
527 std::string volumeRoot = StringPrintf("%s/%d", getInternalPath().c_str(), user_id);
528
529 return volumeRoot;
530}
531
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800532} // namespace vold
533} // namespace android