Songchun Fan | 3c82a30 | 2019-11-29 14:23:45 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2019 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | #pragma once |
| 18 | |
Yurii Zubrytskyi | 629051fd | 2020-04-17 23:13:47 -0700 | [diff] [blame] | 19 | #include <android/content/pm/BnDataLoaderStatusListener.h> |
Songchun Fan | 3c82a30 | 2019-11-29 14:23:45 -0800 | [diff] [blame] | 20 | #include <android/content/pm/DataLoaderParamsParcel.h> |
Alex Buynytskyy | cca2c11 | 2020-05-05 12:48:41 -0700 | [diff] [blame] | 21 | #include <android/content/pm/FileSystemControlParcel.h> |
Yurii Zubrytskyi | 629051fd | 2020-04-17 23:13:47 -0700 | [diff] [blame] | 22 | #include <android/content/pm/IDataLoaderStatusListener.h> |
| 23 | #include <android/os/incremental/BnIncrementalServiceConnector.h> |
Alex Buynytskyy | 8ef61ae | 2020-05-08 16:18:52 -0700 | [diff] [blame] | 24 | #include <android/os/incremental/BnStorageHealthListener.h> |
Songchun Fan | a709859 | 2020-09-03 11:45:53 -0700 | [diff] [blame] | 25 | #include <android/os/incremental/BnStorageLoadingProgressListener.h> |
Alex Buynytskyy | 8ef61ae | 2020-05-08 16:18:52 -0700 | [diff] [blame] | 26 | #include <android/os/incremental/StorageHealthCheckParams.h> |
Yurii Zubrytskyi | 629051fd | 2020-04-17 23:13:47 -0700 | [diff] [blame] | 27 | #include <binder/IAppOpsCallback.h> |
Songchun Fan | 3c82a30 | 2019-11-29 14:23:45 -0800 | [diff] [blame] | 28 | #include <utils/String16.h> |
| 29 | #include <utils/StrongPointer.h> |
Yurii Zubrytskyi | da20801 | 2020-04-07 15:35:21 -0700 | [diff] [blame] | 30 | #include <ziparchive/zip_archive.h> |
Songchun Fan | 3c82a30 | 2019-11-29 14:23:45 -0800 | [diff] [blame] | 31 | |
| 32 | #include <atomic> |
| 33 | #include <chrono> |
Yurii Zubrytskyi | da20801 | 2020-04-07 15:35:21 -0700 | [diff] [blame] | 34 | #include <condition_variable> |
| 35 | #include <functional> |
Songchun Fan | 3c82a30 | 2019-11-29 14:23:45 -0800 | [diff] [blame] | 36 | #include <limits> |
| 37 | #include <map> |
| 38 | #include <mutex> |
Alex Buynytskyy | 4760d8f | 2020-05-08 16:18:52 -0700 | [diff] [blame] | 39 | #include <set> |
Songchun Fan | 9b75308 | 2020-02-26 13:08:06 -0800 | [diff] [blame] | 40 | #include <span> |
Songchun Fan | 3c82a30 | 2019-11-29 14:23:45 -0800 | [diff] [blame] | 41 | #include <string> |
| 42 | #include <string_view> |
Yurii Zubrytskyi | da20801 | 2020-04-07 15:35:21 -0700 | [diff] [blame] | 43 | #include <thread> |
Songchun Fan | 3c82a30 | 2019-11-29 14:23:45 -0800 | [diff] [blame] | 44 | #include <unordered_map> |
Yurii Zubrytskyi | 629051fd | 2020-04-17 23:13:47 -0700 | [diff] [blame] | 45 | #include <unordered_set> |
Songchun Fan | 3c82a30 | 2019-11-29 14:23:45 -0800 | [diff] [blame] | 46 | #include <utility> |
| 47 | #include <vector> |
| 48 | |
| 49 | #include "ServiceWrappers.h" |
Songchun Fan | 3c82a30 | 2019-11-29 14:23:45 -0800 | [diff] [blame] | 50 | #include "incfs.h" |
| 51 | #include "path.h" |
| 52 | |
Songchun Fan | 3c82a30 | 2019-11-29 14:23:45 -0800 | [diff] [blame] | 53 | namespace android::incremental { |
| 54 | |
| 55 | using MountId = int; |
| 56 | using StorageId = int; |
Yurii Zubrytskyi | 4a25dfb | 2020-01-10 11:53:24 -0800 | [diff] [blame] | 57 | using FileId = incfs::FileId; |
Songchun Fan | 3c82a30 | 2019-11-29 14:23:45 -0800 | [diff] [blame] | 58 | using BlockIndex = incfs::BlockIndex; |
| 59 | using RawMetadata = incfs::RawMetadata; |
Songchun Fan | 3c82a30 | 2019-11-29 14:23:45 -0800 | [diff] [blame] | 60 | using Seconds = std::chrono::seconds; |
Alex Buynytskyy | 8ef61ae | 2020-05-08 16:18:52 -0700 | [diff] [blame] | 61 | using BootClockTsUs = uint64_t; |
Songchun Fan | 3c82a30 | 2019-11-29 14:23:45 -0800 | [diff] [blame] | 62 | |
Alex Buynytskyy | 0ea4ff4 | 2020-04-09 17:25:42 -0700 | [diff] [blame] | 63 | using IDataLoaderStatusListener = ::android::content::pm::IDataLoaderStatusListener; |
| 64 | using DataLoaderStatusListener = ::android::sp<IDataLoaderStatusListener>; |
Alex Buynytskyy | 04f7391 | 2020-02-10 08:34:18 -0800 | [diff] [blame] | 65 | |
Alex Buynytskyy | 8ef61ae | 2020-05-08 16:18:52 -0700 | [diff] [blame] | 66 | using StorageHealthCheckParams = ::android::os::incremental::StorageHealthCheckParams; |
| 67 | using IStorageHealthListener = ::android::os::incremental::IStorageHealthListener; |
| 68 | using StorageHealthListener = ::android::sp<IStorageHealthListener>; |
Songchun Fan | a709859 | 2020-09-03 11:45:53 -0700 | [diff] [blame] | 69 | using IStorageLoadingProgressListener = ::android::os::incremental::IStorageLoadingProgressListener; |
| 70 | using StorageLoadingProgressListener = ::android::sp<IStorageLoadingProgressListener>; |
Alex Buynytskyy | 8ef61ae | 2020-05-08 16:18:52 -0700 | [diff] [blame] | 71 | |
Yurii Zubrytskyi | 4a25dfb | 2020-01-10 11:53:24 -0800 | [diff] [blame] | 72 | class IncrementalService final { |
Songchun Fan | 3c82a30 | 2019-11-29 14:23:45 -0800 | [diff] [blame] | 73 | public: |
Yurii Zubrytskyi | 4a25dfb | 2020-01-10 11:53:24 -0800 | [diff] [blame] | 74 | explicit IncrementalService(ServiceManagerWrapper&& sm, std::string_view rootDir); |
Songchun Fan | 3c82a30 | 2019-11-29 14:23:45 -0800 | [diff] [blame] | 75 | |
| 76 | #pragma GCC diagnostic push |
| 77 | #pragma GCC diagnostic ignored "-Wnon-virtual-dtor" |
| 78 | ~IncrementalService(); |
| 79 | #pragma GCC diagnostic pop |
| 80 | |
| 81 | static constexpr StorageId kInvalidStorageId = -1; |
| 82 | static constexpr StorageId kMaxStorageId = std::numeric_limits<int>::max(); |
| 83 | |
Alex Buynytskyy | 8ef61ae | 2020-05-08 16:18:52 -0700 | [diff] [blame] | 84 | static constexpr BootClockTsUs kMaxBootClockTsUs = std::numeric_limits<BootClockTsUs>::max(); |
| 85 | |
Songchun Fan | 3c82a30 | 2019-11-29 14:23:45 -0800 | [diff] [blame] | 86 | enum CreateOptions { |
| 87 | TemporaryBind = 1, |
| 88 | PermanentBind = 2, |
| 89 | CreateNew = 4, |
| 90 | OpenExisting = 8, |
| 91 | |
| 92 | Default = TemporaryBind | CreateNew |
| 93 | }; |
| 94 | |
| 95 | enum class BindKind { |
| 96 | Temporary = 0, |
| 97 | Permanent = 1, |
| 98 | }; |
| 99 | |
Alex Buynytskyy | 3697d9e | 2020-06-06 20:15:58 -0700 | [diff] [blame] | 100 | enum StorageFlags { |
| 101 | ReadLogsEnabled = 1, |
| 102 | }; |
| 103 | |
Yurii Zubrytskyi | 4a25dfb | 2020-01-10 11:53:24 -0800 | [diff] [blame] | 104 | static FileId idFromMetadata(std::span<const uint8_t> metadata); |
| 105 | static inline FileId idFromMetadata(std::span<const char> metadata) { |
| 106 | return idFromMetadata({(const uint8_t*)metadata.data(), metadata.size()}); |
| 107 | } |
| 108 | |
Alex Buynytskyy | 18b07a4 | 2020-02-03 20:06:00 -0800 | [diff] [blame] | 109 | void onDump(int fd); |
| 110 | |
Alex Buynytskyy | 0ea4ff4 | 2020-04-09 17:25:42 -0700 | [diff] [blame] | 111 | void onSystemReady(); |
Songchun Fan | 3c82a30 | 2019-11-29 14:23:45 -0800 | [diff] [blame] | 112 | |
Yurii Zubrytskyi | 629051fd | 2020-04-17 23:13:47 -0700 | [diff] [blame] | 113 | StorageId createStorage(std::string_view mountPoint, |
| 114 | content::pm::DataLoaderParamsParcel&& dataLoaderParams, |
Alex Buynytskyy | 8ef61ae | 2020-05-08 16:18:52 -0700 | [diff] [blame] | 115 | CreateOptions options, const DataLoaderStatusListener& statusListener, |
| 116 | StorageHealthCheckParams&& healthCheckParams, |
| 117 | const StorageHealthListener& healthListener); |
Songchun Fan | 3c82a30 | 2019-11-29 14:23:45 -0800 | [diff] [blame] | 118 | StorageId createLinkedStorage(std::string_view mountPoint, StorageId linkedStorage, |
| 119 | CreateOptions options = CreateOptions::Default); |
Yurii Zubrytskyi | 4a25dfb | 2020-01-10 11:53:24 -0800 | [diff] [blame] | 120 | StorageId openStorage(std::string_view path); |
Songchun Fan | 3c82a30 | 2019-11-29 14:23:45 -0800 | [diff] [blame] | 121 | |
Yurii Zubrytskyi | 4a25dfb | 2020-01-10 11:53:24 -0800 | [diff] [blame] | 122 | int bind(StorageId storage, std::string_view source, std::string_view target, BindKind kind); |
Songchun Fan | 3c82a30 | 2019-11-29 14:23:45 -0800 | [diff] [blame] | 123 | int unbind(StorageId storage, std::string_view target); |
| 124 | void deleteStorage(StorageId storage); |
| 125 | |
Alex Buynytskyy | 3697d9e | 2020-06-06 20:15:58 -0700 | [diff] [blame] | 126 | void disableReadLogs(StorageId storage); |
Alex Buynytskyy | 5e860ba | 2020-03-31 15:30:21 -0700 | [diff] [blame] | 127 | int setStorageParams(StorageId storage, bool enableReadLogs); |
| 128 | |
Yurii Zubrytskyi | 4a25dfb | 2020-01-10 11:53:24 -0800 | [diff] [blame] | 129 | int makeFile(StorageId storage, std::string_view path, int mode, FileId id, |
Alex Buynytskyy | b39d13e | 2020-09-12 16:12:36 -0700 | [diff] [blame] | 130 | incfs::NewFileParams params, std::span<const uint8_t> data); |
Songchun Fan | 9610093 | 2020-02-03 19:20:58 -0800 | [diff] [blame] | 131 | int makeDir(StorageId storage, std::string_view path, int mode = 0755); |
| 132 | int makeDirs(StorageId storage, std::string_view path, int mode = 0755); |
Songchun Fan | 3c82a30 | 2019-11-29 14:23:45 -0800 | [diff] [blame] | 133 | |
Yurii Zubrytskyi | 4a25dfb | 2020-01-10 11:53:24 -0800 | [diff] [blame] | 134 | int link(StorageId sourceStorageId, std::string_view oldPath, StorageId destStorageId, |
| 135 | std::string_view newPath); |
| 136 | int unlink(StorageId storage, std::string_view path); |
Songchun Fan | 3c82a30 | 2019-11-29 14:23:45 -0800 | [diff] [blame] | 137 | |
Alex Buynytskyy | bc0a7e6 | 2020-08-25 12:45:22 -0700 | [diff] [blame] | 138 | int isFileFullyLoaded(StorageId storage, const std::string& path) const; |
Songchun Fan | 374f765 | 2020-08-20 08:40:29 -0700 | [diff] [blame] | 139 | float getLoadingProgress(StorageId storage) const; |
Songchun Fan | a709859 | 2020-09-03 11:45:53 -0700 | [diff] [blame] | 140 | bool registerLoadingProgressListener(StorageId storage, |
| 141 | const StorageLoadingProgressListener& progressListener); |
| 142 | bool unregisterLoadingProgressListener(StorageId storage); |
Songchun Fan | 2570ec0 | 2020-10-08 17:22:33 -0700 | [diff] [blame] | 143 | bool registerStorageHealthListener(StorageId storage, |
| 144 | StorageHealthCheckParams&& healthCheckParams, |
| 145 | const StorageHealthListener& healthListener); |
| 146 | void unregisterStorageHealthListener(StorageId storage); |
Yurii Zubrytskyi | 629051fd | 2020-04-17 23:13:47 -0700 | [diff] [blame] | 147 | RawMetadata getMetadata(StorageId storage, std::string_view path) const; |
Yurii Zubrytskyi | 4a25dfb | 2020-01-10 11:53:24 -0800 | [diff] [blame] | 148 | RawMetadata getMetadata(StorageId storage, FileId node) const; |
Songchun Fan | 3c82a30 | 2019-11-29 14:23:45 -0800 | [diff] [blame] | 149 | |
Songchun Fan | 3c82a30 | 2019-11-29 14:23:45 -0800 | [diff] [blame] | 150 | bool startLoading(StorageId storage) const; |
Yurii Zubrytskyi | da20801 | 2020-04-07 15:35:21 -0700 | [diff] [blame] | 151 | |
Songchun Fan | 0f8b6fe | 2020-02-05 17:41:25 -0800 | [diff] [blame] | 152 | bool configureNativeBinaries(StorageId storage, std::string_view apkFullPath, |
Songchun Fan | 14f6c3c | 2020-05-21 18:19:07 -0700 | [diff] [blame] | 153 | std::string_view libDirRelativePath, std::string_view abi, |
| 154 | bool extractNativeLibs); |
Yurii Zubrytskyi | da20801 | 2020-04-07 15:35:21 -0700 | [diff] [blame] | 155 | bool waitForNativeBinariesExtraction(StorageId storage); |
Alex Buynytskyy | 96e350b | 2020-04-02 20:03:47 -0700 | [diff] [blame] | 156 | |
Alex Buynytskyy | 96e350b | 2020-04-02 20:03:47 -0700 | [diff] [blame] | 157 | class AppOpsListener : public android::BnAppOpsCallback { |
| 158 | public: |
Yurii Zubrytskyi | da20801 | 2020-04-07 15:35:21 -0700 | [diff] [blame] | 159 | AppOpsListener(IncrementalService& incrementalService, std::string packageName) |
| 160 | : incrementalService(incrementalService), packageName(std::move(packageName)) {} |
Alex Buynytskyy | f415679 | 2020-04-07 14:26:55 -0700 | [diff] [blame] | 161 | void opChanged(int32_t op, const String16& packageName) final; |
Alex Buynytskyy | 96e350b | 2020-04-02 20:03:47 -0700 | [diff] [blame] | 162 | |
| 163 | private: |
| 164 | IncrementalService& incrementalService; |
| 165 | const std::string packageName; |
| 166 | }; |
| 167 | |
Yurii Zubrytskyi | 629051fd | 2020-04-17 23:13:47 -0700 | [diff] [blame] | 168 | class IncrementalServiceConnector : public os::incremental::BnIncrementalServiceConnector { |
Alex Buynytskyy | f415679 | 2020-04-07 14:26:55 -0700 | [diff] [blame] | 169 | public: |
| 170 | IncrementalServiceConnector(IncrementalService& incrementalService, int32_t storage) |
Alex Buynytskyy | 5f9e3a0 | 2020-04-07 21:13:41 -0700 | [diff] [blame] | 171 | : incrementalService(incrementalService), storage(storage) {} |
Alex Buynytskyy | f415679 | 2020-04-07 14:26:55 -0700 | [diff] [blame] | 172 | binder::Status setStorageParams(bool enableReadLogs, int32_t* _aidl_return) final; |
| 173 | |
| 174 | private: |
| 175 | IncrementalService& incrementalService; |
Alex Buynytskyy | 5f9e3a0 | 2020-04-07 21:13:41 -0700 | [diff] [blame] | 176 | int32_t const storage; |
Alex Buynytskyy | f415679 | 2020-04-07 14:26:55 -0700 | [diff] [blame] | 177 | }; |
| 178 | |
Songchun Fan | 3c82a30 | 2019-11-29 14:23:45 -0800 | [diff] [blame] | 179 | private: |
Alex Buynytskyy | 0ea4ff4 | 2020-04-09 17:25:42 -0700 | [diff] [blame] | 180 | struct IncFsMount; |
| 181 | |
Yurii Zubrytskyi | 629051fd | 2020-04-17 23:13:47 -0700 | [diff] [blame] | 182 | class DataLoaderStub : public content::pm::BnDataLoaderStatusListener { |
Alex Buynytskyy | 0ea4ff4 | 2020-04-09 17:25:42 -0700 | [diff] [blame] | 183 | public: |
Yurii Zubrytskyi | 629051fd | 2020-04-17 23:13:47 -0700 | [diff] [blame] | 184 | DataLoaderStub(IncrementalService& service, MountId id, |
| 185 | content::pm::DataLoaderParamsParcel&& params, |
| 186 | content::pm::FileSystemControlParcel&& control, |
Alex Buynytskyy | 8ef61ae | 2020-05-08 16:18:52 -0700 | [diff] [blame] | 187 | const DataLoaderStatusListener* statusListener, |
| 188 | StorageHealthCheckParams&& healthCheckParams, |
| 189 | const StorageHealthListener* healthListener, std::string&& healthPath); |
Alex Buynytskyy | 0ea4ff4 | 2020-04-09 17:25:42 -0700 | [diff] [blame] | 190 | ~DataLoaderStub(); |
Alex Buynytskyy | 9a54579a | 2020-04-17 15:34:47 -0700 | [diff] [blame] | 191 | // Cleans up the internal state and invalidates DataLoaderStub. Any subsequent calls will |
| 192 | // result in an error. |
| 193 | void cleanupResources(); |
Alex Buynytskyy | 0ea4ff4 | 2020-04-09 17:25:42 -0700 | [diff] [blame] | 194 | |
Alex Buynytskyy | ab65cb1 | 2020-04-17 10:01:47 -0700 | [diff] [blame] | 195 | bool requestCreate(); |
Alex Buynytskyy | 0b20266 | 2020-04-13 09:53:04 -0700 | [diff] [blame] | 196 | bool requestStart(); |
Alex Buynytskyy | ab65cb1 | 2020-04-17 10:01:47 -0700 | [diff] [blame] | 197 | bool requestDestroy(); |
Alex Buynytskyy | 0ea4ff4 | 2020-04-09 17:25:42 -0700 | [diff] [blame] | 198 | |
Alex Buynytskyy | ab65cb1 | 2020-04-17 10:01:47 -0700 | [diff] [blame] | 199 | void onDump(int fd); |
| 200 | |
Alex Buynytskyy | 4760d8f | 2020-05-08 16:18:52 -0700 | [diff] [blame] | 201 | MountId id() const { return mId.load(std::memory_order_relaxed); } |
Yurii Zubrytskyi | 629051fd | 2020-04-17 23:13:47 -0700 | [diff] [blame] | 202 | const content::pm::DataLoaderParamsParcel& params() const { return mParams; } |
Songchun Fan | 2570ec0 | 2020-10-08 17:22:33 -0700 | [diff] [blame] | 203 | void setHealthListener(StorageHealthCheckParams&& healthCheckParams, |
| 204 | const StorageHealthListener* healthListener); |
Alex Buynytskyy | 0ea4ff4 | 2020-04-09 17:25:42 -0700 | [diff] [blame] | 205 | |
| 206 | private: |
| 207 | binder::Status onStatusChanged(MountId mount, int newStatus) final; |
Songchun Fan | 3309398 | 2020-09-10 13:12:39 -0700 | [diff] [blame] | 208 | binder::Status reportStreamHealth(MountId mount, int newStatus) final; |
Alex Buynytskyy | 0ea4ff4 | 2020-04-09 17:25:42 -0700 | [diff] [blame] | 209 | |
Alex Buynytskyy | 0bdbccf | 2020-04-23 20:36:42 -0700 | [diff] [blame] | 210 | sp<content::pm::IDataLoader> getDataLoader(); |
Alex Buynytskyy | 9a54579a | 2020-04-17 15:34:47 -0700 | [diff] [blame] | 211 | |
Alex Buynytskyy | ea1390f | 2020-04-22 16:08:50 -0700 | [diff] [blame] | 212 | bool bind(); |
Alex Buynytskyy | ab65cb1 | 2020-04-17 10:01:47 -0700 | [diff] [blame] | 213 | bool create(); |
Alex Buynytskyy | 0b20266 | 2020-04-13 09:53:04 -0700 | [diff] [blame] | 214 | bool start(); |
Alex Buynytskyy | ab65cb1 | 2020-04-17 10:01:47 -0700 | [diff] [blame] | 215 | bool destroy(); |
| 216 | |
| 217 | bool setTargetStatus(int status); |
Alex Buynytskyy | 7e0a1a8 | 2020-04-27 17:06:10 -0700 | [diff] [blame] | 218 | void setTargetStatusLocked(int status); |
Alex Buynytskyy | ab65cb1 | 2020-04-17 10:01:47 -0700 | [diff] [blame] | 219 | |
| 220 | bool fsmStep(); |
Alex Buynytskyy | 4760d8f | 2020-05-08 16:18:52 -0700 | [diff] [blame] | 221 | bool fsmStep(int currentStatus, int targetStatus); |
Alex Buynytskyy | 0b20266 | 2020-04-13 09:53:04 -0700 | [diff] [blame] | 222 | |
Alex Buynytskyy | 4760d8f | 2020-05-08 16:18:52 -0700 | [diff] [blame] | 223 | void onHealthStatus(StorageHealthListener healthListener, int healthStatus); |
| 224 | void updateHealthStatus(bool baseline = false); |
| 225 | |
| 226 | bool isValid() const { return id() != kInvalidStorageId; } |
| 227 | |
| 228 | bool isHealthParamsValid() const; |
| 229 | |
| 230 | const incfs::UniqueControl& initializeHealthControl(); |
| 231 | void resetHealthControl(); |
| 232 | |
| 233 | BootClockTsUs getOldestPendingReadTs(); |
| 234 | |
| 235 | void registerForPendingReads(); |
| 236 | void unregisterFromPendingReads(); |
Alex Buynytskyy | d0855a3 | 2020-05-07 18:40:51 -0700 | [diff] [blame] | 237 | |
Alex Buynytskyy | 0ea4ff4 | 2020-04-09 17:25:42 -0700 | [diff] [blame] | 238 | IncrementalService& mService; |
Alex Buynytskyy | b0ea448 | 2020-05-04 18:39:58 -0700 | [diff] [blame] | 239 | |
| 240 | std::mutex mMutex; |
Alex Buynytskyy | 4760d8f | 2020-05-08 16:18:52 -0700 | [diff] [blame] | 241 | std::atomic<MountId> mId = kInvalidStorageId; |
Yurii Zubrytskyi | 629051fd | 2020-04-17 23:13:47 -0700 | [diff] [blame] | 242 | content::pm::DataLoaderParamsParcel mParams; |
| 243 | content::pm::FileSystemControlParcel mControl; |
Alex Buynytskyy | 8ef61ae | 2020-05-08 16:18:52 -0700 | [diff] [blame] | 244 | DataLoaderStatusListener mStatusListener; |
| 245 | StorageHealthListener mHealthListener; |
Alex Buynytskyy | 0ea4ff4 | 2020-04-09 17:25:42 -0700 | [diff] [blame] | 246 | |
Alex Buynytskyy | 0b20266 | 2020-04-13 09:53:04 -0700 | [diff] [blame] | 247 | std::condition_variable mStatusCondition; |
Yurii Zubrytskyi | 629051fd | 2020-04-17 23:13:47 -0700 | [diff] [blame] | 248 | int mCurrentStatus = content::pm::IDataLoaderStatusListener::DATA_LOADER_DESTROYED; |
| 249 | int mTargetStatus = content::pm::IDataLoaderStatusListener::DATA_LOADER_DESTROYED; |
Alex Buynytskyy | ab65cb1 | 2020-04-17 10:01:47 -0700 | [diff] [blame] | 250 | TimePoint mTargetStatusTs = {}; |
Alex Buynytskyy | cca2c11 | 2020-05-05 12:48:41 -0700 | [diff] [blame] | 251 | |
Alex Buynytskyy | d0855a3 | 2020-05-07 18:40:51 -0700 | [diff] [blame] | 252 | std::string mHealthPath; |
| 253 | incfs::UniqueControl mHealthControl; |
Alex Buynytskyy | 4760d8f | 2020-05-08 16:18:52 -0700 | [diff] [blame] | 254 | struct { |
| 255 | TimePoint userTs; |
| 256 | BootClockTsUs kernelTsUs; |
| 257 | } mHealthBase = {TimePoint::max(), kMaxBootClockTsUs}; |
| 258 | StorageHealthCheckParams mHealthCheckParams; |
Songchun Fan | 2570ec0 | 2020-10-08 17:22:33 -0700 | [diff] [blame] | 259 | int mStreamStatus = content::pm::IDataLoaderStatusListener::STREAM_HEALTHY; |
Alex Buynytskyy | 0ea4ff4 | 2020-04-09 17:25:42 -0700 | [diff] [blame] | 260 | }; |
| 261 | using DataLoaderStubPtr = sp<DataLoaderStub>; |
| 262 | |
Songchun Fan | 3c82a30 | 2019-11-29 14:23:45 -0800 | [diff] [blame] | 263 | struct IncFsMount { |
| 264 | struct Bind { |
| 265 | StorageId storage; |
| 266 | std::string savedFilename; |
| 267 | std::string sourceDir; |
| 268 | BindKind kind; |
| 269 | }; |
| 270 | |
| 271 | struct Storage { |
| 272 | std::string name; |
Songchun Fan | 3c82a30 | 2019-11-29 14:23:45 -0800 | [diff] [blame] | 273 | }; |
| 274 | |
Yurii Zubrytskyi | 4a25dfb | 2020-01-10 11:53:24 -0800 | [diff] [blame] | 275 | using Control = incfs::UniqueControl; |
Songchun Fan | 3c82a30 | 2019-11-29 14:23:45 -0800 | [diff] [blame] | 276 | |
Yurii Zubrytskyi | 629051fd | 2020-04-17 23:13:47 -0700 | [diff] [blame] | 277 | using BindMap = std::map<std::string, Bind, path::PathLess>; |
Songchun Fan | 3c82a30 | 2019-11-29 14:23:45 -0800 | [diff] [blame] | 278 | using StorageMap = std::unordered_map<StorageId, Storage>; |
| 279 | |
| 280 | mutable std::mutex lock; |
| 281 | const std::string root; |
| 282 | Control control; |
| 283 | /*const*/ MountId mountId; |
Alex Buynytskyy | 3697d9e | 2020-06-06 20:15:58 -0700 | [diff] [blame] | 284 | int32_t flags = StorageFlags::ReadLogsEnabled; |
Songchun Fan | 3c82a30 | 2019-11-29 14:23:45 -0800 | [diff] [blame] | 285 | StorageMap storages; |
| 286 | BindMap bindPoints; |
Alex Buynytskyy | 0ea4ff4 | 2020-04-09 17:25:42 -0700 | [diff] [blame] | 287 | DataLoaderStubPtr dataLoaderStub; |
Songchun Fan | 3c82a30 | 2019-11-29 14:23:45 -0800 | [diff] [blame] | 288 | std::atomic<int> nextStorageDirNo{0}; |
Songchun Fan | 3c82a30 | 2019-11-29 14:23:45 -0800 | [diff] [blame] | 289 | const IncrementalService& incrementalService; |
| 290 | |
| 291 | IncFsMount(std::string root, MountId mountId, Control control, |
| 292 | const IncrementalService& incrementalService) |
| 293 | : root(std::move(root)), |
| 294 | control(std::move(control)), |
| 295 | mountId(mountId), |
Alex Buynytskyy | 1d89216 | 2020-04-03 23:00:19 -0700 | [diff] [blame] | 296 | incrementalService(incrementalService) {} |
Songchun Fan | 3c82a30 | 2019-11-29 14:23:45 -0800 | [diff] [blame] | 297 | IncFsMount(IncFsMount&&) = delete; |
| 298 | IncFsMount& operator=(IncFsMount&&) = delete; |
| 299 | ~IncFsMount(); |
| 300 | |
| 301 | StorageMap::iterator makeStorage(StorageId id); |
| 302 | |
Alex Buynytskyy | 3697d9e | 2020-06-06 20:15:58 -0700 | [diff] [blame] | 303 | void disableReadLogs() { flags &= ~StorageFlags::ReadLogsEnabled; } |
| 304 | int32_t readLogsEnabled() const { return (flags & StorageFlags::ReadLogsEnabled); } |
| 305 | |
Songchun Fan | 3c82a30 | 2019-11-29 14:23:45 -0800 | [diff] [blame] | 306 | static void cleanupFilesystem(std::string_view root); |
| 307 | }; |
| 308 | |
| 309 | using IfsMountPtr = std::shared_ptr<IncFsMount>; |
| 310 | using MountMap = std::unordered_map<MountId, IfsMountPtr>; |
| 311 | using BindPathMap = std::map<std::string, IncFsMount::BindMap::iterator, path::PathLess>; |
| 312 | |
Yurii Zubrytskyi | 629051fd | 2020-04-17 23:13:47 -0700 | [diff] [blame] | 313 | static bool perfLoggingEnabled(); |
| 314 | |
| 315 | std::unordered_set<std::string_view> adoptMountedInstances(); |
| 316 | void mountExistingImages(const std::unordered_set<std::string_view>& mountedRootNames); |
Yurii Zubrytskyi | 107ae35 | 2020-04-03 13:12:51 -0700 | [diff] [blame] | 317 | bool mountExistingImage(std::string_view root); |
Songchun Fan | 3c82a30 | 2019-11-29 14:23:45 -0800 | [diff] [blame] | 318 | |
| 319 | IfsMountPtr getIfs(StorageId storage) const; |
| 320 | const IfsMountPtr& getIfsLocked(StorageId storage) const; |
Yurii Zubrytskyi | 4a25dfb | 2020-01-10 11:53:24 -0800 | [diff] [blame] | 321 | int addBindMount(IncFsMount& ifs, StorageId storage, std::string_view storageRoot, |
| 322 | std::string&& source, std::string&& target, BindKind kind, |
| 323 | std::unique_lock<std::mutex>& mainLock); |
Songchun Fan | 3c82a30 | 2019-11-29 14:23:45 -0800 | [diff] [blame] | 324 | |
| 325 | int addBindMountWithMd(IncFsMount& ifs, StorageId storage, std::string&& metadataName, |
Yurii Zubrytskyi | 4a25dfb | 2020-01-10 11:53:24 -0800 | [diff] [blame] | 326 | std::string&& source, std::string&& target, BindKind kind, |
Songchun Fan | 3c82a30 | 2019-11-29 14:23:45 -0800 | [diff] [blame] | 327 | std::unique_lock<std::mutex>& mainLock); |
| 328 | |
Yurii Zubrytskyi | 629051fd | 2020-04-17 23:13:47 -0700 | [diff] [blame] | 329 | void addBindMountRecordLocked(IncFsMount& ifs, StorageId storage, std::string&& metadataName, |
| 330 | std::string&& source, std::string&& target, BindKind kind); |
| 331 | |
| 332 | DataLoaderStubPtr prepareDataLoader(IncFsMount& ifs, |
| 333 | content::pm::DataLoaderParamsParcel&& params, |
Alex Buynytskyy | 8ef61ae | 2020-05-08 16:18:52 -0700 | [diff] [blame] | 334 | const DataLoaderStatusListener* statusListener = nullptr, |
| 335 | StorageHealthCheckParams&& healthCheckParams = {}, |
| 336 | const StorageHealthListener* healthListener = nullptr); |
Yurii Zubrytskyi | 629051fd | 2020-04-17 23:13:47 -0700 | [diff] [blame] | 337 | void prepareDataLoaderLocked(IncFsMount& ifs, content::pm::DataLoaderParamsParcel&& params, |
Alex Buynytskyy | 8ef61ae | 2020-05-08 16:18:52 -0700 | [diff] [blame] | 338 | const DataLoaderStatusListener* statusListener = nullptr, |
| 339 | StorageHealthCheckParams&& healthCheckParams = {}, |
| 340 | const StorageHealthListener* healthListener = nullptr); |
Alex Buynytskyy | bf1c063 | 2020-03-10 15:49:29 -0700 | [diff] [blame] | 341 | |
Songchun Fan | 3c82a30 | 2019-11-29 14:23:45 -0800 | [diff] [blame] | 342 | BindPathMap::const_iterator findStorageLocked(std::string_view path) const; |
| 343 | StorageId findStorageId(std::string_view path) const; |
| 344 | |
| 345 | void deleteStorage(IncFsMount& ifs); |
| 346 | void deleteStorageLocked(IncFsMount& ifs, std::unique_lock<std::mutex>&& ifsLock); |
| 347 | MountMap::iterator getStorageSlotLocked(); |
Yurii Zubrytskyi | efebb45 | 2020-04-22 13:59:06 -0700 | [diff] [blame] | 348 | std::string normalizePathToStorage(const IncFsMount& incfs, StorageId storage, |
Yurii Zubrytskyi | 629051fd | 2020-04-17 23:13:47 -0700 | [diff] [blame] | 349 | std::string_view path) const; |
Yurii Zubrytskyi | efebb45 | 2020-04-22 13:59:06 -0700 | [diff] [blame] | 350 | std::string normalizePathToStorageLocked(const IncFsMount& incfs, |
| 351 | IncFsMount::StorageMap::const_iterator storageIt, |
Yurii Zubrytskyi | 629051fd | 2020-04-17 23:13:47 -0700 | [diff] [blame] | 352 | std::string_view path) const; |
Yurii Zubrytskyi | efebb45 | 2020-04-22 13:59:06 -0700 | [diff] [blame] | 353 | int makeDirs(const IncFsMount& ifs, StorageId storageId, std::string_view path, int mode); |
Alex Buynytskyy | 1d89216 | 2020-04-03 23:00:19 -0700 | [diff] [blame] | 354 | binder::Status applyStorageParams(IncFsMount& ifs, bool enableReadLogs); |
Alex Buynytskyy | 96e350b | 2020-04-02 20:03:47 -0700 | [diff] [blame] | 355 | |
Alex Buynytskyy | bc0a7e6 | 2020-08-25 12:45:22 -0700 | [diff] [blame] | 356 | int isFileFullyLoadedFromPath(const IncFsMount& ifs, std::string_view filePath) const; |
Songchun Fan | 374f765 | 2020-08-20 08:40:29 -0700 | [diff] [blame] | 357 | float getLoadingProgressFromPath(const IncFsMount& ifs, std::string_view path) const; |
| 358 | |
Alex Buynytskyy | b39d13e | 2020-09-12 16:12:36 -0700 | [diff] [blame] | 359 | int setFileContent(const IfsMountPtr& ifs, const incfs::FileId& fileId, |
| 360 | std::string_view debugFilePath, std::span<const uint8_t> data) const; |
| 361 | |
Alex Buynytskyy | 96e350b | 2020-04-02 20:03:47 -0700 | [diff] [blame] | 362 | void registerAppOpsCallback(const std::string& packageName); |
Alex Buynytskyy | 1d89216 | 2020-04-03 23:00:19 -0700 | [diff] [blame] | 363 | bool unregisterAppOpsCallback(const std::string& packageName); |
| 364 | void onAppOpChanged(const std::string& packageName); |
Alex Buynytskyy | 96e350b | 2020-04-02 20:03:47 -0700 | [diff] [blame] | 365 | |
Yurii Zubrytskyi | da20801 | 2020-04-07 15:35:21 -0700 | [diff] [blame] | 366 | void runJobProcessing(); |
| 367 | void extractZipFile(const IfsMountPtr& ifs, ZipArchiveHandle zipFile, ZipEntry& entry, |
Alex Buynytskyy | b39d13e | 2020-09-12 16:12:36 -0700 | [diff] [blame] | 368 | const incfs::FileId& libFileId, std::string_view debugLibPath, |
Yurii Zubrytskyi | da20801 | 2020-04-07 15:35:21 -0700 | [diff] [blame] | 369 | Clock::time_point scheduledTs); |
| 370 | |
Alex Buynytskyy | cca2c11 | 2020-05-05 12:48:41 -0700 | [diff] [blame] | 371 | void runCmdLooper(); |
| 372 | |
Songchun Fan | a709859 | 2020-09-03 11:45:53 -0700 | [diff] [blame] | 373 | bool addTimedJob(TimedQueueWrapper& timedQueue, MountId id, Milliseconds after, Job what); |
| 374 | bool removeTimedJobs(TimedQueueWrapper& timedQueue, MountId id); |
| 375 | bool updateLoadingProgress(int32_t storageId, |
| 376 | const StorageLoadingProgressListener& progressListener); |
Alex Buynytskyy | 4760d8f | 2020-05-08 16:18:52 -0700 | [diff] [blame] | 377 | |
Yurii Zubrytskyi | da20801 | 2020-04-07 15:35:21 -0700 | [diff] [blame] | 378 | private: |
Yurii Zubrytskyi | 8632140 | 2020-04-09 19:22:30 -0700 | [diff] [blame] | 379 | const std::unique_ptr<VoldServiceWrapper> mVold; |
| 380 | const std::unique_ptr<DataLoaderManagerWrapper> mDataLoaderManager; |
| 381 | const std::unique_ptr<IncFsWrapper> mIncFs; |
| 382 | const std::unique_ptr<AppOpsManagerWrapper> mAppOpsManager; |
| 383 | const std::unique_ptr<JniWrapper> mJni; |
Alex Buynytskyy | cca2c11 | 2020-05-05 12:48:41 -0700 | [diff] [blame] | 384 | const std::unique_ptr<LooperWrapper> mLooper; |
Alex Buynytskyy | 46d3ddb | 2020-05-29 12:05:05 -0700 | [diff] [blame] | 385 | const std::unique_ptr<TimedQueueWrapper> mTimedQueue; |
Songchun Fan | a709859 | 2020-09-03 11:45:53 -0700 | [diff] [blame] | 386 | const std::unique_ptr<TimedQueueWrapper> mProgressUpdateJobQueue; |
Songchun Fan | 374f765 | 2020-08-20 08:40:29 -0700 | [diff] [blame] | 387 | const std::unique_ptr<FsWrapper> mFs; |
Songchun Fan | 3c82a30 | 2019-11-29 14:23:45 -0800 | [diff] [blame] | 388 | const std::string mIncrementalDir; |
| 389 | |
| 390 | mutable std::mutex mLock; |
| 391 | mutable std::mutex mMountOperationLock; |
| 392 | MountMap mMounts; |
| 393 | BindPathMap mBindsByPath; |
| 394 | |
Alex Buynytskyy | 96e350b | 2020-04-02 20:03:47 -0700 | [diff] [blame] | 395 | std::mutex mCallbacksLock; |
Alex Buynytskyy | 1d89216 | 2020-04-03 23:00:19 -0700 | [diff] [blame] | 396 | std::map<std::string, sp<AppOpsListener>> mCallbackRegistered; |
Alex Buynytskyy | 96e350b | 2020-04-02 20:03:47 -0700 | [diff] [blame] | 397 | |
Songchun Fan | 3c82a30 | 2019-11-29 14:23:45 -0800 | [diff] [blame] | 398 | std::atomic_bool mSystemReady = false; |
| 399 | StorageId mNextId = 0; |
Yurii Zubrytskyi | da20801 | 2020-04-07 15:35:21 -0700 | [diff] [blame] | 400 | |
Alex Buynytskyy | cca2c11 | 2020-05-05 12:48:41 -0700 | [diff] [blame] | 401 | std::atomic_bool mRunning{true}; |
| 402 | |
Yurii Zubrytskyi | 721ac4d | 2020-04-13 11:34:32 -0700 | [diff] [blame] | 403 | std::unordered_map<MountId, std::vector<Job>> mJobQueue; |
| 404 | MountId mPendingJobsMount = kInvalidStorageId; |
Yurii Zubrytskyi | da20801 | 2020-04-07 15:35:21 -0700 | [diff] [blame] | 405 | std::condition_variable mJobCondition; |
| 406 | std::mutex mJobMutex; |
| 407 | std::thread mJobProcessor; |
Alex Buynytskyy | cca2c11 | 2020-05-05 12:48:41 -0700 | [diff] [blame] | 408 | |
| 409 | std::thread mCmdLooperThread; |
Songchun Fan | 3c82a30 | 2019-11-29 14:23:45 -0800 | [diff] [blame] | 410 | }; |
| 411 | |
| 412 | } // namespace android::incremental |