1 /*
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/logging.h>
22 #include <android-base/no_destructor.h>
23 #include <android-base/properties.h>
24 #include <android-base/stringprintf.h>
25 #include <binder/AppOpsManager.h>
26 #include <binder/Nullable.h>
27 #include <binder/Status.h>
28 #include <sys/stat.h>
29 #include <uuid/uuid.h>
30 
31 #include <charconv>
32 #include <ctime>
33 #include <iterator>
34 #include <span>
35 #include <type_traits>
36 
37 #include "IncrementalServiceValidation.h"
38 #include "Metadata.pb.h"
39 
40 using namespace std::literals;
41 namespace fs = std::filesystem;
42 
43 constexpr const char* kDataUsageStats = "android.permission.LOADER_USAGE_STATS";
44 constexpr const char* kOpUsage = "android:loader_usage_stats";
45 
46 namespace android::incremental {
47 
48 using content::pm::DataLoaderParamsParcel;
49 using content::pm::FileSystemControlParcel;
50 using content::pm::IDataLoader;
51 
52 namespace {
53 
54 using IncrementalFileSystemControlParcel = os::incremental::IncrementalFileSystemControlParcel;
55 
56 struct Constants {
57     static constexpr auto backing = "backing_store"sv;
58     static constexpr auto mount = "mount"sv;
59     static constexpr auto mountKeyPrefix = "MT_"sv;
60     static constexpr auto storagePrefix = "st"sv;
61     static constexpr auto mountpointMdPrefix = ".mountpoint."sv;
62     static constexpr auto infoMdName = ".info"sv;
63     static constexpr auto readLogsDisabledMarkerName = ".readlogs_disabled"sv;
64     static constexpr auto libDir = "lib"sv;
65     static constexpr auto libSuffix = ".so"sv;
66     static constexpr auto blockSize = 4096;
67     static constexpr auto systemPackage = "android"sv;
68 };
69 
constants()70 static const Constants& constants() {
71     static constexpr Constants c;
72     return c;
73 }
74 
75 template <base::LogSeverity level = base::ERROR>
mkdirOrLog(std::string_view name,int mode=0770,bool allowExisting=true)76 bool mkdirOrLog(std::string_view name, int mode = 0770, bool allowExisting = true) {
77     auto cstr = path::c_str(name);
78     if (::mkdir(cstr, mode)) {
79         if (!allowExisting || errno != EEXIST) {
80             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     }
89     if (::chmod(cstr, mode)) {
90         PLOG(level) << "Changing permission failed for '" << name << '\'';
91         return false;
92     }
93 
94     return true;
95 }
96 
toMountKey(std::string_view path)97 static 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     }
107     if (path.size() > 16) {
108         path = path.substr(0, 16);
109     }
110     std::string res(path);
111     std::replace_if(
112             res.begin(), res.end(), [](char c) { return c == '/' || c == '@'; }, '_');
113     return std::string(constants().mountKeyPrefix) += res;
114 }
115 
makeMountDir(std::string_view incrementalDir,std::string_view path)116 static 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);
123         if (mkdirOrLog(mountRoot, 0777, false)) {
124             return {mountKey, mountRoot};
125         }
126     }
127     return {};
128 }
129 
130 template <class Map>
findParentPath(const Map & map,std::string_view path)131 typename 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 
dup(base::borrowed_fd fd)143 static 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 
148 template <class ProtoMessage, class Control>
parseFromIncfs(const IncFsWrapper * incfs,const Control & control,std::string_view path)149 static ProtoMessage parseFromIncfs(const IncFsWrapper* incfs, const Control& control,
150                                    std::string_view path) {
151     auto md = incfs->getMetadata(control, path);
152     ProtoMessage message;
153     return message.ParseFromArray(md.data(), md.size()) ? message : ProtoMessage{};
154 }
155 
isValidMountTarget(std::string_view path)156 static bool isValidMountTarget(std::string_view path) {
157     return path::isAbsolute(path) && path::isEmptyDir(path).value_or(true);
158 }
159 
makeBindMdName()160 std::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 }
176 
checkReadLogsDisabledMarker(std::string_view root)177 static 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 
183 } // namespace
184 
~IncFsMount()185 IncrementalService::IncFsMount::~IncFsMount() {
186     if (dataLoaderStub) {
187         dataLoaderStub->cleanupResources();
188         dataLoaderStub = {};
189     }
190     control.close();
191     LOG(INFO) << "Unmounting and cleaning up mount " << mountId << " with root '" << root << '\'';
192     for (auto&& [target, _] : bindPoints) {
193         LOG(INFO) << "  bind: " << target;
194         incrementalService.mVold->unmountIncFs(target);
195     }
196     LOG(INFO) << "  root: " << root;
197     incrementalService.mVold->unmountIncFs(path::join(root, constants().mount));
198     cleanupFilesystem(root);
199 }
200 
makeStorage(StorageId id)201 auto IncrementalService::IncFsMount::makeStorage(StorageId id) -> StorageMap::iterator {
202     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();
206         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);
209         if (auto err = incrementalService.mIncFs->makeDir(control, fullName, 0755); !err) {
210             std::lock_guard l(lock);
211             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;
215         }
216     }
217     nextStorageDirNo = 0;
218     return storages.end();
219 }
220 
221 template <class Func>
makeCleanup(Func && f)222 static auto makeCleanup(Func&& f) {
223     auto deleter = [f = std::move(f)](auto) { f(); };
224     // &f is a dangling pointer here, but we actually never use it as deleter moves it in.
225     return std::unique_ptr<Func, decltype(deleter)>(&f, std::move(deleter));
226 }
227 
openDir(const char * dir)228 static std::unique_ptr<DIR, decltype(&::closedir)> openDir(const char* dir) {
229     return {::opendir(dir), ::closedir};
230 }
231 
openDir(std::string_view dir)232 static auto openDir(std::string_view dir) {
233     return openDir(path::c_str(dir));
234 }
235 
rmDirContent(const char * path)236 static 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         }
245         auto fullPath = base::StringPrintf("%s/%s", path, entry->d_name);
246         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 
cleanupFilesystem(std::string_view root)265 void IncrementalService::IncFsMount::cleanupFilesystem(std::string_view root) {
266     rmDirContent(path::join(root, constants().backing).c_str());
267     ::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 
IncrementalService(ServiceManagerWrapper && sm,std::string_view rootDir)272 IncrementalService::IncrementalService(ServiceManagerWrapper&& sm, std::string_view rootDir)
273       : mVold(sm.getVoldService()),
274         mDataLoaderManager(sm.getDataLoaderManager()),
275         mIncFs(sm.getIncFs()),
276         mAppOpsManager(sm.getAppOpsManager()),
277         mJni(sm.getJni()),
278         mLooper(sm.getLooper()),
279         mTimedQueue(sm.getTimedQueue()),
280         mIncrementalDir(rootDir) {
281     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";
287 
288     mJobQueue.reserve(16);
289     mJobProcessor = std::thread([this]() {
290         mJni->initializeForCurrentThread();
291         runJobProcessing();
292     });
293     mCmdLooperThread = std::thread([this]() {
294         mJni->initializeForCurrentThread();
295         runCmdLooper();
296     });
297 
298     const auto mountedRootNames = adoptMountedInstances();
299     mountExistingImages(mountedRootNames);
300 }
301 
~IncrementalService()302 IncrementalService::~IncrementalService() {
303     {
304         std::lock_guard lock(mJobMutex);
305         mRunning = false;
306     }
307     mJobCondition.notify_all();
308     mJobProcessor.join();
309     mCmdLooperThread.join();
310     mTimedQueue->stop();
311     // Ensure that mounts are destroyed while the service is still valid.
312     mBindsByPath.clear();
313     mMounts.clear();
314 }
315 
toString(IncrementalService::BindKind kind)316 static const char* toString(IncrementalService::BindKind kind) {
317     switch (kind) {
318         case IncrementalService::BindKind::Temporary:
319             return "Temporary";
320         case IncrementalService::BindKind::Permanent:
321             return "Permanent";
322     }
323 }
324 
onDump(int fd)325 void IncrementalService::onDump(int fd) {
326     dprintf(fd, "Incremental is %s\n", incfs::enabled() ? "ENABLED" : "DISABLED");
327     dprintf(fd, "Incremental dir: %s\n", mIncrementalDir.c_str());
328 
329     std::unique_lock l(mLock);
330 
331     dprintf(fd, "Mounts (%d): {\n", int(mMounts.size()));
332     for (auto&& [id, ifs] : mMounts) {
333         const IncFsMount& mnt = *ifs;
334         dprintf(fd, "  [%d]: {\n", id);
335         if (id != mnt.mountId) {
336             dprintf(fd, "    reference to mountId: %d\n", mnt.mountId);
337         } else {
338             dprintf(fd, "    mountId: %d\n", mnt.mountId);
339             dprintf(fd, "    root: %s\n", mnt.root.c_str());
340             dprintf(fd, "    nextStorageDirNo: %d\n", mnt.nextStorageDirNo.load());
341             if (mnt.dataLoaderStub) {
342                 mnt.dataLoaderStub->onDump(fd);
343             } else {
344                 dprintf(fd, "    dataLoader: null\n");
345             }
346             dprintf(fd, "    storages (%d): {\n", int(mnt.storages.size()));
347             for (auto&& [storageId, storage] : mnt.storages) {
348                 dprintf(fd, "      [%d] -> [%s]\n", storageId, storage.name.c_str());
349             }
350             dprintf(fd, "    }\n");
351 
352             dprintf(fd, "    bindPoints (%d): {\n", int(mnt.bindPoints.size()));
353             for (auto&& [target, bind] : mnt.bindPoints) {
354                 dprintf(fd, "      [%s]->[%d]:\n", target.c_str(), bind.storage);
355                 dprintf(fd, "        savedFilename: %s\n", bind.savedFilename.c_str());
356                 dprintf(fd, "        sourceDir: %s\n", bind.sourceDir.c_str());
357                 dprintf(fd, "        kind: %s\n", toString(bind.kind));
358             }
359             dprintf(fd, "    }\n");
360         }
361         dprintf(fd, "  }\n");
362     }
363     dprintf(fd, "}\n");
364     dprintf(fd, "Sorted binds (%d): {\n", int(mBindsByPath.size()));
365     for (auto&& [target, mountPairIt] : mBindsByPath) {
366         const auto& bind = mountPairIt->second;
367         dprintf(fd, "    [%s]->[%d]:\n", target.c_str(), bind.storage);
368         dprintf(fd, "      savedFilename: %s\n", bind.savedFilename.c_str());
369         dprintf(fd, "      sourceDir: %s\n", bind.sourceDir.c_str());
370         dprintf(fd, "      kind: %s\n", toString(bind.kind));
371     }
372     dprintf(fd, "}\n");
373 }
374 
onSystemReady()375 void IncrementalService::onSystemReady() {
376     if (mSystemReady.exchange(true)) {
377         return;
378     }
379 
380     std::vector<IfsMountPtr> mounts;
381     {
382         std::lock_guard l(mLock);
383         mounts.reserve(mMounts.size());
384         for (auto&& [id, ifs] : mMounts) {
385             if (ifs->mountId == id &&
386                 ifs->dataLoaderStub->params().packageName == Constants::systemPackage) {
387                 mounts.push_back(ifs);
388             }
389         }
390     }
391 
392     if (mounts.empty()) {
393         return;
394     }
395 
396     std::thread([this, mounts = std::move(mounts)]() {
397         mJni->initializeForCurrentThread();
398         for (auto&& ifs : mounts) {
399             ifs->dataLoaderStub->requestStart();
400         }
401     }).detach();
402 }
403 
getStorageSlotLocked()404 auto IncrementalService::getStorageSlotLocked() -> MountMap::iterator {
405     for (;;) {
406         if (mNextId == kMaxStorageId) {
407             mNextId = 0;
408         }
409         auto id = ++mNextId;
410         auto [it, inserted] = mMounts.try_emplace(id, nullptr);
411         if (inserted) {
412             return it;
413         }
414     }
415 }
416 
createStorage(std::string_view mountPoint,content::pm::DataLoaderParamsParcel && dataLoaderParams,CreateOptions options,const DataLoaderStatusListener & statusListener,StorageHealthCheckParams && healthCheckParams,const StorageHealthListener & healthListener)417 StorageId IncrementalService::createStorage(std::string_view mountPoint,
418                                             content::pm::DataLoaderParamsParcel&& dataLoaderParams,
419                                             CreateOptions options,
420                                             const DataLoaderStatusListener& statusListener,
421                                             StorageHealthCheckParams&& healthCheckParams,
422                                             const StorageHealthListener& healthListener) {
423     LOG(INFO) << "createStorage: " << mountPoint << " | " << int(options);
424     if (!path::isAbsolute(mountPoint)) {
425         LOG(ERROR) << "path is not absolute: " << mountPoint;
426         return kInvalidStorageId;
427     }
428 
429     auto mountNorm = path::normalize(mountPoint);
430     {
431         const auto id = findStorageId(mountNorm);
432         if (id != kInvalidStorageId) {
433             if (options & CreateOptions::OpenExisting) {
434                 LOG(INFO) << "Opened existing storage " << id;
435                 return id;
436             }
437             LOG(ERROR) << "Directory " << mountPoint << " is already mounted at storage " << id;
438             return kInvalidStorageId;
439         }
440     }
441 
442     if (!(options & CreateOptions::CreateNew)) {
443         LOG(ERROR) << "not requirested create new storage, and it doesn't exist: " << mountPoint;
444         return kInvalidStorageId;
445     }
446 
447     if (!path::isEmptyDir(mountNorm)) {
448         LOG(ERROR) << "Mounting over existing non-empty directory is not supported: " << mountNorm;
449         return kInvalidStorageId;
450     }
451     auto [mountKey, mountRoot] = makeMountDir(mIncrementalDir, mountNorm);
452     if (mountRoot.empty()) {
453         LOG(ERROR) << "Bad mount point";
454         return kInvalidStorageId;
455     }
456     // Make sure the code removes all crap it may create while still failing.
457     auto firstCleanup = [](const std::string* ptr) { IncFsMount::cleanupFilesystem(*ptr); };
458     auto firstCleanupOnFailure =
459             std::unique_ptr<std::string, decltype(firstCleanup)>(&mountRoot, firstCleanup);
460 
461     auto mountTarget = path::join(mountRoot, constants().mount);
462     const auto backing = path::join(mountRoot, constants().backing);
463     if (!mkdirOrLog(backing, 0777) || !mkdirOrLog(mountTarget)) {
464         return kInvalidStorageId;
465     }
466 
467     IncFsMount::Control control;
468     {
469         std::lock_guard l(mMountOperationLock);
470         IncrementalFileSystemControlParcel controlParcel;
471 
472         if (auto err = rmDirContent(backing.c_str())) {
473             LOG(ERROR) << "Coudn't clean the backing directory " << backing << ": " << err;
474             return kInvalidStorageId;
475         }
476         if (!mkdirOrLog(path::join(backing, ".index"), 0777)) {
477             return kInvalidStorageId;
478         }
479         auto status = mVold->mountIncFs(backing, mountTarget, 0, &controlParcel);
480         if (!status.isOk()) {
481             LOG(ERROR) << "Vold::mountIncFs() failed: " << status.toString8();
482             return kInvalidStorageId;
483         }
484         if (controlParcel.cmd.get() < 0 || controlParcel.pendingReads.get() < 0 ||
485             controlParcel.log.get() < 0) {
486             LOG(ERROR) << "Vold::mountIncFs() returned invalid control parcel.";
487             return kInvalidStorageId;
488         }
489         int cmd = controlParcel.cmd.release().release();
490         int pendingReads = controlParcel.pendingReads.release().release();
491         int logs = controlParcel.log.release().release();
492         control = mIncFs->createControl(cmd, pendingReads, logs);
493     }
494 
495     std::unique_lock l(mLock);
496     const auto mountIt = getStorageSlotLocked();
497     const auto mountId = mountIt->first;
498     l.unlock();
499 
500     auto ifs =
501             std::make_shared<IncFsMount>(std::move(mountRoot), mountId, std::move(control), *this);
502     // Now it's the |ifs|'s responsibility to clean up after itself, and the only cleanup we need
503     // is the removal of the |ifs|.
504     firstCleanupOnFailure.release();
505 
506     auto secondCleanup = [this, &l](auto itPtr) {
507         if (!l.owns_lock()) {
508             l.lock();
509         }
510         mMounts.erase(*itPtr);
511     };
512     auto secondCleanupOnFailure =
513             std::unique_ptr<decltype(mountIt), decltype(secondCleanup)>(&mountIt, secondCleanup);
514 
515     const auto storageIt = ifs->makeStorage(ifs->mountId);
516     if (storageIt == ifs->storages.end()) {
517         LOG(ERROR) << "Can't create a default storage directory";
518         return kInvalidStorageId;
519     }
520 
521     {
522         metadata::Mount m;
523         m.mutable_storage()->set_id(ifs->mountId);
524         m.mutable_loader()->set_type((int)dataLoaderParams.type);
525         m.mutable_loader()->set_allocated_package_name(&dataLoaderParams.packageName);
526         m.mutable_loader()->set_allocated_class_name(&dataLoaderParams.className);
527         m.mutable_loader()->set_allocated_arguments(&dataLoaderParams.arguments);
528         const auto metadata = m.SerializeAsString();
529         m.mutable_loader()->release_arguments();
530         m.mutable_loader()->release_class_name();
531         m.mutable_loader()->release_package_name();
532         if (auto err =
533                     mIncFs->makeFile(ifs->control,
534                                      path::join(ifs->root, constants().mount,
535                                                 constants().infoMdName),
536                                      0777, idFromMetadata(metadata),
537                                      {.metadata = {metadata.data(), (IncFsSize)metadata.size()}})) {
538             LOG(ERROR) << "Saving mount metadata failed: " << -err;
539             return kInvalidStorageId;
540         }
541     }
542 
543     const auto bk =
544             (options & CreateOptions::PermanentBind) ? BindKind::Permanent : BindKind::Temporary;
545     if (auto err = addBindMount(*ifs, storageIt->first, storageIt->second.name,
546                                 std::string(storageIt->second.name), std::move(mountNorm), bk, l);
547         err < 0) {
548         LOG(ERROR) << "adding bind mount failed: " << -err;
549         return kInvalidStorageId;
550     }
551 
552     // Done here as well, all data structures are in good state.
553     secondCleanupOnFailure.release();
554 
555     auto dataLoaderStub = prepareDataLoader(*ifs, std::move(dataLoaderParams), &statusListener,
556                                             std::move(healthCheckParams), &healthListener);
557     CHECK(dataLoaderStub);
558 
559     mountIt->second = std::move(ifs);
560     l.unlock();
561 
562     if (mSystemReady.load(std::memory_order_relaxed) && !dataLoaderStub->requestCreate()) {
563         // failed to create data loader
564         LOG(ERROR) << "initializeDataLoader() failed";
565         deleteStorage(dataLoaderStub->id());
566         return kInvalidStorageId;
567     }
568 
569     LOG(INFO) << "created storage " << mountId;
570     return mountId;
571 }
572 
createLinkedStorage(std::string_view mountPoint,StorageId linkedStorage,IncrementalService::CreateOptions options)573 StorageId IncrementalService::createLinkedStorage(std::string_view mountPoint,
574                                                   StorageId linkedStorage,
575                                                   IncrementalService::CreateOptions options) {
576     if (!isValidMountTarget(mountPoint)) {
577         LOG(ERROR) << "Mount point is invalid or missing";
578         return kInvalidStorageId;
579     }
580 
581     std::unique_lock l(mLock);
582     auto ifs = getIfsLocked(linkedStorage);
583     if (!ifs) {
584         LOG(ERROR) << "Ifs unavailable";
585         return kInvalidStorageId;
586     }
587 
588     const auto mountIt = getStorageSlotLocked();
589     const auto storageId = mountIt->first;
590     const auto storageIt = ifs->makeStorage(storageId);
591     if (storageIt == ifs->storages.end()) {
592         LOG(ERROR) << "Can't create a new storage";
593         mMounts.erase(mountIt);
594         return kInvalidStorageId;
595     }
596 
597     l.unlock();
598 
599     const auto bk =
600             (options & CreateOptions::PermanentBind) ? BindKind::Permanent : BindKind::Temporary;
601     if (auto err = addBindMount(*ifs, storageIt->first, storageIt->second.name,
602                                 std::string(storageIt->second.name), path::normalize(mountPoint),
603                                 bk, l);
604         err < 0) {
605         LOG(ERROR) << "bindMount failed with error: " << err;
606         (void)mIncFs->unlink(ifs->control, storageIt->second.name);
607         ifs->storages.erase(storageIt);
608         return kInvalidStorageId;
609     }
610 
611     mountIt->second = ifs;
612     return storageId;
613 }
614 
findStorageLocked(std::string_view path) const615 IncrementalService::BindPathMap::const_iterator IncrementalService::findStorageLocked(
616         std::string_view path) const {
617     return findParentPath(mBindsByPath, path);
618 }
619 
findStorageId(std::string_view path) const620 StorageId IncrementalService::findStorageId(std::string_view path) const {
621     std::lock_guard l(mLock);
622     auto it = findStorageLocked(path);
623     if (it == mBindsByPath.end()) {
624         return kInvalidStorageId;
625     }
626     return it->second->second.storage;
627 }
628 
disableReadLogs(StorageId storageId)629 void IncrementalService::disableReadLogs(StorageId storageId) {
630     std::unique_lock l(mLock);
631     const auto ifs = getIfsLocked(storageId);
632     if (!ifs) {
633         LOG(ERROR) << "disableReadLogs failed, invalid storageId: " << storageId;
634         return;
635     }
636     if (!ifs->readLogsEnabled()) {
637         return;
638     }
639     ifs->disableReadLogs();
640     l.unlock();
641 
642     const auto metadata = constants().readLogsDisabledMarkerName;
643     if (auto err = mIncFs->makeFile(ifs->control,
644                                     path::join(ifs->root, constants().mount,
645                                                constants().readLogsDisabledMarkerName),
646                                     0777, idFromMetadata(metadata), {})) {
647         //{.metadata = {metadata.data(), (IncFsSize)metadata.size()}})) {
648         LOG(ERROR) << "Failed to make marker file for storageId: " << storageId;
649         return;
650     }
651 
652     setStorageParams(storageId, /*enableReadLogs=*/false);
653 }
654 
setStorageParams(StorageId storageId,bool enableReadLogs)655 int IncrementalService::setStorageParams(StorageId storageId, bool enableReadLogs) {
656     const auto ifs = getIfs(storageId);
657     if (!ifs) {
658         LOG(ERROR) << "setStorageParams failed, invalid storageId: " << storageId;
659         return -EINVAL;
660     }
661 
662     const auto& params = ifs->dataLoaderStub->params();
663     if (enableReadLogs) {
664         if (!ifs->readLogsEnabled()) {
665             LOG(ERROR) << "setStorageParams failed, readlogs disabled for storageId: " << storageId;
666             return -EPERM;
667         }
668 
669         if (auto status = mAppOpsManager->checkPermission(kDataUsageStats, kOpUsage,
670                                                           params.packageName.c_str());
671             !status.isOk()) {
672             LOG(ERROR) << "checkPermission failed: " << status.toString8();
673             return fromBinderStatus(status);
674         }
675     }
676 
677     if (auto status = applyStorageParams(*ifs, enableReadLogs); !status.isOk()) {
678         LOG(ERROR) << "applyStorageParams failed: " << status.toString8();
679         return fromBinderStatus(status);
680     }
681 
682     if (enableReadLogs) {
683         registerAppOpsCallback(params.packageName);
684     }
685 
686     return 0;
687 }
688 
applyStorageParams(IncFsMount & ifs,bool enableReadLogs)689 binder::Status IncrementalService::applyStorageParams(IncFsMount& ifs, bool enableReadLogs) {
690     os::incremental::IncrementalFileSystemControlParcel control;
691     control.cmd.reset(dup(ifs.control.cmd()));
692     control.pendingReads.reset(dup(ifs.control.pendingReads()));
693     auto logsFd = ifs.control.logs();
694     if (logsFd >= 0) {
695         control.log.reset(dup(logsFd));
696     }
697 
698     std::lock_guard l(mMountOperationLock);
699     return mVold->setIncFsMountOptions(control, enableReadLogs);
700 }
701 
deleteStorage(StorageId storageId)702 void IncrementalService::deleteStorage(StorageId storageId) {
703     const auto ifs = getIfs(storageId);
704     if (!ifs) {
705         return;
706     }
707     deleteStorage(*ifs);
708 }
709 
deleteStorage(IncrementalService::IncFsMount & ifs)710 void IncrementalService::deleteStorage(IncrementalService::IncFsMount& ifs) {
711     std::unique_lock l(ifs.lock);
712     deleteStorageLocked(ifs, std::move(l));
713 }
714 
deleteStorageLocked(IncrementalService::IncFsMount & ifs,std::unique_lock<std::mutex> && ifsLock)715 void IncrementalService::deleteStorageLocked(IncrementalService::IncFsMount& ifs,
716                                              std::unique_lock<std::mutex>&& ifsLock) {
717     const auto storages = std::move(ifs.storages);
718     // Don't move the bind points out: Ifs's dtor will use them to unmount everything.
719     const auto bindPoints = ifs.bindPoints;
720     ifsLock.unlock();
721 
722     std::lock_guard l(mLock);
723     for (auto&& [id, _] : storages) {
724         if (id != ifs.mountId) {
725             mMounts.erase(id);
726         }
727     }
728     for (auto&& [path, _] : bindPoints) {
729         mBindsByPath.erase(path);
730     }
731     mMounts.erase(ifs.mountId);
732 }
733 
openStorage(std::string_view pathInMount)734 StorageId IncrementalService::openStorage(std::string_view pathInMount) {
735     if (!path::isAbsolute(pathInMount)) {
736         return kInvalidStorageId;
737     }
738 
739     return findStorageId(path::normalize(pathInMount));
740 }
741 
getIfs(StorageId storage) const742 IncrementalService::IfsMountPtr IncrementalService::getIfs(StorageId storage) const {
743     std::lock_guard l(mLock);
744     return getIfsLocked(storage);
745 }
746 
getIfsLocked(StorageId storage) const747 const IncrementalService::IfsMountPtr& IncrementalService::getIfsLocked(StorageId storage) const {
748     auto it = mMounts.find(storage);
749     if (it == mMounts.end()) {
750         static const base::NoDestructor<IfsMountPtr> kEmpty{};
751         return *kEmpty;
752     }
753     return it->second;
754 }
755 
bind(StorageId storage,std::string_view source,std::string_view target,BindKind kind)756 int IncrementalService::bind(StorageId storage, std::string_view source, std::string_view target,
757                              BindKind kind) {
758     if (!isValidMountTarget(target)) {
759         LOG(ERROR) << __func__ << ": not a valid bind target " << target;
760         return -EINVAL;
761     }
762 
763     const auto ifs = getIfs(storage);
764     if (!ifs) {
765         LOG(ERROR) << __func__ << ": no ifs object for storage " << storage;
766         return -EINVAL;
767     }
768 
769     std::unique_lock l(ifs->lock);
770     const auto storageInfo = ifs->storages.find(storage);
771     if (storageInfo == ifs->storages.end()) {
772         LOG(ERROR) << "no storage";
773         return -EINVAL;
774     }
775     std::string normSource = normalizePathToStorageLocked(*ifs, storageInfo, source);
776     if (normSource.empty()) {
777         LOG(ERROR) << "invalid source path";
778         return -EINVAL;
779     }
780     l.unlock();
781     std::unique_lock l2(mLock, std::defer_lock);
782     return addBindMount(*ifs, storage, storageInfo->second.name, std::move(normSource),
783                         path::normalize(target), kind, l2);
784 }
785 
unbind(StorageId storage,std::string_view target)786 int IncrementalService::unbind(StorageId storage, std::string_view target) {
787     if (!path::isAbsolute(target)) {
788         return -EINVAL;
789     }
790 
791     LOG(INFO) << "Removing bind point " << target << " for storage " << storage;
792 
793     // Here we should only look up by the exact target, not by a subdirectory of any existing mount,
794     // otherwise there's a chance to unmount something completely unrelated
795     const auto norm = path::normalize(target);
796     std::unique_lock l(mLock);
797     const auto storageIt = mBindsByPath.find(norm);
798     if (storageIt == mBindsByPath.end() || storageIt->second->second.storage != storage) {
799         return -EINVAL;
800     }
801     const auto bindIt = storageIt->second;
802     const auto storageId = bindIt->second.storage;
803     const auto ifs = getIfsLocked(storageId);
804     if (!ifs) {
805         LOG(ERROR) << "Internal error: storageId " << storageId << " for bound path " << target
806                    << " is missing";
807         return -EFAULT;
808     }
809     mBindsByPath.erase(storageIt);
810     l.unlock();
811 
812     mVold->unmountIncFs(bindIt->first);
813     std::unique_lock l2(ifs->lock);
814     if (ifs->bindPoints.size() <= 1) {
815         ifs->bindPoints.clear();
816         deleteStorageLocked(*ifs, std::move(l2));
817     } else {
818         const std::string savedFile = std::move(bindIt->second.savedFilename);
819         ifs->bindPoints.erase(bindIt);
820         l2.unlock();
821         if (!savedFile.empty()) {
822             mIncFs->unlink(ifs->control, path::join(ifs->root, constants().mount, savedFile));
823         }
824     }
825 
826     return 0;
827 }
828 
normalizePathToStorageLocked(const IncFsMount & incfs,IncFsMount::StorageMap::const_iterator storageIt,std::string_view path) const829 std::string IncrementalService::normalizePathToStorageLocked(
830         const IncFsMount& incfs, IncFsMount::StorageMap::const_iterator storageIt,
831         std::string_view path) const {
832     if (!path::isAbsolute(path)) {
833         return path::normalize(path::join(storageIt->second.name, path));
834     }
835     auto normPath = path::normalize(path);
836     if (path::startsWith(normPath, storageIt->second.name)) {
837         return normPath;
838     }
839     // not that easy: need to find if any of the bind points match
840     const auto bindIt = findParentPath(incfs.bindPoints, normPath);
841     if (bindIt == incfs.bindPoints.end()) {
842         return {};
843     }
844     return path::join(bindIt->second.sourceDir, path::relativize(bindIt->first, normPath));
845 }
846 
normalizePathToStorage(const IncFsMount & ifs,StorageId storage,std::string_view path) const847 std::string IncrementalService::normalizePathToStorage(const IncFsMount& ifs, StorageId storage,
848                                                        std::string_view path) const {
849     std::unique_lock l(ifs.lock);
850     const auto storageInfo = ifs.storages.find(storage);
851     if (storageInfo == ifs.storages.end()) {
852         return {};
853     }
854     return normalizePathToStorageLocked(ifs, storageInfo, path);
855 }
856 
makeFile(StorageId storage,std::string_view path,int mode,FileId id,incfs::NewFileParams params)857 int IncrementalService::makeFile(StorageId storage, std::string_view path, int mode, FileId id,
858                                  incfs::NewFileParams params) {
859     if (auto ifs = getIfs(storage)) {
860         std::string normPath = normalizePathToStorage(*ifs, storage, path);
861         if (normPath.empty()) {
862             LOG(ERROR) << "Internal error: storageId " << storage
863                        << " failed to normalize: " << path;
864             return -EINVAL;
865         }
866         auto err = mIncFs->makeFile(ifs->control, normPath, mode, id, params);
867         if (err) {
868             LOG(ERROR) << "Internal error: storageId " << storage << " failed to makeFile: " << err;
869             return err;
870         }
871         return 0;
872     }
873     return -EINVAL;
874 }
875 
makeDir(StorageId storageId,std::string_view path,int mode)876 int IncrementalService::makeDir(StorageId storageId, std::string_view path, int mode) {
877     if (auto ifs = getIfs(storageId)) {
878         std::string normPath = normalizePathToStorage(*ifs, storageId, path);
879         if (normPath.empty()) {
880             return -EINVAL;
881         }
882         return mIncFs->makeDir(ifs->control, normPath, mode);
883     }
884     return -EINVAL;
885 }
886 
makeDirs(StorageId storageId,std::string_view path,int mode)887 int IncrementalService::makeDirs(StorageId storageId, std::string_view path, int mode) {
888     const auto ifs = getIfs(storageId);
889     if (!ifs) {
890         return -EINVAL;
891     }
892     return makeDirs(*ifs, storageId, path, mode);
893 }
894 
makeDirs(const IncFsMount & ifs,StorageId storageId,std::string_view path,int mode)895 int IncrementalService::makeDirs(const IncFsMount& ifs, StorageId storageId, std::string_view path,
896                                  int mode) {
897     std::string normPath = normalizePathToStorage(ifs, storageId, path);
898     if (normPath.empty()) {
899         return -EINVAL;
900     }
901     return mIncFs->makeDirs(ifs.control, normPath, mode);
902 }
903 
link(StorageId sourceStorageId,std::string_view oldPath,StorageId destStorageId,std::string_view newPath)904 int IncrementalService::link(StorageId sourceStorageId, std::string_view oldPath,
905                              StorageId destStorageId, std::string_view newPath) {
906     std::unique_lock l(mLock);
907     auto ifsSrc = getIfsLocked(sourceStorageId);
908     if (!ifsSrc) {
909         return -EINVAL;
910     }
911     if (sourceStorageId != destStorageId && getIfsLocked(destStorageId) != ifsSrc) {
912         return -EINVAL;
913     }
914     l.unlock();
915     std::string normOldPath = normalizePathToStorage(*ifsSrc, sourceStorageId, oldPath);
916     std::string normNewPath = normalizePathToStorage(*ifsSrc, destStorageId, newPath);
917     if (normOldPath.empty() || normNewPath.empty()) {
918         LOG(ERROR) << "Invalid paths in link(): " << normOldPath << " | " << normNewPath;
919         return -EINVAL;
920     }
921     return mIncFs->link(ifsSrc->control, normOldPath, normNewPath);
922 }
923 
unlink(StorageId storage,std::string_view path)924 int IncrementalService::unlink(StorageId storage, std::string_view path) {
925     if (auto ifs = getIfs(storage)) {
926         std::string normOldPath = normalizePathToStorage(*ifs, storage, path);
927         return mIncFs->unlink(ifs->control, normOldPath);
928     }
929     return -EINVAL;
930 }
931 
addBindMount(IncFsMount & ifs,StorageId storage,std::string_view storageRoot,std::string && source,std::string && target,BindKind kind,std::unique_lock<std::mutex> & mainLock)932 int IncrementalService::addBindMount(IncFsMount& ifs, StorageId storage,
933                                      std::string_view storageRoot, std::string&& source,
934                                      std::string&& target, BindKind kind,
935                                      std::unique_lock<std::mutex>& mainLock) {
936     if (!isValidMountTarget(target)) {
937         LOG(ERROR) << __func__ << ": invalid mount target " << target;
938         return -EINVAL;
939     }
940 
941     std::string mdFileName;
942     std::string metadataFullPath;
943     if (kind != BindKind::Temporary) {
944         metadata::BindPoint bp;
945         bp.set_storage_id(storage);
946         bp.set_allocated_dest_path(&target);
947         bp.set_allocated_source_subdir(&source);
948         const auto metadata = bp.SerializeAsString();
949         bp.release_dest_path();
950         bp.release_source_subdir();
951         mdFileName = makeBindMdName();
952         metadataFullPath = path::join(ifs.root, constants().mount, mdFileName);
953         auto node = mIncFs->makeFile(ifs.control, metadataFullPath, 0444, idFromMetadata(metadata),
954                                      {.metadata = {metadata.data(), (IncFsSize)metadata.size()}});
955         if (node) {
956             LOG(ERROR) << __func__ << ": couldn't create a mount node " << mdFileName;
957             return int(node);
958         }
959     }
960 
961     const auto res = addBindMountWithMd(ifs, storage, std::move(mdFileName), std::move(source),
962                                         std::move(target), kind, mainLock);
963     if (res) {
964         mIncFs->unlink(ifs.control, metadataFullPath);
965     }
966     return res;
967 }
968 
addBindMountWithMd(IncrementalService::IncFsMount & ifs,StorageId storage,std::string && metadataName,std::string && source,std::string && target,BindKind kind,std::unique_lock<std::mutex> & mainLock)969 int IncrementalService::addBindMountWithMd(IncrementalService::IncFsMount& ifs, StorageId storage,
970                                            std::string&& metadataName, std::string&& source,
971                                            std::string&& target, BindKind kind,
972                                            std::unique_lock<std::mutex>& mainLock) {
973     {
974         std::lock_guard l(mMountOperationLock);
975         const auto status = mVold->bindMount(source, target);
976         if (!status.isOk()) {
977             LOG(ERROR) << "Calling Vold::bindMount() failed: " << status.toString8();
978             return status.exceptionCode() == binder::Status::EX_SERVICE_SPECIFIC
979                     ? status.serviceSpecificErrorCode() > 0 ? -status.serviceSpecificErrorCode()
980                                                             : status.serviceSpecificErrorCode() == 0
981                                     ? -EFAULT
982                                     : status.serviceSpecificErrorCode()
983                     : -EIO;
984         }
985     }
986 
987     if (!mainLock.owns_lock()) {
988         mainLock.lock();
989     }
990     std::lock_guard l(ifs.lock);
991     addBindMountRecordLocked(ifs, storage, std::move(metadataName), std::move(source),
992                              std::move(target), kind);
993     return 0;
994 }
995 
addBindMountRecordLocked(IncFsMount & ifs,StorageId storage,std::string && metadataName,std::string && source,std::string && target,BindKind kind)996 void IncrementalService::addBindMountRecordLocked(IncFsMount& ifs, StorageId storage,
997                                                   std::string&& metadataName, std::string&& source,
998                                                   std::string&& target, BindKind kind) {
999     const auto [it, _] =
1000             ifs.bindPoints.insert_or_assign(target,
1001                                             IncFsMount::Bind{storage, std::move(metadataName),
1002                                                              std::move(source), kind});
1003     mBindsByPath[std::move(target)] = it;
1004 }
1005 
getMetadata(StorageId storage,std::string_view path) const1006 RawMetadata IncrementalService::getMetadata(StorageId storage, std::string_view path) const {
1007     const auto ifs = getIfs(storage);
1008     if (!ifs) {
1009         return {};
1010     }
1011     const auto normPath = normalizePathToStorage(*ifs, storage, path);
1012     if (normPath.empty()) {
1013         return {};
1014     }
1015     return mIncFs->getMetadata(ifs->control, normPath);
1016 }
1017 
getMetadata(StorageId storage,FileId node) const1018 RawMetadata IncrementalService::getMetadata(StorageId storage, FileId node) const {
1019     const auto ifs = getIfs(storage);
1020     if (!ifs) {
1021         return {};
1022     }
1023     return mIncFs->getMetadata(ifs->control, node);
1024 }
1025 
startLoading(StorageId storage) const1026 bool IncrementalService::startLoading(StorageId storage) const {
1027     DataLoaderStubPtr dataLoaderStub;
1028     {
1029         std::unique_lock l(mLock);
1030         const auto& ifs = getIfsLocked(storage);
1031         if (!ifs) {
1032             return false;
1033         }
1034         dataLoaderStub = ifs->dataLoaderStub;
1035         if (!dataLoaderStub) {
1036             return false;
1037         }
1038     }
1039     dataLoaderStub->requestStart();
1040     return true;
1041 }
1042 
adoptMountedInstances()1043 std::unordered_set<std::string_view> IncrementalService::adoptMountedInstances() {
1044     std::unordered_set<std::string_view> mountedRootNames;
1045     mIncFs->listExistingMounts([this, &mountedRootNames](auto root, auto backingDir, auto binds) {
1046         LOG(INFO) << "Existing mount: " << backingDir << "->" << root;
1047         for (auto [source, target] : binds) {
1048             LOG(INFO) << "  bind: '" << source << "'->'" << target << "'";
1049             LOG(INFO) << "         " << path::join(root, source);
1050         }
1051 
1052         // Ensure it's a kind of a mount that's managed by IncrementalService
1053         if (path::basename(root) != constants().mount ||
1054             path::basename(backingDir) != constants().backing) {
1055             return;
1056         }
1057         const auto expectedRoot = path::dirname(root);
1058         if (path::dirname(backingDir) != expectedRoot) {
1059             return;
1060         }
1061         if (path::dirname(expectedRoot) != mIncrementalDir) {
1062             return;
1063         }
1064         if (!path::basename(expectedRoot).starts_with(constants().mountKeyPrefix)) {
1065             return;
1066         }
1067 
1068         LOG(INFO) << "Looks like an IncrementalService-owned: " << expectedRoot;
1069 
1070         // make sure we clean up the mount if it happens to be a bad one.
1071         // Note: unmounting needs to run first, so the cleanup object is created _last_.
1072         auto cleanupFiles = makeCleanup([&]() {
1073             LOG(INFO) << "Failed to adopt existing mount, deleting files: " << expectedRoot;
1074             IncFsMount::cleanupFilesystem(expectedRoot);
1075         });
1076         auto cleanupMounts = makeCleanup([&]() {
1077             LOG(INFO) << "Failed to adopt existing mount, cleaning up: " << expectedRoot;
1078             for (auto&& [_, target] : binds) {
1079                 mVold->unmountIncFs(std::string(target));
1080             }
1081             mVold->unmountIncFs(std::string(root));
1082         });
1083 
1084         auto control = mIncFs->openMount(root);
1085         if (!control) {
1086             LOG(INFO) << "failed to open mount " << root;
1087             return;
1088         }
1089 
1090         auto mountRecord =
1091                 parseFromIncfs<metadata::Mount>(mIncFs.get(), control,
1092                                                 path::join(root, constants().infoMdName));
1093         if (!mountRecord.has_loader() || !mountRecord.has_storage()) {
1094             LOG(ERROR) << "Bad mount metadata in mount at " << expectedRoot;
1095             return;
1096         }
1097 
1098         auto mountId = mountRecord.storage().id();
1099         mNextId = std::max(mNextId, mountId + 1);
1100 
1101         DataLoaderParamsParcel dataLoaderParams;
1102         {
1103             const auto& loader = mountRecord.loader();
1104             dataLoaderParams.type = (content::pm::DataLoaderType)loader.type();
1105             dataLoaderParams.packageName = loader.package_name();
1106             dataLoaderParams.className = loader.class_name();
1107             dataLoaderParams.arguments = loader.arguments();
1108         }
1109 
1110         auto ifs = std::make_shared<IncFsMount>(std::string(expectedRoot), mountId,
1111                                                 std::move(control), *this);
1112         cleanupFiles.release(); // ifs will take care of that now
1113 
1114         // Check if marker file present.
1115         if (checkReadLogsDisabledMarker(root)) {
1116             ifs->disableReadLogs();
1117         }
1118 
1119         std::vector<std::pair<std::string, metadata::BindPoint>> permanentBindPoints;
1120         auto d = openDir(root);
1121         while (auto e = ::readdir(d.get())) {
1122             if (e->d_type == DT_REG) {
1123                 auto name = std::string_view(e->d_name);
1124                 if (name.starts_with(constants().mountpointMdPrefix)) {
1125                     permanentBindPoints
1126                             .emplace_back(name,
1127                                           parseFromIncfs<metadata::BindPoint>(mIncFs.get(),
1128                                                                               ifs->control,
1129                                                                               path::join(root,
1130                                                                                          name)));
1131                     if (permanentBindPoints.back().second.dest_path().empty() ||
1132                         permanentBindPoints.back().second.source_subdir().empty()) {
1133                         permanentBindPoints.pop_back();
1134                         mIncFs->unlink(ifs->control, path::join(root, name));
1135                     } else {
1136                         LOG(INFO) << "Permanent bind record: '"
1137                                   << permanentBindPoints.back().second.source_subdir() << "'->'"
1138                                   << permanentBindPoints.back().second.dest_path() << "'";
1139                     }
1140                 }
1141             } else if (e->d_type == DT_DIR) {
1142                 if (e->d_name == "."sv || e->d_name == ".."sv) {
1143                     continue;
1144                 }
1145                 auto name = std::string_view(e->d_name);
1146                 if (name.starts_with(constants().storagePrefix)) {
1147                     int storageId;
1148                     const auto res =
1149                             std::from_chars(name.data() + constants().storagePrefix.size() + 1,
1150                                             name.data() + name.size(), storageId);
1151                     if (res.ec != std::errc{} || *res.ptr != '_') {
1152                         LOG(WARNING) << "Ignoring storage with invalid name '" << name
1153                                      << "' for mount " << expectedRoot;
1154                         continue;
1155                     }
1156                     auto [_, inserted] = mMounts.try_emplace(storageId, ifs);
1157                     if (!inserted) {
1158                         LOG(WARNING) << "Ignoring storage with duplicate id " << storageId
1159                                      << " for mount " << expectedRoot;
1160                         continue;
1161                     }
1162                     ifs->storages.insert_or_assign(storageId,
1163                                                    IncFsMount::Storage{path::join(root, name)});
1164                     mNextId = std::max(mNextId, storageId + 1);
1165                 }
1166             }
1167         }
1168 
1169         if (ifs->storages.empty()) {
1170             LOG(WARNING) << "No valid storages in mount " << root;
1171             return;
1172         }
1173 
1174         // now match the mounted directories with what we expect to have in the metadata
1175         {
1176             std::unique_lock l(mLock, std::defer_lock);
1177             for (auto&& [metadataFile, bindRecord] : permanentBindPoints) {
1178                 auto mountedIt = std::find_if(binds.begin(), binds.end(),
1179                                               [&, bindRecord = bindRecord](auto&& bind) {
1180                                                   return bind.second == bindRecord.dest_path() &&
1181                                                           path::join(root, bind.first) ==
1182                                                           bindRecord.source_subdir();
1183                                               });
1184                 if (mountedIt != binds.end()) {
1185                     LOG(INFO) << "Matched permanent bound " << bindRecord.source_subdir()
1186                               << " to mount " << mountedIt->first;
1187                     addBindMountRecordLocked(*ifs, bindRecord.storage_id(), std::move(metadataFile),
1188                                              std::move(*bindRecord.mutable_source_subdir()),
1189                                              std::move(*bindRecord.mutable_dest_path()),
1190                                              BindKind::Permanent);
1191                     if (mountedIt != binds.end() - 1) {
1192                         std::iter_swap(mountedIt, binds.end() - 1);
1193                     }
1194                     binds = binds.first(binds.size() - 1);
1195                 } else {
1196                     LOG(INFO) << "Didn't match permanent bound " << bindRecord.source_subdir()
1197                               << ", mounting";
1198                     // doesn't exist - try mounting back
1199                     if (addBindMountWithMd(*ifs, bindRecord.storage_id(), std::move(metadataFile),
1200                                            std::move(*bindRecord.mutable_source_subdir()),
1201                                            std::move(*bindRecord.mutable_dest_path()),
1202                                            BindKind::Permanent, l)) {
1203                         mIncFs->unlink(ifs->control, metadataFile);
1204                     }
1205                 }
1206             }
1207         }
1208 
1209         // if anything stays in |binds| those are probably temporary binds; system restarted since
1210         // they were mounted - so let's unmount them all.
1211         for (auto&& [source, target] : binds) {
1212             if (source.empty()) {
1213                 continue;
1214             }
1215             mVold->unmountIncFs(std::string(target));
1216         }
1217         cleanupMounts.release(); // ifs now manages everything
1218 
1219         if (ifs->bindPoints.empty()) {
1220             LOG(WARNING) << "No valid bind points for mount " << expectedRoot;
1221             deleteStorage(*ifs);
1222             return;
1223         }
1224 
1225         prepareDataLoaderLocked(*ifs, std::move(dataLoaderParams));
1226         CHECK(ifs->dataLoaderStub);
1227 
1228         mountedRootNames.insert(path::basename(ifs->root));
1229 
1230         // not locking here at all: we're still in the constructor, no other calls can happen
1231         mMounts[ifs->mountId] = std::move(ifs);
1232     });
1233 
1234     return mountedRootNames;
1235 }
1236 
mountExistingImages(const std::unordered_set<std::string_view> & mountedRootNames)1237 void IncrementalService::mountExistingImages(
1238         const std::unordered_set<std::string_view>& mountedRootNames) {
1239     auto dir = openDir(mIncrementalDir);
1240     if (!dir) {
1241         PLOG(WARNING) << "Couldn't open the root incremental dir " << mIncrementalDir;
1242         return;
1243     }
1244     while (auto entry = ::readdir(dir.get())) {
1245         if (entry->d_type != DT_DIR) {
1246             continue;
1247         }
1248         std::string_view name = entry->d_name;
1249         if (!name.starts_with(constants().mountKeyPrefix)) {
1250             continue;
1251         }
1252         if (mountedRootNames.find(name) != mountedRootNames.end()) {
1253             continue;
1254         }
1255         const auto root = path::join(mIncrementalDir, name);
1256         if (!mountExistingImage(root)) {
1257             IncFsMount::cleanupFilesystem(root);
1258         }
1259     }
1260 }
1261 
mountExistingImage(std::string_view root)1262 bool IncrementalService::mountExistingImage(std::string_view root) {
1263     auto mountTarget = path::join(root, constants().mount);
1264     const auto backing = path::join(root, constants().backing);
1265 
1266     IncrementalFileSystemControlParcel controlParcel;
1267     auto status = mVold->mountIncFs(backing, mountTarget, 0, &controlParcel);
1268     if (!status.isOk()) {
1269         LOG(ERROR) << "Vold::mountIncFs() failed: " << status.toString8();
1270         return false;
1271     }
1272 
1273     int cmd = controlParcel.cmd.release().release();
1274     int pendingReads = controlParcel.pendingReads.release().release();
1275     int logs = controlParcel.log.release().release();
1276     IncFsMount::Control control = mIncFs->createControl(cmd, pendingReads, logs);
1277 
1278     auto ifs = std::make_shared<IncFsMount>(std::string(root), -1, std::move(control), *this);
1279 
1280     auto mount = parseFromIncfs<metadata::Mount>(mIncFs.get(), ifs->control,
1281                                                  path::join(mountTarget, constants().infoMdName));
1282     if (!mount.has_loader() || !mount.has_storage()) {
1283         LOG(ERROR) << "Bad mount metadata in mount at " << root;
1284         return false;
1285     }
1286 
1287     ifs->mountId = mount.storage().id();
1288     mNextId = std::max(mNextId, ifs->mountId + 1);
1289 
1290     // Check if marker file present.
1291     if (checkReadLogsDisabledMarker(mountTarget)) {
1292         ifs->disableReadLogs();
1293     }
1294 
1295     // DataLoader params
1296     DataLoaderParamsParcel dataLoaderParams;
1297     {
1298         const auto& loader = mount.loader();
1299         dataLoaderParams.type = (content::pm::DataLoaderType)loader.type();
1300         dataLoaderParams.packageName = loader.package_name();
1301         dataLoaderParams.className = loader.class_name();
1302         dataLoaderParams.arguments = loader.arguments();
1303     }
1304 
1305     prepareDataLoader(*ifs, std::move(dataLoaderParams));
1306     CHECK(ifs->dataLoaderStub);
1307 
1308     std::vector<std::pair<std::string, metadata::BindPoint>> bindPoints;
1309     auto d = openDir(mountTarget);
1310     while (auto e = ::readdir(d.get())) {
1311         if (e->d_type == DT_REG) {
1312             auto name = std::string_view(e->d_name);
1313             if (name.starts_with(constants().mountpointMdPrefix)) {
1314                 bindPoints.emplace_back(name,
1315                                         parseFromIncfs<metadata::BindPoint>(mIncFs.get(),
1316                                                                             ifs->control,
1317                                                                             path::join(mountTarget,
1318                                                                                        name)));
1319                 if (bindPoints.back().second.dest_path().empty() ||
1320                     bindPoints.back().second.source_subdir().empty()) {
1321                     bindPoints.pop_back();
1322                     mIncFs->unlink(ifs->control, path::join(ifs->root, constants().mount, name));
1323                 }
1324             }
1325         } else if (e->d_type == DT_DIR) {
1326             if (e->d_name == "."sv || e->d_name == ".."sv) {
1327                 continue;
1328             }
1329             auto name = std::string_view(e->d_name);
1330             if (name.starts_with(constants().storagePrefix)) {
1331                 int storageId;
1332                 const auto res = std::from_chars(name.data() + constants().storagePrefix.size() + 1,
1333                                                  name.data() + name.size(), storageId);
1334                 if (res.ec != std::errc{} || *res.ptr != '_') {
1335                     LOG(WARNING) << "Ignoring storage with invalid name '" << name << "' for mount "
1336                                  << root;
1337                     continue;
1338                 }
1339                 auto [_, inserted] = mMounts.try_emplace(storageId, ifs);
1340                 if (!inserted) {
1341                     LOG(WARNING) << "Ignoring storage with duplicate id " << storageId
1342                                  << " for mount " << root;
1343                     continue;
1344                 }
1345                 ifs->storages.insert_or_assign(storageId,
1346                                                IncFsMount::Storage{
1347                                                        path::join(root, constants().mount, name)});
1348                 mNextId = std::max(mNextId, storageId + 1);
1349             }
1350         }
1351     }
1352 
1353     if (ifs->storages.empty()) {
1354         LOG(WARNING) << "No valid storages in mount " << root;
1355         return false;
1356     }
1357 
1358     int bindCount = 0;
1359     {
1360         std::unique_lock l(mLock, std::defer_lock);
1361         for (auto&& bp : bindPoints) {
1362             bindCount += !addBindMountWithMd(*ifs, bp.second.storage_id(), std::move(bp.first),
1363                                              std::move(*bp.second.mutable_source_subdir()),
1364                                              std::move(*bp.second.mutable_dest_path()),
1365                                              BindKind::Permanent, l);
1366         }
1367     }
1368 
1369     if (bindCount == 0) {
1370         LOG(WARNING) << "No valid bind points for mount " << root;
1371         deleteStorage(*ifs);
1372         return false;
1373     }
1374 
1375     // not locking here at all: we're still in the constructor, no other calls can happen
1376     mMounts[ifs->mountId] = std::move(ifs);
1377     return true;
1378 }
1379 
runCmdLooper()1380 void IncrementalService::runCmdLooper() {
1381     constexpr auto kTimeoutMsecs = 1000;
1382     while (mRunning.load(std::memory_order_relaxed)) {
1383         mLooper->pollAll(kTimeoutMsecs);
1384     }
1385 }
1386 
prepareDataLoader(IncFsMount & ifs,DataLoaderParamsParcel && params,const DataLoaderStatusListener * statusListener,StorageHealthCheckParams && healthCheckParams,const StorageHealthListener * healthListener)1387 IncrementalService::DataLoaderStubPtr IncrementalService::prepareDataLoader(
1388         IncFsMount& ifs, DataLoaderParamsParcel&& params,
1389         const DataLoaderStatusListener* statusListener,
1390         StorageHealthCheckParams&& healthCheckParams, const StorageHealthListener* healthListener) {
1391     std::unique_lock l(ifs.lock);
1392     prepareDataLoaderLocked(ifs, std::move(params), statusListener, std::move(healthCheckParams),
1393                             healthListener);
1394     return ifs.dataLoaderStub;
1395 }
1396 
prepareDataLoaderLocked(IncFsMount & ifs,DataLoaderParamsParcel && params,const DataLoaderStatusListener * statusListener,StorageHealthCheckParams && healthCheckParams,const StorageHealthListener * healthListener)1397 void IncrementalService::prepareDataLoaderLocked(IncFsMount& ifs, DataLoaderParamsParcel&& params,
1398                                                  const DataLoaderStatusListener* statusListener,
1399                                                  StorageHealthCheckParams&& healthCheckParams,
1400                                                  const StorageHealthListener* healthListener) {
1401     if (ifs.dataLoaderStub) {
1402         LOG(INFO) << "Skipped data loader preparation because it already exists";
1403         return;
1404     }
1405 
1406     FileSystemControlParcel fsControlParcel;
1407     fsControlParcel.incremental = aidl::make_nullable<IncrementalFileSystemControlParcel>();
1408     fsControlParcel.incremental->cmd.reset(dup(ifs.control.cmd()));
1409     fsControlParcel.incremental->pendingReads.reset(dup(ifs.control.pendingReads()));
1410     fsControlParcel.incremental->log.reset(dup(ifs.control.logs()));
1411     fsControlParcel.service = new IncrementalServiceConnector(*this, ifs.mountId);
1412 
1413     ifs.dataLoaderStub =
1414             new DataLoaderStub(*this, ifs.mountId, std::move(params), std::move(fsControlParcel),
1415                                statusListener, std::move(healthCheckParams), healthListener,
1416                                path::join(ifs.root, constants().mount));
1417 }
1418 
1419 template <class Duration>
elapsedMcs(Duration start,Duration end)1420 static long elapsedMcs(Duration start, Duration end) {
1421     return std::chrono::duration_cast<std::chrono::microseconds>(end - start).count();
1422 }
1423 
1424 // Extract lib files from zip, create new files in incfs and write data to them
1425 // Lib files should be placed next to the APK file in the following matter:
1426 // Example:
1427 // /path/to/base.apk
1428 // /path/to/lib/arm/first.so
1429 // /path/to/lib/arm/second.so
configureNativeBinaries(StorageId storage,std::string_view apkFullPath,std::string_view libDirRelativePath,std::string_view abi,bool extractNativeLibs)1430 bool IncrementalService::configureNativeBinaries(StorageId storage, std::string_view apkFullPath,
1431                                                  std::string_view libDirRelativePath,
1432                                                  std::string_view abi, bool extractNativeLibs) {
1433     auto start = Clock::now();
1434 
1435     const auto ifs = getIfs(storage);
1436     if (!ifs) {
1437         LOG(ERROR) << "Invalid storage " << storage;
1438         return false;
1439     }
1440 
1441     const auto targetLibPathRelativeToStorage =
1442             path::join(path::dirname(normalizePathToStorage(*ifs, storage, apkFullPath)),
1443                        libDirRelativePath);
1444 
1445     // First prepare target directories if they don't exist yet
1446     if (auto res = makeDirs(*ifs, storage, targetLibPathRelativeToStorage, 0755)) {
1447         LOG(ERROR) << "Failed to prepare target lib directory " << targetLibPathRelativeToStorage
1448                    << " errno: " << res;
1449         return false;
1450     }
1451 
1452     auto mkDirsTs = Clock::now();
1453     ZipArchiveHandle zipFileHandle;
1454     if (OpenArchive(path::c_str(apkFullPath), &zipFileHandle)) {
1455         LOG(ERROR) << "Failed to open zip file at " << apkFullPath;
1456         return false;
1457     }
1458 
1459     // Need a shared pointer: will be passing it into all unpacking jobs.
1460     std::shared_ptr<ZipArchive> zipFile(zipFileHandle, [](ZipArchiveHandle h) { CloseArchive(h); });
1461     void* cookie = nullptr;
1462     const auto libFilePrefix = path::join(constants().libDir, abi);
1463     if (StartIteration(zipFile.get(), &cookie, libFilePrefix, constants().libSuffix)) {
1464         LOG(ERROR) << "Failed to start zip iteration for " << apkFullPath;
1465         return false;
1466     }
1467     auto endIteration = [](void* cookie) { EndIteration(cookie); };
1468     auto iterationCleaner = std::unique_ptr<void, decltype(endIteration)>(cookie, endIteration);
1469 
1470     auto openZipTs = Clock::now();
1471 
1472     std::vector<Job> jobQueue;
1473     ZipEntry entry;
1474     std::string_view fileName;
1475     while (!Next(cookie, &entry, &fileName)) {
1476         if (fileName.empty()) {
1477             continue;
1478         }
1479 
1480         if (!extractNativeLibs) {
1481             // ensure the file is properly aligned and unpacked
1482             if (entry.method != kCompressStored) {
1483                 LOG(WARNING) << "Library " << fileName << " must be uncompressed to mmap it";
1484                 return false;
1485             }
1486             if ((entry.offset & (constants().blockSize - 1)) != 0) {
1487                 LOG(WARNING) << "Library " << fileName
1488                              << " must be page-aligned to mmap it, offset = 0x" << std::hex
1489                              << entry.offset;
1490                 return false;
1491             }
1492             continue;
1493         }
1494 
1495         auto startFileTs = Clock::now();
1496 
1497         const auto libName = path::basename(fileName);
1498         auto targetLibPath = path::join(targetLibPathRelativeToStorage, libName);
1499         const auto targetLibPathAbsolute = normalizePathToStorage(*ifs, storage, targetLibPath);
1500         // If the extract file already exists, skip
1501         if (access(targetLibPathAbsolute.c_str(), F_OK) == 0) {
1502             if (perfLoggingEnabled()) {
1503                 LOG(INFO) << "incfs: Native lib file already exists: " << targetLibPath
1504                           << "; skipping extraction, spent "
1505                           << elapsedMcs(startFileTs, Clock::now()) << "mcs";
1506             }
1507             continue;
1508         }
1509 
1510         // Create new lib file without signature info
1511         incfs::NewFileParams libFileParams = {
1512                 .size = entry.uncompressed_length,
1513                 .signature = {},
1514                 // Metadata of the new lib file is its relative path
1515                 .metadata = {targetLibPath.c_str(), (IncFsSize)targetLibPath.size()},
1516         };
1517         incfs::FileId libFileId = idFromMetadata(targetLibPath);
1518         if (auto res = mIncFs->makeFile(ifs->control, targetLibPathAbsolute, 0777, libFileId,
1519                                         libFileParams)) {
1520             LOG(ERROR) << "Failed to make file for: " << targetLibPath << " errno: " << res;
1521             // If one lib file fails to be created, abort others as well
1522             return false;
1523         }
1524 
1525         auto makeFileTs = Clock::now();
1526 
1527         // If it is a zero-byte file, skip data writing
1528         if (entry.uncompressed_length == 0) {
1529             if (perfLoggingEnabled()) {
1530                 LOG(INFO) << "incfs: Extracted " << libName
1531                           << "(0 bytes): " << elapsedMcs(startFileTs, makeFileTs) << "mcs";
1532             }
1533             continue;
1534         }
1535 
1536         jobQueue.emplace_back([this, zipFile, entry, ifs = std::weak_ptr<IncFsMount>(ifs),
1537                                libFileId, libPath = std::move(targetLibPath),
1538                                makeFileTs]() mutable {
1539             extractZipFile(ifs.lock(), zipFile.get(), entry, libFileId, libPath, makeFileTs);
1540         });
1541 
1542         if (perfLoggingEnabled()) {
1543             auto prepareJobTs = Clock::now();
1544             LOG(INFO) << "incfs: Processed " << libName << ": "
1545                       << elapsedMcs(startFileTs, prepareJobTs)
1546                       << "mcs, make file: " << elapsedMcs(startFileTs, makeFileTs)
1547                       << " prepare job: " << elapsedMcs(makeFileTs, prepareJobTs);
1548         }
1549     }
1550 
1551     auto processedTs = Clock::now();
1552 
1553     if (!jobQueue.empty()) {
1554         {
1555             std::lock_guard lock(mJobMutex);
1556             if (mRunning) {
1557                 auto& existingJobs = mJobQueue[ifs->mountId];
1558                 if (existingJobs.empty()) {
1559                     existingJobs = std::move(jobQueue);
1560                 } else {
1561                     existingJobs.insert(existingJobs.end(), std::move_iterator(jobQueue.begin()),
1562                                         std::move_iterator(jobQueue.end()));
1563                 }
1564             }
1565         }
1566         mJobCondition.notify_all();
1567     }
1568 
1569     if (perfLoggingEnabled()) {
1570         auto end = Clock::now();
1571         LOG(INFO) << "incfs: configureNativeBinaries complete in " << elapsedMcs(start, end)
1572                   << "mcs, make dirs: " << elapsedMcs(start, mkDirsTs)
1573                   << " open zip: " << elapsedMcs(mkDirsTs, openZipTs)
1574                   << " make files: " << elapsedMcs(openZipTs, processedTs)
1575                   << " schedule jobs: " << elapsedMcs(processedTs, end);
1576     }
1577 
1578     return true;
1579 }
1580 
extractZipFile(const IfsMountPtr & ifs,ZipArchiveHandle zipFile,ZipEntry & entry,const incfs::FileId & libFileId,std::string_view targetLibPath,Clock::time_point scheduledTs)1581 void IncrementalService::extractZipFile(const IfsMountPtr& ifs, ZipArchiveHandle zipFile,
1582                                         ZipEntry& entry, const incfs::FileId& libFileId,
1583                                         std::string_view targetLibPath,
1584                                         Clock::time_point scheduledTs) {
1585     if (!ifs) {
1586         LOG(INFO) << "Skipping zip file " << targetLibPath << " extraction for an expired mount";
1587         return;
1588     }
1589 
1590     auto libName = path::basename(targetLibPath);
1591     auto startedTs = Clock::now();
1592 
1593     // Write extracted data to new file
1594     // NOTE: don't zero-initialize memory, it may take a while for nothing
1595     auto libData = std::unique_ptr<uint8_t[]>(new uint8_t[entry.uncompressed_length]);
1596     if (ExtractToMemory(zipFile, &entry, libData.get(), entry.uncompressed_length)) {
1597         LOG(ERROR) << "Failed to extract native lib zip entry: " << libName;
1598         return;
1599     }
1600 
1601     auto extractFileTs = Clock::now();
1602 
1603     const auto writeFd = mIncFs->openForSpecialOps(ifs->control, libFileId);
1604     if (!writeFd.ok()) {
1605         LOG(ERROR) << "Failed to open write fd for: " << targetLibPath << " errno: " << writeFd;
1606         return;
1607     }
1608 
1609     auto openFileTs = Clock::now();
1610     const int numBlocks =
1611             (entry.uncompressed_length + constants().blockSize - 1) / constants().blockSize;
1612     std::vector<IncFsDataBlock> instructions(numBlocks);
1613     auto remainingData = std::span(libData.get(), entry.uncompressed_length);
1614     for (int i = 0; i < numBlocks; i++) {
1615         const auto blockSize = std::min<long>(constants().blockSize, remainingData.size());
1616         instructions[i] = IncFsDataBlock{
1617                 .fileFd = writeFd.get(),
1618                 .pageIndex = static_cast<IncFsBlockIndex>(i),
1619                 .compression = INCFS_COMPRESSION_KIND_NONE,
1620                 .kind = INCFS_BLOCK_KIND_DATA,
1621                 .dataSize = static_cast<uint32_t>(blockSize),
1622                 .data = reinterpret_cast<const char*>(remainingData.data()),
1623         };
1624         remainingData = remainingData.subspan(blockSize);
1625     }
1626     auto prepareInstsTs = Clock::now();
1627 
1628     size_t res = mIncFs->writeBlocks(instructions);
1629     if (res != instructions.size()) {
1630         LOG(ERROR) << "Failed to write data into: " << targetLibPath;
1631         return;
1632     }
1633 
1634     if (perfLoggingEnabled()) {
1635         auto endFileTs = Clock::now();
1636         LOG(INFO) << "incfs: Extracted " << libName << "(" << entry.compressed_length << " -> "
1637                   << entry.uncompressed_length << " bytes): " << elapsedMcs(startedTs, endFileTs)
1638                   << "mcs, scheduling delay: " << elapsedMcs(scheduledTs, startedTs)
1639                   << " extract: " << elapsedMcs(startedTs, extractFileTs)
1640                   << " open: " << elapsedMcs(extractFileTs, openFileTs)
1641                   << " prepare: " << elapsedMcs(openFileTs, prepareInstsTs)
1642                   << " write: " << elapsedMcs(prepareInstsTs, endFileTs);
1643     }
1644 }
1645 
waitForNativeBinariesExtraction(StorageId storage)1646 bool IncrementalService::waitForNativeBinariesExtraction(StorageId storage) {
1647     struct WaitPrinter {
1648         const Clock::time_point startTs = Clock::now();
1649         ~WaitPrinter() noexcept {
1650             if (perfLoggingEnabled()) {
1651                 const auto endTs = Clock::now();
1652                 LOG(INFO) << "incfs: waitForNativeBinariesExtraction() complete in "
1653                           << elapsedMcs(startTs, endTs) << "mcs";
1654             }
1655         }
1656     } waitPrinter;
1657 
1658     MountId mount;
1659     {
1660         auto ifs = getIfs(storage);
1661         if (!ifs) {
1662             return true;
1663         }
1664         mount = ifs->mountId;
1665     }
1666 
1667     std::unique_lock lock(mJobMutex);
1668     mJobCondition.wait(lock, [this, mount] {
1669         return !mRunning ||
1670                 (mPendingJobsMount != mount && mJobQueue.find(mount) == mJobQueue.end());
1671     });
1672     return mRunning;
1673 }
1674 
perfLoggingEnabled()1675 bool IncrementalService::perfLoggingEnabled() {
1676     static const bool enabled = base::GetBoolProperty("incremental.perflogging", false);
1677     return enabled;
1678 }
1679 
runJobProcessing()1680 void IncrementalService::runJobProcessing() {
1681     for (;;) {
1682         std::unique_lock lock(mJobMutex);
1683         mJobCondition.wait(lock, [this]() { return !mRunning || !mJobQueue.empty(); });
1684         if (!mRunning) {
1685             return;
1686         }
1687 
1688         auto it = mJobQueue.begin();
1689         mPendingJobsMount = it->first;
1690         auto queue = std::move(it->second);
1691         mJobQueue.erase(it);
1692         lock.unlock();
1693 
1694         for (auto&& job : queue) {
1695             job();
1696         }
1697 
1698         lock.lock();
1699         mPendingJobsMount = kInvalidStorageId;
1700         lock.unlock();
1701         mJobCondition.notify_all();
1702     }
1703 }
1704 
registerAppOpsCallback(const std::string & packageName)1705 void IncrementalService::registerAppOpsCallback(const std::string& packageName) {
1706     sp<IAppOpsCallback> listener;
1707     {
1708         std::unique_lock lock{mCallbacksLock};
1709         auto& cb = mCallbackRegistered[packageName];
1710         if (cb) {
1711             return;
1712         }
1713         cb = new AppOpsListener(*this, packageName);
1714         listener = cb;
1715     }
1716 
1717     mAppOpsManager->startWatchingMode(AppOpsManager::OP_GET_USAGE_STATS,
1718                                       String16(packageName.c_str()), listener);
1719 }
1720 
unregisterAppOpsCallback(const std::string & packageName)1721 bool IncrementalService::unregisterAppOpsCallback(const std::string& packageName) {
1722     sp<IAppOpsCallback> listener;
1723     {
1724         std::unique_lock lock{mCallbacksLock};
1725         auto found = mCallbackRegistered.find(packageName);
1726         if (found == mCallbackRegistered.end()) {
1727             return false;
1728         }
1729         listener = found->second;
1730         mCallbackRegistered.erase(found);
1731     }
1732 
1733     mAppOpsManager->stopWatchingMode(listener);
1734     return true;
1735 }
1736 
onAppOpChanged(const std::string & packageName)1737 void IncrementalService::onAppOpChanged(const std::string& packageName) {
1738     if (!unregisterAppOpsCallback(packageName)) {
1739         return;
1740     }
1741 
1742     std::vector<IfsMountPtr> affected;
1743     {
1744         std::lock_guard l(mLock);
1745         affected.reserve(mMounts.size());
1746         for (auto&& [id, ifs] : mMounts) {
1747             if (ifs->mountId == id && ifs->dataLoaderStub->params().packageName == packageName) {
1748                 affected.push_back(ifs);
1749             }
1750         }
1751     }
1752     for (auto&& ifs : affected) {
1753         applyStorageParams(*ifs, false);
1754     }
1755 }
1756 
addTimedJob(MountId id,Milliseconds after,Job what)1757 void IncrementalService::addTimedJob(MountId id, Milliseconds after, Job what) {
1758     if (id == kInvalidStorageId) {
1759         return;
1760     }
1761     mTimedQueue->addJob(id, after, std::move(what));
1762 }
1763 
removeTimedJobs(MountId id)1764 void IncrementalService::removeTimedJobs(MountId id) {
1765     if (id == kInvalidStorageId) {
1766         return;
1767     }
1768     mTimedQueue->removeJobs(id);
1769 }
1770 
DataLoaderStub(IncrementalService & service,MountId id,DataLoaderParamsParcel && params,FileSystemControlParcel && control,const DataLoaderStatusListener * statusListener,StorageHealthCheckParams && healthCheckParams,const StorageHealthListener * healthListener,std::string && healthPath)1771 IncrementalService::DataLoaderStub::DataLoaderStub(IncrementalService& service, MountId id,
1772                                                    DataLoaderParamsParcel&& params,
1773                                                    FileSystemControlParcel&& control,
1774                                                    const DataLoaderStatusListener* statusListener,
1775                                                    StorageHealthCheckParams&& healthCheckParams,
1776                                                    const StorageHealthListener* healthListener,
1777                                                    std::string&& healthPath)
1778       : mService(service),
1779         mId(id),
1780         mParams(std::move(params)),
1781         mControl(std::move(control)),
1782         mStatusListener(statusListener ? *statusListener : DataLoaderStatusListener()),
1783         mHealthListener(healthListener ? *healthListener : StorageHealthListener()),
1784         mHealthPath(std::move(healthPath)),
1785         mHealthCheckParams(std::move(healthCheckParams)) {
1786     if (mHealthListener) {
1787         if (!isHealthParamsValid()) {
1788             mHealthListener = {};
1789         }
1790     } else {
1791         // Disable advanced health check statuses.
1792         mHealthCheckParams.blockedTimeoutMs = -1;
1793     }
1794     updateHealthStatus();
1795 }
1796 
~DataLoaderStub()1797 IncrementalService::DataLoaderStub::~DataLoaderStub() {
1798     if (isValid()) {
1799         cleanupResources();
1800     }
1801 }
1802 
cleanupResources()1803 void IncrementalService::DataLoaderStub::cleanupResources() {
1804     auto now = Clock::now();
1805     {
1806         std::unique_lock lock(mMutex);
1807         mHealthPath.clear();
1808         unregisterFromPendingReads();
1809         resetHealthControl();
1810         mService.removeTimedJobs(mId);
1811     }
1812 
1813     requestDestroy();
1814 
1815     {
1816         std::unique_lock lock(mMutex);
1817         mParams = {};
1818         mControl = {};
1819         mHealthControl = {};
1820         mHealthListener = {};
1821         mStatusCondition.wait_until(lock, now + 60s, [this] {
1822             return mCurrentStatus == IDataLoaderStatusListener::DATA_LOADER_DESTROYED;
1823         });
1824         mStatusListener = {};
1825         mId = kInvalidStorageId;
1826     }
1827 }
1828 
getDataLoader()1829 sp<content::pm::IDataLoader> IncrementalService::DataLoaderStub::getDataLoader() {
1830     sp<IDataLoader> dataloader;
1831     auto status = mService.mDataLoaderManager->getDataLoader(id(), &dataloader);
1832     if (!status.isOk()) {
1833         LOG(ERROR) << "Failed to get dataloader: " << status.toString8();
1834         return {};
1835     }
1836     if (!dataloader) {
1837         LOG(ERROR) << "DataLoader is null: " << status.toString8();
1838         return {};
1839     }
1840     return dataloader;
1841 }
1842 
requestCreate()1843 bool IncrementalService::DataLoaderStub::requestCreate() {
1844     return setTargetStatus(IDataLoaderStatusListener::DATA_LOADER_CREATED);
1845 }
1846 
requestStart()1847 bool IncrementalService::DataLoaderStub::requestStart() {
1848     return setTargetStatus(IDataLoaderStatusListener::DATA_LOADER_STARTED);
1849 }
1850 
requestDestroy()1851 bool IncrementalService::DataLoaderStub::requestDestroy() {
1852     return setTargetStatus(IDataLoaderStatusListener::DATA_LOADER_DESTROYED);
1853 }
1854 
setTargetStatus(int newStatus)1855 bool IncrementalService::DataLoaderStub::setTargetStatus(int newStatus) {
1856     {
1857         std::unique_lock lock(mMutex);
1858         setTargetStatusLocked(newStatus);
1859     }
1860     return fsmStep();
1861 }
1862 
setTargetStatusLocked(int status)1863 void IncrementalService::DataLoaderStub::setTargetStatusLocked(int status) {
1864     auto oldStatus = mTargetStatus;
1865     mTargetStatus = status;
1866     mTargetStatusTs = Clock::now();
1867     LOG(DEBUG) << "Target status update for DataLoader " << id() << ": " << oldStatus << " -> "
1868                << status << " (current " << mCurrentStatus << ")";
1869 }
1870 
bind()1871 bool IncrementalService::DataLoaderStub::bind() {
1872     bool result = false;
1873     auto status = mService.mDataLoaderManager->bindToDataLoader(id(), mParams, this, &result);
1874     if (!status.isOk() || !result) {
1875         LOG(ERROR) << "Failed to bind a data loader for mount " << id();
1876         return false;
1877     }
1878     return true;
1879 }
1880 
create()1881 bool IncrementalService::DataLoaderStub::create() {
1882     auto dataloader = getDataLoader();
1883     if (!dataloader) {
1884         return false;
1885     }
1886     auto status = dataloader->create(id(), mParams, mControl, this);
1887     if (!status.isOk()) {
1888         LOG(ERROR) << "Failed to create DataLoader: " << status.toString8();
1889         return false;
1890     }
1891     return true;
1892 }
1893 
start()1894 bool IncrementalService::DataLoaderStub::start() {
1895     auto dataloader = getDataLoader();
1896     if (!dataloader) {
1897         return false;
1898     }
1899     auto status = dataloader->start(id());
1900     if (!status.isOk()) {
1901         LOG(ERROR) << "Failed to start DataLoader: " << status.toString8();
1902         return false;
1903     }
1904     return true;
1905 }
1906 
destroy()1907 bool IncrementalService::DataLoaderStub::destroy() {
1908     return mService.mDataLoaderManager->unbindFromDataLoader(id()).isOk();
1909 }
1910 
fsmStep()1911 bool IncrementalService::DataLoaderStub::fsmStep() {
1912     if (!isValid()) {
1913         return false;
1914     }
1915 
1916     int currentStatus;
1917     int targetStatus;
1918     {
1919         std::unique_lock lock(mMutex);
1920         currentStatus = mCurrentStatus;
1921         targetStatus = mTargetStatus;
1922     }
1923 
1924     LOG(DEBUG) << "fsmStep: " << id() << ": " << currentStatus << " -> " << targetStatus;
1925 
1926     if (currentStatus == targetStatus) {
1927         return true;
1928     }
1929 
1930     switch (targetStatus) {
1931         case IDataLoaderStatusListener::DATA_LOADER_UNAVAILABLE:
1932             // Do nothing, this is a reset state.
1933             break;
1934         case IDataLoaderStatusListener::DATA_LOADER_DESTROYED: {
1935             return destroy();
1936         }
1937         case IDataLoaderStatusListener::DATA_LOADER_STARTED: {
1938             switch (currentStatus) {
1939                 case IDataLoaderStatusListener::DATA_LOADER_CREATED:
1940                 case IDataLoaderStatusListener::DATA_LOADER_STOPPED:
1941                     return start();
1942             }
1943             [[fallthrough]];
1944         }
1945         case IDataLoaderStatusListener::DATA_LOADER_CREATED:
1946             switch (currentStatus) {
1947                 case IDataLoaderStatusListener::DATA_LOADER_DESTROYED:
1948                 case IDataLoaderStatusListener::DATA_LOADER_UNAVAILABLE:
1949                     return bind();
1950                 case IDataLoaderStatusListener::DATA_LOADER_BOUND:
1951                     return create();
1952             }
1953             break;
1954         default:
1955             LOG(ERROR) << "Invalid target status: " << targetStatus
1956                        << ", current status: " << currentStatus;
1957             break;
1958     }
1959     return false;
1960 }
1961 
onStatusChanged(MountId mountId,int newStatus)1962 binder::Status IncrementalService::DataLoaderStub::onStatusChanged(MountId mountId, int newStatus) {
1963     if (!isValid()) {
1964         return binder::Status::
1965                 fromServiceSpecificError(-EINVAL, "onStatusChange came to invalid DataLoaderStub");
1966     }
1967     if (id() != mountId) {
1968         LOG(ERROR) << "Mount ID mismatch: expected " << id() << ", but got: " << mountId;
1969         return binder::Status::fromServiceSpecificError(-EPERM, "Mount ID mismatch.");
1970     }
1971 
1972     int targetStatus, oldStatus;
1973     DataLoaderStatusListener listener;
1974     {
1975         std::unique_lock lock(mMutex);
1976         if (mCurrentStatus == newStatus) {
1977             return binder::Status::ok();
1978         }
1979 
1980         oldStatus = mCurrentStatus;
1981         mCurrentStatus = newStatus;
1982         targetStatus = mTargetStatus;
1983 
1984         listener = mStatusListener;
1985 
1986         if (mCurrentStatus == IDataLoaderStatusListener::DATA_LOADER_UNAVAILABLE) {
1987             // For unavailable, unbind from DataLoader to ensure proper re-commit.
1988             setTargetStatusLocked(IDataLoaderStatusListener::DATA_LOADER_DESTROYED);
1989         }
1990     }
1991 
1992     LOG(DEBUG) << "Current status update for DataLoader " << id() << ": " << oldStatus << " -> "
1993                << newStatus << " (target " << targetStatus << ")";
1994 
1995     if (listener) {
1996         listener->onStatusChanged(mountId, newStatus);
1997     }
1998 
1999     fsmStep();
2000 
2001     mStatusCondition.notify_all();
2002 
2003     return binder::Status::ok();
2004 }
2005 
isHealthParamsValid() const2006 bool IncrementalService::DataLoaderStub::isHealthParamsValid() const {
2007     return mHealthCheckParams.blockedTimeoutMs > 0 &&
2008             mHealthCheckParams.blockedTimeoutMs < mHealthCheckParams.unhealthyTimeoutMs;
2009 }
2010 
onHealthStatus(StorageHealthListener healthListener,int healthStatus)2011 void IncrementalService::DataLoaderStub::onHealthStatus(StorageHealthListener healthListener,
2012                                                         int healthStatus) {
2013     LOG(DEBUG) << id() << ": healthStatus: " << healthStatus;
2014     if (healthListener) {
2015         healthListener->onHealthStatus(id(), healthStatus);
2016     }
2017 }
2018 
updateHealthStatus(bool baseline)2019 void IncrementalService::DataLoaderStub::updateHealthStatus(bool baseline) {
2020     LOG(DEBUG) << id() << ": updateHealthStatus" << (baseline ? " (baseline)" : "");
2021 
2022     int healthStatusToReport = -1;
2023     StorageHealthListener healthListener;
2024 
2025     {
2026         std::unique_lock lock(mMutex);
2027         unregisterFromPendingReads();
2028 
2029         healthListener = mHealthListener;
2030 
2031         // Healthcheck depends on timestamp of the oldest pending read.
2032         // To get it, we need to re-open a pendingReads FD to get a full list of reads.
2033         // Additionally we need to re-register for epoll with fresh FDs in case there are no reads.
2034         const auto now = Clock::now();
2035         const auto kernelTsUs = getOldestPendingReadTs();
2036         if (baseline) {
2037             // Updating baseline only on looper/epoll callback, i.e. on new set of pending reads.
2038             mHealthBase = {now, kernelTsUs};
2039         }
2040 
2041         if (kernelTsUs == kMaxBootClockTsUs || mHealthBase.kernelTsUs == kMaxBootClockTsUs ||
2042             mHealthBase.userTs > now) {
2043             LOG(DEBUG) << id() << ": No pending reads or invalid base, report Ok and wait.";
2044             registerForPendingReads();
2045             healthStatusToReport = IStorageHealthListener::HEALTH_STATUS_OK;
2046             lock.unlock();
2047             onHealthStatus(healthListener, healthStatusToReport);
2048             return;
2049         }
2050 
2051         resetHealthControl();
2052 
2053         // Always make sure the data loader is started.
2054         setTargetStatusLocked(IDataLoaderStatusListener::DATA_LOADER_STARTED);
2055 
2056         // Skip any further processing if health check params are invalid.
2057         if (!isHealthParamsValid()) {
2058             LOG(DEBUG) << id()
2059                        << ": Skip any further processing if health check params are invalid.";
2060             healthStatusToReport = IStorageHealthListener::HEALTH_STATUS_READS_PENDING;
2061             lock.unlock();
2062             onHealthStatus(healthListener, healthStatusToReport);
2063             // Triggering data loader start. This is a one-time action.
2064             fsmStep();
2065             return;
2066         }
2067 
2068         // Don't schedule timer job less than 500ms in advance.
2069         static constexpr auto kTolerance = 500ms;
2070 
2071         const auto blockedTimeout = std::chrono::milliseconds(mHealthCheckParams.blockedTimeoutMs);
2072         const auto unhealthyTimeout =
2073                 std::chrono::milliseconds(mHealthCheckParams.unhealthyTimeoutMs);
2074         const auto unhealthyMonitoring =
2075                 std::max(1000ms,
2076                          std::chrono::milliseconds(mHealthCheckParams.unhealthyMonitoringMs));
2077 
2078         const auto kernelDeltaUs = kernelTsUs - mHealthBase.kernelTsUs;
2079         const auto userTs = mHealthBase.userTs + std::chrono::microseconds(kernelDeltaUs);
2080         const auto delta = std::chrono::duration_cast<std::chrono::milliseconds>(now - userTs);
2081 
2082         Milliseconds checkBackAfter;
2083         if (delta + kTolerance < blockedTimeout) {
2084             LOG(DEBUG) << id() << ": Report reads pending and wait for blocked status.";
2085             checkBackAfter = blockedTimeout - delta;
2086             healthStatusToReport = IStorageHealthListener::HEALTH_STATUS_READS_PENDING;
2087         } else if (delta + kTolerance < unhealthyTimeout) {
2088             LOG(DEBUG) << id() << ": Report blocked and wait for unhealthy.";
2089             checkBackAfter = unhealthyTimeout - delta;
2090             healthStatusToReport = IStorageHealthListener::HEALTH_STATUS_BLOCKED;
2091         } else {
2092             LOG(DEBUG) << id() << ": Report unhealthy and continue monitoring.";
2093             checkBackAfter = unhealthyMonitoring;
2094             healthStatusToReport = IStorageHealthListener::HEALTH_STATUS_UNHEALTHY;
2095         }
2096         LOG(DEBUG) << id() << ": updateHealthStatus in " << double(checkBackAfter.count()) / 1000.0
2097                    << "secs";
2098         mService.addTimedJob(id(), checkBackAfter, [this]() { updateHealthStatus(); });
2099     }
2100 
2101     // With kTolerance we are expecting these to execute before the next update.
2102     if (healthStatusToReport != -1) {
2103         onHealthStatus(healthListener, healthStatusToReport);
2104     }
2105 
2106     fsmStep();
2107 }
2108 
initializeHealthControl()2109 const incfs::UniqueControl& IncrementalService::DataLoaderStub::initializeHealthControl() {
2110     if (mHealthPath.empty()) {
2111         resetHealthControl();
2112         return mHealthControl;
2113     }
2114     if (mHealthControl.pendingReads() < 0) {
2115         mHealthControl = mService.mIncFs->openMount(mHealthPath);
2116     }
2117     if (mHealthControl.pendingReads() < 0) {
2118         LOG(ERROR) << "Failed to open health control for: " << id() << ", path: " << mHealthPath
2119                    << "(" << mHealthControl.cmd() << ":" << mHealthControl.pendingReads() << ":"
2120                    << mHealthControl.logs() << ")";
2121     }
2122     return mHealthControl;
2123 }
2124 
resetHealthControl()2125 void IncrementalService::DataLoaderStub::resetHealthControl() {
2126     mHealthControl = {};
2127 }
2128 
getOldestPendingReadTs()2129 BootClockTsUs IncrementalService::DataLoaderStub::getOldestPendingReadTs() {
2130     auto result = kMaxBootClockTsUs;
2131 
2132     const auto& control = initializeHealthControl();
2133     if (control.pendingReads() < 0) {
2134         return result;
2135     }
2136 
2137     std::vector<incfs::ReadInfo> pendingReads;
2138     if (mService.mIncFs->waitForPendingReads(control, 0ms, &pendingReads) !=
2139                 android::incfs::WaitResult::HaveData ||
2140         pendingReads.empty()) {
2141         return result;
2142     }
2143 
2144     LOG(DEBUG) << id() << ": pendingReads: " << control.pendingReads() << ", "
2145                << pendingReads.size() << ": " << pendingReads.front().bootClockTsUs;
2146 
2147     for (auto&& pendingRead : pendingReads) {
2148         result = std::min(result, pendingRead.bootClockTsUs);
2149     }
2150     return result;
2151 }
2152 
registerForPendingReads()2153 void IncrementalService::DataLoaderStub::registerForPendingReads() {
2154     const auto pendingReadsFd = mHealthControl.pendingReads();
2155     if (pendingReadsFd < 0) {
2156         return;
2157     }
2158 
2159     LOG(DEBUG) << id() << ": addFd(pendingReadsFd): " << pendingReadsFd;
2160 
2161     mService.mLooper->addFd(
2162             pendingReadsFd, android::Looper::POLL_CALLBACK, android::Looper::EVENT_INPUT,
2163             [](int, int, void* data) -> int {
2164                 auto&& self = (DataLoaderStub*)data;
2165                 self->updateHealthStatus(/*baseline=*/true);
2166                 return 0;
2167             },
2168             this);
2169     mService.mLooper->wake();
2170 }
2171 
unregisterFromPendingReads()2172 void IncrementalService::DataLoaderStub::unregisterFromPendingReads() {
2173     const auto pendingReadsFd = mHealthControl.pendingReads();
2174     if (pendingReadsFd < 0) {
2175         return;
2176     }
2177 
2178     LOG(DEBUG) << id() << ": removeFd(pendingReadsFd): " << pendingReadsFd;
2179 
2180     mService.mLooper->removeFd(pendingReadsFd);
2181     mService.mLooper->wake();
2182 }
2183 
onDump(int fd)2184 void IncrementalService::DataLoaderStub::onDump(int fd) {
2185     dprintf(fd, "    dataLoader: {\n");
2186     dprintf(fd, "      currentStatus: %d\n", mCurrentStatus);
2187     dprintf(fd, "      targetStatus: %d\n", mTargetStatus);
2188     dprintf(fd, "      targetStatusTs: %lldmcs\n",
2189             (long long)(elapsedMcs(mTargetStatusTs, Clock::now())));
2190     dprintf(fd, "      health: {\n");
2191     dprintf(fd, "        path: %s\n", mHealthPath.c_str());
2192     dprintf(fd, "        base: %lldmcs (%lld)\n",
2193             (long long)(elapsedMcs(mHealthBase.userTs, Clock::now())),
2194             (long long)mHealthBase.kernelTsUs);
2195     dprintf(fd, "        blockedTimeoutMs: %d\n", int(mHealthCheckParams.blockedTimeoutMs));
2196     dprintf(fd, "        unhealthyTimeoutMs: %d\n", int(mHealthCheckParams.unhealthyTimeoutMs));
2197     dprintf(fd, "        unhealthyMonitoringMs: %d\n",
2198             int(mHealthCheckParams.unhealthyMonitoringMs));
2199     dprintf(fd, "      }\n");
2200     const auto& params = mParams;
2201     dprintf(fd, "      dataLoaderParams: {\n");
2202     dprintf(fd, "        type: %s\n", toString(params.type).c_str());
2203     dprintf(fd, "        packageName: %s\n", params.packageName.c_str());
2204     dprintf(fd, "        className: %s\n", params.className.c_str());
2205     dprintf(fd, "        arguments: %s\n", params.arguments.c_str());
2206     dprintf(fd, "      }\n");
2207     dprintf(fd, "    }\n");
2208 }
2209 
opChanged(int32_t,const String16 &)2210 void IncrementalService::AppOpsListener::opChanged(int32_t, const String16&) {
2211     incrementalService.onAppOpChanged(packageName);
2212 }
2213 
setStorageParams(bool enableReadLogs,int32_t * _aidl_return)2214 binder::Status IncrementalService::IncrementalServiceConnector::setStorageParams(
2215         bool enableReadLogs, int32_t* _aidl_return) {
2216     *_aidl_return = incrementalService.setStorageParams(storage, enableReadLogs);
2217     return binder::Status::ok();
2218 }
2219 
idFromMetadata(std::span<const uint8_t> metadata)2220 FileId IncrementalService::idFromMetadata(std::span<const uint8_t> metadata) {
2221     return IncFs_FileIdFromMetadata({(const char*)metadata.data(), metadata.size()});
2222 }
2223 
2224 } // namespace android::incremental
2225