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> |
| 21 | #include <android/os/incremental/IIncrementalManager.h> |
| 22 | #include <android/content/pm/DataLoaderParamsParcel.h> |
| 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 | |
Songchun Fan | 3c82a30 | 2019-11-29 14:23:45 -0800 | [diff] [blame] | 93 | std::optional<std::future<void>> onSystemReady(); |
| 94 | |
| 95 | StorageId createStorage(std::string_view mountPoint, |
| 96 | DataLoaderParamsParcel&& dataLoaderParams, |
| 97 | CreateOptions options = CreateOptions::Default); |
| 98 | StorageId createLinkedStorage(std::string_view mountPoint, StorageId linkedStorage, |
| 99 | CreateOptions options = CreateOptions::Default); |
Yurii Zubrytskyi | 4a25dfb | 2020-01-10 11:53:24 -0800 | [diff] [blame] | 100 | StorageId openStorage(std::string_view path); |
Songchun Fan | 3c82a30 | 2019-11-29 14:23:45 -0800 | [diff] [blame] | 101 | |
Yurii Zubrytskyi | 4a25dfb | 2020-01-10 11:53:24 -0800 | [diff] [blame] | 102 | FileId nodeFor(StorageId storage, std::string_view path) const; |
| 103 | std::pair<FileId, std::string_view> parentAndNameFor(StorageId storage, |
| 104 | std::string_view path) const; |
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 | |
Yurii Zubrytskyi | 4a25dfb | 2020-01-10 11:53:24 -0800 | [diff] [blame] | 110 | int makeFile(StorageId storage, std::string_view path, int mode, FileId id, |
| 111 | incfs::NewFileParams params); |
| 112 | int makeDir(StorageId storage, std::string_view path, int mode = 0555); |
| 113 | int makeDirs(StorageId storage, std::string_view path, int mode = 0555); |
Songchun Fan | 3c82a30 | 2019-11-29 14:23:45 -0800 | [diff] [blame] | 114 | |
Yurii Zubrytskyi | 4a25dfb | 2020-01-10 11:53:24 -0800 | [diff] [blame] | 115 | int link(StorageId sourceStorageId, std::string_view oldPath, StorageId destStorageId, |
| 116 | std::string_view newPath); |
| 117 | int unlink(StorageId storage, std::string_view path); |
Songchun Fan | 3c82a30 | 2019-11-29 14:23:45 -0800 | [diff] [blame] | 118 | |
Yurii Zubrytskyi | 4a25dfb | 2020-01-10 11:53:24 -0800 | [diff] [blame] | 119 | bool isRangeLoaded(StorageId storage, FileId file, std::pair<BlockIndex, BlockIndex> range) { |
Songchun Fan | 3c82a30 | 2019-11-29 14:23:45 -0800 | [diff] [blame] | 120 | return false; |
| 121 | } |
| 122 | |
Yurii Zubrytskyi | 4a25dfb | 2020-01-10 11:53:24 -0800 | [diff] [blame] | 123 | RawMetadata getMetadata(StorageId storage, FileId node) const; |
| 124 | std::string getSignatureData(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; |
| 128 | |
| 129 | class IncrementalDataLoaderListener : public android::content::pm::BnDataLoaderStatusListener { |
| 130 | public: |
| 131 | IncrementalDataLoaderListener(IncrementalService& incrementalService) |
| 132 | : incrementalService(incrementalService) {} |
| 133 | // Callbacks interface |
| 134 | binder::Status onStatusChanged(MountId mount, int newStatus) override; |
| 135 | |
| 136 | private: |
| 137 | IncrementalService& incrementalService; |
| 138 | }; |
| 139 | |
| 140 | private: |
| 141 | struct IncFsMount { |
| 142 | struct Bind { |
| 143 | StorageId storage; |
| 144 | std::string savedFilename; |
| 145 | std::string sourceDir; |
| 146 | BindKind kind; |
| 147 | }; |
| 148 | |
| 149 | struct Storage { |
| 150 | std::string name; |
Songchun Fan | 3c82a30 | 2019-11-29 14:23:45 -0800 | [diff] [blame] | 151 | }; |
| 152 | |
Yurii Zubrytskyi | 4a25dfb | 2020-01-10 11:53:24 -0800 | [diff] [blame] | 153 | using Control = incfs::UniqueControl; |
Songchun Fan | 3c82a30 | 2019-11-29 14:23:45 -0800 | [diff] [blame] | 154 | |
| 155 | using BindMap = std::map<std::string, Bind>; |
| 156 | using StorageMap = std::unordered_map<StorageId, Storage>; |
| 157 | |
| 158 | mutable std::mutex lock; |
| 159 | const std::string root; |
| 160 | Control control; |
| 161 | /*const*/ MountId mountId; |
| 162 | StorageMap storages; |
| 163 | BindMap bindPoints; |
| 164 | std::optional<DataLoaderParamsParcel> savedDataLoaderParams; |
| 165 | std::atomic<int> nextStorageDirNo{0}; |
| 166 | std::atomic<int> dataLoaderStatus = -1; |
| 167 | std::condition_variable dataLoaderReady; |
| 168 | TimePoint connectionLostTime = TimePoint(); |
| 169 | const IncrementalService& incrementalService; |
| 170 | |
| 171 | IncFsMount(std::string root, MountId mountId, Control control, |
| 172 | const IncrementalService& incrementalService) |
| 173 | : root(std::move(root)), |
| 174 | control(std::move(control)), |
| 175 | mountId(mountId), |
| 176 | incrementalService(incrementalService) {} |
| 177 | IncFsMount(IncFsMount&&) = delete; |
| 178 | IncFsMount& operator=(IncFsMount&&) = delete; |
| 179 | ~IncFsMount(); |
| 180 | |
| 181 | StorageMap::iterator makeStorage(StorageId id); |
| 182 | |
| 183 | static void cleanupFilesystem(std::string_view root); |
| 184 | }; |
| 185 | |
| 186 | using IfsMountPtr = std::shared_ptr<IncFsMount>; |
| 187 | using MountMap = std::unordered_map<MountId, IfsMountPtr>; |
| 188 | using BindPathMap = std::map<std::string, IncFsMount::BindMap::iterator, path::PathLess>; |
| 189 | |
| 190 | void mountExistingImages(); |
| 191 | bool mountExistingImage(std::string_view root, std::string_view key); |
| 192 | |
| 193 | IfsMountPtr getIfs(StorageId storage) const; |
| 194 | const IfsMountPtr& getIfsLocked(StorageId storage) const; |
Yurii Zubrytskyi | 4a25dfb | 2020-01-10 11:53:24 -0800 | [diff] [blame] | 195 | int addBindMount(IncFsMount& ifs, StorageId storage, std::string_view storageRoot, |
| 196 | std::string&& source, std::string&& target, BindKind kind, |
| 197 | std::unique_lock<std::mutex>& mainLock); |
Songchun Fan | 3c82a30 | 2019-11-29 14:23:45 -0800 | [diff] [blame] | 198 | |
| 199 | int addBindMountWithMd(IncFsMount& ifs, StorageId storage, std::string&& metadataName, |
Yurii Zubrytskyi | 4a25dfb | 2020-01-10 11:53:24 -0800 | [diff] [blame] | 200 | std::string&& source, std::string&& target, BindKind kind, |
Songchun Fan | 3c82a30 | 2019-11-29 14:23:45 -0800 | [diff] [blame] | 201 | std::unique_lock<std::mutex>& mainLock); |
| 202 | |
| 203 | bool prepareDataLoader(IncFsMount& ifs, DataLoaderParamsParcel* params); |
| 204 | BindPathMap::const_iterator findStorageLocked(std::string_view path) const; |
| 205 | StorageId findStorageId(std::string_view path) const; |
| 206 | |
| 207 | void deleteStorage(IncFsMount& ifs); |
| 208 | void deleteStorageLocked(IncFsMount& ifs, std::unique_lock<std::mutex>&& ifsLock); |
| 209 | MountMap::iterator getStorageSlotLocked(); |
| 210 | |
| 211 | // Member variables |
Yurii Zubrytskyi | 4a25dfb | 2020-01-10 11:53:24 -0800 | [diff] [blame] | 212 | std::unique_ptr<VoldServiceWrapper> mVold; |
| 213 | std::unique_ptr<IncrementalManagerWrapper> mIncrementalManager; |
| 214 | std::unique_ptr<IncFsWrapper> mIncFs; |
Songchun Fan | 3c82a30 | 2019-11-29 14:23:45 -0800 | [diff] [blame] | 215 | const std::string mIncrementalDir; |
| 216 | |
| 217 | mutable std::mutex mLock; |
| 218 | mutable std::mutex mMountOperationLock; |
| 219 | MountMap mMounts; |
| 220 | BindPathMap mBindsByPath; |
| 221 | |
| 222 | std::atomic_bool mSystemReady = false; |
| 223 | StorageId mNextId = 0; |
| 224 | std::promise<void> mPrepareDataLoaders; |
| 225 | }; |
| 226 | |
| 227 | } // namespace android::incremental |