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