blob: 640ca53e80d495091da9ac4b16486bda7b833bbf [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>
24#include <binder/IAppOpsCallback.h>
Songchun Fan3c82a302019-11-29 14:23:45 -080025#include <utils/String16.h>
26#include <utils/StrongPointer.h>
Yurii Zubrytskyida208012020-04-07 15:35:21 -070027#include <ziparchive/zip_archive.h>
Songchun Fan3c82a302019-11-29 14:23:45 -080028
29#include <atomic>
30#include <chrono>
Yurii Zubrytskyida208012020-04-07 15:35:21 -070031#include <condition_variable>
32#include <functional>
Songchun Fan3c82a302019-11-29 14:23:45 -080033#include <limits>
34#include <map>
35#include <mutex>
Songchun Fan9b753082020-02-26 13:08:06 -080036#include <span>
Songchun Fan3c82a302019-11-29 14:23:45 -080037#include <string>
38#include <string_view>
Yurii Zubrytskyida208012020-04-07 15:35:21 -070039#include <thread>
Songchun Fan3c82a302019-11-29 14:23:45 -080040#include <unordered_map>
Yurii Zubrytskyi629051fd2020-04-17 23:13:47 -070041#include <unordered_set>
Songchun Fan3c82a302019-11-29 14:23:45 -080042#include <utility>
43#include <vector>
44
45#include "ServiceWrappers.h"
Songchun Fan3c82a302019-11-29 14:23:45 -080046#include "incfs.h"
47#include "path.h"
48
Songchun Fan3c82a302019-11-29 14:23:45 -080049namespace android::incremental {
50
51using MountId = int;
52using StorageId = int;
Yurii Zubrytskyi4a25dfb2020-01-10 11:53:24 -080053using FileId = incfs::FileId;
Songchun Fan3c82a302019-11-29 14:23:45 -080054using BlockIndex = incfs::BlockIndex;
55using RawMetadata = incfs::RawMetadata;
56using Clock = std::chrono::steady_clock;
57using TimePoint = std::chrono::time_point<Clock>;
58using Seconds = std::chrono::seconds;
59
Alex Buynytskyy0ea4ff42020-04-09 17:25:42 -070060using IDataLoaderStatusListener = ::android::content::pm::IDataLoaderStatusListener;
61using DataLoaderStatusListener = ::android::sp<IDataLoaderStatusListener>;
Alex Buynytskyy04f73912020-02-10 08:34:18 -080062
Yurii Zubrytskyi4a25dfb2020-01-10 11:53:24 -080063class IncrementalService final {
Songchun Fan3c82a302019-11-29 14:23:45 -080064public:
Yurii Zubrytskyi4a25dfb2020-01-10 11:53:24 -080065 explicit IncrementalService(ServiceManagerWrapper&& sm, std::string_view rootDir);
Songchun Fan3c82a302019-11-29 14:23:45 -080066
67#pragma GCC diagnostic push
68#pragma GCC diagnostic ignored "-Wnon-virtual-dtor"
69 ~IncrementalService();
70#pragma GCC diagnostic pop
71
72 static constexpr StorageId kInvalidStorageId = -1;
73 static constexpr StorageId kMaxStorageId = std::numeric_limits<int>::max();
74
75 enum CreateOptions {
76 TemporaryBind = 1,
77 PermanentBind = 2,
78 CreateNew = 4,
79 OpenExisting = 8,
80
81 Default = TemporaryBind | CreateNew
82 };
83
84 enum class BindKind {
85 Temporary = 0,
86 Permanent = 1,
87 };
88
Yurii Zubrytskyi4a25dfb2020-01-10 11:53:24 -080089 static FileId idFromMetadata(std::span<const uint8_t> metadata);
90 static inline FileId idFromMetadata(std::span<const char> metadata) {
91 return idFromMetadata({(const uint8_t*)metadata.data(), metadata.size()});
92 }
93
Alex Buynytskyy18b07a42020-02-03 20:06:00 -080094 void onDump(int fd);
95
Alex Buynytskyy0ea4ff42020-04-09 17:25:42 -070096 void onSystemReady();
Songchun Fan3c82a302019-11-29 14:23:45 -080097
Yurii Zubrytskyi629051fd2020-04-17 23:13:47 -070098 StorageId createStorage(std::string_view mountPoint,
99 content::pm::DataLoaderParamsParcel&& dataLoaderParams,
Alex Buynytskyy04f73912020-02-10 08:34:18 -0800100 const DataLoaderStatusListener& dataLoaderStatusListener,
Songchun Fan3c82a302019-11-29 14:23:45 -0800101 CreateOptions options = CreateOptions::Default);
102 StorageId createLinkedStorage(std::string_view mountPoint, StorageId linkedStorage,
103 CreateOptions options = CreateOptions::Default);
Yurii Zubrytskyi4a25dfb2020-01-10 11:53:24 -0800104 StorageId openStorage(std::string_view path);
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
Alex Buynytskyy5e860ba2020-03-31 15:30:21 -0700110 int setStorageParams(StorageId storage, bool enableReadLogs);
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);
Songchun Fan96100932020-02-03 19:20:58 -0800114 int makeDir(StorageId storage, std::string_view path, int mode = 0755);
115 int makeDirs(StorageId storage, std::string_view path, int mode = 0755);
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 Zubrytskyi629051fd2020-04-17 23:13:47 -0700125 RawMetadata getMetadata(StorageId storage, std::string_view path) const;
Yurii Zubrytskyi4a25dfb2020-01-10 11:53:24 -0800126 RawMetadata getMetadata(StorageId storage, FileId node) const;
Songchun Fan3c82a302019-11-29 14:23:45 -0800127
Songchun Fan3c82a302019-11-29 14:23:45 -0800128 bool startLoading(StorageId storage) const;
Yurii Zubrytskyida208012020-04-07 15:35:21 -0700129
Songchun Fan0f8b6fe2020-02-05 17:41:25 -0800130 bool configureNativeBinaries(StorageId storage, std::string_view apkFullPath,
131 std::string_view libDirRelativePath, std::string_view abi);
Yurii Zubrytskyida208012020-04-07 15:35:21 -0700132 bool waitForNativeBinariesExtraction(StorageId storage);
Alex Buynytskyy96e350b2020-04-02 20:03:47 -0700133
Alex Buynytskyy96e350b2020-04-02 20:03:47 -0700134 class AppOpsListener : public android::BnAppOpsCallback {
135 public:
Yurii Zubrytskyida208012020-04-07 15:35:21 -0700136 AppOpsListener(IncrementalService& incrementalService, std::string packageName)
137 : incrementalService(incrementalService), packageName(std::move(packageName)) {}
Alex Buynytskyyf4156792020-04-07 14:26:55 -0700138 void opChanged(int32_t op, const String16& packageName) final;
Alex Buynytskyy96e350b2020-04-02 20:03:47 -0700139
140 private:
141 IncrementalService& incrementalService;
142 const std::string packageName;
143 };
144
Yurii Zubrytskyi629051fd2020-04-17 23:13:47 -0700145 class IncrementalServiceConnector : public os::incremental::BnIncrementalServiceConnector {
Alex Buynytskyyf4156792020-04-07 14:26:55 -0700146 public:
147 IncrementalServiceConnector(IncrementalService& incrementalService, int32_t storage)
Alex Buynytskyy5f9e3a02020-04-07 21:13:41 -0700148 : incrementalService(incrementalService), storage(storage) {}
Alex Buynytskyyf4156792020-04-07 14:26:55 -0700149 binder::Status setStorageParams(bool enableReadLogs, int32_t* _aidl_return) final;
150
151 private:
152 IncrementalService& incrementalService;
Alex Buynytskyy5f9e3a02020-04-07 21:13:41 -0700153 int32_t const storage;
Alex Buynytskyyf4156792020-04-07 14:26:55 -0700154 };
155
Songchun Fan3c82a302019-11-29 14:23:45 -0800156private:
Alex Buynytskyy0ea4ff42020-04-09 17:25:42 -0700157 struct IncFsMount;
158
Yurii Zubrytskyi629051fd2020-04-17 23:13:47 -0700159 class DataLoaderStub : public content::pm::BnDataLoaderStatusListener {
Alex Buynytskyy0ea4ff42020-04-09 17:25:42 -0700160 public:
Yurii Zubrytskyi629051fd2020-04-17 23:13:47 -0700161 DataLoaderStub(IncrementalService& service, MountId id,
162 content::pm::DataLoaderParamsParcel&& params,
163 content::pm::FileSystemControlParcel&& control,
Alex Buynytskyycca2c112020-05-05 12:48:41 -0700164 incfs::UniqueControl&& healthControl,
Alex Buynytskyyab65cb12020-04-17 10:01:47 -0700165 const DataLoaderStatusListener* externalListener);
Alex Buynytskyy0ea4ff42020-04-09 17:25:42 -0700166 ~DataLoaderStub();
Alex Buynytskyy9a54579a2020-04-17 15:34:47 -0700167 // Cleans up the internal state and invalidates DataLoaderStub. Any subsequent calls will
168 // result in an error.
169 void cleanupResources();
Alex Buynytskyy0ea4ff42020-04-09 17:25:42 -0700170
Alex Buynytskyyab65cb12020-04-17 10:01:47 -0700171 bool requestCreate();
Alex Buynytskyy0b202662020-04-13 09:53:04 -0700172 bool requestStart();
Alex Buynytskyyab65cb12020-04-17 10:01:47 -0700173 bool requestDestroy();
Alex Buynytskyy0ea4ff42020-04-09 17:25:42 -0700174
Alex Buynytskyyab65cb12020-04-17 10:01:47 -0700175 void onDump(int fd);
176
Alex Buynytskyy0ea4ff42020-04-09 17:25:42 -0700177 MountId id() const { return mId; }
Yurii Zubrytskyi629051fd2020-04-17 23:13:47 -0700178 const content::pm::DataLoaderParamsParcel& params() const { return mParams; }
Alex Buynytskyy0ea4ff42020-04-09 17:25:42 -0700179
180 private:
181 binder::Status onStatusChanged(MountId mount, int newStatus) final;
182
Alex Buynytskyycca2c112020-05-05 12:48:41 -0700183 void addToCmdLooperLocked();
184 void removeFromCmdLooperLocked();
185 int onCmdLooperEvent();
186
Alex Buynytskyy9a54579a2020-04-17 15:34:47 -0700187 bool isValid() const { return mId != kInvalidStorageId; }
Alex Buynytskyy0bdbccf2020-04-23 20:36:42 -0700188 sp<content::pm::IDataLoader> getDataLoader();
Alex Buynytskyy9a54579a2020-04-17 15:34:47 -0700189
Alex Buynytskyyea1390f2020-04-22 16:08:50 -0700190 bool bind();
Alex Buynytskyyab65cb12020-04-17 10:01:47 -0700191 bool create();
Alex Buynytskyy0b202662020-04-13 09:53:04 -0700192 bool start();
Alex Buynytskyyab65cb12020-04-17 10:01:47 -0700193 bool destroy();
194
195 bool setTargetStatus(int status);
Alex Buynytskyy7e0a1a82020-04-27 17:06:10 -0700196 void setTargetStatusLocked(int status);
Alex Buynytskyyab65cb12020-04-17 10:01:47 -0700197
198 bool fsmStep();
Alex Buynytskyy0b202662020-04-13 09:53:04 -0700199
Alex Buynytskyy0ea4ff42020-04-09 17:25:42 -0700200 IncrementalService& mService;
Alex Buynytskyyb0ea4482020-05-04 18:39:58 -0700201
202 std::mutex mMutex;
Alex Buynytskyy9a54579a2020-04-17 15:34:47 -0700203 MountId mId = kInvalidStorageId;
Yurii Zubrytskyi629051fd2020-04-17 23:13:47 -0700204 content::pm::DataLoaderParamsParcel mParams;
205 content::pm::FileSystemControlParcel mControl;
Alex Buynytskyycca2c112020-05-05 12:48:41 -0700206 incfs::UniqueControl mHealthControl;
Alex Buynytskyy9a54579a2020-04-17 15:34:47 -0700207 DataLoaderStatusListener mListener;
Alex Buynytskyy0ea4ff42020-04-09 17:25:42 -0700208
Alex Buynytskyy0b202662020-04-13 09:53:04 -0700209 std::condition_variable mStatusCondition;
Yurii Zubrytskyi629051fd2020-04-17 23:13:47 -0700210 int mCurrentStatus = content::pm::IDataLoaderStatusListener::DATA_LOADER_DESTROYED;
211 int mTargetStatus = content::pm::IDataLoaderStatusListener::DATA_LOADER_DESTROYED;
Alex Buynytskyyab65cb12020-04-17 10:01:47 -0700212 TimePoint mTargetStatusTs = {};
Alex Buynytskyycca2c112020-05-05 12:48:41 -0700213
214 TimePoint mEarliestMissingPageTs{Clock::duration::max()};
Alex Buynytskyy0ea4ff42020-04-09 17:25:42 -0700215 };
216 using DataLoaderStubPtr = sp<DataLoaderStub>;
217
Songchun Fan3c82a302019-11-29 14:23:45 -0800218 struct IncFsMount {
219 struct Bind {
220 StorageId storage;
221 std::string savedFilename;
222 std::string sourceDir;
223 BindKind kind;
224 };
225
226 struct Storage {
227 std::string name;
Songchun Fan3c82a302019-11-29 14:23:45 -0800228 };
229
Yurii Zubrytskyi4a25dfb2020-01-10 11:53:24 -0800230 using Control = incfs::UniqueControl;
Songchun Fan3c82a302019-11-29 14:23:45 -0800231
Yurii Zubrytskyi629051fd2020-04-17 23:13:47 -0700232 using BindMap = std::map<std::string, Bind, path::PathLess>;
Songchun Fan3c82a302019-11-29 14:23:45 -0800233 using StorageMap = std::unordered_map<StorageId, Storage>;
234
235 mutable std::mutex lock;
236 const std::string root;
237 Control control;
238 /*const*/ MountId mountId;
239 StorageMap storages;
240 BindMap bindPoints;
Alex Buynytskyy0ea4ff42020-04-09 17:25:42 -0700241 DataLoaderStubPtr dataLoaderStub;
Songchun Fan3c82a302019-11-29 14:23:45 -0800242 std::atomic<int> nextStorageDirNo{0};
Songchun Fan3c82a302019-11-29 14:23:45 -0800243 const IncrementalService& incrementalService;
244
245 IncFsMount(std::string root, MountId mountId, Control control,
246 const IncrementalService& incrementalService)
247 : root(std::move(root)),
248 control(std::move(control)),
249 mountId(mountId),
Alex Buynytskyy1d892162020-04-03 23:00:19 -0700250 incrementalService(incrementalService) {}
Songchun Fan3c82a302019-11-29 14:23:45 -0800251 IncFsMount(IncFsMount&&) = delete;
252 IncFsMount& operator=(IncFsMount&&) = delete;
253 ~IncFsMount();
254
255 StorageMap::iterator makeStorage(StorageId id);
256
257 static void cleanupFilesystem(std::string_view root);
258 };
259
260 using IfsMountPtr = std::shared_ptr<IncFsMount>;
261 using MountMap = std::unordered_map<MountId, IfsMountPtr>;
262 using BindPathMap = std::map<std::string, IncFsMount::BindMap::iterator, path::PathLess>;
263
Yurii Zubrytskyi629051fd2020-04-17 23:13:47 -0700264 static bool perfLoggingEnabled();
265
266 std::unordered_set<std::string_view> adoptMountedInstances();
267 void mountExistingImages(const std::unordered_set<std::string_view>& mountedRootNames);
Yurii Zubrytskyi107ae352020-04-03 13:12:51 -0700268 bool mountExistingImage(std::string_view root);
Songchun Fan3c82a302019-11-29 14:23:45 -0800269
270 IfsMountPtr getIfs(StorageId storage) const;
271 const IfsMountPtr& getIfsLocked(StorageId storage) const;
Yurii Zubrytskyi4a25dfb2020-01-10 11:53:24 -0800272 int addBindMount(IncFsMount& ifs, StorageId storage, std::string_view storageRoot,
273 std::string&& source, std::string&& target, BindKind kind,
274 std::unique_lock<std::mutex>& mainLock);
Songchun Fan3c82a302019-11-29 14:23:45 -0800275
276 int addBindMountWithMd(IncFsMount& ifs, StorageId storage, std::string&& metadataName,
Yurii Zubrytskyi4a25dfb2020-01-10 11:53:24 -0800277 std::string&& source, std::string&& target, BindKind kind,
Songchun Fan3c82a302019-11-29 14:23:45 -0800278 std::unique_lock<std::mutex>& mainLock);
279
Yurii Zubrytskyi629051fd2020-04-17 23:13:47 -0700280 void addBindMountRecordLocked(IncFsMount& ifs, StorageId storage, std::string&& metadataName,
281 std::string&& source, std::string&& target, BindKind kind);
282
283 DataLoaderStubPtr prepareDataLoader(IncFsMount& ifs,
284 content::pm::DataLoaderParamsParcel&& params,
Alex Buynytskyy0ea4ff42020-04-09 17:25:42 -0700285 const DataLoaderStatusListener* externalListener = nullptr);
Yurii Zubrytskyi629051fd2020-04-17 23:13:47 -0700286 void prepareDataLoaderLocked(IncFsMount& ifs, content::pm::DataLoaderParamsParcel&& params,
287 const DataLoaderStatusListener* externalListener = nullptr);
Alex Buynytskyybf1c0632020-03-10 15:49:29 -0700288
Songchun Fan3c82a302019-11-29 14:23:45 -0800289 BindPathMap::const_iterator findStorageLocked(std::string_view path) const;
290 StorageId findStorageId(std::string_view path) const;
291
292 void deleteStorage(IncFsMount& ifs);
293 void deleteStorageLocked(IncFsMount& ifs, std::unique_lock<std::mutex>&& ifsLock);
294 MountMap::iterator getStorageSlotLocked();
Yurii Zubrytskyiefebb452020-04-22 13:59:06 -0700295 std::string normalizePathToStorage(const IncFsMount& incfs, StorageId storage,
Yurii Zubrytskyi629051fd2020-04-17 23:13:47 -0700296 std::string_view path) const;
Yurii Zubrytskyiefebb452020-04-22 13:59:06 -0700297 std::string normalizePathToStorageLocked(const IncFsMount& incfs,
298 IncFsMount::StorageMap::const_iterator storageIt,
Yurii Zubrytskyi629051fd2020-04-17 23:13:47 -0700299 std::string_view path) const;
Yurii Zubrytskyiefebb452020-04-22 13:59:06 -0700300 int makeDirs(const IncFsMount& ifs, StorageId storageId, std::string_view path, int mode);
Alex Buynytskyy1d892162020-04-03 23:00:19 -0700301 binder::Status applyStorageParams(IncFsMount& ifs, bool enableReadLogs);
Alex Buynytskyy96e350b2020-04-02 20:03:47 -0700302
303 void registerAppOpsCallback(const std::string& packageName);
Alex Buynytskyy1d892162020-04-03 23:00:19 -0700304 bool unregisterAppOpsCallback(const std::string& packageName);
305 void onAppOpChanged(const std::string& packageName);
Alex Buynytskyy96e350b2020-04-02 20:03:47 -0700306
Yurii Zubrytskyida208012020-04-07 15:35:21 -0700307 void runJobProcessing();
308 void extractZipFile(const IfsMountPtr& ifs, ZipArchiveHandle zipFile, ZipEntry& entry,
309 const incfs::FileId& libFileId, std::string_view targetLibPath,
310 Clock::time_point scheduledTs);
311
Alex Buynytskyycca2c112020-05-05 12:48:41 -0700312 void runCmdLooper();
313
Yurii Zubrytskyida208012020-04-07 15:35:21 -0700314private:
Yurii Zubrytskyi86321402020-04-09 19:22:30 -0700315 const std::unique_ptr<VoldServiceWrapper> mVold;
316 const std::unique_ptr<DataLoaderManagerWrapper> mDataLoaderManager;
317 const std::unique_ptr<IncFsWrapper> mIncFs;
318 const std::unique_ptr<AppOpsManagerWrapper> mAppOpsManager;
319 const std::unique_ptr<JniWrapper> mJni;
Alex Buynytskyycca2c112020-05-05 12:48:41 -0700320 const std::unique_ptr<LooperWrapper> mLooper;
Songchun Fan3c82a302019-11-29 14:23:45 -0800321 const std::string mIncrementalDir;
322
323 mutable std::mutex mLock;
324 mutable std::mutex mMountOperationLock;
325 MountMap mMounts;
326 BindPathMap mBindsByPath;
327
Alex Buynytskyy96e350b2020-04-02 20:03:47 -0700328 std::mutex mCallbacksLock;
Alex Buynytskyy1d892162020-04-03 23:00:19 -0700329 std::map<std::string, sp<AppOpsListener>> mCallbackRegistered;
Alex Buynytskyy96e350b2020-04-02 20:03:47 -0700330
Songchun Fan3c82a302019-11-29 14:23:45 -0800331 std::atomic_bool mSystemReady = false;
332 StorageId mNextId = 0;
Yurii Zubrytskyida208012020-04-07 15:35:21 -0700333
Alex Buynytskyycca2c112020-05-05 12:48:41 -0700334 std::atomic_bool mRunning{true};
335
Yurii Zubrytskyida208012020-04-07 15:35:21 -0700336 using Job = std::function<void()>;
Yurii Zubrytskyi721ac4d2020-04-13 11:34:32 -0700337 std::unordered_map<MountId, std::vector<Job>> mJobQueue;
338 MountId mPendingJobsMount = kInvalidStorageId;
Yurii Zubrytskyida208012020-04-07 15:35:21 -0700339 std::condition_variable mJobCondition;
340 std::mutex mJobMutex;
341 std::thread mJobProcessor;
Alex Buynytskyycca2c112020-05-05 12:48:41 -0700342
343 std::thread mCmdLooperThread;
Songchun Fan3c82a302019-11-29 14:23:45 -0800344};
345
346} // namespace android::incremental