blob: e355a20907d85b2ebf322f814837047703fb6692 [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#define LOG_TAG "IncrementalService"
18
19#include "IncrementalService.h"
20
Songchun Fan3c82a302019-11-29 14:23:45 -080021#include <android-base/logging.h>
Yurii Zubrytskyi0cd80122020-04-09 23:08:31 -070022#include <android-base/no_destructor.h>
Songchun Fan3c82a302019-11-29 14:23:45 -080023#include <android-base/properties.h>
24#include <android-base/stringprintf.h>
Yurii Zubrytskyi629051fd2020-04-17 23:13:47 -070025#include <binder/AppOpsManager.h>
Jooyung Han66c567a2020-03-07 21:47:09 +090026#include <binder/Nullable.h>
Songchun Fan3c82a302019-11-29 14:23:45 -080027#include <binder/Status.h>
28#include <sys/stat.h>
29#include <uuid/uuid.h>
Songchun Fan3c82a302019-11-29 14:23:45 -080030
Yurii Zubrytskyi107ae352020-04-03 13:12:51 -070031#include <charconv>
Alex Buynytskyy18b07a42020-02-03 20:06:00 -080032#include <ctime>
Songchun Fan3c82a302019-11-29 14:23:45 -080033#include <iterator>
34#include <span>
Songchun Fan3c82a302019-11-29 14:23:45 -080035#include <type_traits>
36
Yurii Zubrytskyi629051fd2020-04-17 23:13:47 -070037#include "IncrementalServiceValidation.h"
Songchun Fan3c82a302019-11-29 14:23:45 -080038#include "Metadata.pb.h"
39
40using namespace std::literals;
Songchun Fan1124fd32020-02-10 12:49:41 -080041namespace fs = std::filesystem;
Songchun Fan3c82a302019-11-29 14:23:45 -080042
Alex Buynytskyy96e350b2020-04-02 20:03:47 -070043constexpr const char* kDataUsageStats = "android.permission.LOADER_USAGE_STATS";
Alex Buynytskyy119de1f2020-04-08 16:15:35 -070044constexpr const char* kOpUsage = "android:loader_usage_stats";
Alex Buynytskyy96e350b2020-04-02 20:03:47 -070045
Songchun Fan3c82a302019-11-29 14:23:45 -080046namespace android::incremental {
47
Yurii Zubrytskyi629051fd2020-04-17 23:13:47 -070048using content::pm::DataLoaderParamsParcel;
49using content::pm::FileSystemControlParcel;
50using content::pm::IDataLoader;
51
Songchun Fan3c82a302019-11-29 14:23:45 -080052namespace {
53
Yurii Zubrytskyi629051fd2020-04-17 23:13:47 -070054using IncrementalFileSystemControlParcel = os::incremental::IncrementalFileSystemControlParcel;
Songchun Fan3c82a302019-11-29 14:23:45 -080055
56struct Constants {
57 static constexpr auto backing = "backing_store"sv;
58 static constexpr auto mount = "mount"sv;
Songchun Fan1124fd32020-02-10 12:49:41 -080059 static constexpr auto mountKeyPrefix = "MT_"sv;
Songchun Fan3c82a302019-11-29 14:23:45 -080060 static constexpr auto storagePrefix = "st"sv;
61 static constexpr auto mountpointMdPrefix = ".mountpoint."sv;
62 static constexpr auto infoMdName = ".info"sv;
Alex Buynytskyy04035452020-06-06 20:15:58 -070063 static constexpr auto readLogsDisabledMarkerName = ".readlogs_disabled"sv;
Songchun Fan0f8b6fe2020-02-05 17:41:25 -080064 static constexpr auto libDir = "lib"sv;
65 static constexpr auto libSuffix = ".so"sv;
66 static constexpr auto blockSize = 4096;
Alex Buynytskyyea96c1f2020-05-18 10:06:01 -070067 static constexpr auto systemPackage = "android"sv;
Songchun Fan3c82a302019-11-29 14:23:45 -080068};
69
70static const Constants& constants() {
Yurii Zubrytskyi3787c9f2020-04-06 23:10:28 -070071 static constexpr Constants c;
Songchun Fan3c82a302019-11-29 14:23:45 -080072 return c;
73}
74
75template <base::LogSeverity level = base::ERROR>
76bool mkdirOrLog(std::string_view name, int mode = 0770, bool allowExisting = true) {
77 auto cstr = path::c_str(name);
78 if (::mkdir(cstr, mode)) {
Yurii Zubrytskyi4a25dfb2020-01-10 11:53:24 -080079 if (!allowExisting || errno != EEXIST) {
Songchun Fan3c82a302019-11-29 14:23:45 -080080 PLOG(level) << "Can't create directory '" << name << '\'';
81 return false;
82 }
83 struct stat st;
84 if (::stat(cstr, &st) || !S_ISDIR(st.st_mode)) {
85 PLOG(level) << "Path exists but is not a directory: '" << name << '\'';
86 return false;
87 }
88 }
Yurii Zubrytskyi4a25dfb2020-01-10 11:53:24 -080089 if (::chmod(cstr, mode)) {
90 PLOG(level) << "Changing permission failed for '" << name << '\'';
91 return false;
92 }
93
Songchun Fan3c82a302019-11-29 14:23:45 -080094 return true;
95}
96
97static std::string toMountKey(std::string_view path) {
98 if (path.empty()) {
99 return "@none";
100 }
101 if (path == "/"sv) {
102 return "@root";
103 }
104 if (path::isAbsolute(path)) {
105 path.remove_prefix(1);
106 }
Yurii Zubrytskyi629051fd2020-04-17 23:13:47 -0700107 if (path.size() > 16) {
108 path = path.substr(0, 16);
109 }
Songchun Fan3c82a302019-11-29 14:23:45 -0800110 std::string res(path);
Yurii Zubrytskyi629051fd2020-04-17 23:13:47 -0700111 std::replace_if(
112 res.begin(), res.end(), [](char c) { return c == '/' || c == '@'; }, '_');
113 return std::string(constants().mountKeyPrefix) += res;
Songchun Fan3c82a302019-11-29 14:23:45 -0800114}
115
116static std::pair<std::string, std::string> makeMountDir(std::string_view incrementalDir,
117 std::string_view path) {
118 auto mountKey = toMountKey(path);
119 const auto prefixSize = mountKey.size();
120 for (int counter = 0; counter < 1000;
121 mountKey.resize(prefixSize), base::StringAppendF(&mountKey, "%d", counter++)) {
122 auto mountRoot = path::join(incrementalDir, mountKey);
Yurii Zubrytskyi4a25dfb2020-01-10 11:53:24 -0800123 if (mkdirOrLog(mountRoot, 0777, false)) {
Songchun Fan3c82a302019-11-29 14:23:45 -0800124 return {mountKey, mountRoot};
125 }
126 }
127 return {};
128}
129
Yurii Zubrytskyi629051fd2020-04-17 23:13:47 -0700130template <class Map>
131typename Map::const_iterator findParentPath(const Map& map, std::string_view path) {
132 const auto nextIt = map.upper_bound(path);
133 if (nextIt == map.begin()) {
134 return map.end();
135 }
136 const auto suspectIt = std::prev(nextIt);
137 if (!path::startsWith(path, suspectIt->first)) {
138 return map.end();
139 }
140 return suspectIt;
141}
142
143static base::unique_fd dup(base::borrowed_fd fd) {
144 const auto res = fcntl(fd.get(), F_DUPFD_CLOEXEC, 0);
145 return base::unique_fd(res);
146}
147
Songchun Fan3c82a302019-11-29 14:23:45 -0800148template <class ProtoMessage, class Control>
Yurii Zubrytskyi629051fd2020-04-17 23:13:47 -0700149static ProtoMessage parseFromIncfs(const IncFsWrapper* incfs, const Control& control,
Songchun Fan3c82a302019-11-29 14:23:45 -0800150 std::string_view path) {
Yurii Zubrytskyi4a25dfb2020-01-10 11:53:24 -0800151 auto md = incfs->getMetadata(control, path);
Songchun Fan3c82a302019-11-29 14:23:45 -0800152 ProtoMessage message;
153 return message.ParseFromArray(md.data(), md.size()) ? message : ProtoMessage{};
154}
155
156static bool isValidMountTarget(std::string_view path) {
157 return path::isAbsolute(path) && path::isEmptyDir(path).value_or(true);
158}
159
160std::string makeBindMdName() {
161 static constexpr auto uuidStringSize = 36;
162
163 uuid_t guid;
164 uuid_generate(guid);
165
166 std::string name;
167 const auto prefixSize = constants().mountpointMdPrefix.size();
168 name.reserve(prefixSize + uuidStringSize);
169
170 name = constants().mountpointMdPrefix;
171 name.resize(prefixSize + uuidStringSize);
172 uuid_unparse(guid, name.data() + prefixSize);
173
174 return name;
175}
Alex Buynytskyy04035452020-06-06 20:15:58 -0700176
177static bool checkReadLogsDisabledMarker(std::string_view root) {
178 const auto markerPath = path::c_str(path::join(root, constants().readLogsDisabledMarkerName));
179 struct stat st;
180 return (::stat(markerPath, &st) == 0);
181}
182
Songchun Fan3c82a302019-11-29 14:23:45 -0800183} // namespace
184
185IncrementalService::IncFsMount::~IncFsMount() {
Alex Buynytskyy0ea4ff42020-04-09 17:25:42 -0700186 if (dataLoaderStub) {
Alex Buynytskyy9a545792020-04-17 15:34:47 -0700187 dataLoaderStub->cleanupResources();
188 dataLoaderStub = {};
Alex Buynytskyy0ea4ff42020-04-09 17:25:42 -0700189 }
Yurii Zubrytskyi629051fd2020-04-17 23:13:47 -0700190 control.close();
Songchun Fan3c82a302019-11-29 14:23:45 -0800191 LOG(INFO) << "Unmounting and cleaning up mount " << mountId << " with root '" << root << '\'';
192 for (auto&& [target, _] : bindPoints) {
Yurii Zubrytskyi629051fd2020-04-17 23:13:47 -0700193 LOG(INFO) << " bind: " << target;
Songchun Fan3c82a302019-11-29 14:23:45 -0800194 incrementalService.mVold->unmountIncFs(target);
195 }
Yurii Zubrytskyi629051fd2020-04-17 23:13:47 -0700196 LOG(INFO) << " root: " << root;
Songchun Fan3c82a302019-11-29 14:23:45 -0800197 incrementalService.mVold->unmountIncFs(path::join(root, constants().mount));
198 cleanupFilesystem(root);
199}
200
201auto IncrementalService::IncFsMount::makeStorage(StorageId id) -> StorageMap::iterator {
Songchun Fan3c82a302019-11-29 14:23:45 -0800202 std::string name;
203 for (int no = nextStorageDirNo.fetch_add(1, std::memory_order_relaxed), i = 0;
204 i < 1024 && no >= 0; no = nextStorageDirNo.fetch_add(1, std::memory_order_relaxed), ++i) {
205 name.clear();
Yurii Zubrytskyi4a25dfb2020-01-10 11:53:24 -0800206 base::StringAppendF(&name, "%.*s_%d_%d", int(constants().storagePrefix.size()),
207 constants().storagePrefix.data(), id, no);
208 auto fullName = path::join(root, constants().mount, name);
Songchun Fan96100932020-02-03 19:20:58 -0800209 if (auto err = incrementalService.mIncFs->makeDir(control, fullName, 0755); !err) {
Songchun Fan3c82a302019-11-29 14:23:45 -0800210 std::lock_guard l(lock);
Yurii Zubrytskyi4a25dfb2020-01-10 11:53:24 -0800211 return storages.insert_or_assign(id, Storage{std::move(fullName)}).first;
212 } else if (err != EEXIST) {
213 LOG(ERROR) << __func__ << "(): failed to create dir |" << fullName << "| " << err;
214 break;
Songchun Fan3c82a302019-11-29 14:23:45 -0800215 }
216 }
217 nextStorageDirNo = 0;
218 return storages.end();
219}
220
Yurii Zubrytskyi629051fd2020-04-17 23:13:47 -0700221template <class Func>
222static auto makeCleanup(Func&& f) {
223 auto deleter = [f = std::move(f)](auto) { f(); };
Yurii Zubrytskyiefebb452020-04-22 13:59:06 -0700224 // &f is a dangling pointer here, but we actually never use it as deleter moves it in.
Yurii Zubrytskyi629051fd2020-04-17 23:13:47 -0700225 return std::unique_ptr<Func, decltype(deleter)>(&f, std::move(deleter));
226}
227
228static std::unique_ptr<DIR, decltype(&::closedir)> openDir(const char* dir) {
229 return {::opendir(dir), ::closedir};
230}
231
232static auto openDir(std::string_view dir) {
233 return openDir(path::c_str(dir));
Yurii Zubrytskyi4a25dfb2020-01-10 11:53:24 -0800234}
235
236static int rmDirContent(const char* path) {
237 auto dir = openDir(path);
238 if (!dir) {
239 return -EINVAL;
240 }
241 while (auto entry = ::readdir(dir.get())) {
242 if (entry->d_name == "."sv || entry->d_name == ".."sv) {
243 continue;
244 }
Yurii Zubrytskyi629051fd2020-04-17 23:13:47 -0700245 auto fullPath = base::StringPrintf("%s/%s", path, entry->d_name);
Yurii Zubrytskyi4a25dfb2020-01-10 11:53:24 -0800246 if (entry->d_type == DT_DIR) {
247 if (const auto err = rmDirContent(fullPath.c_str()); err != 0) {
248 PLOG(WARNING) << "Failed to delete " << fullPath << " content";
249 return err;
250 }
251 if (const auto err = ::rmdir(fullPath.c_str()); err != 0) {
252 PLOG(WARNING) << "Failed to rmdir " << fullPath;
253 return err;
254 }
255 } else {
256 if (const auto err = ::unlink(fullPath.c_str()); err != 0) {
257 PLOG(WARNING) << "Failed to delete " << fullPath;
258 return err;
259 }
260 }
261 }
262 return 0;
263}
264
Songchun Fan3c82a302019-11-29 14:23:45 -0800265void IncrementalService::IncFsMount::cleanupFilesystem(std::string_view root) {
Yurii Zubrytskyi4a25dfb2020-01-10 11:53:24 -0800266 rmDirContent(path::join(root, constants().backing).c_str());
Songchun Fan3c82a302019-11-29 14:23:45 -0800267 ::rmdir(path::join(root, constants().backing).c_str());
268 ::rmdir(path::join(root, constants().mount).c_str());
269 ::rmdir(path::c_str(root));
270}
271
Yurii Zubrytskyi4a25dfb2020-01-10 11:53:24 -0800272IncrementalService::IncrementalService(ServiceManagerWrapper&& sm, std::string_view rootDir)
Songchun Fan3c82a302019-11-29 14:23:45 -0800273 : mVold(sm.getVoldService()),
Songchun Fan68645c42020-02-27 15:57:35 -0800274 mDataLoaderManager(sm.getDataLoaderManager()),
Songchun Fan3c82a302019-11-29 14:23:45 -0800275 mIncFs(sm.getIncFs()),
Alex Buynytskyy96e350b2020-04-02 20:03:47 -0700276 mAppOpsManager(sm.getAppOpsManager()),
Yurii Zubrytskyi86321402020-04-09 19:22:30 -0700277 mJni(sm.getJni()),
Alex Buynytskyycca2c112020-05-05 12:48:41 -0700278 mLooper(sm.getLooper()),
Alex Buynytskyy46d3ddb2020-05-29 12:05:05 -0700279 mTimedQueue(sm.getTimedQueue()),
Songchun Fan3c82a302019-11-29 14:23:45 -0800280 mIncrementalDir(rootDir) {
Alex Buynytskyy46d3ddb2020-05-29 12:05:05 -0700281 CHECK(mVold) << "Vold service is unavailable";
282 CHECK(mDataLoaderManager) << "DataLoaderManagerService is unavailable";
283 CHECK(mAppOpsManager) << "AppOpsManager is unavailable";
284 CHECK(mJni) << "JNI is unavailable";
285 CHECK(mLooper) << "Looper is unavailable";
286 CHECK(mTimedQueue) << "TimedQueue is unavailable";
Yurii Zubrytskyida208012020-04-07 15:35:21 -0700287
288 mJobQueue.reserve(16);
Yurii Zubrytskyi86321402020-04-09 19:22:30 -0700289 mJobProcessor = std::thread([this]() {
290 mJni->initializeForCurrentThread();
291 runJobProcessing();
292 });
Alex Buynytskyycca2c112020-05-05 12:48:41 -0700293 mCmdLooperThread = std::thread([this]() {
294 mJni->initializeForCurrentThread();
295 runCmdLooper();
296 });
Yurii Zubrytskyida208012020-04-07 15:35:21 -0700297
Yurii Zubrytskyi629051fd2020-04-17 23:13:47 -0700298 const auto mountedRootNames = adoptMountedInstances();
299 mountExistingImages(mountedRootNames);
Songchun Fan3c82a302019-11-29 14:23:45 -0800300}
301
Yurii Zubrytskyida208012020-04-07 15:35:21 -0700302IncrementalService::~IncrementalService() {
303 {
304 std::lock_guard lock(mJobMutex);
305 mRunning = false;
306 }
307 mJobCondition.notify_all();
308 mJobProcessor.join();
Alex Buynytskyybbb73552020-09-22 11:39:53 -0700309 mLooper->wake();
Alex Buynytskyycca2c112020-05-05 12:48:41 -0700310 mCmdLooperThread.join();
Alex Buynytskyy46d3ddb2020-05-29 12:05:05 -0700311 mTimedQueue->stop();
Alex Buynytskyy4760d8f2020-05-08 16:18:52 -0700312 // Ensure that mounts are destroyed while the service is still valid.
313 mBindsByPath.clear();
314 mMounts.clear();
Yurii Zubrytskyida208012020-04-07 15:35:21 -0700315}
Songchun Fan3c82a302019-11-29 14:23:45 -0800316
Yurii Zubrytskyi629051fd2020-04-17 23:13:47 -0700317static const char* toString(IncrementalService::BindKind kind) {
Alex Buynytskyy18b07a42020-02-03 20:06:00 -0800318 switch (kind) {
Songchun Fan0f8b6fe2020-02-05 17:41:25 -0800319 case IncrementalService::BindKind::Temporary:
320 return "Temporary";
321 case IncrementalService::BindKind::Permanent:
322 return "Permanent";
Alex Buynytskyy18b07a42020-02-03 20:06:00 -0800323 }
324}
325
326void IncrementalService::onDump(int fd) {
327 dprintf(fd, "Incremental is %s\n", incfs::enabled() ? "ENABLED" : "DISABLED");
328 dprintf(fd, "Incremental dir: %s\n", mIncrementalDir.c_str());
329
330 std::unique_lock l(mLock);
331
Yurii Zubrytskyi629051fd2020-04-17 23:13:47 -0700332 dprintf(fd, "Mounts (%d): {\n", int(mMounts.size()));
Alex Buynytskyy18b07a42020-02-03 20:06:00 -0800333 for (auto&& [id, ifs] : mMounts) {
Yurii Zubrytskyi629051fd2020-04-17 23:13:47 -0700334 const IncFsMount& mnt = *ifs;
335 dprintf(fd, " [%d]: {\n", id);
336 if (id != mnt.mountId) {
337 dprintf(fd, " reference to mountId: %d\n", mnt.mountId);
338 } else {
339 dprintf(fd, " mountId: %d\n", mnt.mountId);
340 dprintf(fd, " root: %s\n", mnt.root.c_str());
341 dprintf(fd, " nextStorageDirNo: %d\n", mnt.nextStorageDirNo.load());
342 if (mnt.dataLoaderStub) {
343 mnt.dataLoaderStub->onDump(fd);
344 } else {
345 dprintf(fd, " dataLoader: null\n");
346 }
347 dprintf(fd, " storages (%d): {\n", int(mnt.storages.size()));
348 for (auto&& [storageId, storage] : mnt.storages) {
349 dprintf(fd, " [%d] -> [%s]\n", storageId, storage.name.c_str());
350 }
351 dprintf(fd, " }\n");
Alex Buynytskyy18b07a42020-02-03 20:06:00 -0800352
Yurii Zubrytskyi629051fd2020-04-17 23:13:47 -0700353 dprintf(fd, " bindPoints (%d): {\n", int(mnt.bindPoints.size()));
354 for (auto&& [target, bind] : mnt.bindPoints) {
355 dprintf(fd, " [%s]->[%d]:\n", target.c_str(), bind.storage);
356 dprintf(fd, " savedFilename: %s\n", bind.savedFilename.c_str());
357 dprintf(fd, " sourceDir: %s\n", bind.sourceDir.c_str());
358 dprintf(fd, " kind: %s\n", toString(bind.kind));
359 }
360 dprintf(fd, " }\n");
Alex Buynytskyy18b07a42020-02-03 20:06:00 -0800361 }
Yurii Zubrytskyi629051fd2020-04-17 23:13:47 -0700362 dprintf(fd, " }\n");
Alex Buynytskyy18b07a42020-02-03 20:06:00 -0800363 }
Yurii Zubrytskyi629051fd2020-04-17 23:13:47 -0700364 dprintf(fd, "}\n");
365 dprintf(fd, "Sorted binds (%d): {\n", int(mBindsByPath.size()));
Alex Buynytskyy18b07a42020-02-03 20:06:00 -0800366 for (auto&& [target, mountPairIt] : mBindsByPath) {
367 const auto& bind = mountPairIt->second;
Yurii Zubrytskyi629051fd2020-04-17 23:13:47 -0700368 dprintf(fd, " [%s]->[%d]:\n", target.c_str(), bind.storage);
369 dprintf(fd, " savedFilename: %s\n", bind.savedFilename.c_str());
370 dprintf(fd, " sourceDir: %s\n", bind.sourceDir.c_str());
371 dprintf(fd, " kind: %s\n", toString(bind.kind));
Alex Buynytskyy18b07a42020-02-03 20:06:00 -0800372 }
Yurii Zubrytskyi629051fd2020-04-17 23:13:47 -0700373 dprintf(fd, "}\n");
Alex Buynytskyy18b07a42020-02-03 20:06:00 -0800374}
375
Alex Buynytskyy0ea4ff42020-04-09 17:25:42 -0700376void IncrementalService::onSystemReady() {
Songchun Fan3c82a302019-11-29 14:23:45 -0800377 if (mSystemReady.exchange(true)) {
Alex Buynytskyy0ea4ff42020-04-09 17:25:42 -0700378 return;
Songchun Fan3c82a302019-11-29 14:23:45 -0800379 }
380
381 std::vector<IfsMountPtr> mounts;
382 {
383 std::lock_guard l(mLock);
384 mounts.reserve(mMounts.size());
385 for (auto&& [id, ifs] : mMounts) {
Alex Buynytskyyea96c1f2020-05-18 10:06:01 -0700386 if (ifs->mountId == id &&
387 ifs->dataLoaderStub->params().packageName == Constants::systemPackage) {
Songchun Fan3c82a302019-11-29 14:23:45 -0800388 mounts.push_back(ifs);
389 }
390 }
391 }
392
Alex Buynytskyy69941662020-04-11 21:40:37 -0700393 if (mounts.empty()) {
394 return;
395 }
396
Songchun Fan3c82a302019-11-29 14:23:45 -0800397 std::thread([this, mounts = std::move(mounts)]() {
Alex Buynytskyy69941662020-04-11 21:40:37 -0700398 mJni->initializeForCurrentThread();
Songchun Fan3c82a302019-11-29 14:23:45 -0800399 for (auto&& ifs : mounts) {
Alex Buynytskyyab65cb12020-04-17 10:01:47 -0700400 ifs->dataLoaderStub->requestStart();
Songchun Fan3c82a302019-11-29 14:23:45 -0800401 }
Songchun Fan3c82a302019-11-29 14:23:45 -0800402 }).detach();
Songchun Fan3c82a302019-11-29 14:23:45 -0800403}
404
405auto IncrementalService::getStorageSlotLocked() -> MountMap::iterator {
406 for (;;) {
407 if (mNextId == kMaxStorageId) {
408 mNextId = 0;
409 }
410 auto id = ++mNextId;
411 auto [it, inserted] = mMounts.try_emplace(id, nullptr);
412 if (inserted) {
413 return it;
414 }
415 }
416}
417
Alex Buynytskyy8ef61ae2020-05-08 16:18:52 -0700418StorageId IncrementalService::createStorage(std::string_view mountPoint,
419 content::pm::DataLoaderParamsParcel&& dataLoaderParams,
420 CreateOptions options,
421 const DataLoaderStatusListener& statusListener,
422 StorageHealthCheckParams&& healthCheckParams,
423 const StorageHealthListener& healthListener) {
Songchun Fan3c82a302019-11-29 14:23:45 -0800424 LOG(INFO) << "createStorage: " << mountPoint << " | " << int(options);
425 if (!path::isAbsolute(mountPoint)) {
426 LOG(ERROR) << "path is not absolute: " << mountPoint;
427 return kInvalidStorageId;
428 }
429
430 auto mountNorm = path::normalize(mountPoint);
431 {
432 const auto id = findStorageId(mountNorm);
433 if (id != kInvalidStorageId) {
434 if (options & CreateOptions::OpenExisting) {
435 LOG(INFO) << "Opened existing storage " << id;
436 return id;
437 }
438 LOG(ERROR) << "Directory " << mountPoint << " is already mounted at storage " << id;
439 return kInvalidStorageId;
440 }
441 }
442
443 if (!(options & CreateOptions::CreateNew)) {
444 LOG(ERROR) << "not requirested create new storage, and it doesn't exist: " << mountPoint;
445 return kInvalidStorageId;
446 }
447
448 if (!path::isEmptyDir(mountNorm)) {
449 LOG(ERROR) << "Mounting over existing non-empty directory is not supported: " << mountNorm;
450 return kInvalidStorageId;
451 }
452 auto [mountKey, mountRoot] = makeMountDir(mIncrementalDir, mountNorm);
453 if (mountRoot.empty()) {
454 LOG(ERROR) << "Bad mount point";
455 return kInvalidStorageId;
456 }
457 // Make sure the code removes all crap it may create while still failing.
458 auto firstCleanup = [](const std::string* ptr) { IncFsMount::cleanupFilesystem(*ptr); };
459 auto firstCleanupOnFailure =
460 std::unique_ptr<std::string, decltype(firstCleanup)>(&mountRoot, firstCleanup);
461
462 auto mountTarget = path::join(mountRoot, constants().mount);
Yurii Zubrytskyi4a25dfb2020-01-10 11:53:24 -0800463 const auto backing = path::join(mountRoot, constants().backing);
464 if (!mkdirOrLog(backing, 0777) || !mkdirOrLog(mountTarget)) {
Songchun Fan3c82a302019-11-29 14:23:45 -0800465 return kInvalidStorageId;
466 }
467
Songchun Fan3c82a302019-11-29 14:23:45 -0800468 IncFsMount::Control control;
469 {
470 std::lock_guard l(mMountOperationLock);
471 IncrementalFileSystemControlParcel controlParcel;
Yurii Zubrytskyi4a25dfb2020-01-10 11:53:24 -0800472
473 if (auto err = rmDirContent(backing.c_str())) {
474 LOG(ERROR) << "Coudn't clean the backing directory " << backing << ": " << err;
475 return kInvalidStorageId;
476 }
477 if (!mkdirOrLog(path::join(backing, ".index"), 0777)) {
478 return kInvalidStorageId;
479 }
480 auto status = mVold->mountIncFs(backing, mountTarget, 0, &controlParcel);
Songchun Fan3c82a302019-11-29 14:23:45 -0800481 if (!status.isOk()) {
482 LOG(ERROR) << "Vold::mountIncFs() failed: " << status.toString8();
483 return kInvalidStorageId;
484 }
Yurii Zubrytskyi4a25dfb2020-01-10 11:53:24 -0800485 if (controlParcel.cmd.get() < 0 || controlParcel.pendingReads.get() < 0 ||
486 controlParcel.log.get() < 0) {
Songchun Fan3c82a302019-11-29 14:23:45 -0800487 LOG(ERROR) << "Vold::mountIncFs() returned invalid control parcel.";
488 return kInvalidStorageId;
489 }
Songchun Fan20d6ef22020-03-03 09:47:15 -0800490 int cmd = controlParcel.cmd.release().release();
491 int pendingReads = controlParcel.pendingReads.release().release();
492 int logs = controlParcel.log.release().release();
493 control = mIncFs->createControl(cmd, pendingReads, logs);
Songchun Fan3c82a302019-11-29 14:23:45 -0800494 }
495
496 std::unique_lock l(mLock);
497 const auto mountIt = getStorageSlotLocked();
498 const auto mountId = mountIt->first;
499 l.unlock();
500
501 auto ifs =
502 std::make_shared<IncFsMount>(std::move(mountRoot), mountId, std::move(control), *this);
503 // Now it's the |ifs|'s responsibility to clean up after itself, and the only cleanup we need
504 // is the removal of the |ifs|.
505 firstCleanupOnFailure.release();
506
507 auto secondCleanup = [this, &l](auto itPtr) {
508 if (!l.owns_lock()) {
509 l.lock();
510 }
511 mMounts.erase(*itPtr);
512 };
513 auto secondCleanupOnFailure =
514 std::unique_ptr<decltype(mountIt), decltype(secondCleanup)>(&mountIt, secondCleanup);
515
516 const auto storageIt = ifs->makeStorage(ifs->mountId);
517 if (storageIt == ifs->storages.end()) {
Yurii Zubrytskyi4a25dfb2020-01-10 11:53:24 -0800518 LOG(ERROR) << "Can't create a default storage directory";
Songchun Fan3c82a302019-11-29 14:23:45 -0800519 return kInvalidStorageId;
520 }
521
522 {
523 metadata::Mount m;
524 m.mutable_storage()->set_id(ifs->mountId);
Alex Buynytskyy0ea4ff42020-04-09 17:25:42 -0700525 m.mutable_loader()->set_type((int)dataLoaderParams.type);
Yurii Zubrytskyi629051fd2020-04-17 23:13:47 -0700526 m.mutable_loader()->set_allocated_package_name(&dataLoaderParams.packageName);
527 m.mutable_loader()->set_allocated_class_name(&dataLoaderParams.className);
528 m.mutable_loader()->set_allocated_arguments(&dataLoaderParams.arguments);
Songchun Fan3c82a302019-11-29 14:23:45 -0800529 const auto metadata = m.SerializeAsString();
530 m.mutable_loader()->release_arguments();
Alex Buynytskyy1ecfcec2019-12-17 12:10:41 -0800531 m.mutable_loader()->release_class_name();
Songchun Fan3c82a302019-11-29 14:23:45 -0800532 m.mutable_loader()->release_package_name();
Yurii Zubrytskyi4a25dfb2020-01-10 11:53:24 -0800533 if (auto err =
534 mIncFs->makeFile(ifs->control,
535 path::join(ifs->root, constants().mount,
536 constants().infoMdName),
537 0777, idFromMetadata(metadata),
538 {.metadata = {metadata.data(), (IncFsSize)metadata.size()}})) {
Songchun Fan3c82a302019-11-29 14:23:45 -0800539 LOG(ERROR) << "Saving mount metadata failed: " << -err;
540 return kInvalidStorageId;
541 }
542 }
543
544 const auto bk =
545 (options & CreateOptions::PermanentBind) ? BindKind::Permanent : BindKind::Temporary;
Yurii Zubrytskyi4a25dfb2020-01-10 11:53:24 -0800546 if (auto err = addBindMount(*ifs, storageIt->first, storageIt->second.name,
547 std::string(storageIt->second.name), std::move(mountNorm), bk, l);
Songchun Fan3c82a302019-11-29 14:23:45 -0800548 err < 0) {
549 LOG(ERROR) << "adding bind mount failed: " << -err;
550 return kInvalidStorageId;
551 }
552
553 // Done here as well, all data structures are in good state.
554 secondCleanupOnFailure.release();
555
Alex Buynytskyy8ef61ae2020-05-08 16:18:52 -0700556 auto dataLoaderStub = prepareDataLoader(*ifs, std::move(dataLoaderParams), &statusListener,
557 std::move(healthCheckParams), &healthListener);
Alex Buynytskyy0ea4ff42020-04-09 17:25:42 -0700558 CHECK(dataLoaderStub);
Songchun Fan3c82a302019-11-29 14:23:45 -0800559
560 mountIt->second = std::move(ifs);
561 l.unlock();
Alex Buynytskyy0ea4ff42020-04-09 17:25:42 -0700562
Alex Buynytskyyab65cb12020-04-17 10:01:47 -0700563 if (mSystemReady.load(std::memory_order_relaxed) && !dataLoaderStub->requestCreate()) {
Alex Buynytskyy0ea4ff42020-04-09 17:25:42 -0700564 // failed to create data loader
565 LOG(ERROR) << "initializeDataLoader() failed";
566 deleteStorage(dataLoaderStub->id());
567 return kInvalidStorageId;
568 }
569
Songchun Fan3c82a302019-11-29 14:23:45 -0800570 LOG(INFO) << "created storage " << mountId;
571 return mountId;
572}
573
574StorageId IncrementalService::createLinkedStorage(std::string_view mountPoint,
575 StorageId linkedStorage,
576 IncrementalService::CreateOptions options) {
577 if (!isValidMountTarget(mountPoint)) {
578 LOG(ERROR) << "Mount point is invalid or missing";
579 return kInvalidStorageId;
580 }
581
582 std::unique_lock l(mLock);
Yurii Zubrytskyi629051fd2020-04-17 23:13:47 -0700583 auto ifs = getIfsLocked(linkedStorage);
Songchun Fan3c82a302019-11-29 14:23:45 -0800584 if (!ifs) {
585 LOG(ERROR) << "Ifs unavailable";
586 return kInvalidStorageId;
587 }
588
589 const auto mountIt = getStorageSlotLocked();
590 const auto storageId = mountIt->first;
591 const auto storageIt = ifs->makeStorage(storageId);
592 if (storageIt == ifs->storages.end()) {
593 LOG(ERROR) << "Can't create a new storage";
594 mMounts.erase(mountIt);
595 return kInvalidStorageId;
596 }
597
598 l.unlock();
599
600 const auto bk =
601 (options & CreateOptions::PermanentBind) ? BindKind::Permanent : BindKind::Temporary;
Yurii Zubrytskyi4a25dfb2020-01-10 11:53:24 -0800602 if (auto err = addBindMount(*ifs, storageIt->first, storageIt->second.name,
603 std::string(storageIt->second.name), path::normalize(mountPoint),
604 bk, l);
Songchun Fan3c82a302019-11-29 14:23:45 -0800605 err < 0) {
606 LOG(ERROR) << "bindMount failed with error: " << err;
Yurii Zubrytskyi629051fd2020-04-17 23:13:47 -0700607 (void)mIncFs->unlink(ifs->control, storageIt->second.name);
608 ifs->storages.erase(storageIt);
Songchun Fan3c82a302019-11-29 14:23:45 -0800609 return kInvalidStorageId;
610 }
611
612 mountIt->second = ifs;
613 return storageId;
614}
615
616IncrementalService::BindPathMap::const_iterator IncrementalService::findStorageLocked(
617 std::string_view path) const {
Yurii Zubrytskyi629051fd2020-04-17 23:13:47 -0700618 return findParentPath(mBindsByPath, path);
Songchun Fan3c82a302019-11-29 14:23:45 -0800619}
620
621StorageId IncrementalService::findStorageId(std::string_view path) const {
622 std::lock_guard l(mLock);
623 auto it = findStorageLocked(path);
624 if (it == mBindsByPath.end()) {
625 return kInvalidStorageId;
626 }
627 return it->second->second.storage;
628}
629
Alex Buynytskyy04035452020-06-06 20:15:58 -0700630void IncrementalService::disableReadLogs(StorageId storageId) {
631 std::unique_lock l(mLock);
632 const auto ifs = getIfsLocked(storageId);
633 if (!ifs) {
634 LOG(ERROR) << "disableReadLogs failed, invalid storageId: " << storageId;
635 return;
636 }
637 if (!ifs->readLogsEnabled()) {
638 return;
639 }
640 ifs->disableReadLogs();
641 l.unlock();
642
643 const auto metadata = constants().readLogsDisabledMarkerName;
644 if (auto err = mIncFs->makeFile(ifs->control,
645 path::join(ifs->root, constants().mount,
646 constants().readLogsDisabledMarkerName),
647 0777, idFromMetadata(metadata), {})) {
648 //{.metadata = {metadata.data(), (IncFsSize)metadata.size()}})) {
649 LOG(ERROR) << "Failed to make marker file for storageId: " << storageId;
650 return;
651 }
652
653 setStorageParams(storageId, /*enableReadLogs=*/false);
654}
655
Alex Buynytskyy5e860ba2020-03-31 15:30:21 -0700656int IncrementalService::setStorageParams(StorageId storageId, bool enableReadLogs) {
657 const auto ifs = getIfs(storageId);
658 if (!ifs) {
Alex Buynytskyy5f9e3a02020-04-07 21:13:41 -0700659 LOG(ERROR) << "setStorageParams failed, invalid storageId: " << storageId;
Alex Buynytskyy5e860ba2020-03-31 15:30:21 -0700660 return -EINVAL;
661 }
662
Alex Buynytskyy0ea4ff42020-04-09 17:25:42 -0700663 const auto& params = ifs->dataLoaderStub->params();
Alex Buynytskyy96e350b2020-04-02 20:03:47 -0700664 if (enableReadLogs) {
Alex Buynytskyy04035452020-06-06 20:15:58 -0700665 if (!ifs->readLogsEnabled()) {
666 LOG(ERROR) << "setStorageParams failed, readlogs disabled for storageId: " << storageId;
667 return -EPERM;
668 }
669
Alex Buynytskyy0ea4ff42020-04-09 17:25:42 -0700670 if (auto status = mAppOpsManager->checkPermission(kDataUsageStats, kOpUsage,
671 params.packageName.c_str());
Alex Buynytskyy96e350b2020-04-02 20:03:47 -0700672 !status.isOk()) {
Alex Buynytskyy1d892162020-04-03 23:00:19 -0700673 LOG(ERROR) << "checkPermission failed: " << status.toString8();
Alex Buynytskyy96e350b2020-04-02 20:03:47 -0700674 return fromBinderStatus(status);
675 }
676 }
677
Alex Buynytskyy1d892162020-04-03 23:00:19 -0700678 if (auto status = applyStorageParams(*ifs, enableReadLogs); !status.isOk()) {
679 LOG(ERROR) << "applyStorageParams failed: " << status.toString8();
680 return fromBinderStatus(status);
681 }
682
683 if (enableReadLogs) {
Alex Buynytskyy0ea4ff42020-04-09 17:25:42 -0700684 registerAppOpsCallback(params.packageName);
Alex Buynytskyy1d892162020-04-03 23:00:19 -0700685 }
686
687 return 0;
688}
689
690binder::Status IncrementalService::applyStorageParams(IncFsMount& ifs, bool enableReadLogs) {
Yurii Zubrytskyi629051fd2020-04-17 23:13:47 -0700691 os::incremental::IncrementalFileSystemControlParcel control;
692 control.cmd.reset(dup(ifs.control.cmd()));
693 control.pendingReads.reset(dup(ifs.control.pendingReads()));
Alex Buynytskyy96e350b2020-04-02 20:03:47 -0700694 auto logsFd = ifs.control.logs();
Alex Buynytskyy5e860ba2020-03-31 15:30:21 -0700695 if (logsFd >= 0) {
Yurii Zubrytskyi629051fd2020-04-17 23:13:47 -0700696 control.log.reset(dup(logsFd));
Alex Buynytskyy5e860ba2020-03-31 15:30:21 -0700697 }
698
699 std::lock_guard l(mMountOperationLock);
Alex Buynytskyy1d892162020-04-03 23:00:19 -0700700 return mVold->setIncFsMountOptions(control, enableReadLogs);
Alex Buynytskyy5e860ba2020-03-31 15:30:21 -0700701}
702
Songchun Fan3c82a302019-11-29 14:23:45 -0800703void IncrementalService::deleteStorage(StorageId storageId) {
704 const auto ifs = getIfs(storageId);
705 if (!ifs) {
706 return;
707 }
708 deleteStorage(*ifs);
709}
710
711void IncrementalService::deleteStorage(IncrementalService::IncFsMount& ifs) {
712 std::unique_lock l(ifs.lock);
713 deleteStorageLocked(ifs, std::move(l));
714}
715
716void IncrementalService::deleteStorageLocked(IncrementalService::IncFsMount& ifs,
717 std::unique_lock<std::mutex>&& ifsLock) {
718 const auto storages = std::move(ifs.storages);
719 // Don't move the bind points out: Ifs's dtor will use them to unmount everything.
720 const auto bindPoints = ifs.bindPoints;
721 ifsLock.unlock();
722
723 std::lock_guard l(mLock);
724 for (auto&& [id, _] : storages) {
725 if (id != ifs.mountId) {
726 mMounts.erase(id);
727 }
728 }
729 for (auto&& [path, _] : bindPoints) {
730 mBindsByPath.erase(path);
731 }
732 mMounts.erase(ifs.mountId);
733}
734
735StorageId IncrementalService::openStorage(std::string_view pathInMount) {
736 if (!path::isAbsolute(pathInMount)) {
737 return kInvalidStorageId;
738 }
739
740 return findStorageId(path::normalize(pathInMount));
741}
742
Songchun Fan3c82a302019-11-29 14:23:45 -0800743IncrementalService::IfsMountPtr IncrementalService::getIfs(StorageId storage) const {
744 std::lock_guard l(mLock);
745 return getIfsLocked(storage);
746}
747
748const IncrementalService::IfsMountPtr& IncrementalService::getIfsLocked(StorageId storage) const {
749 auto it = mMounts.find(storage);
750 if (it == mMounts.end()) {
Yurii Zubrytskyi629051fd2020-04-17 23:13:47 -0700751 static const base::NoDestructor<IfsMountPtr> kEmpty{};
Yurii Zubrytskyi0cd80122020-04-09 23:08:31 -0700752 return *kEmpty;
Songchun Fan3c82a302019-11-29 14:23:45 -0800753 }
754 return it->second;
755}
756
Yurii Zubrytskyi4a25dfb2020-01-10 11:53:24 -0800757int IncrementalService::bind(StorageId storage, std::string_view source, std::string_view target,
758 BindKind kind) {
Songchun Fan3c82a302019-11-29 14:23:45 -0800759 if (!isValidMountTarget(target)) {
Yurii Zubrytskyi629051fd2020-04-17 23:13:47 -0700760 LOG(ERROR) << __func__ << ": not a valid bind target " << target;
Songchun Fan3c82a302019-11-29 14:23:45 -0800761 return -EINVAL;
762 }
763
764 const auto ifs = getIfs(storage);
765 if (!ifs) {
Yurii Zubrytskyi629051fd2020-04-17 23:13:47 -0700766 LOG(ERROR) << __func__ << ": no ifs object for storage " << storage;
Songchun Fan3c82a302019-11-29 14:23:45 -0800767 return -EINVAL;
768 }
Yurii Zubrytskyi4a25dfb2020-01-10 11:53:24 -0800769
Songchun Fan3c82a302019-11-29 14:23:45 -0800770 std::unique_lock l(ifs->lock);
771 const auto storageInfo = ifs->storages.find(storage);
772 if (storageInfo == ifs->storages.end()) {
Yurii Zubrytskyi629051fd2020-04-17 23:13:47 -0700773 LOG(ERROR) << "no storage";
Songchun Fan3c82a302019-11-29 14:23:45 -0800774 return -EINVAL;
775 }
Yurii Zubrytskyiefebb452020-04-22 13:59:06 -0700776 std::string normSource = normalizePathToStorageLocked(*ifs, storageInfo, source);
Yurii Zubrytskyi3787c9f2020-04-06 23:10:28 -0700777 if (normSource.empty()) {
Yurii Zubrytskyi629051fd2020-04-17 23:13:47 -0700778 LOG(ERROR) << "invalid source path";
Yurii Zubrytskyi3787c9f2020-04-06 23:10:28 -0700779 return -EINVAL;
780 }
Songchun Fan3c82a302019-11-29 14:23:45 -0800781 l.unlock();
782 std::unique_lock l2(mLock, std::defer_lock);
Yurii Zubrytskyi4a25dfb2020-01-10 11:53:24 -0800783 return addBindMount(*ifs, storage, storageInfo->second.name, std::move(normSource),
784 path::normalize(target), kind, l2);
Songchun Fan3c82a302019-11-29 14:23:45 -0800785}
786
787int IncrementalService::unbind(StorageId storage, std::string_view target) {
788 if (!path::isAbsolute(target)) {
789 return -EINVAL;
790 }
791
Alex Buynytskyy4dbc0602020-05-12 11:24:14 -0700792 LOG(INFO) << "Removing bind point " << target << " for storage " << storage;
Songchun Fan3c82a302019-11-29 14:23:45 -0800793
794 // Here we should only look up by the exact target, not by a subdirectory of any existing mount,
795 // otherwise there's a chance to unmount something completely unrelated
796 const auto norm = path::normalize(target);
797 std::unique_lock l(mLock);
798 const auto storageIt = mBindsByPath.find(norm);
799 if (storageIt == mBindsByPath.end() || storageIt->second->second.storage != storage) {
800 return -EINVAL;
801 }
802 const auto bindIt = storageIt->second;
803 const auto storageId = bindIt->second.storage;
804 const auto ifs = getIfsLocked(storageId);
805 if (!ifs) {
806 LOG(ERROR) << "Internal error: storageId " << storageId << " for bound path " << target
807 << " is missing";
808 return -EFAULT;
809 }
810 mBindsByPath.erase(storageIt);
811 l.unlock();
812
813 mVold->unmountIncFs(bindIt->first);
814 std::unique_lock l2(ifs->lock);
815 if (ifs->bindPoints.size() <= 1) {
816 ifs->bindPoints.clear();
Alex Buynytskyy64067b22020-04-25 15:56:52 -0700817 deleteStorageLocked(*ifs, std::move(l2));
Songchun Fan3c82a302019-11-29 14:23:45 -0800818 } else {
819 const std::string savedFile = std::move(bindIt->second.savedFilename);
820 ifs->bindPoints.erase(bindIt);
821 l2.unlock();
822 if (!savedFile.empty()) {
Yurii Zubrytskyi4a25dfb2020-01-10 11:53:24 -0800823 mIncFs->unlink(ifs->control, path::join(ifs->root, constants().mount, savedFile));
Songchun Fan3c82a302019-11-29 14:23:45 -0800824 }
825 }
Alex Buynytskyy0bdbccf2020-04-23 20:36:42 -0700826
Songchun Fan3c82a302019-11-29 14:23:45 -0800827 return 0;
828}
829
Yurii Zubrytskyi3787c9f2020-04-06 23:10:28 -0700830std::string IncrementalService::normalizePathToStorageLocked(
Yurii Zubrytskyiefebb452020-04-22 13:59:06 -0700831 const IncFsMount& incfs, IncFsMount::StorageMap::const_iterator storageIt,
Yurii Zubrytskyi629051fd2020-04-17 23:13:47 -0700832 std::string_view path) const {
833 if (!path::isAbsolute(path)) {
834 return path::normalize(path::join(storageIt->second.name, path));
Yurii Zubrytskyi3787c9f2020-04-06 23:10:28 -0700835 }
Yurii Zubrytskyi629051fd2020-04-17 23:13:47 -0700836 auto normPath = path::normalize(path);
837 if (path::startsWith(normPath, storageIt->second.name)) {
838 return normPath;
839 }
840 // not that easy: need to find if any of the bind points match
Yurii Zubrytskyiefebb452020-04-22 13:59:06 -0700841 const auto bindIt = findParentPath(incfs.bindPoints, normPath);
842 if (bindIt == incfs.bindPoints.end()) {
Yurii Zubrytskyi629051fd2020-04-17 23:13:47 -0700843 return {};
844 }
845 return path::join(bindIt->second.sourceDir, path::relativize(bindIt->first, normPath));
Yurii Zubrytskyi3787c9f2020-04-06 23:10:28 -0700846}
847
Yurii Zubrytskyiefebb452020-04-22 13:59:06 -0700848std::string IncrementalService::normalizePathToStorage(const IncFsMount& ifs, StorageId storage,
Yurii Zubrytskyi629051fd2020-04-17 23:13:47 -0700849 std::string_view path) const {
Yurii Zubrytskyiefebb452020-04-22 13:59:06 -0700850 std::unique_lock l(ifs.lock);
851 const auto storageInfo = ifs.storages.find(storage);
852 if (storageInfo == ifs.storages.end()) {
Songchun Fan103ba1d2020-02-03 17:32:32 -0800853 return {};
854 }
Yurii Zubrytskyi629051fd2020-04-17 23:13:47 -0700855 return normalizePathToStorageLocked(ifs, storageInfo, path);
Songchun Fan103ba1d2020-02-03 17:32:32 -0800856}
857
Yurii Zubrytskyi4a25dfb2020-01-10 11:53:24 -0800858int IncrementalService::makeFile(StorageId storage, std::string_view path, int mode, FileId id,
859 incfs::NewFileParams params) {
860 if (auto ifs = getIfs(storage)) {
Yurii Zubrytskyiefebb452020-04-22 13:59:06 -0700861 std::string normPath = normalizePathToStorage(*ifs, storage, path);
Songchun Fan103ba1d2020-02-03 17:32:32 -0800862 if (normPath.empty()) {
Yurii Zubrytskyi3787c9f2020-04-06 23:10:28 -0700863 LOG(ERROR) << "Internal error: storageId " << storage
864 << " failed to normalize: " << path;
Songchun Fan54c6aed2020-01-31 16:52:41 -0800865 return -EINVAL;
866 }
867 auto err = mIncFs->makeFile(ifs->control, normPath, mode, id, params);
Yurii Zubrytskyi4a25dfb2020-01-10 11:53:24 -0800868 if (err) {
Alex Buynytskyy5e860ba2020-03-31 15:30:21 -0700869 LOG(ERROR) << "Internal error: storageId " << storage << " failed to makeFile: " << err;
Yurii Zubrytskyi4a25dfb2020-01-10 11:53:24 -0800870 return err;
Songchun Fan3c82a302019-11-29 14:23:45 -0800871 }
Yurii Zubrytskyi4a25dfb2020-01-10 11:53:24 -0800872 return 0;
Songchun Fan3c82a302019-11-29 14:23:45 -0800873 }
874 return -EINVAL;
875}
876
Yurii Zubrytskyi4a25dfb2020-01-10 11:53:24 -0800877int IncrementalService::makeDir(StorageId storageId, std::string_view path, int mode) {
Songchun Fan3c82a302019-11-29 14:23:45 -0800878 if (auto ifs = getIfs(storageId)) {
Yurii Zubrytskyiefebb452020-04-22 13:59:06 -0700879 std::string normPath = normalizePathToStorage(*ifs, storageId, path);
Songchun Fan103ba1d2020-02-03 17:32:32 -0800880 if (normPath.empty()) {
881 return -EINVAL;
882 }
883 return mIncFs->makeDir(ifs->control, normPath, mode);
Songchun Fan3c82a302019-11-29 14:23:45 -0800884 }
885 return -EINVAL;
886}
887
Yurii Zubrytskyi4a25dfb2020-01-10 11:53:24 -0800888int IncrementalService::makeDirs(StorageId storageId, std::string_view path, int mode) {
Songchun Fan3c82a302019-11-29 14:23:45 -0800889 const auto ifs = getIfs(storageId);
890 if (!ifs) {
891 return -EINVAL;
892 }
Yurii Zubrytskyiefebb452020-04-22 13:59:06 -0700893 return makeDirs(*ifs, storageId, path, mode);
894}
895
896int IncrementalService::makeDirs(const IncFsMount& ifs, StorageId storageId, std::string_view path,
897 int mode) {
Songchun Fan103ba1d2020-02-03 17:32:32 -0800898 std::string normPath = normalizePathToStorage(ifs, storageId, path);
899 if (normPath.empty()) {
900 return -EINVAL;
901 }
Yurii Zubrytskyiefebb452020-04-22 13:59:06 -0700902 return mIncFs->makeDirs(ifs.control, normPath, mode);
Songchun Fan3c82a302019-11-29 14:23:45 -0800903}
904
Yurii Zubrytskyi4a25dfb2020-01-10 11:53:24 -0800905int IncrementalService::link(StorageId sourceStorageId, std::string_view oldPath,
906 StorageId destStorageId, std::string_view newPath) {
Yurii Zubrytskyi629051fd2020-04-17 23:13:47 -0700907 std::unique_lock l(mLock);
908 auto ifsSrc = getIfsLocked(sourceStorageId);
909 if (!ifsSrc) {
910 return -EINVAL;
Songchun Fan3c82a302019-11-29 14:23:45 -0800911 }
Yurii Zubrytskyi629051fd2020-04-17 23:13:47 -0700912 if (sourceStorageId != destStorageId && getIfsLocked(destStorageId) != ifsSrc) {
913 return -EINVAL;
914 }
915 l.unlock();
Yurii Zubrytskyiefebb452020-04-22 13:59:06 -0700916 std::string normOldPath = normalizePathToStorage(*ifsSrc, sourceStorageId, oldPath);
917 std::string normNewPath = normalizePathToStorage(*ifsSrc, destStorageId, newPath);
Yurii Zubrytskyi629051fd2020-04-17 23:13:47 -0700918 if (normOldPath.empty() || normNewPath.empty()) {
919 LOG(ERROR) << "Invalid paths in link(): " << normOldPath << " | " << normNewPath;
920 return -EINVAL;
921 }
922 return mIncFs->link(ifsSrc->control, normOldPath, normNewPath);
Songchun Fan3c82a302019-11-29 14:23:45 -0800923}
924
Yurii Zubrytskyi4a25dfb2020-01-10 11:53:24 -0800925int IncrementalService::unlink(StorageId storage, std::string_view path) {
Songchun Fan3c82a302019-11-29 14:23:45 -0800926 if (auto ifs = getIfs(storage)) {
Yurii Zubrytskyiefebb452020-04-22 13:59:06 -0700927 std::string normOldPath = normalizePathToStorage(*ifs, storage, path);
Songchun Fan103ba1d2020-02-03 17:32:32 -0800928 return mIncFs->unlink(ifs->control, normOldPath);
Songchun Fan3c82a302019-11-29 14:23:45 -0800929 }
930 return -EINVAL;
931}
932
Yurii Zubrytskyi4a25dfb2020-01-10 11:53:24 -0800933int IncrementalService::addBindMount(IncFsMount& ifs, StorageId storage,
934 std::string_view storageRoot, std::string&& source,
Songchun Fan3c82a302019-11-29 14:23:45 -0800935 std::string&& target, BindKind kind,
936 std::unique_lock<std::mutex>& mainLock) {
937 if (!isValidMountTarget(target)) {
Yurii Zubrytskyi629051fd2020-04-17 23:13:47 -0700938 LOG(ERROR) << __func__ << ": invalid mount target " << target;
Songchun Fan3c82a302019-11-29 14:23:45 -0800939 return -EINVAL;
940 }
941
942 std::string mdFileName;
Yurii Zubrytskyi629051fd2020-04-17 23:13:47 -0700943 std::string metadataFullPath;
Songchun Fan3c82a302019-11-29 14:23:45 -0800944 if (kind != BindKind::Temporary) {
945 metadata::BindPoint bp;
946 bp.set_storage_id(storage);
947 bp.set_allocated_dest_path(&target);
Songchun Fan1124fd32020-02-10 12:49:41 -0800948 bp.set_allocated_source_subdir(&source);
Songchun Fan3c82a302019-11-29 14:23:45 -0800949 const auto metadata = bp.SerializeAsString();
Songchun Fan3c82a302019-11-29 14:23:45 -0800950 bp.release_dest_path();
Songchun Fan1124fd32020-02-10 12:49:41 -0800951 bp.release_source_subdir();
Songchun Fan3c82a302019-11-29 14:23:45 -0800952 mdFileName = makeBindMdName();
Yurii Zubrytskyi629051fd2020-04-17 23:13:47 -0700953 metadataFullPath = path::join(ifs.root, constants().mount, mdFileName);
954 auto node = mIncFs->makeFile(ifs.control, metadataFullPath, 0444, idFromMetadata(metadata),
955 {.metadata = {metadata.data(), (IncFsSize)metadata.size()}});
Yurii Zubrytskyi4a25dfb2020-01-10 11:53:24 -0800956 if (node) {
Yurii Zubrytskyi629051fd2020-04-17 23:13:47 -0700957 LOG(ERROR) << __func__ << ": couldn't create a mount node " << mdFileName;
Songchun Fan3c82a302019-11-29 14:23:45 -0800958 return int(node);
959 }
960 }
961
Yurii Zubrytskyi629051fd2020-04-17 23:13:47 -0700962 const auto res = addBindMountWithMd(ifs, storage, std::move(mdFileName), std::move(source),
963 std::move(target), kind, mainLock);
964 if (res) {
965 mIncFs->unlink(ifs.control, metadataFullPath);
966 }
967 return res;
Songchun Fan3c82a302019-11-29 14:23:45 -0800968}
969
970int IncrementalService::addBindMountWithMd(IncrementalService::IncFsMount& ifs, StorageId storage,
Yurii Zubrytskyi4a25dfb2020-01-10 11:53:24 -0800971 std::string&& metadataName, std::string&& source,
Songchun Fan3c82a302019-11-29 14:23:45 -0800972 std::string&& target, BindKind kind,
973 std::unique_lock<std::mutex>& mainLock) {
Songchun Fan3c82a302019-11-29 14:23:45 -0800974 {
Songchun Fan3c82a302019-11-29 14:23:45 -0800975 std::lock_guard l(mMountOperationLock);
Yurii Zubrytskyi4a25dfb2020-01-10 11:53:24 -0800976 const auto status = mVold->bindMount(source, target);
Songchun Fan3c82a302019-11-29 14:23:45 -0800977 if (!status.isOk()) {
978 LOG(ERROR) << "Calling Vold::bindMount() failed: " << status.toString8();
979 return status.exceptionCode() == binder::Status::EX_SERVICE_SPECIFIC
980 ? status.serviceSpecificErrorCode() > 0 ? -status.serviceSpecificErrorCode()
981 : status.serviceSpecificErrorCode() == 0
982 ? -EFAULT
983 : status.serviceSpecificErrorCode()
984 : -EIO;
985 }
986 }
987
988 if (!mainLock.owns_lock()) {
989 mainLock.lock();
990 }
991 std::lock_guard l(ifs.lock);
Yurii Zubrytskyi629051fd2020-04-17 23:13:47 -0700992 addBindMountRecordLocked(ifs, storage, std::move(metadataName), std::move(source),
993 std::move(target), kind);
994 return 0;
995}
996
997void IncrementalService::addBindMountRecordLocked(IncFsMount& ifs, StorageId storage,
998 std::string&& metadataName, std::string&& source,
999 std::string&& target, BindKind kind) {
Songchun Fan3c82a302019-11-29 14:23:45 -08001000 const auto [it, _] =
1001 ifs.bindPoints.insert_or_assign(target,
1002 IncFsMount::Bind{storage, std::move(metadataName),
Yurii Zubrytskyi4a25dfb2020-01-10 11:53:24 -08001003 std::move(source), kind});
Songchun Fan3c82a302019-11-29 14:23:45 -08001004 mBindsByPath[std::move(target)] = it;
Yurii Zubrytskyi629051fd2020-04-17 23:13:47 -07001005}
1006
1007RawMetadata IncrementalService::getMetadata(StorageId storage, std::string_view path) const {
1008 const auto ifs = getIfs(storage);
1009 if (!ifs) {
1010 return {};
1011 }
Yurii Zubrytskyiefebb452020-04-22 13:59:06 -07001012 const auto normPath = normalizePathToStorage(*ifs, storage, path);
Yurii Zubrytskyi629051fd2020-04-17 23:13:47 -07001013 if (normPath.empty()) {
1014 return {};
1015 }
1016 return mIncFs->getMetadata(ifs->control, normPath);
Songchun Fan3c82a302019-11-29 14:23:45 -08001017}
1018
Yurii Zubrytskyi4a25dfb2020-01-10 11:53:24 -08001019RawMetadata IncrementalService::getMetadata(StorageId storage, FileId node) const {
Songchun Fan3c82a302019-11-29 14:23:45 -08001020 const auto ifs = getIfs(storage);
1021 if (!ifs) {
1022 return {};
1023 }
1024 return mIncFs->getMetadata(ifs->control, node);
1025}
1026
Songchun Fan3c82a302019-11-29 14:23:45 -08001027bool IncrementalService::startLoading(StorageId storage) const {
Alex Buynytskyy0ea4ff42020-04-09 17:25:42 -07001028 DataLoaderStubPtr dataLoaderStub;
Alex Buynytskyybf1c0632020-03-10 15:49:29 -07001029 {
1030 std::unique_lock l(mLock);
1031 const auto& ifs = getIfsLocked(storage);
1032 if (!ifs) {
Songchun Fan3c82a302019-11-29 14:23:45 -08001033 return false;
1034 }
Alex Buynytskyy0ea4ff42020-04-09 17:25:42 -07001035 dataLoaderStub = ifs->dataLoaderStub;
1036 if (!dataLoaderStub) {
1037 return false;
Alex Buynytskyybf1c0632020-03-10 15:49:29 -07001038 }
Songchun Fan3c82a302019-11-29 14:23:45 -08001039 }
Alex Buynytskyy9a545792020-04-17 15:34:47 -07001040 dataLoaderStub->requestStart();
1041 return true;
Songchun Fan3c82a302019-11-29 14:23:45 -08001042}
1043
Yurii Zubrytskyi629051fd2020-04-17 23:13:47 -07001044std::unordered_set<std::string_view> IncrementalService::adoptMountedInstances() {
1045 std::unordered_set<std::string_view> mountedRootNames;
1046 mIncFs->listExistingMounts([this, &mountedRootNames](auto root, auto backingDir, auto binds) {
1047 LOG(INFO) << "Existing mount: " << backingDir << "->" << root;
1048 for (auto [source, target] : binds) {
1049 LOG(INFO) << " bind: '" << source << "'->'" << target << "'";
1050 LOG(INFO) << " " << path::join(root, source);
1051 }
1052
1053 // Ensure it's a kind of a mount that's managed by IncrementalService
1054 if (path::basename(root) != constants().mount ||
1055 path::basename(backingDir) != constants().backing) {
1056 return;
1057 }
1058 const auto expectedRoot = path::dirname(root);
1059 if (path::dirname(backingDir) != expectedRoot) {
1060 return;
1061 }
1062 if (path::dirname(expectedRoot) != mIncrementalDir) {
1063 return;
1064 }
1065 if (!path::basename(expectedRoot).starts_with(constants().mountKeyPrefix)) {
1066 return;
1067 }
1068
1069 LOG(INFO) << "Looks like an IncrementalService-owned: " << expectedRoot;
1070
1071 // make sure we clean up the mount if it happens to be a bad one.
1072 // Note: unmounting needs to run first, so the cleanup object is created _last_.
1073 auto cleanupFiles = makeCleanup([&]() {
1074 LOG(INFO) << "Failed to adopt existing mount, deleting files: " << expectedRoot;
1075 IncFsMount::cleanupFilesystem(expectedRoot);
1076 });
1077 auto cleanupMounts = makeCleanup([&]() {
1078 LOG(INFO) << "Failed to adopt existing mount, cleaning up: " << expectedRoot;
1079 for (auto&& [_, target] : binds) {
1080 mVold->unmountIncFs(std::string(target));
1081 }
1082 mVold->unmountIncFs(std::string(root));
1083 });
1084
1085 auto control = mIncFs->openMount(root);
1086 if (!control) {
1087 LOG(INFO) << "failed to open mount " << root;
1088 return;
1089 }
1090
1091 auto mountRecord =
1092 parseFromIncfs<metadata::Mount>(mIncFs.get(), control,
1093 path::join(root, constants().infoMdName));
1094 if (!mountRecord.has_loader() || !mountRecord.has_storage()) {
1095 LOG(ERROR) << "Bad mount metadata in mount at " << expectedRoot;
1096 return;
1097 }
1098
1099 auto mountId = mountRecord.storage().id();
1100 mNextId = std::max(mNextId, mountId + 1);
1101
1102 DataLoaderParamsParcel dataLoaderParams;
1103 {
1104 const auto& loader = mountRecord.loader();
1105 dataLoaderParams.type = (content::pm::DataLoaderType)loader.type();
1106 dataLoaderParams.packageName = loader.package_name();
1107 dataLoaderParams.className = loader.class_name();
1108 dataLoaderParams.arguments = loader.arguments();
1109 }
1110
1111 auto ifs = std::make_shared<IncFsMount>(std::string(expectedRoot), mountId,
1112 std::move(control), *this);
1113 cleanupFiles.release(); // ifs will take care of that now
1114
Alex Buynytskyy04035452020-06-06 20:15:58 -07001115 // Check if marker file present.
1116 if (checkReadLogsDisabledMarker(root)) {
1117 ifs->disableReadLogs();
1118 }
1119
Yurii Zubrytskyi629051fd2020-04-17 23:13:47 -07001120 std::vector<std::pair<std::string, metadata::BindPoint>> permanentBindPoints;
1121 auto d = openDir(root);
1122 while (auto e = ::readdir(d.get())) {
1123 if (e->d_type == DT_REG) {
1124 auto name = std::string_view(e->d_name);
1125 if (name.starts_with(constants().mountpointMdPrefix)) {
1126 permanentBindPoints
1127 .emplace_back(name,
1128 parseFromIncfs<metadata::BindPoint>(mIncFs.get(),
1129 ifs->control,
1130 path::join(root,
1131 name)));
1132 if (permanentBindPoints.back().second.dest_path().empty() ||
1133 permanentBindPoints.back().second.source_subdir().empty()) {
1134 permanentBindPoints.pop_back();
1135 mIncFs->unlink(ifs->control, path::join(root, name));
1136 } else {
1137 LOG(INFO) << "Permanent bind record: '"
1138 << permanentBindPoints.back().second.source_subdir() << "'->'"
1139 << permanentBindPoints.back().second.dest_path() << "'";
1140 }
1141 }
1142 } else if (e->d_type == DT_DIR) {
1143 if (e->d_name == "."sv || e->d_name == ".."sv) {
1144 continue;
1145 }
1146 auto name = std::string_view(e->d_name);
1147 if (name.starts_with(constants().storagePrefix)) {
1148 int storageId;
1149 const auto res =
1150 std::from_chars(name.data() + constants().storagePrefix.size() + 1,
1151 name.data() + name.size(), storageId);
1152 if (res.ec != std::errc{} || *res.ptr != '_') {
1153 LOG(WARNING) << "Ignoring storage with invalid name '" << name
1154 << "' for mount " << expectedRoot;
1155 continue;
1156 }
1157 auto [_, inserted] = mMounts.try_emplace(storageId, ifs);
1158 if (!inserted) {
1159 LOG(WARNING) << "Ignoring storage with duplicate id " << storageId
1160 << " for mount " << expectedRoot;
1161 continue;
1162 }
1163 ifs->storages.insert_or_assign(storageId,
1164 IncFsMount::Storage{path::join(root, name)});
1165 mNextId = std::max(mNextId, storageId + 1);
1166 }
1167 }
1168 }
1169
1170 if (ifs->storages.empty()) {
1171 LOG(WARNING) << "No valid storages in mount " << root;
1172 return;
1173 }
1174
1175 // now match the mounted directories with what we expect to have in the metadata
1176 {
1177 std::unique_lock l(mLock, std::defer_lock);
1178 for (auto&& [metadataFile, bindRecord] : permanentBindPoints) {
1179 auto mountedIt = std::find_if(binds.begin(), binds.end(),
1180 [&, bindRecord = bindRecord](auto&& bind) {
1181 return bind.second == bindRecord.dest_path() &&
1182 path::join(root, bind.first) ==
1183 bindRecord.source_subdir();
1184 });
1185 if (mountedIt != binds.end()) {
1186 LOG(INFO) << "Matched permanent bound " << bindRecord.source_subdir()
1187 << " to mount " << mountedIt->first;
1188 addBindMountRecordLocked(*ifs, bindRecord.storage_id(), std::move(metadataFile),
1189 std::move(*bindRecord.mutable_source_subdir()),
1190 std::move(*bindRecord.mutable_dest_path()),
1191 BindKind::Permanent);
1192 if (mountedIt != binds.end() - 1) {
1193 std::iter_swap(mountedIt, binds.end() - 1);
1194 }
1195 binds = binds.first(binds.size() - 1);
1196 } else {
1197 LOG(INFO) << "Didn't match permanent bound " << bindRecord.source_subdir()
1198 << ", mounting";
1199 // doesn't exist - try mounting back
1200 if (addBindMountWithMd(*ifs, bindRecord.storage_id(), std::move(metadataFile),
1201 std::move(*bindRecord.mutable_source_subdir()),
1202 std::move(*bindRecord.mutable_dest_path()),
1203 BindKind::Permanent, l)) {
1204 mIncFs->unlink(ifs->control, metadataFile);
1205 }
1206 }
1207 }
1208 }
1209
1210 // if anything stays in |binds| those are probably temporary binds; system restarted since
1211 // they were mounted - so let's unmount them all.
1212 for (auto&& [source, target] : binds) {
1213 if (source.empty()) {
1214 continue;
1215 }
1216 mVold->unmountIncFs(std::string(target));
1217 }
1218 cleanupMounts.release(); // ifs now manages everything
1219
1220 if (ifs->bindPoints.empty()) {
1221 LOG(WARNING) << "No valid bind points for mount " << expectedRoot;
1222 deleteStorage(*ifs);
1223 return;
1224 }
1225
1226 prepareDataLoaderLocked(*ifs, std::move(dataLoaderParams));
1227 CHECK(ifs->dataLoaderStub);
1228
1229 mountedRootNames.insert(path::basename(ifs->root));
1230
1231 // not locking here at all: we're still in the constructor, no other calls can happen
1232 mMounts[ifs->mountId] = std::move(ifs);
1233 });
1234
1235 return mountedRootNames;
1236}
1237
1238void IncrementalService::mountExistingImages(
1239 const std::unordered_set<std::string_view>& mountedRootNames) {
1240 auto dir = openDir(mIncrementalDir);
1241 if (!dir) {
1242 PLOG(WARNING) << "Couldn't open the root incremental dir " << mIncrementalDir;
1243 return;
1244 }
1245 while (auto entry = ::readdir(dir.get())) {
1246 if (entry->d_type != DT_DIR) {
1247 continue;
1248 }
1249 std::string_view name = entry->d_name;
1250 if (!name.starts_with(constants().mountKeyPrefix)) {
1251 continue;
1252 }
1253 if (mountedRootNames.find(name) != mountedRootNames.end()) {
Songchun Fan3c82a302019-11-29 14:23:45 -08001254 continue;
1255 }
Songchun Fan1124fd32020-02-10 12:49:41 -08001256 const auto root = path::join(mIncrementalDir, name);
Yurii Zubrytskyi107ae352020-04-03 13:12:51 -07001257 if (!mountExistingImage(root)) {
Yurii Zubrytskyi629051fd2020-04-17 23:13:47 -07001258 IncFsMount::cleanupFilesystem(root);
Songchun Fan3c82a302019-11-29 14:23:45 -08001259 }
1260 }
1261}
1262
Yurii Zubrytskyi107ae352020-04-03 13:12:51 -07001263bool IncrementalService::mountExistingImage(std::string_view root) {
Songchun Fan3c82a302019-11-29 14:23:45 -08001264 auto mountTarget = path::join(root, constants().mount);
Yurii Zubrytskyi4a25dfb2020-01-10 11:53:24 -08001265 const auto backing = path::join(root, constants().backing);
Songchun Fan3c82a302019-11-29 14:23:45 -08001266
Songchun Fan3c82a302019-11-29 14:23:45 -08001267 IncrementalFileSystemControlParcel controlParcel;
Yurii Zubrytskyi4a25dfb2020-01-10 11:53:24 -08001268 auto status = mVold->mountIncFs(backing, mountTarget, 0, &controlParcel);
Songchun Fan3c82a302019-11-29 14:23:45 -08001269 if (!status.isOk()) {
1270 LOG(ERROR) << "Vold::mountIncFs() failed: " << status.toString8();
1271 return false;
1272 }
Songchun Fan20d6ef22020-03-03 09:47:15 -08001273
1274 int cmd = controlParcel.cmd.release().release();
1275 int pendingReads = controlParcel.pendingReads.release().release();
1276 int logs = controlParcel.log.release().release();
1277 IncFsMount::Control control = mIncFs->createControl(cmd, pendingReads, logs);
Songchun Fan3c82a302019-11-29 14:23:45 -08001278
1279 auto ifs = std::make_shared<IncFsMount>(std::string(root), -1, std::move(control), *this);
1280
Alex Buynytskyy96e350b2020-04-02 20:03:47 -07001281 auto mount = parseFromIncfs<metadata::Mount>(mIncFs.get(), ifs->control,
1282 path::join(mountTarget, constants().infoMdName));
1283 if (!mount.has_loader() || !mount.has_storage()) {
Songchun Fan3c82a302019-11-29 14:23:45 -08001284 LOG(ERROR) << "Bad mount metadata in mount at " << root;
1285 return false;
1286 }
1287
Alex Buynytskyy96e350b2020-04-02 20:03:47 -07001288 ifs->mountId = mount.storage().id();
Songchun Fan3c82a302019-11-29 14:23:45 -08001289 mNextId = std::max(mNextId, ifs->mountId + 1);
1290
Alex Buynytskyy04035452020-06-06 20:15:58 -07001291 // Check if marker file present.
1292 if (checkReadLogsDisabledMarker(mountTarget)) {
1293 ifs->disableReadLogs();
1294 }
1295
Alex Buynytskyy96e350b2020-04-02 20:03:47 -07001296 // DataLoader params
Alex Buynytskyy0ea4ff42020-04-09 17:25:42 -07001297 DataLoaderParamsParcel dataLoaderParams;
Alex Buynytskyy96e350b2020-04-02 20:03:47 -07001298 {
Alex Buynytskyy96e350b2020-04-02 20:03:47 -07001299 const auto& loader = mount.loader();
Yurii Zubrytskyi629051fd2020-04-17 23:13:47 -07001300 dataLoaderParams.type = (content::pm::DataLoaderType)loader.type();
Alex Buynytskyy0ea4ff42020-04-09 17:25:42 -07001301 dataLoaderParams.packageName = loader.package_name();
1302 dataLoaderParams.className = loader.class_name();
1303 dataLoaderParams.arguments = loader.arguments();
Alex Buynytskyy96e350b2020-04-02 20:03:47 -07001304 }
1305
Alex Buynytskyy8ef61ae2020-05-08 16:18:52 -07001306 prepareDataLoader(*ifs, std::move(dataLoaderParams));
Alex Buynytskyy69941662020-04-11 21:40:37 -07001307 CHECK(ifs->dataLoaderStub);
1308
Songchun Fan3c82a302019-11-29 14:23:45 -08001309 std::vector<std::pair<std::string, metadata::BindPoint>> bindPoints;
Yurii Zubrytskyi629051fd2020-04-17 23:13:47 -07001310 auto d = openDir(mountTarget);
Songchun Fan3c82a302019-11-29 14:23:45 -08001311 while (auto e = ::readdir(d.get())) {
1312 if (e->d_type == DT_REG) {
1313 auto name = std::string_view(e->d_name);
1314 if (name.starts_with(constants().mountpointMdPrefix)) {
1315 bindPoints.emplace_back(name,
1316 parseFromIncfs<metadata::BindPoint>(mIncFs.get(),
1317 ifs->control,
1318 path::join(mountTarget,
1319 name)));
1320 if (bindPoints.back().second.dest_path().empty() ||
1321 bindPoints.back().second.source_subdir().empty()) {
1322 bindPoints.pop_back();
Yurii Zubrytskyi4a25dfb2020-01-10 11:53:24 -08001323 mIncFs->unlink(ifs->control, path::join(ifs->root, constants().mount, name));
Songchun Fan3c82a302019-11-29 14:23:45 -08001324 }
1325 }
1326 } else if (e->d_type == DT_DIR) {
1327 if (e->d_name == "."sv || e->d_name == ".."sv) {
1328 continue;
1329 }
1330 auto name = std::string_view(e->d_name);
1331 if (name.starts_with(constants().storagePrefix)) {
Yurii Zubrytskyi107ae352020-04-03 13:12:51 -07001332 int storageId;
1333 const auto res = std::from_chars(name.data() + constants().storagePrefix.size() + 1,
1334 name.data() + name.size(), storageId);
1335 if (res.ec != std::errc{} || *res.ptr != '_') {
1336 LOG(WARNING) << "Ignoring storage with invalid name '" << name << "' for mount "
1337 << root;
1338 continue;
1339 }
1340 auto [_, inserted] = mMounts.try_emplace(storageId, ifs);
Songchun Fan3c82a302019-11-29 14:23:45 -08001341 if (!inserted) {
Yurii Zubrytskyi107ae352020-04-03 13:12:51 -07001342 LOG(WARNING) << "Ignoring storage with duplicate id " << storageId
Songchun Fan3c82a302019-11-29 14:23:45 -08001343 << " for mount " << root;
1344 continue;
1345 }
Yurii Zubrytskyi107ae352020-04-03 13:12:51 -07001346 ifs->storages.insert_or_assign(storageId,
1347 IncFsMount::Storage{
1348 path::join(root, constants().mount, name)});
1349 mNextId = std::max(mNextId, storageId + 1);
Songchun Fan3c82a302019-11-29 14:23:45 -08001350 }
1351 }
1352 }
1353
1354 if (ifs->storages.empty()) {
1355 LOG(WARNING) << "No valid storages in mount " << root;
1356 return false;
1357 }
1358
1359 int bindCount = 0;
Yurii Zubrytskyi629051fd2020-04-17 23:13:47 -07001360 {
Songchun Fan3c82a302019-11-29 14:23:45 -08001361 std::unique_lock l(mLock, std::defer_lock);
Yurii Zubrytskyi629051fd2020-04-17 23:13:47 -07001362 for (auto&& bp : bindPoints) {
1363 bindCount += !addBindMountWithMd(*ifs, bp.second.storage_id(), std::move(bp.first),
1364 std::move(*bp.second.mutable_source_subdir()),
1365 std::move(*bp.second.mutable_dest_path()),
1366 BindKind::Permanent, l);
1367 }
Songchun Fan3c82a302019-11-29 14:23:45 -08001368 }
1369
1370 if (bindCount == 0) {
1371 LOG(WARNING) << "No valid bind points for mount " << root;
1372 deleteStorage(*ifs);
1373 return false;
1374 }
1375
Yurii Zubrytskyi629051fd2020-04-17 23:13:47 -07001376 // not locking here at all: we're still in the constructor, no other calls can happen
Songchun Fan3c82a302019-11-29 14:23:45 -08001377 mMounts[ifs->mountId] = std::move(ifs);
1378 return true;
1379}
1380
Alex Buynytskyycca2c112020-05-05 12:48:41 -07001381void IncrementalService::runCmdLooper() {
Alex Buynytskyybbb73552020-09-22 11:39:53 -07001382 constexpr auto kTimeoutMsecs = -1;
Alex Buynytskyycca2c112020-05-05 12:48:41 -07001383 while (mRunning.load(std::memory_order_relaxed)) {
1384 mLooper->pollAll(kTimeoutMsecs);
1385 }
1386}
1387
Alex Buynytskyy0ea4ff42020-04-09 17:25:42 -07001388IncrementalService::DataLoaderStubPtr IncrementalService::prepareDataLoader(
Yurii Zubrytskyi629051fd2020-04-17 23:13:47 -07001389 IncFsMount& ifs, DataLoaderParamsParcel&& params,
Alex Buynytskyy8ef61ae2020-05-08 16:18:52 -07001390 const DataLoaderStatusListener* statusListener,
1391 StorageHealthCheckParams&& healthCheckParams, const StorageHealthListener* healthListener) {
Songchun Fan3c82a302019-11-29 14:23:45 -08001392 std::unique_lock l(ifs.lock);
Alex Buynytskyy8ef61ae2020-05-08 16:18:52 -07001393 prepareDataLoaderLocked(ifs, std::move(params), statusListener, std::move(healthCheckParams),
1394 healthListener);
Yurii Zubrytskyi629051fd2020-04-17 23:13:47 -07001395 return ifs.dataLoaderStub;
1396}
1397
1398void IncrementalService::prepareDataLoaderLocked(IncFsMount& ifs, DataLoaderParamsParcel&& params,
Alex Buynytskyy8ef61ae2020-05-08 16:18:52 -07001399 const DataLoaderStatusListener* statusListener,
1400 StorageHealthCheckParams&& healthCheckParams,
1401 const StorageHealthListener* healthListener) {
Alex Buynytskyy0ea4ff42020-04-09 17:25:42 -07001402 if (ifs.dataLoaderStub) {
Songchun Fan3c82a302019-11-29 14:23:45 -08001403 LOG(INFO) << "Skipped data loader preparation because it already exists";
Yurii Zubrytskyi629051fd2020-04-17 23:13:47 -07001404 return;
Songchun Fan3c82a302019-11-29 14:23:45 -08001405 }
1406
Songchun Fan3c82a302019-11-29 14:23:45 -08001407 FileSystemControlParcel fsControlParcel;
Jooyung Han66c567a2020-03-07 21:47:09 +09001408 fsControlParcel.incremental = aidl::make_nullable<IncrementalFileSystemControlParcel>();
Yurii Zubrytskyi629051fd2020-04-17 23:13:47 -07001409 fsControlParcel.incremental->cmd.reset(dup(ifs.control.cmd()));
1410 fsControlParcel.incremental->pendingReads.reset(dup(ifs.control.pendingReads()));
1411 fsControlParcel.incremental->log.reset(dup(ifs.control.logs()));
Alex Buynytskyyf4156792020-04-07 14:26:55 -07001412 fsControlParcel.service = new IncrementalServiceConnector(*this, ifs.mountId);
Alex Buynytskyy0ea4ff42020-04-09 17:25:42 -07001413
Alex Buynytskyycca2c112020-05-05 12:48:41 -07001414 ifs.dataLoaderStub =
1415 new DataLoaderStub(*this, ifs.mountId, std::move(params), std::move(fsControlParcel),
Alex Buynytskyy8ef61ae2020-05-08 16:18:52 -07001416 statusListener, std::move(healthCheckParams), healthListener,
1417 path::join(ifs.root, constants().mount));
Songchun Fan3c82a302019-11-29 14:23:45 -08001418}
1419
Yurii Zubrytskyi3787c9f2020-04-06 23:10:28 -07001420template <class Duration>
1421static long elapsedMcs(Duration start, Duration end) {
1422 return std::chrono::duration_cast<std::chrono::microseconds>(end - start).count();
1423}
1424
1425// Extract lib files from zip, create new files in incfs and write data to them
Songchun Fanc8975312020-07-13 12:14:37 -07001426// Lib files should be placed next to the APK file in the following matter:
1427// Example:
1428// /path/to/base.apk
1429// /path/to/lib/arm/first.so
1430// /path/to/lib/arm/second.so
Songchun Fan0f8b6fe2020-02-05 17:41:25 -08001431bool IncrementalService::configureNativeBinaries(StorageId storage, std::string_view apkFullPath,
1432 std::string_view libDirRelativePath,
Songchun Fan14f6c3c2020-05-21 18:19:07 -07001433 std::string_view abi, bool extractNativeLibs) {
Yurii Zubrytskyi3787c9f2020-04-06 23:10:28 -07001434 auto start = Clock::now();
1435
Songchun Fan0f8b6fe2020-02-05 17:41:25 -08001436 const auto ifs = getIfs(storage);
Yurii Zubrytskyi3787c9f2020-04-06 23:10:28 -07001437 if (!ifs) {
1438 LOG(ERROR) << "Invalid storage " << storage;
1439 return false;
1440 }
1441
Songchun Fanc8975312020-07-13 12:14:37 -07001442 const auto targetLibPathRelativeToStorage =
1443 path::join(path::dirname(normalizePathToStorage(*ifs, storage, apkFullPath)),
1444 libDirRelativePath);
1445
Songchun Fan0f8b6fe2020-02-05 17:41:25 -08001446 // First prepare target directories if they don't exist yet
Songchun Fanc8975312020-07-13 12:14:37 -07001447 if (auto res = makeDirs(*ifs, storage, targetLibPathRelativeToStorage, 0755)) {
1448 LOG(ERROR) << "Failed to prepare target lib directory " << targetLibPathRelativeToStorage
Songchun Fan0f8b6fe2020-02-05 17:41:25 -08001449 << " errno: " << res;
1450 return false;
1451 }
1452
Yurii Zubrytskyi3787c9f2020-04-06 23:10:28 -07001453 auto mkDirsTs = Clock::now();
Yurii Zubrytskyida208012020-04-07 15:35:21 -07001454 ZipArchiveHandle zipFileHandle;
1455 if (OpenArchive(path::c_str(apkFullPath), &zipFileHandle)) {
Songchun Fan0f8b6fe2020-02-05 17:41:25 -08001456 LOG(ERROR) << "Failed to open zip file at " << apkFullPath;
1457 return false;
1458 }
Yurii Zubrytskyida208012020-04-07 15:35:21 -07001459
1460 // Need a shared pointer: will be passing it into all unpacking jobs.
1461 std::shared_ptr<ZipArchive> zipFile(zipFileHandle, [](ZipArchiveHandle h) { CloseArchive(h); });
Songchun Fan0f8b6fe2020-02-05 17:41:25 -08001462 void* cookie = nullptr;
1463 const auto libFilePrefix = path::join(constants().libDir, abi);
Yurii Zubrytskyida208012020-04-07 15:35:21 -07001464 if (StartIteration(zipFile.get(), &cookie, libFilePrefix, constants().libSuffix)) {
Songchun Fan0f8b6fe2020-02-05 17:41:25 -08001465 LOG(ERROR) << "Failed to start zip iteration for " << apkFullPath;
1466 return false;
1467 }
Yurii Zubrytskyida208012020-04-07 15:35:21 -07001468 auto endIteration = [](void* cookie) { EndIteration(cookie); };
Yurii Zubrytskyi3787c9f2020-04-06 23:10:28 -07001469 auto iterationCleaner = std::unique_ptr<void, decltype(endIteration)>(cookie, endIteration);
1470
1471 auto openZipTs = Clock::now();
1472
Yurii Zubrytskyida208012020-04-07 15:35:21 -07001473 std::vector<Job> jobQueue;
1474 ZipEntry entry;
1475 std::string_view fileName;
1476 while (!Next(cookie, &entry, &fileName)) {
1477 if (fileName.empty()) {
Songchun Fan0f8b6fe2020-02-05 17:41:25 -08001478 continue;
1479 }
Yurii Zubrytskyida208012020-04-07 15:35:21 -07001480
Songchun Fan14f6c3c2020-05-21 18:19:07 -07001481 if (!extractNativeLibs) {
1482 // ensure the file is properly aligned and unpacked
1483 if (entry.method != kCompressStored) {
1484 LOG(WARNING) << "Library " << fileName << " must be uncompressed to mmap it";
1485 return false;
1486 }
1487 if ((entry.offset & (constants().blockSize - 1)) != 0) {
1488 LOG(WARNING) << "Library " << fileName
1489 << " must be page-aligned to mmap it, offset = 0x" << std::hex
1490 << entry.offset;
1491 return false;
1492 }
1493 continue;
1494 }
1495
Yurii Zubrytskyida208012020-04-07 15:35:21 -07001496 auto startFileTs = Clock::now();
1497
Songchun Fan0f8b6fe2020-02-05 17:41:25 -08001498 const auto libName = path::basename(fileName);
Songchun Fanc8975312020-07-13 12:14:37 -07001499 auto targetLibPath = path::join(targetLibPathRelativeToStorage, libName);
Yurii Zubrytskyiefebb452020-04-22 13:59:06 -07001500 const auto targetLibPathAbsolute = normalizePathToStorage(*ifs, storage, targetLibPath);
Songchun Fan0f8b6fe2020-02-05 17:41:25 -08001501 // If the extract file already exists, skip
Yurii Zubrytskyi3787c9f2020-04-06 23:10:28 -07001502 if (access(targetLibPathAbsolute.c_str(), F_OK) == 0) {
Yurii Zubrytskyi629051fd2020-04-17 23:13:47 -07001503 if (perfLoggingEnabled()) {
Yurii Zubrytskyi3787c9f2020-04-06 23:10:28 -07001504 LOG(INFO) << "incfs: Native lib file already exists: " << targetLibPath
1505 << "; skipping extraction, spent "
1506 << elapsedMcs(startFileTs, Clock::now()) << "mcs";
1507 }
Songchun Fan0f8b6fe2020-02-05 17:41:25 -08001508 continue;
1509 }
1510
Songchun Fan0f8b6fe2020-02-05 17:41:25 -08001511 // Create new lib file without signature info
Yurii Zubrytskyi3787c9f2020-04-06 23:10:28 -07001512 incfs::NewFileParams libFileParams = {
Yurii Zubrytskyida208012020-04-07 15:35:21 -07001513 .size = entry.uncompressed_length,
Yurii Zubrytskyi3787c9f2020-04-06 23:10:28 -07001514 .signature = {},
1515 // Metadata of the new lib file is its relative path
1516 .metadata = {targetLibPath.c_str(), (IncFsSize)targetLibPath.size()},
1517 };
Songchun Fan0f8b6fe2020-02-05 17:41:25 -08001518 incfs::FileId libFileId = idFromMetadata(targetLibPath);
Yurii Zubrytskyi3787c9f2020-04-06 23:10:28 -07001519 if (auto res = mIncFs->makeFile(ifs->control, targetLibPathAbsolute, 0777, libFileId,
1520 libFileParams)) {
Songchun Fan0f8b6fe2020-02-05 17:41:25 -08001521 LOG(ERROR) << "Failed to make file for: " << targetLibPath << " errno: " << res;
Songchun Fan0f8b6fe2020-02-05 17:41:25 -08001522 // If one lib file fails to be created, abort others as well
Yurii Zubrytskyi3787c9f2020-04-06 23:10:28 -07001523 return false;
Songchun Fan0f8b6fe2020-02-05 17:41:25 -08001524 }
Yurii Zubrytskyi3787c9f2020-04-06 23:10:28 -07001525
1526 auto makeFileTs = Clock::now();
1527
Songchun Fanafaf6e92020-03-18 14:12:20 -07001528 // If it is a zero-byte file, skip data writing
Yurii Zubrytskyida208012020-04-07 15:35:21 -07001529 if (entry.uncompressed_length == 0) {
Yurii Zubrytskyi629051fd2020-04-17 23:13:47 -07001530 if (perfLoggingEnabled()) {
Yurii Zubrytskyida208012020-04-07 15:35:21 -07001531 LOG(INFO) << "incfs: Extracted " << libName
1532 << "(0 bytes): " << elapsedMcs(startFileTs, makeFileTs) << "mcs";
Yurii Zubrytskyi3787c9f2020-04-06 23:10:28 -07001533 }
Songchun Fanafaf6e92020-03-18 14:12:20 -07001534 continue;
1535 }
Songchun Fan0f8b6fe2020-02-05 17:41:25 -08001536
Yurii Zubrytskyi86321402020-04-09 19:22:30 -07001537 jobQueue.emplace_back([this, zipFile, entry, ifs = std::weak_ptr<IncFsMount>(ifs),
1538 libFileId, libPath = std::move(targetLibPath),
1539 makeFileTs]() mutable {
1540 extractZipFile(ifs.lock(), zipFile.get(), entry, libFileId, libPath, makeFileTs);
Yurii Zubrytskyida208012020-04-07 15:35:21 -07001541 });
Yurii Zubrytskyi3787c9f2020-04-06 23:10:28 -07001542
Yurii Zubrytskyi629051fd2020-04-17 23:13:47 -07001543 if (perfLoggingEnabled()) {
Yurii Zubrytskyida208012020-04-07 15:35:21 -07001544 auto prepareJobTs = Clock::now();
1545 LOG(INFO) << "incfs: Processed " << libName << ": "
1546 << elapsedMcs(startFileTs, prepareJobTs)
1547 << "mcs, make file: " << elapsedMcs(startFileTs, makeFileTs)
1548 << " prepare job: " << elapsedMcs(makeFileTs, prepareJobTs);
Yurii Zubrytskyi3787c9f2020-04-06 23:10:28 -07001549 }
Songchun Fan0f8b6fe2020-02-05 17:41:25 -08001550 }
Yurii Zubrytskyi3787c9f2020-04-06 23:10:28 -07001551
Yurii Zubrytskyida208012020-04-07 15:35:21 -07001552 auto processedTs = Clock::now();
1553
1554 if (!jobQueue.empty()) {
1555 {
1556 std::lock_guard lock(mJobMutex);
1557 if (mRunning) {
Yurii Zubrytskyi721ac4d2020-04-13 11:34:32 -07001558 auto& existingJobs = mJobQueue[ifs->mountId];
Yurii Zubrytskyida208012020-04-07 15:35:21 -07001559 if (existingJobs.empty()) {
1560 existingJobs = std::move(jobQueue);
1561 } else {
1562 existingJobs.insert(existingJobs.end(), std::move_iterator(jobQueue.begin()),
1563 std::move_iterator(jobQueue.end()));
1564 }
1565 }
1566 }
1567 mJobCondition.notify_all();
1568 }
1569
Yurii Zubrytskyi629051fd2020-04-17 23:13:47 -07001570 if (perfLoggingEnabled()) {
Yurii Zubrytskyi3787c9f2020-04-06 23:10:28 -07001571 auto end = Clock::now();
1572 LOG(INFO) << "incfs: configureNativeBinaries complete in " << elapsedMcs(start, end)
1573 << "mcs, make dirs: " << elapsedMcs(start, mkDirsTs)
1574 << " open zip: " << elapsedMcs(mkDirsTs, openZipTs)
Yurii Zubrytskyida208012020-04-07 15:35:21 -07001575 << " make files: " << elapsedMcs(openZipTs, processedTs)
1576 << " schedule jobs: " << elapsedMcs(processedTs, end);
Yurii Zubrytskyi3787c9f2020-04-06 23:10:28 -07001577 }
1578
1579 return true;
Songchun Fan0f8b6fe2020-02-05 17:41:25 -08001580}
1581
Yurii Zubrytskyida208012020-04-07 15:35:21 -07001582void IncrementalService::extractZipFile(const IfsMountPtr& ifs, ZipArchiveHandle zipFile,
1583 ZipEntry& entry, const incfs::FileId& libFileId,
1584 std::string_view targetLibPath,
1585 Clock::time_point scheduledTs) {
Yurii Zubrytskyi86321402020-04-09 19:22:30 -07001586 if (!ifs) {
1587 LOG(INFO) << "Skipping zip file " << targetLibPath << " extraction for an expired mount";
1588 return;
1589 }
1590
Yurii Zubrytskyida208012020-04-07 15:35:21 -07001591 auto libName = path::basename(targetLibPath);
1592 auto startedTs = Clock::now();
1593
1594 // Write extracted data to new file
1595 // NOTE: don't zero-initialize memory, it may take a while for nothing
1596 auto libData = std::unique_ptr<uint8_t[]>(new uint8_t[entry.uncompressed_length]);
1597 if (ExtractToMemory(zipFile, &entry, libData.get(), entry.uncompressed_length)) {
1598 LOG(ERROR) << "Failed to extract native lib zip entry: " << libName;
1599 return;
1600 }
1601
1602 auto extractFileTs = Clock::now();
1603
1604 const auto writeFd = mIncFs->openForSpecialOps(ifs->control, libFileId);
1605 if (!writeFd.ok()) {
1606 LOG(ERROR) << "Failed to open write fd for: " << targetLibPath << " errno: " << writeFd;
1607 return;
1608 }
1609
1610 auto openFileTs = Clock::now();
1611 const int numBlocks =
1612 (entry.uncompressed_length + constants().blockSize - 1) / constants().blockSize;
1613 std::vector<IncFsDataBlock> instructions(numBlocks);
1614 auto remainingData = std::span(libData.get(), entry.uncompressed_length);
1615 for (int i = 0; i < numBlocks; i++) {
Yurii Zubrytskyi6c65a562020-04-14 15:25:49 -07001616 const auto blockSize = std::min<long>(constants().blockSize, remainingData.size());
Yurii Zubrytskyida208012020-04-07 15:35:21 -07001617 instructions[i] = IncFsDataBlock{
1618 .fileFd = writeFd.get(),
1619 .pageIndex = static_cast<IncFsBlockIndex>(i),
1620 .compression = INCFS_COMPRESSION_KIND_NONE,
1621 .kind = INCFS_BLOCK_KIND_DATA,
Yurii Zubrytskyi6c65a562020-04-14 15:25:49 -07001622 .dataSize = static_cast<uint32_t>(blockSize),
Yurii Zubrytskyida208012020-04-07 15:35:21 -07001623 .data = reinterpret_cast<const char*>(remainingData.data()),
1624 };
1625 remainingData = remainingData.subspan(blockSize);
1626 }
1627 auto prepareInstsTs = Clock::now();
1628
1629 size_t res = mIncFs->writeBlocks(instructions);
1630 if (res != instructions.size()) {
1631 LOG(ERROR) << "Failed to write data into: " << targetLibPath;
1632 return;
1633 }
1634
Yurii Zubrytskyi629051fd2020-04-17 23:13:47 -07001635 if (perfLoggingEnabled()) {
Yurii Zubrytskyida208012020-04-07 15:35:21 -07001636 auto endFileTs = Clock::now();
1637 LOG(INFO) << "incfs: Extracted " << libName << "(" << entry.compressed_length << " -> "
1638 << entry.uncompressed_length << " bytes): " << elapsedMcs(startedTs, endFileTs)
1639 << "mcs, scheduling delay: " << elapsedMcs(scheduledTs, startedTs)
1640 << " extract: " << elapsedMcs(startedTs, extractFileTs)
1641 << " open: " << elapsedMcs(extractFileTs, openFileTs)
1642 << " prepare: " << elapsedMcs(openFileTs, prepareInstsTs)
1643 << " write: " << elapsedMcs(prepareInstsTs, endFileTs);
1644 }
1645}
1646
1647bool IncrementalService::waitForNativeBinariesExtraction(StorageId storage) {
Yurii Zubrytskyi721ac4d2020-04-13 11:34:32 -07001648 struct WaitPrinter {
1649 const Clock::time_point startTs = Clock::now();
1650 ~WaitPrinter() noexcept {
Yurii Zubrytskyi629051fd2020-04-17 23:13:47 -07001651 if (perfLoggingEnabled()) {
Yurii Zubrytskyi721ac4d2020-04-13 11:34:32 -07001652 const auto endTs = Clock::now();
1653 LOG(INFO) << "incfs: waitForNativeBinariesExtraction() complete in "
1654 << elapsedMcs(startTs, endTs) << "mcs";
1655 }
1656 }
1657 } waitPrinter;
1658
1659 MountId mount;
1660 {
1661 auto ifs = getIfs(storage);
1662 if (!ifs) {
1663 return true;
1664 }
1665 mount = ifs->mountId;
1666 }
1667
Yurii Zubrytskyida208012020-04-07 15:35:21 -07001668 std::unique_lock lock(mJobMutex);
Yurii Zubrytskyi721ac4d2020-04-13 11:34:32 -07001669 mJobCondition.wait(lock, [this, mount] {
Yurii Zubrytskyida208012020-04-07 15:35:21 -07001670 return !mRunning ||
Yurii Zubrytskyi721ac4d2020-04-13 11:34:32 -07001671 (mPendingJobsMount != mount && mJobQueue.find(mount) == mJobQueue.end());
Yurii Zubrytskyida208012020-04-07 15:35:21 -07001672 });
Yurii Zubrytskyi721ac4d2020-04-13 11:34:32 -07001673 return mRunning;
Yurii Zubrytskyida208012020-04-07 15:35:21 -07001674}
1675
Yurii Zubrytskyi629051fd2020-04-17 23:13:47 -07001676bool IncrementalService::perfLoggingEnabled() {
1677 static const bool enabled = base::GetBoolProperty("incremental.perflogging", false);
1678 return enabled;
1679}
1680
Yurii Zubrytskyida208012020-04-07 15:35:21 -07001681void IncrementalService::runJobProcessing() {
1682 for (;;) {
1683 std::unique_lock lock(mJobMutex);
1684 mJobCondition.wait(lock, [this]() { return !mRunning || !mJobQueue.empty(); });
1685 if (!mRunning) {
1686 return;
1687 }
1688
1689 auto it = mJobQueue.begin();
Yurii Zubrytskyi721ac4d2020-04-13 11:34:32 -07001690 mPendingJobsMount = it->first;
Yurii Zubrytskyida208012020-04-07 15:35:21 -07001691 auto queue = std::move(it->second);
1692 mJobQueue.erase(it);
1693 lock.unlock();
1694
1695 for (auto&& job : queue) {
1696 job();
1697 }
1698
1699 lock.lock();
Yurii Zubrytskyi721ac4d2020-04-13 11:34:32 -07001700 mPendingJobsMount = kInvalidStorageId;
Yurii Zubrytskyida208012020-04-07 15:35:21 -07001701 lock.unlock();
1702 mJobCondition.notify_all();
1703 }
1704}
1705
Alex Buynytskyy96e350b2020-04-02 20:03:47 -07001706void IncrementalService::registerAppOpsCallback(const std::string& packageName) {
Alex Buynytskyy1d892162020-04-03 23:00:19 -07001707 sp<IAppOpsCallback> listener;
1708 {
1709 std::unique_lock lock{mCallbacksLock};
1710 auto& cb = mCallbackRegistered[packageName];
1711 if (cb) {
1712 return;
1713 }
1714 cb = new AppOpsListener(*this, packageName);
1715 listener = cb;
1716 }
1717
Yurii Zubrytskyida208012020-04-07 15:35:21 -07001718 mAppOpsManager->startWatchingMode(AppOpsManager::OP_GET_USAGE_STATS,
1719 String16(packageName.c_str()), listener);
Alex Buynytskyy1d892162020-04-03 23:00:19 -07001720}
1721
1722bool IncrementalService::unregisterAppOpsCallback(const std::string& packageName) {
1723 sp<IAppOpsCallback> listener;
1724 {
1725 std::unique_lock lock{mCallbacksLock};
1726 auto found = mCallbackRegistered.find(packageName);
1727 if (found == mCallbackRegistered.end()) {
1728 return false;
1729 }
1730 listener = found->second;
1731 mCallbackRegistered.erase(found);
1732 }
1733
1734 mAppOpsManager->stopWatchingMode(listener);
1735 return true;
1736}
1737
1738void IncrementalService::onAppOpChanged(const std::string& packageName) {
1739 if (!unregisterAppOpsCallback(packageName)) {
Alex Buynytskyy96e350b2020-04-02 20:03:47 -07001740 return;
1741 }
1742
Alex Buynytskyy96e350b2020-04-02 20:03:47 -07001743 std::vector<IfsMountPtr> affected;
1744 {
1745 std::lock_guard l(mLock);
1746 affected.reserve(mMounts.size());
1747 for (auto&& [id, ifs] : mMounts) {
Alex Buynytskyy0ea4ff42020-04-09 17:25:42 -07001748 if (ifs->mountId == id && ifs->dataLoaderStub->params().packageName == packageName) {
Alex Buynytskyy96e350b2020-04-02 20:03:47 -07001749 affected.push_back(ifs);
1750 }
1751 }
1752 }
Alex Buynytskyy96e350b2020-04-02 20:03:47 -07001753 for (auto&& ifs : affected) {
Alex Buynytskyy1d892162020-04-03 23:00:19 -07001754 applyStorageParams(*ifs, false);
Alex Buynytskyy96e350b2020-04-02 20:03:47 -07001755 }
Alex Buynytskyy96e350b2020-04-02 20:03:47 -07001756}
1757
Alex Buynytskyy46d3ddb2020-05-29 12:05:05 -07001758void IncrementalService::addTimedJob(MountId id, Milliseconds after, Job what) {
Alex Buynytskyy4760d8f2020-05-08 16:18:52 -07001759 if (id == kInvalidStorageId) {
1760 return;
1761 }
Alex Buynytskyy46d3ddb2020-05-29 12:05:05 -07001762 mTimedQueue->addJob(id, after, std::move(what));
Alex Buynytskyy4760d8f2020-05-08 16:18:52 -07001763}
1764
1765void IncrementalService::removeTimedJobs(MountId id) {
1766 if (id == kInvalidStorageId) {
1767 return;
1768 }
Alex Buynytskyy46d3ddb2020-05-29 12:05:05 -07001769 mTimedQueue->removeJobs(id);
Alex Buynytskyy4760d8f2020-05-08 16:18:52 -07001770}
1771
Alex Buynytskyyab65cb12020-04-17 10:01:47 -07001772IncrementalService::DataLoaderStub::DataLoaderStub(IncrementalService& service, MountId id,
1773 DataLoaderParamsParcel&& params,
1774 FileSystemControlParcel&& control,
Alex Buynytskyy8ef61ae2020-05-08 16:18:52 -07001775 const DataLoaderStatusListener* statusListener,
1776 StorageHealthCheckParams&& healthCheckParams,
1777 const StorageHealthListener* healthListener,
Alex Buynytskyyd0855a32020-05-07 18:40:51 -07001778 std::string&& healthPath)
Alex Buynytskyyab65cb12020-04-17 10:01:47 -07001779 : mService(service),
1780 mId(id),
1781 mParams(std::move(params)),
1782 mControl(std::move(control)),
Alex Buynytskyy8ef61ae2020-05-08 16:18:52 -07001783 mStatusListener(statusListener ? *statusListener : DataLoaderStatusListener()),
1784 mHealthListener(healthListener ? *healthListener : StorageHealthListener()),
Alex Buynytskyy4760d8f2020-05-08 16:18:52 -07001785 mHealthPath(std::move(healthPath)),
1786 mHealthCheckParams(std::move(healthCheckParams)) {
1787 if (mHealthListener) {
1788 if (!isHealthParamsValid()) {
1789 mHealthListener = {};
1790 }
1791 } else {
1792 // Disable advanced health check statuses.
1793 mHealthCheckParams.blockedTimeoutMs = -1;
1794 }
1795 updateHealthStatus();
Alex Buynytskyyab65cb12020-04-17 10:01:47 -07001796}
1797
Alex Buynytskyycca2c112020-05-05 12:48:41 -07001798IncrementalService::DataLoaderStub::~DataLoaderStub() {
Alex Buynytskyy8ef61ae2020-05-08 16:18:52 -07001799 if (isValid()) {
Alex Buynytskyycca2c112020-05-05 12:48:41 -07001800 cleanupResources();
1801 }
1802}
Alex Buynytskyy9a545792020-04-17 15:34:47 -07001803
1804void IncrementalService::DataLoaderStub::cleanupResources() {
Alex Buynytskyy4760d8f2020-05-08 16:18:52 -07001805 auto now = Clock::now();
1806 {
1807 std::unique_lock lock(mMutex);
1808 mHealthPath.clear();
1809 unregisterFromPendingReads();
1810 resetHealthControl();
1811 mService.removeTimedJobs(mId);
1812 }
1813
Alex Buynytskyy9a545792020-04-17 15:34:47 -07001814 requestDestroy();
Alex Buynytskyyb0ea4482020-05-04 18:39:58 -07001815
Alex Buynytskyy4760d8f2020-05-08 16:18:52 -07001816 {
1817 std::unique_lock lock(mMutex);
1818 mParams = {};
1819 mControl = {};
1820 mHealthControl = {};
1821 mHealthListener = {};
1822 mStatusCondition.wait_until(lock, now + 60s, [this] {
1823 return mCurrentStatus == IDataLoaderStatusListener::DATA_LOADER_DESTROYED;
1824 });
1825 mStatusListener = {};
1826 mId = kInvalidStorageId;
1827 }
Alex Buynytskyy0ea4ff42020-04-09 17:25:42 -07001828}
1829
Alex Buynytskyy0bdbccf2020-04-23 20:36:42 -07001830sp<content::pm::IDataLoader> IncrementalService::DataLoaderStub::getDataLoader() {
1831 sp<IDataLoader> dataloader;
Alex Buynytskyy8ef61ae2020-05-08 16:18:52 -07001832 auto status = mService.mDataLoaderManager->getDataLoader(id(), &dataloader);
Alex Buynytskyy0bdbccf2020-04-23 20:36:42 -07001833 if (!status.isOk()) {
1834 LOG(ERROR) << "Failed to get dataloader: " << status.toString8();
1835 return {};
1836 }
1837 if (!dataloader) {
1838 LOG(ERROR) << "DataLoader is null: " << status.toString8();
1839 return {};
1840 }
1841 return dataloader;
1842}
1843
Alex Buynytskyyab65cb12020-04-17 10:01:47 -07001844bool IncrementalService::DataLoaderStub::requestCreate() {
1845 return setTargetStatus(IDataLoaderStatusListener::DATA_LOADER_CREATED);
1846}
1847
1848bool IncrementalService::DataLoaderStub::requestStart() {
1849 return setTargetStatus(IDataLoaderStatusListener::DATA_LOADER_STARTED);
1850}
1851
1852bool IncrementalService::DataLoaderStub::requestDestroy() {
1853 return setTargetStatus(IDataLoaderStatusListener::DATA_LOADER_DESTROYED);
1854}
1855
Yurii Zubrytskyi629051fd2020-04-17 23:13:47 -07001856bool IncrementalService::DataLoaderStub::setTargetStatus(int newStatus) {
Alex Buynytskyy0b202662020-04-13 09:53:04 -07001857 {
Alex Buynytskyyb0ea4482020-05-04 18:39:58 -07001858 std::unique_lock lock(mMutex);
Alex Buynytskyy7e0a1a82020-04-27 17:06:10 -07001859 setTargetStatusLocked(newStatus);
Alex Buynytskyy0b202662020-04-13 09:53:04 -07001860 }
Alex Buynytskyyab65cb12020-04-17 10:01:47 -07001861 return fsmStep();
1862}
1863
Alex Buynytskyy7e0a1a82020-04-27 17:06:10 -07001864void IncrementalService::DataLoaderStub::setTargetStatusLocked(int status) {
Alex Buynytskyycca2c112020-05-05 12:48:41 -07001865 auto oldStatus = mTargetStatus;
Alex Buynytskyy7e0a1a82020-04-27 17:06:10 -07001866 mTargetStatus = status;
1867 mTargetStatusTs = Clock::now();
Alex Buynytskyy8ef61ae2020-05-08 16:18:52 -07001868 LOG(DEBUG) << "Target status update for DataLoader " << id() << ": " << oldStatus << " -> "
Alex Buynytskyycca2c112020-05-05 12:48:41 -07001869 << status << " (current " << mCurrentStatus << ")";
Alex Buynytskyy7e0a1a82020-04-27 17:06:10 -07001870}
1871
Alex Buynytskyyea1390f2020-04-22 16:08:50 -07001872bool IncrementalService::DataLoaderStub::bind() {
1873 bool result = false;
Alex Buynytskyy8ef61ae2020-05-08 16:18:52 -07001874 auto status = mService.mDataLoaderManager->bindToDataLoader(id(), mParams, this, &result);
Alex Buynytskyyea1390f2020-04-22 16:08:50 -07001875 if (!status.isOk() || !result) {
Alex Buynytskyy8ef61ae2020-05-08 16:18:52 -07001876 LOG(ERROR) << "Failed to bind a data loader for mount " << id();
Alex Buynytskyyea1390f2020-04-22 16:08:50 -07001877 return false;
1878 }
1879 return true;
1880}
1881
Alex Buynytskyyab65cb12020-04-17 10:01:47 -07001882bool IncrementalService::DataLoaderStub::create() {
Alex Buynytskyy0bdbccf2020-04-23 20:36:42 -07001883 auto dataloader = getDataLoader();
Alex Buynytskyyea1390f2020-04-22 16:08:50 -07001884 if (!dataloader) {
Alex Buynytskyyea1390f2020-04-22 16:08:50 -07001885 return false;
1886 }
Alex Buynytskyy8ef61ae2020-05-08 16:18:52 -07001887 auto status = dataloader->create(id(), mParams, mControl, this);
Alex Buynytskyyea1390f2020-04-22 16:08:50 -07001888 if (!status.isOk()) {
Alex Buynytskyy8ef61ae2020-05-08 16:18:52 -07001889 LOG(ERROR) << "Failed to create DataLoader: " << status.toString8();
Alex Buynytskyy0ea4ff42020-04-09 17:25:42 -07001890 return false;
1891 }
1892 return true;
1893}
1894
Alex Buynytskyy0b202662020-04-13 09:53:04 -07001895bool IncrementalService::DataLoaderStub::start() {
Alex Buynytskyy0bdbccf2020-04-23 20:36:42 -07001896 auto dataloader = getDataLoader();
Alex Buynytskyy0ea4ff42020-04-09 17:25:42 -07001897 if (!dataloader) {
1898 return false;
1899 }
Alex Buynytskyy8ef61ae2020-05-08 16:18:52 -07001900 auto status = dataloader->start(id());
Alex Buynytskyy0ea4ff42020-04-09 17:25:42 -07001901 if (!status.isOk()) {
Alex Buynytskyyab65cb12020-04-17 10:01:47 -07001902 LOG(ERROR) << "Failed to start DataLoader: " << status.toString8();
Alex Buynytskyy0ea4ff42020-04-09 17:25:42 -07001903 return false;
1904 }
1905 return true;
1906}
1907
Alex Buynytskyyab65cb12020-04-17 10:01:47 -07001908bool IncrementalService::DataLoaderStub::destroy() {
Alex Buynytskyy8ef61ae2020-05-08 16:18:52 -07001909 return mService.mDataLoaderManager->unbindFromDataLoader(id()).isOk();
Alex Buynytskyy0b202662020-04-13 09:53:04 -07001910}
1911
Alex Buynytskyyab65cb12020-04-17 10:01:47 -07001912bool IncrementalService::DataLoaderStub::fsmStep() {
Alex Buynytskyy9a545792020-04-17 15:34:47 -07001913 if (!isValid()) {
1914 return false;
1915 }
1916
Alex Buynytskyyab65cb12020-04-17 10:01:47 -07001917 int currentStatus;
1918 int targetStatus;
1919 {
Alex Buynytskyyb0ea4482020-05-04 18:39:58 -07001920 std::unique_lock lock(mMutex);
Alex Buynytskyyab65cb12020-04-17 10:01:47 -07001921 currentStatus = mCurrentStatus;
1922 targetStatus = mTargetStatus;
1923 }
1924
Alex Buynytskyy4760d8f2020-05-08 16:18:52 -07001925 LOG(DEBUG) << "fsmStep: " << id() << ": " << currentStatus << " -> " << targetStatus;
Alex Buynytskyy4dbc0602020-05-12 11:24:14 -07001926
Alex Buynytskyyab65cb12020-04-17 10:01:47 -07001927 if (currentStatus == targetStatus) {
1928 return true;
1929 }
1930
1931 switch (targetStatus) {
Alex Buynytskyy7e0a1a82020-04-27 17:06:10 -07001932 case IDataLoaderStatusListener::DATA_LOADER_UNAVAILABLE:
1933 // Do nothing, this is a reset state.
1934 break;
Alex Buynytskyyab65cb12020-04-17 10:01:47 -07001935 case IDataLoaderStatusListener::DATA_LOADER_DESTROYED: {
1936 return destroy();
1937 }
1938 case IDataLoaderStatusListener::DATA_LOADER_STARTED: {
1939 switch (currentStatus) {
1940 case IDataLoaderStatusListener::DATA_LOADER_CREATED:
1941 case IDataLoaderStatusListener::DATA_LOADER_STOPPED:
1942 return start();
1943 }
Alex Buynytskyyd0855a32020-05-07 18:40:51 -07001944 [[fallthrough]];
Alex Buynytskyyab65cb12020-04-17 10:01:47 -07001945 }
1946 case IDataLoaderStatusListener::DATA_LOADER_CREATED:
1947 switch (currentStatus) {
1948 case IDataLoaderStatusListener::DATA_LOADER_DESTROYED:
Alex Buynytskyy7e0a1a82020-04-27 17:06:10 -07001949 case IDataLoaderStatusListener::DATA_LOADER_UNAVAILABLE:
Alex Buynytskyyea1390f2020-04-22 16:08:50 -07001950 return bind();
1951 case IDataLoaderStatusListener::DATA_LOADER_BOUND:
Alex Buynytskyyab65cb12020-04-17 10:01:47 -07001952 return create();
1953 }
1954 break;
1955 default:
1956 LOG(ERROR) << "Invalid target status: " << targetStatus
1957 << ", current status: " << currentStatus;
1958 break;
1959 }
1960 return false;
Alex Buynytskyy0ea4ff42020-04-09 17:25:42 -07001961}
1962
1963binder::Status IncrementalService::DataLoaderStub::onStatusChanged(MountId mountId, int newStatus) {
Alex Buynytskyy9a545792020-04-17 15:34:47 -07001964 if (!isValid()) {
1965 return binder::Status::
1966 fromServiceSpecificError(-EINVAL, "onStatusChange came to invalid DataLoaderStub");
1967 }
Alex Buynytskyy8ef61ae2020-05-08 16:18:52 -07001968 if (id() != mountId) {
1969 LOG(ERROR) << "Mount ID mismatch: expected " << id() << ", but got: " << mountId;
Alex Buynytskyy9a545792020-04-17 15:34:47 -07001970 return binder::Status::fromServiceSpecificError(-EPERM, "Mount ID mismatch.");
1971 }
1972
Yurii Zubrytskyi629051fd2020-04-17 23:13:47 -07001973 int targetStatus, oldStatus;
Alex Buynytskyyb0ea4482020-05-04 18:39:58 -07001974 DataLoaderStatusListener listener;
Alex Buynytskyyab65cb12020-04-17 10:01:47 -07001975 {
Alex Buynytskyyb0ea4482020-05-04 18:39:58 -07001976 std::unique_lock lock(mMutex);
Alex Buynytskyyab65cb12020-04-17 10:01:47 -07001977 if (mCurrentStatus == newStatus) {
1978 return binder::Status::ok();
1979 }
Alex Buynytskyy7e0a1a82020-04-27 17:06:10 -07001980
Yurii Zubrytskyi629051fd2020-04-17 23:13:47 -07001981 oldStatus = mCurrentStatus;
Alex Buynytskyy0bdbccf2020-04-23 20:36:42 -07001982 mCurrentStatus = newStatus;
Yurii Zubrytskyi629051fd2020-04-17 23:13:47 -07001983 targetStatus = mTargetStatus;
Alex Buynytskyy7e0a1a82020-04-27 17:06:10 -07001984
Alex Buynytskyy8ef61ae2020-05-08 16:18:52 -07001985 listener = mStatusListener;
Alex Buynytskyyb0ea4482020-05-04 18:39:58 -07001986
Alex Buynytskyy7e0a1a82020-04-27 17:06:10 -07001987 if (mCurrentStatus == IDataLoaderStatusListener::DATA_LOADER_UNAVAILABLE) {
Alex Buynytskyy4dbc0602020-05-12 11:24:14 -07001988 // For unavailable, unbind from DataLoader to ensure proper re-commit.
1989 setTargetStatusLocked(IDataLoaderStatusListener::DATA_LOADER_DESTROYED);
Alex Buynytskyy7e0a1a82020-04-27 17:06:10 -07001990 }
Alex Buynytskyy0ea4ff42020-04-09 17:25:42 -07001991 }
1992
Alex Buynytskyy8ef61ae2020-05-08 16:18:52 -07001993 LOG(DEBUG) << "Current status update for DataLoader " << id() << ": " << oldStatus << " -> "
Yurii Zubrytskyi629051fd2020-04-17 23:13:47 -07001994 << newStatus << " (target " << targetStatus << ")";
1995
Alex Buynytskyyb0ea4482020-05-04 18:39:58 -07001996 if (listener) {
1997 listener->onStatusChanged(mountId, newStatus);
Alex Buynytskyy0ea4ff42020-04-09 17:25:42 -07001998 }
1999
Alex Buynytskyyab65cb12020-04-17 10:01:47 -07002000 fsmStep();
Songchun Fan3c82a302019-11-29 14:23:45 -08002001
Alex Buynytskyyc2a645d2020-04-20 14:11:55 -07002002 mStatusCondition.notify_all();
2003
Songchun Fan3c82a302019-11-29 14:23:45 -08002004 return binder::Status::ok();
2005}
2006
Alex Buynytskyy4760d8f2020-05-08 16:18:52 -07002007bool IncrementalService::DataLoaderStub::isHealthParamsValid() const {
2008 return mHealthCheckParams.blockedTimeoutMs > 0 &&
2009 mHealthCheckParams.blockedTimeoutMs < mHealthCheckParams.unhealthyTimeoutMs;
Alex Buynytskyyd0855a32020-05-07 18:40:51 -07002010}
2011
Alex Buynytskyy4760d8f2020-05-08 16:18:52 -07002012void IncrementalService::DataLoaderStub::onHealthStatus(StorageHealthListener healthListener,
2013 int healthStatus) {
2014 LOG(DEBUG) << id() << ": healthStatus: " << healthStatus;
2015 if (healthListener) {
2016 healthListener->onHealthStatus(id(), healthStatus);
2017 }
Alex Buynytskyyd0855a32020-05-07 18:40:51 -07002018}
2019
Alex Buynytskyy4760d8f2020-05-08 16:18:52 -07002020void IncrementalService::DataLoaderStub::updateHealthStatus(bool baseline) {
2021 LOG(DEBUG) << id() << ": updateHealthStatus" << (baseline ? " (baseline)" : "");
Alex Buynytskyyd0855a32020-05-07 18:40:51 -07002022
Alex Buynytskyy4760d8f2020-05-08 16:18:52 -07002023 int healthStatusToReport = -1;
2024 StorageHealthListener healthListener;
Alex Buynytskyyd0855a32020-05-07 18:40:51 -07002025
Alex Buynytskyy4760d8f2020-05-08 16:18:52 -07002026 {
2027 std::unique_lock lock(mMutex);
2028 unregisterFromPendingReads();
2029
2030 healthListener = mHealthListener;
2031
2032 // Healthcheck depends on timestamp of the oldest pending read.
2033 // To get it, we need to re-open a pendingReads FD to get a full list of reads.
2034 // Additionally we need to re-register for epoll with fresh FDs in case there are no reads.
2035 const auto now = Clock::now();
2036 const auto kernelTsUs = getOldestPendingReadTs();
2037 if (baseline) {
2038 // Updating baseline only on looper/epoll callback, i.e. on new set of pending reads.
2039 mHealthBase = {now, kernelTsUs};
2040 }
2041
Alex Buynytskyy46d3ddb2020-05-29 12:05:05 -07002042 if (kernelTsUs == kMaxBootClockTsUs || mHealthBase.kernelTsUs == kMaxBootClockTsUs ||
2043 mHealthBase.userTs > now) {
Alex Buynytskyy4760d8f2020-05-08 16:18:52 -07002044 LOG(DEBUG) << id() << ": No pending reads or invalid base, report Ok and wait.";
2045 registerForPendingReads();
2046 healthStatusToReport = IStorageHealthListener::HEALTH_STATUS_OK;
2047 lock.unlock();
2048 onHealthStatus(healthListener, healthStatusToReport);
Alex Buynytskyyd0855a32020-05-07 18:40:51 -07002049 return;
2050 }
Alex Buynytskyy4760d8f2020-05-08 16:18:52 -07002051
2052 resetHealthControl();
2053
2054 // Always make sure the data loader is started.
2055 setTargetStatusLocked(IDataLoaderStatusListener::DATA_LOADER_STARTED);
2056
2057 // Skip any further processing if health check params are invalid.
2058 if (!isHealthParamsValid()) {
2059 LOG(DEBUG) << id()
2060 << ": Skip any further processing if health check params are invalid.";
2061 healthStatusToReport = IStorageHealthListener::HEALTH_STATUS_READS_PENDING;
2062 lock.unlock();
2063 onHealthStatus(healthListener, healthStatusToReport);
2064 // Triggering data loader start. This is a one-time action.
2065 fsmStep();
2066 return;
2067 }
2068
Alex Buynytskyy46d3ddb2020-05-29 12:05:05 -07002069 // Don't schedule timer job less than 500ms in advance.
2070 static constexpr auto kTolerance = 500ms;
2071
Alex Buynytskyy4760d8f2020-05-08 16:18:52 -07002072 const auto blockedTimeout = std::chrono::milliseconds(mHealthCheckParams.blockedTimeoutMs);
2073 const auto unhealthyTimeout =
2074 std::chrono::milliseconds(mHealthCheckParams.unhealthyTimeoutMs);
2075 const auto unhealthyMonitoring =
2076 std::max(1000ms,
2077 std::chrono::milliseconds(mHealthCheckParams.unhealthyMonitoringMs));
2078
2079 const auto kernelDeltaUs = kernelTsUs - mHealthBase.kernelTsUs;
2080 const auto userTs = mHealthBase.userTs + std::chrono::microseconds(kernelDeltaUs);
Alex Buynytskyy46d3ddb2020-05-29 12:05:05 -07002081 const auto delta = std::chrono::duration_cast<std::chrono::milliseconds>(now - userTs);
Alex Buynytskyy4760d8f2020-05-08 16:18:52 -07002082
Alex Buynytskyy46d3ddb2020-05-29 12:05:05 -07002083 Milliseconds checkBackAfter;
2084 if (delta + kTolerance < blockedTimeout) {
Alex Buynytskyy4760d8f2020-05-08 16:18:52 -07002085 LOG(DEBUG) << id() << ": Report reads pending and wait for blocked status.";
Alex Buynytskyy46d3ddb2020-05-29 12:05:05 -07002086 checkBackAfter = blockedTimeout - delta;
Alex Buynytskyy4760d8f2020-05-08 16:18:52 -07002087 healthStatusToReport = IStorageHealthListener::HEALTH_STATUS_READS_PENDING;
Alex Buynytskyy46d3ddb2020-05-29 12:05:05 -07002088 } else if (delta + kTolerance < unhealthyTimeout) {
Alex Buynytskyy4760d8f2020-05-08 16:18:52 -07002089 LOG(DEBUG) << id() << ": Report blocked and wait for unhealthy.";
Alex Buynytskyy46d3ddb2020-05-29 12:05:05 -07002090 checkBackAfter = unhealthyTimeout - delta;
Alex Buynytskyy4760d8f2020-05-08 16:18:52 -07002091 healthStatusToReport = IStorageHealthListener::HEALTH_STATUS_BLOCKED;
2092 } else {
2093 LOG(DEBUG) << id() << ": Report unhealthy and continue monitoring.";
Alex Buynytskyy46d3ddb2020-05-29 12:05:05 -07002094 checkBackAfter = unhealthyMonitoring;
Alex Buynytskyy4760d8f2020-05-08 16:18:52 -07002095 healthStatusToReport = IStorageHealthListener::HEALTH_STATUS_UNHEALTHY;
2096 }
Alex Buynytskyy46d3ddb2020-05-29 12:05:05 -07002097 LOG(DEBUG) << id() << ": updateHealthStatus in " << double(checkBackAfter.count()) / 1000.0
Alex Buynytskyy4760d8f2020-05-08 16:18:52 -07002098 << "secs";
Alex Buynytskyy46d3ddb2020-05-29 12:05:05 -07002099 mService.addTimedJob(id(), checkBackAfter, [this]() { updateHealthStatus(); });
Alex Buynytskyycca2c112020-05-05 12:48:41 -07002100 }
2101
Alex Buynytskyy46d3ddb2020-05-29 12:05:05 -07002102 // With kTolerance we are expecting these to execute before the next update.
Alex Buynytskyy4760d8f2020-05-08 16:18:52 -07002103 if (healthStatusToReport != -1) {
2104 onHealthStatus(healthListener, healthStatusToReport);
2105 }
2106
2107 fsmStep();
2108}
2109
2110const incfs::UniqueControl& IncrementalService::DataLoaderStub::initializeHealthControl() {
2111 if (mHealthPath.empty()) {
2112 resetHealthControl();
2113 return mHealthControl;
2114 }
2115 if (mHealthControl.pendingReads() < 0) {
2116 mHealthControl = mService.mIncFs->openMount(mHealthPath);
2117 }
2118 if (mHealthControl.pendingReads() < 0) {
2119 LOG(ERROR) << "Failed to open health control for: " << id() << ", path: " << mHealthPath
2120 << "(" << mHealthControl.cmd() << ":" << mHealthControl.pendingReads() << ":"
2121 << mHealthControl.logs() << ")";
2122 }
2123 return mHealthControl;
2124}
2125
2126void IncrementalService::DataLoaderStub::resetHealthControl() {
2127 mHealthControl = {};
2128}
2129
2130BootClockTsUs IncrementalService::DataLoaderStub::getOldestPendingReadTs() {
2131 auto result = kMaxBootClockTsUs;
2132
2133 const auto& control = initializeHealthControl();
2134 if (control.pendingReads() < 0) {
2135 return result;
2136 }
2137
2138 std::vector<incfs::ReadInfo> pendingReads;
2139 if (mService.mIncFs->waitForPendingReads(control, 0ms, &pendingReads) !=
2140 android::incfs::WaitResult::HaveData ||
2141 pendingReads.empty()) {
2142 return result;
2143 }
2144
2145 LOG(DEBUG) << id() << ": pendingReads: " << control.pendingReads() << ", "
2146 << pendingReads.size() << ": " << pendingReads.front().bootClockTsUs;
2147
2148 for (auto&& pendingRead : pendingReads) {
2149 result = std::min(result, pendingRead.bootClockTsUs);
2150 }
2151 return result;
2152}
2153
2154void IncrementalService::DataLoaderStub::registerForPendingReads() {
2155 const auto pendingReadsFd = mHealthControl.pendingReads();
2156 if (pendingReadsFd < 0) {
2157 return;
2158 }
2159
2160 LOG(DEBUG) << id() << ": addFd(pendingReadsFd): " << pendingReadsFd;
2161
Alex Buynytskyycca2c112020-05-05 12:48:41 -07002162 mService.mLooper->addFd(
2163 pendingReadsFd, android::Looper::POLL_CALLBACK, android::Looper::EVENT_INPUT,
2164 [](int, int, void* data) -> int {
2165 auto&& self = (DataLoaderStub*)data;
Alex Buynytskyy4760d8f2020-05-08 16:18:52 -07002166 self->updateHealthStatus(/*baseline=*/true);
2167 return 0;
Alex Buynytskyycca2c112020-05-05 12:48:41 -07002168 },
2169 this);
2170 mService.mLooper->wake();
2171}
2172
Alex Buynytskyyd0855a32020-05-07 18:40:51 -07002173void IncrementalService::DataLoaderStub::unregisterFromPendingReads() {
Alex Buynytskyycca2c112020-05-05 12:48:41 -07002174 const auto pendingReadsFd = mHealthControl.pendingReads();
2175 if (pendingReadsFd < 0) {
2176 return;
2177 }
2178
Alex Buynytskyy4760d8f2020-05-08 16:18:52 -07002179 LOG(DEBUG) << id() << ": removeFd(pendingReadsFd): " << pendingReadsFd;
2180
Alex Buynytskyycca2c112020-05-05 12:48:41 -07002181 mService.mLooper->removeFd(pendingReadsFd);
2182 mService.mLooper->wake();
Alex Buynytskyycca2c112020-05-05 12:48:41 -07002183}
2184
Alex Buynytskyyab65cb12020-04-17 10:01:47 -07002185void IncrementalService::DataLoaderStub::onDump(int fd) {
Yurii Zubrytskyi629051fd2020-04-17 23:13:47 -07002186 dprintf(fd, " dataLoader: {\n");
2187 dprintf(fd, " currentStatus: %d\n", mCurrentStatus);
2188 dprintf(fd, " targetStatus: %d\n", mTargetStatus);
2189 dprintf(fd, " targetStatusTs: %lldmcs\n",
Alex Buynytskyyab65cb12020-04-17 10:01:47 -07002190 (long long)(elapsedMcs(mTargetStatusTs, Clock::now())));
Alex Buynytskyy46d3ddb2020-05-29 12:05:05 -07002191 dprintf(fd, " health: {\n");
2192 dprintf(fd, " path: %s\n", mHealthPath.c_str());
2193 dprintf(fd, " base: %lldmcs (%lld)\n",
2194 (long long)(elapsedMcs(mHealthBase.userTs, Clock::now())),
2195 (long long)mHealthBase.kernelTsUs);
2196 dprintf(fd, " blockedTimeoutMs: %d\n", int(mHealthCheckParams.blockedTimeoutMs));
2197 dprintf(fd, " unhealthyTimeoutMs: %d\n", int(mHealthCheckParams.unhealthyTimeoutMs));
2198 dprintf(fd, " unhealthyMonitoringMs: %d\n",
2199 int(mHealthCheckParams.unhealthyMonitoringMs));
2200 dprintf(fd, " }\n");
Alex Buynytskyyab65cb12020-04-17 10:01:47 -07002201 const auto& params = mParams;
Yurii Zubrytskyi629051fd2020-04-17 23:13:47 -07002202 dprintf(fd, " dataLoaderParams: {\n");
2203 dprintf(fd, " type: %s\n", toString(params.type).c_str());
2204 dprintf(fd, " packageName: %s\n", params.packageName.c_str());
2205 dprintf(fd, " className: %s\n", params.className.c_str());
2206 dprintf(fd, " arguments: %s\n", params.arguments.c_str());
2207 dprintf(fd, " }\n");
2208 dprintf(fd, " }\n");
Alex Buynytskyyab65cb12020-04-17 10:01:47 -07002209}
2210
Alex Buynytskyy1d892162020-04-03 23:00:19 -07002211void IncrementalService::AppOpsListener::opChanged(int32_t, const String16&) {
2212 incrementalService.onAppOpChanged(packageName);
Alex Buynytskyy96e350b2020-04-02 20:03:47 -07002213}
2214
Alex Buynytskyyf4156792020-04-07 14:26:55 -07002215binder::Status IncrementalService::IncrementalServiceConnector::setStorageParams(
2216 bool enableReadLogs, int32_t* _aidl_return) {
2217 *_aidl_return = incrementalService.setStorageParams(storage, enableReadLogs);
2218 return binder::Status::ok();
2219}
2220
Alex Buynytskyy0b202662020-04-13 09:53:04 -07002221FileId IncrementalService::idFromMetadata(std::span<const uint8_t> metadata) {
2222 return IncFs_FileIdFromMetadata({(const char*)metadata.data(), metadata.size()});
2223}
2224
Songchun Fan3c82a302019-11-29 14:23:45 -08002225} // namespace android::incremental