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