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