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