blob: 4b4ce348385a668febb888e7ac6b3c06824d80a6 [file] [log] [blame]
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001/*
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
17package com.android.server;
18
Martijn Coenen496ac002020-01-08 14:55:53 +010019import static android.Manifest.permission.ACCESS_MTP;
Sudheer Shanka3a0df3b2018-12-12 12:43:43 -080020import static android.Manifest.permission.INSTALL_PACKAGES;
Jeff Sharkey6fd69942019-03-26 17:53:35 -060021import static android.Manifest.permission.READ_EXTERNAL_STORAGE;
22import static android.Manifest.permission.WRITE_EXTERNAL_STORAGE;
Sudheer Shanka3a0df3b2018-12-12 12:43:43 -080023import static android.Manifest.permission.WRITE_MEDIA_STORAGE;
Zim64726cb2019-11-27 13:06:34 +000024import static android.app.ActivityManager.PROCESS_STATE_NONEXISTENT;
Sudheer Shanka3a0df3b2018-12-12 12:43:43 -080025import static android.app.AppOpsManager.MODE_ALLOWED;
Jeff Sharkey11697f52018-12-13 10:14:42 -070026import static android.app.AppOpsManager.OP_LEGACY_STORAGE;
Jeff Sharkey6fd69942019-03-26 17:53:35 -060027import static android.app.AppOpsManager.OP_READ_EXTERNAL_STORAGE;
Sudheer Shanka3a0df3b2018-12-12 12:43:43 -080028import static android.app.AppOpsManager.OP_REQUEST_INSTALL_PACKAGES;
Jeff Sharkey6fd69942019-03-26 17:53:35 -060029import static android.app.AppOpsManager.OP_WRITE_EXTERNAL_STORAGE;
Sudheer Shanka3a0df3b2018-12-12 12:43:43 -080030import static android.content.pm.PackageManager.PERMISSION_GRANTED;
Jeff Sharkey39466322018-12-05 19:19:52 -070031import static android.os.ParcelFileDescriptor.MODE_READ_WRITE;
Jeff Sharkey0095a822018-02-15 13:06:53 -070032import static android.os.storage.OnObbStateChangeListener.ERROR_ALREADY_MOUNTED;
33import static android.os.storage.OnObbStateChangeListener.ERROR_COULD_NOT_MOUNT;
34import static android.os.storage.OnObbStateChangeListener.ERROR_COULD_NOT_UNMOUNT;
35import static android.os.storage.OnObbStateChangeListener.ERROR_INTERNAL;
36import static android.os.storage.OnObbStateChangeListener.ERROR_NOT_MOUNTED;
37import static android.os.storage.OnObbStateChangeListener.ERROR_PERMISSION_DENIED;
38import static android.os.storage.OnObbStateChangeListener.MOUNTED;
39import static android.os.storage.OnObbStateChangeListener.UNMOUNTED;
Zim69e9c092020-01-21 13:32:32 +000040import static android.os.storage.StorageManager.PROP_FUSE;
41import static android.os.storage.StorageManager.PROP_SETTINGS_FUSE;
Jeff Sharkey5790af02018-08-13 17:42:54 -060042
Jeff Sharkeyd95d3bf2015-04-14 21:39:44 -070043import static com.android.internal.util.XmlUtils.readIntAttribute;
Jeff Sharkeye8a4b662015-06-27 15:43:45 -070044import static com.android.internal.util.XmlUtils.readLongAttribute;
Jeff Sharkeyd95d3bf2015-04-14 21:39:44 -070045import static com.android.internal.util.XmlUtils.readStringAttribute;
46import static com.android.internal.util.XmlUtils.writeIntAttribute;
Jeff Sharkeye8a4b662015-06-27 15:43:45 -070047import static com.android.internal.util.XmlUtils.writeLongAttribute;
Jeff Sharkeyd95d3bf2015-04-14 21:39:44 -070048import static com.android.internal.util.XmlUtils.writeStringAttribute;
Zim7924a512020-01-15 15:23:54 +000049import static com.android.server.storage.StorageUserConnection.REMOTE_TIMEOUT_SECONDS;
Jeff Sharkey5790af02018-08-13 17:42:54 -060050
Jeff Sharkeyd95d3bf2015-04-14 21:39:44 -070051import static org.xmlpull.v1.XmlPullParser.END_DOCUMENT;
52import static org.xmlpull.v1.XmlPullParser.START_TAG;
53
Jason parks8888c592011-01-20 22:46:41 -060054import android.Manifest;
Jeff Sharkeyef10ee02015-07-05 14:17:27 -070055import android.annotation.Nullable;
Xiaohui Chen621b3fc2015-10-02 14:41:42 -070056import android.app.ActivityManager;
Martijn Coenen7084c6a2019-11-22 16:32:09 +010057import android.app.ActivityManagerInternal;
Jeff Sharkey2d8b4e82013-09-17 17:30:33 -070058import android.app.AppOpsManager;
Jeff Sharkey14cbe522015-07-08 14:06:37 -070059import android.app.IActivityManager;
Jeff Sharkey9765e442017-12-14 22:15:14 -070060import android.app.KeyguardManager;
Pavel Grafovce72ef02018-01-10 17:14:11 +000061import android.app.admin.SecurityLog;
Jeff Sharkey9bed0702017-01-23 20:37:05 -070062import android.app.usage.StorageStatsManager;
Jeff Sharkeybcd262d2015-06-10 09:41:17 -070063import android.content.BroadcastReceiver;
Jeff Sharkeyb0c363b22018-12-15 11:53:03 -070064import android.content.ContentResolver;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080065import android.content.Context;
66import android.content.Intent;
Jeff Sharkeybcd262d2015-06-10 09:41:17 -070067import android.content.IntentFilter;
Nandana Duttdc15bb42020-01-03 16:43:46 +000068import android.content.pm.ApplicationInfo;
Sudheer Shanka3a0df3b2018-12-12 12:43:43 -080069import android.content.pm.IPackageManager;
Jeff Sharkey275e3e42015-04-24 16:10:32 -070070import android.content.pm.IPackageMoveObserver;
71import android.content.pm.PackageManager;
Sudheer Shanka0a541a52018-07-31 13:21:11 -070072import android.content.pm.PackageManagerInternal;
Jeff Sharkey14cbe522015-07-08 14:06:37 -070073import android.content.pm.ProviderInfo;
Jeff Sharkeybcd262d2015-06-10 09:41:17 -070074import android.content.pm.UserInfo;
Elliott Hughesf839b4f2014-09-26 12:30:47 -070075import android.content.res.Configuration;
Kenny Root02c87302010-07-01 08:10:18 -070076import android.content.res.ObbInfo;
Daniel Colascione766b6322018-01-08 19:10:36 -080077import android.database.ContentObserver;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080078import android.net.Uri;
Kenny Root02c87302010-07-01 08:10:18 -070079import android.os.Binder;
Jeff Sharkey4c099d02015-05-15 13:45:00 -070080import android.os.DropBoxManager;
Kenny Roota02b8b02010-08-05 16:14:17 -070081import android.os.Environment;
Suchi Amalapurapuc42e29e2010-02-22 16:03:53 -080082import android.os.Handler;
Dianne Hackbornefa92b22013-05-03 14:11:43 -070083import android.os.HandlerThread;
Kenny Roota02b8b02010-08-05 16:14:17 -070084import android.os.IBinder;
Jin Qian12690d52017-10-13 18:17:04 -070085import android.os.IStoraged;
Jeff Sharkey1019de92017-09-06 13:47:03 -060086import android.os.IVold;
Jeff Sharkey8058fe62017-09-13 11:50:33 -060087import android.os.IVoldListener;
Zim95eca1d2019-11-15 18:03:00 +000088import android.os.IVoldMountCallback;
Jeff Sharkeyb302c542017-09-15 12:57:59 -060089import android.os.IVoldTaskListener;
Daniel Sandler5f27ef42010-03-16 15:42:02 -040090import android.os.Looper;
Suchi Amalapurapuc42e29e2010-02-22 16:03:53 -080091import android.os.Message;
Daichi Hirono9e8d9e22015-11-13 14:37:00 +090092import android.os.ParcelFileDescriptor;
Jeff Sharkey500ce9e2017-02-12 02:39:24 -070093import android.os.ParcelableException;
Jeff Sharkeyb302c542017-09-15 12:57:59 -060094import android.os.PersistableBundle;
Jeff Sharkeyce14cd02015-12-07 15:35:42 -070095import android.os.PowerManager;
Jeff Sharkey9527b222015-06-24 15:24:48 -070096import android.os.Process;
Jeff Sharkey7151a9a2015-04-04 15:22:37 -070097import android.os.RemoteCallbackList;
San Mehat4270e1e2010-01-29 05:32:19 -080098import android.os.RemoteException;
Suchi Amalapurapufd3530f2010-01-18 00:15:59 -080099import android.os.ServiceManager;
Paul Crowleyfc0b5192018-07-02 13:58:10 -0700100import android.os.ServiceSpecificException;
Jeff Sharkeyedcdaf62015-07-09 09:45:36 -0700101import android.os.SystemClock;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800102import android.os.SystemProperties;
Dianne Hackbornf02b60a2012-08-16 10:48:27 -0700103import android.os.UserHandle;
Emily Bernier92aa5a22014-07-07 10:11:48 -0400104import android.os.UserManager;
Sudheer Shankab1613982019-05-16 16:55:50 -0700105import android.os.UserManagerInternal;
Jeff Sharkey1b8ef7e2015-04-03 17:14:45 -0700106import android.os.storage.DiskInfo;
Kenny Roota02b8b02010-08-05 16:14:17 -0700107import android.os.storage.IObbActionListener;
Jeff Sharkeyd5d5e922017-02-21 10:51:23 -0700108import android.os.storage.IStorageEventListener;
Sudheer Shanka2250d562016-11-07 15:41:02 -0800109import android.os.storage.IStorageManager;
Jeff Sharkeyd5d5e922017-02-21 10:51:23 -0700110import android.os.storage.IStorageShutdownObserver;
Kenny Rootaf9d6672010-10-08 09:21:39 -0700111import android.os.storage.OnObbStateChangeListener;
Paul Lawrence46791e72014-04-03 09:10:26 -0700112import android.os.storage.StorageManager;
Jeff Sharkeyd5d5e922017-02-21 10:51:23 -0700113import android.os.storage.StorageManagerInternal;
Mike Lockwood2f6a3882011-05-09 19:08:06 -0700114import android.os.storage.StorageVolume;
Jeff Sharkey1b8ef7e2015-04-03 17:14:45 -0700115import android.os.storage.VolumeInfo;
Jeff Sharkeyb36586a2015-04-27 08:42:28 -0700116import android.os.storage.VolumeRecord;
Jeff Sharkey5d0c55c2019-01-24 14:32:31 -0700117import android.provider.DeviceConfig;
Martijn Coenen496ac002020-01-08 14:55:53 +0100118import android.provider.Downloads;
Jeff Sharkey14cbe522015-07-08 14:06:37 -0700119import android.provider.MediaStore;
Jeff Sharkeye8a4b662015-06-27 15:43:45 -0700120import android.provider.Settings;
Inseob Kimc1246e62018-11-08 13:13:54 +0900121import android.sysprop.VoldProperties;
Jason parksf7b3cd42011-01-27 09:28:25 -0600122import android.text.TextUtils;
Jeff Sharkey1783f142015-04-17 10:52:51 -0700123import android.text.format.DateUtils;
Jeff Sharkey48877892015-03-18 11:27:19 -0700124import android.util.ArrayMap;
Jeff Sharkey04b4ba12019-12-15 22:42:42 -0700125import android.util.ArraySet;
Jeff Sharkeyd95d3bf2015-04-14 21:39:44 -0700126import android.util.AtomicFile;
Jeff Sharkey9f2dc052018-01-07 16:47:31 -0700127import android.util.DataUnit;
Abhijeet Kaurb9847712019-11-19 16:35:47 +0000128import android.util.FeatureFlagUtils;
Jeff Sharkey1b8ef7e2015-04-03 17:14:45 -0700129import android.util.Log;
Felipe Leme281389a2016-10-10 17:12:20 -0700130import android.util.Pair;
San Mehata5078592010-03-25 09:36:54 -0700131import android.util.Slog;
Jeff Sharkeye8a4b662015-06-27 15:43:45 -0700132import android.util.TimeUtils;
Jeff Sharkeyd95d3bf2015-04-14 21:39:44 -0700133import android.util.Xml;
Jeff Sharkey48877892015-03-18 11:27:19 -0700134
Jeff Sharkey8b2c3a142012-11-12 11:45:05 -0800135import com.android.internal.annotations.GuardedBy;
Sudheer Shankaff971bc2018-12-13 17:39:59 -0800136import com.android.internal.app.IAppOpsCallback;
Sudheer Shanka3a0df3b2018-12-12 12:43:43 -0800137import com.android.internal.app.IAppOpsService;
Daichi Hirono9fb00182016-11-08 14:12:17 +0900138import com.android.internal.os.AppFuseMount;
Jeff Sharkey1019de92017-09-06 13:47:03 -0600139import com.android.internal.os.BackgroundThread;
Daichi Hirono812c95d2017-02-08 16:20:20 +0900140import com.android.internal.os.FuseUnavailableMountException;
Jeff Sharkey7151a9a2015-04-04 15:22:37 -0700141import com.android.internal.os.SomeArgs;
Jeff Sharkey9527b222015-06-24 15:24:48 -0700142import com.android.internal.os.Zygote;
Jeff Sharkey48877892015-03-18 11:27:19 -0700143import com.android.internal.util.ArrayUtils;
Jeff Sharkey11697f52018-12-13 10:14:42 -0700144import com.android.internal.util.CollectionUtils;
Jeff Sharkeyfe9a53b2017-03-31 14:08:23 -0600145import com.android.internal.util.DumpUtils;
Jeff Sharkeyd95d3bf2015-04-14 21:39:44 -0700146import com.android.internal.util.FastXmlSerializer;
Jeff Sharkeyf9fc6d62015-11-08 16:46:05 -0800147import com.android.internal.util.HexDump;
Jeff Sharkey5aca2b82013-10-16 16:21:54 -0700148import com.android.internal.util.IndentingPrintWriter;
Jeff Sharkey4fbbda42012-09-24 18:34:07 -0700149import com.android.internal.util.Preconditions;
Jeff Sharkeyb9fe5372015-12-03 15:23:08 -0700150import com.android.internal.widget.LockPatternUtils;
Ricky Wai4482ab52019-12-10 19:08:18 +0000151import com.android.server.pm.Installer;
Daichi Hirono9fb00182016-11-08 14:12:17 +0900152import com.android.server.storage.AppFuseBridge;
Zim42f1e9f2019-08-15 17:35:00 +0100153import com.android.server.storage.StorageSessionController;
Zim17be6f92019-09-25 14:37:55 +0100154import com.android.server.storage.StorageSessionController.ExternalStorageServiceException;
Wale Ogunwale9e4f3e02018-05-17 09:35:39 -0700155import com.android.server.wm.ActivityTaskManagerInternal;
156import com.android.server.wm.ActivityTaskManagerInternal.ScreenObserver;
Jeff Sharkey5217cac2015-12-20 15:34:01 -0700157
Jeff Sharkey5790af02018-08-13 17:42:54 -0600158import libcore.io.IoUtils;
159import libcore.util.EmptyArray;
160
Jeff Sharkeyd95d3bf2015-04-14 21:39:44 -0700161import org.xmlpull.v1.XmlPullParser;
162import org.xmlpull.v1.XmlPullParserException;
163import org.xmlpull.v1.XmlSerializer;
164
Jeff Sharkeyb049e212012-09-07 23:16:01 -0700165import java.io.File;
Kenny Root38cf8862010-09-26 14:18:51 -0700166import java.io.FileDescriptor;
Jeff Sharkeyd95d3bf2015-04-14 21:39:44 -0700167import java.io.FileInputStream;
168import java.io.FileNotFoundException;
Christopher Tate7265abe2014-11-21 13:54:45 -0800169import java.io.FileOutputStream;
Kenny Root05105f72010-09-22 17:29:43 -0700170import java.io.IOException;
Kenny Root38cf8862010-09-26 14:18:51 -0700171import java.io.PrintWriter;
Kenny Root3b1abba2010-10-13 15:00:07 -0700172import java.math.BigInteger;
Paul Lawrence8e397362014-01-27 15:22:30 -0800173import java.nio.charset.StandardCharsets;
Jeff Sharkey0095a822018-02-15 13:06:53 -0700174import java.security.GeneralSecurityException;
Kenny Root3b1abba2010-10-13 15:00:07 -0700175import java.security.spec.KeySpec;
San Mehat22dd86e2010-01-12 12:21:18 -0800176import java.util.ArrayList;
Jeff Sharkeyf9fc6d62015-11-08 16:46:05 -0800177import java.util.Arrays;
Kenny Roota02b8b02010-08-05 16:14:17 -0700178import java.util.HashMap;
Kenny Root38cf8862010-09-26 14:18:51 -0700179import java.util.Iterator;
Kenny Roota02b8b02010-08-05 16:14:17 -0700180import java.util.LinkedList;
181import java.util.List;
Elliott Hughesf839b4f2014-09-26 12:30:47 -0700182import java.util.Locale;
Kenny Roota02b8b02010-08-05 16:14:17 -0700183import java.util.Map;
Kenny Root38cf8862010-09-26 14:18:51 -0700184import java.util.Map.Entry;
Jeff Sharkey7151a9a2015-04-04 15:22:37 -0700185import java.util.Objects;
Svet Ganov6ee871e2015-07-10 14:29:33 -0700186import java.util.concurrent.CopyOnWriteArrayList;
Kenny Root51a573c2012-05-17 13:30:28 -0700187import java.util.concurrent.CountDownLatch;
188import java.util.concurrent.TimeUnit;
Jeff Sharkeyedcdaf62015-07-09 09:45:36 -0700189import java.util.concurrent.TimeoutException;
Martijn Coenen9fd2b642019-12-24 13:04:36 +0100190import java.util.regex.Matcher;
191import java.util.regex.Pattern;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800192
Kenny Root3b1abba2010-10-13 15:00:07 -0700193import javax.crypto.SecretKey;
194import javax.crypto.SecretKeyFactory;
195import javax.crypto.spec.PBEKeySpec;
196
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800197/**
Jeff Sharkey48877892015-03-18 11:27:19 -0700198 * Service responsible for various storage media. Connects to {@code vold} to
199 * watch for and manage dynamically added storage, such as SD cards and USB mass
200 * storage. Also decides how storage should be presented to users on the device.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800201 */
Jeff Sharkey9765e442017-12-14 22:15:14 -0700202class StorageManagerService extends IStorageManager.Stub
203 implements Watchdog.Monitor, ScreenObserver {
Jason parks5af0b912010-11-29 09:05:25 -0600204
Christopher Tated417d622013-08-19 16:14:25 -0700205 // Static direct instance pointer for the tightly-coupled idle service to use
Sudheer Shanka2250d562016-11-07 15:41:02 -0800206 static StorageManagerService sSelf = null;
Christopher Tated417d622013-08-19 16:14:25 -0700207
Daniel Colascione766b6322018-01-08 19:10:36 -0800208 /* Read during boot to decide whether to enable zram when available */
209 private static final String ZRAM_ENABLED_PROPERTY =
Sudheer Shanka0a541a52018-07-31 13:21:11 -0700210 "persist.sys.zram_enabled";
211
Jeff Sharkey10ec9d82018-11-28 14:52:45 -0700212 private static final boolean ENABLE_ISOLATED_STORAGE = StorageManager.hasIsolatedStorage();
Sudheer Shanka0a541a52018-07-31 13:21:11 -0700213
Matt Pape19ea8a62019-03-18 10:38:25 -0700214 /**
215 * If {@code 1}, enables the isolated storage feature. If {@code -1},
216 * disables the isolated storage feature. If {@code 0}, uses the default
217 * value from the build system.
218 */
219 private static final String ISOLATED_STORAGE_ENABLED = "isolated_storage_enabled";
220
shafikb2992b62019-10-01 15:31:02 +0100221 /**
222 * If {@code 1}, enables FuseDaemon to intercept file system ops. If {@code -1},
223 * disables FuseDaemon. If {@code 0}, uses the default value from the build system.
224 */
225 private static final String FUSE_ENABLED = "fuse_enabled";
226
Jeff Sharkey56e62932015-03-21 20:41:00 -0700227 public static class Lifecycle extends SystemService {
Sudheer Shanka2250d562016-11-07 15:41:02 -0800228 private StorageManagerService mStorageManagerService;
Jeff Sharkey56e62932015-03-21 20:41:00 -0700229
230 public Lifecycle(Context context) {
231 super(context);
232 }
233
234 @Override
235 public void onStart() {
Sudheer Shanka2250d562016-11-07 15:41:02 -0800236 mStorageManagerService = new StorageManagerService(getContext());
237 publishBinderService("mount", mStorageManagerService);
238 mStorageManagerService.start();
Jeff Sharkey56e62932015-03-21 20:41:00 -0700239 }
240
241 @Override
242 public void onBootPhase(int phase) {
Jeff Sharkey11697f52018-12-13 10:14:42 -0700243 if (phase == SystemService.PHASE_SYSTEM_SERVICES_READY) {
244 mStorageManagerService.servicesReady();
245 } else if (phase == SystemService.PHASE_ACTIVITY_MANAGER_READY) {
Sudheer Shanka2250d562016-11-07 15:41:02 -0800246 mStorageManagerService.systemReady();
Yasuhiro Matsuda87a38b52015-07-24 22:10:16 +0900247 } else if (phase == SystemService.PHASE_BOOT_COMPLETED) {
Sudheer Shanka2250d562016-11-07 15:41:02 -0800248 mStorageManagerService.bootCompleted();
Jeff Sharkey56e62932015-03-21 20:41:00 -0700249 }
250 }
Jeff Sharkey48877892015-03-18 11:27:19 -0700251
252 @Override
Jeff Sharkeyab15c392016-05-05 11:45:01 -0600253 public void onSwitchUser(int userHandle) {
Sudheer Shanka2250d562016-11-07 15:41:02 -0800254 mStorageManagerService.mCurrentUserId = userHandle;
Jeff Sharkeyab15c392016-05-05 11:45:01 -0600255 }
256
257 @Override
Jeff Sharkeyce14cd02015-12-07 15:35:42 -0700258 public void onUnlockUser(int userHandle) {
Sudheer Shanka2250d562016-11-07 15:41:02 -0800259 mStorageManagerService.onUnlockUser(userHandle);
Jeff Sharkey48877892015-03-18 11:27:19 -0700260 }
261
262 @Override
263 public void onCleanupUser(int userHandle) {
Sudheer Shanka2250d562016-11-07 15:41:02 -0800264 mStorageManagerService.onCleanupUser(userHandle);
Jeff Sharkey48877892015-03-18 11:27:19 -0700265 }
Martijn Coenen7084c6a2019-11-22 16:32:09 +0100266
267 @Override
268 public void onStopUser(int userHandle) {
269 mStorageManagerService.onStopUser(userHandle);
270 }
Jeff Sharkey56e62932015-03-21 20:41:00 -0700271 }
272
Dianne Hackborn40e9f292012-11-27 19:12:23 -0800273 private static final boolean DEBUG_EVENTS = false;
Kenny Rootb7db2722011-01-25 16:39:35 -0800274 private static final boolean DEBUG_OBB = false;
Kenny Root02c87302010-07-01 08:10:18 -0700275
Jeff Sharkeyc3c72872018-06-06 15:35:53 -0600276 /**
277 * We now talk to vold over Binder, and it has its own internal lock to
278 * serialize certain calls. All long-running operations have been migrated
279 * to be async with callbacks, so we want watchdog to fire if vold wedges.
280 */
281 private static final boolean WATCHDOG_ENABLE = true;
Kenny Root07714d42011-08-17 17:49:28 -0700282
Jeff Sharkey00455bf2016-11-04 14:45:24 -0600283 /**
284 * Our goal is for all Android devices to be usable as development devices,
285 * which includes the new Direct Boot mode added in N. For devices that
286 * don't have native FBE support, we offer an emulation mode for developer
287 * testing purposes, but if it's prohibitively difficult to support this
288 * mode, it can be disabled for specific products using this flag.
289 */
290 private static final boolean EMULATE_FBE_SUPPORTED = true;
291
Sudheer Shanka2250d562016-11-07 15:41:02 -0800292 private static final String TAG = "StorageManagerService";
Jeff Sharkey6fd69942019-03-26 17:53:35 -0600293 private static final boolean LOCAL_LOGV = Log.isLoggable(TAG, Log.VERBOSE);
Jeff Sharkeye8a4b662015-06-27 15:43:45 -0700294
Jeff Sharkey9756d752015-05-14 21:07:42 -0700295 private static final String TAG_STORAGE_BENCHMARK = "storage_benchmark";
Jeff Sharkeye8a4b662015-06-27 15:43:45 -0700296 private static final String TAG_STORAGE_TRIM = "storage_trim";
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800297
Jeff Sharkey275e3e42015-04-24 16:10:32 -0700298 /** Magic value sent by MoveTask.cpp */
299 private static final int MOVE_STATUS_COPY_FINISHED = 82;
300
Jeff Sharkey620b32b2015-04-23 19:36:02 -0700301 private static final int VERSION_INIT = 1;
302 private static final int VERSION_ADD_PRIMARY = 2;
Jeff Sharkeyfced5342015-05-10 14:53:34 -0700303 private static final int VERSION_FIX_PRIMARY = 3;
Jeff Sharkey620b32b2015-04-23 19:36:02 -0700304
Jeff Sharkeyd95d3bf2015-04-14 21:39:44 -0700305 private static final String TAG_VOLUMES = "volumes";
Jeff Sharkey620b32b2015-04-23 19:36:02 -0700306 private static final String ATTR_VERSION = "version";
307 private static final String ATTR_PRIMARY_STORAGE_UUID = "primaryStorageUuid";
Jeff Sharkeyd95d3bf2015-04-14 21:39:44 -0700308 private static final String TAG_VOLUME = "volume";
309 private static final String ATTR_TYPE = "type";
310 private static final String ATTR_FS_UUID = "fsUuid";
Jeff Sharkey5cc0df22015-06-17 19:44:05 -0700311 private static final String ATTR_PART_GUID = "partGuid";
Jeff Sharkeyd95d3bf2015-04-14 21:39:44 -0700312 private static final String ATTR_NICKNAME = "nickname";
313 private static final String ATTR_USER_FLAGS = "userFlags";
Jeff Sharkeye8a4b662015-06-27 15:43:45 -0700314 private static final String ATTR_CREATED_MILLIS = "createdMillis";
Jeff Sharkey3811f352019-05-14 11:54:36 -0600315 private static final String ATTR_LAST_SEEN_MILLIS = "lastSeenMillis";
Jeff Sharkeye8a4b662015-06-27 15:43:45 -0700316 private static final String ATTR_LAST_TRIM_MILLIS = "lastTrimMillis";
317 private static final String ATTR_LAST_BENCH_MILLIS = "lastBenchMillis";
Jeff Sharkeyd95d3bf2015-04-14 21:39:44 -0700318
Philip P. Moltmannc94ba822019-03-04 16:48:22 -0800319 private static final String[] ALL_STORAGE_PERMISSIONS = {
320 Manifest.permission.READ_EXTERNAL_STORAGE,
Philip P. Moltmann129a0b02019-03-27 12:24:45 -0700321 Manifest.permission.WRITE_EXTERNAL_STORAGE
Philip P. Moltmannc94ba822019-03-04 16:48:22 -0800322 };
323
Jeff Sharkeyb36586a2015-04-27 08:42:28 -0700324 private final AtomicFile mSettingsFile;
Jeff Sharkeyd95d3bf2015-04-14 21:39:44 -0700325
Jeff Sharkey48877892015-03-18 11:27:19 -0700326 /**
327 * <em>Never</em> hold the lock while performing downcalls into vold, since
328 * unsolicited events can suddenly appear to update data structures.
329 */
Jeff Sharkey5f3e9342017-03-13 14:53:11 -0600330 private final Object mLock = LockGuard.installNewLock(LockGuard.INDEX_STORAGE);
Jeff Sharkey48877892015-03-18 11:27:19 -0700331
Sudheer Shanka0a541a52018-07-31 13:21:11 -0700332 /**
333 * Similar to {@link #mLock}, never hold this lock while performing downcalls into vold.
334 * Also, never hold this while calling into PackageManagerService since it is used in callbacks
335 * from PackageManagerService.
336 *
337 * If both {@link #mLock} and this lock need to be held, {@link #mLock} should be acquired
338 * before this.
339 *
340 * Use -PL suffix for methods that need to called with this lock held.
341 */
342 private final Object mPackagesLock = new Object();
343
Jeff Sharkeyce14cd02015-12-07 15:35:42 -0700344 /** Set of users that we know are unlocked. */
Jeff Sharkey48877892015-03-18 11:27:19 -0700345 @GuardedBy("mLock")
Jeff Sharkeyce14cd02015-12-07 15:35:42 -0700346 private int[] mLocalUnlockedUsers = EmptyArray.INT;
347 /** Set of users that system knows are unlocked. */
Jeff Sharkeyf9fc6d62015-11-08 16:46:05 -0800348 @GuardedBy("mLock")
Jeff Sharkeyce14cd02015-12-07 15:35:42 -0700349 private int[] mSystemUnlockedUsers = EmptyArray.INT;
Jeff Sharkeyd95d3bf2015-04-14 21:39:44 -0700350
351 /** Map from disk ID to disk */
Jeff Sharkey48877892015-03-18 11:27:19 -0700352 @GuardedBy("mLock")
Jeff Sharkey1b8ef7e2015-04-03 17:14:45 -0700353 private ArrayMap<String, DiskInfo> mDisks = new ArrayMap<>();
Jeff Sharkeyd95d3bf2015-04-14 21:39:44 -0700354 /** Map from volume ID to disk */
Jeff Sharkey48877892015-03-18 11:27:19 -0700355 @GuardedBy("mLock")
Xiaohui Chen621b3fc2015-10-02 14:41:42 -0700356 private final ArrayMap<String, VolumeInfo> mVolumes = new ArrayMap<>();
Jeff Sharkey48877892015-03-18 11:27:19 -0700357
Jeff Sharkeyb36586a2015-04-27 08:42:28 -0700358 /** Map from UUID to record */
Jeff Sharkeyd95d3bf2015-04-14 21:39:44 -0700359 @GuardedBy("mLock")
Jeff Sharkeyb36586a2015-04-27 08:42:28 -0700360 private ArrayMap<String, VolumeRecord> mRecords = new ArrayMap<>();
Jeff Sharkey620b32b2015-04-23 19:36:02 -0700361 @GuardedBy("mLock")
362 private String mPrimaryStorageUuid;
Jeff Sharkeyd95d3bf2015-04-14 21:39:44 -0700363
Jeff Sharkeyeba260d2015-04-19 14:35:16 -0700364 /** Map from disk ID to latches */
365 @GuardedBy("mLock")
366 private ArrayMap<String, CountDownLatch> mDiskScanLatches = new ArrayMap<>();
367
Jeff Sharkey275e3e42015-04-24 16:10:32 -0700368 @GuardedBy("mLock")
369 private IPackageMoveObserver mMoveCallback;
370 @GuardedBy("mLock")
371 private String mMoveTargetUuid;
372
Martijn Coenen44db1ac2019-12-03 16:06:19 +0100373 private volatile int mMediaStoreAuthorityAppId = -1;
Zim74a9bba2019-09-03 20:49:13 +0100374
Martijn Coenen496ac002020-01-08 14:55:53 +0100375 private volatile int mDownloadsAuthorityAppId = -1;
376
Jeff Sharkeyab15c392016-05-05 11:45:01 -0600377 private volatile int mCurrentUserId = UserHandle.USER_SYSTEM;
378
Ricky Wai4482ab52019-12-10 19:08:18 +0000379 private final Installer mInstaller;
380
Daichi Hirono9fb00182016-11-08 14:12:17 +0900381 /** Holding lock for AppFuse business */
382 private final Object mAppFuseLock = new Object();
383
384 @GuardedBy("mAppFuseLock")
385 private int mNextAppFuseName = 0;
386
387 @GuardedBy("mAppFuseLock")
Daichi Hironoe56740d2017-02-02 13:56:45 +0900388 private AppFuseBridge mAppFuseBridge = null;
Daichi Hirono9fb00182016-11-08 14:12:17 +0900389
Martijn Coenen9fd2b642019-12-24 13:04:36 +0100390 /** Matches known application dir paths. The first group contains the generic part of the path,
391 * the second group contains the user id (or null if it's a public volume without users), the
392 * third group contains the package name, and the fourth group the remainder of the path.
393 */
394 public static final Pattern KNOWN_APP_DIR_PATHS = Pattern.compile(
395 "(?i)(^/storage/[^/]+/(?:([0-9]+)/)?Android/(?:data|media|obb|sandbox)/)([^/]+)(/.*)?");
396
397
Jeff Sharkeyef10ee02015-07-05 14:17:27 -0700398 private VolumeInfo findVolumeByIdOrThrow(String id) {
Jeff Sharkey7151a9a2015-04-04 15:22:37 -0700399 synchronized (mLock) {
400 final VolumeInfo vol = mVolumes.get(id);
401 if (vol != null) {
402 return vol;
403 }
404 }
405 throw new IllegalArgumentException("No volume found for ID " + id);
406 }
407
Jeff Sharkeyef10ee02015-07-05 14:17:27 -0700408 private String findVolumeIdForPathOrThrow(String path) {
Jeff Sharkey48877892015-03-18 11:27:19 -0700409 synchronized (mLock) {
Jeff Sharkey1b8ef7e2015-04-03 17:14:45 -0700410 for (int i = 0; i < mVolumes.size(); i++) {
411 final VolumeInfo vol = mVolumes.valueAt(i);
Jeff Sharkey7151a9a2015-04-04 15:22:37 -0700412 if (vol.path != null && path.startsWith(vol.path)) {
413 return vol.id;
Jeff Sharkey48877892015-03-18 11:27:19 -0700414 }
415 }
416 }
Jeff Sharkey7151a9a2015-04-04 15:22:37 -0700417 throw new IllegalArgumentException("No volume found for path " + path);
Jeff Sharkey48877892015-03-18 11:27:19 -0700418 }
419
Jeff Sharkeye8a4b662015-06-27 15:43:45 -0700420 private VolumeRecord findRecordForPath(String path) {
421 synchronized (mLock) {
422 for (int i = 0; i < mVolumes.size(); i++) {
423 final VolumeInfo vol = mVolumes.valueAt(i);
424 if (vol.path != null && path.startsWith(vol.path)) {
425 return mRecords.get(vol.fsUuid);
426 }
427 }
428 }
429 return null;
430 }
431
432 private String scrubPath(String path) {
433 if (path.startsWith(Environment.getDataDirectory().getAbsolutePath())) {
434 return "internal";
435 }
436 final VolumeRecord rec = findRecordForPath(path);
437 if (rec == null || rec.createdMillis == 0) {
438 return "unknown";
439 } else {
440 return "ext:" + (int) ((System.currentTimeMillis() - rec.createdMillis)
441 / DateUtils.WEEK_IN_MILLIS) + "w";
442 }
443 }
444
Jeff Sharkeyef10ee02015-07-05 14:17:27 -0700445 private @Nullable VolumeInfo findStorageForUuid(String volumeUuid) {
Jeff Sharkey275e3e42015-04-24 16:10:32 -0700446 final StorageManager storage = mContext.getSystemService(StorageManager.class);
447 if (Objects.equals(StorageManager.UUID_PRIVATE_INTERNAL, volumeUuid)) {
Zim17be6f92019-09-25 14:37:55 +0100448 return storage.findVolumeById(VolumeInfo.ID_EMULATED_INTERNAL + ";" + 0);
Jeff Sharkey275e3e42015-04-24 16:10:32 -0700449 } else if (Objects.equals(StorageManager.UUID_PRIMARY_PHYSICAL, volumeUuid)) {
450 return storage.getPrimaryPhysicalVolume();
451 } else {
452 return storage.findEmulatedForPrivate(storage.findVolumeByUuid(volumeUuid));
453 }
454 }
455
Jeff Sharkeye8a4b662015-06-27 15:43:45 -0700456 private boolean shouldBenchmark() {
457 final long benchInterval = Settings.Global.getLong(mContext.getContentResolver(),
458 Settings.Global.STORAGE_BENCHMARK_INTERVAL, DateUtils.WEEK_IN_MILLIS);
Jeff Sharkeye83d8a92015-09-09 14:53:38 -0700459 if (benchInterval == -1) {
460 return false;
461 } else if (benchInterval == 0) {
462 return true;
463 }
464
Jeff Sharkeye8a4b662015-06-27 15:43:45 -0700465 synchronized (mLock) {
466 for (int i = 0; i < mVolumes.size(); i++) {
467 final VolumeInfo vol = mVolumes.valueAt(i);
468 final VolumeRecord rec = mRecords.get(vol.fsUuid);
Jeff Sharkeye83d8a92015-09-09 14:53:38 -0700469 if (vol.isMountedWritable() && rec != null) {
Jeff Sharkeye8a4b662015-06-27 15:43:45 -0700470 final long benchAge = System.currentTimeMillis() - rec.lastBenchMillis;
471 if (benchAge >= benchInterval) {
472 return true;
473 }
474 }
475 }
476 return false;
477 }
478 }
479
Jeff Sharkeyeba260d2015-04-19 14:35:16 -0700480 private CountDownLatch findOrCreateDiskScanLatch(String diskId) {
481 synchronized (mLock) {
482 CountDownLatch latch = mDiskScanLatches.get(diskId);
483 if (latch == null) {
484 latch = new CountDownLatch(1);
485 mDiskScanLatches.put(diskId, latch);
486 }
487 return latch;
488 }
489 }
490
Paul Lawrence8e397362014-01-27 15:22:30 -0800491 /** List of crypto types.
492 * These must match CRYPT_TYPE_XXX in cryptfs.h AND their
493 * corresponding commands in CommandListener.cpp */
494 public static final String[] CRYPTO_TYPES
495 = { "password", "default", "pattern", "pin" };
496
Brian Carlstrom7395a8a2014-04-28 22:11:01 -0700497 private final Context mContext;
Jeff Sharkey5d0c55c2019-01-24 14:32:31 -0700498 private final ContentResolver mResolver;
Jeff Sharkeycd575992016-03-29 14:12:49 -0600499
Jeff Sharkey1019de92017-09-06 13:47:03 -0600500 private volatile IVold mVold;
Jin Qian12690d52017-10-13 18:17:04 -0700501 private volatile IStoraged mStoraged;
Jeff Sharkey1019de92017-09-06 13:47:03 -0600502
Yasuhiro Matsuda87a38b52015-07-24 22:10:16 +0900503 private volatile boolean mBootCompleted = false;
Jeff Sharkey48877892015-03-18 11:27:19 -0700504 private volatile boolean mDaemonConnected = false;
Jeff Sharkey9765e442017-12-14 22:15:14 -0700505 private volatile boolean mSecureKeyguardShowing = true;
Jeff Sharkeyb049e212012-09-07 23:16:01 -0700506
Sudheer Shanka0a541a52018-07-31 13:21:11 -0700507 private PackageManagerInternal mPmInternal;
Jeff Sharkey7151a9a2015-04-04 15:22:37 -0700508
Sudheer Shanka3a0df3b2018-12-12 12:43:43 -0800509 private IPackageManager mIPackageManager;
510 private IAppOpsService mIAppOpsService;
511
Jeff Sharkey7151a9a2015-04-04 15:22:37 -0700512 private final Callbacks mCallbacks;
Jeff Sharkeyb9fe5372015-12-03 15:23:08 -0700513 private final LockPatternUtils mLockPatternUtils;
Jeff Sharkey48877892015-03-18 11:27:19 -0700514
San Mehat6cdd9c02010-02-09 14:45:20 -0800515 /**
Kenny Root3b1abba2010-10-13 15:00:07 -0700516 * The size of the crypto algorithm key in bits for OBB files. Currently
517 * Twofish is used which takes 128-bit keys.
518 */
519 private static final int CRYPTO_ALGORITHM_KEY_SIZE = 128;
520
521 /**
522 * The number of times to run SHA1 in the PBKDF2 function for OBB files.
523 * 1024 is reasonably secure and not too slow.
524 */
525 private static final int PBKDF2_HASH_ROUNDS = 1024;
526
527 /**
Kenny Roota02b8b02010-08-05 16:14:17 -0700528 * Mounted OBB tracking information. Used to track the current state of all
529 * OBBs.
Kenny Root02c87302010-07-01 08:10:18 -0700530 */
Kenny Root735de3b2010-09-30 14:11:39 -0700531 final private Map<IBinder, List<ObbState>> mObbMounts = new HashMap<IBinder, List<ObbState>>();
Jeff Sharkey4fbbda42012-09-24 18:34:07 -0700532
533 /** Map from raw paths to {@link ObbState}. */
Kenny Roota02b8b02010-08-05 16:14:17 -0700534 final private Map<String, ObbState> mObbPathToStateMap = new HashMap<String, ObbState>();
535
Svet Ganov6ee871e2015-07-10 14:29:33 -0700536 // Not guarded by a lock.
Sudheer Shanka2250d562016-11-07 15:41:02 -0800537 private final StorageManagerInternalImpl mStorageManagerInternal
538 = new StorageManagerInternalImpl();
Svet Ganov6ee871e2015-07-10 14:29:33 -0700539
Zim42f1e9f2019-08-15 17:35:00 +0100540 // Not guarded by a lock.
541 private final StorageSessionController mStorageSessionController;
542
Zim17be6f92019-09-25 14:37:55 +0100543 private final boolean mIsFuseEnabled;
544
Kenny Roota02b8b02010-08-05 16:14:17 -0700545 class ObbState implements IBinder.DeathRecipient {
Jeff Sharkey4fbbda42012-09-24 18:34:07 -0700546 public ObbState(String rawPath, String canonicalPath, int callingUid,
Jeff Sharkey41cd6812017-09-11 10:32:17 -0600547 IObbActionListener token, int nonce, String volId) {
Jeff Sharkey4fbbda42012-09-24 18:34:07 -0700548 this.rawPath = rawPath;
Xiaohui Chen621b3fc2015-10-02 14:41:42 -0700549 this.canonicalPath = canonicalPath;
Jeff Sharkey4fbbda42012-09-24 18:34:07 -0700550 this.ownerGid = UserHandle.getSharedAppGid(callingUid);
Kenny Rootaf9d6672010-10-08 09:21:39 -0700551 this.token = token;
552 this.nonce = nonce;
Jeff Sharkey41cd6812017-09-11 10:32:17 -0600553 this.volId = volId;
Kenny Roota02b8b02010-08-05 16:14:17 -0700554 }
555
Jeff Sharkey4fbbda42012-09-24 18:34:07 -0700556 final String rawPath;
557 final String canonicalPath;
Kenny Roota02b8b02010-08-05 16:14:17 -0700558
Jeff Sharkey4fbbda42012-09-24 18:34:07 -0700559 final int ownerGid;
Kenny Roota02b8b02010-08-05 16:14:17 -0700560
Kenny Rootaf9d6672010-10-08 09:21:39 -0700561 // Token of remote Binder caller
562 final IObbActionListener token;
563
564 // Identifier to pass back to the token
565 final int nonce;
Kenny Roota02b8b02010-08-05 16:14:17 -0700566
Jeff Sharkey41cd6812017-09-11 10:32:17 -0600567 String volId;
568
Kenny Root735de3b2010-09-30 14:11:39 -0700569 public IBinder getBinder() {
570 return token.asBinder();
571 }
572
Kenny Roota02b8b02010-08-05 16:14:17 -0700573 @Override
574 public void binderDied() {
575 ObbAction action = new UnmountObbAction(this, true);
576 mObbActionHandler.sendMessage(mObbActionHandler.obtainMessage(OBB_RUN_ACTION, action));
Kenny Root735de3b2010-09-30 14:11:39 -0700577 }
Kenny Roota02b8b02010-08-05 16:14:17 -0700578
Kenny Root5919ac62010-10-05 09:49:40 -0700579 public void link() throws RemoteException {
580 getBinder().linkToDeath(this, 0);
581 }
582
583 public void unlink() {
Kenny Root735de3b2010-09-30 14:11:39 -0700584 getBinder().unlinkToDeath(this, 0);
Kenny Roota02b8b02010-08-05 16:14:17 -0700585 }
Kenny Root38cf8862010-09-26 14:18:51 -0700586
587 @Override
588 public String toString() {
589 StringBuilder sb = new StringBuilder("ObbState{");
Jeff Sharkey4fbbda42012-09-24 18:34:07 -0700590 sb.append("rawPath=").append(rawPath);
591 sb.append(",canonicalPath=").append(canonicalPath);
Jeff Sharkey4fbbda42012-09-24 18:34:07 -0700592 sb.append(",ownerGid=").append(ownerGid);
593 sb.append(",token=").append(token);
594 sb.append(",binder=").append(getBinder());
Jeff Sharkey41cd6812017-09-11 10:32:17 -0600595 sb.append(",volId=").append(volId);
Kenny Root38cf8862010-09-26 14:18:51 -0700596 sb.append('}');
597 return sb.toString();
598 }
Kenny Roota02b8b02010-08-05 16:14:17 -0700599 }
600
601 // OBB Action Handler
602 final private ObbActionHandler mObbActionHandler;
603
604 // OBB action handler messages
605 private static final int OBB_RUN_ACTION = 1;
Sudheer Shanka25469aa2018-08-27 15:50:23 -0700606 private static final int OBB_FLUSH_MOUNT_STATE = 2;
Kenny Root02c87302010-07-01 08:10:18 -0700607
Christopher Tate7265abe2014-11-21 13:54:45 -0800608 // Last fstrim operation tracking
609 private static final String LAST_FSTRIM_FILE = "last-fstrim";
610 private final File mLastMaintenanceFile;
611 private long mLastMaintenance;
612
Kenny Root02c87302010-07-01 08:10:18 -0700613 // Handler messages
Jeff Sharkey48877892015-03-18 11:27:19 -0700614 private static final int H_SYSTEM_READY = 1;
615 private static final int H_DAEMON_CONNECTED = 2;
616 private static final int H_SHUTDOWN = 3;
617 private static final int H_FSTRIM = 4;
618 private static final int H_VOLUME_MOUNT = 5;
619 private static final int H_VOLUME_BROADCAST = 6;
Jeff Sharkeyabc3e852015-08-03 14:41:13 -0700620 private static final int H_INTERNAL_BROADCAST = 7;
Xiaohui Chen621b3fc2015-10-02 14:41:42 -0700621 private static final int H_VOLUME_UNMOUNT = 8;
Jeff Sharkey5a9bb742015-11-03 10:15:57 -0800622 private static final int H_PARTITION_FORGET = 9;
623 private static final int H_RESET = 10;
Jin Qiana85b9912017-10-17 15:48:18 -0700624 private static final int H_RUN_IDLE_MAINT = 11;
625 private static final int H_ABORT_IDLE_MAINT = 12;
Daniel Rosenberg137aed12019-03-15 18:41:11 -0700626 private static final int H_BOOT_COMPLETED = 13;
Narayan Kamath157dd1d2019-06-12 13:06:30 +0100627 private static final int H_COMPLETE_UNLOCK_USER = 14;
Suchi Amalapurapu6ffce2e2010-03-08 14:48:40 -0800628
Sudheer Shanka2250d562016-11-07 15:41:02 -0800629 class StorageManagerServiceHandler extends Handler {
630 public StorageManagerServiceHandler(Looper looper) {
Jeff Sharkey48877892015-03-18 11:27:19 -0700631 super(looper);
Daniel Sandler5f27ef42010-03-16 15:42:02 -0400632 }
633
Jason parks5af0b912010-11-29 09:05:25 -0600634 @Override
Suchi Amalapurapuc42e29e2010-02-22 16:03:53 -0800635 public void handleMessage(Message msg) {
636 switch (msg.what) {
Jeff Sharkeyb049e212012-09-07 23:16:01 -0700637 case H_SYSTEM_READY: {
Jeff Sharkey48877892015-03-18 11:27:19 -0700638 handleSystemReady();
639 break;
640 }
Daniel Rosenberg137aed12019-03-15 18:41:11 -0700641 case H_BOOT_COMPLETED: {
642 handleBootCompleted();
643 break;
644 }
Jeff Sharkey48877892015-03-18 11:27:19 -0700645 case H_DAEMON_CONNECTED: {
646 handleDaemonConnected();
Jeff Sharkeyb049e212012-09-07 23:16:01 -0700647 break;
648 }
Christopher Tated417d622013-08-19 16:14:25 -0700649 case H_FSTRIM: {
Christopher Tated417d622013-08-19 16:14:25 -0700650 Slog.i(TAG, "Running fstrim idle maintenance");
Christopher Tate7265abe2014-11-21 13:54:45 -0800651
652 // Remember when we kicked it off
653 try {
654 mLastMaintenance = System.currentTimeMillis();
655 mLastMaintenanceFile.setLastModified(mLastMaintenance);
656 } catch (Exception e) {
657 Slog.e(TAG, "Unable to record last fstrim!");
658 }
659
Jeff Sharkeyb302c542017-09-15 12:57:59 -0600660 // TODO: Reintroduce shouldBenchmark() test
Jeff Sharkey7e19f532017-11-06 13:54:11 -0700661 fstrim(0, null);
Christopher Tate7265abe2014-11-21 13:54:45 -0800662
Christopher Tated417d622013-08-19 16:14:25 -0700663 // invoke the completion callback, if any
Jeff Sharkeye8a4b662015-06-27 15:43:45 -0700664 // TODO: fstrim is non-blocking, so remove this useless callback
Christopher Tated417d622013-08-19 16:14:25 -0700665 Runnable callback = (Runnable) msg.obj;
666 if (callback != null) {
667 callback.run();
668 }
669 break;
670 }
Jeff Sharkey48877892015-03-18 11:27:19 -0700671 case H_SHUTDOWN: {
Sudheer Shanka2250d562016-11-07 15:41:02 -0800672 final IStorageShutdownObserver obs = (IStorageShutdownObserver) msg.obj;
Jeff Sharkey48877892015-03-18 11:27:19 -0700673 boolean success = false;
674 try {
Jeff Sharkey54402792017-09-15 16:05:19 -0600675 mVold.shutdown();
676 success = true;
Jeff Sharkeyace874b2017-09-07 15:27:33 -0600677 } catch (Exception e) {
678 Slog.wtf(TAG, e);
Jeff Sharkey48877892015-03-18 11:27:19 -0700679 }
680 if (obs != null) {
681 try {
682 obs.onShutDownComplete(success ? 0 : -1);
Jeff Sharkeyace874b2017-09-07 15:27:33 -0600683 } catch (Exception ignored) {
Jeff Sharkey48877892015-03-18 11:27:19 -0700684 }
685 }
686 break;
687 }
688 case H_VOLUME_MOUNT: {
Jeff Sharkey1b8ef7e2015-04-03 17:14:45 -0700689 final VolumeInfo vol = (VolumeInfo) msg.obj;
Jeff Sharkey2e606d72015-07-27 14:19:54 -0700690 if (isMountDisallowed(vol)) {
691 Slog.i(TAG, "Ignoring mount " + vol.getId() + " due to policy");
692 break;
693 }
Zim42f1e9f2019-08-15 17:35:00 +0100694
Zim17be6f92019-09-25 14:37:55 +0100695 mount(vol);
Jeff Sharkey48877892015-03-18 11:27:19 -0700696 break;
697 }
Xiaohui Chen621b3fc2015-10-02 14:41:42 -0700698 case H_VOLUME_UNMOUNT: {
699 final VolumeInfo vol = (VolumeInfo) msg.obj;
Sudheer Shanka3f0645b2018-09-18 13:07:59 -0700700 unmount(vol);
Xiaohui Chen621b3fc2015-10-02 14:41:42 -0700701 break;
702 }
Jeff Sharkey48877892015-03-18 11:27:19 -0700703 case H_VOLUME_BROADCAST: {
704 final StorageVolume userVol = (StorageVolume) msg.obj;
Jeff Sharkey7151a9a2015-04-04 15:22:37 -0700705 final String envState = userVol.getState();
706 Slog.d(TAG, "Volume " + userVol.getId() + " broadcasting " + envState + " to "
Jeff Sharkey48877892015-03-18 11:27:19 -0700707 + userVol.getOwner());
708
Jeff Sharkey7151a9a2015-04-04 15:22:37 -0700709 final String action = VolumeInfo.getBroadcastForEnvironment(envState);
Jeff Sharkey48877892015-03-18 11:27:19 -0700710 if (action != null) {
711 final Intent intent = new Intent(action,
712 Uri.fromFile(userVol.getPathFile()));
713 intent.putExtra(StorageVolume.EXTRA_STORAGE_VOLUME, userVol);
Jeff Sharkey082f83b2017-03-26 14:34:47 -0600714 intent.addFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY_BEFORE_BOOT
715 | Intent.FLAG_RECEIVER_INCLUDE_BACKGROUND);
Jeff Sharkey48877892015-03-18 11:27:19 -0700716 mContext.sendBroadcastAsUser(intent, userVol.getOwner());
717 }
718 break;
719 }
Jeff Sharkeyabc3e852015-08-03 14:41:13 -0700720 case H_INTERNAL_BROADCAST: {
721 // Internal broadcasts aimed at system components, not for
722 // third-party apps.
723 final Intent intent = (Intent) msg.obj;
724 mContext.sendBroadcastAsUser(intent, UserHandle.ALL,
725 android.Manifest.permission.WRITE_MEDIA_STORAGE);
Jeff Sharkey5a9bb742015-11-03 10:15:57 -0800726 break;
727 }
728 case H_PARTITION_FORGET: {
Jeff Sharkeydb4b6192017-10-24 11:08:50 -0600729 final VolumeRecord rec = (VolumeRecord) msg.obj;
730 forgetPartition(rec.partGuid, rec.fsUuid);
Jeff Sharkey5a9bb742015-11-03 10:15:57 -0800731 break;
732 }
733 case H_RESET: {
Daniel Rosenberg137aed12019-03-15 18:41:11 -0700734 resetIfBootedAndConnected();
Jeff Sharkey5a9bb742015-11-03 10:15:57 -0800735 break;
Jeff Sharkeyabc3e852015-08-03 14:41:13 -0700736 }
Jin Qiana85b9912017-10-17 15:48:18 -0700737 case H_RUN_IDLE_MAINT: {
738 Slog.i(TAG, "Running idle maintenance");
739 runIdleMaint((Runnable)msg.obj);
740 break;
741 }
742 case H_ABORT_IDLE_MAINT: {
743 Slog.i(TAG, "Aborting idle maintenance");
744 abortIdleMaint((Runnable)msg.obj);
745 break;
746 }
Narayan Kamath157dd1d2019-06-12 13:06:30 +0100747 case H_COMPLETE_UNLOCK_USER: {
748 completeUnlockUser((int) msg.obj);
749 break;
750 }
Suchi Amalapurapuc42e29e2010-02-22 16:03:53 -0800751 }
752 }
Jeff Sharkey48877892015-03-18 11:27:19 -0700753 }
Jeff Sharkeyb049e212012-09-07 23:16:01 -0700754
Jeff Sharkeyb049e212012-09-07 23:16:01 -0700755 private final Handler mHandler;
Suchi Amalapurapuc42e29e2010-02-22 16:03:53 -0800756
Jeff Sharkeybcd262d2015-06-10 09:41:17 -0700757 private BroadcastReceiver mUserReceiver = new BroadcastReceiver() {
758 @Override
759 public void onReceive(Context context, Intent intent) {
760 final String action = intent.getAction();
761 final int userId = intent.getIntExtra(Intent.EXTRA_USER_HANDLE, -1);
Xiaohui Chen621b3fc2015-10-02 14:41:42 -0700762 Preconditions.checkArgument(userId >= 0);
Jeff Sharkeybcd262d2015-06-10 09:41:17 -0700763
764 try {
765 if (Intent.ACTION_USER_ADDED.equals(action)) {
766 final UserManager um = mContext.getSystemService(UserManager.class);
767 final int userSerialNumber = um.getUserSerialNumber(userId);
Jeff Sharkey54402792017-09-15 16:05:19 -0600768 mVold.onUserAdded(userId, userSerialNumber);
Jeff Sharkeybcd262d2015-06-10 09:41:17 -0700769 } else if (Intent.ACTION_USER_REMOVED.equals(action)) {
Xiaohui Chen621b3fc2015-10-02 14:41:42 -0700770 synchronized (mVolumes) {
771 final int size = mVolumes.size();
772 for (int i = 0; i < size; i++) {
773 final VolumeInfo vol = mVolumes.valueAt(i);
774 if (vol.mountUserId == userId) {
775 vol.mountUserId = UserHandle.USER_NULL;
776 mHandler.obtainMessage(H_VOLUME_UNMOUNT, vol).sendToTarget();
777 }
778 }
779 }
Jeff Sharkey54402792017-09-15 16:05:19 -0600780 mVold.onUserRemoved(userId);
Jeff Sharkeybcd262d2015-06-10 09:41:17 -0700781 }
Jeff Sharkeyace874b2017-09-07 15:27:33 -0600782 } catch (Exception e) {
783 Slog.wtf(TAG, e);
Jeff Sharkeybcd262d2015-06-10 09:41:17 -0700784 }
785 }
786 };
787
Jeff Sharkeyedcdaf62015-07-09 09:45:36 -0700788 private void waitForLatch(CountDownLatch latch, String condition, long timeoutMillis)
789 throws TimeoutException {
790 final long startMillis = SystemClock.elapsedRealtime();
Jeff Sharkeyeba260d2015-04-19 14:35:16 -0700791 while (true) {
Kenny Root51a573c2012-05-17 13:30:28 -0700792 try {
793 if (latch.await(5000, TimeUnit.MILLISECONDS)) {
San Mehat207e5382010-02-04 20:46:54 -0800794 return;
Kenny Root51a573c2012-05-17 13:30:28 -0700795 } else {
796 Slog.w(TAG, "Thread " + Thread.currentThread().getName()
Jeff Sharkey48877892015-03-18 11:27:19 -0700797 + " still waiting for " + condition + "...");
San Mehat207e5382010-02-04 20:46:54 -0800798 }
Kenny Root51a573c2012-05-17 13:30:28 -0700799 } catch (InterruptedException e) {
Jeff Sharkeyeba260d2015-04-19 14:35:16 -0700800 Slog.w(TAG, "Interrupt while waiting for " + condition);
San Mehat207e5382010-02-04 20:46:54 -0800801 }
Jeff Sharkeyedcdaf62015-07-09 09:45:36 -0700802 if (timeoutMillis > 0 && SystemClock.elapsedRealtime() > startMillis + timeoutMillis) {
803 throw new TimeoutException("Thread " + Thread.currentThread().getName()
804 + " gave up waiting for " + condition + " after " + timeoutMillis + "ms");
805 }
San Mehat207e5382010-02-04 20:46:54 -0800806 }
San Mehat1f6301e2010-01-07 22:40:27 -0800807 }
Kenny Root02c87302010-07-01 08:10:18 -0700808
Jeff Sharkeyb049e212012-09-07 23:16:01 -0700809 private void handleSystemReady() {
Jeff Sharkey48877892015-03-18 11:27:19 -0700810 // Start scheduling nominally-daily fstrim operations
Christopher Tate115afda2014-06-06 19:06:26 -0700811 MountServiceIdler.scheduleIdlePass(mContext);
Daniel Colascione766b6322018-01-08 19:10:36 -0800812
813 // Toggle zram-enable system property in response to settings
814 mContext.getContentResolver().registerContentObserver(
815 Settings.Global.getUriFor(Settings.Global.ZRAM_ENABLED),
816 false /*notifyForDescendants*/,
817 new ContentObserver(null /* current thread */) {
818 @Override
819 public void onChange(boolean selfChange) {
820 refreshZramSettings();
821 }
822 });
823 refreshZramSettings();
Jeff Sharkeyb0c363b22018-12-15 11:53:03 -0700824
Srinivas Paladuguc8261752019-01-09 10:54:52 -0800825 // Schedule zram writeback unless zram is disabled by persist.sys.zram_enabled
826 String zramPropValue = SystemProperties.get(ZRAM_ENABLED_PROPERTY);
827 if (!zramPropValue.equals("0")
828 && mContext.getResources().getBoolean(
829 com.android.internal.R.bool.config_zramWriteback)) {
830 ZramWriteback.scheduleZramWriteback(mContext);
831 }
Jeff Sharkeyb0c363b22018-12-15 11:53:03 -0700832 // Toggle isolated-enable system property in response to settings
833 mContext.getContentResolver().registerContentObserver(
834 Settings.Global.getUriFor(Settings.Global.ISOLATED_STORAGE_REMOTE),
835 false /*notifyForDescendants*/,
836 new ContentObserver(null /* current thread */) {
837 @Override
838 public void onChange(boolean selfChange) {
839 refreshIsolatedStorageSettings();
840 }
841 });
Jeff Sharkey5d0c55c2019-01-24 14:32:31 -0700842 // For now, simply clone property when it changes
shafik91b34612019-09-23 15:41:44 +0100843 DeviceConfig.addOnPropertiesChangedListener(DeviceConfig.NAMESPACE_STORAGE_NATIVE_BOOT,
Matt Pape15769e22019-04-19 12:31:24 -0700844 mContext.getMainExecutor(), (properties) -> {
Jeff Sharkey8b499a02019-01-28 13:31:50 -0700845 refreshIsolatedStorageSettings();
shafikb2992b62019-10-01 15:31:02 +0100846 refreshFuseSettings();
Jeff Sharkey5d0c55c2019-01-24 14:32:31 -0700847 });
Jeff Sharkeyb0c363b22018-12-15 11:53:03 -0700848 refreshIsolatedStorageSettings();
Daniel Colascione766b6322018-01-08 19:10:36 -0800849 }
850
851 /**
852 * Update the zram_enabled system property (which init reads to
853 * decide whether to enable zram) to reflect the zram_enabled
854 * preference (which we can change for experimentation purposes).
855 */
856 private void refreshZramSettings() {
857 String propertyValue = SystemProperties.get(ZRAM_ENABLED_PROPERTY);
858 if ("".equals(propertyValue)) {
859 return; // System doesn't have zram toggling support
860 }
861 String desiredPropertyValue =
862 Settings.Global.getInt(mContext.getContentResolver(),
863 Settings.Global.ZRAM_ENABLED,
864 1) != 0
865 ? "1" : "0";
866 if (!desiredPropertyValue.equals(propertyValue)) {
867 // Avoid redundant disk writes by setting only if we're
868 // changing the property value. There's no race: we're the
869 // sole writer.
870 SystemProperties.set(ZRAM_ENABLED_PROPERTY, desiredPropertyValue);
Srinivas Paladuguc8261752019-01-09 10:54:52 -0800871 // Schedule writeback only if zram is being enabled.
872 if (desiredPropertyValue.equals("1")
873 && mContext.getResources().getBoolean(
874 com.android.internal.R.bool.config_zramWriteback)) {
875 ZramWriteback.scheduleZramWriteback(mContext);
876 }
Daniel Colascione766b6322018-01-08 19:10:36 -0800877 }
Jeff Sharkeyb049e212012-09-07 23:16:01 -0700878 }
879
Jeff Sharkeyb0c363b22018-12-15 11:53:03 -0700880 private void refreshIsolatedStorageSettings() {
Jeff Sharkey8b499a02019-01-28 13:31:50 -0700881 // Always copy value from newer DeviceConfig location
882 Settings.Global.putString(mResolver,
883 Settings.Global.ISOLATED_STORAGE_REMOTE,
shafik91b34612019-09-23 15:41:44 +0100884 DeviceConfig.getProperty(DeviceConfig.NAMESPACE_STORAGE_NATIVE_BOOT,
885 ISOLATED_STORAGE_ENABLED));
Jeff Sharkey8b499a02019-01-28 13:31:50 -0700886
Jeff Sharkeyb0c363b22018-12-15 11:53:03 -0700887 final int local = Settings.Global.getInt(mContext.getContentResolver(),
888 Settings.Global.ISOLATED_STORAGE_LOCAL, 0);
889 final int remote = Settings.Global.getInt(mContext.getContentResolver(),
890 Settings.Global.ISOLATED_STORAGE_REMOTE, 0);
891
892 // Walk down precedence chain; we prefer local settings first, then
893 // remote settings, before finally falling back to hard-coded default.
894 final boolean res;
895 if (local == -1) {
896 res = false;
897 } else if (local == 1) {
898 res = true;
899 } else if (remote == -1) {
900 res = false;
901 } else if (remote == 1) {
902 res = true;
903 } else {
Jeff Sharkey06376802019-02-11 12:20:02 -0700904 res = true;
Jeff Sharkeyb0c363b22018-12-15 11:53:03 -0700905 }
906
907 Slog.d(TAG, "Isolated storage local flag " + local + " and remote flag "
908 + remote + " resolved to " + res);
909 SystemProperties.set(StorageManager.PROP_ISOLATED_STORAGE, Boolean.toString(res));
910 }
911
Abhijeet Kaurb9847712019-11-19 16:35:47 +0000912 /**
913 * The most recent flag change takes precedence. Change fuse Settings flag if Device Config is
914 * changed. Settings flag change will in turn change fuse system property (persist.sys.fuse)
915 * whenever the user reboots.
916 */
shafikb2992b62019-10-01 15:31:02 +0100917 private void refreshFuseSettings() {
918 int isFuseEnabled = DeviceConfig.getInt(DeviceConfig.NAMESPACE_STORAGE_NATIVE_BOOT,
919 FUSE_ENABLED, 0);
920 if (isFuseEnabled == 1) {
Abhijeet Kaurb9847712019-11-19 16:35:47 +0000921 Slog.d(TAG, "Device Config flag for FUSE is enabled, turn Settings fuse flag on");
922 SystemProperties.set(FeatureFlagUtils.PERSIST_PREFIX
923 + FeatureFlagUtils.SETTINGS_FUSE_FLAG, "true");
shafikb2992b62019-10-01 15:31:02 +0100924 } else if (isFuseEnabled == -1) {
Abhijeet Kaurb9847712019-11-19 16:35:47 +0000925 Slog.d(TAG, "Device Config flag for FUSE is disabled, turn Settings fuse flag off");
926 SystemProperties.set(FeatureFlagUtils.PERSIST_PREFIX
927 + FeatureFlagUtils.SETTINGS_FUSE_FLAG, "false");
shafikb2992b62019-10-01 15:31:02 +0100928 }
929 // else, keep the build config.
Abhijeet Kaurb9847712019-11-19 16:35:47 +0000930 // This can be overridden by direct adjustment of persist.sys.fflag.override.settings_fuse
shafikb2992b62019-10-01 15:31:02 +0100931 }
932
Jeff Sharkey14cbe522015-07-08 14:06:37 -0700933 /**
934 * MediaProvider has a ton of code that makes assumptions about storage
935 * paths never changing, so we outright kill them to pick up new state.
936 */
937 @Deprecated
Xiaohui Chen621b3fc2015-10-02 14:41:42 -0700938 private void killMediaProvider(List<UserInfo> users) {
939 if (users == null) return;
940
Jeff Sharkeyb3cf9532015-07-17 15:12:39 -0700941 final long token = Binder.clearCallingIdentity();
942 try {
Xiaohui Chen621b3fc2015-10-02 14:41:42 -0700943 for (UserInfo user : users) {
944 // System user does not have media provider, so skip.
945 if (user.isSystemOnly()) continue;
946
Jeff Sharkey5790af02018-08-13 17:42:54 -0600947 final ProviderInfo provider = mPmInternal.resolveContentProvider(
948 MediaStore.AUTHORITY, PackageManager.MATCH_DIRECT_BOOT_AWARE
Jeff Sharkey8a372a02016-03-16 16:25:45 -0600949 | PackageManager.MATCH_DIRECT_BOOT_UNAWARE,
950 user.id);
Xiaohui Chen621b3fc2015-10-02 14:41:42 -0700951 if (provider != null) {
Sudheer Shankadc589ac2016-11-10 15:30:17 -0800952 final IActivityManager am = ActivityManager.getService();
Xiaohui Chen621b3fc2015-10-02 14:41:42 -0700953 try {
Jeff Sharkey85f449e2016-06-23 09:26:00 -0600954 am.killApplication(provider.applicationInfo.packageName,
955 UserHandle.getAppId(provider.applicationInfo.uid),
956 UserHandle.USER_ALL, "vold reset");
Xiaohui Chen621b3fc2015-10-02 14:41:42 -0700957 // We only need to run this once. It will kill all users' media processes.
958 break;
959 } catch (RemoteException e) {
960 }
Jeff Sharkeyb3cf9532015-07-17 15:12:39 -0700961 }
Jeff Sharkey14cbe522015-07-08 14:06:37 -0700962 }
Jeff Sharkeyb3cf9532015-07-17 15:12:39 -0700963 } finally {
964 Binder.restoreCallingIdentity(token);
Jeff Sharkey14cbe522015-07-08 14:06:37 -0700965 }
966 }
967
Andreas Gampea36dc622018-02-05 17:19:22 -0800968 @GuardedBy("mLock")
Jeff Sharkey5a9bb742015-11-03 10:15:57 -0800969 private void addInternalVolumeLocked() {
Amith Yamasania7892482015-08-07 11:09:05 -0700970 // Create a stub volume that represents internal storage
971 final VolumeInfo internal = new VolumeInfo(VolumeInfo.ID_PRIVATE_INTERNAL,
972 VolumeInfo.TYPE_PRIVATE, null, null);
973 internal.state = VolumeInfo.STATE_MOUNTED;
974 internal.path = Environment.getDataDirectory().getAbsolutePath();
975 mVolumes.put(internal.id, internal);
976 }
977
Daniel Rosenberg137aed12019-03-15 18:41:11 -0700978 private void initIfBootedAndConnected() {
979 Slog.d(TAG, "Thinking about init, mBootCompleted=" + mBootCompleted
Jeff Sharkey8924e872015-11-30 12:52:10 -0700980 + ", mDaemonConnected=" + mDaemonConnected);
Eric Biggers1127d922019-04-05 12:46:35 -0700981 if (mBootCompleted && mDaemonConnected
982 && !StorageManager.isFileEncryptedNativeOnly()) {
983 // When booting a device without native support, make sure that our
984 // user directories are locked or unlocked based on the current
985 // emulation status.
986 final boolean initLocked = StorageManager.isFileEncryptedEmulatedOnly();
987 Slog.d(TAG, "Setting up emulation state, initlocked=" + initLocked);
Jeff Sharkeyce14cd02015-12-07 15:35:42 -0700988 final List<UserInfo> users = mContext.getSystemService(UserManager.class).getUsers();
Jeff Sharkey8924e872015-11-30 12:52:10 -0700989 for (UserInfo user : users) {
990 try {
Jeff Sharkeyce14cd02015-12-07 15:35:42 -0700991 if (initLocked) {
Jeff Sharkey54402792017-09-15 16:05:19 -0600992 mVold.lockUserKey(user.id);
Jeff Sharkeyce14cd02015-12-07 15:35:42 -0700993 } else {
Jeff Sharkey54402792017-09-15 16:05:19 -0600994 mVold.unlockUserKey(user.id, user.serialNumber, encodeBytes(null),
995 encodeBytes(null));
Jeff Sharkeyce14cd02015-12-07 15:35:42 -0700996 }
Jeff Sharkey43e12112017-09-12 16:31:45 -0600997 } catch (Exception e) {
998 Slog.wtf(TAG, e);
Jeff Sharkey8924e872015-11-30 12:52:10 -0700999 }
1000 }
1001 }
1002 }
1003
Daniel Rosenberg137aed12019-03-15 18:41:11 -07001004 private void resetIfBootedAndConnected() {
1005 Slog.d(TAG, "Thinking about reset, mBootCompleted=" + mBootCompleted
Jeff Sharkey48877892015-03-18 11:27:19 -07001006 + ", mDaemonConnected=" + mDaemonConnected);
Daniel Rosenberg137aed12019-03-15 18:41:11 -07001007 if (mBootCompleted && mDaemonConnected) {
Jeff Sharkey5a9bb742015-11-03 10:15:57 -08001008 final List<UserInfo> users = mContext.getSystemService(UserManager.class).getUsers();
Zim17be6f92019-09-25 14:37:55 +01001009
1010 if (mIsFuseEnabled) {
1011 mStorageSessionController.onReset(mVold, mHandler);
1012 } else {
1013 killMediaProvider(users);
1014 }
Jeff Sharkey14cbe522015-07-08 14:06:37 -07001015
Jeff Sharkeyce14cd02015-12-07 15:35:42 -07001016 final int[] systemUnlockedUsers;
Jeff Sharkey5a9bb742015-11-03 10:15:57 -08001017 synchronized (mLock) {
Jeff Sharkeyce14cd02015-12-07 15:35:42 -07001018 systemUnlockedUsers = mSystemUnlockedUsers;
Jeff Sharkeyb049e212012-09-07 23:16:01 -07001019
Jeff Sharkey5a9bb742015-11-03 10:15:57 -08001020 mDisks.clear();
1021 mVolumes.clear();
1022
1023 addInternalVolumeLocked();
1024 }
Jeff Sharkey59d577a2015-04-11 21:27:21 -07001025
Jeff Sharkey48877892015-03-18 11:27:19 -07001026 try {
Zim42f1e9f2019-08-15 17:35:00 +01001027 // TODO(b/135341433): Remove paranoid logging when FUSE is stable
Zim17be6f92019-09-25 14:37:55 +01001028 Slog.i(TAG, "Resetting vold...");
Jeff Sharkey54402792017-09-15 16:05:19 -06001029 mVold.reset();
Zim42f1e9f2019-08-15 17:35:00 +01001030 Slog.i(TAG, "Reset vold");
Jeff Sharkeybcd262d2015-06-10 09:41:17 -07001031
1032 // Tell vold about all existing and started users
Jeff Sharkeybcd262d2015-06-10 09:41:17 -07001033 for (UserInfo user : users) {
Jeff Sharkey54402792017-09-15 16:05:19 -06001034 mVold.onUserAdded(user.id, user.serialNumber);
Jeff Sharkeybcd262d2015-06-10 09:41:17 -07001035 }
Jeff Sharkeyce14cd02015-12-07 15:35:42 -07001036 for (int userId : systemUnlockedUsers) {
Sudheer Shanka64501e52019-04-29 10:46:26 -07001037 mVold.onUserStarted(userId);
Jin Qian12690d52017-10-13 18:17:04 -07001038 mStoraged.onUserStarted(userId);
Jeff Sharkey50a05452015-04-29 11:24:52 -07001039 }
Jeff Sharkey9765e442017-12-14 22:15:14 -07001040 mVold.onSecureKeyguardStateChanged(mSecureKeyguardShowing);
Risanaec0ee72018-10-31 10:10:12 +09001041 mStorageManagerInternal.onReset(mVold);
Jeff Sharkeyace874b2017-09-07 15:27:33 -06001042 } catch (Exception e) {
1043 Slog.wtf(TAG, e);
Jeff Sharkeyb049e212012-09-07 23:16:01 -07001044 }
1045 }
Jeff Sharkey48877892015-03-18 11:27:19 -07001046 }
Jeff Sharkeyb049e212012-09-07 23:16:01 -07001047
Jeff Sharkeyce14cd02015-12-07 15:35:42 -07001048 private void onUnlockUser(int userId) {
1049 Slog.d(TAG, "onUnlockUser " + userId);
Jeff Sharkey48877892015-03-18 11:27:19 -07001050
1051 // We purposefully block here to make sure that user-specific
1052 // staging area is ready so it's ready for zygote-forked apps to
1053 // bind mount against.
1054 try {
Zim17be6f92019-09-25 14:37:55 +01001055 mStorageSessionController.onUnlockUser(userId);
Sudheer Shanka64501e52019-04-29 10:46:26 -07001056 mVold.onUserStarted(userId);
Jin Qian12690d52017-10-13 18:17:04 -07001057 mStoraged.onUserStarted(userId);
Jeff Sharkeyace874b2017-09-07 15:27:33 -06001058 } catch (Exception e) {
1059 Slog.wtf(TAG, e);
Jeff Sharkeyb049e212012-09-07 23:16:01 -07001060 }
Jeff Sharkey48877892015-03-18 11:27:19 -07001061
Narayan Kamath157dd1d2019-06-12 13:06:30 +01001062 mHandler.obtainMessage(H_COMPLETE_UNLOCK_USER, userId).sendToTarget();
1063 }
1064
1065 private void completeUnlockUser(int userId) {
1066 // If user 0 has completed unlock, perform a one-time migration of legacy obb data
1067 // to its new location. This may take time depending on the size of the data to be copied
1068 // so it's done on the StorageManager handler thread.
1069 if (userId == 0) {
1070 mPmInternal.migrateLegacyObbData();
1071 }
1072
Jeff Sharkey48877892015-03-18 11:27:19 -07001073 // Record user as started so newly mounted volumes kick off events
1074 // correctly, then synthesize events for any already-mounted volumes.
yuanhuihuiefd1f122016-07-13 21:21:03 +08001075 synchronized (mLock) {
Jeff Sharkey1b8ef7e2015-04-03 17:14:45 -07001076 for (int i = 0; i < mVolumes.size(); i++) {
1077 final VolumeInfo vol = mVolumes.valueAt(i);
Jeff Sharkey46349872015-07-28 10:49:47 -07001078 if (vol.isVisibleForRead(userId) && vol.isMountedReadable()) {
Svet Ganov6ee871e2015-07-10 14:29:33 -07001079 final StorageVolume userVol = vol.buildStorageVolume(mContext, userId, false);
Jeff Sharkey48877892015-03-18 11:27:19 -07001080 mHandler.obtainMessage(H_VOLUME_BROADCAST, userVol).sendToTarget();
Jeff Sharkey7151a9a2015-04-04 15:22:37 -07001081
Jeff Sharkey27de30d2015-04-18 16:20:27 -07001082 final String envState = VolumeInfo.getEnvironmentForState(vol.getState());
1083 mCallbacks.notifyStorageStateChanged(userVol.getPath(), envState, envState);
Jeff Sharkey48877892015-03-18 11:27:19 -07001084 }
1085 }
Jeff Sharkeyce14cd02015-12-07 15:35:42 -07001086 mSystemUnlockedUsers = ArrayUtils.appendInt(mSystemUnlockedUsers, userId);
Jeff Sharkey48877892015-03-18 11:27:19 -07001087 }
1088 }
1089
1090 private void onCleanupUser(int userId) {
1091 Slog.d(TAG, "onCleanupUser " + userId);
1092
1093 try {
Jeff Sharkey54402792017-09-15 16:05:19 -06001094 mVold.onUserStopped(userId);
Jin Qian12690d52017-10-13 18:17:04 -07001095 mStoraged.onUserStopped(userId);
Jeff Sharkeyace874b2017-09-07 15:27:33 -06001096 } catch (Exception e) {
1097 Slog.wtf(TAG, e);
Jeff Sharkey48877892015-03-18 11:27:19 -07001098 }
1099
yuanhuihuiefd1f122016-07-13 21:21:03 +08001100 synchronized (mLock) {
Jeff Sharkeyce14cd02015-12-07 15:35:42 -07001101 mSystemUnlockedUsers = ArrayUtils.removeInt(mSystemUnlockedUsers, userId);
Jeff Sharkey48877892015-03-18 11:27:19 -07001102 }
1103 }
Jeff Sharkeyb049e212012-09-07 23:16:01 -07001104
Martijn Coenen7084c6a2019-11-22 16:32:09 +01001105 private void onStopUser(int userId) {
1106 Slog.i(TAG, "onStopUser " + userId);
1107 try {
1108 mStorageSessionController.onUserStopping(userId);
1109 } catch (Exception e) {
1110 Slog.wtf(TAG, e);
1111 }
1112 }
1113
Sudheer Shanka64501e52019-04-29 10:46:26 -07001114 private boolean supportsBlockCheckpoint() throws RemoteException {
Paul Lawrence15a54462019-04-30 11:14:25 -07001115 enforcePermission(android.Manifest.permission.MOUNT_FORMAT_FILESYSTEMS);
Sandeep Patil7d5c4912019-04-15 09:27:30 -07001116 return mVold.supportsBlockCheckpoint();
1117 }
1118
Jeff Sharkey9765e442017-12-14 22:15:14 -07001119 @Override
1120 public void onAwakeStateChanged(boolean isAwake) {
1121 // Ignored
1122 }
1123
1124 @Override
1125 public void onKeyguardStateChanged(boolean isShowing) {
1126 // Push down current secure keyguard status so that we ignore malicious
1127 // USB devices while locked.
1128 mSecureKeyguardShowing = isShowing
Martijn Coenen7c1df2d2020-01-07 10:21:42 +01001129 && mContext.getSystemService(KeyguardManager.class).isDeviceSecure(mCurrentUserId);
Jeff Sharkey9765e442017-12-14 22:15:14 -07001130 try {
1131 mVold.onSecureKeyguardStateChanged(mSecureKeyguardShowing);
1132 } catch (Exception e) {
1133 Slog.wtf(TAG, e);
1134 }
1135 }
1136
Christopher Tated417d622013-08-19 16:14:25 -07001137 void runIdleMaintenance(Runnable callback) {
1138 mHandler.sendMessage(mHandler.obtainMessage(H_FSTRIM, callback));
1139 }
1140
Christopher Tate7265abe2014-11-21 13:54:45 -08001141 // Binder entry point for kicking off an immediate fstrim
1142 @Override
1143 public void runMaintenance() {
Jeff Sharkey48877892015-03-18 11:27:19 -07001144 enforcePermission(android.Manifest.permission.MOUNT_UNMOUNT_FILESYSTEMS);
Christopher Tate7265abe2014-11-21 13:54:45 -08001145 runIdleMaintenance(null);
1146 }
1147
1148 @Override
1149 public long lastMaintenance() {
1150 return mLastMaintenance;
1151 }
1152
San Mehat4270e1e2010-01-29 05:32:19 -08001153 public void onDaemonConnected() {
Jeff Sharkey48877892015-03-18 11:27:19 -07001154 mDaemonConnected = true;
1155 mHandler.obtainMessage(H_DAEMON_CONNECTED).sendToTarget();
1156 }
1157
1158 private void handleDaemonConnected() {
Daniel Rosenberg137aed12019-03-15 18:41:11 -07001159 initIfBootedAndConnected();
1160 resetIfBootedAndConnected();
Jeff Sharkey48877892015-03-18 11:27:19 -07001161
Jeff Sharkey48877892015-03-18 11:27:19 -07001162 // On an encrypted device we can't see system properties yet, so pull
1163 // the system locale out of the mount service.
Inseob Kimc1246e62018-11-08 13:13:54 +09001164 if ("".equals(VoldProperties.encrypt_progress().orElse(""))) {
Jeff Sharkey48877892015-03-18 11:27:19 -07001165 copyLocaleFromMountService();
1166 }
San Mehat4270e1e2010-01-29 05:32:19 -08001167 }
1168
Elliott Hughesf839b4f2014-09-26 12:30:47 -07001169 private void copyLocaleFromMountService() {
1170 String systemLocale;
1171 try {
1172 systemLocale = getField(StorageManager.SYSTEM_LOCALE_KEY);
1173 } catch (RemoteException e) {
1174 return;
1175 }
1176 if (TextUtils.isEmpty(systemLocale)) {
1177 return;
1178 }
1179
1180 Slog.d(TAG, "Got locale " + systemLocale + " from mount service");
1181 Locale locale = Locale.forLanguageTag(systemLocale);
1182 Configuration config = new Configuration();
1183 config.setLocale(locale);
1184 try {
Sudheer Shankadc589ac2016-11-10 15:30:17 -08001185 ActivityManager.getService().updatePersistentConfiguration(config);
Elliott Hughesf839b4f2014-09-26 12:30:47 -07001186 } catch (RemoteException e) {
1187 Slog.e(TAG, "Error setting system locale from mount service", e);
1188 }
Elliott Hughes9c33f282014-10-13 12:39:56 -07001189
1190 // Temporary workaround for http://b/17945169.
1191 Slog.d(TAG, "Setting system properties to " + systemLocale + " from mount service");
Narayan Kamathd30dbb82015-01-15 14:48:15 +00001192 SystemProperties.set("persist.sys.locale", locale.toLanguageTag());
Elliott Hughesf839b4f2014-09-26 12:30:47 -07001193 }
1194
Jeff Sharkey8058fe62017-09-13 11:50:33 -06001195 private final IVoldListener mListener = new IVoldListener.Stub() {
1196 @Override
1197 public void onDiskCreated(String diskId, int flags) {
1198 synchronized (mLock) {
Jeff Sharkey901c0422018-04-20 13:11:20 -06001199 final String value = SystemProperties.get(StorageManager.PROP_ADOPTABLE);
1200 switch (value) {
1201 case "force_on":
1202 flags |= DiskInfo.FLAG_ADOPTABLE;
1203 break;
1204 case "force_off":
1205 flags &= ~DiskInfo.FLAG_ADOPTABLE;
1206 break;
Jeff Sharkey8058fe62017-09-13 11:50:33 -06001207 }
Jeff Sharkey8058fe62017-09-13 11:50:33 -06001208 mDisks.put(diskId, new DiskInfo(diskId, flags));
1209 }
1210 }
1211
1212 @Override
1213 public void onDiskScanned(String diskId) {
1214 synchronized (mLock) {
1215 final DiskInfo disk = mDisks.get(diskId);
1216 if (disk != null) {
1217 onDiskScannedLocked(disk);
1218 }
1219 }
1220 }
1221
1222 @Override
1223 public void onDiskMetadataChanged(String diskId, long sizeBytes, String label,
1224 String sysPath) {
1225 synchronized (mLock) {
1226 final DiskInfo disk = mDisks.get(diskId);
1227 if (disk != null) {
1228 disk.size = sizeBytes;
1229 disk.label = label;
1230 disk.sysPath = sysPath;
1231 }
1232 }
1233 }
1234
1235 @Override
1236 public void onDiskDestroyed(String diskId) {
1237 synchronized (mLock) {
1238 final DiskInfo disk = mDisks.remove(diskId);
1239 if (disk != null) {
1240 mCallbacks.notifyDiskDestroyed(disk);
1241 }
1242 }
1243 }
1244
1245 @Override
Zim17be6f92019-09-25 14:37:55 +01001246 public void onVolumeCreated(String volId, int type, String diskId, String partGuid,
1247 int userId) {
Jeff Sharkey8058fe62017-09-13 11:50:33 -06001248 synchronized (mLock) {
1249 final DiskInfo disk = mDisks.get(diskId);
1250 final VolumeInfo vol = new VolumeInfo(volId, type, disk, partGuid);
Zim17be6f92019-09-25 14:37:55 +01001251 vol.mountUserId = userId;
Jeff Sharkey8058fe62017-09-13 11:50:33 -06001252 mVolumes.put(volId, vol);
1253 onVolumeCreatedLocked(vol);
1254 }
1255 }
1256
1257 @Override
1258 public void onVolumeStateChanged(String volId, int state) {
1259 synchronized (mLock) {
1260 final VolumeInfo vol = mVolumes.get(volId);
1261 if (vol != null) {
1262 final int oldState = vol.state;
1263 final int newState = state;
1264 vol.state = newState;
1265 onVolumeStateChangedLocked(vol, oldState, newState);
1266 }
Ricky Wai4482ab52019-12-10 19:08:18 +00001267 try {
1268 if (vol.type == VolumeInfo.TYPE_PRIVATE && state == VolumeInfo.STATE_MOUNTED) {
1269 mInstaller.onPrivateVolumeMounted(vol.getFsUuid());
1270 }
1271 } catch (Installer.InstallerException e) {
1272 Slog.i(TAG, "Failed when private volume mounted " + vol, e);
1273 }
Jeff Sharkey8058fe62017-09-13 11:50:33 -06001274 }
1275 }
1276
1277 @Override
1278 public void onVolumeMetadataChanged(String volId, String fsType, String fsUuid,
1279 String fsLabel) {
1280 synchronized (mLock) {
1281 final VolumeInfo vol = mVolumes.get(volId);
1282 if (vol != null) {
1283 vol.fsType = fsType;
1284 vol.fsUuid = fsUuid;
1285 vol.fsLabel = fsLabel;
1286 }
1287 }
1288 }
1289
1290 @Override
1291 public void onVolumePathChanged(String volId, String path) {
1292 synchronized (mLock) {
1293 final VolumeInfo vol = mVolumes.get(volId);
1294 if (vol != null) {
1295 vol.path = path;
1296 }
1297 }
1298 }
1299
1300 @Override
1301 public void onVolumeInternalPathChanged(String volId, String internalPath) {
1302 synchronized (mLock) {
1303 final VolumeInfo vol = mVolumes.get(volId);
1304 if (vol != null) {
1305 vol.internalPath = internalPath;
1306 }
1307 }
1308 }
1309
1310 @Override
1311 public void onVolumeDestroyed(String volId) {
Zim17be6f92019-09-25 14:37:55 +01001312 VolumeInfo vol = null;
Jeff Sharkey8058fe62017-09-13 11:50:33 -06001313 synchronized (mLock) {
Zim17be6f92019-09-25 14:37:55 +01001314 vol = mVolumes.remove(volId);
1315 }
1316
1317 if (vol != null) {
1318 mStorageSessionController.onVolumeRemove(vol);
Ricky Wai4482ab52019-12-10 19:08:18 +00001319 try {
1320 if (vol.type == VolumeInfo.TYPE_PRIVATE) {
1321 mInstaller.onPrivateVolumeRemoved(vol.getFsUuid());
1322 }
1323 } catch (Installer.InstallerException e) {
1324 Slog.i(TAG, "Failed when private volume unmounted " + vol, e);
1325 }
Jeff Sharkey8058fe62017-09-13 11:50:33 -06001326 }
1327 }
1328 };
1329
Andreas Gampea36dc622018-02-05 17:19:22 -08001330 @GuardedBy("mLock")
Jeff Sharkeyeba260d2015-04-19 14:35:16 -07001331 private void onDiskScannedLocked(DiskInfo disk) {
Jeff Sharkey620b32b2015-04-23 19:36:02 -07001332 int volumeCount = 0;
Jeff Sharkeyeba260d2015-04-19 14:35:16 -07001333 for (int i = 0; i < mVolumes.size(); i++) {
1334 final VolumeInfo vol = mVolumes.valueAt(i);
1335 if (Objects.equals(disk.id, vol.getDiskId())) {
Jeff Sharkey620b32b2015-04-23 19:36:02 -07001336 volumeCount++;
Jeff Sharkeyeba260d2015-04-19 14:35:16 -07001337 }
1338 }
1339
Jeff Sharkeyc7acac62015-06-12 16:16:56 -07001340 final Intent intent = new Intent(DiskInfo.ACTION_DISK_SCANNED);
Jeff Sharkey7732e1e2016-03-30 17:14:23 -06001341 intent.addFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY_BEFORE_BOOT
1342 | Intent.FLAG_RECEIVER_INCLUDE_BACKGROUND);
Jeff Sharkeyc7acac62015-06-12 16:16:56 -07001343 intent.putExtra(DiskInfo.EXTRA_DISK_ID, disk.id);
1344 intent.putExtra(DiskInfo.EXTRA_VOLUME_COUNT, volumeCount);
Jeff Sharkeyabc3e852015-08-03 14:41:13 -07001345 mHandler.obtainMessage(H_INTERNAL_BROADCAST, intent).sendToTarget();
Jeff Sharkeyc7acac62015-06-12 16:16:56 -07001346
1347 final CountDownLatch latch = mDiskScanLatches.remove(disk.id);
1348 if (latch != null) {
1349 latch.countDown();
1350 }
1351
Jeff Sharkeyf5a6bd72015-05-19 14:42:38 -07001352 disk.volumeCount = volumeCount;
Jeff Sharkey620b32b2015-04-23 19:36:02 -07001353 mCallbacks.notifyDiskScanned(disk, volumeCount);
Jeff Sharkeyeba260d2015-04-19 14:35:16 -07001354 }
1355
Andreas Gampea36dc622018-02-05 17:19:22 -08001356 @GuardedBy("mLock")
Jeff Sharkey1b8ef7e2015-04-03 17:14:45 -07001357 private void onVolumeCreatedLocked(VolumeInfo vol) {
Jeff Sharkey5790af02018-08-13 17:42:54 -06001358 if (mPmInternal.isOnlyCoreApps()) {
Jeff Sharkey6855c482016-03-31 14:34:38 -06001359 Slog.d(TAG, "System booted in core-only mode; ignoring volume " + vol.getId());
1360 return;
1361 }
Martijn Coenen7084c6a2019-11-22 16:32:09 +01001362 final ActivityManagerInternal amInternal =
1363 LocalServices.getService(ActivityManagerInternal.class);
1364
1365 if (mIsFuseEnabled && vol.mountUserId >= 0
1366 && !amInternal.isUserRunning(vol.mountUserId, 0)) {
1367 Slog.d(TAG, "Ignoring volume " + vol.getId() + " because user "
1368 + Integer.toString(vol.mountUserId) + " is no longer running.");
1369 return;
1370 }
Jeff Sharkey6855c482016-03-31 14:34:38 -06001371
Jeff Sharkey275e3e42015-04-24 16:10:32 -07001372 if (vol.type == VolumeInfo.TYPE_EMULATED) {
1373 final StorageManager storage = mContext.getSystemService(StorageManager.class);
1374 final VolumeInfo privateVol = storage.findPrivateForEmulated(vol);
1375
1376 if (Objects.equals(StorageManager.UUID_PRIVATE_INTERNAL, mPrimaryStorageUuid)
1377 && VolumeInfo.ID_PRIVATE_INTERNAL.equals(privateVol.id)) {
1378 Slog.v(TAG, "Found primary storage at " + vol);
1379 vol.mountFlags |= VolumeInfo.MOUNT_FLAG_PRIMARY;
1380 vol.mountFlags |= VolumeInfo.MOUNT_FLAG_VISIBLE;
1381 mHandler.obtainMessage(H_VOLUME_MOUNT, vol).sendToTarget();
1382
1383 } else if (Objects.equals(privateVol.fsUuid, mPrimaryStorageUuid)) {
1384 Slog.v(TAG, "Found primary storage at " + vol);
1385 vol.mountFlags |= VolumeInfo.MOUNT_FLAG_PRIMARY;
1386 vol.mountFlags |= VolumeInfo.MOUNT_FLAG_VISIBLE;
1387 mHandler.obtainMessage(H_VOLUME_MOUNT, vol).sendToTarget();
1388 }
Jeff Sharkeyb049e212012-09-07 23:16:01 -07001389
Jeff Sharkey1b8ef7e2015-04-03 17:14:45 -07001390 } else if (vol.type == VolumeInfo.TYPE_PUBLIC) {
Jeff Sharkey275e3e42015-04-24 16:10:32 -07001391 // TODO: only look at first public partition
1392 if (Objects.equals(StorageManager.UUID_PRIMARY_PHYSICAL, mPrimaryStorageUuid)
1393 && vol.disk.isDefaultPrimary()) {
1394 Slog.v(TAG, "Found primary storage at " + vol);
Jeff Sharkey7e92ef32015-04-17 17:35:07 -07001395 vol.mountFlags |= VolumeInfo.MOUNT_FLAG_PRIMARY;
1396 vol.mountFlags |= VolumeInfo.MOUNT_FLAG_VISIBLE;
San Mehat4270e1e2010-01-29 05:32:19 -08001397 }
Jeff Sharkey7e92ef32015-04-17 17:35:07 -07001398
1399 // Adoptable public disks are visible to apps, since they meet
1400 // public API requirement of being in a stable location.
Jeff Sharkey275e3e42015-04-24 16:10:32 -07001401 if (vol.disk.isAdoptable()) {
Jeff Sharkey7e92ef32015-04-17 17:35:07 -07001402 vol.mountFlags |= VolumeInfo.MOUNT_FLAG_VISIBLE;
1403 }
1404
Jeff Sharkeyab15c392016-05-05 11:45:01 -06001405 vol.mountUserId = mCurrentUserId;
Jeff Sharkey48877892015-03-18 11:27:19 -07001406 mHandler.obtainMessage(H_VOLUME_MOUNT, vol).sendToTarget();
San Mehat4270e1e2010-01-29 05:32:19 -08001407
Jeff Sharkeyb2b9ab82015-04-05 21:10:42 -07001408 } else if (vol.type == VolumeInfo.TYPE_PRIVATE) {
1409 mHandler.obtainMessage(H_VOLUME_MOUNT, vol).sendToTarget();
1410
Risan05c41e62018-10-29 08:57:43 +09001411 } else if (vol.type == VolumeInfo.TYPE_STUB) {
1412 vol.mountUserId = mCurrentUserId;
1413 mHandler.obtainMessage(H_VOLUME_MOUNT, vol).sendToTarget();
San Mehat4270e1e2010-01-29 05:32:19 -08001414 } else {
Jeff Sharkey48877892015-03-18 11:27:19 -07001415 Slog.d(TAG, "Skipping automatic mounting of " + vol);
San Mehat4270e1e2010-01-29 05:32:19 -08001416 }
1417 }
1418
Jeff Sharkeye6c04f92015-04-18 21:38:05 -07001419 private boolean isBroadcastWorthy(VolumeInfo vol) {
1420 switch (vol.getType()) {
Jeff Sharkeyc7acac62015-06-12 16:16:56 -07001421 case VolumeInfo.TYPE_PRIVATE:
Jeff Sharkeye6c04f92015-04-18 21:38:05 -07001422 case VolumeInfo.TYPE_PUBLIC:
1423 case VolumeInfo.TYPE_EMULATED:
Risan05c41e62018-10-29 08:57:43 +09001424 case VolumeInfo.TYPE_STUB:
Jeff Sharkeye6c04f92015-04-18 21:38:05 -07001425 break;
1426 default:
1427 return false;
1428 }
1429
1430 switch (vol.getState()) {
1431 case VolumeInfo.STATE_MOUNTED:
1432 case VolumeInfo.STATE_MOUNTED_READ_ONLY:
1433 case VolumeInfo.STATE_EJECTING:
1434 case VolumeInfo.STATE_UNMOUNTED:
Jeff Sharkeyc7acac62015-06-12 16:16:56 -07001435 case VolumeInfo.STATE_UNMOUNTABLE:
Tony Mantlerf0d71052015-06-24 11:45:25 -07001436 case VolumeInfo.STATE_BAD_REMOVAL:
Jeff Sharkeye6c04f92015-04-18 21:38:05 -07001437 break;
1438 default:
1439 return false;
1440 }
1441
1442 return true;
1443 }
1444
Andreas Gampea36dc622018-02-05 17:19:22 -08001445 @GuardedBy("mLock")
Jeff Sharkey1b8ef7e2015-04-03 17:14:45 -07001446 private void onVolumeStateChangedLocked(VolumeInfo vol, int oldState, int newState) {
Jeff Sharkeyb36586a2015-04-27 08:42:28 -07001447 // Remember that we saw this volume so we're ready to accept user
1448 // metadata, or so we can annoy them when a private volume is ejected
Jeff Sharkey3811f352019-05-14 11:54:36 -06001449 if (!TextUtils.isEmpty(vol.fsUuid)) {
Jeff Sharkey5cc0df22015-06-17 19:44:05 -07001450 VolumeRecord rec = mRecords.get(vol.fsUuid);
1451 if (rec == null) {
1452 rec = new VolumeRecord(vol.type, vol.fsUuid);
1453 rec.partGuid = vol.partGuid;
Jeff Sharkeye8a4b662015-06-27 15:43:45 -07001454 rec.createdMillis = System.currentTimeMillis();
Jeff Sharkeyb36586a2015-04-27 08:42:28 -07001455 if (vol.type == VolumeInfo.TYPE_PRIVATE) {
1456 rec.nickname = vol.disk.getDescription();
1457 }
1458 mRecords.put(rec.fsUuid, rec);
Jeff Sharkey5cc0df22015-06-17 19:44:05 -07001459 } else {
1460 // Handle upgrade case where we didn't store partition GUID
1461 if (TextUtils.isEmpty(rec.partGuid)) {
1462 rec.partGuid = vol.partGuid;
Jeff Sharkey5cc0df22015-06-17 19:44:05 -07001463 }
Jeff Sharkeyb36586a2015-04-27 08:42:28 -07001464 }
Jeff Sharkey3811f352019-05-14 11:54:36 -06001465
1466 rec.lastSeenMillis = System.currentTimeMillis();
1467 writeSettingsLocked();
Jeff Sharkeyb36586a2015-04-27 08:42:28 -07001468 }
1469
Jeff Sharkeye6c04f92015-04-18 21:38:05 -07001470 mCallbacks.notifyVolumeStateChanged(vol, oldState, newState);
1471
Yasuhiro Matsuda87a38b52015-07-24 22:10:16 +09001472 // Do not broadcast before boot has completed to avoid launching the
1473 // processes that receive the intent unnecessarily.
1474 if (mBootCompleted && isBroadcastWorthy(vol)) {
Jeff Sharkeye6c04f92015-04-18 21:38:05 -07001475 final Intent intent = new Intent(VolumeInfo.ACTION_VOLUME_STATE_CHANGED);
Jeff Sharkeyc7acac62015-06-12 16:16:56 -07001476 intent.putExtra(VolumeInfo.EXTRA_VOLUME_ID, vol.id);
1477 intent.putExtra(VolumeInfo.EXTRA_VOLUME_STATE, newState);
Tony Mantlerf0d71052015-06-24 11:45:25 -07001478 intent.putExtra(VolumeRecord.EXTRA_FS_UUID, vol.fsUuid);
Jeff Sharkey7732e1e2016-03-30 17:14:23 -06001479 intent.addFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY_BEFORE_BOOT
1480 | Intent.FLAG_RECEIVER_INCLUDE_BACKGROUND);
Jeff Sharkeyabc3e852015-08-03 14:41:13 -07001481 mHandler.obtainMessage(H_INTERNAL_BROADCAST, intent).sendToTarget();
Jeff Sharkeye6c04f92015-04-18 21:38:05 -07001482 }
Jeff Sharkeyb049e212012-09-07 23:16:01 -07001483
Jeff Sharkey7151a9a2015-04-04 15:22:37 -07001484 final String oldStateEnv = VolumeInfo.getEnvironmentForState(oldState);
1485 final String newStateEnv = VolumeInfo.getEnvironmentForState(newState);
Emily Bernier92aa5a22014-07-07 10:11:48 -04001486
Jeff Sharkey7151a9a2015-04-04 15:22:37 -07001487 if (!Objects.equals(oldStateEnv, newStateEnv)) {
1488 // Kick state changed event towards all started users. Any users
1489 // started after this point will trigger additional
1490 // user-specific broadcasts.
Jeff Sharkeyce14cd02015-12-07 15:35:42 -07001491 for (int userId : mSystemUnlockedUsers) {
Jeff Sharkey46349872015-07-28 10:49:47 -07001492 if (vol.isVisibleForRead(userId)) {
Svet Ganov6ee871e2015-07-10 14:29:33 -07001493 final StorageVolume userVol = vol.buildStorageVolume(mContext, userId, false);
Jeff Sharkey7151a9a2015-04-04 15:22:37 -07001494 mHandler.obtainMessage(H_VOLUME_BROADCAST, userVol).sendToTarget();
Jeff Sharkey48877892015-03-18 11:27:19 -07001495
Jeff Sharkey7151a9a2015-04-04 15:22:37 -07001496 mCallbacks.notifyStorageStateChanged(userVol.getPath(), oldStateEnv,
1497 newStateEnv);
San Mehat4270e1e2010-01-29 05:32:19 -08001498 }
1499 }
1500 }
Jeff Sharkey7151a9a2015-04-04 15:22:37 -07001501
Risan05c41e62018-10-29 08:57:43 +09001502 if ((vol.type == VolumeInfo.TYPE_PUBLIC || vol.type == VolumeInfo.TYPE_STUB)
1503 && vol.state == VolumeInfo.STATE_EJECTING) {
Jeff Sharkey7151a9a2015-04-04 15:22:37 -07001504 // TODO: this should eventually be handled by new ObbVolume state changes
1505 /*
1506 * Some OBBs might have been unmounted when this volume was
1507 * unmounted, so send a message to the handler to let it know to
1508 * remove those from the list of mounted OBBS.
1509 */
1510 mObbActionHandler.sendMessage(mObbActionHandler.obtainMessage(
1511 OBB_FLUSH_MOUNT_STATE, vol.path));
1512 }
Pavel Grafovce72ef02018-01-10 17:14:11 +00001513 maybeLogMediaMount(vol, newState);
1514 }
1515
1516 private void maybeLogMediaMount(VolumeInfo vol, int newState) {
1517 if (!SecurityLog.isLoggingEnabled()) {
1518 return;
1519 }
1520
1521 final DiskInfo disk = vol.getDisk();
1522 if (disk == null || (disk.flags & (DiskInfo.FLAG_SD | DiskInfo.FLAG_USB)) == 0) {
1523 return;
1524 }
1525
1526 // Sometimes there is a newline character.
1527 final String label = disk.label != null ? disk.label.trim() : "";
1528
1529 if (newState == VolumeInfo.STATE_MOUNTED
1530 || newState == VolumeInfo.STATE_MOUNTED_READ_ONLY) {
1531 SecurityLog.writeEvent(SecurityLog.TAG_MEDIA_MOUNT, vol.path, label);
1532 } else if (newState == VolumeInfo.STATE_UNMOUNTED
1533 || newState == VolumeInfo.STATE_BAD_REMOVAL) {
1534 SecurityLog.writeEvent(SecurityLog.TAG_MEDIA_UNMOUNT, vol.path, label);
1535 }
San Mehat4270e1e2010-01-29 05:32:19 -08001536 }
1537
Andreas Gampea36dc622018-02-05 17:19:22 -08001538 @GuardedBy("mLock")
Jeff Sharkey275e3e42015-04-24 16:10:32 -07001539 private void onMoveStatusLocked(int status) {
1540 if (mMoveCallback == null) {
1541 Slog.w(TAG, "Odd, status but no move requested");
1542 return;
1543 }
1544
1545 // TODO: estimate remaining time
1546 try {
Jeff Sharkey50a05452015-04-29 11:24:52 -07001547 mMoveCallback.onStatusChanged(-1, status, -1);
Jeff Sharkey275e3e42015-04-24 16:10:32 -07001548 } catch (RemoteException ignored) {
1549 }
1550
1551 // We've finished copying and we're about to clean up old data, so
1552 // remember that move was successful if we get rebooted
1553 if (status == MOVE_STATUS_COPY_FINISHED) {
1554 Slog.d(TAG, "Move to " + mMoveTargetUuid + " copy phase finshed; persisting");
1555
1556 mPrimaryStorageUuid = mMoveTargetUuid;
Jeff Sharkeyb36586a2015-04-27 08:42:28 -07001557 writeSettingsLocked();
Jeff Sharkey275e3e42015-04-24 16:10:32 -07001558 }
1559
1560 if (PackageManager.isMoveStatusFinished(status)) {
1561 Slog.d(TAG, "Move to " + mMoveTargetUuid + " finished with status " + status);
1562
1563 mMoveCallback = null;
1564 mMoveTargetUuid = null;
1565 }
1566 }
1567
Jeff Sharkey48877892015-03-18 11:27:19 -07001568 private void enforcePermission(String perm) {
1569 mContext.enforceCallingOrSelfPermission(perm, perm);
Mike Lockwooda5250c92011-05-23 13:44:04 -04001570 }
1571
Jeff Sharkey2e606d72015-07-27 14:19:54 -07001572 /**
1573 * Decide if volume is mountable per device policies.
1574 */
1575 private boolean isMountDisallowed(VolumeInfo vol) {
Philip P. Moltmannec3cbb22016-09-14 13:24:52 -07001576 UserManager userManager = mContext.getSystemService(UserManager.class);
1577
1578 boolean isUsbRestricted = false;
1579 if (vol.disk != null && vol.disk.isUsb()) {
1580 isUsbRestricted = userManager.hasUserRestriction(UserManager.DISALLOW_USB_FILE_TRANSFER,
Jeff Sharkey2e606d72015-07-27 14:19:54 -07001581 Binder.getCallingUserHandle());
Emily Bernier92aa5a22014-07-07 10:11:48 -04001582 }
Philip P. Moltmannec3cbb22016-09-14 13:24:52 -07001583
1584 boolean isTypeRestricted = false;
Risan05c41e62018-10-29 08:57:43 +09001585 if (vol.type == VolumeInfo.TYPE_PUBLIC || vol.type == VolumeInfo.TYPE_PRIVATE
1586 || vol.type == VolumeInfo.TYPE_STUB) {
Philip P. Moltmannec3cbb22016-09-14 13:24:52 -07001587 isTypeRestricted = userManager
1588 .hasUserRestriction(UserManager.DISALLOW_MOUNT_PHYSICAL_MEDIA,
1589 Binder.getCallingUserHandle());
1590 }
1591
1592 return isUsbRestricted || isTypeRestricted;
Emily Bernier92aa5a22014-07-07 10:11:48 -04001593 }
1594
Amith Yamasani462ac3a2015-06-30 14:21:01 -07001595 private void enforceAdminUser() {
1596 UserManager um = (UserManager) mContext.getSystemService(Context.USER_SERVICE);
1597 final int callingUserId = UserHandle.getCallingUserId();
1598 boolean isAdmin;
1599 long token = Binder.clearCallingIdentity();
1600 try {
1601 isAdmin = um.getUserInfo(callingUserId).isAdmin();
1602 } finally {
1603 Binder.restoreCallingIdentity(token);
1604 }
1605 if (!isAdmin) {
1606 throw new SecurityException("Only admin users can adopt sd cards");
1607 }
1608 }
1609
Jeff Sharkeyb049e212012-09-07 23:16:01 -07001610 /**
Sudheer Shanka2250d562016-11-07 15:41:02 -08001611 * Constructs a new StorageManagerService instance
San Mehat207e5382010-02-04 20:46:54 -08001612 *
1613 * @param context Binder context for this service
1614 */
Sudheer Shanka2250d562016-11-07 15:41:02 -08001615 public StorageManagerService(Context context) {
Christopher Tated417d622013-08-19 16:14:25 -07001616 sSelf = this;
1617
Jeff Sharkey342b4bf2018-12-18 11:12:40 -07001618 // Snapshot feature flag used for this boot
1619 SystemProperties.set(StorageManager.PROP_ISOLATED_STORAGE_SNAPSHOT, Boolean.toString(
Jeff Sharkey06376802019-02-11 12:20:02 -07001620 SystemProperties.getBoolean(StorageManager.PROP_ISOLATED_STORAGE, true)));
shafik78fcd502019-09-25 13:50:04 +01001621
Zim69e9c092020-01-21 13:32:32 +00001622 // If there is no value in the property yet (first boot after data wipe), this value may be
1623 // incorrect until #updateFusePropFromSettings where we set the correct value and reboot if
1624 // different
1625 mIsFuseEnabled = SystemProperties.getBoolean(PROP_FUSE, false);
San Mehat207e5382010-02-04 20:46:54 -08001626 mContext = context;
Jeff Sharkey5d0c55c2019-01-24 14:32:31 -07001627 mResolver = mContext.getContentResolver();
Jeff Sharkey7151a9a2015-04-04 15:22:37 -07001628 mCallbacks = new Callbacks(FgThread.get().getLooper());
Jeff Sharkeyb9fe5372015-12-03 15:23:08 -07001629 mLockPatternUtils = new LockPatternUtils(mContext);
San Mehat207e5382010-02-04 20:46:54 -08001630
Dianne Hackbornefa92b22013-05-03 14:11:43 -07001631 HandlerThread hthread = new HandlerThread(TAG);
1632 hthread.start();
Sudheer Shanka2250d562016-11-07 15:41:02 -08001633 mHandler = new StorageManagerServiceHandler(hthread.getLooper());
Daniel Sandler5f27ef42010-03-16 15:42:02 -04001634
Sudheer Shanka2250d562016-11-07 15:41:02 -08001635 // Add OBB Action Handler to StorageManagerService thread.
Dianne Hackborn8d044e82013-04-30 17:24:15 -07001636 mObbActionHandler = new ObbActionHandler(IoThread.get().getLooper());
Kenny Roota02b8b02010-08-05 16:14:17 -07001637
Zim17be6f92019-09-25 14:37:55 +01001638 mStorageSessionController = new StorageSessionController(mContext, mIsFuseEnabled);
Zim42f1e9f2019-08-15 17:35:00 +01001639
Ricky Wai4482ab52019-12-10 19:08:18 +00001640 mInstaller = new Installer(mContext);
1641 mInstaller.onStart();
1642
Christopher Tate7265abe2014-11-21 13:54:45 -08001643 // Initialize the last-fstrim tracking if necessary
1644 File dataDir = Environment.getDataDirectory();
1645 File systemDir = new File(dataDir, "system");
1646 mLastMaintenanceFile = new File(systemDir, LAST_FSTRIM_FILE);
1647 if (!mLastMaintenanceFile.exists()) {
1648 // Not setting mLastMaintenance here means that we will force an
1649 // fstrim during reboot following the OTA that installs this code.
1650 try {
1651 (new FileOutputStream(mLastMaintenanceFile)).close();
1652 } catch (IOException e) {
1653 Slog.e(TAG, "Unable to create fstrim record " + mLastMaintenanceFile.getPath());
1654 }
1655 } else {
1656 mLastMaintenance = mLastMaintenanceFile.lastModified();
1657 }
1658
Jeff Sharkeyb36586a2015-04-27 08:42:28 -07001659 mSettingsFile = new AtomicFile(
Dianne Hackborne17b4452018-01-10 13:15:40 -08001660 new File(Environment.getDataSystemDirectory(), "storage.xml"), "storage-settings");
Jeff Sharkeyd95d3bf2015-04-14 21:39:44 -07001661
1662 synchronized (mLock) {
Jeff Sharkeyb36586a2015-04-27 08:42:28 -07001663 readSettingsLocked();
Jeff Sharkeyd95d3bf2015-04-14 21:39:44 -07001664 }
1665
Sudheer Shanka2250d562016-11-07 15:41:02 -08001666 LocalServices.addService(StorageManagerInternal.class, mStorageManagerInternal);
Svet Ganov6ee871e2015-07-10 14:29:33 -07001667
Jeff Sharkeybcd262d2015-06-10 09:41:17 -07001668 final IntentFilter userFilter = new IntentFilter();
1669 userFilter.addAction(Intent.ACTION_USER_ADDED);
1670 userFilter.addAction(Intent.ACTION_USER_REMOVED);
1671 mContext.registerReceiver(mUserReceiver, userFilter, null, mHandler);
1672
Jeff Sharkey5a9bb742015-11-03 10:15:57 -08001673 synchronized (mLock) {
1674 addInternalVolumeLocked();
1675 }
Amith Yamasania7892482015-08-07 11:09:05 -07001676
Kenny Root07714d42011-08-17 17:49:28 -07001677 // Add ourself to the Watchdog monitors if enabled.
1678 if (WATCHDOG_ENABLE) {
1679 Watchdog.getInstance().addMonitor(this);
1680 }
San Mehat207e5382010-02-04 20:46:54 -08001681 }
1682
Abhijeet Kaurb9847712019-11-19 16:35:47 +00001683 /**
1684 * Checks if user changed the persistent settings_fuse flag from Settings UI
1685 * and updates PROP_FUSE (reboots if changed).
1686 */
1687 private void updateFusePropFromSettings() {
Zim69e9c092020-01-21 13:32:32 +00001688 boolean defaultFuseFlag = false;
1689 boolean settingsFuseFlag = SystemProperties.getBoolean(PROP_SETTINGS_FUSE, defaultFuseFlag);
1690 Slog.d(TAG, "FUSE flags. Settings: " + settingsFuseFlag + ". Default: " + defaultFuseFlag);
1691
1692 if (TextUtils.isEmpty(SystemProperties.get(PROP_SETTINGS_FUSE))) {
1693 // Set default value of PROP_SETTINGS_FUSE and PROP_FUSE if it
1694 // is unset (neither true nor false).
1695 // This happens only on the first boot after wiping data partition
1696 SystemProperties.set(PROP_SETTINGS_FUSE, Boolean.toString(defaultFuseFlag));
1697 SystemProperties.set(PROP_FUSE, Boolean.toString(defaultFuseFlag));
Abhijeet Kaura4f40562019-12-09 13:55:42 +00001698 return;
1699 }
1700
Zim69e9c092020-01-21 13:32:32 +00001701 if (mIsFuseEnabled != settingsFuseFlag) {
1702 Slog.i(TAG, "Toggling persist.sys.fuse to " + settingsFuseFlag);
1703 SystemProperties.set(PROP_FUSE, Boolean.toString(settingsFuseFlag));
Zim41cd8de2020-01-21 13:51:15 +00001704
1705 PowerManager powerManager = mContext.getSystemService(PowerManager.class);
1706 if (powerManager.isRebootingUserspaceSupported()) {
1707 // Perform userspace reboot to kick policy into place
1708 powerManager.reboot(PowerManager.REBOOT_USERSPACE);
1709 } else {
1710 // Perform hard reboot to kick policy into place
1711 powerManager.reboot("fuse_prop");
1712 }
Abhijeet Kaurb9847712019-11-19 16:35:47 +00001713 }
1714 }
1715
Jeff Sharkeycd575992016-03-29 14:12:49 -06001716 private void start() {
peter.zhangb0d11d12019-06-13 16:39:30 +08001717 connectStoraged();
1718 connectVold();
Jeff Sharkeycd575992016-03-29 14:12:49 -06001719 }
1720
peter.zhangb0d11d12019-06-13 16:39:30 +08001721 private void connectStoraged() {
Jin Qian12690d52017-10-13 18:17:04 -07001722 IBinder binder = ServiceManager.getService("storaged");
1723 if (binder != null) {
1724 try {
1725 binder.linkToDeath(new DeathRecipient() {
1726 @Override
1727 public void binderDied() {
1728 Slog.w(TAG, "storaged died; reconnecting");
1729 mStoraged = null;
peter.zhangb0d11d12019-06-13 16:39:30 +08001730 connectStoraged();
Jin Qian12690d52017-10-13 18:17:04 -07001731 }
1732 }, 0);
1733 } catch (RemoteException e) {
1734 binder = null;
1735 }
1736 }
1737
1738 if (binder != null) {
1739 mStoraged = IStoraged.Stub.asInterface(binder);
1740 } else {
1741 Slog.w(TAG, "storaged not found; trying again");
1742 }
1743
peter.zhangb0d11d12019-06-13 16:39:30 +08001744 if (mStoraged == null) {
1745 BackgroundThread.getHandler().postDelayed(() -> {
1746 connectStoraged();
1747 }, DateUtils.SECOND_IN_MILLIS);
1748 } else {
1749 onDaemonConnected();
1750 }
1751 }
1752
1753 private void connectVold() {
1754 IBinder binder = ServiceManager.getService("vold");
Jeff Sharkey1019de92017-09-06 13:47:03 -06001755 if (binder != null) {
1756 try {
1757 binder.linkToDeath(new DeathRecipient() {
1758 @Override
1759 public void binderDied() {
1760 Slog.w(TAG, "vold died; reconnecting");
Jin Qian12690d52017-10-13 18:17:04 -07001761 mVold = null;
peter.zhangb0d11d12019-06-13 16:39:30 +08001762 connectVold();
Jeff Sharkey1019de92017-09-06 13:47:03 -06001763 }
1764 }, 0);
1765 } catch (RemoteException e) {
1766 binder = null;
1767 }
1768 }
1769
1770 if (binder != null) {
1771 mVold = IVold.Stub.asInterface(binder);
Jeff Sharkey8058fe62017-09-13 11:50:33 -06001772 try {
1773 mVold.setListener(mListener);
Jeff Sharkey8058fe62017-09-13 11:50:33 -06001774 } catch (RemoteException e) {
Jin Qian12690d52017-10-13 18:17:04 -07001775 mVold = null;
Jeff Sharkey8058fe62017-09-13 11:50:33 -06001776 Slog.w(TAG, "vold listener rejected; trying again", e);
1777 }
Jeff Sharkey1019de92017-09-06 13:47:03 -06001778 } else {
1779 Slog.w(TAG, "vold not found; trying again");
Jeff Sharkey1019de92017-09-06 13:47:03 -06001780 }
Jeff Sharkey8058fe62017-09-13 11:50:33 -06001781
peter.zhangb0d11d12019-06-13 16:39:30 +08001782 if (mVold == null) {
Jin Qian12690d52017-10-13 18:17:04 -07001783 BackgroundThread.getHandler().postDelayed(() -> {
peter.zhangb0d11d12019-06-13 16:39:30 +08001784 connectVold();
Jin Qian12690d52017-10-13 18:17:04 -07001785 }, DateUtils.SECOND_IN_MILLIS);
1786 } else {
1787 onDaemonConnected();
1788 }
Jeff Sharkey1019de92017-09-06 13:47:03 -06001789 }
1790
Jeff Sharkey11697f52018-12-13 10:14:42 -07001791 private void servicesReady() {
Sudheer Shankaff585072019-01-29 23:19:45 -08001792 mPmInternal = LocalServices.getService(PackageManagerInternal.class);
Sudheer Shankaff585072019-01-29 23:19:45 -08001793
1794 mIPackageManager = IPackageManager.Stub.asInterface(
1795 ServiceManager.getService("package"));
1796 mIAppOpsService = IAppOpsService.Stub.asInterface(
1797 ServiceManager.getService(Context.APP_OPS_SERVICE));
Zim74a9bba2019-09-03 20:49:13 +01001798
1799 ProviderInfo provider = mPmInternal.resolveContentProvider(
1800 MediaStore.AUTHORITY, PackageManager.MATCH_DIRECT_BOOT_AWARE
1801 | PackageManager.MATCH_DIRECT_BOOT_UNAWARE,
1802 UserHandle.getUserId(UserHandle.USER_SYSTEM));
1803 if (provider != null) {
Martijn Coenen44db1ac2019-12-03 16:06:19 +01001804 mMediaStoreAuthorityAppId = UserHandle.getAppId(provider.applicationInfo.uid);
Zim74a9bba2019-09-03 20:49:13 +01001805 }
1806
Martijn Coenen496ac002020-01-08 14:55:53 +01001807 provider = mPmInternal.resolveContentProvider(
1808 Downloads.Impl.AUTHORITY, PackageManager.MATCH_DIRECT_BOOT_AWARE
1809 | PackageManager.MATCH_DIRECT_BOOT_UNAWARE,
1810 UserHandle.getUserId(UserHandle.USER_SYSTEM));
1811
1812 if (provider != null) {
1813 mDownloadsAuthorityAppId = UserHandle.getAppId(provider.applicationInfo.uid);
1814 }
1815
Sudheer Shankaff585072019-01-29 23:19:45 -08001816 try {
1817 mIAppOpsService.startWatchingMode(OP_REQUEST_INSTALL_PACKAGES, null, mAppOpsCallback);
Sudheer Shanka783c90e2019-04-12 13:55:20 -07001818 mIAppOpsService.startWatchingMode(OP_LEGACY_STORAGE, null, mAppOpsCallback);
Sudheer Shankaff585072019-01-29 23:19:45 -08001819 } catch (RemoteException e) {
1820 }
Jeff Sharkey11697f52018-12-13 10:14:42 -07001821 }
1822
1823 private static long getLastAccessTime(AppOpsManager manager,
1824 int uid, String packageName, int[] ops) {
1825 long maxTime = 0;
1826 final List<AppOpsManager.PackageOps> pkgs = manager.getOpsForPackage(uid, packageName, ops);
Eugene Susla04d021c2018-12-17 14:49:59 -08001827 for (AppOpsManager.PackageOps pkg : CollectionUtils.emptyIfNull(pkgs)) {
1828 for (AppOpsManager.OpEntry op : CollectionUtils.emptyIfNull(pkg.getOps())) {
Svet Ganovaf189e32019-02-15 18:45:29 -08001829 maxTime = Math.max(maxTime, op.getLastAccessTime(
1830 AppOpsManager.OP_FLAGS_ALL_TRUSTED));
Jeff Sharkey11697f52018-12-13 10:14:42 -07001831 }
1832 }
1833 return maxTime;
1834 }
1835
Jeff Sharkey56e62932015-03-21 20:41:00 -07001836 private void systemReady() {
Wale Ogunwale6767eae2018-05-03 15:52:51 -07001837 LocalServices.getService(ActivityTaskManagerInternal.class)
Jeff Sharkey9765e442017-12-14 22:15:14 -07001838 .registerScreenObserver(this);
1839
Jeff Sharkeyb049e212012-09-07 23:16:01 -07001840 mHandler.obtainMessage(H_SYSTEM_READY).sendToTarget();
1841 }
1842
Yasuhiro Matsuda87a38b52015-07-24 22:10:16 +09001843 private void bootCompleted() {
1844 mBootCompleted = true;
Daniel Rosenberg137aed12019-03-15 18:41:11 -07001845 mHandler.obtainMessage(H_BOOT_COMPLETED).sendToTarget();
Abhijeet Kaur10c56552019-12-04 14:17:40 +00001846 updateFusePropFromSettings();
Daniel Rosenberg137aed12019-03-15 18:41:11 -07001847 }
1848
1849 private void handleBootCompleted() {
1850 initIfBootedAndConnected();
1851 resetIfBootedAndConnected();
Yasuhiro Matsuda87a38b52015-07-24 22:10:16 +09001852 }
1853
Jeff Sharkeyfced5342015-05-10 14:53:34 -07001854 private String getDefaultPrimaryStorageUuid() {
1855 if (SystemProperties.getBoolean(StorageManager.PROP_PRIMARY_PHYSICAL, false)) {
1856 return StorageManager.UUID_PRIMARY_PHYSICAL;
1857 } else {
1858 return StorageManager.UUID_PRIVATE_INTERNAL;
1859 }
1860 }
1861
Andreas Gampea36dc622018-02-05 17:19:22 -08001862 @GuardedBy("mLock")
Jeff Sharkeyb36586a2015-04-27 08:42:28 -07001863 private void readSettingsLocked() {
1864 mRecords.clear();
Jeff Sharkeyfced5342015-05-10 14:53:34 -07001865 mPrimaryStorageUuid = getDefaultPrimaryStorageUuid();
Jeff Sharkeyd95d3bf2015-04-14 21:39:44 -07001866
1867 FileInputStream fis = null;
1868 try {
Jeff Sharkeyb36586a2015-04-27 08:42:28 -07001869 fis = mSettingsFile.openRead();
Jeff Sharkeyd95d3bf2015-04-14 21:39:44 -07001870 final XmlPullParser in = Xml.newPullParser();
Wojciech Staszkiewicz9e9e2e72015-05-08 14:58:46 +01001871 in.setInput(fis, StandardCharsets.UTF_8.name());
Jeff Sharkeyd95d3bf2015-04-14 21:39:44 -07001872
1873 int type;
1874 while ((type = in.next()) != END_DOCUMENT) {
1875 if (type == START_TAG) {
1876 final String tag = in.getName();
Jeff Sharkey620b32b2015-04-23 19:36:02 -07001877 if (TAG_VOLUMES.equals(tag)) {
1878 final int version = readIntAttribute(in, ATTR_VERSION, VERSION_INIT);
Jeff Sharkeyfced5342015-05-10 14:53:34 -07001879 final boolean primaryPhysical = SystemProperties.getBoolean(
1880 StorageManager.PROP_PRIMARY_PHYSICAL, false);
1881 final boolean validAttr = (version >= VERSION_FIX_PRIMARY)
1882 || (version >= VERSION_ADD_PRIMARY && !primaryPhysical);
1883 if (validAttr) {
Jeff Sharkey620b32b2015-04-23 19:36:02 -07001884 mPrimaryStorageUuid = readStringAttribute(in,
1885 ATTR_PRIMARY_STORAGE_UUID);
Jeff Sharkey620b32b2015-04-23 19:36:02 -07001886 }
Jeff Sharkey620b32b2015-04-23 19:36:02 -07001887 } else if (TAG_VOLUME.equals(tag)) {
Jeff Sharkeyb36586a2015-04-27 08:42:28 -07001888 final VolumeRecord rec = readVolumeRecord(in);
1889 mRecords.put(rec.fsUuid, rec);
Jeff Sharkeyd95d3bf2015-04-14 21:39:44 -07001890 }
1891 }
1892 }
1893 } catch (FileNotFoundException e) {
1894 // Missing metadata is okay, probably first boot
1895 } catch (IOException e) {
1896 Slog.wtf(TAG, "Failed reading metadata", e);
1897 } catch (XmlPullParserException e) {
1898 Slog.wtf(TAG, "Failed reading metadata", e);
1899 } finally {
1900 IoUtils.closeQuietly(fis);
1901 }
1902 }
1903
Andreas Gampea36dc622018-02-05 17:19:22 -08001904 @GuardedBy("mLock")
Jeff Sharkeyb36586a2015-04-27 08:42:28 -07001905 private void writeSettingsLocked() {
Jeff Sharkeyd95d3bf2015-04-14 21:39:44 -07001906 FileOutputStream fos = null;
1907 try {
Jeff Sharkeyb36586a2015-04-27 08:42:28 -07001908 fos = mSettingsFile.startWrite();
Jeff Sharkeyd95d3bf2015-04-14 21:39:44 -07001909
1910 XmlSerializer out = new FastXmlSerializer();
Wojciech Staszkiewicz9e9e2e72015-05-08 14:58:46 +01001911 out.setOutput(fos, StandardCharsets.UTF_8.name());
Jeff Sharkeyd95d3bf2015-04-14 21:39:44 -07001912 out.startDocument(null, true);
1913 out.startTag(null, TAG_VOLUMES);
Jeff Sharkeyfced5342015-05-10 14:53:34 -07001914 writeIntAttribute(out, ATTR_VERSION, VERSION_FIX_PRIMARY);
Jeff Sharkey620b32b2015-04-23 19:36:02 -07001915 writeStringAttribute(out, ATTR_PRIMARY_STORAGE_UUID, mPrimaryStorageUuid);
Jeff Sharkeyb36586a2015-04-27 08:42:28 -07001916 final int size = mRecords.size();
Jeff Sharkeyd95d3bf2015-04-14 21:39:44 -07001917 for (int i = 0; i < size; i++) {
Jeff Sharkeyb36586a2015-04-27 08:42:28 -07001918 final VolumeRecord rec = mRecords.valueAt(i);
1919 writeVolumeRecord(out, rec);
Jeff Sharkeyd95d3bf2015-04-14 21:39:44 -07001920 }
1921 out.endTag(null, TAG_VOLUMES);
1922 out.endDocument();
1923
Jeff Sharkeyb36586a2015-04-27 08:42:28 -07001924 mSettingsFile.finishWrite(fos);
Jeff Sharkeyd95d3bf2015-04-14 21:39:44 -07001925 } catch (IOException e) {
1926 if (fos != null) {
Jeff Sharkeyb36586a2015-04-27 08:42:28 -07001927 mSettingsFile.failWrite(fos);
Jeff Sharkeyd95d3bf2015-04-14 21:39:44 -07001928 }
1929 }
1930 }
1931
Jeff Sharkeyb36586a2015-04-27 08:42:28 -07001932 public static VolumeRecord readVolumeRecord(XmlPullParser in) throws IOException {
1933 final int type = readIntAttribute(in, ATTR_TYPE);
1934 final String fsUuid = readStringAttribute(in, ATTR_FS_UUID);
1935 final VolumeRecord meta = new VolumeRecord(type, fsUuid);
Jeff Sharkey5cc0df22015-06-17 19:44:05 -07001936 meta.partGuid = readStringAttribute(in, ATTR_PART_GUID);
Jeff Sharkeyb36586a2015-04-27 08:42:28 -07001937 meta.nickname = readStringAttribute(in, ATTR_NICKNAME);
1938 meta.userFlags = readIntAttribute(in, ATTR_USER_FLAGS);
Jeff Sharkey3811f352019-05-14 11:54:36 -06001939 meta.createdMillis = readLongAttribute(in, ATTR_CREATED_MILLIS, 0);
1940 meta.lastSeenMillis = readLongAttribute(in, ATTR_LAST_SEEN_MILLIS, 0);
1941 meta.lastTrimMillis = readLongAttribute(in, ATTR_LAST_TRIM_MILLIS, 0);
1942 meta.lastBenchMillis = readLongAttribute(in, ATTR_LAST_BENCH_MILLIS, 0);
Jeff Sharkeyb36586a2015-04-27 08:42:28 -07001943 return meta;
1944 }
1945
1946 public static void writeVolumeRecord(XmlSerializer out, VolumeRecord rec) throws IOException {
1947 out.startTag(null, TAG_VOLUME);
1948 writeIntAttribute(out, ATTR_TYPE, rec.type);
1949 writeStringAttribute(out, ATTR_FS_UUID, rec.fsUuid);
Jeff Sharkey5cc0df22015-06-17 19:44:05 -07001950 writeStringAttribute(out, ATTR_PART_GUID, rec.partGuid);
Jeff Sharkeyb36586a2015-04-27 08:42:28 -07001951 writeStringAttribute(out, ATTR_NICKNAME, rec.nickname);
1952 writeIntAttribute(out, ATTR_USER_FLAGS, rec.userFlags);
Jeff Sharkeye8a4b662015-06-27 15:43:45 -07001953 writeLongAttribute(out, ATTR_CREATED_MILLIS, rec.createdMillis);
Jeff Sharkey3811f352019-05-14 11:54:36 -06001954 writeLongAttribute(out, ATTR_LAST_SEEN_MILLIS, rec.lastSeenMillis);
Jeff Sharkeye8a4b662015-06-27 15:43:45 -07001955 writeLongAttribute(out, ATTR_LAST_TRIM_MILLIS, rec.lastTrimMillis);
1956 writeLongAttribute(out, ATTR_LAST_BENCH_MILLIS, rec.lastBenchMillis);
Jeff Sharkeyb36586a2015-04-27 08:42:28 -07001957 out.endTag(null, TAG_VOLUME);
1958 }
1959
San Mehat207e5382010-02-04 20:46:54 -08001960 /**
San Mehat4270e1e2010-01-29 05:32:19 -08001961 * Exposed API calls below here
1962 */
1963
Jeff Sharkey7151a9a2015-04-04 15:22:37 -07001964 @Override
Sudheer Shanka2250d562016-11-07 15:41:02 -08001965 public void registerListener(IStorageEventListener listener) {
Jeff Sharkey7151a9a2015-04-04 15:22:37 -07001966 mCallbacks.register(listener);
San Mehat4270e1e2010-01-29 05:32:19 -08001967 }
1968
Jeff Sharkey7151a9a2015-04-04 15:22:37 -07001969 @Override
Sudheer Shanka2250d562016-11-07 15:41:02 -08001970 public void unregisterListener(IStorageEventListener listener) {
Jeff Sharkey7151a9a2015-04-04 15:22:37 -07001971 mCallbacks.unregister(listener);
San Mehat4270e1e2010-01-29 05:32:19 -08001972 }
1973
Jeff Sharkey48877892015-03-18 11:27:19 -07001974 @Override
Sudheer Shanka2250d562016-11-07 15:41:02 -08001975 public void shutdown(final IStorageShutdownObserver observer) {
Jeff Sharkey48877892015-03-18 11:27:19 -07001976 enforcePermission(android.Manifest.permission.SHUTDOWN);
San Mehat4270e1e2010-01-29 05:32:19 -08001977
San Mehata5078592010-03-25 09:36:54 -07001978 Slog.i(TAG, "Shutting down");
Jeff Sharkey48877892015-03-18 11:27:19 -07001979 mHandler.obtainMessage(H_SHUTDOWN, observer).sendToTarget();
San Mehat4270e1e2010-01-29 05:32:19 -08001980 }
1981
Jeff Sharkey48877892015-03-18 11:27:19 -07001982 @Override
Jeff Sharkey7151a9a2015-04-04 15:22:37 -07001983 public void mount(String volId) {
1984 enforcePermission(android.Manifest.permission.MOUNT_UNMOUNT_FILESYSTEMS);
Jeff Sharkey7151a9a2015-04-04 15:22:37 -07001985
Jeff Sharkeyef10ee02015-07-05 14:17:27 -07001986 final VolumeInfo vol = findVolumeByIdOrThrow(volId);
Jeff Sharkey2e606d72015-07-27 14:19:54 -07001987 if (isMountDisallowed(vol)) {
1988 throw new SecurityException("Mounting " + volId + " restricted by policy");
Jeff Sharkey7151a9a2015-04-04 15:22:37 -07001989 }
Zim17be6f92019-09-25 14:37:55 +01001990
Sudheer Shanka3f0645b2018-09-18 13:07:59 -07001991 mount(vol);
1992 }
1993
Zim17be6f92019-09-25 14:37:55 +01001994 private void mount(VolumeInfo vol) {
Jeff Sharkey7151a9a2015-04-04 15:22:37 -07001995 try {
Zim17be6f92019-09-25 14:37:55 +01001996 // TODO(b/135341433): Remove paranoid logging when FUSE is stable
1997 Slog.i(TAG, "Mounting volume " + vol);
Zim95eca1d2019-11-15 18:03:00 +00001998 mVold.mount(vol.id, vol.mountFlags, vol.mountUserId, new IVoldMountCallback.Stub() {
1999 @Override
Zim7924a512020-01-15 15:23:54 +00002000 public boolean onVolumeChecking(FileDescriptor fd, String path,
Zim95eca1d2019-11-15 18:03:00 +00002001 String internalPath) {
2002 vol.path = path;
2003 vol.internalPath = internalPath;
Zim7924a512020-01-15 15:23:54 +00002004 ParcelFileDescriptor pfd = new ParcelFileDescriptor(fd);
Zim95eca1d2019-11-15 18:03:00 +00002005 try {
Zim7924a512020-01-15 15:23:54 +00002006 mStorageSessionController.onVolumeMount(pfd, vol);
Zim95eca1d2019-11-15 18:03:00 +00002007 return true;
2008 } catch (ExternalStorageServiceException e) {
Zim7924a512020-01-15 15:23:54 +00002009 Slog.e(TAG, "Failed to mount volume " + vol, e);
2010
Zimb8ca0542020-01-16 22:54:48 +00002011 int nextResetSeconds = REMOTE_TIMEOUT_SECONDS * 2;
2012 Slog.i(TAG, "Scheduling reset in " + nextResetSeconds + "s");
Zim7924a512020-01-15 15:23:54 +00002013 mHandler.removeMessages(H_RESET);
2014 mHandler.sendMessageDelayed(mHandler.obtainMessage(H_RESET),
Zimb8ca0542020-01-16 22:54:48 +00002015 TimeUnit.SECONDS.toMillis(nextResetSeconds));
Zim95eca1d2019-11-15 18:03:00 +00002016 return false;
Zim7924a512020-01-15 15:23:54 +00002017 } finally {
2018 try {
2019 pfd.close();
2020 } catch (Exception e) {
2021 Slog.e(TAG, "Failed to close FUSE device fd", e);
2022 }
Zim95eca1d2019-11-15 18:03:00 +00002023 }
2024 }
2025 });
Zim17be6f92019-09-25 14:37:55 +01002026 Slog.i(TAG, "Mounted volume " + vol);
Jeff Sharkeyace874b2017-09-07 15:27:33 -06002027 } catch (Exception e) {
2028 Slog.wtf(TAG, e);
Jeff Sharkey7151a9a2015-04-04 15:22:37 -07002029 }
2030 }
2031
2032 @Override
2033 public void unmount(String volId) {
2034 enforcePermission(android.Manifest.permission.MOUNT_UNMOUNT_FILESYSTEMS);
Jeff Sharkey7151a9a2015-04-04 15:22:37 -07002035
Jeff Sharkeyef10ee02015-07-05 14:17:27 -07002036 final VolumeInfo vol = findVolumeByIdOrThrow(volId);
Sudheer Shanka3f0645b2018-09-18 13:07:59 -07002037 unmount(vol);
2038 }
2039
2040 private void unmount(VolumeInfo vol) {
Jeff Sharkey7151a9a2015-04-04 15:22:37 -07002041 try {
Jeff Sharkey54402792017-09-15 16:05:19 -06002042 mVold.unmount(vol.id);
Zim17be6f92019-09-25 14:37:55 +01002043 mStorageSessionController.onVolumeUnmount(vol);
Ricky Wai4482ab52019-12-10 19:08:18 +00002044 try {
2045 if (vol.type == VolumeInfo.TYPE_PRIVATE) {
2046 mInstaller.onPrivateVolumeRemoved(vol.getFsUuid());
2047 }
2048 } catch (Installer.InstallerException e) {
2049 Slog.e(TAG, "Failed unmount mirror data", e);
2050 }
Jeff Sharkeyace874b2017-09-07 15:27:33 -06002051 } catch (Exception e) {
2052 Slog.wtf(TAG, e);
Jeff Sharkey7151a9a2015-04-04 15:22:37 -07002053 }
2054 }
2055
2056 @Override
2057 public void format(String volId) {
Jeff Sharkey48877892015-03-18 11:27:19 -07002058 enforcePermission(android.Manifest.permission.MOUNT_FORMAT_FILESYSTEMS);
San Mehat5b77dab2010-01-26 13:28:50 -08002059
Jeff Sharkeyef10ee02015-07-05 14:17:27 -07002060 final VolumeInfo vol = findVolumeByIdOrThrow(volId);
Jeff Sharkey7151a9a2015-04-04 15:22:37 -07002061 try {
Jeff Sharkey54402792017-09-15 16:05:19 -06002062 mVold.format(vol.id, "auto");
Jeff Sharkeyace874b2017-09-07 15:27:33 -06002063 } catch (Exception e) {
2064 Slog.wtf(TAG, e);
Jeff Sharkey48877892015-03-18 11:27:19 -07002065 }
Jeff Sharkey7151a9a2015-04-04 15:22:37 -07002066 }
2067
2068 @Override
Jeff Sharkey7e19f532017-11-06 13:54:11 -07002069 public void benchmark(String volId, IVoldTaskListener listener) {
Jeff Sharkey9756d752015-05-14 21:07:42 -07002070 enforcePermission(android.Manifest.permission.MOUNT_FORMAT_FILESYSTEMS);
Jeff Sharkey9756d752015-05-14 21:07:42 -07002071
2072 try {
Jeff Sharkeyb302c542017-09-15 12:57:59 -06002073 mVold.benchmark(volId, new IVoldTaskListener.Stub() {
2074 @Override
2075 public void onStatus(int status, PersistableBundle extras) {
Jeff Sharkey7e19f532017-11-06 13:54:11 -07002076 dispatchOnStatus(listener, status, extras);
Jeff Sharkeyb302c542017-09-15 12:57:59 -06002077 }
2078
2079 @Override
2080 public void onFinished(int status, PersistableBundle extras) {
Jeff Sharkey7e19f532017-11-06 13:54:11 -07002081 dispatchOnFinished(listener, status, extras);
Jeff Sharkeyb302c542017-09-15 12:57:59 -06002082
2083 final String path = extras.getString("path");
2084 final String ident = extras.getString("ident");
2085 final long create = extras.getLong("create");
2086 final long run = extras.getLong("run");
2087 final long destroy = extras.getLong("destroy");
2088
2089 final DropBoxManager dropBox = mContext.getSystemService(DropBoxManager.class);
2090 dropBox.addText(TAG_STORAGE_BENCHMARK, scrubPath(path)
2091 + " " + ident + " " + create + " " + run + " " + destroy);
2092
2093 synchronized (mLock) {
2094 final VolumeRecord rec = findRecordForPath(path);
2095 if (rec != null) {
2096 rec.lastBenchMillis = System.currentTimeMillis();
2097 writeSettingsLocked();
2098 }
2099 }
2100 }
2101 });
Jeff Sharkey7e19f532017-11-06 13:54:11 -07002102 } catch (RemoteException e) {
2103 throw e.rethrowAsRuntimeException();
Jeff Sharkey9756d752015-05-14 21:07:42 -07002104 }
2105 }
2106
2107 @Override
Jeff Sharkey7151a9a2015-04-04 15:22:37 -07002108 public void partitionPublic(String diskId) {
2109 enforcePermission(android.Manifest.permission.MOUNT_FORMAT_FILESYSTEMS);
Jeff Sharkey7151a9a2015-04-04 15:22:37 -07002110
Jeff Sharkeyeba260d2015-04-19 14:35:16 -07002111 final CountDownLatch latch = findOrCreateDiskScanLatch(diskId);
Jeff Sharkey7151a9a2015-04-04 15:22:37 -07002112 try {
Jeff Sharkey54402792017-09-15 16:05:19 -06002113 mVold.partition(diskId, IVold.PARTITION_TYPE_PUBLIC, -1);
Jeff Sharkeyedcdaf62015-07-09 09:45:36 -07002114 waitForLatch(latch, "partitionPublic", 3 * DateUtils.MINUTE_IN_MILLIS);
Jeff Sharkeyace874b2017-09-07 15:27:33 -06002115 } catch (Exception e) {
2116 Slog.wtf(TAG, e);
Jeff Sharkey7151a9a2015-04-04 15:22:37 -07002117 }
2118 }
2119
2120 @Override
2121 public void partitionPrivate(String diskId) {
2122 enforcePermission(android.Manifest.permission.MOUNT_FORMAT_FILESYSTEMS);
Amith Yamasani462ac3a2015-06-30 14:21:01 -07002123 enforceAdminUser();
Jeff Sharkey7151a9a2015-04-04 15:22:37 -07002124
Jeff Sharkeyeba260d2015-04-19 14:35:16 -07002125 final CountDownLatch latch = findOrCreateDiskScanLatch(diskId);
Jeff Sharkey7151a9a2015-04-04 15:22:37 -07002126 try {
Jeff Sharkey54402792017-09-15 16:05:19 -06002127 mVold.partition(diskId, IVold.PARTITION_TYPE_PRIVATE, -1);
Jeff Sharkeyedcdaf62015-07-09 09:45:36 -07002128 waitForLatch(latch, "partitionPrivate", 3 * DateUtils.MINUTE_IN_MILLIS);
Jeff Sharkeyace874b2017-09-07 15:27:33 -06002129 } catch (Exception e) {
2130 Slog.wtf(TAG, e);
Jeff Sharkey7151a9a2015-04-04 15:22:37 -07002131 }
2132 }
2133
2134 @Override
2135 public void partitionMixed(String diskId, int ratio) {
2136 enforcePermission(android.Manifest.permission.MOUNT_FORMAT_FILESYSTEMS);
Amith Yamasani462ac3a2015-06-30 14:21:01 -07002137 enforceAdminUser();
Jeff Sharkey7151a9a2015-04-04 15:22:37 -07002138
Jeff Sharkeyeba260d2015-04-19 14:35:16 -07002139 final CountDownLatch latch = findOrCreateDiskScanLatch(diskId);
Jeff Sharkey7151a9a2015-04-04 15:22:37 -07002140 try {
Jeff Sharkey54402792017-09-15 16:05:19 -06002141 mVold.partition(diskId, IVold.PARTITION_TYPE_MIXED, ratio);
Jeff Sharkeyedcdaf62015-07-09 09:45:36 -07002142 waitForLatch(latch, "partitionMixed", 3 * DateUtils.MINUTE_IN_MILLIS);
Jeff Sharkeyace874b2017-09-07 15:27:33 -06002143 } catch (Exception e) {
2144 Slog.wtf(TAG, e);
Jeff Sharkey7151a9a2015-04-04 15:22:37 -07002145 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002146 }
2147
Jeff Sharkey48877892015-03-18 11:27:19 -07002148 @Override
Jeff Sharkeyb36586a2015-04-27 08:42:28 -07002149 public void setVolumeNickname(String fsUuid, String nickname) {
Jeff Sharkeyd95d3bf2015-04-14 21:39:44 -07002150 enforcePermission(android.Manifest.permission.MOUNT_UNMOUNT_FILESYSTEMS);
Jeff Sharkeyd95d3bf2015-04-14 21:39:44 -07002151
Daulet Zhanguzinea1a7ca2020-01-03 09:46:50 +00002152 Objects.requireNonNull(fsUuid);
Jeff Sharkeyd95d3bf2015-04-14 21:39:44 -07002153 synchronized (mLock) {
Jeff Sharkeyb36586a2015-04-27 08:42:28 -07002154 final VolumeRecord rec = mRecords.get(fsUuid);
2155 rec.nickname = nickname;
Jeff Sharkey50a05452015-04-29 11:24:52 -07002156 mCallbacks.notifyVolumeRecordChanged(rec);
Jeff Sharkeyb36586a2015-04-27 08:42:28 -07002157 writeSettingsLocked();
Jeff Sharkeyd95d3bf2015-04-14 21:39:44 -07002158 }
2159 }
2160
2161 @Override
Jeff Sharkeyb36586a2015-04-27 08:42:28 -07002162 public void setVolumeUserFlags(String fsUuid, int flags, int mask) {
Jeff Sharkeyd95d3bf2015-04-14 21:39:44 -07002163 enforcePermission(android.Manifest.permission.MOUNT_UNMOUNT_FILESYSTEMS);
Jeff Sharkeyd95d3bf2015-04-14 21:39:44 -07002164
Daulet Zhanguzinea1a7ca2020-01-03 09:46:50 +00002165 Objects.requireNonNull(fsUuid);
Jeff Sharkeyd95d3bf2015-04-14 21:39:44 -07002166 synchronized (mLock) {
Jeff Sharkeyb36586a2015-04-27 08:42:28 -07002167 final VolumeRecord rec = mRecords.get(fsUuid);
2168 rec.userFlags = (rec.userFlags & ~mask) | (flags & mask);
Jeff Sharkey50a05452015-04-29 11:24:52 -07002169 mCallbacks.notifyVolumeRecordChanged(rec);
Jeff Sharkeyb36586a2015-04-27 08:42:28 -07002170 writeSettingsLocked();
2171 }
2172 }
2173
2174 @Override
2175 public void forgetVolume(String fsUuid) {
2176 enforcePermission(android.Manifest.permission.MOUNT_UNMOUNT_FILESYSTEMS);
Jeff Sharkeyb36586a2015-04-27 08:42:28 -07002177
Daulet Zhanguzinea1a7ca2020-01-03 09:46:50 +00002178 Objects.requireNonNull(fsUuid);
Jeff Sharkey5a9bb742015-11-03 10:15:57 -08002179
Jeff Sharkeyb36586a2015-04-27 08:42:28 -07002180 synchronized (mLock) {
Jeff Sharkey5cc0df22015-06-17 19:44:05 -07002181 final VolumeRecord rec = mRecords.remove(fsUuid);
2182 if (rec != null && !TextUtils.isEmpty(rec.partGuid)) {
Jeff Sharkeydb4b6192017-10-24 11:08:50 -06002183 mHandler.obtainMessage(H_PARTITION_FORGET, rec).sendToTarget();
Jeff Sharkey5cc0df22015-06-17 19:44:05 -07002184 }
2185 mCallbacks.notifyVolumeForgotten(fsUuid);
Jeff Sharkey50a05452015-04-29 11:24:52 -07002186
2187 // If this had been primary storage, revert back to internal and
2188 // reset vold so we bind into new volume into place.
2189 if (Objects.equals(mPrimaryStorageUuid, fsUuid)) {
Jeff Sharkeyfced5342015-05-10 14:53:34 -07002190 mPrimaryStorageUuid = getDefaultPrimaryStorageUuid();
Jeff Sharkey5a9bb742015-11-03 10:15:57 -08002191 mHandler.obtainMessage(H_RESET).sendToTarget();
Jeff Sharkey50a05452015-04-29 11:24:52 -07002192 }
2193
Jeff Sharkeyb36586a2015-04-27 08:42:28 -07002194 writeSettingsLocked();
Jeff Sharkeyd95d3bf2015-04-14 21:39:44 -07002195 }
2196 }
2197
Jeff Sharkey7d2af542015-05-12 15:27:15 -07002198 @Override
2199 public void forgetAllVolumes() {
2200 enforcePermission(android.Manifest.permission.MOUNT_UNMOUNT_FILESYSTEMS);
Jeff Sharkey7d2af542015-05-12 15:27:15 -07002201
Jeff Sharkey50a05452015-04-29 11:24:52 -07002202 synchronized (mLock) {
2203 for (int i = 0; i < mRecords.size(); i++) {
2204 final String fsUuid = mRecords.keyAt(i);
Jeff Sharkey5cc0df22015-06-17 19:44:05 -07002205 final VolumeRecord rec = mRecords.valueAt(i);
2206 if (!TextUtils.isEmpty(rec.partGuid)) {
Jeff Sharkeydb4b6192017-10-24 11:08:50 -06002207 mHandler.obtainMessage(H_PARTITION_FORGET, rec).sendToTarget();
Jeff Sharkey5cc0df22015-06-17 19:44:05 -07002208 }
Jeff Sharkey50a05452015-04-29 11:24:52 -07002209 mCallbacks.notifyVolumeForgotten(fsUuid);
2210 }
Jeff Sharkey50a05452015-04-29 11:24:52 -07002211 mRecords.clear();
Jeff Sharkey50a05452015-04-29 11:24:52 -07002212
Jeff Sharkeyfced5342015-05-10 14:53:34 -07002213 if (!Objects.equals(StorageManager.UUID_PRIVATE_INTERNAL, mPrimaryStorageUuid)) {
2214 mPrimaryStorageUuid = getDefaultPrimaryStorageUuid();
2215 }
2216
2217 writeSettingsLocked();
Jeff Sharkey5a9bb742015-11-03 10:15:57 -08002218 mHandler.obtainMessage(H_RESET).sendToTarget();
Jeff Sharkey50a05452015-04-29 11:24:52 -07002219 }
2220 }
2221
Jeff Sharkeydb4b6192017-10-24 11:08:50 -06002222 private void forgetPartition(String partGuid, String fsUuid) {
Jeff Sharkey5cc0df22015-06-17 19:44:05 -07002223 try {
Jeff Sharkeydb4b6192017-10-24 11:08:50 -06002224 mVold.forgetPartition(partGuid, fsUuid);
Jeff Sharkeyace874b2017-09-07 15:27:33 -06002225 } catch (Exception e) {
2226 Slog.wtf(TAG, e);
Jeff Sharkey5cc0df22015-06-17 19:44:05 -07002227 }
2228 }
2229
Jeff Sharkey31d0b702016-11-21 14:16:53 -07002230 @Override
Jeff Sharkey7e19f532017-11-06 13:54:11 -07002231 public void fstrim(int flags, IVoldTaskListener listener) {
Jeff Sharkey31d0b702016-11-21 14:16:53 -07002232 enforcePermission(android.Manifest.permission.MOUNT_FORMAT_FILESYSTEMS);
Jeff Sharkey31d0b702016-11-21 14:16:53 -07002233
2234 try {
Sandeep Patil7d5c4912019-04-15 09:27:30 -07002235 // Block based checkpoint process runs fstrim. So, if checkpoint is in progress
2236 // (first boot after OTA), We skip idle maintenance and make sure the last
2237 // fstrim time is still updated. If file based checkpoints are used, we run
2238 // idle maintenance (GC + fstrim) regardless of checkpoint status.
2239 if (!needsCheckpoint() || !supportsBlockCheckpoint()) {
2240 mVold.fstrim(flags, new IVoldTaskListener.Stub() {
2241 @Override
2242 public void onStatus(int status, PersistableBundle extras) {
2243 dispatchOnStatus(listener, status, extras);
Jeff Sharkey7e19f532017-11-06 13:54:11 -07002244
Sandeep Patil7d5c4912019-04-15 09:27:30 -07002245 // Ignore trim failures
2246 if (status != 0) return;
Jeff Sharkeyb302c542017-09-15 12:57:59 -06002247
Sandeep Patil7d5c4912019-04-15 09:27:30 -07002248 final String path = extras.getString("path");
2249 final long bytes = extras.getLong("bytes");
2250 final long time = extras.getLong("time");
Jeff Sharkeyb302c542017-09-15 12:57:59 -06002251
Sandeep Patil7d5c4912019-04-15 09:27:30 -07002252 final DropBoxManager dropBox = mContext.getSystemService(DropBoxManager.class);
2253 dropBox.addText(TAG_STORAGE_TRIM, scrubPath(path) + " " + bytes + " " + time);
Jeff Sharkeyb302c542017-09-15 12:57:59 -06002254
Sandeep Patil7d5c4912019-04-15 09:27:30 -07002255 synchronized (mLock) {
2256 final VolumeRecord rec = findRecordForPath(path);
2257 if (rec != null) {
2258 rec.lastTrimMillis = System.currentTimeMillis();
2259 writeSettingsLocked();
2260 }
Jeff Sharkeyb302c542017-09-15 12:57:59 -06002261 }
2262 }
Jeff Sharkeyb302c542017-09-15 12:57:59 -06002263
Sandeep Patil7d5c4912019-04-15 09:27:30 -07002264 @Override
2265 public void onFinished(int status, PersistableBundle extras) {
2266 dispatchOnFinished(listener, status, extras);
Jeff Sharkey7e19f532017-11-06 13:54:11 -07002267
Sandeep Patil7d5c4912019-04-15 09:27:30 -07002268 // TODO: benchmark when desired
2269 }
2270 });
2271 } else {
2272 Slog.i(TAG, "Skipping fstrim - block based checkpoint in progress");
2273 }
Jeff Sharkey7e19f532017-11-06 13:54:11 -07002274 } catch (RemoteException e) {
2275 throw e.rethrowAsRuntimeException();
Jeff Sharkey31d0b702016-11-21 14:16:53 -07002276 }
2277 }
2278
Jin Qiana85b9912017-10-17 15:48:18 -07002279 void runIdleMaint(Runnable callback) {
2280 enforcePermission(android.Manifest.permission.MOUNT_FORMAT_FILESYSTEMS);
2281
2282 try {
Sandeep Patil7d5c4912019-04-15 09:27:30 -07002283 // Block based checkpoint process runs fstrim. So, if checkpoint is in progress
2284 // (first boot after OTA), We skip idle maintenance and make sure the last
2285 // fstrim time is still updated. If file based checkpoints are used, we run
2286 // idle maintenance (GC + fstrim) regardless of checkpoint status.
2287 if (!needsCheckpoint() || !supportsBlockCheckpoint()) {
2288 mVold.runIdleMaint(new IVoldTaskListener.Stub() {
2289 @Override
2290 public void onStatus(int status, PersistableBundle extras) {
2291 // Not currently used
Jin Qiana85b9912017-10-17 15:48:18 -07002292 }
Sandeep Patil7d5c4912019-04-15 09:27:30 -07002293 @Override
2294 public void onFinished(int status, PersistableBundle extras) {
2295 if (callback != null) {
2296 BackgroundThread.getHandler().post(callback);
2297 }
2298 }
2299 });
2300 } else {
2301 Slog.i(TAG, "Skipping idle maintenance - block based checkpoint in progress");
2302 }
Jin Qiana85b9912017-10-17 15:48:18 -07002303 } catch (Exception e) {
2304 Slog.wtf(TAG, e);
2305 }
2306 }
2307
2308 @Override
2309 public void runIdleMaintenance() {
2310 runIdleMaint(null);
2311 }
2312
2313 void abortIdleMaint(Runnable callback) {
2314 enforcePermission(android.Manifest.permission.MOUNT_FORMAT_FILESYSTEMS);
2315
2316 try {
2317 mVold.abortIdleMaint(new IVoldTaskListener.Stub() {
2318 @Override
2319 public void onStatus(int status, PersistableBundle extras) {
2320 // Not currently used
2321 }
2322 @Override
2323 public void onFinished(int status, PersistableBundle extras) {
2324 if (callback != null) {
2325 BackgroundThread.getHandler().post(callback);
2326 }
2327 }
2328 });
2329 } catch (Exception e) {
2330 Slog.wtf(TAG, e);
2331 }
2332 }
2333
2334 @Override
2335 public void abortIdleMaintenance() {
2336 abortIdleMaint(null);
2337 }
2338
Svet Ganov6ee871e2015-07-10 14:29:33 -07002339 private void remountUidExternalStorage(int uid, int mode) {
Zim4f1a24d2019-11-27 16:35:58 +00002340 if (uid == Process.SYSTEM_UID) {
2341 // No need to remount uid for system because it has all access anyways
2342 return;
2343 }
2344
Jeff Sharkey9527b222015-06-24 15:24:48 -07002345 try {
Jeff Sharkey54402792017-09-15 16:05:19 -06002346 mVold.remountUid(uid, mode);
Jeff Sharkeyace874b2017-09-07 15:27:33 -06002347 } catch (Exception e) {
2348 Slog.wtf(TAG, e);
Jeff Sharkey9527b222015-06-24 15:24:48 -07002349 }
2350 }
2351
2352 @Override
Jeff Sharkey4c099d02015-05-15 13:45:00 -07002353 public void setDebugFlags(int flags, int mask) {
2354 enforcePermission(android.Manifest.permission.MOUNT_UNMOUNT_FILESYSTEMS);
Jeff Sharkey4c099d02015-05-15 13:45:00 -07002355
Jeff Sharkeyba512352015-11-12 20:17:45 -08002356 if ((mask & StorageManager.DEBUG_EMULATE_FBE) != 0) {
Jeff Sharkey00455bf2016-11-04 14:45:24 -06002357 if (!EMULATE_FBE_SUPPORTED) {
2358 throw new IllegalStateException(
2359 "Emulation not supported on this device");
2360 }
Paul Lawrence20be5d62016-02-26 13:51:17 -08002361 if (StorageManager.isFileEncryptedNativeOnly()) {
Jeff Sharkeyce14cd02015-12-07 15:35:42 -07002362 throw new IllegalStateException(
Jeff Sharkey00455bf2016-11-04 14:45:24 -06002363 "Emulation not supported on device with native FBE");
Jeff Sharkeyce14cd02015-12-07 15:35:42 -07002364 }
Jeff Sharkey5a785162016-03-21 13:02:06 -06002365 if (mLockPatternUtils.isCredentialRequiredToDecrypt(false)) {
2366 throw new IllegalStateException(
2367 "Emulation requires disabling 'Secure start-up' in Settings > Security");
2368 }
Jeff Sharkeyce14cd02015-12-07 15:35:42 -07002369
Jeff Sharkey1176e512016-02-29 17:01:26 -07002370 final long token = Binder.clearCallingIdentity();
2371 try {
2372 final boolean emulateFbe = (flags & StorageManager.DEBUG_EMULATE_FBE) != 0;
2373 SystemProperties.set(StorageManager.PROP_EMULATE_FBE, Boolean.toString(emulateFbe));
Jeff Sharkeyce14cd02015-12-07 15:35:42 -07002374
Jeff Sharkey1176e512016-02-29 17:01:26 -07002375 // Perform hard reboot to kick policy into place
2376 mContext.getSystemService(PowerManager.class).reboot(null);
2377 } finally {
2378 Binder.restoreCallingIdentity(token);
2379 }
Jeff Sharkeyba512352015-11-12 20:17:45 -08002380 }
Jeff Sharkey4c099d02015-05-15 13:45:00 -07002381
Jeff Sharkey901c0422018-04-20 13:11:20 -06002382 if ((mask & (StorageManager.DEBUG_ADOPTABLE_FORCE_ON
2383 | StorageManager.DEBUG_ADOPTABLE_FORCE_OFF)) != 0) {
2384 final String value;
2385 if ((flags & StorageManager.DEBUG_ADOPTABLE_FORCE_ON) != 0) {
2386 value = "force_on";
2387 } else if ((flags & StorageManager.DEBUG_ADOPTABLE_FORCE_OFF) != 0) {
2388 value = "force_off";
2389 } else {
2390 value = "";
2391 }
Jeff Sharkeyba512352015-11-12 20:17:45 -08002392
Jeff Sharkey901c0422018-04-20 13:11:20 -06002393 final long token = Binder.clearCallingIdentity();
2394 try {
2395 SystemProperties.set(StorageManager.PROP_ADOPTABLE, value);
2396
2397 // Reset storage to kick new setting into place
Jeff Sharkeyba512352015-11-12 20:17:45 -08002398 mHandler.obtainMessage(H_RESET).sendToTarget();
Jeff Sharkey901c0422018-04-20 13:11:20 -06002399 } finally {
2400 Binder.restoreCallingIdentity(token);
Jeff Sharkeyba512352015-11-12 20:17:45 -08002401 }
Jeff Sharkey4c099d02015-05-15 13:45:00 -07002402 }
Jeff Sharkey33dd1562016-04-07 11:05:33 -06002403
2404 if ((mask & (StorageManager.DEBUG_SDCARDFS_FORCE_ON
2405 | StorageManager.DEBUG_SDCARDFS_FORCE_OFF)) != 0) {
2406 final String value;
2407 if ((flags & StorageManager.DEBUG_SDCARDFS_FORCE_ON) != 0) {
2408 value = "force_on";
2409 } else if ((flags & StorageManager.DEBUG_SDCARDFS_FORCE_OFF) != 0) {
2410 value = "force_off";
2411 } else {
2412 value = "";
2413 }
2414
2415 final long token = Binder.clearCallingIdentity();
2416 try {
2417 SystemProperties.set(StorageManager.PROP_SDCARDFS, value);
2418
2419 // Reset storage to kick new setting into place
2420 mHandler.obtainMessage(H_RESET).sendToTarget();
2421 } finally {
2422 Binder.restoreCallingIdentity(token);
2423 }
2424 }
Jeff Sharkeye53e2d92017-03-25 23:14:06 -06002425
2426 if ((mask & StorageManager.DEBUG_VIRTUAL_DISK) != 0) {
2427 final boolean enabled = (flags & StorageManager.DEBUG_VIRTUAL_DISK) != 0;
2428
2429 final long token = Binder.clearCallingIdentity();
2430 try {
2431 SystemProperties.set(StorageManager.PROP_VIRTUAL_DISK, Boolean.toString(enabled));
2432
2433 // Reset storage to kick new setting into place
2434 mHandler.obtainMessage(H_RESET).sendToTarget();
2435 } finally {
2436 Binder.restoreCallingIdentity(token);
2437 }
2438 }
Sudheer Shankabe0febe2018-11-07 18:24:37 -08002439
Jeff Sharkeyb0c363b22018-12-15 11:53:03 -07002440 if ((mask & (StorageManager.DEBUG_ISOLATED_STORAGE_FORCE_ON
2441 | StorageManager.DEBUG_ISOLATED_STORAGE_FORCE_OFF)) != 0) {
2442 final int value;
2443 if ((flags & StorageManager.DEBUG_ISOLATED_STORAGE_FORCE_ON) != 0) {
2444 value = 1;
2445 } else if ((flags & StorageManager.DEBUG_ISOLATED_STORAGE_FORCE_OFF) != 0) {
2446 value = -1;
2447 } else {
2448 value = 0;
2449 }
Sudheer Shankabe0febe2018-11-07 18:24:37 -08002450
2451 final long token = Binder.clearCallingIdentity();
2452 try {
Jeff Sharkeyb0c363b22018-12-15 11:53:03 -07002453 Settings.Global.putInt(mContext.getContentResolver(),
2454 Settings.Global.ISOLATED_STORAGE_LOCAL, value);
2455 refreshIsolatedStorageSettings();
Sudheer Shankabe0febe2018-11-07 18:24:37 -08002456
2457 // Perform hard reboot to kick policy into place
Jeff Sharkey36274992019-02-27 12:09:57 -07002458 mHandler.post(() -> {
2459 mContext.getSystemService(PowerManager.class).reboot(null);
2460 });
2461 } finally {
2462 Binder.restoreCallingIdentity(token);
2463 }
2464 }
Jeff Sharkey4c099d02015-05-15 13:45:00 -07002465 }
2466
2467 @Override
Jeff Sharkey275e3e42015-04-24 16:10:32 -07002468 public String getPrimaryStorageUuid() {
Jeff Sharkey620b32b2015-04-23 19:36:02 -07002469 synchronized (mLock) {
2470 return mPrimaryStorageUuid;
2471 }
2472 }
2473
2474 @Override
Jeff Sharkey275e3e42015-04-24 16:10:32 -07002475 public void setPrimaryStorageUuid(String volumeUuid, IPackageMoveObserver callback) {
2476 enforcePermission(android.Manifest.permission.MOUNT_UNMOUNT_FILESYSTEMS);
Jeff Sharkey620b32b2015-04-23 19:36:02 -07002477
Jeff Sharkeya31460c2016-06-22 09:04:33 -06002478 final VolumeInfo from;
2479 final VolumeInfo to;
2480
Jeff Sharkey275e3e42015-04-24 16:10:32 -07002481 synchronized (mLock) {
Jeff Sharkeyfced5342015-05-10 14:53:34 -07002482 if (Objects.equals(mPrimaryStorageUuid, volumeUuid)) {
2483 throw new IllegalArgumentException("Primary storage already at " + volumeUuid);
Jeff Sharkey275e3e42015-04-24 16:10:32 -07002484 }
2485
2486 if (mMoveCallback != null) {
2487 throw new IllegalStateException("Move already in progress");
2488 }
2489 mMoveCallback = callback;
2490 mMoveTargetUuid = volumeUuid;
2491
Jeff Sharkeya65e6492017-06-21 13:45:11 -06002492 // We need all the users unlocked to move their primary storage
2493 final List<UserInfo> users = mContext.getSystemService(UserManager.class).getUsers();
2494 for (UserInfo user : users) {
2495 if (StorageManager.isFileEncryptedNativeOrEmulated()
2496 && !isUserKeyUnlocked(user.id)) {
2497 Slog.w(TAG, "Failing move due to locked user " + user.id);
2498 onMoveStatusLocked(PackageManager.MOVE_FAILED_LOCKED_USER);
2499 return;
2500 }
2501 }
2502
Jeff Sharkeyfced5342015-05-10 14:53:34 -07002503 // When moving to/from primary physical volume, we probably just nuked
2504 // the current storage location, so we have nothing to move.
2505 if (Objects.equals(StorageManager.UUID_PRIMARY_PHYSICAL, mPrimaryStorageUuid)
2506 || Objects.equals(StorageManager.UUID_PRIMARY_PHYSICAL, volumeUuid)) {
2507 Slog.d(TAG, "Skipping move to/from primary physical");
2508 onMoveStatusLocked(MOVE_STATUS_COPY_FINISHED);
2509 onMoveStatusLocked(PackageManager.MOVE_SUCCEEDED);
Jeff Sharkey5a9bb742015-11-03 10:15:57 -08002510 mHandler.obtainMessage(H_RESET).sendToTarget();
Jeff Sharkeya31460c2016-06-22 09:04:33 -06002511 return;
Jeff Sharkeyfced5342015-05-10 14:53:34 -07002512
2513 } else {
Jeff Sharkeya31460c2016-06-22 09:04:33 -06002514 from = findStorageForUuid(mPrimaryStorageUuid);
2515 to = findStorageForUuid(volumeUuid);
Jeff Sharkeyef10ee02015-07-05 14:17:27 -07002516
2517 if (from == null) {
2518 Slog.w(TAG, "Failing move due to missing from volume " + mPrimaryStorageUuid);
2519 onMoveStatusLocked(PackageManager.MOVE_FAILED_INTERNAL_ERROR);
2520 return;
2521 } else if (to == null) {
2522 Slog.w(TAG, "Failing move due to missing to volume " + volumeUuid);
2523 onMoveStatusLocked(PackageManager.MOVE_FAILED_INTERNAL_ERROR);
2524 return;
2525 }
Jeff Sharkey275e3e42015-04-24 16:10:32 -07002526 }
Jeff Sharkey620b32b2015-04-23 19:36:02 -07002527 }
Jeff Sharkeya31460c2016-06-22 09:04:33 -06002528
2529 try {
Jeff Sharkeyb302c542017-09-15 12:57:59 -06002530 mVold.moveStorage(from.id, to.id, new IVoldTaskListener.Stub() {
2531 @Override
2532 public void onStatus(int status, PersistableBundle extras) {
2533 synchronized (mLock) {
2534 onMoveStatusLocked(status);
2535 }
2536 }
2537
2538 @Override
2539 public void onFinished(int status, PersistableBundle extras) {
2540 // Not currently used
2541 }
2542 });
Jeff Sharkeyace874b2017-09-07 15:27:33 -06002543 } catch (Exception e) {
2544 Slog.wtf(TAG, e);
Jeff Sharkeya31460c2016-06-22 09:04:33 -06002545 }
Jeff Sharkey620b32b2015-04-23 19:36:02 -07002546 }
2547
San Mehatb1043402010-02-05 08:26:50 -08002548 private void warnOnNotMounted() {
Jeff Sharkey48877892015-03-18 11:27:19 -07002549 synchronized (mLock) {
Jeff Sharkey1b8ef7e2015-04-03 17:14:45 -07002550 for (int i = 0; i < mVolumes.size(); i++) {
2551 final VolumeInfo vol = mVolumes.valueAt(i);
Jeff Sharkey27de30d2015-04-18 16:20:27 -07002552 if (vol.isPrimary() && vol.isMountedWritable()) {
Jeff Sharkey48877892015-03-18 11:27:19 -07002553 // Cool beans, we have a mounted primary volume
2554 return;
2555 }
Jeff Sharkey32ee8312012-09-30 13:21:31 -07002556 }
San Mehatb1043402010-02-05 08:26:50 -08002557 }
Jeff Sharkey48877892015-03-18 11:27:19 -07002558
2559 Slog.w(TAG, "No primary storage mounted!");
San Mehatb1043402010-02-05 08:26:50 -08002560 }
2561
Kenny Roota02b8b02010-08-05 16:14:17 -07002562 private boolean isUidOwnerOfPackageOrSystem(String packageName, int callerUid) {
2563 if (callerUid == android.os.Process.SYSTEM_UID) {
2564 return true;
2565 }
2566
Kenny Root02c87302010-07-01 08:10:18 -07002567 if (packageName == null) {
2568 return false;
2569 }
2570
Jeff Sharkey5790af02018-08-13 17:42:54 -06002571 final int packageUid = mPmInternal.getPackageUid(packageName,
Jeff Sharkeycd654482016-01-08 17:42:11 -07002572 PackageManager.MATCH_DEBUG_TRIAGED_MISSING, UserHandle.getUserId(callerUid));
Kenny Root02c87302010-07-01 08:10:18 -07002573
2574 if (DEBUG_OBB) {
2575 Slog.d(TAG, "packageName = " + packageName + ", packageUid = " +
2576 packageUid + ", callerUid = " + callerUid);
2577 }
2578
2579 return callerUid == packageUid;
2580 }
2581
Jeff Sharkey54402792017-09-15 16:05:19 -06002582 @Override
Jeff Sharkey4fbbda42012-09-24 18:34:07 -07002583 public String getMountedObbPath(String rawPath) {
Daulet Zhanguzinea1a7ca2020-01-03 09:46:50 +00002584 Objects.requireNonNull(rawPath, "rawPath cannot be null");
Kenny Rootaf9d6672010-10-08 09:21:39 -07002585
Kenny Root02c87302010-07-01 08:10:18 -07002586 warnOnNotMounted();
2587
Jeff Sharkey4fbbda42012-09-24 18:34:07 -07002588 final ObbState state;
Rubin Xucd7a0142015-04-17 23:45:27 +01002589 synchronized (mObbMounts) {
Jeff Sharkey4fbbda42012-09-24 18:34:07 -07002590 state = mObbPathToStateMap.get(rawPath);
2591 }
2592 if (state == null) {
2593 Slog.w(TAG, "Failed to find OBB mounted at " + rawPath);
2594 return null;
2595 }
2596
Jeff Sharkey54402792017-09-15 16:05:19 -06002597 return findVolumeByIdOrThrow(state.volId).getPath().getAbsolutePath();
Kenny Root02c87302010-07-01 08:10:18 -07002598 }
2599
Jeff Sharkey4fbbda42012-09-24 18:34:07 -07002600 @Override
2601 public boolean isObbMounted(String rawPath) {
Daulet Zhanguzinea1a7ca2020-01-03 09:46:50 +00002602 Objects.requireNonNull(rawPath, "rawPath cannot be null");
Kenny Rootaf9d6672010-10-08 09:21:39 -07002603 synchronized (mObbMounts) {
Jeff Sharkey4fbbda42012-09-24 18:34:07 -07002604 return mObbPathToStateMap.containsKey(rawPath);
Kenny Rootaf9d6672010-10-08 09:21:39 -07002605 }
Kenny Root02c87302010-07-01 08:10:18 -07002606 }
2607
Jeff Sharkey4fbbda42012-09-24 18:34:07 -07002608 @Override
Sudheer Shanka25469aa2018-08-27 15:50:23 -07002609 public void mountObb(String rawPath, String canonicalPath, String key,
2610 IObbActionListener token, int nonce, ObbInfo obbInfo) {
Daulet Zhanguzinea1a7ca2020-01-03 09:46:50 +00002611 Objects.requireNonNull(rawPath, "rawPath cannot be null");
2612 Objects.requireNonNull(canonicalPath, "canonicalPath cannot be null");
2613 Objects.requireNonNull(token, "token cannot be null");
2614 Objects.requireNonNull(obbInfo, "obbIfno cannot be null");
Kenny Rootaf9d6672010-10-08 09:21:39 -07002615
Jeff Sharkey4fbbda42012-09-24 18:34:07 -07002616 final int callingUid = Binder.getCallingUid();
Jeff Sharkey41cd6812017-09-11 10:32:17 -06002617 final ObbState obbState = new ObbState(rawPath, canonicalPath,
2618 callingUid, token, nonce, null);
Sudheer Shanka25469aa2018-08-27 15:50:23 -07002619 final ObbAction action = new MountObbAction(obbState, key, callingUid, obbInfo);
Kenny Roota02b8b02010-08-05 16:14:17 -07002620 mObbActionHandler.sendMessage(mObbActionHandler.obtainMessage(OBB_RUN_ACTION, action));
2621
2622 if (DEBUG_OBB)
2623 Slog.i(TAG, "Send to OBB handler: " + action.toString());
Kenny Root02c87302010-07-01 08:10:18 -07002624 }
2625
Jeff Sharkey4fbbda42012-09-24 18:34:07 -07002626 @Override
2627 public void unmountObb(String rawPath, boolean force, IObbActionListener token, int nonce) {
Daulet Zhanguzinea1a7ca2020-01-03 09:46:50 +00002628 Objects.requireNonNull(rawPath, "rawPath cannot be null");
Jeff Sharkey4fbbda42012-09-24 18:34:07 -07002629
2630 final ObbState existingState;
Rubin Xucd7a0142015-04-17 23:45:27 +01002631 synchronized (mObbMounts) {
Jeff Sharkey4fbbda42012-09-24 18:34:07 -07002632 existingState = mObbPathToStateMap.get(rawPath);
Kenny Rootf1121dc2010-09-29 07:30:53 -07002633 }
2634
Jeff Sharkey4fbbda42012-09-24 18:34:07 -07002635 if (existingState != null) {
2636 // TODO: separate state object from request data
2637 final int callingUid = Binder.getCallingUid();
Jeff Sharkey41cd6812017-09-11 10:32:17 -06002638 final ObbState newState = new ObbState(rawPath, existingState.canonicalPath,
2639 callingUid, token, nonce, existingState.volId);
Jeff Sharkey4fbbda42012-09-24 18:34:07 -07002640 final ObbAction action = new UnmountObbAction(newState, force);
2641 mObbActionHandler.sendMessage(mObbActionHandler.obtainMessage(OBB_RUN_ACTION, action));
Kenny Root02c87302010-07-01 08:10:18 -07002642
Jeff Sharkey4fbbda42012-09-24 18:34:07 -07002643 if (DEBUG_OBB)
2644 Slog.i(TAG, "Send to OBB handler: " + action.toString());
2645 } else {
2646 Slog.w(TAG, "Unknown OBB mount at " + rawPath);
2647 }
Kenny Roota02b8b02010-08-05 16:14:17 -07002648 }
2649
Ben Komalo444eca22011-09-01 15:17:44 -07002650 @Override
2651 public int getEncryptionState() {
2652 mContext.enforceCallingOrSelfPermission(Manifest.permission.CRYPT_KEEPER,
2653 "no permission to access the crypt keeper");
2654
Ben Komalo444eca22011-09-01 15:17:44 -07002655 try {
Jeff Sharkey54402792017-09-15 16:05:19 -06002656 return mVold.fdeComplete();
2657 } catch (Exception e) {
2658 Slog.wtf(TAG, e);
Sudheer Shankaf7341142016-10-18 17:15:18 -07002659 return StorageManager.ENCRYPTION_STATE_ERROR_UNKNOWN;
Ben Komalo444eca22011-09-01 15:17:44 -07002660 }
2661 }
2662
2663 @Override
Jason parks5af0b912010-11-29 09:05:25 -06002664 public int decryptStorage(String password) {
Jason parks8888c592011-01-20 22:46:41 -06002665 mContext.enforceCallingOrSelfPermission(Manifest.permission.CRYPT_KEEPER,
2666 "no permission to access the crypt keeper");
Jason parks5af0b912010-11-29 09:05:25 -06002667
Jeff Sharkey8058fe62017-09-13 11:50:33 -06002668 if (TextUtils.isEmpty(password)) {
2669 throw new IllegalArgumentException("password cannot be empty");
2670 }
2671
Jason parks5af0b912010-11-29 09:05:25 -06002672 if (DEBUG_EVENTS) {
2673 Slog.i(TAG, "decrypting storage...");
2674 }
2675
2676 try {
Jeff Sharkey54402792017-09-15 16:05:19 -06002677 mVold.fdeCheckPassword(password);
2678 mHandler.postDelayed(() -> {
2679 try {
2680 mVold.fdeRestart();
2681 } catch (Exception e) {
2682 Slog.wtf(TAG, e);
2683 }
2684 }, DateUtils.SECOND_IN_MILLIS);
2685 return 0;
Paul Crowleyfc0b5192018-07-02 13:58:10 -07002686 } catch (ServiceSpecificException e) {
2687 Slog.e(TAG, "fdeCheckPassword failed", e);
2688 return e.errorCode;
Jeff Sharkey54402792017-09-15 16:05:19 -06002689 } catch (Exception e) {
2690 Slog.wtf(TAG, e);
2691 return StorageManager.ENCRYPTION_STATE_ERROR_UNKNOWN;
Jason parks5af0b912010-11-29 09:05:25 -06002692 }
Jason parks5af0b912010-11-29 09:05:25 -06002693 }
2694
Jeff Sharkey54402792017-09-15 16:05:19 -06002695 @Override
Paul Lawrence46791e72014-04-03 09:10:26 -07002696 public int encryptStorage(int type, String password) {
Jason parks8888c592011-01-20 22:46:41 -06002697 mContext.enforceCallingOrSelfPermission(Manifest.permission.CRYPT_KEEPER,
2698 "no permission to access the crypt keeper");
Jason parks56aa5322011-01-07 09:01:15 -06002699
Jeff Sharkey8058fe62017-09-13 11:50:33 -06002700 if (type == StorageManager.CRYPT_TYPE_DEFAULT) {
2701 password = "";
2702 } else if (TextUtils.isEmpty(password)) {
2703 throw new IllegalArgumentException("password cannot be empty");
2704 }
2705
Jason parks56aa5322011-01-07 09:01:15 -06002706 if (DEBUG_EVENTS) {
Jason parks8888c592011-01-20 22:46:41 -06002707 Slog.i(TAG, "encrypting storage...");
Jason parks56aa5322011-01-07 09:01:15 -06002708 }
2709
2710 try {
Paul Lawrence89a0ae42017-12-22 10:11:10 -08002711 mVold.fdeEnable(type, password, 0);
Jeff Sharkey43e12112017-09-12 16:31:45 -06002712 } catch (Exception e) {
2713 Slog.wtf(TAG, e);
2714 return -1;
Jason parks56aa5322011-01-07 09:01:15 -06002715 }
2716
2717 return 0;
2718 }
2719
Paul Lawrence8e397362014-01-27 15:22:30 -08002720 /** Set the password for encrypting the master key.
2721 * @param type One of the CRYPTO_TYPE_XXX consts defined in StorageManager.
2722 * @param password The password to set.
2723 */
Jeff Sharkey54402792017-09-15 16:05:19 -06002724 @Override
Paul Lawrence8e397362014-01-27 15:22:30 -08002725 public int changeEncryptionPassword(int type, String password) {
Jason parksf7b3cd42011-01-27 09:28:25 -06002726 mContext.enforceCallingOrSelfPermission(Manifest.permission.CRYPT_KEEPER,
2727 "no permission to access the crypt keeper");
2728
Jeff Sharkeyae266462017-11-27 13:32:24 -07002729 if (StorageManager.isFileEncryptedNativeOnly()) {
2730 // Not supported on FBE devices
2731 return -1;
2732 }
2733
Jeff Sharkey8058fe62017-09-13 11:50:33 -06002734 if (type == StorageManager.CRYPT_TYPE_DEFAULT) {
2735 password = "";
2736 } else if (TextUtils.isEmpty(password)) {
2737 throw new IllegalArgumentException("password cannot be empty");
2738 }
2739
Jason parksf7b3cd42011-01-27 09:28:25 -06002740 if (DEBUG_EVENTS) {
2741 Slog.i(TAG, "changing encryption password...");
2742 }
2743
2744 try {
Jeff Sharkey54402792017-09-15 16:05:19 -06002745 mVold.fdeChangePassword(type, password);
2746 return 0;
2747 } catch (Exception e) {
2748 Slog.wtf(TAG, e);
2749 return -1;
Jason parksf7b3cd42011-01-27 09:28:25 -06002750 }
2751 }
2752
Christopher Tate32418be2011-10-10 13:51:12 -07002753 /**
2754 * Validate a user-supplied password string with cryptfs
2755 */
2756 @Override
2757 public int verifyEncryptionPassword(String password) throws RemoteException {
2758 // Only the system process is permitted to validate passwords
2759 if (Binder.getCallingUid() != android.os.Process.SYSTEM_UID) {
2760 throw new SecurityException("no permission to access the crypt keeper");
2761 }
2762
2763 mContext.enforceCallingOrSelfPermission(Manifest.permission.CRYPT_KEEPER,
2764 "no permission to access the crypt keeper");
2765
2766 if (TextUtils.isEmpty(password)) {
2767 throw new IllegalArgumentException("password cannot be empty");
2768 }
2769
Christopher Tate32418be2011-10-10 13:51:12 -07002770 if (DEBUG_EVENTS) {
2771 Slog.i(TAG, "validating encryption password...");
2772 }
2773
2774 try {
Jeff Sharkey54402792017-09-15 16:05:19 -06002775 mVold.fdeVerifyPassword(password);
2776 return 0;
2777 } catch (Exception e) {
2778 Slog.wtf(TAG, e);
2779 return -1;
Christopher Tate32418be2011-10-10 13:51:12 -07002780 }
2781 }
2782
Paul Lawrence8e397362014-01-27 15:22:30 -08002783 /**
2784 * Get the type of encryption used to encrypt the master key.
2785 * @return The type, one of the CRYPT_TYPE_XXX consts from StorageManager.
2786 */
2787 @Override
Svetoslav16e4a1a2014-09-29 18:16:20 -07002788 public int getPasswordType() {
Paul Lawrence76a40572017-03-15 11:08:04 -07002789 mContext.enforceCallingOrSelfPermission(Manifest.permission.CRYPT_KEEPER,
Paul Lawrence9de713d2016-05-02 22:45:33 +00002790 "no permission to access the crypt keeper");
2791
Paul Lawrence8e397362014-01-27 15:22:30 -08002792 try {
Jeff Sharkey54402792017-09-15 16:05:19 -06002793 return mVold.fdeGetPasswordType();
2794 } catch (Exception e) {
2795 Slog.wtf(TAG, e);
2796 return -1;
Paul Lawrence8e397362014-01-27 15:22:30 -08002797 }
2798 }
2799
Paul Lawrencee51dcf92014-03-18 10:56:00 -07002800 /**
2801 * Set a field in the crypto header.
2802 * @param field field to set
2803 * @param contents contents to set in field
2804 */
2805 @Override
2806 public void setField(String field, String contents) throws RemoteException {
Paul Lawrence76a40572017-03-15 11:08:04 -07002807 mContext.enforceCallingOrSelfPermission(Manifest.permission.CRYPT_KEEPER,
Paul Lawrence0bbd1082016-04-26 15:21:02 -07002808 "no permission to access the crypt keeper");
Paul Lawrencee51dcf92014-03-18 10:56:00 -07002809
yusukes14a8b1f2018-07-23 17:34:42 -07002810 if (!StorageManager.isBlockEncrypted()) {
2811 // Only supported on FDE devices
Jeff Sharkeyae266462017-11-27 13:32:24 -07002812 return;
2813 }
2814
Paul Lawrencee51dcf92014-03-18 10:56:00 -07002815 try {
Jeff Sharkey54402792017-09-15 16:05:19 -06002816 mVold.fdeSetField(field, contents);
2817 return;
2818 } catch (Exception e) {
2819 Slog.wtf(TAG, e);
2820 return;
Paul Lawrencee51dcf92014-03-18 10:56:00 -07002821 }
2822 }
2823
2824 /**
2825 * Gets a field from the crypto header.
2826 * @param field field to get
2827 * @return contents of field
2828 */
2829 @Override
2830 public String getField(String field) throws RemoteException {
Paul Lawrence76a40572017-03-15 11:08:04 -07002831 mContext.enforceCallingOrSelfPermission(Manifest.permission.CRYPT_KEEPER,
Paul Lawrence0bbd1082016-04-26 15:21:02 -07002832 "no permission to access the crypt keeper");
Paul Lawrencee51dcf92014-03-18 10:56:00 -07002833
yusukes14a8b1f2018-07-23 17:34:42 -07002834 if (!StorageManager.isBlockEncrypted()) {
2835 // Only supported on FDE devices
Jeff Sharkeyae266462017-11-27 13:32:24 -07002836 return null;
2837 }
2838
Paul Lawrencee51dcf92014-03-18 10:56:00 -07002839 try {
Jeff Sharkey54402792017-09-15 16:05:19 -06002840 return mVold.fdeGetField(field);
2841 } catch (Exception e) {
2842 Slog.wtf(TAG, e);
2843 return null;
Paul Lawrencee51dcf92014-03-18 10:56:00 -07002844 }
2845 }
2846
Paul Lawrence3806d9c2015-10-29 10:30:46 -07002847 /**
2848 * Is userdata convertible to file based encryption?
2849 * @return non zero for convertible
2850 */
2851 @Override
2852 public boolean isConvertibleToFBE() throws RemoteException {
Paul Lawrence76a40572017-03-15 11:08:04 -07002853 mContext.enforceCallingOrSelfPermission(Manifest.permission.CRYPT_KEEPER,
Paul Lawrence0bbd1082016-04-26 15:21:02 -07002854 "no permission to access the crypt keeper");
Paul Lawrence3806d9c2015-10-29 10:30:46 -07002855
Paul Lawrence3806d9c2015-10-29 10:30:46 -07002856 try {
Jeff Sharkey54402792017-09-15 16:05:19 -06002857 return mVold.isConvertibleToFbe();
2858 } catch (Exception e) {
2859 Slog.wtf(TAG, e);
2860 return false;
Paul Lawrence3806d9c2015-10-29 10:30:46 -07002861 }
2862 }
2863
Daniel Rosenberge129e5c2018-11-07 19:25:55 -08002864 /**
Martijn Coenenf04aca42019-03-11 10:35:34 +01002865 * Check whether the device supports filesystem checkpointing.
2866 *
2867 * @return true if the device supports filesystem checkpointing, false otherwise.
2868 */
2869 @Override
2870 public boolean supportsCheckpoint() throws RemoteException {
Martijn Coenenf04aca42019-03-11 10:35:34 +01002871 return mVold.supportsCheckpoint();
2872 }
2873
2874 /**
2875 * Signal that checkpointing partitions should start a checkpoint on the next boot.
2876 *
2877 * @param numTries Number of times to try booting in checkpoint mode, before we will boot
2878 * non-checkpoint mode and commit all changes immediately. Callers are
2879 * responsible for ensuring that boot is safe (eg, by rolling back updates).
2880 */
2881 @Override
2882 public void startCheckpoint(int numTries) throws RemoteException {
Nikita Ioffe6d749422020-01-14 22:19:01 +00002883 // Only the root, system_server and shell processes are permitted to start checkpoints
2884 final int callingUid = Binder.getCallingUid();
2885 if (callingUid != Process.SYSTEM_UID && callingUid != Process.ROOT_UID
2886 && callingUid != Process.SHELL_UID) {
Martijn Coenenf04aca42019-03-11 10:35:34 +01002887 throw new SecurityException("no permission to start filesystem checkpoint");
2888 }
2889
2890 mVold.startCheckpoint(numTries);
2891 }
2892
2893 /**
Daniel Rosenberge129e5c2018-11-07 19:25:55 -08002894 * Signal that checkpointing partitions should commit changes
2895 */
2896 @Override
2897 public void commitChanges() throws RemoteException {
2898 // Only the system process is permitted to commit checkpoints
2899 if (Binder.getCallingUid() != android.os.Process.SYSTEM_UID) {
2900 throw new SecurityException("no permission to commit checkpoint changes");
2901 }
2902
2903 mVold.commitChanges();
2904 }
2905
Daniel Rosenberge3a924d2019-03-19 18:19:08 -07002906 /**
2907 * Check if we should be mounting with checkpointing or are checkpointing now
2908 */
2909 @Override
2910 public boolean needsCheckpoint() throws RemoteException {
Paul Lawrence15a54462019-04-30 11:14:25 -07002911 enforcePermission(android.Manifest.permission.MOUNT_FORMAT_FILESYSTEMS);
Daniel Rosenberge3a924d2019-03-19 18:19:08 -07002912 return mVold.needsCheckpoint();
2913 }
2914
Daniel Rosenberg600799b2019-03-20 17:42:50 -07002915 /**
2916 * Abort the current set of changes and either try again, or abort entirely
2917 */
2918 @Override
2919 public void abortChanges(String message, boolean retry) throws RemoteException {
2920 // Only the system process is permitted to abort checkpoints
2921 if (Binder.getCallingUid() != android.os.Process.SYSTEM_UID) {
2922 throw new SecurityException("no permission to commit checkpoint changes");
2923 }
2924
2925 mVold.abortChanges(message, retry);
2926 }
2927
Jeff Sharkeyb049e212012-09-07 23:16:01 -07002928 @Override
Paul Lawrence945490c2014-03-27 16:37:28 +00002929 public String getPassword() throws RemoteException {
Paul Lawrence76a40572017-03-15 11:08:04 -07002930 mContext.enforceCallingOrSelfPermission(Manifest.permission.CRYPT_KEEPER,
Rubin Xucd7a0142015-04-17 23:45:27 +01002931 "only keyguard can retrieve password");
Paul Lawrence0bbd1082016-04-26 15:21:02 -07002932
Paul Lawrence945490c2014-03-27 16:37:28 +00002933 try {
Jeff Sharkey54402792017-09-15 16:05:19 -06002934 return mVold.fdeGetPassword();
2935 } catch (Exception e) {
2936 Slog.wtf(TAG, e);
Paul Lawrence24063b52015-01-06 13:11:23 -08002937 return null;
Paul Lawrence945490c2014-03-27 16:37:28 +00002938 }
2939 }
2940
2941 @Override
2942 public void clearPassword() throws RemoteException {
Paul Lawrence76a40572017-03-15 11:08:04 -07002943 mContext.enforceCallingOrSelfPermission(Manifest.permission.CRYPT_KEEPER,
Paul Lawrence0bbd1082016-04-26 15:21:02 -07002944 "only keyguard can clear password");
2945
Paul Lawrence945490c2014-03-27 16:37:28 +00002946 try {
Jeff Sharkey54402792017-09-15 16:05:19 -06002947 mVold.fdeClearPassword();
2948 return;
2949 } catch (Exception e) {
2950 Slog.wtf(TAG, e);
2951 return;
Paul Lawrence945490c2014-03-27 16:37:28 +00002952 }
2953 }
2954
2955 @Override
Lenka Trochtovac4dd0212015-11-18 12:22:06 +01002956 public void createUserKey(int userId, int serialNumber, boolean ephemeral) {
Jeff Sharkeyf9fc6d62015-11-08 16:46:05 -08002957 enforcePermission(android.Manifest.permission.STORAGE_INTERNAL);
Paul Crowleybcf48ed2015-04-22 13:36:59 +01002958
Paul Crowleybcf48ed2015-04-22 13:36:59 +01002959 try {
Jeff Sharkey54402792017-09-15 16:05:19 -06002960 mVold.createUserKey(userId, serialNumber, ephemeral);
Jeff Sharkey43e12112017-09-12 16:31:45 -06002961 } catch (Exception e) {
2962 Slog.wtf(TAG, e);
Paul Crowleybcf48ed2015-04-22 13:36:59 +01002963 }
2964 }
2965
Paul Crowley7ec733f2015-05-19 12:42:00 +01002966 @Override
Jeff Sharkeyf9fc6d62015-11-08 16:46:05 -08002967 public void destroyUserKey(int userId) {
2968 enforcePermission(android.Manifest.permission.STORAGE_INTERNAL);
Paul Crowley7ec733f2015-05-19 12:42:00 +01002969
Jeff Sharkeyf9fc6d62015-11-08 16:46:05 -08002970 try {
Jeff Sharkey54402792017-09-15 16:05:19 -06002971 mVold.destroyUserKey(userId);
Jeff Sharkey43e12112017-09-12 16:31:45 -06002972 } catch (Exception e) {
2973 Slog.wtf(TAG, e);
Jeff Sharkeyf9fc6d62015-11-08 16:46:05 -08002974 }
2975 }
2976
Jeff Sharkey43e12112017-09-12 16:31:45 -06002977 private String encodeBytes(byte[] bytes) {
Paul Crowleyfaeb3eb2016-02-08 15:58:29 +00002978 if (ArrayUtils.isEmpty(bytes)) {
Jeff Sharkey43e12112017-09-12 16:31:45 -06002979 return "!";
Paul Crowleyfaeb3eb2016-02-08 15:58:29 +00002980 } else {
Jeff Sharkey43e12112017-09-12 16:31:45 -06002981 return HexDump.toHexString(bytes);
Paul Crowleyfaeb3eb2016-02-08 15:58:29 +00002982 }
2983 }
2984
Paul Crowleycc701552016-05-17 14:18:49 -07002985 /*
2986 * Add this token/secret pair to the set of ways we can recover a disk encryption key.
2987 * Changing the token/secret for a disk encryption key is done in two phases: first, adding
2988 * a new token/secret pair with this call, then delting all other pairs with
2989 * fixateNewestUserKeyAuth. This allows other places where a credential is used, such as
2990 * Gatekeeper, to be updated between the two calls.
2991 */
Jeff Sharkeyf9fc6d62015-11-08 16:46:05 -08002992 @Override
Paul Crowleycc701552016-05-17 14:18:49 -07002993 public void addUserKeyAuth(int userId, int serialNumber, byte[] token, byte[] secret) {
Paul Crowleyfaeb3eb2016-02-08 15:58:29 +00002994 enforcePermission(android.Manifest.permission.STORAGE_INTERNAL);
Paul Crowleyfaeb3eb2016-02-08 15:58:29 +00002995
2996 try {
Jeff Sharkey54402792017-09-15 16:05:19 -06002997 mVold.addUserKeyAuth(userId, serialNumber, encodeBytes(token), encodeBytes(secret));
Jeff Sharkey43e12112017-09-12 16:31:45 -06002998 } catch (Exception e) {
2999 Slog.wtf(TAG, e);
Paul Crowleycc701552016-05-17 14:18:49 -07003000 }
3001 }
3002
3003 /*
Barani Muthukumaran9ba99ed2019-12-18 00:36:06 -08003004 * Clear disk encryption key bound to the associated token / secret pair. Removing the user
3005 * binding of the Disk encryption key is done in two phases: first, this call will retrieve
3006 * the disk encryption key using the provided token / secret pair and store it by
3007 * encrypting it with a keymaster key not bound to the user, then fixateNewestUserKeyAuth
3008 * is called to delete all other bindings of the disk encryption key.
3009 */
3010 @Override
3011 public void clearUserKeyAuth(int userId, int serialNumber, byte[] token, byte[] secret) {
3012 enforcePermission(android.Manifest.permission.STORAGE_INTERNAL);
3013
3014 try {
3015 mVold.clearUserKeyAuth(userId, serialNumber, encodeBytes(token), encodeBytes(secret));
3016 } catch (Exception e) {
3017 Slog.wtf(TAG, e);
3018 }
3019 }
3020
3021 /*
Paul Crowleycc701552016-05-17 14:18:49 -07003022 * Delete all disk encryption token/secret pairs except the most recently added one
3023 */
3024 @Override
3025 public void fixateNewestUserKeyAuth(int userId) {
3026 enforcePermission(android.Manifest.permission.STORAGE_INTERNAL);
Paul Crowleycc701552016-05-17 14:18:49 -07003027
3028 try {
Jeff Sharkey54402792017-09-15 16:05:19 -06003029 mVold.fixateNewestUserKeyAuth(userId);
Jeff Sharkey43e12112017-09-12 16:31:45 -06003030 } catch (Exception e) {
3031 Slog.wtf(TAG, e);
Paul Crowleyfaeb3eb2016-02-08 15:58:29 +00003032 }
3033 }
3034
3035 @Override
3036 public void unlockUserKey(int userId, int serialNumber, byte[] token, byte[] secret) {
Sudheer Shanka03b20ec2019-02-21 15:11:00 -08003037 Slog.d(TAG, "unlockUserKey: " + userId);
Jeff Sharkeyf9fc6d62015-11-08 16:46:05 -08003038 enforcePermission(android.Manifest.permission.STORAGE_INTERNAL);
Jeff Sharkeyf9fc6d62015-11-08 16:46:05 -08003039
Jeff Sharkeyce18c812016-04-27 16:00:41 -06003040 if (StorageManager.isFileEncryptedNativeOrEmulated()) {
Jeff Sharkeyce18c812016-04-27 16:00:41 -06003041 try {
Jeff Sharkey54402792017-09-15 16:05:19 -06003042 mVold.unlockUserKey(userId, serialNumber, encodeBytes(token),
3043 encodeBytes(secret));
Jeff Sharkey43e12112017-09-12 16:31:45 -06003044 } catch (Exception e) {
3045 Slog.wtf(TAG, e);
3046 return;
Jeff Sharkeyce18c812016-04-27 16:00:41 -06003047 }
Jeff Sharkeyf9fc6d62015-11-08 16:46:05 -08003048 }
3049
3050 synchronized (mLock) {
Jeff Sharkeyce14cd02015-12-07 15:35:42 -07003051 mLocalUnlockedUsers = ArrayUtils.appendInt(mLocalUnlockedUsers, userId);
Jeff Sharkeyf9fc6d62015-11-08 16:46:05 -08003052 }
3053 }
3054
3055 @Override
3056 public void lockUserKey(int userId) {
3057 enforcePermission(android.Manifest.permission.STORAGE_INTERNAL);
Jeff Sharkeyf9fc6d62015-11-08 16:46:05 -08003058
3059 try {
Jeff Sharkey54402792017-09-15 16:05:19 -06003060 mVold.lockUserKey(userId);
Jeff Sharkey43e12112017-09-12 16:31:45 -06003061 } catch (Exception e) {
3062 Slog.wtf(TAG, e);
3063 return;
Jeff Sharkeyf9fc6d62015-11-08 16:46:05 -08003064 }
3065
3066 synchronized (mLock) {
Jeff Sharkeyce14cd02015-12-07 15:35:42 -07003067 mLocalUnlockedUsers = ArrayUtils.removeInt(mLocalUnlockedUsers, userId);
Jeff Sharkeyf9fc6d62015-11-08 16:46:05 -08003068 }
3069 }
3070
3071 @Override
3072 public boolean isUserKeyUnlocked(int userId) {
Jeff Sharkeyce18c812016-04-27 16:00:41 -06003073 synchronized (mLock) {
3074 return ArrayUtils.contains(mLocalUnlockedUsers, userId);
Jeff Sharkeyf9fc6d62015-11-08 16:46:05 -08003075 }
3076 }
3077
Narayan Kamath157dd1d2019-06-12 13:06:30 +01003078 private boolean isSystemUnlocked(int userId) {
3079 synchronized (mLock) {
3080 return ArrayUtils.contains(mSystemUnlockedUsers, userId);
3081 }
3082 }
3083
Jeff Sharkeyf9fc6d62015-11-08 16:46:05 -08003084 @Override
Jeff Sharkey47f71082016-02-01 17:03:54 -07003085 public void prepareUserStorage(String volumeUuid, int userId, int serialNumber, int flags) {
Jeff Sharkeyf9fc6d62015-11-08 16:46:05 -08003086 enforcePermission(android.Manifest.permission.STORAGE_INTERNAL);
Jeff Sharkeyf9fc6d62015-11-08 16:46:05 -08003087
3088 try {
Jeff Sharkey54402792017-09-15 16:05:19 -06003089 mVold.prepareUserStorage(volumeUuid, userId, serialNumber, flags);
Jeff Sharkey43e12112017-09-12 16:31:45 -06003090 } catch (Exception e) {
3091 Slog.wtf(TAG, e);
Paul Crowley7ec733f2015-05-19 12:42:00 +01003092 }
3093 }
3094
Paul Crowleybcf48ed2015-04-22 13:36:59 +01003095 @Override
Jeff Sharkeyfcf1e552016-04-14 20:44:58 -06003096 public void destroyUserStorage(String volumeUuid, int userId, int flags) {
3097 enforcePermission(android.Manifest.permission.STORAGE_INTERNAL);
Jeff Sharkeyfcf1e552016-04-14 20:44:58 -06003098
3099 try {
Jeff Sharkey54402792017-09-15 16:05:19 -06003100 mVold.destroyUserStorage(volumeUuid, userId, flags);
Jeff Sharkey43e12112017-09-12 16:31:45 -06003101 } catch (Exception e) {
3102 Slog.wtf(TAG, e);
Jeff Sharkeyfcf1e552016-04-14 20:44:58 -06003103 }
3104 }
3105
Anton Hanssonbe9c9902019-05-28 13:45:26 +01003106 /** Not thread safe */
Daichi Hironoe56740d2017-02-02 13:56:45 +09003107 class AppFuseMountScope extends AppFuseBridge.MountScope {
Anton Hanssonbe9c9902019-05-28 13:45:26 +01003108 private boolean mMounted = false;
Daichi Hirono812c95d2017-02-08 16:20:20 +09003109
Ryo Hashimoto38bcbf02018-05-16 18:20:37 +09003110 public AppFuseMountScope(int uid, int mountId) {
3111 super(uid, mountId);
Daichi Hirono812c95d2017-02-08 16:20:20 +09003112 }
3113
3114 @Override
3115 public ParcelFileDescriptor open() throws NativeDaemonConnectorException {
Jeff Sharkey54402792017-09-15 16:05:19 -06003116 try {
Anton Hanssonbe9c9902019-05-28 13:45:26 +01003117 final FileDescriptor fd = mVold.mountAppFuse(uid, mountId);
3118 mMounted = true;
3119 return new ParcelFileDescriptor(fd);
Jeff Sharkey54402792017-09-15 16:05:19 -06003120 } catch (Exception e) {
3121 throw new NativeDaemonConnectorException("Failed to mount", e);
Daichi Hirono812c95d2017-02-08 16:20:20 +09003122 }
Daichi Hirono9fb00182016-11-08 14:12:17 +09003123 }
3124
3125 @Override
Ryo Hashimoto38bcbf02018-05-16 18:20:37 +09003126 public ParcelFileDescriptor openFile(int mountId, int fileId, int flags)
3127 throws NativeDaemonConnectorException {
3128 try {
3129 return new ParcelFileDescriptor(
3130 mVold.openAppFuseFile(uid, mountId, fileId, flags));
3131 } catch (Exception e) {
3132 throw new NativeDaemonConnectorException("Failed to open", e);
3133 }
3134 }
3135
3136 @Override
Daichi Hironoe56740d2017-02-02 13:56:45 +09003137 public void close() throws Exception {
Anton Hanssonbe9c9902019-05-28 13:45:26 +01003138 if (mMounted) {
Ryo Hashimoto38bcbf02018-05-16 18:20:37 +09003139 mVold.unmountAppFuse(uid, mountId);
Anton Hanssonbe9c9902019-05-28 13:45:26 +01003140 mMounted = false;
Daichi Hirono812c95d2017-02-08 16:20:20 +09003141 }
Daichi Hirono9fb00182016-11-08 14:12:17 +09003142 }
3143 }
3144
3145 @Override
Daichi Hirono812c95d2017-02-08 16:20:20 +09003146 public @Nullable AppFuseMount mountProxyFileDescriptorBridge() {
Daichi Hironoe56740d2017-02-02 13:56:45 +09003147 Slog.v(TAG, "mountProxyFileDescriptorBridge");
Daichi Hirono9fb00182016-11-08 14:12:17 +09003148 final int uid = Binder.getCallingUid();
Daichi Hirono9fb00182016-11-08 14:12:17 +09003149
Daichi Hironoe56740d2017-02-02 13:56:45 +09003150 while (true) {
3151 synchronized (mAppFuseLock) {
3152 boolean newlyCreated = false;
3153 if (mAppFuseBridge == null) {
3154 mAppFuseBridge = new AppFuseBridge();
3155 new Thread(mAppFuseBridge, AppFuseBridge.TAG).start();
3156 newlyCreated = true;
Daichi Hirono9fb00182016-11-08 14:12:17 +09003157 }
Daichi Hironoe56740d2017-02-02 13:56:45 +09003158 try {
3159 final int name = mNextAppFuseName++;
3160 try {
3161 return new AppFuseMount(
Ryo Hashimoto38bcbf02018-05-16 18:20:37 +09003162 name, mAppFuseBridge.addBridge(new AppFuseMountScope(uid, name)));
Daichi Hirono812c95d2017-02-08 16:20:20 +09003163 } catch (FuseUnavailableMountException e) {
Daichi Hironoe56740d2017-02-02 13:56:45 +09003164 if (newlyCreated) {
3165 // If newly created bridge fails, it's a real error.
Daichi Hirono812c95d2017-02-08 16:20:20 +09003166 Slog.e(TAG, "", e);
3167 return null;
Daichi Hironoe56740d2017-02-02 13:56:45 +09003168 }
3169 // It seems the thread of mAppFuseBridge has already been terminated.
3170 mAppFuseBridge = null;
3171 }
3172 } catch (NativeDaemonConnectorException e) {
3173 throw e.rethrowAsParcelableException();
3174 }
Daichi Hirono9fb00182016-11-08 14:12:17 +09003175 }
Daichi Hirono9fb00182016-11-08 14:12:17 +09003176 }
3177 }
3178
3179 @Override
Daichi Hirono812c95d2017-02-08 16:20:20 +09003180 public @Nullable ParcelFileDescriptor openProxyFileDescriptor(
3181 int mountId, int fileId, int mode) {
3182 Slog.v(TAG, "mountProxyFileDescriptor");
Jeff Sharkey39466322018-12-05 19:19:52 -07003183
3184 // We only support a narrow set of incoming mode flags
Jeff Sharkey3b1c2542018-12-13 15:01:38 -07003185 mode &= MODE_READ_WRITE;
Jeff Sharkey39466322018-12-05 19:19:52 -07003186
Daichi Hirono9fb00182016-11-08 14:12:17 +09003187 try {
3188 synchronized (mAppFuseLock) {
Daichi Hironoe56740d2017-02-02 13:56:45 +09003189 if (mAppFuseBridge == null) {
Daichi Hirono812c95d2017-02-08 16:20:20 +09003190 Slog.e(TAG, "FuseBridge has not been created");
3191 return null;
Daichi Hirono9fb00182016-11-08 14:12:17 +09003192 }
Ryo Hashimoto38bcbf02018-05-16 18:20:37 +09003193 return mAppFuseBridge.openFile(mountId, fileId, mode);
Daichi Hirono9fb00182016-11-08 14:12:17 +09003194 }
Daichi Hirono812c95d2017-02-08 16:20:20 +09003195 } catch (FuseUnavailableMountException | InterruptedException error) {
3196 Slog.v(TAG, "The mount point has already been invalid", error);
3197 return null;
Daichi Hirono9fb00182016-11-08 14:12:17 +09003198 }
3199 }
3200
Daichi Hirono9e8d9e22015-11-13 14:37:00 +09003201 @Override
Jeff Sharkeyae266462017-11-27 13:32:24 -07003202 public void mkdirs(String callingPkg, String appPath) {
Sudheer Shankaa63bfb32019-02-28 11:15:58 -08003203 final int callingUid = Binder.getCallingUid();
3204 final int userId = UserHandle.getUserId(callingUid);
Farid Zare Seisanac094512018-04-02 15:06:13 -07003205 final String propertyName = "sys.user." + userId + ".ce_available";
Jeff Sharkey2d8b4e82013-09-17 17:30:33 -07003206
Jeff Sharkey196c7552018-03-26 15:56:50 -06003207 // Ignore requests to create directories while storage is locked
Farid Zare Seisanac094512018-04-02 15:06:13 -07003208 if (!isUserKeyUnlocked(userId)) {
3209 throw new IllegalStateException("Failed to prepare " + appPath);
3210 }
3211
3212 // Ignore requests to create directories if CE storage is not available
Farid Zare Seisan7f6b8122018-03-30 11:19:10 -07003213 if ((userId == UserHandle.USER_SYSTEM)
3214 && !SystemProperties.getBoolean(propertyName, false)) {
Farid Zare Seisanac094512018-04-02 15:06:13 -07003215 throw new IllegalStateException("Failed to prepare " + appPath);
3216 }
Jeff Sharkey196c7552018-03-26 15:56:50 -06003217
Jeff Sharkey2d8b4e82013-09-17 17:30:33 -07003218 // Validate that reported package name belongs to caller
3219 final AppOpsManager appOps = (AppOpsManager) mContext.getSystemService(
3220 Context.APP_OPS_SERVICE);
Sudheer Shankaa63bfb32019-02-28 11:15:58 -08003221 appOps.checkPackage(callingUid, callingPkg);
Jeff Sharkey2d8b4e82013-09-17 17:30:33 -07003222
Jeff Sharkey48877892015-03-18 11:27:19 -07003223 File appFile = null;
Jeff Sharkey2d8b4e82013-09-17 17:30:33 -07003224 try {
Jeff Sharkey48877892015-03-18 11:27:19 -07003225 appFile = new File(appPath).getCanonicalFile();
Jeff Sharkey2d8b4e82013-09-17 17:30:33 -07003226 } catch (IOException e) {
Jeff Sharkeyae266462017-11-27 13:32:24 -07003227 throw new IllegalStateException("Failed to resolve " + appPath + ": " + e);
Jeff Sharkey2d8b4e82013-09-17 17:30:33 -07003228 }
3229
Martijn Coenen9fd2b642019-12-24 13:04:36 +01003230 appPath = appFile.getAbsolutePath();
3231 if (!appPath.endsWith("/")) {
3232 appPath = appPath + "/";
3233 }
3234 // Ensure that the path we're asked to create is a known application directory
3235 // path.
3236 final Matcher matcher = KNOWN_APP_DIR_PATHS.matcher(appPath);
3237 if (matcher.matches()) {
3238 // And that the package dir matches the calling package
3239 if (!matcher.group(3).equals(callingPkg)) {
3240 throw new SecurityException("Invalid mkdirs path: " + appFile
3241 + " does not contain calling package " + callingPkg);
Jeff Sharkey48877892015-03-18 11:27:19 -07003242 }
Martijn Coenen9fd2b642019-12-24 13:04:36 +01003243 // And that the user id part of the path (if any) matches the calling user id,
3244 // or if for a public volume (no user id), the user matches the current user
3245 if ((matcher.group(2) != null && !matcher.group(2).equals(Integer.toString(userId)))
3246 || (matcher.group(2) == null && userId != mCurrentUserId)) {
3247 throw new SecurityException("Invalid mkdirs path: " + appFile
3248 + " does not match calling user id " + userId);
3249 }
Jeff Sharkey2d8b4e82013-09-17 17:30:33 -07003250 try {
Martijn Coenen9fd2b642019-12-24 13:04:36 +01003251 mVold.setupAppDir(appPath, matcher.group(1), callingUid);
3252 } catch (RemoteException e) {
Sudheer Shanka64501e52019-04-29 10:46:26 -07003253 throw new IllegalStateException("Failed to prepare " + appPath + ": " + e);
Jeff Sharkey2d8b4e82013-09-17 17:30:33 -07003254 }
Jeff Sharkey2d8b4e82013-09-17 17:30:33 -07003255
Martijn Coenen9fd2b642019-12-24 13:04:36 +01003256 return;
3257 }
3258 throw new SecurityException("Invalid mkdirs path: " + appFile
3259 + " is not a known app path.");
Jeff Sharkey2d8b4e82013-09-17 17:30:33 -07003260 }
3261
3262 @Override
Jeff Sharkey46349872015-07-28 10:49:47 -07003263 public StorageVolume[] getVolumeList(int uid, String packageName, int flags) {
Jeff Sharkeyce14cd02015-12-07 15:35:42 -07003264 final int userId = UserHandle.getUserId(uid);
Jeff Sharkeyc02bfae2016-03-27 15:06:53 -06003265
Jeff Sharkey46349872015-07-28 10:49:47 -07003266 final boolean forWrite = (flags & StorageManager.FLAG_FOR_WRITE) != 0;
Jeff Sharkeyc02bfae2016-03-27 15:06:53 -06003267 final boolean realState = (flags & StorageManager.FLAG_REAL_STATE) != 0;
3268 final boolean includeInvisible = (flags & StorageManager.FLAG_INCLUDE_INVISIBLE) != 0;
Jeff Sharkey04b4ba12019-12-15 22:42:42 -07003269 final boolean includeRecent = (flags & StorageManager.FLAG_INCLUDE_RECENT) != 0;
Jeff Sharkey46349872015-07-28 10:49:47 -07003270
Narayan Kamath157dd1d2019-06-12 13:06:30 +01003271 // Report all volumes as unmounted until we've recorded that user 0 has unlocked. There
3272 // are no guarantees that callers will see a consistent view of the volume before that
3273 // point
3274 final boolean systemUserUnlocked = isSystemUnlocked(UserHandle.USER_SYSTEM);
3275
Jeff Sharkey04b4ba12019-12-15 22:42:42 -07003276 final boolean userIsDemo;
Jeff Sharkeyc02bfae2016-03-27 15:06:53 -06003277 final boolean userKeyUnlocked;
3278 final boolean storagePermission;
3279 final long token = Binder.clearCallingIdentity();
Svetoslav38c3dbb2015-07-14 11:27:06 -07003280 try {
Jeff Sharkey04b4ba12019-12-15 22:42:42 -07003281 userIsDemo = LocalServices.getService(UserManagerInternal.class)
3282 .getUserInfo(userId).isDemo();
Jeff Sharkeyc02bfae2016-03-27 15:06:53 -06003283 userKeyUnlocked = isUserKeyUnlocked(userId);
Sudheer Shanka2250d562016-11-07 15:41:02 -08003284 storagePermission = mStorageManagerInternal.hasExternalStorage(uid, packageName);
Svetoslav38c3dbb2015-07-14 11:27:06 -07003285 } finally {
Jeff Sharkeyc02bfae2016-03-27 15:06:53 -06003286 Binder.restoreCallingIdentity(token);
Svetoslav38c3dbb2015-07-14 11:27:06 -07003287 }
Svet Ganov6ee871e2015-07-10 14:29:33 -07003288
Jeff Sharkeyc02bfae2016-03-27 15:06:53 -06003289 boolean foundPrimary = false;
3290
Jeff Sharkeyce14cd02015-12-07 15:35:42 -07003291 final ArrayList<StorageVolume> res = new ArrayList<>();
Jeff Sharkey04b4ba12019-12-15 22:42:42 -07003292 final ArraySet<String> resUuids = new ArraySet<>();
Jeff Sharkey48877892015-03-18 11:27:19 -07003293 synchronized (mLock) {
Jeff Sharkey1b8ef7e2015-04-03 17:14:45 -07003294 for (int i = 0; i < mVolumes.size(); i++) {
3295 final VolumeInfo vol = mVolumes.valueAt(i);
Jeff Sharkeyc02bfae2016-03-27 15:06:53 -06003296 switch (vol.getType()) {
3297 case VolumeInfo.TYPE_PUBLIC:
Risan05c41e62018-10-29 08:57:43 +09003298 case VolumeInfo.TYPE_STUB:
Jeff Sharkeyc02bfae2016-03-27 15:06:53 -06003299 break;
Zim17be6f92019-09-25 14:37:55 +01003300 case VolumeInfo.TYPE_EMULATED:
3301 if (vol.getMountUserId() == userId) {
3302 break;
3303 }
3304 // Skip if emulated volume not for userId
Jeff Sharkeyc02bfae2016-03-27 15:06:53 -06003305 default:
3306 continue;
3307 }
3308
3309 boolean match = false;
3310 if (forWrite) {
3311 match = vol.isVisibleForWrite(userId);
3312 } else {
Felipe Leme123a0e72016-06-10 11:09:11 -07003313 match = vol.isVisibleForRead(userId)
3314 || (includeInvisible && vol.getPath() != null);
Jeff Sharkeyc02bfae2016-03-27 15:06:53 -06003315 }
3316 if (!match) continue;
3317
3318 boolean reportUnmounted = false;
Narayan Kamath157dd1d2019-06-12 13:06:30 +01003319 if (!systemUserUnlocked) {
3320 reportUnmounted = true;
3321 } else if ((vol.getType() == VolumeInfo.TYPE_EMULATED) && !userKeyUnlocked) {
Jeff Sharkeyc02bfae2016-03-27 15:06:53 -06003322 reportUnmounted = true;
3323 } else if (!storagePermission && !realState) {
3324 reportUnmounted = true;
3325 }
3326
3327 final StorageVolume userVol = vol.buildStorageVolume(mContext, userId,
3328 reportUnmounted);
3329 if (vol.isPrimary()) {
3330 res.add(0, userVol);
3331 foundPrimary = true;
3332 } else {
3333 res.add(userVol);
Jeff Sharkeyb049e212012-09-07 23:16:01 -07003334 }
Jeff Sharkey04b4ba12019-12-15 22:42:42 -07003335 resUuids.add(userVol.getUuid());
Mike Lockwood8fa5f802011-03-24 08:12:30 -07003336 }
Jeff Sharkey04b4ba12019-12-15 22:42:42 -07003337
3338 if (includeRecent) {
3339 final long lastWeek = System.currentTimeMillis() - DateUtils.WEEK_IN_MILLIS;
3340 for (int i = 0; i < mRecords.size(); i++) {
3341 final VolumeRecord rec = mRecords.valueAt(i);
3342
3343 // Skip if we've already included it above
3344 if (resUuids.contains(rec.fsUuid)) continue;
3345
3346 // Treat as recent if mounted within the last week
3347 if (rec.lastSeenMillis > 0 && rec.lastSeenMillis < lastWeek) {
3348 final StorageVolume userVol = rec.buildStorageVolume(mContext);
3349 res.add(userVol);
3350 resUuids.add(userVol.getUuid());
3351 }
3352 }
3353 }
3354 }
3355
3356 // Synthesize a volume for preloaded media under demo users, so that
3357 // it's scanned into MediaStore
3358 if (userIsDemo) {
3359 final String id = "demo";
3360 final File path = Environment.getDataPreloadsMediaDirectory();
3361 final boolean primary = false;
3362 final boolean removable = false;
3363 final boolean emulated = true;
3364 final boolean allowMassStorage = false;
3365 final long maxFileSize = 0;
3366 final UserHandle user = new UserHandle(userId);
3367 final String envState = Environment.MEDIA_MOUNTED_READ_ONLY;
3368 final String description = mContext.getString(android.R.string.unknownName);
3369
3370 res.add(new StorageVolume(id, path, path, description, primary, removable,
3371 emulated, allowMassStorage, maxFileSize, user, id, envState));
Mike Lockwood8fa5f802011-03-24 08:12:30 -07003372 }
Jeff Sharkey48877892015-03-18 11:27:19 -07003373
3374 if (!foundPrimary) {
Jeff Sharkey11697f52018-12-13 10:14:42 -07003375 Slog.w(TAG, "No primary storage defined yet; hacking together a stub");
Jeff Sharkey48877892015-03-18 11:27:19 -07003376
3377 final boolean primaryPhysical = SystemProperties.getBoolean(
Jeff Sharkey1b8ef7e2015-04-03 17:14:45 -07003378 StorageManager.PROP_PRIMARY_PHYSICAL, false);
Jeff Sharkey48877892015-03-18 11:27:19 -07003379
3380 final String id = "stub_primary";
3381 final File path = Environment.getLegacyExternalStorageDirectory();
Jeff Sharkey1b8ef7e2015-04-03 17:14:45 -07003382 final String description = mContext.getString(android.R.string.unknownName);
Jeff Sharkey48877892015-03-18 11:27:19 -07003383 final boolean primary = true;
3384 final boolean removable = primaryPhysical;
3385 final boolean emulated = !primaryPhysical;
Jeff Sharkey48877892015-03-18 11:27:19 -07003386 final boolean allowMassStorage = false;
3387 final long maxFileSize = 0L;
3388 final UserHandle owner = new UserHandle(userId);
3389 final String uuid = null;
Jeff Sharkey48877892015-03-18 11:27:19 -07003390 final String state = Environment.MEDIA_REMOVED;
3391
Jerry Zhang71938e12018-05-10 18:28:29 -07003392 res.add(0, new StorageVolume(id, path, path,
Jerry Zhangf9c5c252017-08-16 18:07:51 -07003393 description, primary, removable, emulated,
Jeff Sharkey1b8ef7e2015-04-03 17:14:45 -07003394 allowMassStorage, maxFileSize, owner, uuid, state));
Jeff Sharkey48877892015-03-18 11:27:19 -07003395 }
3396
3397 return res.toArray(new StorageVolume[res.size()]);
Mike Lockwood8fa5f802011-03-24 08:12:30 -07003398 }
3399
Jeff Sharkey1b8ef7e2015-04-03 17:14:45 -07003400 @Override
3401 public DiskInfo[] getDisks() {
3402 synchronized (mLock) {
3403 final DiskInfo[] res = new DiskInfo[mDisks.size()];
3404 for (int i = 0; i < mDisks.size(); i++) {
3405 res[i] = mDisks.valueAt(i);
3406 }
3407 return res;
3408 }
3409 }
3410
3411 @Override
Jeff Sharkeyd95d3bf2015-04-14 21:39:44 -07003412 public VolumeInfo[] getVolumes(int flags) {
Jeff Sharkey1b8ef7e2015-04-03 17:14:45 -07003413 synchronized (mLock) {
3414 final VolumeInfo[] res = new VolumeInfo[mVolumes.size()];
3415 for (int i = 0; i < mVolumes.size(); i++) {
3416 res[i] = mVolumes.valueAt(i);
3417 }
3418 return res;
3419 }
3420 }
3421
Jeff Sharkeyb36586a2015-04-27 08:42:28 -07003422 @Override
3423 public VolumeRecord[] getVolumeRecords(int flags) {
3424 synchronized (mLock) {
3425 final VolumeRecord[] res = new VolumeRecord[mRecords.size()];
3426 for (int i = 0; i < mRecords.size(); i++) {
3427 res[i] = mRecords.valueAt(i);
3428 }
3429 return res;
3430 }
3431 }
3432
Jeff Sharkey9bed0702017-01-23 20:37:05 -07003433 @Override
3434 public long getCacheQuotaBytes(String volumeUuid, int uid) {
3435 if (uid != Binder.getCallingUid()) {
3436 mContext.enforceCallingPermission(android.Manifest.permission.STORAGE_INTERNAL, TAG);
3437 }
Daniel Nishi80fdb012017-03-09 14:30:07 -08003438 final long token = Binder.clearCallingIdentity();
3439 final StorageStatsManager stats = mContext.getSystemService(StorageStatsManager.class);
3440 try {
3441 return stats.getCacheQuotaBytes(volumeUuid, uid);
3442 } finally {
3443 Binder.restoreCallingIdentity(token);
3444 }
Jeff Sharkey9bed0702017-01-23 20:37:05 -07003445 }
3446
3447 @Override
3448 public long getCacheSizeBytes(String volumeUuid, int uid) {
3449 if (uid != Binder.getCallingUid()) {
3450 mContext.enforceCallingPermission(android.Manifest.permission.STORAGE_INTERNAL, TAG);
3451 }
3452 final long token = Binder.clearCallingIdentity();
3453 try {
3454 return mContext.getSystemService(StorageStatsManager.class)
3455 .queryStatsForUid(volumeUuid, uid).getCacheBytes();
Jeff Sharkey789a8fc2017-04-16 13:18:35 -06003456 } catch (IOException e) {
3457 throw new ParcelableException(e);
Jeff Sharkey9bed0702017-01-23 20:37:05 -07003458 } finally {
3459 Binder.restoreCallingIdentity(token);
3460 }
3461 }
3462
Jeff Sharkey35e46d22017-06-09 10:01:20 -06003463 private int adjustAllocateFlags(int flags, int callingUid, String callingPackage) {
3464 // Require permission to allocate aggressively
3465 if ((flags & StorageManager.FLAG_ALLOCATE_AGGRESSIVE) != 0) {
Jeff Sharkeyd5d5e922017-02-21 10:51:23 -07003466 mContext.enforceCallingOrSelfPermission(
3467 android.Manifest.permission.ALLOCATE_AGGRESSIVE, TAG);
3468 }
3469
Jeff Sharkey35e46d22017-06-09 10:01:20 -06003470 // Apps normally can't directly defy reserved space
3471 flags &= ~StorageManager.FLAG_ALLOCATE_DEFY_ALL_RESERVED;
3472 flags &= ~StorageManager.FLAG_ALLOCATE_DEFY_HALF_RESERVED;
3473
3474 // However, if app is actively using the camera, then we're willing to
3475 // clear up to half of the reserved cache space, since the user might be
3476 // trying to capture an important memory.
3477 final AppOpsManager appOps = mContext.getSystemService(AppOpsManager.class);
3478 final long token = Binder.clearCallingIdentity();
3479 try {
3480 if (appOps.isOperationActive(AppOpsManager.OP_CAMERA, callingUid, callingPackage)) {
3481 Slog.d(TAG, "UID " + callingUid + " is actively using camera;"
3482 + " letting them defy reserved cached data");
3483 flags |= StorageManager.FLAG_ALLOCATE_DEFY_HALF_RESERVED;
3484 }
3485 } finally {
3486 Binder.restoreCallingIdentity(token);
3487 }
3488
3489 return flags;
3490 }
3491
3492 @Override
3493 public long getAllocatableBytes(String volumeUuid, int flags, String callingPackage) {
3494 flags = adjustAllocateFlags(flags, Binder.getCallingUid(), callingPackage);
3495
3496 final StorageManager storage = mContext.getSystemService(StorageManager.class);
3497 final StorageStatsManager stats = mContext.getSystemService(StorageStatsManager.class);
Jeff Sharkeyd5d5e922017-02-21 10:51:23 -07003498 final long token = Binder.clearCallingIdentity();
3499 try {
3500 // In general, apps can allocate as much space as they want, except
3501 // we never let them eat into either the minimum cache space or into
Jeff Sharkeyddff8072017-05-26 13:10:46 -06003502 // the low disk warning space. To avoid user confusion, this logic
3503 // should be kept in sync with getFreeBytes().
Jeff Sharkeyd5d5e922017-02-21 10:51:23 -07003504 final File path = storage.findPathForUuid(volumeUuid);
Jeff Sharkeyddff8072017-05-26 13:10:46 -06003505
Noah Zimmtb2558072019-07-25 16:15:06 -07003506 long usable = 0;
3507 long lowReserved = 0;
3508 long fullReserved = 0;
3509 long cacheClearable = 0;
Jeff Sharkeyddff8072017-05-26 13:10:46 -06003510
Noah Zimmtb2558072019-07-25 16:15:06 -07003511 if ((flags & StorageManager.FLAG_ALLOCATE_CACHE_ONLY) == 0) {
3512 usable = path.getUsableSpace();
3513 lowReserved = storage.getStorageLowBytes(path);
3514 fullReserved = storage.getStorageFullBytes(path);
3515 }
3516
3517 if ((flags & StorageManager.FLAG_ALLOCATE_NON_CACHE_ONLY) == 0
3518 && stats.isQuotaSupported(volumeUuid)) {
Jeff Sharkeyddff8072017-05-26 13:10:46 -06003519 final long cacheTotal = stats.getCacheBytes(volumeUuid);
Jeff Sharkey35e46d22017-06-09 10:01:20 -06003520 final long cacheReserved = storage.getStorageCacheBytes(path, flags);
Noah Zimmtb2558072019-07-25 16:15:06 -07003521 cacheClearable = Math.max(0, cacheTotal - cacheReserved);
3522 }
Jeff Sharkeyddff8072017-05-26 13:10:46 -06003523
Noah Zimmtb2558072019-07-25 16:15:06 -07003524 if ((flags & StorageManager.FLAG_ALLOCATE_AGGRESSIVE) != 0) {
3525 return Math.max(0, (usable + cacheClearable) - fullReserved);
Jeff Sharkeyd5d5e922017-02-21 10:51:23 -07003526 } else {
Noah Zimmtb2558072019-07-25 16:15:06 -07003527 return Math.max(0, (usable + cacheClearable) - lowReserved);
Jeff Sharkeyd5d5e922017-02-21 10:51:23 -07003528 }
Jeff Sharkey789a8fc2017-04-16 13:18:35 -06003529 } catch (IOException e) {
3530 throw new ParcelableException(e);
Jeff Sharkeyd5d5e922017-02-21 10:51:23 -07003531 } finally {
3532 Binder.restoreCallingIdentity(token);
3533 }
Jeff Sharkey500ce9e2017-02-12 02:39:24 -07003534 }
3535
3536 @Override
Jeff Sharkey35e46d22017-06-09 10:01:20 -06003537 public void allocateBytes(String volumeUuid, long bytes, int flags, String callingPackage) {
3538 flags = adjustAllocateFlags(flags, Binder.getCallingUid(), callingPackage);
Jeff Sharkeyd5d5e922017-02-21 10:51:23 -07003539
Noah Zimmtb2558072019-07-25 16:15:06 -07003540 final long allocatableBytes = getAllocatableBytes(volumeUuid,
3541 flags | StorageManager.FLAG_ALLOCATE_NON_CACHE_ONLY, callingPackage);
Jeff Sharkeyd5d5e922017-02-21 10:51:23 -07003542 if (bytes > allocatableBytes) {
Noah Zimmtb2558072019-07-25 16:15:06 -07003543 // If we don't have room without taking cache into account, check to see if we'd have
3544 // room if we included freeable cache space.
3545 final long cacheClearable = getAllocatableBytes(volumeUuid,
3546 flags | StorageManager.FLAG_ALLOCATE_CACHE_ONLY, callingPackage);
3547 if (bytes > allocatableBytes + cacheClearable) {
3548 throw new ParcelableException(new IOException("Failed to allocate " + bytes
3549 + " because only " + (allocatableBytes + cacheClearable) + " allocatable"));
3550 }
Jeff Sharkeyd5d5e922017-02-21 10:51:23 -07003551 }
3552
Jeff Sharkey35e46d22017-06-09 10:01:20 -06003553 final StorageManager storage = mContext.getSystemService(StorageManager.class);
Jeff Sharkeyd5d5e922017-02-21 10:51:23 -07003554 final long token = Binder.clearCallingIdentity();
3555 try {
Jeff Sharkey789a8fc2017-04-16 13:18:35 -06003556 // Free up enough disk space to satisfy both the requested allocation
3557 // and our low disk warning space.
3558 final File path = storage.findPathForUuid(volumeUuid);
Jeff Sharkeyddff8072017-05-26 13:10:46 -06003559 if ((flags & StorageManager.FLAG_ALLOCATE_AGGRESSIVE) != 0) {
3560 bytes += storage.getStorageFullBytes(path);
3561 } else {
3562 bytes += storage.getStorageLowBytes(path);
3563 }
Jeff Sharkey789a8fc2017-04-16 13:18:35 -06003564
Jeff Sharkey5790af02018-08-13 17:42:54 -06003565 mPmInternal.freeStorage(volumeUuid, bytes, flags);
Jeff Sharkeyd5d5e922017-02-21 10:51:23 -07003566 } catch (IOException e) {
3567 throw new ParcelableException(e);
3568 } finally {
3569 Binder.restoreCallingIdentity(token);
Jeff Sharkey500ce9e2017-02-12 02:39:24 -07003570 }
3571 }
3572
Sudheer Shankaff971bc2018-12-13 17:39:59 -08003573 private IAppOpsCallback.Stub mAppOpsCallback = new IAppOpsCallback.Stub() {
3574 @Override
3575 public void opChanged(int op, int uid, String packageName) throws RemoteException {
3576 if (!ENABLE_ISOLATED_STORAGE) return;
3577
Zim64726cb2019-11-27 13:06:34 +00003578 int mountMode = getMountMode(uid, packageName);
3579 boolean isUidActive = LocalServices.getService(ActivityManagerInternal.class)
3580 .getUidProcessState(uid) != PROCESS_STATE_NONEXISTENT;
3581
3582 if (isUidActive) {
3583 remountUidExternalStorage(uid, mountMode);
3584 }
Sudheer Shankaff971bc2018-12-13 17:39:59 -08003585 }
3586 };
3587
Kenny Rootaf9d6672010-10-08 09:21:39 -07003588 private void addObbStateLocked(ObbState obbState) throws RemoteException {
3589 final IBinder binder = obbState.getBinder();
3590 List<ObbState> obbStates = mObbMounts.get(binder);
Kenny Root5919ac62010-10-05 09:49:40 -07003591
Kenny Rootaf9d6672010-10-08 09:21:39 -07003592 if (obbStates == null) {
3593 obbStates = new ArrayList<ObbState>();
3594 mObbMounts.put(binder, obbStates);
3595 } else {
3596 for (final ObbState o : obbStates) {
Jeff Sharkey4fbbda42012-09-24 18:34:07 -07003597 if (o.rawPath.equals(obbState.rawPath)) {
Kenny Rootaf9d6672010-10-08 09:21:39 -07003598 throw new IllegalStateException("Attempt to add ObbState twice. "
Sudheer Shanka2250d562016-11-07 15:41:02 -08003599 + "This indicates an error in the StorageManagerService logic.");
Kenny Root5919ac62010-10-05 09:49:40 -07003600 }
3601 }
Kenny Roota02b8b02010-08-05 16:14:17 -07003602 }
Kenny Rootaf9d6672010-10-08 09:21:39 -07003603
3604 obbStates.add(obbState);
3605 try {
3606 obbState.link();
3607 } catch (RemoteException e) {
3608 /*
3609 * The binder died before we could link it, so clean up our state
3610 * and return failure.
3611 */
3612 obbStates.remove(obbState);
3613 if (obbStates.isEmpty()) {
3614 mObbMounts.remove(binder);
3615 }
3616
3617 // Rethrow the error so mountObb can get it
3618 throw e;
3619 }
3620
Jeff Sharkey4fbbda42012-09-24 18:34:07 -07003621 mObbPathToStateMap.put(obbState.rawPath, obbState);
Kenny Roota02b8b02010-08-05 16:14:17 -07003622 }
3623
Kenny Rootaf9d6672010-10-08 09:21:39 -07003624 private void removeObbStateLocked(ObbState obbState) {
3625 final IBinder binder = obbState.getBinder();
3626 final List<ObbState> obbStates = mObbMounts.get(binder);
3627 if (obbStates != null) {
3628 if (obbStates.remove(obbState)) {
3629 obbState.unlink();
Kenny Root05105f72010-09-22 17:29:43 -07003630 }
Kenny Rootaf9d6672010-10-08 09:21:39 -07003631 if (obbStates.isEmpty()) {
3632 mObbMounts.remove(binder);
3633 }
Kenny Roota02b8b02010-08-05 16:14:17 -07003634 }
Kenny Roota02b8b02010-08-05 16:14:17 -07003635
Jeff Sharkey4fbbda42012-09-24 18:34:07 -07003636 mObbPathToStateMap.remove(obbState.rawPath);
Kenny Root38cf8862010-09-26 14:18:51 -07003637 }
3638
Kenny Roota02b8b02010-08-05 16:14:17 -07003639 private class ObbActionHandler extends Handler {
Kenny Roota02b8b02010-08-05 16:14:17 -07003640
3641 ObbActionHandler(Looper l) {
3642 super(l);
3643 }
3644
3645 @Override
3646 public void handleMessage(Message msg) {
3647 switch (msg.what) {
3648 case OBB_RUN_ACTION: {
Kenny Root480afe72010-10-07 10:17:50 -07003649 final ObbAction action = (ObbAction) msg.obj;
Kenny Roota02b8b02010-08-05 16:14:17 -07003650
3651 if (DEBUG_OBB)
3652 Slog.i(TAG, "OBB_RUN_ACTION: " + action.toString());
3653
Sudheer Shanka25469aa2018-08-27 15:50:23 -07003654 action.execute(this);
Kenny Roota02b8b02010-08-05 16:14:17 -07003655 break;
3656 }
Kenny Rootaf9d6672010-10-08 09:21:39 -07003657 case OBB_FLUSH_MOUNT_STATE: {
3658 final String path = (String) msg.obj;
3659
3660 if (DEBUG_OBB)
3661 Slog.i(TAG, "Flushing all OBB state for path " + path);
3662
3663 synchronized (mObbMounts) {
3664 final List<ObbState> obbStatesToRemove = new LinkedList<ObbState>();
3665
Jeff Sharkey4fbbda42012-09-24 18:34:07 -07003666 final Iterator<ObbState> i = mObbPathToStateMap.values().iterator();
Kenny Rootaf9d6672010-10-08 09:21:39 -07003667 while (i.hasNext()) {
Jeff Sharkey4fbbda42012-09-24 18:34:07 -07003668 final ObbState state = i.next();
Kenny Rootaf9d6672010-10-08 09:21:39 -07003669
3670 /*
3671 * If this entry's source file is in the volume path
3672 * that got unmounted, remove it because it's no
3673 * longer valid.
3674 */
Jeff Sharkey4fbbda42012-09-24 18:34:07 -07003675 if (state.canonicalPath.startsWith(path)) {
3676 obbStatesToRemove.add(state);
Kenny Rootaf9d6672010-10-08 09:21:39 -07003677 }
3678 }
3679
3680 for (final ObbState obbState : obbStatesToRemove) {
3681 if (DEBUG_OBB)
Jeff Sharkey4fbbda42012-09-24 18:34:07 -07003682 Slog.i(TAG, "Removing state for " + obbState.rawPath);
Kenny Rootaf9d6672010-10-08 09:21:39 -07003683
3684 removeObbStateLocked(obbState);
3685
3686 try {
Jeff Sharkey4fbbda42012-09-24 18:34:07 -07003687 obbState.token.onObbResult(obbState.rawPath, obbState.nonce,
Kenny Rootaf9d6672010-10-08 09:21:39 -07003688 OnObbStateChangeListener.UNMOUNTED);
3689 } catch (RemoteException e) {
3690 Slog.i(TAG, "Couldn't send unmount notification for OBB: "
Jeff Sharkey4fbbda42012-09-24 18:34:07 -07003691 + obbState.rawPath);
Kenny Rootaf9d6672010-10-08 09:21:39 -07003692 }
3693 }
3694 }
3695 break;
3696 }
Kenny Roota02b8b02010-08-05 16:14:17 -07003697 }
3698 }
Kenny Roota02b8b02010-08-05 16:14:17 -07003699 }
3700
Jeff Sharkey0095a822018-02-15 13:06:53 -07003701 private static class ObbException extends Exception {
3702 public final int status;
3703
3704 public ObbException(int status, String message) {
3705 super(message);
3706 this.status = status;
3707 }
3708
3709 public ObbException(int status, Throwable cause) {
3710 super(cause.getMessage(), cause);
3711 this.status = status;
3712 }
3713 }
3714
Kenny Roota02b8b02010-08-05 16:14:17 -07003715 abstract class ObbAction {
Kenny Roota02b8b02010-08-05 16:14:17 -07003716
3717 ObbState mObbState;
3718
3719 ObbAction(ObbState obbState) {
3720 mObbState = obbState;
3721 }
3722
3723 public void execute(ObbActionHandler handler) {
3724 try {
3725 if (DEBUG_OBB)
Ben Komalo444eca22011-09-01 15:17:44 -07003726 Slog.i(TAG, "Starting to execute action: " + toString());
Sudheer Shanka25469aa2018-08-27 15:50:23 -07003727 handleExecute();
Jeff Sharkey0095a822018-02-15 13:06:53 -07003728 } catch (ObbException e) {
3729 notifyObbStateChange(e);
Kenny Roota02b8b02010-08-05 16:14:17 -07003730 }
3731 }
3732
Jeff Sharkey0095a822018-02-15 13:06:53 -07003733 abstract void handleExecute() throws ObbException;
Kenny Root38cf8862010-09-26 14:18:51 -07003734
Jeff Sharkey0095a822018-02-15 13:06:53 -07003735 protected void notifyObbStateChange(ObbException e) {
3736 Slog.w(TAG, e);
3737 notifyObbStateChange(e.status);
3738 }
3739
3740 protected void notifyObbStateChange(int status) {
Kenny Rootaf9d6672010-10-08 09:21:39 -07003741 if (mObbState == null || mObbState.token == null) {
3742 return;
3743 }
3744
Kenny Root38cf8862010-09-26 14:18:51 -07003745 try {
Jeff Sharkey4fbbda42012-09-24 18:34:07 -07003746 mObbState.token.onObbResult(mObbState.rawPath, mObbState.nonce, status);
Kenny Root38cf8862010-09-26 14:18:51 -07003747 } catch (RemoteException e) {
Sudheer Shanka2250d562016-11-07 15:41:02 -08003748 Slog.w(TAG, "StorageEventListener went away while calling onObbStateChanged");
Kenny Root38cf8862010-09-26 14:18:51 -07003749 }
3750 }
Kenny Roota02b8b02010-08-05 16:14:17 -07003751 }
3752
3753 class MountObbAction extends ObbAction {
Ben Komalo444eca22011-09-01 15:17:44 -07003754 private final String mKey;
Jeff Sharkey4fbbda42012-09-24 18:34:07 -07003755 private final int mCallingUid;
Sudheer Shanka25469aa2018-08-27 15:50:23 -07003756 private ObbInfo mObbInfo;
Kenny Roota02b8b02010-08-05 16:14:17 -07003757
Sudheer Shanka25469aa2018-08-27 15:50:23 -07003758 MountObbAction(ObbState obbState, String key, int callingUid, ObbInfo obbInfo) {
Kenny Roota02b8b02010-08-05 16:14:17 -07003759 super(obbState);
3760 mKey = key;
Jeff Sharkey4fbbda42012-09-24 18:34:07 -07003761 mCallingUid = callingUid;
Sudheer Shanka25469aa2018-08-27 15:50:23 -07003762 mObbInfo = obbInfo;
Kenny Roota02b8b02010-08-05 16:14:17 -07003763 }
3764
Jason parks5af0b912010-11-29 09:05:25 -06003765 @Override
Jeff Sharkey0095a822018-02-15 13:06:53 -07003766 public void handleExecute() throws ObbException {
Kenny Rootaf9d6672010-10-08 09:21:39 -07003767 warnOnNotMounted();
3768
Sudheer Shanka25469aa2018-08-27 15:50:23 -07003769 if (!isUidOwnerOfPackageOrSystem(mObbInfo.packageName, mCallingUid)) {
Jeff Sharkey0095a822018-02-15 13:06:53 -07003770 throw new ObbException(ERROR_PERMISSION_DENIED, "Denied attempt to mount OBB "
Sudheer Shanka25469aa2018-08-27 15:50:23 -07003771 + mObbInfo.filename + " which is owned by " + mObbInfo.packageName);
Kenny Roota02b8b02010-08-05 16:14:17 -07003772 }
3773
Kenny Rootaf9d6672010-10-08 09:21:39 -07003774 final boolean isMounted;
3775 synchronized (mObbMounts) {
Jeff Sharkey4fbbda42012-09-24 18:34:07 -07003776 isMounted = mObbPathToStateMap.containsKey(mObbState.rawPath);
Kenny Rootaf9d6672010-10-08 09:21:39 -07003777 }
3778 if (isMounted) {
Jeff Sharkey0095a822018-02-15 13:06:53 -07003779 throw new ObbException(ERROR_ALREADY_MOUNTED,
Sudheer Shanka25469aa2018-08-27 15:50:23 -07003780 "Attempt to mount OBB which is already mounted: " + mObbInfo.filename);
Kenny Rootaf9d6672010-10-08 09:21:39 -07003781 }
3782
Kenny Rootaf9d6672010-10-08 09:21:39 -07003783 final String hashedKey;
Jeff Sharkey41cd6812017-09-11 10:32:17 -06003784 final String binderKey;
Kenny Rootaf9d6672010-10-08 09:21:39 -07003785 if (mKey == null) {
3786 hashedKey = "none";
Jeff Sharkey41cd6812017-09-11 10:32:17 -06003787 binderKey = "";
Kenny Rootaf9d6672010-10-08 09:21:39 -07003788 } else {
Kenny Rootaf9d6672010-10-08 09:21:39 -07003789 try {
Kenny Root3b1abba2010-10-13 15:00:07 -07003790 SecretKeyFactory factory = SecretKeyFactory.getInstance("PBKDF2WithHmacSHA1");
3791
Sudheer Shanka25469aa2018-08-27 15:50:23 -07003792 KeySpec ks = new PBEKeySpec(mKey.toCharArray(), mObbInfo.salt,
Kenny Root3b1abba2010-10-13 15:00:07 -07003793 PBKDF2_HASH_ROUNDS, CRYPTO_ALGORITHM_KEY_SIZE);
3794 SecretKey key = factory.generateSecret(ks);
3795 BigInteger bi = new BigInteger(key.getEncoded());
3796 hashedKey = bi.toString(16);
Jeff Sharkey41cd6812017-09-11 10:32:17 -06003797 binderKey = hashedKey;
Jeff Sharkey0095a822018-02-15 13:06:53 -07003798 } catch (GeneralSecurityException e) {
3799 throw new ObbException(ERROR_INTERNAL, e);
Kenny Root38cf8862010-09-26 14:18:51 -07003800 }
Kenny Rootaf9d6672010-10-08 09:21:39 -07003801 }
Kenny Root38cf8862010-09-26 14:18:51 -07003802
Kenny Rootaf9d6672010-10-08 09:21:39 -07003803 try {
Jeff Sharkey54402792017-09-15 16:05:19 -06003804 mObbState.volId = mVold.createObb(mObbState.canonicalPath, binderKey,
3805 mObbState.ownerGid);
Zim95eca1d2019-11-15 18:03:00 +00003806 mVold.mount(mObbState.volId, 0, -1, null);
Kenny Roota02b8b02010-08-05 16:14:17 -07003807
Kenny Rootaf9d6672010-10-08 09:21:39 -07003808 if (DEBUG_OBB)
Xiaohui Chen621b3fc2015-10-02 14:41:42 -07003809 Slog.d(TAG, "Successfully mounted OBB " + mObbState.canonicalPath);
Kenny Rootaf9d6672010-10-08 09:21:39 -07003810
3811 synchronized (mObbMounts) {
3812 addObbStateLocked(mObbState);
3813 }
3814
Jeff Sharkey0095a822018-02-15 13:06:53 -07003815 notifyObbStateChange(MOUNTED);
3816 } catch (Exception e) {
3817 throw new ObbException(ERROR_COULD_NOT_MOUNT, e);
Kenny Root02c87302010-07-01 08:10:18 -07003818 }
3819 }
3820
Jason parks5af0b912010-11-29 09:05:25 -06003821 @Override
Kenny Roota02b8b02010-08-05 16:14:17 -07003822 public String toString() {
3823 StringBuilder sb = new StringBuilder();
3824 sb.append("MountObbAction{");
Jeff Sharkey4fbbda42012-09-24 18:34:07 -07003825 sb.append(mObbState);
Kenny Roota02b8b02010-08-05 16:14:17 -07003826 sb.append('}');
3827 return sb.toString();
3828 }
3829 }
3830
3831 class UnmountObbAction extends ObbAction {
Ben Komalo444eca22011-09-01 15:17:44 -07003832 private final boolean mForceUnmount;
Kenny Roota02b8b02010-08-05 16:14:17 -07003833
3834 UnmountObbAction(ObbState obbState, boolean force) {
3835 super(obbState);
3836 mForceUnmount = force;
3837 }
3838
Jason parks5af0b912010-11-29 09:05:25 -06003839 @Override
Jeff Sharkey0095a822018-02-15 13:06:53 -07003840 public void handleExecute() throws ObbException {
Kenny Rootaf9d6672010-10-08 09:21:39 -07003841 warnOnNotMounted();
3842
Jeff Sharkey4fbbda42012-09-24 18:34:07 -07003843 final ObbState existingState;
Kenny Root38cf8862010-09-26 14:18:51 -07003844 synchronized (mObbMounts) {
Jeff Sharkey4fbbda42012-09-24 18:34:07 -07003845 existingState = mObbPathToStateMap.get(mObbState.rawPath);
Kenny Rootaf9d6672010-10-08 09:21:39 -07003846 }
Kenny Root38cf8862010-09-26 14:18:51 -07003847
Jeff Sharkey4fbbda42012-09-24 18:34:07 -07003848 if (existingState == null) {
Jeff Sharkey0095a822018-02-15 13:06:53 -07003849 throw new ObbException(ERROR_NOT_MOUNTED, "Missing existingState");
Kenny Rootaf9d6672010-10-08 09:21:39 -07003850 }
3851
Jeff Sharkey4fbbda42012-09-24 18:34:07 -07003852 if (existingState.ownerGid != mObbState.ownerGid) {
Jeff Sharkey0095a822018-02-15 13:06:53 -07003853 notifyObbStateChange(new ObbException(ERROR_PERMISSION_DENIED,
3854 "Permission denied to unmount OBB " + existingState.rawPath
3855 + " (owned by GID " + existingState.ownerGid + ")"));
Kenny Rootaf9d6672010-10-08 09:21:39 -07003856 return;
3857 }
3858
Kenny Rootaf9d6672010-10-08 09:21:39 -07003859 try {
Jeff Sharkey54402792017-09-15 16:05:19 -06003860 mVold.unmount(mObbState.volId);
3861 mVold.destroyObb(mObbState.volId);
3862 mObbState.volId = null;
Kenny Roota02b8b02010-08-05 16:14:17 -07003863
Kenny Rootaf9d6672010-10-08 09:21:39 -07003864 synchronized (mObbMounts) {
Jeff Sharkey4fbbda42012-09-24 18:34:07 -07003865 removeObbStateLocked(existingState);
Kenny Root38cf8862010-09-26 14:18:51 -07003866 }
3867
Jeff Sharkey0095a822018-02-15 13:06:53 -07003868 notifyObbStateChange(UNMOUNTED);
3869 } catch (Exception e) {
3870 throw new ObbException(ERROR_COULD_NOT_UNMOUNT, e);
Kenny Roota02b8b02010-08-05 16:14:17 -07003871 }
3872 }
3873
Jason parks5af0b912010-11-29 09:05:25 -06003874 @Override
Kenny Roota02b8b02010-08-05 16:14:17 -07003875 public String toString() {
3876 StringBuilder sb = new StringBuilder();
3877 sb.append("UnmountObbAction{");
Jeff Sharkey4fbbda42012-09-24 18:34:07 -07003878 sb.append(mObbState);
Kenny Roota02b8b02010-08-05 16:14:17 -07003879 sb.append(",force=");
3880 sb.append(mForceUnmount);
Kenny Roota02b8b02010-08-05 16:14:17 -07003881 sb.append('}');
3882 return sb.toString();
3883 }
Kenny Root02c87302010-07-01 08:10:18 -07003884 }
Kenny Root38cf8862010-09-26 14:18:51 -07003885
Jeff Sharkey7e19f532017-11-06 13:54:11 -07003886 private void dispatchOnStatus(IVoldTaskListener listener, int status,
3887 PersistableBundle extras) {
3888 if (listener != null) {
3889 try {
3890 listener.onStatus(status, extras);
3891 } catch (RemoteException ignored) {
3892 }
3893 }
3894 }
3895
3896 private void dispatchOnFinished(IVoldTaskListener listener, int status,
3897 PersistableBundle extras) {
3898 if (listener != null) {
3899 try {
3900 listener.onFinished(status, extras);
3901 } catch (RemoteException ignored) {
3902 }
3903 }
3904 }
3905
Sudheer Shanka3a0df3b2018-12-12 12:43:43 -08003906 private int getMountMode(int uid, String packageName) {
Jeff Sharkey6fd69942019-03-26 17:53:35 -06003907 final int mode = getMountModeInternal(uid, packageName);
3908 if (LOCAL_LOGV) {
3909 Slog.v(TAG, "Resolved mode " + mode + " for " + packageName + "/"
3910 + UserHandle.formatUid(uid));
3911 }
3912 return mode;
3913 }
3914
3915 private int getMountModeInternal(int uid, String packageName) {
Sudheer Shanka3a0df3b2018-12-12 12:43:43 -08003916 try {
Jeff Sharkey6fd69942019-03-26 17:53:35 -06003917 // Get some easy cases out of the way first
Sudheer Shanka3a0df3b2018-12-12 12:43:43 -08003918 if (Process.isIsolated(uid)) {
3919 return Zygote.MOUNT_EXTERNAL_NONE;
3920 }
Sudheer Shankab1613982019-05-16 16:55:50 -07003921
3922 final String[] packagesForUid = mIPackageManager.getPackagesForUid(uid);
Sudheer Shanka1df72db2019-05-24 10:59:52 -07003923 if (ArrayUtils.isEmpty(packagesForUid)) {
3924 // It's possible the package got uninstalled already, so just ignore.
3925 return Zygote.MOUNT_EXTERNAL_NONE;
3926 }
Sudheer Shankab1613982019-05-16 16:55:50 -07003927 if (packageName == null) {
3928 packageName = packagesForUid[0];
3929 }
3930
Jeff Sharkey6fd69942019-03-26 17:53:35 -06003931 if (mPmInternal.isInstantApp(packageName, UserHandle.getUserId(uid))) {
Winson Chiu5118d492019-05-15 18:24:06 +00003932 return Zygote.MOUNT_EXTERNAL_NONE;
Jeff Sharkey6fd69942019-03-26 17:53:35 -06003933 }
3934
Martijn Coenen44db1ac2019-12-03 16:06:19 +01003935 if (mIsFuseEnabled && mMediaStoreAuthorityAppId == UserHandle.getAppId(uid)) {
3936 // Determine if caller requires pass_through mount; note that we do this for
3937 // all processes that share a UID with MediaProvider; but this is fine, since
3938 // those processes anyway share the same rights as MediaProvider.
Zim74a9bba2019-09-03 20:49:13 +01003939 return Zygote.MOUNT_EXTERNAL_PASS_THROUGH;
3940 }
3941
Martijn Coenen496ac002020-01-08 14:55:53 +01003942 if (mIsFuseEnabled && mDownloadsAuthorityAppId == UserHandle.getAppId(uid)) {
3943 // DownloadManager can write in app-private directories on behalf of apps;
3944 // give it write access to Android/
3945 return Zygote.MOUNT_EXTERNAL_ANDROID_WRITABLE;
3946 }
3947
3948 final boolean hasMtp = mIPackageManager.checkUidPermission(ACCESS_MTP, uid) ==
3949 PERMISSION_GRANTED;
3950 if (mIsFuseEnabled && hasMtp) {
3951 // The process hosting the MTP server should be able to write in Android/
3952 return Zygote.MOUNT_EXTERNAL_ANDROID_WRITABLE;
3953 }
3954
Jeff Sharkey6fd69942019-03-26 17:53:35 -06003955 // Determine if caller is holding runtime permission
Chad Brubaker45810af2019-04-08 19:19:48 -07003956 final boolean hasRead = StorageManager.checkPermissionAndCheckOp(mContext, false, 0,
Jeff Sharkey6fd69942019-03-26 17:53:35 -06003957 uid, packageName, READ_EXTERNAL_STORAGE, OP_READ_EXTERNAL_STORAGE);
Chad Brubaker45810af2019-04-08 19:19:48 -07003958 final boolean hasWrite = StorageManager.checkPermissionAndCheckOp(mContext, false, 0,
Jeff Sharkey6fd69942019-03-26 17:53:35 -06003959 uid, packageName, WRITE_EXTERNAL_STORAGE, OP_WRITE_EXTERNAL_STORAGE);
Jeff Sharkey6fd69942019-03-26 17:53:35 -06003960
3961 // We're only willing to give out broad access if they also hold
3962 // runtime permission; this is a firm CDD requirement
3963 final boolean hasFull = mIPackageManager.checkUidPermission(WRITE_MEDIA_STORAGE,
3964 uid) == PERMISSION_GRANTED;
Jeff Sharkey26874a22019-04-17 21:30:47 -06003965 if (hasFull && hasWrite) {
Jeff Sharkey6fd69942019-03-26 17:53:35 -06003966 return Zygote.MOUNT_EXTERNAL_FULL;
3967 }
3968
3969 // We're only willing to give out installer access if they also hold
3970 // runtime permission; this is a firm CDD requirement
3971 final boolean hasInstall = mIPackageManager.checkUidPermission(INSTALL_PACKAGES,
3972 uid) == PERMISSION_GRANTED;
Sudheer Shankab1613982019-05-16 16:55:50 -07003973 boolean hasInstallOp = false;
3974 // OP_REQUEST_INSTALL_PACKAGES is granted/denied per package but vold can't
3975 // update mountpoints of a specific package. So, check the appop for all packages
3976 // sharing the uid and allow same level of storage access for all packages even if
3977 // one of the packages has the appop granted.
3978 for (String uidPackageName : packagesForUid) {
3979 if (mIAppOpsService.checkOperation(
3980 OP_REQUEST_INSTALL_PACKAGES, uid, uidPackageName) == MODE_ALLOWED) {
3981 hasInstallOp = true;
3982 break;
3983 }
3984 }
Jeff Sharkey26874a22019-04-17 21:30:47 -06003985 if ((hasInstall || hasInstallOp) && hasWrite) {
Martijn Coenenfc9c3b62019-12-13 16:36:27 +01003986 return Zygote.MOUNT_EXTERNAL_INSTALLER;
Jeff Sharkey6fd69942019-03-26 17:53:35 -06003987 }
3988
3989 // Otherwise we're willing to give out sandboxed or non-sandboxed if
3990 // they hold the runtime permission
Nandana Duttdc15bb42020-01-03 16:43:46 +00003991 boolean hasLegacy = mIAppOpsService.checkOperation(OP_LEGACY_STORAGE,
Jeff Sharkey6fd69942019-03-26 17:53:35 -06003992 uid, packageName) == MODE_ALLOWED;
Nandana Duttdc15bb42020-01-03 16:43:46 +00003993
3994 // Hack(b/147137425): we have to honor hasRequestedLegacyExternalStorage for a short
3995 // while to enable 2 cases.
3996 // 1) Apps that want to be in scoped storage in R, but want to opt out in Q devices,
3997 // because they want to use raw file paths, would fail until fuse is enabled by default.
3998 // 2) Test apps that target current sdk will fail. They would fail even after fuse is
3999 // enabled, but we are fixing it with b/142395442. We are not planning to enable
4000 // fuse by default until b/142395442 is fixed.
4001 if (!hasLegacy && !mIsFuseEnabled) {
4002 ApplicationInfo ai = mIPackageManager.getApplicationInfo(packageName,
4003 0, UserHandle.getUserId(uid));
4004 hasLegacy = ai.hasRequestedLegacyExternalStorage();
4005 }
4006
Jeff Sharkey26874a22019-04-17 21:30:47 -06004007 if (hasLegacy && hasWrite) {
4008 return Zygote.MOUNT_EXTERNAL_WRITE;
4009 } else if (hasLegacy && hasRead) {
4010 return Zygote.MOUNT_EXTERNAL_READ;
Sudheer Shanka3a0df3b2018-12-12 12:43:43 -08004011 } else {
Sudheer Shanka783c90e2019-04-12 13:55:20 -07004012 return Zygote.MOUNT_EXTERNAL_DEFAULT;
Sudheer Shanka3a0df3b2018-12-12 12:43:43 -08004013 }
4014 } catch (RemoteException e) {
4015 // Should not happen
4016 }
4017 return Zygote.MOUNT_EXTERNAL_NONE;
4018 }
4019
Jeff Sharkey7151a9a2015-04-04 15:22:37 -07004020 private static class Callbacks extends Handler {
4021 private static final int MSG_STORAGE_STATE_CHANGED = 1;
4022 private static final int MSG_VOLUME_STATE_CHANGED = 2;
Jeff Sharkey50a05452015-04-29 11:24:52 -07004023 private static final int MSG_VOLUME_RECORD_CHANGED = 3;
4024 private static final int MSG_VOLUME_FORGOTTEN = 4;
4025 private static final int MSG_DISK_SCANNED = 5;
Makoto Onuki9dc575d2015-06-12 16:10:25 -07004026 private static final int MSG_DISK_DESTROYED = 6;
Jeff Sharkey7151a9a2015-04-04 15:22:37 -07004027
Sudheer Shanka2250d562016-11-07 15:41:02 -08004028 private final RemoteCallbackList<IStorageEventListener>
Jeff Sharkey7151a9a2015-04-04 15:22:37 -07004029 mCallbacks = new RemoteCallbackList<>();
4030
4031 public Callbacks(Looper looper) {
4032 super(looper);
4033 }
4034
Sudheer Shanka2250d562016-11-07 15:41:02 -08004035 public void register(IStorageEventListener callback) {
Jeff Sharkey7151a9a2015-04-04 15:22:37 -07004036 mCallbacks.register(callback);
4037 }
4038
Sudheer Shanka2250d562016-11-07 15:41:02 -08004039 public void unregister(IStorageEventListener callback) {
Jeff Sharkey7151a9a2015-04-04 15:22:37 -07004040 mCallbacks.unregister(callback);
4041 }
4042
4043 @Override
4044 public void handleMessage(Message msg) {
4045 final SomeArgs args = (SomeArgs) msg.obj;
4046 final int n = mCallbacks.beginBroadcast();
4047 for (int i = 0; i < n; i++) {
Sudheer Shanka2250d562016-11-07 15:41:02 -08004048 final IStorageEventListener callback = mCallbacks.getBroadcastItem(i);
Jeff Sharkey7151a9a2015-04-04 15:22:37 -07004049 try {
4050 invokeCallback(callback, msg.what, args);
4051 } catch (RemoteException ignored) {
4052 }
4053 }
4054 mCallbacks.finishBroadcast();
4055 args.recycle();
4056 }
4057
Sudheer Shanka2250d562016-11-07 15:41:02 -08004058 private void invokeCallback(IStorageEventListener callback, int what, SomeArgs args)
Jeff Sharkey7151a9a2015-04-04 15:22:37 -07004059 throws RemoteException {
4060 switch (what) {
4061 case MSG_STORAGE_STATE_CHANGED: {
4062 callback.onStorageStateChanged((String) args.arg1, (String) args.arg2,
4063 (String) args.arg3);
4064 break;
4065 }
4066 case MSG_VOLUME_STATE_CHANGED: {
4067 callback.onVolumeStateChanged((VolumeInfo) args.arg1, args.argi2, args.argi3);
4068 break;
4069 }
Jeff Sharkey50a05452015-04-29 11:24:52 -07004070 case MSG_VOLUME_RECORD_CHANGED: {
4071 callback.onVolumeRecordChanged((VolumeRecord) args.arg1);
4072 break;
4073 }
4074 case MSG_VOLUME_FORGOTTEN: {
4075 callback.onVolumeForgotten((String) args.arg1);
Jeff Sharkeyd95d3bf2015-04-14 21:39:44 -07004076 break;
4077 }
Jeff Sharkey620b32b2015-04-23 19:36:02 -07004078 case MSG_DISK_SCANNED: {
4079 callback.onDiskScanned((DiskInfo) args.arg1, args.argi2);
Jeff Sharkey7e92ef32015-04-17 17:35:07 -07004080 break;
4081 }
Makoto Onuki9dc575d2015-06-12 16:10:25 -07004082 case MSG_DISK_DESTROYED: {
4083 callback.onDiskDestroyed((DiskInfo) args.arg1);
4084 break;
4085 }
Jeff Sharkey7151a9a2015-04-04 15:22:37 -07004086 }
4087 }
4088
4089 private void notifyStorageStateChanged(String path, String oldState, String newState) {
4090 final SomeArgs args = SomeArgs.obtain();
4091 args.arg1 = path;
4092 args.arg2 = oldState;
4093 args.arg3 = newState;
4094 obtainMessage(MSG_STORAGE_STATE_CHANGED, args).sendToTarget();
4095 }
4096
4097 private void notifyVolumeStateChanged(VolumeInfo vol, int oldState, int newState) {
4098 final SomeArgs args = SomeArgs.obtain();
Jeff Sharkeyb36586a2015-04-27 08:42:28 -07004099 args.arg1 = vol.clone();
Jeff Sharkey7151a9a2015-04-04 15:22:37 -07004100 args.argi2 = oldState;
4101 args.argi3 = newState;
4102 obtainMessage(MSG_VOLUME_STATE_CHANGED, args).sendToTarget();
4103 }
Jeff Sharkeyd95d3bf2015-04-14 21:39:44 -07004104
Jeff Sharkey50a05452015-04-29 11:24:52 -07004105 private void notifyVolumeRecordChanged(VolumeRecord rec) {
4106 final SomeArgs args = SomeArgs.obtain();
4107 args.arg1 = rec.clone();
4108 obtainMessage(MSG_VOLUME_RECORD_CHANGED, args).sendToTarget();
4109 }
4110
4111 private void notifyVolumeForgotten(String fsUuid) {
Jeff Sharkeyd95d3bf2015-04-14 21:39:44 -07004112 final SomeArgs args = SomeArgs.obtain();
Jeff Sharkeyb36586a2015-04-27 08:42:28 -07004113 args.arg1 = fsUuid;
Jeff Sharkey50a05452015-04-29 11:24:52 -07004114 obtainMessage(MSG_VOLUME_FORGOTTEN, args).sendToTarget();
Jeff Sharkeyd95d3bf2015-04-14 21:39:44 -07004115 }
Jeff Sharkey7e92ef32015-04-17 17:35:07 -07004116
Jeff Sharkey620b32b2015-04-23 19:36:02 -07004117 private void notifyDiskScanned(DiskInfo disk, int volumeCount) {
Jeff Sharkey7e92ef32015-04-17 17:35:07 -07004118 final SomeArgs args = SomeArgs.obtain();
Jeff Sharkeyb36586a2015-04-27 08:42:28 -07004119 args.arg1 = disk.clone();
Jeff Sharkey620b32b2015-04-23 19:36:02 -07004120 args.argi2 = volumeCount;
4121 obtainMessage(MSG_DISK_SCANNED, args).sendToTarget();
Jeff Sharkey7e92ef32015-04-17 17:35:07 -07004122 }
Makoto Onuki9dc575d2015-06-12 16:10:25 -07004123
4124 private void notifyDiskDestroyed(DiskInfo disk) {
4125 final SomeArgs args = SomeArgs.obtain();
4126 args.arg1 = disk.clone();
4127 obtainMessage(MSG_DISK_DESTROYED, args).sendToTarget();
4128 }
Jeff Sharkey7151a9a2015-04-04 15:22:37 -07004129 }
4130
Kenny Root38cf8862010-09-26 14:18:51 -07004131 @Override
Jeff Sharkey5aca2b82013-10-16 16:21:54 -07004132 protected void dump(FileDescriptor fd, PrintWriter writer, String[] args) {
Jeff Sharkeyfe9a53b2017-03-31 14:08:23 -06004133 if (!DumpUtils.checkDumpPermission(mContext, TAG, writer)) return;
Jeff Sharkey5aca2b82013-10-16 16:21:54 -07004134
4135 final IndentingPrintWriter pw = new IndentingPrintWriter(writer, " ", 160);
Jeff Sharkey27de30d2015-04-18 16:20:27 -07004136 synchronized (mLock) {
4137 pw.println("Disks:");
4138 pw.increaseIndent();
4139 for (int i = 0; i < mDisks.size(); i++) {
4140 final DiskInfo disk = mDisks.valueAt(i);
4141 disk.dump(pw);
4142 }
4143 pw.decreaseIndent();
4144
4145 pw.println();
4146 pw.println("Volumes:");
4147 pw.increaseIndent();
4148 for (int i = 0; i < mVolumes.size(); i++) {
4149 final VolumeInfo vol = mVolumes.valueAt(i);
4150 if (VolumeInfo.ID_PRIVATE_INTERNAL.equals(vol.id)) continue;
4151 vol.dump(pw);
4152 }
4153 pw.decreaseIndent();
4154
4155 pw.println();
Jeff Sharkeyb36586a2015-04-27 08:42:28 -07004156 pw.println("Records:");
Jeff Sharkey27de30d2015-04-18 16:20:27 -07004157 pw.increaseIndent();
Jeff Sharkeyb36586a2015-04-27 08:42:28 -07004158 for (int i = 0; i < mRecords.size(); i++) {
4159 final VolumeRecord note = mRecords.valueAt(i);
4160 note.dump(pw);
Jeff Sharkey27de30d2015-04-18 16:20:27 -07004161 }
4162 pw.decreaseIndent();
Jeff Sharkey275e3e42015-04-24 16:10:32 -07004163
4164 pw.println();
4165 pw.println("Primary storage UUID: " + mPrimaryStorageUuid);
Jeff Sharkeyb0c363b22018-12-15 11:53:03 -07004166
4167 pw.println();
Felipe Leme281389a2016-10-10 17:12:20 -07004168 final Pair<String, Long> pair = StorageManager.getPrimaryStoragePathAndSize();
4169 if (pair == null) {
4170 pw.println("Internal storage total size: N/A");
4171 } else {
4172 pw.print("Internal storage (");
4173 pw.print(pair.first);
4174 pw.print(") total size: ");
4175 pw.print(pair.second);
4176 pw.print(" (");
Jeff Sharkey9f2dc052018-01-07 16:47:31 -07004177 pw.print(DataUnit.MEBIBYTES.toBytes(pair.second));
4178 pw.println(" MiB)");
Felipe Leme281389a2016-10-10 17:12:20 -07004179 }
Jeff Sharkeyb0c363b22018-12-15 11:53:03 -07004180
4181 pw.println();
Jeff Sharkeyce14cd02015-12-07 15:35:42 -07004182 pw.println("Local unlocked users: " + Arrays.toString(mLocalUnlockedUsers));
4183 pw.println("System unlocked users: " + Arrays.toString(mSystemUnlockedUsers));
Jeff Sharkeyb0c363b22018-12-15 11:53:03 -07004184
4185 final ContentResolver cr = mContext.getContentResolver();
4186 pw.println();
4187 pw.println("Isolated storage, local feature flag: "
4188 + Settings.Global.getInt(cr, Settings.Global.ISOLATED_STORAGE_LOCAL, 0));
4189 pw.println("Isolated storage, remote feature flag: "
4190 + Settings.Global.getInt(cr, Settings.Global.ISOLATED_STORAGE_REMOTE, 0));
4191 pw.println("Isolated storage, resolved: " + StorageManager.hasIsolatedStorage());
Jeff Sharkey27de30d2015-04-18 16:20:27 -07004192 }
Kenny Root38cf8862010-09-26 14:18:51 -07004193
Kenny Root38cf8862010-09-26 14:18:51 -07004194 synchronized (mObbMounts) {
Jeff Sharkey27de30d2015-04-18 16:20:27 -07004195 pw.println();
Jeff Sharkey5aca2b82013-10-16 16:21:54 -07004196 pw.println("mObbMounts:");
4197 pw.increaseIndent();
4198 final Iterator<Entry<IBinder, List<ObbState>>> binders = mObbMounts.entrySet()
4199 .iterator();
Kenny Rootaf9d6672010-10-08 09:21:39 -07004200 while (binders.hasNext()) {
4201 Entry<IBinder, List<ObbState>> e = binders.next();
Jeff Sharkey5aca2b82013-10-16 16:21:54 -07004202 pw.println(e.getKey() + ":");
4203 pw.increaseIndent();
Kenny Rootaf9d6672010-10-08 09:21:39 -07004204 final List<ObbState> obbStates = e.getValue();
Kenny Root38cf8862010-09-26 14:18:51 -07004205 for (final ObbState obbState : obbStates) {
Jeff Sharkey5aca2b82013-10-16 16:21:54 -07004206 pw.println(obbState);
Kenny Root38cf8862010-09-26 14:18:51 -07004207 }
Jeff Sharkey5aca2b82013-10-16 16:21:54 -07004208 pw.decreaseIndent();
Kenny Root38cf8862010-09-26 14:18:51 -07004209 }
Jeff Sharkey5aca2b82013-10-16 16:21:54 -07004210 pw.decreaseIndent();
Kenny Rootaf9d6672010-10-08 09:21:39 -07004211
Jeff Sharkey5aca2b82013-10-16 16:21:54 -07004212 pw.println();
4213 pw.println("mObbPathToStateMap:");
4214 pw.increaseIndent();
Kenny Rootaf9d6672010-10-08 09:21:39 -07004215 final Iterator<Entry<String, ObbState>> maps = mObbPathToStateMap.entrySet().iterator();
4216 while (maps.hasNext()) {
4217 final Entry<String, ObbState> e = maps.next();
Jeff Sharkey5aca2b82013-10-16 16:21:54 -07004218 pw.print(e.getKey());
4219 pw.print(" -> ");
4220 pw.println(e.getValue());
Kenny Rootaf9d6672010-10-08 09:21:39 -07004221 }
Jeff Sharkey5aca2b82013-10-16 16:21:54 -07004222 pw.decreaseIndent();
Kenny Root38cf8862010-09-26 14:18:51 -07004223 }
Kenny Root4161f9b2011-07-13 09:48:33 -07004224
Robert Greenwalt470fd722012-01-18 12:51:15 -08004225 pw.println();
Christopher Tate7265abe2014-11-21 13:54:45 -08004226 pw.print("Last maintenance: ");
Jeff Sharkeye8a4b662015-06-27 15:43:45 -07004227 pw.println(TimeUtils.formatForLogging(mLastMaintenance));
Kenny Root38cf8862010-09-26 14:18:51 -07004228 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004229
Jeff Sharkeyfa23c5a2011-08-09 21:44:24 -07004230 /** {@inheritDoc} */
Jeff Sharkey48877892015-03-18 11:27:19 -07004231 @Override
Jeff Sharkeyfa23c5a2011-08-09 21:44:24 -07004232 public void monitor() {
Jeff Sharkey54402792017-09-15 16:05:19 -06004233 try {
4234 mVold.monitor();
4235 } catch (Exception e) {
4236 Slog.wtf(TAG, e);
Paul Lawrence1c62cbb2015-06-03 14:14:52 -07004237 }
Jeff Sharkeyfa23c5a2011-08-09 21:44:24 -07004238 }
Svet Ganov6ee871e2015-07-10 14:29:33 -07004239
Sudheer Shanka2250d562016-11-07 15:41:02 -08004240 private final class StorageManagerInternalImpl extends StorageManagerInternal {
Svet Ganov6ee871e2015-07-10 14:29:33 -07004241 // Not guarded by a lock.
4242 private final CopyOnWriteArrayList<ExternalStorageMountPolicy> mPolicies =
4243 new CopyOnWriteArrayList<>();
4244
Risanaec0ee72018-10-31 10:10:12 +09004245 @GuardedBy("mResetListeners")
4246 private final List<StorageManagerInternal.ResetListener> mResetListeners =
4247 new ArrayList<>();
4248
Svet Ganov6ee871e2015-07-10 14:29:33 -07004249 @Override
4250 public void addExternalStoragePolicy(ExternalStorageMountPolicy policy) {
4251 // No locking - CopyOnWriteArrayList
4252 mPolicies.add(policy);
4253 }
4254
4255 @Override
4256 public void onExternalStoragePolicyChanged(int uid, String packageName) {
4257 final int mountMode = getExternalStorageMountMode(uid, packageName);
4258 remountUidExternalStorage(uid, mountMode);
4259 }
4260
4261 @Override
4262 public int getExternalStorageMountMode(int uid, String packageName) {
Sudheer Shanka3a0df3b2018-12-12 12:43:43 -08004263 if (ENABLE_ISOLATED_STORAGE) {
4264 return getMountMode(uid, packageName);
4265 }
Sudheer Shankab1613982019-05-16 16:55:50 -07004266 try {
4267 if (packageName == null) {
4268 final String[] packagesForUid = mIPackageManager.getPackagesForUid(uid);
4269 packageName = packagesForUid[0];
4270 }
4271 } catch (RemoteException e) {
4272 // Should not happen - same process
4273 }
Svet Ganov6ee871e2015-07-10 14:29:33 -07004274 // No locking - CopyOnWriteArrayList
4275 int mountMode = Integer.MAX_VALUE;
4276 for (ExternalStorageMountPolicy policy : mPolicies) {
4277 final int policyMode = policy.getMountMode(uid, packageName);
4278 if (policyMode == Zygote.MOUNT_EXTERNAL_NONE) {
4279 return Zygote.MOUNT_EXTERNAL_NONE;
4280 }
4281 mountMode = Math.min(mountMode, policyMode);
4282 }
4283 if (mountMode == Integer.MAX_VALUE) {
4284 return Zygote.MOUNT_EXTERNAL_NONE;
4285 }
4286 return mountMode;
4287 }
4288
Risanaec0ee72018-10-31 10:10:12 +09004289 @Override
4290 public void addResetListener(StorageManagerInternal.ResetListener listener) {
4291 synchronized (mResetListeners) {
4292 mResetListeners.add(listener);
4293 }
4294 }
4295
4296 public void onReset(IVold vold) {
4297 synchronized (mResetListeners) {
4298 for (StorageManagerInternal.ResetListener listener : mResetListeners) {
4299 listener.onReset(vold);
4300 }
4301 }
4302 }
4303
Martijn Coenen73918202019-12-09 17:02:44 +01004304 @Override
4305 public void resetUser(int userId) {
4306 // TODO(b/145931219): ideally, we only reset storage for the user in question,
4307 // but for now, reset everything.
4308 mHandler.obtainMessage(H_RESET).sendToTarget();
4309 }
4310
Svet Ganov6ee871e2015-07-10 14:29:33 -07004311 public boolean hasExternalStorage(int uid, String packageName) {
Amith Yamasani2bd5cff2015-07-22 14:42:31 -07004312 // No need to check for system uid. This avoids a deadlock between
4313 // PackageManagerService and AppOpsService.
4314 if (uid == Process.SYSTEM_UID) {
4315 return true;
4316 }
Sudheer Shanka3a0df3b2018-12-12 12:43:43 -08004317 if (ENABLE_ISOLATED_STORAGE) {
4318 return getMountMode(uid, packageName) != Zygote.MOUNT_EXTERNAL_NONE;
4319 }
Svet Ganov6ee871e2015-07-10 14:29:33 -07004320 // No locking - CopyOnWriteArrayList
4321 for (ExternalStorageMountPolicy policy : mPolicies) {
4322 final boolean policyHasStorage = policy.hasExternalStorage(uid, packageName);
4323 if (!policyHasStorage) {
4324 return false;
4325 }
4326 }
4327 return true;
4328 }
Sudheer Shankab1613982019-05-16 16:55:50 -07004329
4330 public void onAppOpsChanged(int code, int uid,
4331 @Nullable String packageName, int mode) {
4332 if (mode == MODE_ALLOWED && (code == OP_READ_EXTERNAL_STORAGE
4333 || code == OP_WRITE_EXTERNAL_STORAGE
4334 || code == OP_REQUEST_INSTALL_PACKAGES)) {
4335 final long token = Binder.clearCallingIdentity();
4336 try {
4337 final UserManagerInternal userManagerInternal =
4338 LocalServices.getService(UserManagerInternal.class);
4339 if (userManagerInternal.isUserInitialized(UserHandle.getUserId(uid))) {
4340 onExternalStoragePolicyChanged(uid, packageName);
4341 }
4342 } finally {
4343 Binder.restoreCallingIdentity(token);
4344 }
4345 }
4346 }
Svet Ganov6ee871e2015-07-10 14:29:33 -07004347 }
Jeff Sharkeyfa23c5a2011-08-09 21:44:24 -07004348}