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)); |
Shafik Nassar | 07dcbbc | 2020-01-28 18:35:25 +0000 | [diff] [blame] | 1704 | // Perform hard reboot to kick policy into place |
| 1705 | mContext.getSystemService(PowerManager.class).reboot("fuse_prop"); |
Abhijeet Kaur | b984771 | 2019-11-19 16:35:47 +0000 | [diff] [blame] | 1706 | } |
| 1707 | } |
| 1708 | |
Jeff Sharkey | cd57599 | 2016-03-29 14:12:49 -0600 | [diff] [blame] | 1709 | private void start() { |
peter.zhang | b0d11d1 | 2019-06-13 16:39:30 +0800 | [diff] [blame] | 1710 | connectStoraged(); |
| 1711 | connectVold(); |
Jeff Sharkey | cd57599 | 2016-03-29 14:12:49 -0600 | [diff] [blame] | 1712 | } |
| 1713 | |
peter.zhang | b0d11d1 | 2019-06-13 16:39:30 +0800 | [diff] [blame] | 1714 | private void connectStoraged() { |
Jin Qian | 12690d5 | 2017-10-13 18:17:04 -0700 | [diff] [blame] | 1715 | IBinder binder = ServiceManager.getService("storaged"); |
| 1716 | if (binder != null) { |
| 1717 | try { |
| 1718 | binder.linkToDeath(new DeathRecipient() { |
| 1719 | @Override |
| 1720 | public void binderDied() { |
| 1721 | Slog.w(TAG, "storaged died; reconnecting"); |
| 1722 | mStoraged = null; |
peter.zhang | b0d11d1 | 2019-06-13 16:39:30 +0800 | [diff] [blame] | 1723 | connectStoraged(); |
Jin Qian | 12690d5 | 2017-10-13 18:17:04 -0700 | [diff] [blame] | 1724 | } |
| 1725 | }, 0); |
| 1726 | } catch (RemoteException e) { |
| 1727 | binder = null; |
| 1728 | } |
| 1729 | } |
| 1730 | |
| 1731 | if (binder != null) { |
| 1732 | mStoraged = IStoraged.Stub.asInterface(binder); |
| 1733 | } else { |
| 1734 | Slog.w(TAG, "storaged not found; trying again"); |
| 1735 | } |
| 1736 | |
peter.zhang | b0d11d1 | 2019-06-13 16:39:30 +0800 | [diff] [blame] | 1737 | if (mStoraged == null) { |
| 1738 | BackgroundThread.getHandler().postDelayed(() -> { |
| 1739 | connectStoraged(); |
| 1740 | }, DateUtils.SECOND_IN_MILLIS); |
| 1741 | } else { |
| 1742 | onDaemonConnected(); |
| 1743 | } |
| 1744 | } |
| 1745 | |
| 1746 | private void connectVold() { |
| 1747 | IBinder binder = ServiceManager.getService("vold"); |
Jeff Sharkey | 1019de9 | 2017-09-06 13:47:03 -0600 | [diff] [blame] | 1748 | if (binder != null) { |
| 1749 | try { |
| 1750 | binder.linkToDeath(new DeathRecipient() { |
| 1751 | @Override |
| 1752 | public void binderDied() { |
| 1753 | Slog.w(TAG, "vold died; reconnecting"); |
Jin Qian | 12690d5 | 2017-10-13 18:17:04 -0700 | [diff] [blame] | 1754 | mVold = null; |
peter.zhang | b0d11d1 | 2019-06-13 16:39:30 +0800 | [diff] [blame] | 1755 | connectVold(); |
Jeff Sharkey | 1019de9 | 2017-09-06 13:47:03 -0600 | [diff] [blame] | 1756 | } |
| 1757 | }, 0); |
| 1758 | } catch (RemoteException e) { |
| 1759 | binder = null; |
| 1760 | } |
| 1761 | } |
| 1762 | |
| 1763 | if (binder != null) { |
| 1764 | mVold = IVold.Stub.asInterface(binder); |
Jeff Sharkey | 8058fe6 | 2017-09-13 11:50:33 -0600 | [diff] [blame] | 1765 | try { |
| 1766 | mVold.setListener(mListener); |
Jeff Sharkey | 8058fe6 | 2017-09-13 11:50:33 -0600 | [diff] [blame] | 1767 | } catch (RemoteException e) { |
Jin Qian | 12690d5 | 2017-10-13 18:17:04 -0700 | [diff] [blame] | 1768 | mVold = null; |
Jeff Sharkey | 8058fe6 | 2017-09-13 11:50:33 -0600 | [diff] [blame] | 1769 | Slog.w(TAG, "vold listener rejected; trying again", e); |
| 1770 | } |
Jeff Sharkey | 1019de9 | 2017-09-06 13:47:03 -0600 | [diff] [blame] | 1771 | } else { |
| 1772 | Slog.w(TAG, "vold not found; trying again"); |
Jeff Sharkey | 1019de9 | 2017-09-06 13:47:03 -0600 | [diff] [blame] | 1773 | } |
Jeff Sharkey | 8058fe6 | 2017-09-13 11:50:33 -0600 | [diff] [blame] | 1774 | |
peter.zhang | b0d11d1 | 2019-06-13 16:39:30 +0800 | [diff] [blame] | 1775 | if (mVold == null) { |
Jin Qian | 12690d5 | 2017-10-13 18:17:04 -0700 | [diff] [blame] | 1776 | BackgroundThread.getHandler().postDelayed(() -> { |
peter.zhang | b0d11d1 | 2019-06-13 16:39:30 +0800 | [diff] [blame] | 1777 | connectVold(); |
Jin Qian | 12690d5 | 2017-10-13 18:17:04 -0700 | [diff] [blame] | 1778 | }, DateUtils.SECOND_IN_MILLIS); |
| 1779 | } else { |
| 1780 | onDaemonConnected(); |
| 1781 | } |
Jeff Sharkey | 1019de9 | 2017-09-06 13:47:03 -0600 | [diff] [blame] | 1782 | } |
| 1783 | |
Jeff Sharkey | 11697f5 | 2018-12-13 10:14:42 -0700 | [diff] [blame] | 1784 | private void servicesReady() { |
Sudheer Shanka | ff58507 | 2019-01-29 23:19:45 -0800 | [diff] [blame] | 1785 | mPmInternal = LocalServices.getService(PackageManagerInternal.class); |
Sudheer Shanka | ff58507 | 2019-01-29 23:19:45 -0800 | [diff] [blame] | 1786 | |
| 1787 | mIPackageManager = IPackageManager.Stub.asInterface( |
| 1788 | ServiceManager.getService("package")); |
| 1789 | mIAppOpsService = IAppOpsService.Stub.asInterface( |
| 1790 | ServiceManager.getService(Context.APP_OPS_SERVICE)); |
Zim | 74a9bba | 2019-09-03 20:49:13 +0100 | [diff] [blame] | 1791 | |
| 1792 | ProviderInfo provider = mPmInternal.resolveContentProvider( |
| 1793 | MediaStore.AUTHORITY, PackageManager.MATCH_DIRECT_BOOT_AWARE |
| 1794 | | PackageManager.MATCH_DIRECT_BOOT_UNAWARE, |
| 1795 | UserHandle.getUserId(UserHandle.USER_SYSTEM)); |
| 1796 | if (provider != null) { |
Martijn Coenen | 44db1ac | 2019-12-03 16:06:19 +0100 | [diff] [blame] | 1797 | mMediaStoreAuthorityAppId = UserHandle.getAppId(provider.applicationInfo.uid); |
Zim | 74a9bba | 2019-09-03 20:49:13 +0100 | [diff] [blame] | 1798 | } |
| 1799 | |
Martijn Coenen | 496ac00 | 2020-01-08 14:55:53 +0100 | [diff] [blame] | 1800 | provider = mPmInternal.resolveContentProvider( |
| 1801 | Downloads.Impl.AUTHORITY, PackageManager.MATCH_DIRECT_BOOT_AWARE |
| 1802 | | PackageManager.MATCH_DIRECT_BOOT_UNAWARE, |
| 1803 | UserHandle.getUserId(UserHandle.USER_SYSTEM)); |
| 1804 | |
| 1805 | if (provider != null) { |
| 1806 | mDownloadsAuthorityAppId = UserHandle.getAppId(provider.applicationInfo.uid); |
| 1807 | } |
| 1808 | |
Sudheer Shanka | ff58507 | 2019-01-29 23:19:45 -0800 | [diff] [blame] | 1809 | try { |
| 1810 | mIAppOpsService.startWatchingMode(OP_REQUEST_INSTALL_PACKAGES, null, mAppOpsCallback); |
Sudheer Shanka | 783c90e | 2019-04-12 13:55:20 -0700 | [diff] [blame] | 1811 | mIAppOpsService.startWatchingMode(OP_LEGACY_STORAGE, null, mAppOpsCallback); |
Sudheer Shanka | ff58507 | 2019-01-29 23:19:45 -0800 | [diff] [blame] | 1812 | } catch (RemoteException e) { |
| 1813 | } |
Jeff Sharkey | 11697f5 | 2018-12-13 10:14:42 -0700 | [diff] [blame] | 1814 | } |
| 1815 | |
| 1816 | private static long getLastAccessTime(AppOpsManager manager, |
| 1817 | int uid, String packageName, int[] ops) { |
| 1818 | long maxTime = 0; |
| 1819 | final List<AppOpsManager.PackageOps> pkgs = manager.getOpsForPackage(uid, packageName, ops); |
Eugene Susla | 04d021c | 2018-12-17 14:49:59 -0800 | [diff] [blame] | 1820 | for (AppOpsManager.PackageOps pkg : CollectionUtils.emptyIfNull(pkgs)) { |
| 1821 | for (AppOpsManager.OpEntry op : CollectionUtils.emptyIfNull(pkg.getOps())) { |
Svet Ganov | af189e3 | 2019-02-15 18:45:29 -0800 | [diff] [blame] | 1822 | maxTime = Math.max(maxTime, op.getLastAccessTime( |
| 1823 | AppOpsManager.OP_FLAGS_ALL_TRUSTED)); |
Jeff Sharkey | 11697f5 | 2018-12-13 10:14:42 -0700 | [diff] [blame] | 1824 | } |
| 1825 | } |
| 1826 | return maxTime; |
| 1827 | } |
| 1828 | |
Jeff Sharkey | 56e6293 | 2015-03-21 20:41:00 -0700 | [diff] [blame] | 1829 | private void systemReady() { |
Wale Ogunwale | 6767eae | 2018-05-03 15:52:51 -0700 | [diff] [blame] | 1830 | LocalServices.getService(ActivityTaskManagerInternal.class) |
Jeff Sharkey | 9765e44 | 2017-12-14 22:15:14 -0700 | [diff] [blame] | 1831 | .registerScreenObserver(this); |
| 1832 | |
Jeff Sharkey | b049e21 | 2012-09-07 23:16:01 -0700 | [diff] [blame] | 1833 | mHandler.obtainMessage(H_SYSTEM_READY).sendToTarget(); |
| 1834 | } |
| 1835 | |
Yasuhiro Matsuda | 87a38b5 | 2015-07-24 22:10:16 +0900 | [diff] [blame] | 1836 | private void bootCompleted() { |
| 1837 | mBootCompleted = true; |
Daniel Rosenberg | 137aed1 | 2019-03-15 18:41:11 -0700 | [diff] [blame] | 1838 | mHandler.obtainMessage(H_BOOT_COMPLETED).sendToTarget(); |
Abhijeet Kaur | 10c5655 | 2019-12-04 14:17:40 +0000 | [diff] [blame] | 1839 | updateFusePropFromSettings(); |
Daniel Rosenberg | 137aed1 | 2019-03-15 18:41:11 -0700 | [diff] [blame] | 1840 | } |
| 1841 | |
| 1842 | private void handleBootCompleted() { |
| 1843 | initIfBootedAndConnected(); |
| 1844 | resetIfBootedAndConnected(); |
Yasuhiro Matsuda | 87a38b5 | 2015-07-24 22:10:16 +0900 | [diff] [blame] | 1845 | } |
| 1846 | |
Jeff Sharkey | fced534 | 2015-05-10 14:53:34 -0700 | [diff] [blame] | 1847 | private String getDefaultPrimaryStorageUuid() { |
| 1848 | if (SystemProperties.getBoolean(StorageManager.PROP_PRIMARY_PHYSICAL, false)) { |
| 1849 | return StorageManager.UUID_PRIMARY_PHYSICAL; |
| 1850 | } else { |
| 1851 | return StorageManager.UUID_PRIVATE_INTERNAL; |
| 1852 | } |
| 1853 | } |
| 1854 | |
Andreas Gampe | a36dc62 | 2018-02-05 17:19:22 -0800 | [diff] [blame] | 1855 | @GuardedBy("mLock") |
Jeff Sharkey | b36586a | 2015-04-27 08:42:28 -0700 | [diff] [blame] | 1856 | private void readSettingsLocked() { |
| 1857 | mRecords.clear(); |
Jeff Sharkey | fced534 | 2015-05-10 14:53:34 -0700 | [diff] [blame] | 1858 | mPrimaryStorageUuid = getDefaultPrimaryStorageUuid(); |
Jeff Sharkey | d95d3bf | 2015-04-14 21:39:44 -0700 | [diff] [blame] | 1859 | |
| 1860 | FileInputStream fis = null; |
| 1861 | try { |
Jeff Sharkey | b36586a | 2015-04-27 08:42:28 -0700 | [diff] [blame] | 1862 | fis = mSettingsFile.openRead(); |
Jeff Sharkey | d95d3bf | 2015-04-14 21:39:44 -0700 | [diff] [blame] | 1863 | final XmlPullParser in = Xml.newPullParser(); |
Wojciech Staszkiewicz | 9e9e2e7 | 2015-05-08 14:58:46 +0100 | [diff] [blame] | 1864 | in.setInput(fis, StandardCharsets.UTF_8.name()); |
Jeff Sharkey | d95d3bf | 2015-04-14 21:39:44 -0700 | [diff] [blame] | 1865 | |
| 1866 | int type; |
| 1867 | while ((type = in.next()) != END_DOCUMENT) { |
| 1868 | if (type == START_TAG) { |
| 1869 | final String tag = in.getName(); |
Jeff Sharkey | 620b32b | 2015-04-23 19:36:02 -0700 | [diff] [blame] | 1870 | if (TAG_VOLUMES.equals(tag)) { |
| 1871 | final int version = readIntAttribute(in, ATTR_VERSION, VERSION_INIT); |
Jeff Sharkey | fced534 | 2015-05-10 14:53:34 -0700 | [diff] [blame] | 1872 | final boolean primaryPhysical = SystemProperties.getBoolean( |
| 1873 | StorageManager.PROP_PRIMARY_PHYSICAL, false); |
| 1874 | final boolean validAttr = (version >= VERSION_FIX_PRIMARY) |
| 1875 | || (version >= VERSION_ADD_PRIMARY && !primaryPhysical); |
| 1876 | if (validAttr) { |
Jeff Sharkey | 620b32b | 2015-04-23 19:36:02 -0700 | [diff] [blame] | 1877 | mPrimaryStorageUuid = readStringAttribute(in, |
| 1878 | ATTR_PRIMARY_STORAGE_UUID); |
Jeff Sharkey | 620b32b | 2015-04-23 19:36:02 -0700 | [diff] [blame] | 1879 | } |
Jeff Sharkey | 620b32b | 2015-04-23 19:36:02 -0700 | [diff] [blame] | 1880 | } else if (TAG_VOLUME.equals(tag)) { |
Jeff Sharkey | b36586a | 2015-04-27 08:42:28 -0700 | [diff] [blame] | 1881 | final VolumeRecord rec = readVolumeRecord(in); |
| 1882 | mRecords.put(rec.fsUuid, rec); |
Jeff Sharkey | d95d3bf | 2015-04-14 21:39:44 -0700 | [diff] [blame] | 1883 | } |
| 1884 | } |
| 1885 | } |
| 1886 | } catch (FileNotFoundException e) { |
| 1887 | // Missing metadata is okay, probably first boot |
| 1888 | } catch (IOException e) { |
| 1889 | Slog.wtf(TAG, "Failed reading metadata", e); |
| 1890 | } catch (XmlPullParserException e) { |
| 1891 | Slog.wtf(TAG, "Failed reading metadata", e); |
| 1892 | } finally { |
| 1893 | IoUtils.closeQuietly(fis); |
| 1894 | } |
| 1895 | } |
| 1896 | |
Andreas Gampe | a36dc62 | 2018-02-05 17:19:22 -0800 | [diff] [blame] | 1897 | @GuardedBy("mLock") |
Jeff Sharkey | b36586a | 2015-04-27 08:42:28 -0700 | [diff] [blame] | 1898 | private void writeSettingsLocked() { |
Jeff Sharkey | d95d3bf | 2015-04-14 21:39:44 -0700 | [diff] [blame] | 1899 | FileOutputStream fos = null; |
| 1900 | try { |
Jeff Sharkey | b36586a | 2015-04-27 08:42:28 -0700 | [diff] [blame] | 1901 | fos = mSettingsFile.startWrite(); |
Jeff Sharkey | d95d3bf | 2015-04-14 21:39:44 -0700 | [diff] [blame] | 1902 | |
| 1903 | XmlSerializer out = new FastXmlSerializer(); |
Wojciech Staszkiewicz | 9e9e2e7 | 2015-05-08 14:58:46 +0100 | [diff] [blame] | 1904 | out.setOutput(fos, StandardCharsets.UTF_8.name()); |
Jeff Sharkey | d95d3bf | 2015-04-14 21:39:44 -0700 | [diff] [blame] | 1905 | out.startDocument(null, true); |
| 1906 | out.startTag(null, TAG_VOLUMES); |
Jeff Sharkey | fced534 | 2015-05-10 14:53:34 -0700 | [diff] [blame] | 1907 | writeIntAttribute(out, ATTR_VERSION, VERSION_FIX_PRIMARY); |
Jeff Sharkey | 620b32b | 2015-04-23 19:36:02 -0700 | [diff] [blame] | 1908 | writeStringAttribute(out, ATTR_PRIMARY_STORAGE_UUID, mPrimaryStorageUuid); |
Jeff Sharkey | b36586a | 2015-04-27 08:42:28 -0700 | [diff] [blame] | 1909 | final int size = mRecords.size(); |
Jeff Sharkey | d95d3bf | 2015-04-14 21:39:44 -0700 | [diff] [blame] | 1910 | for (int i = 0; i < size; i++) { |
Jeff Sharkey | b36586a | 2015-04-27 08:42:28 -0700 | [diff] [blame] | 1911 | final VolumeRecord rec = mRecords.valueAt(i); |
| 1912 | writeVolumeRecord(out, rec); |
Jeff Sharkey | d95d3bf | 2015-04-14 21:39:44 -0700 | [diff] [blame] | 1913 | } |
| 1914 | out.endTag(null, TAG_VOLUMES); |
| 1915 | out.endDocument(); |
| 1916 | |
Jeff Sharkey | b36586a | 2015-04-27 08:42:28 -0700 | [diff] [blame] | 1917 | mSettingsFile.finishWrite(fos); |
Jeff Sharkey | d95d3bf | 2015-04-14 21:39:44 -0700 | [diff] [blame] | 1918 | } catch (IOException e) { |
| 1919 | if (fos != null) { |
Jeff Sharkey | b36586a | 2015-04-27 08:42:28 -0700 | [diff] [blame] | 1920 | mSettingsFile.failWrite(fos); |
Jeff Sharkey | d95d3bf | 2015-04-14 21:39:44 -0700 | [diff] [blame] | 1921 | } |
| 1922 | } |
| 1923 | } |
| 1924 | |
Jeff Sharkey | b36586a | 2015-04-27 08:42:28 -0700 | [diff] [blame] | 1925 | public static VolumeRecord readVolumeRecord(XmlPullParser in) throws IOException { |
| 1926 | final int type = readIntAttribute(in, ATTR_TYPE); |
| 1927 | final String fsUuid = readStringAttribute(in, ATTR_FS_UUID); |
| 1928 | final VolumeRecord meta = new VolumeRecord(type, fsUuid); |
Jeff Sharkey | 5cc0df2 | 2015-06-17 19:44:05 -0700 | [diff] [blame] | 1929 | meta.partGuid = readStringAttribute(in, ATTR_PART_GUID); |
Jeff Sharkey | b36586a | 2015-04-27 08:42:28 -0700 | [diff] [blame] | 1930 | meta.nickname = readStringAttribute(in, ATTR_NICKNAME); |
| 1931 | meta.userFlags = readIntAttribute(in, ATTR_USER_FLAGS); |
Jeff Sharkey | 3811f35 | 2019-05-14 11:54:36 -0600 | [diff] [blame] | 1932 | meta.createdMillis = readLongAttribute(in, ATTR_CREATED_MILLIS, 0); |
| 1933 | meta.lastSeenMillis = readLongAttribute(in, ATTR_LAST_SEEN_MILLIS, 0); |
| 1934 | meta.lastTrimMillis = readLongAttribute(in, ATTR_LAST_TRIM_MILLIS, 0); |
| 1935 | meta.lastBenchMillis = readLongAttribute(in, ATTR_LAST_BENCH_MILLIS, 0); |
Jeff Sharkey | b36586a | 2015-04-27 08:42:28 -0700 | [diff] [blame] | 1936 | return meta; |
| 1937 | } |
| 1938 | |
| 1939 | public static void writeVolumeRecord(XmlSerializer out, VolumeRecord rec) throws IOException { |
| 1940 | out.startTag(null, TAG_VOLUME); |
| 1941 | writeIntAttribute(out, ATTR_TYPE, rec.type); |
| 1942 | writeStringAttribute(out, ATTR_FS_UUID, rec.fsUuid); |
Jeff Sharkey | 5cc0df2 | 2015-06-17 19:44:05 -0700 | [diff] [blame] | 1943 | writeStringAttribute(out, ATTR_PART_GUID, rec.partGuid); |
Jeff Sharkey | b36586a | 2015-04-27 08:42:28 -0700 | [diff] [blame] | 1944 | writeStringAttribute(out, ATTR_NICKNAME, rec.nickname); |
| 1945 | writeIntAttribute(out, ATTR_USER_FLAGS, rec.userFlags); |
Jeff Sharkey | e8a4b66 | 2015-06-27 15:43:45 -0700 | [diff] [blame] | 1946 | writeLongAttribute(out, ATTR_CREATED_MILLIS, rec.createdMillis); |
Jeff Sharkey | 3811f35 | 2019-05-14 11:54:36 -0600 | [diff] [blame] | 1947 | writeLongAttribute(out, ATTR_LAST_SEEN_MILLIS, rec.lastSeenMillis); |
Jeff Sharkey | e8a4b66 | 2015-06-27 15:43:45 -0700 | [diff] [blame] | 1948 | writeLongAttribute(out, ATTR_LAST_TRIM_MILLIS, rec.lastTrimMillis); |
| 1949 | writeLongAttribute(out, ATTR_LAST_BENCH_MILLIS, rec.lastBenchMillis); |
Jeff Sharkey | b36586a | 2015-04-27 08:42:28 -0700 | [diff] [blame] | 1950 | out.endTag(null, TAG_VOLUME); |
| 1951 | } |
| 1952 | |
San Mehat | 207e538 | 2010-02-04 20:46:54 -0800 | [diff] [blame] | 1953 | /** |
San Mehat | 4270e1e | 2010-01-29 05:32:19 -0800 | [diff] [blame] | 1954 | * Exposed API calls below here |
| 1955 | */ |
| 1956 | |
Jeff Sharkey | 7151a9a | 2015-04-04 15:22:37 -0700 | [diff] [blame] | 1957 | @Override |
Sudheer Shanka | 2250d56 | 2016-11-07 15:41:02 -0800 | [diff] [blame] | 1958 | public void registerListener(IStorageEventListener listener) { |
Jeff Sharkey | 7151a9a | 2015-04-04 15:22:37 -0700 | [diff] [blame] | 1959 | mCallbacks.register(listener); |
San Mehat | 4270e1e | 2010-01-29 05:32:19 -0800 | [diff] [blame] | 1960 | } |
| 1961 | |
Jeff Sharkey | 7151a9a | 2015-04-04 15:22:37 -0700 | [diff] [blame] | 1962 | @Override |
Sudheer Shanka | 2250d56 | 2016-11-07 15:41:02 -0800 | [diff] [blame] | 1963 | public void unregisterListener(IStorageEventListener listener) { |
Jeff Sharkey | 7151a9a | 2015-04-04 15:22:37 -0700 | [diff] [blame] | 1964 | mCallbacks.unregister(listener); |
San Mehat | 4270e1e | 2010-01-29 05:32:19 -0800 | [diff] [blame] | 1965 | } |
| 1966 | |
Jeff Sharkey | 4887789 | 2015-03-18 11:27:19 -0700 | [diff] [blame] | 1967 | @Override |
Sudheer Shanka | 2250d56 | 2016-11-07 15:41:02 -0800 | [diff] [blame] | 1968 | public void shutdown(final IStorageShutdownObserver observer) { |
Jeff Sharkey | 4887789 | 2015-03-18 11:27:19 -0700 | [diff] [blame] | 1969 | enforcePermission(android.Manifest.permission.SHUTDOWN); |
San Mehat | 4270e1e | 2010-01-29 05:32:19 -0800 | [diff] [blame] | 1970 | |
San Mehat | a507859 | 2010-03-25 09:36:54 -0700 | [diff] [blame] | 1971 | Slog.i(TAG, "Shutting down"); |
Jeff Sharkey | 4887789 | 2015-03-18 11:27:19 -0700 | [diff] [blame] | 1972 | mHandler.obtainMessage(H_SHUTDOWN, observer).sendToTarget(); |
San Mehat | 4270e1e | 2010-01-29 05:32:19 -0800 | [diff] [blame] | 1973 | } |
| 1974 | |
Jeff Sharkey | 4887789 | 2015-03-18 11:27:19 -0700 | [diff] [blame] | 1975 | @Override |
Jeff Sharkey | 7151a9a | 2015-04-04 15:22:37 -0700 | [diff] [blame] | 1976 | public void mount(String volId) { |
| 1977 | enforcePermission(android.Manifest.permission.MOUNT_UNMOUNT_FILESYSTEMS); |
Jeff Sharkey | 7151a9a | 2015-04-04 15:22:37 -0700 | [diff] [blame] | 1978 | |
Jeff Sharkey | ef10ee0 | 2015-07-05 14:17:27 -0700 | [diff] [blame] | 1979 | final VolumeInfo vol = findVolumeByIdOrThrow(volId); |
Jeff Sharkey | 2e606d7 | 2015-07-27 14:19:54 -0700 | [diff] [blame] | 1980 | if (isMountDisallowed(vol)) { |
| 1981 | throw new SecurityException("Mounting " + volId + " restricted by policy"); |
Jeff Sharkey | 7151a9a | 2015-04-04 15:22:37 -0700 | [diff] [blame] | 1982 | } |
Zim | 17be6f9 | 2019-09-25 14:37:55 +0100 | [diff] [blame] | 1983 | |
Sudheer Shanka | 3f0645b | 2018-09-18 13:07:59 -0700 | [diff] [blame] | 1984 | mount(vol); |
| 1985 | } |
| 1986 | |
Zim | 17be6f9 | 2019-09-25 14:37:55 +0100 | [diff] [blame] | 1987 | private void mount(VolumeInfo vol) { |
Jeff Sharkey | 7151a9a | 2015-04-04 15:22:37 -0700 | [diff] [blame] | 1988 | try { |
Zim | 17be6f9 | 2019-09-25 14:37:55 +0100 | [diff] [blame] | 1989 | // TODO(b/135341433): Remove paranoid logging when FUSE is stable |
| 1990 | Slog.i(TAG, "Mounting volume " + vol); |
Zim | 95eca1d | 2019-11-15 18:03:00 +0000 | [diff] [blame] | 1991 | mVold.mount(vol.id, vol.mountFlags, vol.mountUserId, new IVoldMountCallback.Stub() { |
| 1992 | @Override |
Zim | 7924a51 | 2020-01-15 15:23:54 +0000 | [diff] [blame] | 1993 | public boolean onVolumeChecking(FileDescriptor fd, String path, |
Zim | 95eca1d | 2019-11-15 18:03:00 +0000 | [diff] [blame] | 1994 | String internalPath) { |
| 1995 | vol.path = path; |
| 1996 | vol.internalPath = internalPath; |
Zim | 7924a51 | 2020-01-15 15:23:54 +0000 | [diff] [blame] | 1997 | ParcelFileDescriptor pfd = new ParcelFileDescriptor(fd); |
Zim | 95eca1d | 2019-11-15 18:03:00 +0000 | [diff] [blame] | 1998 | try { |
Zim | 7924a51 | 2020-01-15 15:23:54 +0000 | [diff] [blame] | 1999 | mStorageSessionController.onVolumeMount(pfd, vol); |
Zim | 95eca1d | 2019-11-15 18:03:00 +0000 | [diff] [blame] | 2000 | return true; |
| 2001 | } catch (ExternalStorageServiceException e) { |
Zim | 7924a51 | 2020-01-15 15:23:54 +0000 | [diff] [blame] | 2002 | Slog.e(TAG, "Failed to mount volume " + vol, e); |
| 2003 | |
Zim | b8ca054 | 2020-01-16 22:54:48 +0000 | [diff] [blame] | 2004 | int nextResetSeconds = REMOTE_TIMEOUT_SECONDS * 2; |
| 2005 | Slog.i(TAG, "Scheduling reset in " + nextResetSeconds + "s"); |
Zim | 7924a51 | 2020-01-15 15:23:54 +0000 | [diff] [blame] | 2006 | mHandler.removeMessages(H_RESET); |
| 2007 | mHandler.sendMessageDelayed(mHandler.obtainMessage(H_RESET), |
Zim | b8ca054 | 2020-01-16 22:54:48 +0000 | [diff] [blame] | 2008 | TimeUnit.SECONDS.toMillis(nextResetSeconds)); |
Zim | 95eca1d | 2019-11-15 18:03:00 +0000 | [diff] [blame] | 2009 | return false; |
Zim | 7924a51 | 2020-01-15 15:23:54 +0000 | [diff] [blame] | 2010 | } finally { |
| 2011 | try { |
| 2012 | pfd.close(); |
| 2013 | } catch (Exception e) { |
| 2014 | Slog.e(TAG, "Failed to close FUSE device fd", e); |
| 2015 | } |
Zim | 95eca1d | 2019-11-15 18:03:00 +0000 | [diff] [blame] | 2016 | } |
| 2017 | } |
| 2018 | }); |
Zim | 17be6f9 | 2019-09-25 14:37:55 +0100 | [diff] [blame] | 2019 | Slog.i(TAG, "Mounted volume " + vol); |
Jeff Sharkey | ace874b | 2017-09-07 15:27:33 -0600 | [diff] [blame] | 2020 | } catch (Exception e) { |
| 2021 | Slog.wtf(TAG, e); |
Jeff Sharkey | 7151a9a | 2015-04-04 15:22:37 -0700 | [diff] [blame] | 2022 | } |
| 2023 | } |
| 2024 | |
| 2025 | @Override |
| 2026 | public void unmount(String volId) { |
| 2027 | enforcePermission(android.Manifest.permission.MOUNT_UNMOUNT_FILESYSTEMS); |
Jeff Sharkey | 7151a9a | 2015-04-04 15:22:37 -0700 | [diff] [blame] | 2028 | |
Jeff Sharkey | ef10ee0 | 2015-07-05 14:17:27 -0700 | [diff] [blame] | 2029 | final VolumeInfo vol = findVolumeByIdOrThrow(volId); |
Sudheer Shanka | 3f0645b | 2018-09-18 13:07:59 -0700 | [diff] [blame] | 2030 | unmount(vol); |
| 2031 | } |
| 2032 | |
| 2033 | private void unmount(VolumeInfo vol) { |
Jeff Sharkey | 7151a9a | 2015-04-04 15:22:37 -0700 | [diff] [blame] | 2034 | try { |
Jeff Sharkey | 5440279 | 2017-09-15 16:05:19 -0600 | [diff] [blame] | 2035 | mVold.unmount(vol.id); |
Zim | 17be6f9 | 2019-09-25 14:37:55 +0100 | [diff] [blame] | 2036 | mStorageSessionController.onVolumeUnmount(vol); |
Ricky Wai | 4482ab5 | 2019-12-10 19:08:18 +0000 | [diff] [blame] | 2037 | try { |
| 2038 | if (vol.type == VolumeInfo.TYPE_PRIVATE) { |
| 2039 | mInstaller.onPrivateVolumeRemoved(vol.getFsUuid()); |
| 2040 | } |
| 2041 | } catch (Installer.InstallerException e) { |
| 2042 | Slog.e(TAG, "Failed unmount mirror data", e); |
| 2043 | } |
Jeff Sharkey | ace874b | 2017-09-07 15:27:33 -0600 | [diff] [blame] | 2044 | } catch (Exception e) { |
| 2045 | Slog.wtf(TAG, e); |
Jeff Sharkey | 7151a9a | 2015-04-04 15:22:37 -0700 | [diff] [blame] | 2046 | } |
| 2047 | } |
| 2048 | |
| 2049 | @Override |
| 2050 | public void format(String volId) { |
Jeff Sharkey | 4887789 | 2015-03-18 11:27:19 -0700 | [diff] [blame] | 2051 | enforcePermission(android.Manifest.permission.MOUNT_FORMAT_FILESYSTEMS); |
San Mehat | 5b77dab | 2010-01-26 13:28:50 -0800 | [diff] [blame] | 2052 | |
Jeff Sharkey | ef10ee0 | 2015-07-05 14:17:27 -0700 | [diff] [blame] | 2053 | final VolumeInfo vol = findVolumeByIdOrThrow(volId); |
Jeff Sharkey | 7151a9a | 2015-04-04 15:22:37 -0700 | [diff] [blame] | 2054 | try { |
Jeff Sharkey | 5440279 | 2017-09-15 16:05:19 -0600 | [diff] [blame] | 2055 | mVold.format(vol.id, "auto"); |
Jeff Sharkey | ace874b | 2017-09-07 15:27:33 -0600 | [diff] [blame] | 2056 | } catch (Exception e) { |
| 2057 | Slog.wtf(TAG, e); |
Jeff Sharkey | 4887789 | 2015-03-18 11:27:19 -0700 | [diff] [blame] | 2058 | } |
Jeff Sharkey | 7151a9a | 2015-04-04 15:22:37 -0700 | [diff] [blame] | 2059 | } |
| 2060 | |
| 2061 | @Override |
Jeff Sharkey | 7e19f53 | 2017-11-06 13:54:11 -0700 | [diff] [blame] | 2062 | public void benchmark(String volId, IVoldTaskListener listener) { |
Jeff Sharkey | 9756d75 | 2015-05-14 21:07:42 -0700 | [diff] [blame] | 2063 | enforcePermission(android.Manifest.permission.MOUNT_FORMAT_FILESYSTEMS); |
Jeff Sharkey | 9756d75 | 2015-05-14 21:07:42 -0700 | [diff] [blame] | 2064 | |
| 2065 | try { |
Jeff Sharkey | b302c54 | 2017-09-15 12:57:59 -0600 | [diff] [blame] | 2066 | mVold.benchmark(volId, new IVoldTaskListener.Stub() { |
| 2067 | @Override |
| 2068 | public void onStatus(int status, PersistableBundle extras) { |
Jeff Sharkey | 7e19f53 | 2017-11-06 13:54:11 -0700 | [diff] [blame] | 2069 | dispatchOnStatus(listener, status, extras); |
Jeff Sharkey | b302c54 | 2017-09-15 12:57:59 -0600 | [diff] [blame] | 2070 | } |
| 2071 | |
| 2072 | @Override |
| 2073 | public void onFinished(int status, PersistableBundle extras) { |
Jeff Sharkey | 7e19f53 | 2017-11-06 13:54:11 -0700 | [diff] [blame] | 2074 | dispatchOnFinished(listener, status, extras); |
Jeff Sharkey | b302c54 | 2017-09-15 12:57:59 -0600 | [diff] [blame] | 2075 | |
| 2076 | final String path = extras.getString("path"); |
| 2077 | final String ident = extras.getString("ident"); |
| 2078 | final long create = extras.getLong("create"); |
| 2079 | final long run = extras.getLong("run"); |
| 2080 | final long destroy = extras.getLong("destroy"); |
| 2081 | |
| 2082 | final DropBoxManager dropBox = mContext.getSystemService(DropBoxManager.class); |
| 2083 | dropBox.addText(TAG_STORAGE_BENCHMARK, scrubPath(path) |
| 2084 | + " " + ident + " " + create + " " + run + " " + destroy); |
| 2085 | |
| 2086 | synchronized (mLock) { |
| 2087 | final VolumeRecord rec = findRecordForPath(path); |
| 2088 | if (rec != null) { |
| 2089 | rec.lastBenchMillis = System.currentTimeMillis(); |
| 2090 | writeSettingsLocked(); |
| 2091 | } |
| 2092 | } |
| 2093 | } |
| 2094 | }); |
Jeff Sharkey | 7e19f53 | 2017-11-06 13:54:11 -0700 | [diff] [blame] | 2095 | } catch (RemoteException e) { |
| 2096 | throw e.rethrowAsRuntimeException(); |
Jeff Sharkey | 9756d75 | 2015-05-14 21:07:42 -0700 | [diff] [blame] | 2097 | } |
| 2098 | } |
| 2099 | |
| 2100 | @Override |
Jeff Sharkey | 7151a9a | 2015-04-04 15:22:37 -0700 | [diff] [blame] | 2101 | public void partitionPublic(String diskId) { |
| 2102 | enforcePermission(android.Manifest.permission.MOUNT_FORMAT_FILESYSTEMS); |
Jeff Sharkey | 7151a9a | 2015-04-04 15:22:37 -0700 | [diff] [blame] | 2103 | |
Jeff Sharkey | eba260d | 2015-04-19 14:35:16 -0700 | [diff] [blame] | 2104 | final CountDownLatch latch = findOrCreateDiskScanLatch(diskId); |
Jeff Sharkey | 7151a9a | 2015-04-04 15:22:37 -0700 | [diff] [blame] | 2105 | try { |
Jeff Sharkey | 5440279 | 2017-09-15 16:05:19 -0600 | [diff] [blame] | 2106 | mVold.partition(diskId, IVold.PARTITION_TYPE_PUBLIC, -1); |
Jeff Sharkey | edcdaf6 | 2015-07-09 09:45:36 -0700 | [diff] [blame] | 2107 | waitForLatch(latch, "partitionPublic", 3 * DateUtils.MINUTE_IN_MILLIS); |
Jeff Sharkey | ace874b | 2017-09-07 15:27:33 -0600 | [diff] [blame] | 2108 | } catch (Exception e) { |
| 2109 | Slog.wtf(TAG, e); |
Jeff Sharkey | 7151a9a | 2015-04-04 15:22:37 -0700 | [diff] [blame] | 2110 | } |
| 2111 | } |
| 2112 | |
| 2113 | @Override |
| 2114 | public void partitionPrivate(String diskId) { |
| 2115 | enforcePermission(android.Manifest.permission.MOUNT_FORMAT_FILESYSTEMS); |
Amith Yamasani | 462ac3a | 2015-06-30 14:21:01 -0700 | [diff] [blame] | 2116 | enforceAdminUser(); |
Jeff Sharkey | 7151a9a | 2015-04-04 15:22:37 -0700 | [diff] [blame] | 2117 | |
Jeff Sharkey | eba260d | 2015-04-19 14:35:16 -0700 | [diff] [blame] | 2118 | final CountDownLatch latch = findOrCreateDiskScanLatch(diskId); |
Jeff Sharkey | 7151a9a | 2015-04-04 15:22:37 -0700 | [diff] [blame] | 2119 | try { |
Jeff Sharkey | 5440279 | 2017-09-15 16:05:19 -0600 | [diff] [blame] | 2120 | mVold.partition(diskId, IVold.PARTITION_TYPE_PRIVATE, -1); |
Jeff Sharkey | edcdaf6 | 2015-07-09 09:45:36 -0700 | [diff] [blame] | 2121 | waitForLatch(latch, "partitionPrivate", 3 * DateUtils.MINUTE_IN_MILLIS); |
Jeff Sharkey | ace874b | 2017-09-07 15:27:33 -0600 | [diff] [blame] | 2122 | } catch (Exception e) { |
| 2123 | Slog.wtf(TAG, e); |
Jeff Sharkey | 7151a9a | 2015-04-04 15:22:37 -0700 | [diff] [blame] | 2124 | } |
| 2125 | } |
| 2126 | |
| 2127 | @Override |
| 2128 | public void partitionMixed(String diskId, int ratio) { |
| 2129 | enforcePermission(android.Manifest.permission.MOUNT_FORMAT_FILESYSTEMS); |
Amith Yamasani | 462ac3a | 2015-06-30 14:21:01 -0700 | [diff] [blame] | 2130 | enforceAdminUser(); |
Jeff Sharkey | 7151a9a | 2015-04-04 15:22:37 -0700 | [diff] [blame] | 2131 | |
Jeff Sharkey | eba260d | 2015-04-19 14:35:16 -0700 | [diff] [blame] | 2132 | final CountDownLatch latch = findOrCreateDiskScanLatch(diskId); |
Jeff Sharkey | 7151a9a | 2015-04-04 15:22:37 -0700 | [diff] [blame] | 2133 | try { |
Jeff Sharkey | 5440279 | 2017-09-15 16:05:19 -0600 | [diff] [blame] | 2134 | mVold.partition(diskId, IVold.PARTITION_TYPE_MIXED, ratio); |
Jeff Sharkey | edcdaf6 | 2015-07-09 09:45:36 -0700 | [diff] [blame] | 2135 | waitForLatch(latch, "partitionMixed", 3 * DateUtils.MINUTE_IN_MILLIS); |
Jeff Sharkey | ace874b | 2017-09-07 15:27:33 -0600 | [diff] [blame] | 2136 | } catch (Exception e) { |
| 2137 | Slog.wtf(TAG, e); |
Jeff Sharkey | 7151a9a | 2015-04-04 15:22:37 -0700 | [diff] [blame] | 2138 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2139 | } |
| 2140 | |
Jeff Sharkey | 4887789 | 2015-03-18 11:27:19 -0700 | [diff] [blame] | 2141 | @Override |
Jeff Sharkey | b36586a | 2015-04-27 08:42:28 -0700 | [diff] [blame] | 2142 | public void setVolumeNickname(String fsUuid, String nickname) { |
Jeff Sharkey | d95d3bf | 2015-04-14 21:39:44 -0700 | [diff] [blame] | 2143 | enforcePermission(android.Manifest.permission.MOUNT_UNMOUNT_FILESYSTEMS); |
Jeff Sharkey | d95d3bf | 2015-04-14 21:39:44 -0700 | [diff] [blame] | 2144 | |
Daulet Zhanguzin | ea1a7ca | 2020-01-03 09:46:50 +0000 | [diff] [blame] | 2145 | Objects.requireNonNull(fsUuid); |
Jeff Sharkey | d95d3bf | 2015-04-14 21:39:44 -0700 | [diff] [blame] | 2146 | synchronized (mLock) { |
Jeff Sharkey | b36586a | 2015-04-27 08:42:28 -0700 | [diff] [blame] | 2147 | final VolumeRecord rec = mRecords.get(fsUuid); |
| 2148 | rec.nickname = nickname; |
Jeff Sharkey | 50a0545 | 2015-04-29 11:24:52 -0700 | [diff] [blame] | 2149 | mCallbacks.notifyVolumeRecordChanged(rec); |
Jeff Sharkey | b36586a | 2015-04-27 08:42:28 -0700 | [diff] [blame] | 2150 | writeSettingsLocked(); |
Jeff Sharkey | d95d3bf | 2015-04-14 21:39:44 -0700 | [diff] [blame] | 2151 | } |
| 2152 | } |
| 2153 | |
| 2154 | @Override |
Jeff Sharkey | b36586a | 2015-04-27 08:42:28 -0700 | [diff] [blame] | 2155 | public void setVolumeUserFlags(String fsUuid, int flags, int mask) { |
Jeff Sharkey | d95d3bf | 2015-04-14 21:39:44 -0700 | [diff] [blame] | 2156 | enforcePermission(android.Manifest.permission.MOUNT_UNMOUNT_FILESYSTEMS); |
Jeff Sharkey | d95d3bf | 2015-04-14 21:39:44 -0700 | [diff] [blame] | 2157 | |
Daulet Zhanguzin | ea1a7ca | 2020-01-03 09:46:50 +0000 | [diff] [blame] | 2158 | Objects.requireNonNull(fsUuid); |
Jeff Sharkey | d95d3bf | 2015-04-14 21:39:44 -0700 | [diff] [blame] | 2159 | synchronized (mLock) { |
Jeff Sharkey | b36586a | 2015-04-27 08:42:28 -0700 | [diff] [blame] | 2160 | final VolumeRecord rec = mRecords.get(fsUuid); |
| 2161 | rec.userFlags = (rec.userFlags & ~mask) | (flags & mask); |
Jeff Sharkey | 50a0545 | 2015-04-29 11:24:52 -0700 | [diff] [blame] | 2162 | mCallbacks.notifyVolumeRecordChanged(rec); |
Jeff Sharkey | b36586a | 2015-04-27 08:42:28 -0700 | [diff] [blame] | 2163 | writeSettingsLocked(); |
| 2164 | } |
| 2165 | } |
| 2166 | |
| 2167 | @Override |
| 2168 | public void forgetVolume(String fsUuid) { |
| 2169 | enforcePermission(android.Manifest.permission.MOUNT_UNMOUNT_FILESYSTEMS); |
Jeff Sharkey | b36586a | 2015-04-27 08:42:28 -0700 | [diff] [blame] | 2170 | |
Daulet Zhanguzin | ea1a7ca | 2020-01-03 09:46:50 +0000 | [diff] [blame] | 2171 | Objects.requireNonNull(fsUuid); |
Jeff Sharkey | 5a9bb74 | 2015-11-03 10:15:57 -0800 | [diff] [blame] | 2172 | |
Jeff Sharkey | b36586a | 2015-04-27 08:42:28 -0700 | [diff] [blame] | 2173 | synchronized (mLock) { |
Jeff Sharkey | 5cc0df2 | 2015-06-17 19:44:05 -0700 | [diff] [blame] | 2174 | final VolumeRecord rec = mRecords.remove(fsUuid); |
| 2175 | if (rec != null && !TextUtils.isEmpty(rec.partGuid)) { |
Jeff Sharkey | db4b619 | 2017-10-24 11:08:50 -0600 | [diff] [blame] | 2176 | mHandler.obtainMessage(H_PARTITION_FORGET, rec).sendToTarget(); |
Jeff Sharkey | 5cc0df2 | 2015-06-17 19:44:05 -0700 | [diff] [blame] | 2177 | } |
| 2178 | mCallbacks.notifyVolumeForgotten(fsUuid); |
Jeff Sharkey | 50a0545 | 2015-04-29 11:24:52 -0700 | [diff] [blame] | 2179 | |
| 2180 | // If this had been primary storage, revert back to internal and |
| 2181 | // reset vold so we bind into new volume into place. |
| 2182 | if (Objects.equals(mPrimaryStorageUuid, fsUuid)) { |
Jeff Sharkey | fced534 | 2015-05-10 14:53:34 -0700 | [diff] [blame] | 2183 | mPrimaryStorageUuid = getDefaultPrimaryStorageUuid(); |
Jeff Sharkey | 5a9bb74 | 2015-11-03 10:15:57 -0800 | [diff] [blame] | 2184 | mHandler.obtainMessage(H_RESET).sendToTarget(); |
Jeff Sharkey | 50a0545 | 2015-04-29 11:24:52 -0700 | [diff] [blame] | 2185 | } |
| 2186 | |
Jeff Sharkey | b36586a | 2015-04-27 08:42:28 -0700 | [diff] [blame] | 2187 | writeSettingsLocked(); |
Jeff Sharkey | d95d3bf | 2015-04-14 21:39:44 -0700 | [diff] [blame] | 2188 | } |
| 2189 | } |
| 2190 | |
Jeff Sharkey | 7d2af54 | 2015-05-12 15:27:15 -0700 | [diff] [blame] | 2191 | @Override |
| 2192 | public void forgetAllVolumes() { |
| 2193 | enforcePermission(android.Manifest.permission.MOUNT_UNMOUNT_FILESYSTEMS); |
Jeff Sharkey | 7d2af54 | 2015-05-12 15:27:15 -0700 | [diff] [blame] | 2194 | |
Jeff Sharkey | 50a0545 | 2015-04-29 11:24:52 -0700 | [diff] [blame] | 2195 | synchronized (mLock) { |
| 2196 | for (int i = 0; i < mRecords.size(); i++) { |
| 2197 | final String fsUuid = mRecords.keyAt(i); |
Jeff Sharkey | 5cc0df2 | 2015-06-17 19:44:05 -0700 | [diff] [blame] | 2198 | final VolumeRecord rec = mRecords.valueAt(i); |
| 2199 | if (!TextUtils.isEmpty(rec.partGuid)) { |
Jeff Sharkey | db4b619 | 2017-10-24 11:08:50 -0600 | [diff] [blame] | 2200 | mHandler.obtainMessage(H_PARTITION_FORGET, rec).sendToTarget(); |
Jeff Sharkey | 5cc0df2 | 2015-06-17 19:44:05 -0700 | [diff] [blame] | 2201 | } |
Jeff Sharkey | 50a0545 | 2015-04-29 11:24:52 -0700 | [diff] [blame] | 2202 | mCallbacks.notifyVolumeForgotten(fsUuid); |
| 2203 | } |
Jeff Sharkey | 50a0545 | 2015-04-29 11:24:52 -0700 | [diff] [blame] | 2204 | mRecords.clear(); |
Jeff Sharkey | 50a0545 | 2015-04-29 11:24:52 -0700 | [diff] [blame] | 2205 | |
Jeff Sharkey | fced534 | 2015-05-10 14:53:34 -0700 | [diff] [blame] | 2206 | if (!Objects.equals(StorageManager.UUID_PRIVATE_INTERNAL, mPrimaryStorageUuid)) { |
| 2207 | mPrimaryStorageUuid = getDefaultPrimaryStorageUuid(); |
| 2208 | } |
| 2209 | |
| 2210 | writeSettingsLocked(); |
Jeff Sharkey | 5a9bb74 | 2015-11-03 10:15:57 -0800 | [diff] [blame] | 2211 | mHandler.obtainMessage(H_RESET).sendToTarget(); |
Jeff Sharkey | 50a0545 | 2015-04-29 11:24:52 -0700 | [diff] [blame] | 2212 | } |
| 2213 | } |
| 2214 | |
Jeff Sharkey | db4b619 | 2017-10-24 11:08:50 -0600 | [diff] [blame] | 2215 | private void forgetPartition(String partGuid, String fsUuid) { |
Jeff Sharkey | 5cc0df2 | 2015-06-17 19:44:05 -0700 | [diff] [blame] | 2216 | try { |
Jeff Sharkey | db4b619 | 2017-10-24 11:08:50 -0600 | [diff] [blame] | 2217 | mVold.forgetPartition(partGuid, fsUuid); |
Jeff Sharkey | ace874b | 2017-09-07 15:27:33 -0600 | [diff] [blame] | 2218 | } catch (Exception e) { |
| 2219 | Slog.wtf(TAG, e); |
Jeff Sharkey | 5cc0df2 | 2015-06-17 19:44:05 -0700 | [diff] [blame] | 2220 | } |
| 2221 | } |
| 2222 | |
Jeff Sharkey | 31d0b70 | 2016-11-21 14:16:53 -0700 | [diff] [blame] | 2223 | @Override |
Jeff Sharkey | 7e19f53 | 2017-11-06 13:54:11 -0700 | [diff] [blame] | 2224 | public void fstrim(int flags, IVoldTaskListener listener) { |
Jeff Sharkey | 31d0b70 | 2016-11-21 14:16:53 -0700 | [diff] [blame] | 2225 | enforcePermission(android.Manifest.permission.MOUNT_FORMAT_FILESYSTEMS); |
Jeff Sharkey | 31d0b70 | 2016-11-21 14:16:53 -0700 | [diff] [blame] | 2226 | |
| 2227 | try { |
Sandeep Patil | 7d5c491 | 2019-04-15 09:27:30 -0700 | [diff] [blame] | 2228 | // Block based checkpoint process runs fstrim. So, if checkpoint is in progress |
| 2229 | // (first boot after OTA), We skip idle maintenance and make sure the last |
| 2230 | // fstrim time is still updated. If file based checkpoints are used, we run |
| 2231 | // idle maintenance (GC + fstrim) regardless of checkpoint status. |
| 2232 | if (!needsCheckpoint() || !supportsBlockCheckpoint()) { |
| 2233 | mVold.fstrim(flags, new IVoldTaskListener.Stub() { |
| 2234 | @Override |
| 2235 | public void onStatus(int status, PersistableBundle extras) { |
| 2236 | dispatchOnStatus(listener, status, extras); |
Jeff Sharkey | 7e19f53 | 2017-11-06 13:54:11 -0700 | [diff] [blame] | 2237 | |
Sandeep Patil | 7d5c491 | 2019-04-15 09:27:30 -0700 | [diff] [blame] | 2238 | // Ignore trim failures |
| 2239 | if (status != 0) return; |
Jeff Sharkey | b302c54 | 2017-09-15 12:57:59 -0600 | [diff] [blame] | 2240 | |
Sandeep Patil | 7d5c491 | 2019-04-15 09:27:30 -0700 | [diff] [blame] | 2241 | final String path = extras.getString("path"); |
| 2242 | final long bytes = extras.getLong("bytes"); |
| 2243 | final long time = extras.getLong("time"); |
Jeff Sharkey | b302c54 | 2017-09-15 12:57:59 -0600 | [diff] [blame] | 2244 | |
Sandeep Patil | 7d5c491 | 2019-04-15 09:27:30 -0700 | [diff] [blame] | 2245 | final DropBoxManager dropBox = mContext.getSystemService(DropBoxManager.class); |
| 2246 | dropBox.addText(TAG_STORAGE_TRIM, scrubPath(path) + " " + bytes + " " + time); |
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 | synchronized (mLock) { |
| 2249 | final VolumeRecord rec = findRecordForPath(path); |
| 2250 | if (rec != null) { |
| 2251 | rec.lastTrimMillis = System.currentTimeMillis(); |
| 2252 | writeSettingsLocked(); |
| 2253 | } |
Jeff Sharkey | b302c54 | 2017-09-15 12:57:59 -0600 | [diff] [blame] | 2254 | } |
| 2255 | } |
Jeff Sharkey | b302c54 | 2017-09-15 12:57:59 -0600 | [diff] [blame] | 2256 | |
Sandeep Patil | 7d5c491 | 2019-04-15 09:27:30 -0700 | [diff] [blame] | 2257 | @Override |
| 2258 | public void onFinished(int status, PersistableBundle extras) { |
| 2259 | dispatchOnFinished(listener, status, extras); |
Jeff Sharkey | 7e19f53 | 2017-11-06 13:54:11 -0700 | [diff] [blame] | 2260 | |
Sandeep Patil | 7d5c491 | 2019-04-15 09:27:30 -0700 | [diff] [blame] | 2261 | // TODO: benchmark when desired |
| 2262 | } |
| 2263 | }); |
| 2264 | } else { |
| 2265 | Slog.i(TAG, "Skipping fstrim - block based checkpoint in progress"); |
| 2266 | } |
Jeff Sharkey | 7e19f53 | 2017-11-06 13:54:11 -0700 | [diff] [blame] | 2267 | } catch (RemoteException e) { |
| 2268 | throw e.rethrowAsRuntimeException(); |
Jeff Sharkey | 31d0b70 | 2016-11-21 14:16:53 -0700 | [diff] [blame] | 2269 | } |
| 2270 | } |
| 2271 | |
Jin Qian | a85b991 | 2017-10-17 15:48:18 -0700 | [diff] [blame] | 2272 | void runIdleMaint(Runnable callback) { |
| 2273 | enforcePermission(android.Manifest.permission.MOUNT_FORMAT_FILESYSTEMS); |
| 2274 | |
| 2275 | try { |
Sandeep Patil | 7d5c491 | 2019-04-15 09:27:30 -0700 | [diff] [blame] | 2276 | // Block based checkpoint process runs fstrim. So, if checkpoint is in progress |
| 2277 | // (first boot after OTA), We skip idle maintenance and make sure the last |
| 2278 | // fstrim time is still updated. If file based checkpoints are used, we run |
| 2279 | // idle maintenance (GC + fstrim) regardless of checkpoint status. |
| 2280 | if (!needsCheckpoint() || !supportsBlockCheckpoint()) { |
| 2281 | mVold.runIdleMaint(new IVoldTaskListener.Stub() { |
| 2282 | @Override |
| 2283 | public void onStatus(int status, PersistableBundle extras) { |
| 2284 | // Not currently used |
Jin Qian | a85b991 | 2017-10-17 15:48:18 -0700 | [diff] [blame] | 2285 | } |
Sandeep Patil | 7d5c491 | 2019-04-15 09:27:30 -0700 | [diff] [blame] | 2286 | @Override |
| 2287 | public void onFinished(int status, PersistableBundle extras) { |
| 2288 | if (callback != null) { |
| 2289 | BackgroundThread.getHandler().post(callback); |
| 2290 | } |
| 2291 | } |
| 2292 | }); |
| 2293 | } else { |
| 2294 | Slog.i(TAG, "Skipping idle maintenance - block based checkpoint in progress"); |
| 2295 | } |
Jin Qian | a85b991 | 2017-10-17 15:48:18 -0700 | [diff] [blame] | 2296 | } catch (Exception e) { |
| 2297 | Slog.wtf(TAG, e); |
| 2298 | } |
| 2299 | } |
| 2300 | |
| 2301 | @Override |
| 2302 | public void runIdleMaintenance() { |
| 2303 | runIdleMaint(null); |
| 2304 | } |
| 2305 | |
| 2306 | void abortIdleMaint(Runnable callback) { |
| 2307 | enforcePermission(android.Manifest.permission.MOUNT_FORMAT_FILESYSTEMS); |
| 2308 | |
| 2309 | try { |
| 2310 | mVold.abortIdleMaint(new IVoldTaskListener.Stub() { |
| 2311 | @Override |
| 2312 | public void onStatus(int status, PersistableBundle extras) { |
| 2313 | // Not currently used |
| 2314 | } |
| 2315 | @Override |
| 2316 | public void onFinished(int status, PersistableBundle extras) { |
| 2317 | if (callback != null) { |
| 2318 | BackgroundThread.getHandler().post(callback); |
| 2319 | } |
| 2320 | } |
| 2321 | }); |
| 2322 | } catch (Exception e) { |
| 2323 | Slog.wtf(TAG, e); |
| 2324 | } |
| 2325 | } |
| 2326 | |
| 2327 | @Override |
| 2328 | public void abortIdleMaintenance() { |
| 2329 | abortIdleMaint(null); |
| 2330 | } |
| 2331 | |
Svet Ganov | 6ee871e | 2015-07-10 14:29:33 -0700 | [diff] [blame] | 2332 | private void remountUidExternalStorage(int uid, int mode) { |
Zim | 4f1a24d | 2019-11-27 16:35:58 +0000 | [diff] [blame] | 2333 | if (uid == Process.SYSTEM_UID) { |
| 2334 | // No need to remount uid for system because it has all access anyways |
| 2335 | return; |
| 2336 | } |
| 2337 | |
Jeff Sharkey | 9527b22 | 2015-06-24 15:24:48 -0700 | [diff] [blame] | 2338 | try { |
Jeff Sharkey | 5440279 | 2017-09-15 16:05:19 -0600 | [diff] [blame] | 2339 | mVold.remountUid(uid, mode); |
Jeff Sharkey | ace874b | 2017-09-07 15:27:33 -0600 | [diff] [blame] | 2340 | } catch (Exception e) { |
| 2341 | Slog.wtf(TAG, e); |
Jeff Sharkey | 9527b22 | 2015-06-24 15:24:48 -0700 | [diff] [blame] | 2342 | } |
| 2343 | } |
| 2344 | |
| 2345 | @Override |
Jeff Sharkey | 4c099d0 | 2015-05-15 13:45:00 -0700 | [diff] [blame] | 2346 | public void setDebugFlags(int flags, int mask) { |
| 2347 | enforcePermission(android.Manifest.permission.MOUNT_UNMOUNT_FILESYSTEMS); |
Jeff Sharkey | 4c099d0 | 2015-05-15 13:45:00 -0700 | [diff] [blame] | 2348 | |
Jeff Sharkey | ba51235 | 2015-11-12 20:17:45 -0800 | [diff] [blame] | 2349 | if ((mask & StorageManager.DEBUG_EMULATE_FBE) != 0) { |
Jeff Sharkey | 00455bf | 2016-11-04 14:45:24 -0600 | [diff] [blame] | 2350 | if (!EMULATE_FBE_SUPPORTED) { |
| 2351 | throw new IllegalStateException( |
| 2352 | "Emulation not supported on this device"); |
| 2353 | } |
Paul Lawrence | 20be5d6 | 2016-02-26 13:51:17 -0800 | [diff] [blame] | 2354 | if (StorageManager.isFileEncryptedNativeOnly()) { |
Jeff Sharkey | ce14cd0 | 2015-12-07 15:35:42 -0700 | [diff] [blame] | 2355 | throw new IllegalStateException( |
Jeff Sharkey | 00455bf | 2016-11-04 14:45:24 -0600 | [diff] [blame] | 2356 | "Emulation not supported on device with native FBE"); |
Jeff Sharkey | ce14cd0 | 2015-12-07 15:35:42 -0700 | [diff] [blame] | 2357 | } |
Jeff Sharkey | 5a78516 | 2016-03-21 13:02:06 -0600 | [diff] [blame] | 2358 | if (mLockPatternUtils.isCredentialRequiredToDecrypt(false)) { |
| 2359 | throw new IllegalStateException( |
| 2360 | "Emulation requires disabling 'Secure start-up' in Settings > Security"); |
| 2361 | } |
Jeff Sharkey | ce14cd0 | 2015-12-07 15:35:42 -0700 | [diff] [blame] | 2362 | |
Jeff Sharkey | 1176e51 | 2016-02-29 17:01:26 -0700 | [diff] [blame] | 2363 | final long token = Binder.clearCallingIdentity(); |
| 2364 | try { |
| 2365 | final boolean emulateFbe = (flags & StorageManager.DEBUG_EMULATE_FBE) != 0; |
| 2366 | SystemProperties.set(StorageManager.PROP_EMULATE_FBE, Boolean.toString(emulateFbe)); |
Jeff Sharkey | ce14cd0 | 2015-12-07 15:35:42 -0700 | [diff] [blame] | 2367 | |
Jeff Sharkey | 1176e51 | 2016-02-29 17:01:26 -0700 | [diff] [blame] | 2368 | // Perform hard reboot to kick policy into place |
| 2369 | mContext.getSystemService(PowerManager.class).reboot(null); |
| 2370 | } finally { |
| 2371 | Binder.restoreCallingIdentity(token); |
| 2372 | } |
Jeff Sharkey | ba51235 | 2015-11-12 20:17:45 -0800 | [diff] [blame] | 2373 | } |
Jeff Sharkey | 4c099d0 | 2015-05-15 13:45:00 -0700 | [diff] [blame] | 2374 | |
Jeff Sharkey | 901c042 | 2018-04-20 13:11:20 -0600 | [diff] [blame] | 2375 | if ((mask & (StorageManager.DEBUG_ADOPTABLE_FORCE_ON |
| 2376 | | StorageManager.DEBUG_ADOPTABLE_FORCE_OFF)) != 0) { |
| 2377 | final String value; |
| 2378 | if ((flags & StorageManager.DEBUG_ADOPTABLE_FORCE_ON) != 0) { |
| 2379 | value = "force_on"; |
| 2380 | } else if ((flags & StorageManager.DEBUG_ADOPTABLE_FORCE_OFF) != 0) { |
| 2381 | value = "force_off"; |
| 2382 | } else { |
| 2383 | value = ""; |
| 2384 | } |
Jeff Sharkey | ba51235 | 2015-11-12 20:17:45 -0800 | [diff] [blame] | 2385 | |
Jeff Sharkey | 901c042 | 2018-04-20 13:11:20 -0600 | [diff] [blame] | 2386 | final long token = Binder.clearCallingIdentity(); |
| 2387 | try { |
| 2388 | SystemProperties.set(StorageManager.PROP_ADOPTABLE, value); |
| 2389 | |
| 2390 | // Reset storage to kick new setting into place |
Jeff Sharkey | ba51235 | 2015-11-12 20:17:45 -0800 | [diff] [blame] | 2391 | mHandler.obtainMessage(H_RESET).sendToTarget(); |
Jeff Sharkey | 901c042 | 2018-04-20 13:11:20 -0600 | [diff] [blame] | 2392 | } finally { |
| 2393 | Binder.restoreCallingIdentity(token); |
Jeff Sharkey | ba51235 | 2015-11-12 20:17:45 -0800 | [diff] [blame] | 2394 | } |
Jeff Sharkey | 4c099d0 | 2015-05-15 13:45:00 -0700 | [diff] [blame] | 2395 | } |
Jeff Sharkey | 33dd156 | 2016-04-07 11:05:33 -0600 | [diff] [blame] | 2396 | |
| 2397 | if ((mask & (StorageManager.DEBUG_SDCARDFS_FORCE_ON |
| 2398 | | StorageManager.DEBUG_SDCARDFS_FORCE_OFF)) != 0) { |
| 2399 | final String value; |
| 2400 | if ((flags & StorageManager.DEBUG_SDCARDFS_FORCE_ON) != 0) { |
| 2401 | value = "force_on"; |
| 2402 | } else if ((flags & StorageManager.DEBUG_SDCARDFS_FORCE_OFF) != 0) { |
| 2403 | value = "force_off"; |
| 2404 | } else { |
| 2405 | value = ""; |
| 2406 | } |
| 2407 | |
| 2408 | final long token = Binder.clearCallingIdentity(); |
| 2409 | try { |
| 2410 | SystemProperties.set(StorageManager.PROP_SDCARDFS, value); |
| 2411 | |
| 2412 | // Reset storage to kick new setting into place |
| 2413 | mHandler.obtainMessage(H_RESET).sendToTarget(); |
| 2414 | } finally { |
| 2415 | Binder.restoreCallingIdentity(token); |
| 2416 | } |
| 2417 | } |
Jeff Sharkey | e53e2d9 | 2017-03-25 23:14:06 -0600 | [diff] [blame] | 2418 | |
| 2419 | if ((mask & StorageManager.DEBUG_VIRTUAL_DISK) != 0) { |
| 2420 | final boolean enabled = (flags & StorageManager.DEBUG_VIRTUAL_DISK) != 0; |
| 2421 | |
| 2422 | final long token = Binder.clearCallingIdentity(); |
| 2423 | try { |
| 2424 | SystemProperties.set(StorageManager.PROP_VIRTUAL_DISK, Boolean.toString(enabled)); |
| 2425 | |
| 2426 | // Reset storage to kick new setting into place |
| 2427 | mHandler.obtainMessage(H_RESET).sendToTarget(); |
| 2428 | } finally { |
| 2429 | Binder.restoreCallingIdentity(token); |
| 2430 | } |
| 2431 | } |
Sudheer Shanka | be0febe | 2018-11-07 18:24:37 -0800 | [diff] [blame] | 2432 | |
Jeff Sharkey | b0c363b2 | 2018-12-15 11:53:03 -0700 | [diff] [blame] | 2433 | if ((mask & (StorageManager.DEBUG_ISOLATED_STORAGE_FORCE_ON |
| 2434 | | StorageManager.DEBUG_ISOLATED_STORAGE_FORCE_OFF)) != 0) { |
| 2435 | final int value; |
| 2436 | if ((flags & StorageManager.DEBUG_ISOLATED_STORAGE_FORCE_ON) != 0) { |
| 2437 | value = 1; |
| 2438 | } else if ((flags & StorageManager.DEBUG_ISOLATED_STORAGE_FORCE_OFF) != 0) { |
| 2439 | value = -1; |
| 2440 | } else { |
| 2441 | value = 0; |
| 2442 | } |
Sudheer Shanka | be0febe | 2018-11-07 18:24:37 -0800 | [diff] [blame] | 2443 | |
| 2444 | final long token = Binder.clearCallingIdentity(); |
| 2445 | try { |
Jeff Sharkey | b0c363b2 | 2018-12-15 11:53:03 -0700 | [diff] [blame] | 2446 | Settings.Global.putInt(mContext.getContentResolver(), |
| 2447 | Settings.Global.ISOLATED_STORAGE_LOCAL, value); |
| 2448 | refreshIsolatedStorageSettings(); |
Sudheer Shanka | be0febe | 2018-11-07 18:24:37 -0800 | [diff] [blame] | 2449 | |
| 2450 | // Perform hard reboot to kick policy into place |
Jeff Sharkey | 3627499 | 2019-02-27 12:09:57 -0700 | [diff] [blame] | 2451 | mHandler.post(() -> { |
| 2452 | mContext.getSystemService(PowerManager.class).reboot(null); |
| 2453 | }); |
| 2454 | } finally { |
| 2455 | Binder.restoreCallingIdentity(token); |
| 2456 | } |
| 2457 | } |
Jeff Sharkey | 4c099d0 | 2015-05-15 13:45:00 -0700 | [diff] [blame] | 2458 | } |
| 2459 | |
| 2460 | @Override |
Jeff Sharkey | 275e3e4 | 2015-04-24 16:10:32 -0700 | [diff] [blame] | 2461 | public String getPrimaryStorageUuid() { |
Jeff Sharkey | 620b32b | 2015-04-23 19:36:02 -0700 | [diff] [blame] | 2462 | synchronized (mLock) { |
| 2463 | return mPrimaryStorageUuid; |
| 2464 | } |
| 2465 | } |
| 2466 | |
| 2467 | @Override |
Jeff Sharkey | 275e3e4 | 2015-04-24 16:10:32 -0700 | [diff] [blame] | 2468 | public void setPrimaryStorageUuid(String volumeUuid, IPackageMoveObserver callback) { |
| 2469 | enforcePermission(android.Manifest.permission.MOUNT_UNMOUNT_FILESYSTEMS); |
Jeff Sharkey | 620b32b | 2015-04-23 19:36:02 -0700 | [diff] [blame] | 2470 | |
Jeff Sharkey | a31460c | 2016-06-22 09:04:33 -0600 | [diff] [blame] | 2471 | final VolumeInfo from; |
| 2472 | final VolumeInfo to; |
| 2473 | |
Jeff Sharkey | 275e3e4 | 2015-04-24 16:10:32 -0700 | [diff] [blame] | 2474 | synchronized (mLock) { |
Jeff Sharkey | fced534 | 2015-05-10 14:53:34 -0700 | [diff] [blame] | 2475 | if (Objects.equals(mPrimaryStorageUuid, volumeUuid)) { |
| 2476 | throw new IllegalArgumentException("Primary storage already at " + volumeUuid); |
Jeff Sharkey | 275e3e4 | 2015-04-24 16:10:32 -0700 | [diff] [blame] | 2477 | } |
| 2478 | |
| 2479 | if (mMoveCallback != null) { |
| 2480 | throw new IllegalStateException("Move already in progress"); |
| 2481 | } |
| 2482 | mMoveCallback = callback; |
| 2483 | mMoveTargetUuid = volumeUuid; |
| 2484 | |
Jeff Sharkey | a65e649 | 2017-06-21 13:45:11 -0600 | [diff] [blame] | 2485 | // We need all the users unlocked to move their primary storage |
| 2486 | final List<UserInfo> users = mContext.getSystemService(UserManager.class).getUsers(); |
| 2487 | for (UserInfo user : users) { |
| 2488 | if (StorageManager.isFileEncryptedNativeOrEmulated() |
| 2489 | && !isUserKeyUnlocked(user.id)) { |
| 2490 | Slog.w(TAG, "Failing move due to locked user " + user.id); |
| 2491 | onMoveStatusLocked(PackageManager.MOVE_FAILED_LOCKED_USER); |
| 2492 | return; |
| 2493 | } |
| 2494 | } |
| 2495 | |
Jeff Sharkey | fced534 | 2015-05-10 14:53:34 -0700 | [diff] [blame] | 2496 | // When moving to/from primary physical volume, we probably just nuked |
| 2497 | // the current storage location, so we have nothing to move. |
| 2498 | if (Objects.equals(StorageManager.UUID_PRIMARY_PHYSICAL, mPrimaryStorageUuid) |
| 2499 | || Objects.equals(StorageManager.UUID_PRIMARY_PHYSICAL, volumeUuid)) { |
| 2500 | Slog.d(TAG, "Skipping move to/from primary physical"); |
| 2501 | onMoveStatusLocked(MOVE_STATUS_COPY_FINISHED); |
| 2502 | onMoveStatusLocked(PackageManager.MOVE_SUCCEEDED); |
Jeff Sharkey | 5a9bb74 | 2015-11-03 10:15:57 -0800 | [diff] [blame] | 2503 | mHandler.obtainMessage(H_RESET).sendToTarget(); |
Jeff Sharkey | a31460c | 2016-06-22 09:04:33 -0600 | [diff] [blame] | 2504 | return; |
Jeff Sharkey | fced534 | 2015-05-10 14:53:34 -0700 | [diff] [blame] | 2505 | |
| 2506 | } else { |
Jeff Sharkey | a31460c | 2016-06-22 09:04:33 -0600 | [diff] [blame] | 2507 | from = findStorageForUuid(mPrimaryStorageUuid); |
| 2508 | to = findStorageForUuid(volumeUuid); |
Jeff Sharkey | ef10ee0 | 2015-07-05 14:17:27 -0700 | [diff] [blame] | 2509 | |
| 2510 | if (from == null) { |
| 2511 | Slog.w(TAG, "Failing move due to missing from volume " + mPrimaryStorageUuid); |
| 2512 | onMoveStatusLocked(PackageManager.MOVE_FAILED_INTERNAL_ERROR); |
| 2513 | return; |
| 2514 | } else if (to == null) { |
| 2515 | Slog.w(TAG, "Failing move due to missing to volume " + volumeUuid); |
| 2516 | onMoveStatusLocked(PackageManager.MOVE_FAILED_INTERNAL_ERROR); |
| 2517 | return; |
| 2518 | } |
Jeff Sharkey | 275e3e4 | 2015-04-24 16:10:32 -0700 | [diff] [blame] | 2519 | } |
Jeff Sharkey | 620b32b | 2015-04-23 19:36:02 -0700 | [diff] [blame] | 2520 | } |
Jeff Sharkey | a31460c | 2016-06-22 09:04:33 -0600 | [diff] [blame] | 2521 | |
| 2522 | try { |
Jeff Sharkey | b302c54 | 2017-09-15 12:57:59 -0600 | [diff] [blame] | 2523 | mVold.moveStorage(from.id, to.id, new IVoldTaskListener.Stub() { |
| 2524 | @Override |
| 2525 | public void onStatus(int status, PersistableBundle extras) { |
| 2526 | synchronized (mLock) { |
| 2527 | onMoveStatusLocked(status); |
| 2528 | } |
| 2529 | } |
| 2530 | |
| 2531 | @Override |
| 2532 | public void onFinished(int status, PersistableBundle extras) { |
| 2533 | // Not currently used |
| 2534 | } |
| 2535 | }); |
Jeff Sharkey | ace874b | 2017-09-07 15:27:33 -0600 | [diff] [blame] | 2536 | } catch (Exception e) { |
| 2537 | Slog.wtf(TAG, e); |
Jeff Sharkey | a31460c | 2016-06-22 09:04:33 -0600 | [diff] [blame] | 2538 | } |
Jeff Sharkey | 620b32b | 2015-04-23 19:36:02 -0700 | [diff] [blame] | 2539 | } |
| 2540 | |
San Mehat | b104340 | 2010-02-05 08:26:50 -0800 | [diff] [blame] | 2541 | private void warnOnNotMounted() { |
Jeff Sharkey | 4887789 | 2015-03-18 11:27:19 -0700 | [diff] [blame] | 2542 | synchronized (mLock) { |
Jeff Sharkey | 1b8ef7e | 2015-04-03 17:14:45 -0700 | [diff] [blame] | 2543 | for (int i = 0; i < mVolumes.size(); i++) { |
| 2544 | final VolumeInfo vol = mVolumes.valueAt(i); |
Jeff Sharkey | 27de30d | 2015-04-18 16:20:27 -0700 | [diff] [blame] | 2545 | if (vol.isPrimary() && vol.isMountedWritable()) { |
Jeff Sharkey | 4887789 | 2015-03-18 11:27:19 -0700 | [diff] [blame] | 2546 | // Cool beans, we have a mounted primary volume |
| 2547 | return; |
| 2548 | } |
Jeff Sharkey | 32ee831 | 2012-09-30 13:21:31 -0700 | [diff] [blame] | 2549 | } |
San Mehat | b104340 | 2010-02-05 08:26:50 -0800 | [diff] [blame] | 2550 | } |
Jeff Sharkey | 4887789 | 2015-03-18 11:27:19 -0700 | [diff] [blame] | 2551 | |
| 2552 | Slog.w(TAG, "No primary storage mounted!"); |
San Mehat | b104340 | 2010-02-05 08:26:50 -0800 | [diff] [blame] | 2553 | } |
| 2554 | |
Kenny Root | a02b8b0 | 2010-08-05 16:14:17 -0700 | [diff] [blame] | 2555 | private boolean isUidOwnerOfPackageOrSystem(String packageName, int callerUid) { |
| 2556 | if (callerUid == android.os.Process.SYSTEM_UID) { |
| 2557 | return true; |
| 2558 | } |
| 2559 | |
Kenny Root | 02c8730 | 2010-07-01 08:10:18 -0700 | [diff] [blame] | 2560 | if (packageName == null) { |
| 2561 | return false; |
| 2562 | } |
| 2563 | |
Jeff Sharkey | 5790af0 | 2018-08-13 17:42:54 -0600 | [diff] [blame] | 2564 | final int packageUid = mPmInternal.getPackageUid(packageName, |
Jeff Sharkey | cd65448 | 2016-01-08 17:42:11 -0700 | [diff] [blame] | 2565 | PackageManager.MATCH_DEBUG_TRIAGED_MISSING, UserHandle.getUserId(callerUid)); |
Kenny Root | 02c8730 | 2010-07-01 08:10:18 -0700 | [diff] [blame] | 2566 | |
| 2567 | if (DEBUG_OBB) { |
| 2568 | Slog.d(TAG, "packageName = " + packageName + ", packageUid = " + |
| 2569 | packageUid + ", callerUid = " + callerUid); |
| 2570 | } |
| 2571 | |
| 2572 | return callerUid == packageUid; |
| 2573 | } |
| 2574 | |
Jeff Sharkey | 5440279 | 2017-09-15 16:05:19 -0600 | [diff] [blame] | 2575 | @Override |
Jeff Sharkey | 4fbbda4 | 2012-09-24 18:34:07 -0700 | [diff] [blame] | 2576 | public String getMountedObbPath(String rawPath) { |
Daulet Zhanguzin | ea1a7ca | 2020-01-03 09:46:50 +0000 | [diff] [blame] | 2577 | Objects.requireNonNull(rawPath, "rawPath cannot be null"); |
Kenny Root | af9d667 | 2010-10-08 09:21:39 -0700 | [diff] [blame] | 2578 | |
Kenny Root | 02c8730 | 2010-07-01 08:10:18 -0700 | [diff] [blame] | 2579 | warnOnNotMounted(); |
| 2580 | |
Jeff Sharkey | 4fbbda4 | 2012-09-24 18:34:07 -0700 | [diff] [blame] | 2581 | final ObbState state; |
Rubin Xu | cd7a014 | 2015-04-17 23:45:27 +0100 | [diff] [blame] | 2582 | synchronized (mObbMounts) { |
Jeff Sharkey | 4fbbda4 | 2012-09-24 18:34:07 -0700 | [diff] [blame] | 2583 | state = mObbPathToStateMap.get(rawPath); |
| 2584 | } |
| 2585 | if (state == null) { |
| 2586 | Slog.w(TAG, "Failed to find OBB mounted at " + rawPath); |
| 2587 | return null; |
| 2588 | } |
| 2589 | |
Jeff Sharkey | 5440279 | 2017-09-15 16:05:19 -0600 | [diff] [blame] | 2590 | return findVolumeByIdOrThrow(state.volId).getPath().getAbsolutePath(); |
Kenny Root | 02c8730 | 2010-07-01 08:10:18 -0700 | [diff] [blame] | 2591 | } |
| 2592 | |
Jeff Sharkey | 4fbbda4 | 2012-09-24 18:34:07 -0700 | [diff] [blame] | 2593 | @Override |
| 2594 | public boolean isObbMounted(String rawPath) { |
Daulet Zhanguzin | ea1a7ca | 2020-01-03 09:46:50 +0000 | [diff] [blame] | 2595 | Objects.requireNonNull(rawPath, "rawPath cannot be null"); |
Kenny Root | af9d667 | 2010-10-08 09:21:39 -0700 | [diff] [blame] | 2596 | synchronized (mObbMounts) { |
Jeff Sharkey | 4fbbda4 | 2012-09-24 18:34:07 -0700 | [diff] [blame] | 2597 | return mObbPathToStateMap.containsKey(rawPath); |
Kenny Root | af9d667 | 2010-10-08 09:21:39 -0700 | [diff] [blame] | 2598 | } |
Kenny Root | 02c8730 | 2010-07-01 08:10:18 -0700 | [diff] [blame] | 2599 | } |
| 2600 | |
Jeff Sharkey | 4fbbda4 | 2012-09-24 18:34:07 -0700 | [diff] [blame] | 2601 | @Override |
Sudheer Shanka | 25469aa | 2018-08-27 15:50:23 -0700 | [diff] [blame] | 2602 | public void mountObb(String rawPath, String canonicalPath, String key, |
| 2603 | IObbActionListener token, int nonce, ObbInfo obbInfo) { |
Daulet Zhanguzin | ea1a7ca | 2020-01-03 09:46:50 +0000 | [diff] [blame] | 2604 | Objects.requireNonNull(rawPath, "rawPath cannot be null"); |
| 2605 | Objects.requireNonNull(canonicalPath, "canonicalPath cannot be null"); |
| 2606 | Objects.requireNonNull(token, "token cannot be null"); |
| 2607 | Objects.requireNonNull(obbInfo, "obbIfno cannot be null"); |
Kenny Root | af9d667 | 2010-10-08 09:21:39 -0700 | [diff] [blame] | 2608 | |
Jeff Sharkey | 4fbbda4 | 2012-09-24 18:34:07 -0700 | [diff] [blame] | 2609 | final int callingUid = Binder.getCallingUid(); |
Jeff Sharkey | 41cd681 | 2017-09-11 10:32:17 -0600 | [diff] [blame] | 2610 | final ObbState obbState = new ObbState(rawPath, canonicalPath, |
| 2611 | callingUid, token, nonce, null); |
Sudheer Shanka | 25469aa | 2018-08-27 15:50:23 -0700 | [diff] [blame] | 2612 | final ObbAction action = new MountObbAction(obbState, key, callingUid, obbInfo); |
Kenny Root | a02b8b0 | 2010-08-05 16:14:17 -0700 | [diff] [blame] | 2613 | mObbActionHandler.sendMessage(mObbActionHandler.obtainMessage(OBB_RUN_ACTION, action)); |
| 2614 | |
| 2615 | if (DEBUG_OBB) |
| 2616 | Slog.i(TAG, "Send to OBB handler: " + action.toString()); |
Kenny Root | 02c8730 | 2010-07-01 08:10:18 -0700 | [diff] [blame] | 2617 | } |
| 2618 | |
Jeff Sharkey | 4fbbda4 | 2012-09-24 18:34:07 -0700 | [diff] [blame] | 2619 | @Override |
| 2620 | public void unmountObb(String rawPath, boolean force, IObbActionListener token, int nonce) { |
Daulet Zhanguzin | ea1a7ca | 2020-01-03 09:46:50 +0000 | [diff] [blame] | 2621 | Objects.requireNonNull(rawPath, "rawPath cannot be null"); |
Jeff Sharkey | 4fbbda4 | 2012-09-24 18:34:07 -0700 | [diff] [blame] | 2622 | |
| 2623 | final ObbState existingState; |
Rubin Xu | cd7a014 | 2015-04-17 23:45:27 +0100 | [diff] [blame] | 2624 | synchronized (mObbMounts) { |
Jeff Sharkey | 4fbbda4 | 2012-09-24 18:34:07 -0700 | [diff] [blame] | 2625 | existingState = mObbPathToStateMap.get(rawPath); |
Kenny Root | f1121dc | 2010-09-29 07:30:53 -0700 | [diff] [blame] | 2626 | } |
| 2627 | |
Jeff Sharkey | 4fbbda4 | 2012-09-24 18:34:07 -0700 | [diff] [blame] | 2628 | if (existingState != null) { |
| 2629 | // TODO: separate state object from request data |
| 2630 | final int callingUid = Binder.getCallingUid(); |
Jeff Sharkey | 41cd681 | 2017-09-11 10:32:17 -0600 | [diff] [blame] | 2631 | final ObbState newState = new ObbState(rawPath, existingState.canonicalPath, |
| 2632 | callingUid, token, nonce, existingState.volId); |
Jeff Sharkey | 4fbbda4 | 2012-09-24 18:34:07 -0700 | [diff] [blame] | 2633 | final ObbAction action = new UnmountObbAction(newState, force); |
| 2634 | mObbActionHandler.sendMessage(mObbActionHandler.obtainMessage(OBB_RUN_ACTION, action)); |
Kenny Root | 02c8730 | 2010-07-01 08:10:18 -0700 | [diff] [blame] | 2635 | |
Jeff Sharkey | 4fbbda4 | 2012-09-24 18:34:07 -0700 | [diff] [blame] | 2636 | if (DEBUG_OBB) |
| 2637 | Slog.i(TAG, "Send to OBB handler: " + action.toString()); |
| 2638 | } else { |
| 2639 | Slog.w(TAG, "Unknown OBB mount at " + rawPath); |
| 2640 | } |
Kenny Root | a02b8b0 | 2010-08-05 16:14:17 -0700 | [diff] [blame] | 2641 | } |
| 2642 | |
Ben Komalo | 444eca2 | 2011-09-01 15:17:44 -0700 | [diff] [blame] | 2643 | @Override |
| 2644 | public int getEncryptionState() { |
| 2645 | mContext.enforceCallingOrSelfPermission(Manifest.permission.CRYPT_KEEPER, |
| 2646 | "no permission to access the crypt keeper"); |
| 2647 | |
Ben Komalo | 444eca2 | 2011-09-01 15:17:44 -0700 | [diff] [blame] | 2648 | try { |
Jeff Sharkey | 5440279 | 2017-09-15 16:05:19 -0600 | [diff] [blame] | 2649 | return mVold.fdeComplete(); |
| 2650 | } catch (Exception e) { |
| 2651 | Slog.wtf(TAG, e); |
Sudheer Shanka | f734114 | 2016-10-18 17:15:18 -0700 | [diff] [blame] | 2652 | return StorageManager.ENCRYPTION_STATE_ERROR_UNKNOWN; |
Ben Komalo | 444eca2 | 2011-09-01 15:17:44 -0700 | [diff] [blame] | 2653 | } |
| 2654 | } |
| 2655 | |
| 2656 | @Override |
Jason parks | 5af0b91 | 2010-11-29 09:05:25 -0600 | [diff] [blame] | 2657 | public int decryptStorage(String password) { |
Jason parks | 8888c59 | 2011-01-20 22:46:41 -0600 | [diff] [blame] | 2658 | mContext.enforceCallingOrSelfPermission(Manifest.permission.CRYPT_KEEPER, |
| 2659 | "no permission to access the crypt keeper"); |
Jason parks | 5af0b91 | 2010-11-29 09:05:25 -0600 | [diff] [blame] | 2660 | |
Jeff Sharkey | 8058fe6 | 2017-09-13 11:50:33 -0600 | [diff] [blame] | 2661 | if (TextUtils.isEmpty(password)) { |
| 2662 | throw new IllegalArgumentException("password cannot be empty"); |
| 2663 | } |
| 2664 | |
Jason parks | 5af0b91 | 2010-11-29 09:05:25 -0600 | [diff] [blame] | 2665 | if (DEBUG_EVENTS) { |
| 2666 | Slog.i(TAG, "decrypting storage..."); |
| 2667 | } |
| 2668 | |
| 2669 | try { |
Jeff Sharkey | 5440279 | 2017-09-15 16:05:19 -0600 | [diff] [blame] | 2670 | mVold.fdeCheckPassword(password); |
| 2671 | mHandler.postDelayed(() -> { |
| 2672 | try { |
| 2673 | mVold.fdeRestart(); |
| 2674 | } catch (Exception e) { |
| 2675 | Slog.wtf(TAG, e); |
| 2676 | } |
| 2677 | }, DateUtils.SECOND_IN_MILLIS); |
| 2678 | return 0; |
Paul Crowley | fc0b519 | 2018-07-02 13:58:10 -0700 | [diff] [blame] | 2679 | } catch (ServiceSpecificException e) { |
| 2680 | Slog.e(TAG, "fdeCheckPassword failed", e); |
| 2681 | return e.errorCode; |
Jeff Sharkey | 5440279 | 2017-09-15 16:05:19 -0600 | [diff] [blame] | 2682 | } catch (Exception e) { |
| 2683 | Slog.wtf(TAG, e); |
| 2684 | return StorageManager.ENCRYPTION_STATE_ERROR_UNKNOWN; |
Jason parks | 5af0b91 | 2010-11-29 09:05:25 -0600 | [diff] [blame] | 2685 | } |
Jason parks | 5af0b91 | 2010-11-29 09:05:25 -0600 | [diff] [blame] | 2686 | } |
| 2687 | |
Jeff Sharkey | 5440279 | 2017-09-15 16:05:19 -0600 | [diff] [blame] | 2688 | @Override |
Paul Lawrence | 46791e7 | 2014-04-03 09:10:26 -0700 | [diff] [blame] | 2689 | public int encryptStorage(int type, String password) { |
Jason parks | 8888c59 | 2011-01-20 22:46:41 -0600 | [diff] [blame] | 2690 | mContext.enforceCallingOrSelfPermission(Manifest.permission.CRYPT_KEEPER, |
| 2691 | "no permission to access the crypt keeper"); |
Jason parks | 56aa532 | 2011-01-07 09:01:15 -0600 | [diff] [blame] | 2692 | |
Jeff Sharkey | 8058fe6 | 2017-09-13 11:50:33 -0600 | [diff] [blame] | 2693 | if (type == StorageManager.CRYPT_TYPE_DEFAULT) { |
| 2694 | password = ""; |
| 2695 | } else if (TextUtils.isEmpty(password)) { |
| 2696 | throw new IllegalArgumentException("password cannot be empty"); |
| 2697 | } |
| 2698 | |
Jason parks | 56aa532 | 2011-01-07 09:01:15 -0600 | [diff] [blame] | 2699 | if (DEBUG_EVENTS) { |
Jason parks | 8888c59 | 2011-01-20 22:46:41 -0600 | [diff] [blame] | 2700 | Slog.i(TAG, "encrypting storage..."); |
Jason parks | 56aa532 | 2011-01-07 09:01:15 -0600 | [diff] [blame] | 2701 | } |
| 2702 | |
| 2703 | try { |
Paul Lawrence | 89a0ae4 | 2017-12-22 10:11:10 -0800 | [diff] [blame] | 2704 | mVold.fdeEnable(type, password, 0); |
Jeff Sharkey | 43e1211 | 2017-09-12 16:31:45 -0600 | [diff] [blame] | 2705 | } catch (Exception e) { |
| 2706 | Slog.wtf(TAG, e); |
| 2707 | return -1; |
Jason parks | 56aa532 | 2011-01-07 09:01:15 -0600 | [diff] [blame] | 2708 | } |
| 2709 | |
| 2710 | return 0; |
| 2711 | } |
| 2712 | |
Paul Lawrence | 8e39736 | 2014-01-27 15:22:30 -0800 | [diff] [blame] | 2713 | /** Set the password for encrypting the master key. |
| 2714 | * @param type One of the CRYPTO_TYPE_XXX consts defined in StorageManager. |
| 2715 | * @param password The password to set. |
| 2716 | */ |
Jeff Sharkey | 5440279 | 2017-09-15 16:05:19 -0600 | [diff] [blame] | 2717 | @Override |
Paul Lawrence | 8e39736 | 2014-01-27 15:22:30 -0800 | [diff] [blame] | 2718 | public int changeEncryptionPassword(int type, String password) { |
Jason parks | f7b3cd4 | 2011-01-27 09:28:25 -0600 | [diff] [blame] | 2719 | mContext.enforceCallingOrSelfPermission(Manifest.permission.CRYPT_KEEPER, |
| 2720 | "no permission to access the crypt keeper"); |
| 2721 | |
Jeff Sharkey | ae26646 | 2017-11-27 13:32:24 -0700 | [diff] [blame] | 2722 | if (StorageManager.isFileEncryptedNativeOnly()) { |
| 2723 | // Not supported on FBE devices |
| 2724 | return -1; |
| 2725 | } |
| 2726 | |
Jeff Sharkey | 8058fe6 | 2017-09-13 11:50:33 -0600 | [diff] [blame] | 2727 | if (type == StorageManager.CRYPT_TYPE_DEFAULT) { |
| 2728 | password = ""; |
| 2729 | } else if (TextUtils.isEmpty(password)) { |
| 2730 | throw new IllegalArgumentException("password cannot be empty"); |
| 2731 | } |
| 2732 | |
Jason parks | f7b3cd4 | 2011-01-27 09:28:25 -0600 | [diff] [blame] | 2733 | if (DEBUG_EVENTS) { |
| 2734 | Slog.i(TAG, "changing encryption password..."); |
| 2735 | } |
| 2736 | |
| 2737 | try { |
Jeff Sharkey | 5440279 | 2017-09-15 16:05:19 -0600 | [diff] [blame] | 2738 | mVold.fdeChangePassword(type, password); |
| 2739 | return 0; |
| 2740 | } catch (Exception e) { |
| 2741 | Slog.wtf(TAG, e); |
| 2742 | return -1; |
Jason parks | f7b3cd4 | 2011-01-27 09:28:25 -0600 | [diff] [blame] | 2743 | } |
| 2744 | } |
| 2745 | |
Christopher Tate | 32418be | 2011-10-10 13:51:12 -0700 | [diff] [blame] | 2746 | /** |
| 2747 | * Validate a user-supplied password string with cryptfs |
| 2748 | */ |
| 2749 | @Override |
| 2750 | public int verifyEncryptionPassword(String password) throws RemoteException { |
| 2751 | // Only the system process is permitted to validate passwords |
| 2752 | if (Binder.getCallingUid() != android.os.Process.SYSTEM_UID) { |
| 2753 | throw new SecurityException("no permission to access the crypt keeper"); |
| 2754 | } |
| 2755 | |
| 2756 | mContext.enforceCallingOrSelfPermission(Manifest.permission.CRYPT_KEEPER, |
| 2757 | "no permission to access the crypt keeper"); |
| 2758 | |
| 2759 | if (TextUtils.isEmpty(password)) { |
| 2760 | throw new IllegalArgumentException("password cannot be empty"); |
| 2761 | } |
| 2762 | |
Christopher Tate | 32418be | 2011-10-10 13:51:12 -0700 | [diff] [blame] | 2763 | if (DEBUG_EVENTS) { |
| 2764 | Slog.i(TAG, "validating encryption password..."); |
| 2765 | } |
| 2766 | |
| 2767 | try { |
Jeff Sharkey | 5440279 | 2017-09-15 16:05:19 -0600 | [diff] [blame] | 2768 | mVold.fdeVerifyPassword(password); |
| 2769 | return 0; |
| 2770 | } catch (Exception e) { |
| 2771 | Slog.wtf(TAG, e); |
| 2772 | return -1; |
Christopher Tate | 32418be | 2011-10-10 13:51:12 -0700 | [diff] [blame] | 2773 | } |
| 2774 | } |
| 2775 | |
Paul Lawrence | 8e39736 | 2014-01-27 15:22:30 -0800 | [diff] [blame] | 2776 | /** |
| 2777 | * Get the type of encryption used to encrypt the master key. |
| 2778 | * @return The type, one of the CRYPT_TYPE_XXX consts from StorageManager. |
| 2779 | */ |
| 2780 | @Override |
Svetoslav | 16e4a1a | 2014-09-29 18:16:20 -0700 | [diff] [blame] | 2781 | public int getPasswordType() { |
Paul Lawrence | 76a4057 | 2017-03-15 11:08:04 -0700 | [diff] [blame] | 2782 | mContext.enforceCallingOrSelfPermission(Manifest.permission.CRYPT_KEEPER, |
Paul Lawrence | 9de713d | 2016-05-02 22:45:33 +0000 | [diff] [blame] | 2783 | "no permission to access the crypt keeper"); |
| 2784 | |
Paul Lawrence | 8e39736 | 2014-01-27 15:22:30 -0800 | [diff] [blame] | 2785 | try { |
Jeff Sharkey | 5440279 | 2017-09-15 16:05:19 -0600 | [diff] [blame] | 2786 | return mVold.fdeGetPasswordType(); |
| 2787 | } catch (Exception e) { |
| 2788 | Slog.wtf(TAG, e); |
| 2789 | return -1; |
Paul Lawrence | 8e39736 | 2014-01-27 15:22:30 -0800 | [diff] [blame] | 2790 | } |
| 2791 | } |
| 2792 | |
Paul Lawrence | e51dcf9 | 2014-03-18 10:56:00 -0700 | [diff] [blame] | 2793 | /** |
| 2794 | * Set a field in the crypto header. |
| 2795 | * @param field field to set |
| 2796 | * @param contents contents to set in field |
| 2797 | */ |
| 2798 | @Override |
| 2799 | public void setField(String field, String contents) throws RemoteException { |
Paul Lawrence | 76a4057 | 2017-03-15 11:08:04 -0700 | [diff] [blame] | 2800 | mContext.enforceCallingOrSelfPermission(Manifest.permission.CRYPT_KEEPER, |
Paul Lawrence | 0bbd108 | 2016-04-26 15:21:02 -0700 | [diff] [blame] | 2801 | "no permission to access the crypt keeper"); |
Paul Lawrence | e51dcf9 | 2014-03-18 10:56:00 -0700 | [diff] [blame] | 2802 | |
yusukes | 14a8b1f | 2018-07-23 17:34:42 -0700 | [diff] [blame] | 2803 | if (!StorageManager.isBlockEncrypted()) { |
| 2804 | // Only supported on FDE devices |
Jeff Sharkey | ae26646 | 2017-11-27 13:32:24 -0700 | [diff] [blame] | 2805 | return; |
| 2806 | } |
| 2807 | |
Paul Lawrence | e51dcf9 | 2014-03-18 10:56:00 -0700 | [diff] [blame] | 2808 | try { |
Jeff Sharkey | 5440279 | 2017-09-15 16:05:19 -0600 | [diff] [blame] | 2809 | mVold.fdeSetField(field, contents); |
| 2810 | return; |
| 2811 | } catch (Exception e) { |
| 2812 | Slog.wtf(TAG, e); |
| 2813 | return; |
Paul Lawrence | e51dcf9 | 2014-03-18 10:56:00 -0700 | [diff] [blame] | 2814 | } |
| 2815 | } |
| 2816 | |
| 2817 | /** |
| 2818 | * Gets a field from the crypto header. |
| 2819 | * @param field field to get |
| 2820 | * @return contents of field |
| 2821 | */ |
| 2822 | @Override |
| 2823 | public String getField(String field) throws RemoteException { |
Paul Lawrence | 76a4057 | 2017-03-15 11:08:04 -0700 | [diff] [blame] | 2824 | mContext.enforceCallingOrSelfPermission(Manifest.permission.CRYPT_KEEPER, |
Paul Lawrence | 0bbd108 | 2016-04-26 15:21:02 -0700 | [diff] [blame] | 2825 | "no permission to access the crypt keeper"); |
Paul Lawrence | e51dcf9 | 2014-03-18 10:56:00 -0700 | [diff] [blame] | 2826 | |
yusukes | 14a8b1f | 2018-07-23 17:34:42 -0700 | [diff] [blame] | 2827 | if (!StorageManager.isBlockEncrypted()) { |
| 2828 | // Only supported on FDE devices |
Jeff Sharkey | ae26646 | 2017-11-27 13:32:24 -0700 | [diff] [blame] | 2829 | return null; |
| 2830 | } |
| 2831 | |
Paul Lawrence | e51dcf9 | 2014-03-18 10:56:00 -0700 | [diff] [blame] | 2832 | try { |
Jeff Sharkey | 5440279 | 2017-09-15 16:05:19 -0600 | [diff] [blame] | 2833 | return mVold.fdeGetField(field); |
| 2834 | } catch (Exception e) { |
| 2835 | Slog.wtf(TAG, e); |
| 2836 | return null; |
Paul Lawrence | e51dcf9 | 2014-03-18 10:56:00 -0700 | [diff] [blame] | 2837 | } |
| 2838 | } |
| 2839 | |
Paul Lawrence | 3806d9c | 2015-10-29 10:30:46 -0700 | [diff] [blame] | 2840 | /** |
| 2841 | * Is userdata convertible to file based encryption? |
| 2842 | * @return non zero for convertible |
| 2843 | */ |
| 2844 | @Override |
| 2845 | public boolean isConvertibleToFBE() throws RemoteException { |
Paul Lawrence | 76a4057 | 2017-03-15 11:08:04 -0700 | [diff] [blame] | 2846 | mContext.enforceCallingOrSelfPermission(Manifest.permission.CRYPT_KEEPER, |
Paul Lawrence | 0bbd108 | 2016-04-26 15:21:02 -0700 | [diff] [blame] | 2847 | "no permission to access the crypt keeper"); |
Paul Lawrence | 3806d9c | 2015-10-29 10:30:46 -0700 | [diff] [blame] | 2848 | |
Paul Lawrence | 3806d9c | 2015-10-29 10:30:46 -0700 | [diff] [blame] | 2849 | try { |
Jeff Sharkey | 5440279 | 2017-09-15 16:05:19 -0600 | [diff] [blame] | 2850 | return mVold.isConvertibleToFbe(); |
| 2851 | } catch (Exception e) { |
| 2852 | Slog.wtf(TAG, e); |
| 2853 | return false; |
Paul Lawrence | 3806d9c | 2015-10-29 10:30:46 -0700 | [diff] [blame] | 2854 | } |
| 2855 | } |
| 2856 | |
Daniel Rosenberg | e129e5c | 2018-11-07 19:25:55 -0800 | [diff] [blame] | 2857 | /** |
Martijn Coenen | f04aca4 | 2019-03-11 10:35:34 +0100 | [diff] [blame] | 2858 | * Check whether the device supports filesystem checkpointing. |
| 2859 | * |
| 2860 | * @return true if the device supports filesystem checkpointing, false otherwise. |
| 2861 | */ |
| 2862 | @Override |
| 2863 | public boolean supportsCheckpoint() throws RemoteException { |
Martijn Coenen | f04aca4 | 2019-03-11 10:35:34 +0100 | [diff] [blame] | 2864 | return mVold.supportsCheckpoint(); |
| 2865 | } |
| 2866 | |
| 2867 | /** |
| 2868 | * Signal that checkpointing partitions should start a checkpoint on the next boot. |
| 2869 | * |
| 2870 | * @param numTries Number of times to try booting in checkpoint mode, before we will boot |
| 2871 | * non-checkpoint mode and commit all changes immediately. Callers are |
| 2872 | * responsible for ensuring that boot is safe (eg, by rolling back updates). |
| 2873 | */ |
| 2874 | @Override |
| 2875 | public void startCheckpoint(int numTries) throws RemoteException { |
Nikita Ioffe | 6d74942 | 2020-01-14 22:19:01 +0000 | [diff] [blame] | 2876 | // Only the root, system_server and shell processes are permitted to start checkpoints |
| 2877 | final int callingUid = Binder.getCallingUid(); |
| 2878 | if (callingUid != Process.SYSTEM_UID && callingUid != Process.ROOT_UID |
| 2879 | && callingUid != Process.SHELL_UID) { |
Martijn Coenen | f04aca4 | 2019-03-11 10:35:34 +0100 | [diff] [blame] | 2880 | throw new SecurityException("no permission to start filesystem checkpoint"); |
| 2881 | } |
| 2882 | |
| 2883 | mVold.startCheckpoint(numTries); |
| 2884 | } |
| 2885 | |
| 2886 | /** |
Daniel Rosenberg | e129e5c | 2018-11-07 19:25:55 -0800 | [diff] [blame] | 2887 | * Signal that checkpointing partitions should commit changes |
| 2888 | */ |
| 2889 | @Override |
| 2890 | public void commitChanges() throws RemoteException { |
| 2891 | // Only the system process is permitted to commit checkpoints |
| 2892 | if (Binder.getCallingUid() != android.os.Process.SYSTEM_UID) { |
| 2893 | throw new SecurityException("no permission to commit checkpoint changes"); |
| 2894 | } |
| 2895 | |
| 2896 | mVold.commitChanges(); |
| 2897 | } |
| 2898 | |
Daniel Rosenberg | e3a924d | 2019-03-19 18:19:08 -0700 | [diff] [blame] | 2899 | /** |
| 2900 | * Check if we should be mounting with checkpointing or are checkpointing now |
| 2901 | */ |
| 2902 | @Override |
| 2903 | public boolean needsCheckpoint() throws RemoteException { |
Paul Lawrence | 15a5446 | 2019-04-30 11:14:25 -0700 | [diff] [blame] | 2904 | enforcePermission(android.Manifest.permission.MOUNT_FORMAT_FILESYSTEMS); |
Daniel Rosenberg | e3a924d | 2019-03-19 18:19:08 -0700 | [diff] [blame] | 2905 | return mVold.needsCheckpoint(); |
| 2906 | } |
| 2907 | |
Daniel Rosenberg | 600799b | 2019-03-20 17:42:50 -0700 | [diff] [blame] | 2908 | /** |
| 2909 | * Abort the current set of changes and either try again, or abort entirely |
| 2910 | */ |
| 2911 | @Override |
| 2912 | public void abortChanges(String message, boolean retry) throws RemoteException { |
| 2913 | // Only the system process is permitted to abort checkpoints |
| 2914 | if (Binder.getCallingUid() != android.os.Process.SYSTEM_UID) { |
| 2915 | throw new SecurityException("no permission to commit checkpoint changes"); |
| 2916 | } |
| 2917 | |
| 2918 | mVold.abortChanges(message, retry); |
| 2919 | } |
| 2920 | |
Jeff Sharkey | b049e21 | 2012-09-07 23:16:01 -0700 | [diff] [blame] | 2921 | @Override |
Paul Lawrence | 945490c | 2014-03-27 16:37:28 +0000 | [diff] [blame] | 2922 | public String getPassword() throws RemoteException { |
Paul Lawrence | 76a4057 | 2017-03-15 11:08:04 -0700 | [diff] [blame] | 2923 | mContext.enforceCallingOrSelfPermission(Manifest.permission.CRYPT_KEEPER, |
Rubin Xu | cd7a014 | 2015-04-17 23:45:27 +0100 | [diff] [blame] | 2924 | "only keyguard can retrieve password"); |
Paul Lawrence | 0bbd108 | 2016-04-26 15:21:02 -0700 | [diff] [blame] | 2925 | |
Paul Lawrence | 945490c | 2014-03-27 16:37:28 +0000 | [diff] [blame] | 2926 | try { |
Jeff Sharkey | 5440279 | 2017-09-15 16:05:19 -0600 | [diff] [blame] | 2927 | return mVold.fdeGetPassword(); |
| 2928 | } catch (Exception e) { |
| 2929 | Slog.wtf(TAG, e); |
Paul Lawrence | 24063b5 | 2015-01-06 13:11:23 -0800 | [diff] [blame] | 2930 | return null; |
Paul Lawrence | 945490c | 2014-03-27 16:37:28 +0000 | [diff] [blame] | 2931 | } |
| 2932 | } |
| 2933 | |
| 2934 | @Override |
| 2935 | public void clearPassword() throws RemoteException { |
Paul Lawrence | 76a4057 | 2017-03-15 11:08:04 -0700 | [diff] [blame] | 2936 | mContext.enforceCallingOrSelfPermission(Manifest.permission.CRYPT_KEEPER, |
Paul Lawrence | 0bbd108 | 2016-04-26 15:21:02 -0700 | [diff] [blame] | 2937 | "only keyguard can clear password"); |
| 2938 | |
Paul Lawrence | 945490c | 2014-03-27 16:37:28 +0000 | [diff] [blame] | 2939 | try { |
Jeff Sharkey | 5440279 | 2017-09-15 16:05:19 -0600 | [diff] [blame] | 2940 | mVold.fdeClearPassword(); |
| 2941 | return; |
| 2942 | } catch (Exception e) { |
| 2943 | Slog.wtf(TAG, e); |
| 2944 | return; |
Paul Lawrence | 945490c | 2014-03-27 16:37:28 +0000 | [diff] [blame] | 2945 | } |
| 2946 | } |
| 2947 | |
| 2948 | @Override |
Lenka Trochtova | c4dd021 | 2015-11-18 12:22:06 +0100 | [diff] [blame] | 2949 | public void createUserKey(int userId, int serialNumber, boolean ephemeral) { |
Jeff Sharkey | f9fc6d6 | 2015-11-08 16:46:05 -0800 | [diff] [blame] | 2950 | enforcePermission(android.Manifest.permission.STORAGE_INTERNAL); |
Paul Crowley | bcf48ed | 2015-04-22 13:36:59 +0100 | [diff] [blame] | 2951 | |
Paul Crowley | bcf48ed | 2015-04-22 13:36:59 +0100 | [diff] [blame] | 2952 | try { |
Jeff Sharkey | 5440279 | 2017-09-15 16:05:19 -0600 | [diff] [blame] | 2953 | mVold.createUserKey(userId, serialNumber, ephemeral); |
Jeff Sharkey | 43e1211 | 2017-09-12 16:31:45 -0600 | [diff] [blame] | 2954 | } catch (Exception e) { |
| 2955 | Slog.wtf(TAG, e); |
Paul Crowley | bcf48ed | 2015-04-22 13:36:59 +0100 | [diff] [blame] | 2956 | } |
| 2957 | } |
| 2958 | |
Paul Crowley | 7ec733f | 2015-05-19 12:42:00 +0100 | [diff] [blame] | 2959 | @Override |
Jeff Sharkey | f9fc6d6 | 2015-11-08 16:46:05 -0800 | [diff] [blame] | 2960 | public void destroyUserKey(int userId) { |
| 2961 | enforcePermission(android.Manifest.permission.STORAGE_INTERNAL); |
Paul Crowley | 7ec733f | 2015-05-19 12:42:00 +0100 | [diff] [blame] | 2962 | |
Jeff Sharkey | f9fc6d6 | 2015-11-08 16:46:05 -0800 | [diff] [blame] | 2963 | try { |
Jeff Sharkey | 5440279 | 2017-09-15 16:05:19 -0600 | [diff] [blame] | 2964 | mVold.destroyUserKey(userId); |
Jeff Sharkey | 43e1211 | 2017-09-12 16:31:45 -0600 | [diff] [blame] | 2965 | } catch (Exception e) { |
| 2966 | Slog.wtf(TAG, e); |
Jeff Sharkey | f9fc6d6 | 2015-11-08 16:46:05 -0800 | [diff] [blame] | 2967 | } |
| 2968 | } |
| 2969 | |
Jeff Sharkey | 43e1211 | 2017-09-12 16:31:45 -0600 | [diff] [blame] | 2970 | private String encodeBytes(byte[] bytes) { |
Paul Crowley | faeb3eb | 2016-02-08 15:58:29 +0000 | [diff] [blame] | 2971 | if (ArrayUtils.isEmpty(bytes)) { |
Jeff Sharkey | 43e1211 | 2017-09-12 16:31:45 -0600 | [diff] [blame] | 2972 | return "!"; |
Paul Crowley | faeb3eb | 2016-02-08 15:58:29 +0000 | [diff] [blame] | 2973 | } else { |
Jeff Sharkey | 43e1211 | 2017-09-12 16:31:45 -0600 | [diff] [blame] | 2974 | return HexDump.toHexString(bytes); |
Paul Crowley | faeb3eb | 2016-02-08 15:58:29 +0000 | [diff] [blame] | 2975 | } |
| 2976 | } |
| 2977 | |
Paul Crowley | cc70155 | 2016-05-17 14:18:49 -0700 | [diff] [blame] | 2978 | /* |
| 2979 | * Add this token/secret pair to the set of ways we can recover a disk encryption key. |
| 2980 | * Changing the token/secret for a disk encryption key is done in two phases: first, adding |
| 2981 | * a new token/secret pair with this call, then delting all other pairs with |
| 2982 | * fixateNewestUserKeyAuth. This allows other places where a credential is used, such as |
| 2983 | * Gatekeeper, to be updated between the two calls. |
| 2984 | */ |
Jeff Sharkey | f9fc6d6 | 2015-11-08 16:46:05 -0800 | [diff] [blame] | 2985 | @Override |
Paul Crowley | cc70155 | 2016-05-17 14:18:49 -0700 | [diff] [blame] | 2986 | public void addUserKeyAuth(int userId, int serialNumber, byte[] token, byte[] secret) { |
Paul Crowley | faeb3eb | 2016-02-08 15:58:29 +0000 | [diff] [blame] | 2987 | enforcePermission(android.Manifest.permission.STORAGE_INTERNAL); |
Paul Crowley | faeb3eb | 2016-02-08 15:58:29 +0000 | [diff] [blame] | 2988 | |
| 2989 | try { |
Jeff Sharkey | 5440279 | 2017-09-15 16:05:19 -0600 | [diff] [blame] | 2990 | mVold.addUserKeyAuth(userId, serialNumber, encodeBytes(token), encodeBytes(secret)); |
Jeff Sharkey | 43e1211 | 2017-09-12 16:31:45 -0600 | [diff] [blame] | 2991 | } catch (Exception e) { |
| 2992 | Slog.wtf(TAG, e); |
Paul Crowley | cc70155 | 2016-05-17 14:18:49 -0700 | [diff] [blame] | 2993 | } |
| 2994 | } |
| 2995 | |
| 2996 | /* |
Barani Muthukumaran | 9ba99ed | 2019-12-18 00:36:06 -0800 | [diff] [blame] | 2997 | * Clear disk encryption key bound to the associated token / secret pair. Removing the user |
| 2998 | * binding of the Disk encryption key is done in two phases: first, this call will retrieve |
| 2999 | * the disk encryption key using the provided token / secret pair and store it by |
| 3000 | * encrypting it with a keymaster key not bound to the user, then fixateNewestUserKeyAuth |
| 3001 | * is called to delete all other bindings of the disk encryption key. |
| 3002 | */ |
| 3003 | @Override |
| 3004 | public void clearUserKeyAuth(int userId, int serialNumber, byte[] token, byte[] secret) { |
| 3005 | enforcePermission(android.Manifest.permission.STORAGE_INTERNAL); |
| 3006 | |
| 3007 | try { |
| 3008 | mVold.clearUserKeyAuth(userId, serialNumber, encodeBytes(token), encodeBytes(secret)); |
| 3009 | } catch (Exception e) { |
| 3010 | Slog.wtf(TAG, e); |
| 3011 | } |
| 3012 | } |
| 3013 | |
| 3014 | /* |
Paul Crowley | cc70155 | 2016-05-17 14:18:49 -0700 | [diff] [blame] | 3015 | * Delete all disk encryption token/secret pairs except the most recently added one |
| 3016 | */ |
| 3017 | @Override |
| 3018 | public void fixateNewestUserKeyAuth(int userId) { |
| 3019 | enforcePermission(android.Manifest.permission.STORAGE_INTERNAL); |
Paul Crowley | cc70155 | 2016-05-17 14:18:49 -0700 | [diff] [blame] | 3020 | |
| 3021 | try { |
Jeff Sharkey | 5440279 | 2017-09-15 16:05:19 -0600 | [diff] [blame] | 3022 | mVold.fixateNewestUserKeyAuth(userId); |
Jeff Sharkey | 43e1211 | 2017-09-12 16:31:45 -0600 | [diff] [blame] | 3023 | } catch (Exception e) { |
| 3024 | Slog.wtf(TAG, e); |
Paul Crowley | faeb3eb | 2016-02-08 15:58:29 +0000 | [diff] [blame] | 3025 | } |
| 3026 | } |
| 3027 | |
| 3028 | @Override |
| 3029 | public void unlockUserKey(int userId, int serialNumber, byte[] token, byte[] secret) { |
Sudheer Shanka | 03b20ec | 2019-02-21 15:11:00 -0800 | [diff] [blame] | 3030 | Slog.d(TAG, "unlockUserKey: " + userId); |
Jeff Sharkey | f9fc6d6 | 2015-11-08 16:46:05 -0800 | [diff] [blame] | 3031 | enforcePermission(android.Manifest.permission.STORAGE_INTERNAL); |
Jeff Sharkey | f9fc6d6 | 2015-11-08 16:46:05 -0800 | [diff] [blame] | 3032 | |
Jeff Sharkey | ce18c81 | 2016-04-27 16:00:41 -0600 | [diff] [blame] | 3033 | if (StorageManager.isFileEncryptedNativeOrEmulated()) { |
Jeff Sharkey | ce18c81 | 2016-04-27 16:00:41 -0600 | [diff] [blame] | 3034 | try { |
Jeff Sharkey | 5440279 | 2017-09-15 16:05:19 -0600 | [diff] [blame] | 3035 | mVold.unlockUserKey(userId, serialNumber, encodeBytes(token), |
| 3036 | encodeBytes(secret)); |
Jeff Sharkey | 43e1211 | 2017-09-12 16:31:45 -0600 | [diff] [blame] | 3037 | } catch (Exception e) { |
| 3038 | Slog.wtf(TAG, e); |
| 3039 | return; |
Jeff Sharkey | ce18c81 | 2016-04-27 16:00:41 -0600 | [diff] [blame] | 3040 | } |
Jeff Sharkey | f9fc6d6 | 2015-11-08 16:46:05 -0800 | [diff] [blame] | 3041 | } |
| 3042 | |
| 3043 | synchronized (mLock) { |
Jeff Sharkey | ce14cd0 | 2015-12-07 15:35:42 -0700 | [diff] [blame] | 3044 | mLocalUnlockedUsers = ArrayUtils.appendInt(mLocalUnlockedUsers, userId); |
Jeff Sharkey | f9fc6d6 | 2015-11-08 16:46:05 -0800 | [diff] [blame] | 3045 | } |
| 3046 | } |
| 3047 | |
| 3048 | @Override |
| 3049 | public void lockUserKey(int userId) { |
| 3050 | enforcePermission(android.Manifest.permission.STORAGE_INTERNAL); |
Jeff Sharkey | f9fc6d6 | 2015-11-08 16:46:05 -0800 | [diff] [blame] | 3051 | |
| 3052 | try { |
Jeff Sharkey | 5440279 | 2017-09-15 16:05:19 -0600 | [diff] [blame] | 3053 | mVold.lockUserKey(userId); |
Jeff Sharkey | 43e1211 | 2017-09-12 16:31:45 -0600 | [diff] [blame] | 3054 | } catch (Exception e) { |
| 3055 | Slog.wtf(TAG, e); |
| 3056 | return; |
Jeff Sharkey | f9fc6d6 | 2015-11-08 16:46:05 -0800 | [diff] [blame] | 3057 | } |
| 3058 | |
| 3059 | synchronized (mLock) { |
Jeff Sharkey | ce14cd0 | 2015-12-07 15:35:42 -0700 | [diff] [blame] | 3060 | mLocalUnlockedUsers = ArrayUtils.removeInt(mLocalUnlockedUsers, userId); |
Jeff Sharkey | f9fc6d6 | 2015-11-08 16:46:05 -0800 | [diff] [blame] | 3061 | } |
| 3062 | } |
| 3063 | |
| 3064 | @Override |
| 3065 | public boolean isUserKeyUnlocked(int userId) { |
Jeff Sharkey | ce18c81 | 2016-04-27 16:00:41 -0600 | [diff] [blame] | 3066 | synchronized (mLock) { |
| 3067 | return ArrayUtils.contains(mLocalUnlockedUsers, userId); |
Jeff Sharkey | f9fc6d6 | 2015-11-08 16:46:05 -0800 | [diff] [blame] | 3068 | } |
| 3069 | } |
| 3070 | |
Narayan Kamath | 157dd1d | 2019-06-12 13:06:30 +0100 | [diff] [blame] | 3071 | private boolean isSystemUnlocked(int userId) { |
| 3072 | synchronized (mLock) { |
| 3073 | return ArrayUtils.contains(mSystemUnlockedUsers, userId); |
| 3074 | } |
| 3075 | } |
| 3076 | |
Jeff Sharkey | f9fc6d6 | 2015-11-08 16:46:05 -0800 | [diff] [blame] | 3077 | @Override |
Jeff Sharkey | 47f7108 | 2016-02-01 17:03:54 -0700 | [diff] [blame] | 3078 | public void prepareUserStorage(String volumeUuid, int userId, int serialNumber, int flags) { |
Jeff Sharkey | f9fc6d6 | 2015-11-08 16:46:05 -0800 | [diff] [blame] | 3079 | enforcePermission(android.Manifest.permission.STORAGE_INTERNAL); |
Jeff Sharkey | f9fc6d6 | 2015-11-08 16:46:05 -0800 | [diff] [blame] | 3080 | |
| 3081 | try { |
Jeff Sharkey | 5440279 | 2017-09-15 16:05:19 -0600 | [diff] [blame] | 3082 | mVold.prepareUserStorage(volumeUuid, userId, serialNumber, flags); |
Jeff Sharkey | 43e1211 | 2017-09-12 16:31:45 -0600 | [diff] [blame] | 3083 | } catch (Exception e) { |
| 3084 | Slog.wtf(TAG, e); |
Paul Crowley | 7ec733f | 2015-05-19 12:42:00 +0100 | [diff] [blame] | 3085 | } |
| 3086 | } |
| 3087 | |
Paul Crowley | bcf48ed | 2015-04-22 13:36:59 +0100 | [diff] [blame] | 3088 | @Override |
Jeff Sharkey | fcf1e55 | 2016-04-14 20:44:58 -0600 | [diff] [blame] | 3089 | public void destroyUserStorage(String volumeUuid, int userId, int flags) { |
| 3090 | enforcePermission(android.Manifest.permission.STORAGE_INTERNAL); |
Jeff Sharkey | fcf1e55 | 2016-04-14 20:44:58 -0600 | [diff] [blame] | 3091 | |
| 3092 | try { |
Jeff Sharkey | 5440279 | 2017-09-15 16:05:19 -0600 | [diff] [blame] | 3093 | mVold.destroyUserStorage(volumeUuid, userId, flags); |
Jeff Sharkey | 43e1211 | 2017-09-12 16:31:45 -0600 | [diff] [blame] | 3094 | } catch (Exception e) { |
| 3095 | Slog.wtf(TAG, e); |
Jeff Sharkey | fcf1e55 | 2016-04-14 20:44:58 -0600 | [diff] [blame] | 3096 | } |
| 3097 | } |
| 3098 | |
Anton Hansson | be9c990 | 2019-05-28 13:45:26 +0100 | [diff] [blame] | 3099 | /** Not thread safe */ |
Daichi Hirono | e56740d | 2017-02-02 13:56:45 +0900 | [diff] [blame] | 3100 | class AppFuseMountScope extends AppFuseBridge.MountScope { |
Anton Hansson | be9c990 | 2019-05-28 13:45:26 +0100 | [diff] [blame] | 3101 | private boolean mMounted = false; |
Daichi Hirono | 812c95d | 2017-02-08 16:20:20 +0900 | [diff] [blame] | 3102 | |
Ryo Hashimoto | 38bcbf0 | 2018-05-16 18:20:37 +0900 | [diff] [blame] | 3103 | public AppFuseMountScope(int uid, int mountId) { |
| 3104 | super(uid, mountId); |
Daichi Hirono | 812c95d | 2017-02-08 16:20:20 +0900 | [diff] [blame] | 3105 | } |
| 3106 | |
| 3107 | @Override |
| 3108 | public ParcelFileDescriptor open() throws NativeDaemonConnectorException { |
Jeff Sharkey | 5440279 | 2017-09-15 16:05:19 -0600 | [diff] [blame] | 3109 | try { |
Anton Hansson | be9c990 | 2019-05-28 13:45:26 +0100 | [diff] [blame] | 3110 | final FileDescriptor fd = mVold.mountAppFuse(uid, mountId); |
| 3111 | mMounted = true; |
| 3112 | return new ParcelFileDescriptor(fd); |
Jeff Sharkey | 5440279 | 2017-09-15 16:05:19 -0600 | [diff] [blame] | 3113 | } catch (Exception e) { |
| 3114 | throw new NativeDaemonConnectorException("Failed to mount", e); |
Daichi Hirono | 812c95d | 2017-02-08 16:20:20 +0900 | [diff] [blame] | 3115 | } |
Daichi Hirono | 9fb0018 | 2016-11-08 14:12:17 +0900 | [diff] [blame] | 3116 | } |
| 3117 | |
| 3118 | @Override |
Ryo Hashimoto | 38bcbf0 | 2018-05-16 18:20:37 +0900 | [diff] [blame] | 3119 | public ParcelFileDescriptor openFile(int mountId, int fileId, int flags) |
| 3120 | throws NativeDaemonConnectorException { |
| 3121 | try { |
| 3122 | return new ParcelFileDescriptor( |
| 3123 | mVold.openAppFuseFile(uid, mountId, fileId, flags)); |
| 3124 | } catch (Exception e) { |
| 3125 | throw new NativeDaemonConnectorException("Failed to open", e); |
| 3126 | } |
| 3127 | } |
| 3128 | |
| 3129 | @Override |
Daichi Hirono | e56740d | 2017-02-02 13:56:45 +0900 | [diff] [blame] | 3130 | public void close() throws Exception { |
Anton Hansson | be9c990 | 2019-05-28 13:45:26 +0100 | [diff] [blame] | 3131 | if (mMounted) { |
Ryo Hashimoto | 38bcbf0 | 2018-05-16 18:20:37 +0900 | [diff] [blame] | 3132 | mVold.unmountAppFuse(uid, mountId); |
Anton Hansson | be9c990 | 2019-05-28 13:45:26 +0100 | [diff] [blame] | 3133 | mMounted = false; |
Daichi Hirono | 812c95d | 2017-02-08 16:20:20 +0900 | [diff] [blame] | 3134 | } |
Daichi Hirono | 9fb0018 | 2016-11-08 14:12:17 +0900 | [diff] [blame] | 3135 | } |
| 3136 | } |
| 3137 | |
| 3138 | @Override |
Daichi Hirono | 812c95d | 2017-02-08 16:20:20 +0900 | [diff] [blame] | 3139 | public @Nullable AppFuseMount mountProxyFileDescriptorBridge() { |
Daichi Hirono | e56740d | 2017-02-02 13:56:45 +0900 | [diff] [blame] | 3140 | Slog.v(TAG, "mountProxyFileDescriptorBridge"); |
Daichi Hirono | 9fb0018 | 2016-11-08 14:12:17 +0900 | [diff] [blame] | 3141 | final int uid = Binder.getCallingUid(); |
Daichi Hirono | 9fb0018 | 2016-11-08 14:12:17 +0900 | [diff] [blame] | 3142 | |
Daichi Hirono | e56740d | 2017-02-02 13:56:45 +0900 | [diff] [blame] | 3143 | while (true) { |
| 3144 | synchronized (mAppFuseLock) { |
| 3145 | boolean newlyCreated = false; |
| 3146 | if (mAppFuseBridge == null) { |
| 3147 | mAppFuseBridge = new AppFuseBridge(); |
| 3148 | new Thread(mAppFuseBridge, AppFuseBridge.TAG).start(); |
| 3149 | newlyCreated = true; |
Daichi Hirono | 9fb0018 | 2016-11-08 14:12:17 +0900 | [diff] [blame] | 3150 | } |
Daichi Hirono | e56740d | 2017-02-02 13:56:45 +0900 | [diff] [blame] | 3151 | try { |
| 3152 | final int name = mNextAppFuseName++; |
| 3153 | try { |
| 3154 | return new AppFuseMount( |
Ryo Hashimoto | 38bcbf0 | 2018-05-16 18:20:37 +0900 | [diff] [blame] | 3155 | name, mAppFuseBridge.addBridge(new AppFuseMountScope(uid, name))); |
Daichi Hirono | 812c95d | 2017-02-08 16:20:20 +0900 | [diff] [blame] | 3156 | } catch (FuseUnavailableMountException e) { |
Daichi Hirono | e56740d | 2017-02-02 13:56:45 +0900 | [diff] [blame] | 3157 | if (newlyCreated) { |
| 3158 | // If newly created bridge fails, it's a real error. |
Daichi Hirono | 812c95d | 2017-02-08 16:20:20 +0900 | [diff] [blame] | 3159 | Slog.e(TAG, "", e); |
| 3160 | return null; |
Daichi Hirono | e56740d | 2017-02-02 13:56:45 +0900 | [diff] [blame] | 3161 | } |
| 3162 | // It seems the thread of mAppFuseBridge has already been terminated. |
| 3163 | mAppFuseBridge = null; |
| 3164 | } |
| 3165 | } catch (NativeDaemonConnectorException e) { |
| 3166 | throw e.rethrowAsParcelableException(); |
| 3167 | } |
Daichi Hirono | 9fb0018 | 2016-11-08 14:12:17 +0900 | [diff] [blame] | 3168 | } |
Daichi Hirono | 9fb0018 | 2016-11-08 14:12:17 +0900 | [diff] [blame] | 3169 | } |
| 3170 | } |
| 3171 | |
| 3172 | @Override |
Daichi Hirono | 812c95d | 2017-02-08 16:20:20 +0900 | [diff] [blame] | 3173 | public @Nullable ParcelFileDescriptor openProxyFileDescriptor( |
| 3174 | int mountId, int fileId, int mode) { |
| 3175 | Slog.v(TAG, "mountProxyFileDescriptor"); |
Jeff Sharkey | 3946632 | 2018-12-05 19:19:52 -0700 | [diff] [blame] | 3176 | |
| 3177 | // We only support a narrow set of incoming mode flags |
Jeff Sharkey | 3b1c254 | 2018-12-13 15:01:38 -0700 | [diff] [blame] | 3178 | mode &= MODE_READ_WRITE; |
Jeff Sharkey | 3946632 | 2018-12-05 19:19:52 -0700 | [diff] [blame] | 3179 | |
Daichi Hirono | 9fb0018 | 2016-11-08 14:12:17 +0900 | [diff] [blame] | 3180 | try { |
| 3181 | synchronized (mAppFuseLock) { |
Daichi Hirono | e56740d | 2017-02-02 13:56:45 +0900 | [diff] [blame] | 3182 | if (mAppFuseBridge == null) { |
Daichi Hirono | 812c95d | 2017-02-08 16:20:20 +0900 | [diff] [blame] | 3183 | Slog.e(TAG, "FuseBridge has not been created"); |
| 3184 | return null; |
Daichi Hirono | 9fb0018 | 2016-11-08 14:12:17 +0900 | [diff] [blame] | 3185 | } |
Ryo Hashimoto | 38bcbf0 | 2018-05-16 18:20:37 +0900 | [diff] [blame] | 3186 | return mAppFuseBridge.openFile(mountId, fileId, mode); |
Daichi Hirono | 9fb0018 | 2016-11-08 14:12:17 +0900 | [diff] [blame] | 3187 | } |
Daichi Hirono | 812c95d | 2017-02-08 16:20:20 +0900 | [diff] [blame] | 3188 | } catch (FuseUnavailableMountException | InterruptedException error) { |
| 3189 | Slog.v(TAG, "The mount point has already been invalid", error); |
| 3190 | return null; |
Daichi Hirono | 9fb0018 | 2016-11-08 14:12:17 +0900 | [diff] [blame] | 3191 | } |
| 3192 | } |
| 3193 | |
Daichi Hirono | 9e8d9e2 | 2015-11-13 14:37:00 +0900 | [diff] [blame] | 3194 | @Override |
Jeff Sharkey | ae26646 | 2017-11-27 13:32:24 -0700 | [diff] [blame] | 3195 | public void mkdirs(String callingPkg, String appPath) { |
Sudheer Shanka | a63bfb3 | 2019-02-28 11:15:58 -0800 | [diff] [blame] | 3196 | final int callingUid = Binder.getCallingUid(); |
| 3197 | final int userId = UserHandle.getUserId(callingUid); |
Farid Zare Seisan | ac09451 | 2018-04-02 15:06:13 -0700 | [diff] [blame] | 3198 | final String propertyName = "sys.user." + userId + ".ce_available"; |
Jeff Sharkey | 2d8b4e8 | 2013-09-17 17:30:33 -0700 | [diff] [blame] | 3199 | |
Jeff Sharkey | 196c755 | 2018-03-26 15:56:50 -0600 | [diff] [blame] | 3200 | // Ignore requests to create directories while storage is locked |
Farid Zare Seisan | ac09451 | 2018-04-02 15:06:13 -0700 | [diff] [blame] | 3201 | if (!isUserKeyUnlocked(userId)) { |
| 3202 | throw new IllegalStateException("Failed to prepare " + appPath); |
| 3203 | } |
| 3204 | |
| 3205 | // Ignore requests to create directories if CE storage is not available |
Farid Zare Seisan | 7f6b812 | 2018-03-30 11:19:10 -0700 | [diff] [blame] | 3206 | if ((userId == UserHandle.USER_SYSTEM) |
| 3207 | && !SystemProperties.getBoolean(propertyName, false)) { |
Farid Zare Seisan | ac09451 | 2018-04-02 15:06:13 -0700 | [diff] [blame] | 3208 | throw new IllegalStateException("Failed to prepare " + appPath); |
| 3209 | } |
Jeff Sharkey | 196c755 | 2018-03-26 15:56:50 -0600 | [diff] [blame] | 3210 | |
Jeff Sharkey | 2d8b4e8 | 2013-09-17 17:30:33 -0700 | [diff] [blame] | 3211 | // Validate that reported package name belongs to caller |
| 3212 | final AppOpsManager appOps = (AppOpsManager) mContext.getSystemService( |
| 3213 | Context.APP_OPS_SERVICE); |
Sudheer Shanka | a63bfb3 | 2019-02-28 11:15:58 -0800 | [diff] [blame] | 3214 | appOps.checkPackage(callingUid, callingPkg); |
Jeff Sharkey | 2d8b4e8 | 2013-09-17 17:30:33 -0700 | [diff] [blame] | 3215 | |
Jeff Sharkey | 4887789 | 2015-03-18 11:27:19 -0700 | [diff] [blame] | 3216 | File appFile = null; |
Jeff Sharkey | 2d8b4e8 | 2013-09-17 17:30:33 -0700 | [diff] [blame] | 3217 | try { |
Jeff Sharkey | 4887789 | 2015-03-18 11:27:19 -0700 | [diff] [blame] | 3218 | appFile = new File(appPath).getCanonicalFile(); |
Jeff Sharkey | 2d8b4e8 | 2013-09-17 17:30:33 -0700 | [diff] [blame] | 3219 | } catch (IOException e) { |
Jeff Sharkey | ae26646 | 2017-11-27 13:32:24 -0700 | [diff] [blame] | 3220 | throw new IllegalStateException("Failed to resolve " + appPath + ": " + e); |
Jeff Sharkey | 2d8b4e8 | 2013-09-17 17:30:33 -0700 | [diff] [blame] | 3221 | } |
| 3222 | |
Martijn Coenen | 9fd2b64 | 2019-12-24 13:04:36 +0100 | [diff] [blame] | 3223 | appPath = appFile.getAbsolutePath(); |
| 3224 | if (!appPath.endsWith("/")) { |
| 3225 | appPath = appPath + "/"; |
| 3226 | } |
| 3227 | // Ensure that the path we're asked to create is a known application directory |
| 3228 | // path. |
| 3229 | final Matcher matcher = KNOWN_APP_DIR_PATHS.matcher(appPath); |
| 3230 | if (matcher.matches()) { |
| 3231 | // And that the package dir matches the calling package |
| 3232 | if (!matcher.group(3).equals(callingPkg)) { |
| 3233 | throw new SecurityException("Invalid mkdirs path: " + appFile |
| 3234 | + " does not contain calling package " + callingPkg); |
Jeff Sharkey | 4887789 | 2015-03-18 11:27:19 -0700 | [diff] [blame] | 3235 | } |
Martijn Coenen | 9fd2b64 | 2019-12-24 13:04:36 +0100 | [diff] [blame] | 3236 | // And that the user id part of the path (if any) matches the calling user id, |
| 3237 | // or if for a public volume (no user id), the user matches the current user |
| 3238 | if ((matcher.group(2) != null && !matcher.group(2).equals(Integer.toString(userId))) |
| 3239 | || (matcher.group(2) == null && userId != mCurrentUserId)) { |
| 3240 | throw new SecurityException("Invalid mkdirs path: " + appFile |
| 3241 | + " does not match calling user id " + userId); |
| 3242 | } |
Jeff Sharkey | 2d8b4e8 | 2013-09-17 17:30:33 -0700 | [diff] [blame] | 3243 | try { |
Martijn Coenen | 9fd2b64 | 2019-12-24 13:04:36 +0100 | [diff] [blame] | 3244 | mVold.setupAppDir(appPath, matcher.group(1), callingUid); |
| 3245 | } catch (RemoteException e) { |
Sudheer Shanka | 64501e5 | 2019-04-29 10:46:26 -0700 | [diff] [blame] | 3246 | throw new IllegalStateException("Failed to prepare " + appPath + ": " + e); |
Jeff Sharkey | 2d8b4e8 | 2013-09-17 17:30:33 -0700 | [diff] [blame] | 3247 | } |
Jeff Sharkey | 2d8b4e8 | 2013-09-17 17:30:33 -0700 | [diff] [blame] | 3248 | |
Martijn Coenen | 9fd2b64 | 2019-12-24 13:04:36 +0100 | [diff] [blame] | 3249 | return; |
| 3250 | } |
| 3251 | throw new SecurityException("Invalid mkdirs path: " + appFile |
| 3252 | + " is not a known app path."); |
Jeff Sharkey | 2d8b4e8 | 2013-09-17 17:30:33 -0700 | [diff] [blame] | 3253 | } |
| 3254 | |
| 3255 | @Override |
Jeff Sharkey | 4634987 | 2015-07-28 10:49:47 -0700 | [diff] [blame] | 3256 | public StorageVolume[] getVolumeList(int uid, String packageName, int flags) { |
Jeff Sharkey | ce14cd0 | 2015-12-07 15:35:42 -0700 | [diff] [blame] | 3257 | final int userId = UserHandle.getUserId(uid); |
Jeff Sharkey | c02bfae | 2016-03-27 15:06:53 -0600 | [diff] [blame] | 3258 | |
Jeff Sharkey | 4634987 | 2015-07-28 10:49:47 -0700 | [diff] [blame] | 3259 | final boolean forWrite = (flags & StorageManager.FLAG_FOR_WRITE) != 0; |
Jeff Sharkey | c02bfae | 2016-03-27 15:06:53 -0600 | [diff] [blame] | 3260 | final boolean realState = (flags & StorageManager.FLAG_REAL_STATE) != 0; |
| 3261 | final boolean includeInvisible = (flags & StorageManager.FLAG_INCLUDE_INVISIBLE) != 0; |
Jeff Sharkey | 04b4ba1 | 2019-12-15 22:42:42 -0700 | [diff] [blame] | 3262 | final boolean includeRecent = (flags & StorageManager.FLAG_INCLUDE_RECENT) != 0; |
Jeff Sharkey | 4634987 | 2015-07-28 10:49:47 -0700 | [diff] [blame] | 3263 | |
Narayan Kamath | 157dd1d | 2019-06-12 13:06:30 +0100 | [diff] [blame] | 3264 | // Report all volumes as unmounted until we've recorded that user 0 has unlocked. There |
| 3265 | // are no guarantees that callers will see a consistent view of the volume before that |
| 3266 | // point |
| 3267 | final boolean systemUserUnlocked = isSystemUnlocked(UserHandle.USER_SYSTEM); |
| 3268 | |
Jeff Sharkey | 04b4ba1 | 2019-12-15 22:42:42 -0700 | [diff] [blame] | 3269 | final boolean userIsDemo; |
Jeff Sharkey | c02bfae | 2016-03-27 15:06:53 -0600 | [diff] [blame] | 3270 | final boolean userKeyUnlocked; |
| 3271 | final boolean storagePermission; |
| 3272 | final long token = Binder.clearCallingIdentity(); |
Svetoslav | 38c3dbb | 2015-07-14 11:27:06 -0700 | [diff] [blame] | 3273 | try { |
Jeff Sharkey | 04b4ba1 | 2019-12-15 22:42:42 -0700 | [diff] [blame] | 3274 | userIsDemo = LocalServices.getService(UserManagerInternal.class) |
| 3275 | .getUserInfo(userId).isDemo(); |
Jeff Sharkey | c02bfae | 2016-03-27 15:06:53 -0600 | [diff] [blame] | 3276 | userKeyUnlocked = isUserKeyUnlocked(userId); |
Sudheer Shanka | 2250d56 | 2016-11-07 15:41:02 -0800 | [diff] [blame] | 3277 | storagePermission = mStorageManagerInternal.hasExternalStorage(uid, packageName); |
Svetoslav | 38c3dbb | 2015-07-14 11:27:06 -0700 | [diff] [blame] | 3278 | } finally { |
Jeff Sharkey | c02bfae | 2016-03-27 15:06:53 -0600 | [diff] [blame] | 3279 | Binder.restoreCallingIdentity(token); |
Svetoslav | 38c3dbb | 2015-07-14 11:27:06 -0700 | [diff] [blame] | 3280 | } |
Svet Ganov | 6ee871e | 2015-07-10 14:29:33 -0700 | [diff] [blame] | 3281 | |
Jeff Sharkey | c02bfae | 2016-03-27 15:06:53 -0600 | [diff] [blame] | 3282 | boolean foundPrimary = false; |
| 3283 | |
Jeff Sharkey | ce14cd0 | 2015-12-07 15:35:42 -0700 | [diff] [blame] | 3284 | final ArrayList<StorageVolume> res = new ArrayList<>(); |
Jeff Sharkey | 04b4ba1 | 2019-12-15 22:42:42 -0700 | [diff] [blame] | 3285 | final ArraySet<String> resUuids = new ArraySet<>(); |
Jeff Sharkey | 4887789 | 2015-03-18 11:27:19 -0700 | [diff] [blame] | 3286 | synchronized (mLock) { |
Jeff Sharkey | 1b8ef7e | 2015-04-03 17:14:45 -0700 | [diff] [blame] | 3287 | for (int i = 0; i < mVolumes.size(); i++) { |
| 3288 | final VolumeInfo vol = mVolumes.valueAt(i); |
Jeff Sharkey | c02bfae | 2016-03-27 15:06:53 -0600 | [diff] [blame] | 3289 | switch (vol.getType()) { |
| 3290 | case VolumeInfo.TYPE_PUBLIC: |
Risan | 05c41e6 | 2018-10-29 08:57:43 +0900 | [diff] [blame] | 3291 | case VolumeInfo.TYPE_STUB: |
Jeff Sharkey | c02bfae | 2016-03-27 15:06:53 -0600 | [diff] [blame] | 3292 | break; |
Zim | 17be6f9 | 2019-09-25 14:37:55 +0100 | [diff] [blame] | 3293 | case VolumeInfo.TYPE_EMULATED: |
| 3294 | if (vol.getMountUserId() == userId) { |
| 3295 | break; |
| 3296 | } |
| 3297 | // Skip if emulated volume not for userId |
Jeff Sharkey | c02bfae | 2016-03-27 15:06:53 -0600 | [diff] [blame] | 3298 | default: |
| 3299 | continue; |
| 3300 | } |
| 3301 | |
| 3302 | boolean match = false; |
| 3303 | if (forWrite) { |
| 3304 | match = vol.isVisibleForWrite(userId); |
| 3305 | } else { |
Felipe Leme | 123a0e7 | 2016-06-10 11:09:11 -0700 | [diff] [blame] | 3306 | match = vol.isVisibleForRead(userId) |
| 3307 | || (includeInvisible && vol.getPath() != null); |
Jeff Sharkey | c02bfae | 2016-03-27 15:06:53 -0600 | [diff] [blame] | 3308 | } |
| 3309 | if (!match) continue; |
| 3310 | |
| 3311 | boolean reportUnmounted = false; |
Narayan Kamath | 157dd1d | 2019-06-12 13:06:30 +0100 | [diff] [blame] | 3312 | if (!systemUserUnlocked) { |
| 3313 | reportUnmounted = true; |
| 3314 | } else if ((vol.getType() == VolumeInfo.TYPE_EMULATED) && !userKeyUnlocked) { |
Jeff Sharkey | c02bfae | 2016-03-27 15:06:53 -0600 | [diff] [blame] | 3315 | reportUnmounted = true; |
| 3316 | } else if (!storagePermission && !realState) { |
| 3317 | reportUnmounted = true; |
| 3318 | } |
| 3319 | |
| 3320 | final StorageVolume userVol = vol.buildStorageVolume(mContext, userId, |
| 3321 | reportUnmounted); |
| 3322 | if (vol.isPrimary()) { |
| 3323 | res.add(0, userVol); |
| 3324 | foundPrimary = true; |
| 3325 | } else { |
| 3326 | res.add(userVol); |
Jeff Sharkey | b049e21 | 2012-09-07 23:16:01 -0700 | [diff] [blame] | 3327 | } |
Jeff Sharkey | 04b4ba1 | 2019-12-15 22:42:42 -0700 | [diff] [blame] | 3328 | resUuids.add(userVol.getUuid()); |
Mike Lockwood | 8fa5f80 | 2011-03-24 08:12:30 -0700 | [diff] [blame] | 3329 | } |
Jeff Sharkey | 04b4ba1 | 2019-12-15 22:42:42 -0700 | [diff] [blame] | 3330 | |
| 3331 | if (includeRecent) { |
| 3332 | final long lastWeek = System.currentTimeMillis() - DateUtils.WEEK_IN_MILLIS; |
| 3333 | for (int i = 0; i < mRecords.size(); i++) { |
| 3334 | final VolumeRecord rec = mRecords.valueAt(i); |
| 3335 | |
| 3336 | // Skip if we've already included it above |
| 3337 | if (resUuids.contains(rec.fsUuid)) continue; |
| 3338 | |
| 3339 | // Treat as recent if mounted within the last week |
| 3340 | if (rec.lastSeenMillis > 0 && rec.lastSeenMillis < lastWeek) { |
| 3341 | final StorageVolume userVol = rec.buildStorageVolume(mContext); |
| 3342 | res.add(userVol); |
| 3343 | resUuids.add(userVol.getUuid()); |
| 3344 | } |
| 3345 | } |
| 3346 | } |
| 3347 | } |
| 3348 | |
| 3349 | // Synthesize a volume for preloaded media under demo users, so that |
| 3350 | // it's scanned into MediaStore |
| 3351 | if (userIsDemo) { |
| 3352 | final String id = "demo"; |
| 3353 | final File path = Environment.getDataPreloadsMediaDirectory(); |
| 3354 | final boolean primary = false; |
| 3355 | final boolean removable = false; |
| 3356 | final boolean emulated = true; |
| 3357 | final boolean allowMassStorage = false; |
| 3358 | final long maxFileSize = 0; |
| 3359 | final UserHandle user = new UserHandle(userId); |
| 3360 | final String envState = Environment.MEDIA_MOUNTED_READ_ONLY; |
| 3361 | final String description = mContext.getString(android.R.string.unknownName); |
| 3362 | |
| 3363 | res.add(new StorageVolume(id, path, path, description, primary, removable, |
| 3364 | emulated, allowMassStorage, maxFileSize, user, id, envState)); |
Mike Lockwood | 8fa5f80 | 2011-03-24 08:12:30 -0700 | [diff] [blame] | 3365 | } |
Jeff Sharkey | 4887789 | 2015-03-18 11:27:19 -0700 | [diff] [blame] | 3366 | |
| 3367 | if (!foundPrimary) { |
Jeff Sharkey | 11697f5 | 2018-12-13 10:14:42 -0700 | [diff] [blame] | 3368 | Slog.w(TAG, "No primary storage defined yet; hacking together a stub"); |
Jeff Sharkey | 4887789 | 2015-03-18 11:27:19 -0700 | [diff] [blame] | 3369 | |
| 3370 | final boolean primaryPhysical = SystemProperties.getBoolean( |
Jeff Sharkey | 1b8ef7e | 2015-04-03 17:14:45 -0700 | [diff] [blame] | 3371 | StorageManager.PROP_PRIMARY_PHYSICAL, false); |
Jeff Sharkey | 4887789 | 2015-03-18 11:27:19 -0700 | [diff] [blame] | 3372 | |
| 3373 | final String id = "stub_primary"; |
| 3374 | final File path = Environment.getLegacyExternalStorageDirectory(); |
Jeff Sharkey | 1b8ef7e | 2015-04-03 17:14:45 -0700 | [diff] [blame] | 3375 | final String description = mContext.getString(android.R.string.unknownName); |
Jeff Sharkey | 4887789 | 2015-03-18 11:27:19 -0700 | [diff] [blame] | 3376 | final boolean primary = true; |
| 3377 | final boolean removable = primaryPhysical; |
| 3378 | final boolean emulated = !primaryPhysical; |
Jeff Sharkey | 4887789 | 2015-03-18 11:27:19 -0700 | [diff] [blame] | 3379 | final boolean allowMassStorage = false; |
| 3380 | final long maxFileSize = 0L; |
| 3381 | final UserHandle owner = new UserHandle(userId); |
| 3382 | final String uuid = null; |
Jeff Sharkey | 4887789 | 2015-03-18 11:27:19 -0700 | [diff] [blame] | 3383 | final String state = Environment.MEDIA_REMOVED; |
| 3384 | |
Jerry Zhang | 71938e1 | 2018-05-10 18:28:29 -0700 | [diff] [blame] | 3385 | res.add(0, new StorageVolume(id, path, path, |
Jerry Zhang | f9c5c25 | 2017-08-16 18:07:51 -0700 | [diff] [blame] | 3386 | description, primary, removable, emulated, |
Jeff Sharkey | 1b8ef7e | 2015-04-03 17:14:45 -0700 | [diff] [blame] | 3387 | allowMassStorage, maxFileSize, owner, uuid, state)); |
Jeff Sharkey | 4887789 | 2015-03-18 11:27:19 -0700 | [diff] [blame] | 3388 | } |
| 3389 | |
| 3390 | return res.toArray(new StorageVolume[res.size()]); |
Mike Lockwood | 8fa5f80 | 2011-03-24 08:12:30 -0700 | [diff] [blame] | 3391 | } |
| 3392 | |
Jeff Sharkey | 1b8ef7e | 2015-04-03 17:14:45 -0700 | [diff] [blame] | 3393 | @Override |
| 3394 | public DiskInfo[] getDisks() { |
| 3395 | synchronized (mLock) { |
| 3396 | final DiskInfo[] res = new DiskInfo[mDisks.size()]; |
| 3397 | for (int i = 0; i < mDisks.size(); i++) { |
| 3398 | res[i] = mDisks.valueAt(i); |
| 3399 | } |
| 3400 | return res; |
| 3401 | } |
| 3402 | } |
| 3403 | |
| 3404 | @Override |
Jeff Sharkey | d95d3bf | 2015-04-14 21:39:44 -0700 | [diff] [blame] | 3405 | public VolumeInfo[] getVolumes(int flags) { |
Jeff Sharkey | 1b8ef7e | 2015-04-03 17:14:45 -0700 | [diff] [blame] | 3406 | synchronized (mLock) { |
| 3407 | final VolumeInfo[] res = new VolumeInfo[mVolumes.size()]; |
| 3408 | for (int i = 0; i < mVolumes.size(); i++) { |
| 3409 | res[i] = mVolumes.valueAt(i); |
| 3410 | } |
| 3411 | return res; |
| 3412 | } |
| 3413 | } |
| 3414 | |
Jeff Sharkey | b36586a | 2015-04-27 08:42:28 -0700 | [diff] [blame] | 3415 | @Override |
| 3416 | public VolumeRecord[] getVolumeRecords(int flags) { |
| 3417 | synchronized (mLock) { |
| 3418 | final VolumeRecord[] res = new VolumeRecord[mRecords.size()]; |
| 3419 | for (int i = 0; i < mRecords.size(); i++) { |
| 3420 | res[i] = mRecords.valueAt(i); |
| 3421 | } |
| 3422 | return res; |
| 3423 | } |
| 3424 | } |
| 3425 | |
Jeff Sharkey | 9bed070 | 2017-01-23 20:37:05 -0700 | [diff] [blame] | 3426 | @Override |
| 3427 | public long getCacheQuotaBytes(String volumeUuid, int uid) { |
| 3428 | if (uid != Binder.getCallingUid()) { |
| 3429 | mContext.enforceCallingPermission(android.Manifest.permission.STORAGE_INTERNAL, TAG); |
| 3430 | } |
Daniel Nishi | 80fdb01 | 2017-03-09 14:30:07 -0800 | [diff] [blame] | 3431 | final long token = Binder.clearCallingIdentity(); |
| 3432 | final StorageStatsManager stats = mContext.getSystemService(StorageStatsManager.class); |
| 3433 | try { |
| 3434 | return stats.getCacheQuotaBytes(volumeUuid, uid); |
| 3435 | } finally { |
| 3436 | Binder.restoreCallingIdentity(token); |
| 3437 | } |
Jeff Sharkey | 9bed070 | 2017-01-23 20:37:05 -0700 | [diff] [blame] | 3438 | } |
| 3439 | |
| 3440 | @Override |
| 3441 | public long getCacheSizeBytes(String volumeUuid, int uid) { |
| 3442 | if (uid != Binder.getCallingUid()) { |
| 3443 | mContext.enforceCallingPermission(android.Manifest.permission.STORAGE_INTERNAL, TAG); |
| 3444 | } |
| 3445 | final long token = Binder.clearCallingIdentity(); |
| 3446 | try { |
| 3447 | return mContext.getSystemService(StorageStatsManager.class) |
| 3448 | .queryStatsForUid(volumeUuid, uid).getCacheBytes(); |
Jeff Sharkey | 789a8fc | 2017-04-16 13:18:35 -0600 | [diff] [blame] | 3449 | } catch (IOException e) { |
| 3450 | throw new ParcelableException(e); |
Jeff Sharkey | 9bed070 | 2017-01-23 20:37:05 -0700 | [diff] [blame] | 3451 | } finally { |
| 3452 | Binder.restoreCallingIdentity(token); |
| 3453 | } |
| 3454 | } |
| 3455 | |
Jeff Sharkey | 35e46d2 | 2017-06-09 10:01:20 -0600 | [diff] [blame] | 3456 | private int adjustAllocateFlags(int flags, int callingUid, String callingPackage) { |
| 3457 | // Require permission to allocate aggressively |
| 3458 | if ((flags & StorageManager.FLAG_ALLOCATE_AGGRESSIVE) != 0) { |
Jeff Sharkey | d5d5e92 | 2017-02-21 10:51:23 -0700 | [diff] [blame] | 3459 | mContext.enforceCallingOrSelfPermission( |
| 3460 | android.Manifest.permission.ALLOCATE_AGGRESSIVE, TAG); |
| 3461 | } |
| 3462 | |
Jeff Sharkey | 35e46d2 | 2017-06-09 10:01:20 -0600 | [diff] [blame] | 3463 | // Apps normally can't directly defy reserved space |
| 3464 | flags &= ~StorageManager.FLAG_ALLOCATE_DEFY_ALL_RESERVED; |
| 3465 | flags &= ~StorageManager.FLAG_ALLOCATE_DEFY_HALF_RESERVED; |
| 3466 | |
| 3467 | // However, if app is actively using the camera, then we're willing to |
| 3468 | // clear up to half of the reserved cache space, since the user might be |
| 3469 | // trying to capture an important memory. |
| 3470 | final AppOpsManager appOps = mContext.getSystemService(AppOpsManager.class); |
| 3471 | final long token = Binder.clearCallingIdentity(); |
| 3472 | try { |
| 3473 | if (appOps.isOperationActive(AppOpsManager.OP_CAMERA, callingUid, callingPackage)) { |
| 3474 | Slog.d(TAG, "UID " + callingUid + " is actively using camera;" |
| 3475 | + " letting them defy reserved cached data"); |
| 3476 | flags |= StorageManager.FLAG_ALLOCATE_DEFY_HALF_RESERVED; |
| 3477 | } |
| 3478 | } finally { |
| 3479 | Binder.restoreCallingIdentity(token); |
| 3480 | } |
| 3481 | |
| 3482 | return flags; |
| 3483 | } |
| 3484 | |
| 3485 | @Override |
| 3486 | public long getAllocatableBytes(String volumeUuid, int flags, String callingPackage) { |
| 3487 | flags = adjustAllocateFlags(flags, Binder.getCallingUid(), callingPackage); |
| 3488 | |
| 3489 | final StorageManager storage = mContext.getSystemService(StorageManager.class); |
| 3490 | final StorageStatsManager stats = mContext.getSystemService(StorageStatsManager.class); |
Jeff Sharkey | d5d5e92 | 2017-02-21 10:51:23 -0700 | [diff] [blame] | 3491 | final long token = Binder.clearCallingIdentity(); |
| 3492 | try { |
| 3493 | // In general, apps can allocate as much space as they want, except |
| 3494 | // 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] | 3495 | // the low disk warning space. To avoid user confusion, this logic |
| 3496 | // should be kept in sync with getFreeBytes(). |
Jeff Sharkey | d5d5e92 | 2017-02-21 10:51:23 -0700 | [diff] [blame] | 3497 | final File path = storage.findPathForUuid(volumeUuid); |
Jeff Sharkey | ddff807 | 2017-05-26 13:10:46 -0600 | [diff] [blame] | 3498 | |
Noah Zimmt | b255807 | 2019-07-25 16:15:06 -0700 | [diff] [blame] | 3499 | long usable = 0; |
| 3500 | long lowReserved = 0; |
| 3501 | long fullReserved = 0; |
| 3502 | long cacheClearable = 0; |
Jeff Sharkey | ddff807 | 2017-05-26 13:10:46 -0600 | [diff] [blame] | 3503 | |
Noah Zimmt | b255807 | 2019-07-25 16:15:06 -0700 | [diff] [blame] | 3504 | if ((flags & StorageManager.FLAG_ALLOCATE_CACHE_ONLY) == 0) { |
| 3505 | usable = path.getUsableSpace(); |
| 3506 | lowReserved = storage.getStorageLowBytes(path); |
| 3507 | fullReserved = storage.getStorageFullBytes(path); |
| 3508 | } |
| 3509 | |
| 3510 | if ((flags & StorageManager.FLAG_ALLOCATE_NON_CACHE_ONLY) == 0 |
| 3511 | && stats.isQuotaSupported(volumeUuid)) { |
Jeff Sharkey | ddff807 | 2017-05-26 13:10:46 -0600 | [diff] [blame] | 3512 | final long cacheTotal = stats.getCacheBytes(volumeUuid); |
Jeff Sharkey | 35e46d2 | 2017-06-09 10:01:20 -0600 | [diff] [blame] | 3513 | final long cacheReserved = storage.getStorageCacheBytes(path, flags); |
Noah Zimmt | b255807 | 2019-07-25 16:15:06 -0700 | [diff] [blame] | 3514 | cacheClearable = Math.max(0, cacheTotal - cacheReserved); |
| 3515 | } |
Jeff Sharkey | ddff807 | 2017-05-26 13:10:46 -0600 | [diff] [blame] | 3516 | |
Noah Zimmt | b255807 | 2019-07-25 16:15:06 -0700 | [diff] [blame] | 3517 | if ((flags & StorageManager.FLAG_ALLOCATE_AGGRESSIVE) != 0) { |
| 3518 | return Math.max(0, (usable + cacheClearable) - fullReserved); |
Jeff Sharkey | d5d5e92 | 2017-02-21 10:51:23 -0700 | [diff] [blame] | 3519 | } else { |
Noah Zimmt | b255807 | 2019-07-25 16:15:06 -0700 | [diff] [blame] | 3520 | return Math.max(0, (usable + cacheClearable) - lowReserved); |
Jeff Sharkey | d5d5e92 | 2017-02-21 10:51:23 -0700 | [diff] [blame] | 3521 | } |
Jeff Sharkey | 789a8fc | 2017-04-16 13:18:35 -0600 | [diff] [blame] | 3522 | } catch (IOException e) { |
| 3523 | throw new ParcelableException(e); |
Jeff Sharkey | d5d5e92 | 2017-02-21 10:51:23 -0700 | [diff] [blame] | 3524 | } finally { |
| 3525 | Binder.restoreCallingIdentity(token); |
| 3526 | } |
Jeff Sharkey | 500ce9e | 2017-02-12 02:39:24 -0700 | [diff] [blame] | 3527 | } |
| 3528 | |
| 3529 | @Override |
Jeff Sharkey | 35e46d2 | 2017-06-09 10:01:20 -0600 | [diff] [blame] | 3530 | public void allocateBytes(String volumeUuid, long bytes, int flags, String callingPackage) { |
| 3531 | flags = adjustAllocateFlags(flags, Binder.getCallingUid(), callingPackage); |
Jeff Sharkey | d5d5e92 | 2017-02-21 10:51:23 -0700 | [diff] [blame] | 3532 | |
Noah Zimmt | b255807 | 2019-07-25 16:15:06 -0700 | [diff] [blame] | 3533 | final long allocatableBytes = getAllocatableBytes(volumeUuid, |
| 3534 | flags | StorageManager.FLAG_ALLOCATE_NON_CACHE_ONLY, callingPackage); |
Jeff Sharkey | d5d5e92 | 2017-02-21 10:51:23 -0700 | [diff] [blame] | 3535 | if (bytes > allocatableBytes) { |
Noah Zimmt | b255807 | 2019-07-25 16:15:06 -0700 | [diff] [blame] | 3536 | // If we don't have room without taking cache into account, check to see if we'd have |
| 3537 | // room if we included freeable cache space. |
| 3538 | final long cacheClearable = getAllocatableBytes(volumeUuid, |
| 3539 | flags | StorageManager.FLAG_ALLOCATE_CACHE_ONLY, callingPackage); |
| 3540 | if (bytes > allocatableBytes + cacheClearable) { |
| 3541 | throw new ParcelableException(new IOException("Failed to allocate " + bytes |
| 3542 | + " because only " + (allocatableBytes + cacheClearable) + " allocatable")); |
| 3543 | } |
Jeff Sharkey | d5d5e92 | 2017-02-21 10:51:23 -0700 | [diff] [blame] | 3544 | } |
| 3545 | |
Jeff Sharkey | 35e46d2 | 2017-06-09 10:01:20 -0600 | [diff] [blame] | 3546 | final StorageManager storage = mContext.getSystemService(StorageManager.class); |
Jeff Sharkey | d5d5e92 | 2017-02-21 10:51:23 -0700 | [diff] [blame] | 3547 | final long token = Binder.clearCallingIdentity(); |
| 3548 | try { |
Jeff Sharkey | 789a8fc | 2017-04-16 13:18:35 -0600 | [diff] [blame] | 3549 | // Free up enough disk space to satisfy both the requested allocation |
| 3550 | // and our low disk warning space. |
| 3551 | final File path = storage.findPathForUuid(volumeUuid); |
Jeff Sharkey | ddff807 | 2017-05-26 13:10:46 -0600 | [diff] [blame] | 3552 | if ((flags & StorageManager.FLAG_ALLOCATE_AGGRESSIVE) != 0) { |
| 3553 | bytes += storage.getStorageFullBytes(path); |
| 3554 | } else { |
| 3555 | bytes += storage.getStorageLowBytes(path); |
| 3556 | } |
Jeff Sharkey | 789a8fc | 2017-04-16 13:18:35 -0600 | [diff] [blame] | 3557 | |
Jeff Sharkey | 5790af0 | 2018-08-13 17:42:54 -0600 | [diff] [blame] | 3558 | mPmInternal.freeStorage(volumeUuid, bytes, flags); |
Jeff Sharkey | d5d5e92 | 2017-02-21 10:51:23 -0700 | [diff] [blame] | 3559 | } catch (IOException e) { |
| 3560 | throw new ParcelableException(e); |
| 3561 | } finally { |
| 3562 | Binder.restoreCallingIdentity(token); |
Jeff Sharkey | 500ce9e | 2017-02-12 02:39:24 -0700 | [diff] [blame] | 3563 | } |
| 3564 | } |
| 3565 | |
Sudheer Shanka | ff971bc | 2018-12-13 17:39:59 -0800 | [diff] [blame] | 3566 | private IAppOpsCallback.Stub mAppOpsCallback = new IAppOpsCallback.Stub() { |
| 3567 | @Override |
| 3568 | public void opChanged(int op, int uid, String packageName) throws RemoteException { |
| 3569 | if (!ENABLE_ISOLATED_STORAGE) return; |
| 3570 | |
Zim | 64726cb | 2019-11-27 13:06:34 +0000 | [diff] [blame] | 3571 | int mountMode = getMountMode(uid, packageName); |
| 3572 | boolean isUidActive = LocalServices.getService(ActivityManagerInternal.class) |
| 3573 | .getUidProcessState(uid) != PROCESS_STATE_NONEXISTENT; |
| 3574 | |
| 3575 | if (isUidActive) { |
| 3576 | remountUidExternalStorage(uid, mountMode); |
| 3577 | } |
Sudheer Shanka | ff971bc | 2018-12-13 17:39:59 -0800 | [diff] [blame] | 3578 | } |
| 3579 | }; |
| 3580 | |
Kenny Root | af9d667 | 2010-10-08 09:21:39 -0700 | [diff] [blame] | 3581 | private void addObbStateLocked(ObbState obbState) throws RemoteException { |
| 3582 | final IBinder binder = obbState.getBinder(); |
| 3583 | List<ObbState> obbStates = mObbMounts.get(binder); |
Kenny Root | 5919ac6 | 2010-10-05 09:49:40 -0700 | [diff] [blame] | 3584 | |
Kenny Root | af9d667 | 2010-10-08 09:21:39 -0700 | [diff] [blame] | 3585 | if (obbStates == null) { |
| 3586 | obbStates = new ArrayList<ObbState>(); |
| 3587 | mObbMounts.put(binder, obbStates); |
| 3588 | } else { |
| 3589 | for (final ObbState o : obbStates) { |
Jeff Sharkey | 4fbbda4 | 2012-09-24 18:34:07 -0700 | [diff] [blame] | 3590 | if (o.rawPath.equals(obbState.rawPath)) { |
Kenny Root | af9d667 | 2010-10-08 09:21:39 -0700 | [diff] [blame] | 3591 | throw new IllegalStateException("Attempt to add ObbState twice. " |
Sudheer Shanka | 2250d56 | 2016-11-07 15:41:02 -0800 | [diff] [blame] | 3592 | + "This indicates an error in the StorageManagerService logic."); |
Kenny Root | 5919ac6 | 2010-10-05 09:49:40 -0700 | [diff] [blame] | 3593 | } |
| 3594 | } |
Kenny Root | a02b8b0 | 2010-08-05 16:14:17 -0700 | [diff] [blame] | 3595 | } |
Kenny Root | af9d667 | 2010-10-08 09:21:39 -0700 | [diff] [blame] | 3596 | |
| 3597 | obbStates.add(obbState); |
| 3598 | try { |
| 3599 | obbState.link(); |
| 3600 | } catch (RemoteException e) { |
| 3601 | /* |
| 3602 | * The binder died before we could link it, so clean up our state |
| 3603 | * and return failure. |
| 3604 | */ |
| 3605 | obbStates.remove(obbState); |
| 3606 | if (obbStates.isEmpty()) { |
| 3607 | mObbMounts.remove(binder); |
| 3608 | } |
| 3609 | |
| 3610 | // Rethrow the error so mountObb can get it |
| 3611 | throw e; |
| 3612 | } |
| 3613 | |
Jeff Sharkey | 4fbbda4 | 2012-09-24 18:34:07 -0700 | [diff] [blame] | 3614 | mObbPathToStateMap.put(obbState.rawPath, obbState); |
Kenny Root | a02b8b0 | 2010-08-05 16:14:17 -0700 | [diff] [blame] | 3615 | } |
| 3616 | |
Kenny Root | af9d667 | 2010-10-08 09:21:39 -0700 | [diff] [blame] | 3617 | private void removeObbStateLocked(ObbState obbState) { |
| 3618 | final IBinder binder = obbState.getBinder(); |
| 3619 | final List<ObbState> obbStates = mObbMounts.get(binder); |
| 3620 | if (obbStates != null) { |
| 3621 | if (obbStates.remove(obbState)) { |
| 3622 | obbState.unlink(); |
Kenny Root | 05105f7 | 2010-09-22 17:29:43 -0700 | [diff] [blame] | 3623 | } |
Kenny Root | af9d667 | 2010-10-08 09:21:39 -0700 | [diff] [blame] | 3624 | if (obbStates.isEmpty()) { |
| 3625 | mObbMounts.remove(binder); |
| 3626 | } |
Kenny Root | a02b8b0 | 2010-08-05 16:14:17 -0700 | [diff] [blame] | 3627 | } |
Kenny Root | a02b8b0 | 2010-08-05 16:14:17 -0700 | [diff] [blame] | 3628 | |
Jeff Sharkey | 4fbbda4 | 2012-09-24 18:34:07 -0700 | [diff] [blame] | 3629 | mObbPathToStateMap.remove(obbState.rawPath); |
Kenny Root | 38cf886 | 2010-09-26 14:18:51 -0700 | [diff] [blame] | 3630 | } |
| 3631 | |
Kenny Root | a02b8b0 | 2010-08-05 16:14:17 -0700 | [diff] [blame] | 3632 | private class ObbActionHandler extends Handler { |
Kenny Root | a02b8b0 | 2010-08-05 16:14:17 -0700 | [diff] [blame] | 3633 | |
| 3634 | ObbActionHandler(Looper l) { |
| 3635 | super(l); |
| 3636 | } |
| 3637 | |
| 3638 | @Override |
| 3639 | public void handleMessage(Message msg) { |
| 3640 | switch (msg.what) { |
| 3641 | case OBB_RUN_ACTION: { |
Kenny Root | 480afe7 | 2010-10-07 10:17:50 -0700 | [diff] [blame] | 3642 | final ObbAction action = (ObbAction) msg.obj; |
Kenny Root | a02b8b0 | 2010-08-05 16:14:17 -0700 | [diff] [blame] | 3643 | |
| 3644 | if (DEBUG_OBB) |
| 3645 | Slog.i(TAG, "OBB_RUN_ACTION: " + action.toString()); |
| 3646 | |
Sudheer Shanka | 25469aa | 2018-08-27 15:50:23 -0700 | [diff] [blame] | 3647 | action.execute(this); |
Kenny Root | a02b8b0 | 2010-08-05 16:14:17 -0700 | [diff] [blame] | 3648 | break; |
| 3649 | } |
Kenny Root | af9d667 | 2010-10-08 09:21:39 -0700 | [diff] [blame] | 3650 | case OBB_FLUSH_MOUNT_STATE: { |
| 3651 | final String path = (String) msg.obj; |
| 3652 | |
| 3653 | if (DEBUG_OBB) |
| 3654 | Slog.i(TAG, "Flushing all OBB state for path " + path); |
| 3655 | |
| 3656 | synchronized (mObbMounts) { |
| 3657 | final List<ObbState> obbStatesToRemove = new LinkedList<ObbState>(); |
| 3658 | |
Jeff Sharkey | 4fbbda4 | 2012-09-24 18:34:07 -0700 | [diff] [blame] | 3659 | final Iterator<ObbState> i = mObbPathToStateMap.values().iterator(); |
Kenny Root | af9d667 | 2010-10-08 09:21:39 -0700 | [diff] [blame] | 3660 | while (i.hasNext()) { |
Jeff Sharkey | 4fbbda4 | 2012-09-24 18:34:07 -0700 | [diff] [blame] | 3661 | final ObbState state = i.next(); |
Kenny Root | af9d667 | 2010-10-08 09:21:39 -0700 | [diff] [blame] | 3662 | |
| 3663 | /* |
| 3664 | * If this entry's source file is in the volume path |
| 3665 | * that got unmounted, remove it because it's no |
| 3666 | * longer valid. |
| 3667 | */ |
Jeff Sharkey | 4fbbda4 | 2012-09-24 18:34:07 -0700 | [diff] [blame] | 3668 | if (state.canonicalPath.startsWith(path)) { |
| 3669 | obbStatesToRemove.add(state); |
Kenny Root | af9d667 | 2010-10-08 09:21:39 -0700 | [diff] [blame] | 3670 | } |
| 3671 | } |
| 3672 | |
| 3673 | for (final ObbState obbState : obbStatesToRemove) { |
| 3674 | if (DEBUG_OBB) |
Jeff Sharkey | 4fbbda4 | 2012-09-24 18:34:07 -0700 | [diff] [blame] | 3675 | Slog.i(TAG, "Removing state for " + obbState.rawPath); |
Kenny Root | af9d667 | 2010-10-08 09:21:39 -0700 | [diff] [blame] | 3676 | |
| 3677 | removeObbStateLocked(obbState); |
| 3678 | |
| 3679 | try { |
Jeff Sharkey | 4fbbda4 | 2012-09-24 18:34:07 -0700 | [diff] [blame] | 3680 | obbState.token.onObbResult(obbState.rawPath, obbState.nonce, |
Kenny Root | af9d667 | 2010-10-08 09:21:39 -0700 | [diff] [blame] | 3681 | OnObbStateChangeListener.UNMOUNTED); |
| 3682 | } catch (RemoteException e) { |
| 3683 | Slog.i(TAG, "Couldn't send unmount notification for OBB: " |
Jeff Sharkey | 4fbbda4 | 2012-09-24 18:34:07 -0700 | [diff] [blame] | 3684 | + obbState.rawPath); |
Kenny Root | af9d667 | 2010-10-08 09:21:39 -0700 | [diff] [blame] | 3685 | } |
| 3686 | } |
| 3687 | } |
| 3688 | break; |
| 3689 | } |
Kenny Root | a02b8b0 | 2010-08-05 16:14:17 -0700 | [diff] [blame] | 3690 | } |
| 3691 | } |
Kenny Root | a02b8b0 | 2010-08-05 16:14:17 -0700 | [diff] [blame] | 3692 | } |
| 3693 | |
Jeff Sharkey | 0095a82 | 2018-02-15 13:06:53 -0700 | [diff] [blame] | 3694 | private static class ObbException extends Exception { |
| 3695 | public final int status; |
| 3696 | |
| 3697 | public ObbException(int status, String message) { |
| 3698 | super(message); |
| 3699 | this.status = status; |
| 3700 | } |
| 3701 | |
| 3702 | public ObbException(int status, Throwable cause) { |
| 3703 | super(cause.getMessage(), cause); |
| 3704 | this.status = status; |
| 3705 | } |
| 3706 | } |
| 3707 | |
Kenny Root | a02b8b0 | 2010-08-05 16:14:17 -0700 | [diff] [blame] | 3708 | abstract class ObbAction { |
Kenny Root | a02b8b0 | 2010-08-05 16:14:17 -0700 | [diff] [blame] | 3709 | |
| 3710 | ObbState mObbState; |
| 3711 | |
| 3712 | ObbAction(ObbState obbState) { |
| 3713 | mObbState = obbState; |
| 3714 | } |
| 3715 | |
| 3716 | public void execute(ObbActionHandler handler) { |
| 3717 | try { |
| 3718 | if (DEBUG_OBB) |
Ben Komalo | 444eca2 | 2011-09-01 15:17:44 -0700 | [diff] [blame] | 3719 | Slog.i(TAG, "Starting to execute action: " + toString()); |
Sudheer Shanka | 25469aa | 2018-08-27 15:50:23 -0700 | [diff] [blame] | 3720 | handleExecute(); |
Jeff Sharkey | 0095a82 | 2018-02-15 13:06:53 -0700 | [diff] [blame] | 3721 | } catch (ObbException e) { |
| 3722 | notifyObbStateChange(e); |
Kenny Root | a02b8b0 | 2010-08-05 16:14:17 -0700 | [diff] [blame] | 3723 | } |
| 3724 | } |
| 3725 | |
Jeff Sharkey | 0095a82 | 2018-02-15 13:06:53 -0700 | [diff] [blame] | 3726 | abstract void handleExecute() throws ObbException; |
Kenny Root | 38cf886 | 2010-09-26 14:18:51 -0700 | [diff] [blame] | 3727 | |
Jeff Sharkey | 0095a82 | 2018-02-15 13:06:53 -0700 | [diff] [blame] | 3728 | protected void notifyObbStateChange(ObbException e) { |
| 3729 | Slog.w(TAG, e); |
| 3730 | notifyObbStateChange(e.status); |
| 3731 | } |
| 3732 | |
| 3733 | protected void notifyObbStateChange(int status) { |
Kenny Root | af9d667 | 2010-10-08 09:21:39 -0700 | [diff] [blame] | 3734 | if (mObbState == null || mObbState.token == null) { |
| 3735 | return; |
| 3736 | } |
| 3737 | |
Kenny Root | 38cf886 | 2010-09-26 14:18:51 -0700 | [diff] [blame] | 3738 | try { |
Jeff Sharkey | 4fbbda4 | 2012-09-24 18:34:07 -0700 | [diff] [blame] | 3739 | mObbState.token.onObbResult(mObbState.rawPath, mObbState.nonce, status); |
Kenny Root | 38cf886 | 2010-09-26 14:18:51 -0700 | [diff] [blame] | 3740 | } catch (RemoteException e) { |
Sudheer Shanka | 2250d56 | 2016-11-07 15:41:02 -0800 | [diff] [blame] | 3741 | Slog.w(TAG, "StorageEventListener went away while calling onObbStateChanged"); |
Kenny Root | 38cf886 | 2010-09-26 14:18:51 -0700 | [diff] [blame] | 3742 | } |
| 3743 | } |
Kenny Root | a02b8b0 | 2010-08-05 16:14:17 -0700 | [diff] [blame] | 3744 | } |
| 3745 | |
| 3746 | class MountObbAction extends ObbAction { |
Ben Komalo | 444eca2 | 2011-09-01 15:17:44 -0700 | [diff] [blame] | 3747 | private final String mKey; |
Jeff Sharkey | 4fbbda4 | 2012-09-24 18:34:07 -0700 | [diff] [blame] | 3748 | private final int mCallingUid; |
Sudheer Shanka | 25469aa | 2018-08-27 15:50:23 -0700 | [diff] [blame] | 3749 | private ObbInfo mObbInfo; |
Kenny Root | a02b8b0 | 2010-08-05 16:14:17 -0700 | [diff] [blame] | 3750 | |
Sudheer Shanka | 25469aa | 2018-08-27 15:50:23 -0700 | [diff] [blame] | 3751 | MountObbAction(ObbState obbState, String key, int callingUid, ObbInfo obbInfo) { |
Kenny Root | a02b8b0 | 2010-08-05 16:14:17 -0700 | [diff] [blame] | 3752 | super(obbState); |
| 3753 | mKey = key; |
Jeff Sharkey | 4fbbda4 | 2012-09-24 18:34:07 -0700 | [diff] [blame] | 3754 | mCallingUid = callingUid; |
Sudheer Shanka | 25469aa | 2018-08-27 15:50:23 -0700 | [diff] [blame] | 3755 | mObbInfo = obbInfo; |
Kenny Root | a02b8b0 | 2010-08-05 16:14:17 -0700 | [diff] [blame] | 3756 | } |
| 3757 | |
Jason parks | 5af0b91 | 2010-11-29 09:05:25 -0600 | [diff] [blame] | 3758 | @Override |
Jeff Sharkey | 0095a82 | 2018-02-15 13:06:53 -0700 | [diff] [blame] | 3759 | public void handleExecute() throws ObbException { |
Kenny Root | af9d667 | 2010-10-08 09:21:39 -0700 | [diff] [blame] | 3760 | warnOnNotMounted(); |
| 3761 | |
Sudheer Shanka | 25469aa | 2018-08-27 15:50:23 -0700 | [diff] [blame] | 3762 | if (!isUidOwnerOfPackageOrSystem(mObbInfo.packageName, mCallingUid)) { |
Jeff Sharkey | 0095a82 | 2018-02-15 13:06:53 -0700 | [diff] [blame] | 3763 | throw new ObbException(ERROR_PERMISSION_DENIED, "Denied attempt to mount OBB " |
Sudheer Shanka | 25469aa | 2018-08-27 15:50:23 -0700 | [diff] [blame] | 3764 | + mObbInfo.filename + " which is owned by " + mObbInfo.packageName); |
Kenny Root | a02b8b0 | 2010-08-05 16:14:17 -0700 | [diff] [blame] | 3765 | } |
| 3766 | |
Kenny Root | af9d667 | 2010-10-08 09:21:39 -0700 | [diff] [blame] | 3767 | final boolean isMounted; |
| 3768 | synchronized (mObbMounts) { |
Jeff Sharkey | 4fbbda4 | 2012-09-24 18:34:07 -0700 | [diff] [blame] | 3769 | isMounted = mObbPathToStateMap.containsKey(mObbState.rawPath); |
Kenny Root | af9d667 | 2010-10-08 09:21:39 -0700 | [diff] [blame] | 3770 | } |
| 3771 | if (isMounted) { |
Jeff Sharkey | 0095a82 | 2018-02-15 13:06:53 -0700 | [diff] [blame] | 3772 | throw new ObbException(ERROR_ALREADY_MOUNTED, |
Sudheer Shanka | 25469aa | 2018-08-27 15:50:23 -0700 | [diff] [blame] | 3773 | "Attempt to mount OBB which is already mounted: " + mObbInfo.filename); |
Kenny Root | af9d667 | 2010-10-08 09:21:39 -0700 | [diff] [blame] | 3774 | } |
| 3775 | |
Kenny Root | af9d667 | 2010-10-08 09:21:39 -0700 | [diff] [blame] | 3776 | final String hashedKey; |
Jeff Sharkey | 41cd681 | 2017-09-11 10:32:17 -0600 | [diff] [blame] | 3777 | final String binderKey; |
Kenny Root | af9d667 | 2010-10-08 09:21:39 -0700 | [diff] [blame] | 3778 | if (mKey == null) { |
| 3779 | hashedKey = "none"; |
Jeff Sharkey | 41cd681 | 2017-09-11 10:32:17 -0600 | [diff] [blame] | 3780 | binderKey = ""; |
Kenny Root | af9d667 | 2010-10-08 09:21:39 -0700 | [diff] [blame] | 3781 | } else { |
Kenny Root | af9d667 | 2010-10-08 09:21:39 -0700 | [diff] [blame] | 3782 | try { |
Kenny Root | 3b1abba | 2010-10-13 15:00:07 -0700 | [diff] [blame] | 3783 | SecretKeyFactory factory = SecretKeyFactory.getInstance("PBKDF2WithHmacSHA1"); |
| 3784 | |
Sudheer Shanka | 25469aa | 2018-08-27 15:50:23 -0700 | [diff] [blame] | 3785 | KeySpec ks = new PBEKeySpec(mKey.toCharArray(), mObbInfo.salt, |
Kenny Root | 3b1abba | 2010-10-13 15:00:07 -0700 | [diff] [blame] | 3786 | PBKDF2_HASH_ROUNDS, CRYPTO_ALGORITHM_KEY_SIZE); |
| 3787 | SecretKey key = factory.generateSecret(ks); |
| 3788 | BigInteger bi = new BigInteger(key.getEncoded()); |
| 3789 | hashedKey = bi.toString(16); |
Jeff Sharkey | 41cd681 | 2017-09-11 10:32:17 -0600 | [diff] [blame] | 3790 | binderKey = hashedKey; |
Jeff Sharkey | 0095a82 | 2018-02-15 13:06:53 -0700 | [diff] [blame] | 3791 | } catch (GeneralSecurityException e) { |
| 3792 | throw new ObbException(ERROR_INTERNAL, e); |
Kenny Root | 38cf886 | 2010-09-26 14:18:51 -0700 | [diff] [blame] | 3793 | } |
Kenny Root | af9d667 | 2010-10-08 09:21:39 -0700 | [diff] [blame] | 3794 | } |
Kenny Root | 38cf886 | 2010-09-26 14:18:51 -0700 | [diff] [blame] | 3795 | |
Kenny Root | af9d667 | 2010-10-08 09:21:39 -0700 | [diff] [blame] | 3796 | try { |
Jeff Sharkey | 5440279 | 2017-09-15 16:05:19 -0600 | [diff] [blame] | 3797 | mObbState.volId = mVold.createObb(mObbState.canonicalPath, binderKey, |
| 3798 | mObbState.ownerGid); |
Zim | 95eca1d | 2019-11-15 18:03:00 +0000 | [diff] [blame] | 3799 | mVold.mount(mObbState.volId, 0, -1, null); |
Kenny Root | a02b8b0 | 2010-08-05 16:14:17 -0700 | [diff] [blame] | 3800 | |
Kenny Root | af9d667 | 2010-10-08 09:21:39 -0700 | [diff] [blame] | 3801 | if (DEBUG_OBB) |
Xiaohui Chen | 621b3fc | 2015-10-02 14:41:42 -0700 | [diff] [blame] | 3802 | Slog.d(TAG, "Successfully mounted OBB " + mObbState.canonicalPath); |
Kenny Root | af9d667 | 2010-10-08 09:21:39 -0700 | [diff] [blame] | 3803 | |
| 3804 | synchronized (mObbMounts) { |
| 3805 | addObbStateLocked(mObbState); |
| 3806 | } |
| 3807 | |
Jeff Sharkey | 0095a82 | 2018-02-15 13:06:53 -0700 | [diff] [blame] | 3808 | notifyObbStateChange(MOUNTED); |
| 3809 | } catch (Exception e) { |
| 3810 | throw new ObbException(ERROR_COULD_NOT_MOUNT, e); |
Kenny Root | 02c8730 | 2010-07-01 08:10:18 -0700 | [diff] [blame] | 3811 | } |
| 3812 | } |
| 3813 | |
Jason parks | 5af0b91 | 2010-11-29 09:05:25 -0600 | [diff] [blame] | 3814 | @Override |
Kenny Root | a02b8b0 | 2010-08-05 16:14:17 -0700 | [diff] [blame] | 3815 | public String toString() { |
| 3816 | StringBuilder sb = new StringBuilder(); |
| 3817 | sb.append("MountObbAction{"); |
Jeff Sharkey | 4fbbda4 | 2012-09-24 18:34:07 -0700 | [diff] [blame] | 3818 | sb.append(mObbState); |
Kenny Root | a02b8b0 | 2010-08-05 16:14:17 -0700 | [diff] [blame] | 3819 | sb.append('}'); |
| 3820 | return sb.toString(); |
| 3821 | } |
| 3822 | } |
| 3823 | |
| 3824 | class UnmountObbAction extends ObbAction { |
Ben Komalo | 444eca2 | 2011-09-01 15:17:44 -0700 | [diff] [blame] | 3825 | private final boolean mForceUnmount; |
Kenny Root | a02b8b0 | 2010-08-05 16:14:17 -0700 | [diff] [blame] | 3826 | |
| 3827 | UnmountObbAction(ObbState obbState, boolean force) { |
| 3828 | super(obbState); |
| 3829 | mForceUnmount = force; |
| 3830 | } |
| 3831 | |
Jason parks | 5af0b91 | 2010-11-29 09:05:25 -0600 | [diff] [blame] | 3832 | @Override |
Jeff Sharkey | 0095a82 | 2018-02-15 13:06:53 -0700 | [diff] [blame] | 3833 | public void handleExecute() throws ObbException { |
Kenny Root | af9d667 | 2010-10-08 09:21:39 -0700 | [diff] [blame] | 3834 | warnOnNotMounted(); |
| 3835 | |
Jeff Sharkey | 4fbbda4 | 2012-09-24 18:34:07 -0700 | [diff] [blame] | 3836 | final ObbState existingState; |
Kenny Root | 38cf886 | 2010-09-26 14:18:51 -0700 | [diff] [blame] | 3837 | synchronized (mObbMounts) { |
Jeff Sharkey | 4fbbda4 | 2012-09-24 18:34:07 -0700 | [diff] [blame] | 3838 | existingState = mObbPathToStateMap.get(mObbState.rawPath); |
Kenny Root | af9d667 | 2010-10-08 09:21:39 -0700 | [diff] [blame] | 3839 | } |
Kenny Root | 38cf886 | 2010-09-26 14:18:51 -0700 | [diff] [blame] | 3840 | |
Jeff Sharkey | 4fbbda4 | 2012-09-24 18:34:07 -0700 | [diff] [blame] | 3841 | if (existingState == null) { |
Jeff Sharkey | 0095a82 | 2018-02-15 13:06:53 -0700 | [diff] [blame] | 3842 | throw new ObbException(ERROR_NOT_MOUNTED, "Missing existingState"); |
Kenny Root | af9d667 | 2010-10-08 09:21:39 -0700 | [diff] [blame] | 3843 | } |
| 3844 | |
Jeff Sharkey | 4fbbda4 | 2012-09-24 18:34:07 -0700 | [diff] [blame] | 3845 | if (existingState.ownerGid != mObbState.ownerGid) { |
Jeff Sharkey | 0095a82 | 2018-02-15 13:06:53 -0700 | [diff] [blame] | 3846 | notifyObbStateChange(new ObbException(ERROR_PERMISSION_DENIED, |
| 3847 | "Permission denied to unmount OBB " + existingState.rawPath |
| 3848 | + " (owned by GID " + existingState.ownerGid + ")")); |
Kenny Root | af9d667 | 2010-10-08 09:21:39 -0700 | [diff] [blame] | 3849 | return; |
| 3850 | } |
| 3851 | |
Kenny Root | af9d667 | 2010-10-08 09:21:39 -0700 | [diff] [blame] | 3852 | try { |
Jeff Sharkey | 5440279 | 2017-09-15 16:05:19 -0600 | [diff] [blame] | 3853 | mVold.unmount(mObbState.volId); |
| 3854 | mVold.destroyObb(mObbState.volId); |
| 3855 | mObbState.volId = null; |
Kenny Root | a02b8b0 | 2010-08-05 16:14:17 -0700 | [diff] [blame] | 3856 | |
Kenny Root | af9d667 | 2010-10-08 09:21:39 -0700 | [diff] [blame] | 3857 | synchronized (mObbMounts) { |
Jeff Sharkey | 4fbbda4 | 2012-09-24 18:34:07 -0700 | [diff] [blame] | 3858 | removeObbStateLocked(existingState); |
Kenny Root | 38cf886 | 2010-09-26 14:18:51 -0700 | [diff] [blame] | 3859 | } |
| 3860 | |
Jeff Sharkey | 0095a82 | 2018-02-15 13:06:53 -0700 | [diff] [blame] | 3861 | notifyObbStateChange(UNMOUNTED); |
| 3862 | } catch (Exception e) { |
| 3863 | throw new ObbException(ERROR_COULD_NOT_UNMOUNT, e); |
Kenny Root | a02b8b0 | 2010-08-05 16:14:17 -0700 | [diff] [blame] | 3864 | } |
| 3865 | } |
| 3866 | |
Jason parks | 5af0b91 | 2010-11-29 09:05:25 -0600 | [diff] [blame] | 3867 | @Override |
Kenny Root | a02b8b0 | 2010-08-05 16:14:17 -0700 | [diff] [blame] | 3868 | public String toString() { |
| 3869 | StringBuilder sb = new StringBuilder(); |
| 3870 | sb.append("UnmountObbAction{"); |
Jeff Sharkey | 4fbbda4 | 2012-09-24 18:34:07 -0700 | [diff] [blame] | 3871 | sb.append(mObbState); |
Kenny Root | a02b8b0 | 2010-08-05 16:14:17 -0700 | [diff] [blame] | 3872 | sb.append(",force="); |
| 3873 | sb.append(mForceUnmount); |
Kenny Root | a02b8b0 | 2010-08-05 16:14:17 -0700 | [diff] [blame] | 3874 | sb.append('}'); |
| 3875 | return sb.toString(); |
| 3876 | } |
Kenny Root | 02c8730 | 2010-07-01 08:10:18 -0700 | [diff] [blame] | 3877 | } |
Kenny Root | 38cf886 | 2010-09-26 14:18:51 -0700 | [diff] [blame] | 3878 | |
Jeff Sharkey | 7e19f53 | 2017-11-06 13:54:11 -0700 | [diff] [blame] | 3879 | private void dispatchOnStatus(IVoldTaskListener listener, int status, |
| 3880 | PersistableBundle extras) { |
| 3881 | if (listener != null) { |
| 3882 | try { |
| 3883 | listener.onStatus(status, extras); |
| 3884 | } catch (RemoteException ignored) { |
| 3885 | } |
| 3886 | } |
| 3887 | } |
| 3888 | |
| 3889 | private void dispatchOnFinished(IVoldTaskListener listener, int status, |
| 3890 | PersistableBundle extras) { |
| 3891 | if (listener != null) { |
| 3892 | try { |
| 3893 | listener.onFinished(status, extras); |
| 3894 | } catch (RemoteException ignored) { |
| 3895 | } |
| 3896 | } |
| 3897 | } |
| 3898 | |
Sudheer Shanka | 3a0df3b | 2018-12-12 12:43:43 -0800 | [diff] [blame] | 3899 | private int getMountMode(int uid, String packageName) { |
Jeff Sharkey | 6fd6994 | 2019-03-26 17:53:35 -0600 | [diff] [blame] | 3900 | final int mode = getMountModeInternal(uid, packageName); |
| 3901 | if (LOCAL_LOGV) { |
| 3902 | Slog.v(TAG, "Resolved mode " + mode + " for " + packageName + "/" |
| 3903 | + UserHandle.formatUid(uid)); |
| 3904 | } |
| 3905 | return mode; |
| 3906 | } |
| 3907 | |
| 3908 | private int getMountModeInternal(int uid, String packageName) { |
Sudheer Shanka | 3a0df3b | 2018-12-12 12:43:43 -0800 | [diff] [blame] | 3909 | try { |
Jeff Sharkey | 6fd6994 | 2019-03-26 17:53:35 -0600 | [diff] [blame] | 3910 | // Get some easy cases out of the way first |
Sudheer Shanka | 3a0df3b | 2018-12-12 12:43:43 -0800 | [diff] [blame] | 3911 | if (Process.isIsolated(uid)) { |
| 3912 | return Zygote.MOUNT_EXTERNAL_NONE; |
| 3913 | } |
Sudheer Shanka | b161398 | 2019-05-16 16:55:50 -0700 | [diff] [blame] | 3914 | |
| 3915 | final String[] packagesForUid = mIPackageManager.getPackagesForUid(uid); |
Sudheer Shanka | 1df72db | 2019-05-24 10:59:52 -0700 | [diff] [blame] | 3916 | if (ArrayUtils.isEmpty(packagesForUid)) { |
| 3917 | // It's possible the package got uninstalled already, so just ignore. |
| 3918 | return Zygote.MOUNT_EXTERNAL_NONE; |
| 3919 | } |
Sudheer Shanka | b161398 | 2019-05-16 16:55:50 -0700 | [diff] [blame] | 3920 | if (packageName == null) { |
| 3921 | packageName = packagesForUid[0]; |
| 3922 | } |
| 3923 | |
Jeff Sharkey | 6fd6994 | 2019-03-26 17:53:35 -0600 | [diff] [blame] | 3924 | if (mPmInternal.isInstantApp(packageName, UserHandle.getUserId(uid))) { |
Winson Chiu | 5118d49 | 2019-05-15 18:24:06 +0000 | [diff] [blame] | 3925 | return Zygote.MOUNT_EXTERNAL_NONE; |
Jeff Sharkey | 6fd6994 | 2019-03-26 17:53:35 -0600 | [diff] [blame] | 3926 | } |
| 3927 | |
Martijn Coenen | 44db1ac | 2019-12-03 16:06:19 +0100 | [diff] [blame] | 3928 | if (mIsFuseEnabled && mMediaStoreAuthorityAppId == UserHandle.getAppId(uid)) { |
| 3929 | // Determine if caller requires pass_through mount; note that we do this for |
| 3930 | // all processes that share a UID with MediaProvider; but this is fine, since |
| 3931 | // those processes anyway share the same rights as MediaProvider. |
Zim | 74a9bba | 2019-09-03 20:49:13 +0100 | [diff] [blame] | 3932 | return Zygote.MOUNT_EXTERNAL_PASS_THROUGH; |
| 3933 | } |
| 3934 | |
Martijn Coenen | 496ac00 | 2020-01-08 14:55:53 +0100 | [diff] [blame] | 3935 | if (mIsFuseEnabled && mDownloadsAuthorityAppId == UserHandle.getAppId(uid)) { |
| 3936 | // DownloadManager can write in app-private directories on behalf of apps; |
| 3937 | // give it write access to Android/ |
| 3938 | return Zygote.MOUNT_EXTERNAL_ANDROID_WRITABLE; |
| 3939 | } |
| 3940 | |
| 3941 | final boolean hasMtp = mIPackageManager.checkUidPermission(ACCESS_MTP, uid) == |
| 3942 | PERMISSION_GRANTED; |
| 3943 | if (mIsFuseEnabled && hasMtp) { |
| 3944 | // The process hosting the MTP server should be able to write in Android/ |
| 3945 | return Zygote.MOUNT_EXTERNAL_ANDROID_WRITABLE; |
| 3946 | } |
| 3947 | |
Jeff Sharkey | 6fd6994 | 2019-03-26 17:53:35 -0600 | [diff] [blame] | 3948 | // Determine if caller is holding runtime permission |
Chad Brubaker | 45810af | 2019-04-08 19:19:48 -0700 | [diff] [blame] | 3949 | final boolean hasRead = StorageManager.checkPermissionAndCheckOp(mContext, false, 0, |
Jeff Sharkey | 6fd6994 | 2019-03-26 17:53:35 -0600 | [diff] [blame] | 3950 | uid, packageName, READ_EXTERNAL_STORAGE, OP_READ_EXTERNAL_STORAGE); |
Chad Brubaker | 45810af | 2019-04-08 19:19:48 -0700 | [diff] [blame] | 3951 | final boolean hasWrite = StorageManager.checkPermissionAndCheckOp(mContext, false, 0, |
Jeff Sharkey | 6fd6994 | 2019-03-26 17:53:35 -0600 | [diff] [blame] | 3952 | uid, packageName, WRITE_EXTERNAL_STORAGE, OP_WRITE_EXTERNAL_STORAGE); |
Jeff Sharkey | 6fd6994 | 2019-03-26 17:53:35 -0600 | [diff] [blame] | 3953 | |
| 3954 | // We're only willing to give out broad access if they also hold |
| 3955 | // runtime permission; this is a firm CDD requirement |
| 3956 | final boolean hasFull = mIPackageManager.checkUidPermission(WRITE_MEDIA_STORAGE, |
| 3957 | uid) == PERMISSION_GRANTED; |
Jeff Sharkey | 26874a2 | 2019-04-17 21:30:47 -0600 | [diff] [blame] | 3958 | if (hasFull && hasWrite) { |
Jeff Sharkey | 6fd6994 | 2019-03-26 17:53:35 -0600 | [diff] [blame] | 3959 | return Zygote.MOUNT_EXTERNAL_FULL; |
| 3960 | } |
| 3961 | |
| 3962 | // We're only willing to give out installer access if they also hold |
| 3963 | // runtime permission; this is a firm CDD requirement |
| 3964 | final boolean hasInstall = mIPackageManager.checkUidPermission(INSTALL_PACKAGES, |
| 3965 | uid) == PERMISSION_GRANTED; |
Sudheer Shanka | b161398 | 2019-05-16 16:55:50 -0700 | [diff] [blame] | 3966 | boolean hasInstallOp = false; |
| 3967 | // OP_REQUEST_INSTALL_PACKAGES is granted/denied per package but vold can't |
| 3968 | // update mountpoints of a specific package. So, check the appop for all packages |
| 3969 | // sharing the uid and allow same level of storage access for all packages even if |
| 3970 | // one of the packages has the appop granted. |
| 3971 | for (String uidPackageName : packagesForUid) { |
| 3972 | if (mIAppOpsService.checkOperation( |
| 3973 | OP_REQUEST_INSTALL_PACKAGES, uid, uidPackageName) == MODE_ALLOWED) { |
| 3974 | hasInstallOp = true; |
| 3975 | break; |
| 3976 | } |
| 3977 | } |
Jeff Sharkey | 26874a2 | 2019-04-17 21:30:47 -0600 | [diff] [blame] | 3978 | if ((hasInstall || hasInstallOp) && hasWrite) { |
Martijn Coenen | fc9c3b6 | 2019-12-13 16:36:27 +0100 | [diff] [blame] | 3979 | return Zygote.MOUNT_EXTERNAL_INSTALLER; |
Jeff Sharkey | 6fd6994 | 2019-03-26 17:53:35 -0600 | [diff] [blame] | 3980 | } |
| 3981 | |
| 3982 | // Otherwise we're willing to give out sandboxed or non-sandboxed if |
| 3983 | // they hold the runtime permission |
Nandana Dutt | dc15bb4 | 2020-01-03 16:43:46 +0000 | [diff] [blame] | 3984 | boolean hasLegacy = mIAppOpsService.checkOperation(OP_LEGACY_STORAGE, |
Jeff Sharkey | 6fd6994 | 2019-03-26 17:53:35 -0600 | [diff] [blame] | 3985 | uid, packageName) == MODE_ALLOWED; |
Nandana Dutt | dc15bb4 | 2020-01-03 16:43:46 +0000 | [diff] [blame] | 3986 | |
| 3987 | // Hack(b/147137425): we have to honor hasRequestedLegacyExternalStorage for a short |
| 3988 | // while to enable 2 cases. |
| 3989 | // 1) Apps that want to be in scoped storage in R, but want to opt out in Q devices, |
| 3990 | // because they want to use raw file paths, would fail until fuse is enabled by default. |
| 3991 | // 2) Test apps that target current sdk will fail. They would fail even after fuse is |
| 3992 | // enabled, but we are fixing it with b/142395442. We are not planning to enable |
| 3993 | // fuse by default until b/142395442 is fixed. |
| 3994 | if (!hasLegacy && !mIsFuseEnabled) { |
| 3995 | ApplicationInfo ai = mIPackageManager.getApplicationInfo(packageName, |
| 3996 | 0, UserHandle.getUserId(uid)); |
| 3997 | hasLegacy = ai.hasRequestedLegacyExternalStorage(); |
| 3998 | } |
| 3999 | |
Jeff Sharkey | 26874a2 | 2019-04-17 21:30:47 -0600 | [diff] [blame] | 4000 | if (hasLegacy && hasWrite) { |
| 4001 | return Zygote.MOUNT_EXTERNAL_WRITE; |
| 4002 | } else if (hasLegacy && hasRead) { |
| 4003 | return Zygote.MOUNT_EXTERNAL_READ; |
Sudheer Shanka | 3a0df3b | 2018-12-12 12:43:43 -0800 | [diff] [blame] | 4004 | } else { |
Sudheer Shanka | 783c90e | 2019-04-12 13:55:20 -0700 | [diff] [blame] | 4005 | return Zygote.MOUNT_EXTERNAL_DEFAULT; |
Sudheer Shanka | 3a0df3b | 2018-12-12 12:43:43 -0800 | [diff] [blame] | 4006 | } |
| 4007 | } catch (RemoteException e) { |
| 4008 | // Should not happen |
| 4009 | } |
| 4010 | return Zygote.MOUNT_EXTERNAL_NONE; |
| 4011 | } |
| 4012 | |
Jeff Sharkey | 7151a9a | 2015-04-04 15:22:37 -0700 | [diff] [blame] | 4013 | private static class Callbacks extends Handler { |
| 4014 | private static final int MSG_STORAGE_STATE_CHANGED = 1; |
| 4015 | private static final int MSG_VOLUME_STATE_CHANGED = 2; |
Jeff Sharkey | 50a0545 | 2015-04-29 11:24:52 -0700 | [diff] [blame] | 4016 | private static final int MSG_VOLUME_RECORD_CHANGED = 3; |
| 4017 | private static final int MSG_VOLUME_FORGOTTEN = 4; |
| 4018 | private static final int MSG_DISK_SCANNED = 5; |
Makoto Onuki | 9dc575d | 2015-06-12 16:10:25 -0700 | [diff] [blame] | 4019 | private static final int MSG_DISK_DESTROYED = 6; |
Jeff Sharkey | 7151a9a | 2015-04-04 15:22:37 -0700 | [diff] [blame] | 4020 | |
Sudheer Shanka | 2250d56 | 2016-11-07 15:41:02 -0800 | [diff] [blame] | 4021 | private final RemoteCallbackList<IStorageEventListener> |
Jeff Sharkey | 7151a9a | 2015-04-04 15:22:37 -0700 | [diff] [blame] | 4022 | mCallbacks = new RemoteCallbackList<>(); |
| 4023 | |
| 4024 | public Callbacks(Looper looper) { |
| 4025 | super(looper); |
| 4026 | } |
| 4027 | |
Sudheer Shanka | 2250d56 | 2016-11-07 15:41:02 -0800 | [diff] [blame] | 4028 | public void register(IStorageEventListener callback) { |
Jeff Sharkey | 7151a9a | 2015-04-04 15:22:37 -0700 | [diff] [blame] | 4029 | mCallbacks.register(callback); |
| 4030 | } |
| 4031 | |
Sudheer Shanka | 2250d56 | 2016-11-07 15:41:02 -0800 | [diff] [blame] | 4032 | public void unregister(IStorageEventListener callback) { |
Jeff Sharkey | 7151a9a | 2015-04-04 15:22:37 -0700 | [diff] [blame] | 4033 | mCallbacks.unregister(callback); |
| 4034 | } |
| 4035 | |
| 4036 | @Override |
| 4037 | public void handleMessage(Message msg) { |
| 4038 | final SomeArgs args = (SomeArgs) msg.obj; |
| 4039 | final int n = mCallbacks.beginBroadcast(); |
| 4040 | for (int i = 0; i < n; i++) { |
Sudheer Shanka | 2250d56 | 2016-11-07 15:41:02 -0800 | [diff] [blame] | 4041 | final IStorageEventListener callback = mCallbacks.getBroadcastItem(i); |
Jeff Sharkey | 7151a9a | 2015-04-04 15:22:37 -0700 | [diff] [blame] | 4042 | try { |
| 4043 | invokeCallback(callback, msg.what, args); |
| 4044 | } catch (RemoteException ignored) { |
| 4045 | } |
| 4046 | } |
| 4047 | mCallbacks.finishBroadcast(); |
| 4048 | args.recycle(); |
| 4049 | } |
| 4050 | |
Sudheer Shanka | 2250d56 | 2016-11-07 15:41:02 -0800 | [diff] [blame] | 4051 | private void invokeCallback(IStorageEventListener callback, int what, SomeArgs args) |
Jeff Sharkey | 7151a9a | 2015-04-04 15:22:37 -0700 | [diff] [blame] | 4052 | throws RemoteException { |
| 4053 | switch (what) { |
| 4054 | case MSG_STORAGE_STATE_CHANGED: { |
| 4055 | callback.onStorageStateChanged((String) args.arg1, (String) args.arg2, |
| 4056 | (String) args.arg3); |
| 4057 | break; |
| 4058 | } |
| 4059 | case MSG_VOLUME_STATE_CHANGED: { |
| 4060 | callback.onVolumeStateChanged((VolumeInfo) args.arg1, args.argi2, args.argi3); |
| 4061 | break; |
| 4062 | } |
Jeff Sharkey | 50a0545 | 2015-04-29 11:24:52 -0700 | [diff] [blame] | 4063 | case MSG_VOLUME_RECORD_CHANGED: { |
| 4064 | callback.onVolumeRecordChanged((VolumeRecord) args.arg1); |
| 4065 | break; |
| 4066 | } |
| 4067 | case MSG_VOLUME_FORGOTTEN: { |
| 4068 | callback.onVolumeForgotten((String) args.arg1); |
Jeff Sharkey | d95d3bf | 2015-04-14 21:39:44 -0700 | [diff] [blame] | 4069 | break; |
| 4070 | } |
Jeff Sharkey | 620b32b | 2015-04-23 19:36:02 -0700 | [diff] [blame] | 4071 | case MSG_DISK_SCANNED: { |
| 4072 | callback.onDiskScanned((DiskInfo) args.arg1, args.argi2); |
Jeff Sharkey | 7e92ef3 | 2015-04-17 17:35:07 -0700 | [diff] [blame] | 4073 | break; |
| 4074 | } |
Makoto Onuki | 9dc575d | 2015-06-12 16:10:25 -0700 | [diff] [blame] | 4075 | case MSG_DISK_DESTROYED: { |
| 4076 | callback.onDiskDestroyed((DiskInfo) args.arg1); |
| 4077 | break; |
| 4078 | } |
Jeff Sharkey | 7151a9a | 2015-04-04 15:22:37 -0700 | [diff] [blame] | 4079 | } |
| 4080 | } |
| 4081 | |
| 4082 | private void notifyStorageStateChanged(String path, String oldState, String newState) { |
| 4083 | final SomeArgs args = SomeArgs.obtain(); |
| 4084 | args.arg1 = path; |
| 4085 | args.arg2 = oldState; |
| 4086 | args.arg3 = newState; |
| 4087 | obtainMessage(MSG_STORAGE_STATE_CHANGED, args).sendToTarget(); |
| 4088 | } |
| 4089 | |
| 4090 | private void notifyVolumeStateChanged(VolumeInfo vol, int oldState, int newState) { |
| 4091 | final SomeArgs args = SomeArgs.obtain(); |
Jeff Sharkey | b36586a | 2015-04-27 08:42:28 -0700 | [diff] [blame] | 4092 | args.arg1 = vol.clone(); |
Jeff Sharkey | 7151a9a | 2015-04-04 15:22:37 -0700 | [diff] [blame] | 4093 | args.argi2 = oldState; |
| 4094 | args.argi3 = newState; |
| 4095 | obtainMessage(MSG_VOLUME_STATE_CHANGED, args).sendToTarget(); |
| 4096 | } |
Jeff Sharkey | d95d3bf | 2015-04-14 21:39:44 -0700 | [diff] [blame] | 4097 | |
Jeff Sharkey | 50a0545 | 2015-04-29 11:24:52 -0700 | [diff] [blame] | 4098 | private void notifyVolumeRecordChanged(VolumeRecord rec) { |
| 4099 | final SomeArgs args = SomeArgs.obtain(); |
| 4100 | args.arg1 = rec.clone(); |
| 4101 | obtainMessage(MSG_VOLUME_RECORD_CHANGED, args).sendToTarget(); |
| 4102 | } |
| 4103 | |
| 4104 | private void notifyVolumeForgotten(String fsUuid) { |
Jeff Sharkey | d95d3bf | 2015-04-14 21:39:44 -0700 | [diff] [blame] | 4105 | final SomeArgs args = SomeArgs.obtain(); |
Jeff Sharkey | b36586a | 2015-04-27 08:42:28 -0700 | [diff] [blame] | 4106 | args.arg1 = fsUuid; |
Jeff Sharkey | 50a0545 | 2015-04-29 11:24:52 -0700 | [diff] [blame] | 4107 | obtainMessage(MSG_VOLUME_FORGOTTEN, args).sendToTarget(); |
Jeff Sharkey | d95d3bf | 2015-04-14 21:39:44 -0700 | [diff] [blame] | 4108 | } |
Jeff Sharkey | 7e92ef3 | 2015-04-17 17:35:07 -0700 | [diff] [blame] | 4109 | |
Jeff Sharkey | 620b32b | 2015-04-23 19:36:02 -0700 | [diff] [blame] | 4110 | private void notifyDiskScanned(DiskInfo disk, int volumeCount) { |
Jeff Sharkey | 7e92ef3 | 2015-04-17 17:35:07 -0700 | [diff] [blame] | 4111 | final SomeArgs args = SomeArgs.obtain(); |
Jeff Sharkey | b36586a | 2015-04-27 08:42:28 -0700 | [diff] [blame] | 4112 | args.arg1 = disk.clone(); |
Jeff Sharkey | 620b32b | 2015-04-23 19:36:02 -0700 | [diff] [blame] | 4113 | args.argi2 = volumeCount; |
| 4114 | obtainMessage(MSG_DISK_SCANNED, args).sendToTarget(); |
Jeff Sharkey | 7e92ef3 | 2015-04-17 17:35:07 -0700 | [diff] [blame] | 4115 | } |
Makoto Onuki | 9dc575d | 2015-06-12 16:10:25 -0700 | [diff] [blame] | 4116 | |
| 4117 | private void notifyDiskDestroyed(DiskInfo disk) { |
| 4118 | final SomeArgs args = SomeArgs.obtain(); |
| 4119 | args.arg1 = disk.clone(); |
| 4120 | obtainMessage(MSG_DISK_DESTROYED, args).sendToTarget(); |
| 4121 | } |
Jeff Sharkey | 7151a9a | 2015-04-04 15:22:37 -0700 | [diff] [blame] | 4122 | } |
| 4123 | |
Kenny Root | 38cf886 | 2010-09-26 14:18:51 -0700 | [diff] [blame] | 4124 | @Override |
Jeff Sharkey | 5aca2b8 | 2013-10-16 16:21:54 -0700 | [diff] [blame] | 4125 | protected void dump(FileDescriptor fd, PrintWriter writer, String[] args) { |
Jeff Sharkey | fe9a53b | 2017-03-31 14:08:23 -0600 | [diff] [blame] | 4126 | if (!DumpUtils.checkDumpPermission(mContext, TAG, writer)) return; |
Jeff Sharkey | 5aca2b8 | 2013-10-16 16:21:54 -0700 | [diff] [blame] | 4127 | |
| 4128 | final IndentingPrintWriter pw = new IndentingPrintWriter(writer, " ", 160); |
Jeff Sharkey | 27de30d | 2015-04-18 16:20:27 -0700 | [diff] [blame] | 4129 | synchronized (mLock) { |
| 4130 | pw.println("Disks:"); |
| 4131 | pw.increaseIndent(); |
| 4132 | for (int i = 0; i < mDisks.size(); i++) { |
| 4133 | final DiskInfo disk = mDisks.valueAt(i); |
| 4134 | disk.dump(pw); |
| 4135 | } |
| 4136 | pw.decreaseIndent(); |
| 4137 | |
| 4138 | pw.println(); |
| 4139 | pw.println("Volumes:"); |
| 4140 | pw.increaseIndent(); |
| 4141 | for (int i = 0; i < mVolumes.size(); i++) { |
| 4142 | final VolumeInfo vol = mVolumes.valueAt(i); |
| 4143 | if (VolumeInfo.ID_PRIVATE_INTERNAL.equals(vol.id)) continue; |
| 4144 | vol.dump(pw); |
| 4145 | } |
| 4146 | pw.decreaseIndent(); |
| 4147 | |
| 4148 | pw.println(); |
Jeff Sharkey | b36586a | 2015-04-27 08:42:28 -0700 | [diff] [blame] | 4149 | pw.println("Records:"); |
Jeff Sharkey | 27de30d | 2015-04-18 16:20:27 -0700 | [diff] [blame] | 4150 | pw.increaseIndent(); |
Jeff Sharkey | b36586a | 2015-04-27 08:42:28 -0700 | [diff] [blame] | 4151 | for (int i = 0; i < mRecords.size(); i++) { |
| 4152 | final VolumeRecord note = mRecords.valueAt(i); |
| 4153 | note.dump(pw); |
Jeff Sharkey | 27de30d | 2015-04-18 16:20:27 -0700 | [diff] [blame] | 4154 | } |
| 4155 | pw.decreaseIndent(); |
Jeff Sharkey | 275e3e4 | 2015-04-24 16:10:32 -0700 | [diff] [blame] | 4156 | |
| 4157 | pw.println(); |
| 4158 | pw.println("Primary storage UUID: " + mPrimaryStorageUuid); |
Jeff Sharkey | b0c363b2 | 2018-12-15 11:53:03 -0700 | [diff] [blame] | 4159 | |
| 4160 | pw.println(); |
Felipe Leme | 281389a | 2016-10-10 17:12:20 -0700 | [diff] [blame] | 4161 | final Pair<String, Long> pair = StorageManager.getPrimaryStoragePathAndSize(); |
| 4162 | if (pair == null) { |
| 4163 | pw.println("Internal storage total size: N/A"); |
| 4164 | } else { |
| 4165 | pw.print("Internal storage ("); |
| 4166 | pw.print(pair.first); |
| 4167 | pw.print(") total size: "); |
| 4168 | pw.print(pair.second); |
| 4169 | pw.print(" ("); |
Jeff Sharkey | 9f2dc05 | 2018-01-07 16:47:31 -0700 | [diff] [blame] | 4170 | pw.print(DataUnit.MEBIBYTES.toBytes(pair.second)); |
| 4171 | pw.println(" MiB)"); |
Felipe Leme | 281389a | 2016-10-10 17:12:20 -0700 | [diff] [blame] | 4172 | } |
Jeff Sharkey | b0c363b2 | 2018-12-15 11:53:03 -0700 | [diff] [blame] | 4173 | |
| 4174 | pw.println(); |
Jeff Sharkey | ce14cd0 | 2015-12-07 15:35:42 -0700 | [diff] [blame] | 4175 | pw.println("Local unlocked users: " + Arrays.toString(mLocalUnlockedUsers)); |
| 4176 | pw.println("System unlocked users: " + Arrays.toString(mSystemUnlockedUsers)); |
Jeff Sharkey | b0c363b2 | 2018-12-15 11:53:03 -0700 | [diff] [blame] | 4177 | |
| 4178 | final ContentResolver cr = mContext.getContentResolver(); |
| 4179 | pw.println(); |
| 4180 | pw.println("Isolated storage, local feature flag: " |
| 4181 | + Settings.Global.getInt(cr, Settings.Global.ISOLATED_STORAGE_LOCAL, 0)); |
| 4182 | pw.println("Isolated storage, remote feature flag: " |
| 4183 | + Settings.Global.getInt(cr, Settings.Global.ISOLATED_STORAGE_REMOTE, 0)); |
| 4184 | pw.println("Isolated storage, resolved: " + StorageManager.hasIsolatedStorage()); |
Jeff Sharkey | 27de30d | 2015-04-18 16:20:27 -0700 | [diff] [blame] | 4185 | } |
Kenny Root | 38cf886 | 2010-09-26 14:18:51 -0700 | [diff] [blame] | 4186 | |
Kenny Root | 38cf886 | 2010-09-26 14:18:51 -0700 | [diff] [blame] | 4187 | synchronized (mObbMounts) { |
Jeff Sharkey | 27de30d | 2015-04-18 16:20:27 -0700 | [diff] [blame] | 4188 | pw.println(); |
Jeff Sharkey | 5aca2b8 | 2013-10-16 16:21:54 -0700 | [diff] [blame] | 4189 | pw.println("mObbMounts:"); |
| 4190 | pw.increaseIndent(); |
| 4191 | final Iterator<Entry<IBinder, List<ObbState>>> binders = mObbMounts.entrySet() |
| 4192 | .iterator(); |
Kenny Root | af9d667 | 2010-10-08 09:21:39 -0700 | [diff] [blame] | 4193 | while (binders.hasNext()) { |
| 4194 | Entry<IBinder, List<ObbState>> e = binders.next(); |
Jeff Sharkey | 5aca2b8 | 2013-10-16 16:21:54 -0700 | [diff] [blame] | 4195 | pw.println(e.getKey() + ":"); |
| 4196 | pw.increaseIndent(); |
Kenny Root | af9d667 | 2010-10-08 09:21:39 -0700 | [diff] [blame] | 4197 | final List<ObbState> obbStates = e.getValue(); |
Kenny Root | 38cf886 | 2010-09-26 14:18:51 -0700 | [diff] [blame] | 4198 | for (final ObbState obbState : obbStates) { |
Jeff Sharkey | 5aca2b8 | 2013-10-16 16:21:54 -0700 | [diff] [blame] | 4199 | pw.println(obbState); |
Kenny Root | 38cf886 | 2010-09-26 14:18:51 -0700 | [diff] [blame] | 4200 | } |
Jeff Sharkey | 5aca2b8 | 2013-10-16 16:21:54 -0700 | [diff] [blame] | 4201 | pw.decreaseIndent(); |
Kenny Root | 38cf886 | 2010-09-26 14:18:51 -0700 | [diff] [blame] | 4202 | } |
Jeff Sharkey | 5aca2b8 | 2013-10-16 16:21:54 -0700 | [diff] [blame] | 4203 | pw.decreaseIndent(); |
Kenny Root | af9d667 | 2010-10-08 09:21:39 -0700 | [diff] [blame] | 4204 | |
Jeff Sharkey | 5aca2b8 | 2013-10-16 16:21:54 -0700 | [diff] [blame] | 4205 | pw.println(); |
| 4206 | pw.println("mObbPathToStateMap:"); |
| 4207 | pw.increaseIndent(); |
Kenny Root | af9d667 | 2010-10-08 09:21:39 -0700 | [diff] [blame] | 4208 | final Iterator<Entry<String, ObbState>> maps = mObbPathToStateMap.entrySet().iterator(); |
| 4209 | while (maps.hasNext()) { |
| 4210 | final Entry<String, ObbState> e = maps.next(); |
Jeff Sharkey | 5aca2b8 | 2013-10-16 16:21:54 -0700 | [diff] [blame] | 4211 | pw.print(e.getKey()); |
| 4212 | pw.print(" -> "); |
| 4213 | pw.println(e.getValue()); |
Kenny Root | af9d667 | 2010-10-08 09:21:39 -0700 | [diff] [blame] | 4214 | } |
Jeff Sharkey | 5aca2b8 | 2013-10-16 16:21:54 -0700 | [diff] [blame] | 4215 | pw.decreaseIndent(); |
Kenny Root | 38cf886 | 2010-09-26 14:18:51 -0700 | [diff] [blame] | 4216 | } |
Kenny Root | 4161f9b | 2011-07-13 09:48:33 -0700 | [diff] [blame] | 4217 | |
Robert Greenwalt | 470fd72 | 2012-01-18 12:51:15 -0800 | [diff] [blame] | 4218 | pw.println(); |
Christopher Tate | 7265abe | 2014-11-21 13:54:45 -0800 | [diff] [blame] | 4219 | pw.print("Last maintenance: "); |
Jeff Sharkey | e8a4b66 | 2015-06-27 15:43:45 -0700 | [diff] [blame] | 4220 | pw.println(TimeUtils.formatForLogging(mLastMaintenance)); |
Kenny Root | 38cf886 | 2010-09-26 14:18:51 -0700 | [diff] [blame] | 4221 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4222 | |
Jeff Sharkey | fa23c5a | 2011-08-09 21:44:24 -0700 | [diff] [blame] | 4223 | /** {@inheritDoc} */ |
Jeff Sharkey | 4887789 | 2015-03-18 11:27:19 -0700 | [diff] [blame] | 4224 | @Override |
Jeff Sharkey | fa23c5a | 2011-08-09 21:44:24 -0700 | [diff] [blame] | 4225 | public void monitor() { |
Jeff Sharkey | 5440279 | 2017-09-15 16:05:19 -0600 | [diff] [blame] | 4226 | try { |
| 4227 | mVold.monitor(); |
| 4228 | } catch (Exception e) { |
| 4229 | Slog.wtf(TAG, e); |
Paul Lawrence | 1c62cbb | 2015-06-03 14:14:52 -0700 | [diff] [blame] | 4230 | } |
Jeff Sharkey | fa23c5a | 2011-08-09 21:44:24 -0700 | [diff] [blame] | 4231 | } |
Svet Ganov | 6ee871e | 2015-07-10 14:29:33 -0700 | [diff] [blame] | 4232 | |
Sudheer Shanka | 2250d56 | 2016-11-07 15:41:02 -0800 | [diff] [blame] | 4233 | private final class StorageManagerInternalImpl extends StorageManagerInternal { |
Svet Ganov | 6ee871e | 2015-07-10 14:29:33 -0700 | [diff] [blame] | 4234 | // Not guarded by a lock. |
| 4235 | private final CopyOnWriteArrayList<ExternalStorageMountPolicy> mPolicies = |
| 4236 | new CopyOnWriteArrayList<>(); |
| 4237 | |
Risan | aec0ee7 | 2018-10-31 10:10:12 +0900 | [diff] [blame] | 4238 | @GuardedBy("mResetListeners") |
| 4239 | private final List<StorageManagerInternal.ResetListener> mResetListeners = |
| 4240 | new ArrayList<>(); |
| 4241 | |
Svet Ganov | 6ee871e | 2015-07-10 14:29:33 -0700 | [diff] [blame] | 4242 | @Override |
| 4243 | public void addExternalStoragePolicy(ExternalStorageMountPolicy policy) { |
| 4244 | // No locking - CopyOnWriteArrayList |
| 4245 | mPolicies.add(policy); |
| 4246 | } |
| 4247 | |
| 4248 | @Override |
| 4249 | public void onExternalStoragePolicyChanged(int uid, String packageName) { |
| 4250 | final int mountMode = getExternalStorageMountMode(uid, packageName); |
| 4251 | remountUidExternalStorage(uid, mountMode); |
| 4252 | } |
| 4253 | |
| 4254 | @Override |
| 4255 | public int getExternalStorageMountMode(int uid, String packageName) { |
Sudheer Shanka | 3a0df3b | 2018-12-12 12:43:43 -0800 | [diff] [blame] | 4256 | if (ENABLE_ISOLATED_STORAGE) { |
| 4257 | return getMountMode(uid, packageName); |
| 4258 | } |
Sudheer Shanka | b161398 | 2019-05-16 16:55:50 -0700 | [diff] [blame] | 4259 | try { |
| 4260 | if (packageName == null) { |
| 4261 | final String[] packagesForUid = mIPackageManager.getPackagesForUid(uid); |
| 4262 | packageName = packagesForUid[0]; |
| 4263 | } |
| 4264 | } catch (RemoteException e) { |
| 4265 | // Should not happen - same process |
| 4266 | } |
Svet Ganov | 6ee871e | 2015-07-10 14:29:33 -0700 | [diff] [blame] | 4267 | // No locking - CopyOnWriteArrayList |
| 4268 | int mountMode = Integer.MAX_VALUE; |
| 4269 | for (ExternalStorageMountPolicy policy : mPolicies) { |
| 4270 | final int policyMode = policy.getMountMode(uid, packageName); |
| 4271 | if (policyMode == Zygote.MOUNT_EXTERNAL_NONE) { |
| 4272 | return Zygote.MOUNT_EXTERNAL_NONE; |
| 4273 | } |
| 4274 | mountMode = Math.min(mountMode, policyMode); |
| 4275 | } |
| 4276 | if (mountMode == Integer.MAX_VALUE) { |
| 4277 | return Zygote.MOUNT_EXTERNAL_NONE; |
| 4278 | } |
| 4279 | return mountMode; |
| 4280 | } |
| 4281 | |
Risan | aec0ee7 | 2018-10-31 10:10:12 +0900 | [diff] [blame] | 4282 | @Override |
| 4283 | public void addResetListener(StorageManagerInternal.ResetListener listener) { |
| 4284 | synchronized (mResetListeners) { |
| 4285 | mResetListeners.add(listener); |
| 4286 | } |
| 4287 | } |
| 4288 | |
| 4289 | public void onReset(IVold vold) { |
| 4290 | synchronized (mResetListeners) { |
| 4291 | for (StorageManagerInternal.ResetListener listener : mResetListeners) { |
| 4292 | listener.onReset(vold); |
| 4293 | } |
| 4294 | } |
| 4295 | } |
| 4296 | |
Martijn Coenen | 7391820 | 2019-12-09 17:02:44 +0100 | [diff] [blame] | 4297 | @Override |
| 4298 | public void resetUser(int userId) { |
| 4299 | // TODO(b/145931219): ideally, we only reset storage for the user in question, |
| 4300 | // but for now, reset everything. |
| 4301 | mHandler.obtainMessage(H_RESET).sendToTarget(); |
| 4302 | } |
| 4303 | |
Svet Ganov | 6ee871e | 2015-07-10 14:29:33 -0700 | [diff] [blame] | 4304 | public boolean hasExternalStorage(int uid, String packageName) { |
Amith Yamasani | 2bd5cff | 2015-07-22 14:42:31 -0700 | [diff] [blame] | 4305 | // No need to check for system uid. This avoids a deadlock between |
| 4306 | // PackageManagerService and AppOpsService. |
| 4307 | if (uid == Process.SYSTEM_UID) { |
| 4308 | return true; |
| 4309 | } |
Sudheer Shanka | 3a0df3b | 2018-12-12 12:43:43 -0800 | [diff] [blame] | 4310 | if (ENABLE_ISOLATED_STORAGE) { |
| 4311 | return getMountMode(uid, packageName) != Zygote.MOUNT_EXTERNAL_NONE; |
| 4312 | } |
Svet Ganov | 6ee871e | 2015-07-10 14:29:33 -0700 | [diff] [blame] | 4313 | // No locking - CopyOnWriteArrayList |
| 4314 | for (ExternalStorageMountPolicy policy : mPolicies) { |
| 4315 | final boolean policyHasStorage = policy.hasExternalStorage(uid, packageName); |
| 4316 | if (!policyHasStorage) { |
| 4317 | return false; |
| 4318 | } |
| 4319 | } |
| 4320 | return true; |
| 4321 | } |
Sudheer Shanka | b161398 | 2019-05-16 16:55:50 -0700 | [diff] [blame] | 4322 | |
| 4323 | public void onAppOpsChanged(int code, int uid, |
| 4324 | @Nullable String packageName, int mode) { |
Martijn Coenen | 28ec85a | 2020-01-28 17:02:23 +0100 | [diff] [blame] | 4325 | if (code == OP_REQUEST_INSTALL_PACKAGES && mIsFuseEnabled) { |
| 4326 | // When using FUSE, we basically have no other choice but to kill the app |
| 4327 | // after the app op is either granted or rejected. |
| 4328 | final IActivityManager am = ActivityManager.getService(); |
| 4329 | try { |
| 4330 | am.killApplication(packageName, |
| 4331 | UserHandle.getAppId(uid), |
| 4332 | UserHandle.USER_ALL, AppOpsManager.opToName(code) + " changed."); |
| 4333 | } catch (RemoteException e) { |
| 4334 | } |
| 4335 | |
| 4336 | return; |
| 4337 | } |
Sudheer Shanka | b161398 | 2019-05-16 16:55:50 -0700 | [diff] [blame] | 4338 | if (mode == MODE_ALLOWED && (code == OP_READ_EXTERNAL_STORAGE |
| 4339 | || code == OP_WRITE_EXTERNAL_STORAGE |
| 4340 | || code == OP_REQUEST_INSTALL_PACKAGES)) { |
| 4341 | final long token = Binder.clearCallingIdentity(); |
| 4342 | try { |
| 4343 | final UserManagerInternal userManagerInternal = |
| 4344 | LocalServices.getService(UserManagerInternal.class); |
| 4345 | if (userManagerInternal.isUserInitialized(UserHandle.getUserId(uid))) { |
| 4346 | onExternalStoragePolicyChanged(uid, packageName); |
| 4347 | } |
| 4348 | } finally { |
| 4349 | Binder.restoreCallingIdentity(token); |
| 4350 | } |
| 4351 | } |
| 4352 | } |
Svet Ganov | 6ee871e | 2015-07-10 14:29:33 -0700 | [diff] [blame] | 4353 | } |
Jeff Sharkey | fa23c5a | 2011-08-09 21:44:24 -0700 | [diff] [blame] | 4354 | } |