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