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