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> |
Songchun Fan | 103ba1d | 2020-02-03 17:32:32 -0800 | [diff] [blame] | 22 | #include <android/os/incremental/IIncrementalManager.h> |
Songchun Fan | 3c82a30 | 2019-11-29 14:23:45 -0800 | [diff] [blame] | 23 | #include <binder/IServiceManager.h> |
| 24 | #include <utils/String16.h> |
| 25 | #include <utils/StrongPointer.h> |
| 26 | #include <utils/Vector.h> |
| 27 | |
| 28 | #include <atomic> |
| 29 | #include <chrono> |
| 30 | #include <future> |
| 31 | #include <limits> |
| 32 | #include <map> |
| 33 | #include <mutex> |
| 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 | |
Yurii Zubrytskyi | 4a25dfb | 2020-01-10 11:53:24 -0800 | [diff] [blame] | 62 | class IncrementalService final { |
Songchun Fan | 3c82a30 | 2019-11-29 14:23:45 -0800 | [diff] [blame] | 63 | public: |
Yurii Zubrytskyi | 4a25dfb | 2020-01-10 11:53:24 -0800 | [diff] [blame] | 64 | explicit IncrementalService(ServiceManagerWrapper&& sm, std::string_view rootDir); |
Songchun Fan | 3c82a30 | 2019-11-29 14:23:45 -0800 | [diff] [blame] | 65 | |
| 66 | #pragma GCC diagnostic push |
| 67 | #pragma GCC diagnostic ignored "-Wnon-virtual-dtor" |
| 68 | ~IncrementalService(); |
| 69 | #pragma GCC diagnostic pop |
| 70 | |
| 71 | static constexpr StorageId kInvalidStorageId = -1; |
| 72 | static constexpr StorageId kMaxStorageId = std::numeric_limits<int>::max(); |
| 73 | |
| 74 | enum CreateOptions { |
| 75 | TemporaryBind = 1, |
| 76 | PermanentBind = 2, |
| 77 | CreateNew = 4, |
| 78 | OpenExisting = 8, |
| 79 | |
| 80 | Default = TemporaryBind | CreateNew |
| 81 | }; |
| 82 | |
| 83 | enum class BindKind { |
| 84 | Temporary = 0, |
| 85 | Permanent = 1, |
| 86 | }; |
| 87 | |
Yurii Zubrytskyi | 4a25dfb | 2020-01-10 11:53:24 -0800 | [diff] [blame] | 88 | static FileId idFromMetadata(std::span<const uint8_t> metadata); |
| 89 | static inline FileId idFromMetadata(std::span<const char> metadata) { |
| 90 | return idFromMetadata({(const uint8_t*)metadata.data(), metadata.size()}); |
| 91 | } |
| 92 | |
Alex Buynytskyy | 18b07a4 | 2020-02-03 20:06:00 -0800 | [diff] [blame] | 93 | void onDump(int fd); |
| 94 | |
Songchun Fan | 3c82a30 | 2019-11-29 14:23:45 -0800 | [diff] [blame] | 95 | std::optional<std::future<void>> onSystemReady(); |
| 96 | |
Songchun Fan | 103ba1d | 2020-02-03 17:32:32 -0800 | [diff] [blame] | 97 | StorageId createStorage(std::string_view mountPoint, DataLoaderParamsParcel&& dataLoaderParams, |
Songchun Fan | 3c82a30 | 2019-11-29 14:23:45 -0800 | [diff] [blame] | 98 | CreateOptions options = CreateOptions::Default); |
| 99 | StorageId createLinkedStorage(std::string_view mountPoint, StorageId linkedStorage, |
| 100 | CreateOptions options = CreateOptions::Default); |
Yurii Zubrytskyi | 4a25dfb | 2020-01-10 11:53:24 -0800 | [diff] [blame] | 101 | StorageId openStorage(std::string_view path); |
Songchun Fan | 3c82a30 | 2019-11-29 14:23:45 -0800 | [diff] [blame] | 102 | |
Yurii Zubrytskyi | 4a25dfb | 2020-01-10 11:53:24 -0800 | [diff] [blame] | 103 | FileId nodeFor(StorageId storage, std::string_view path) const; |
| 104 | std::pair<FileId, std::string_view> parentAndNameFor(StorageId storage, |
| 105 | std::string_view path) const; |
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 | 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] | 108 | int unbind(StorageId storage, std::string_view target); |
| 109 | void deleteStorage(StorageId storage); |
| 110 | |
Yurii Zubrytskyi | 4a25dfb | 2020-01-10 11:53:24 -0800 | [diff] [blame] | 111 | int makeFile(StorageId storage, std::string_view path, int mode, FileId id, |
| 112 | incfs::NewFileParams params); |
Songchun Fan | 9610093 | 2020-02-03 19:20:58 -0800 | [diff] [blame] | 113 | int makeDir(StorageId storage, std::string_view path, int mode = 0755); |
| 114 | int makeDirs(StorageId storage, std::string_view path, int mode = 0755); |
Songchun Fan | 3c82a30 | 2019-11-29 14:23:45 -0800 | [diff] [blame] | 115 | |
Yurii Zubrytskyi | 4a25dfb | 2020-01-10 11:53:24 -0800 | [diff] [blame] | 116 | int link(StorageId sourceStorageId, std::string_view oldPath, StorageId destStorageId, |
| 117 | std::string_view newPath); |
| 118 | int unlink(StorageId storage, std::string_view path); |
Songchun Fan | 3c82a30 | 2019-11-29 14:23:45 -0800 | [diff] [blame] | 119 | |
Yurii Zubrytskyi | 4a25dfb | 2020-01-10 11:53:24 -0800 | [diff] [blame] | 120 | bool isRangeLoaded(StorageId storage, FileId file, std::pair<BlockIndex, BlockIndex> range) { |
Songchun Fan | 3c82a30 | 2019-11-29 14:23:45 -0800 | [diff] [blame] | 121 | return false; |
| 122 | } |
| 123 | |
Yurii Zubrytskyi | 4a25dfb | 2020-01-10 11:53:24 -0800 | [diff] [blame] | 124 | RawMetadata getMetadata(StorageId storage, FileId node) const; |
Songchun Fan | 3c82a30 | 2019-11-29 14:23:45 -0800 | [diff] [blame] | 125 | |
| 126 | std::vector<std::string> listFiles(StorageId storage) const; |
| 127 | bool startLoading(StorageId storage) const; |
Songchun Fan | 0f8b6fe | 2020-02-05 17:41:25 -0800 | [diff] [blame] | 128 | bool configureNativeBinaries(StorageId storage, std::string_view apkFullPath, |
| 129 | std::string_view libDirRelativePath, std::string_view abi); |
Songchun Fan | 3c82a30 | 2019-11-29 14:23:45 -0800 | [diff] [blame] | 130 | class IncrementalDataLoaderListener : public android::content::pm::BnDataLoaderStatusListener { |
| 131 | public: |
| 132 | IncrementalDataLoaderListener(IncrementalService& incrementalService) |
| 133 | : incrementalService(incrementalService) {} |
| 134 | // Callbacks interface |
| 135 | binder::Status onStatusChanged(MountId mount, int newStatus) override; |
| 136 | |
| 137 | private: |
| 138 | IncrementalService& incrementalService; |
| 139 | }; |
| 140 | |
| 141 | private: |
| 142 | struct IncFsMount { |
| 143 | struct Bind { |
| 144 | StorageId storage; |
| 145 | std::string savedFilename; |
| 146 | std::string sourceDir; |
| 147 | BindKind kind; |
| 148 | }; |
| 149 | |
| 150 | struct Storage { |
| 151 | std::string name; |
Songchun Fan | 3c82a30 | 2019-11-29 14:23:45 -0800 | [diff] [blame] | 152 | }; |
| 153 | |
Yurii Zubrytskyi | 4a25dfb | 2020-01-10 11:53:24 -0800 | [diff] [blame] | 154 | using Control = incfs::UniqueControl; |
Songchun Fan | 3c82a30 | 2019-11-29 14:23:45 -0800 | [diff] [blame] | 155 | |
| 156 | using BindMap = std::map<std::string, Bind>; |
| 157 | using StorageMap = std::unordered_map<StorageId, Storage>; |
| 158 | |
| 159 | mutable std::mutex lock; |
| 160 | const std::string root; |
| 161 | Control control; |
| 162 | /*const*/ MountId mountId; |
| 163 | StorageMap storages; |
| 164 | BindMap bindPoints; |
| 165 | std::optional<DataLoaderParamsParcel> savedDataLoaderParams; |
| 166 | std::atomic<int> nextStorageDirNo{0}; |
| 167 | std::atomic<int> dataLoaderStatus = -1; |
| 168 | std::condition_variable dataLoaderReady; |
| 169 | TimePoint connectionLostTime = TimePoint(); |
| 170 | const IncrementalService& incrementalService; |
| 171 | |
| 172 | IncFsMount(std::string root, MountId mountId, Control control, |
| 173 | const IncrementalService& incrementalService) |
| 174 | : root(std::move(root)), |
| 175 | control(std::move(control)), |
| 176 | mountId(mountId), |
| 177 | incrementalService(incrementalService) {} |
| 178 | IncFsMount(IncFsMount&&) = delete; |
| 179 | IncFsMount& operator=(IncFsMount&&) = delete; |
| 180 | ~IncFsMount(); |
| 181 | |
| 182 | StorageMap::iterator makeStorage(StorageId id); |
| 183 | |
| 184 | static void cleanupFilesystem(std::string_view root); |
| 185 | }; |
| 186 | |
| 187 | using IfsMountPtr = std::shared_ptr<IncFsMount>; |
| 188 | using MountMap = std::unordered_map<MountId, IfsMountPtr>; |
| 189 | using BindPathMap = std::map<std::string, IncFsMount::BindMap::iterator, path::PathLess>; |
| 190 | |
| 191 | void mountExistingImages(); |
| 192 | bool mountExistingImage(std::string_view root, std::string_view key); |
| 193 | |
| 194 | IfsMountPtr getIfs(StorageId storage) const; |
| 195 | const IfsMountPtr& getIfsLocked(StorageId storage) const; |
Yurii Zubrytskyi | 4a25dfb | 2020-01-10 11:53:24 -0800 | [diff] [blame] | 196 | int addBindMount(IncFsMount& ifs, StorageId storage, std::string_view storageRoot, |
| 197 | std::string&& source, std::string&& target, BindKind kind, |
| 198 | std::unique_lock<std::mutex>& mainLock); |
Songchun Fan | 3c82a30 | 2019-11-29 14:23:45 -0800 | [diff] [blame] | 199 | |
| 200 | int addBindMountWithMd(IncFsMount& ifs, StorageId storage, std::string&& metadataName, |
Yurii Zubrytskyi | 4a25dfb | 2020-01-10 11:53:24 -0800 | [diff] [blame] | 201 | std::string&& source, std::string&& target, BindKind kind, |
Songchun Fan | 3c82a30 | 2019-11-29 14:23:45 -0800 | [diff] [blame] | 202 | std::unique_lock<std::mutex>& mainLock); |
| 203 | |
| 204 | bool prepareDataLoader(IncFsMount& ifs, DataLoaderParamsParcel* params); |
| 205 | BindPathMap::const_iterator findStorageLocked(std::string_view path) const; |
| 206 | StorageId findStorageId(std::string_view path) const; |
| 207 | |
| 208 | void deleteStorage(IncFsMount& ifs); |
| 209 | void deleteStorageLocked(IncFsMount& ifs, std::unique_lock<std::mutex>&& ifsLock); |
| 210 | MountMap::iterator getStorageSlotLocked(); |
Songchun Fan | 103ba1d | 2020-02-03 17:32:32 -0800 | [diff] [blame] | 211 | std::string normalizePathToStorage(const IfsMountPtr incfs, StorageId storage, |
| 212 | std::string_view path); |
Songchun Fan | 3c82a30 | 2019-11-29 14:23:45 -0800 | [diff] [blame] | 213 | |
| 214 | // Member variables |
Yurii Zubrytskyi | 4a25dfb | 2020-01-10 11:53:24 -0800 | [diff] [blame] | 215 | std::unique_ptr<VoldServiceWrapper> mVold; |
| 216 | std::unique_ptr<IncrementalManagerWrapper> mIncrementalManager; |
| 217 | std::unique_ptr<IncFsWrapper> mIncFs; |
Songchun Fan | 3c82a30 | 2019-11-29 14:23:45 -0800 | [diff] [blame] | 218 | const std::string mIncrementalDir; |
| 219 | |
| 220 | mutable std::mutex mLock; |
| 221 | mutable std::mutex mMountOperationLock; |
| 222 | MountMap mMounts; |
| 223 | BindPathMap mBindsByPath; |
| 224 | |
| 225 | std::atomic_bool mSystemReady = false; |
| 226 | StorageId mNextId = 0; |
| 227 | std::promise<void> mPrepareDataLoaders; |
| 228 | }; |
| 229 | |
| 230 | } // namespace android::incremental |