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 | |
| 19 | #include <android-base/strings.h> |
| 20 | #include <android-base/unique_fd.h> |
Songchun Fan | 3c82a30 | 2019-11-29 14:23:45 -0800 | [diff] [blame] | 21 | #include <android/content/pm/DataLoaderParamsParcel.h> |
| 22 | #include <binder/IServiceManager.h> |
| 23 | #include <utils/String16.h> |
| 24 | #include <utils/StrongPointer.h> |
| 25 | #include <utils/Vector.h> |
| 26 | |
| 27 | #include <atomic> |
| 28 | #include <chrono> |
| 29 | #include <future> |
| 30 | #include <limits> |
| 31 | #include <map> |
| 32 | #include <mutex> |
Songchun Fan | 9b75308 | 2020-02-26 13:08:06 -0800 | [diff] [blame] | 33 | #include <span> |
Songchun Fan | 3c82a30 | 2019-11-29 14:23:45 -0800 | [diff] [blame] | 34 | #include <string> |
| 35 | #include <string_view> |
| 36 | #include <unordered_map> |
| 37 | #include <utility> |
| 38 | #include <vector> |
| 39 | |
| 40 | #include "ServiceWrappers.h" |
| 41 | #include "android/content/pm/BnDataLoaderStatusListener.h" |
Alex Buynytskyy | f415679 | 2020-04-07 14:26:55 -0700 | [diff] [blame] | 42 | #include "android/os/incremental/BnIncrementalServiceConnector.h" |
Songchun Fan | 3c82a30 | 2019-11-29 14:23:45 -0800 | [diff] [blame] | 43 | #include "incfs.h" |
| 44 | #include "path.h" |
| 45 | |
| 46 | using namespace android::os::incremental; |
| 47 | |
| 48 | namespace android::os { |
| 49 | class IVold; |
| 50 | } |
| 51 | |
| 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; |
| 62 | |
Alex Buynytskyy | 04f7391 | 2020-02-10 08:34:18 -0800 | [diff] [blame] | 63 | using DataLoaderStatusListener = ::android::sp<::android::content::pm::IDataLoaderStatusListener>; |
| 64 | |
Yurii Zubrytskyi | 4a25dfb | 2020-01-10 11:53:24 -0800 | [diff] [blame] | 65 | class IncrementalService final { |
Songchun Fan | 3c82a30 | 2019-11-29 14:23:45 -0800 | [diff] [blame] | 66 | public: |
Yurii Zubrytskyi | 4a25dfb | 2020-01-10 11:53:24 -0800 | [diff] [blame] | 67 | explicit IncrementalService(ServiceManagerWrapper&& sm, std::string_view rootDir); |
Songchun Fan | 3c82a30 | 2019-11-29 14:23:45 -0800 | [diff] [blame] | 68 | |
| 69 | #pragma GCC diagnostic push |
| 70 | #pragma GCC diagnostic ignored "-Wnon-virtual-dtor" |
| 71 | ~IncrementalService(); |
| 72 | #pragma GCC diagnostic pop |
| 73 | |
| 74 | static constexpr StorageId kInvalidStorageId = -1; |
| 75 | static constexpr StorageId kMaxStorageId = std::numeric_limits<int>::max(); |
| 76 | |
| 77 | enum CreateOptions { |
| 78 | TemporaryBind = 1, |
| 79 | PermanentBind = 2, |
| 80 | CreateNew = 4, |
| 81 | OpenExisting = 8, |
| 82 | |
| 83 | Default = TemporaryBind | CreateNew |
| 84 | }; |
| 85 | |
| 86 | enum class BindKind { |
| 87 | Temporary = 0, |
| 88 | Permanent = 1, |
| 89 | }; |
| 90 | |
Yurii Zubrytskyi | 4a25dfb | 2020-01-10 11:53:24 -0800 | [diff] [blame] | 91 | static FileId idFromMetadata(std::span<const uint8_t> metadata); |
| 92 | static inline FileId idFromMetadata(std::span<const char> metadata) { |
| 93 | return idFromMetadata({(const uint8_t*)metadata.data(), metadata.size()}); |
| 94 | } |
| 95 | |
Alex Buynytskyy | 18b07a4 | 2020-02-03 20:06:00 -0800 | [diff] [blame] | 96 | void onDump(int fd); |
| 97 | |
Songchun Fan | 3c82a30 | 2019-11-29 14:23:45 -0800 | [diff] [blame] | 98 | std::optional<std::future<void>> onSystemReady(); |
| 99 | |
Songchun Fan | 9b75308 | 2020-02-26 13:08:06 -0800 | [diff] [blame] | 100 | StorageId createStorage(std::string_view mountPoint, DataLoaderParamsParcel&& dataLoaderParams, |
Alex Buynytskyy | 04f7391 | 2020-02-10 08:34:18 -0800 | [diff] [blame] | 101 | const DataLoaderStatusListener& dataLoaderStatusListener, |
Songchun Fan | 3c82a30 | 2019-11-29 14:23:45 -0800 | [diff] [blame] | 102 | CreateOptions options = CreateOptions::Default); |
| 103 | StorageId createLinkedStorage(std::string_view mountPoint, StorageId linkedStorage, |
| 104 | CreateOptions options = CreateOptions::Default); |
Yurii Zubrytskyi | 4a25dfb | 2020-01-10 11:53:24 -0800 | [diff] [blame] | 105 | StorageId openStorage(std::string_view path); |
Songchun Fan | 3c82a30 | 2019-11-29 14:23:45 -0800 | [diff] [blame] | 106 | |
Yurii Zubrytskyi | 4a25dfb | 2020-01-10 11:53:24 -0800 | [diff] [blame] | 107 | FileId nodeFor(StorageId storage, std::string_view path) const; |
| 108 | std::pair<FileId, std::string_view> parentAndNameFor(StorageId storage, |
| 109 | std::string_view path) const; |
Songchun Fan | 3c82a30 | 2019-11-29 14:23:45 -0800 | [diff] [blame] | 110 | |
Yurii Zubrytskyi | 4a25dfb | 2020-01-10 11:53:24 -0800 | [diff] [blame] | 111 | 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] | 112 | int unbind(StorageId storage, std::string_view target); |
| 113 | void deleteStorage(StorageId storage); |
| 114 | |
Alex Buynytskyy | 5e860ba | 2020-03-31 15:30:21 -0700 | [diff] [blame] | 115 | int setStorageParams(StorageId storage, bool enableReadLogs); |
| 116 | |
Yurii Zubrytskyi | 4a25dfb | 2020-01-10 11:53:24 -0800 | [diff] [blame] | 117 | int makeFile(StorageId storage, std::string_view path, int mode, FileId id, |
| 118 | incfs::NewFileParams params); |
Songchun Fan | 9610093 | 2020-02-03 19:20:58 -0800 | [diff] [blame] | 119 | int makeDir(StorageId storage, std::string_view path, int mode = 0755); |
| 120 | int makeDirs(StorageId storage, std::string_view path, int mode = 0755); |
Songchun Fan | 3c82a30 | 2019-11-29 14:23:45 -0800 | [diff] [blame] | 121 | |
Yurii Zubrytskyi | 4a25dfb | 2020-01-10 11:53:24 -0800 | [diff] [blame] | 122 | int link(StorageId sourceStorageId, std::string_view oldPath, StorageId destStorageId, |
| 123 | std::string_view newPath); |
| 124 | int unlink(StorageId storage, std::string_view path); |
Songchun Fan | 3c82a30 | 2019-11-29 14:23:45 -0800 | [diff] [blame] | 125 | |
Yurii Zubrytskyi | 4a25dfb | 2020-01-10 11:53:24 -0800 | [diff] [blame] | 126 | bool isRangeLoaded(StorageId storage, FileId file, std::pair<BlockIndex, BlockIndex> range) { |
Songchun Fan | 3c82a30 | 2019-11-29 14:23:45 -0800 | [diff] [blame] | 127 | return false; |
| 128 | } |
| 129 | |
Yurii Zubrytskyi | 4a25dfb | 2020-01-10 11:53:24 -0800 | [diff] [blame] | 130 | RawMetadata getMetadata(StorageId storage, FileId node) const; |
Songchun Fan | 3c82a30 | 2019-11-29 14:23:45 -0800 | [diff] [blame] | 131 | |
| 132 | std::vector<std::string> listFiles(StorageId storage) const; |
| 133 | bool startLoading(StorageId storage) const; |
Songchun Fan | 0f8b6fe | 2020-02-05 17:41:25 -0800 | [diff] [blame] | 134 | bool configureNativeBinaries(StorageId storage, std::string_view apkFullPath, |
| 135 | std::string_view libDirRelativePath, std::string_view abi); |
Alex Buynytskyy | 96e350b | 2020-04-02 20:03:47 -0700 | [diff] [blame] | 136 | |
Songchun Fan | 3c82a30 | 2019-11-29 14:23:45 -0800 | [diff] [blame] | 137 | class IncrementalDataLoaderListener : public android::content::pm::BnDataLoaderStatusListener { |
| 138 | public: |
Songchun Fan | 9b75308 | 2020-02-26 13:08:06 -0800 | [diff] [blame] | 139 | IncrementalDataLoaderListener(IncrementalService& incrementalService, |
| 140 | DataLoaderStatusListener externalListener) |
Alex Buynytskyy | 04f7391 | 2020-02-10 08:34:18 -0800 | [diff] [blame] | 141 | : incrementalService(incrementalService), externalListener(externalListener) {} |
Songchun Fan | 3c82a30 | 2019-11-29 14:23:45 -0800 | [diff] [blame] | 142 | // Callbacks interface |
Alex Buynytskyy | f415679 | 2020-04-07 14:26:55 -0700 | [diff] [blame] | 143 | binder::Status onStatusChanged(MountId mount, int newStatus) final; |
Songchun Fan | 3c82a30 | 2019-11-29 14:23:45 -0800 | [diff] [blame] | 144 | |
| 145 | private: |
| 146 | IncrementalService& incrementalService; |
Alex Buynytskyy | 04f7391 | 2020-02-10 08:34:18 -0800 | [diff] [blame] | 147 | DataLoaderStatusListener externalListener; |
Songchun Fan | 3c82a30 | 2019-11-29 14:23:45 -0800 | [diff] [blame] | 148 | }; |
| 149 | |
Alex Buynytskyy | 96e350b | 2020-04-02 20:03:47 -0700 | [diff] [blame] | 150 | class AppOpsListener : public android::BnAppOpsCallback { |
| 151 | public: |
| 152 | AppOpsListener(IncrementalService& incrementalService, std::string packageName) : incrementalService(incrementalService), packageName(std::move(packageName)) {} |
Alex Buynytskyy | f415679 | 2020-04-07 14:26:55 -0700 | [diff] [blame] | 153 | void opChanged(int32_t op, const String16& packageName) final; |
Alex Buynytskyy | 96e350b | 2020-04-02 20:03:47 -0700 | [diff] [blame] | 154 | |
| 155 | private: |
| 156 | IncrementalService& incrementalService; |
| 157 | const std::string packageName; |
| 158 | }; |
| 159 | |
Alex Buynytskyy | f415679 | 2020-04-07 14:26:55 -0700 | [diff] [blame] | 160 | class IncrementalServiceConnector : public BnIncrementalServiceConnector { |
| 161 | public: |
| 162 | IncrementalServiceConnector(IncrementalService& incrementalService, int32_t storage) |
Alex Buynytskyy | 5f9e3a0 | 2020-04-07 21:13:41 -0700 | [diff] [blame^] | 163 | : incrementalService(incrementalService), storage(storage) {} |
Alex Buynytskyy | f415679 | 2020-04-07 14:26:55 -0700 | [diff] [blame] | 164 | binder::Status setStorageParams(bool enableReadLogs, int32_t* _aidl_return) final; |
| 165 | |
| 166 | private: |
| 167 | IncrementalService& incrementalService; |
Alex Buynytskyy | 5f9e3a0 | 2020-04-07 21:13:41 -0700 | [diff] [blame^] | 168 | int32_t const storage; |
Alex Buynytskyy | f415679 | 2020-04-07 14:26:55 -0700 | [diff] [blame] | 169 | }; |
| 170 | |
Songchun Fan | 3c82a30 | 2019-11-29 14:23:45 -0800 | [diff] [blame] | 171 | private: |
| 172 | struct IncFsMount { |
| 173 | struct Bind { |
| 174 | StorageId storage; |
| 175 | std::string savedFilename; |
| 176 | std::string sourceDir; |
| 177 | BindKind kind; |
| 178 | }; |
| 179 | |
| 180 | struct Storage { |
| 181 | std::string name; |
Songchun Fan | 3c82a30 | 2019-11-29 14:23:45 -0800 | [diff] [blame] | 182 | }; |
| 183 | |
Yurii Zubrytskyi | 4a25dfb | 2020-01-10 11:53:24 -0800 | [diff] [blame] | 184 | using Control = incfs::UniqueControl; |
Songchun Fan | 3c82a30 | 2019-11-29 14:23:45 -0800 | [diff] [blame] | 185 | |
| 186 | using BindMap = std::map<std::string, Bind>; |
| 187 | using StorageMap = std::unordered_map<StorageId, Storage>; |
| 188 | |
| 189 | mutable std::mutex lock; |
| 190 | const std::string root; |
| 191 | Control control; |
| 192 | /*const*/ MountId mountId; |
| 193 | StorageMap storages; |
| 194 | BindMap bindPoints; |
Alex Buynytskyy | 96e350b | 2020-04-02 20:03:47 -0700 | [diff] [blame] | 195 | DataLoaderParamsParcel dataLoaderParams; |
Songchun Fan | 3c82a30 | 2019-11-29 14:23:45 -0800 | [diff] [blame] | 196 | std::atomic<int> nextStorageDirNo{0}; |
| 197 | std::atomic<int> dataLoaderStatus = -1; |
Alex Buynytskyy | bf1c063 | 2020-03-10 15:49:29 -0700 | [diff] [blame] | 198 | bool dataLoaderStartRequested = false; |
Songchun Fan | 3c82a30 | 2019-11-29 14:23:45 -0800 | [diff] [blame] | 199 | const IncrementalService& incrementalService; |
| 200 | |
| 201 | IncFsMount(std::string root, MountId mountId, Control control, |
| 202 | const IncrementalService& incrementalService) |
| 203 | : root(std::move(root)), |
| 204 | control(std::move(control)), |
| 205 | mountId(mountId), |
Alex Buynytskyy | 1d89216 | 2020-04-03 23:00:19 -0700 | [diff] [blame] | 206 | incrementalService(incrementalService) {} |
Songchun Fan | 3c82a30 | 2019-11-29 14:23:45 -0800 | [diff] [blame] | 207 | IncFsMount(IncFsMount&&) = delete; |
| 208 | IncFsMount& operator=(IncFsMount&&) = delete; |
| 209 | ~IncFsMount(); |
| 210 | |
| 211 | StorageMap::iterator makeStorage(StorageId id); |
| 212 | |
| 213 | static void cleanupFilesystem(std::string_view root); |
| 214 | }; |
| 215 | |
| 216 | using IfsMountPtr = std::shared_ptr<IncFsMount>; |
| 217 | using MountMap = std::unordered_map<MountId, IfsMountPtr>; |
| 218 | using BindPathMap = std::map<std::string, IncFsMount::BindMap::iterator, path::PathLess>; |
| 219 | |
| 220 | void mountExistingImages(); |
Yurii Zubrytskyi | 107ae35 | 2020-04-03 13:12:51 -0700 | [diff] [blame] | 221 | bool mountExistingImage(std::string_view root); |
Songchun Fan | 3c82a30 | 2019-11-29 14:23:45 -0800 | [diff] [blame] | 222 | |
| 223 | IfsMountPtr getIfs(StorageId storage) const; |
| 224 | const IfsMountPtr& getIfsLocked(StorageId storage) const; |
Yurii Zubrytskyi | 4a25dfb | 2020-01-10 11:53:24 -0800 | [diff] [blame] | 225 | int addBindMount(IncFsMount& ifs, StorageId storage, std::string_view storageRoot, |
| 226 | std::string&& source, std::string&& target, BindKind kind, |
| 227 | std::unique_lock<std::mutex>& mainLock); |
Songchun Fan | 3c82a30 | 2019-11-29 14:23:45 -0800 | [diff] [blame] | 228 | |
| 229 | int addBindMountWithMd(IncFsMount& ifs, StorageId storage, std::string&& metadataName, |
Yurii Zubrytskyi | 4a25dfb | 2020-01-10 11:53:24 -0800 | [diff] [blame] | 230 | std::string&& source, std::string&& target, BindKind kind, |
Songchun Fan | 3c82a30 | 2019-11-29 14:23:45 -0800 | [diff] [blame] | 231 | std::unique_lock<std::mutex>& mainLock); |
| 232 | |
Alex Buynytskyy | 96e350b | 2020-04-02 20:03:47 -0700 | [diff] [blame] | 233 | bool prepareDataLoader(IncFsMount& ifs, const DataLoaderStatusListener* externalListener = nullptr); |
Alex Buynytskyy | bf1c063 | 2020-03-10 15:49:29 -0700 | [diff] [blame] | 234 | bool startDataLoader(MountId mountId) const; |
| 235 | |
Songchun Fan | 3c82a30 | 2019-11-29 14:23:45 -0800 | [diff] [blame] | 236 | BindPathMap::const_iterator findStorageLocked(std::string_view path) const; |
| 237 | StorageId findStorageId(std::string_view path) const; |
| 238 | |
| 239 | void deleteStorage(IncFsMount& ifs); |
| 240 | void deleteStorageLocked(IncFsMount& ifs, std::unique_lock<std::mutex>&& ifsLock); |
| 241 | MountMap::iterator getStorageSlotLocked(); |
Songchun Fan | 103ba1d | 2020-02-03 17:32:32 -0800 | [diff] [blame] | 242 | std::string normalizePathToStorage(const IfsMountPtr incfs, StorageId storage, |
| 243 | std::string_view path); |
Songchun Fan | 3c82a30 | 2019-11-29 14:23:45 -0800 | [diff] [blame] | 244 | |
Alex Buynytskyy | 1d89216 | 2020-04-03 23:00:19 -0700 | [diff] [blame] | 245 | binder::Status applyStorageParams(IncFsMount& ifs, bool enableReadLogs); |
Alex Buynytskyy | 96e350b | 2020-04-02 20:03:47 -0700 | [diff] [blame] | 246 | |
| 247 | void registerAppOpsCallback(const std::string& packageName); |
Alex Buynytskyy | 1d89216 | 2020-04-03 23:00:19 -0700 | [diff] [blame] | 248 | bool unregisterAppOpsCallback(const std::string& packageName); |
| 249 | void onAppOpChanged(const std::string& packageName); |
Alex Buynytskyy | 96e350b | 2020-04-02 20:03:47 -0700 | [diff] [blame] | 250 | |
Songchun Fan | 3c82a30 | 2019-11-29 14:23:45 -0800 | [diff] [blame] | 251 | // Member variables |
Alex Buynytskyy | 96e350b | 2020-04-02 20:03:47 -0700 | [diff] [blame] | 252 | std::unique_ptr<VoldServiceWrapper> const mVold; |
| 253 | std::unique_ptr<DataLoaderManagerWrapper> const mDataLoaderManager; |
| 254 | std::unique_ptr<IncFsWrapper> const mIncFs; |
| 255 | std::unique_ptr<AppOpsManagerWrapper> const mAppOpsManager; |
Songchun Fan | 3c82a30 | 2019-11-29 14:23:45 -0800 | [diff] [blame] | 256 | const std::string mIncrementalDir; |
| 257 | |
| 258 | mutable std::mutex mLock; |
| 259 | mutable std::mutex mMountOperationLock; |
| 260 | MountMap mMounts; |
| 261 | BindPathMap mBindsByPath; |
| 262 | |
Alex Buynytskyy | 96e350b | 2020-04-02 20:03:47 -0700 | [diff] [blame] | 263 | std::mutex mCallbacksLock; |
Alex Buynytskyy | 1d89216 | 2020-04-03 23:00:19 -0700 | [diff] [blame] | 264 | std::map<std::string, sp<AppOpsListener>> mCallbackRegistered; |
Alex Buynytskyy | 96e350b | 2020-04-02 20:03:47 -0700 | [diff] [blame] | 265 | |
Songchun Fan | 3c82a30 | 2019-11-29 14:23:45 -0800 | [diff] [blame] | 266 | std::atomic_bool mSystemReady = false; |
| 267 | StorageId mNextId = 0; |
| 268 | std::promise<void> mPrepareDataLoaders; |
| 269 | }; |
| 270 | |
| 271 | } // namespace android::incremental |