blob: bca21cd66117362595874c3331dd26696e3a9d9b [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
21#include <android-base/file.h>
22#include <android-base/logging.h>
23#include <android-base/properties.h>
24#include <android-base/stringprintf.h>
25#include <android-base/strings.h>
26#include <android/content/pm/IDataLoaderStatusListener.h>
27#include <android/os/IVold.h>
28#include <androidfw/ZipFileRO.h>
29#include <androidfw/ZipUtils.h>
30#include <binder/BinderService.h>
Jooyung Han66c567a2020-03-07 21:47:09 +090031#include <binder/Nullable.h>
Songchun Fan3c82a302019-11-29 14:23:45 -080032#include <binder/ParcelFileDescriptor.h>
33#include <binder/Status.h>
34#include <sys/stat.h>
35#include <uuid/uuid.h>
36#include <zlib.h>
37
Yurii Zubrytskyi107ae352020-04-03 13:12:51 -070038#include <charconv>
Alex Buynytskyy18b07a42020-02-03 20:06:00 -080039#include <ctime>
Songchun Fan1124fd32020-02-10 12:49:41 -080040#include <filesystem>
Songchun Fan3c82a302019-11-29 14:23:45 -080041#include <iterator>
42#include <span>
43#include <stack>
44#include <thread>
45#include <type_traits>
46
47#include "Metadata.pb.h"
48
49using namespace std::literals;
50using namespace android::content::pm;
Songchun Fan1124fd32020-02-10 12:49:41 -080051namespace fs = std::filesystem;
Songchun Fan3c82a302019-11-29 14:23:45 -080052
Alex Buynytskyy96e350b2020-04-02 20:03:47 -070053constexpr const char* kDataUsageStats = "android.permission.LOADER_USAGE_STATS";
54constexpr const char* kOpUsage = "android:get_usage_stats";
55
Songchun Fan3c82a302019-11-29 14:23:45 -080056namespace android::incremental {
57
58namespace {
59
60using IncrementalFileSystemControlParcel =
61 ::android::os::incremental::IncrementalFileSystemControlParcel;
62
63struct Constants {
64 static constexpr auto backing = "backing_store"sv;
65 static constexpr auto mount = "mount"sv;
Songchun Fan1124fd32020-02-10 12:49:41 -080066 static constexpr auto mountKeyPrefix = "MT_"sv;
Songchun Fan3c82a302019-11-29 14:23:45 -080067 static constexpr auto storagePrefix = "st"sv;
68 static constexpr auto mountpointMdPrefix = ".mountpoint."sv;
69 static constexpr auto infoMdName = ".info"sv;
Songchun Fan0f8b6fe2020-02-05 17:41:25 -080070 static constexpr auto libDir = "lib"sv;
71 static constexpr auto libSuffix = ".so"sv;
72 static constexpr auto blockSize = 4096;
Songchun Fan3c82a302019-11-29 14:23:45 -080073};
74
75static const Constants& constants() {
76 static Constants c;
77 return c;
78}
79
80template <base::LogSeverity level = base::ERROR>
81bool mkdirOrLog(std::string_view name, int mode = 0770, bool allowExisting = true) {
82 auto cstr = path::c_str(name);
83 if (::mkdir(cstr, mode)) {
Yurii Zubrytskyi4a25dfb2020-01-10 11:53:24 -080084 if (!allowExisting || errno != EEXIST) {
Songchun Fan3c82a302019-11-29 14:23:45 -080085 PLOG(level) << "Can't create directory '" << name << '\'';
86 return false;
87 }
88 struct stat st;
89 if (::stat(cstr, &st) || !S_ISDIR(st.st_mode)) {
90 PLOG(level) << "Path exists but is not a directory: '" << name << '\'';
91 return false;
92 }
93 }
Yurii Zubrytskyi4a25dfb2020-01-10 11:53:24 -080094 if (::chmod(cstr, mode)) {
95 PLOG(level) << "Changing permission failed for '" << name << '\'';
96 return false;
97 }
98
Songchun Fan3c82a302019-11-29 14:23:45 -080099 return true;
100}
101
102static std::string toMountKey(std::string_view path) {
103 if (path.empty()) {
104 return "@none";
105 }
106 if (path == "/"sv) {
107 return "@root";
108 }
109 if (path::isAbsolute(path)) {
110 path.remove_prefix(1);
111 }
112 std::string res(path);
113 std::replace(res.begin(), res.end(), '/', '_');
114 std::replace(res.begin(), res.end(), '@', '_');
Songchun Fan1124fd32020-02-10 12:49:41 -0800115 return std::string(constants().mountKeyPrefix) + res;
Songchun Fan3c82a302019-11-29 14:23:45 -0800116}
117
118static std::pair<std::string, std::string> makeMountDir(std::string_view incrementalDir,
119 std::string_view path) {
120 auto mountKey = toMountKey(path);
121 const auto prefixSize = mountKey.size();
122 for (int counter = 0; counter < 1000;
123 mountKey.resize(prefixSize), base::StringAppendF(&mountKey, "%d", counter++)) {
124 auto mountRoot = path::join(incrementalDir, mountKey);
Yurii Zubrytskyi4a25dfb2020-01-10 11:53:24 -0800125 if (mkdirOrLog(mountRoot, 0777, false)) {
Songchun Fan3c82a302019-11-29 14:23:45 -0800126 return {mountKey, mountRoot};
127 }
128 }
129 return {};
130}
131
132template <class ProtoMessage, class Control>
133static ProtoMessage parseFromIncfs(const IncFsWrapper* incfs, Control&& control,
134 std::string_view path) {
Yurii Zubrytskyi4a25dfb2020-01-10 11:53:24 -0800135 auto md = incfs->getMetadata(control, path);
Songchun Fan3c82a302019-11-29 14:23:45 -0800136 ProtoMessage message;
137 return message.ParseFromArray(md.data(), md.size()) ? message : ProtoMessage{};
138}
139
140static bool isValidMountTarget(std::string_view path) {
141 return path::isAbsolute(path) && path::isEmptyDir(path).value_or(true);
142}
143
144std::string makeBindMdName() {
145 static constexpr auto uuidStringSize = 36;
146
147 uuid_t guid;
148 uuid_generate(guid);
149
150 std::string name;
151 const auto prefixSize = constants().mountpointMdPrefix.size();
152 name.reserve(prefixSize + uuidStringSize);
153
154 name = constants().mountpointMdPrefix;
155 name.resize(prefixSize + uuidStringSize);
156 uuid_unparse(guid, name.data() + prefixSize);
157
158 return name;
159}
160} // namespace
161
162IncrementalService::IncFsMount::~IncFsMount() {
Songchun Fan68645c42020-02-27 15:57:35 -0800163 incrementalService.mDataLoaderManager->destroyDataLoader(mountId);
Songchun Fan3c82a302019-11-29 14:23:45 -0800164 LOG(INFO) << "Unmounting and cleaning up mount " << mountId << " with root '" << root << '\'';
165 for (auto&& [target, _] : bindPoints) {
166 LOG(INFO) << "\tbind: " << target;
167 incrementalService.mVold->unmountIncFs(target);
168 }
169 LOG(INFO) << "\troot: " << root;
170 incrementalService.mVold->unmountIncFs(path::join(root, constants().mount));
171 cleanupFilesystem(root);
172}
173
174auto IncrementalService::IncFsMount::makeStorage(StorageId id) -> StorageMap::iterator {
Songchun Fan3c82a302019-11-29 14:23:45 -0800175 std::string name;
176 for (int no = nextStorageDirNo.fetch_add(1, std::memory_order_relaxed), i = 0;
177 i < 1024 && no >= 0; no = nextStorageDirNo.fetch_add(1, std::memory_order_relaxed), ++i) {
178 name.clear();
Yurii Zubrytskyi4a25dfb2020-01-10 11:53:24 -0800179 base::StringAppendF(&name, "%.*s_%d_%d", int(constants().storagePrefix.size()),
180 constants().storagePrefix.data(), id, no);
181 auto fullName = path::join(root, constants().mount, name);
Songchun Fan96100932020-02-03 19:20:58 -0800182 if (auto err = incrementalService.mIncFs->makeDir(control, fullName, 0755); !err) {
Songchun Fan3c82a302019-11-29 14:23:45 -0800183 std::lock_guard l(lock);
Yurii Zubrytskyi4a25dfb2020-01-10 11:53:24 -0800184 return storages.insert_or_assign(id, Storage{std::move(fullName)}).first;
185 } else if (err != EEXIST) {
186 LOG(ERROR) << __func__ << "(): failed to create dir |" << fullName << "| " << err;
187 break;
Songchun Fan3c82a302019-11-29 14:23:45 -0800188 }
189 }
190 nextStorageDirNo = 0;
191 return storages.end();
192}
193
Yurii Zubrytskyi4a25dfb2020-01-10 11:53:24 -0800194static std::unique_ptr<DIR, decltype(&::closedir)> openDir(const char* path) {
195 return {::opendir(path), ::closedir};
196}
197
198static int rmDirContent(const char* path) {
199 auto dir = openDir(path);
200 if (!dir) {
201 return -EINVAL;
202 }
203 while (auto entry = ::readdir(dir.get())) {
204 if (entry->d_name == "."sv || entry->d_name == ".."sv) {
205 continue;
206 }
207 auto fullPath = android::base::StringPrintf("%s/%s", path, entry->d_name);
208 if (entry->d_type == DT_DIR) {
209 if (const auto err = rmDirContent(fullPath.c_str()); err != 0) {
210 PLOG(WARNING) << "Failed to delete " << fullPath << " content";
211 return err;
212 }
213 if (const auto err = ::rmdir(fullPath.c_str()); err != 0) {
214 PLOG(WARNING) << "Failed to rmdir " << fullPath;
215 return err;
216 }
217 } else {
218 if (const auto err = ::unlink(fullPath.c_str()); err != 0) {
219 PLOG(WARNING) << "Failed to delete " << fullPath;
220 return err;
221 }
222 }
223 }
224 return 0;
225}
226
Songchun Fan3c82a302019-11-29 14:23:45 -0800227void IncrementalService::IncFsMount::cleanupFilesystem(std::string_view root) {
Yurii Zubrytskyi4a25dfb2020-01-10 11:53:24 -0800228 rmDirContent(path::join(root, constants().backing).c_str());
Songchun Fan3c82a302019-11-29 14:23:45 -0800229 ::rmdir(path::join(root, constants().backing).c_str());
230 ::rmdir(path::join(root, constants().mount).c_str());
231 ::rmdir(path::c_str(root));
232}
233
Yurii Zubrytskyi4a25dfb2020-01-10 11:53:24 -0800234IncrementalService::IncrementalService(ServiceManagerWrapper&& sm, std::string_view rootDir)
Songchun Fan3c82a302019-11-29 14:23:45 -0800235 : mVold(sm.getVoldService()),
Songchun Fan68645c42020-02-27 15:57:35 -0800236 mDataLoaderManager(sm.getDataLoaderManager()),
Songchun Fan3c82a302019-11-29 14:23:45 -0800237 mIncFs(sm.getIncFs()),
Alex Buynytskyy96e350b2020-04-02 20:03:47 -0700238 mAppOpsManager(sm.getAppOpsManager()),
Songchun Fan3c82a302019-11-29 14:23:45 -0800239 mIncrementalDir(rootDir) {
240 if (!mVold) {
241 LOG(FATAL) << "Vold service is unavailable";
242 }
Songchun Fan68645c42020-02-27 15:57:35 -0800243 if (!mDataLoaderManager) {
244 LOG(FATAL) << "DataLoaderManagerService is unavailable";
Songchun Fan3c82a302019-11-29 14:23:45 -0800245 }
Alex Buynytskyy96e350b2020-04-02 20:03:47 -0700246 if (!mAppOpsManager) {
247 LOG(FATAL) << "AppOpsManager is unavailable";
248 }
Songchun Fan1124fd32020-02-10 12:49:41 -0800249 mountExistingImages();
Songchun Fan3c82a302019-11-29 14:23:45 -0800250}
251
Yurii Zubrytskyi4a25dfb2020-01-10 11:53:24 -0800252FileId IncrementalService::idFromMetadata(std::span<const uint8_t> metadata) {
Alex Buynytskyy04f73912020-02-10 08:34:18 -0800253 return IncFs_FileIdFromMetadata({(const char*)metadata.data(), metadata.size()});
Yurii Zubrytskyi4a25dfb2020-01-10 11:53:24 -0800254}
255
Songchun Fan3c82a302019-11-29 14:23:45 -0800256IncrementalService::~IncrementalService() = default;
257
Alex Buynytskyy18b07a42020-02-03 20:06:00 -0800258inline const char* toString(TimePoint t) {
259 using SystemClock = std::chrono::system_clock;
Songchun Fan0f8b6fe2020-02-05 17:41:25 -0800260 time_t time = SystemClock::to_time_t(
261 SystemClock::now() +
262 std::chrono::duration_cast<SystemClock::duration>(t - Clock::now()));
Alex Buynytskyy18b07a42020-02-03 20:06:00 -0800263 return std::ctime(&time);
264}
265
266inline const char* toString(IncrementalService::BindKind kind) {
267 switch (kind) {
Songchun Fan0f8b6fe2020-02-05 17:41:25 -0800268 case IncrementalService::BindKind::Temporary:
269 return "Temporary";
270 case IncrementalService::BindKind::Permanent:
271 return "Permanent";
Alex Buynytskyy18b07a42020-02-03 20:06:00 -0800272 }
273}
274
275void IncrementalService::onDump(int fd) {
276 dprintf(fd, "Incremental is %s\n", incfs::enabled() ? "ENABLED" : "DISABLED");
277 dprintf(fd, "Incremental dir: %s\n", mIncrementalDir.c_str());
278
279 std::unique_lock l(mLock);
280
281 dprintf(fd, "Mounts (%d):\n", int(mMounts.size()));
282 for (auto&& [id, ifs] : mMounts) {
283 const IncFsMount& mnt = *ifs.get();
284 dprintf(fd, "\t[%d]:\n", id);
285 dprintf(fd, "\t\tmountId: %d\n", mnt.mountId);
Yurii Zubrytskyi107ae352020-04-03 13:12:51 -0700286 dprintf(fd, "\t\troot: %s\n", mnt.root.c_str());
Alex Buynytskyy18b07a42020-02-03 20:06:00 -0800287 dprintf(fd, "\t\tnextStorageDirNo: %d\n", mnt.nextStorageDirNo.load());
288 dprintf(fd, "\t\tdataLoaderStatus: %d\n", mnt.dataLoaderStatus.load());
Alex Buynytskyy96e350b2020-04-02 20:03:47 -0700289 {
290 const auto& params = mnt.dataLoaderParams;
291 dprintf(fd, "\t\tdataLoaderParams:\n");
Alex Buynytskyy18b07a42020-02-03 20:06:00 -0800292 dprintf(fd, "\t\t\ttype: %s\n", toString(params.type).c_str());
293 dprintf(fd, "\t\t\tpackageName: %s\n", params.packageName.c_str());
294 dprintf(fd, "\t\t\tclassName: %s\n", params.className.c_str());
295 dprintf(fd, "\t\t\targuments: %s\n", params.arguments.c_str());
Alex Buynytskyy18b07a42020-02-03 20:06:00 -0800296 }
297 dprintf(fd, "\t\tstorages (%d):\n", int(mnt.storages.size()));
298 for (auto&& [storageId, storage] : mnt.storages) {
299 dprintf(fd, "\t\t\t[%d] -> [%s]\n", storageId, storage.name.c_str());
300 }
301
302 dprintf(fd, "\t\tbindPoints (%d):\n", int(mnt.bindPoints.size()));
303 for (auto&& [target, bind] : mnt.bindPoints) {
304 dprintf(fd, "\t\t\t[%s]->[%d]:\n", target.c_str(), bind.storage);
305 dprintf(fd, "\t\t\t\tsavedFilename: %s\n", bind.savedFilename.c_str());
306 dprintf(fd, "\t\t\t\tsourceDir: %s\n", bind.sourceDir.c_str());
307 dprintf(fd, "\t\t\t\tkind: %s\n", toString(bind.kind));
308 }
309 }
310
311 dprintf(fd, "Sorted binds (%d):\n", int(mBindsByPath.size()));
312 for (auto&& [target, mountPairIt] : mBindsByPath) {
313 const auto& bind = mountPairIt->second;
314 dprintf(fd, "\t\t[%s]->[%d]:\n", target.c_str(), bind.storage);
315 dprintf(fd, "\t\t\tsavedFilename: %s\n", bind.savedFilename.c_str());
316 dprintf(fd, "\t\t\tsourceDir: %s\n", bind.sourceDir.c_str());
317 dprintf(fd, "\t\t\tkind: %s\n", toString(bind.kind));
318 }
319}
320
Songchun Fan3c82a302019-11-29 14:23:45 -0800321std::optional<std::future<void>> IncrementalService::onSystemReady() {
322 std::promise<void> threadFinished;
323 if (mSystemReady.exchange(true)) {
324 return {};
325 }
326
327 std::vector<IfsMountPtr> mounts;
328 {
329 std::lock_guard l(mLock);
330 mounts.reserve(mMounts.size());
331 for (auto&& [id, ifs] : mMounts) {
332 if (ifs->mountId == id) {
333 mounts.push_back(ifs);
334 }
335 }
336 }
337
338 std::thread([this, mounts = std::move(mounts)]() {
Alex Buynytskyy96e350b2020-04-02 20:03:47 -0700339 /* TODO(b/151241369): restore data loaders on reboot.
Songchun Fan3c82a302019-11-29 14:23:45 -0800340 for (auto&& ifs : mounts) {
Alex Buynytskyy04f73912020-02-10 08:34:18 -0800341 if (prepareDataLoader(*ifs)) {
Songchun Fan3c82a302019-11-29 14:23:45 -0800342 LOG(INFO) << "Successfully started data loader for mount " << ifs->mountId;
343 } else {
Songchun Fan1124fd32020-02-10 12:49:41 -0800344 // TODO(b/133435829): handle data loader start failures
Songchun Fan3c82a302019-11-29 14:23:45 -0800345 LOG(WARNING) << "Failed to start data loader for mount " << ifs->mountId;
Songchun Fan3c82a302019-11-29 14:23:45 -0800346 }
347 }
Alex Buynytskyy96e350b2020-04-02 20:03:47 -0700348 */
Songchun Fan3c82a302019-11-29 14:23:45 -0800349 mPrepareDataLoaders.set_value_at_thread_exit();
350 }).detach();
351 return mPrepareDataLoaders.get_future();
352}
353
354auto IncrementalService::getStorageSlotLocked() -> MountMap::iterator {
355 for (;;) {
356 if (mNextId == kMaxStorageId) {
357 mNextId = 0;
358 }
359 auto id = ++mNextId;
360 auto [it, inserted] = mMounts.try_emplace(id, nullptr);
361 if (inserted) {
362 return it;
363 }
364 }
365}
366
Songchun Fan1124fd32020-02-10 12:49:41 -0800367StorageId IncrementalService::createStorage(
368 std::string_view mountPoint, DataLoaderParamsParcel&& dataLoaderParams,
369 const DataLoaderStatusListener& dataLoaderStatusListener, CreateOptions options) {
Songchun Fan3c82a302019-11-29 14:23:45 -0800370 LOG(INFO) << "createStorage: " << mountPoint << " | " << int(options);
371 if (!path::isAbsolute(mountPoint)) {
372 LOG(ERROR) << "path is not absolute: " << mountPoint;
373 return kInvalidStorageId;
374 }
375
376 auto mountNorm = path::normalize(mountPoint);
377 {
378 const auto id = findStorageId(mountNorm);
379 if (id != kInvalidStorageId) {
380 if (options & CreateOptions::OpenExisting) {
381 LOG(INFO) << "Opened existing storage " << id;
382 return id;
383 }
384 LOG(ERROR) << "Directory " << mountPoint << " is already mounted at storage " << id;
385 return kInvalidStorageId;
386 }
387 }
388
389 if (!(options & CreateOptions::CreateNew)) {
390 LOG(ERROR) << "not requirested create new storage, and it doesn't exist: " << mountPoint;
391 return kInvalidStorageId;
392 }
393
394 if (!path::isEmptyDir(mountNorm)) {
395 LOG(ERROR) << "Mounting over existing non-empty directory is not supported: " << mountNorm;
396 return kInvalidStorageId;
397 }
398 auto [mountKey, mountRoot] = makeMountDir(mIncrementalDir, mountNorm);
399 if (mountRoot.empty()) {
400 LOG(ERROR) << "Bad mount point";
401 return kInvalidStorageId;
402 }
403 // Make sure the code removes all crap it may create while still failing.
404 auto firstCleanup = [](const std::string* ptr) { IncFsMount::cleanupFilesystem(*ptr); };
405 auto firstCleanupOnFailure =
406 std::unique_ptr<std::string, decltype(firstCleanup)>(&mountRoot, firstCleanup);
407
408 auto mountTarget = path::join(mountRoot, constants().mount);
Yurii Zubrytskyi4a25dfb2020-01-10 11:53:24 -0800409 const auto backing = path::join(mountRoot, constants().backing);
410 if (!mkdirOrLog(backing, 0777) || !mkdirOrLog(mountTarget)) {
Songchun Fan3c82a302019-11-29 14:23:45 -0800411 return kInvalidStorageId;
412 }
413
Songchun Fan3c82a302019-11-29 14:23:45 -0800414 IncFsMount::Control control;
415 {
416 std::lock_guard l(mMountOperationLock);
417 IncrementalFileSystemControlParcel controlParcel;
Yurii Zubrytskyi4a25dfb2020-01-10 11:53:24 -0800418
419 if (auto err = rmDirContent(backing.c_str())) {
420 LOG(ERROR) << "Coudn't clean the backing directory " << backing << ": " << err;
421 return kInvalidStorageId;
422 }
423 if (!mkdirOrLog(path::join(backing, ".index"), 0777)) {
424 return kInvalidStorageId;
425 }
426 auto status = mVold->mountIncFs(backing, mountTarget, 0, &controlParcel);
Songchun Fan3c82a302019-11-29 14:23:45 -0800427 if (!status.isOk()) {
428 LOG(ERROR) << "Vold::mountIncFs() failed: " << status.toString8();
429 return kInvalidStorageId;
430 }
Yurii Zubrytskyi4a25dfb2020-01-10 11:53:24 -0800431 if (controlParcel.cmd.get() < 0 || controlParcel.pendingReads.get() < 0 ||
432 controlParcel.log.get() < 0) {
Songchun Fan3c82a302019-11-29 14:23:45 -0800433 LOG(ERROR) << "Vold::mountIncFs() returned invalid control parcel.";
434 return kInvalidStorageId;
435 }
Songchun Fan20d6ef22020-03-03 09:47:15 -0800436 int cmd = controlParcel.cmd.release().release();
437 int pendingReads = controlParcel.pendingReads.release().release();
438 int logs = controlParcel.log.release().release();
439 control = mIncFs->createControl(cmd, pendingReads, logs);
Songchun Fan3c82a302019-11-29 14:23:45 -0800440 }
441
442 std::unique_lock l(mLock);
443 const auto mountIt = getStorageSlotLocked();
444 const auto mountId = mountIt->first;
445 l.unlock();
446
447 auto ifs =
448 std::make_shared<IncFsMount>(std::move(mountRoot), mountId, std::move(control), *this);
449 // Now it's the |ifs|'s responsibility to clean up after itself, and the only cleanup we need
450 // is the removal of the |ifs|.
451 firstCleanupOnFailure.release();
452
453 auto secondCleanup = [this, &l](auto itPtr) {
454 if (!l.owns_lock()) {
455 l.lock();
456 }
457 mMounts.erase(*itPtr);
458 };
459 auto secondCleanupOnFailure =
460 std::unique_ptr<decltype(mountIt), decltype(secondCleanup)>(&mountIt, secondCleanup);
461
462 const auto storageIt = ifs->makeStorage(ifs->mountId);
463 if (storageIt == ifs->storages.end()) {
Yurii Zubrytskyi4a25dfb2020-01-10 11:53:24 -0800464 LOG(ERROR) << "Can't create a default storage directory";
Songchun Fan3c82a302019-11-29 14:23:45 -0800465 return kInvalidStorageId;
466 }
467
Alex Buynytskyy96e350b2020-04-02 20:03:47 -0700468 ifs->dataLoaderParams = std::move(dataLoaderParams);
469
Songchun Fan3c82a302019-11-29 14:23:45 -0800470 {
471 metadata::Mount m;
472 m.mutable_storage()->set_id(ifs->mountId);
Alex Buynytskyy96e350b2020-04-02 20:03:47 -0700473 m.mutable_loader()->set_type((int)ifs->dataLoaderParams.type);
474 m.mutable_loader()->set_package_name(ifs->dataLoaderParams.packageName);
475 m.mutable_loader()->set_class_name(ifs->dataLoaderParams.className);
476 m.mutable_loader()->set_arguments(ifs->dataLoaderParams.arguments);
Songchun Fan3c82a302019-11-29 14:23:45 -0800477 const auto metadata = m.SerializeAsString();
478 m.mutable_loader()->release_arguments();
Alex Buynytskyy1ecfcec2019-12-17 12:10:41 -0800479 m.mutable_loader()->release_class_name();
Songchun Fan3c82a302019-11-29 14:23:45 -0800480 m.mutable_loader()->release_package_name();
Yurii Zubrytskyi4a25dfb2020-01-10 11:53:24 -0800481 if (auto err =
482 mIncFs->makeFile(ifs->control,
483 path::join(ifs->root, constants().mount,
484 constants().infoMdName),
485 0777, idFromMetadata(metadata),
486 {.metadata = {metadata.data(), (IncFsSize)metadata.size()}})) {
Songchun Fan3c82a302019-11-29 14:23:45 -0800487 LOG(ERROR) << "Saving mount metadata failed: " << -err;
488 return kInvalidStorageId;
489 }
490 }
491
492 const auto bk =
493 (options & CreateOptions::PermanentBind) ? BindKind::Permanent : BindKind::Temporary;
Yurii Zubrytskyi4a25dfb2020-01-10 11:53:24 -0800494 if (auto err = addBindMount(*ifs, storageIt->first, storageIt->second.name,
495 std::string(storageIt->second.name), std::move(mountNorm), bk, l);
Songchun Fan3c82a302019-11-29 14:23:45 -0800496 err < 0) {
497 LOG(ERROR) << "adding bind mount failed: " << -err;
498 return kInvalidStorageId;
499 }
500
501 // Done here as well, all data structures are in good state.
502 secondCleanupOnFailure.release();
503
Alex Buynytskyy96e350b2020-04-02 20:03:47 -0700504 if (!prepareDataLoader(*ifs, &dataLoaderStatusListener)) {
Songchun Fan3c82a302019-11-29 14:23:45 -0800505 LOG(ERROR) << "prepareDataLoader() failed";
506 deleteStorageLocked(*ifs, std::move(l));
507 return kInvalidStorageId;
508 }
509
510 mountIt->second = std::move(ifs);
511 l.unlock();
512 LOG(INFO) << "created storage " << mountId;
513 return mountId;
514}
515
516StorageId IncrementalService::createLinkedStorage(std::string_view mountPoint,
517 StorageId linkedStorage,
518 IncrementalService::CreateOptions options) {
519 if (!isValidMountTarget(mountPoint)) {
520 LOG(ERROR) << "Mount point is invalid or missing";
521 return kInvalidStorageId;
522 }
523
524 std::unique_lock l(mLock);
525 const auto& ifs = getIfsLocked(linkedStorage);
526 if (!ifs) {
527 LOG(ERROR) << "Ifs unavailable";
528 return kInvalidStorageId;
529 }
530
531 const auto mountIt = getStorageSlotLocked();
532 const auto storageId = mountIt->first;
533 const auto storageIt = ifs->makeStorage(storageId);
534 if (storageIt == ifs->storages.end()) {
535 LOG(ERROR) << "Can't create a new storage";
536 mMounts.erase(mountIt);
537 return kInvalidStorageId;
538 }
539
540 l.unlock();
541
542 const auto bk =
543 (options & CreateOptions::PermanentBind) ? BindKind::Permanent : BindKind::Temporary;
Yurii Zubrytskyi4a25dfb2020-01-10 11:53:24 -0800544 if (auto err = addBindMount(*ifs, storageIt->first, storageIt->second.name,
545 std::string(storageIt->second.name), path::normalize(mountPoint),
546 bk, l);
Songchun Fan3c82a302019-11-29 14:23:45 -0800547 err < 0) {
548 LOG(ERROR) << "bindMount failed with error: " << err;
549 return kInvalidStorageId;
550 }
551
552 mountIt->second = ifs;
553 return storageId;
554}
555
556IncrementalService::BindPathMap::const_iterator IncrementalService::findStorageLocked(
557 std::string_view path) const {
558 auto bindPointIt = mBindsByPath.upper_bound(path);
559 if (bindPointIt == mBindsByPath.begin()) {
560 return mBindsByPath.end();
561 }
562 --bindPointIt;
563 if (!path::startsWith(path, bindPointIt->first)) {
564 return mBindsByPath.end();
565 }
566 return bindPointIt;
567}
568
569StorageId IncrementalService::findStorageId(std::string_view path) const {
570 std::lock_guard l(mLock);
571 auto it = findStorageLocked(path);
572 if (it == mBindsByPath.end()) {
573 return kInvalidStorageId;
574 }
575 return it->second->second.storage;
576}
577
Alex Buynytskyy5e860ba2020-03-31 15:30:21 -0700578int IncrementalService::setStorageParams(StorageId storageId, bool enableReadLogs) {
579 const auto ifs = getIfs(storageId);
580 if (!ifs) {
Alex Buynytskyy5f9e3a02020-04-07 21:13:41 -0700581 LOG(ERROR) << "setStorageParams failed, invalid storageId: " << storageId;
Alex Buynytskyy5e860ba2020-03-31 15:30:21 -0700582 return -EINVAL;
583 }
584
Alex Buynytskyy96e350b2020-04-02 20:03:47 -0700585 if (enableReadLogs) {
Alex Buynytskyy1d892162020-04-03 23:00:19 -0700586 if (auto status =
587 mAppOpsManager->checkPermission(kDataUsageStats, kOpUsage,
588 ifs->dataLoaderParams.packageName.c_str());
Alex Buynytskyy96e350b2020-04-02 20:03:47 -0700589 !status.isOk()) {
Alex Buynytskyy1d892162020-04-03 23:00:19 -0700590 LOG(ERROR) << "checkPermission failed: " << status.toString8();
Alex Buynytskyy96e350b2020-04-02 20:03:47 -0700591 return fromBinderStatus(status);
592 }
593 }
594
Alex Buynytskyy1d892162020-04-03 23:00:19 -0700595 if (auto status = applyStorageParams(*ifs, enableReadLogs); !status.isOk()) {
596 LOG(ERROR) << "applyStorageParams failed: " << status.toString8();
597 return fromBinderStatus(status);
598 }
599
600 if (enableReadLogs) {
601 registerAppOpsCallback(ifs->dataLoaderParams.packageName);
602 }
603
604 return 0;
605}
606
607binder::Status IncrementalService::applyStorageParams(IncFsMount& ifs, bool enableReadLogs) {
Alex Buynytskyy5e860ba2020-03-31 15:30:21 -0700608 using unique_fd = ::android::base::unique_fd;
609 ::android::os::incremental::IncrementalFileSystemControlParcel control;
Alex Buynytskyy96e350b2020-04-02 20:03:47 -0700610 control.cmd.reset(unique_fd(dup(ifs.control.cmd())));
611 control.pendingReads.reset(unique_fd(dup(ifs.control.pendingReads())));
612 auto logsFd = ifs.control.logs();
Alex Buynytskyy5e860ba2020-03-31 15:30:21 -0700613 if (logsFd >= 0) {
614 control.log.reset(unique_fd(dup(logsFd)));
615 }
616
617 std::lock_guard l(mMountOperationLock);
Alex Buynytskyy1d892162020-04-03 23:00:19 -0700618 return mVold->setIncFsMountOptions(control, enableReadLogs);
Alex Buynytskyy5e860ba2020-03-31 15:30:21 -0700619}
620
Songchun Fan3c82a302019-11-29 14:23:45 -0800621void IncrementalService::deleteStorage(StorageId storageId) {
622 const auto ifs = getIfs(storageId);
623 if (!ifs) {
624 return;
625 }
626 deleteStorage(*ifs);
627}
628
629void IncrementalService::deleteStorage(IncrementalService::IncFsMount& ifs) {
630 std::unique_lock l(ifs.lock);
631 deleteStorageLocked(ifs, std::move(l));
632}
633
634void IncrementalService::deleteStorageLocked(IncrementalService::IncFsMount& ifs,
635 std::unique_lock<std::mutex>&& ifsLock) {
636 const auto storages = std::move(ifs.storages);
637 // Don't move the bind points out: Ifs's dtor will use them to unmount everything.
638 const auto bindPoints = ifs.bindPoints;
639 ifsLock.unlock();
640
641 std::lock_guard l(mLock);
642 for (auto&& [id, _] : storages) {
643 if (id != ifs.mountId) {
644 mMounts.erase(id);
645 }
646 }
647 for (auto&& [path, _] : bindPoints) {
648 mBindsByPath.erase(path);
649 }
650 mMounts.erase(ifs.mountId);
651}
652
653StorageId IncrementalService::openStorage(std::string_view pathInMount) {
654 if (!path::isAbsolute(pathInMount)) {
655 return kInvalidStorageId;
656 }
657
658 return findStorageId(path::normalize(pathInMount));
659}
660
Yurii Zubrytskyi4a25dfb2020-01-10 11:53:24 -0800661FileId IncrementalService::nodeFor(StorageId storage, std::string_view subpath) const {
Songchun Fan3c82a302019-11-29 14:23:45 -0800662 const auto ifs = getIfs(storage);
663 if (!ifs) {
Yurii Zubrytskyi4a25dfb2020-01-10 11:53:24 -0800664 return kIncFsInvalidFileId;
Songchun Fan3c82a302019-11-29 14:23:45 -0800665 }
666 std::unique_lock l(ifs->lock);
667 auto storageIt = ifs->storages.find(storage);
668 if (storageIt == ifs->storages.end()) {
Yurii Zubrytskyi4a25dfb2020-01-10 11:53:24 -0800669 return kIncFsInvalidFileId;
Songchun Fan3c82a302019-11-29 14:23:45 -0800670 }
671 if (subpath.empty() || subpath == "."sv) {
Yurii Zubrytskyi4a25dfb2020-01-10 11:53:24 -0800672 return kIncFsInvalidFileId;
Songchun Fan3c82a302019-11-29 14:23:45 -0800673 }
674 auto path = path::join(ifs->root, constants().mount, storageIt->second.name, subpath);
675 l.unlock();
Yurii Zubrytskyi4a25dfb2020-01-10 11:53:24 -0800676 return mIncFs->getFileId(ifs->control, path);
Songchun Fan3c82a302019-11-29 14:23:45 -0800677}
678
Yurii Zubrytskyi4a25dfb2020-01-10 11:53:24 -0800679std::pair<FileId, std::string_view> IncrementalService::parentAndNameFor(
Songchun Fan3c82a302019-11-29 14:23:45 -0800680 StorageId storage, std::string_view subpath) const {
681 auto name = path::basename(subpath);
682 if (name.empty()) {
Yurii Zubrytskyi4a25dfb2020-01-10 11:53:24 -0800683 return {kIncFsInvalidFileId, {}};
Songchun Fan3c82a302019-11-29 14:23:45 -0800684 }
685 auto dir = path::dirname(subpath);
686 if (dir.empty() || dir == "/"sv) {
Yurii Zubrytskyi4a25dfb2020-01-10 11:53:24 -0800687 return {kIncFsInvalidFileId, {}};
Songchun Fan3c82a302019-11-29 14:23:45 -0800688 }
Yurii Zubrytskyi4a25dfb2020-01-10 11:53:24 -0800689 auto id = nodeFor(storage, dir);
690 return {id, name};
Songchun Fan3c82a302019-11-29 14:23:45 -0800691}
692
693IncrementalService::IfsMountPtr IncrementalService::getIfs(StorageId storage) const {
694 std::lock_guard l(mLock);
695 return getIfsLocked(storage);
696}
697
698const IncrementalService::IfsMountPtr& IncrementalService::getIfsLocked(StorageId storage) const {
699 auto it = mMounts.find(storage);
700 if (it == mMounts.end()) {
701 static const IfsMountPtr kEmpty = {};
702 return kEmpty;
703 }
704 return it->second;
705}
706
Yurii Zubrytskyi4a25dfb2020-01-10 11:53:24 -0800707int IncrementalService::bind(StorageId storage, std::string_view source, std::string_view target,
708 BindKind kind) {
Songchun Fan3c82a302019-11-29 14:23:45 -0800709 if (!isValidMountTarget(target)) {
710 return -EINVAL;
711 }
712
713 const auto ifs = getIfs(storage);
714 if (!ifs) {
715 return -EINVAL;
716 }
Yurii Zubrytskyi4a25dfb2020-01-10 11:53:24 -0800717
Songchun Fan3c82a302019-11-29 14:23:45 -0800718 std::unique_lock l(ifs->lock);
719 const auto storageInfo = ifs->storages.find(storage);
720 if (storageInfo == ifs->storages.end()) {
721 return -EINVAL;
722 }
Songchun Fan103ba1d2020-02-03 17:32:32 -0800723 std::string normSource = normalizePathToStorage(ifs, storage, source);
Songchun Fan3c82a302019-11-29 14:23:45 -0800724 l.unlock();
725 std::unique_lock l2(mLock, std::defer_lock);
Yurii Zubrytskyi4a25dfb2020-01-10 11:53:24 -0800726 return addBindMount(*ifs, storage, storageInfo->second.name, std::move(normSource),
727 path::normalize(target), kind, l2);
Songchun Fan3c82a302019-11-29 14:23:45 -0800728}
729
730int IncrementalService::unbind(StorageId storage, std::string_view target) {
731 if (!path::isAbsolute(target)) {
732 return -EINVAL;
733 }
734
735 LOG(INFO) << "Removing bind point " << target;
736
737 // Here we should only look up by the exact target, not by a subdirectory of any existing mount,
738 // otherwise there's a chance to unmount something completely unrelated
739 const auto norm = path::normalize(target);
740 std::unique_lock l(mLock);
741 const auto storageIt = mBindsByPath.find(norm);
742 if (storageIt == mBindsByPath.end() || storageIt->second->second.storage != storage) {
743 return -EINVAL;
744 }
745 const auto bindIt = storageIt->second;
746 const auto storageId = bindIt->second.storage;
747 const auto ifs = getIfsLocked(storageId);
748 if (!ifs) {
749 LOG(ERROR) << "Internal error: storageId " << storageId << " for bound path " << target
750 << " is missing";
751 return -EFAULT;
752 }
753 mBindsByPath.erase(storageIt);
754 l.unlock();
755
756 mVold->unmountIncFs(bindIt->first);
757 std::unique_lock l2(ifs->lock);
758 if (ifs->bindPoints.size() <= 1) {
759 ifs->bindPoints.clear();
760 deleteStorageLocked(*ifs, std::move(l2));
761 } else {
762 const std::string savedFile = std::move(bindIt->second.savedFilename);
763 ifs->bindPoints.erase(bindIt);
764 l2.unlock();
765 if (!savedFile.empty()) {
Yurii Zubrytskyi4a25dfb2020-01-10 11:53:24 -0800766 mIncFs->unlink(ifs->control, path::join(ifs->root, constants().mount, savedFile));
Songchun Fan3c82a302019-11-29 14:23:45 -0800767 }
768 }
769 return 0;
770}
771
Songchun Fan103ba1d2020-02-03 17:32:32 -0800772std::string IncrementalService::normalizePathToStorage(const IncrementalService::IfsMountPtr ifs,
773 StorageId storage, std::string_view path) {
774 const auto storageInfo = ifs->storages.find(storage);
775 if (storageInfo == ifs->storages.end()) {
776 return {};
777 }
778 std::string normPath;
779 if (path::isAbsolute(path)) {
780 normPath = path::normalize(path);
781 } else {
782 normPath = path::normalize(path::join(storageInfo->second.name, path));
783 }
784 if (!path::startsWith(normPath, storageInfo->second.name)) {
785 return {};
786 }
787 return normPath;
788}
789
Yurii Zubrytskyi4a25dfb2020-01-10 11:53:24 -0800790int IncrementalService::makeFile(StorageId storage, std::string_view path, int mode, FileId id,
791 incfs::NewFileParams params) {
792 if (auto ifs = getIfs(storage)) {
Songchun Fan103ba1d2020-02-03 17:32:32 -0800793 std::string normPath = normalizePathToStorage(ifs, storage, path);
794 if (normPath.empty()) {
Alex Buynytskyy5e860ba2020-03-31 15:30:21 -0700795 LOG(ERROR) << "Internal error: storageId " << storage << " failed to normalize: " << path;
Songchun Fan54c6aed2020-01-31 16:52:41 -0800796 return -EINVAL;
797 }
798 auto err = mIncFs->makeFile(ifs->control, normPath, mode, id, params);
Yurii Zubrytskyi4a25dfb2020-01-10 11:53:24 -0800799 if (err) {
Alex Buynytskyy5e860ba2020-03-31 15:30:21 -0700800 LOG(ERROR) << "Internal error: storageId " << storage << " failed to makeFile: " << err;
Yurii Zubrytskyi4a25dfb2020-01-10 11:53:24 -0800801 return err;
Songchun Fan3c82a302019-11-29 14:23:45 -0800802 }
Yurii Zubrytskyi4a25dfb2020-01-10 11:53:24 -0800803 std::vector<uint8_t> metadataBytes;
804 if (params.metadata.data && params.metadata.size > 0) {
805 metadataBytes.assign(params.metadata.data, params.metadata.data + params.metadata.size);
Songchun Fan3c82a302019-11-29 14:23:45 -0800806 }
Yurii Zubrytskyi4a25dfb2020-01-10 11:53:24 -0800807 return 0;
Songchun Fan3c82a302019-11-29 14:23:45 -0800808 }
809 return -EINVAL;
810}
811
Yurii Zubrytskyi4a25dfb2020-01-10 11:53:24 -0800812int IncrementalService::makeDir(StorageId storageId, std::string_view path, int mode) {
Songchun Fan3c82a302019-11-29 14:23:45 -0800813 if (auto ifs = getIfs(storageId)) {
Songchun Fan103ba1d2020-02-03 17:32:32 -0800814 std::string normPath = normalizePathToStorage(ifs, storageId, path);
815 if (normPath.empty()) {
816 return -EINVAL;
817 }
818 return mIncFs->makeDir(ifs->control, normPath, mode);
Songchun Fan3c82a302019-11-29 14:23:45 -0800819 }
820 return -EINVAL;
821}
822
Yurii Zubrytskyi4a25dfb2020-01-10 11:53:24 -0800823int IncrementalService::makeDirs(StorageId storageId, std::string_view path, int mode) {
Songchun Fan3c82a302019-11-29 14:23:45 -0800824 const auto ifs = getIfs(storageId);
825 if (!ifs) {
826 return -EINVAL;
827 }
Songchun Fan103ba1d2020-02-03 17:32:32 -0800828 std::string normPath = normalizePathToStorage(ifs, storageId, path);
829 if (normPath.empty()) {
830 return -EINVAL;
831 }
832 auto err = mIncFs->makeDir(ifs->control, normPath, mode);
Yurii Zubrytskyi4a25dfb2020-01-10 11:53:24 -0800833 if (err == -EEXIST) {
834 return 0;
835 } else if (err != -ENOENT) {
836 return err;
Songchun Fan3c82a302019-11-29 14:23:45 -0800837 }
Songchun Fan103ba1d2020-02-03 17:32:32 -0800838 if (auto err = makeDirs(storageId, path::dirname(normPath), mode)) {
Yurii Zubrytskyi4a25dfb2020-01-10 11:53:24 -0800839 return err;
Songchun Fan3c82a302019-11-29 14:23:45 -0800840 }
Songchun Fan103ba1d2020-02-03 17:32:32 -0800841 return mIncFs->makeDir(ifs->control, normPath, mode);
Songchun Fan3c82a302019-11-29 14:23:45 -0800842}
843
Yurii Zubrytskyi4a25dfb2020-01-10 11:53:24 -0800844int IncrementalService::link(StorageId sourceStorageId, std::string_view oldPath,
845 StorageId destStorageId, std::string_view newPath) {
846 if (auto ifsSrc = getIfs(sourceStorageId), ifsDest = getIfs(destStorageId);
847 ifsSrc && ifsSrc == ifsDest) {
Songchun Fan103ba1d2020-02-03 17:32:32 -0800848 std::string normOldPath = normalizePathToStorage(ifsSrc, sourceStorageId, oldPath);
849 std::string normNewPath = normalizePathToStorage(ifsDest, destStorageId, newPath);
850 if (normOldPath.empty() || normNewPath.empty()) {
851 return -EINVAL;
852 }
853 return mIncFs->link(ifsSrc->control, normOldPath, normNewPath);
Songchun Fan3c82a302019-11-29 14:23:45 -0800854 }
855 return -EINVAL;
856}
857
Yurii Zubrytskyi4a25dfb2020-01-10 11:53:24 -0800858int IncrementalService::unlink(StorageId storage, std::string_view path) {
Songchun Fan3c82a302019-11-29 14:23:45 -0800859 if (auto ifs = getIfs(storage)) {
Songchun Fan103ba1d2020-02-03 17:32:32 -0800860 std::string normOldPath = normalizePathToStorage(ifs, storage, path);
861 return mIncFs->unlink(ifs->control, normOldPath);
Songchun Fan3c82a302019-11-29 14:23:45 -0800862 }
863 return -EINVAL;
864}
865
Yurii Zubrytskyi4a25dfb2020-01-10 11:53:24 -0800866int IncrementalService::addBindMount(IncFsMount& ifs, StorageId storage,
867 std::string_view storageRoot, std::string&& source,
Songchun Fan3c82a302019-11-29 14:23:45 -0800868 std::string&& target, BindKind kind,
869 std::unique_lock<std::mutex>& mainLock) {
870 if (!isValidMountTarget(target)) {
871 return -EINVAL;
872 }
873
874 std::string mdFileName;
875 if (kind != BindKind::Temporary) {
876 metadata::BindPoint bp;
877 bp.set_storage_id(storage);
878 bp.set_allocated_dest_path(&target);
Songchun Fan1124fd32020-02-10 12:49:41 -0800879 bp.set_allocated_source_subdir(&source);
Songchun Fan3c82a302019-11-29 14:23:45 -0800880 const auto metadata = bp.SerializeAsString();
Songchun Fan3c82a302019-11-29 14:23:45 -0800881 bp.release_dest_path();
Songchun Fan1124fd32020-02-10 12:49:41 -0800882 bp.release_source_subdir();
Songchun Fan3c82a302019-11-29 14:23:45 -0800883 mdFileName = makeBindMdName();
Yurii Zubrytskyi4a25dfb2020-01-10 11:53:24 -0800884 auto node =
885 mIncFs->makeFile(ifs.control, path::join(ifs.root, constants().mount, mdFileName),
886 0444, idFromMetadata(metadata),
887 {.metadata = {metadata.data(), (IncFsSize)metadata.size()}});
888 if (node) {
Songchun Fan3c82a302019-11-29 14:23:45 -0800889 return int(node);
890 }
891 }
892
Yurii Zubrytskyi4a25dfb2020-01-10 11:53:24 -0800893 return addBindMountWithMd(ifs, storage, std::move(mdFileName), std::move(source),
Songchun Fan3c82a302019-11-29 14:23:45 -0800894 std::move(target), kind, mainLock);
895}
896
897int IncrementalService::addBindMountWithMd(IncrementalService::IncFsMount& ifs, StorageId storage,
Yurii Zubrytskyi4a25dfb2020-01-10 11:53:24 -0800898 std::string&& metadataName, std::string&& source,
Songchun Fan3c82a302019-11-29 14:23:45 -0800899 std::string&& target, BindKind kind,
900 std::unique_lock<std::mutex>& mainLock) {
Songchun Fan3c82a302019-11-29 14:23:45 -0800901 {
Songchun Fan3c82a302019-11-29 14:23:45 -0800902 std::lock_guard l(mMountOperationLock);
Yurii Zubrytskyi4a25dfb2020-01-10 11:53:24 -0800903 const auto status = mVold->bindMount(source, target);
Songchun Fan3c82a302019-11-29 14:23:45 -0800904 if (!status.isOk()) {
905 LOG(ERROR) << "Calling Vold::bindMount() failed: " << status.toString8();
906 return status.exceptionCode() == binder::Status::EX_SERVICE_SPECIFIC
907 ? status.serviceSpecificErrorCode() > 0 ? -status.serviceSpecificErrorCode()
908 : status.serviceSpecificErrorCode() == 0
909 ? -EFAULT
910 : status.serviceSpecificErrorCode()
911 : -EIO;
912 }
913 }
914
915 if (!mainLock.owns_lock()) {
916 mainLock.lock();
917 }
918 std::lock_guard l(ifs.lock);
919 const auto [it, _] =
920 ifs.bindPoints.insert_or_assign(target,
921 IncFsMount::Bind{storage, std::move(metadataName),
Yurii Zubrytskyi4a25dfb2020-01-10 11:53:24 -0800922 std::move(source), kind});
Songchun Fan3c82a302019-11-29 14:23:45 -0800923 mBindsByPath[std::move(target)] = it;
924 return 0;
925}
926
Yurii Zubrytskyi4a25dfb2020-01-10 11:53:24 -0800927RawMetadata IncrementalService::getMetadata(StorageId storage, FileId node) const {
Songchun Fan3c82a302019-11-29 14:23:45 -0800928 const auto ifs = getIfs(storage);
929 if (!ifs) {
930 return {};
931 }
932 return mIncFs->getMetadata(ifs->control, node);
933}
934
935std::vector<std::string> IncrementalService::listFiles(StorageId storage) const {
936 const auto ifs = getIfs(storage);
937 if (!ifs) {
938 return {};
939 }
940
941 std::unique_lock l(ifs->lock);
942 auto subdirIt = ifs->storages.find(storage);
943 if (subdirIt == ifs->storages.end()) {
944 return {};
945 }
946 auto dir = path::join(ifs->root, constants().mount, subdirIt->second.name);
947 l.unlock();
948
949 const auto prefixSize = dir.size() + 1;
950 std::vector<std::string> todoDirs{std::move(dir)};
951 std::vector<std::string> result;
952 do {
953 auto currDir = std::move(todoDirs.back());
954 todoDirs.pop_back();
955
956 auto d =
957 std::unique_ptr<DIR, decltype(&::closedir)>(::opendir(currDir.c_str()), ::closedir);
958 while (auto e = ::readdir(d.get())) {
959 if (e->d_type == DT_REG) {
960 result.emplace_back(
961 path::join(std::string_view(currDir).substr(prefixSize), e->d_name));
962 continue;
963 }
964 if (e->d_type == DT_DIR) {
965 if (e->d_name == "."sv || e->d_name == ".."sv) {
966 continue;
967 }
968 todoDirs.emplace_back(path::join(currDir, e->d_name));
969 continue;
970 }
971 }
972 } while (!todoDirs.empty());
973 return result;
974}
975
976bool IncrementalService::startLoading(StorageId storage) const {
Alex Buynytskyybf1c0632020-03-10 15:49:29 -0700977 {
978 std::unique_lock l(mLock);
979 const auto& ifs = getIfsLocked(storage);
980 if (!ifs) {
Songchun Fan3c82a302019-11-29 14:23:45 -0800981 return false;
982 }
Alex Buynytskyybf1c0632020-03-10 15:49:29 -0700983 if (ifs->dataLoaderStatus != IDataLoaderStatusListener::DATA_LOADER_CREATED) {
984 ifs->dataLoaderStartRequested = true;
985 return true;
986 }
Songchun Fan3c82a302019-11-29 14:23:45 -0800987 }
Alex Buynytskyybf1c0632020-03-10 15:49:29 -0700988 return startDataLoader(storage);
989}
990
991bool IncrementalService::startDataLoader(MountId mountId) const {
Songchun Fan68645c42020-02-27 15:57:35 -0800992 sp<IDataLoader> dataloader;
Alex Buynytskyybf1c0632020-03-10 15:49:29 -0700993 auto status = mDataLoaderManager->getDataLoader(mountId, &dataloader);
Songchun Fan3c82a302019-11-29 14:23:45 -0800994 if (!status.isOk()) {
995 return false;
996 }
Songchun Fan68645c42020-02-27 15:57:35 -0800997 if (!dataloader) {
998 return false;
999 }
Alex Buynytskyyb6e02f72020-03-17 18:12:23 -07001000 status = dataloader->start(mountId);
Songchun Fan68645c42020-02-27 15:57:35 -08001001 if (!status.isOk()) {
1002 return false;
1003 }
1004 return true;
Songchun Fan3c82a302019-11-29 14:23:45 -08001005}
1006
1007void IncrementalService::mountExistingImages() {
Songchun Fan1124fd32020-02-10 12:49:41 -08001008 for (const auto& entry : fs::directory_iterator(mIncrementalDir)) {
1009 const auto path = entry.path().u8string();
1010 const auto name = entry.path().filename().u8string();
1011 if (!base::StartsWith(name, constants().mountKeyPrefix)) {
Songchun Fan3c82a302019-11-29 14:23:45 -08001012 continue;
1013 }
Songchun Fan1124fd32020-02-10 12:49:41 -08001014 const auto root = path::join(mIncrementalDir, name);
Yurii Zubrytskyi107ae352020-04-03 13:12:51 -07001015 if (!mountExistingImage(root)) {
Songchun Fan1124fd32020-02-10 12:49:41 -08001016 IncFsMount::cleanupFilesystem(path);
Songchun Fan3c82a302019-11-29 14:23:45 -08001017 }
1018 }
1019}
1020
Yurii Zubrytskyi107ae352020-04-03 13:12:51 -07001021bool IncrementalService::mountExistingImage(std::string_view root) {
Songchun Fan3c82a302019-11-29 14:23:45 -08001022 auto mountTarget = path::join(root, constants().mount);
Yurii Zubrytskyi4a25dfb2020-01-10 11:53:24 -08001023 const auto backing = path::join(root, constants().backing);
Songchun Fan3c82a302019-11-29 14:23:45 -08001024
Songchun Fan3c82a302019-11-29 14:23:45 -08001025 IncrementalFileSystemControlParcel controlParcel;
Yurii Zubrytskyi4a25dfb2020-01-10 11:53:24 -08001026 auto status = mVold->mountIncFs(backing, mountTarget, 0, &controlParcel);
Songchun Fan3c82a302019-11-29 14:23:45 -08001027 if (!status.isOk()) {
1028 LOG(ERROR) << "Vold::mountIncFs() failed: " << status.toString8();
1029 return false;
1030 }
Songchun Fan20d6ef22020-03-03 09:47:15 -08001031
1032 int cmd = controlParcel.cmd.release().release();
1033 int pendingReads = controlParcel.pendingReads.release().release();
1034 int logs = controlParcel.log.release().release();
1035 IncFsMount::Control control = mIncFs->createControl(cmd, pendingReads, logs);
Songchun Fan3c82a302019-11-29 14:23:45 -08001036
1037 auto ifs = std::make_shared<IncFsMount>(std::string(root), -1, std::move(control), *this);
1038
Alex Buynytskyy96e350b2020-04-02 20:03:47 -07001039 auto mount = parseFromIncfs<metadata::Mount>(mIncFs.get(), ifs->control,
1040 path::join(mountTarget, constants().infoMdName));
1041 if (!mount.has_loader() || !mount.has_storage()) {
Songchun Fan3c82a302019-11-29 14:23:45 -08001042 LOG(ERROR) << "Bad mount metadata in mount at " << root;
1043 return false;
1044 }
1045
Alex Buynytskyy96e350b2020-04-02 20:03:47 -07001046 ifs->mountId = mount.storage().id();
Songchun Fan3c82a302019-11-29 14:23:45 -08001047 mNextId = std::max(mNextId, ifs->mountId + 1);
1048
Alex Buynytskyy96e350b2020-04-02 20:03:47 -07001049 // DataLoader params
1050 {
1051 auto& dlp = ifs->dataLoaderParams;
1052 const auto& loader = mount.loader();
1053 dlp.type = (android::content::pm::DataLoaderType)loader.type();
1054 dlp.packageName = loader.package_name();
1055 dlp.className = loader.class_name();
1056 dlp.arguments = loader.arguments();
1057 }
1058
Songchun Fan3c82a302019-11-29 14:23:45 -08001059 std::vector<std::pair<std::string, metadata::BindPoint>> bindPoints;
Yurii Zubrytskyi4a25dfb2020-01-10 11:53:24 -08001060 auto d = openDir(path::c_str(mountTarget));
Songchun Fan3c82a302019-11-29 14:23:45 -08001061 while (auto e = ::readdir(d.get())) {
1062 if (e->d_type == DT_REG) {
1063 auto name = std::string_view(e->d_name);
1064 if (name.starts_with(constants().mountpointMdPrefix)) {
1065 bindPoints.emplace_back(name,
1066 parseFromIncfs<metadata::BindPoint>(mIncFs.get(),
1067 ifs->control,
1068 path::join(mountTarget,
1069 name)));
1070 if (bindPoints.back().second.dest_path().empty() ||
1071 bindPoints.back().second.source_subdir().empty()) {
1072 bindPoints.pop_back();
Yurii Zubrytskyi4a25dfb2020-01-10 11:53:24 -08001073 mIncFs->unlink(ifs->control, path::join(ifs->root, constants().mount, name));
Songchun Fan3c82a302019-11-29 14:23:45 -08001074 }
1075 }
1076 } else if (e->d_type == DT_DIR) {
1077 if (e->d_name == "."sv || e->d_name == ".."sv) {
1078 continue;
1079 }
1080 auto name = std::string_view(e->d_name);
1081 if (name.starts_with(constants().storagePrefix)) {
Yurii Zubrytskyi107ae352020-04-03 13:12:51 -07001082 int storageId;
1083 const auto res = std::from_chars(name.data() + constants().storagePrefix.size() + 1,
1084 name.data() + name.size(), storageId);
1085 if (res.ec != std::errc{} || *res.ptr != '_') {
1086 LOG(WARNING) << "Ignoring storage with invalid name '" << name << "' for mount "
1087 << root;
1088 continue;
1089 }
1090 auto [_, inserted] = mMounts.try_emplace(storageId, ifs);
Songchun Fan3c82a302019-11-29 14:23:45 -08001091 if (!inserted) {
Yurii Zubrytskyi107ae352020-04-03 13:12:51 -07001092 LOG(WARNING) << "Ignoring storage with duplicate id " << storageId
Songchun Fan3c82a302019-11-29 14:23:45 -08001093 << " for mount " << root;
1094 continue;
1095 }
Yurii Zubrytskyi107ae352020-04-03 13:12:51 -07001096 ifs->storages.insert_or_assign(storageId,
1097 IncFsMount::Storage{
1098 path::join(root, constants().mount, name)});
1099 mNextId = std::max(mNextId, storageId + 1);
Songchun Fan3c82a302019-11-29 14:23:45 -08001100 }
1101 }
1102 }
1103
1104 if (ifs->storages.empty()) {
1105 LOG(WARNING) << "No valid storages in mount " << root;
1106 return false;
1107 }
1108
1109 int bindCount = 0;
1110 for (auto&& bp : bindPoints) {
1111 std::unique_lock l(mLock, std::defer_lock);
1112 bindCount += !addBindMountWithMd(*ifs, bp.second.storage_id(), std::move(bp.first),
1113 std::move(*bp.second.mutable_source_subdir()),
1114 std::move(*bp.second.mutable_dest_path()),
1115 BindKind::Permanent, l);
1116 }
1117
1118 if (bindCount == 0) {
1119 LOG(WARNING) << "No valid bind points for mount " << root;
1120 deleteStorage(*ifs);
1121 return false;
1122 }
1123
Songchun Fan3c82a302019-11-29 14:23:45 -08001124 mMounts[ifs->mountId] = std::move(ifs);
1125 return true;
1126}
1127
1128bool IncrementalService::prepareDataLoader(IncrementalService::IncFsMount& ifs,
Alex Buynytskyy04f73912020-02-10 08:34:18 -08001129 const DataLoaderStatusListener* externalListener) {
Songchun Fan3c82a302019-11-29 14:23:45 -08001130 if (!mSystemReady.load(std::memory_order_relaxed)) {
1131 std::unique_lock l(ifs.lock);
Songchun Fan3c82a302019-11-29 14:23:45 -08001132 return true; // eventually...
1133 }
Songchun Fan3c82a302019-11-29 14:23:45 -08001134
1135 std::unique_lock l(ifs.lock);
Alex Buynytskyybf1c0632020-03-10 15:49:29 -07001136 if (ifs.dataLoaderStatus != -1) {
Songchun Fan3c82a302019-11-29 14:23:45 -08001137 LOG(INFO) << "Skipped data loader preparation because it already exists";
1138 return true;
1139 }
1140
Songchun Fan3c82a302019-11-29 14:23:45 -08001141 FileSystemControlParcel fsControlParcel;
Jooyung Han66c567a2020-03-07 21:47:09 +09001142 fsControlParcel.incremental = aidl::make_nullable<IncrementalFileSystemControlParcel>();
Songchun Fan20d6ef22020-03-03 09:47:15 -08001143 fsControlParcel.incremental->cmd.reset(base::unique_fd(::dup(ifs.control.cmd())));
Yurii Zubrytskyi4a25dfb2020-01-10 11:53:24 -08001144 fsControlParcel.incremental->pendingReads.reset(
Songchun Fan20d6ef22020-03-03 09:47:15 -08001145 base::unique_fd(::dup(ifs.control.pendingReads())));
1146 fsControlParcel.incremental->log.reset(base::unique_fd(::dup(ifs.control.logs())));
Alex Buynytskyyf4156792020-04-07 14:26:55 -07001147 fsControlParcel.service = new IncrementalServiceConnector(*this, ifs.mountId);
Songchun Fan1124fd32020-02-10 12:49:41 -08001148 sp<IncrementalDataLoaderListener> listener =
Songchun Fan306b7df2020-03-17 12:37:07 -07001149 new IncrementalDataLoaderListener(*this,
1150 externalListener ? *externalListener
1151 : DataLoaderStatusListener());
Songchun Fan3c82a302019-11-29 14:23:45 -08001152 bool created = false;
Alex Buynytskyy96e350b2020-04-02 20:03:47 -07001153 auto status = mDataLoaderManager->initializeDataLoader(ifs.mountId, ifs.dataLoaderParams, fsControlParcel, listener, &created);
Songchun Fan3c82a302019-11-29 14:23:45 -08001154 if (!status.isOk() || !created) {
1155 LOG(ERROR) << "Failed to create a data loader for mount " << ifs.mountId;
1156 return false;
1157 }
Songchun Fan3c82a302019-11-29 14:23:45 -08001158 return true;
1159}
1160
Songchun Fan0f8b6fe2020-02-05 17:41:25 -08001161// Extract lib filse from zip, create new files in incfs and write data to them
1162bool IncrementalService::configureNativeBinaries(StorageId storage, std::string_view apkFullPath,
1163 std::string_view libDirRelativePath,
1164 std::string_view abi) {
1165 const auto ifs = getIfs(storage);
1166 // First prepare target directories if they don't exist yet
1167 if (auto res = makeDirs(storage, libDirRelativePath, 0755)) {
1168 LOG(ERROR) << "Failed to prepare target lib directory " << libDirRelativePath
1169 << " errno: " << res;
1170 return false;
1171 }
1172
1173 std::unique_ptr<ZipFileRO> zipFile(ZipFileRO::open(apkFullPath.data()));
1174 if (!zipFile) {
1175 LOG(ERROR) << "Failed to open zip file at " << apkFullPath;
1176 return false;
1177 }
1178 void* cookie = nullptr;
1179 const auto libFilePrefix = path::join(constants().libDir, abi);
1180 if (!zipFile.get()->startIteration(&cookie, libFilePrefix.c_str() /* prefix */,
1181 constants().libSuffix.data() /* suffix */)) {
1182 LOG(ERROR) << "Failed to start zip iteration for " << apkFullPath;
1183 return false;
1184 }
1185 ZipEntryRO entry = nullptr;
1186 bool success = true;
1187 while ((entry = zipFile.get()->nextEntry(cookie)) != nullptr) {
1188 char fileName[PATH_MAX];
1189 if (zipFile.get()->getEntryFileName(entry, fileName, sizeof(fileName))) {
1190 continue;
1191 }
1192 const auto libName = path::basename(fileName);
1193 const auto targetLibPath = path::join(libDirRelativePath, libName);
1194 const auto targetLibPathAbsolute = normalizePathToStorage(ifs, storage, targetLibPath);
1195 // If the extract file already exists, skip
1196 struct stat st;
1197 if (stat(targetLibPathAbsolute.c_str(), &st) == 0) {
1198 LOG(INFO) << "Native lib file already exists: " << targetLibPath
1199 << "; skipping extraction";
1200 continue;
1201 }
1202
1203 uint32_t uncompressedLen;
1204 if (!zipFile.get()->getEntryInfo(entry, nullptr, &uncompressedLen, nullptr, nullptr,
1205 nullptr, nullptr)) {
1206 LOG(ERROR) << "Failed to read native lib entry: " << fileName;
1207 success = false;
1208 break;
1209 }
1210
1211 // Create new lib file without signature info
George Burgess IVdd5275d2020-02-10 11:18:07 -08001212 incfs::NewFileParams libFileParams{};
Songchun Fan0f8b6fe2020-02-05 17:41:25 -08001213 libFileParams.size = uncompressedLen;
Alex Buynytskyyf5e605a2020-03-13 13:31:12 -07001214 libFileParams.signature = {};
Songchun Fan0f8b6fe2020-02-05 17:41:25 -08001215 // Metadata of the new lib file is its relative path
1216 IncFsSpan libFileMetadata;
1217 libFileMetadata.data = targetLibPath.c_str();
1218 libFileMetadata.size = targetLibPath.size();
1219 libFileParams.metadata = libFileMetadata;
1220 incfs::FileId libFileId = idFromMetadata(targetLibPath);
1221 if (auto res = makeFile(storage, targetLibPath, 0777, libFileId, libFileParams)) {
1222 LOG(ERROR) << "Failed to make file for: " << targetLibPath << " errno: " << res;
1223 success = false;
1224 // If one lib file fails to be created, abort others as well
1225 break;
1226 }
Songchun Fanafaf6e92020-03-18 14:12:20 -07001227 // If it is a zero-byte file, skip data writing
1228 if (uncompressedLen == 0) {
1229 continue;
1230 }
Songchun Fan0f8b6fe2020-02-05 17:41:25 -08001231
1232 // Write extracted data to new file
1233 std::vector<uint8_t> libData(uncompressedLen);
1234 if (!zipFile.get()->uncompressEntry(entry, &libData[0], uncompressedLen)) {
1235 LOG(ERROR) << "Failed to extract native lib zip entry: " << fileName;
1236 success = false;
1237 break;
1238 }
Yurii Zubrytskyie82cdd72020-04-01 12:19:26 -07001239 const auto writeFd = mIncFs->openForSpecialOps(ifs->control, libFileId);
1240 if (!writeFd.ok()) {
Songchun Fan0f8b6fe2020-02-05 17:41:25 -08001241 LOG(ERROR) << "Failed to open write fd for: " << targetLibPath << " errno: " << writeFd;
1242 success = false;
1243 break;
1244 }
1245 const int numBlocks = uncompressedLen / constants().blockSize + 1;
1246 std::vector<IncFsDataBlock> instructions;
1247 auto remainingData = std::span(libData);
1248 for (int i = 0; i < numBlocks - 1; i++) {
1249 auto inst = IncFsDataBlock{
1250 .fileFd = writeFd,
1251 .pageIndex = static_cast<IncFsBlockIndex>(i),
1252 .compression = INCFS_COMPRESSION_KIND_NONE,
1253 .kind = INCFS_BLOCK_KIND_DATA,
1254 .dataSize = static_cast<uint16_t>(constants().blockSize),
1255 .data = reinterpret_cast<const char*>(remainingData.data()),
1256 };
1257 instructions.push_back(inst);
1258 remainingData = remainingData.subspan(constants().blockSize);
1259 }
1260 // Last block
1261 auto inst = IncFsDataBlock{
1262 .fileFd = writeFd,
1263 .pageIndex = static_cast<IncFsBlockIndex>(numBlocks - 1),
1264 .compression = INCFS_COMPRESSION_KIND_NONE,
1265 .kind = INCFS_BLOCK_KIND_DATA,
1266 .dataSize = static_cast<uint16_t>(remainingData.size()),
1267 .data = reinterpret_cast<const char*>(remainingData.data()),
1268 };
1269 instructions.push_back(inst);
1270 size_t res = mIncFs->writeBlocks(instructions);
1271 if (res != instructions.size()) {
1272 LOG(ERROR) << "Failed to write data into: " << targetLibPath;
1273 success = false;
1274 }
1275 instructions.clear();
1276 }
1277 zipFile.get()->endIteration(cookie);
1278 return success;
1279}
1280
Alex Buynytskyy96e350b2020-04-02 20:03:47 -07001281void IncrementalService::registerAppOpsCallback(const std::string& packageName) {
Alex Buynytskyy1d892162020-04-03 23:00:19 -07001282 sp<IAppOpsCallback> listener;
1283 {
1284 std::unique_lock lock{mCallbacksLock};
1285 auto& cb = mCallbackRegistered[packageName];
1286 if (cb) {
1287 return;
1288 }
1289 cb = new AppOpsListener(*this, packageName);
1290 listener = cb;
1291 }
1292
1293 mAppOpsManager->startWatchingMode(AppOpsManager::OP_GET_USAGE_STATS, String16(packageName.c_str()), listener);
1294}
1295
1296bool IncrementalService::unregisterAppOpsCallback(const std::string& packageName) {
1297 sp<IAppOpsCallback> listener;
1298 {
1299 std::unique_lock lock{mCallbacksLock};
1300 auto found = mCallbackRegistered.find(packageName);
1301 if (found == mCallbackRegistered.end()) {
1302 return false;
1303 }
1304 listener = found->second;
1305 mCallbackRegistered.erase(found);
1306 }
1307
1308 mAppOpsManager->stopWatchingMode(listener);
1309 return true;
1310}
1311
1312void IncrementalService::onAppOpChanged(const std::string& packageName) {
1313 if (!unregisterAppOpsCallback(packageName)) {
Alex Buynytskyy96e350b2020-04-02 20:03:47 -07001314 return;
1315 }
1316
Alex Buynytskyy96e350b2020-04-02 20:03:47 -07001317 std::vector<IfsMountPtr> affected;
1318 {
1319 std::lock_guard l(mLock);
1320 affected.reserve(mMounts.size());
1321 for (auto&& [id, ifs] : mMounts) {
Alex Buynytskyy1d892162020-04-03 23:00:19 -07001322 if (ifs->mountId == id && ifs->dataLoaderParams.packageName == packageName) {
Alex Buynytskyy96e350b2020-04-02 20:03:47 -07001323 affected.push_back(ifs);
1324 }
1325 }
1326 }
Alex Buynytskyy96e350b2020-04-02 20:03:47 -07001327 for (auto&& ifs : affected) {
Alex Buynytskyy1d892162020-04-03 23:00:19 -07001328 applyStorageParams(*ifs, false);
Alex Buynytskyy96e350b2020-04-02 20:03:47 -07001329 }
Alex Buynytskyy96e350b2020-04-02 20:03:47 -07001330}
1331
Songchun Fan3c82a302019-11-29 14:23:45 -08001332binder::Status IncrementalService::IncrementalDataLoaderListener::onStatusChanged(MountId mountId,
1333 int newStatus) {
Alex Buynytskyy04f73912020-02-10 08:34:18 -08001334 if (externalListener) {
1335 // Give an external listener a chance to act before we destroy something.
1336 externalListener->onStatusChanged(mountId, newStatus);
1337 }
1338
Alex Buynytskyybf1c0632020-03-10 15:49:29 -07001339 bool startRequested = false;
1340 {
1341 std::unique_lock l(incrementalService.mLock);
1342 const auto& ifs = incrementalService.getIfsLocked(mountId);
1343 if (!ifs) {
Songchun Fan306b7df2020-03-17 12:37:07 -07001344 LOG(WARNING) << "Received data loader status " << int(newStatus)
1345 << " for unknown mount " << mountId;
Alex Buynytskyybf1c0632020-03-10 15:49:29 -07001346 return binder::Status::ok();
1347 }
1348 ifs->dataLoaderStatus = newStatus;
1349
1350 if (newStatus == IDataLoaderStatusListener::DATA_LOADER_DESTROYED) {
1351 ifs->dataLoaderStatus = IDataLoaderStatusListener::DATA_LOADER_STOPPED;
1352 incrementalService.deleteStorageLocked(*ifs, std::move(l));
1353 return binder::Status::ok();
1354 }
1355
1356 startRequested = ifs->dataLoaderStartRequested;
Songchun Fan3c82a302019-11-29 14:23:45 -08001357 }
Alex Buynytskyybf1c0632020-03-10 15:49:29 -07001358
Songchun Fan3c82a302019-11-29 14:23:45 -08001359 switch (newStatus) {
Alex Buynytskyy1ecfcec2019-12-17 12:10:41 -08001360 case IDataLoaderStatusListener::DATA_LOADER_CREATED: {
Alex Buynytskyybf1c0632020-03-10 15:49:29 -07001361 if (startRequested) {
1362 incrementalService.startDataLoader(mountId);
1363 }
Songchun Fan3c82a302019-11-29 14:23:45 -08001364 break;
1365 }
Alex Buynytskyy1ecfcec2019-12-17 12:10:41 -08001366 case IDataLoaderStatusListener::DATA_LOADER_DESTROYED: {
Songchun Fan3c82a302019-11-29 14:23:45 -08001367 break;
1368 }
Alex Buynytskyy1ecfcec2019-12-17 12:10:41 -08001369 case IDataLoaderStatusListener::DATA_LOADER_STARTED: {
Songchun Fan3c82a302019-11-29 14:23:45 -08001370 break;
1371 }
1372 case IDataLoaderStatusListener::DATA_LOADER_STOPPED: {
1373 break;
1374 }
Alex Buynytskyy04f73912020-02-10 08:34:18 -08001375 case IDataLoaderStatusListener::DATA_LOADER_IMAGE_READY: {
1376 break;
1377 }
1378 case IDataLoaderStatusListener::DATA_LOADER_IMAGE_NOT_READY: {
1379 break;
1380 }
Alex Buynytskyy2cf1d182020-03-17 09:33:45 -07001381 case IDataLoaderStatusListener::DATA_LOADER_UNRECOVERABLE: {
1382 // Nothing for now. Rely on externalListener to handle this.
1383 break;
1384 }
Songchun Fan3c82a302019-11-29 14:23:45 -08001385 default: {
1386 LOG(WARNING) << "Unknown data loader status: " << newStatus
1387 << " for mount: " << mountId;
1388 break;
1389 }
1390 }
1391
1392 return binder::Status::ok();
1393}
1394
Alex Buynytskyy1d892162020-04-03 23:00:19 -07001395void IncrementalService::AppOpsListener::opChanged(int32_t, const String16&) {
1396 incrementalService.onAppOpChanged(packageName);
Alex Buynytskyy96e350b2020-04-02 20:03:47 -07001397}
1398
Alex Buynytskyyf4156792020-04-07 14:26:55 -07001399binder::Status IncrementalService::IncrementalServiceConnector::setStorageParams(
1400 bool enableReadLogs, int32_t* _aidl_return) {
1401 *_aidl_return = incrementalService.setStorageParams(storage, enableReadLogs);
1402 return binder::Status::ok();
1403}
1404
Songchun Fan3c82a302019-11-29 14:23:45 -08001405} // namespace android::incremental