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