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