blob: ca5e4dbd9231fc3456d26f5ffae8984b783e1f3c [file] [log] [blame]
Songchun Fan3c82a302019-11-29 14:23:45 -08001/*
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
45using namespace android::os::incremental;
46
47namespace android::os {
48class IVold;
49}
50
51namespace android::incremental {
52
53using MountId = int;
54using StorageId = int;
Yurii Zubrytskyi4a25dfb2020-01-10 11:53:24 -080055using FileId = incfs::FileId;
Songchun Fan3c82a302019-11-29 14:23:45 -080056using BlockIndex = incfs::BlockIndex;
57using RawMetadata = incfs::RawMetadata;
58using Clock = std::chrono::steady_clock;
59using TimePoint = std::chrono::time_point<Clock>;
60using Seconds = std::chrono::seconds;
61
Yurii Zubrytskyi4a25dfb2020-01-10 11:53:24 -080062class IncrementalService final {
Songchun Fan3c82a302019-11-29 14:23:45 -080063public:
Yurii Zubrytskyi4a25dfb2020-01-10 11:53:24 -080064 explicit IncrementalService(ServiceManagerWrapper&& sm, std::string_view rootDir);
Songchun Fan3c82a302019-11-29 14:23:45 -080065
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 Zubrytskyi4a25dfb2020-01-10 11:53:24 -080088 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 Fan3c82a302019-11-29 14:23:45 -080093 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 Zubrytskyi4a25dfb2020-01-10 11:53:24 -0800100 StorageId openStorage(std::string_view path);
Songchun Fan3c82a302019-11-29 14:23:45 -0800101
Yurii Zubrytskyi4a25dfb2020-01-10 11:53:24 -0800102 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 Fan3c82a302019-11-29 14:23:45 -0800105
Yurii Zubrytskyi4a25dfb2020-01-10 11:53:24 -0800106 int bind(StorageId storage, std::string_view source, std::string_view target, BindKind kind);
Songchun Fan3c82a302019-11-29 14:23:45 -0800107 int unbind(StorageId storage, std::string_view target);
108 void deleteStorage(StorageId storage);
109
Yurii Zubrytskyi4a25dfb2020-01-10 11:53:24 -0800110 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 Fan3c82a302019-11-29 14:23:45 -0800114
Yurii Zubrytskyi4a25dfb2020-01-10 11:53:24 -0800115 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 Fan3c82a302019-11-29 14:23:45 -0800118
Yurii Zubrytskyi4a25dfb2020-01-10 11:53:24 -0800119 bool isRangeLoaded(StorageId storage, FileId file, std::pair<BlockIndex, BlockIndex> range) {
Songchun Fan3c82a302019-11-29 14:23:45 -0800120 return false;
121 }
122
Yurii Zubrytskyi4a25dfb2020-01-10 11:53:24 -0800123 RawMetadata getMetadata(StorageId storage, FileId node) const;
124 std::string getSignatureData(StorageId storage, FileId node) const;
Songchun Fan3c82a302019-11-29 14:23:45 -0800125
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
140private:
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 Fan3c82a302019-11-29 14:23:45 -0800151 };
152
Yurii Zubrytskyi4a25dfb2020-01-10 11:53:24 -0800153 using Control = incfs::UniqueControl;
Songchun Fan3c82a302019-11-29 14:23:45 -0800154
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 Zubrytskyi4a25dfb2020-01-10 11:53:24 -0800195 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 Fan3c82a302019-11-29 14:23:45 -0800198
199 int addBindMountWithMd(IncFsMount& ifs, StorageId storage, std::string&& metadataName,
Yurii Zubrytskyi4a25dfb2020-01-10 11:53:24 -0800200 std::string&& source, std::string&& target, BindKind kind,
Songchun Fan3c82a302019-11-29 14:23:45 -0800201 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 Zubrytskyi4a25dfb2020-01-10 11:53:24 -0800212 std::unique_ptr<VoldServiceWrapper> mVold;
213 std::unique_ptr<IncrementalManagerWrapper> mIncrementalManager;
214 std::unique_ptr<IncFsWrapper> mIncFs;
Songchun Fan3c82a302019-11-29 14:23:45 -0800215 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