blob: 8280287426243de79ebe38dc8365104270d5603e [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
Alex Buynytskyy18b07a42020-02-03 20:06:00 -080093 void onDump(int fd);
94
Songchun Fan3c82a302019-11-29 14:23:45 -080095 std::optional<std::future<void>> onSystemReady();
96
97 StorageId createStorage(std::string_view mountPoint,
98 DataLoaderParamsParcel&& dataLoaderParams,
99 CreateOptions options = CreateOptions::Default);
100 StorageId createLinkedStorage(std::string_view mountPoint, StorageId linkedStorage,
101 CreateOptions options = CreateOptions::Default);
Yurii Zubrytskyi4a25dfb2020-01-10 11:53:24 -0800102 StorageId openStorage(std::string_view path);
Songchun Fan3c82a302019-11-29 14:23:45 -0800103
Yurii Zubrytskyi4a25dfb2020-01-10 11:53:24 -0800104 FileId nodeFor(StorageId storage, std::string_view path) const;
105 std::pair<FileId, std::string_view> parentAndNameFor(StorageId storage,
106 std::string_view path) const;
Songchun Fan3c82a302019-11-29 14:23:45 -0800107
Yurii Zubrytskyi4a25dfb2020-01-10 11:53:24 -0800108 int bind(StorageId storage, std::string_view source, std::string_view target, BindKind kind);
Songchun Fan3c82a302019-11-29 14:23:45 -0800109 int unbind(StorageId storage, std::string_view target);
110 void deleteStorage(StorageId storage);
111
Yurii Zubrytskyi4a25dfb2020-01-10 11:53:24 -0800112 int makeFile(StorageId storage, std::string_view path, int mode, FileId id,
113 incfs::NewFileParams params);
114 int makeDir(StorageId storage, std::string_view path, int mode = 0555);
115 int makeDirs(StorageId storage, std::string_view path, int mode = 0555);
Songchun Fan3c82a302019-11-29 14:23:45 -0800116
Yurii Zubrytskyi4a25dfb2020-01-10 11:53:24 -0800117 int link(StorageId sourceStorageId, std::string_view oldPath, StorageId destStorageId,
118 std::string_view newPath);
119 int unlink(StorageId storage, std::string_view path);
Songchun Fan3c82a302019-11-29 14:23:45 -0800120
Yurii Zubrytskyi4a25dfb2020-01-10 11:53:24 -0800121 bool isRangeLoaded(StorageId storage, FileId file, std::pair<BlockIndex, BlockIndex> range) {
Songchun Fan3c82a302019-11-29 14:23:45 -0800122 return false;
123 }
124
Yurii Zubrytskyi4a25dfb2020-01-10 11:53:24 -0800125 RawMetadata getMetadata(StorageId storage, FileId node) const;
126 std::string getSignatureData(StorageId storage, FileId node) const;
Songchun Fan3c82a302019-11-29 14:23:45 -0800127
128 std::vector<std::string> listFiles(StorageId storage) const;
129 bool startLoading(StorageId storage) const;
130
131 class IncrementalDataLoaderListener : public android::content::pm::BnDataLoaderStatusListener {
132 public:
133 IncrementalDataLoaderListener(IncrementalService& incrementalService)
134 : incrementalService(incrementalService) {}
135 // Callbacks interface
136 binder::Status onStatusChanged(MountId mount, int newStatus) override;
137
138 private:
139 IncrementalService& incrementalService;
140 };
141
142private:
143 struct IncFsMount {
144 struct Bind {
145 StorageId storage;
146 std::string savedFilename;
147 std::string sourceDir;
148 BindKind kind;
149 };
150
151 struct Storage {
152 std::string name;
Songchun Fan3c82a302019-11-29 14:23:45 -0800153 };
154
Yurii Zubrytskyi4a25dfb2020-01-10 11:53:24 -0800155 using Control = incfs::UniqueControl;
Songchun Fan3c82a302019-11-29 14:23:45 -0800156
157 using BindMap = std::map<std::string, Bind>;
158 using StorageMap = std::unordered_map<StorageId, Storage>;
159
160 mutable std::mutex lock;
161 const std::string root;
162 Control control;
163 /*const*/ MountId mountId;
164 StorageMap storages;
165 BindMap bindPoints;
166 std::optional<DataLoaderParamsParcel> savedDataLoaderParams;
167 std::atomic<int> nextStorageDirNo{0};
168 std::atomic<int> dataLoaderStatus = -1;
169 std::condition_variable dataLoaderReady;
170 TimePoint connectionLostTime = TimePoint();
171 const IncrementalService& incrementalService;
172
173 IncFsMount(std::string root, MountId mountId, Control control,
174 const IncrementalService& incrementalService)
175 : root(std::move(root)),
176 control(std::move(control)),
177 mountId(mountId),
178 incrementalService(incrementalService) {}
179 IncFsMount(IncFsMount&&) = delete;
180 IncFsMount& operator=(IncFsMount&&) = delete;
181 ~IncFsMount();
182
183 StorageMap::iterator makeStorage(StorageId id);
184
185 static void cleanupFilesystem(std::string_view root);
186 };
187
188 using IfsMountPtr = std::shared_ptr<IncFsMount>;
189 using MountMap = std::unordered_map<MountId, IfsMountPtr>;
190 using BindPathMap = std::map<std::string, IncFsMount::BindMap::iterator, path::PathLess>;
191
192 void mountExistingImages();
193 bool mountExistingImage(std::string_view root, std::string_view key);
194
195 IfsMountPtr getIfs(StorageId storage) const;
196 const IfsMountPtr& getIfsLocked(StorageId storage) const;
Yurii Zubrytskyi4a25dfb2020-01-10 11:53:24 -0800197 int addBindMount(IncFsMount& ifs, StorageId storage, std::string_view storageRoot,
198 std::string&& source, std::string&& target, BindKind kind,
199 std::unique_lock<std::mutex>& mainLock);
Songchun Fan3c82a302019-11-29 14:23:45 -0800200
201 int addBindMountWithMd(IncFsMount& ifs, StorageId storage, std::string&& metadataName,
Yurii Zubrytskyi4a25dfb2020-01-10 11:53:24 -0800202 std::string&& source, std::string&& target, BindKind kind,
Songchun Fan3c82a302019-11-29 14:23:45 -0800203 std::unique_lock<std::mutex>& mainLock);
204
205 bool prepareDataLoader(IncFsMount& ifs, DataLoaderParamsParcel* params);
206 BindPathMap::const_iterator findStorageLocked(std::string_view path) const;
207 StorageId findStorageId(std::string_view path) const;
208
209 void deleteStorage(IncFsMount& ifs);
210 void deleteStorageLocked(IncFsMount& ifs, std::unique_lock<std::mutex>&& ifsLock);
211 MountMap::iterator getStorageSlotLocked();
212
213 // Member variables
Yurii Zubrytskyi4a25dfb2020-01-10 11:53:24 -0800214 std::unique_ptr<VoldServiceWrapper> mVold;
215 std::unique_ptr<IncrementalManagerWrapper> mIncrementalManager;
216 std::unique_ptr<IncFsWrapper> mIncFs;
Songchun Fan3c82a302019-11-29 14:23:45 -0800217 const std::string mIncrementalDir;
218
219 mutable std::mutex mLock;
220 mutable std::mutex mMountOperationLock;
221 MountMap mMounts;
222 BindPathMap mBindsByPath;
223
224 std::atomic_bool mSystemReady = false;
225 StorageId mNextId = 0;
226 std::promise<void> mPrepareDataLoaders;
227};
228
229} // namespace android::incremental