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