The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2007 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 | |
| 17 | package com.android.server; |
| 18 | |
Martijn Coenen | 496ac00 | 2020-01-08 14:55:53 +0100 | [diff] [blame] | 19 | import static android.Manifest.permission.ACCESS_MTP; |
Sudheer Shanka | 3a0df3b | 2018-12-12 12:43:43 -0800 | [diff] [blame] | 20 | import static android.Manifest.permission.INSTALL_PACKAGES; |
Jeff Sharkey | 6fd6994 | 2019-03-26 17:53:35 -0600 | [diff] [blame] | 21 | import static android.Manifest.permission.READ_EXTERNAL_STORAGE; |
| 22 | import static android.Manifest.permission.WRITE_EXTERNAL_STORAGE; |
Sudheer Shanka | 3a0df3b | 2018-12-12 12:43:43 -0800 | [diff] [blame] | 23 | import static android.Manifest.permission.WRITE_MEDIA_STORAGE; |
Zim | 64726cb | 2019-11-27 13:06:34 +0000 | [diff] [blame] | 24 | import static android.app.ActivityManager.PROCESS_STATE_NONEXISTENT; |
Sudheer Shanka | 3a0df3b | 2018-12-12 12:43:43 -0800 | [diff] [blame] | 25 | import static android.app.AppOpsManager.MODE_ALLOWED; |
Jeff Sharkey | 11697f5 | 2018-12-13 10:14:42 -0700 | [diff] [blame] | 26 | import static android.app.AppOpsManager.OP_LEGACY_STORAGE; |
Jeff Sharkey | 6fd6994 | 2019-03-26 17:53:35 -0600 | [diff] [blame] | 27 | import static android.app.AppOpsManager.OP_READ_EXTERNAL_STORAGE; |
Sudheer Shanka | 3a0df3b | 2018-12-12 12:43:43 -0800 | [diff] [blame] | 28 | import static android.app.AppOpsManager.OP_REQUEST_INSTALL_PACKAGES; |
Jeff Sharkey | 6fd6994 | 2019-03-26 17:53:35 -0600 | [diff] [blame] | 29 | import static android.app.AppOpsManager.OP_WRITE_EXTERNAL_STORAGE; |
Sudheer Shanka | 3a0df3b | 2018-12-12 12:43:43 -0800 | [diff] [blame] | 30 | import static android.content.pm.PackageManager.PERMISSION_GRANTED; |
Jeff Sharkey | 3946632 | 2018-12-05 19:19:52 -0700 | [diff] [blame] | 31 | import static android.os.ParcelFileDescriptor.MODE_READ_WRITE; |
Jeff Sharkey | 0095a82 | 2018-02-15 13:06:53 -0700 | [diff] [blame] | 32 | import static android.os.storage.OnObbStateChangeListener.ERROR_ALREADY_MOUNTED; |
| 33 | import static android.os.storage.OnObbStateChangeListener.ERROR_COULD_NOT_MOUNT; |
| 34 | import static android.os.storage.OnObbStateChangeListener.ERROR_COULD_NOT_UNMOUNT; |
| 35 | import static android.os.storage.OnObbStateChangeListener.ERROR_INTERNAL; |
| 36 | import static android.os.storage.OnObbStateChangeListener.ERROR_NOT_MOUNTED; |
| 37 | import static android.os.storage.OnObbStateChangeListener.ERROR_PERMISSION_DENIED; |
| 38 | import static android.os.storage.OnObbStateChangeListener.MOUNTED; |
| 39 | import static android.os.storage.OnObbStateChangeListener.UNMOUNTED; |
Zim | 69e9c09 | 2020-01-21 13:32:32 +0000 | [diff] [blame] | 40 | import static android.os.storage.StorageManager.PROP_FUSE; |
| 41 | import static android.os.storage.StorageManager.PROP_SETTINGS_FUSE; |
Jeff Sharkey | 5790af0 | 2018-08-13 17:42:54 -0600 | [diff] [blame] | 42 | |
Jeff Sharkey | d95d3bf | 2015-04-14 21:39:44 -0700 | [diff] [blame] | 43 | import static com.android.internal.util.XmlUtils.readIntAttribute; |
Jeff Sharkey | e8a4b66 | 2015-06-27 15:43:45 -0700 | [diff] [blame] | 44 | import static com.android.internal.util.XmlUtils.readLongAttribute; |
Jeff Sharkey | d95d3bf | 2015-04-14 21:39:44 -0700 | [diff] [blame] | 45 | import static com.android.internal.util.XmlUtils.readStringAttribute; |
| 46 | import static com.android.internal.util.XmlUtils.writeIntAttribute; |
Jeff Sharkey | e8a4b66 | 2015-06-27 15:43:45 -0700 | [diff] [blame] | 47 | import static com.android.internal.util.XmlUtils.writeLongAttribute; |
Jeff Sharkey | d95d3bf | 2015-04-14 21:39:44 -0700 | [diff] [blame] | 48 | import static com.android.internal.util.XmlUtils.writeStringAttribute; |
Zim | 7924a51 | 2020-01-15 15:23:54 +0000 | [diff] [blame] | 49 | import static com.android.server.storage.StorageUserConnection.REMOTE_TIMEOUT_SECONDS; |
Jeff Sharkey | 5790af0 | 2018-08-13 17:42:54 -0600 | [diff] [blame] | 50 | |
Jeff Sharkey | d95d3bf | 2015-04-14 21:39:44 -0700 | [diff] [blame] | 51 | import static org.xmlpull.v1.XmlPullParser.END_DOCUMENT; |
| 52 | import static org.xmlpull.v1.XmlPullParser.START_TAG; |
| 53 | |
Jason parks | 8888c59 | 2011-01-20 22:46:41 -0600 | [diff] [blame] | 54 | import android.Manifest; |
Jeff Sharkey | ef10ee0 | 2015-07-05 14:17:27 -0700 | [diff] [blame] | 55 | import android.annotation.Nullable; |
Xiaohui Chen | 621b3fc | 2015-10-02 14:41:42 -0700 | [diff] [blame] | 56 | import android.app.ActivityManager; |
Martijn Coenen | 7084c6a | 2019-11-22 16:32:09 +0100 | [diff] [blame] | 57 | import android.app.ActivityManagerInternal; |
Jeff Sharkey | 2d8b4e8 | 2013-09-17 17:30:33 -0700 | [diff] [blame] | 58 | import android.app.AppOpsManager; |
Jeff Sharkey | 14cbe52 | 2015-07-08 14:06:37 -0700 | [diff] [blame] | 59 | import android.app.IActivityManager; |
Jeff Sharkey | 9765e44 | 2017-12-14 22:15:14 -0700 | [diff] [blame] | 60 | import android.app.KeyguardManager; |
Pavel Grafov | ce72ef0 | 2018-01-10 17:14:11 +0000 | [diff] [blame] | 61 | import android.app.admin.SecurityLog; |
Jeff Sharkey | 9bed070 | 2017-01-23 20:37:05 -0700 | [diff] [blame] | 62 | import android.app.usage.StorageStatsManager; |
Jeff Sharkey | bcd262d | 2015-06-10 09:41:17 -0700 | [diff] [blame] | 63 | import android.content.BroadcastReceiver; |
Jeff Sharkey | b0c363b2 | 2018-12-15 11:53:03 -0700 | [diff] [blame] | 64 | import android.content.ContentResolver; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 65 | import android.content.Context; |
| 66 | import android.content.Intent; |
Jeff Sharkey | bcd262d | 2015-06-10 09:41:17 -0700 | [diff] [blame] | 67 | import android.content.IntentFilter; |
Nandana Dutt | dc15bb4 | 2020-01-03 16:43:46 +0000 | [diff] [blame] | 68 | import android.content.pm.ApplicationInfo; |
Sudheer Shanka | 3a0df3b | 2018-12-12 12:43:43 -0800 | [diff] [blame] | 69 | import android.content.pm.IPackageManager; |
Jeff Sharkey | 275e3e4 | 2015-04-24 16:10:32 -0700 | [diff] [blame] | 70 | import android.content.pm.IPackageMoveObserver; |
| 71 | import android.content.pm.PackageManager; |
Sudheer Shanka | 0a541a5 | 2018-07-31 13:21:11 -0700 | [diff] [blame] | 72 | import android.content.pm.PackageManagerInternal; |
Jeff Sharkey | 14cbe52 | 2015-07-08 14:06:37 -0700 | [diff] [blame] | 73 | import android.content.pm.ProviderInfo; |
Jeff Sharkey | bcd262d | 2015-06-10 09:41:17 -0700 | [diff] [blame] | 74 | import android.content.pm.UserInfo; |
Elliott Hughes | f839b4f | 2014-09-26 12:30:47 -0700 | [diff] [blame] | 75 | import android.content.res.Configuration; |
Kenny Root | 02c8730 | 2010-07-01 08:10:18 -0700 | [diff] [blame] | 76 | import android.content.res.ObbInfo; |
Daniel Colascione | 766b632 | 2018-01-08 19:10:36 -0800 | [diff] [blame] | 77 | import android.database.ContentObserver; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 78 | import android.net.Uri; |
Kenny Root | 02c8730 | 2010-07-01 08:10:18 -0700 | [diff] [blame] | 79 | import android.os.Binder; |
Jeff Sharkey | 4c099d0 | 2015-05-15 13:45:00 -0700 | [diff] [blame] | 80 | import android.os.DropBoxManager; |
Kenny Root | a02b8b0 | 2010-08-05 16:14:17 -0700 | [diff] [blame] | 81 | import android.os.Environment; |
Suchi Amalapurapu | c42e29e | 2010-02-22 16:03:53 -0800 | [diff] [blame] | 82 | import android.os.Handler; |
Dianne Hackborn | efa92b2 | 2013-05-03 14:11:43 -0700 | [diff] [blame] | 83 | import android.os.HandlerThread; |
Kenny Root | a02b8b0 | 2010-08-05 16:14:17 -0700 | [diff] [blame] | 84 | import android.os.IBinder; |
Jin Qian | 12690d5 | 2017-10-13 18:17:04 -0700 | [diff] [blame] | 85 | import android.os.IStoraged; |
Jeff Sharkey | 1019de9 | 2017-09-06 13:47:03 -0600 | [diff] [blame] | 86 | import android.os.IVold; |
Jeff Sharkey | 8058fe6 | 2017-09-13 11:50:33 -0600 | [diff] [blame] | 87 | import android.os.IVoldListener; |
Zim | 95eca1d | 2019-11-15 18:03:00 +0000 | [diff] [blame] | 88 | import android.os.IVoldMountCallback; |
Jeff Sharkey | b302c54 | 2017-09-15 12:57:59 -0600 | [diff] [blame] | 89 | import android.os.IVoldTaskListener; |
Daniel Sandler | 5f27ef4 | 2010-03-16 15:42:02 -0400 | [diff] [blame] | 90 | import android.os.Looper; |
Suchi Amalapurapu | c42e29e | 2010-02-22 16:03:53 -0800 | [diff] [blame] | 91 | import android.os.Message; |
Daichi Hirono | 9e8d9e2 | 2015-11-13 14:37:00 +0900 | [diff] [blame] | 92 | import android.os.ParcelFileDescriptor; |
Jeff Sharkey | 500ce9e | 2017-02-12 02:39:24 -0700 | [diff] [blame] | 93 | import android.os.ParcelableException; |
Jeff Sharkey | b302c54 | 2017-09-15 12:57:59 -0600 | [diff] [blame] | 94 | import android.os.PersistableBundle; |
Jeff Sharkey | ce14cd0 | 2015-12-07 15:35:42 -0700 | [diff] [blame] | 95 | import android.os.PowerManager; |
Jeff Sharkey | 9527b22 | 2015-06-24 15:24:48 -0700 | [diff] [blame] | 96 | import android.os.Process; |
Jeff Sharkey | 7151a9a | 2015-04-04 15:22:37 -0700 | [diff] [blame] | 97 | import android.os.RemoteCallbackList; |
San Mehat | 4270e1e | 2010-01-29 05:32:19 -0800 | [diff] [blame] | 98 | import android.os.RemoteException; |
Suchi Amalapurapu | fd3530f | 2010-01-18 00:15:59 -0800 | [diff] [blame] | 99 | import android.os.ServiceManager; |
Paul Crowley | fc0b519 | 2018-07-02 13:58:10 -0700 | [diff] [blame] | 100 | import android.os.ServiceSpecificException; |
Jeff Sharkey | edcdaf6 | 2015-07-09 09:45:36 -0700 | [diff] [blame] | 101 | import android.os.SystemClock; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 102 | import android.os.SystemProperties; |
Dianne Hackborn | f02b60a | 2012-08-16 10:48:27 -0700 | [diff] [blame] | 103 | import android.os.UserHandle; |
Emily Bernier | 92aa5a2 | 2014-07-07 10:11:48 -0400 | [diff] [blame] | 104 | import android.os.UserManager; |
Sudheer Shanka | b161398 | 2019-05-16 16:55:50 -0700 | [diff] [blame] | 105 | import android.os.UserManagerInternal; |
Jeff Sharkey | 1b8ef7e | 2015-04-03 17:14:45 -0700 | [diff] [blame] | 106 | import android.os.storage.DiskInfo; |
Kenny Root | a02b8b0 | 2010-08-05 16:14:17 -0700 | [diff] [blame] | 107 | import android.os.storage.IObbActionListener; |
Jeff Sharkey | d5d5e92 | 2017-02-21 10:51:23 -0700 | [diff] [blame] | 108 | import android.os.storage.IStorageEventListener; |
Sudheer Shanka | 2250d56 | 2016-11-07 15:41:02 -0800 | [diff] [blame] | 109 | import android.os.storage.IStorageManager; |
Jeff Sharkey | d5d5e92 | 2017-02-21 10:51:23 -0700 | [diff] [blame] | 110 | import android.os.storage.IStorageShutdownObserver; |
Kenny Root | af9d667 | 2010-10-08 09:21:39 -0700 | [diff] [blame] | 111 | import android.os.storage.OnObbStateChangeListener; |
Paul Lawrence | 46791e7 | 2014-04-03 09:10:26 -0700 | [diff] [blame] | 112 | import android.os.storage.StorageManager; |
Jeff Sharkey | d5d5e92 | 2017-02-21 10:51:23 -0700 | [diff] [blame] | 113 | import android.os.storage.StorageManagerInternal; |
Mike Lockwood | 2f6a388 | 2011-05-09 19:08:06 -0700 | [diff] [blame] | 114 | import android.os.storage.StorageVolume; |
Jeff Sharkey | 1b8ef7e | 2015-04-03 17:14:45 -0700 | [diff] [blame] | 115 | import android.os.storage.VolumeInfo; |
Jeff Sharkey | b36586a | 2015-04-27 08:42:28 -0700 | [diff] [blame] | 116 | import android.os.storage.VolumeRecord; |
Jeff Sharkey | 5d0c55c | 2019-01-24 14:32:31 -0700 | [diff] [blame] | 117 | import android.provider.DeviceConfig; |
Martijn Coenen | 496ac00 | 2020-01-08 14:55:53 +0100 | [diff] [blame] | 118 | import android.provider.Downloads; |
Jeff Sharkey | 14cbe52 | 2015-07-08 14:06:37 -0700 | [diff] [blame] | 119 | import android.provider.MediaStore; |
Jeff Sharkey | e8a4b66 | 2015-06-27 15:43:45 -0700 | [diff] [blame] | 120 | import android.provider.Settings; |
Inseob Kim | c1246e6 | 2018-11-08 13:13:54 +0900 | [diff] [blame] | 121 | import android.sysprop.VoldProperties; |
Jason parks | f7b3cd4 | 2011-01-27 09:28:25 -0600 | [diff] [blame] | 122 | import android.text.TextUtils; |
Jeff Sharkey | 1783f14 | 2015-04-17 10:52:51 -0700 | [diff] [blame] | 123 | import android.text.format.DateUtils; |
Jeff Sharkey | 4887789 | 2015-03-18 11:27:19 -0700 | [diff] [blame] | 124 | import android.util.ArrayMap; |
Jeff Sharkey | 04b4ba1 | 2019-12-15 22:42:42 -0700 | [diff] [blame] | 125 | import android.util.ArraySet; |
Jeff Sharkey | d95d3bf | 2015-04-14 21:39:44 -0700 | [diff] [blame] | 126 | import android.util.AtomicFile; |
Jeff Sharkey | 9f2dc05 | 2018-01-07 16:47:31 -0700 | [diff] [blame] | 127 | import android.util.DataUnit; |
Abhijeet Kaur | b984771 | 2019-11-19 16:35:47 +0000 | [diff] [blame] | 128 | import android.util.FeatureFlagUtils; |
Jeff Sharkey | 1b8ef7e | 2015-04-03 17:14:45 -0700 | [diff] [blame] | 129 | import android.util.Log; |
Felipe Leme | 281389a | 2016-10-10 17:12:20 -0700 | [diff] [blame] | 130 | import android.util.Pair; |
San Mehat | a507859 | 2010-03-25 09:36:54 -0700 | [diff] [blame] | 131 | import android.util.Slog; |
Jeff Sharkey | e8a4b66 | 2015-06-27 15:43:45 -0700 | [diff] [blame] | 132 | import android.util.TimeUtils; |
Jeff Sharkey | d95d3bf | 2015-04-14 21:39:44 -0700 | [diff] [blame] | 133 | import android.util.Xml; |
Jeff Sharkey | 4887789 | 2015-03-18 11:27:19 -0700 | [diff] [blame] | 134 | |
Jeff Sharkey | 8b2c3a14 | 2012-11-12 11:45:05 -0800 | [diff] [blame] | 135 | import com.android.internal.annotations.GuardedBy; |
Sudheer Shanka | ff971bc | 2018-12-13 17:39:59 -0800 | [diff] [blame] | 136 | import com.android.internal.app.IAppOpsCallback; |
Sudheer Shanka | 3a0df3b | 2018-12-12 12:43:43 -0800 | [diff] [blame] | 137 | import com.android.internal.app.IAppOpsService; |
Daichi Hirono | 9fb0018 | 2016-11-08 14:12:17 +0900 | [diff] [blame] | 138 | import com.android.internal.os.AppFuseMount; |
Jeff Sharkey | 1019de9 | 2017-09-06 13:47:03 -0600 | [diff] [blame] | 139 | import com.android.internal.os.BackgroundThread; |
Daichi Hirono | 812c95d | 2017-02-08 16:20:20 +0900 | [diff] [blame] | 140 | import com.android.internal.os.FuseUnavailableMountException; |
Jeff Sharkey | 7151a9a | 2015-04-04 15:22:37 -0700 | [diff] [blame] | 141 | import com.android.internal.os.SomeArgs; |
Jeff Sharkey | 9527b22 | 2015-06-24 15:24:48 -0700 | [diff] [blame] | 142 | import com.android.internal.os.Zygote; |
Jeff Sharkey | 4887789 | 2015-03-18 11:27:19 -0700 | [diff] [blame] | 143 | import com.android.internal.util.ArrayUtils; |
Jeff Sharkey | 11697f5 | 2018-12-13 10:14:42 -0700 | [diff] [blame] | 144 | import com.android.internal.util.CollectionUtils; |
Jeff Sharkey | fe9a53b | 2017-03-31 14:08:23 -0600 | [diff] [blame] | 145 | import com.android.internal.util.DumpUtils; |
Jeff Sharkey | d95d3bf | 2015-04-14 21:39:44 -0700 | [diff] [blame] | 146 | import com.android.internal.util.FastXmlSerializer; |
Jeff Sharkey | f9fc6d6 | 2015-11-08 16:46:05 -0800 | [diff] [blame] | 147 | import com.android.internal.util.HexDump; |
Jeff Sharkey | 5aca2b8 | 2013-10-16 16:21:54 -0700 | [diff] [blame] | 148 | import com.android.internal.util.IndentingPrintWriter; |
Jeff Sharkey | 4fbbda4 | 2012-09-24 18:34:07 -0700 | [diff] [blame] | 149 | import com.android.internal.util.Preconditions; |
Jeff Sharkey | b9fe537 | 2015-12-03 15:23:08 -0700 | [diff] [blame] | 150 | import com.android.internal.widget.LockPatternUtils; |
Ricky Wai | 4482ab5 | 2019-12-10 19:08:18 +0000 | [diff] [blame] | 151 | import com.android.server.pm.Installer; |
Daichi Hirono | 9fb0018 | 2016-11-08 14:12:17 +0900 | [diff] [blame] | 152 | import com.android.server.storage.AppFuseBridge; |
Zim | 42f1e9f | 2019-08-15 17:35:00 +0100 | [diff] [blame] | 153 | import com.android.server.storage.StorageSessionController; |
Zim | 17be6f9 | 2019-09-25 14:37:55 +0100 | [diff] [blame] | 154 | import com.android.server.storage.StorageSessionController.ExternalStorageServiceException; |
Wale Ogunwale | 9e4f3e0 | 2018-05-17 09:35:39 -0700 | [diff] [blame] | 155 | import com.android.server.wm.ActivityTaskManagerInternal; |
| 156 | import com.android.server.wm.ActivityTaskManagerInternal.ScreenObserver; |
Jeff Sharkey | 5217cac | 2015-12-20 15:34:01 -0700 | [diff] [blame] | 157 | |
Jeff Sharkey | 5790af0 | 2018-08-13 17:42:54 -0600 | [diff] [blame] | 158 | import libcore.io.IoUtils; |
| 159 | import libcore.util.EmptyArray; |
| 160 | |
Jeff Sharkey | d95d3bf | 2015-04-14 21:39:44 -0700 | [diff] [blame] | 161 | import org.xmlpull.v1.XmlPullParser; |
| 162 | import org.xmlpull.v1.XmlPullParserException; |
| 163 | import org.xmlpull.v1.XmlSerializer; |
| 164 | |
Jeff Sharkey | b049e21 | 2012-09-07 23:16:01 -0700 | [diff] [blame] | 165 | import java.io.File; |
Kenny Root | 38cf886 | 2010-09-26 14:18:51 -0700 | [diff] [blame] | 166 | import java.io.FileDescriptor; |
Jeff Sharkey | d95d3bf | 2015-04-14 21:39:44 -0700 | [diff] [blame] | 167 | import java.io.FileInputStream; |
| 168 | import java.io.FileNotFoundException; |
Christopher Tate | 7265abe | 2014-11-21 13:54:45 -0800 | [diff] [blame] | 169 | import java.io.FileOutputStream; |
Kenny Root | 05105f7 | 2010-09-22 17:29:43 -0700 | [diff] [blame] | 170 | import java.io.IOException; |
Kenny Root | 38cf886 | 2010-09-26 14:18:51 -0700 | [diff] [blame] | 171 | import java.io.PrintWriter; |
Kenny Root | 3b1abba | 2010-10-13 15:00:07 -0700 | [diff] [blame] | 172 | import java.math.BigInteger; |
Paul Lawrence | 8e39736 | 2014-01-27 15:22:30 -0800 | [diff] [blame] | 173 | import java.nio.charset.StandardCharsets; |
Jeff Sharkey | 0095a82 | 2018-02-15 13:06:53 -0700 | [diff] [blame] | 174 | import java.security.GeneralSecurityException; |
Kenny Root | 3b1abba | 2010-10-13 15:00:07 -0700 | [diff] [blame] | 175 | import java.security.spec.KeySpec; |
San Mehat | 22dd86e | 2010-01-12 12:21:18 -0800 | [diff] [blame] | 176 | import java.util.ArrayList; |
Jeff Sharkey | f9fc6d6 | 2015-11-08 16:46:05 -0800 | [diff] [blame] | 177 | import java.util.Arrays; |
Kenny Root | a02b8b0 | 2010-08-05 16:14:17 -0700 | [diff] [blame] | 178 | import java.util.HashMap; |
Kenny Root | 38cf886 | 2010-09-26 14:18:51 -0700 | [diff] [blame] | 179 | import java.util.Iterator; |
Kenny Root | a02b8b0 | 2010-08-05 16:14:17 -0700 | [diff] [blame] | 180 | import java.util.LinkedList; |
| 181 | import java.util.List; |
Elliott Hughes | f839b4f | 2014-09-26 12:30:47 -0700 | [diff] [blame] | 182 | import java.util.Locale; |
Kenny Root | a02b8b0 | 2010-08-05 16:14:17 -0700 | [diff] [blame] | 183 | import java.util.Map; |
Kenny Root | 38cf886 | 2010-09-26 14:18:51 -0700 | [diff] [blame] | 184 | import java.util.Map.Entry; |
Jeff Sharkey | 7151a9a | 2015-04-04 15:22:37 -0700 | [diff] [blame] | 185 | import java.util.Objects; |
Svet Ganov | 6ee871e | 2015-07-10 14:29:33 -0700 | [diff] [blame] | 186 | import java.util.concurrent.CopyOnWriteArrayList; |
Kenny Root | 51a573c | 2012-05-17 13:30:28 -0700 | [diff] [blame] | 187 | import java.util.concurrent.CountDownLatch; |
| 188 | import java.util.concurrent.TimeUnit; |
Jeff Sharkey | edcdaf6 | 2015-07-09 09:45:36 -0700 | [diff] [blame] | 189 | import java.util.concurrent.TimeoutException; |
Martijn Coenen | 9fd2b64 | 2019-12-24 13:04:36 +0100 | [diff] [blame] | 190 | import java.util.regex.Matcher; |
| 191 | import java.util.regex.Pattern; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 192 | |
Kenny Root | 3b1abba | 2010-10-13 15:00:07 -0700 | [diff] [blame] | 193 | import javax.crypto.SecretKey; |
| 194 | import javax.crypto.SecretKeyFactory; |
| 195 | import javax.crypto.spec.PBEKeySpec; |
| 196 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 197 | /** |
Jeff Sharkey | 4887789 | 2015-03-18 11:27:19 -0700 | [diff] [blame] | 198 | * Service responsible for various storage media. Connects to {@code vold} to |
| 199 | * watch for and manage dynamically added storage, such as SD cards and USB mass |
| 200 | * storage. Also decides how storage should be presented to users on the device. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 201 | */ |
Jeff Sharkey | 9765e44 | 2017-12-14 22:15:14 -0700 | [diff] [blame] | 202 | class StorageManagerService extends IStorageManager.Stub |
| 203 | implements Watchdog.Monitor, ScreenObserver { |
Jason parks | 5af0b91 | 2010-11-29 09:05:25 -0600 | [diff] [blame] | 204 | |
Christopher Tate | d417d62 | 2013-08-19 16:14:25 -0700 | [diff] [blame] | 205 | // Static direct instance pointer for the tightly-coupled idle service to use |
Sudheer Shanka | 2250d56 | 2016-11-07 15:41:02 -0800 | [diff] [blame] | 206 | static StorageManagerService sSelf = null; |
Christopher Tate | d417d62 | 2013-08-19 16:14:25 -0700 | [diff] [blame] | 207 | |
Daniel Colascione | 766b632 | 2018-01-08 19:10:36 -0800 | [diff] [blame] | 208 | /* Read during boot to decide whether to enable zram when available */ |
| 209 | private static final String ZRAM_ENABLED_PROPERTY = |
Sudheer Shanka | 0a541a5 | 2018-07-31 13:21:11 -0700 | [diff] [blame] | 210 | "persist.sys.zram_enabled"; |
| 211 | |
Jeff Sharkey | 10ec9d8 | 2018-11-28 14:52:45 -0700 | [diff] [blame] | 212 | private static final boolean ENABLE_ISOLATED_STORAGE = StorageManager.hasIsolatedStorage(); |
Sudheer Shanka | 0a541a5 | 2018-07-31 13:21:11 -0700 | [diff] [blame] | 213 | |
Matt Pape | 19ea8a6 | 2019-03-18 10:38:25 -0700 | [diff] [blame] | 214 | /** |
| 215 | * If {@code 1}, enables the isolated storage feature. If {@code -1}, |
| 216 | * disables the isolated storage feature. If {@code 0}, uses the default |
| 217 | * value from the build system. |
| 218 | */ |
| 219 | private static final String ISOLATED_STORAGE_ENABLED = "isolated_storage_enabled"; |
| 220 | |
shafik | b2992b6 | 2019-10-01 15:31:02 +0100 | [diff] [blame] | 221 | /** |
| 222 | * If {@code 1}, enables FuseDaemon to intercept file system ops. If {@code -1}, |
| 223 | * disables FuseDaemon. If {@code 0}, uses the default value from the build system. |
| 224 | */ |
| 225 | private static final String FUSE_ENABLED = "fuse_enabled"; |
| 226 | |
Jeff Sharkey | 56e6293 | 2015-03-21 20:41:00 -0700 | [diff] [blame] | 227 | public static class Lifecycle extends SystemService { |
Sudheer Shanka | 2250d56 | 2016-11-07 15:41:02 -0800 | [diff] [blame] | 228 | private StorageManagerService mStorageManagerService; |
Jeff Sharkey | 56e6293 | 2015-03-21 20:41:00 -0700 | [diff] [blame] | 229 | |
| 230 | public Lifecycle(Context context) { |
| 231 | super(context); |
| 232 | } |
| 233 | |
| 234 | @Override |
| 235 | public void onStart() { |
Sudheer Shanka | 2250d56 | 2016-11-07 15:41:02 -0800 | [diff] [blame] | 236 | mStorageManagerService = new StorageManagerService(getContext()); |
| 237 | publishBinderService("mount", mStorageManagerService); |
| 238 | mStorageManagerService.start(); |
Jeff Sharkey | 56e6293 | 2015-03-21 20:41:00 -0700 | [diff] [blame] | 239 | } |
| 240 | |
| 241 | @Override |
| 242 | public void onBootPhase(int phase) { |
Jeff Sharkey | 11697f5 | 2018-12-13 10:14:42 -0700 | [diff] [blame] | 243 | if (phase == SystemService.PHASE_SYSTEM_SERVICES_READY) { |
| 244 | mStorageManagerService.servicesReady(); |
| 245 | } else if (phase == SystemService.PHASE_ACTIVITY_MANAGER_READY) { |
Sudheer Shanka | 2250d56 | 2016-11-07 15:41:02 -0800 | [diff] [blame] | 246 | mStorageManagerService.systemReady(); |
Yasuhiro Matsuda | 87a38b5 | 2015-07-24 22:10:16 +0900 | [diff] [blame] | 247 | } else if (phase == SystemService.PHASE_BOOT_COMPLETED) { |
Sudheer Shanka | 2250d56 | 2016-11-07 15:41:02 -0800 | [diff] [blame] | 248 | mStorageManagerService.bootCompleted(); |
Jeff Sharkey | 56e6293 | 2015-03-21 20:41:00 -0700 | [diff] [blame] | 249 | } |
| 250 | } |
Jeff Sharkey | 4887789 | 2015-03-18 11:27:19 -0700 | [diff] [blame] | 251 | |
| 252 | @Override |
Jeff Sharkey | ab15c39 | 2016-05-05 11:45:01 -0600 | [diff] [blame] | 253 | public void onSwitchUser(int userHandle) { |
Sudheer Shanka | 2250d56 | 2016-11-07 15:41:02 -0800 | [diff] [blame] | 254 | mStorageManagerService.mCurrentUserId = userHandle; |
Jeff Sharkey | ab15c39 | 2016-05-05 11:45:01 -0600 | [diff] [blame] | 255 | } |
| 256 | |
| 257 | @Override |
Jeff Sharkey | ce14cd0 | 2015-12-07 15:35:42 -0700 | [diff] [blame] | 258 | public void onUnlockUser(int userHandle) { |
Sudheer Shanka | 2250d56 | 2016-11-07 15:41:02 -0800 | [diff] [blame] | 259 | mStorageManagerService.onUnlockUser(userHandle); |
Jeff Sharkey | 4887789 | 2015-03-18 11:27:19 -0700 | [diff] [blame] | 260 | } |
| 261 | |
| 262 | @Override |
| 263 | public void onCleanupUser(int userHandle) { |
Sudheer Shanka | 2250d56 | 2016-11-07 15:41:02 -0800 | [diff] [blame] | 264 | mStorageManagerService.onCleanupUser(userHandle); |
Jeff Sharkey | 4887789 | 2015-03-18 11:27:19 -0700 | [diff] [blame] | 265 | } |
Martijn Coenen | 7084c6a | 2019-11-22 16:32:09 +0100 | [diff] [blame] | 266 | |
| 267 | @Override |
| 268 | public void onStopUser(int userHandle) { |
| 269 | mStorageManagerService.onStopUser(userHandle); |
| 270 | } |
Jeff Sharkey | 56e6293 | 2015-03-21 20:41:00 -0700 | [diff] [blame] | 271 | } |
| 272 | |
Dianne Hackborn | 40e9f29 | 2012-11-27 19:12:23 -0800 | [diff] [blame] | 273 | private static final boolean DEBUG_EVENTS = false; |
Kenny Root | b7db272 | 2011-01-25 16:39:35 -0800 | [diff] [blame] | 274 | private static final boolean DEBUG_OBB = false; |
Kenny Root | 02c8730 | 2010-07-01 08:10:18 -0700 | [diff] [blame] | 275 | |
Jeff Sharkey | c3c7287 | 2018-06-06 15:35:53 -0600 | [diff] [blame] | 276 | /** |
| 277 | * We now talk to vold over Binder, and it has its own internal lock to |
| 278 | * serialize certain calls. All long-running operations have been migrated |
| 279 | * to be async with callbacks, so we want watchdog to fire if vold wedges. |
| 280 | */ |
| 281 | private static final boolean WATCHDOG_ENABLE = true; |
Kenny Root | 07714d4 | 2011-08-17 17:49:28 -0700 | [diff] [blame] | 282 | |
Jeff Sharkey | 00455bf | 2016-11-04 14:45:24 -0600 | [diff] [blame] | 283 | /** |
| 284 | * Our goal is for all Android devices to be usable as development devices, |
| 285 | * which includes the new Direct Boot mode added in N. For devices that |
| 286 | * don't have native FBE support, we offer an emulation mode for developer |
| 287 | * testing purposes, but if it's prohibitively difficult to support this |
| 288 | * mode, it can be disabled for specific products using this flag. |
| 289 | */ |
| 290 | private static final boolean EMULATE_FBE_SUPPORTED = true; |
| 291 | |
Sudheer Shanka | 2250d56 | 2016-11-07 15:41:02 -0800 | [diff] [blame] | 292 | private static final String TAG = "StorageManagerService"; |
Jeff Sharkey | 6fd6994 | 2019-03-26 17:53:35 -0600 | [diff] [blame] | 293 | private static final boolean LOCAL_LOGV = Log.isLoggable(TAG, Log.VERBOSE); |
Jeff Sharkey | e8a4b66 | 2015-06-27 15:43:45 -0700 | [diff] [blame] | 294 | |
Jeff Sharkey | 9756d75 | 2015-05-14 21:07:42 -0700 | [diff] [blame] | 295 | private static final String TAG_STORAGE_BENCHMARK = "storage_benchmark"; |
Jeff Sharkey | e8a4b66 | 2015-06-27 15:43:45 -0700 | [diff] [blame] | 296 | private static final String TAG_STORAGE_TRIM = "storage_trim"; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 297 | |
Jeff Sharkey | 275e3e4 | 2015-04-24 16:10:32 -0700 | [diff] [blame] | 298 | /** Magic value sent by MoveTask.cpp */ |
| 299 | private static final int MOVE_STATUS_COPY_FINISHED = 82; |
| 300 | |
Jeff Sharkey | 620b32b | 2015-04-23 19:36:02 -0700 | [diff] [blame] | 301 | private static final int VERSION_INIT = 1; |
| 302 | private static final int VERSION_ADD_PRIMARY = 2; |
Jeff Sharkey | fced534 | 2015-05-10 14:53:34 -0700 | [diff] [blame] | 303 | private static final int VERSION_FIX_PRIMARY = 3; |
Jeff Sharkey | 620b32b | 2015-04-23 19:36:02 -0700 | [diff] [blame] | 304 | |
Jeff Sharkey | d95d3bf | 2015-04-14 21:39:44 -0700 | [diff] [blame] | 305 | private static final String TAG_VOLUMES = "volumes"; |
Jeff Sharkey | 620b32b | 2015-04-23 19:36:02 -0700 | [diff] [blame] | 306 | private static final String ATTR_VERSION = "version"; |
| 307 | private static final String ATTR_PRIMARY_STORAGE_UUID = "primaryStorageUuid"; |
Jeff Sharkey | d95d3bf | 2015-04-14 21:39:44 -0700 | [diff] [blame] | 308 | private static final String TAG_VOLUME = "volume"; |
| 309 | private static final String ATTR_TYPE = "type"; |
| 310 | private static final String ATTR_FS_UUID = "fsUuid"; |
Jeff Sharkey | 5cc0df2 | 2015-06-17 19:44:05 -0700 | [diff] [blame] | 311 | private static final String ATTR_PART_GUID = "partGuid"; |
Jeff Sharkey | d95d3bf | 2015-04-14 21:39:44 -0700 | [diff] [blame] | 312 | private static final String ATTR_NICKNAME = "nickname"; |
| 313 | private static final String ATTR_USER_FLAGS = "userFlags"; |
Jeff Sharkey | e8a4b66 | 2015-06-27 15:43:45 -0700 | [diff] [blame] | 314 | private static final String ATTR_CREATED_MILLIS = "createdMillis"; |
Jeff Sharkey | 3811f35 | 2019-05-14 11:54:36 -0600 | [diff] [blame] | 315 | private static final String ATTR_LAST_SEEN_MILLIS = "lastSeenMillis"; |
Jeff Sharkey | e8a4b66 | 2015-06-27 15:43:45 -0700 | [diff] [blame] | 316 | private static final String ATTR_LAST_TRIM_MILLIS = "lastTrimMillis"; |
| 317 | private static final String ATTR_LAST_BENCH_MILLIS = "lastBenchMillis"; |
Jeff Sharkey | d95d3bf | 2015-04-14 21:39:44 -0700 | [diff] [blame] | 318 | |
Philip P. Moltmann | c94ba82 | 2019-03-04 16:48:22 -0800 | [diff] [blame] | 319 | private static final String[] ALL_STORAGE_PERMISSIONS = { |
| 320 | Manifest.permission.READ_EXTERNAL_STORAGE, |
Philip P. Moltmann | 129a0b0 | 2019-03-27 12:24:45 -0700 | [diff] [blame] | 321 | Manifest.permission.WRITE_EXTERNAL_STORAGE |
Philip P. Moltmann | c94ba82 | 2019-03-04 16:48:22 -0800 | [diff] [blame] | 322 | }; |
| 323 | |
Jeff Sharkey | b36586a | 2015-04-27 08:42:28 -0700 | [diff] [blame] | 324 | private final AtomicFile mSettingsFile; |
Jeff Sharkey | d95d3bf | 2015-04-14 21:39:44 -0700 | [diff] [blame] | 325 | |
Jeff Sharkey | 4887789 | 2015-03-18 11:27:19 -0700 | [diff] [blame] | 326 | /** |
| 327 | * <em>Never</em> hold the lock while performing downcalls into vold, since |
| 328 | * unsolicited events can suddenly appear to update data structures. |
| 329 | */ |
Jeff Sharkey | 5f3e934 | 2017-03-13 14:53:11 -0600 | [diff] [blame] | 330 | private final Object mLock = LockGuard.installNewLock(LockGuard.INDEX_STORAGE); |
Jeff Sharkey | 4887789 | 2015-03-18 11:27:19 -0700 | [diff] [blame] | 331 | |
Sudheer Shanka | 0a541a5 | 2018-07-31 13:21:11 -0700 | [diff] [blame] | 332 | /** |
| 333 | * Similar to {@link #mLock}, never hold this lock while performing downcalls into vold. |
| 334 | * Also, never hold this while calling into PackageManagerService since it is used in callbacks |
| 335 | * from PackageManagerService. |
| 336 | * |
| 337 | * If both {@link #mLock} and this lock need to be held, {@link #mLock} should be acquired |
| 338 | * before this. |
| 339 | * |
| 340 | * Use -PL suffix for methods that need to called with this lock held. |
| 341 | */ |
| 342 | private final Object mPackagesLock = new Object(); |
| 343 | |
Jeff Sharkey | ce14cd0 | 2015-12-07 15:35:42 -0700 | [diff] [blame] | 344 | /** Set of users that we know are unlocked. */ |
Jeff Sharkey | 4887789 | 2015-03-18 11:27:19 -0700 | [diff] [blame] | 345 | @GuardedBy("mLock") |
Jeff Sharkey | ce14cd0 | 2015-12-07 15:35:42 -0700 | [diff] [blame] | 346 | private int[] mLocalUnlockedUsers = EmptyArray.INT; |
| 347 | /** Set of users that system knows are unlocked. */ |
Jeff Sharkey | f9fc6d6 | 2015-11-08 16:46:05 -0800 | [diff] [blame] | 348 | @GuardedBy("mLock") |
Jeff Sharkey | ce14cd0 | 2015-12-07 15:35:42 -0700 | [diff] [blame] | 349 | private int[] mSystemUnlockedUsers = EmptyArray.INT; |
Jeff Sharkey | d95d3bf | 2015-04-14 21:39:44 -0700 | [diff] [blame] | 350 | |
| 351 | /** Map from disk ID to disk */ |
Jeff Sharkey | 4887789 | 2015-03-18 11:27:19 -0700 | [diff] [blame] | 352 | @GuardedBy("mLock") |
Jeff Sharkey | 1b8ef7e | 2015-04-03 17:14:45 -0700 | [diff] [blame] | 353 | private ArrayMap<String, DiskInfo> mDisks = new ArrayMap<>(); |
Jeff Sharkey | d95d3bf | 2015-04-14 21:39:44 -0700 | [diff] [blame] | 354 | /** Map from volume ID to disk */ |
Jeff Sharkey | 4887789 | 2015-03-18 11:27:19 -0700 | [diff] [blame] | 355 | @GuardedBy("mLock") |
Xiaohui Chen | 621b3fc | 2015-10-02 14:41:42 -0700 | [diff] [blame] | 356 | private final ArrayMap<String, VolumeInfo> mVolumes = new ArrayMap<>(); |
Jeff Sharkey | 4887789 | 2015-03-18 11:27:19 -0700 | [diff] [blame] | 357 | |
Jeff Sharkey | b36586a | 2015-04-27 08:42:28 -0700 | [diff] [blame] | 358 | /** Map from UUID to record */ |
Jeff Sharkey | d95d3bf | 2015-04-14 21:39:44 -0700 | [diff] [blame] | 359 | @GuardedBy("mLock") |
Jeff Sharkey | b36586a | 2015-04-27 08:42:28 -0700 | [diff] [blame] | 360 | private ArrayMap<String, VolumeRecord> mRecords = new ArrayMap<>(); |
Jeff Sharkey | 620b32b | 2015-04-23 19:36:02 -0700 | [diff] [blame] | 361 | @GuardedBy("mLock") |
| 362 | private String mPrimaryStorageUuid; |
Jeff Sharkey | d95d3bf | 2015-04-14 21:39:44 -0700 | [diff] [blame] | 363 | |
Jeff Sharkey | eba260d | 2015-04-19 14:35:16 -0700 | [diff] [blame] | 364 | /** Map from disk ID to latches */ |
| 365 | @GuardedBy("mLock") |
| 366 | private ArrayMap<String, CountDownLatch> mDiskScanLatches = new ArrayMap<>(); |
| 367 | |
Jeff Sharkey | 275e3e4 | 2015-04-24 16:10:32 -0700 | [diff] [blame] | 368 | @GuardedBy("mLock") |
| 369 | private IPackageMoveObserver mMoveCallback; |
| 370 | @GuardedBy("mLock") |
| 371 | private String mMoveTargetUuid; |
| 372 | |
Martijn Coenen | 44db1ac | 2019-12-03 16:06:19 +0100 | [diff] [blame] | 373 | private volatile int mMediaStoreAuthorityAppId = -1; |
Zim | 74a9bba | 2019-09-03 20:49:13 +0100 | [diff] [blame] | 374 | |
Martijn Coenen | 496ac00 | 2020-01-08 14:55:53 +0100 | [diff] [blame] | 375 | private volatile int mDownloadsAuthorityAppId = -1; |
| 376 | |
Jeff Sharkey | ab15c39 | 2016-05-05 11:45:01 -0600 | [diff] [blame] | 377 | private volatile int mCurrentUserId = UserHandle.USER_SYSTEM; |
| 378 | |
Ricky Wai | 4482ab5 | 2019-12-10 19:08:18 +0000 | [diff] [blame] | 379 | private final Installer mInstaller; |
| 380 | |
Daichi Hirono | 9fb0018 | 2016-11-08 14:12:17 +0900 | [diff] [blame] | 381 | /** Holding lock for AppFuse business */ |
| 382 | private final Object mAppFuseLock = new Object(); |
| 383 | |
| 384 | @GuardedBy("mAppFuseLock") |
| 385 | private int mNextAppFuseName = 0; |
| 386 | |
| 387 | @GuardedBy("mAppFuseLock") |
Daichi Hirono | e56740d | 2017-02-02 13:56:45 +0900 | [diff] [blame] | 388 | private AppFuseBridge mAppFuseBridge = null; |
Daichi Hirono | 9fb0018 | 2016-11-08 14:12:17 +0900 | [diff] [blame] | 389 | |
Martijn Coenen | 9fd2b64 | 2019-12-24 13:04:36 +0100 | [diff] [blame] | 390 | /** Matches known application dir paths. The first group contains the generic part of the path, |
| 391 | * the second group contains the user id (or null if it's a public volume without users), the |
| 392 | * third group contains the package name, and the fourth group the remainder of the path. |
| 393 | */ |
| 394 | public static final Pattern KNOWN_APP_DIR_PATHS = Pattern.compile( |
| 395 | "(?i)(^/storage/[^/]+/(?:([0-9]+)/)?Android/(?:data|media|obb|sandbox)/)([^/]+)(/.*)?"); |
| 396 | |
| 397 | |
Jeff Sharkey | ef10ee0 | 2015-07-05 14:17:27 -0700 | [diff] [blame] | 398 | private VolumeInfo findVolumeByIdOrThrow(String id) { |
Jeff Sharkey | 7151a9a | 2015-04-04 15:22:37 -0700 | [diff] [blame] | 399 | synchronized (mLock) { |
| 400 | final VolumeInfo vol = mVolumes.get(id); |
| 401 | if (vol != null) { |
| 402 | return vol; |
| 403 | } |
| 404 | } |
| 405 | throw new IllegalArgumentException("No volume found for ID " + id); |
| 406 | } |
| 407 | |
Jeff Sharkey | ef10ee0 | 2015-07-05 14:17:27 -0700 | [diff] [blame] | 408 | private String findVolumeIdForPathOrThrow(String path) { |
Jeff Sharkey | 4887789 | 2015-03-18 11:27:19 -0700 | [diff] [blame] | 409 | synchronized (mLock) { |
Jeff Sharkey | 1b8ef7e | 2015-04-03 17:14:45 -0700 | [diff] [blame] | 410 | for (int i = 0; i < mVolumes.size(); i++) { |
| 411 | final VolumeInfo vol = mVolumes.valueAt(i); |
Jeff Sharkey | 7151a9a | 2015-04-04 15:22:37 -0700 | [diff] [blame] | 412 | if (vol.path != null && path.startsWith(vol.path)) { |
| 413 | return vol.id; |
Jeff Sharkey | 4887789 | 2015-03-18 11:27:19 -0700 | [diff] [blame] | 414 | } |
| 415 | } |
| 416 | } |
Jeff Sharkey | 7151a9a | 2015-04-04 15:22:37 -0700 | [diff] [blame] | 417 | throw new IllegalArgumentException("No volume found for path " + path); |
Jeff Sharkey | 4887789 | 2015-03-18 11:27:19 -0700 | [diff] [blame] | 418 | } |
| 419 | |
Jeff Sharkey | e8a4b66 | 2015-06-27 15:43:45 -0700 | [diff] [blame] | 420 | private VolumeRecord findRecordForPath(String path) { |
| 421 | synchronized (mLock) { |
| 422 | for (int i = 0; i < mVolumes.size(); i++) { |
| 423 | final VolumeInfo vol = mVolumes.valueAt(i); |
| 424 | if (vol.path != null && path.startsWith(vol.path)) { |
| 425 | return mRecords.get(vol.fsUuid); |
| 426 | } |
| 427 | } |
| 428 | } |
| 429 | return null; |
| 430 | } |
| 431 | |
| 432 | private String scrubPath(String path) { |
| 433 | if (path.startsWith(Environment.getDataDirectory().getAbsolutePath())) { |
| 434 | return "internal"; |
| 435 | } |
| 436 | final VolumeRecord rec = findRecordForPath(path); |
| 437 | if (rec == null || rec.createdMillis == 0) { |
| 438 | return "unknown"; |
| 439 | } else { |
| 440 | return "ext:" + (int) ((System.currentTimeMillis() - rec.createdMillis) |
| 441 | / DateUtils.WEEK_IN_MILLIS) + "w"; |
| 442 | } |
| 443 | } |
| 444 | |
Jeff Sharkey | ef10ee0 | 2015-07-05 14:17:27 -0700 | [diff] [blame] | 445 | private @Nullable VolumeInfo findStorageForUuid(String volumeUuid) { |
Jeff Sharkey | 275e3e4 | 2015-04-24 16:10:32 -0700 | [diff] [blame] | 446 | final StorageManager storage = mContext.getSystemService(StorageManager.class); |
| 447 | if (Objects.equals(StorageManager.UUID_PRIVATE_INTERNAL, volumeUuid)) { |
Zim | 17be6f9 | 2019-09-25 14:37:55 +0100 | [diff] [blame] | 448 | return storage.findVolumeById(VolumeInfo.ID_EMULATED_INTERNAL + ";" + 0); |
Jeff Sharkey | 275e3e4 | 2015-04-24 16:10:32 -0700 | [diff] [blame] | 449 | } else if (Objects.equals(StorageManager.UUID_PRIMARY_PHYSICAL, volumeUuid)) { |
| 450 | return storage.getPrimaryPhysicalVolume(); |
| 451 | } else { |
| 452 | return storage.findEmulatedForPrivate(storage.findVolumeByUuid(volumeUuid)); |
| 453 | } |
| 454 | } |
| 455 | |
Jeff Sharkey | e8a4b66 | 2015-06-27 15:43:45 -0700 | [diff] [blame] | 456 | private boolean shouldBenchmark() { |
| 457 | final long benchInterval = Settings.Global.getLong(mContext.getContentResolver(), |
| 458 | Settings.Global.STORAGE_BENCHMARK_INTERVAL, DateUtils.WEEK_IN_MILLIS); |
Jeff Sharkey | e83d8a9 | 2015-09-09 14:53:38 -0700 | [diff] [blame] | 459 | if (benchInterval == -1) { |
| 460 | return false; |
| 461 | } else if (benchInterval == 0) { |
| 462 | return true; |
| 463 | } |
| 464 | |
Jeff Sharkey | e8a4b66 | 2015-06-27 15:43:45 -0700 | [diff] [blame] | 465 | synchronized (mLock) { |
| 466 | for (int i = 0; i < mVolumes.size(); i++) { |
| 467 | final VolumeInfo vol = mVolumes.valueAt(i); |
| 468 | final VolumeRecord rec = mRecords.get(vol.fsUuid); |
Jeff Sharkey | e83d8a9 | 2015-09-09 14:53:38 -0700 | [diff] [blame] | 469 | if (vol.isMountedWritable() && rec != null) { |
Jeff Sharkey | e8a4b66 | 2015-06-27 15:43:45 -0700 | [diff] [blame] | 470 | final long benchAge = System.currentTimeMillis() - rec.lastBenchMillis; |
| 471 | if (benchAge >= benchInterval) { |
| 472 | return true; |
| 473 | } |
| 474 | } |
| 475 | } |
| 476 | return false; |
| 477 | } |
| 478 | } |
| 479 | |
Jeff Sharkey | eba260d | 2015-04-19 14:35:16 -0700 | [diff] [blame] | 480 | private CountDownLatch findOrCreateDiskScanLatch(String diskId) { |
| 481 | synchronized (mLock) { |
| 482 | CountDownLatch latch = mDiskScanLatches.get(diskId); |
| 483 | if (latch == null) { |
| 484 | latch = new CountDownLatch(1); |
| 485 | mDiskScanLatches.put(diskId, latch); |
| 486 | } |
| 487 | return latch; |
| 488 | } |
| 489 | } |
| 490 | |
Paul Lawrence | 8e39736 | 2014-01-27 15:22:30 -0800 | [diff] [blame] | 491 | /** List of crypto types. |
| 492 | * These must match CRYPT_TYPE_XXX in cryptfs.h AND their |
| 493 | * corresponding commands in CommandListener.cpp */ |
| 494 | public static final String[] CRYPTO_TYPES |
| 495 | = { "password", "default", "pattern", "pin" }; |
| 496 | |
Brian Carlstrom | 7395a8a | 2014-04-28 22:11:01 -0700 | [diff] [blame] | 497 | private final Context mContext; |
Jeff Sharkey | 5d0c55c | 2019-01-24 14:32:31 -0700 | [diff] [blame] | 498 | private final ContentResolver mResolver; |
Jeff Sharkey | cd57599 | 2016-03-29 14:12:49 -0600 | [diff] [blame] | 499 | |
Jeff Sharkey | 1019de9 | 2017-09-06 13:47:03 -0600 | [diff] [blame] | 500 | private volatile IVold mVold; |
Jin Qian | 12690d5 | 2017-10-13 18:17:04 -0700 | [diff] [blame] | 501 | private volatile IStoraged mStoraged; |
Jeff Sharkey | 1019de9 | 2017-09-06 13:47:03 -0600 | [diff] [blame] | 502 | |
Yasuhiro Matsuda | 87a38b5 | 2015-07-24 22:10:16 +0900 | [diff] [blame] | 503 | private volatile boolean mBootCompleted = false; |
Jeff Sharkey | 4887789 | 2015-03-18 11:27:19 -0700 | [diff] [blame] | 504 | private volatile boolean mDaemonConnected = false; |
Jeff Sharkey | 9765e44 | 2017-12-14 22:15:14 -0700 | [diff] [blame] | 505 | private volatile boolean mSecureKeyguardShowing = true; |
Jeff Sharkey | b049e21 | 2012-09-07 23:16:01 -0700 | [diff] [blame] | 506 | |
Sudheer Shanka | 0a541a5 | 2018-07-31 13:21:11 -0700 | [diff] [blame] | 507 | private PackageManagerInternal mPmInternal; |
Jeff Sharkey | 7151a9a | 2015-04-04 15:22:37 -0700 | [diff] [blame] | 508 | |
Sudheer Shanka | 3a0df3b | 2018-12-12 12:43:43 -0800 | [diff] [blame] | 509 | private IPackageManager mIPackageManager; |
| 510 | private IAppOpsService mIAppOpsService; |
| 511 | |
Jeff Sharkey | 7151a9a | 2015-04-04 15:22:37 -0700 | [diff] [blame] | 512 | private final Callbacks mCallbacks; |
Jeff Sharkey | b9fe537 | 2015-12-03 15:23:08 -0700 | [diff] [blame] | 513 | private final LockPatternUtils mLockPatternUtils; |
Jeff Sharkey | 4887789 | 2015-03-18 11:27:19 -0700 | [diff] [blame] | 514 | |
San Mehat | 6cdd9c0 | 2010-02-09 14:45:20 -0800 | [diff] [blame] | 515 | /** |
Kenny Root | 3b1abba | 2010-10-13 15:00:07 -0700 | [diff] [blame] | 516 | * The size of the crypto algorithm key in bits for OBB files. Currently |
| 517 | * Twofish is used which takes 128-bit keys. |
| 518 | */ |
| 519 | private static final int CRYPTO_ALGORITHM_KEY_SIZE = 128; |
| 520 | |
| 521 | /** |
| 522 | * The number of times to run SHA1 in the PBKDF2 function for OBB files. |
| 523 | * 1024 is reasonably secure and not too slow. |
| 524 | */ |
| 525 | private static final int PBKDF2_HASH_ROUNDS = 1024; |
| 526 | |
| 527 | /** |
Kenny Root | a02b8b0 | 2010-08-05 16:14:17 -0700 | [diff] [blame] | 528 | * Mounted OBB tracking information. Used to track the current state of all |
| 529 | * OBBs. |
Kenny Root | 02c8730 | 2010-07-01 08:10:18 -0700 | [diff] [blame] | 530 | */ |
Kenny Root | 735de3b | 2010-09-30 14:11:39 -0700 | [diff] [blame] | 531 | final private Map<IBinder, List<ObbState>> mObbMounts = new HashMap<IBinder, List<ObbState>>(); |
Jeff Sharkey | 4fbbda4 | 2012-09-24 18:34:07 -0700 | [diff] [blame] | 532 | |
| 533 | /** Map from raw paths to {@link ObbState}. */ |
Kenny Root | a02b8b0 | 2010-08-05 16:14:17 -0700 | [diff] [blame] | 534 | final private Map<String, ObbState> mObbPathToStateMap = new HashMap<String, ObbState>(); |
| 535 | |
Svet Ganov | 6ee871e | 2015-07-10 14:29:33 -0700 | [diff] [blame] | 536 | // Not guarded by a lock. |
Sudheer Shanka | 2250d56 | 2016-11-07 15:41:02 -0800 | [diff] [blame] | 537 | private final StorageManagerInternalImpl mStorageManagerInternal |
| 538 | = new StorageManagerInternalImpl(); |
Svet Ganov | 6ee871e | 2015-07-10 14:29:33 -0700 | [diff] [blame] | 539 | |
Zim | 42f1e9f | 2019-08-15 17:35:00 +0100 | [diff] [blame] | 540 | // Not guarded by a lock. |
| 541 | private final StorageSessionController mStorageSessionController; |
| 542 | |
Zim | 17be6f9 | 2019-09-25 14:37:55 +0100 | [diff] [blame] | 543 | private final boolean mIsFuseEnabled; |
| 544 | |
Kenny Root | a02b8b0 | 2010-08-05 16:14:17 -0700 | [diff] [blame] | 545 | class ObbState implements IBinder.DeathRecipient { |
Jeff Sharkey | 4fbbda4 | 2012-09-24 18:34:07 -0700 | [diff] [blame] | 546 | public ObbState(String rawPath, String canonicalPath, int callingUid, |
Jeff Sharkey | 41cd681 | 2017-09-11 10:32:17 -0600 | [diff] [blame] | 547 | IObbActionListener token, int nonce, String volId) { |
Jeff Sharkey | 4fbbda4 | 2012-09-24 18:34:07 -0700 | [diff] [blame] | 548 | this.rawPath = rawPath; |
Xiaohui Chen | 621b3fc | 2015-10-02 14:41:42 -0700 | [diff] [blame] | 549 | this.canonicalPath = canonicalPath; |
Jeff Sharkey | 4fbbda4 | 2012-09-24 18:34:07 -0700 | [diff] [blame] | 550 | this.ownerGid = UserHandle.getSharedAppGid(callingUid); |
Kenny Root | af9d667 | 2010-10-08 09:21:39 -0700 | [diff] [blame] | 551 | this.token = token; |
| 552 | this.nonce = nonce; |
Jeff Sharkey | 41cd681 | 2017-09-11 10:32:17 -0600 | [diff] [blame] | 553 | this.volId = volId; |
Kenny Root | a02b8b0 | 2010-08-05 16:14:17 -0700 | [diff] [blame] | 554 | } |
| 555 | |
Jeff Sharkey | 4fbbda4 | 2012-09-24 18:34:07 -0700 | [diff] [blame] | 556 | final String rawPath; |
| 557 | final String canonicalPath; |
Kenny Root | a02b8b0 | 2010-08-05 16:14:17 -0700 | [diff] [blame] | 558 | |
Jeff Sharkey | 4fbbda4 | 2012-09-24 18:34:07 -0700 | [diff] [blame] | 559 | final int ownerGid; |
Kenny Root | a02b8b0 | 2010-08-05 16:14:17 -0700 | [diff] [blame] | 560 | |
Kenny Root | af9d667 | 2010-10-08 09:21:39 -0700 | [diff] [blame] | 561 | // Token of remote Binder caller |
| 562 | final IObbActionListener token; |
| 563 | |
| 564 | // Identifier to pass back to the token |
| 565 | final int nonce; |
Kenny Root | a02b8b0 | 2010-08-05 16:14:17 -0700 | [diff] [blame] | 566 | |
Jeff Sharkey | 41cd681 | 2017-09-11 10:32:17 -0600 | [diff] [blame] | 567 | String volId; |
| 568 | |
Kenny Root | 735de3b | 2010-09-30 14:11:39 -0700 | [diff] [blame] | 569 | public IBinder getBinder() { |
| 570 | return token.asBinder(); |
| 571 | } |
| 572 | |
Kenny Root | a02b8b0 | 2010-08-05 16:14:17 -0700 | [diff] [blame] | 573 | @Override |
| 574 | public void binderDied() { |
| 575 | ObbAction action = new UnmountObbAction(this, true); |
| 576 | mObbActionHandler.sendMessage(mObbActionHandler.obtainMessage(OBB_RUN_ACTION, action)); |
Kenny Root | 735de3b | 2010-09-30 14:11:39 -0700 | [diff] [blame] | 577 | } |
Kenny Root | a02b8b0 | 2010-08-05 16:14:17 -0700 | [diff] [blame] | 578 | |
Kenny Root | 5919ac6 | 2010-10-05 09:49:40 -0700 | [diff] [blame] | 579 | public void link() throws RemoteException { |
| 580 | getBinder().linkToDeath(this, 0); |
| 581 | } |
| 582 | |
| 583 | public void unlink() { |
Kenny Root | 735de3b | 2010-09-30 14:11:39 -0700 | [diff] [blame] | 584 | getBinder().unlinkToDeath(this, 0); |
Kenny Root | a02b8b0 | 2010-08-05 16:14:17 -0700 | [diff] [blame] | 585 | } |
Kenny Root | 38cf886 | 2010-09-26 14:18:51 -0700 | [diff] [blame] | 586 | |
| 587 | @Override |
| 588 | public String toString() { |
| 589 | StringBuilder sb = new StringBuilder("ObbState{"); |
Jeff Sharkey | 4fbbda4 | 2012-09-24 18:34:07 -0700 | [diff] [blame] | 590 | sb.append("rawPath=").append(rawPath); |
| 591 | sb.append(",canonicalPath=").append(canonicalPath); |
Jeff Sharkey | 4fbbda4 | 2012-09-24 18:34:07 -0700 | [diff] [blame] | 592 | sb.append(",ownerGid=").append(ownerGid); |
| 593 | sb.append(",token=").append(token); |
| 594 | sb.append(",binder=").append(getBinder()); |
Jeff Sharkey | 41cd681 | 2017-09-11 10:32:17 -0600 | [diff] [blame] | 595 | sb.append(",volId=").append(volId); |
Kenny Root | 38cf886 | 2010-09-26 14:18:51 -0700 | [diff] [blame] | 596 | sb.append('}'); |
| 597 | return sb.toString(); |
| 598 | } |
Kenny Root | a02b8b0 | 2010-08-05 16:14:17 -0700 | [diff] [blame] | 599 | } |
| 600 | |
| 601 | // OBB Action Handler |
| 602 | final private ObbActionHandler mObbActionHandler; |
| 603 | |
| 604 | // OBB action handler messages |
| 605 | private static final int OBB_RUN_ACTION = 1; |
Sudheer Shanka | 25469aa | 2018-08-27 15:50:23 -0700 | [diff] [blame] | 606 | private static final int OBB_FLUSH_MOUNT_STATE = 2; |
Kenny Root | 02c8730 | 2010-07-01 08:10:18 -0700 | [diff] [blame] | 607 | |
Christopher Tate | 7265abe | 2014-11-21 13:54:45 -0800 | [diff] [blame] | 608 | // Last fstrim operation tracking |
| 609 | private static final String LAST_FSTRIM_FILE = "last-fstrim"; |
| 610 | private final File mLastMaintenanceFile; |
| 611 | private long mLastMaintenance; |
| 612 | |
Kenny Root | 02c8730 | 2010-07-01 08:10:18 -0700 | [diff] [blame] | 613 | // Handler messages |
Jeff Sharkey | 4887789 | 2015-03-18 11:27:19 -0700 | [diff] [blame] | 614 | private static final int H_SYSTEM_READY = 1; |
| 615 | private static final int H_DAEMON_CONNECTED = 2; |
| 616 | private static final int H_SHUTDOWN = 3; |
| 617 | private static final int H_FSTRIM = 4; |
| 618 | private static final int H_VOLUME_MOUNT = 5; |
| 619 | private static final int H_VOLUME_BROADCAST = 6; |
Jeff Sharkey | abc3e85 | 2015-08-03 14:41:13 -0700 | [diff] [blame] | 620 | private static final int H_INTERNAL_BROADCAST = 7; |
Xiaohui Chen | 621b3fc | 2015-10-02 14:41:42 -0700 | [diff] [blame] | 621 | private static final int H_VOLUME_UNMOUNT = 8; |
Jeff Sharkey | 5a9bb74 | 2015-11-03 10:15:57 -0800 | [diff] [blame] | 622 | private static final int H_PARTITION_FORGET = 9; |
| 623 | private static final int H_RESET = 10; |
Jin Qian | a85b991 | 2017-10-17 15:48:18 -0700 | [diff] [blame] | 624 | private static final int H_RUN_IDLE_MAINT = 11; |
| 625 | private static final int H_ABORT_IDLE_MAINT = 12; |
Daniel Rosenberg | 137aed1 | 2019-03-15 18:41:11 -0700 | [diff] [blame] | 626 | private static final int H_BOOT_COMPLETED = 13; |
Narayan Kamath | 157dd1d | 2019-06-12 13:06:30 +0100 | [diff] [blame] | 627 | private static final int H_COMPLETE_UNLOCK_USER = 14; |
Suchi Amalapurapu | 6ffce2e | 2010-03-08 14:48:40 -0800 | [diff] [blame] | 628 | |
Sudheer Shanka | 2250d56 | 2016-11-07 15:41:02 -0800 | [diff] [blame] | 629 | class StorageManagerServiceHandler extends Handler { |
| 630 | public StorageManagerServiceHandler(Looper looper) { |
Jeff Sharkey | 4887789 | 2015-03-18 11:27:19 -0700 | [diff] [blame] | 631 | super(looper); |
Daniel Sandler | 5f27ef4 | 2010-03-16 15:42:02 -0400 | [diff] [blame] | 632 | } |
| 633 | |
Jason parks | 5af0b91 | 2010-11-29 09:05:25 -0600 | [diff] [blame] | 634 | @Override |
Suchi Amalapurapu | c42e29e | 2010-02-22 16:03:53 -0800 | [diff] [blame] | 635 | public void handleMessage(Message msg) { |
| 636 | switch (msg.what) { |
Jeff Sharkey | b049e21 | 2012-09-07 23:16:01 -0700 | [diff] [blame] | 637 | case H_SYSTEM_READY: { |
Jeff Sharkey | 4887789 | 2015-03-18 11:27:19 -0700 | [diff] [blame] | 638 | handleSystemReady(); |
| 639 | break; |
| 640 | } |
Daniel Rosenberg | 137aed1 | 2019-03-15 18:41:11 -0700 | [diff] [blame] | 641 | case H_BOOT_COMPLETED: { |
| 642 | handleBootCompleted(); |
| 643 | break; |
| 644 | } |
Jeff Sharkey | 4887789 | 2015-03-18 11:27:19 -0700 | [diff] [blame] | 645 | case H_DAEMON_CONNECTED: { |
| 646 | handleDaemonConnected(); |
Jeff Sharkey | b049e21 | 2012-09-07 23:16:01 -0700 | [diff] [blame] | 647 | break; |
| 648 | } |
Christopher Tate | d417d62 | 2013-08-19 16:14:25 -0700 | [diff] [blame] | 649 | case H_FSTRIM: { |
Christopher Tate | d417d62 | 2013-08-19 16:14:25 -0700 | [diff] [blame] | 650 | Slog.i(TAG, "Running fstrim idle maintenance"); |
Christopher Tate | 7265abe | 2014-11-21 13:54:45 -0800 | [diff] [blame] | 651 | |
| 652 | // Remember when we kicked it off |
| 653 | try { |
| 654 | mLastMaintenance = System.currentTimeMillis(); |
| 655 | mLastMaintenanceFile.setLastModified(mLastMaintenance); |
| 656 | } catch (Exception e) { |
| 657 | Slog.e(TAG, "Unable to record last fstrim!"); |
| 658 | } |
| 659 | |
Jeff Sharkey | b302c54 | 2017-09-15 12:57:59 -0600 | [diff] [blame] | 660 | // TODO: Reintroduce shouldBenchmark() test |
Jeff Sharkey | 7e19f53 | 2017-11-06 13:54:11 -0700 | [diff] [blame] | 661 | fstrim(0, null); |
Christopher Tate | 7265abe | 2014-11-21 13:54:45 -0800 | [diff] [blame] | 662 | |
Christopher Tate | d417d62 | 2013-08-19 16:14:25 -0700 | [diff] [blame] | 663 | // invoke the completion callback, if any |
Jeff Sharkey | e8a4b66 | 2015-06-27 15:43:45 -0700 | [diff] [blame] | 664 | // TODO: fstrim is non-blocking, so remove this useless callback |
Christopher Tate | d417d62 | 2013-08-19 16:14:25 -0700 | [diff] [blame] | 665 | Runnable callback = (Runnable) msg.obj; |
| 666 | if (callback != null) { |
| 667 | callback.run(); |
| 668 | } |
| 669 | break; |
| 670 | } |
Jeff Sharkey | 4887789 | 2015-03-18 11:27:19 -0700 | [diff] [blame] | 671 | case H_SHUTDOWN: { |
Sudheer Shanka | 2250d56 | 2016-11-07 15:41:02 -0800 | [diff] [blame] | 672 | final IStorageShutdownObserver obs = (IStorageShutdownObserver) msg.obj; |
Jeff Sharkey | 4887789 | 2015-03-18 11:27:19 -0700 | [diff] [blame] | 673 | boolean success = false; |
| 674 | try { |
Jeff Sharkey | 5440279 | 2017-09-15 16:05:19 -0600 | [diff] [blame] | 675 | mVold.shutdown(); |
| 676 | success = true; |
Jeff Sharkey | ace874b | 2017-09-07 15:27:33 -0600 | [diff] [blame] | 677 | } catch (Exception e) { |
| 678 | Slog.wtf(TAG, e); |
Jeff Sharkey | 4887789 | 2015-03-18 11:27:19 -0700 | [diff] [blame] | 679 | } |
| 680 | if (obs != null) { |
| 681 | try { |
| 682 | obs.onShutDownComplete(success ? 0 : -1); |
Jeff Sharkey | ace874b | 2017-09-07 15:27:33 -0600 | [diff] [blame] | 683 | } catch (Exception ignored) { |
Jeff Sharkey | 4887789 | 2015-03-18 11:27:19 -0700 | [diff] [blame] | 684 | } |
| 685 | } |
| 686 | break; |
| 687 | } |
| 688 | case H_VOLUME_MOUNT: { |
Jeff Sharkey | 1b8ef7e | 2015-04-03 17:14:45 -0700 | [diff] [blame] | 689 | final VolumeInfo vol = (VolumeInfo) msg.obj; |
Jeff Sharkey | 2e606d7 | 2015-07-27 14:19:54 -0700 | [diff] [blame] | 690 | if (isMountDisallowed(vol)) { |
| 691 | Slog.i(TAG, "Ignoring mount " + vol.getId() + " due to policy"); |
| 692 | break; |
| 693 | } |
Zim | 42f1e9f | 2019-08-15 17:35:00 +0100 | [diff] [blame] | 694 | |
Zim | 17be6f9 | 2019-09-25 14:37:55 +0100 | [diff] [blame] | 695 | mount(vol); |
Jeff Sharkey | 4887789 | 2015-03-18 11:27:19 -0700 | [diff] [blame] | 696 | break; |
| 697 | } |
Xiaohui Chen | 621b3fc | 2015-10-02 14:41:42 -0700 | [diff] [blame] | 698 | case H_VOLUME_UNMOUNT: { |
| 699 | final VolumeInfo vol = (VolumeInfo) msg.obj; |
Sudheer Shanka | 3f0645b | 2018-09-18 13:07:59 -0700 | [diff] [blame] | 700 | unmount(vol); |
Xiaohui Chen | 621b3fc | 2015-10-02 14:41:42 -0700 | [diff] [blame] | 701 | break; |
| 702 | } |
Jeff Sharkey | 4887789 | 2015-03-18 11:27:19 -0700 | [diff] [blame] | 703 | case H_VOLUME_BROADCAST: { |
| 704 | final StorageVolume userVol = (StorageVolume) msg.obj; |
Jeff Sharkey | 7151a9a | 2015-04-04 15:22:37 -0700 | [diff] [blame] | 705 | final String envState = userVol.getState(); |
| 706 | Slog.d(TAG, "Volume " + userVol.getId() + " broadcasting " + envState + " to " |
Jeff Sharkey | 4887789 | 2015-03-18 11:27:19 -0700 | [diff] [blame] | 707 | + userVol.getOwner()); |
| 708 | |
Jeff Sharkey | 7151a9a | 2015-04-04 15:22:37 -0700 | [diff] [blame] | 709 | final String action = VolumeInfo.getBroadcastForEnvironment(envState); |
Jeff Sharkey | 4887789 | 2015-03-18 11:27:19 -0700 | [diff] [blame] | 710 | if (action != null) { |
| 711 | final Intent intent = new Intent(action, |
| 712 | Uri.fromFile(userVol.getPathFile())); |
| 713 | intent.putExtra(StorageVolume.EXTRA_STORAGE_VOLUME, userVol); |
Jeff Sharkey | 082f83b | 2017-03-26 14:34:47 -0600 | [diff] [blame] | 714 | intent.addFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY_BEFORE_BOOT |
| 715 | | Intent.FLAG_RECEIVER_INCLUDE_BACKGROUND); |
Jeff Sharkey | 4887789 | 2015-03-18 11:27:19 -0700 | [diff] [blame] | 716 | mContext.sendBroadcastAsUser(intent, userVol.getOwner()); |
| 717 | } |
| 718 | break; |
| 719 | } |
Jeff Sharkey | abc3e85 | 2015-08-03 14:41:13 -0700 | [diff] [blame] | 720 | case H_INTERNAL_BROADCAST: { |
| 721 | // Internal broadcasts aimed at system components, not for |
| 722 | // third-party apps. |
| 723 | final Intent intent = (Intent) msg.obj; |
| 724 | mContext.sendBroadcastAsUser(intent, UserHandle.ALL, |
| 725 | android.Manifest.permission.WRITE_MEDIA_STORAGE); |
Jeff Sharkey | 5a9bb74 | 2015-11-03 10:15:57 -0800 | [diff] [blame] | 726 | break; |
| 727 | } |
| 728 | case H_PARTITION_FORGET: { |
Jeff Sharkey | db4b619 | 2017-10-24 11:08:50 -0600 | [diff] [blame] | 729 | final VolumeRecord rec = (VolumeRecord) msg.obj; |
| 730 | forgetPartition(rec.partGuid, rec.fsUuid); |
Jeff Sharkey | 5a9bb74 | 2015-11-03 10:15:57 -0800 | [diff] [blame] | 731 | break; |
| 732 | } |
| 733 | case H_RESET: { |
Daniel Rosenberg | 137aed1 | 2019-03-15 18:41:11 -0700 | [diff] [blame] | 734 | resetIfBootedAndConnected(); |
Jeff Sharkey | 5a9bb74 | 2015-11-03 10:15:57 -0800 | [diff] [blame] | 735 | break; |
Jeff Sharkey | abc3e85 | 2015-08-03 14:41:13 -0700 | [diff] [blame] | 736 | } |
Jin Qian | a85b991 | 2017-10-17 15:48:18 -0700 | [diff] [blame] | 737 | case H_RUN_IDLE_MAINT: { |
| 738 | Slog.i(TAG, "Running idle maintenance"); |
| 739 | runIdleMaint((Runnable)msg.obj); |
| 740 | break; |
| 741 | } |
| 742 | case H_ABORT_IDLE_MAINT: { |
| 743 | Slog.i(TAG, "Aborting idle maintenance"); |
| 744 | abortIdleMaint((Runnable)msg.obj); |
| 745 | break; |
| 746 | } |
Narayan Kamath | 157dd1d | 2019-06-12 13:06:30 +0100 | [diff] [blame] | 747 | case H_COMPLETE_UNLOCK_USER: { |
| 748 | completeUnlockUser((int) msg.obj); |
| 749 | break; |
| 750 | } |
Suchi Amalapurapu | c42e29e | 2010-02-22 16:03:53 -0800 | [diff] [blame] | 751 | } |
| 752 | } |
Jeff Sharkey | 4887789 | 2015-03-18 11:27:19 -0700 | [diff] [blame] | 753 | } |
Jeff Sharkey | b049e21 | 2012-09-07 23:16:01 -0700 | [diff] [blame] | 754 | |
Jeff Sharkey | b049e21 | 2012-09-07 23:16:01 -0700 | [diff] [blame] | 755 | private final Handler mHandler; |
Suchi Amalapurapu | c42e29e | 2010-02-22 16:03:53 -0800 | [diff] [blame] | 756 | |
Jeff Sharkey | bcd262d | 2015-06-10 09:41:17 -0700 | [diff] [blame] | 757 | private BroadcastReceiver mUserReceiver = new BroadcastReceiver() { |
| 758 | @Override |
| 759 | public void onReceive(Context context, Intent intent) { |
| 760 | final String action = intent.getAction(); |
| 761 | final int userId = intent.getIntExtra(Intent.EXTRA_USER_HANDLE, -1); |
Xiaohui Chen | 621b3fc | 2015-10-02 14:41:42 -0700 | [diff] [blame] | 762 | Preconditions.checkArgument(userId >= 0); |
Jeff Sharkey | bcd262d | 2015-06-10 09:41:17 -0700 | [diff] [blame] | 763 | |
| 764 | try { |
| 765 | if (Intent.ACTION_USER_ADDED.equals(action)) { |
| 766 | final UserManager um = mContext.getSystemService(UserManager.class); |
| 767 | final int userSerialNumber = um.getUserSerialNumber(userId); |
Jeff Sharkey | 5440279 | 2017-09-15 16:05:19 -0600 | [diff] [blame] | 768 | mVold.onUserAdded(userId, userSerialNumber); |
Jeff Sharkey | bcd262d | 2015-06-10 09:41:17 -0700 | [diff] [blame] | 769 | } else if (Intent.ACTION_USER_REMOVED.equals(action)) { |
Xiaohui Chen | 621b3fc | 2015-10-02 14:41:42 -0700 | [diff] [blame] | 770 | synchronized (mVolumes) { |
| 771 | final int size = mVolumes.size(); |
| 772 | for (int i = 0; i < size; i++) { |
| 773 | final VolumeInfo vol = mVolumes.valueAt(i); |
| 774 | if (vol.mountUserId == userId) { |
| 775 | vol.mountUserId = UserHandle.USER_NULL; |
| 776 | mHandler.obtainMessage(H_VOLUME_UNMOUNT, vol).sendToTarget(); |
| 777 | } |
| 778 | } |
| 779 | } |
Jeff Sharkey | 5440279 | 2017-09-15 16:05:19 -0600 | [diff] [blame] | 780 | mVold.onUserRemoved(userId); |
Jeff Sharkey | bcd262d | 2015-06-10 09:41:17 -0700 | [diff] [blame] | 781 | } |
Jeff Sharkey | ace874b | 2017-09-07 15:27:33 -0600 | [diff] [blame] | 782 | } catch (Exception e) { |
| 783 | Slog.wtf(TAG, e); |
Jeff Sharkey | bcd262d | 2015-06-10 09:41:17 -0700 | [diff] [blame] | 784 | } |
| 785 | } |
| 786 | }; |
| 787 | |
Jeff Sharkey | edcdaf6 | 2015-07-09 09:45:36 -0700 | [diff] [blame] | 788 | private void waitForLatch(CountDownLatch latch, String condition, long timeoutMillis) |
| 789 | throws TimeoutException { |
| 790 | final long startMillis = SystemClock.elapsedRealtime(); |
Jeff Sharkey | eba260d | 2015-04-19 14:35:16 -0700 | [diff] [blame] | 791 | while (true) { |
Kenny Root | 51a573c | 2012-05-17 13:30:28 -0700 | [diff] [blame] | 792 | try { |
| 793 | if (latch.await(5000, TimeUnit.MILLISECONDS)) { |
San Mehat | 207e538 | 2010-02-04 20:46:54 -0800 | [diff] [blame] | 794 | return; |
Kenny Root | 51a573c | 2012-05-17 13:30:28 -0700 | [diff] [blame] | 795 | } else { |
| 796 | Slog.w(TAG, "Thread " + Thread.currentThread().getName() |
Jeff Sharkey | 4887789 | 2015-03-18 11:27:19 -0700 | [diff] [blame] | 797 | + " still waiting for " + condition + "..."); |
San Mehat | 207e538 | 2010-02-04 20:46:54 -0800 | [diff] [blame] | 798 | } |
Kenny Root | 51a573c | 2012-05-17 13:30:28 -0700 | [diff] [blame] | 799 | } catch (InterruptedException e) { |
Jeff Sharkey | eba260d | 2015-04-19 14:35:16 -0700 | [diff] [blame] | 800 | Slog.w(TAG, "Interrupt while waiting for " + condition); |
San Mehat | 207e538 | 2010-02-04 20:46:54 -0800 | [diff] [blame] | 801 | } |
Jeff Sharkey | edcdaf6 | 2015-07-09 09:45:36 -0700 | [diff] [blame] | 802 | if (timeoutMillis > 0 && SystemClock.elapsedRealtime() > startMillis + timeoutMillis) { |
| 803 | throw new TimeoutException("Thread " + Thread.currentThread().getName() |
| 804 | + " gave up waiting for " + condition + " after " + timeoutMillis + "ms"); |
| 805 | } |
San Mehat | 207e538 | 2010-02-04 20:46:54 -0800 | [diff] [blame] | 806 | } |
San Mehat | 1f6301e | 2010-01-07 22:40:27 -0800 | [diff] [blame] | 807 | } |
Kenny Root | 02c8730 | 2010-07-01 08:10:18 -0700 | [diff] [blame] | 808 | |
Jeff Sharkey | b049e21 | 2012-09-07 23:16:01 -0700 | [diff] [blame] | 809 | private void handleSystemReady() { |
Jeff Sharkey | 4887789 | 2015-03-18 11:27:19 -0700 | [diff] [blame] | 810 | // Start scheduling nominally-daily fstrim operations |
Christopher Tate | 115afda | 2014-06-06 19:06:26 -0700 | [diff] [blame] | 811 | MountServiceIdler.scheduleIdlePass(mContext); |
Daniel Colascione | 766b632 | 2018-01-08 19:10:36 -0800 | [diff] [blame] | 812 | |
| 813 | // Toggle zram-enable system property in response to settings |
| 814 | mContext.getContentResolver().registerContentObserver( |
| 815 | Settings.Global.getUriFor(Settings.Global.ZRAM_ENABLED), |
| 816 | false /*notifyForDescendants*/, |
| 817 | new ContentObserver(null /* current thread */) { |
| 818 | @Override |
| 819 | public void onChange(boolean selfChange) { |
| 820 | refreshZramSettings(); |
| 821 | } |
| 822 | }); |
| 823 | refreshZramSettings(); |
Jeff Sharkey | b0c363b2 | 2018-12-15 11:53:03 -0700 | [diff] [blame] | 824 | |
Srinivas Paladugu | c826175 | 2019-01-09 10:54:52 -0800 | [diff] [blame] | 825 | // Schedule zram writeback unless zram is disabled by persist.sys.zram_enabled |
| 826 | String zramPropValue = SystemProperties.get(ZRAM_ENABLED_PROPERTY); |
| 827 | if (!zramPropValue.equals("0") |
| 828 | && mContext.getResources().getBoolean( |
| 829 | com.android.internal.R.bool.config_zramWriteback)) { |
| 830 | ZramWriteback.scheduleZramWriteback(mContext); |
| 831 | } |
Jeff Sharkey | b0c363b2 | 2018-12-15 11:53:03 -0700 | [diff] [blame] | 832 | // Toggle isolated-enable system property in response to settings |
| 833 | mContext.getContentResolver().registerContentObserver( |
| 834 | Settings.Global.getUriFor(Settings.Global.ISOLATED_STORAGE_REMOTE), |
| 835 | false /*notifyForDescendants*/, |
| 836 | new ContentObserver(null /* current thread */) { |
| 837 | @Override |
| 838 | public void onChange(boolean selfChange) { |
| 839 | refreshIsolatedStorageSettings(); |
| 840 | } |
| 841 | }); |
Jeff Sharkey | 5d0c55c | 2019-01-24 14:32:31 -0700 | [diff] [blame] | 842 | // For now, simply clone property when it changes |
shafik | 91b3461 | 2019-09-23 15:41:44 +0100 | [diff] [blame] | 843 | DeviceConfig.addOnPropertiesChangedListener(DeviceConfig.NAMESPACE_STORAGE_NATIVE_BOOT, |
Matt Pape | 15769e2 | 2019-04-19 12:31:24 -0700 | [diff] [blame] | 844 | mContext.getMainExecutor(), (properties) -> { |
Jeff Sharkey | 8b499a0 | 2019-01-28 13:31:50 -0700 | [diff] [blame] | 845 | refreshIsolatedStorageSettings(); |
shafik | b2992b6 | 2019-10-01 15:31:02 +0100 | [diff] [blame] | 846 | refreshFuseSettings(); |
Jeff Sharkey | 5d0c55c | 2019-01-24 14:32:31 -0700 | [diff] [blame] | 847 | }); |
Jeff Sharkey | b0c363b2 | 2018-12-15 11:53:03 -0700 | [diff] [blame] | 848 | refreshIsolatedStorageSettings(); |
Daniel Colascione | 766b632 | 2018-01-08 19:10:36 -0800 | [diff] [blame] | 849 | } |
| 850 | |
| 851 | /** |
| 852 | * Update the zram_enabled system property (which init reads to |
| 853 | * decide whether to enable zram) to reflect the zram_enabled |
| 854 | * preference (which we can change for experimentation purposes). |
| 855 | */ |
| 856 | private void refreshZramSettings() { |
| 857 | String propertyValue = SystemProperties.get(ZRAM_ENABLED_PROPERTY); |
| 858 | if ("".equals(propertyValue)) { |
| 859 | return; // System doesn't have zram toggling support |
| 860 | } |
| 861 | String desiredPropertyValue = |
| 862 | Settings.Global.getInt(mContext.getContentResolver(), |
| 863 | Settings.Global.ZRAM_ENABLED, |
| 864 | 1) != 0 |
| 865 | ? "1" : "0"; |
| 866 | if (!desiredPropertyValue.equals(propertyValue)) { |
| 867 | // Avoid redundant disk writes by setting only if we're |
| 868 | // changing the property value. There's no race: we're the |
| 869 | // sole writer. |
| 870 | SystemProperties.set(ZRAM_ENABLED_PROPERTY, desiredPropertyValue); |
Srinivas Paladugu | c826175 | 2019-01-09 10:54:52 -0800 | [diff] [blame] | 871 | // Schedule writeback only if zram is being enabled. |
| 872 | if (desiredPropertyValue.equals("1") |
| 873 | && mContext.getResources().getBoolean( |
| 874 | com.android.internal.R.bool.config_zramWriteback)) { |
| 875 | ZramWriteback.scheduleZramWriteback(mContext); |
| 876 | } |
Daniel Colascione | 766b632 | 2018-01-08 19:10:36 -0800 | [diff] [blame] | 877 | } |
Jeff Sharkey | b049e21 | 2012-09-07 23:16:01 -0700 | [diff] [blame] | 878 | } |
| 879 | |
Jeff Sharkey | b0c363b2 | 2018-12-15 11:53:03 -0700 | [diff] [blame] | 880 | private void refreshIsolatedStorageSettings() { |
Jeff Sharkey | 8b499a0 | 2019-01-28 13:31:50 -0700 | [diff] [blame] | 881 | // Always copy value from newer DeviceConfig location |
| 882 | Settings.Global.putString(mResolver, |
| 883 | Settings.Global.ISOLATED_STORAGE_REMOTE, |
shafik | 91b3461 | 2019-09-23 15:41:44 +0100 | [diff] [blame] | 884 | DeviceConfig.getProperty(DeviceConfig.NAMESPACE_STORAGE_NATIVE_BOOT, |
| 885 | ISOLATED_STORAGE_ENABLED)); |
Jeff Sharkey | 8b499a0 | 2019-01-28 13:31:50 -0700 | [diff] [blame] | 886 | |
Jeff Sharkey | b0c363b2 | 2018-12-15 11:53:03 -0700 | [diff] [blame] | 887 | final int local = Settings.Global.getInt(mContext.getContentResolver(), |
| 888 | Settings.Global.ISOLATED_STORAGE_LOCAL, 0); |
| 889 | final int remote = Settings.Global.getInt(mContext.getContentResolver(), |
| 890 | Settings.Global.ISOLATED_STORAGE_REMOTE, 0); |
| 891 | |
| 892 | // Walk down precedence chain; we prefer local settings first, then |
| 893 | // remote settings, before finally falling back to hard-coded default. |
| 894 | final boolean res; |
| 895 | if (local == -1) { |
| 896 | res = false; |
| 897 | } else if (local == 1) { |
| 898 | res = true; |
| 899 | } else if (remote == -1) { |
| 900 | res = false; |
| 901 | } else if (remote == 1) { |
| 902 | res = true; |
| 903 | } else { |
Jeff Sharkey | 0637680 | 2019-02-11 12:20:02 -0700 | [diff] [blame] | 904 | res = true; |
Jeff Sharkey | b0c363b2 | 2018-12-15 11:53:03 -0700 | [diff] [blame] | 905 | } |
| 906 | |
| 907 | Slog.d(TAG, "Isolated storage local flag " + local + " and remote flag " |
| 908 | + remote + " resolved to " + res); |
| 909 | SystemProperties.set(StorageManager.PROP_ISOLATED_STORAGE, Boolean.toString(res)); |
| 910 | } |
| 911 | |
Abhijeet Kaur | b984771 | 2019-11-19 16:35:47 +0000 | [diff] [blame] | 912 | /** |
| 913 | * The most recent flag change takes precedence. Change fuse Settings flag if Device Config is |
| 914 | * changed. Settings flag change will in turn change fuse system property (persist.sys.fuse) |
| 915 | * whenever the user reboots. |
| 916 | */ |
shafik | b2992b6 | 2019-10-01 15:31:02 +0100 | [diff] [blame] | 917 | private void refreshFuseSettings() { |
| 918 | int isFuseEnabled = DeviceConfig.getInt(DeviceConfig.NAMESPACE_STORAGE_NATIVE_BOOT, |
| 919 | FUSE_ENABLED, 0); |
| 920 | if (isFuseEnabled == 1) { |
Abhijeet Kaur | b984771 | 2019-11-19 16:35:47 +0000 | [diff] [blame] | 921 | Slog.d(TAG, "Device Config flag for FUSE is enabled, turn Settings fuse flag on"); |
| 922 | SystemProperties.set(FeatureFlagUtils.PERSIST_PREFIX |
| 923 | + FeatureFlagUtils.SETTINGS_FUSE_FLAG, "true"); |
shafik | b2992b6 | 2019-10-01 15:31:02 +0100 | [diff] [blame] | 924 | } else if (isFuseEnabled == -1) { |
Abhijeet Kaur | b984771 | 2019-11-19 16:35:47 +0000 | [diff] [blame] | 925 | Slog.d(TAG, "Device Config flag for FUSE is disabled, turn Settings fuse flag off"); |
| 926 | SystemProperties.set(FeatureFlagUtils.PERSIST_PREFIX |
| 927 | + FeatureFlagUtils.SETTINGS_FUSE_FLAG, "false"); |
shafik | b2992b6 | 2019-10-01 15:31:02 +0100 | [diff] [blame] | 928 | } |
| 929 | // else, keep the build config. |
Abhijeet Kaur | b984771 | 2019-11-19 16:35:47 +0000 | [diff] [blame] | 930 | // This can be overridden by direct adjustment of persist.sys.fflag.override.settings_fuse |
shafik | b2992b6 | 2019-10-01 15:31:02 +0100 | [diff] [blame] | 931 | } |
| 932 | |
Jeff Sharkey | 14cbe52 | 2015-07-08 14:06:37 -0700 | [diff] [blame] | 933 | /** |
| 934 | * MediaProvider has a ton of code that makes assumptions about storage |
| 935 | * paths never changing, so we outright kill them to pick up new state. |
| 936 | */ |
| 937 | @Deprecated |
Xiaohui Chen | 621b3fc | 2015-10-02 14:41:42 -0700 | [diff] [blame] | 938 | private void killMediaProvider(List<UserInfo> users) { |
| 939 | if (users == null) return; |
| 940 | |
Jeff Sharkey | b3cf953 | 2015-07-17 15:12:39 -0700 | [diff] [blame] | 941 | final long token = Binder.clearCallingIdentity(); |
| 942 | try { |
Xiaohui Chen | 621b3fc | 2015-10-02 14:41:42 -0700 | [diff] [blame] | 943 | for (UserInfo user : users) { |
| 944 | // System user does not have media provider, so skip. |
| 945 | if (user.isSystemOnly()) continue; |
| 946 | |
Jeff Sharkey | 5790af0 | 2018-08-13 17:42:54 -0600 | [diff] [blame] | 947 | final ProviderInfo provider = mPmInternal.resolveContentProvider( |
| 948 | MediaStore.AUTHORITY, PackageManager.MATCH_DIRECT_BOOT_AWARE |
Jeff Sharkey | 8a372a0 | 2016-03-16 16:25:45 -0600 | [diff] [blame] | 949 | | PackageManager.MATCH_DIRECT_BOOT_UNAWARE, |
| 950 | user.id); |
Xiaohui Chen | 621b3fc | 2015-10-02 14:41:42 -0700 | [diff] [blame] | 951 | if (provider != null) { |
Sudheer Shanka | dc589ac | 2016-11-10 15:30:17 -0800 | [diff] [blame] | 952 | final IActivityManager am = ActivityManager.getService(); |
Xiaohui Chen | 621b3fc | 2015-10-02 14:41:42 -0700 | [diff] [blame] | 953 | try { |
Jeff Sharkey | 85f449e | 2016-06-23 09:26:00 -0600 | [diff] [blame] | 954 | am.killApplication(provider.applicationInfo.packageName, |
| 955 | UserHandle.getAppId(provider.applicationInfo.uid), |
| 956 | UserHandle.USER_ALL, "vold reset"); |
Xiaohui Chen | 621b3fc | 2015-10-02 14:41:42 -0700 | [diff] [blame] | 957 | // We only need to run this once. It will kill all users' media processes. |
| 958 | break; |
| 959 | } catch (RemoteException e) { |
| 960 | } |
Jeff Sharkey | b3cf953 | 2015-07-17 15:12:39 -0700 | [diff] [blame] | 961 | } |
Jeff Sharkey | 14cbe52 | 2015-07-08 14:06:37 -0700 | [diff] [blame] | 962 | } |
Jeff Sharkey | b3cf953 | 2015-07-17 15:12:39 -0700 | [diff] [blame] | 963 | } finally { |
| 964 | Binder.restoreCallingIdentity(token); |
Jeff Sharkey | 14cbe52 | 2015-07-08 14:06:37 -0700 | [diff] [blame] | 965 | } |
| 966 | } |
| 967 | |
Andreas Gampe | a36dc62 | 2018-02-05 17:19:22 -0800 | [diff] [blame] | 968 | @GuardedBy("mLock") |
Jeff Sharkey | 5a9bb74 | 2015-11-03 10:15:57 -0800 | [diff] [blame] | 969 | private void addInternalVolumeLocked() { |
Amith Yamasani | a789248 | 2015-08-07 11:09:05 -0700 | [diff] [blame] | 970 | // Create a stub volume that represents internal storage |
| 971 | final VolumeInfo internal = new VolumeInfo(VolumeInfo.ID_PRIVATE_INTERNAL, |
| 972 | VolumeInfo.TYPE_PRIVATE, null, null); |
| 973 | internal.state = VolumeInfo.STATE_MOUNTED; |
| 974 | internal.path = Environment.getDataDirectory().getAbsolutePath(); |
| 975 | mVolumes.put(internal.id, internal); |
| 976 | } |
| 977 | |
Daniel Rosenberg | 137aed1 | 2019-03-15 18:41:11 -0700 | [diff] [blame] | 978 | private void initIfBootedAndConnected() { |
| 979 | Slog.d(TAG, "Thinking about init, mBootCompleted=" + mBootCompleted |
Jeff Sharkey | 8924e87 | 2015-11-30 12:52:10 -0700 | [diff] [blame] | 980 | + ", mDaemonConnected=" + mDaemonConnected); |
Eric Biggers | 1127d92 | 2019-04-05 12:46:35 -0700 | [diff] [blame] | 981 | if (mBootCompleted && mDaemonConnected |
| 982 | && !StorageManager.isFileEncryptedNativeOnly()) { |
| 983 | // When booting a device without native support, make sure that our |
| 984 | // user directories are locked or unlocked based on the current |
| 985 | // emulation status. |
| 986 | final boolean initLocked = StorageManager.isFileEncryptedEmulatedOnly(); |
| 987 | Slog.d(TAG, "Setting up emulation state, initlocked=" + initLocked); |
Jeff Sharkey | ce14cd0 | 2015-12-07 15:35:42 -0700 | [diff] [blame] | 988 | final List<UserInfo> users = mContext.getSystemService(UserManager.class).getUsers(); |
Jeff Sharkey | 8924e87 | 2015-11-30 12:52:10 -0700 | [diff] [blame] | 989 | for (UserInfo user : users) { |
| 990 | try { |
Jeff Sharkey | ce14cd0 | 2015-12-07 15:35:42 -0700 | [diff] [blame] | 991 | if (initLocked) { |
Jeff Sharkey | 5440279 | 2017-09-15 16:05:19 -0600 | [diff] [blame] | 992 | mVold.lockUserKey(user.id); |
Jeff Sharkey | ce14cd0 | 2015-12-07 15:35:42 -0700 | [diff] [blame] | 993 | } else { |
Jeff Sharkey | 5440279 | 2017-09-15 16:05:19 -0600 | [diff] [blame] | 994 | mVold.unlockUserKey(user.id, user.serialNumber, encodeBytes(null), |
| 995 | encodeBytes(null)); |
Jeff Sharkey | ce14cd0 | 2015-12-07 15:35:42 -0700 | [diff] [blame] | 996 | } |
Jeff Sharkey | 43e1211 | 2017-09-12 16:31:45 -0600 | [diff] [blame] | 997 | } catch (Exception e) { |
| 998 | Slog.wtf(TAG, e); |
Jeff Sharkey | 8924e87 | 2015-11-30 12:52:10 -0700 | [diff] [blame] | 999 | } |
| 1000 | } |
| 1001 | } |
| 1002 | } |
| 1003 | |
Daniel Rosenberg | 137aed1 | 2019-03-15 18:41:11 -0700 | [diff] [blame] | 1004 | private void resetIfBootedAndConnected() { |
| 1005 | Slog.d(TAG, "Thinking about reset, mBootCompleted=" + mBootCompleted |
Jeff Sharkey | 4887789 | 2015-03-18 11:27:19 -0700 | [diff] [blame] | 1006 | + ", mDaemonConnected=" + mDaemonConnected); |
Daniel Rosenberg | 137aed1 | 2019-03-15 18:41:11 -0700 | [diff] [blame] | 1007 | if (mBootCompleted && mDaemonConnected) { |
Jeff Sharkey | 5a9bb74 | 2015-11-03 10:15:57 -0800 | [diff] [blame] | 1008 | final List<UserInfo> users = mContext.getSystemService(UserManager.class).getUsers(); |
Zim | 17be6f9 | 2019-09-25 14:37:55 +0100 | [diff] [blame] | 1009 | |
| 1010 | if (mIsFuseEnabled) { |
| 1011 | mStorageSessionController.onReset(mVold, mHandler); |
| 1012 | } else { |
| 1013 | killMediaProvider(users); |
| 1014 | } |
Jeff Sharkey | 14cbe52 | 2015-07-08 14:06:37 -0700 | [diff] [blame] | 1015 | |
Jeff Sharkey | ce14cd0 | 2015-12-07 15:35:42 -0700 | [diff] [blame] | 1016 | final int[] systemUnlockedUsers; |
Jeff Sharkey | 5a9bb74 | 2015-11-03 10:15:57 -0800 | [diff] [blame] | 1017 | synchronized (mLock) { |
Jeff Sharkey | ce14cd0 | 2015-12-07 15:35:42 -0700 | [diff] [blame] | 1018 | systemUnlockedUsers = mSystemUnlockedUsers; |
Jeff Sharkey | b049e21 | 2012-09-07 23:16:01 -0700 | [diff] [blame] | 1019 | |
Jeff Sharkey | 5a9bb74 | 2015-11-03 10:15:57 -0800 | [diff] [blame] | 1020 | mDisks.clear(); |
| 1021 | mVolumes.clear(); |
| 1022 | |
| 1023 | addInternalVolumeLocked(); |
| 1024 | } |
Jeff Sharkey | 59d577a | 2015-04-11 21:27:21 -0700 | [diff] [blame] | 1025 | |
Jeff Sharkey | 4887789 | 2015-03-18 11:27:19 -0700 | [diff] [blame] | 1026 | try { |
Zim | 42f1e9f | 2019-08-15 17:35:00 +0100 | [diff] [blame] | 1027 | // TODO(b/135341433): Remove paranoid logging when FUSE is stable |
Zim | 17be6f9 | 2019-09-25 14:37:55 +0100 | [diff] [blame] | 1028 | Slog.i(TAG, "Resetting vold..."); |
Jeff Sharkey | 5440279 | 2017-09-15 16:05:19 -0600 | [diff] [blame] | 1029 | mVold.reset(); |
Zim | 42f1e9f | 2019-08-15 17:35:00 +0100 | [diff] [blame] | 1030 | Slog.i(TAG, "Reset vold"); |
Jeff Sharkey | bcd262d | 2015-06-10 09:41:17 -0700 | [diff] [blame] | 1031 | |
| 1032 | // Tell vold about all existing and started users |
Jeff Sharkey | bcd262d | 2015-06-10 09:41:17 -0700 | [diff] [blame] | 1033 | for (UserInfo user : users) { |
Jeff Sharkey | 5440279 | 2017-09-15 16:05:19 -0600 | [diff] [blame] | 1034 | mVold.onUserAdded(user.id, user.serialNumber); |
Jeff Sharkey | bcd262d | 2015-06-10 09:41:17 -0700 | [diff] [blame] | 1035 | } |
Jeff Sharkey | ce14cd0 | 2015-12-07 15:35:42 -0700 | [diff] [blame] | 1036 | for (int userId : systemUnlockedUsers) { |
Sudheer Shanka | 64501e5 | 2019-04-29 10:46:26 -0700 | [diff] [blame] | 1037 | mVold.onUserStarted(userId); |
Jin Qian | 12690d5 | 2017-10-13 18:17:04 -0700 | [diff] [blame] | 1038 | mStoraged.onUserStarted(userId); |
Jeff Sharkey | 50a0545 | 2015-04-29 11:24:52 -0700 | [diff] [blame] | 1039 | } |
Jeff Sharkey | 9765e44 | 2017-12-14 22:15:14 -0700 | [diff] [blame] | 1040 | mVold.onSecureKeyguardStateChanged(mSecureKeyguardShowing); |
Risan | aec0ee7 | 2018-10-31 10:10:12 +0900 | [diff] [blame] | 1041 | mStorageManagerInternal.onReset(mVold); |
Jeff Sharkey | ace874b | 2017-09-07 15:27:33 -0600 | [diff] [blame] | 1042 | } catch (Exception e) { |
| 1043 | Slog.wtf(TAG, e); |
Jeff Sharkey | b049e21 | 2012-09-07 23:16:01 -0700 | [diff] [blame] | 1044 | } |
| 1045 | } |
Jeff Sharkey | 4887789 | 2015-03-18 11:27:19 -0700 | [diff] [blame] | 1046 | } |
Jeff Sharkey | b049e21 | 2012-09-07 23:16:01 -0700 | [diff] [blame] | 1047 | |
Jeff Sharkey | ce14cd0 | 2015-12-07 15:35:42 -0700 | [diff] [blame] | 1048 | private void onUnlockUser(int userId) { |
| 1049 | Slog.d(TAG, "onUnlockUser " + userId); |
Jeff Sharkey | 4887789 | 2015-03-18 11:27:19 -0700 | [diff] [blame] | 1050 | |
| 1051 | // We purposefully block here to make sure that user-specific |
| 1052 | // staging area is ready so it's ready for zygote-forked apps to |
| 1053 | // bind mount against. |
| 1054 | try { |
Zim | 17be6f9 | 2019-09-25 14:37:55 +0100 | [diff] [blame] | 1055 | mStorageSessionController.onUnlockUser(userId); |
Sudheer Shanka | 64501e5 | 2019-04-29 10:46:26 -0700 | [diff] [blame] | 1056 | mVold.onUserStarted(userId); |
Jin Qian | 12690d5 | 2017-10-13 18:17:04 -0700 | [diff] [blame] | 1057 | mStoraged.onUserStarted(userId); |
Jeff Sharkey | ace874b | 2017-09-07 15:27:33 -0600 | [diff] [blame] | 1058 | } catch (Exception e) { |
| 1059 | Slog.wtf(TAG, e); |
Jeff Sharkey | b049e21 | 2012-09-07 23:16:01 -0700 | [diff] [blame] | 1060 | } |
Jeff Sharkey | 4887789 | 2015-03-18 11:27:19 -0700 | [diff] [blame] | 1061 | |
Narayan Kamath | 157dd1d | 2019-06-12 13:06:30 +0100 | [diff] [blame] | 1062 | mHandler.obtainMessage(H_COMPLETE_UNLOCK_USER, userId).sendToTarget(); |
| 1063 | } |
| 1064 | |
| 1065 | private void completeUnlockUser(int userId) { |
| 1066 | // If user 0 has completed unlock, perform a one-time migration of legacy obb data |
| 1067 | // to its new location. This may take time depending on the size of the data to be copied |
| 1068 | // so it's done on the StorageManager handler thread. |
| 1069 | if (userId == 0) { |
| 1070 | mPmInternal.migrateLegacyObbData(); |
| 1071 | } |
| 1072 | |
Jeff Sharkey | 4887789 | 2015-03-18 11:27:19 -0700 | [diff] [blame] | 1073 | // Record user as started so newly mounted volumes kick off events |
| 1074 | // correctly, then synthesize events for any already-mounted volumes. |
yuanhuihui | efd1f12 | 2016-07-13 21:21:03 +0800 | [diff] [blame] | 1075 | synchronized (mLock) { |
Jeff Sharkey | 1b8ef7e | 2015-04-03 17:14:45 -0700 | [diff] [blame] | 1076 | for (int i = 0; i < mVolumes.size(); i++) { |
| 1077 | final VolumeInfo vol = mVolumes.valueAt(i); |
Jeff Sharkey | 4634987 | 2015-07-28 10:49:47 -0700 | [diff] [blame] | 1078 | if (vol.isVisibleForRead(userId) && vol.isMountedReadable()) { |
Svet Ganov | 6ee871e | 2015-07-10 14:29:33 -0700 | [diff] [blame] | 1079 | final StorageVolume userVol = vol.buildStorageVolume(mContext, userId, false); |
Jeff Sharkey | 4887789 | 2015-03-18 11:27:19 -0700 | [diff] [blame] | 1080 | mHandler.obtainMessage(H_VOLUME_BROADCAST, userVol).sendToTarget(); |
Jeff Sharkey | 7151a9a | 2015-04-04 15:22:37 -0700 | [diff] [blame] | 1081 | |
Jeff Sharkey | 27de30d | 2015-04-18 16:20:27 -0700 | [diff] [blame] | 1082 | final String envState = VolumeInfo.getEnvironmentForState(vol.getState()); |
| 1083 | mCallbacks.notifyStorageStateChanged(userVol.getPath(), envState, envState); |
Jeff Sharkey | 4887789 | 2015-03-18 11:27:19 -0700 | [diff] [blame] | 1084 | } |
| 1085 | } |
Jeff Sharkey | ce14cd0 | 2015-12-07 15:35:42 -0700 | [diff] [blame] | 1086 | mSystemUnlockedUsers = ArrayUtils.appendInt(mSystemUnlockedUsers, userId); |
Jeff Sharkey | 4887789 | 2015-03-18 11:27:19 -0700 | [diff] [blame] | 1087 | } |
| 1088 | } |
| 1089 | |
| 1090 | private void onCleanupUser(int userId) { |
| 1091 | Slog.d(TAG, "onCleanupUser " + userId); |
| 1092 | |
| 1093 | try { |
Jeff Sharkey | 5440279 | 2017-09-15 16:05:19 -0600 | [diff] [blame] | 1094 | mVold.onUserStopped(userId); |
Jin Qian | 12690d5 | 2017-10-13 18:17:04 -0700 | [diff] [blame] | 1095 | mStoraged.onUserStopped(userId); |
Jeff Sharkey | ace874b | 2017-09-07 15:27:33 -0600 | [diff] [blame] | 1096 | } catch (Exception e) { |
| 1097 | Slog.wtf(TAG, e); |
Jeff Sharkey | 4887789 | 2015-03-18 11:27:19 -0700 | [diff] [blame] | 1098 | } |
| 1099 | |
yuanhuihui | efd1f12 | 2016-07-13 21:21:03 +0800 | [diff] [blame] | 1100 | synchronized (mLock) { |
Jeff Sharkey | ce14cd0 | 2015-12-07 15:35:42 -0700 | [diff] [blame] | 1101 | mSystemUnlockedUsers = ArrayUtils.removeInt(mSystemUnlockedUsers, userId); |
Jeff Sharkey | 4887789 | 2015-03-18 11:27:19 -0700 | [diff] [blame] | 1102 | } |
| 1103 | } |
Jeff Sharkey | b049e21 | 2012-09-07 23:16:01 -0700 | [diff] [blame] | 1104 | |
Martijn Coenen | 7084c6a | 2019-11-22 16:32:09 +0100 | [diff] [blame] | 1105 | private void onStopUser(int userId) { |
| 1106 | Slog.i(TAG, "onStopUser " + userId); |
| 1107 | try { |
| 1108 | mStorageSessionController.onUserStopping(userId); |
| 1109 | } catch (Exception e) { |
| 1110 | Slog.wtf(TAG, e); |
| 1111 | } |
| 1112 | } |
| 1113 | |
Sudheer Shanka | 64501e5 | 2019-04-29 10:46:26 -0700 | [diff] [blame] | 1114 | private boolean supportsBlockCheckpoint() throws RemoteException { |
Paul Lawrence | 15a5446 | 2019-04-30 11:14:25 -0700 | [diff] [blame] | 1115 | enforcePermission(android.Manifest.permission.MOUNT_FORMAT_FILESYSTEMS); |
Sandeep Patil | 7d5c491 | 2019-04-15 09:27:30 -0700 | [diff] [blame] | 1116 | return mVold.supportsBlockCheckpoint(); |
| 1117 | } |
| 1118 | |
Jeff Sharkey | 9765e44 | 2017-12-14 22:15:14 -0700 | [diff] [blame] | 1119 | @Override |
| 1120 | public void onAwakeStateChanged(boolean isAwake) { |
| 1121 | // Ignored |
| 1122 | } |
| 1123 | |
| 1124 | @Override |
| 1125 | public void onKeyguardStateChanged(boolean isShowing) { |
| 1126 | // Push down current secure keyguard status so that we ignore malicious |
| 1127 | // USB devices while locked. |
| 1128 | mSecureKeyguardShowing = isShowing |
Martijn Coenen | 7c1df2d | 2020-01-07 10:21:42 +0100 | [diff] [blame] | 1129 | && mContext.getSystemService(KeyguardManager.class).isDeviceSecure(mCurrentUserId); |
Jeff Sharkey | 9765e44 | 2017-12-14 22:15:14 -0700 | [diff] [blame] | 1130 | try { |
| 1131 | mVold.onSecureKeyguardStateChanged(mSecureKeyguardShowing); |
| 1132 | } catch (Exception e) { |
| 1133 | Slog.wtf(TAG, e); |
| 1134 | } |
| 1135 | } |
| 1136 | |
Christopher Tate | d417d62 | 2013-08-19 16:14:25 -0700 | [diff] [blame] | 1137 | void runIdleMaintenance(Runnable callback) { |
| 1138 | mHandler.sendMessage(mHandler.obtainMessage(H_FSTRIM, callback)); |
| 1139 | } |
| 1140 | |
Christopher Tate | 7265abe | 2014-11-21 13:54:45 -0800 | [diff] [blame] | 1141 | // Binder entry point for kicking off an immediate fstrim |
| 1142 | @Override |
| 1143 | public void runMaintenance() { |
Jeff Sharkey | 4887789 | 2015-03-18 11:27:19 -0700 | [diff] [blame] | 1144 | enforcePermission(android.Manifest.permission.MOUNT_UNMOUNT_FILESYSTEMS); |
Christopher Tate | 7265abe | 2014-11-21 13:54:45 -0800 | [diff] [blame] | 1145 | runIdleMaintenance(null); |
| 1146 | } |
| 1147 | |
| 1148 | @Override |
| 1149 | public long lastMaintenance() { |
| 1150 | return mLastMaintenance; |
| 1151 | } |
| 1152 | |
San Mehat | 4270e1e | 2010-01-29 05:32:19 -0800 | [diff] [blame] | 1153 | public void onDaemonConnected() { |
Jeff Sharkey | 4887789 | 2015-03-18 11:27:19 -0700 | [diff] [blame] | 1154 | mDaemonConnected = true; |
| 1155 | mHandler.obtainMessage(H_DAEMON_CONNECTED).sendToTarget(); |
| 1156 | } |
| 1157 | |
| 1158 | private void handleDaemonConnected() { |
Daniel Rosenberg | 137aed1 | 2019-03-15 18:41:11 -0700 | [diff] [blame] | 1159 | initIfBootedAndConnected(); |
| 1160 | resetIfBootedAndConnected(); |
Jeff Sharkey | 4887789 | 2015-03-18 11:27:19 -0700 | [diff] [blame] | 1161 | |
Jeff Sharkey | 4887789 | 2015-03-18 11:27:19 -0700 | [diff] [blame] | 1162 | // On an encrypted device we can't see system properties yet, so pull |
| 1163 | // the system locale out of the mount service. |
Inseob Kim | c1246e6 | 2018-11-08 13:13:54 +0900 | [diff] [blame] | 1164 | if ("".equals(VoldProperties.encrypt_progress().orElse(""))) { |
Jeff Sharkey | 4887789 | 2015-03-18 11:27:19 -0700 | [diff] [blame] | 1165 | copyLocaleFromMountService(); |
| 1166 | } |
San Mehat | 4270e1e | 2010-01-29 05:32:19 -0800 | [diff] [blame] | 1167 | } |
| 1168 | |
Elliott Hughes | f839b4f | 2014-09-26 12:30:47 -0700 | [diff] [blame] | 1169 | private void copyLocaleFromMountService() { |
| 1170 | String systemLocale; |
| 1171 | try { |
| 1172 | systemLocale = getField(StorageManager.SYSTEM_LOCALE_KEY); |
| 1173 | } catch (RemoteException e) { |
| 1174 | return; |
| 1175 | } |
| 1176 | if (TextUtils.isEmpty(systemLocale)) { |
| 1177 | return; |
| 1178 | } |
| 1179 | |
| 1180 | Slog.d(TAG, "Got locale " + systemLocale + " from mount service"); |
| 1181 | Locale locale = Locale.forLanguageTag(systemLocale); |
| 1182 | Configuration config = new Configuration(); |
| 1183 | config.setLocale(locale); |
| 1184 | try { |
Sudheer Shanka | dc589ac | 2016-11-10 15:30:17 -0800 | [diff] [blame] | 1185 | ActivityManager.getService().updatePersistentConfiguration(config); |
Elliott Hughes | f839b4f | 2014-09-26 12:30:47 -0700 | [diff] [blame] | 1186 | } catch (RemoteException e) { |
| 1187 | Slog.e(TAG, "Error setting system locale from mount service", e); |
| 1188 | } |
Elliott Hughes | 9c33f28 | 2014-10-13 12:39:56 -0700 | [diff] [blame] | 1189 | |
| 1190 | // Temporary workaround for http://b/17945169. |
| 1191 | Slog.d(TAG, "Setting system properties to " + systemLocale + " from mount service"); |
Narayan Kamath | d30dbb8 | 2015-01-15 14:48:15 +0000 | [diff] [blame] | 1192 | SystemProperties.set("persist.sys.locale", locale.toLanguageTag()); |
Elliott Hughes | f839b4f | 2014-09-26 12:30:47 -0700 | [diff] [blame] | 1193 | } |
| 1194 | |
Jeff Sharkey | 8058fe6 | 2017-09-13 11:50:33 -0600 | [diff] [blame] | 1195 | private final IVoldListener mListener = new IVoldListener.Stub() { |
| 1196 | @Override |
| 1197 | public void onDiskCreated(String diskId, int flags) { |
| 1198 | synchronized (mLock) { |
Jeff Sharkey | 901c042 | 2018-04-20 13:11:20 -0600 | [diff] [blame] | 1199 | final String value = SystemProperties.get(StorageManager.PROP_ADOPTABLE); |
| 1200 | switch (value) { |
| 1201 | case "force_on": |
| 1202 | flags |= DiskInfo.FLAG_ADOPTABLE; |
| 1203 | break; |
| 1204 | case "force_off": |
| 1205 | flags &= ~DiskInfo.FLAG_ADOPTABLE; |
| 1206 | break; |
Jeff Sharkey | 8058fe6 | 2017-09-13 11:50:33 -0600 | [diff] [blame] | 1207 | } |
Jeff Sharkey | 8058fe6 | 2017-09-13 11:50:33 -0600 | [diff] [blame] | 1208 | mDisks.put(diskId, new DiskInfo(diskId, flags)); |
| 1209 | } |
| 1210 | } |
| 1211 | |
| 1212 | @Override |
| 1213 | public void onDiskScanned(String diskId) { |
| 1214 | synchronized (mLock) { |
| 1215 | final DiskInfo disk = mDisks.get(diskId); |
| 1216 | if (disk != null) { |
| 1217 | onDiskScannedLocked(disk); |
| 1218 | } |
| 1219 | } |
| 1220 | } |
| 1221 | |
| 1222 | @Override |
| 1223 | public void onDiskMetadataChanged(String diskId, long sizeBytes, String label, |
| 1224 | String sysPath) { |
| 1225 | synchronized (mLock) { |
| 1226 | final DiskInfo disk = mDisks.get(diskId); |
| 1227 | if (disk != null) { |
| 1228 | disk.size = sizeBytes; |
| 1229 | disk.label = label; |
| 1230 | disk.sysPath = sysPath; |
| 1231 | } |
| 1232 | } |
| 1233 | } |
| 1234 | |
| 1235 | @Override |
| 1236 | public void onDiskDestroyed(String diskId) { |
| 1237 | synchronized (mLock) { |
| 1238 | final DiskInfo disk = mDisks.remove(diskId); |
| 1239 | if (disk != null) { |
| 1240 | mCallbacks.notifyDiskDestroyed(disk); |
| 1241 | } |
| 1242 | } |
| 1243 | } |
| 1244 | |
| 1245 | @Override |
Zim | 17be6f9 | 2019-09-25 14:37:55 +0100 | [diff] [blame] | 1246 | public void onVolumeCreated(String volId, int type, String diskId, String partGuid, |
| 1247 | int userId) { |
Jeff Sharkey | 8058fe6 | 2017-09-13 11:50:33 -0600 | [diff] [blame] | 1248 | synchronized (mLock) { |
| 1249 | final DiskInfo disk = mDisks.get(diskId); |
| 1250 | final VolumeInfo vol = new VolumeInfo(volId, type, disk, partGuid); |
Zim | 17be6f9 | 2019-09-25 14:37:55 +0100 | [diff] [blame] | 1251 | vol.mountUserId = userId; |
Jeff Sharkey | 8058fe6 | 2017-09-13 11:50:33 -0600 | [diff] [blame] | 1252 | mVolumes.put(volId, vol); |
| 1253 | onVolumeCreatedLocked(vol); |
| 1254 | } |
| 1255 | } |
| 1256 | |
| 1257 | @Override |
| 1258 | public void onVolumeStateChanged(String volId, int state) { |
| 1259 | synchronized (mLock) { |
| 1260 | final VolumeInfo vol = mVolumes.get(volId); |
| 1261 | if (vol != null) { |
| 1262 | final int oldState = vol.state; |
| 1263 | final int newState = state; |
| 1264 | vol.state = newState; |
| 1265 | onVolumeStateChangedLocked(vol, oldState, newState); |
| 1266 | } |
Ricky Wai | 4482ab5 | 2019-12-10 19:08:18 +0000 | [diff] [blame] | 1267 | try { |
| 1268 | if (vol.type == VolumeInfo.TYPE_PRIVATE && state == VolumeInfo.STATE_MOUNTED) { |
| 1269 | mInstaller.onPrivateVolumeMounted(vol.getFsUuid()); |
| 1270 | } |
| 1271 | } catch (Installer.InstallerException e) { |
| 1272 | Slog.i(TAG, "Failed when private volume mounted " + vol, e); |
| 1273 | } |
Jeff Sharkey | 8058fe6 | 2017-09-13 11:50:33 -0600 | [diff] [blame] | 1274 | } |
| 1275 | } |
| 1276 | |
| 1277 | @Override |
| 1278 | public void onVolumeMetadataChanged(String volId, String fsType, String fsUuid, |
| 1279 | String fsLabel) { |
| 1280 | synchronized (mLock) { |
| 1281 | final VolumeInfo vol = mVolumes.get(volId); |
| 1282 | if (vol != null) { |
| 1283 | vol.fsType = fsType; |
| 1284 | vol.fsUuid = fsUuid; |
| 1285 | vol.fsLabel = fsLabel; |
| 1286 | } |
| 1287 | } |
| 1288 | } |
| 1289 | |
| 1290 | @Override |
| 1291 | public void onVolumePathChanged(String volId, String path) { |
| 1292 | synchronized (mLock) { |
| 1293 | final VolumeInfo vol = mVolumes.get(volId); |
| 1294 | if (vol != null) { |
| 1295 | vol.path = path; |
| 1296 | } |
| 1297 | } |
| 1298 | } |
| 1299 | |
| 1300 | @Override |
| 1301 | public void onVolumeInternalPathChanged(String volId, String internalPath) { |
| 1302 | synchronized (mLock) { |
| 1303 | final VolumeInfo vol = mVolumes.get(volId); |
| 1304 | if (vol != null) { |
| 1305 | vol.internalPath = internalPath; |
| 1306 | } |
| 1307 | } |
| 1308 | } |
| 1309 | |
| 1310 | @Override |
| 1311 | public void onVolumeDestroyed(String volId) { |
Zim | 17be6f9 | 2019-09-25 14:37:55 +0100 | [diff] [blame] | 1312 | VolumeInfo vol = null; |
Jeff Sharkey | 8058fe6 | 2017-09-13 11:50:33 -0600 | [diff] [blame] | 1313 | synchronized (mLock) { |
Zim | 17be6f9 | 2019-09-25 14:37:55 +0100 | [diff] [blame] | 1314 | vol = mVolumes.remove(volId); |
| 1315 | } |
| 1316 | |
| 1317 | if (vol != null) { |
| 1318 | mStorageSessionController.onVolumeRemove(vol); |
Ricky Wai | 4482ab5 | 2019-12-10 19:08:18 +0000 | [diff] [blame] | 1319 | try { |
| 1320 | if (vol.type == VolumeInfo.TYPE_PRIVATE) { |
| 1321 | mInstaller.onPrivateVolumeRemoved(vol.getFsUuid()); |
| 1322 | } |
| 1323 | } catch (Installer.InstallerException e) { |
| 1324 | Slog.i(TAG, "Failed when private volume unmounted " + vol, e); |
| 1325 | } |
Jeff Sharkey | 8058fe6 | 2017-09-13 11:50:33 -0600 | [diff] [blame] | 1326 | } |
| 1327 | } |
| 1328 | }; |
| 1329 | |
Andreas Gampe | a36dc62 | 2018-02-05 17:19:22 -0800 | [diff] [blame] | 1330 | @GuardedBy("mLock") |
Jeff Sharkey | eba260d | 2015-04-19 14:35:16 -0700 | [diff] [blame] | 1331 | private void onDiskScannedLocked(DiskInfo disk) { |
Jeff Sharkey | 620b32b | 2015-04-23 19:36:02 -0700 | [diff] [blame] | 1332 | int volumeCount = 0; |
Jeff Sharkey | eba260d | 2015-04-19 14:35:16 -0700 | [diff] [blame] | 1333 | for (int i = 0; i < mVolumes.size(); i++) { |
| 1334 | final VolumeInfo vol = mVolumes.valueAt(i); |
| 1335 | if (Objects.equals(disk.id, vol.getDiskId())) { |
Jeff Sharkey | 620b32b | 2015-04-23 19:36:02 -0700 | [diff] [blame] | 1336 | volumeCount++; |
Jeff Sharkey | eba260d | 2015-04-19 14:35:16 -0700 | [diff] [blame] | 1337 | } |
| 1338 | } |
| 1339 | |
Jeff Sharkey | c7acac6 | 2015-06-12 16:16:56 -0700 | [diff] [blame] | 1340 | final Intent intent = new Intent(DiskInfo.ACTION_DISK_SCANNED); |
Jeff Sharkey | 7732e1e | 2016-03-30 17:14:23 -0600 | [diff] [blame] | 1341 | intent.addFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY_BEFORE_BOOT |
| 1342 | | Intent.FLAG_RECEIVER_INCLUDE_BACKGROUND); |
Jeff Sharkey | c7acac6 | 2015-06-12 16:16:56 -0700 | [diff] [blame] | 1343 | intent.putExtra(DiskInfo.EXTRA_DISK_ID, disk.id); |
| 1344 | intent.putExtra(DiskInfo.EXTRA_VOLUME_COUNT, volumeCount); |
Jeff Sharkey | abc3e85 | 2015-08-03 14:41:13 -0700 | [diff] [blame] | 1345 | mHandler.obtainMessage(H_INTERNAL_BROADCAST, intent).sendToTarget(); |
Jeff Sharkey | c7acac6 | 2015-06-12 16:16:56 -0700 | [diff] [blame] | 1346 | |
| 1347 | final CountDownLatch latch = mDiskScanLatches.remove(disk.id); |
| 1348 | if (latch != null) { |
| 1349 | latch.countDown(); |
| 1350 | } |
| 1351 | |
Jeff Sharkey | f5a6bd7 | 2015-05-19 14:42:38 -0700 | [diff] [blame] | 1352 | disk.volumeCount = volumeCount; |
Jeff Sharkey | 620b32b | 2015-04-23 19:36:02 -0700 | [diff] [blame] | 1353 | mCallbacks.notifyDiskScanned(disk, volumeCount); |
Jeff Sharkey | eba260d | 2015-04-19 14:35:16 -0700 | [diff] [blame] | 1354 | } |
| 1355 | |
Andreas Gampe | a36dc62 | 2018-02-05 17:19:22 -0800 | [diff] [blame] | 1356 | @GuardedBy("mLock") |
Jeff Sharkey | 1b8ef7e | 2015-04-03 17:14:45 -0700 | [diff] [blame] | 1357 | private void onVolumeCreatedLocked(VolumeInfo vol) { |
Jeff Sharkey | 5790af0 | 2018-08-13 17:42:54 -0600 | [diff] [blame] | 1358 | if (mPmInternal.isOnlyCoreApps()) { |
Jeff Sharkey | 6855c48 | 2016-03-31 14:34:38 -0600 | [diff] [blame] | 1359 | Slog.d(TAG, "System booted in core-only mode; ignoring volume " + vol.getId()); |
| 1360 | return; |
| 1361 | } |
Martijn Coenen | 7084c6a | 2019-11-22 16:32:09 +0100 | [diff] [blame] | 1362 | final ActivityManagerInternal amInternal = |
| 1363 | LocalServices.getService(ActivityManagerInternal.class); |
| 1364 | |
| 1365 | if (mIsFuseEnabled && vol.mountUserId >= 0 |
| 1366 | && !amInternal.isUserRunning(vol.mountUserId, 0)) { |
| 1367 | Slog.d(TAG, "Ignoring volume " + vol.getId() + " because user " |
| 1368 | + Integer.toString(vol.mountUserId) + " is no longer running."); |
| 1369 | return; |
| 1370 | } |
Jeff Sharkey | 6855c48 | 2016-03-31 14:34:38 -0600 | [diff] [blame] | 1371 | |
Jeff Sharkey | 275e3e4 | 2015-04-24 16:10:32 -0700 | [diff] [blame] | 1372 | if (vol.type == VolumeInfo.TYPE_EMULATED) { |
| 1373 | final StorageManager storage = mContext.getSystemService(StorageManager.class); |
| 1374 | final VolumeInfo privateVol = storage.findPrivateForEmulated(vol); |
| 1375 | |
| 1376 | if (Objects.equals(StorageManager.UUID_PRIVATE_INTERNAL, mPrimaryStorageUuid) |
| 1377 | && VolumeInfo.ID_PRIVATE_INTERNAL.equals(privateVol.id)) { |
| 1378 | Slog.v(TAG, "Found primary storage at " + vol); |
| 1379 | vol.mountFlags |= VolumeInfo.MOUNT_FLAG_PRIMARY; |
| 1380 | vol.mountFlags |= VolumeInfo.MOUNT_FLAG_VISIBLE; |
| 1381 | mHandler.obtainMessage(H_VOLUME_MOUNT, vol).sendToTarget(); |
| 1382 | |
| 1383 | } else if (Objects.equals(privateVol.fsUuid, mPrimaryStorageUuid)) { |
| 1384 | Slog.v(TAG, "Found primary storage at " + vol); |
| 1385 | vol.mountFlags |= VolumeInfo.MOUNT_FLAG_PRIMARY; |
| 1386 | vol.mountFlags |= VolumeInfo.MOUNT_FLAG_VISIBLE; |
| 1387 | mHandler.obtainMessage(H_VOLUME_MOUNT, vol).sendToTarget(); |
| 1388 | } |
Jeff Sharkey | b049e21 | 2012-09-07 23:16:01 -0700 | [diff] [blame] | 1389 | |
Jeff Sharkey | 1b8ef7e | 2015-04-03 17:14:45 -0700 | [diff] [blame] | 1390 | } else if (vol.type == VolumeInfo.TYPE_PUBLIC) { |
Jeff Sharkey | 275e3e4 | 2015-04-24 16:10:32 -0700 | [diff] [blame] | 1391 | // TODO: only look at first public partition |
| 1392 | if (Objects.equals(StorageManager.UUID_PRIMARY_PHYSICAL, mPrimaryStorageUuid) |
| 1393 | && vol.disk.isDefaultPrimary()) { |
| 1394 | Slog.v(TAG, "Found primary storage at " + vol); |
Jeff Sharkey | 7e92ef3 | 2015-04-17 17:35:07 -0700 | [diff] [blame] | 1395 | vol.mountFlags |= VolumeInfo.MOUNT_FLAG_PRIMARY; |
| 1396 | vol.mountFlags |= VolumeInfo.MOUNT_FLAG_VISIBLE; |
San Mehat | 4270e1e | 2010-01-29 05:32:19 -0800 | [diff] [blame] | 1397 | } |
Jeff Sharkey | 7e92ef3 | 2015-04-17 17:35:07 -0700 | [diff] [blame] | 1398 | |
| 1399 | // Adoptable public disks are visible to apps, since they meet |
| 1400 | // public API requirement of being in a stable location. |
Jeff Sharkey | 275e3e4 | 2015-04-24 16:10:32 -0700 | [diff] [blame] | 1401 | if (vol.disk.isAdoptable()) { |
Jeff Sharkey | 7e92ef3 | 2015-04-17 17:35:07 -0700 | [diff] [blame] | 1402 | vol.mountFlags |= VolumeInfo.MOUNT_FLAG_VISIBLE; |
| 1403 | } |
| 1404 | |
Jeff Sharkey | ab15c39 | 2016-05-05 11:45:01 -0600 | [diff] [blame] | 1405 | vol.mountUserId = mCurrentUserId; |
Jeff Sharkey | 4887789 | 2015-03-18 11:27:19 -0700 | [diff] [blame] | 1406 | mHandler.obtainMessage(H_VOLUME_MOUNT, vol).sendToTarget(); |
San Mehat | 4270e1e | 2010-01-29 05:32:19 -0800 | [diff] [blame] | 1407 | |
Jeff Sharkey | b2b9ab8 | 2015-04-05 21:10:42 -0700 | [diff] [blame] | 1408 | } else if (vol.type == VolumeInfo.TYPE_PRIVATE) { |
| 1409 | mHandler.obtainMessage(H_VOLUME_MOUNT, vol).sendToTarget(); |
| 1410 | |
Risan | 05c41e6 | 2018-10-29 08:57:43 +0900 | [diff] [blame] | 1411 | } else if (vol.type == VolumeInfo.TYPE_STUB) { |
| 1412 | vol.mountUserId = mCurrentUserId; |
| 1413 | mHandler.obtainMessage(H_VOLUME_MOUNT, vol).sendToTarget(); |
San Mehat | 4270e1e | 2010-01-29 05:32:19 -0800 | [diff] [blame] | 1414 | } else { |
Jeff Sharkey | 4887789 | 2015-03-18 11:27:19 -0700 | [diff] [blame] | 1415 | Slog.d(TAG, "Skipping automatic mounting of " + vol); |
San Mehat | 4270e1e | 2010-01-29 05:32:19 -0800 | [diff] [blame] | 1416 | } |
| 1417 | } |
| 1418 | |
Jeff Sharkey | e6c04f9 | 2015-04-18 21:38:05 -0700 | [diff] [blame] | 1419 | private boolean isBroadcastWorthy(VolumeInfo vol) { |
| 1420 | switch (vol.getType()) { |
Jeff Sharkey | c7acac6 | 2015-06-12 16:16:56 -0700 | [diff] [blame] | 1421 | case VolumeInfo.TYPE_PRIVATE: |
Jeff Sharkey | e6c04f9 | 2015-04-18 21:38:05 -0700 | [diff] [blame] | 1422 | case VolumeInfo.TYPE_PUBLIC: |
| 1423 | case VolumeInfo.TYPE_EMULATED: |
Risan | 05c41e6 | 2018-10-29 08:57:43 +0900 | [diff] [blame] | 1424 | case VolumeInfo.TYPE_STUB: |
Jeff Sharkey | e6c04f9 | 2015-04-18 21:38:05 -0700 | [diff] [blame] | 1425 | break; |
| 1426 | default: |
| 1427 | return false; |
| 1428 | } |
| 1429 | |
| 1430 | switch (vol.getState()) { |
| 1431 | case VolumeInfo.STATE_MOUNTED: |
| 1432 | case VolumeInfo.STATE_MOUNTED_READ_ONLY: |
| 1433 | case VolumeInfo.STATE_EJECTING: |
| 1434 | case VolumeInfo.STATE_UNMOUNTED: |
Jeff Sharkey | c7acac6 | 2015-06-12 16:16:56 -0700 | [diff] [blame] | 1435 | case VolumeInfo.STATE_UNMOUNTABLE: |
Tony Mantler | f0d7105 | 2015-06-24 11:45:25 -0700 | [diff] [blame] | 1436 | case VolumeInfo.STATE_BAD_REMOVAL: |
Jeff Sharkey | e6c04f9 | 2015-04-18 21:38:05 -0700 | [diff] [blame] | 1437 | break; |
| 1438 | default: |
| 1439 | return false; |
| 1440 | } |
| 1441 | |
| 1442 | return true; |
| 1443 | } |
| 1444 | |
Andreas Gampe | a36dc62 | 2018-02-05 17:19:22 -0800 | [diff] [blame] | 1445 | @GuardedBy("mLock") |
Jeff Sharkey | 1b8ef7e | 2015-04-03 17:14:45 -0700 | [diff] [blame] | 1446 | private void onVolumeStateChangedLocked(VolumeInfo vol, int oldState, int newState) { |
Jeff Sharkey | b36586a | 2015-04-27 08:42:28 -0700 | [diff] [blame] | 1447 | // Remember that we saw this volume so we're ready to accept user |
| 1448 | // metadata, or so we can annoy them when a private volume is ejected |
Jeff Sharkey | 3811f35 | 2019-05-14 11:54:36 -0600 | [diff] [blame] | 1449 | if (!TextUtils.isEmpty(vol.fsUuid)) { |
Jeff Sharkey | 5cc0df2 | 2015-06-17 19:44:05 -0700 | [diff] [blame] | 1450 | VolumeRecord rec = mRecords.get(vol.fsUuid); |
| 1451 | if (rec == null) { |
| 1452 | rec = new VolumeRecord(vol.type, vol.fsUuid); |
| 1453 | rec.partGuid = vol.partGuid; |
Jeff Sharkey | e8a4b66 | 2015-06-27 15:43:45 -0700 | [diff] [blame] | 1454 | rec.createdMillis = System.currentTimeMillis(); |
Jeff Sharkey | b36586a | 2015-04-27 08:42:28 -0700 | [diff] [blame] | 1455 | if (vol.type == VolumeInfo.TYPE_PRIVATE) { |
| 1456 | rec.nickname = vol.disk.getDescription(); |
| 1457 | } |
| 1458 | mRecords.put(rec.fsUuid, rec); |
Jeff Sharkey | 5cc0df2 | 2015-06-17 19:44:05 -0700 | [diff] [blame] | 1459 | } else { |
| 1460 | // Handle upgrade case where we didn't store partition GUID |
| 1461 | if (TextUtils.isEmpty(rec.partGuid)) { |
| 1462 | rec.partGuid = vol.partGuid; |
Jeff Sharkey | 5cc0df2 | 2015-06-17 19:44:05 -0700 | [diff] [blame] | 1463 | } |
Jeff Sharkey | b36586a | 2015-04-27 08:42:28 -0700 | [diff] [blame] | 1464 | } |
Jeff Sharkey | 3811f35 | 2019-05-14 11:54:36 -0600 | [diff] [blame] | 1465 | |
| 1466 | rec.lastSeenMillis = System.currentTimeMillis(); |
| 1467 | writeSettingsLocked(); |
Jeff Sharkey | b36586a | 2015-04-27 08:42:28 -0700 | [diff] [blame] | 1468 | } |
| 1469 | |
Jeff Sharkey | e6c04f9 | 2015-04-18 21:38:05 -0700 | [diff] [blame] | 1470 | mCallbacks.notifyVolumeStateChanged(vol, oldState, newState); |
| 1471 | |
Yasuhiro Matsuda | 87a38b5 | 2015-07-24 22:10:16 +0900 | [diff] [blame] | 1472 | // Do not broadcast before boot has completed to avoid launching the |
| 1473 | // processes that receive the intent unnecessarily. |
| 1474 | if (mBootCompleted && isBroadcastWorthy(vol)) { |
Jeff Sharkey | e6c04f9 | 2015-04-18 21:38:05 -0700 | [diff] [blame] | 1475 | final Intent intent = new Intent(VolumeInfo.ACTION_VOLUME_STATE_CHANGED); |
Jeff Sharkey | c7acac6 | 2015-06-12 16:16:56 -0700 | [diff] [blame] | 1476 | intent.putExtra(VolumeInfo.EXTRA_VOLUME_ID, vol.id); |
| 1477 | intent.putExtra(VolumeInfo.EXTRA_VOLUME_STATE, newState); |
Tony Mantler | f0d7105 | 2015-06-24 11:45:25 -0700 | [diff] [blame] | 1478 | intent.putExtra(VolumeRecord.EXTRA_FS_UUID, vol.fsUuid); |
Jeff Sharkey | 7732e1e | 2016-03-30 17:14:23 -0600 | [diff] [blame] | 1479 | intent.addFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY_BEFORE_BOOT |
| 1480 | | Intent.FLAG_RECEIVER_INCLUDE_BACKGROUND); |
Jeff Sharkey | abc3e85 | 2015-08-03 14:41:13 -0700 | [diff] [blame] | 1481 | mHandler.obtainMessage(H_INTERNAL_BROADCAST, intent).sendToTarget(); |
Jeff Sharkey | e6c04f9 | 2015-04-18 21:38:05 -0700 | [diff] [blame] | 1482 | } |
Jeff Sharkey | b049e21 | 2012-09-07 23:16:01 -0700 | [diff] [blame] | 1483 | |
Jeff Sharkey | 7151a9a | 2015-04-04 15:22:37 -0700 | [diff] [blame] | 1484 | final String oldStateEnv = VolumeInfo.getEnvironmentForState(oldState); |
| 1485 | final String newStateEnv = VolumeInfo.getEnvironmentForState(newState); |
Emily Bernier | 92aa5a2 | 2014-07-07 10:11:48 -0400 | [diff] [blame] | 1486 | |
Jeff Sharkey | 7151a9a | 2015-04-04 15:22:37 -0700 | [diff] [blame] | 1487 | if (!Objects.equals(oldStateEnv, newStateEnv)) { |
| 1488 | // Kick state changed event towards all started users. Any users |
| 1489 | // started after this point will trigger additional |
| 1490 | // user-specific broadcasts. |
Jeff Sharkey | ce14cd0 | 2015-12-07 15:35:42 -0700 | [diff] [blame] | 1491 | for (int userId : mSystemUnlockedUsers) { |
Jeff Sharkey | 4634987 | 2015-07-28 10:49:47 -0700 | [diff] [blame] | 1492 | if (vol.isVisibleForRead(userId)) { |
Svet Ganov | 6ee871e | 2015-07-10 14:29:33 -0700 | [diff] [blame] | 1493 | final StorageVolume userVol = vol.buildStorageVolume(mContext, userId, false); |
Jeff Sharkey | 7151a9a | 2015-04-04 15:22:37 -0700 | [diff] [blame] | 1494 | mHandler.obtainMessage(H_VOLUME_BROADCAST, userVol).sendToTarget(); |
Jeff Sharkey | 4887789 | 2015-03-18 11:27:19 -0700 | [diff] [blame] | 1495 | |
Jeff Sharkey | 7151a9a | 2015-04-04 15:22:37 -0700 | [diff] [blame] | 1496 | mCallbacks.notifyStorageStateChanged(userVol.getPath(), oldStateEnv, |
| 1497 | newStateEnv); |
San Mehat | 4270e1e | 2010-01-29 05:32:19 -0800 | [diff] [blame] | 1498 | } |
| 1499 | } |
| 1500 | } |
Jeff Sharkey | 7151a9a | 2015-04-04 15:22:37 -0700 | [diff] [blame] | 1501 | |
Risan | 05c41e6 | 2018-10-29 08:57:43 +0900 | [diff] [blame] | 1502 | if ((vol.type == VolumeInfo.TYPE_PUBLIC || vol.type == VolumeInfo.TYPE_STUB) |
| 1503 | && vol.state == VolumeInfo.STATE_EJECTING) { |
Jeff Sharkey | 7151a9a | 2015-04-04 15:22:37 -0700 | [diff] [blame] | 1504 | // TODO: this should eventually be handled by new ObbVolume state changes |
| 1505 | /* |
| 1506 | * Some OBBs might have been unmounted when this volume was |
| 1507 | * unmounted, so send a message to the handler to let it know to |
| 1508 | * remove those from the list of mounted OBBS. |
| 1509 | */ |
| 1510 | mObbActionHandler.sendMessage(mObbActionHandler.obtainMessage( |
| 1511 | OBB_FLUSH_MOUNT_STATE, vol.path)); |
| 1512 | } |
Pavel Grafov | ce72ef0 | 2018-01-10 17:14:11 +0000 | [diff] [blame] | 1513 | maybeLogMediaMount(vol, newState); |
| 1514 | } |
| 1515 | |
| 1516 | private void maybeLogMediaMount(VolumeInfo vol, int newState) { |
| 1517 | if (!SecurityLog.isLoggingEnabled()) { |
| 1518 | return; |
| 1519 | } |
| 1520 | |
| 1521 | final DiskInfo disk = vol.getDisk(); |
| 1522 | if (disk == null || (disk.flags & (DiskInfo.FLAG_SD | DiskInfo.FLAG_USB)) == 0) { |
| 1523 | return; |
| 1524 | } |
| 1525 | |
| 1526 | // Sometimes there is a newline character. |
| 1527 | final String label = disk.label != null ? disk.label.trim() : ""; |
| 1528 | |
| 1529 | if (newState == VolumeInfo.STATE_MOUNTED |
| 1530 | || newState == VolumeInfo.STATE_MOUNTED_READ_ONLY) { |
| 1531 | SecurityLog.writeEvent(SecurityLog.TAG_MEDIA_MOUNT, vol.path, label); |
| 1532 | } else if (newState == VolumeInfo.STATE_UNMOUNTED |
| 1533 | || newState == VolumeInfo.STATE_BAD_REMOVAL) { |
| 1534 | SecurityLog.writeEvent(SecurityLog.TAG_MEDIA_UNMOUNT, vol.path, label); |
| 1535 | } |
San Mehat | 4270e1e | 2010-01-29 05:32:19 -0800 | [diff] [blame] | 1536 | } |
| 1537 | |
Andreas Gampe | a36dc62 | 2018-02-05 17:19:22 -0800 | [diff] [blame] | 1538 | @GuardedBy("mLock") |
Jeff Sharkey | 275e3e4 | 2015-04-24 16:10:32 -0700 | [diff] [blame] | 1539 | private void onMoveStatusLocked(int status) { |
| 1540 | if (mMoveCallback == null) { |
| 1541 | Slog.w(TAG, "Odd, status but no move requested"); |
| 1542 | return; |
| 1543 | } |
| 1544 | |
| 1545 | // TODO: estimate remaining time |
| 1546 | try { |
Jeff Sharkey | 50a0545 | 2015-04-29 11:24:52 -0700 | [diff] [blame] | 1547 | mMoveCallback.onStatusChanged(-1, status, -1); |
Jeff Sharkey | 275e3e4 | 2015-04-24 16:10:32 -0700 | [diff] [blame] | 1548 | } catch (RemoteException ignored) { |
| 1549 | } |
| 1550 | |
| 1551 | // We've finished copying and we're about to clean up old data, so |
| 1552 | // remember that move was successful if we get rebooted |
| 1553 | if (status == MOVE_STATUS_COPY_FINISHED) { |
| 1554 | Slog.d(TAG, "Move to " + mMoveTargetUuid + " copy phase finshed; persisting"); |
| 1555 | |
| 1556 | mPrimaryStorageUuid = mMoveTargetUuid; |
Jeff Sharkey | b36586a | 2015-04-27 08:42:28 -0700 | [diff] [blame] | 1557 | writeSettingsLocked(); |
Jeff Sharkey | 275e3e4 | 2015-04-24 16:10:32 -0700 | [diff] [blame] | 1558 | } |
| 1559 | |
| 1560 | if (PackageManager.isMoveStatusFinished(status)) { |
| 1561 | Slog.d(TAG, "Move to " + mMoveTargetUuid + " finished with status " + status); |
| 1562 | |
| 1563 | mMoveCallback = null; |
| 1564 | mMoveTargetUuid = null; |
| 1565 | } |
| 1566 | } |
| 1567 | |
Jeff Sharkey | 4887789 | 2015-03-18 11:27:19 -0700 | [diff] [blame] | 1568 | private void enforcePermission(String perm) { |
| 1569 | mContext.enforceCallingOrSelfPermission(perm, perm); |
Mike Lockwood | a5250c9 | 2011-05-23 13:44:04 -0400 | [diff] [blame] | 1570 | } |
| 1571 | |
Jeff Sharkey | 2e606d7 | 2015-07-27 14:19:54 -0700 | [diff] [blame] | 1572 | /** |
| 1573 | * Decide if volume is mountable per device policies. |
| 1574 | */ |
| 1575 | private boolean isMountDisallowed(VolumeInfo vol) { |
Philip P. Moltmann | ec3cbb2 | 2016-09-14 13:24:52 -0700 | [diff] [blame] | 1576 | UserManager userManager = mContext.getSystemService(UserManager.class); |
| 1577 | |
| 1578 | boolean isUsbRestricted = false; |
| 1579 | if (vol.disk != null && vol.disk.isUsb()) { |
| 1580 | isUsbRestricted = userManager.hasUserRestriction(UserManager.DISALLOW_USB_FILE_TRANSFER, |
Jeff Sharkey | 2e606d7 | 2015-07-27 14:19:54 -0700 | [diff] [blame] | 1581 | Binder.getCallingUserHandle()); |
Emily Bernier | 92aa5a2 | 2014-07-07 10:11:48 -0400 | [diff] [blame] | 1582 | } |
Philip P. Moltmann | ec3cbb2 | 2016-09-14 13:24:52 -0700 | [diff] [blame] | 1583 | |
| 1584 | boolean isTypeRestricted = false; |
Risan | 05c41e6 | 2018-10-29 08:57:43 +0900 | [diff] [blame] | 1585 | if (vol.type == VolumeInfo.TYPE_PUBLIC || vol.type == VolumeInfo.TYPE_PRIVATE |
| 1586 | || vol.type == VolumeInfo.TYPE_STUB) { |
Philip P. Moltmann | ec3cbb2 | 2016-09-14 13:24:52 -0700 | [diff] [blame] | 1587 | isTypeRestricted = userManager |
| 1588 | .hasUserRestriction(UserManager.DISALLOW_MOUNT_PHYSICAL_MEDIA, |
| 1589 | Binder.getCallingUserHandle()); |
| 1590 | } |
| 1591 | |
| 1592 | return isUsbRestricted || isTypeRestricted; |
Emily Bernier | 92aa5a2 | 2014-07-07 10:11:48 -0400 | [diff] [blame] | 1593 | } |
| 1594 | |
Amith Yamasani | 462ac3a | 2015-06-30 14:21:01 -0700 | [diff] [blame] | 1595 | private void enforceAdminUser() { |
| 1596 | UserManager um = (UserManager) mContext.getSystemService(Context.USER_SERVICE); |
| 1597 | final int callingUserId = UserHandle.getCallingUserId(); |
| 1598 | boolean isAdmin; |
| 1599 | long token = Binder.clearCallingIdentity(); |
| 1600 | try { |
| 1601 | isAdmin = um.getUserInfo(callingUserId).isAdmin(); |
| 1602 | } finally { |
| 1603 | Binder.restoreCallingIdentity(token); |
| 1604 | } |
| 1605 | if (!isAdmin) { |
| 1606 | throw new SecurityException("Only admin users can adopt sd cards"); |
| 1607 | } |
| 1608 | } |
| 1609 | |
Jeff Sharkey | b049e21 | 2012-09-07 23:16:01 -0700 | [diff] [blame] | 1610 | /** |
Sudheer Shanka | 2250d56 | 2016-11-07 15:41:02 -0800 | [diff] [blame] | 1611 | * Constructs a new StorageManagerService instance |
San Mehat | 207e538 | 2010-02-04 20:46:54 -0800 | [diff] [blame] | 1612 | * |
| 1613 | * @param context Binder context for this service |
| 1614 | */ |
Sudheer Shanka | 2250d56 | 2016-11-07 15:41:02 -0800 | [diff] [blame] | 1615 | public StorageManagerService(Context context) { |
Christopher Tate | d417d62 | 2013-08-19 16:14:25 -0700 | [diff] [blame] | 1616 | sSelf = this; |
| 1617 | |
Jeff Sharkey | 342b4bf | 2018-12-18 11:12:40 -0700 | [diff] [blame] | 1618 | // Snapshot feature flag used for this boot |
| 1619 | SystemProperties.set(StorageManager.PROP_ISOLATED_STORAGE_SNAPSHOT, Boolean.toString( |
Jeff Sharkey | 0637680 | 2019-02-11 12:20:02 -0700 | [diff] [blame] | 1620 | SystemProperties.getBoolean(StorageManager.PROP_ISOLATED_STORAGE, true))); |
shafik | 78fcd50 | 2019-09-25 13:50:04 +0100 | [diff] [blame] | 1621 | |
Zim | 69e9c09 | 2020-01-21 13:32:32 +0000 | [diff] [blame] | 1622 | // If there is no value in the property yet (first boot after data wipe), this value may be |
| 1623 | // incorrect until #updateFusePropFromSettings where we set the correct value and reboot if |
| 1624 | // different |
| 1625 | mIsFuseEnabled = SystemProperties.getBoolean(PROP_FUSE, false); |
San Mehat | 207e538 | 2010-02-04 20:46:54 -0800 | [diff] [blame] | 1626 | mContext = context; |
Jeff Sharkey | 5d0c55c | 2019-01-24 14:32:31 -0700 | [diff] [blame] | 1627 | mResolver = mContext.getContentResolver(); |
Jeff Sharkey | 7151a9a | 2015-04-04 15:22:37 -0700 | [diff] [blame] | 1628 | mCallbacks = new Callbacks(FgThread.get().getLooper()); |
Jeff Sharkey | b9fe537 | 2015-12-03 15:23:08 -0700 | [diff] [blame] | 1629 | mLockPatternUtils = new LockPatternUtils(mContext); |
San Mehat | 207e538 | 2010-02-04 20:46:54 -0800 | [diff] [blame] | 1630 | |
Dianne Hackborn | efa92b2 | 2013-05-03 14:11:43 -0700 | [diff] [blame] | 1631 | HandlerThread hthread = new HandlerThread(TAG); |
| 1632 | hthread.start(); |
Sudheer Shanka | 2250d56 | 2016-11-07 15:41:02 -0800 | [diff] [blame] | 1633 | mHandler = new StorageManagerServiceHandler(hthread.getLooper()); |
Daniel Sandler | 5f27ef4 | 2010-03-16 15:42:02 -0400 | [diff] [blame] | 1634 | |
Sudheer Shanka | 2250d56 | 2016-11-07 15:41:02 -0800 | [diff] [blame] | 1635 | // Add OBB Action Handler to StorageManagerService thread. |
Dianne Hackborn | 8d044e8 | 2013-04-30 17:24:15 -0700 | [diff] [blame] | 1636 | mObbActionHandler = new ObbActionHandler(IoThread.get().getLooper()); |
Kenny Root | a02b8b0 | 2010-08-05 16:14:17 -0700 | [diff] [blame] | 1637 | |
Zim | 17be6f9 | 2019-09-25 14:37:55 +0100 | [diff] [blame] | 1638 | mStorageSessionController = new StorageSessionController(mContext, mIsFuseEnabled); |
Zim | 42f1e9f | 2019-08-15 17:35:00 +0100 | [diff] [blame] | 1639 | |
Ricky Wai | 4482ab5 | 2019-12-10 19:08:18 +0000 | [diff] [blame] | 1640 | mInstaller = new Installer(mContext); |
| 1641 | mInstaller.onStart(); |
| 1642 | |
Christopher Tate | 7265abe | 2014-11-21 13:54:45 -0800 | [diff] [blame] | 1643 | // Initialize the last-fstrim tracking if necessary |
| 1644 | File dataDir = Environment.getDataDirectory(); |
| 1645 | File systemDir = new File(dataDir, "system"); |
| 1646 | mLastMaintenanceFile = new File(systemDir, LAST_FSTRIM_FILE); |
| 1647 | if (!mLastMaintenanceFile.exists()) { |
| 1648 | // Not setting mLastMaintenance here means that we will force an |
| 1649 | // fstrim during reboot following the OTA that installs this code. |
| 1650 | try { |
| 1651 | (new FileOutputStream(mLastMaintenanceFile)).close(); |
| 1652 | } catch (IOException e) { |
| 1653 | Slog.e(TAG, "Unable to create fstrim record " + mLastMaintenanceFile.getPath()); |
| 1654 | } |
| 1655 | } else { |
| 1656 | mLastMaintenance = mLastMaintenanceFile.lastModified(); |
| 1657 | } |
| 1658 | |
Jeff Sharkey | b36586a | 2015-04-27 08:42:28 -0700 | [diff] [blame] | 1659 | mSettingsFile = new AtomicFile( |
Dianne Hackborn | e17b445 | 2018-01-10 13:15:40 -0800 | [diff] [blame] | 1660 | new File(Environment.getDataSystemDirectory(), "storage.xml"), "storage-settings"); |
Jeff Sharkey | d95d3bf | 2015-04-14 21:39:44 -0700 | [diff] [blame] | 1661 | |
| 1662 | synchronized (mLock) { |
Jeff Sharkey | b36586a | 2015-04-27 08:42:28 -0700 | [diff] [blame] | 1663 | readSettingsLocked(); |
Jeff Sharkey | d95d3bf | 2015-04-14 21:39:44 -0700 | [diff] [blame] | 1664 | } |
| 1665 | |
Sudheer Shanka | 2250d56 | 2016-11-07 15:41:02 -0800 | [diff] [blame] | 1666 | LocalServices.addService(StorageManagerInternal.class, mStorageManagerInternal); |
Svet Ganov | 6ee871e | 2015-07-10 14:29:33 -0700 | [diff] [blame] | 1667 | |
Jeff Sharkey | bcd262d | 2015-06-10 09:41:17 -0700 | [diff] [blame] | 1668 | final IntentFilter userFilter = new IntentFilter(); |
| 1669 | userFilter.addAction(Intent.ACTION_USER_ADDED); |
| 1670 | userFilter.addAction(Intent.ACTION_USER_REMOVED); |
| 1671 | mContext.registerReceiver(mUserReceiver, userFilter, null, mHandler); |
| 1672 | |
Jeff Sharkey | 5a9bb74 | 2015-11-03 10:15:57 -0800 | [diff] [blame] | 1673 | synchronized (mLock) { |
| 1674 | addInternalVolumeLocked(); |
| 1675 | } |
Amith Yamasani | a789248 | 2015-08-07 11:09:05 -0700 | [diff] [blame] | 1676 | |
Kenny Root | 07714d4 | 2011-08-17 17:49:28 -0700 | [diff] [blame] | 1677 | // Add ourself to the Watchdog monitors if enabled. |
| 1678 | if (WATCHDOG_ENABLE) { |
| 1679 | Watchdog.getInstance().addMonitor(this); |
| 1680 | } |
San Mehat | 207e538 | 2010-02-04 20:46:54 -0800 | [diff] [blame] | 1681 | } |
| 1682 | |
Abhijeet Kaur | b984771 | 2019-11-19 16:35:47 +0000 | [diff] [blame] | 1683 | /** |
| 1684 | * Checks if user changed the persistent settings_fuse flag from Settings UI |
| 1685 | * and updates PROP_FUSE (reboots if changed). |
| 1686 | */ |
| 1687 | private void updateFusePropFromSettings() { |
Zim | 69e9c09 | 2020-01-21 13:32:32 +0000 | [diff] [blame] | 1688 | boolean defaultFuseFlag = false; |
| 1689 | boolean settingsFuseFlag = SystemProperties.getBoolean(PROP_SETTINGS_FUSE, defaultFuseFlag); |
| 1690 | Slog.d(TAG, "FUSE flags. Settings: " + settingsFuseFlag + ". Default: " + defaultFuseFlag); |
| 1691 | |
| 1692 | if (TextUtils.isEmpty(SystemProperties.get(PROP_SETTINGS_FUSE))) { |
| 1693 | // Set default value of PROP_SETTINGS_FUSE and PROP_FUSE if it |
| 1694 | // is unset (neither true nor false). |
| 1695 | // This happens only on the first boot after wiping data partition |
| 1696 | SystemProperties.set(PROP_SETTINGS_FUSE, Boolean.toString(defaultFuseFlag)); |
| 1697 | SystemProperties.set(PROP_FUSE, Boolean.toString(defaultFuseFlag)); |
Abhijeet Kaur | a4f4056 | 2019-12-09 13:55:42 +0000 | [diff] [blame] | 1698 | return; |
| 1699 | } |
| 1700 | |
Zim | 69e9c09 | 2020-01-21 13:32:32 +0000 | [diff] [blame] | 1701 | if (mIsFuseEnabled != settingsFuseFlag) { |
| 1702 | Slog.i(TAG, "Toggling persist.sys.fuse to " + settingsFuseFlag); |
| 1703 | SystemProperties.set(PROP_FUSE, Boolean.toString(settingsFuseFlag)); |
Zim | 41cd8de | 2020-01-21 13:51:15 +0000 | [diff] [blame] | 1704 | |
| 1705 | PowerManager powerManager = mContext.getSystemService(PowerManager.class); |
| 1706 | if (powerManager.isRebootingUserspaceSupported()) { |
| 1707 | // Perform userspace reboot to kick policy into place |
| 1708 | powerManager.reboot(PowerManager.REBOOT_USERSPACE); |
| 1709 | } else { |
| 1710 | // Perform hard reboot to kick policy into place |
| 1711 | powerManager.reboot("fuse_prop"); |
| 1712 | } |
Abhijeet Kaur | b984771 | 2019-11-19 16:35:47 +0000 | [diff] [blame] | 1713 | } |
| 1714 | } |
| 1715 | |
Jeff Sharkey | cd57599 | 2016-03-29 14:12:49 -0600 | [diff] [blame] | 1716 | private void start() { |
peter.zhang | b0d11d1 | 2019-06-13 16:39:30 +0800 | [diff] [blame] | 1717 | connectStoraged(); |
| 1718 | connectVold(); |
Jeff Sharkey | cd57599 | 2016-03-29 14:12:49 -0600 | [diff] [blame] | 1719 | } |
| 1720 | |
peter.zhang | b0d11d1 | 2019-06-13 16:39:30 +0800 | [diff] [blame] | 1721 | private void connectStoraged() { |
Jin Qian | 12690d5 | 2017-10-13 18:17:04 -0700 | [diff] [blame] | 1722 | IBinder binder = ServiceManager.getService("storaged"); |
| 1723 | if (binder != null) { |
| 1724 | try { |
| 1725 | binder.linkToDeath(new DeathRecipient() { |
| 1726 | @Override |
| 1727 | public void binderDied() { |
| 1728 | Slog.w(TAG, "storaged died; reconnecting"); |
| 1729 | mStoraged = null; |
peter.zhang | b0d11d1 | 2019-06-13 16:39:30 +0800 | [diff] [blame] | 1730 | connectStoraged(); |
Jin Qian | 12690d5 | 2017-10-13 18:17:04 -0700 | [diff] [blame] | 1731 | } |
| 1732 | }, 0); |
| 1733 | } catch (RemoteException e) { |
| 1734 | binder = null; |
| 1735 | } |
| 1736 | } |
| 1737 | |
| 1738 | if (binder != null) { |
| 1739 | mStoraged = IStoraged.Stub.asInterface(binder); |
| 1740 | } else { |
| 1741 | Slog.w(TAG, "storaged not found; trying again"); |
| 1742 | } |
| 1743 | |
peter.zhang | b0d11d1 | 2019-06-13 16:39:30 +0800 | [diff] [blame] | 1744 | if (mStoraged == null) { |
| 1745 | BackgroundThread.getHandler().postDelayed(() -> { |
| 1746 | connectStoraged(); |
| 1747 | }, DateUtils.SECOND_IN_MILLIS); |
| 1748 | } else { |
| 1749 | onDaemonConnected(); |
| 1750 | } |
| 1751 | } |
| 1752 | |
| 1753 | private void connectVold() { |
| 1754 | IBinder binder = ServiceManager.getService("vold"); |
Jeff Sharkey | 1019de9 | 2017-09-06 13:47:03 -0600 | [diff] [blame] | 1755 | if (binder != null) { |
| 1756 | try { |
| 1757 | binder.linkToDeath(new DeathRecipient() { |
| 1758 | @Override |
| 1759 | public void binderDied() { |
| 1760 | Slog.w(TAG, "vold died; reconnecting"); |
Jin Qian | 12690d5 | 2017-10-13 18:17:04 -0700 | [diff] [blame] | 1761 | mVold = null; |
peter.zhang | b0d11d1 | 2019-06-13 16:39:30 +0800 | [diff] [blame] | 1762 | connectVold(); |
Jeff Sharkey | 1019de9 | 2017-09-06 13:47:03 -0600 | [diff] [blame] | 1763 | } |
| 1764 | }, 0); |
| 1765 | } catch (RemoteException e) { |
| 1766 | binder = null; |
| 1767 | } |
| 1768 | } |
| 1769 | |
| 1770 | if (binder != null) { |
| 1771 | mVold = IVold.Stub.asInterface(binder); |
Jeff Sharkey | 8058fe6 | 2017-09-13 11:50:33 -0600 | [diff] [blame] | 1772 | try { |
| 1773 | mVold.setListener(mListener); |
Jeff Sharkey | 8058fe6 | 2017-09-13 11:50:33 -0600 | [diff] [blame] | 1774 | } catch (RemoteException e) { |
Jin Qian | 12690d5 | 2017-10-13 18:17:04 -0700 | [diff] [blame] | 1775 | mVold = null; |
Jeff Sharkey | 8058fe6 | 2017-09-13 11:50:33 -0600 | [diff] [blame] | 1776 | Slog.w(TAG, "vold listener rejected; trying again", e); |
| 1777 | } |
Jeff Sharkey | 1019de9 | 2017-09-06 13:47:03 -0600 | [diff] [blame] | 1778 | } else { |
| 1779 | Slog.w(TAG, "vold not found; trying again"); |
Jeff Sharkey | 1019de9 | 2017-09-06 13:47:03 -0600 | [diff] [blame] | 1780 | } |
Jeff Sharkey | 8058fe6 | 2017-09-13 11:50:33 -0600 | [diff] [blame] | 1781 | |
peter.zhang | b0d11d1 | 2019-06-13 16:39:30 +0800 | [diff] [blame] | 1782 | if (mVold == null) { |
Jin Qian | 12690d5 | 2017-10-13 18:17:04 -0700 | [diff] [blame] | 1783 | BackgroundThread.getHandler().postDelayed(() -> { |
peter.zhang | b0d11d1 | 2019-06-13 16:39:30 +0800 | [diff] [blame] | 1784 | connectVold(); |
Jin Qian | 12690d5 | 2017-10-13 18:17:04 -0700 | [diff] [blame] | 1785 | }, DateUtils.SECOND_IN_MILLIS); |
| 1786 | } else { |
| 1787 | onDaemonConnected(); |
| 1788 | } |
Jeff Sharkey | 1019de9 | 2017-09-06 13:47:03 -0600 | [diff] [blame] | 1789 | } |
| 1790 | |
Jeff Sharkey | 11697f5 | 2018-12-13 10:14:42 -0700 | [diff] [blame] | 1791 | private void servicesReady() { |
Sudheer Shanka | ff58507 | 2019-01-29 23:19:45 -0800 | [diff] [blame] | 1792 | mPmInternal = LocalServices.getService(PackageManagerInternal.class); |
Sudheer Shanka | ff58507 | 2019-01-29 23:19:45 -0800 | [diff] [blame] | 1793 | |
| 1794 | mIPackageManager = IPackageManager.Stub.asInterface( |
| 1795 | ServiceManager.getService("package")); |
| 1796 | mIAppOpsService = IAppOpsService.Stub.asInterface( |
| 1797 | ServiceManager.getService(Context.APP_OPS_SERVICE)); |
Zim | 74a9bba | 2019-09-03 20:49:13 +0100 | [diff] [blame] | 1798 | |
| 1799 | ProviderInfo provider = mPmInternal.resolveContentProvider( |
| 1800 | MediaStore.AUTHORITY, PackageManager.MATCH_DIRECT_BOOT_AWARE |
| 1801 | | PackageManager.MATCH_DIRECT_BOOT_UNAWARE, |
| 1802 | UserHandle.getUserId(UserHandle.USER_SYSTEM)); |
| 1803 | if (provider != null) { |
Martijn Coenen | 44db1ac | 2019-12-03 16:06:19 +0100 | [diff] [blame] | 1804 | mMediaStoreAuthorityAppId = UserHandle.getAppId(provider.applicationInfo.uid); |
Zim | 74a9bba | 2019-09-03 20:49:13 +0100 | [diff] [blame] | 1805 | } |
| 1806 | |
Martijn Coenen | 496ac00 | 2020-01-08 14:55:53 +0100 | [diff] [blame] | 1807 | provider = mPmInternal.resolveContentProvider( |
| 1808 | Downloads.Impl.AUTHORITY, PackageManager.MATCH_DIRECT_BOOT_AWARE |
| 1809 | | PackageManager.MATCH_DIRECT_BOOT_UNAWARE, |
| 1810 | UserHandle.getUserId(UserHandle.USER_SYSTEM)); |
| 1811 | |
| 1812 | if (provider != null) { |
| 1813 | mDownloadsAuthorityAppId = UserHandle.getAppId(provider.applicationInfo.uid); |
| 1814 | } |
| 1815 | |
Sudheer Shanka | ff58507 | 2019-01-29 23:19:45 -0800 | [diff] [blame] | 1816 | try { |
| 1817 | mIAppOpsService.startWatchingMode(OP_REQUEST_INSTALL_PACKAGES, null, mAppOpsCallback); |
Sudheer Shanka | 783c90e | 2019-04-12 13:55:20 -0700 | [diff] [blame] | 1818 | mIAppOpsService.startWatchingMode(OP_LEGACY_STORAGE, null, mAppOpsCallback); |
Sudheer Shanka | ff58507 | 2019-01-29 23:19:45 -0800 | [diff] [blame] | 1819 | } catch (RemoteException e) { |
| 1820 | } |
Jeff Sharkey | 11697f5 | 2018-12-13 10:14:42 -0700 | [diff] [blame] | 1821 | } |
| 1822 | |
| 1823 | private static long getLastAccessTime(AppOpsManager manager, |
| 1824 | int uid, String packageName, int[] ops) { |
| 1825 | long maxTime = 0; |
| 1826 | final List<AppOpsManager.PackageOps> pkgs = manager.getOpsForPackage(uid, packageName, ops); |
Eugene Susla | 04d021c | 2018-12-17 14:49:59 -0800 | [diff] [blame] | 1827 | for (AppOpsManager.PackageOps pkg : CollectionUtils.emptyIfNull(pkgs)) { |
| 1828 | for (AppOpsManager.OpEntry op : CollectionUtils.emptyIfNull(pkg.getOps())) { |
Svet Ganov | af189e3 | 2019-02-15 18:45:29 -0800 | [diff] [blame] | 1829 | maxTime = Math.max(maxTime, op.getLastAccessTime( |
| 1830 | AppOpsManager.OP_FLAGS_ALL_TRUSTED)); |
Jeff Sharkey | 11697f5 | 2018-12-13 10:14:42 -0700 | [diff] [blame] | 1831 | } |
| 1832 | } |
| 1833 | return maxTime; |
| 1834 | } |
| 1835 | |
Jeff Sharkey | 56e6293 | 2015-03-21 20:41:00 -0700 | [diff] [blame] | 1836 | private void systemReady() { |
Wale Ogunwale | 6767eae | 2018-05-03 15:52:51 -0700 | [diff] [blame] | 1837 | LocalServices.getService(ActivityTaskManagerInternal.class) |
Jeff Sharkey | 9765e44 | 2017-12-14 22:15:14 -0700 | [diff] [blame] | 1838 | .registerScreenObserver(this); |
| 1839 | |
Jeff Sharkey | b049e21 | 2012-09-07 23:16:01 -0700 | [diff] [blame] | 1840 | mHandler.obtainMessage(H_SYSTEM_READY).sendToTarget(); |
| 1841 | } |
| 1842 | |
Yasuhiro Matsuda | 87a38b5 | 2015-07-24 22:10:16 +0900 | [diff] [blame] | 1843 | private void bootCompleted() { |
| 1844 | mBootCompleted = true; |
Daniel Rosenberg | 137aed1 | 2019-03-15 18:41:11 -0700 | [diff] [blame] | 1845 | mHandler.obtainMessage(H_BOOT_COMPLETED).sendToTarget(); |
Abhijeet Kaur | 10c5655 | 2019-12-04 14:17:40 +0000 | [diff] [blame] | 1846 | updateFusePropFromSettings(); |
Daniel Rosenberg | 137aed1 | 2019-03-15 18:41:11 -0700 | [diff] [blame] | 1847 | } |
| 1848 | |
| 1849 | private void handleBootCompleted() { |
| 1850 | initIfBootedAndConnected(); |
| 1851 | resetIfBootedAndConnected(); |
Yasuhiro Matsuda | 87a38b5 | 2015-07-24 22:10:16 +0900 | [diff] [blame] | 1852 | } |
| 1853 | |
Jeff Sharkey | fced534 | 2015-05-10 14:53:34 -0700 | [diff] [blame] | 1854 | private String getDefaultPrimaryStorageUuid() { |
| 1855 | if (SystemProperties.getBoolean(StorageManager.PROP_PRIMARY_PHYSICAL, false)) { |
| 1856 | return StorageManager.UUID_PRIMARY_PHYSICAL; |
| 1857 | } else { |
| 1858 | return StorageManager.UUID_PRIVATE_INTERNAL; |
| 1859 | } |
| 1860 | } |
| 1861 | |
Andreas Gampe | a36dc62 | 2018-02-05 17:19:22 -0800 | [diff] [blame] | 1862 | @GuardedBy("mLock") |
Jeff Sharkey | b36586a | 2015-04-27 08:42:28 -0700 | [diff] [blame] | 1863 | private void readSettingsLocked() { |
| 1864 | mRecords.clear(); |
Jeff Sharkey | fced534 | 2015-05-10 14:53:34 -0700 | [diff] [blame] | 1865 | mPrimaryStorageUuid = getDefaultPrimaryStorageUuid(); |
Jeff Sharkey | d95d3bf | 2015-04-14 21:39:44 -0700 | [diff] [blame] | 1866 | |
| 1867 | FileInputStream fis = null; |
| 1868 | try { |
Jeff Sharkey | b36586a | 2015-04-27 08:42:28 -0700 | [diff] [blame] | 1869 | fis = mSettingsFile.openRead(); |
Jeff Sharkey | d95d3bf | 2015-04-14 21:39:44 -0700 | [diff] [blame] | 1870 | final XmlPullParser in = Xml.newPullParser(); |
Wojciech Staszkiewicz | 9e9e2e7 | 2015-05-08 14:58:46 +0100 | [diff] [blame] | 1871 | in.setInput(fis, StandardCharsets.UTF_8.name()); |
Jeff Sharkey | d95d3bf | 2015-04-14 21:39:44 -0700 | [diff] [blame] | 1872 | |
| 1873 | int type; |
| 1874 | while ((type = in.next()) != END_DOCUMENT) { |
| 1875 | if (type == START_TAG) { |
| 1876 | final String tag = in.getName(); |
Jeff Sharkey | 620b32b | 2015-04-23 19:36:02 -0700 | [diff] [blame] | 1877 | if (TAG_VOLUMES.equals(tag)) { |
| 1878 | final int version = readIntAttribute(in, ATTR_VERSION, VERSION_INIT); |
Jeff Sharkey | fced534 | 2015-05-10 14:53:34 -0700 | [diff] [blame] | 1879 | final boolean primaryPhysical = SystemProperties.getBoolean( |
| 1880 | StorageManager.PROP_PRIMARY_PHYSICAL, false); |
| 1881 | final boolean validAttr = (version >= VERSION_FIX_PRIMARY) |
| 1882 | || (version >= VERSION_ADD_PRIMARY && !primaryPhysical); |
| 1883 | if (validAttr) { |
Jeff Sharkey | 620b32b | 2015-04-23 19:36:02 -0700 | [diff] [blame] | 1884 | mPrimaryStorageUuid = readStringAttribute(in, |
| 1885 | ATTR_PRIMARY_STORAGE_UUID); |
Jeff Sharkey | 620b32b | 2015-04-23 19:36:02 -0700 | [diff] [blame] | 1886 | } |
Jeff Sharkey | 620b32b | 2015-04-23 19:36:02 -0700 | [diff] [blame] | 1887 | } else if (TAG_VOLUME.equals(tag)) { |
Jeff Sharkey | b36586a | 2015-04-27 08:42:28 -0700 | [diff] [blame] | 1888 | final VolumeRecord rec = readVolumeRecord(in); |
| 1889 | mRecords.put(rec.fsUuid, rec); |
Jeff Sharkey | d95d3bf | 2015-04-14 21:39:44 -0700 | [diff] [blame] | 1890 | } |
| 1891 | } |
| 1892 | } |
| 1893 | } catch (FileNotFoundException e) { |
| 1894 | // Missing metadata is okay, probably first boot |
| 1895 | } catch (IOException e) { |
| 1896 | Slog.wtf(TAG, "Failed reading metadata", e); |
| 1897 | } catch (XmlPullParserException e) { |
| 1898 | Slog.wtf(TAG, "Failed reading metadata", e); |
| 1899 | } finally { |
| 1900 | IoUtils.closeQuietly(fis); |
| 1901 | } |
| 1902 | } |
| 1903 | |
Andreas Gampe | a36dc62 | 2018-02-05 17:19:22 -0800 | [diff] [blame] | 1904 | @GuardedBy("mLock") |
Jeff Sharkey | b36586a | 2015-04-27 08:42:28 -0700 | [diff] [blame] | 1905 | private void writeSettingsLocked() { |
Jeff Sharkey | d95d3bf | 2015-04-14 21:39:44 -0700 | [diff] [blame] | 1906 | FileOutputStream fos = null; |
| 1907 | try { |
Jeff Sharkey | b36586a | 2015-04-27 08:42:28 -0700 | [diff] [blame] | 1908 | fos = mSettingsFile.startWrite(); |
Jeff Sharkey | d95d3bf | 2015-04-14 21:39:44 -0700 | [diff] [blame] | 1909 | |
| 1910 | XmlSerializer out = new FastXmlSerializer(); |
Wojciech Staszkiewicz | 9e9e2e7 | 2015-05-08 14:58:46 +0100 | [diff] [blame] | 1911 | out.setOutput(fos, StandardCharsets.UTF_8.name()); |
Jeff Sharkey | d95d3bf | 2015-04-14 21:39:44 -0700 | [diff] [blame] | 1912 | out.startDocument(null, true); |
| 1913 | out.startTag(null, TAG_VOLUMES); |
Jeff Sharkey | fced534 | 2015-05-10 14:53:34 -0700 | [diff] [blame] | 1914 | writeIntAttribute(out, ATTR_VERSION, VERSION_FIX_PRIMARY); |
Jeff Sharkey | 620b32b | 2015-04-23 19:36:02 -0700 | [diff] [blame] | 1915 | writeStringAttribute(out, ATTR_PRIMARY_STORAGE_UUID, mPrimaryStorageUuid); |
Jeff Sharkey | b36586a | 2015-04-27 08:42:28 -0700 | [diff] [blame] | 1916 | final int size = mRecords.size(); |
Jeff Sharkey | d95d3bf | 2015-04-14 21:39:44 -0700 | [diff] [blame] | 1917 | for (int i = 0; i < size; i++) { |
Jeff Sharkey | b36586a | 2015-04-27 08:42:28 -0700 | [diff] [blame] | 1918 | final VolumeRecord rec = mRecords.valueAt(i); |
| 1919 | writeVolumeRecord(out, rec); |
Jeff Sharkey | d95d3bf | 2015-04-14 21:39:44 -0700 | [diff] [blame] | 1920 | } |
| 1921 | out.endTag(null, TAG_VOLUMES); |
| 1922 | out.endDocument(); |
| 1923 | |
Jeff Sharkey | b36586a | 2015-04-27 08:42:28 -0700 | [diff] [blame] | 1924 | mSettingsFile.finishWrite(fos); |
Jeff Sharkey | d95d3bf | 2015-04-14 21:39:44 -0700 | [diff] [blame] | 1925 | } catch (IOException e) { |
| 1926 | if (fos != null) { |
Jeff Sharkey | b36586a | 2015-04-27 08:42:28 -0700 | [diff] [blame] | 1927 | mSettingsFile.failWrite(fos); |
Jeff Sharkey | d95d3bf | 2015-04-14 21:39:44 -0700 | [diff] [blame] | 1928 | } |
| 1929 | } |
| 1930 | } |
| 1931 | |
Jeff Sharkey | b36586a | 2015-04-27 08:42:28 -0700 | [diff] [blame] | 1932 | public static VolumeRecord readVolumeRecord(XmlPullParser in) throws IOException { |
| 1933 | final int type = readIntAttribute(in, ATTR_TYPE); |
| 1934 | final String fsUuid = readStringAttribute(in, ATTR_FS_UUID); |
| 1935 | final VolumeRecord meta = new VolumeRecord(type, fsUuid); |
Jeff Sharkey | 5cc0df2 | 2015-06-17 19:44:05 -0700 | [diff] [blame] | 1936 | meta.partGuid = readStringAttribute(in, ATTR_PART_GUID); |
Jeff Sharkey | b36586a | 2015-04-27 08:42:28 -0700 | [diff] [blame] | 1937 | meta.nickname = readStringAttribute(in, ATTR_NICKNAME); |
| 1938 | meta.userFlags = readIntAttribute(in, ATTR_USER_FLAGS); |
Jeff Sharkey | 3811f35 | 2019-05-14 11:54:36 -0600 | [diff] [blame] | 1939 | meta.createdMillis = readLongAttribute(in, ATTR_CREATED_MILLIS, 0); |
| 1940 | meta.lastSeenMillis = readLongAttribute(in, ATTR_LAST_SEEN_MILLIS, 0); |
| 1941 | meta.lastTrimMillis = readLongAttribute(in, ATTR_LAST_TRIM_MILLIS, 0); |
| 1942 | meta.lastBenchMillis = readLongAttribute(in, ATTR_LAST_BENCH_MILLIS, 0); |
Jeff Sharkey | b36586a | 2015-04-27 08:42:28 -0700 | [diff] [blame] | 1943 | return meta; |
| 1944 | } |
| 1945 | |
| 1946 | public static void writeVolumeRecord(XmlSerializer out, VolumeRecord rec) throws IOException { |
| 1947 | out.startTag(null, TAG_VOLUME); |
| 1948 | writeIntAttribute(out, ATTR_TYPE, rec.type); |
| 1949 | writeStringAttribute(out, ATTR_FS_UUID, rec.fsUuid); |
Jeff Sharkey | 5cc0df2 | 2015-06-17 19:44:05 -0700 | [diff] [blame] | 1950 | writeStringAttribute(out, ATTR_PART_GUID, rec.partGuid); |
Jeff Sharkey | b36586a | 2015-04-27 08:42:28 -0700 | [diff] [blame] | 1951 | writeStringAttribute(out, ATTR_NICKNAME, rec.nickname); |
| 1952 | writeIntAttribute(out, ATTR_USER_FLAGS, rec.userFlags); |
Jeff Sharkey | e8a4b66 | 2015-06-27 15:43:45 -0700 | [diff] [blame] | 1953 | writeLongAttribute(out, ATTR_CREATED_MILLIS, rec.createdMillis); |
Jeff Sharkey | 3811f35 | 2019-05-14 11:54:36 -0600 | [diff] [blame] | 1954 | writeLongAttribute(out, ATTR_LAST_SEEN_MILLIS, rec.lastSeenMillis); |
Jeff Sharkey | e8a4b66 | 2015-06-27 15:43:45 -0700 | [diff] [blame] | 1955 | writeLongAttribute(out, ATTR_LAST_TRIM_MILLIS, rec.lastTrimMillis); |
| 1956 | writeLongAttribute(out, ATTR_LAST_BENCH_MILLIS, rec.lastBenchMillis); |
Jeff Sharkey | b36586a | 2015-04-27 08:42:28 -0700 | [diff] [blame] | 1957 | out.endTag(null, TAG_VOLUME); |
| 1958 | } |
| 1959 | |
San Mehat | 207e538 | 2010-02-04 20:46:54 -0800 | [diff] [blame] | 1960 | /** |
San Mehat | 4270e1e | 2010-01-29 05:32:19 -0800 | [diff] [blame] | 1961 | * Exposed API calls below here |
| 1962 | */ |
| 1963 | |
Jeff Sharkey | 7151a9a | 2015-04-04 15:22:37 -0700 | [diff] [blame] | 1964 | @Override |
Sudheer Shanka | 2250d56 | 2016-11-07 15:41:02 -0800 | [diff] [blame] | 1965 | public void registerListener(IStorageEventListener listener) { |
Jeff Sharkey | 7151a9a | 2015-04-04 15:22:37 -0700 | [diff] [blame] | 1966 | mCallbacks.register(listener); |
San Mehat | 4270e1e | 2010-01-29 05:32:19 -0800 | [diff] [blame] | 1967 | } |
| 1968 | |
Jeff Sharkey | 7151a9a | 2015-04-04 15:22:37 -0700 | [diff] [blame] | 1969 | @Override |
Sudheer Shanka | 2250d56 | 2016-11-07 15:41:02 -0800 | [diff] [blame] | 1970 | public void unregisterListener(IStorageEventListener listener) { |
Jeff Sharkey | 7151a9a | 2015-04-04 15:22:37 -0700 | [diff] [blame] | 1971 | mCallbacks.unregister(listener); |
San Mehat | 4270e1e | 2010-01-29 05:32:19 -0800 | [diff] [blame] | 1972 | } |
| 1973 | |
Jeff Sharkey | 4887789 | 2015-03-18 11:27:19 -0700 | [diff] [blame] | 1974 | @Override |
Sudheer Shanka | 2250d56 | 2016-11-07 15:41:02 -0800 | [diff] [blame] | 1975 | public void shutdown(final IStorageShutdownObserver observer) { |
Jeff Sharkey | 4887789 | 2015-03-18 11:27:19 -0700 | [diff] [blame] | 1976 | enforcePermission(android.Manifest.permission.SHUTDOWN); |
San Mehat | 4270e1e | 2010-01-29 05:32:19 -0800 | [diff] [blame] | 1977 | |
San Mehat | a507859 | 2010-03-25 09:36:54 -0700 | [diff] [blame] | 1978 | Slog.i(TAG, "Shutting down"); |
Jeff Sharkey | 4887789 | 2015-03-18 11:27:19 -0700 | [diff] [blame] | 1979 | mHandler.obtainMessage(H_SHUTDOWN, observer).sendToTarget(); |
San Mehat | 4270e1e | 2010-01-29 05:32:19 -0800 | [diff] [blame] | 1980 | } |
| 1981 | |
Jeff Sharkey | 4887789 | 2015-03-18 11:27:19 -0700 | [diff] [blame] | 1982 | @Override |
Jeff Sharkey | 7151a9a | 2015-04-04 15:22:37 -0700 | [diff] [blame] | 1983 | public void mount(String volId) { |
| 1984 | enforcePermission(android.Manifest.permission.MOUNT_UNMOUNT_FILESYSTEMS); |
Jeff Sharkey | 7151a9a | 2015-04-04 15:22:37 -0700 | [diff] [blame] | 1985 | |
Jeff Sharkey | ef10ee0 | 2015-07-05 14:17:27 -0700 | [diff] [blame] | 1986 | final VolumeInfo vol = findVolumeByIdOrThrow(volId); |
Jeff Sharkey | 2e606d7 | 2015-07-27 14:19:54 -0700 | [diff] [blame] | 1987 | if (isMountDisallowed(vol)) { |
| 1988 | throw new SecurityException("Mounting " + volId + " restricted by policy"); |
Jeff Sharkey | 7151a9a | 2015-04-04 15:22:37 -0700 | [diff] [blame] | 1989 | } |
Zim | 17be6f9 | 2019-09-25 14:37:55 +0100 | [diff] [blame] | 1990 | |
Sudheer Shanka | 3f0645b | 2018-09-18 13:07:59 -0700 | [diff] [blame] | 1991 | mount(vol); |
| 1992 | } |
| 1993 | |
Zim | 17be6f9 | 2019-09-25 14:37:55 +0100 | [diff] [blame] | 1994 | private void mount(VolumeInfo vol) { |
Jeff Sharkey | 7151a9a | 2015-04-04 15:22:37 -0700 | [diff] [blame] | 1995 | try { |
Zim | 17be6f9 | 2019-09-25 14:37:55 +0100 | [diff] [blame] | 1996 | // TODO(b/135341433): Remove paranoid logging when FUSE is stable |
| 1997 | Slog.i(TAG, "Mounting volume " + vol); |
Zim | 95eca1d | 2019-11-15 18:03:00 +0000 | [diff] [blame] | 1998 | mVold.mount(vol.id, vol.mountFlags, vol.mountUserId, new IVoldMountCallback.Stub() { |
| 1999 | @Override |
Zim | 7924a51 | 2020-01-15 15:23:54 +0000 | [diff] [blame] | 2000 | public boolean onVolumeChecking(FileDescriptor fd, String path, |
Zim | 95eca1d | 2019-11-15 18:03:00 +0000 | [diff] [blame] | 2001 | String internalPath) { |
| 2002 | vol.path = path; |
| 2003 | vol.internalPath = internalPath; |
Zim | 7924a51 | 2020-01-15 15:23:54 +0000 | [diff] [blame] | 2004 | ParcelFileDescriptor pfd = new ParcelFileDescriptor(fd); |
Zim | 95eca1d | 2019-11-15 18:03:00 +0000 | [diff] [blame] | 2005 | try { |
Zim | 7924a51 | 2020-01-15 15:23:54 +0000 | [diff] [blame] | 2006 | mStorageSessionController.onVolumeMount(pfd, vol); |
Zim | 95eca1d | 2019-11-15 18:03:00 +0000 | [diff] [blame] | 2007 | return true; |
| 2008 | } catch (ExternalStorageServiceException e) { |
Zim | 7924a51 | 2020-01-15 15:23:54 +0000 | [diff] [blame] | 2009 | Slog.e(TAG, "Failed to mount volume " + vol, e); |
| 2010 | |
Zim | b8ca054 | 2020-01-16 22:54:48 +0000 | [diff] [blame] | 2011 | int nextResetSeconds = REMOTE_TIMEOUT_SECONDS * 2; |
| 2012 | Slog.i(TAG, "Scheduling reset in " + nextResetSeconds + "s"); |
Zim | 7924a51 | 2020-01-15 15:23:54 +0000 | [diff] [blame] | 2013 | mHandler.removeMessages(H_RESET); |
| 2014 | mHandler.sendMessageDelayed(mHandler.obtainMessage(H_RESET), |
Zim | b8ca054 | 2020-01-16 22:54:48 +0000 | [diff] [blame] | 2015 | TimeUnit.SECONDS.toMillis(nextResetSeconds)); |
Zim | 95eca1d | 2019-11-15 18:03:00 +0000 | [diff] [blame] | 2016 | return false; |
Zim | 7924a51 | 2020-01-15 15:23:54 +0000 | [diff] [blame] | 2017 | } finally { |
| 2018 | try { |
| 2019 | pfd.close(); |
| 2020 | } catch (Exception e) { |
| 2021 | Slog.e(TAG, "Failed to close FUSE device fd", e); |
| 2022 | } |
Zim | 95eca1d | 2019-11-15 18:03:00 +0000 | [diff] [blame] | 2023 | } |
| 2024 | } |
| 2025 | }); |
Zim | 17be6f9 | 2019-09-25 14:37:55 +0100 | [diff] [blame] | 2026 | Slog.i(TAG, "Mounted volume " + vol); |
Jeff Sharkey | ace874b | 2017-09-07 15:27:33 -0600 | [diff] [blame] | 2027 | } catch (Exception e) { |
| 2028 | Slog.wtf(TAG, e); |
Jeff Sharkey | 7151a9a | 2015-04-04 15:22:37 -0700 | [diff] [blame] | 2029 | } |
| 2030 | } |
| 2031 | |
| 2032 | @Override |
| 2033 | public void unmount(String volId) { |
| 2034 | enforcePermission(android.Manifest.permission.MOUNT_UNMOUNT_FILESYSTEMS); |
Jeff Sharkey | 7151a9a | 2015-04-04 15:22:37 -0700 | [diff] [blame] | 2035 | |
Jeff Sharkey | ef10ee0 | 2015-07-05 14:17:27 -0700 | [diff] [blame] | 2036 | final VolumeInfo vol = findVolumeByIdOrThrow(volId); |
Sudheer Shanka | 3f0645b | 2018-09-18 13:07:59 -0700 | [diff] [blame] | 2037 | unmount(vol); |
| 2038 | } |
| 2039 | |
| 2040 | private void unmount(VolumeInfo vol) { |
Jeff Sharkey | 7151a9a | 2015-04-04 15:22:37 -0700 | [diff] [blame] | 2041 | try { |
Jeff Sharkey | 5440279 | 2017-09-15 16:05:19 -0600 | [diff] [blame] | 2042 | mVold.unmount(vol.id); |
Zim | 17be6f9 | 2019-09-25 14:37:55 +0100 | [diff] [blame] | 2043 | mStorageSessionController.onVolumeUnmount(vol); |
Ricky Wai | 4482ab5 | 2019-12-10 19:08:18 +0000 | [diff] [blame] | 2044 | try { |
| 2045 | if (vol.type == VolumeInfo.TYPE_PRIVATE) { |
| 2046 | mInstaller.onPrivateVolumeRemoved(vol.getFsUuid()); |
| 2047 | } |
| 2048 | } catch (Installer.InstallerException e) { |
| 2049 | Slog.e(TAG, "Failed unmount mirror data", e); |
| 2050 | } |
Jeff Sharkey | ace874b | 2017-09-07 15:27:33 -0600 | [diff] [blame] | 2051 | } catch (Exception e) { |
| 2052 | Slog.wtf(TAG, e); |
Jeff Sharkey | 7151a9a | 2015-04-04 15:22:37 -0700 | [diff] [blame] | 2053 | } |
| 2054 | } |
| 2055 | |
| 2056 | @Override |
| 2057 | public void format(String volId) { |
Jeff Sharkey | 4887789 | 2015-03-18 11:27:19 -0700 | [diff] [blame] | 2058 | enforcePermission(android.Manifest.permission.MOUNT_FORMAT_FILESYSTEMS); |
San Mehat | 5b77dab | 2010-01-26 13:28:50 -0800 | [diff] [blame] | 2059 | |
Jeff Sharkey | ef10ee0 | 2015-07-05 14:17:27 -0700 | [diff] [blame] | 2060 | final VolumeInfo vol = findVolumeByIdOrThrow(volId); |
Jeff Sharkey | 7151a9a | 2015-04-04 15:22:37 -0700 | [diff] [blame] | 2061 | try { |
Jeff Sharkey | 5440279 | 2017-09-15 16:05:19 -0600 | [diff] [blame] | 2062 | mVold.format(vol.id, "auto"); |
Jeff Sharkey | ace874b | 2017-09-07 15:27:33 -0600 | [diff] [blame] | 2063 | } catch (Exception e) { |
| 2064 | Slog.wtf(TAG, e); |
Jeff Sharkey | 4887789 | 2015-03-18 11:27:19 -0700 | [diff] [blame] | 2065 | } |
Jeff Sharkey | 7151a9a | 2015-04-04 15:22:37 -0700 | [diff] [blame] | 2066 | } |
| 2067 | |
| 2068 | @Override |
Jeff Sharkey | 7e19f53 | 2017-11-06 13:54:11 -0700 | [diff] [blame] | 2069 | public void benchmark(String volId, IVoldTaskListener listener) { |
Jeff Sharkey | 9756d75 | 2015-05-14 21:07:42 -0700 | [diff] [blame] | 2070 | enforcePermission(android.Manifest.permission.MOUNT_FORMAT_FILESYSTEMS); |
Jeff Sharkey | 9756d75 | 2015-05-14 21:07:42 -0700 | [diff] [blame] | 2071 | |
| 2072 | try { |
Jeff Sharkey | b302c54 | 2017-09-15 12:57:59 -0600 | [diff] [blame] | 2073 | mVold.benchmark(volId, new IVoldTaskListener.Stub() { |
| 2074 | @Override |
| 2075 | public void onStatus(int status, PersistableBundle extras) { |
Jeff Sharkey | 7e19f53 | 2017-11-06 13:54:11 -0700 | [diff] [blame] | 2076 | dispatchOnStatus(listener, status, extras); |
Jeff Sharkey | b302c54 | 2017-09-15 12:57:59 -0600 | [diff] [blame] | 2077 | } |
| 2078 | |
| 2079 | @Override |
| 2080 | public void onFinished(int status, PersistableBundle extras) { |
Jeff Sharkey | 7e19f53 | 2017-11-06 13:54:11 -0700 | [diff] [blame] | 2081 | dispatchOnFinished(listener, status, extras); |
Jeff Sharkey | b302c54 | 2017-09-15 12:57:59 -0600 | [diff] [blame] | 2082 | |
| 2083 | final String path = extras.getString("path"); |
| 2084 | final String ident = extras.getString("ident"); |
| 2085 | final long create = extras.getLong("create"); |
| 2086 | final long run = extras.getLong("run"); |
| 2087 | final long destroy = extras.getLong("destroy"); |
| 2088 | |
| 2089 | final DropBoxManager dropBox = mContext.getSystemService(DropBoxManager.class); |
| 2090 | dropBox.addText(TAG_STORAGE_BENCHMARK, scrubPath(path) |
| 2091 | + " " + ident + " " + create + " " + run + " " + destroy); |
| 2092 | |
| 2093 | synchronized (mLock) { |
| 2094 | final VolumeRecord rec = findRecordForPath(path); |
| 2095 | if (rec != null) { |
| 2096 | rec.lastBenchMillis = System.currentTimeMillis(); |
| 2097 | writeSettingsLocked(); |
| 2098 | } |
| 2099 | } |
| 2100 | } |
| 2101 | }); |
Jeff Sharkey | 7e19f53 | 2017-11-06 13:54:11 -0700 | [diff] [blame] | 2102 | } catch (RemoteException e) { |
| 2103 | throw e.rethrowAsRuntimeException(); |
Jeff Sharkey | 9756d75 | 2015-05-14 21:07:42 -0700 | [diff] [blame] | 2104 | } |
| 2105 | } |
| 2106 | |
| 2107 | @Override |
Jeff Sharkey | 7151a9a | 2015-04-04 15:22:37 -0700 | [diff] [blame] | 2108 | public void partitionPublic(String diskId) { |
| 2109 | enforcePermission(android.Manifest.permission.MOUNT_FORMAT_FILESYSTEMS); |
Jeff Sharkey | 7151a9a | 2015-04-04 15:22:37 -0700 | [diff] [blame] | 2110 | |
Jeff Sharkey | eba260d | 2015-04-19 14:35:16 -0700 | [diff] [blame] | 2111 | final CountDownLatch latch = findOrCreateDiskScanLatch(diskId); |
Jeff Sharkey | 7151a9a | 2015-04-04 15:22:37 -0700 | [diff] [blame] | 2112 | try { |
Jeff Sharkey | 5440279 | 2017-09-15 16:05:19 -0600 | [diff] [blame] | 2113 | mVold.partition(diskId, IVold.PARTITION_TYPE_PUBLIC, -1); |
Jeff Sharkey | edcdaf6 | 2015-07-09 09:45:36 -0700 | [diff] [blame] | 2114 | waitForLatch(latch, "partitionPublic", 3 * DateUtils.MINUTE_IN_MILLIS); |
Jeff Sharkey | ace874b | 2017-09-07 15:27:33 -0600 | [diff] [blame] | 2115 | } catch (Exception e) { |
| 2116 | Slog.wtf(TAG, e); |
Jeff Sharkey | 7151a9a | 2015-04-04 15:22:37 -0700 | [diff] [blame] | 2117 | } |
| 2118 | } |
| 2119 | |
| 2120 | @Override |
| 2121 | public void partitionPrivate(String diskId) { |
| 2122 | enforcePermission(android.Manifest.permission.MOUNT_FORMAT_FILESYSTEMS); |
Amith Yamasani | 462ac3a | 2015-06-30 14:21:01 -0700 | [diff] [blame] | 2123 | enforceAdminUser(); |
Jeff Sharkey | 7151a9a | 2015-04-04 15:22:37 -0700 | [diff] [blame] | 2124 | |
Jeff Sharkey | eba260d | 2015-04-19 14:35:16 -0700 | [diff] [blame] | 2125 | final CountDownLatch latch = findOrCreateDiskScanLatch(diskId); |
Jeff Sharkey | 7151a9a | 2015-04-04 15:22:37 -0700 | [diff] [blame] | 2126 | try { |
Jeff Sharkey | 5440279 | 2017-09-15 16:05:19 -0600 | [diff] [blame] | 2127 | mVold.partition(diskId, IVold.PARTITION_TYPE_PRIVATE, -1); |
Jeff Sharkey | edcdaf6 | 2015-07-09 09:45:36 -0700 | [diff] [blame] | 2128 | waitForLatch(latch, "partitionPrivate", 3 * DateUtils.MINUTE_IN_MILLIS); |
Jeff Sharkey | ace874b | 2017-09-07 15:27:33 -0600 | [diff] [blame] | 2129 | } catch (Exception e) { |
| 2130 | Slog.wtf(TAG, e); |
Jeff Sharkey | 7151a9a | 2015-04-04 15:22:37 -0700 | [diff] [blame] | 2131 | } |
| 2132 | } |
| 2133 | |
| 2134 | @Override |
| 2135 | public void partitionMixed(String diskId, int ratio) { |
| 2136 | enforcePermission(android.Manifest.permission.MOUNT_FORMAT_FILESYSTEMS); |
Amith Yamasani | 462ac3a | 2015-06-30 14:21:01 -0700 | [diff] [blame] | 2137 | enforceAdminUser(); |
Jeff Sharkey | 7151a9a | 2015-04-04 15:22:37 -0700 | [diff] [blame] | 2138 | |
Jeff Sharkey | eba260d | 2015-04-19 14:35:16 -0700 | [diff] [blame] | 2139 | final CountDownLatch latch = findOrCreateDiskScanLatch(diskId); |
Jeff Sharkey | 7151a9a | 2015-04-04 15:22:37 -0700 | [diff] [blame] | 2140 | try { |
Jeff Sharkey | 5440279 | 2017-09-15 16:05:19 -0600 | [diff] [blame] | 2141 | mVold.partition(diskId, IVold.PARTITION_TYPE_MIXED, ratio); |
Jeff Sharkey | edcdaf6 | 2015-07-09 09:45:36 -0700 | [diff] [blame] | 2142 | waitForLatch(latch, "partitionMixed", 3 * DateUtils.MINUTE_IN_MILLIS); |
Jeff Sharkey | ace874b | 2017-09-07 15:27:33 -0600 | [diff] [blame] | 2143 | } catch (Exception e) { |
| 2144 | Slog.wtf(TAG, e); |
Jeff Sharkey | 7151a9a | 2015-04-04 15:22:37 -0700 | [diff] [blame] | 2145 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2146 | } |
| 2147 | |
Jeff Sharkey | 4887789 | 2015-03-18 11:27:19 -0700 | [diff] [blame] | 2148 | @Override |
Jeff Sharkey | b36586a | 2015-04-27 08:42:28 -0700 | [diff] [blame] | 2149 | public void setVolumeNickname(String fsUuid, String nickname) { |
Jeff Sharkey | d95d3bf | 2015-04-14 21:39:44 -0700 | [diff] [blame] | 2150 | enforcePermission(android.Manifest.permission.MOUNT_UNMOUNT_FILESYSTEMS); |
Jeff Sharkey | d95d3bf | 2015-04-14 21:39:44 -0700 | [diff] [blame] | 2151 | |
Daulet Zhanguzin | ea1a7ca | 2020-01-03 09:46:50 +0000 | [diff] [blame] | 2152 | Objects.requireNonNull(fsUuid); |
Jeff Sharkey | d95d3bf | 2015-04-14 21:39:44 -0700 | [diff] [blame] | 2153 | synchronized (mLock) { |
Jeff Sharkey | b36586a | 2015-04-27 08:42:28 -0700 | [diff] [blame] | 2154 | final VolumeRecord rec = mRecords.get(fsUuid); |
| 2155 | rec.nickname = nickname; |
Jeff Sharkey | 50a0545 | 2015-04-29 11:24:52 -0700 | [diff] [blame] | 2156 | mCallbacks.notifyVolumeRecordChanged(rec); |
Jeff Sharkey | b36586a | 2015-04-27 08:42:28 -0700 | [diff] [blame] | 2157 | writeSettingsLocked(); |
Jeff Sharkey | d95d3bf | 2015-04-14 21:39:44 -0700 | [diff] [blame] | 2158 | } |
| 2159 | } |
| 2160 | |
| 2161 | @Override |
Jeff Sharkey | b36586a | 2015-04-27 08:42:28 -0700 | [diff] [blame] | 2162 | public void setVolumeUserFlags(String fsUuid, int flags, int mask) { |
Jeff Sharkey | d95d3bf | 2015-04-14 21:39:44 -0700 | [diff] [blame] | 2163 | enforcePermission(android.Manifest.permission.MOUNT_UNMOUNT_FILESYSTEMS); |
Jeff Sharkey | d95d3bf | 2015-04-14 21:39:44 -0700 | [diff] [blame] | 2164 | |
Daulet Zhanguzin | ea1a7ca | 2020-01-03 09:46:50 +0000 | [diff] [blame] | 2165 | Objects.requireNonNull(fsUuid); |
Jeff Sharkey | d95d3bf | 2015-04-14 21:39:44 -0700 | [diff] [blame] | 2166 | synchronized (mLock) { |
Jeff Sharkey | b36586a | 2015-04-27 08:42:28 -0700 | [diff] [blame] | 2167 | final VolumeRecord rec = mRecords.get(fsUuid); |
| 2168 | rec.userFlags = (rec.userFlags & ~mask) | (flags & mask); |
Jeff Sharkey | 50a0545 | 2015-04-29 11:24:52 -0700 | [diff] [blame] | 2169 | mCallbacks.notifyVolumeRecordChanged(rec); |
Jeff Sharkey | b36586a | 2015-04-27 08:42:28 -0700 | [diff] [blame] | 2170 | writeSettingsLocked(); |
| 2171 | } |
| 2172 | } |
| 2173 | |
| 2174 | @Override |
| 2175 | public void forgetVolume(String fsUuid) { |
| 2176 | enforcePermission(android.Manifest.permission.MOUNT_UNMOUNT_FILESYSTEMS); |
Jeff Sharkey | b36586a | 2015-04-27 08:42:28 -0700 | [diff] [blame] | 2177 | |
Daulet Zhanguzin | ea1a7ca | 2020-01-03 09:46:50 +0000 | [diff] [blame] | 2178 | Objects.requireNonNull(fsUuid); |
Jeff Sharkey | 5a9bb74 | 2015-11-03 10:15:57 -0800 | [diff] [blame] | 2179 | |
Jeff Sharkey | b36586a | 2015-04-27 08:42:28 -0700 | [diff] [blame] | 2180 | synchronized (mLock) { |
Jeff Sharkey | 5cc0df2 | 2015-06-17 19:44:05 -0700 | [diff] [blame] | 2181 | final VolumeRecord rec = mRecords.remove(fsUuid); |
| 2182 | if (rec != null && !TextUtils.isEmpty(rec.partGuid)) { |
Jeff Sharkey | db4b619 | 2017-10-24 11:08:50 -0600 | [diff] [blame] | 2183 | mHandler.obtainMessage(H_PARTITION_FORGET, rec).sendToTarget(); |
Jeff Sharkey | 5cc0df2 | 2015-06-17 19:44:05 -0700 | [diff] [blame] | 2184 | } |
| 2185 | mCallbacks.notifyVolumeForgotten(fsUuid); |
Jeff Sharkey | 50a0545 | 2015-04-29 11:24:52 -0700 | [diff] [blame] | 2186 | |
| 2187 | // If this had been primary storage, revert back to internal and |
| 2188 | // reset vold so we bind into new volume into place. |
| 2189 | if (Objects.equals(mPrimaryStorageUuid, fsUuid)) { |
Jeff Sharkey | fced534 | 2015-05-10 14:53:34 -0700 | [diff] [blame] | 2190 | mPrimaryStorageUuid = getDefaultPrimaryStorageUuid(); |
Jeff Sharkey | 5a9bb74 | 2015-11-03 10:15:57 -0800 | [diff] [blame] | 2191 | mHandler.obtainMessage(H_RESET).sendToTarget(); |
Jeff Sharkey | 50a0545 | 2015-04-29 11:24:52 -0700 | [diff] [blame] | 2192 | } |
| 2193 | |
Jeff Sharkey | b36586a | 2015-04-27 08:42:28 -0700 | [diff] [blame] | 2194 | writeSettingsLocked(); |
Jeff Sharkey | d95d3bf | 2015-04-14 21:39:44 -0700 | [diff] [blame] | 2195 | } |
| 2196 | } |
| 2197 | |
Jeff Sharkey | 7d2af54 | 2015-05-12 15:27:15 -0700 | [diff] [blame] | 2198 | @Override |
| 2199 | public void forgetAllVolumes() { |
| 2200 | enforcePermission(android.Manifest.permission.MOUNT_UNMOUNT_FILESYSTEMS); |
Jeff Sharkey | 7d2af54 | 2015-05-12 15:27:15 -0700 | [diff] [blame] | 2201 | |
Jeff Sharkey | 50a0545 | 2015-04-29 11:24:52 -0700 | [diff] [blame] | 2202 | synchronized (mLock) { |
| 2203 | for (int i = 0; i < mRecords.size(); i++) { |
| 2204 | final String fsUuid = mRecords.keyAt(i); |
Jeff Sharkey | 5cc0df2 | 2015-06-17 19:44:05 -0700 | [diff] [blame] | 2205 | final VolumeRecord rec = mRecords.valueAt(i); |
| 2206 | if (!TextUtils.isEmpty(rec.partGuid)) { |
Jeff Sharkey | db4b619 | 2017-10-24 11:08:50 -0600 | [diff] [blame] | 2207 | mHandler.obtainMessage(H_PARTITION_FORGET, rec).sendToTarget(); |
Jeff Sharkey | 5cc0df2 | 2015-06-17 19:44:05 -0700 | [diff] [blame] | 2208 | } |
Jeff Sharkey | 50a0545 | 2015-04-29 11:24:52 -0700 | [diff] [blame] | 2209 | mCallbacks.notifyVolumeForgotten(fsUuid); |
| 2210 | } |
Jeff Sharkey | 50a0545 | 2015-04-29 11:24:52 -0700 | [diff] [blame] | 2211 | mRecords.clear(); |
Jeff Sharkey | 50a0545 | 2015-04-29 11:24:52 -0700 | [diff] [blame] | 2212 | |
Jeff Sharkey | fced534 | 2015-05-10 14:53:34 -0700 | [diff] [blame] | 2213 | if (!Objects.equals(StorageManager.UUID_PRIVATE_INTERNAL, mPrimaryStorageUuid)) { |
| 2214 | mPrimaryStorageUuid = getDefaultPrimaryStorageUuid(); |
| 2215 | } |
| 2216 | |
| 2217 | writeSettingsLocked(); |
Jeff Sharkey | 5a9bb74 | 2015-11-03 10:15:57 -0800 | [diff] [blame] | 2218 | mHandler.obtainMessage(H_RESET).sendToTarget(); |
Jeff Sharkey | 50a0545 | 2015-04-29 11:24:52 -0700 | [diff] [blame] | 2219 | } |
| 2220 | } |
| 2221 | |
Jeff Sharkey | db4b619 | 2017-10-24 11:08:50 -0600 | [diff] [blame] | 2222 | private void forgetPartition(String partGuid, String fsUuid) { |
Jeff Sharkey | 5cc0df2 | 2015-06-17 19:44:05 -0700 | [diff] [blame] | 2223 | try { |
Jeff Sharkey | db4b619 | 2017-10-24 11:08:50 -0600 | [diff] [blame] | 2224 | mVold.forgetPartition(partGuid, fsUuid); |
Jeff Sharkey | ace874b | 2017-09-07 15:27:33 -0600 | [diff] [blame] | 2225 | } catch (Exception e) { |
| 2226 | Slog.wtf(TAG, e); |
Jeff Sharkey | 5cc0df2 | 2015-06-17 19:44:05 -0700 | [diff] [blame] | 2227 | } |
| 2228 | } |
| 2229 | |
Jeff Sharkey | 31d0b70 | 2016-11-21 14:16:53 -0700 | [diff] [blame] | 2230 | @Override |
Jeff Sharkey | 7e19f53 | 2017-11-06 13:54:11 -0700 | [diff] [blame] | 2231 | public void fstrim(int flags, IVoldTaskListener listener) { |
Jeff Sharkey | 31d0b70 | 2016-11-21 14:16:53 -0700 | [diff] [blame] | 2232 | enforcePermission(android.Manifest.permission.MOUNT_FORMAT_FILESYSTEMS); |
Jeff Sharkey | 31d0b70 | 2016-11-21 14:16:53 -0700 | [diff] [blame] | 2233 | |
| 2234 | try { |
Sandeep Patil | 7d5c491 | 2019-04-15 09:27:30 -0700 | [diff] [blame] | 2235 | // Block based checkpoint process runs fstrim. So, if checkpoint is in progress |
| 2236 | // (first boot after OTA), We skip idle maintenance and make sure the last |
| 2237 | // fstrim time is still updated. If file based checkpoints are used, we run |
| 2238 | // idle maintenance (GC + fstrim) regardless of checkpoint status. |
| 2239 | if (!needsCheckpoint() || !supportsBlockCheckpoint()) { |
| 2240 | mVold.fstrim(flags, new IVoldTaskListener.Stub() { |
| 2241 | @Override |
| 2242 | public void onStatus(int status, PersistableBundle extras) { |
| 2243 | dispatchOnStatus(listener, status, extras); |
Jeff Sharkey | 7e19f53 | 2017-11-06 13:54:11 -0700 | [diff] [blame] | 2244 | |
Sandeep Patil | 7d5c491 | 2019-04-15 09:27:30 -0700 | [diff] [blame] | 2245 | // Ignore trim failures |
| 2246 | if (status != 0) return; |
Jeff Sharkey | b302c54 | 2017-09-15 12:57:59 -0600 | [diff] [blame] | 2247 | |
Sandeep Patil | 7d5c491 | 2019-04-15 09:27:30 -0700 | [diff] [blame] | 2248 | final String path = extras.getString("path"); |
| 2249 | final long bytes = extras.getLong("bytes"); |
| 2250 | final long time = extras.getLong("time"); |
Jeff Sharkey | b302c54 | 2017-09-15 12:57:59 -0600 | [diff] [blame] | 2251 | |
Sandeep Patil | 7d5c491 | 2019-04-15 09:27:30 -0700 | [diff] [blame] | 2252 | final DropBoxManager dropBox = mContext.getSystemService(DropBoxManager.class); |
| 2253 | dropBox.addText(TAG_STORAGE_TRIM, scrubPath(path) + " " + bytes + " " + time); |
Jeff Sharkey | b302c54 | 2017-09-15 12:57:59 -0600 | [diff] [blame] | 2254 | |
Sandeep Patil | 7d5c491 | 2019-04-15 09:27:30 -0700 | [diff] [blame] | 2255 | synchronized (mLock) { |
| 2256 | final VolumeRecord rec = findRecordForPath(path); |
| 2257 | if (rec != null) { |
| 2258 | rec.lastTrimMillis = System.currentTimeMillis(); |
| 2259 | writeSettingsLocked(); |
| 2260 | } |
Jeff Sharkey | b302c54 | 2017-09-15 12:57:59 -0600 | [diff] [blame] | 2261 | } |
| 2262 | } |
Jeff Sharkey | b302c54 | 2017-09-15 12:57:59 -0600 | [diff] [blame] | 2263 | |
Sandeep Patil | 7d5c491 | 2019-04-15 09:27:30 -0700 | [diff] [blame] | 2264 | @Override |
| 2265 | public void onFinished(int status, PersistableBundle extras) { |
| 2266 | dispatchOnFinished(listener, status, extras); |
Jeff Sharkey | 7e19f53 | 2017-11-06 13:54:11 -0700 | [diff] [blame] | 2267 | |
Sandeep Patil | 7d5c491 | 2019-04-15 09:27:30 -0700 | [diff] [blame] | 2268 | // TODO: benchmark when desired |
| 2269 | } |
| 2270 | }); |
| 2271 | } else { |
| 2272 | Slog.i(TAG, "Skipping fstrim - block based checkpoint in progress"); |
| 2273 | } |
Jeff Sharkey | 7e19f53 | 2017-11-06 13:54:11 -0700 | [diff] [blame] | 2274 | } catch (RemoteException e) { |
| 2275 | throw e.rethrowAsRuntimeException(); |
Jeff Sharkey | 31d0b70 | 2016-11-21 14:16:53 -0700 | [diff] [blame] | 2276 | } |
| 2277 | } |
| 2278 | |
Jin Qian | a85b991 | 2017-10-17 15:48:18 -0700 | [diff] [blame] | 2279 | void runIdleMaint(Runnable callback) { |
| 2280 | enforcePermission(android.Manifest.permission.MOUNT_FORMAT_FILESYSTEMS); |
| 2281 | |
| 2282 | try { |
Sandeep Patil | 7d5c491 | 2019-04-15 09:27:30 -0700 | [diff] [blame] | 2283 | // Block based checkpoint process runs fstrim. So, if checkpoint is in progress |
| 2284 | // (first boot after OTA), We skip idle maintenance and make sure the last |
| 2285 | // fstrim time is still updated. If file based checkpoints are used, we run |
| 2286 | // idle maintenance (GC + fstrim) regardless of checkpoint status. |
| 2287 | if (!needsCheckpoint() || !supportsBlockCheckpoint()) { |
| 2288 | mVold.runIdleMaint(new IVoldTaskListener.Stub() { |
| 2289 | @Override |
| 2290 | public void onStatus(int status, PersistableBundle extras) { |
| 2291 | // Not currently used |
Jin Qian | a85b991 | 2017-10-17 15:48:18 -0700 | [diff] [blame] | 2292 | } |
Sandeep Patil | 7d5c491 | 2019-04-15 09:27:30 -0700 | [diff] [blame] | 2293 | @Override |
| 2294 | public void onFinished(int status, PersistableBundle extras) { |
| 2295 | if (callback != null) { |
| 2296 | BackgroundThread.getHandler().post(callback); |
| 2297 | } |
| 2298 | } |
| 2299 | }); |
| 2300 | } else { |
| 2301 | Slog.i(TAG, "Skipping idle maintenance - block based checkpoint in progress"); |
| 2302 | } |
Jin Qian | a85b991 | 2017-10-17 15:48:18 -0700 | [diff] [blame] | 2303 | } catch (Exception e) { |
| 2304 | Slog.wtf(TAG, e); |
| 2305 | } |
| 2306 | } |
| 2307 | |
| 2308 | @Override |
| 2309 | public void runIdleMaintenance() { |
| 2310 | runIdleMaint(null); |
| 2311 | } |
| 2312 | |
| 2313 | void abortIdleMaint(Runnable callback) { |
| 2314 | enforcePermission(android.Manifest.permission.MOUNT_FORMAT_FILESYSTEMS); |
| 2315 | |
| 2316 | try { |
| 2317 | mVold.abortIdleMaint(new IVoldTaskListener.Stub() { |
| 2318 | @Override |
| 2319 | public void onStatus(int status, PersistableBundle extras) { |
| 2320 | // Not currently used |
| 2321 | } |
| 2322 | @Override |
| 2323 | public void onFinished(int status, PersistableBundle extras) { |
| 2324 | if (callback != null) { |
| 2325 | BackgroundThread.getHandler().post(callback); |
| 2326 | } |
| 2327 | } |
| 2328 | }); |
| 2329 | } catch (Exception e) { |
| 2330 | Slog.wtf(TAG, e); |
| 2331 | } |
| 2332 | } |
| 2333 | |
| 2334 | @Override |
| 2335 | public void abortIdleMaintenance() { |
| 2336 | abortIdleMaint(null); |
| 2337 | } |
| 2338 | |
Svet Ganov | 6ee871e | 2015-07-10 14:29:33 -0700 | [diff] [blame] | 2339 | private void remountUidExternalStorage(int uid, int mode) { |
Zim | 4f1a24d | 2019-11-27 16:35:58 +0000 | [diff] [blame] | 2340 | if (uid == Process.SYSTEM_UID) { |
| 2341 | // No need to remount uid for system because it has all access anyways |
| 2342 | return; |
| 2343 | } |
| 2344 | |
Jeff Sharkey | 9527b22 | 2015-06-24 15:24:48 -0700 | [diff] [blame] | 2345 | try { |
Jeff Sharkey | 5440279 | 2017-09-15 16:05:19 -0600 | [diff] [blame] | 2346 | mVold.remountUid(uid, mode); |
Jeff Sharkey | ace874b | 2017-09-07 15:27:33 -0600 | [diff] [blame] | 2347 | } catch (Exception e) { |
| 2348 | Slog.wtf(TAG, e); |
Jeff Sharkey | 9527b22 | 2015-06-24 15:24:48 -0700 | [diff] [blame] | 2349 | } |
| 2350 | } |
| 2351 | |
| 2352 | @Override |
Jeff Sharkey | 4c099d0 | 2015-05-15 13:45:00 -0700 | [diff] [blame] | 2353 | public void setDebugFlags(int flags, int mask) { |
| 2354 | enforcePermission(android.Manifest.permission.MOUNT_UNMOUNT_FILESYSTEMS); |
Jeff Sharkey | 4c099d0 | 2015-05-15 13:45:00 -0700 | [diff] [blame] | 2355 | |
Jeff Sharkey | ba51235 | 2015-11-12 20:17:45 -0800 | [diff] [blame] | 2356 | if ((mask & StorageManager.DEBUG_EMULATE_FBE) != 0) { |
Jeff Sharkey | 00455bf | 2016-11-04 14:45:24 -0600 | [diff] [blame] | 2357 | if (!EMULATE_FBE_SUPPORTED) { |
| 2358 | throw new IllegalStateException( |
| 2359 | "Emulation not supported on this device"); |
| 2360 | } |
Paul Lawrence | 20be5d6 | 2016-02-26 13:51:17 -0800 | [diff] [blame] | 2361 | if (StorageManager.isFileEncryptedNativeOnly()) { |
Jeff Sharkey | ce14cd0 | 2015-12-07 15:35:42 -0700 | [diff] [blame] | 2362 | throw new IllegalStateException( |
Jeff Sharkey | 00455bf | 2016-11-04 14:45:24 -0600 | [diff] [blame] | 2363 | "Emulation not supported on device with native FBE"); |
Jeff Sharkey | ce14cd0 | 2015-12-07 15:35:42 -0700 | [diff] [blame] | 2364 | } |
Jeff Sharkey | 5a78516 | 2016-03-21 13:02:06 -0600 | [diff] [blame] | 2365 | if (mLockPatternUtils.isCredentialRequiredToDecrypt(false)) { |
| 2366 | throw new IllegalStateException( |
| 2367 | "Emulation requires disabling 'Secure start-up' in Settings > Security"); |
| 2368 | } |
Jeff Sharkey | ce14cd0 | 2015-12-07 15:35:42 -0700 | [diff] [blame] | 2369 | |
Jeff Sharkey | 1176e51 | 2016-02-29 17:01:26 -0700 | [diff] [blame] | 2370 | final long token = Binder.clearCallingIdentity(); |
| 2371 | try { |
| 2372 | final boolean emulateFbe = (flags & StorageManager.DEBUG_EMULATE_FBE) != 0; |
| 2373 | SystemProperties.set(StorageManager.PROP_EMULATE_FBE, Boolean.toString(emulateFbe)); |
Jeff Sharkey | ce14cd0 | 2015-12-07 15:35:42 -0700 | [diff] [blame] | 2374 | |
Jeff Sharkey | 1176e51 | 2016-02-29 17:01:26 -0700 | [diff] [blame] | 2375 | // Perform hard reboot to kick policy into place |
| 2376 | mContext.getSystemService(PowerManager.class).reboot(null); |
| 2377 | } finally { |
| 2378 | Binder.restoreCallingIdentity(token); |
| 2379 | } |
Jeff Sharkey | ba51235 | 2015-11-12 20:17:45 -0800 | [diff] [blame] | 2380 | } |
Jeff Sharkey | 4c099d0 | 2015-05-15 13:45:00 -0700 | [diff] [blame] | 2381 | |
Jeff Sharkey | 901c042 | 2018-04-20 13:11:20 -0600 | [diff] [blame] | 2382 | if ((mask & (StorageManager.DEBUG_ADOPTABLE_FORCE_ON |
| 2383 | | StorageManager.DEBUG_ADOPTABLE_FORCE_OFF)) != 0) { |
| 2384 | final String value; |
| 2385 | if ((flags & StorageManager.DEBUG_ADOPTABLE_FORCE_ON) != 0) { |
| 2386 | value = "force_on"; |
| 2387 | } else if ((flags & StorageManager.DEBUG_ADOPTABLE_FORCE_OFF) != 0) { |
| 2388 | value = "force_off"; |
| 2389 | } else { |
| 2390 | value = ""; |
| 2391 | } |
Jeff Sharkey | ba51235 | 2015-11-12 20:17:45 -0800 | [diff] [blame] | 2392 | |
Jeff Sharkey | 901c042 | 2018-04-20 13:11:20 -0600 | [diff] [blame] | 2393 | final long token = Binder.clearCallingIdentity(); |
| 2394 | try { |
| 2395 | SystemProperties.set(StorageManager.PROP_ADOPTABLE, value); |
| 2396 | |
| 2397 | // Reset storage to kick new setting into place |
Jeff Sharkey | ba51235 | 2015-11-12 20:17:45 -0800 | [diff] [blame] | 2398 | mHandler.obtainMessage(H_RESET).sendToTarget(); |
Jeff Sharkey | 901c042 | 2018-04-20 13:11:20 -0600 | [diff] [blame] | 2399 | } finally { |
| 2400 | Binder.restoreCallingIdentity(token); |
Jeff Sharkey | ba51235 | 2015-11-12 20:17:45 -0800 | [diff] [blame] | 2401 | } |
Jeff Sharkey | 4c099d0 | 2015-05-15 13:45:00 -0700 | [diff] [blame] | 2402 | } |
Jeff Sharkey | 33dd156 | 2016-04-07 11:05:33 -0600 | [diff] [blame] | 2403 | |
| 2404 | if ((mask & (StorageManager.DEBUG_SDCARDFS_FORCE_ON |
| 2405 | | StorageManager.DEBUG_SDCARDFS_FORCE_OFF)) != 0) { |
| 2406 | final String value; |
| 2407 | if ((flags & StorageManager.DEBUG_SDCARDFS_FORCE_ON) != 0) { |
| 2408 | value = "force_on"; |
| 2409 | } else if ((flags & StorageManager.DEBUG_SDCARDFS_FORCE_OFF) != 0) { |
| 2410 | value = "force_off"; |
| 2411 | } else { |
| 2412 | value = ""; |
| 2413 | } |
| 2414 | |
| 2415 | final long token = Binder.clearCallingIdentity(); |
| 2416 | try { |
| 2417 | SystemProperties.set(StorageManager.PROP_SDCARDFS, value); |
| 2418 | |
| 2419 | // Reset storage to kick new setting into place |
| 2420 | mHandler.obtainMessage(H_RESET).sendToTarget(); |
| 2421 | } finally { |
| 2422 | Binder.restoreCallingIdentity(token); |
| 2423 | } |
| 2424 | } |
Jeff Sharkey | e53e2d9 | 2017-03-25 23:14:06 -0600 | [diff] [blame] | 2425 | |
| 2426 | if ((mask & StorageManager.DEBUG_VIRTUAL_DISK) != 0) { |
| 2427 | final boolean enabled = (flags & StorageManager.DEBUG_VIRTUAL_DISK) != 0; |
| 2428 | |
| 2429 | final long token = Binder.clearCallingIdentity(); |
| 2430 | try { |
| 2431 | SystemProperties.set(StorageManager.PROP_VIRTUAL_DISK, Boolean.toString(enabled)); |
| 2432 | |
| 2433 | // Reset storage to kick new setting into place |
| 2434 | mHandler.obtainMessage(H_RESET).sendToTarget(); |
| 2435 | } finally { |
| 2436 | Binder.restoreCallingIdentity(token); |
| 2437 | } |
| 2438 | } |
Sudheer Shanka | be0febe | 2018-11-07 18:24:37 -0800 | [diff] [blame] | 2439 | |
Jeff Sharkey | b0c363b2 | 2018-12-15 11:53:03 -0700 | [diff] [blame] | 2440 | if ((mask & (StorageManager.DEBUG_ISOLATED_STORAGE_FORCE_ON |
| 2441 | | StorageManager.DEBUG_ISOLATED_STORAGE_FORCE_OFF)) != 0) { |
| 2442 | final int value; |
| 2443 | if ((flags & StorageManager.DEBUG_ISOLATED_STORAGE_FORCE_ON) != 0) { |
| 2444 | value = 1; |
| 2445 | } else if ((flags & StorageManager.DEBUG_ISOLATED_STORAGE_FORCE_OFF) != 0) { |
| 2446 | value = -1; |
| 2447 | } else { |
| 2448 | value = 0; |
| 2449 | } |
Sudheer Shanka | be0febe | 2018-11-07 18:24:37 -0800 | [diff] [blame] | 2450 | |
| 2451 | final long token = Binder.clearCallingIdentity(); |
| 2452 | try { |
Jeff Sharkey | b0c363b2 | 2018-12-15 11:53:03 -0700 | [diff] [blame] | 2453 | Settings.Global.putInt(mContext.getContentResolver(), |
| 2454 | Settings.Global.ISOLATED_STORAGE_LOCAL, value); |
| 2455 | refreshIsolatedStorageSettings(); |
Sudheer Shanka | be0febe | 2018-11-07 18:24:37 -0800 | [diff] [blame] | 2456 | |
| 2457 | // Perform hard reboot to kick policy into place |
Jeff Sharkey | 3627499 | 2019-02-27 12:09:57 -0700 | [diff] [blame] | 2458 | mHandler.post(() -> { |
| 2459 | mContext.getSystemService(PowerManager.class).reboot(null); |
| 2460 | }); |
| 2461 | } finally { |
| 2462 | Binder.restoreCallingIdentity(token); |
| 2463 | } |
| 2464 | } |
Jeff Sharkey | 4c099d0 | 2015-05-15 13:45:00 -0700 | [diff] [blame] | 2465 | } |
| 2466 | |
| 2467 | @Override |
Jeff Sharkey | 275e3e4 | 2015-04-24 16:10:32 -0700 | [diff] [blame] | 2468 | public String getPrimaryStorageUuid() { |
Jeff Sharkey | 620b32b | 2015-04-23 19:36:02 -0700 | [diff] [blame] | 2469 | synchronized (mLock) { |
| 2470 | return mPrimaryStorageUuid; |
| 2471 | } |
| 2472 | } |
| 2473 | |
| 2474 | @Override |
Jeff Sharkey | 275e3e4 | 2015-04-24 16:10:32 -0700 | [diff] [blame] | 2475 | public void setPrimaryStorageUuid(String volumeUuid, IPackageMoveObserver callback) { |
| 2476 | enforcePermission(android.Manifest.permission.MOUNT_UNMOUNT_FILESYSTEMS); |
Jeff Sharkey | 620b32b | 2015-04-23 19:36:02 -0700 | [diff] [blame] | 2477 | |
Jeff Sharkey | a31460c | 2016-06-22 09:04:33 -0600 | [diff] [blame] | 2478 | final VolumeInfo from; |
| 2479 | final VolumeInfo to; |
| 2480 | |
Jeff Sharkey | 275e3e4 | 2015-04-24 16:10:32 -0700 | [diff] [blame] | 2481 | synchronized (mLock) { |
Jeff Sharkey | fced534 | 2015-05-10 14:53:34 -0700 | [diff] [blame] | 2482 | if (Objects.equals(mPrimaryStorageUuid, volumeUuid)) { |
| 2483 | throw new IllegalArgumentException("Primary storage already at " + volumeUuid); |
Jeff Sharkey | 275e3e4 | 2015-04-24 16:10:32 -0700 | [diff] [blame] | 2484 | } |
| 2485 | |
| 2486 | if (mMoveCallback != null) { |
| 2487 | throw new IllegalStateException("Move already in progress"); |
| 2488 | } |
| 2489 | mMoveCallback = callback; |
| 2490 | mMoveTargetUuid = volumeUuid; |
| 2491 | |
Jeff Sharkey | a65e649 | 2017-06-21 13:45:11 -0600 | [diff] [blame] | 2492 | // We need all the users unlocked to move their primary storage |
| 2493 | final List<UserInfo> users = mContext.getSystemService(UserManager.class).getUsers(); |
| 2494 | for (UserInfo user : users) { |
| 2495 | if (StorageManager.isFileEncryptedNativeOrEmulated() |
| 2496 | && !isUserKeyUnlocked(user.id)) { |
| 2497 | Slog.w(TAG, "Failing move due to locked user " + user.id); |
| 2498 | onMoveStatusLocked(PackageManager.MOVE_FAILED_LOCKED_USER); |
| 2499 | return; |
| 2500 | } |
| 2501 | } |
| 2502 | |
Jeff Sharkey | fced534 | 2015-05-10 14:53:34 -0700 | [diff] [blame] | 2503 | // When moving to/from primary physical volume, we probably just nuked |
| 2504 | // the current storage location, so we have nothing to move. |
| 2505 | if (Objects.equals(StorageManager.UUID_PRIMARY_PHYSICAL, mPrimaryStorageUuid) |
| 2506 | || Objects.equals(StorageManager.UUID_PRIMARY_PHYSICAL, volumeUuid)) { |
| 2507 | Slog.d(TAG, "Skipping move to/from primary physical"); |
| 2508 | onMoveStatusLocked(MOVE_STATUS_COPY_FINISHED); |
| 2509 | onMoveStatusLocked(PackageManager.MOVE_SUCCEEDED); |
Jeff Sharkey | 5a9bb74 | 2015-11-03 10:15:57 -0800 | [diff] [blame] | 2510 | mHandler.obtainMessage(H_RESET).sendToTarget(); |
Jeff Sharkey | a31460c | 2016-06-22 09:04:33 -0600 | [diff] [blame] | 2511 | return; |
Jeff Sharkey | fced534 | 2015-05-10 14:53:34 -0700 | [diff] [blame] | 2512 | |
| 2513 | } else { |
Jeff Sharkey | a31460c | 2016-06-22 09:04:33 -0600 | [diff] [blame] | 2514 | from = findStorageForUuid(mPrimaryStorageUuid); |
| 2515 | to = findStorageForUuid(volumeUuid); |
Jeff Sharkey | ef10ee0 | 2015-07-05 14:17:27 -0700 | [diff] [blame] | 2516 | |
| 2517 | if (from == null) { |
| 2518 | Slog.w(TAG, "Failing move due to missing from volume " + mPrimaryStorageUuid); |
| 2519 | onMoveStatusLocked(PackageManager.MOVE_FAILED_INTERNAL_ERROR); |
| 2520 | return; |
| 2521 | } else if (to == null) { |
| 2522 | Slog.w(TAG, "Failing move due to missing to volume " + volumeUuid); |
| 2523 | onMoveStatusLocked(PackageManager.MOVE_FAILED_INTERNAL_ERROR); |
| 2524 | return; |
| 2525 | } |
Jeff Sharkey | 275e3e4 | 2015-04-24 16:10:32 -0700 | [diff] [blame] | 2526 | } |
Jeff Sharkey | 620b32b | 2015-04-23 19:36:02 -0700 | [diff] [blame] | 2527 | } |
Jeff Sharkey | a31460c | 2016-06-22 09:04:33 -0600 | [diff] [blame] | 2528 | |
| 2529 | try { |
Jeff Sharkey | b302c54 | 2017-09-15 12:57:59 -0600 | [diff] [blame] | 2530 | mVold.moveStorage(from.id, to.id, new IVoldTaskListener.Stub() { |
| 2531 | @Override |
| 2532 | public void onStatus(int status, PersistableBundle extras) { |
| 2533 | synchronized (mLock) { |
| 2534 | onMoveStatusLocked(status); |
| 2535 | } |
| 2536 | } |
| 2537 | |
| 2538 | @Override |
| 2539 | public void onFinished(int status, PersistableBundle extras) { |
| 2540 | // Not currently used |
| 2541 | } |
| 2542 | }); |
Jeff Sharkey | ace874b | 2017-09-07 15:27:33 -0600 | [diff] [blame] | 2543 | } catch (Exception e) { |
| 2544 | Slog.wtf(TAG, e); |
Jeff Sharkey | a31460c | 2016-06-22 09:04:33 -0600 | [diff] [blame] | 2545 | } |
Jeff Sharkey | 620b32b | 2015-04-23 19:36:02 -0700 | [diff] [blame] | 2546 | } |
| 2547 | |
San Mehat | b104340 | 2010-02-05 08:26:50 -0800 | [diff] [blame] | 2548 | private void warnOnNotMounted() { |
Jeff Sharkey | 4887789 | 2015-03-18 11:27:19 -0700 | [diff] [blame] | 2549 | synchronized (mLock) { |
Jeff Sharkey | 1b8ef7e | 2015-04-03 17:14:45 -0700 | [diff] [blame] | 2550 | for (int i = 0; i < mVolumes.size(); i++) { |
| 2551 | final VolumeInfo vol = mVolumes.valueAt(i); |
Jeff Sharkey | 27de30d | 2015-04-18 16:20:27 -0700 | [diff] [blame] | 2552 | if (vol.isPrimary() && vol.isMountedWritable()) { |
Jeff Sharkey | 4887789 | 2015-03-18 11:27:19 -0700 | [diff] [blame] | 2553 | // Cool beans, we have a mounted primary volume |
| 2554 | return; |
| 2555 | } |
Jeff Sharkey | 32ee831 | 2012-09-30 13:21:31 -0700 | [diff] [blame] | 2556 | } |
San Mehat | b104340 | 2010-02-05 08:26:50 -0800 | [diff] [blame] | 2557 | } |
Jeff Sharkey | 4887789 | 2015-03-18 11:27:19 -0700 | [diff] [blame] | 2558 | |
| 2559 | Slog.w(TAG, "No primary storage mounted!"); |
San Mehat | b104340 | 2010-02-05 08:26:50 -0800 | [diff] [blame] | 2560 | } |
| 2561 | |
Kenny Root | a02b8b0 | 2010-08-05 16:14:17 -0700 | [diff] [blame] | 2562 | private boolean isUidOwnerOfPackageOrSystem(String packageName, int callerUid) { |
| 2563 | if (callerUid == android.os.Process.SYSTEM_UID) { |
| 2564 | return true; |
| 2565 | } |
| 2566 | |
Kenny Root | 02c8730 | 2010-07-01 08:10:18 -0700 | [diff] [blame] | 2567 | if (packageName == null) { |
| 2568 | return false; |
| 2569 | } |
| 2570 | |
Jeff Sharkey | 5790af0 | 2018-08-13 17:42:54 -0600 | [diff] [blame] | 2571 | final int packageUid = mPmInternal.getPackageUid(packageName, |
Jeff Sharkey | cd65448 | 2016-01-08 17:42:11 -0700 | [diff] [blame] | 2572 | PackageManager.MATCH_DEBUG_TRIAGED_MISSING, UserHandle.getUserId(callerUid)); |
Kenny Root | 02c8730 | 2010-07-01 08:10:18 -0700 | [diff] [blame] | 2573 | |
| 2574 | if (DEBUG_OBB) { |
| 2575 | Slog.d(TAG, "packageName = " + packageName + ", packageUid = " + |
| 2576 | packageUid + ", callerUid = " + callerUid); |
| 2577 | } |
| 2578 | |
| 2579 | return callerUid == packageUid; |
| 2580 | } |
| 2581 | |
Jeff Sharkey | 5440279 | 2017-09-15 16:05:19 -0600 | [diff] [blame] | 2582 | @Override |
Jeff Sharkey | 4fbbda4 | 2012-09-24 18:34:07 -0700 | [diff] [blame] | 2583 | public String getMountedObbPath(String rawPath) { |
Daulet Zhanguzin | ea1a7ca | 2020-01-03 09:46:50 +0000 | [diff] [blame] | 2584 | Objects.requireNonNull(rawPath, "rawPath cannot be null"); |
Kenny Root | af9d667 | 2010-10-08 09:21:39 -0700 | [diff] [blame] | 2585 | |
Kenny Root | 02c8730 | 2010-07-01 08:10:18 -0700 | [diff] [blame] | 2586 | warnOnNotMounted(); |
| 2587 | |
Jeff Sharkey | 4fbbda4 | 2012-09-24 18:34:07 -0700 | [diff] [blame] | 2588 | final ObbState state; |
Rubin Xu | cd7a014 | 2015-04-17 23:45:27 +0100 | [diff] [blame] | 2589 | synchronized (mObbMounts) { |
Jeff Sharkey | 4fbbda4 | 2012-09-24 18:34:07 -0700 | [diff] [blame] | 2590 | state = mObbPathToStateMap.get(rawPath); |
| 2591 | } |
| 2592 | if (state == null) { |
| 2593 | Slog.w(TAG, "Failed to find OBB mounted at " + rawPath); |
| 2594 | return null; |
| 2595 | } |
| 2596 | |
Jeff Sharkey | 5440279 | 2017-09-15 16:05:19 -0600 | [diff] [blame] | 2597 | return findVolumeByIdOrThrow(state.volId).getPath().getAbsolutePath(); |
Kenny Root | 02c8730 | 2010-07-01 08:10:18 -0700 | [diff] [blame] | 2598 | } |
| 2599 | |
Jeff Sharkey | 4fbbda4 | 2012-09-24 18:34:07 -0700 | [diff] [blame] | 2600 | @Override |
| 2601 | public boolean isObbMounted(String rawPath) { |
Daulet Zhanguzin | ea1a7ca | 2020-01-03 09:46:50 +0000 | [diff] [blame] | 2602 | Objects.requireNonNull(rawPath, "rawPath cannot be null"); |
Kenny Root | af9d667 | 2010-10-08 09:21:39 -0700 | [diff] [blame] | 2603 | synchronized (mObbMounts) { |
Jeff Sharkey | 4fbbda4 | 2012-09-24 18:34:07 -0700 | [diff] [blame] | 2604 | return mObbPathToStateMap.containsKey(rawPath); |
Kenny Root | af9d667 | 2010-10-08 09:21:39 -0700 | [diff] [blame] | 2605 | } |
Kenny Root | 02c8730 | 2010-07-01 08:10:18 -0700 | [diff] [blame] | 2606 | } |
| 2607 | |
Jeff Sharkey | 4fbbda4 | 2012-09-24 18:34:07 -0700 | [diff] [blame] | 2608 | @Override |
Sudheer Shanka | 25469aa | 2018-08-27 15:50:23 -0700 | [diff] [blame] | 2609 | public void mountObb(String rawPath, String canonicalPath, String key, |
| 2610 | IObbActionListener token, int nonce, ObbInfo obbInfo) { |
Daulet Zhanguzin | ea1a7ca | 2020-01-03 09:46:50 +0000 | [diff] [blame] | 2611 | Objects.requireNonNull(rawPath, "rawPath cannot be null"); |
| 2612 | Objects.requireNonNull(canonicalPath, "canonicalPath cannot be null"); |
| 2613 | Objects.requireNonNull(token, "token cannot be null"); |
| 2614 | Objects.requireNonNull(obbInfo, "obbIfno cannot be null"); |
Kenny Root | af9d667 | 2010-10-08 09:21:39 -0700 | [diff] [blame] | 2615 | |
Jeff Sharkey | 4fbbda4 | 2012-09-24 18:34:07 -0700 | [diff] [blame] | 2616 | final int callingUid = Binder.getCallingUid(); |
Jeff Sharkey | 41cd681 | 2017-09-11 10:32:17 -0600 | [diff] [blame] | 2617 | final ObbState obbState = new ObbState(rawPath, canonicalPath, |
| 2618 | callingUid, token, nonce, null); |
Sudheer Shanka | 25469aa | 2018-08-27 15:50:23 -0700 | [diff] [blame] | 2619 | final ObbAction action = new MountObbAction(obbState, key, callingUid, obbInfo); |
Kenny Root | a02b8b0 | 2010-08-05 16:14:17 -0700 | [diff] [blame] | 2620 | mObbActionHandler.sendMessage(mObbActionHandler.obtainMessage(OBB_RUN_ACTION, action)); |
| 2621 | |
| 2622 | if (DEBUG_OBB) |
| 2623 | Slog.i(TAG, "Send to OBB handler: " + action.toString()); |
Kenny Root | 02c8730 | 2010-07-01 08:10:18 -0700 | [diff] [blame] | 2624 | } |
| 2625 | |
Jeff Sharkey | 4fbbda4 | 2012-09-24 18:34:07 -0700 | [diff] [blame] | 2626 | @Override |
| 2627 | public void unmountObb(String rawPath, boolean force, IObbActionListener token, int nonce) { |
Daulet Zhanguzin | ea1a7ca | 2020-01-03 09:46:50 +0000 | [diff] [blame] | 2628 | Objects.requireNonNull(rawPath, "rawPath cannot be null"); |
Jeff Sharkey | 4fbbda4 | 2012-09-24 18:34:07 -0700 | [diff] [blame] | 2629 | |
| 2630 | final ObbState existingState; |
Rubin Xu | cd7a014 | 2015-04-17 23:45:27 +0100 | [diff] [blame] | 2631 | synchronized (mObbMounts) { |
Jeff Sharkey | 4fbbda4 | 2012-09-24 18:34:07 -0700 | [diff] [blame] | 2632 | existingState = mObbPathToStateMap.get(rawPath); |
Kenny Root | f1121dc | 2010-09-29 07:30:53 -0700 | [diff] [blame] | 2633 | } |
| 2634 | |
Jeff Sharkey | 4fbbda4 | 2012-09-24 18:34:07 -0700 | [diff] [blame] | 2635 | if (existingState != null) { |
| 2636 | // TODO: separate state object from request data |
| 2637 | final int callingUid = Binder.getCallingUid(); |
Jeff Sharkey | 41cd681 | 2017-09-11 10:32:17 -0600 | [diff] [blame] | 2638 | final ObbState newState = new ObbState(rawPath, existingState.canonicalPath, |
| 2639 | callingUid, token, nonce, existingState.volId); |
Jeff Sharkey | 4fbbda4 | 2012-09-24 18:34:07 -0700 | [diff] [blame] | 2640 | final ObbAction action = new UnmountObbAction(newState, force); |
| 2641 | mObbActionHandler.sendMessage(mObbActionHandler.obtainMessage(OBB_RUN_ACTION, action)); |
Kenny Root | 02c8730 | 2010-07-01 08:10:18 -0700 | [diff] [blame] | 2642 | |
Jeff Sharkey | 4fbbda4 | 2012-09-24 18:34:07 -0700 | [diff] [blame] | 2643 | if (DEBUG_OBB) |
| 2644 | Slog.i(TAG, "Send to OBB handler: " + action.toString()); |
| 2645 | } else { |
| 2646 | Slog.w(TAG, "Unknown OBB mount at " + rawPath); |
| 2647 | } |
Kenny Root | a02b8b0 | 2010-08-05 16:14:17 -0700 | [diff] [blame] | 2648 | } |
| 2649 | |
Ben Komalo | 444eca2 | 2011-09-01 15:17:44 -0700 | [diff] [blame] | 2650 | @Override |
| 2651 | public int getEncryptionState() { |
| 2652 | mContext.enforceCallingOrSelfPermission(Manifest.permission.CRYPT_KEEPER, |
| 2653 | "no permission to access the crypt keeper"); |
| 2654 | |
Ben Komalo | 444eca2 | 2011-09-01 15:17:44 -0700 | [diff] [blame] | 2655 | try { |
Jeff Sharkey | 5440279 | 2017-09-15 16:05:19 -0600 | [diff] [blame] | 2656 | return mVold.fdeComplete(); |
| 2657 | } catch (Exception e) { |
| 2658 | Slog.wtf(TAG, e); |
Sudheer Shanka | f734114 | 2016-10-18 17:15:18 -0700 | [diff] [blame] | 2659 | return StorageManager.ENCRYPTION_STATE_ERROR_UNKNOWN; |
Ben Komalo | 444eca2 | 2011-09-01 15:17:44 -0700 | [diff] [blame] | 2660 | } |
| 2661 | } |
| 2662 | |
| 2663 | @Override |
Jason parks | 5af0b91 | 2010-11-29 09:05:25 -0600 | [diff] [blame] | 2664 | public int decryptStorage(String password) { |
Jason parks | 8888c59 | 2011-01-20 22:46:41 -0600 | [diff] [blame] | 2665 | mContext.enforceCallingOrSelfPermission(Manifest.permission.CRYPT_KEEPER, |
| 2666 | "no permission to access the crypt keeper"); |
Jason parks | 5af0b91 | 2010-11-29 09:05:25 -0600 | [diff] [blame] | 2667 | |
Jeff Sharkey | 8058fe6 | 2017-09-13 11:50:33 -0600 | [diff] [blame] | 2668 | if (TextUtils.isEmpty(password)) { |
| 2669 | throw new IllegalArgumentException("password cannot be empty"); |
| 2670 | } |
| 2671 | |
Jason parks | 5af0b91 | 2010-11-29 09:05:25 -0600 | [diff] [blame] | 2672 | if (DEBUG_EVENTS) { |
| 2673 | Slog.i(TAG, "decrypting storage..."); |
| 2674 | } |
| 2675 | |
| 2676 | try { |
Jeff Sharkey | 5440279 | 2017-09-15 16:05:19 -0600 | [diff] [blame] | 2677 | mVold.fdeCheckPassword(password); |
| 2678 | mHandler.postDelayed(() -> { |
| 2679 | try { |
| 2680 | mVold.fdeRestart(); |
| 2681 | } catch (Exception e) { |
| 2682 | Slog.wtf(TAG, e); |
| 2683 | } |
| 2684 | }, DateUtils.SECOND_IN_MILLIS); |
| 2685 | return 0; |
Paul Crowley | fc0b519 | 2018-07-02 13:58:10 -0700 | [diff] [blame] | 2686 | } catch (ServiceSpecificException e) { |
| 2687 | Slog.e(TAG, "fdeCheckPassword failed", e); |
| 2688 | return e.errorCode; |
Jeff Sharkey | 5440279 | 2017-09-15 16:05:19 -0600 | [diff] [blame] | 2689 | } catch (Exception e) { |
| 2690 | Slog.wtf(TAG, e); |
| 2691 | return StorageManager.ENCRYPTION_STATE_ERROR_UNKNOWN; |
Jason parks | 5af0b91 | 2010-11-29 09:05:25 -0600 | [diff] [blame] | 2692 | } |
Jason parks | 5af0b91 | 2010-11-29 09:05:25 -0600 | [diff] [blame] | 2693 | } |
| 2694 | |
Jeff Sharkey | 5440279 | 2017-09-15 16:05:19 -0600 | [diff] [blame] | 2695 | @Override |
Paul Lawrence | 46791e7 | 2014-04-03 09:10:26 -0700 | [diff] [blame] | 2696 | public int encryptStorage(int type, String password) { |
Jason parks | 8888c59 | 2011-01-20 22:46:41 -0600 | [diff] [blame] | 2697 | mContext.enforceCallingOrSelfPermission(Manifest.permission.CRYPT_KEEPER, |
| 2698 | "no permission to access the crypt keeper"); |
Jason parks | 56aa532 | 2011-01-07 09:01:15 -0600 | [diff] [blame] | 2699 | |
Jeff Sharkey | 8058fe6 | 2017-09-13 11:50:33 -0600 | [diff] [blame] | 2700 | if (type == StorageManager.CRYPT_TYPE_DEFAULT) { |
| 2701 | password = ""; |
| 2702 | } else if (TextUtils.isEmpty(password)) { |
| 2703 | throw new IllegalArgumentException("password cannot be empty"); |
| 2704 | } |
| 2705 | |
Jason parks | 56aa532 | 2011-01-07 09:01:15 -0600 | [diff] [blame] | 2706 | if (DEBUG_EVENTS) { |
Jason parks | 8888c59 | 2011-01-20 22:46:41 -0600 | [diff] [blame] | 2707 | Slog.i(TAG, "encrypting storage..."); |
Jason parks | 56aa532 | 2011-01-07 09:01:15 -0600 | [diff] [blame] | 2708 | } |
| 2709 | |
| 2710 | try { |
Paul Lawrence | 89a0ae4 | 2017-12-22 10:11:10 -0800 | [diff] [blame] | 2711 | mVold.fdeEnable(type, password, 0); |
Jeff Sharkey | 43e1211 | 2017-09-12 16:31:45 -0600 | [diff] [blame] | 2712 | } catch (Exception e) { |
| 2713 | Slog.wtf(TAG, e); |
| 2714 | return -1; |
Jason parks | 56aa532 | 2011-01-07 09:01:15 -0600 | [diff] [blame] | 2715 | } |
| 2716 | |
| 2717 | return 0; |
| 2718 | } |
| 2719 | |
Paul Lawrence | 8e39736 | 2014-01-27 15:22:30 -0800 | [diff] [blame] | 2720 | /** Set the password for encrypting the master key. |
| 2721 | * @param type One of the CRYPTO_TYPE_XXX consts defined in StorageManager. |
| 2722 | * @param password The password to set. |
| 2723 | */ |
Jeff Sharkey | 5440279 | 2017-09-15 16:05:19 -0600 | [diff] [blame] | 2724 | @Override |
Paul Lawrence | 8e39736 | 2014-01-27 15:22:30 -0800 | [diff] [blame] | 2725 | public int changeEncryptionPassword(int type, String password) { |
Jason parks | f7b3cd4 | 2011-01-27 09:28:25 -0600 | [diff] [blame] | 2726 | mContext.enforceCallingOrSelfPermission(Manifest.permission.CRYPT_KEEPER, |
| 2727 | "no permission to access the crypt keeper"); |
| 2728 | |
Jeff Sharkey | ae26646 | 2017-11-27 13:32:24 -0700 | [diff] [blame] | 2729 | if (StorageManager.isFileEncryptedNativeOnly()) { |
| 2730 | // Not supported on FBE devices |
| 2731 | return -1; |
| 2732 | } |
| 2733 | |
Jeff Sharkey | 8058fe6 | 2017-09-13 11:50:33 -0600 | [diff] [blame] | 2734 | if (type == StorageManager.CRYPT_TYPE_DEFAULT) { |
| 2735 | password = ""; |
| 2736 | } else if (TextUtils.isEmpty(password)) { |
| 2737 | throw new IllegalArgumentException("password cannot be empty"); |
| 2738 | } |
| 2739 | |
Jason parks | f7b3cd4 | 2011-01-27 09:28:25 -0600 | [diff] [blame] | 2740 | if (DEBUG_EVENTS) { |
| 2741 | Slog.i(TAG, "changing encryption password..."); |
| 2742 | } |
| 2743 | |
| 2744 | try { |
Jeff Sharkey | 5440279 | 2017-09-15 16:05:19 -0600 | [diff] [blame] | 2745 | mVold.fdeChangePassword(type, password); |
| 2746 | return 0; |
| 2747 | } catch (Exception e) { |
| 2748 | Slog.wtf(TAG, e); |
| 2749 | return -1; |
Jason parks | f7b3cd4 | 2011-01-27 09:28:25 -0600 | [diff] [blame] | 2750 | } |
| 2751 | } |
| 2752 | |
Christopher Tate | 32418be | 2011-10-10 13:51:12 -0700 | [diff] [blame] | 2753 | /** |
| 2754 | * Validate a user-supplied password string with cryptfs |
| 2755 | */ |
| 2756 | @Override |
| 2757 | public int verifyEncryptionPassword(String password) throws RemoteException { |
| 2758 | // Only the system process is permitted to validate passwords |
| 2759 | if (Binder.getCallingUid() != android.os.Process.SYSTEM_UID) { |
| 2760 | throw new SecurityException("no permission to access the crypt keeper"); |
| 2761 | } |
| 2762 | |
| 2763 | mContext.enforceCallingOrSelfPermission(Manifest.permission.CRYPT_KEEPER, |
| 2764 | "no permission to access the crypt keeper"); |
| 2765 | |
| 2766 | if (TextUtils.isEmpty(password)) { |
| 2767 | throw new IllegalArgumentException("password cannot be empty"); |
| 2768 | } |
| 2769 | |
Christopher Tate | 32418be | 2011-10-10 13:51:12 -0700 | [diff] [blame] | 2770 | if (DEBUG_EVENTS) { |
| 2771 | Slog.i(TAG, "validating encryption password..."); |
| 2772 | } |
| 2773 | |
| 2774 | try { |
Jeff Sharkey | 5440279 | 2017-09-15 16:05:19 -0600 | [diff] [blame] | 2775 | mVold.fdeVerifyPassword(password); |
| 2776 | return 0; |
| 2777 | } catch (Exception e) { |
| 2778 | Slog.wtf(TAG, e); |
| 2779 | return -1; |
Christopher Tate | 32418be | 2011-10-10 13:51:12 -0700 | [diff] [blame] | 2780 | } |
| 2781 | } |
| 2782 | |
Paul Lawrence | 8e39736 | 2014-01-27 15:22:30 -0800 | [diff] [blame] | 2783 | /** |
| 2784 | * Get the type of encryption used to encrypt the master key. |
| 2785 | * @return The type, one of the CRYPT_TYPE_XXX consts from StorageManager. |
| 2786 | */ |
| 2787 | @Override |
Svetoslav | 16e4a1a | 2014-09-29 18:16:20 -0700 | [diff] [blame] | 2788 | public int getPasswordType() { |
Paul Lawrence | 76a4057 | 2017-03-15 11:08:04 -0700 | [diff] [blame] | 2789 | mContext.enforceCallingOrSelfPermission(Manifest.permission.CRYPT_KEEPER, |
Paul Lawrence | 9de713d | 2016-05-02 22:45:33 +0000 | [diff] [blame] | 2790 | "no permission to access the crypt keeper"); |
| 2791 | |
Paul Lawrence | 8e39736 | 2014-01-27 15:22:30 -0800 | [diff] [blame] | 2792 | try { |
Jeff Sharkey | 5440279 | 2017-09-15 16:05:19 -0600 | [diff] [blame] | 2793 | return mVold.fdeGetPasswordType(); |
| 2794 | } catch (Exception e) { |
| 2795 | Slog.wtf(TAG, e); |
| 2796 | return -1; |
Paul Lawrence | 8e39736 | 2014-01-27 15:22:30 -0800 | [diff] [blame] | 2797 | } |
| 2798 | } |
| 2799 | |
Paul Lawrence | e51dcf9 | 2014-03-18 10:56:00 -0700 | [diff] [blame] | 2800 | /** |
| 2801 | * Set a field in the crypto header. |
| 2802 | * @param field field to set |
| 2803 | * @param contents contents to set in field |
| 2804 | */ |
| 2805 | @Override |
| 2806 | public void setField(String field, String contents) throws RemoteException { |
Paul Lawrence | 76a4057 | 2017-03-15 11:08:04 -0700 | [diff] [blame] | 2807 | mContext.enforceCallingOrSelfPermission(Manifest.permission.CRYPT_KEEPER, |
Paul Lawrence | 0bbd108 | 2016-04-26 15:21:02 -0700 | [diff] [blame] | 2808 | "no permission to access the crypt keeper"); |
Paul Lawrence | e51dcf9 | 2014-03-18 10:56:00 -0700 | [diff] [blame] | 2809 | |
yusukes | 14a8b1f | 2018-07-23 17:34:42 -0700 | [diff] [blame] | 2810 | if (!StorageManager.isBlockEncrypted()) { |
| 2811 | // Only supported on FDE devices |
Jeff Sharkey | ae26646 | 2017-11-27 13:32:24 -0700 | [diff] [blame] | 2812 | return; |
| 2813 | } |
| 2814 | |
Paul Lawrence | e51dcf9 | 2014-03-18 10:56:00 -0700 | [diff] [blame] | 2815 | try { |
Jeff Sharkey | 5440279 | 2017-09-15 16:05:19 -0600 | [diff] [blame] | 2816 | mVold.fdeSetField(field, contents); |
| 2817 | return; |
| 2818 | } catch (Exception e) { |
| 2819 | Slog.wtf(TAG, e); |
| 2820 | return; |
Paul Lawrence | e51dcf9 | 2014-03-18 10:56:00 -0700 | [diff] [blame] | 2821 | } |
| 2822 | } |
| 2823 | |
| 2824 | /** |
| 2825 | * Gets a field from the crypto header. |
| 2826 | * @param field field to get |
| 2827 | * @return contents of field |
| 2828 | */ |
| 2829 | @Override |
| 2830 | public String getField(String field) throws RemoteException { |
Paul Lawrence | 76a4057 | 2017-03-15 11:08:04 -0700 | [diff] [blame] | 2831 | mContext.enforceCallingOrSelfPermission(Manifest.permission.CRYPT_KEEPER, |
Paul Lawrence | 0bbd108 | 2016-04-26 15:21:02 -0700 | [diff] [blame] | 2832 | "no permission to access the crypt keeper"); |
Paul Lawrence | e51dcf9 | 2014-03-18 10:56:00 -0700 | [diff] [blame] | 2833 | |
yusukes | 14a8b1f | 2018-07-23 17:34:42 -0700 | [diff] [blame] | 2834 | if (!StorageManager.isBlockEncrypted()) { |
| 2835 | // Only supported on FDE devices |
Jeff Sharkey | ae26646 | 2017-11-27 13:32:24 -0700 | [diff] [blame] | 2836 | return null; |
| 2837 | } |
| 2838 | |
Paul Lawrence | e51dcf9 | 2014-03-18 10:56:00 -0700 | [diff] [blame] | 2839 | try { |
Jeff Sharkey | 5440279 | 2017-09-15 16:05:19 -0600 | [diff] [blame] | 2840 | return mVold.fdeGetField(field); |
| 2841 | } catch (Exception e) { |
| 2842 | Slog.wtf(TAG, e); |
| 2843 | return null; |
Paul Lawrence | e51dcf9 | 2014-03-18 10:56:00 -0700 | [diff] [blame] | 2844 | } |
| 2845 | } |
| 2846 | |
Paul Lawrence | 3806d9c | 2015-10-29 10:30:46 -0700 | [diff] [blame] | 2847 | /** |
| 2848 | * Is userdata convertible to file based encryption? |
| 2849 | * @return non zero for convertible |
| 2850 | */ |
| 2851 | @Override |
| 2852 | public boolean isConvertibleToFBE() throws RemoteException { |
Paul Lawrence | 76a4057 | 2017-03-15 11:08:04 -0700 | [diff] [blame] | 2853 | mContext.enforceCallingOrSelfPermission(Manifest.permission.CRYPT_KEEPER, |
Paul Lawrence | 0bbd108 | 2016-04-26 15:21:02 -0700 | [diff] [blame] | 2854 | "no permission to access the crypt keeper"); |
Paul Lawrence | 3806d9c | 2015-10-29 10:30:46 -0700 | [diff] [blame] | 2855 | |
Paul Lawrence | 3806d9c | 2015-10-29 10:30:46 -0700 | [diff] [blame] | 2856 | try { |
Jeff Sharkey | 5440279 | 2017-09-15 16:05:19 -0600 | [diff] [blame] | 2857 | return mVold.isConvertibleToFbe(); |
| 2858 | } catch (Exception e) { |
| 2859 | Slog.wtf(TAG, e); |
| 2860 | return false; |
Paul Lawrence | 3806d9c | 2015-10-29 10:30:46 -0700 | [diff] [blame] | 2861 | } |
| 2862 | } |
| 2863 | |
Daniel Rosenberg | e129e5c | 2018-11-07 19:25:55 -0800 | [diff] [blame] | 2864 | /** |
Martijn Coenen | f04aca4 | 2019-03-11 10:35:34 +0100 | [diff] [blame] | 2865 | * Check whether the device supports filesystem checkpointing. |
| 2866 | * |
| 2867 | * @return true if the device supports filesystem checkpointing, false otherwise. |
| 2868 | */ |
| 2869 | @Override |
| 2870 | public boolean supportsCheckpoint() throws RemoteException { |
Martijn Coenen | f04aca4 | 2019-03-11 10:35:34 +0100 | [diff] [blame] | 2871 | return mVold.supportsCheckpoint(); |
| 2872 | } |
| 2873 | |
| 2874 | /** |
| 2875 | * Signal that checkpointing partitions should start a checkpoint on the next boot. |
| 2876 | * |
| 2877 | * @param numTries Number of times to try booting in checkpoint mode, before we will boot |
| 2878 | * non-checkpoint mode and commit all changes immediately. Callers are |
| 2879 | * responsible for ensuring that boot is safe (eg, by rolling back updates). |
| 2880 | */ |
| 2881 | @Override |
| 2882 | public void startCheckpoint(int numTries) throws RemoteException { |
Nikita Ioffe | 6d74942 | 2020-01-14 22:19:01 +0000 | [diff] [blame] | 2883 | // Only the root, system_server and shell processes are permitted to start checkpoints |
| 2884 | final int callingUid = Binder.getCallingUid(); |
| 2885 | if (callingUid != Process.SYSTEM_UID && callingUid != Process.ROOT_UID |
| 2886 | && callingUid != Process.SHELL_UID) { |
Martijn Coenen | f04aca4 | 2019-03-11 10:35:34 +0100 | [diff] [blame] | 2887 | throw new SecurityException("no permission to start filesystem checkpoint"); |
| 2888 | } |
| 2889 | |
| 2890 | mVold.startCheckpoint(numTries); |
| 2891 | } |
| 2892 | |
| 2893 | /** |
Daniel Rosenberg | e129e5c | 2018-11-07 19:25:55 -0800 | [diff] [blame] | 2894 | * Signal that checkpointing partitions should commit changes |
| 2895 | */ |
| 2896 | @Override |
| 2897 | public void commitChanges() throws RemoteException { |
| 2898 | // Only the system process is permitted to commit checkpoints |
| 2899 | if (Binder.getCallingUid() != android.os.Process.SYSTEM_UID) { |
| 2900 | throw new SecurityException("no permission to commit checkpoint changes"); |
| 2901 | } |
| 2902 | |
| 2903 | mVold.commitChanges(); |
| 2904 | } |
| 2905 | |
Daniel Rosenberg | e3a924d | 2019-03-19 18:19:08 -0700 | [diff] [blame] | 2906 | /** |
| 2907 | * Check if we should be mounting with checkpointing or are checkpointing now |
| 2908 | */ |
| 2909 | @Override |
| 2910 | public boolean needsCheckpoint() throws RemoteException { |
Paul Lawrence | 15a5446 | 2019-04-30 11:14:25 -0700 | [diff] [blame] | 2911 | enforcePermission(android.Manifest.permission.MOUNT_FORMAT_FILESYSTEMS); |
Daniel Rosenberg | e3a924d | 2019-03-19 18:19:08 -0700 | [diff] [blame] | 2912 | return mVold.needsCheckpoint(); |
| 2913 | } |
| 2914 | |
Daniel Rosenberg | 600799b | 2019-03-20 17:42:50 -0700 | [diff] [blame] | 2915 | /** |
| 2916 | * Abort the current set of changes and either try again, or abort entirely |
| 2917 | */ |
| 2918 | @Override |
| 2919 | public void abortChanges(String message, boolean retry) throws RemoteException { |
| 2920 | // Only the system process is permitted to abort checkpoints |
| 2921 | if (Binder.getCallingUid() != android.os.Process.SYSTEM_UID) { |
| 2922 | throw new SecurityException("no permission to commit checkpoint changes"); |
| 2923 | } |
| 2924 | |
| 2925 | mVold.abortChanges(message, retry); |
| 2926 | } |
| 2927 | |
Jeff Sharkey | b049e21 | 2012-09-07 23:16:01 -0700 | [diff] [blame] | 2928 | @Override |
Paul Lawrence | 945490c | 2014-03-27 16:37:28 +0000 | [diff] [blame] | 2929 | public String getPassword() throws RemoteException { |
Paul Lawrence | 76a4057 | 2017-03-15 11:08:04 -0700 | [diff] [blame] | 2930 | mContext.enforceCallingOrSelfPermission(Manifest.permission.CRYPT_KEEPER, |
Rubin Xu | cd7a014 | 2015-04-17 23:45:27 +0100 | [diff] [blame] | 2931 | "only keyguard can retrieve password"); |
Paul Lawrence | 0bbd108 | 2016-04-26 15:21:02 -0700 | [diff] [blame] | 2932 | |
Paul Lawrence | 945490c | 2014-03-27 16:37:28 +0000 | [diff] [blame] | 2933 | try { |
Jeff Sharkey | 5440279 | 2017-09-15 16:05:19 -0600 | [diff] [blame] | 2934 | return mVold.fdeGetPassword(); |
| 2935 | } catch (Exception e) { |
| 2936 | Slog.wtf(TAG, e); |
Paul Lawrence | 24063b5 | 2015-01-06 13:11:23 -0800 | [diff] [blame] | 2937 | return null; |
Paul Lawrence | 945490c | 2014-03-27 16:37:28 +0000 | [diff] [blame] | 2938 | } |
| 2939 | } |
| 2940 | |
| 2941 | @Override |
| 2942 | public void clearPassword() throws RemoteException { |
Paul Lawrence | 76a4057 | 2017-03-15 11:08:04 -0700 | [diff] [blame] | 2943 | mContext.enforceCallingOrSelfPermission(Manifest.permission.CRYPT_KEEPER, |
Paul Lawrence | 0bbd108 | 2016-04-26 15:21:02 -0700 | [diff] [blame] | 2944 | "only keyguard can clear password"); |
| 2945 | |
Paul Lawrence | 945490c | 2014-03-27 16:37:28 +0000 | [diff] [blame] | 2946 | try { |
Jeff Sharkey | 5440279 | 2017-09-15 16:05:19 -0600 | [diff] [blame] | 2947 | mVold.fdeClearPassword(); |
| 2948 | return; |
| 2949 | } catch (Exception e) { |
| 2950 | Slog.wtf(TAG, e); |
| 2951 | return; |
Paul Lawrence | 945490c | 2014-03-27 16:37:28 +0000 | [diff] [blame] | 2952 | } |
| 2953 | } |
| 2954 | |
| 2955 | @Override |
Lenka Trochtova | c4dd021 | 2015-11-18 12:22:06 +0100 | [diff] [blame] | 2956 | public void createUserKey(int userId, int serialNumber, boolean ephemeral) { |
Jeff Sharkey | f9fc6d6 | 2015-11-08 16:46:05 -0800 | [diff] [blame] | 2957 | enforcePermission(android.Manifest.permission.STORAGE_INTERNAL); |
Paul Crowley | bcf48ed | 2015-04-22 13:36:59 +0100 | [diff] [blame] | 2958 | |
Paul Crowley | bcf48ed | 2015-04-22 13:36:59 +0100 | [diff] [blame] | 2959 | try { |
Jeff Sharkey | 5440279 | 2017-09-15 16:05:19 -0600 | [diff] [blame] | 2960 | mVold.createUserKey(userId, serialNumber, ephemeral); |
Jeff Sharkey | 43e1211 | 2017-09-12 16:31:45 -0600 | [diff] [blame] | 2961 | } catch (Exception e) { |
| 2962 | Slog.wtf(TAG, e); |
Paul Crowley | bcf48ed | 2015-04-22 13:36:59 +0100 | [diff] [blame] | 2963 | } |
| 2964 | } |
| 2965 | |
Paul Crowley | 7ec733f | 2015-05-19 12:42:00 +0100 | [diff] [blame] | 2966 | @Override |
Jeff Sharkey | f9fc6d6 | 2015-11-08 16:46:05 -0800 | [diff] [blame] | 2967 | public void destroyUserKey(int userId) { |
| 2968 | enforcePermission(android.Manifest.permission.STORAGE_INTERNAL); |
Paul Crowley | 7ec733f | 2015-05-19 12:42:00 +0100 | [diff] [blame] | 2969 | |
Jeff Sharkey | f9fc6d6 | 2015-11-08 16:46:05 -0800 | [diff] [blame] | 2970 | try { |
Jeff Sharkey | 5440279 | 2017-09-15 16:05:19 -0600 | [diff] [blame] | 2971 | mVold.destroyUserKey(userId); |
Jeff Sharkey | 43e1211 | 2017-09-12 16:31:45 -0600 | [diff] [blame] | 2972 | } catch (Exception e) { |
| 2973 | Slog.wtf(TAG, e); |
Jeff Sharkey | f9fc6d6 | 2015-11-08 16:46:05 -0800 | [diff] [blame] | 2974 | } |
| 2975 | } |
| 2976 | |
Jeff Sharkey | 43e1211 | 2017-09-12 16:31:45 -0600 | [diff] [blame] | 2977 | private String encodeBytes(byte[] bytes) { |
Paul Crowley | faeb3eb | 2016-02-08 15:58:29 +0000 | [diff] [blame] | 2978 | if (ArrayUtils.isEmpty(bytes)) { |
Jeff Sharkey | 43e1211 | 2017-09-12 16:31:45 -0600 | [diff] [blame] | 2979 | return "!"; |
Paul Crowley | faeb3eb | 2016-02-08 15:58:29 +0000 | [diff] [blame] | 2980 | } else { |
Jeff Sharkey | 43e1211 | 2017-09-12 16:31:45 -0600 | [diff] [blame] | 2981 | return HexDump.toHexString(bytes); |
Paul Crowley | faeb3eb | 2016-02-08 15:58:29 +0000 | [diff] [blame] | 2982 | } |
| 2983 | } |
| 2984 | |
Paul Crowley | cc70155 | 2016-05-17 14:18:49 -0700 | [diff] [blame] | 2985 | /* |
| 2986 | * Add this token/secret pair to the set of ways we can recover a disk encryption key. |
| 2987 | * Changing the token/secret for a disk encryption key is done in two phases: first, adding |
| 2988 | * a new token/secret pair with this call, then delting all other pairs with |
| 2989 | * fixateNewestUserKeyAuth. This allows other places where a credential is used, such as |
| 2990 | * Gatekeeper, to be updated between the two calls. |
| 2991 | */ |
Jeff Sharkey | f9fc6d6 | 2015-11-08 16:46:05 -0800 | [diff] [blame] | 2992 | @Override |
Paul Crowley | cc70155 | 2016-05-17 14:18:49 -0700 | [diff] [blame] | 2993 | public void addUserKeyAuth(int userId, int serialNumber, byte[] token, byte[] secret) { |
Paul Crowley | faeb3eb | 2016-02-08 15:58:29 +0000 | [diff] [blame] | 2994 | enforcePermission(android.Manifest.permission.STORAGE_INTERNAL); |
Paul Crowley | faeb3eb | 2016-02-08 15:58:29 +0000 | [diff] [blame] | 2995 | |
| 2996 | try { |
Jeff Sharkey | 5440279 | 2017-09-15 16:05:19 -0600 | [diff] [blame] | 2997 | mVold.addUserKeyAuth(userId, serialNumber, encodeBytes(token), encodeBytes(secret)); |
Jeff Sharkey | 43e1211 | 2017-09-12 16:31:45 -0600 | [diff] [blame] | 2998 | } catch (Exception e) { |
| 2999 | Slog.wtf(TAG, e); |
Paul Crowley | cc70155 | 2016-05-17 14:18:49 -0700 | [diff] [blame] | 3000 | } |
| 3001 | } |
| 3002 | |
| 3003 | /* |
Barani Muthukumaran | 9ba99ed | 2019-12-18 00:36:06 -0800 | [diff] [blame] | 3004 | * Clear disk encryption key bound to the associated token / secret pair. Removing the user |
| 3005 | * binding of the Disk encryption key is done in two phases: first, this call will retrieve |
| 3006 | * the disk encryption key using the provided token / secret pair and store it by |
| 3007 | * encrypting it with a keymaster key not bound to the user, then fixateNewestUserKeyAuth |
| 3008 | * is called to delete all other bindings of the disk encryption key. |
| 3009 | */ |
| 3010 | @Override |
| 3011 | public void clearUserKeyAuth(int userId, int serialNumber, byte[] token, byte[] secret) { |
| 3012 | enforcePermission(android.Manifest.permission.STORAGE_INTERNAL); |
| 3013 | |
| 3014 | try { |
| 3015 | mVold.clearUserKeyAuth(userId, serialNumber, encodeBytes(token), encodeBytes(secret)); |
| 3016 | } catch (Exception e) { |
| 3017 | Slog.wtf(TAG, e); |
| 3018 | } |
| 3019 | } |
| 3020 | |
| 3021 | /* |
Paul Crowley | cc70155 | 2016-05-17 14:18:49 -0700 | [diff] [blame] | 3022 | * Delete all disk encryption token/secret pairs except the most recently added one |
| 3023 | */ |
| 3024 | @Override |
| 3025 | public void fixateNewestUserKeyAuth(int userId) { |
| 3026 | enforcePermission(android.Manifest.permission.STORAGE_INTERNAL); |
Paul Crowley | cc70155 | 2016-05-17 14:18:49 -0700 | [diff] [blame] | 3027 | |
| 3028 | try { |
Jeff Sharkey | 5440279 | 2017-09-15 16:05:19 -0600 | [diff] [blame] | 3029 | mVold.fixateNewestUserKeyAuth(userId); |
Jeff Sharkey | 43e1211 | 2017-09-12 16:31:45 -0600 | [diff] [blame] | 3030 | } catch (Exception e) { |
| 3031 | Slog.wtf(TAG, e); |
Paul Crowley | faeb3eb | 2016-02-08 15:58:29 +0000 | [diff] [blame] | 3032 | } |
| 3033 | } |
| 3034 | |
| 3035 | @Override |
| 3036 | public void unlockUserKey(int userId, int serialNumber, byte[] token, byte[] secret) { |
Sudheer Shanka | 03b20ec | 2019-02-21 15:11:00 -0800 | [diff] [blame] | 3037 | Slog.d(TAG, "unlockUserKey: " + userId); |
Jeff Sharkey | f9fc6d6 | 2015-11-08 16:46:05 -0800 | [diff] [blame] | 3038 | enforcePermission(android.Manifest.permission.STORAGE_INTERNAL); |
Jeff Sharkey | f9fc6d6 | 2015-11-08 16:46:05 -0800 | [diff] [blame] | 3039 | |
Jeff Sharkey | ce18c81 | 2016-04-27 16:00:41 -0600 | [diff] [blame] | 3040 | if (StorageManager.isFileEncryptedNativeOrEmulated()) { |
Jeff Sharkey | ce18c81 | 2016-04-27 16:00:41 -0600 | [diff] [blame] | 3041 | try { |
Jeff Sharkey | 5440279 | 2017-09-15 16:05:19 -0600 | [diff] [blame] | 3042 | mVold.unlockUserKey(userId, serialNumber, encodeBytes(token), |
| 3043 | encodeBytes(secret)); |
Jeff Sharkey | 43e1211 | 2017-09-12 16:31:45 -0600 | [diff] [blame] | 3044 | } catch (Exception e) { |
| 3045 | Slog.wtf(TAG, e); |
| 3046 | return; |
Jeff Sharkey | ce18c81 | 2016-04-27 16:00:41 -0600 | [diff] [blame] | 3047 | } |
Jeff Sharkey | f9fc6d6 | 2015-11-08 16:46:05 -0800 | [diff] [blame] | 3048 | } |
| 3049 | |
| 3050 | synchronized (mLock) { |
Jeff Sharkey | ce14cd0 | 2015-12-07 15:35:42 -0700 | [diff] [blame] | 3051 | mLocalUnlockedUsers = ArrayUtils.appendInt(mLocalUnlockedUsers, userId); |
Jeff Sharkey | f9fc6d6 | 2015-11-08 16:46:05 -0800 | [diff] [blame] | 3052 | } |
| 3053 | } |
| 3054 | |
| 3055 | @Override |
| 3056 | public void lockUserKey(int userId) { |
| 3057 | enforcePermission(android.Manifest.permission.STORAGE_INTERNAL); |
Jeff Sharkey | f9fc6d6 | 2015-11-08 16:46:05 -0800 | [diff] [blame] | 3058 | |
| 3059 | try { |
Jeff Sharkey | 5440279 | 2017-09-15 16:05:19 -0600 | [diff] [blame] | 3060 | mVold.lockUserKey(userId); |
Jeff Sharkey | 43e1211 | 2017-09-12 16:31:45 -0600 | [diff] [blame] | 3061 | } catch (Exception e) { |
| 3062 | Slog.wtf(TAG, e); |
| 3063 | return; |
Jeff Sharkey | f9fc6d6 | 2015-11-08 16:46:05 -0800 | [diff] [blame] | 3064 | } |
| 3065 | |
| 3066 | synchronized (mLock) { |
Jeff Sharkey | ce14cd0 | 2015-12-07 15:35:42 -0700 | [diff] [blame] | 3067 | mLocalUnlockedUsers = ArrayUtils.removeInt(mLocalUnlockedUsers, userId); |
Jeff Sharkey | f9fc6d6 | 2015-11-08 16:46:05 -0800 | [diff] [blame] | 3068 | } |
| 3069 | } |
| 3070 | |
| 3071 | @Override |
| 3072 | public boolean isUserKeyUnlocked(int userId) { |
Jeff Sharkey | ce18c81 | 2016-04-27 16:00:41 -0600 | [diff] [blame] | 3073 | synchronized (mLock) { |
| 3074 | return ArrayUtils.contains(mLocalUnlockedUsers, userId); |
Jeff Sharkey | f9fc6d6 | 2015-11-08 16:46:05 -0800 | [diff] [blame] | 3075 | } |
| 3076 | } |
| 3077 | |
Narayan Kamath | 157dd1d | 2019-06-12 13:06:30 +0100 | [diff] [blame] | 3078 | private boolean isSystemUnlocked(int userId) { |
| 3079 | synchronized (mLock) { |
| 3080 | return ArrayUtils.contains(mSystemUnlockedUsers, userId); |
| 3081 | } |
| 3082 | } |
| 3083 | |
Jeff Sharkey | f9fc6d6 | 2015-11-08 16:46:05 -0800 | [diff] [blame] | 3084 | @Override |
Jeff Sharkey | 47f7108 | 2016-02-01 17:03:54 -0700 | [diff] [blame] | 3085 | public void prepareUserStorage(String volumeUuid, int userId, int serialNumber, int flags) { |
Jeff Sharkey | f9fc6d6 | 2015-11-08 16:46:05 -0800 | [diff] [blame] | 3086 | enforcePermission(android.Manifest.permission.STORAGE_INTERNAL); |
Jeff Sharkey | f9fc6d6 | 2015-11-08 16:46:05 -0800 | [diff] [blame] | 3087 | |
| 3088 | try { |
Jeff Sharkey | 5440279 | 2017-09-15 16:05:19 -0600 | [diff] [blame] | 3089 | mVold.prepareUserStorage(volumeUuid, userId, serialNumber, flags); |
Jeff Sharkey | 43e1211 | 2017-09-12 16:31:45 -0600 | [diff] [blame] | 3090 | } catch (Exception e) { |
| 3091 | Slog.wtf(TAG, e); |
Paul Crowley | 7ec733f | 2015-05-19 12:42:00 +0100 | [diff] [blame] | 3092 | } |
| 3093 | } |
| 3094 | |
Paul Crowley | bcf48ed | 2015-04-22 13:36:59 +0100 | [diff] [blame] | 3095 | @Override |
Jeff Sharkey | fcf1e55 | 2016-04-14 20:44:58 -0600 | [diff] [blame] | 3096 | public void destroyUserStorage(String volumeUuid, int userId, int flags) { |
| 3097 | enforcePermission(android.Manifest.permission.STORAGE_INTERNAL); |
Jeff Sharkey | fcf1e55 | 2016-04-14 20:44:58 -0600 | [diff] [blame] | 3098 | |
| 3099 | try { |
Jeff Sharkey | 5440279 | 2017-09-15 16:05:19 -0600 | [diff] [blame] | 3100 | mVold.destroyUserStorage(volumeUuid, userId, flags); |
Jeff Sharkey | 43e1211 | 2017-09-12 16:31:45 -0600 | [diff] [blame] | 3101 | } catch (Exception e) { |
| 3102 | Slog.wtf(TAG, e); |
Jeff Sharkey | fcf1e55 | 2016-04-14 20:44:58 -0600 | [diff] [blame] | 3103 | } |
| 3104 | } |
| 3105 | |
Anton Hansson | be9c990 | 2019-05-28 13:45:26 +0100 | [diff] [blame] | 3106 | /** Not thread safe */ |
Daichi Hirono | e56740d | 2017-02-02 13:56:45 +0900 | [diff] [blame] | 3107 | class AppFuseMountScope extends AppFuseBridge.MountScope { |
Anton Hansson | be9c990 | 2019-05-28 13:45:26 +0100 | [diff] [blame] | 3108 | private boolean mMounted = false; |
Daichi Hirono | 812c95d | 2017-02-08 16:20:20 +0900 | [diff] [blame] | 3109 | |
Ryo Hashimoto | 38bcbf0 | 2018-05-16 18:20:37 +0900 | [diff] [blame] | 3110 | public AppFuseMountScope(int uid, int mountId) { |
| 3111 | super(uid, mountId); |
Daichi Hirono | 812c95d | 2017-02-08 16:20:20 +0900 | [diff] [blame] | 3112 | } |
| 3113 | |
| 3114 | @Override |
| 3115 | public ParcelFileDescriptor open() throws NativeDaemonConnectorException { |
Jeff Sharkey | 5440279 | 2017-09-15 16:05:19 -0600 | [diff] [blame] | 3116 | try { |
Anton Hansson | be9c990 | 2019-05-28 13:45:26 +0100 | [diff] [blame] | 3117 | final FileDescriptor fd = mVold.mountAppFuse(uid, mountId); |
| 3118 | mMounted = true; |
| 3119 | return new ParcelFileDescriptor(fd); |
Jeff Sharkey | 5440279 | 2017-09-15 16:05:19 -0600 | [diff] [blame] | 3120 | } catch (Exception e) { |
| 3121 | throw new NativeDaemonConnectorException("Failed to mount", e); |
Daichi Hirono | 812c95d | 2017-02-08 16:20:20 +0900 | [diff] [blame] | 3122 | } |
Daichi Hirono | 9fb0018 | 2016-11-08 14:12:17 +0900 | [diff] [blame] | 3123 | } |
| 3124 | |
| 3125 | @Override |
Ryo Hashimoto | 38bcbf0 | 2018-05-16 18:20:37 +0900 | [diff] [blame] | 3126 | public ParcelFileDescriptor openFile(int mountId, int fileId, int flags) |
| 3127 | throws NativeDaemonConnectorException { |
| 3128 | try { |
| 3129 | return new ParcelFileDescriptor( |
| 3130 | mVold.openAppFuseFile(uid, mountId, fileId, flags)); |
| 3131 | } catch (Exception e) { |
| 3132 | throw new NativeDaemonConnectorException("Failed to open", e); |
| 3133 | } |
| 3134 | } |
| 3135 | |
| 3136 | @Override |
Daichi Hirono | e56740d | 2017-02-02 13:56:45 +0900 | [diff] [blame] | 3137 | public void close() throws Exception { |
Anton Hansson | be9c990 | 2019-05-28 13:45:26 +0100 | [diff] [blame] | 3138 | if (mMounted) { |
Ryo Hashimoto | 38bcbf0 | 2018-05-16 18:20:37 +0900 | [diff] [blame] | 3139 | mVold.unmountAppFuse(uid, mountId); |
Anton Hansson | be9c990 | 2019-05-28 13:45:26 +0100 | [diff] [blame] | 3140 | mMounted = false; |
Daichi Hirono | 812c95d | 2017-02-08 16:20:20 +0900 | [diff] [blame] | 3141 | } |
Daichi Hirono | 9fb0018 | 2016-11-08 14:12:17 +0900 | [diff] [blame] | 3142 | } |
| 3143 | } |
| 3144 | |
| 3145 | @Override |
Daichi Hirono | 812c95d | 2017-02-08 16:20:20 +0900 | [diff] [blame] | 3146 | public @Nullable AppFuseMount mountProxyFileDescriptorBridge() { |
Daichi Hirono | e56740d | 2017-02-02 13:56:45 +0900 | [diff] [blame] | 3147 | Slog.v(TAG, "mountProxyFileDescriptorBridge"); |
Daichi Hirono | 9fb0018 | 2016-11-08 14:12:17 +0900 | [diff] [blame] | 3148 | final int uid = Binder.getCallingUid(); |
Daichi Hirono | 9fb0018 | 2016-11-08 14:12:17 +0900 | [diff] [blame] | 3149 | |
Daichi Hirono | e56740d | 2017-02-02 13:56:45 +0900 | [diff] [blame] | 3150 | while (true) { |
| 3151 | synchronized (mAppFuseLock) { |
| 3152 | boolean newlyCreated = false; |
| 3153 | if (mAppFuseBridge == null) { |
| 3154 | mAppFuseBridge = new AppFuseBridge(); |
| 3155 | new Thread(mAppFuseBridge, AppFuseBridge.TAG).start(); |
| 3156 | newlyCreated = true; |
Daichi Hirono | 9fb0018 | 2016-11-08 14:12:17 +0900 | [diff] [blame] | 3157 | } |
Daichi Hirono | e56740d | 2017-02-02 13:56:45 +0900 | [diff] [blame] | 3158 | try { |
| 3159 | final int name = mNextAppFuseName++; |
| 3160 | try { |
| 3161 | return new AppFuseMount( |
Ryo Hashimoto | 38bcbf0 | 2018-05-16 18:20:37 +0900 | [diff] [blame] | 3162 | name, mAppFuseBridge.addBridge(new AppFuseMountScope(uid, name))); |
Daichi Hirono | 812c95d | 2017-02-08 16:20:20 +0900 | [diff] [blame] | 3163 | } catch (FuseUnavailableMountException e) { |
Daichi Hirono | e56740d | 2017-02-02 13:56:45 +0900 | [diff] [blame] | 3164 | if (newlyCreated) { |
| 3165 | // If newly created bridge fails, it's a real error. |
Daichi Hirono | 812c95d | 2017-02-08 16:20:20 +0900 | [diff] [blame] | 3166 | Slog.e(TAG, "", e); |
| 3167 | return null; |
Daichi Hirono | e56740d | 2017-02-02 13:56:45 +0900 | [diff] [blame] | 3168 | } |
| 3169 | // It seems the thread of mAppFuseBridge has already been terminated. |
| 3170 | mAppFuseBridge = null; |
| 3171 | } |
| 3172 | } catch (NativeDaemonConnectorException e) { |
| 3173 | throw e.rethrowAsParcelableException(); |
| 3174 | } |
Daichi Hirono | 9fb0018 | 2016-11-08 14:12:17 +0900 | [diff] [blame] | 3175 | } |
Daichi Hirono | 9fb0018 | 2016-11-08 14:12:17 +0900 | [diff] [blame] | 3176 | } |
| 3177 | } |
| 3178 | |
| 3179 | @Override |
Daichi Hirono | 812c95d | 2017-02-08 16:20:20 +0900 | [diff] [blame] | 3180 | public @Nullable ParcelFileDescriptor openProxyFileDescriptor( |
| 3181 | int mountId, int fileId, int mode) { |
| 3182 | Slog.v(TAG, "mountProxyFileDescriptor"); |
Jeff Sharkey | 3946632 | 2018-12-05 19:19:52 -0700 | [diff] [blame] | 3183 | |
| 3184 | // We only support a narrow set of incoming mode flags |
Jeff Sharkey | 3b1c254 | 2018-12-13 15:01:38 -0700 | [diff] [blame] | 3185 | mode &= MODE_READ_WRITE; |
Jeff Sharkey | 3946632 | 2018-12-05 19:19:52 -0700 | [diff] [blame] | 3186 | |
Daichi Hirono | 9fb0018 | 2016-11-08 14:12:17 +0900 | [diff] [blame] | 3187 | try { |
| 3188 | synchronized (mAppFuseLock) { |
Daichi Hirono | e56740d | 2017-02-02 13:56:45 +0900 | [diff] [blame] | 3189 | if (mAppFuseBridge == null) { |
Daichi Hirono | 812c95d | 2017-02-08 16:20:20 +0900 | [diff] [blame] | 3190 | Slog.e(TAG, "FuseBridge has not been created"); |
| 3191 | return null; |
Daichi Hirono | 9fb0018 | 2016-11-08 14:12:17 +0900 | [diff] [blame] | 3192 | } |
Ryo Hashimoto | 38bcbf0 | 2018-05-16 18:20:37 +0900 | [diff] [blame] | 3193 | return mAppFuseBridge.openFile(mountId, fileId, mode); |
Daichi Hirono | 9fb0018 | 2016-11-08 14:12:17 +0900 | [diff] [blame] | 3194 | } |
Daichi Hirono | 812c95d | 2017-02-08 16:20:20 +0900 | [diff] [blame] | 3195 | } catch (FuseUnavailableMountException | InterruptedException error) { |
| 3196 | Slog.v(TAG, "The mount point has already been invalid", error); |
| 3197 | return null; |
Daichi Hirono | 9fb0018 | 2016-11-08 14:12:17 +0900 | [diff] [blame] | 3198 | } |
| 3199 | } |
| 3200 | |
Daichi Hirono | 9e8d9e2 | 2015-11-13 14:37:00 +0900 | [diff] [blame] | 3201 | @Override |
Jeff Sharkey | ae26646 | 2017-11-27 13:32:24 -0700 | [diff] [blame] | 3202 | public void mkdirs(String callingPkg, String appPath) { |
Sudheer Shanka | a63bfb3 | 2019-02-28 11:15:58 -0800 | [diff] [blame] | 3203 | final int callingUid = Binder.getCallingUid(); |
| 3204 | final int userId = UserHandle.getUserId(callingUid); |
Farid Zare Seisan | ac09451 | 2018-04-02 15:06:13 -0700 | [diff] [blame] | 3205 | final String propertyName = "sys.user." + userId + ".ce_available"; |
Jeff Sharkey | 2d8b4e8 | 2013-09-17 17:30:33 -0700 | [diff] [blame] | 3206 | |
Jeff Sharkey | 196c755 | 2018-03-26 15:56:50 -0600 | [diff] [blame] | 3207 | // Ignore requests to create directories while storage is locked |
Farid Zare Seisan | ac09451 | 2018-04-02 15:06:13 -0700 | [diff] [blame] | 3208 | if (!isUserKeyUnlocked(userId)) { |
| 3209 | throw new IllegalStateException("Failed to prepare " + appPath); |
| 3210 | } |
| 3211 | |
| 3212 | // Ignore requests to create directories if CE storage is not available |
Farid Zare Seisan | 7f6b812 | 2018-03-30 11:19:10 -0700 | [diff] [blame] | 3213 | if ((userId == UserHandle.USER_SYSTEM) |
| 3214 | && !SystemProperties.getBoolean(propertyName, false)) { |
Farid Zare Seisan | ac09451 | 2018-04-02 15:06:13 -0700 | [diff] [blame] | 3215 | throw new IllegalStateException("Failed to prepare " + appPath); |
| 3216 | } |
Jeff Sharkey | 196c755 | 2018-03-26 15:56:50 -0600 | [diff] [blame] | 3217 | |
Jeff Sharkey | 2d8b4e8 | 2013-09-17 17:30:33 -0700 | [diff] [blame] | 3218 | // Validate that reported package name belongs to caller |
| 3219 | final AppOpsManager appOps = (AppOpsManager) mContext.getSystemService( |
| 3220 | Context.APP_OPS_SERVICE); |
Sudheer Shanka | a63bfb3 | 2019-02-28 11:15:58 -0800 | [diff] [blame] | 3221 | appOps.checkPackage(callingUid, callingPkg); |
Jeff Sharkey | 2d8b4e8 | 2013-09-17 17:30:33 -0700 | [diff] [blame] | 3222 | |
Jeff Sharkey | 4887789 | 2015-03-18 11:27:19 -0700 | [diff] [blame] | 3223 | File appFile = null; |
Jeff Sharkey | 2d8b4e8 | 2013-09-17 17:30:33 -0700 | [diff] [blame] | 3224 | try { |
Jeff Sharkey | 4887789 | 2015-03-18 11:27:19 -0700 | [diff] [blame] | 3225 | appFile = new File(appPath).getCanonicalFile(); |
Jeff Sharkey | 2d8b4e8 | 2013-09-17 17:30:33 -0700 | [diff] [blame] | 3226 | } catch (IOException e) { |
Jeff Sharkey | ae26646 | 2017-11-27 13:32:24 -0700 | [diff] [blame] | 3227 | throw new IllegalStateException("Failed to resolve " + appPath + ": " + e); |
Jeff Sharkey | 2d8b4e8 | 2013-09-17 17:30:33 -0700 | [diff] [blame] | 3228 | } |
| 3229 | |
Martijn Coenen | 9fd2b64 | 2019-12-24 13:04:36 +0100 | [diff] [blame] | 3230 | appPath = appFile.getAbsolutePath(); |
| 3231 | if (!appPath.endsWith("/")) { |
| 3232 | appPath = appPath + "/"; |
| 3233 | } |
| 3234 | // Ensure that the path we're asked to create is a known application directory |
| 3235 | // path. |
| 3236 | final Matcher matcher = KNOWN_APP_DIR_PATHS.matcher(appPath); |
| 3237 | if (matcher.matches()) { |
| 3238 | // And that the package dir matches the calling package |
| 3239 | if (!matcher.group(3).equals(callingPkg)) { |
| 3240 | throw new SecurityException("Invalid mkdirs path: " + appFile |
| 3241 | + " does not contain calling package " + callingPkg); |
Jeff Sharkey | 4887789 | 2015-03-18 11:27:19 -0700 | [diff] [blame] | 3242 | } |
Martijn Coenen | 9fd2b64 | 2019-12-24 13:04:36 +0100 | [diff] [blame] | 3243 | // And that the user id part of the path (if any) matches the calling user id, |
| 3244 | // or if for a public volume (no user id), the user matches the current user |
| 3245 | if ((matcher.group(2) != null && !matcher.group(2).equals(Integer.toString(userId))) |
| 3246 | || (matcher.group(2) == null && userId != mCurrentUserId)) { |
| 3247 | throw new SecurityException("Invalid mkdirs path: " + appFile |
| 3248 | + " does not match calling user id " + userId); |
| 3249 | } |
Jeff Sharkey | 2d8b4e8 | 2013-09-17 17:30:33 -0700 | [diff] [blame] | 3250 | try { |
Martijn Coenen | 9fd2b64 | 2019-12-24 13:04:36 +0100 | [diff] [blame] | 3251 | mVold.setupAppDir(appPath, matcher.group(1), callingUid); |
| 3252 | } catch (RemoteException e) { |
Sudheer Shanka | 64501e5 | 2019-04-29 10:46:26 -0700 | [diff] [blame] | 3253 | throw new IllegalStateException("Failed to prepare " + appPath + ": " + e); |
Jeff Sharkey | 2d8b4e8 | 2013-09-17 17:30:33 -0700 | [diff] [blame] | 3254 | } |
Jeff Sharkey | 2d8b4e8 | 2013-09-17 17:30:33 -0700 | [diff] [blame] | 3255 | |
Martijn Coenen | 9fd2b64 | 2019-12-24 13:04:36 +0100 | [diff] [blame] | 3256 | return; |
| 3257 | } |
| 3258 | throw new SecurityException("Invalid mkdirs path: " + appFile |
| 3259 | + " is not a known app path."); |
Jeff Sharkey | 2d8b4e8 | 2013-09-17 17:30:33 -0700 | [diff] [blame] | 3260 | } |
| 3261 | |
| 3262 | @Override |
Jeff Sharkey | 4634987 | 2015-07-28 10:49:47 -0700 | [diff] [blame] | 3263 | public StorageVolume[] getVolumeList(int uid, String packageName, int flags) { |
Jeff Sharkey | ce14cd0 | 2015-12-07 15:35:42 -0700 | [diff] [blame] | 3264 | final int userId = UserHandle.getUserId(uid); |
Jeff Sharkey | c02bfae | 2016-03-27 15:06:53 -0600 | [diff] [blame] | 3265 | |
Jeff Sharkey | 4634987 | 2015-07-28 10:49:47 -0700 | [diff] [blame] | 3266 | final boolean forWrite = (flags & StorageManager.FLAG_FOR_WRITE) != 0; |
Jeff Sharkey | c02bfae | 2016-03-27 15:06:53 -0600 | [diff] [blame] | 3267 | final boolean realState = (flags & StorageManager.FLAG_REAL_STATE) != 0; |
| 3268 | final boolean includeInvisible = (flags & StorageManager.FLAG_INCLUDE_INVISIBLE) != 0; |
Jeff Sharkey | 04b4ba1 | 2019-12-15 22:42:42 -0700 | [diff] [blame] | 3269 | final boolean includeRecent = (flags & StorageManager.FLAG_INCLUDE_RECENT) != 0; |
Jeff Sharkey | 4634987 | 2015-07-28 10:49:47 -0700 | [diff] [blame] | 3270 | |
Narayan Kamath | 157dd1d | 2019-06-12 13:06:30 +0100 | [diff] [blame] | 3271 | // Report all volumes as unmounted until we've recorded that user 0 has unlocked. There |
| 3272 | // are no guarantees that callers will see a consistent view of the volume before that |
| 3273 | // point |
| 3274 | final boolean systemUserUnlocked = isSystemUnlocked(UserHandle.USER_SYSTEM); |
| 3275 | |
Jeff Sharkey | 04b4ba1 | 2019-12-15 22:42:42 -0700 | [diff] [blame] | 3276 | final boolean userIsDemo; |
Jeff Sharkey | c02bfae | 2016-03-27 15:06:53 -0600 | [diff] [blame] | 3277 | final boolean userKeyUnlocked; |
| 3278 | final boolean storagePermission; |
| 3279 | final long token = Binder.clearCallingIdentity(); |
Svetoslav | 38c3dbb | 2015-07-14 11:27:06 -0700 | [diff] [blame] | 3280 | try { |
Jeff Sharkey | 04b4ba1 | 2019-12-15 22:42:42 -0700 | [diff] [blame] | 3281 | userIsDemo = LocalServices.getService(UserManagerInternal.class) |
| 3282 | .getUserInfo(userId).isDemo(); |
Jeff Sharkey | c02bfae | 2016-03-27 15:06:53 -0600 | [diff] [blame] | 3283 | userKeyUnlocked = isUserKeyUnlocked(userId); |
Sudheer Shanka | 2250d56 | 2016-11-07 15:41:02 -0800 | [diff] [blame] | 3284 | storagePermission = mStorageManagerInternal.hasExternalStorage(uid, packageName); |
Svetoslav | 38c3dbb | 2015-07-14 11:27:06 -0700 | [diff] [blame] | 3285 | } finally { |
Jeff Sharkey | c02bfae | 2016-03-27 15:06:53 -0600 | [diff] [blame] | 3286 | Binder.restoreCallingIdentity(token); |
Svetoslav | 38c3dbb | 2015-07-14 11:27:06 -0700 | [diff] [blame] | 3287 | } |
Svet Ganov | 6ee871e | 2015-07-10 14:29:33 -0700 | [diff] [blame] | 3288 | |
Jeff Sharkey | c02bfae | 2016-03-27 15:06:53 -0600 | [diff] [blame] | 3289 | boolean foundPrimary = false; |
| 3290 | |
Jeff Sharkey | ce14cd0 | 2015-12-07 15:35:42 -0700 | [diff] [blame] | 3291 | final ArrayList<StorageVolume> res = new ArrayList<>(); |
Jeff Sharkey | 04b4ba1 | 2019-12-15 22:42:42 -0700 | [diff] [blame] | 3292 | final ArraySet<String> resUuids = new ArraySet<>(); |
Jeff Sharkey | 4887789 | 2015-03-18 11:27:19 -0700 | [diff] [blame] | 3293 | synchronized (mLock) { |
Jeff Sharkey | 1b8ef7e | 2015-04-03 17:14:45 -0700 | [diff] [blame] | 3294 | for (int i = 0; i < mVolumes.size(); i++) { |
| 3295 | final VolumeInfo vol = mVolumes.valueAt(i); |
Jeff Sharkey | c02bfae | 2016-03-27 15:06:53 -0600 | [diff] [blame] | 3296 | switch (vol.getType()) { |
| 3297 | case VolumeInfo.TYPE_PUBLIC: |
Risan | 05c41e6 | 2018-10-29 08:57:43 +0900 | [diff] [blame] | 3298 | case VolumeInfo.TYPE_STUB: |
Jeff Sharkey | c02bfae | 2016-03-27 15:06:53 -0600 | [diff] [blame] | 3299 | break; |
Zim | 17be6f9 | 2019-09-25 14:37:55 +0100 | [diff] [blame] | 3300 | case VolumeInfo.TYPE_EMULATED: |
| 3301 | if (vol.getMountUserId() == userId) { |
| 3302 | break; |
| 3303 | } |
| 3304 | // Skip if emulated volume not for userId |
Jeff Sharkey | c02bfae | 2016-03-27 15:06:53 -0600 | [diff] [blame] | 3305 | default: |
| 3306 | continue; |
| 3307 | } |
| 3308 | |
| 3309 | boolean match = false; |
| 3310 | if (forWrite) { |
| 3311 | match = vol.isVisibleForWrite(userId); |
| 3312 | } else { |
Felipe Leme | 123a0e7 | 2016-06-10 11:09:11 -0700 | [diff] [blame] | 3313 | match = vol.isVisibleForRead(userId) |
| 3314 | || (includeInvisible && vol.getPath() != null); |
Jeff Sharkey | c02bfae | 2016-03-27 15:06:53 -0600 | [diff] [blame] | 3315 | } |
| 3316 | if (!match) continue; |
| 3317 | |
| 3318 | boolean reportUnmounted = false; |
Narayan Kamath | 157dd1d | 2019-06-12 13:06:30 +0100 | [diff] [blame] | 3319 | if (!systemUserUnlocked) { |
| 3320 | reportUnmounted = true; |
| 3321 | } else if ((vol.getType() == VolumeInfo.TYPE_EMULATED) && !userKeyUnlocked) { |
Jeff Sharkey | c02bfae | 2016-03-27 15:06:53 -0600 | [diff] [blame] | 3322 | reportUnmounted = true; |
| 3323 | } else if (!storagePermission && !realState) { |
| 3324 | reportUnmounted = true; |
| 3325 | } |
| 3326 | |
| 3327 | final StorageVolume userVol = vol.buildStorageVolume(mContext, userId, |
| 3328 | reportUnmounted); |
| 3329 | if (vol.isPrimary()) { |
| 3330 | res.add(0, userVol); |
| 3331 | foundPrimary = true; |
| 3332 | } else { |
| 3333 | res.add(userVol); |
Jeff Sharkey | b049e21 | 2012-09-07 23:16:01 -0700 | [diff] [blame] | 3334 | } |
Jeff Sharkey | 04b4ba1 | 2019-12-15 22:42:42 -0700 | [diff] [blame] | 3335 | resUuids.add(userVol.getUuid()); |
Mike Lockwood | 8fa5f80 | 2011-03-24 08:12:30 -0700 | [diff] [blame] | 3336 | } |
Jeff Sharkey | 04b4ba1 | 2019-12-15 22:42:42 -0700 | [diff] [blame] | 3337 | |
| 3338 | if (includeRecent) { |
| 3339 | final long lastWeek = System.currentTimeMillis() - DateUtils.WEEK_IN_MILLIS; |
| 3340 | for (int i = 0; i < mRecords.size(); i++) { |
| 3341 | final VolumeRecord rec = mRecords.valueAt(i); |
| 3342 | |
| 3343 | // Skip if we've already included it above |
| 3344 | if (resUuids.contains(rec.fsUuid)) continue; |
| 3345 | |
| 3346 | // Treat as recent if mounted within the last week |
| 3347 | if (rec.lastSeenMillis > 0 && rec.lastSeenMillis < lastWeek) { |
| 3348 | final StorageVolume userVol = rec.buildStorageVolume(mContext); |
| 3349 | res.add(userVol); |
| 3350 | resUuids.add(userVol.getUuid()); |
| 3351 | } |
| 3352 | } |
| 3353 | } |
| 3354 | } |
| 3355 | |
| 3356 | // Synthesize a volume for preloaded media under demo users, so that |
| 3357 | // it's scanned into MediaStore |
| 3358 | if (userIsDemo) { |
| 3359 | final String id = "demo"; |
| 3360 | final File path = Environment.getDataPreloadsMediaDirectory(); |
| 3361 | final boolean primary = false; |
| 3362 | final boolean removable = false; |
| 3363 | final boolean emulated = true; |
| 3364 | final boolean allowMassStorage = false; |
| 3365 | final long maxFileSize = 0; |
| 3366 | final UserHandle user = new UserHandle(userId); |
| 3367 | final String envState = Environment.MEDIA_MOUNTED_READ_ONLY; |
| 3368 | final String description = mContext.getString(android.R.string.unknownName); |
| 3369 | |
| 3370 | res.add(new StorageVolume(id, path, path, description, primary, removable, |
| 3371 | emulated, allowMassStorage, maxFileSize, user, id, envState)); |
Mike Lockwood | 8fa5f80 | 2011-03-24 08:12:30 -0700 | [diff] [blame] | 3372 | } |
Jeff Sharkey | 4887789 | 2015-03-18 11:27:19 -0700 | [diff] [blame] | 3373 | |
| 3374 | if (!foundPrimary) { |
Jeff Sharkey | 11697f5 | 2018-12-13 10:14:42 -0700 | [diff] [blame] | 3375 | Slog.w(TAG, "No primary storage defined yet; hacking together a stub"); |
Jeff Sharkey | 4887789 | 2015-03-18 11:27:19 -0700 | [diff] [blame] | 3376 | |
| 3377 | final boolean primaryPhysical = SystemProperties.getBoolean( |
Jeff Sharkey | 1b8ef7e | 2015-04-03 17:14:45 -0700 | [diff] [blame] | 3378 | StorageManager.PROP_PRIMARY_PHYSICAL, false); |
Jeff Sharkey | 4887789 | 2015-03-18 11:27:19 -0700 | [diff] [blame] | 3379 | |
| 3380 | final String id = "stub_primary"; |
| 3381 | final File path = Environment.getLegacyExternalStorageDirectory(); |
Jeff Sharkey | 1b8ef7e | 2015-04-03 17:14:45 -0700 | [diff] [blame] | 3382 | final String description = mContext.getString(android.R.string.unknownName); |
Jeff Sharkey | 4887789 | 2015-03-18 11:27:19 -0700 | [diff] [blame] | 3383 | final boolean primary = true; |
| 3384 | final boolean removable = primaryPhysical; |
| 3385 | final boolean emulated = !primaryPhysical; |
Jeff Sharkey | 4887789 | 2015-03-18 11:27:19 -0700 | [diff] [blame] | 3386 | final boolean allowMassStorage = false; |
| 3387 | final long maxFileSize = 0L; |
| 3388 | final UserHandle owner = new UserHandle(userId); |
| 3389 | final String uuid = null; |
Jeff Sharkey | 4887789 | 2015-03-18 11:27:19 -0700 | [diff] [blame] | 3390 | final String state = Environment.MEDIA_REMOVED; |
| 3391 | |
Jerry Zhang | 71938e1 | 2018-05-10 18:28:29 -0700 | [diff] [blame] | 3392 | res.add(0, new StorageVolume(id, path, path, |
Jerry Zhang | f9c5c25 | 2017-08-16 18:07:51 -0700 | [diff] [blame] | 3393 | description, primary, removable, emulated, |
Jeff Sharkey | 1b8ef7e | 2015-04-03 17:14:45 -0700 | [diff] [blame] | 3394 | allowMassStorage, maxFileSize, owner, uuid, state)); |
Jeff Sharkey | 4887789 | 2015-03-18 11:27:19 -0700 | [diff] [blame] | 3395 | } |
| 3396 | |
| 3397 | return res.toArray(new StorageVolume[res.size()]); |
Mike Lockwood | 8fa5f80 | 2011-03-24 08:12:30 -0700 | [diff] [blame] | 3398 | } |
| 3399 | |
Jeff Sharkey | 1b8ef7e | 2015-04-03 17:14:45 -0700 | [diff] [blame] | 3400 | @Override |
| 3401 | public DiskInfo[] getDisks() { |
| 3402 | synchronized (mLock) { |
| 3403 | final DiskInfo[] res = new DiskInfo[mDisks.size()]; |
| 3404 | for (int i = 0; i < mDisks.size(); i++) { |
| 3405 | res[i] = mDisks.valueAt(i); |
| 3406 | } |
| 3407 | return res; |
| 3408 | } |
| 3409 | } |
| 3410 | |
| 3411 | @Override |
Jeff Sharkey | d95d3bf | 2015-04-14 21:39:44 -0700 | [diff] [blame] | 3412 | public VolumeInfo[] getVolumes(int flags) { |
Jeff Sharkey | 1b8ef7e | 2015-04-03 17:14:45 -0700 | [diff] [blame] | 3413 | synchronized (mLock) { |
| 3414 | final VolumeInfo[] res = new VolumeInfo[mVolumes.size()]; |
| 3415 | for (int i = 0; i < mVolumes.size(); i++) { |
| 3416 | res[i] = mVolumes.valueAt(i); |
| 3417 | } |
| 3418 | return res; |
| 3419 | } |
| 3420 | } |
| 3421 | |
Jeff Sharkey | b36586a | 2015-04-27 08:42:28 -0700 | [diff] [blame] | 3422 | @Override |
| 3423 | public VolumeRecord[] getVolumeRecords(int flags) { |
| 3424 | synchronized (mLock) { |
| 3425 | final VolumeRecord[] res = new VolumeRecord[mRecords.size()]; |
| 3426 | for (int i = 0; i < mRecords.size(); i++) { |
| 3427 | res[i] = mRecords.valueAt(i); |
| 3428 | } |
| 3429 | return res; |
| 3430 | } |
| 3431 | } |
| 3432 | |
Jeff Sharkey | 9bed070 | 2017-01-23 20:37:05 -0700 | [diff] [blame] | 3433 | @Override |
| 3434 | public long getCacheQuotaBytes(String volumeUuid, int uid) { |
| 3435 | if (uid != Binder.getCallingUid()) { |
| 3436 | mContext.enforceCallingPermission(android.Manifest.permission.STORAGE_INTERNAL, TAG); |
| 3437 | } |
Daniel Nishi | 80fdb01 | 2017-03-09 14:30:07 -0800 | [diff] [blame] | 3438 | final long token = Binder.clearCallingIdentity(); |
| 3439 | final StorageStatsManager stats = mContext.getSystemService(StorageStatsManager.class); |
| 3440 | try { |
| 3441 | return stats.getCacheQuotaBytes(volumeUuid, uid); |
| 3442 | } finally { |
| 3443 | Binder.restoreCallingIdentity(token); |
| 3444 | } |
Jeff Sharkey | 9bed070 | 2017-01-23 20:37:05 -0700 | [diff] [blame] | 3445 | } |
| 3446 | |
| 3447 | @Override |
| 3448 | public long getCacheSizeBytes(String volumeUuid, int uid) { |
| 3449 | if (uid != Binder.getCallingUid()) { |
| 3450 | mContext.enforceCallingPermission(android.Manifest.permission.STORAGE_INTERNAL, TAG); |
| 3451 | } |
| 3452 | final long token = Binder.clearCallingIdentity(); |
| 3453 | try { |
| 3454 | return mContext.getSystemService(StorageStatsManager.class) |
| 3455 | .queryStatsForUid(volumeUuid, uid).getCacheBytes(); |
Jeff Sharkey | 789a8fc | 2017-04-16 13:18:35 -0600 | [diff] [blame] | 3456 | } catch (IOException e) { |
| 3457 | throw new ParcelableException(e); |
Jeff Sharkey | 9bed070 | 2017-01-23 20:37:05 -0700 | [diff] [blame] | 3458 | } finally { |
| 3459 | Binder.restoreCallingIdentity(token); |
| 3460 | } |
| 3461 | } |
| 3462 | |
Jeff Sharkey | 35e46d2 | 2017-06-09 10:01:20 -0600 | [diff] [blame] | 3463 | private int adjustAllocateFlags(int flags, int callingUid, String callingPackage) { |
| 3464 | // Require permission to allocate aggressively |
| 3465 | if ((flags & StorageManager.FLAG_ALLOCATE_AGGRESSIVE) != 0) { |
Jeff Sharkey | d5d5e92 | 2017-02-21 10:51:23 -0700 | [diff] [blame] | 3466 | mContext.enforceCallingOrSelfPermission( |
| 3467 | android.Manifest.permission.ALLOCATE_AGGRESSIVE, TAG); |
| 3468 | } |
| 3469 | |
Jeff Sharkey | 35e46d2 | 2017-06-09 10:01:20 -0600 | [diff] [blame] | 3470 | // Apps normally can't directly defy reserved space |
| 3471 | flags &= ~StorageManager.FLAG_ALLOCATE_DEFY_ALL_RESERVED; |
| 3472 | flags &= ~StorageManager.FLAG_ALLOCATE_DEFY_HALF_RESERVED; |
| 3473 | |
| 3474 | // However, if app is actively using the camera, then we're willing to |
| 3475 | // clear up to half of the reserved cache space, since the user might be |
| 3476 | // trying to capture an important memory. |
| 3477 | final AppOpsManager appOps = mContext.getSystemService(AppOpsManager.class); |
| 3478 | final long token = Binder.clearCallingIdentity(); |
| 3479 | try { |
| 3480 | if (appOps.isOperationActive(AppOpsManager.OP_CAMERA, callingUid, callingPackage)) { |
| 3481 | Slog.d(TAG, "UID " + callingUid + " is actively using camera;" |
| 3482 | + " letting them defy reserved cached data"); |
| 3483 | flags |= StorageManager.FLAG_ALLOCATE_DEFY_HALF_RESERVED; |
| 3484 | } |
| 3485 | } finally { |
| 3486 | Binder.restoreCallingIdentity(token); |
| 3487 | } |
| 3488 | |
| 3489 | return flags; |
| 3490 | } |
| 3491 | |
| 3492 | @Override |
| 3493 | public long getAllocatableBytes(String volumeUuid, int flags, String callingPackage) { |
| 3494 | flags = adjustAllocateFlags(flags, Binder.getCallingUid(), callingPackage); |
| 3495 | |
| 3496 | final StorageManager storage = mContext.getSystemService(StorageManager.class); |
| 3497 | final StorageStatsManager stats = mContext.getSystemService(StorageStatsManager.class); |
Jeff Sharkey | d5d5e92 | 2017-02-21 10:51:23 -0700 | [diff] [blame] | 3498 | final long token = Binder.clearCallingIdentity(); |
| 3499 | try { |
| 3500 | // In general, apps can allocate as much space as they want, except |
| 3501 | // we never let them eat into either the minimum cache space or into |
Jeff Sharkey | ddff807 | 2017-05-26 13:10:46 -0600 | [diff] [blame] | 3502 | // the low disk warning space. To avoid user confusion, this logic |
| 3503 | // should be kept in sync with getFreeBytes(). |
Jeff Sharkey | d5d5e92 | 2017-02-21 10:51:23 -0700 | [diff] [blame] | 3504 | final File path = storage.findPathForUuid(volumeUuid); |
Jeff Sharkey | ddff807 | 2017-05-26 13:10:46 -0600 | [diff] [blame] | 3505 | |
Noah Zimmt | b255807 | 2019-07-25 16:15:06 -0700 | [diff] [blame] | 3506 | long usable = 0; |
| 3507 | long lowReserved = 0; |
| 3508 | long fullReserved = 0; |
| 3509 | long cacheClearable = 0; |
Jeff Sharkey | ddff807 | 2017-05-26 13:10:46 -0600 | [diff] [blame] | 3510 | |
Noah Zimmt | b255807 | 2019-07-25 16:15:06 -0700 | [diff] [blame] | 3511 | if ((flags & StorageManager.FLAG_ALLOCATE_CACHE_ONLY) == 0) { |
| 3512 | usable = path.getUsableSpace(); |
| 3513 | lowReserved = storage.getStorageLowBytes(path); |
| 3514 | fullReserved = storage.getStorageFullBytes(path); |
| 3515 | } |
| 3516 | |
| 3517 | if ((flags & StorageManager.FLAG_ALLOCATE_NON_CACHE_ONLY) == 0 |
| 3518 | && stats.isQuotaSupported(volumeUuid)) { |
Jeff Sharkey | ddff807 | 2017-05-26 13:10:46 -0600 | [diff] [blame] | 3519 | final long cacheTotal = stats.getCacheBytes(volumeUuid); |
Jeff Sharkey | 35e46d2 | 2017-06-09 10:01:20 -0600 | [diff] [blame] | 3520 | final long cacheReserved = storage.getStorageCacheBytes(path, flags); |
Noah Zimmt | b255807 | 2019-07-25 16:15:06 -0700 | [diff] [blame] | 3521 | cacheClearable = Math.max(0, cacheTotal - cacheReserved); |
| 3522 | } |
Jeff Sharkey | ddff807 | 2017-05-26 13:10:46 -0600 | [diff] [blame] | 3523 | |
Noah Zimmt | b255807 | 2019-07-25 16:15:06 -0700 | [diff] [blame] | 3524 | if ((flags & StorageManager.FLAG_ALLOCATE_AGGRESSIVE) != 0) { |
| 3525 | return Math.max(0, (usable + cacheClearable) - fullReserved); |
Jeff Sharkey | d5d5e92 | 2017-02-21 10:51:23 -0700 | [diff] [blame] | 3526 | } else { |
Noah Zimmt | b255807 | 2019-07-25 16:15:06 -0700 | [diff] [blame] | 3527 | return Math.max(0, (usable + cacheClearable) - lowReserved); |
Jeff Sharkey | d5d5e92 | 2017-02-21 10:51:23 -0700 | [diff] [blame] | 3528 | } |
Jeff Sharkey | 789a8fc | 2017-04-16 13:18:35 -0600 | [diff] [blame] | 3529 | } catch (IOException e) { |
| 3530 | throw new ParcelableException(e); |
Jeff Sharkey | d5d5e92 | 2017-02-21 10:51:23 -0700 | [diff] [blame] | 3531 | } finally { |
| 3532 | Binder.restoreCallingIdentity(token); |
| 3533 | } |
Jeff Sharkey | 500ce9e | 2017-02-12 02:39:24 -0700 | [diff] [blame] | 3534 | } |
| 3535 | |
| 3536 | @Override |
Jeff Sharkey | 35e46d2 | 2017-06-09 10:01:20 -0600 | [diff] [blame] | 3537 | public void allocateBytes(String volumeUuid, long bytes, int flags, String callingPackage) { |
| 3538 | flags = adjustAllocateFlags(flags, Binder.getCallingUid(), callingPackage); |
Jeff Sharkey | d5d5e92 | 2017-02-21 10:51:23 -0700 | [diff] [blame] | 3539 | |
Noah Zimmt | b255807 | 2019-07-25 16:15:06 -0700 | [diff] [blame] | 3540 | final long allocatableBytes = getAllocatableBytes(volumeUuid, |
| 3541 | flags | StorageManager.FLAG_ALLOCATE_NON_CACHE_ONLY, callingPackage); |
Jeff Sharkey | d5d5e92 | 2017-02-21 10:51:23 -0700 | [diff] [blame] | 3542 | if (bytes > allocatableBytes) { |
Noah Zimmt | b255807 | 2019-07-25 16:15:06 -0700 | [diff] [blame] | 3543 | // If we don't have room without taking cache into account, check to see if we'd have |
| 3544 | // room if we included freeable cache space. |
| 3545 | final long cacheClearable = getAllocatableBytes(volumeUuid, |
| 3546 | flags | StorageManager.FLAG_ALLOCATE_CACHE_ONLY, callingPackage); |
| 3547 | if (bytes > allocatableBytes + cacheClearable) { |
| 3548 | throw new ParcelableException(new IOException("Failed to allocate " + bytes |
| 3549 | + " because only " + (allocatableBytes + cacheClearable) + " allocatable")); |
| 3550 | } |
Jeff Sharkey | d5d5e92 | 2017-02-21 10:51:23 -0700 | [diff] [blame] | 3551 | } |
| 3552 | |
Jeff Sharkey | 35e46d2 | 2017-06-09 10:01:20 -0600 | [diff] [blame] | 3553 | final StorageManager storage = mContext.getSystemService(StorageManager.class); |
Jeff Sharkey | d5d5e92 | 2017-02-21 10:51:23 -0700 | [diff] [blame] | 3554 | final long token = Binder.clearCallingIdentity(); |
| 3555 | try { |
Jeff Sharkey | 789a8fc | 2017-04-16 13:18:35 -0600 | [diff] [blame] | 3556 | // Free up enough disk space to satisfy both the requested allocation |
| 3557 | // and our low disk warning space. |
| 3558 | final File path = storage.findPathForUuid(volumeUuid); |
Jeff Sharkey | ddff807 | 2017-05-26 13:10:46 -0600 | [diff] [blame] | 3559 | if ((flags & StorageManager.FLAG_ALLOCATE_AGGRESSIVE) != 0) { |
| 3560 | bytes += storage.getStorageFullBytes(path); |
| 3561 | } else { |
| 3562 | bytes += storage.getStorageLowBytes(path); |
| 3563 | } |
Jeff Sharkey | 789a8fc | 2017-04-16 13:18:35 -0600 | [diff] [blame] | 3564 | |
Jeff Sharkey | 5790af0 | 2018-08-13 17:42:54 -0600 | [diff] [blame] | 3565 | mPmInternal.freeStorage(volumeUuid, bytes, flags); |
Jeff Sharkey | d5d5e92 | 2017-02-21 10:51:23 -0700 | [diff] [blame] | 3566 | } catch (IOException e) { |
| 3567 | throw new ParcelableException(e); |
| 3568 | } finally { |
| 3569 | Binder.restoreCallingIdentity(token); |
Jeff Sharkey | 500ce9e | 2017-02-12 02:39:24 -0700 | [diff] [blame] | 3570 | } |
| 3571 | } |
| 3572 | |
Sudheer Shanka | ff971bc | 2018-12-13 17:39:59 -0800 | [diff] [blame] | 3573 | private IAppOpsCallback.Stub mAppOpsCallback = new IAppOpsCallback.Stub() { |
| 3574 | @Override |
| 3575 | public void opChanged(int op, int uid, String packageName) throws RemoteException { |
| 3576 | if (!ENABLE_ISOLATED_STORAGE) return; |
| 3577 | |
Zim | 64726cb | 2019-11-27 13:06:34 +0000 | [diff] [blame] | 3578 | int mountMode = getMountMode(uid, packageName); |
| 3579 | boolean isUidActive = LocalServices.getService(ActivityManagerInternal.class) |
| 3580 | .getUidProcessState(uid) != PROCESS_STATE_NONEXISTENT; |
| 3581 | |
| 3582 | if (isUidActive) { |
| 3583 | remountUidExternalStorage(uid, mountMode); |
| 3584 | } |
Sudheer Shanka | ff971bc | 2018-12-13 17:39:59 -0800 | [diff] [blame] | 3585 | } |
| 3586 | }; |
| 3587 | |
Kenny Root | af9d667 | 2010-10-08 09:21:39 -0700 | [diff] [blame] | 3588 | private void addObbStateLocked(ObbState obbState) throws RemoteException { |
| 3589 | final IBinder binder = obbState.getBinder(); |
| 3590 | List<ObbState> obbStates = mObbMounts.get(binder); |
Kenny Root | 5919ac6 | 2010-10-05 09:49:40 -0700 | [diff] [blame] | 3591 | |
Kenny Root | af9d667 | 2010-10-08 09:21:39 -0700 | [diff] [blame] | 3592 | if (obbStates == null) { |
| 3593 | obbStates = new ArrayList<ObbState>(); |
| 3594 | mObbMounts.put(binder, obbStates); |
| 3595 | } else { |
| 3596 | for (final ObbState o : obbStates) { |
Jeff Sharkey | 4fbbda4 | 2012-09-24 18:34:07 -0700 | [diff] [blame] | 3597 | if (o.rawPath.equals(obbState.rawPath)) { |
Kenny Root | af9d667 | 2010-10-08 09:21:39 -0700 | [diff] [blame] | 3598 | throw new IllegalStateException("Attempt to add ObbState twice. " |
Sudheer Shanka | 2250d56 | 2016-11-07 15:41:02 -0800 | [diff] [blame] | 3599 | + "This indicates an error in the StorageManagerService logic."); |
Kenny Root | 5919ac6 | 2010-10-05 09:49:40 -0700 | [diff] [blame] | 3600 | } |
| 3601 | } |
Kenny Root | a02b8b0 | 2010-08-05 16:14:17 -0700 | [diff] [blame] | 3602 | } |
Kenny Root | af9d667 | 2010-10-08 09:21:39 -0700 | [diff] [blame] | 3603 | |
| 3604 | obbStates.add(obbState); |
| 3605 | try { |
| 3606 | obbState.link(); |
| 3607 | } catch (RemoteException e) { |
| 3608 | /* |
| 3609 | * The binder died before we could link it, so clean up our state |
| 3610 | * and return failure. |
| 3611 | */ |
| 3612 | obbStates.remove(obbState); |
| 3613 | if (obbStates.isEmpty()) { |
| 3614 | mObbMounts.remove(binder); |
| 3615 | } |
| 3616 | |
| 3617 | // Rethrow the error so mountObb can get it |
| 3618 | throw e; |
| 3619 | } |
| 3620 | |
Jeff Sharkey | 4fbbda4 | 2012-09-24 18:34:07 -0700 | [diff] [blame] | 3621 | mObbPathToStateMap.put(obbState.rawPath, obbState); |
Kenny Root | a02b8b0 | 2010-08-05 16:14:17 -0700 | [diff] [blame] | 3622 | } |
| 3623 | |
Kenny Root | af9d667 | 2010-10-08 09:21:39 -0700 | [diff] [blame] | 3624 | private void removeObbStateLocked(ObbState obbState) { |
| 3625 | final IBinder binder = obbState.getBinder(); |
| 3626 | final List<ObbState> obbStates = mObbMounts.get(binder); |
| 3627 | if (obbStates != null) { |
| 3628 | if (obbStates.remove(obbState)) { |
| 3629 | obbState.unlink(); |
Kenny Root | 05105f7 | 2010-09-22 17:29:43 -0700 | [diff] [blame] | 3630 | } |
Kenny Root | af9d667 | 2010-10-08 09:21:39 -0700 | [diff] [blame] | 3631 | if (obbStates.isEmpty()) { |
| 3632 | mObbMounts.remove(binder); |
| 3633 | } |
Kenny Root | a02b8b0 | 2010-08-05 16:14:17 -0700 | [diff] [blame] | 3634 | } |
Kenny Root | a02b8b0 | 2010-08-05 16:14:17 -0700 | [diff] [blame] | 3635 | |
Jeff Sharkey | 4fbbda4 | 2012-09-24 18:34:07 -0700 | [diff] [blame] | 3636 | mObbPathToStateMap.remove(obbState.rawPath); |
Kenny Root | 38cf886 | 2010-09-26 14:18:51 -0700 | [diff] [blame] | 3637 | } |
| 3638 | |
Kenny Root | a02b8b0 | 2010-08-05 16:14:17 -0700 | [diff] [blame] | 3639 | private class ObbActionHandler extends Handler { |
Kenny Root | a02b8b0 | 2010-08-05 16:14:17 -0700 | [diff] [blame] | 3640 | |
| 3641 | ObbActionHandler(Looper l) { |
| 3642 | super(l); |
| 3643 | } |
| 3644 | |
| 3645 | @Override |
| 3646 | public void handleMessage(Message msg) { |
| 3647 | switch (msg.what) { |
| 3648 | case OBB_RUN_ACTION: { |
Kenny Root | 480afe7 | 2010-10-07 10:17:50 -0700 | [diff] [blame] | 3649 | final ObbAction action = (ObbAction) msg.obj; |
Kenny Root | a02b8b0 | 2010-08-05 16:14:17 -0700 | [diff] [blame] | 3650 | |
| 3651 | if (DEBUG_OBB) |
| 3652 | Slog.i(TAG, "OBB_RUN_ACTION: " + action.toString()); |
| 3653 | |
Sudheer Shanka | 25469aa | 2018-08-27 15:50:23 -0700 | [diff] [blame] | 3654 | action.execute(this); |
Kenny Root | a02b8b0 | 2010-08-05 16:14:17 -0700 | [diff] [blame] | 3655 | break; |
| 3656 | } |
Kenny Root | af9d667 | 2010-10-08 09:21:39 -0700 | [diff] [blame] | 3657 | case OBB_FLUSH_MOUNT_STATE: { |
| 3658 | final String path = (String) msg.obj; |
| 3659 | |
| 3660 | if (DEBUG_OBB) |
| 3661 | Slog.i(TAG, "Flushing all OBB state for path " + path); |
| 3662 | |
| 3663 | synchronized (mObbMounts) { |
| 3664 | final List<ObbState> obbStatesToRemove = new LinkedList<ObbState>(); |
| 3665 | |
Jeff Sharkey | 4fbbda4 | 2012-09-24 18:34:07 -0700 | [diff] [blame] | 3666 | final Iterator<ObbState> i = mObbPathToStateMap.values().iterator(); |
Kenny Root | af9d667 | 2010-10-08 09:21:39 -0700 | [diff] [blame] | 3667 | while (i.hasNext()) { |
Jeff Sharkey | 4fbbda4 | 2012-09-24 18:34:07 -0700 | [diff] [blame] | 3668 | final ObbState state = i.next(); |
Kenny Root | af9d667 | 2010-10-08 09:21:39 -0700 | [diff] [blame] | 3669 | |
| 3670 | /* |
| 3671 | * If this entry's source file is in the volume path |
| 3672 | * that got unmounted, remove it because it's no |
| 3673 | * longer valid. |
| 3674 | */ |
Jeff Sharkey | 4fbbda4 | 2012-09-24 18:34:07 -0700 | [diff] [blame] | 3675 | if (state.canonicalPath.startsWith(path)) { |
| 3676 | obbStatesToRemove.add(state); |
Kenny Root | af9d667 | 2010-10-08 09:21:39 -0700 | [diff] [blame] | 3677 | } |
| 3678 | } |
| 3679 | |
| 3680 | for (final ObbState obbState : obbStatesToRemove) { |
| 3681 | if (DEBUG_OBB) |
Jeff Sharkey | 4fbbda4 | 2012-09-24 18:34:07 -0700 | [diff] [blame] | 3682 | Slog.i(TAG, "Removing state for " + obbState.rawPath); |
Kenny Root | af9d667 | 2010-10-08 09:21:39 -0700 | [diff] [blame] | 3683 | |
| 3684 | removeObbStateLocked(obbState); |
| 3685 | |
| 3686 | try { |
Jeff Sharkey | 4fbbda4 | 2012-09-24 18:34:07 -0700 | [diff] [blame] | 3687 | obbState.token.onObbResult(obbState.rawPath, obbState.nonce, |
Kenny Root | af9d667 | 2010-10-08 09:21:39 -0700 | [diff] [blame] | 3688 | OnObbStateChangeListener.UNMOUNTED); |
| 3689 | } catch (RemoteException e) { |
| 3690 | Slog.i(TAG, "Couldn't send unmount notification for OBB: " |
Jeff Sharkey | 4fbbda4 | 2012-09-24 18:34:07 -0700 | [diff] [blame] | 3691 | + obbState.rawPath); |
Kenny Root | af9d667 | 2010-10-08 09:21:39 -0700 | [diff] [blame] | 3692 | } |
| 3693 | } |
| 3694 | } |
| 3695 | break; |
| 3696 | } |
Kenny Root | a02b8b0 | 2010-08-05 16:14:17 -0700 | [diff] [blame] | 3697 | } |
| 3698 | } |
Kenny Root | a02b8b0 | 2010-08-05 16:14:17 -0700 | [diff] [blame] | 3699 | } |
| 3700 | |
Jeff Sharkey | 0095a82 | 2018-02-15 13:06:53 -0700 | [diff] [blame] | 3701 | private static class ObbException extends Exception { |
| 3702 | public final int status; |
| 3703 | |
| 3704 | public ObbException(int status, String message) { |
| 3705 | super(message); |
| 3706 | this.status = status; |
| 3707 | } |
| 3708 | |
| 3709 | public ObbException(int status, Throwable cause) { |
| 3710 | super(cause.getMessage(), cause); |
| 3711 | this.status = status; |
| 3712 | } |
| 3713 | } |
| 3714 | |
Kenny Root | a02b8b0 | 2010-08-05 16:14:17 -0700 | [diff] [blame] | 3715 | abstract class ObbAction { |
Kenny Root | a02b8b0 | 2010-08-05 16:14:17 -0700 | [diff] [blame] | 3716 | |
| 3717 | ObbState mObbState; |
| 3718 | |
| 3719 | ObbAction(ObbState obbState) { |
| 3720 | mObbState = obbState; |
| 3721 | } |
| 3722 | |
| 3723 | public void execute(ObbActionHandler handler) { |
| 3724 | try { |
| 3725 | if (DEBUG_OBB) |
Ben Komalo | 444eca2 | 2011-09-01 15:17:44 -0700 | [diff] [blame] | 3726 | Slog.i(TAG, "Starting to execute action: " + toString()); |
Sudheer Shanka | 25469aa | 2018-08-27 15:50:23 -0700 | [diff] [blame] | 3727 | handleExecute(); |
Jeff Sharkey | 0095a82 | 2018-02-15 13:06:53 -0700 | [diff] [blame] | 3728 | } catch (ObbException e) { |
| 3729 | notifyObbStateChange(e); |
Kenny Root | a02b8b0 | 2010-08-05 16:14:17 -0700 | [diff] [blame] | 3730 | } |
| 3731 | } |
| 3732 | |
Jeff Sharkey | 0095a82 | 2018-02-15 13:06:53 -0700 | [diff] [blame] | 3733 | abstract void handleExecute() throws ObbException; |
Kenny Root | 38cf886 | 2010-09-26 14:18:51 -0700 | [diff] [blame] | 3734 | |
Jeff Sharkey | 0095a82 | 2018-02-15 13:06:53 -0700 | [diff] [blame] | 3735 | protected void notifyObbStateChange(ObbException e) { |
| 3736 | Slog.w(TAG, e); |
| 3737 | notifyObbStateChange(e.status); |
| 3738 | } |
| 3739 | |
| 3740 | protected void notifyObbStateChange(int status) { |
Kenny Root | af9d667 | 2010-10-08 09:21:39 -0700 | [diff] [blame] | 3741 | if (mObbState == null || mObbState.token == null) { |
| 3742 | return; |
| 3743 | } |
| 3744 | |
Kenny Root | 38cf886 | 2010-09-26 14:18:51 -0700 | [diff] [blame] | 3745 | try { |
Jeff Sharkey | 4fbbda4 | 2012-09-24 18:34:07 -0700 | [diff] [blame] | 3746 | mObbState.token.onObbResult(mObbState.rawPath, mObbState.nonce, status); |
Kenny Root | 38cf886 | 2010-09-26 14:18:51 -0700 | [diff] [blame] | 3747 | } catch (RemoteException e) { |
Sudheer Shanka | 2250d56 | 2016-11-07 15:41:02 -0800 | [diff] [blame] | 3748 | Slog.w(TAG, "StorageEventListener went away while calling onObbStateChanged"); |
Kenny Root | 38cf886 | 2010-09-26 14:18:51 -0700 | [diff] [blame] | 3749 | } |
| 3750 | } |
Kenny Root | a02b8b0 | 2010-08-05 16:14:17 -0700 | [diff] [blame] | 3751 | } |
| 3752 | |
| 3753 | class MountObbAction extends ObbAction { |
Ben Komalo | 444eca2 | 2011-09-01 15:17:44 -0700 | [diff] [blame] | 3754 | private final String mKey; |
Jeff Sharkey | 4fbbda4 | 2012-09-24 18:34:07 -0700 | [diff] [blame] | 3755 | private final int mCallingUid; |
Sudheer Shanka | 25469aa | 2018-08-27 15:50:23 -0700 | [diff] [blame] | 3756 | private ObbInfo mObbInfo; |
Kenny Root | a02b8b0 | 2010-08-05 16:14:17 -0700 | [diff] [blame] | 3757 | |
Sudheer Shanka | 25469aa | 2018-08-27 15:50:23 -0700 | [diff] [blame] | 3758 | MountObbAction(ObbState obbState, String key, int callingUid, ObbInfo obbInfo) { |
Kenny Root | a02b8b0 | 2010-08-05 16:14:17 -0700 | [diff] [blame] | 3759 | super(obbState); |
| 3760 | mKey = key; |
Jeff Sharkey | 4fbbda4 | 2012-09-24 18:34:07 -0700 | [diff] [blame] | 3761 | mCallingUid = callingUid; |
Sudheer Shanka | 25469aa | 2018-08-27 15:50:23 -0700 | [diff] [blame] | 3762 | mObbInfo = obbInfo; |
Kenny Root | a02b8b0 | 2010-08-05 16:14:17 -0700 | [diff] [blame] | 3763 | } |
| 3764 | |
Jason parks | 5af0b91 | 2010-11-29 09:05:25 -0600 | [diff] [blame] | 3765 | @Override |
Jeff Sharkey | 0095a82 | 2018-02-15 13:06:53 -0700 | [diff] [blame] | 3766 | public void handleExecute() throws ObbException { |
Kenny Root | af9d667 | 2010-10-08 09:21:39 -0700 | [diff] [blame] | 3767 | warnOnNotMounted(); |
| 3768 | |
Sudheer Shanka | 25469aa | 2018-08-27 15:50:23 -0700 | [diff] [blame] | 3769 | if (!isUidOwnerOfPackageOrSystem(mObbInfo.packageName, mCallingUid)) { |
Jeff Sharkey | 0095a82 | 2018-02-15 13:06:53 -0700 | [diff] [blame] | 3770 | throw new ObbException(ERROR_PERMISSION_DENIED, "Denied attempt to mount OBB " |
Sudheer Shanka | 25469aa | 2018-08-27 15:50:23 -0700 | [diff] [blame] | 3771 | + mObbInfo.filename + " which is owned by " + mObbInfo.packageName); |
Kenny Root | a02b8b0 | 2010-08-05 16:14:17 -0700 | [diff] [blame] | 3772 | } |
| 3773 | |
Kenny Root | af9d667 | 2010-10-08 09:21:39 -0700 | [diff] [blame] | 3774 | final boolean isMounted; |
| 3775 | synchronized (mObbMounts) { |
Jeff Sharkey | 4fbbda4 | 2012-09-24 18:34:07 -0700 | [diff] [blame] | 3776 | isMounted = mObbPathToStateMap.containsKey(mObbState.rawPath); |
Kenny Root | af9d667 | 2010-10-08 09:21:39 -0700 | [diff] [blame] | 3777 | } |
| 3778 | if (isMounted) { |
Jeff Sharkey | 0095a82 | 2018-02-15 13:06:53 -0700 | [diff] [blame] | 3779 | throw new ObbException(ERROR_ALREADY_MOUNTED, |
Sudheer Shanka | 25469aa | 2018-08-27 15:50:23 -0700 | [diff] [blame] | 3780 | "Attempt to mount OBB which is already mounted: " + mObbInfo.filename); |
Kenny Root | af9d667 | 2010-10-08 09:21:39 -0700 | [diff] [blame] | 3781 | } |
| 3782 | |
Kenny Root | af9d667 | 2010-10-08 09:21:39 -0700 | [diff] [blame] | 3783 | final String hashedKey; |
Jeff Sharkey | 41cd681 | 2017-09-11 10:32:17 -0600 | [diff] [blame] | 3784 | final String binderKey; |
Kenny Root | af9d667 | 2010-10-08 09:21:39 -0700 | [diff] [blame] | 3785 | if (mKey == null) { |
| 3786 | hashedKey = "none"; |
Jeff Sharkey | 41cd681 | 2017-09-11 10:32:17 -0600 | [diff] [blame] | 3787 | binderKey = ""; |
Kenny Root | af9d667 | 2010-10-08 09:21:39 -0700 | [diff] [blame] | 3788 | } else { |
Kenny Root | af9d667 | 2010-10-08 09:21:39 -0700 | [diff] [blame] | 3789 | try { |
Kenny Root | 3b1abba | 2010-10-13 15:00:07 -0700 | [diff] [blame] | 3790 | SecretKeyFactory factory = SecretKeyFactory.getInstance("PBKDF2WithHmacSHA1"); |
| 3791 | |
Sudheer Shanka | 25469aa | 2018-08-27 15:50:23 -0700 | [diff] [blame] | 3792 | KeySpec ks = new PBEKeySpec(mKey.toCharArray(), mObbInfo.salt, |
Kenny Root | 3b1abba | 2010-10-13 15:00:07 -0700 | [diff] [blame] | 3793 | PBKDF2_HASH_ROUNDS, CRYPTO_ALGORITHM_KEY_SIZE); |
| 3794 | SecretKey key = factory.generateSecret(ks); |
| 3795 | BigInteger bi = new BigInteger(key.getEncoded()); |
| 3796 | hashedKey = bi.toString(16); |
Jeff Sharkey | 41cd681 | 2017-09-11 10:32:17 -0600 | [diff] [blame] | 3797 | binderKey = hashedKey; |
Jeff Sharkey | 0095a82 | 2018-02-15 13:06:53 -0700 | [diff] [blame] | 3798 | } catch (GeneralSecurityException e) { |
| 3799 | throw new ObbException(ERROR_INTERNAL, e); |
Kenny Root | 38cf886 | 2010-09-26 14:18:51 -0700 | [diff] [blame] | 3800 | } |
Kenny Root | af9d667 | 2010-10-08 09:21:39 -0700 | [diff] [blame] | 3801 | } |
Kenny Root | 38cf886 | 2010-09-26 14:18:51 -0700 | [diff] [blame] | 3802 | |
Kenny Root | af9d667 | 2010-10-08 09:21:39 -0700 | [diff] [blame] | 3803 | try { |
Jeff Sharkey | 5440279 | 2017-09-15 16:05:19 -0600 | [diff] [blame] | 3804 | mObbState.volId = mVold.createObb(mObbState.canonicalPath, binderKey, |
| 3805 | mObbState.ownerGid); |
Zim | 95eca1d | 2019-11-15 18:03:00 +0000 | [diff] [blame] | 3806 | mVold.mount(mObbState.volId, 0, -1, null); |
Kenny Root | a02b8b0 | 2010-08-05 16:14:17 -0700 | [diff] [blame] | 3807 | |
Kenny Root | af9d667 | 2010-10-08 09:21:39 -0700 | [diff] [blame] | 3808 | if (DEBUG_OBB) |
Xiaohui Chen | 621b3fc | 2015-10-02 14:41:42 -0700 | [diff] [blame] | 3809 | Slog.d(TAG, "Successfully mounted OBB " + mObbState.canonicalPath); |
Kenny Root | af9d667 | 2010-10-08 09:21:39 -0700 | [diff] [blame] | 3810 | |
| 3811 | synchronized (mObbMounts) { |
| 3812 | addObbStateLocked(mObbState); |
| 3813 | } |
| 3814 | |
Jeff Sharkey | 0095a82 | 2018-02-15 13:06:53 -0700 | [diff] [blame] | 3815 | notifyObbStateChange(MOUNTED); |
| 3816 | } catch (Exception e) { |
| 3817 | throw new ObbException(ERROR_COULD_NOT_MOUNT, e); |
Kenny Root | 02c8730 | 2010-07-01 08:10:18 -0700 | [diff] [blame] | 3818 | } |
| 3819 | } |
| 3820 | |
Jason parks | 5af0b91 | 2010-11-29 09:05:25 -0600 | [diff] [blame] | 3821 | @Override |
Kenny Root | a02b8b0 | 2010-08-05 16:14:17 -0700 | [diff] [blame] | 3822 | public String toString() { |
| 3823 | StringBuilder sb = new StringBuilder(); |
| 3824 | sb.append("MountObbAction{"); |
Jeff Sharkey | 4fbbda4 | 2012-09-24 18:34:07 -0700 | [diff] [blame] | 3825 | sb.append(mObbState); |
Kenny Root | a02b8b0 | 2010-08-05 16:14:17 -0700 | [diff] [blame] | 3826 | sb.append('}'); |
| 3827 | return sb.toString(); |
| 3828 | } |
| 3829 | } |
| 3830 | |
| 3831 | class UnmountObbAction extends ObbAction { |
Ben Komalo | 444eca2 | 2011-09-01 15:17:44 -0700 | [diff] [blame] | 3832 | private final boolean mForceUnmount; |
Kenny Root | a02b8b0 | 2010-08-05 16:14:17 -0700 | [diff] [blame] | 3833 | |
| 3834 | UnmountObbAction(ObbState obbState, boolean force) { |
| 3835 | super(obbState); |
| 3836 | mForceUnmount = force; |
| 3837 | } |
| 3838 | |
Jason parks | 5af0b91 | 2010-11-29 09:05:25 -0600 | [diff] [blame] | 3839 | @Override |
Jeff Sharkey | 0095a82 | 2018-02-15 13:06:53 -0700 | [diff] [blame] | 3840 | public void handleExecute() throws ObbException { |
Kenny Root | af9d667 | 2010-10-08 09:21:39 -0700 | [diff] [blame] | 3841 | warnOnNotMounted(); |
| 3842 | |
Jeff Sharkey | 4fbbda4 | 2012-09-24 18:34:07 -0700 | [diff] [blame] | 3843 | final ObbState existingState; |
Kenny Root | 38cf886 | 2010-09-26 14:18:51 -0700 | [diff] [blame] | 3844 | synchronized (mObbMounts) { |
Jeff Sharkey | 4fbbda4 | 2012-09-24 18:34:07 -0700 | [diff] [blame] | 3845 | existingState = mObbPathToStateMap.get(mObbState.rawPath); |
Kenny Root | af9d667 | 2010-10-08 09:21:39 -0700 | [diff] [blame] | 3846 | } |
Kenny Root | 38cf886 | 2010-09-26 14:18:51 -0700 | [diff] [blame] | 3847 | |
Jeff Sharkey | 4fbbda4 | 2012-09-24 18:34:07 -0700 | [diff] [blame] | 3848 | if (existingState == null) { |
Jeff Sharkey | 0095a82 | 2018-02-15 13:06:53 -0700 | [diff] [blame] | 3849 | throw new ObbException(ERROR_NOT_MOUNTED, "Missing existingState"); |
Kenny Root | af9d667 | 2010-10-08 09:21:39 -0700 | [diff] [blame] | 3850 | } |
| 3851 | |
Jeff Sharkey | 4fbbda4 | 2012-09-24 18:34:07 -0700 | [diff] [blame] | 3852 | if (existingState.ownerGid != mObbState.ownerGid) { |
Jeff Sharkey | 0095a82 | 2018-02-15 13:06:53 -0700 | [diff] [blame] | 3853 | notifyObbStateChange(new ObbException(ERROR_PERMISSION_DENIED, |
| 3854 | "Permission denied to unmount OBB " + existingState.rawPath |
| 3855 | + " (owned by GID " + existingState.ownerGid + ")")); |
Kenny Root | af9d667 | 2010-10-08 09:21:39 -0700 | [diff] [blame] | 3856 | return; |
| 3857 | } |
| 3858 | |
Kenny Root | af9d667 | 2010-10-08 09:21:39 -0700 | [diff] [blame] | 3859 | try { |
Jeff Sharkey | 5440279 | 2017-09-15 16:05:19 -0600 | [diff] [blame] | 3860 | mVold.unmount(mObbState.volId); |
| 3861 | mVold.destroyObb(mObbState.volId); |
| 3862 | mObbState.volId = null; |
Kenny Root | a02b8b0 | 2010-08-05 16:14:17 -0700 | [diff] [blame] | 3863 | |
Kenny Root | af9d667 | 2010-10-08 09:21:39 -0700 | [diff] [blame] | 3864 | synchronized (mObbMounts) { |
Jeff Sharkey | 4fbbda4 | 2012-09-24 18:34:07 -0700 | [diff] [blame] | 3865 | removeObbStateLocked(existingState); |
Kenny Root | 38cf886 | 2010-09-26 14:18:51 -0700 | [diff] [blame] | 3866 | } |
| 3867 | |
Jeff Sharkey | 0095a82 | 2018-02-15 13:06:53 -0700 | [diff] [blame] | 3868 | notifyObbStateChange(UNMOUNTED); |
| 3869 | } catch (Exception e) { |
| 3870 | throw new ObbException(ERROR_COULD_NOT_UNMOUNT, e); |
Kenny Root | a02b8b0 | 2010-08-05 16:14:17 -0700 | [diff] [blame] | 3871 | } |
| 3872 | } |
| 3873 | |
Jason parks | 5af0b91 | 2010-11-29 09:05:25 -0600 | [diff] [blame] | 3874 | @Override |
Kenny Root | a02b8b0 | 2010-08-05 16:14:17 -0700 | [diff] [blame] | 3875 | public String toString() { |
| 3876 | StringBuilder sb = new StringBuilder(); |
| 3877 | sb.append("UnmountObbAction{"); |
Jeff Sharkey | 4fbbda4 | 2012-09-24 18:34:07 -0700 | [diff] [blame] | 3878 | sb.append(mObbState); |
Kenny Root | a02b8b0 | 2010-08-05 16:14:17 -0700 | [diff] [blame] | 3879 | sb.append(",force="); |
| 3880 | sb.append(mForceUnmount); |
Kenny Root | a02b8b0 | 2010-08-05 16:14:17 -0700 | [diff] [blame] | 3881 | sb.append('}'); |
| 3882 | return sb.toString(); |
| 3883 | } |
Kenny Root | 02c8730 | 2010-07-01 08:10:18 -0700 | [diff] [blame] | 3884 | } |
Kenny Root | 38cf886 | 2010-09-26 14:18:51 -0700 | [diff] [blame] | 3885 | |
Jeff Sharkey | 7e19f53 | 2017-11-06 13:54:11 -0700 | [diff] [blame] | 3886 | private void dispatchOnStatus(IVoldTaskListener listener, int status, |
| 3887 | PersistableBundle extras) { |
| 3888 | if (listener != null) { |
| 3889 | try { |
| 3890 | listener.onStatus(status, extras); |
| 3891 | } catch (RemoteException ignored) { |
| 3892 | } |
| 3893 | } |
| 3894 | } |
| 3895 | |
| 3896 | private void dispatchOnFinished(IVoldTaskListener listener, int status, |
| 3897 | PersistableBundle extras) { |
| 3898 | if (listener != null) { |
| 3899 | try { |
| 3900 | listener.onFinished(status, extras); |
| 3901 | } catch (RemoteException ignored) { |
| 3902 | } |
| 3903 | } |
| 3904 | } |
| 3905 | |
Sudheer Shanka | 3a0df3b | 2018-12-12 12:43:43 -0800 | [diff] [blame] | 3906 | private int getMountMode(int uid, String packageName) { |
Jeff Sharkey | 6fd6994 | 2019-03-26 17:53:35 -0600 | [diff] [blame] | 3907 | final int mode = getMountModeInternal(uid, packageName); |
| 3908 | if (LOCAL_LOGV) { |
| 3909 | Slog.v(TAG, "Resolved mode " + mode + " for " + packageName + "/" |
| 3910 | + UserHandle.formatUid(uid)); |
| 3911 | } |
| 3912 | return mode; |
| 3913 | } |
| 3914 | |
| 3915 | private int getMountModeInternal(int uid, String packageName) { |
Sudheer Shanka | 3a0df3b | 2018-12-12 12:43:43 -0800 | [diff] [blame] | 3916 | try { |
Jeff Sharkey | 6fd6994 | 2019-03-26 17:53:35 -0600 | [diff] [blame] | 3917 | // Get some easy cases out of the way first |
Sudheer Shanka | 3a0df3b | 2018-12-12 12:43:43 -0800 | [diff] [blame] | 3918 | if (Process.isIsolated(uid)) { |
| 3919 | return Zygote.MOUNT_EXTERNAL_NONE; |
| 3920 | } |
Sudheer Shanka | b161398 | 2019-05-16 16:55:50 -0700 | [diff] [blame] | 3921 | |
| 3922 | final String[] packagesForUid = mIPackageManager.getPackagesForUid(uid); |
Sudheer Shanka | 1df72db | 2019-05-24 10:59:52 -0700 | [diff] [blame] | 3923 | if (ArrayUtils.isEmpty(packagesForUid)) { |
| 3924 | // It's possible the package got uninstalled already, so just ignore. |
| 3925 | return Zygote.MOUNT_EXTERNAL_NONE; |
| 3926 | } |
Sudheer Shanka | b161398 | 2019-05-16 16:55:50 -0700 | [diff] [blame] | 3927 | if (packageName == null) { |
| 3928 | packageName = packagesForUid[0]; |
| 3929 | } |
| 3930 | |
Jeff Sharkey | 6fd6994 | 2019-03-26 17:53:35 -0600 | [diff] [blame] | 3931 | if (mPmInternal.isInstantApp(packageName, UserHandle.getUserId(uid))) { |
Winson Chiu | 5118d49 | 2019-05-15 18:24:06 +0000 | [diff] [blame] | 3932 | return Zygote.MOUNT_EXTERNAL_NONE; |
Jeff Sharkey | 6fd6994 | 2019-03-26 17:53:35 -0600 | [diff] [blame] | 3933 | } |
| 3934 | |
Martijn Coenen | 44db1ac | 2019-12-03 16:06:19 +0100 | [diff] [blame] | 3935 | if (mIsFuseEnabled && mMediaStoreAuthorityAppId == UserHandle.getAppId(uid)) { |
| 3936 | // Determine if caller requires pass_through mount; note that we do this for |
| 3937 | // all processes that share a UID with MediaProvider; but this is fine, since |
| 3938 | // those processes anyway share the same rights as MediaProvider. |
Zim | 74a9bba | 2019-09-03 20:49:13 +0100 | [diff] [blame] | 3939 | return Zygote.MOUNT_EXTERNAL_PASS_THROUGH; |
| 3940 | } |
| 3941 | |
Martijn Coenen | 496ac00 | 2020-01-08 14:55:53 +0100 | [diff] [blame] | 3942 | if (mIsFuseEnabled && mDownloadsAuthorityAppId == UserHandle.getAppId(uid)) { |
| 3943 | // DownloadManager can write in app-private directories on behalf of apps; |
| 3944 | // give it write access to Android/ |
| 3945 | return Zygote.MOUNT_EXTERNAL_ANDROID_WRITABLE; |
| 3946 | } |
| 3947 | |
| 3948 | final boolean hasMtp = mIPackageManager.checkUidPermission(ACCESS_MTP, uid) == |
| 3949 | PERMISSION_GRANTED; |
| 3950 | if (mIsFuseEnabled && hasMtp) { |
| 3951 | // The process hosting the MTP server should be able to write in Android/ |
| 3952 | return Zygote.MOUNT_EXTERNAL_ANDROID_WRITABLE; |
| 3953 | } |
| 3954 | |
Jeff Sharkey | 6fd6994 | 2019-03-26 17:53:35 -0600 | [diff] [blame] | 3955 | // Determine if caller is holding runtime permission |
Chad Brubaker | 45810af | 2019-04-08 19:19:48 -0700 | [diff] [blame] | 3956 | final boolean hasRead = StorageManager.checkPermissionAndCheckOp(mContext, false, 0, |
Jeff Sharkey | 6fd6994 | 2019-03-26 17:53:35 -0600 | [diff] [blame] | 3957 | uid, packageName, READ_EXTERNAL_STORAGE, OP_READ_EXTERNAL_STORAGE); |
Chad Brubaker | 45810af | 2019-04-08 19:19:48 -0700 | [diff] [blame] | 3958 | final boolean hasWrite = StorageManager.checkPermissionAndCheckOp(mContext, false, 0, |
Jeff Sharkey | 6fd6994 | 2019-03-26 17:53:35 -0600 | [diff] [blame] | 3959 | uid, packageName, WRITE_EXTERNAL_STORAGE, OP_WRITE_EXTERNAL_STORAGE); |
Jeff Sharkey | 6fd6994 | 2019-03-26 17:53:35 -0600 | [diff] [blame] | 3960 | |
| 3961 | // We're only willing to give out broad access if they also hold |
| 3962 | // runtime permission; this is a firm CDD requirement |
| 3963 | final boolean hasFull = mIPackageManager.checkUidPermission(WRITE_MEDIA_STORAGE, |
| 3964 | uid) == PERMISSION_GRANTED; |
Jeff Sharkey | 26874a2 | 2019-04-17 21:30:47 -0600 | [diff] [blame] | 3965 | if (hasFull && hasWrite) { |
Jeff Sharkey | 6fd6994 | 2019-03-26 17:53:35 -0600 | [diff] [blame] | 3966 | return Zygote.MOUNT_EXTERNAL_FULL; |
| 3967 | } |
| 3968 | |
| 3969 | // We're only willing to give out installer access if they also hold |
| 3970 | // runtime permission; this is a firm CDD requirement |
| 3971 | final boolean hasInstall = mIPackageManager.checkUidPermission(INSTALL_PACKAGES, |
| 3972 | uid) == PERMISSION_GRANTED; |
Sudheer Shanka | b161398 | 2019-05-16 16:55:50 -0700 | [diff] [blame] | 3973 | boolean hasInstallOp = false; |
| 3974 | // OP_REQUEST_INSTALL_PACKAGES is granted/denied per package but vold can't |
| 3975 | // update mountpoints of a specific package. So, check the appop for all packages |
| 3976 | // sharing the uid and allow same level of storage access for all packages even if |
| 3977 | // one of the packages has the appop granted. |
| 3978 | for (String uidPackageName : packagesForUid) { |
| 3979 | if (mIAppOpsService.checkOperation( |
| 3980 | OP_REQUEST_INSTALL_PACKAGES, uid, uidPackageName) == MODE_ALLOWED) { |
| 3981 | hasInstallOp = true; |
| 3982 | break; |
| 3983 | } |
| 3984 | } |
Jeff Sharkey | 26874a2 | 2019-04-17 21:30:47 -0600 | [diff] [blame] | 3985 | if ((hasInstall || hasInstallOp) && hasWrite) { |
Martijn Coenen | fc9c3b6 | 2019-12-13 16:36:27 +0100 | [diff] [blame] | 3986 | return Zygote.MOUNT_EXTERNAL_INSTALLER; |
Jeff Sharkey | 6fd6994 | 2019-03-26 17:53:35 -0600 | [diff] [blame] | 3987 | } |
| 3988 | |
| 3989 | // Otherwise we're willing to give out sandboxed or non-sandboxed if |
| 3990 | // they hold the runtime permission |
Nandana Dutt | dc15bb4 | 2020-01-03 16:43:46 +0000 | [diff] [blame] | 3991 | boolean hasLegacy = mIAppOpsService.checkOperation(OP_LEGACY_STORAGE, |
Jeff Sharkey | 6fd6994 | 2019-03-26 17:53:35 -0600 | [diff] [blame] | 3992 | uid, packageName) == MODE_ALLOWED; |
Nandana Dutt | dc15bb4 | 2020-01-03 16:43:46 +0000 | [diff] [blame] | 3993 | |
| 3994 | // Hack(b/147137425): we have to honor hasRequestedLegacyExternalStorage for a short |
| 3995 | // while to enable 2 cases. |
| 3996 | // 1) Apps that want to be in scoped storage in R, but want to opt out in Q devices, |
| 3997 | // because they want to use raw file paths, would fail until fuse is enabled by default. |
| 3998 | // 2) Test apps that target current sdk will fail. They would fail even after fuse is |
| 3999 | // enabled, but we are fixing it with b/142395442. We are not planning to enable |
| 4000 | // fuse by default until b/142395442 is fixed. |
| 4001 | if (!hasLegacy && !mIsFuseEnabled) { |
| 4002 | ApplicationInfo ai = mIPackageManager.getApplicationInfo(packageName, |
| 4003 | 0, UserHandle.getUserId(uid)); |
| 4004 | hasLegacy = ai.hasRequestedLegacyExternalStorage(); |
| 4005 | } |
| 4006 | |
Jeff Sharkey | 26874a2 | 2019-04-17 21:30:47 -0600 | [diff] [blame] | 4007 | if (hasLegacy && hasWrite) { |
| 4008 | return Zygote.MOUNT_EXTERNAL_WRITE; |
| 4009 | } else if (hasLegacy && hasRead) { |
| 4010 | return Zygote.MOUNT_EXTERNAL_READ; |
Sudheer Shanka | 3a0df3b | 2018-12-12 12:43:43 -0800 | [diff] [blame] | 4011 | } else { |
Sudheer Shanka | 783c90e | 2019-04-12 13:55:20 -0700 | [diff] [blame] | 4012 | return Zygote.MOUNT_EXTERNAL_DEFAULT; |
Sudheer Shanka | 3a0df3b | 2018-12-12 12:43:43 -0800 | [diff] [blame] | 4013 | } |
| 4014 | } catch (RemoteException e) { |
| 4015 | // Should not happen |
| 4016 | } |
| 4017 | return Zygote.MOUNT_EXTERNAL_NONE; |
| 4018 | } |
| 4019 | |
Jeff Sharkey | 7151a9a | 2015-04-04 15:22:37 -0700 | [diff] [blame] | 4020 | private static class Callbacks extends Handler { |
| 4021 | private static final int MSG_STORAGE_STATE_CHANGED = 1; |
| 4022 | private static final int MSG_VOLUME_STATE_CHANGED = 2; |
Jeff Sharkey | 50a0545 | 2015-04-29 11:24:52 -0700 | [diff] [blame] | 4023 | private static final int MSG_VOLUME_RECORD_CHANGED = 3; |
| 4024 | private static final int MSG_VOLUME_FORGOTTEN = 4; |
| 4025 | private static final int MSG_DISK_SCANNED = 5; |
Makoto Onuki | 9dc575d | 2015-06-12 16:10:25 -0700 | [diff] [blame] | 4026 | private static final int MSG_DISK_DESTROYED = 6; |
Jeff Sharkey | 7151a9a | 2015-04-04 15:22:37 -0700 | [diff] [blame] | 4027 | |
Sudheer Shanka | 2250d56 | 2016-11-07 15:41:02 -0800 | [diff] [blame] | 4028 | private final RemoteCallbackList<IStorageEventListener> |
Jeff Sharkey | 7151a9a | 2015-04-04 15:22:37 -0700 | [diff] [blame] | 4029 | mCallbacks = new RemoteCallbackList<>(); |
| 4030 | |
| 4031 | public Callbacks(Looper looper) { |
| 4032 | super(looper); |
| 4033 | } |
| 4034 | |
Sudheer Shanka | 2250d56 | 2016-11-07 15:41:02 -0800 | [diff] [blame] | 4035 | public void register(IStorageEventListener callback) { |
Jeff Sharkey | 7151a9a | 2015-04-04 15:22:37 -0700 | [diff] [blame] | 4036 | mCallbacks.register(callback); |
| 4037 | } |
| 4038 | |
Sudheer Shanka | 2250d56 | 2016-11-07 15:41:02 -0800 | [diff] [blame] | 4039 | public void unregister(IStorageEventListener callback) { |
Jeff Sharkey | 7151a9a | 2015-04-04 15:22:37 -0700 | [diff] [blame] | 4040 | mCallbacks.unregister(callback); |
| 4041 | } |
| 4042 | |
| 4043 | @Override |
| 4044 | public void handleMessage(Message msg) { |
| 4045 | final SomeArgs args = (SomeArgs) msg.obj; |
| 4046 | final int n = mCallbacks.beginBroadcast(); |
| 4047 | for (int i = 0; i < n; i++) { |
Sudheer Shanka | 2250d56 | 2016-11-07 15:41:02 -0800 | [diff] [blame] | 4048 | final IStorageEventListener callback = mCallbacks.getBroadcastItem(i); |
Jeff Sharkey | 7151a9a | 2015-04-04 15:22:37 -0700 | [diff] [blame] | 4049 | try { |
| 4050 | invokeCallback(callback, msg.what, args); |
| 4051 | } catch (RemoteException ignored) { |
| 4052 | } |
| 4053 | } |
| 4054 | mCallbacks.finishBroadcast(); |
| 4055 | args.recycle(); |
| 4056 | } |
| 4057 | |
Sudheer Shanka | 2250d56 | 2016-11-07 15:41:02 -0800 | [diff] [blame] | 4058 | private void invokeCallback(IStorageEventListener callback, int what, SomeArgs args) |
Jeff Sharkey | 7151a9a | 2015-04-04 15:22:37 -0700 | [diff] [blame] | 4059 | throws RemoteException { |
| 4060 | switch (what) { |
| 4061 | case MSG_STORAGE_STATE_CHANGED: { |
| 4062 | callback.onStorageStateChanged((String) args.arg1, (String) args.arg2, |
| 4063 | (String) args.arg3); |
| 4064 | break; |
| 4065 | } |
| 4066 | case MSG_VOLUME_STATE_CHANGED: { |
| 4067 | callback.onVolumeStateChanged((VolumeInfo) args.arg1, args.argi2, args.argi3); |
| 4068 | break; |
| 4069 | } |
Jeff Sharkey | 50a0545 | 2015-04-29 11:24:52 -0700 | [diff] [blame] | 4070 | case MSG_VOLUME_RECORD_CHANGED: { |
| 4071 | callback.onVolumeRecordChanged((VolumeRecord) args.arg1); |
| 4072 | break; |
| 4073 | } |
| 4074 | case MSG_VOLUME_FORGOTTEN: { |
| 4075 | callback.onVolumeForgotten((String) args.arg1); |
Jeff Sharkey | d95d3bf | 2015-04-14 21:39:44 -0700 | [diff] [blame] | 4076 | break; |
| 4077 | } |
Jeff Sharkey | 620b32b | 2015-04-23 19:36:02 -0700 | [diff] [blame] | 4078 | case MSG_DISK_SCANNED: { |
| 4079 | callback.onDiskScanned((DiskInfo) args.arg1, args.argi2); |
Jeff Sharkey | 7e92ef3 | 2015-04-17 17:35:07 -0700 | [diff] [blame] | 4080 | break; |
| 4081 | } |
Makoto Onuki | 9dc575d | 2015-06-12 16:10:25 -0700 | [diff] [blame] | 4082 | case MSG_DISK_DESTROYED: { |
| 4083 | callback.onDiskDestroyed((DiskInfo) args.arg1); |
| 4084 | break; |
| 4085 | } |
Jeff Sharkey | 7151a9a | 2015-04-04 15:22:37 -0700 | [diff] [blame] | 4086 | } |
| 4087 | } |
| 4088 | |
| 4089 | private void notifyStorageStateChanged(String path, String oldState, String newState) { |
| 4090 | final SomeArgs args = SomeArgs.obtain(); |
| 4091 | args.arg1 = path; |
| 4092 | args.arg2 = oldState; |
| 4093 | args.arg3 = newState; |
| 4094 | obtainMessage(MSG_STORAGE_STATE_CHANGED, args).sendToTarget(); |
| 4095 | } |
| 4096 | |
| 4097 | private void notifyVolumeStateChanged(VolumeInfo vol, int oldState, int newState) { |
| 4098 | final SomeArgs args = SomeArgs.obtain(); |
Jeff Sharkey | b36586a | 2015-04-27 08:42:28 -0700 | [diff] [blame] | 4099 | args.arg1 = vol.clone(); |
Jeff Sharkey | 7151a9a | 2015-04-04 15:22:37 -0700 | [diff] [blame] | 4100 | args.argi2 = oldState; |
| 4101 | args.argi3 = newState; |
| 4102 | obtainMessage(MSG_VOLUME_STATE_CHANGED, args).sendToTarget(); |
| 4103 | } |
Jeff Sharkey | d95d3bf | 2015-04-14 21:39:44 -0700 | [diff] [blame] | 4104 | |
Jeff Sharkey | 50a0545 | 2015-04-29 11:24:52 -0700 | [diff] [blame] | 4105 | private void notifyVolumeRecordChanged(VolumeRecord rec) { |
| 4106 | final SomeArgs args = SomeArgs.obtain(); |
| 4107 | args.arg1 = rec.clone(); |
| 4108 | obtainMessage(MSG_VOLUME_RECORD_CHANGED, args).sendToTarget(); |
| 4109 | } |
| 4110 | |
| 4111 | private void notifyVolumeForgotten(String fsUuid) { |
Jeff Sharkey | d95d3bf | 2015-04-14 21:39:44 -0700 | [diff] [blame] | 4112 | final SomeArgs args = SomeArgs.obtain(); |
Jeff Sharkey | b36586a | 2015-04-27 08:42:28 -0700 | [diff] [blame] | 4113 | args.arg1 = fsUuid; |
Jeff Sharkey | 50a0545 | 2015-04-29 11:24:52 -0700 | [diff] [blame] | 4114 | obtainMessage(MSG_VOLUME_FORGOTTEN, args).sendToTarget(); |
Jeff Sharkey | d95d3bf | 2015-04-14 21:39:44 -0700 | [diff] [blame] | 4115 | } |
Jeff Sharkey | 7e92ef3 | 2015-04-17 17:35:07 -0700 | [diff] [blame] | 4116 | |
Jeff Sharkey | 620b32b | 2015-04-23 19:36:02 -0700 | [diff] [blame] | 4117 | private void notifyDiskScanned(DiskInfo disk, int volumeCount) { |
Jeff Sharkey | 7e92ef3 | 2015-04-17 17:35:07 -0700 | [diff] [blame] | 4118 | final SomeArgs args = SomeArgs.obtain(); |
Jeff Sharkey | b36586a | 2015-04-27 08:42:28 -0700 | [diff] [blame] | 4119 | args.arg1 = disk.clone(); |
Jeff Sharkey | 620b32b | 2015-04-23 19:36:02 -0700 | [diff] [blame] | 4120 | args.argi2 = volumeCount; |
| 4121 | obtainMessage(MSG_DISK_SCANNED, args).sendToTarget(); |
Jeff Sharkey | 7e92ef3 | 2015-04-17 17:35:07 -0700 | [diff] [blame] | 4122 | } |
Makoto Onuki | 9dc575d | 2015-06-12 16:10:25 -0700 | [diff] [blame] | 4123 | |
| 4124 | private void notifyDiskDestroyed(DiskInfo disk) { |
| 4125 | final SomeArgs args = SomeArgs.obtain(); |
| 4126 | args.arg1 = disk.clone(); |
| 4127 | obtainMessage(MSG_DISK_DESTROYED, args).sendToTarget(); |
| 4128 | } |
Jeff Sharkey | 7151a9a | 2015-04-04 15:22:37 -0700 | [diff] [blame] | 4129 | } |
| 4130 | |
Kenny Root | 38cf886 | 2010-09-26 14:18:51 -0700 | [diff] [blame] | 4131 | @Override |
Jeff Sharkey | 5aca2b8 | 2013-10-16 16:21:54 -0700 | [diff] [blame] | 4132 | protected void dump(FileDescriptor fd, PrintWriter writer, String[] args) { |
Jeff Sharkey | fe9a53b | 2017-03-31 14:08:23 -0600 | [diff] [blame] | 4133 | if (!DumpUtils.checkDumpPermission(mContext, TAG, writer)) return; |
Jeff Sharkey | 5aca2b8 | 2013-10-16 16:21:54 -0700 | [diff] [blame] | 4134 | |
| 4135 | final IndentingPrintWriter pw = new IndentingPrintWriter(writer, " ", 160); |
Jeff Sharkey | 27de30d | 2015-04-18 16:20:27 -0700 | [diff] [blame] | 4136 | synchronized (mLock) { |
| 4137 | pw.println("Disks:"); |
| 4138 | pw.increaseIndent(); |
| 4139 | for (int i = 0; i < mDisks.size(); i++) { |
| 4140 | final DiskInfo disk = mDisks.valueAt(i); |
| 4141 | disk.dump(pw); |
| 4142 | } |
| 4143 | pw.decreaseIndent(); |
| 4144 | |
| 4145 | pw.println(); |
| 4146 | pw.println("Volumes:"); |
| 4147 | pw.increaseIndent(); |
| 4148 | for (int i = 0; i < mVolumes.size(); i++) { |
| 4149 | final VolumeInfo vol = mVolumes.valueAt(i); |
| 4150 | if (VolumeInfo.ID_PRIVATE_INTERNAL.equals(vol.id)) continue; |
| 4151 | vol.dump(pw); |
| 4152 | } |
| 4153 | pw.decreaseIndent(); |
| 4154 | |
| 4155 | pw.println(); |
Jeff Sharkey | b36586a | 2015-04-27 08:42:28 -0700 | [diff] [blame] | 4156 | pw.println("Records:"); |
Jeff Sharkey | 27de30d | 2015-04-18 16:20:27 -0700 | [diff] [blame] | 4157 | pw.increaseIndent(); |
Jeff Sharkey | b36586a | 2015-04-27 08:42:28 -0700 | [diff] [blame] | 4158 | for (int i = 0; i < mRecords.size(); i++) { |
| 4159 | final VolumeRecord note = mRecords.valueAt(i); |
| 4160 | note.dump(pw); |
Jeff Sharkey | 27de30d | 2015-04-18 16:20:27 -0700 | [diff] [blame] | 4161 | } |
| 4162 | pw.decreaseIndent(); |
Jeff Sharkey | 275e3e4 | 2015-04-24 16:10:32 -0700 | [diff] [blame] | 4163 | |
| 4164 | pw.println(); |
| 4165 | pw.println("Primary storage UUID: " + mPrimaryStorageUuid); |
Jeff Sharkey | b0c363b2 | 2018-12-15 11:53:03 -0700 | [diff] [blame] | 4166 | |
| 4167 | pw.println(); |
Felipe Leme | 281389a | 2016-10-10 17:12:20 -0700 | [diff] [blame] | 4168 | final Pair<String, Long> pair = StorageManager.getPrimaryStoragePathAndSize(); |
| 4169 | if (pair == null) { |
| 4170 | pw.println("Internal storage total size: N/A"); |
| 4171 | } else { |
| 4172 | pw.print("Internal storage ("); |
| 4173 | pw.print(pair.first); |
| 4174 | pw.print(") total size: "); |
| 4175 | pw.print(pair.second); |
| 4176 | pw.print(" ("); |
Jeff Sharkey | 9f2dc05 | 2018-01-07 16:47:31 -0700 | [diff] [blame] | 4177 | pw.print(DataUnit.MEBIBYTES.toBytes(pair.second)); |
| 4178 | pw.println(" MiB)"); |
Felipe Leme | 281389a | 2016-10-10 17:12:20 -0700 | [diff] [blame] | 4179 | } |
Jeff Sharkey | b0c363b2 | 2018-12-15 11:53:03 -0700 | [diff] [blame] | 4180 | |
| 4181 | pw.println(); |
Jeff Sharkey | ce14cd0 | 2015-12-07 15:35:42 -0700 | [diff] [blame] | 4182 | pw.println("Local unlocked users: " + Arrays.toString(mLocalUnlockedUsers)); |
| 4183 | pw.println("System unlocked users: " + Arrays.toString(mSystemUnlockedUsers)); |
Jeff Sharkey | b0c363b2 | 2018-12-15 11:53:03 -0700 | [diff] [blame] | 4184 | |
| 4185 | final ContentResolver cr = mContext.getContentResolver(); |
| 4186 | pw.println(); |
| 4187 | pw.println("Isolated storage, local feature flag: " |
| 4188 | + Settings.Global.getInt(cr, Settings.Global.ISOLATED_STORAGE_LOCAL, 0)); |
| 4189 | pw.println("Isolated storage, remote feature flag: " |
| 4190 | + Settings.Global.getInt(cr, Settings.Global.ISOLATED_STORAGE_REMOTE, 0)); |
| 4191 | pw.println("Isolated storage, resolved: " + StorageManager.hasIsolatedStorage()); |
Jeff Sharkey | 27de30d | 2015-04-18 16:20:27 -0700 | [diff] [blame] | 4192 | } |
Kenny Root | 38cf886 | 2010-09-26 14:18:51 -0700 | [diff] [blame] | 4193 | |
Kenny Root | 38cf886 | 2010-09-26 14:18:51 -0700 | [diff] [blame] | 4194 | synchronized (mObbMounts) { |
Jeff Sharkey | 27de30d | 2015-04-18 16:20:27 -0700 | [diff] [blame] | 4195 | pw.println(); |
Jeff Sharkey | 5aca2b8 | 2013-10-16 16:21:54 -0700 | [diff] [blame] | 4196 | pw.println("mObbMounts:"); |
| 4197 | pw.increaseIndent(); |
| 4198 | final Iterator<Entry<IBinder, List<ObbState>>> binders = mObbMounts.entrySet() |
| 4199 | .iterator(); |
Kenny Root | af9d667 | 2010-10-08 09:21:39 -0700 | [diff] [blame] | 4200 | while (binders.hasNext()) { |
| 4201 | Entry<IBinder, List<ObbState>> e = binders.next(); |
Jeff Sharkey | 5aca2b8 | 2013-10-16 16:21:54 -0700 | [diff] [blame] | 4202 | pw.println(e.getKey() + ":"); |
| 4203 | pw.increaseIndent(); |
Kenny Root | af9d667 | 2010-10-08 09:21:39 -0700 | [diff] [blame] | 4204 | final List<ObbState> obbStates = e.getValue(); |
Kenny Root | 38cf886 | 2010-09-26 14:18:51 -0700 | [diff] [blame] | 4205 | for (final ObbState obbState : obbStates) { |
Jeff Sharkey | 5aca2b8 | 2013-10-16 16:21:54 -0700 | [diff] [blame] | 4206 | pw.println(obbState); |
Kenny Root | 38cf886 | 2010-09-26 14:18:51 -0700 | [diff] [blame] | 4207 | } |
Jeff Sharkey | 5aca2b8 | 2013-10-16 16:21:54 -0700 | [diff] [blame] | 4208 | pw.decreaseIndent(); |
Kenny Root | 38cf886 | 2010-09-26 14:18:51 -0700 | [diff] [blame] | 4209 | } |
Jeff Sharkey | 5aca2b8 | 2013-10-16 16:21:54 -0700 | [diff] [blame] | 4210 | pw.decreaseIndent(); |
Kenny Root | af9d667 | 2010-10-08 09:21:39 -0700 | [diff] [blame] | 4211 | |
Jeff Sharkey | 5aca2b8 | 2013-10-16 16:21:54 -0700 | [diff] [blame] | 4212 | pw.println(); |
| 4213 | pw.println("mObbPathToStateMap:"); |
| 4214 | pw.increaseIndent(); |
Kenny Root | af9d667 | 2010-10-08 09:21:39 -0700 | [diff] [blame] | 4215 | final Iterator<Entry<String, ObbState>> maps = mObbPathToStateMap.entrySet().iterator(); |
| 4216 | while (maps.hasNext()) { |
| 4217 | final Entry<String, ObbState> e = maps.next(); |
Jeff Sharkey | 5aca2b8 | 2013-10-16 16:21:54 -0700 | [diff] [blame] | 4218 | pw.print(e.getKey()); |
| 4219 | pw.print(" -> "); |
| 4220 | pw.println(e.getValue()); |
Kenny Root | af9d667 | 2010-10-08 09:21:39 -0700 | [diff] [blame] | 4221 | } |
Jeff Sharkey | 5aca2b8 | 2013-10-16 16:21:54 -0700 | [diff] [blame] | 4222 | pw.decreaseIndent(); |
Kenny Root | 38cf886 | 2010-09-26 14:18:51 -0700 | [diff] [blame] | 4223 | } |
Kenny Root | 4161f9b | 2011-07-13 09:48:33 -0700 | [diff] [blame] | 4224 | |
Robert Greenwalt | 470fd72 | 2012-01-18 12:51:15 -0800 | [diff] [blame] | 4225 | pw.println(); |
Christopher Tate | 7265abe | 2014-11-21 13:54:45 -0800 | [diff] [blame] | 4226 | pw.print("Last maintenance: "); |
Jeff Sharkey | e8a4b66 | 2015-06-27 15:43:45 -0700 | [diff] [blame] | 4227 | pw.println(TimeUtils.formatForLogging(mLastMaintenance)); |
Kenny Root | 38cf886 | 2010-09-26 14:18:51 -0700 | [diff] [blame] | 4228 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4229 | |
Jeff Sharkey | fa23c5a | 2011-08-09 21:44:24 -0700 | [diff] [blame] | 4230 | /** {@inheritDoc} */ |
Jeff Sharkey | 4887789 | 2015-03-18 11:27:19 -0700 | [diff] [blame] | 4231 | @Override |
Jeff Sharkey | fa23c5a | 2011-08-09 21:44:24 -0700 | [diff] [blame] | 4232 | public void monitor() { |
Jeff Sharkey | 5440279 | 2017-09-15 16:05:19 -0600 | [diff] [blame] | 4233 | try { |
| 4234 | mVold.monitor(); |
| 4235 | } catch (Exception e) { |
| 4236 | Slog.wtf(TAG, e); |
Paul Lawrence | 1c62cbb | 2015-06-03 14:14:52 -0700 | [diff] [blame] | 4237 | } |
Jeff Sharkey | fa23c5a | 2011-08-09 21:44:24 -0700 | [diff] [blame] | 4238 | } |
Svet Ganov | 6ee871e | 2015-07-10 14:29:33 -0700 | [diff] [blame] | 4239 | |
Sudheer Shanka | 2250d56 | 2016-11-07 15:41:02 -0800 | [diff] [blame] | 4240 | private final class StorageManagerInternalImpl extends StorageManagerInternal { |
Svet Ganov | 6ee871e | 2015-07-10 14:29:33 -0700 | [diff] [blame] | 4241 | // Not guarded by a lock. |
| 4242 | private final CopyOnWriteArrayList<ExternalStorageMountPolicy> mPolicies = |
| 4243 | new CopyOnWriteArrayList<>(); |
| 4244 | |
Risan | aec0ee7 | 2018-10-31 10:10:12 +0900 | [diff] [blame] | 4245 | @GuardedBy("mResetListeners") |
| 4246 | private final List<StorageManagerInternal.ResetListener> mResetListeners = |
| 4247 | new ArrayList<>(); |
| 4248 | |
Svet Ganov | 6ee871e | 2015-07-10 14:29:33 -0700 | [diff] [blame] | 4249 | @Override |
| 4250 | public void addExternalStoragePolicy(ExternalStorageMountPolicy policy) { |
| 4251 | // No locking - CopyOnWriteArrayList |
| 4252 | mPolicies.add(policy); |
| 4253 | } |
| 4254 | |
| 4255 | @Override |
| 4256 | public void onExternalStoragePolicyChanged(int uid, String packageName) { |
| 4257 | final int mountMode = getExternalStorageMountMode(uid, packageName); |
| 4258 | remountUidExternalStorage(uid, mountMode); |
| 4259 | } |
| 4260 | |
| 4261 | @Override |
| 4262 | public int getExternalStorageMountMode(int uid, String packageName) { |
Sudheer Shanka | 3a0df3b | 2018-12-12 12:43:43 -0800 | [diff] [blame] | 4263 | if (ENABLE_ISOLATED_STORAGE) { |
| 4264 | return getMountMode(uid, packageName); |
| 4265 | } |
Sudheer Shanka | b161398 | 2019-05-16 16:55:50 -0700 | [diff] [blame] | 4266 | try { |
| 4267 | if (packageName == null) { |
| 4268 | final String[] packagesForUid = mIPackageManager.getPackagesForUid(uid); |
| 4269 | packageName = packagesForUid[0]; |
| 4270 | } |
| 4271 | } catch (RemoteException e) { |
| 4272 | // Should not happen - same process |
| 4273 | } |
Svet Ganov | 6ee871e | 2015-07-10 14:29:33 -0700 | [diff] [blame] | 4274 | // No locking - CopyOnWriteArrayList |
| 4275 | int mountMode = Integer.MAX_VALUE; |
| 4276 | for (ExternalStorageMountPolicy policy : mPolicies) { |
| 4277 | final int policyMode = policy.getMountMode(uid, packageName); |
| 4278 | if (policyMode == Zygote.MOUNT_EXTERNAL_NONE) { |
| 4279 | return Zygote.MOUNT_EXTERNAL_NONE; |
| 4280 | } |
| 4281 | mountMode = Math.min(mountMode, policyMode); |
| 4282 | } |
| 4283 | if (mountMode == Integer.MAX_VALUE) { |
| 4284 | return Zygote.MOUNT_EXTERNAL_NONE; |
| 4285 | } |
| 4286 | return mountMode; |
| 4287 | } |
| 4288 | |
Risan | aec0ee7 | 2018-10-31 10:10:12 +0900 | [diff] [blame] | 4289 | @Override |
| 4290 | public void addResetListener(StorageManagerInternal.ResetListener listener) { |
| 4291 | synchronized (mResetListeners) { |
| 4292 | mResetListeners.add(listener); |
| 4293 | } |
| 4294 | } |
| 4295 | |
| 4296 | public void onReset(IVold vold) { |
| 4297 | synchronized (mResetListeners) { |
| 4298 | for (StorageManagerInternal.ResetListener listener : mResetListeners) { |
| 4299 | listener.onReset(vold); |
| 4300 | } |
| 4301 | } |
| 4302 | } |
| 4303 | |
Martijn Coenen | 7391820 | 2019-12-09 17:02:44 +0100 | [diff] [blame] | 4304 | @Override |
| 4305 | public void resetUser(int userId) { |
| 4306 | // TODO(b/145931219): ideally, we only reset storage for the user in question, |
| 4307 | // but for now, reset everything. |
| 4308 | mHandler.obtainMessage(H_RESET).sendToTarget(); |
| 4309 | } |
| 4310 | |
Svet Ganov | 6ee871e | 2015-07-10 14:29:33 -0700 | [diff] [blame] | 4311 | public boolean hasExternalStorage(int uid, String packageName) { |
Amith Yamasani | 2bd5cff | 2015-07-22 14:42:31 -0700 | [diff] [blame] | 4312 | // No need to check for system uid. This avoids a deadlock between |
| 4313 | // PackageManagerService and AppOpsService. |
| 4314 | if (uid == Process.SYSTEM_UID) { |
| 4315 | return true; |
| 4316 | } |
Sudheer Shanka | 3a0df3b | 2018-12-12 12:43:43 -0800 | [diff] [blame] | 4317 | if (ENABLE_ISOLATED_STORAGE) { |
| 4318 | return getMountMode(uid, packageName) != Zygote.MOUNT_EXTERNAL_NONE; |
| 4319 | } |
Svet Ganov | 6ee871e | 2015-07-10 14:29:33 -0700 | [diff] [blame] | 4320 | // No locking - CopyOnWriteArrayList |
| 4321 | for (ExternalStorageMountPolicy policy : mPolicies) { |
| 4322 | final boolean policyHasStorage = policy.hasExternalStorage(uid, packageName); |
| 4323 | if (!policyHasStorage) { |
| 4324 | return false; |
| 4325 | } |
| 4326 | } |
| 4327 | return true; |
| 4328 | } |
Sudheer Shanka | b161398 | 2019-05-16 16:55:50 -0700 | [diff] [blame] | 4329 | |
| 4330 | public void onAppOpsChanged(int code, int uid, |
| 4331 | @Nullable String packageName, int mode) { |
| 4332 | if (mode == MODE_ALLOWED && (code == OP_READ_EXTERNAL_STORAGE |
| 4333 | || code == OP_WRITE_EXTERNAL_STORAGE |
| 4334 | || code == OP_REQUEST_INSTALL_PACKAGES)) { |
| 4335 | final long token = Binder.clearCallingIdentity(); |
| 4336 | try { |
| 4337 | final UserManagerInternal userManagerInternal = |
| 4338 | LocalServices.getService(UserManagerInternal.class); |
| 4339 | if (userManagerInternal.isUserInitialized(UserHandle.getUserId(uid))) { |
| 4340 | onExternalStoragePolicyChanged(uid, packageName); |
| 4341 | } |
| 4342 | } finally { |
| 4343 | Binder.restoreCallingIdentity(token); |
| 4344 | } |
| 4345 | } |
| 4346 | } |
Svet Ganov | 6ee871e | 2015-07-10 14:29:33 -0700 | [diff] [blame] | 4347 | } |
Jeff Sharkey | fa23c5a | 2011-08-09 21:44:24 -0700 | [diff] [blame] | 4348 | } |