blob: 5d53bac777b52709ece713c45edc3ecb7c4e5072 [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
Yurii Zubrytskyi629051fd2020-04-17 23:13:47 -070019#include <android/content/pm/BnDataLoaderStatusListener.h>
Songchun Fan3c82a302019-11-29 14:23:45 -080020#include <android/content/pm/DataLoaderParamsParcel.h>
Alex Buynytskyycca2c112020-05-05 12:48:41 -070021#include <android/content/pm/FileSystemControlParcel.h>
Yurii Zubrytskyi629051fd2020-04-17 23:13:47 -070022#include <android/content/pm/IDataLoaderStatusListener.h>
23#include <android/os/incremental/BnIncrementalServiceConnector.h>
Alex Buynytskyy8ef61ae2020-05-08 16:18:52 -070024#include <android/os/incremental/BnStorageHealthListener.h>
Songchun Fana7098592020-09-03 11:45:53 -070025#include <android/os/incremental/BnStorageLoadingProgressListener.h>
Alex Buynytskyyaa8e95e2020-12-14 21:50:04 -080026#include <android/os/incremental/PerUidReadTimeouts.h>
Alex Buynytskyy8ef61ae2020-05-08 16:18:52 -070027#include <android/os/incremental/StorageHealthCheckParams.h>
Yurii Zubrytskyi629051fd2020-04-17 23:13:47 -070028#include <binder/IAppOpsCallback.h>
Songchun Fan3c82a302019-11-29 14:23:45 -080029#include <utils/String16.h>
30#include <utils/StrongPointer.h>
Yurii Zubrytskyida208012020-04-07 15:35:21 -070031#include <ziparchive/zip_archive.h>
Songchun Fan3c82a302019-11-29 14:23:45 -080032
33#include <atomic>
34#include <chrono>
Yurii Zubrytskyida208012020-04-07 15:35:21 -070035#include <condition_variable>
36#include <functional>
Songchun Fan3c82a302019-11-29 14:23:45 -080037#include <limits>
38#include <map>
39#include <mutex>
Alex Buynytskyy4760d8f2020-05-08 16:18:52 -070040#include <set>
Songchun Fan9b753082020-02-26 13:08:06 -080041#include <span>
Songchun Fan3c82a302019-11-29 14:23:45 -080042#include <string>
43#include <string_view>
Yurii Zubrytskyida208012020-04-07 15:35:21 -070044#include <thread>
Songchun Fan3c82a302019-11-29 14:23:45 -080045#include <unordered_map>
Yurii Zubrytskyi629051fd2020-04-17 23:13:47 -070046#include <unordered_set>
Songchun Fan3c82a302019-11-29 14:23:45 -080047#include <utility>
48#include <vector>
49
50#include "ServiceWrappers.h"
Songchun Fan3c82a302019-11-29 14:23:45 -080051#include "incfs.h"
52#include "path.h"
53
Songchun Fan3c82a302019-11-29 14:23:45 -080054namespace android::incremental {
55
56using MountId = int;
57using StorageId = int;
Yurii Zubrytskyi4a25dfb2020-01-10 11:53:24 -080058using FileId = incfs::FileId;
Songchun Fan3c82a302019-11-29 14:23:45 -080059using BlockIndex = incfs::BlockIndex;
60using RawMetadata = incfs::RawMetadata;
Songchun Fan3c82a302019-11-29 14:23:45 -080061using Seconds = std::chrono::seconds;
Alex Buynytskyy8ef61ae2020-05-08 16:18:52 -070062using BootClockTsUs = uint64_t;
Songchun Fan3c82a302019-11-29 14:23:45 -080063
Alex Buynytskyy0ea4ff42020-04-09 17:25:42 -070064using IDataLoaderStatusListener = ::android::content::pm::IDataLoaderStatusListener;
65using DataLoaderStatusListener = ::android::sp<IDataLoaderStatusListener>;
Alex Buynytskyy04f73912020-02-10 08:34:18 -080066
Alex Buynytskyy8ef61ae2020-05-08 16:18:52 -070067using StorageHealthCheckParams = ::android::os::incremental::StorageHealthCheckParams;
68using IStorageHealthListener = ::android::os::incremental::IStorageHealthListener;
69using StorageHealthListener = ::android::sp<IStorageHealthListener>;
Songchun Fana7098592020-09-03 11:45:53 -070070using IStorageLoadingProgressListener = ::android::os::incremental::IStorageLoadingProgressListener;
71using StorageLoadingProgressListener = ::android::sp<IStorageLoadingProgressListener>;
Alex Buynytskyy8ef61ae2020-05-08 16:18:52 -070072
Alex Buynytskyyaa8e95e2020-12-14 21:50:04 -080073using PerUidReadTimeouts = ::android::os::incremental::PerUidReadTimeouts;
74
Yurii Zubrytskyi4a25dfb2020-01-10 11:53:24 -080075class IncrementalService final {
Songchun Fan3c82a302019-11-29 14:23:45 -080076public:
Yurii Zubrytskyi4a25dfb2020-01-10 11:53:24 -080077 explicit IncrementalService(ServiceManagerWrapper&& sm, std::string_view rootDir);
Songchun Fan3c82a302019-11-29 14:23:45 -080078
79#pragma GCC diagnostic push
80#pragma GCC diagnostic ignored "-Wnon-virtual-dtor"
81 ~IncrementalService();
82#pragma GCC diagnostic pop
83
84 static constexpr StorageId kInvalidStorageId = -1;
85 static constexpr StorageId kMaxStorageId = std::numeric_limits<int>::max();
86
Alex Buynytskyy8ef61ae2020-05-08 16:18:52 -070087 static constexpr BootClockTsUs kMaxBootClockTsUs = std::numeric_limits<BootClockTsUs>::max();
88
Songchun Fan3c82a302019-11-29 14:23:45 -080089 enum CreateOptions {
90 TemporaryBind = 1,
91 PermanentBind = 2,
92 CreateNew = 4,
93 OpenExisting = 8,
94
95 Default = TemporaryBind | CreateNew
96 };
97
98 enum class BindKind {
99 Temporary = 0,
100 Permanent = 1,
101 };
102
Alex Buynytskyy3697d9e2020-06-06 20:15:58 -0700103 enum StorageFlags {
Alex Buynytskyyaa8e95e2020-12-14 21:50:04 -0800104 ReadLogsAllowed = 1 << 0,
105 ReadLogsEnabled = 1 << 1,
106 };
107
108 struct LoadingProgress {
109 ssize_t filledBlocks;
110 ssize_t totalBlocks;
111
112 bool isError() const { return totalBlocks < 0; }
113 bool started() const { return totalBlocks > 0; }
114 bool fullyLoaded() const { return !isError() && (totalBlocks == filledBlocks); }
115
Alex Buynytskyy07694ed2021-01-27 06:58:55 -0800116 int blocksRemainingOrError() const {
117 return totalBlocks <= 0 ? totalBlocks : totalBlocks - filledBlocks;
118 }
119
Alex Buynytskyyaa8e95e2020-12-14 21:50:04 -0800120 float getProgress() const {
121 return totalBlocks < 0
122 ? totalBlocks
123 : totalBlocks > 0 ? double(filledBlocks) / double(totalBlocks) : 1.f;
124 }
Alex Buynytskyy3697d9e2020-06-06 20:15:58 -0700125 };
126
Yurii Zubrytskyi4a25dfb2020-01-10 11:53:24 -0800127 static FileId idFromMetadata(std::span<const uint8_t> metadata);
128 static inline FileId idFromMetadata(std::span<const char> metadata) {
129 return idFromMetadata({(const uint8_t*)metadata.data(), metadata.size()});
130 }
131
Alex Buynytskyy18b07a42020-02-03 20:06:00 -0800132 void onDump(int fd);
133
Alex Buynytskyy0ea4ff42020-04-09 17:25:42 -0700134 void onSystemReady();
Songchun Fan3c82a302019-11-29 14:23:45 -0800135
Yurii Zubrytskyi629051fd2020-04-17 23:13:47 -0700136 StorageId createStorage(std::string_view mountPoint,
Alex Buynytskyy07694ed2021-01-27 06:58:55 -0800137 const content::pm::DataLoaderParamsParcel& dataLoaderParams,
138 CreateOptions options);
Songchun Fan3c82a302019-11-29 14:23:45 -0800139 StorageId createLinkedStorage(std::string_view mountPoint, StorageId linkedStorage,
140 CreateOptions options = CreateOptions::Default);
Yurii Zubrytskyi4a25dfb2020-01-10 11:53:24 -0800141 StorageId openStorage(std::string_view path);
Songchun Fan3c82a302019-11-29 14:23:45 -0800142
Alex Buynytskyy07694ed2021-01-27 06:58:55 -0800143 bool startLoading(StorageId storage, content::pm::DataLoaderParamsParcel&& dataLoaderParams,
144 const DataLoaderStatusListener& statusListener,
145 StorageHealthCheckParams&& healthCheckParams,
146 const StorageHealthListener& healthListener,
147 const std::vector<PerUidReadTimeouts>& perUidReadTimeouts);
148
Yurii Zubrytskyi4a25dfb2020-01-10 11:53:24 -0800149 int bind(StorageId storage, std::string_view source, std::string_view target, BindKind kind);
Songchun Fan3c82a302019-11-29 14:23:45 -0800150 int unbind(StorageId storage, std::string_view target);
151 void deleteStorage(StorageId storage);
152
Alex Buynytskyyaa8e95e2020-12-14 21:50:04 -0800153 void disallowReadLogs(StorageId storage);
Alex Buynytskyy5e860ba2020-03-31 15:30:21 -0700154 int setStorageParams(StorageId storage, bool enableReadLogs);
155
Yurii Zubrytskyi4a25dfb2020-01-10 11:53:24 -0800156 int makeFile(StorageId storage, std::string_view path, int mode, FileId id,
Alex Buynytskyyb39d13e2020-09-12 16:12:36 -0700157 incfs::NewFileParams params, std::span<const uint8_t> data);
Songchun Fan96100932020-02-03 19:20:58 -0800158 int makeDir(StorageId storage, std::string_view path, int mode = 0755);
159 int makeDirs(StorageId storage, std::string_view path, int mode = 0755);
Songchun Fan3c82a302019-11-29 14:23:45 -0800160
Yurii Zubrytskyi4a25dfb2020-01-10 11:53:24 -0800161 int link(StorageId sourceStorageId, std::string_view oldPath, StorageId destStorageId,
162 std::string_view newPath);
163 int unlink(StorageId storage, std::string_view path);
Songchun Fan3c82a302019-11-29 14:23:45 -0800164
Alex Buynytskyyaa8e95e2020-12-14 21:50:04 -0800165 int isFileFullyLoaded(StorageId storage, std::string_view filePath) const;
Alex Buynytskyy07694ed2021-01-27 06:58:55 -0800166
167 LoadingProgress getLoadingProgress(StorageId storage, bool stopOnFirstIncomplete) const;
168
Songchun Fana7098592020-09-03 11:45:53 -0700169 bool registerLoadingProgressListener(StorageId storage,
170 const StorageLoadingProgressListener& progressListener);
171 bool unregisterLoadingProgressListener(StorageId storage);
Songchun Fan2570ec02020-10-08 17:22:33 -0700172 bool registerStorageHealthListener(StorageId storage,
173 StorageHealthCheckParams&& healthCheckParams,
174 const StorageHealthListener& healthListener);
175 void unregisterStorageHealthListener(StorageId storage);
Yurii Zubrytskyi629051fd2020-04-17 23:13:47 -0700176 RawMetadata getMetadata(StorageId storage, std::string_view path) const;
Yurii Zubrytskyi4a25dfb2020-01-10 11:53:24 -0800177 RawMetadata getMetadata(StorageId storage, FileId node) const;
Songchun Fan3c82a302019-11-29 14:23:45 -0800178
Songchun Fan0f8b6fe2020-02-05 17:41:25 -0800179 bool configureNativeBinaries(StorageId storage, std::string_view apkFullPath,
Songchun Fan14f6c3c2020-05-21 18:19:07 -0700180 std::string_view libDirRelativePath, std::string_view abi,
181 bool extractNativeLibs);
Yurii Zubrytskyida208012020-04-07 15:35:21 -0700182 bool waitForNativeBinariesExtraction(StorageId storage);
Alex Buynytskyy96e350b2020-04-02 20:03:47 -0700183
Alex Buynytskyy96e350b2020-04-02 20:03:47 -0700184 class AppOpsListener : public android::BnAppOpsCallback {
185 public:
Yurii Zubrytskyida208012020-04-07 15:35:21 -0700186 AppOpsListener(IncrementalService& incrementalService, std::string packageName)
187 : incrementalService(incrementalService), packageName(std::move(packageName)) {}
Alex Buynytskyyf4156792020-04-07 14:26:55 -0700188 void opChanged(int32_t op, const String16& packageName) final;
Alex Buynytskyy96e350b2020-04-02 20:03:47 -0700189
190 private:
191 IncrementalService& incrementalService;
192 const std::string packageName;
193 };
194
Yurii Zubrytskyi629051fd2020-04-17 23:13:47 -0700195 class IncrementalServiceConnector : public os::incremental::BnIncrementalServiceConnector {
Alex Buynytskyyf4156792020-04-07 14:26:55 -0700196 public:
197 IncrementalServiceConnector(IncrementalService& incrementalService, int32_t storage)
Alex Buynytskyy5f9e3a02020-04-07 21:13:41 -0700198 : incrementalService(incrementalService), storage(storage) {}
Alex Buynytskyyf4156792020-04-07 14:26:55 -0700199 binder::Status setStorageParams(bool enableReadLogs, int32_t* _aidl_return) final;
200
201 private:
202 IncrementalService& incrementalService;
Alex Buynytskyy5f9e3a02020-04-07 21:13:41 -0700203 int32_t const storage;
Alex Buynytskyyf4156792020-04-07 14:26:55 -0700204 };
205
Songchun Fan3c82a302019-11-29 14:23:45 -0800206private:
Alex Buynytskyy0ea4ff42020-04-09 17:25:42 -0700207 struct IncFsMount;
208
Yurii Zubrytskyi629051fd2020-04-17 23:13:47 -0700209 class DataLoaderStub : public content::pm::BnDataLoaderStatusListener {
Alex Buynytskyy0ea4ff42020-04-09 17:25:42 -0700210 public:
Yurii Zubrytskyi629051fd2020-04-17 23:13:47 -0700211 DataLoaderStub(IncrementalService& service, MountId id,
212 content::pm::DataLoaderParamsParcel&& params,
213 content::pm::FileSystemControlParcel&& control,
Alex Buynytskyy8ef61ae2020-05-08 16:18:52 -0700214 const DataLoaderStatusListener* statusListener,
215 StorageHealthCheckParams&& healthCheckParams,
216 const StorageHealthListener* healthListener, std::string&& healthPath);
Alex Buynytskyy0ea4ff42020-04-09 17:25:42 -0700217 ~DataLoaderStub();
Alex Buynytskyy9a54579a2020-04-17 15:34:47 -0700218 // Cleans up the internal state and invalidates DataLoaderStub. Any subsequent calls will
219 // result in an error.
220 void cleanupResources();
Alex Buynytskyy0ea4ff42020-04-09 17:25:42 -0700221
Alex Buynytskyyab65cb12020-04-17 10:01:47 -0700222 bool requestCreate();
Alex Buynytskyy0b202662020-04-13 09:53:04 -0700223 bool requestStart();
Alex Buynytskyyab65cb12020-04-17 10:01:47 -0700224 bool requestDestroy();
Alex Buynytskyy0ea4ff42020-04-09 17:25:42 -0700225
Alex Buynytskyyab65cb12020-04-17 10:01:47 -0700226 void onDump(int fd);
227
Alex Buynytskyy4760d8f2020-05-08 16:18:52 -0700228 MountId id() const { return mId.load(std::memory_order_relaxed); }
Yurii Zubrytskyi629051fd2020-04-17 23:13:47 -0700229 const content::pm::DataLoaderParamsParcel& params() const { return mParams; }
Songchun Fan2570ec02020-10-08 17:22:33 -0700230 void setHealthListener(StorageHealthCheckParams&& healthCheckParams,
231 const StorageHealthListener* healthListener);
Alex Buynytskyy0ea4ff42020-04-09 17:25:42 -0700232
233 private:
234 binder::Status onStatusChanged(MountId mount, int newStatus) final;
Songchun Fan33093982020-09-10 13:12:39 -0700235 binder::Status reportStreamHealth(MountId mount, int newStatus) final;
Alex Buynytskyy0ea4ff42020-04-09 17:25:42 -0700236
Alex Buynytskyy0bdbccf2020-04-23 20:36:42 -0700237 sp<content::pm::IDataLoader> getDataLoader();
Alex Buynytskyy9a54579a2020-04-17 15:34:47 -0700238
Alex Buynytskyyea1390f2020-04-22 16:08:50 -0700239 bool bind();
Alex Buynytskyyab65cb12020-04-17 10:01:47 -0700240 bool create();
Alex Buynytskyy0b202662020-04-13 09:53:04 -0700241 bool start();
Alex Buynytskyyab65cb12020-04-17 10:01:47 -0700242 bool destroy();
243
244 bool setTargetStatus(int status);
Alex Buynytskyy7e0a1a82020-04-27 17:06:10 -0700245 void setTargetStatusLocked(int status);
Alex Buynytskyyab65cb12020-04-17 10:01:47 -0700246
247 bool fsmStep();
Alex Buynytskyy4760d8f2020-05-08 16:18:52 -0700248 bool fsmStep(int currentStatus, int targetStatus);
Alex Buynytskyy0b202662020-04-13 09:53:04 -0700249
Alex Buynytskyy4760d8f2020-05-08 16:18:52 -0700250 void onHealthStatus(StorageHealthListener healthListener, int healthStatus);
251 void updateHealthStatus(bool baseline = false);
252
253 bool isValid() const { return id() != kInvalidStorageId; }
254
255 bool isHealthParamsValid() const;
256
257 const incfs::UniqueControl& initializeHealthControl();
258 void resetHealthControl();
259
260 BootClockTsUs getOldestPendingReadTs();
261
262 void registerForPendingReads();
263 void unregisterFromPendingReads();
Alex Buynytskyyd0855a32020-05-07 18:40:51 -0700264
Alex Buynytskyy0ea4ff42020-04-09 17:25:42 -0700265 IncrementalService& mService;
Alex Buynytskyyb0ea4482020-05-04 18:39:58 -0700266
267 std::mutex mMutex;
Alex Buynytskyy4760d8f2020-05-08 16:18:52 -0700268 std::atomic<MountId> mId = kInvalidStorageId;
Yurii Zubrytskyi629051fd2020-04-17 23:13:47 -0700269 content::pm::DataLoaderParamsParcel mParams;
270 content::pm::FileSystemControlParcel mControl;
Alex Buynytskyy8ef61ae2020-05-08 16:18:52 -0700271 DataLoaderStatusListener mStatusListener;
272 StorageHealthListener mHealthListener;
Alex Buynytskyy0ea4ff42020-04-09 17:25:42 -0700273
Alex Buynytskyy0b202662020-04-13 09:53:04 -0700274 std::condition_variable mStatusCondition;
Yurii Zubrytskyi629051fd2020-04-17 23:13:47 -0700275 int mCurrentStatus = content::pm::IDataLoaderStatusListener::DATA_LOADER_DESTROYED;
276 int mTargetStatus = content::pm::IDataLoaderStatusListener::DATA_LOADER_DESTROYED;
Alex Buynytskyyab65cb12020-04-17 10:01:47 -0700277 TimePoint mTargetStatusTs = {};
Alex Buynytskyycca2c112020-05-05 12:48:41 -0700278
Alex Buynytskyyd0855a32020-05-07 18:40:51 -0700279 std::string mHealthPath;
280 incfs::UniqueControl mHealthControl;
Alex Buynytskyy4760d8f2020-05-08 16:18:52 -0700281 struct {
282 TimePoint userTs;
283 BootClockTsUs kernelTsUs;
284 } mHealthBase = {TimePoint::max(), kMaxBootClockTsUs};
285 StorageHealthCheckParams mHealthCheckParams;
Songchun Fan2570ec02020-10-08 17:22:33 -0700286 int mStreamStatus = content::pm::IDataLoaderStatusListener::STREAM_HEALTHY;
Songchun Fan6944f1e2020-11-06 15:24:24 -0800287 std::vector<incfs::ReadInfo> mLastPendingReads;
Alex Buynytskyy0ea4ff42020-04-09 17:25:42 -0700288 };
289 using DataLoaderStubPtr = sp<DataLoaderStub>;
290
Songchun Fan3c82a302019-11-29 14:23:45 -0800291 struct IncFsMount {
292 struct Bind {
293 StorageId storage;
294 std::string savedFilename;
295 std::string sourceDir;
296 BindKind kind;
297 };
298
299 struct Storage {
300 std::string name;
Songchun Fan3c82a302019-11-29 14:23:45 -0800301 };
302
Yurii Zubrytskyi4a25dfb2020-01-10 11:53:24 -0800303 using Control = incfs::UniqueControl;
Songchun Fan3c82a302019-11-29 14:23:45 -0800304
Yurii Zubrytskyi629051fd2020-04-17 23:13:47 -0700305 using BindMap = std::map<std::string, Bind, path::PathLess>;
Songchun Fan3c82a302019-11-29 14:23:45 -0800306 using StorageMap = std::unordered_map<StorageId, Storage>;
307
308 mutable std::mutex lock;
309 const std::string root;
310 Control control;
311 /*const*/ MountId mountId;
Alex Buynytskyyaa8e95e2020-12-14 21:50:04 -0800312 int32_t flags = StorageFlags::ReadLogsAllowed;
Songchun Fan3c82a302019-11-29 14:23:45 -0800313 StorageMap storages;
314 BindMap bindPoints;
Alex Buynytskyy0ea4ff42020-04-09 17:25:42 -0700315 DataLoaderStubPtr dataLoaderStub;
Songchun Fan3c82a302019-11-29 14:23:45 -0800316 std::atomic<int> nextStorageDirNo{0};
Songchun Fan3c82a302019-11-29 14:23:45 -0800317 const IncrementalService& incrementalService;
318
319 IncFsMount(std::string root, MountId mountId, Control control,
320 const IncrementalService& incrementalService)
321 : root(std::move(root)),
322 control(std::move(control)),
323 mountId(mountId),
Alex Buynytskyy1d892162020-04-03 23:00:19 -0700324 incrementalService(incrementalService) {}
Songchun Fan3c82a302019-11-29 14:23:45 -0800325 IncFsMount(IncFsMount&&) = delete;
326 IncFsMount& operator=(IncFsMount&&) = delete;
327 ~IncFsMount();
328
329 StorageMap::iterator makeStorage(StorageId id);
330
Alex Buynytskyyaa8e95e2020-12-14 21:50:04 -0800331 void disallowReadLogs() { flags &= ~StorageFlags::ReadLogsAllowed; }
332 int32_t readLogsAllowed() const { return (flags & StorageFlags::ReadLogsAllowed); }
333
334 void setReadLogsEnabled(bool value) {
335 if (value)
336 flags |= StorageFlags::ReadLogsEnabled;
337 else
338 flags &= ~StorageFlags::ReadLogsEnabled;
339 }
Alex Buynytskyy3697d9e2020-06-06 20:15:58 -0700340 int32_t readLogsEnabled() const { return (flags & StorageFlags::ReadLogsEnabled); }
341
Songchun Fan3c82a302019-11-29 14:23:45 -0800342 static void cleanupFilesystem(std::string_view root);
343 };
344
345 using IfsMountPtr = std::shared_ptr<IncFsMount>;
346 using MountMap = std::unordered_map<MountId, IfsMountPtr>;
347 using BindPathMap = std::map<std::string, IncFsMount::BindMap::iterator, path::PathLess>;
348
Yurii Zubrytskyi629051fd2020-04-17 23:13:47 -0700349 static bool perfLoggingEnabled();
350
Alex Buynytskyyaa8e95e2020-12-14 21:50:04 -0800351 void setUidReadTimeouts(StorageId storage,
352 const std::vector<PerUidReadTimeouts>& perUidReadTimeouts);
353 void clearUidReadTimeouts(StorageId storage);
354 void updateUidReadTimeouts(StorageId storage, Clock::time_point timeLimit);
355
Yurii Zubrytskyi629051fd2020-04-17 23:13:47 -0700356 std::unordered_set<std::string_view> adoptMountedInstances();
357 void mountExistingImages(const std::unordered_set<std::string_view>& mountedRootNames);
Yurii Zubrytskyi107ae352020-04-03 13:12:51 -0700358 bool mountExistingImage(std::string_view root);
Songchun Fan3c82a302019-11-29 14:23:45 -0800359
360 IfsMountPtr getIfs(StorageId storage) const;
361 const IfsMountPtr& getIfsLocked(StorageId storage) const;
Yurii Zubrytskyi4a25dfb2020-01-10 11:53:24 -0800362 int addBindMount(IncFsMount& ifs, StorageId storage, std::string_view storageRoot,
363 std::string&& source, std::string&& target, BindKind kind,
364 std::unique_lock<std::mutex>& mainLock);
Songchun Fan3c82a302019-11-29 14:23:45 -0800365
366 int addBindMountWithMd(IncFsMount& ifs, StorageId storage, std::string&& metadataName,
Yurii Zubrytskyi4a25dfb2020-01-10 11:53:24 -0800367 std::string&& source, std::string&& target, BindKind kind,
Songchun Fan3c82a302019-11-29 14:23:45 -0800368 std::unique_lock<std::mutex>& mainLock);
369
Yurii Zubrytskyi629051fd2020-04-17 23:13:47 -0700370 void addBindMountRecordLocked(IncFsMount& ifs, StorageId storage, std::string&& metadataName,
371 std::string&& source, std::string&& target, BindKind kind);
372
373 DataLoaderStubPtr prepareDataLoader(IncFsMount& ifs,
374 content::pm::DataLoaderParamsParcel&& params,
Alex Buynytskyy8ef61ae2020-05-08 16:18:52 -0700375 const DataLoaderStatusListener* statusListener = nullptr,
376 StorageHealthCheckParams&& healthCheckParams = {},
377 const StorageHealthListener* healthListener = nullptr);
Yurii Zubrytskyi629051fd2020-04-17 23:13:47 -0700378 void prepareDataLoaderLocked(IncFsMount& ifs, content::pm::DataLoaderParamsParcel&& params,
Alex Buynytskyy8ef61ae2020-05-08 16:18:52 -0700379 const DataLoaderStatusListener* statusListener = nullptr,
380 StorageHealthCheckParams&& healthCheckParams = {},
381 const StorageHealthListener* healthListener = nullptr);
Alex Buynytskyybf1c0632020-03-10 15:49:29 -0700382
Songchun Fan3c82a302019-11-29 14:23:45 -0800383 BindPathMap::const_iterator findStorageLocked(std::string_view path) const;
384 StorageId findStorageId(std::string_view path) const;
385
386 void deleteStorage(IncFsMount& ifs);
387 void deleteStorageLocked(IncFsMount& ifs, std::unique_lock<std::mutex>&& ifsLock);
388 MountMap::iterator getStorageSlotLocked();
Yurii Zubrytskyiefebb452020-04-22 13:59:06 -0700389 std::string normalizePathToStorage(const IncFsMount& incfs, StorageId storage,
Yurii Zubrytskyi629051fd2020-04-17 23:13:47 -0700390 std::string_view path) const;
Yurii Zubrytskyiefebb452020-04-22 13:59:06 -0700391 std::string normalizePathToStorageLocked(const IncFsMount& incfs,
392 IncFsMount::StorageMap::const_iterator storageIt,
Yurii Zubrytskyi629051fd2020-04-17 23:13:47 -0700393 std::string_view path) const;
Yurii Zubrytskyiefebb452020-04-22 13:59:06 -0700394 int makeDirs(const IncFsMount& ifs, StorageId storageId, std::string_view path, int mode);
Alex Buynytskyy1d892162020-04-03 23:00:19 -0700395 binder::Status applyStorageParams(IncFsMount& ifs, bool enableReadLogs);
Alex Buynytskyy96e350b2020-04-02 20:03:47 -0700396
Alex Buynytskyybc0a7e62020-08-25 12:45:22 -0700397 int isFileFullyLoadedFromPath(const IncFsMount& ifs, std::string_view filePath) const;
Alex Buynytskyy07694ed2021-01-27 06:58:55 -0800398 LoadingProgress getLoadingProgressFromPath(const IncFsMount& ifs, std::string_view path,
399 bool stopOnFirstIncomplete) const;
Songchun Fan374f7652020-08-20 08:40:29 -0700400
Alex Buynytskyyb39d13e2020-09-12 16:12:36 -0700401 int setFileContent(const IfsMountPtr& ifs, const incfs::FileId& fileId,
402 std::string_view debugFilePath, std::span<const uint8_t> data) const;
403
Alex Buynytskyy96e350b2020-04-02 20:03:47 -0700404 void registerAppOpsCallback(const std::string& packageName);
Alex Buynytskyy1d892162020-04-03 23:00:19 -0700405 bool unregisterAppOpsCallback(const std::string& packageName);
406 void onAppOpChanged(const std::string& packageName);
Alex Buynytskyy96e350b2020-04-02 20:03:47 -0700407
Yurii Zubrytskyida208012020-04-07 15:35:21 -0700408 void runJobProcessing();
409 void extractZipFile(const IfsMountPtr& ifs, ZipArchiveHandle zipFile, ZipEntry& entry,
Alex Buynytskyyb39d13e2020-09-12 16:12:36 -0700410 const incfs::FileId& libFileId, std::string_view debugLibPath,
Yurii Zubrytskyida208012020-04-07 15:35:21 -0700411 Clock::time_point scheduledTs);
412
Alex Buynytskyycca2c112020-05-05 12:48:41 -0700413 void runCmdLooper();
414
Songchun Fana7098592020-09-03 11:45:53 -0700415 bool addTimedJob(TimedQueueWrapper& timedQueue, MountId id, Milliseconds after, Job what);
416 bool removeTimedJobs(TimedQueueWrapper& timedQueue, MountId id);
417 bool updateLoadingProgress(int32_t storageId,
418 const StorageLoadingProgressListener& progressListener);
Alex Buynytskyy4760d8f2020-05-08 16:18:52 -0700419
Yurii Zubrytskyida208012020-04-07 15:35:21 -0700420private:
Yurii Zubrytskyi86321402020-04-09 19:22:30 -0700421 const std::unique_ptr<VoldServiceWrapper> mVold;
422 const std::unique_ptr<DataLoaderManagerWrapper> mDataLoaderManager;
423 const std::unique_ptr<IncFsWrapper> mIncFs;
424 const std::unique_ptr<AppOpsManagerWrapper> mAppOpsManager;
425 const std::unique_ptr<JniWrapper> mJni;
Alex Buynytskyycca2c112020-05-05 12:48:41 -0700426 const std::unique_ptr<LooperWrapper> mLooper;
Alex Buynytskyy46d3ddb2020-05-29 12:05:05 -0700427 const std::unique_ptr<TimedQueueWrapper> mTimedQueue;
Songchun Fana7098592020-09-03 11:45:53 -0700428 const std::unique_ptr<TimedQueueWrapper> mProgressUpdateJobQueue;
Songchun Fan374f7652020-08-20 08:40:29 -0700429 const std::unique_ptr<FsWrapper> mFs;
Songchun Fan3c82a302019-11-29 14:23:45 -0800430 const std::string mIncrementalDir;
431
432 mutable std::mutex mLock;
433 mutable std::mutex mMountOperationLock;
434 MountMap mMounts;
435 BindPathMap mBindsByPath;
436
Alex Buynytskyy96e350b2020-04-02 20:03:47 -0700437 std::mutex mCallbacksLock;
Alex Buynytskyy1d892162020-04-03 23:00:19 -0700438 std::map<std::string, sp<AppOpsListener>> mCallbackRegistered;
Alex Buynytskyy96e350b2020-04-02 20:03:47 -0700439
Songchun Fan3c82a302019-11-29 14:23:45 -0800440 std::atomic_bool mSystemReady = false;
441 StorageId mNextId = 0;
Yurii Zubrytskyida208012020-04-07 15:35:21 -0700442
Alex Buynytskyycca2c112020-05-05 12:48:41 -0700443 std::atomic_bool mRunning{true};
444
Yurii Zubrytskyi721ac4d2020-04-13 11:34:32 -0700445 std::unordered_map<MountId, std::vector<Job>> mJobQueue;
446 MountId mPendingJobsMount = kInvalidStorageId;
Yurii Zubrytskyida208012020-04-07 15:35:21 -0700447 std::condition_variable mJobCondition;
448 std::mutex mJobMutex;
449 std::thread mJobProcessor;
Alex Buynytskyycca2c112020-05-05 12:48:41 -0700450
451 std::thread mCmdLooperThread;
Songchun Fan3c82a302019-11-29 14:23:45 -0800452};
453
454} // namespace android::incremental