From bb6b0f57481eeeb5ea217a761092e41a9d110879 Mon Sep 17 00:00:00 2001 From: Arnab Sen Date: Fri, 31 Dec 2021 10:09:06 +0530 Subject: SM: Change volume mountUserId for new user When a volume is ejected from a user but not removed from the device and mounted again by calling StorageManager#mount, the volume is mounted for the user from which it was last ejected. This causes the path of the mounted volume to be inaccessible to the current user. Whenever StorageManager#mount is called, check if the VolumeInfo#mountUserId is same as the current userId maintained by StorageManagerService, if it is not, change the mountUserId for the Volume to the current userId known to StorageManagerService if it is not the primary volume and the volume is visible. This change also fixes a bug where the volumes are unmounted for the wrong user which causes StorageSessionController go out of sync which would cause errors during mounting the volumes again as it maintains sessions per user. The bug also caused volume event broadcasts to be sent to wrong user handles. To solve the issue add an extra paramter in IVoldListener#onVolumeStateChanged for userId which will update the copy which is used for informing StorageSessionController and also for sending broadcasts. Test: Perform the following steps: 1. Format SD Card as portable storage in system user. 2. Eject the SD Card from Storage Settings. 3. Switch to a non-system user 4. Mount the SD Card again. 5. Check that the Storage Summary is shown correctly instead of 0B out of 0B. Also, checked from the logs the volume state broadcasts are sent to the correct user handles. Change-Id: If03c36b021860038c088ca9e02f54fbdbf50c65b --- .../com/android/server/StorageManagerService.java | 20 ++++++++++++++++++-- 1 file changed, 18 insertions(+), 2 deletions(-) diff --git a/services/core/java/com/android/server/StorageManagerService.java b/services/core/java/com/android/server/StorageManagerService.java index a3137a66144a..af0e61e9f6d4 100644 --- a/services/core/java/com/android/server/StorageManagerService.java +++ b/services/core/java/com/android/server/StorageManagerService.java @@ -1357,6 +1357,21 @@ class StorageManagerService extends IStorageManager.Stub } } + /** + * This method checks if the volume is public and the volume is visible and the volume it is + * trying to mount doesn't have the same mount user id as the current user being maintained by + * StorageManagerService and change the mount Id. The checks are same as + * {@link StorageManagerService#maybeRemountVolumes(int)} + * @param VolumeInfo object to consider for changing the mountId + */ + private void updateVolumeMountIdIfRequired(VolumeInfo vol) { + synchronized (mLock) { + if (!vol.isPrimary() && vol.isVisible() && vol.getMountUserId() != mCurrentUserId) { + vol.mountUserId = mCurrentUserId; + } + } + } + private boolean supportsBlockCheckpoint() throws RemoteException { enforcePermission(android.Manifest.permission.MOUNT_FORMAT_FILESYSTEMS); return mVold.supportsBlockCheckpoint(); @@ -1468,13 +1483,14 @@ class StorageManagerService extends IStorageManager.Stub } @Override - public void onVolumeStateChanged(String volId, final int newState) { + public void onVolumeStateChanged(String volId, final int newState, final int userId) { synchronized (mLock) { final VolumeInfo vol = mVolumes.get(volId); if (vol != null) { final int oldState = vol.state; vol.state = newState; final VolumeInfo vInfo = new VolumeInfo(vol); + vInfo.mountUserId = userId; final SomeArgs args = SomeArgs.obtain(); args.arg1 = vInfo; args.argi1 = oldState; @@ -2322,7 +2338,7 @@ class StorageManagerService extends IStorageManager.Stub if (isMountDisallowed(vol)) { throw new SecurityException("Mounting " + volId + " restricted by policy"); } - + updateVolumeMountIdIfRequired(vol); mount(vol); } -- cgit v1.2.3-59-g8ed1b