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