blob: 26d958227cd88fe61a9e1c810322aebffc4579ee [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>
Martijn Coenen449a7d82020-03-16 14:37:33 +010025#include <android-base/scopeguard.h>
Sudheer Shanka53947a32018-08-01 10:24:13 -070026#include <android-base/stringprintf.h>
Jeff Sharkeydeb24052015-03-02 21:01:40 -080027#include <cutils/fs.h>
Jeff Sharkeydeb24052015-03-02 21:01:40 -080028#include <private/android_filesystem_config.h>
Jeff Sharkey7bdf4d52017-09-18 14:47:10 -060029#include <utils/Timers.h>
Jeff Sharkeydeb24052015-03-02 21:01:40 -080030
31#include <fcntl.h>
32#include <stdlib.h>
33#include <sys/mount.h>
34#include <sys/stat.h>
Elliott Hughes0e08e842017-05-18 09:08:24 -070035#include <sys/sysmacros.h>
Paul Crowleyedf7a4e2018-09-18 15:14:18 -070036#include <sys/types.h>
Jeff Sharkeydeb24052015-03-02 21:01:40 -080037#include <sys/wait.h>
38
Dan Albertae9e8902015-03-16 10:35:17 -070039using android::base::StringPrintf;
40
Jeff Sharkeydeb24052015-03-02 21:01:40 -080041namespace android {
42namespace vold {
43
Martijn Coenenadcc8452019-12-09 14:18:01 +010044static const char* kSdcardFsPath = "/system/bin/sdcard";
Jeff Sharkeydeb24052015-03-02 21:01:40 -080045
Zima438b242019-09-25 14:37:38 +010046EmulatedVolume::EmulatedVolume(const std::string& rawPath, int userId)
Martijn Coenenadcc8452019-12-09 14:18:01 +010047 : VolumeBase(Type::kEmulated) {
Zima438b242019-09-25 14:37:38 +010048 setId(StringPrintf("emulated;%u", userId));
Jeff Sharkeydeb24052015-03-02 21:01:40 -080049 mRawPath = rawPath;
Jeff Sharkey66270a22015-06-24 11:49:24 -070050 mLabel = "emulated";
Martijn Coenenfd7362d2019-12-11 14:57:59 +010051 mFuseMounted = false;
Daniel Rosenbergf36bddd2020-05-11 22:58:42 -070052 mUseSdcardFs = IsSdcardfsUsed();
Ricky Wai07e64a42020-02-11 14:31:24 +000053 mAppDataIsolationEnabled = base::GetBoolProperty(kVoldAppDataIsolationEnabled, false);
Jeff Sharkey3161fb32015-04-12 16:03:33 -070054}
55
Zima438b242019-09-25 14:37:38 +010056EmulatedVolume::EmulatedVolume(const std::string& rawPath, dev_t device, const std::string& fsUuid,
57 int userId)
Martijn Coenenadcc8452019-12-09 14:18:01 +010058 : VolumeBase(Type::kEmulated) {
Zima438b242019-09-25 14:37:38 +010059 setId(StringPrintf("emulated:%u,%u;%u", major(device), minor(device), userId));
Jeff Sharkey3161fb32015-04-12 16:03:33 -070060 mRawPath = rawPath;
Jeff Sharkey66270a22015-06-24 11:49:24 -070061 mLabel = fsUuid;
Greg Kaiser5298ccc2019-12-12 05:41:46 -080062 mFuseMounted = false;
Daniel Rosenbergf36bddd2020-05-11 22:58:42 -070063 mUseSdcardFs = IsSdcardfsUsed();
Ricky Wai07e64a42020-02-11 14:31:24 +000064 mAppDataIsolationEnabled = base::GetBoolProperty(kVoldAppDataIsolationEnabled, false);
Jeff Sharkeydeb24052015-03-02 21:01:40 -080065}
66
Paul Crowleyedf7a4e2018-09-18 15:14:18 -070067EmulatedVolume::~EmulatedVolume() {}
Jeff Sharkeydeb24052015-03-02 21:01:40 -080068
Martijn Coenen6f5802e2019-11-28 11:53:53 +010069std::string EmulatedVolume::getLabel() {
Jeff Sharkey81f55c62015-07-07 14:37:03 -070070 // We could have migrated storage to an adopted private volume, so always
71 // call primary storage "emulated" to avoid media rescans.
Jeff Sharkey81f55c62015-07-07 14:37:03 -070072 if (getMountFlags() & MountFlags::kPrimary) {
Martijn Coenen6f5802e2019-11-28 11:53:53 +010073 return "emulated";
74 } else {
75 return mLabel;
Jeff Sharkey81f55c62015-07-07 14:37:03 -070076 }
Martijn Coenen6f5802e2019-11-28 11:53:53 +010077}
78
Martijn Coenen62a4b272020-01-31 15:23:09 +010079// Creates a bind mount from source to target
Martijn Coenen449a7d82020-03-16 14:37:33 +010080static status_t doFuseBindMount(const std::string& source, const std::string& target,
81 std::list<std::string>& pathsToUnmount) {
Martijn Coenen3a2dbfe2020-01-11 19:38:37 +010082 LOG(INFO) << "Bind mounting " << source << " on " << target;
83 auto status = BindMount(source, target);
84 if (status != OK) {
85 return status;
86 }
87 LOG(INFO) << "Bind mounted " << source << " on " << target;
Martijn Coenen449a7d82020-03-16 14:37:33 +010088 pathsToUnmount.push_front(target);
Martijn Coenen3a2dbfe2020-01-11 19:38:37 +010089 return OK;
90}
91
Martijn Coenen86f21a22020-01-06 09:48:14 +010092status_t EmulatedVolume::mountFuseBindMounts() {
93 std::string androidSource;
94 std::string label = getLabel();
95 int userId = getMountUserId();
Martijn Coenen449a7d82020-03-16 14:37:33 +010096 std::list<std::string> pathsToUnmount;
97
98 auto unmounter = [&]() {
99 LOG(INFO) << "mountFuseBindMounts() unmount scope_guard running";
100 for (const auto& path : pathsToUnmount) {
101 LOG(INFO) << "Unmounting " << path;
102 auto status = UnmountTree(path);
103 if (status != OK) {
104 LOG(INFO) << "Failed to unmount " << path;
105 } else {
106 LOG(INFO) << "Unmounted " << path;
107 }
108 }
109 };
110 auto unmount_guard = android::base::make_scope_guard(unmounter);
Martijn Coenen86f21a22020-01-06 09:48:14 +0100111
112 if (mUseSdcardFs) {
113 androidSource = StringPrintf("/mnt/runtime/default/%s/%d/Android", label.c_str(), userId);
114 } else {
115 androidSource = StringPrintf("/%s/%d/Android", mRawPath.c_str(), userId);
116 }
Martijn Coenen57002612019-11-28 11:56:13 +0100117
Ricky Wai07e64a42020-02-11 14:31:24 +0000118 status_t status = OK;
119 // When app data isolation is enabled, obb/ will be mounted per app, otherwise we should
120 // bind mount the whole Android/ to speed up reading.
121 if (!mAppDataIsolationEnabled) {
Martijn Coenen449a7d82020-03-16 14:37:33 +0100122 std::string androidDataSource = StringPrintf("%s/data", androidSource.c_str());
123 std::string androidDataTarget(
124 StringPrintf("/mnt/user/%d/%s/%d/Android/data", userId, label.c_str(), userId));
125 status = doFuseBindMount(androidDataSource, androidDataTarget, pathsToUnmount);
126 if (status != OK) {
127 return status;
128 }
Ricky Wai07e64a42020-02-11 14:31:24 +0000129
Martijn Coenen449a7d82020-03-16 14:37:33 +0100130 std::string androidObbSource = StringPrintf("%s/obb", androidSource.c_str());
131 std::string androidObbTarget(
132 StringPrintf("/mnt/user/%d/%s/%d/Android/obb", userId, label.c_str(), userId));
133 status = doFuseBindMount(androidObbSource, androidObbTarget, pathsToUnmount);
134 if (status != OK) {
135 return status;
136 }
Martijn Coenen57002612019-11-28 11:56:13 +0100137 }
Zimb6488f32020-03-17 15:15:42 +0000138
Martijn Coenen3a2dbfe2020-01-11 19:38:37 +0100139 // Installers get the same view as all other apps, with the sole exception that the
140 // OBB dirs (Android/obb) are writable to them. On sdcardfs devices, this requires
141 // a special bind mount, since app-private and OBB dirs share the same GID, but we
142 // only want to give access to the latter.
Martijn Coenen449a7d82020-03-16 14:37:33 +0100143 if (mUseSdcardFs) {
Ricky Waief639212020-04-07 13:43:20 +0100144 std::string obbSource(StringPrintf("/mnt/runtime/write/%s/%d/Android/obb",
145 label.c_str(), userId));
146 std::string obbInstallerTarget(StringPrintf("/mnt/installer/%d/%s/%d/Android/obb",
147 userId, label.c_str(), userId));
Martijn Coenen3a2dbfe2020-01-11 19:38:37 +0100148
Ricky Waief639212020-04-07 13:43:20 +0100149 status = doFuseBindMount(obbSource, obbInstallerTarget, pathsToUnmount);
150 if (status != OK) {
151 return status;
152 }
153 } else if (mAppDataIsolationEnabled) {
154 std::string obbSource(StringPrintf("%s/obb", androidSource.c_str()));
155 std::string obbInstallerTarget(StringPrintf("/mnt/installer/%d/%s/%d/Android/obb",
156 userId, label.c_str(), userId));
157
158 status = doFuseBindMount(obbSource, obbInstallerTarget, pathsToUnmount);
159 if (status != OK) {
160 return status;
161 }
162 }
163
164 // /mnt/androidwriteable is similar to /mnt/installer, but it's for
165 // MOUNT_EXTERNAL_ANDROID_WRITABLE apps and it can also access DATA (Android/data) dirs.
166 if (mAppDataIsolationEnabled) {
167 std::string obbSource = mUseSdcardFs ?
168 StringPrintf("/mnt/runtime/write/%s/%d/Android/obb", label.c_str(), userId)
169 : StringPrintf("%s/obb", androidSource.c_str());
170
171 std::string obbAndroidWritableTarget(
172 StringPrintf("/mnt/androidwritable/%d/%s/%d/Android/obb",
173 userId, label.c_str(), userId));
174
175 status = doFuseBindMount(obbSource, obbAndroidWritableTarget, pathsToUnmount);
176 if (status != OK) {
177 return status;
178 }
179
180 std::string dataSource = mUseSdcardFs ?
181 StringPrintf("/mnt/runtime/write/%s/%d/Android/data", label.c_str(), userId)
182 : StringPrintf("%s/data", androidSource.c_str());
183 std::string dataTarget(StringPrintf("/mnt/androidwritable/%d/%s/%d/Android/data",
184 userId, label.c_str(), userId));
185
186 status = doFuseBindMount(dataSource, dataTarget, pathsToUnmount);
Martijn Coenen449a7d82020-03-16 14:37:33 +0100187 if (status != OK) {
188 return status;
189 }
Martijn Coenen3a2dbfe2020-01-11 19:38:37 +0100190 }
Martijn Coenen449a7d82020-03-16 14:37:33 +0100191 unmount_guard.Disable();
Martijn Coenen57002612019-11-28 11:56:13 +0100192 return OK;
193}
194
Martijn Coenen86f21a22020-01-06 09:48:14 +0100195status_t EmulatedVolume::unmountFuseBindMounts() {
196 std::string label = getLabel();
197 int userId = getMountUserId();
198
Ricky Waief639212020-04-07 13:43:20 +0100199 if (mUseSdcardFs || mAppDataIsolationEnabled) {
Martijn Coenen3a2dbfe2020-01-11 19:38:37 +0100200 std::string installerTarget(
201 StringPrintf("/mnt/installer/%d/%s/%d/Android/obb", userId, label.c_str(), userId));
202 LOG(INFO) << "Unmounting " << installerTarget;
203 auto status = UnmountTree(installerTarget);
204 if (status != OK) {
205 LOG(ERROR) << "Failed to unmount " << installerTarget;
206 // Intentional continue to try to unmount the other bind mount
207 }
208 }
Ricky Waief639212020-04-07 13:43:20 +0100209 if (mAppDataIsolationEnabled) {
210 std::string obbTarget( StringPrintf("/mnt/androidwritable/%d/%s/%d/Android/obb",
211 userId, label.c_str(), userId));
212 LOG(INFO) << "Unmounting " << obbTarget;
213 auto status = UnmountTree(obbTarget);
214 if (status != OK) {
215 LOG(ERROR) << "Failed to unmount " << obbTarget;
216 // Intentional continue to try to unmount the other bind mount
217 }
218 std::string dataTarget(StringPrintf("/mnt/androidwritable/%d/%s/%d/Android/data",
219 userId, label.c_str(), userId));
220 LOG(INFO) << "Unmounting " << dataTarget;
221 status = UnmountTree(dataTarget);
222 if (status != OK) {
223 LOG(ERROR) << "Failed to unmount " << dataTarget;
224 // Intentional continue to try to unmount the other bind mount
225 }
226 }
227
Ricky Wai07e64a42020-02-11 14:31:24 +0000228 // When app data isolation is enabled, kill all apps that obb/ is mounted, otherwise we should
229 // umount the whole Android/ dir.
230 if (mAppDataIsolationEnabled) {
231 std::string appObbDir(StringPrintf("%s/%d/Android/obb", getPath().c_str(), userId));
232 KillProcessesWithMountPrefix(appObbDir);
233 } else {
Martijn Coenen449a7d82020-03-16 14:37:33 +0100234 std::string androidDataTarget(
235 StringPrintf("/mnt/user/%d/%s/%d/Android/data", userId, label.c_str(), userId));
Martijn Coenen3a2dbfe2020-01-11 19:38:37 +0100236
Martijn Coenen449a7d82020-03-16 14:37:33 +0100237 LOG(INFO) << "Unmounting " << androidDataTarget;
238 auto status = UnmountTree(androidDataTarget);
Ricky Wai07e64a42020-02-11 14:31:24 +0000239 if (status != OK) {
240 return status;
241 }
Martijn Coenen449a7d82020-03-16 14:37:33 +0100242 LOG(INFO) << "Unmounted " << androidDataTarget;
243
244 std::string androidObbTarget(
245 StringPrintf("/mnt/user/%d/%s/%d/Android/obb", userId, label.c_str(), userId));
246
247 LOG(INFO) << "Unmounting " << androidObbTarget;
248 status = UnmountTree(androidObbTarget);
249 if (status != OK) {
250 return status;
251 }
252 LOG(INFO) << "Unmounted " << androidObbTarget;
Martijn Coenen57002612019-11-28 11:56:13 +0100253 }
Martijn Coenen57002612019-11-28 11:56:13 +0100254 return OK;
255}
256
Martijn Coenen449a7d82020-03-16 14:37:33 +0100257status_t EmulatedVolume::unmountSdcardFs() {
258 if (!mUseSdcardFs || getMountUserId() != 0) {
259 // For sdcardfs, only unmount for user 0, since user 0 will always be running
260 // and the paths don't change for different users.
261 return OK;
262 }
263
264 ForceUnmount(mSdcardFsDefault);
265 ForceUnmount(mSdcardFsRead);
266 ForceUnmount(mSdcardFsWrite);
267 ForceUnmount(mSdcardFsFull);
268
269 rmdir(mSdcardFsDefault.c_str());
270 rmdir(mSdcardFsRead.c_str());
271 rmdir(mSdcardFsWrite.c_str());
272 rmdir(mSdcardFsFull.c_str());
273
274 mSdcardFsDefault.clear();
275 mSdcardFsRead.clear();
276 mSdcardFsWrite.clear();
277 mSdcardFsFull.clear();
278
279 return OK;
280}
281
Martijn Coenen6f5802e2019-11-28 11:53:53 +0100282status_t EmulatedVolume::doMount() {
283 std::string label = getLabel();
284 bool isVisible = getMountFlags() & MountFlags::kVisible;
Jeff Sharkey81f55c62015-07-07 14:37:03 -0700285
Martijn Coenenadcc8452019-12-09 14:18:01 +0100286 mSdcardFsDefault = StringPrintf("/mnt/runtime/default/%s", label.c_str());
287 mSdcardFsRead = StringPrintf("/mnt/runtime/read/%s", label.c_str());
288 mSdcardFsWrite = StringPrintf("/mnt/runtime/write/%s", label.c_str());
289 mSdcardFsFull = StringPrintf("/mnt/runtime/full/%s", label.c_str());
Jeff Sharkey66270a22015-06-24 11:49:24 -0700290
291 setInternalPath(mRawPath);
Jeff Sharkey81f55c62015-07-07 14:37:03 -0700292 setPath(StringPrintf("/storage/%s", label.c_str()));
Jeff Sharkey66270a22015-06-24 11:49:24 -0700293
Martijn Coenenadcc8452019-12-09 14:18:01 +0100294 if (fs_prepare_dir(mSdcardFsDefault.c_str(), 0700, AID_ROOT, AID_ROOT) ||
295 fs_prepare_dir(mSdcardFsRead.c_str(), 0700, AID_ROOT, AID_ROOT) ||
296 fs_prepare_dir(mSdcardFsWrite.c_str(), 0700, AID_ROOT, AID_ROOT) ||
297 fs_prepare_dir(mSdcardFsFull.c_str(), 0700, AID_ROOT, AID_ROOT)) {
Jeff Sharkey66270a22015-06-24 11:49:24 -0700298 PLOG(ERROR) << getId() << " failed to create mount points";
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800299 return -errno;
300 }
301
Martijn Coenenadcc8452019-12-09 14:18:01 +0100302 dev_t before = GetDevice(mSdcardFsFull);
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700303
Abhijeet Kaur01fa0e02019-12-13 10:26:32 +0000304 bool isFuse = base::GetBoolProperty(kPropFuse, false);
Zim3623a212019-07-19 16:46:53 +0100305
Martijn Coenen6f5802e2019-11-28 11:53:53 +0100306 // Mount sdcardfs regardless of FUSE, since we need it to bind-mount on top of the
307 // FUSE volume for various reasons.
Martijn Coenen86f21a22020-01-06 09:48:14 +0100308 if (mUseSdcardFs && getMountUserId() == 0) {
Martijn Coenen6f5802e2019-11-28 11:53:53 +0100309 LOG(INFO) << "Executing sdcardfs";
310 int sdcardFsPid;
311 if (!(sdcardFsPid = fork())) {
312 // clang-format off
313 if (execl(kSdcardFsPath, kSdcardFsPath,
314 "-u", "1023", // AID_MEDIA_RW
315 "-g", "1023", // AID_MEDIA_RW
316 "-m",
317 "-w",
318 "-G",
319 "-i",
320 "-o",
321 mRawPath.c_str(),
322 label.c_str(),
323 NULL)) {
324 // clang-format on
325 PLOG(ERROR) << "Failed to exec";
326 }
327
328 LOG(ERROR) << "sdcardfs exiting";
329 _exit(1);
330 }
331
332 if (sdcardFsPid == -1) {
333 PLOG(ERROR) << getId() << " failed to fork";
334 return -errno;
335 }
336
337 nsecs_t start = systemTime(SYSTEM_TIME_BOOTTIME);
338 while (before == GetDevice(mSdcardFsFull)) {
339 LOG(DEBUG) << "Waiting for sdcardfs to spin up...";
340 usleep(50000); // 50ms
341
342 nsecs_t now = systemTime(SYSTEM_TIME_BOOTTIME);
343 if (nanoseconds_to_milliseconds(now - start) > 5000) {
344 LOG(WARNING) << "Timed out while waiting for sdcardfs to spin up";
345 return -ETIMEDOUT;
346 }
347 }
348 /* sdcardfs will have exited already. The filesystem will still be running */
349 TEMP_FAILURE_RETRY(waitpid(sdcardFsPid, nullptr, 0));
350 sdcardFsPid = 0;
351 }
Martijn Coenen449a7d82020-03-16 14:37:33 +0100352
Martijn Coenen6f5802e2019-11-28 11:53:53 +0100353 if (isFuse && isVisible) {
Martijn Coenen449a7d82020-03-16 14:37:33 +0100354 // Make sure we unmount sdcardfs if we bail out with an error below
355 auto sdcardfs_unmounter = [&]() {
356 LOG(INFO) << "sdcardfs_unmounter scope_guard running";
357 unmountSdcardFs();
358 };
359 auto sdcardfs_guard = android::base::make_scope_guard(sdcardfs_unmounter);
360
Zim3623a212019-07-19 16:46:53 +0100361 LOG(INFO) << "Mounting emulated fuse volume";
Nandana Dutta914cc72019-08-29 15:22:42 +0100362 android::base::unique_fd fd;
Zim981222f2019-09-09 10:24:44 +0100363 int user_id = getMountUserId();
Martijn Coenen62a4b272020-01-31 15:23:09 +0100364 auto volumeRoot = getRootPath();
Zim981222f2019-09-09 10:24:44 +0100365
Martijn Coenen62a4b272020-01-31 15:23:09 +0100366 // Make sure Android/ dirs exist for bind mounting
367 status_t res = PrepareAndroidDirs(volumeRoot);
368 if (res != OK) {
369 LOG(ERROR) << "Failed to prepare Android/ directories";
370 return res;
371 }
372
373 res = MountUserFuse(user_id, getInternalPath(), label, &fd);
374 if (res != 0) {
Zim3623a212019-07-19 16:46:53 +0100375 PLOG(ERROR) << "Failed to mount emulated fuse volume";
Martijn Coenen62a4b272020-01-31 15:23:09 +0100376 return res;
Zim3623a212019-07-19 16:46:53 +0100377 }
Zim5048b4b2019-11-19 09:16:03 +0000378
Martijn Coenen6f5802e2019-11-28 11:53:53 +0100379 mFuseMounted = true;
Martijn Coenen449a7d82020-03-16 14:37:33 +0100380 auto fuse_unmounter = [&]() {
381 LOG(INFO) << "fuse_unmounter scope_guard running";
382 fd.reset();
383 if (UnmountUserFuse(user_id, getInternalPath(), label) != OK) {
384 PLOG(INFO) << "UnmountUserFuse failed on emulated fuse volume";
385 }
386 mFuseMounted = false;
387 };
388 auto fuse_guard = android::base::make_scope_guard(fuse_unmounter);
389
Zim5048b4b2019-11-19 09:16:03 +0000390 auto callback = getMountCallback();
391 if (callback) {
392 bool is_ready = false;
393 callback->onVolumeChecking(std::move(fd), getPath(), getInternalPath(), &is_ready);
394 if (!is_ready) {
395 return -EIO;
396 }
397 }
Martijn Coenen57002612019-11-28 11:56:13 +0100398
399 // Only do the bind-mounts when we know for sure the FUSE daemon can resolve the path.
Martijn Coenen62a4b272020-01-31 15:23:09 +0100400 res = mountFuseBindMounts();
Zimdf073f52020-01-15 15:00:07 +0000401 if (res != OK) {
Martijn Coenen449a7d82020-03-16 14:37:33 +0100402 return res;
Zimdf073f52020-01-15 15:00:07 +0000403 }
Martijn Coenen449a7d82020-03-16 14:37:33 +0100404
Nikita Ioffedcee5c12020-06-12 12:59:45 +0100405 ConfigureReadAheadForFuse(GetFuseMountPathForUser(user_id, label), 256u);
406
Martijn Coenen449a7d82020-03-16 14:37:33 +0100407 // All mounts where successful, disable scope guards
408 sdcardfs_guard.Disable();
409 fuse_guard.Disable();
Zim3623a212019-07-19 16:46:53 +0100410 }
411
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800412 return OK;
413}
414
415status_t EmulatedVolume::doUnmount() {
Martijn Coenen8f1e7f22019-11-29 15:38:55 +0100416 int userId = getMountUserId();
417
418 // Kill all processes using the filesystem before we unmount it. If we
419 // unmount the filesystem first, most file system operations will return
Narayan Kamathea243a32016-01-21 12:26:05 +0000420 // ENOTCONN until the unmount completes. This is an exotic and unusual
421 // error code and might cause broken behaviour in applications.
Martijn Coenen8f1e7f22019-11-29 15:38:55 +0100422 if (mFuseMounted) {
423 // For FUSE specifically, we have an emulated volume per user, so only kill
424 // processes using files from this particular user.
425 std::string user_path(StringPrintf("%s/%d", getPath().c_str(), getMountUserId()));
426 LOG(INFO) << "Killing all processes referencing " << user_path;
427 KillProcessesUsingPath(user_path);
428 } else {
429 KillProcessesUsingPath(getPath());
430 }
Zim3623a212019-07-19 16:46:53 +0100431
Martijn Coenen6f5802e2019-11-28 11:53:53 +0100432 if (mFuseMounted) {
433 std::string label = getLabel();
Ricky Wai07e64a42020-02-11 14:31:24 +0000434
Martijn Coenen57002612019-11-28 11:56:13 +0100435 // Ignoring unmount return status because we do want to try to unmount
436 // the rest cleanly.
Martijn Coenen449a7d82020-03-16 14:37:33 +0100437 unmountFuseBindMounts();
438
Martijn Coenen57002612019-11-28 11:56:13 +0100439 if (UnmountUserFuse(userId, getInternalPath(), label) != OK) {
Zima438b242019-09-25 14:37:38 +0100440 PLOG(INFO) << "UnmountUserFuse failed on emulated fuse volume";
441 return -errno;
Zim3623a212019-07-19 16:46:53 +0100442 }
443
Martijn Coenen6f5802e2019-11-28 11:53:53 +0100444 mFuseMounted = false;
445 }
Zim3623a212019-07-19 16:46:53 +0100446
Martijn Coenen449a7d82020-03-16 14:37:33 +0100447 return unmountSdcardFs();
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800448}
449
Martijn Coenen62a4b272020-01-31 15:23:09 +0100450std::string EmulatedVolume::getRootPath() const {
451 int user_id = getMountUserId();
452 std::string volumeRoot = StringPrintf("%s/%d", getInternalPath().c_str(), user_id);
453
454 return volumeRoot;
455}
456
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800457} // namespace vold
458} // namespace android