From bf00e7042647d075ad5379379f4009d4757d2a74 Mon Sep 17 00:00:00 2001 From: Zim Date: Mon, 29 Mar 2021 14:20:17 +0100 Subject: [PATCH] Fix secondary volume mounts on user switch If a public volume is mounted when the foreground user is switched, the old user is stopped but the public volume isn't unmounted so the /mnt/user// path still exists but is inaccessible, even after the foreground user is switched back the old user. Now, we remount the public volumes in such cases to fixup the mounts Test: Manual Bug: 183686263 Bug: 171460866 Change-Id: Ic1226229d377d7106bd78798a03adfc9b7f2efb6 --- .../android/server/StorageManagerService.java | 24 +++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/services/core/java/com/android/server/StorageManagerService.java b/services/core/java/com/android/server/StorageManagerService.java index 09f4c221f3fe1..f5918025db0be 100644 --- a/services/core/java/com/android/server/StorageManagerService.java +++ b/services/core/java/com/android/server/StorageManagerService.java @@ -249,6 +249,8 @@ class StorageManagerService extends IStorageManager.Stub @Override public void onUserSwitching(@Nullable TargetUser from, @NonNull TargetUser to) { mStorageManagerService.mCurrentUserId = to.getUserIdentifier(); + // To reset public volume mounts + mStorageManagerService.onUserSwitching(mStorageManagerService.mCurrentUserId); } @Override @@ -1218,6 +1220,28 @@ class StorageManagerService extends IStorageManager.Stub } } + private void onUserSwitching(int userId) { + boolean reset = false; + List volumesToRemount = new ArrayList<>(); + synchronized (mLock) { + for (int i = 0; i < mVolumes.size(); i++) { + final VolumeInfo vol = mVolumes.valueAt(i); + if (!vol.isPrimary() && vol.isMountedWritable() && vol.isVisible() + && vol.getMountUserId() != mCurrentUserId) { + // If there's a visible secondary volume mounted, + // we need to update the currentUserId and remount + vol.mountUserId = mCurrentUserId; + volumesToRemount.add(vol); + } + } + } + + for (VolumeInfo vol : volumesToRemount) { + mHandler.obtainMessage(H_VOLUME_UNMOUNT, vol).sendToTarget(); + mHandler.obtainMessage(H_VOLUME_MOUNT, vol).sendToTarget(); + } + } + private boolean supportsBlockCheckpoint() throws RemoteException { enforcePermission(android.Manifest.permission.MOUNT_FORMAT_FILESYSTEMS); return mVold.supportsBlockCheckpoint();