• 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/Status.h>
27 #include <sys/stat.h>
28 #include <uuid/uuid.h>
29 
30 #include <charconv>
31 #include <ctime>
32 #include <iterator>
33 #include <span>
34 #include <type_traits>
35 
36 #include "IncrementalServiceValidation.h"
37 #include "Metadata.pb.h"
38 
39 using namespace std::literals;
40 
41 constexpr const char* kLoaderUsageStats = "android.permission.LOADER_USAGE_STATS";
42 constexpr const char* kOpUsage = "android:loader_usage_stats";
43 
44 constexpr const char* kInteractAcrossUsers = "android.permission.INTERACT_ACROSS_USERS";
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     static constexpr auto userStatusDelay = 100ms;
70 
71     static constexpr auto progressUpdateInterval = 1000ms;
72     static constexpr auto perUidTimeoutOffset = progressUpdateInterval * 2;
73     static constexpr auto minPerUidTimeout = progressUpdateInterval * 3;
74 
75     // If DL was up and not crashing for 10mins, we consider it healthy and reset all delays.
76     static constexpr auto healthyDataLoaderUptime = 10min;
77 
78     // For healthy DLs, we'll retry every ~5secs for ~10min
79     static constexpr auto bindRetryInterval = 5s;
80     static constexpr auto bindGracePeriod = 10min;
81 
82     static constexpr auto bindingTimeout = 1min;
83 
84     // 1s, 10s, 100s (~2min), 1000s (~15min), 10000s (~3hrs)
85     static constexpr auto minBindDelay = 1s;
86     static constexpr auto maxBindDelay = 10000s;
87     static constexpr auto bindDelayMultiplier = 10;
88     static constexpr auto bindDelayJitterDivider = 10;
89 
90     // Max interval after system invoked the DL when readlog collection can be enabled.
91     static constexpr auto readLogsMaxInterval = 2h;
92 
93     // How long should we wait till dataLoader reports destroyed.
94     static constexpr auto destroyTimeout = 10s;
95 
96     static constexpr auto anyStatus = INT_MIN;
97 };
98 
constants()99 static const Constants& constants() {
100     static constexpr Constants c;
101     return c;
102 }
103 
isPageAligned(IncFsSize s)104 static bool isPageAligned(IncFsSize s) {
105     return (s & (Constants::blockSize - 1)) == 0;
106 }
107 
getAlwaysEnableReadTimeoutsForSystemDataLoaders()108 static bool getAlwaysEnableReadTimeoutsForSystemDataLoaders() {
109     return android::base::
110             GetBoolProperty("debug.incremental.always_enable_read_timeouts_for_system_dataloaders",
111                             true);
112 }
113 
getEnforceReadLogsMaxIntervalForSystemDataLoaders()114 static bool getEnforceReadLogsMaxIntervalForSystemDataLoaders() {
115     return android::base::GetBoolProperty("debug.incremental.enforce_readlogs_max_interval_for_"
116                                           "system_dataloaders",
117                                           false);
118 }
119 
getReadLogsMaxInterval()120 static Seconds getReadLogsMaxInterval() {
121     constexpr int limit = duration_cast<Seconds>(Constants::readLogsMaxInterval).count();
122     int readlogs_max_interval_secs =
123             std::min(limit,
124                      android::base::GetIntProperty<
125                              int>("debug.incremental.readlogs_max_interval_sec", limit));
126     return Seconds{readlogs_max_interval_secs};
127 }
128 
129 template <base::LogSeverity level = base::ERROR>
mkdirOrLog(std::string_view name,int mode=0770,bool allowExisting=true)130 bool mkdirOrLog(std::string_view name, int mode = 0770, bool allowExisting = true) {
131     auto cstr = path::c_str(name);
132     if (::mkdir(cstr, mode)) {
133         if (!allowExisting || errno != EEXIST) {
134             PLOG(level) << "Can't create directory '" << name << '\'';
135             return false;
136         }
137         struct stat st;
138         if (::stat(cstr, &st) || !S_ISDIR(st.st_mode)) {
139             PLOG(level) << "Path exists but is not a directory: '" << name << '\'';
140             return false;
141         }
142     }
143     if (::chmod(cstr, mode)) {
144         PLOG(level) << "Changing permission failed for '" << name << '\'';
145         return false;
146     }
147 
148     return true;
149 }
150 
toMountKey(std::string_view path)151 static std::string toMountKey(std::string_view path) {
152     if (path.empty()) {
153         return "@none";
154     }
155     if (path == "/"sv) {
156         return "@root";
157     }
158     if (path::isAbsolute(path)) {
159         path.remove_prefix(1);
160     }
161     if (path.size() > 16) {
162         path = path.substr(0, 16);
163     }
164     std::string res(path);
165     std::replace_if(
166             res.begin(), res.end(), [](char c) { return c == '/' || c == '@'; }, '_');
167     return std::string(constants().mountKeyPrefix) += res;
168 }
169 
makeMountDir(std::string_view incrementalDir,std::string_view path)170 static std::pair<std::string, std::string> makeMountDir(std::string_view incrementalDir,
171                                                         std::string_view path) {
172     auto mountKey = toMountKey(path);
173     const auto prefixSize = mountKey.size();
174     for (int counter = 0; counter < 1000;
175          mountKey.resize(prefixSize), base::StringAppendF(&mountKey, "%d", counter++)) {
176         auto mountRoot = path::join(incrementalDir, mountKey);
177         if (mkdirOrLog(mountRoot, 0777, false)) {
178             return {mountKey, mountRoot};
179         }
180     }
181     return {};
182 }
183 
184 template <class Map>
findParentPath(const Map & map,std::string_view path)185 typename Map::const_iterator findParentPath(const Map& map, std::string_view path) {
186     const auto nextIt = map.upper_bound(path);
187     if (nextIt == map.begin()) {
188         return map.end();
189     }
190     const auto suspectIt = std::prev(nextIt);
191     if (!path::startsWith(path, suspectIt->first)) {
192         return map.end();
193     }
194     return suspectIt;
195 }
196 
dup(base::borrowed_fd fd)197 static base::unique_fd dup(base::borrowed_fd fd) {
198     const auto res = fcntl(fd.get(), F_DUPFD_CLOEXEC, 0);
199     return base::unique_fd(res);
200 }
201 
202 template <class ProtoMessage, class Control>
parseFromIncfs(const IncFsWrapper * incfs,const Control & control,std::string_view path)203 static ProtoMessage parseFromIncfs(const IncFsWrapper* incfs, const Control& control,
204                                    std::string_view path) {
205     auto md = incfs->getMetadata(control, path);
206     ProtoMessage message;
207     return message.ParseFromArray(md.data(), md.size()) ? message : ProtoMessage{};
208 }
209 
isValidMountTarget(std::string_view path)210 static bool isValidMountTarget(std::string_view path) {
211     return path::isAbsolute(path) && path::isEmptyDir(path).value_or(true);
212 }
213 
makeUniqueName(std::string_view prefix)214 std::string makeUniqueName(std::string_view prefix) {
215     static constexpr auto uuidStringSize = 36;
216 
217     uuid_t guid;
218     uuid_generate(guid);
219 
220     std::string name;
221     const auto prefixSize = prefix.size();
222     name.reserve(prefixSize + uuidStringSize);
223 
224     name = prefix;
225     name.resize(prefixSize + uuidStringSize);
226     uuid_unparse(guid, name.data() + prefixSize);
227 
228     return name;
229 }
230 
makeBindMdName()231 std::string makeBindMdName() {
232     return makeUniqueName(constants().mountpointMdPrefix);
233 }
234 
checkReadLogsDisabledMarker(std::string_view root)235 static bool checkReadLogsDisabledMarker(std::string_view root) {
236     const auto markerPath = path::c_str(path::join(root, constants().readLogsDisabledMarkerName));
237     struct stat st;
238     return (::stat(markerPath, &st) == 0);
239 }
240 
241 } // namespace
242 
~IncFsMount()243 IncrementalService::IncFsMount::~IncFsMount() {
244     if (dataLoaderStub) {
245         dataLoaderStub->cleanupResources();
246         dataLoaderStub = {};
247     }
248     control.close();
249     LOG(INFO) << "Unmounting and cleaning up mount " << mountId << " with root '" << root << '\'';
250     for (auto&& [target, _] : bindPoints) {
251         LOG(INFO) << "  bind: " << target;
252         incrementalService.mVold->unmountIncFs(target);
253     }
254     LOG(INFO) << "  root: " << root;
255     incrementalService.mVold->unmountIncFs(path::join(root, constants().mount));
256     cleanupFilesystem(root);
257 }
258 
makeStorage(StorageId id)259 auto IncrementalService::IncFsMount::makeStorage(StorageId id) -> StorageMap::iterator {
260     std::string name;
261     for (int no = nextStorageDirNo.fetch_add(1, std::memory_order_relaxed), i = 0;
262          i < 1024 && no >= 0; no = nextStorageDirNo.fetch_add(1, std::memory_order_relaxed), ++i) {
263         name.clear();
264         base::StringAppendF(&name, "%.*s_%d_%d", int(constants().storagePrefix.size()),
265                             constants().storagePrefix.data(), id, no);
266         auto fullName = path::join(root, constants().mount, name);
267         if (auto err = incrementalService.mIncFs->makeDir(control, fullName, 0755); !err) {
268             std::lock_guard l(lock);
269             return storages.insert_or_assign(id, Storage{std::move(fullName)}).first;
270         } else if (err != EEXIST) {
271             LOG(ERROR) << __func__ << "(): failed to create dir |" << fullName << "| " << err;
272             break;
273         }
274     }
275     nextStorageDirNo = 0;
276     return storages.end();
277 }
278 
279 template <class Func>
makeCleanup(Func && f)280 static auto makeCleanup(Func&& f) requires(!std::is_lvalue_reference_v<Func>) {
281     // ok to move a 'forwarding' reference here as lvalues are disabled anyway
282     auto deleter = [f = std::move(f)](auto) { // NOLINT
283         f();
284     };
285     // &f is a dangling pointer here, but we actually never use it as deleter moves it in.
286     return std::unique_ptr<Func, decltype(deleter)>(&f, std::move(deleter));
287 }
288 
openDir(const char * dir)289 static auto openDir(const char* dir) {
290     struct DirCloser {
291         void operator()(DIR* d) const noexcept { ::closedir(d); }
292     };
293     return std::unique_ptr<DIR, DirCloser>(::opendir(dir));
294 }
295 
openDir(std::string_view dir)296 static auto openDir(std::string_view dir) {
297     return openDir(path::c_str(dir));
298 }
299 
rmDirContent(const char * path)300 static int rmDirContent(const char* path) {
301     auto dir = openDir(path);
302     if (!dir) {
303         return -EINVAL;
304     }
305     while (auto entry = ::readdir(dir.get())) {
306         if (entry->d_name == "."sv || entry->d_name == ".."sv) {
307             continue;
308         }
309         auto fullPath = base::StringPrintf("%s/%s", path, entry->d_name);
310         if (entry->d_type == DT_DIR) {
311             if (const auto err = rmDirContent(fullPath.c_str()); err != 0) {
312                 PLOG(WARNING) << "Failed to delete " << fullPath << " content";
313                 return err;
314             }
315             if (const auto err = ::rmdir(fullPath.c_str()); err != 0) {
316                 PLOG(WARNING) << "Failed to rmdir " << fullPath;
317                 return err;
318             }
319         } else {
320             if (const auto err = ::unlink(fullPath.c_str()); err != 0) {
321                 PLOG(WARNING) << "Failed to delete " << fullPath;
322                 return err;
323             }
324         }
325     }
326     return 0;
327 }
328 
cleanupFilesystem(std::string_view root)329 void IncrementalService::IncFsMount::cleanupFilesystem(std::string_view root) {
330     rmDirContent(path::join(root, constants().backing).c_str());
331     ::rmdir(path::join(root, constants().backing).c_str());
332     ::rmdir(path::join(root, constants().mount).c_str());
333     ::rmdir(path::c_str(root));
334 }
335 
setFlag(StorageFlags flag,bool value)336 void IncrementalService::IncFsMount::setFlag(StorageFlags flag, bool value) {
337     if (value) {
338         flags |= flag;
339     } else {
340         flags &= ~flag;
341     }
342 }
343 
IncrementalService(ServiceManagerWrapper && sm,std::string_view rootDir)344 IncrementalService::IncrementalService(ServiceManagerWrapper&& sm, std::string_view rootDir)
345       : mVold(sm.getVoldService()),
346         mDataLoaderManager(sm.getDataLoaderManager()),
347         mIncFs(sm.getIncFs()),
348         mAppOpsManager(sm.getAppOpsManager()),
349         mJni(sm.getJni()),
350         mLooper(sm.getLooper()),
351         mTimedQueue(sm.getTimedQueue()),
352         mProgressUpdateJobQueue(sm.getProgressUpdateJobQueue()),
353         mFs(sm.getFs()),
354         mClock(sm.getClock()),
355         mIncrementalDir(rootDir) {
356     CHECK(mVold) << "Vold service is unavailable";
357     CHECK(mDataLoaderManager) << "DataLoaderManagerService is unavailable";
358     CHECK(mAppOpsManager) << "AppOpsManager is unavailable";
359     CHECK(mJni) << "JNI is unavailable";
360     CHECK(mLooper) << "Looper is unavailable";
361     CHECK(mTimedQueue) << "TimedQueue is unavailable";
362     CHECK(mProgressUpdateJobQueue) << "mProgressUpdateJobQueue is unavailable";
363     CHECK(mFs) << "Fs is unavailable";
364     CHECK(mClock) << "Clock is unavailable";
365 
366     mJobQueue.reserve(16);
367     mJobProcessor = std::thread([this]() {
368         mJni->initializeForCurrentThread();
369         runJobProcessing();
370     });
371     mCmdLooperThread = std::thread([this]() {
372         mJni->initializeForCurrentThread();
373         runCmdLooper();
374     });
375 
376     const auto mountedRootNames = adoptMountedInstances();
377     mountExistingImages(mountedRootNames);
378 }
379 
~IncrementalService()380 IncrementalService::~IncrementalService() {
381     {
382         std::lock_guard lock(mJobMutex);
383         mRunning = false;
384     }
385     mJobCondition.notify_all();
386     mJobProcessor.join();
387     mLooper->wake();
388     mCmdLooperThread.join();
389     mTimedQueue->stop();
390     mProgressUpdateJobQueue->stop();
391     // Ensure that mounts are destroyed while the service is still valid.
392     mBindsByPath.clear();
393     mMounts.clear();
394 }
395 
toString(IncrementalService::BindKind kind)396 static const char* toString(IncrementalService::BindKind kind) {
397     switch (kind) {
398         case IncrementalService::BindKind::Temporary:
399             return "Temporary";
400         case IncrementalService::BindKind::Permanent:
401             return "Permanent";
402     }
403 }
404 
405 template <class Duration>
elapsedMcs(Duration start,Duration end)406 static int64_t elapsedMcs(Duration start, Duration end) {
407     return std::chrono::duration_cast<std::chrono::microseconds>(end - start).count();
408 }
409 
elapsedUsSinceMonoTs(uint64_t monoTsUs)410 int64_t IncrementalService::elapsedUsSinceMonoTs(uint64_t monoTsUs) {
411     const auto now = mClock->now();
412     const auto nowUs = static_cast<uint64_t>(
413             duration_cast<std::chrono::microseconds>(now.time_since_epoch()).count());
414     return nowUs - monoTsUs;
415 }
416 
onDump(int fd)417 void IncrementalService::onDump(int fd) {
418     dprintf(fd, "Incremental is %s\n", incfs::enabled() ? "ENABLED" : "DISABLED");
419     dprintf(fd, "IncFs features: 0x%x\n", int(mIncFs->features()));
420     dprintf(fd, "Incremental dir: %s\n", mIncrementalDir.c_str());
421 
422     std::unique_lock l(mLock);
423 
424     dprintf(fd, "Mounts (%d): {\n", int(mMounts.size()));
425     for (auto&& [id, ifs] : mMounts) {
426         std::unique_lock ll(ifs->lock);
427         const IncFsMount& mnt = *ifs;
428         dprintf(fd, "  [%d]: {\n", id);
429         if (id != mnt.mountId) {
430             dprintf(fd, "    reference to mountId: %d\n", mnt.mountId);
431         } else {
432             dprintf(fd, "    mountId: %d\n", mnt.mountId);
433             dprintf(fd, "    root: %s\n", mnt.root.c_str());
434             const auto& metricsInstanceName = ifs->metricsKey;
435             dprintf(fd, "    metrics instance name: %s\n", path::c_str(metricsInstanceName).get());
436             dprintf(fd, "    nextStorageDirNo: %d\n", mnt.nextStorageDirNo.load());
437             dprintf(fd, "    flags: %d\n", int(mnt.flags));
438             if (mnt.startLoadingTs.time_since_epoch() == Clock::duration::zero()) {
439                 dprintf(fd, "    not loading\n");
440             } else {
441                 dprintf(fd, "    startLoading: %llds\n",
442                         (long long)(elapsedMcs(mnt.startLoadingTs, Clock::now()) / 1000000));
443             }
444             if (mnt.dataLoaderStub) {
445                 mnt.dataLoaderStub->onDump(fd);
446             } else {
447                 dprintf(fd, "    dataLoader: null\n");
448             }
449             dprintf(fd, "    storages (%d): {\n", int(mnt.storages.size()));
450             for (auto&& [storageId, storage] : mnt.storages) {
451                 dprintf(fd, "      [%d] -> [%s] (%d %% loaded) \n", storageId, storage.name.c_str(),
452                         (int)(getLoadingProgressFromPath(mnt, storage.name.c_str()).getProgress() *
453                               100));
454             }
455             dprintf(fd, "    }\n");
456 
457             dprintf(fd, "    bindPoints (%d): {\n", int(mnt.bindPoints.size()));
458             for (auto&& [target, bind] : mnt.bindPoints) {
459                 dprintf(fd, "      [%s]->[%d]:\n", target.c_str(), bind.storage);
460                 dprintf(fd, "        savedFilename: %s\n", bind.savedFilename.c_str());
461                 dprintf(fd, "        sourceDir: %s\n", bind.sourceDir.c_str());
462                 dprintf(fd, "        kind: %s\n", toString(bind.kind));
463             }
464             dprintf(fd, "    }\n");
465 
466             dprintf(fd, "    incfsMetrics: {\n");
467             const auto incfsMetrics = mIncFs->getMetrics(metricsInstanceName);
468             if (incfsMetrics) {
469                 dprintf(fd, "      readsDelayedMin: %d\n", incfsMetrics.value().readsDelayedMin);
470                 dprintf(fd, "      readsDelayedMinUs: %lld\n",
471                         (long long)incfsMetrics.value().readsDelayedMinUs);
472                 dprintf(fd, "      readsDelayedPending: %d\n",
473                         incfsMetrics.value().readsDelayedPending);
474                 dprintf(fd, "      readsDelayedPendingUs: %lld\n",
475                         (long long)incfsMetrics.value().readsDelayedPendingUs);
476                 dprintf(fd, "      readsFailedHashVerification: %d\n",
477                         incfsMetrics.value().readsFailedHashVerification);
478                 dprintf(fd, "      readsFailedOther: %d\n", incfsMetrics.value().readsFailedOther);
479                 dprintf(fd, "      readsFailedTimedOut: %d\n",
480                         incfsMetrics.value().readsFailedTimedOut);
481             } else {
482                 dprintf(fd, "      Metrics not available. Errno: %d\n", errno);
483             }
484             dprintf(fd, "    }\n");
485 
486             const auto lastReadError = mIncFs->getLastReadError(ifs->control);
487             const auto errorNo = errno;
488             dprintf(fd, "    lastReadError: {\n");
489             if (lastReadError) {
490                 if (lastReadError->timestampUs == 0) {
491                     dprintf(fd, "      No read errors.\n");
492                 } else {
493                     dprintf(fd, "      fileId: %s\n",
494                             IncFsWrapper::toString(lastReadError->id).c_str());
495                     dprintf(fd, "      time: %llu microseconds ago\n",
496                             (unsigned long long)elapsedUsSinceMonoTs(lastReadError->timestampUs));
497                     dprintf(fd, "      blockIndex: %d\n", lastReadError->block);
498                     dprintf(fd, "      errno: %d\n", lastReadError->errorNo);
499                 }
500             } else {
501                 dprintf(fd, "      Info not available. Errno: %d\n", errorNo);
502             }
503             dprintf(fd, "    }\n");
504         }
505         dprintf(fd, "  }\n");
506     }
507     dprintf(fd, "}\n");
508     dprintf(fd, "Sorted binds (%d): {\n", int(mBindsByPath.size()));
509     for (auto&& [target, mountPairIt] : mBindsByPath) {
510         const auto& bind = mountPairIt->second;
511         dprintf(fd, "    [%s]->[%d]:\n", target.c_str(), bind.storage);
512         dprintf(fd, "      savedFilename: %s\n", bind.savedFilename.c_str());
513         dprintf(fd, "      sourceDir: %s\n", bind.sourceDir.c_str());
514         dprintf(fd, "      kind: %s\n", toString(bind.kind));
515     }
516     dprintf(fd, "}\n");
517 }
518 
needStartDataLoaderLocked(IncFsMount & ifs)519 bool IncrementalService::needStartDataLoaderLocked(IncFsMount& ifs) {
520     if (!ifs.dataLoaderStub) {
521         return false;
522     }
523     if (ifs.dataLoaderStub->isSystemDataLoader()) {
524         return true;
525     }
526 
527     return mIncFs->isEverythingFullyLoaded(ifs.control) == incfs::LoadingState::MissingBlocks;
528 }
529 
onSystemReady()530 void IncrementalService::onSystemReady() {
531     if (mSystemReady.exchange(true)) {
532         return;
533     }
534 
535     std::vector<IfsMountPtr> mounts;
536     {
537         std::lock_guard l(mLock);
538         mounts.reserve(mMounts.size());
539         for (auto&& [id, ifs] : mMounts) {
540             std::unique_lock ll(ifs->lock);
541 
542             if (ifs->mountId != id) {
543                 continue;
544             }
545 
546             if (needStartDataLoaderLocked(*ifs)) {
547                 mounts.push_back(ifs);
548             }
549         }
550     }
551 
552     if (mounts.empty()) {
553         return;
554     }
555 
556     std::thread([this, mounts = std::move(mounts)]() {
557         mJni->initializeForCurrentThread();
558         for (auto&& ifs : mounts) {
559             std::unique_lock l(ifs->lock);
560             if (ifs->dataLoaderStub) {
561                 ifs->dataLoaderStub->requestStart();
562             }
563         }
564     }).detach();
565 }
566 
getStorageSlotLocked()567 auto IncrementalService::getStorageSlotLocked() -> MountMap::iterator {
568     for (;;) {
569         if (mNextId == kMaxStorageId) {
570             mNextId = 0;
571         }
572         auto id = ++mNextId;
573         auto [it, inserted] = mMounts.try_emplace(id, nullptr);
574         if (inserted) {
575             return it;
576         }
577     }
578 }
579 
createStorage(std::string_view mountPoint,content::pm::DataLoaderParamsParcel dataLoaderParams,CreateOptions options)580 StorageId IncrementalService::createStorage(std::string_view mountPoint,
581                                             content::pm::DataLoaderParamsParcel dataLoaderParams,
582                                             CreateOptions options) {
583     LOG(INFO) << "createStorage: " << mountPoint << " | " << int(options);
584     if (!path::isAbsolute(mountPoint)) {
585         LOG(ERROR) << "path is not absolute: " << mountPoint;
586         return kInvalidStorageId;
587     }
588 
589     auto mountNorm = path::normalize(mountPoint);
590     {
591         const auto id = findStorageId(mountNorm);
592         if (id != kInvalidStorageId) {
593             if (options & CreateOptions::OpenExisting) {
594                 LOG(INFO) << "Opened existing storage " << id;
595                 return id;
596             }
597             LOG(ERROR) << "Directory " << mountPoint << " is already mounted at storage " << id;
598             return kInvalidStorageId;
599         }
600     }
601 
602     if (!(options & CreateOptions::CreateNew)) {
603         LOG(ERROR) << "not requirested create new storage, and it doesn't exist: " << mountPoint;
604         return kInvalidStorageId;
605     }
606 
607     if (!path::isEmptyDir(mountNorm)) {
608         LOG(ERROR) << "Mounting over existing non-empty directory is not supported: " << mountNorm;
609         return kInvalidStorageId;
610     }
611     auto [mountKey, mountRoot] = makeMountDir(mIncrementalDir, mountNorm);
612     if (mountRoot.empty()) {
613         LOG(ERROR) << "Bad mount point";
614         return kInvalidStorageId;
615     }
616     // Make sure the code removes all crap it may create while still failing.
617     auto firstCleanup = [](const std::string* ptr) { IncFsMount::cleanupFilesystem(*ptr); };
618     auto firstCleanupOnFailure =
619             std::unique_ptr<std::string, decltype(firstCleanup)>(&mountRoot, firstCleanup);
620 
621     auto mountTarget = path::join(mountRoot, constants().mount);
622     const auto backing = path::join(mountRoot, constants().backing);
623     if (!mkdirOrLog(backing, 0777) || !mkdirOrLog(mountTarget)) {
624         return kInvalidStorageId;
625     }
626 
627     std::string metricsKey;
628     IncFsMount::Control control;
629     {
630         std::lock_guard l(mMountOperationLock);
631         IncrementalFileSystemControlParcel controlParcel;
632 
633         if (auto err = rmDirContent(backing.c_str())) {
634             LOG(ERROR) << "Coudn't clean the backing directory " << backing << ": " << err;
635             return kInvalidStorageId;
636         }
637         if (!mkdirOrLog(path::join(backing, ".index"), 0777)) {
638             return kInvalidStorageId;
639         }
640         if (!mkdirOrLog(path::join(backing, ".incomplete"), 0777)) {
641             return kInvalidStorageId;
642         }
643         metricsKey = makeUniqueName(mountKey);
644         auto status = mVold->mountIncFs(backing, mountTarget, 0, metricsKey, &controlParcel);
645         if (!status.isOk()) {
646             LOG(ERROR) << "Vold::mountIncFs() failed: " << status.toString8();
647             return kInvalidStorageId;
648         }
649         if (controlParcel.cmd.get() < 0 || controlParcel.pendingReads.get() < 0 ||
650             controlParcel.log.get() < 0) {
651             LOG(ERROR) << "Vold::mountIncFs() returned invalid control parcel.";
652             return kInvalidStorageId;
653         }
654         int cmd = controlParcel.cmd.release().release();
655         int pendingReads = controlParcel.pendingReads.release().release();
656         int logs = controlParcel.log.release().release();
657         int blocksWritten =
658                 controlParcel.blocksWritten ? controlParcel.blocksWritten->release().release() : -1;
659         control = mIncFs->createControl(cmd, pendingReads, logs, blocksWritten);
660     }
661 
662     std::unique_lock l(mLock);
663     const auto mountIt = getStorageSlotLocked();
664     const auto mountId = mountIt->first;
665     l.unlock();
666 
667     auto ifs = std::make_shared<IncFsMount>(std::move(mountRoot), std::move(metricsKey), mountId,
668                                             std::move(control), *this);
669     // Now it's the |ifs|'s responsibility to clean up after itself, and the only cleanup we need
670     // is the removal of the |ifs|.
671     (void)firstCleanupOnFailure.release();
672 
673     auto secondCleanup = [this, &l](auto itPtr) {
674         if (!l.owns_lock()) {
675             l.lock();
676         }
677         mMounts.erase(*itPtr);
678     };
679     auto secondCleanupOnFailure =
680             std::unique_ptr<decltype(mountIt), decltype(secondCleanup)>(&mountIt, secondCleanup);
681 
682     const auto storageIt = ifs->makeStorage(ifs->mountId);
683     if (storageIt == ifs->storages.end()) {
684         LOG(ERROR) << "Can't create a default storage directory";
685         return kInvalidStorageId;
686     }
687 
688     {
689         metadata::Mount m;
690         m.mutable_storage()->set_id(ifs->mountId);
691         m.mutable_loader()->set_type((int)dataLoaderParams.type);
692         m.mutable_loader()->set_package_name(std::move(dataLoaderParams.packageName));
693         m.mutable_loader()->set_class_name(std::move(dataLoaderParams.className));
694         m.mutable_loader()->set_arguments(std::move(dataLoaderParams.arguments));
695         const auto metadata = m.SerializeAsString();
696         if (auto err =
697                     mIncFs->makeFile(ifs->control,
698                                      path::join(ifs->root, constants().mount,
699                                                 constants().infoMdName),
700                                      0777, idFromMetadata(metadata),
701                                      {.metadata = {metadata.data(), (IncFsSize)metadata.size()}})) {
702             LOG(ERROR) << "Saving mount metadata failed: " << -err;
703             return kInvalidStorageId;
704         }
705     }
706 
707     const auto bk =
708             (options & CreateOptions::PermanentBind) ? BindKind::Permanent : BindKind::Temporary;
709     if (auto err = addBindMount(*ifs, storageIt->first, storageIt->second.name,
710                                 std::string(storageIt->second.name), std::move(mountNorm), bk, l);
711         err < 0) {
712         LOG(ERROR) << "Adding bind mount failed: " << -err;
713         return kInvalidStorageId;
714     }
715 
716     // Done here as well, all data structures are in good state.
717     (void)secondCleanupOnFailure.release();
718 
719     mountIt->second = std::move(ifs);
720     l.unlock();
721 
722     LOG(INFO) << "created storage " << mountId;
723     return mountId;
724 }
725 
createLinkedStorage(std::string_view mountPoint,StorageId linkedStorage,IncrementalService::CreateOptions options)726 StorageId IncrementalService::createLinkedStorage(std::string_view mountPoint,
727                                                   StorageId linkedStorage,
728                                                   IncrementalService::CreateOptions options) {
729     if (!isValidMountTarget(mountPoint)) {
730         LOG(ERROR) << "Mount point is invalid or missing";
731         return kInvalidStorageId;
732     }
733 
734     std::unique_lock l(mLock);
735     auto ifs = getIfsLocked(linkedStorage);
736     if (!ifs) {
737         LOG(ERROR) << "Ifs unavailable";
738         return kInvalidStorageId;
739     }
740 
741     const auto mountIt = getStorageSlotLocked();
742     const auto storageId = mountIt->first;
743     const auto storageIt = ifs->makeStorage(storageId);
744     if (storageIt == ifs->storages.end()) {
745         LOG(ERROR) << "Can't create a new storage";
746         mMounts.erase(mountIt);
747         return kInvalidStorageId;
748     }
749 
750     l.unlock();
751 
752     const auto bk =
753             (options & CreateOptions::PermanentBind) ? BindKind::Permanent : BindKind::Temporary;
754     if (auto err = addBindMount(*ifs, storageIt->first, storageIt->second.name,
755                                 std::string(storageIt->second.name), path::normalize(mountPoint),
756                                 bk, l);
757         err < 0) {
758         LOG(ERROR) << "bindMount failed with error: " << err;
759         (void)mIncFs->unlink(ifs->control, storageIt->second.name);
760         ifs->storages.erase(storageIt);
761         return kInvalidStorageId;
762     }
763 
764     mountIt->second = ifs;
765     return storageId;
766 }
767 
startLoading(StorageId storageId,content::pm::DataLoaderParamsParcel dataLoaderParams,DataLoaderStatusListener statusListener,const StorageHealthCheckParams & healthCheckParams,StorageHealthListener healthListener,std::vector<PerUidReadTimeouts> perUidReadTimeouts)768 bool IncrementalService::startLoading(StorageId storageId,
769                                       content::pm::DataLoaderParamsParcel dataLoaderParams,
770                                       DataLoaderStatusListener statusListener,
771                                       const StorageHealthCheckParams& healthCheckParams,
772                                       StorageHealthListener healthListener,
773                                       std::vector<PerUidReadTimeouts> perUidReadTimeouts) {
774     // Per Uid timeouts.
775     if (!perUidReadTimeouts.empty()) {
776         setUidReadTimeouts(storageId, std::move(perUidReadTimeouts));
777     }
778 
779     IfsMountPtr ifs;
780     DataLoaderStubPtr dataLoaderStub;
781 
782     // Re-initialize DataLoader.
783     {
784         ifs = getIfs(storageId);
785         if (!ifs) {
786             return false;
787         }
788 
789         std::unique_lock l(ifs->lock);
790         dataLoaderStub = std::exchange(ifs->dataLoaderStub, nullptr);
791     }
792 
793     if (dataLoaderStub) {
794         dataLoaderStub->cleanupResources();
795         dataLoaderStub = {};
796     }
797 
798     {
799         std::unique_lock l(ifs->lock);
800         if (ifs->dataLoaderStub) {
801             LOG(INFO) << "Skipped data loader stub creation because it already exists";
802             return false;
803         }
804 
805         prepareDataLoaderLocked(*ifs, std::move(dataLoaderParams), std::move(statusListener),
806                                 healthCheckParams, std::move(healthListener));
807         CHECK(ifs->dataLoaderStub);
808         dataLoaderStub = ifs->dataLoaderStub;
809 
810         // Disable long read timeouts for non-system dataloaders.
811         // To be re-enabled after installation is complete.
812         ifs->setReadTimeoutsRequested(dataLoaderStub->isSystemDataLoader() &&
813                                       getAlwaysEnableReadTimeoutsForSystemDataLoaders());
814         applyStorageParamsLocked(*ifs);
815     }
816 
817     if (dataLoaderStub->isSystemDataLoader() &&
818         !getEnforceReadLogsMaxIntervalForSystemDataLoaders()) {
819         // Readlogs from system dataloader (adb) can always be collected.
820         ifs->startLoadingTs = TimePoint::max();
821     } else {
822         // Assign time when installation wants the DL to start streaming.
823         const auto startLoadingTs = mClock->now();
824         ifs->startLoadingTs = startLoadingTs;
825         // Setup a callback to disable the readlogs after max interval.
826         addTimedJob(*mTimedQueue, storageId, getReadLogsMaxInterval(),
827                     [this, storageId, startLoadingTs]() {
828                         const auto ifs = getIfs(storageId);
829                         if (!ifs) {
830                             LOG(WARNING) << "Can't disable the readlogs, invalid storageId: "
831                                          << storageId;
832                             return;
833                         }
834                         std::unique_lock l(ifs->lock);
835                         if (ifs->startLoadingTs != startLoadingTs) {
836                             LOG(INFO) << "Can't disable the readlogs, timestamp mismatch (new "
837                                          "installation?): "
838                                       << storageId;
839                             return;
840                         }
841                         disableReadLogsLocked(*ifs);
842                     });
843     }
844 
845     return dataLoaderStub->requestStart();
846 }
847 
onInstallationComplete(StorageId storage)848 void IncrementalService::onInstallationComplete(StorageId storage) {
849     IfsMountPtr ifs = getIfs(storage);
850     if (!ifs) {
851         return;
852     }
853 
854     // Always enable long read timeouts after installation is complete.
855     std::unique_lock l(ifs->lock);
856     ifs->setReadTimeoutsRequested(true);
857     applyStorageParamsLocked(*ifs);
858 }
859 
findStorageLocked(std::string_view path) const860 IncrementalService::BindPathMap::const_iterator IncrementalService::findStorageLocked(
861         std::string_view path) const {
862     return findParentPath(mBindsByPath, path);
863 }
864 
findStorageId(std::string_view path) const865 StorageId IncrementalService::findStorageId(std::string_view path) const {
866     std::lock_guard l(mLock);
867     auto it = findStorageLocked(path);
868     if (it == mBindsByPath.end()) {
869         return kInvalidStorageId;
870     }
871     return it->second->second.storage;
872 }
873 
disallowReadLogs(StorageId storageId)874 void IncrementalService::disallowReadLogs(StorageId storageId) {
875     const auto ifs = getIfs(storageId);
876     if (!ifs) {
877         LOG(ERROR) << "disallowReadLogs failed, invalid storageId: " << storageId;
878         return;
879     }
880 
881     std::unique_lock l(ifs->lock);
882     if (!ifs->readLogsAllowed()) {
883         return;
884     }
885     ifs->disallowReadLogs();
886 
887     const auto metadata = constants().readLogsDisabledMarkerName;
888     if (auto err = mIncFs->makeFile(ifs->control,
889                                     path::join(ifs->root, constants().mount,
890                                                constants().readLogsDisabledMarkerName),
891                                     0777, idFromMetadata(metadata), {})) {
892         //{.metadata = {metadata.data(), (IncFsSize)metadata.size()}})) {
893         LOG(ERROR) << "Failed to make marker file for storageId: " << storageId;
894         return;
895     }
896 
897     disableReadLogsLocked(*ifs);
898 }
899 
setStorageParams(StorageId storageId,bool enableReadLogs)900 int IncrementalService::setStorageParams(StorageId storageId, bool enableReadLogs) {
901     const auto ifs = getIfs(storageId);
902     if (!ifs) {
903         LOG(ERROR) << "setStorageParams failed, invalid storageId: " << storageId;
904         return -EINVAL;
905     }
906 
907     std::string packageName;
908 
909     {
910         std::unique_lock l(ifs->lock);
911         if (!enableReadLogs) {
912             return disableReadLogsLocked(*ifs);
913         }
914 
915         if (!ifs->readLogsAllowed()) {
916             LOG(ERROR) << "enableReadLogs failed, readlogs disallowed for storageId: " << storageId;
917             return -EPERM;
918         }
919 
920         if (!ifs->dataLoaderStub) {
921             // This should never happen - only DL can call enableReadLogs.
922             LOG(ERROR) << "enableReadLogs failed: invalid state";
923             return -EPERM;
924         }
925 
926         // Check installation time.
927         const auto now = mClock->now();
928         const auto startLoadingTs = ifs->startLoadingTs;
929         if (startLoadingTs <= now && now - startLoadingTs > getReadLogsMaxInterval()) {
930             LOG(ERROR)
931                     << "enableReadLogs failed, readlogs can't be enabled at this time, storageId: "
932                     << storageId;
933             return -EPERM;
934         }
935 
936         packageName = ifs->dataLoaderStub->params().packageName;
937         ifs->setReadLogsRequested(true);
938     }
939 
940     // Check loader usage stats permission and apop.
941     if (auto status =
942                 mAppOpsManager->checkPermission(kLoaderUsageStats, kOpUsage, packageName.c_str());
943         !status.isOk()) {
944         LOG(ERROR) << " Permission: " << kLoaderUsageStats
945                    << " check failed: " << status.toString8();
946         return fromBinderStatus(status);
947     }
948 
949     // Check multiuser permission.
950     if (auto status =
951                 mAppOpsManager->checkPermission(kInteractAcrossUsers, nullptr, packageName.c_str());
952         !status.isOk()) {
953         LOG(ERROR) << " Permission: " << kInteractAcrossUsers
954                    << " check failed: " << status.toString8();
955         return fromBinderStatus(status);
956     }
957 
958     {
959         std::unique_lock l(ifs->lock);
960         if (!ifs->readLogsRequested()) {
961             return 0;
962         }
963         if (auto status = applyStorageParamsLocked(*ifs); status != 0) {
964             return status;
965         }
966     }
967 
968     registerAppOpsCallback(packageName);
969 
970     return 0;
971 }
972 
disableReadLogsLocked(IncFsMount & ifs)973 int IncrementalService::disableReadLogsLocked(IncFsMount& ifs) {
974     ifs.setReadLogsRequested(false);
975     return applyStorageParamsLocked(ifs);
976 }
977 
applyStorageParamsLocked(IncFsMount & ifs)978 int IncrementalService::applyStorageParamsLocked(IncFsMount& ifs) {
979     os::incremental::IncrementalFileSystemControlParcel control;
980     control.cmd.reset(dup(ifs.control.cmd()));
981     control.pendingReads.reset(dup(ifs.control.pendingReads()));
982     auto logsFd = ifs.control.logs();
983     if (logsFd >= 0) {
984         control.log.reset(dup(logsFd));
985     }
986 
987     bool enableReadLogs = ifs.readLogsRequested();
988     bool enableReadTimeouts = ifs.readTimeoutsRequested();
989 
990     std::lock_guard l(mMountOperationLock);
991     auto status = mVold->setIncFsMountOptions(control, enableReadLogs, enableReadTimeouts,
992                                               ifs.metricsKey);
993     if (status.isOk()) {
994         // Store states.
995         ifs.setReadLogsEnabled(enableReadLogs);
996         ifs.setReadTimeoutsEnabled(enableReadTimeouts);
997     } else {
998         LOG(ERROR) << "applyStorageParams failed: " << status.toString8();
999     }
1000     return status.isOk() ? 0 : fromBinderStatus(status);
1001 }
1002 
deleteStorage(StorageId storageId)1003 void IncrementalService::deleteStorage(StorageId storageId) {
1004     const auto ifs = getIfs(storageId);
1005     if (!ifs) {
1006         return;
1007     }
1008     deleteStorage(*ifs);
1009 }
1010 
deleteStorage(IncrementalService::IncFsMount & ifs)1011 void IncrementalService::deleteStorage(IncrementalService::IncFsMount& ifs) {
1012     std::unique_lock l(ifs.lock);
1013     deleteStorageLocked(ifs, std::move(l));
1014 }
1015 
deleteStorageLocked(IncrementalService::IncFsMount & ifs,std::unique_lock<std::mutex> && ifsLock)1016 void IncrementalService::deleteStorageLocked(IncrementalService::IncFsMount& ifs,
1017                                              std::unique_lock<std::mutex>&& ifsLock) {
1018     const auto storages = std::move(ifs.storages);
1019     // Don't move the bind points out: Ifs's dtor will use them to unmount everything.
1020     const auto bindPoints = ifs.bindPoints;
1021     ifsLock.unlock();
1022 
1023     std::lock_guard l(mLock);
1024     for (auto&& [id, _] : storages) {
1025         if (id != ifs.mountId) {
1026             mMounts.erase(id);
1027         }
1028     }
1029     for (auto&& [path, _] : bindPoints) {
1030         mBindsByPath.erase(path);
1031     }
1032     mMounts.erase(ifs.mountId);
1033 }
1034 
openStorage(std::string_view pathInMount)1035 StorageId IncrementalService::openStorage(std::string_view pathInMount) {
1036     if (!path::isAbsolute(pathInMount)) {
1037         return kInvalidStorageId;
1038     }
1039 
1040     return findStorageId(path::normalize(pathInMount));
1041 }
1042 
getIfs(StorageId storage) const1043 IncrementalService::IfsMountPtr IncrementalService::getIfs(StorageId storage) const {
1044     std::lock_guard l(mLock);
1045     return getIfsLocked(storage);
1046 }
1047 
getIfsLocked(StorageId storage) const1048 const IncrementalService::IfsMountPtr& IncrementalService::getIfsLocked(StorageId storage) const {
1049     auto it = mMounts.find(storage);
1050     if (it == mMounts.end()) {
1051         static const base::NoDestructor<IfsMountPtr> kEmpty{};
1052         return *kEmpty;
1053     }
1054     return it->second;
1055 }
1056 
bind(StorageId storage,std::string_view source,std::string_view target,BindKind kind)1057 int IncrementalService::bind(StorageId storage, std::string_view source, std::string_view target,
1058                              BindKind kind) {
1059     if (!isValidMountTarget(target)) {
1060         LOG(ERROR) << __func__ << ": not a valid bind target " << target;
1061         return -EINVAL;
1062     }
1063 
1064     const auto ifs = getIfs(storage);
1065     if (!ifs) {
1066         LOG(ERROR) << __func__ << ": no ifs object for storage " << storage;
1067         return -EINVAL;
1068     }
1069 
1070     std::unique_lock l(ifs->lock);
1071     const auto storageInfo = ifs->storages.find(storage);
1072     if (storageInfo == ifs->storages.end()) {
1073         LOG(ERROR) << "no storage";
1074         return -EINVAL;
1075     }
1076     std::string normSource = normalizePathToStorageLocked(*ifs, storageInfo, source);
1077     if (normSource.empty()) {
1078         LOG(ERROR) << "invalid source path";
1079         return -EINVAL;
1080     }
1081     l.unlock();
1082     std::unique_lock l2(mLock, std::defer_lock);
1083     return addBindMount(*ifs, storage, storageInfo->second.name, std::move(normSource),
1084                         path::normalize(target), kind, l2);
1085 }
1086 
unbind(StorageId storage,std::string_view target)1087 int IncrementalService::unbind(StorageId storage, std::string_view target) {
1088     if (!path::isAbsolute(target)) {
1089         return -EINVAL;
1090     }
1091 
1092     LOG(INFO) << "Removing bind point " << target << " for storage " << storage;
1093 
1094     // Here we should only look up by the exact target, not by a subdirectory of any existing mount,
1095     // otherwise there's a chance to unmount something completely unrelated
1096     const auto norm = path::normalize(target);
1097     std::unique_lock l(mLock);
1098     const auto storageIt = mBindsByPath.find(norm);
1099     if (storageIt == mBindsByPath.end() || storageIt->second->second.storage != storage) {
1100         return -EINVAL;
1101     }
1102     const auto bindIt = storageIt->second;
1103     const auto storageId = bindIt->second.storage;
1104     const auto ifs = getIfsLocked(storageId);
1105     if (!ifs) {
1106         LOG(ERROR) << "Internal error: storageId " << storageId << " for bound path " << target
1107                    << " is missing";
1108         return -EFAULT;
1109     }
1110     mBindsByPath.erase(storageIt);
1111     l.unlock();
1112 
1113     mVold->unmountIncFs(bindIt->first);
1114     std::unique_lock l2(ifs->lock);
1115     if (ifs->bindPoints.size() <= 1) {
1116         ifs->bindPoints.clear();
1117         deleteStorageLocked(*ifs, std::move(l2));
1118     } else {
1119         const std::string savedFile = std::move(bindIt->second.savedFilename);
1120         ifs->bindPoints.erase(bindIt);
1121         l2.unlock();
1122         if (!savedFile.empty()) {
1123             mIncFs->unlink(ifs->control, path::join(ifs->root, constants().mount, savedFile));
1124         }
1125     }
1126 
1127     return 0;
1128 }
1129 
normalizePathToStorageLocked(const IncFsMount & incfs,IncFsMount::StorageMap::const_iterator storageIt,std::string_view path) const1130 std::string IncrementalService::normalizePathToStorageLocked(
1131         const IncFsMount& incfs, IncFsMount::StorageMap::const_iterator storageIt,
1132         std::string_view path) const {
1133     if (!path::isAbsolute(path)) {
1134         return path::normalize(path::join(storageIt->second.name, path));
1135     }
1136     auto normPath = path::normalize(path);
1137     if (path::startsWith(normPath, storageIt->second.name)) {
1138         return normPath;
1139     }
1140     // not that easy: need to find if any of the bind points match
1141     const auto bindIt = findParentPath(incfs.bindPoints, normPath);
1142     if (bindIt == incfs.bindPoints.end()) {
1143         return {};
1144     }
1145     return path::join(bindIt->second.sourceDir, path::relativize(bindIt->first, normPath));
1146 }
1147 
normalizePathToStorage(const IncFsMount & ifs,StorageId storage,std::string_view path) const1148 std::string IncrementalService::normalizePathToStorage(const IncFsMount& ifs, StorageId storage,
1149                                                        std::string_view path) const {
1150     std::unique_lock l(ifs.lock);
1151     const auto storageInfo = ifs.storages.find(storage);
1152     if (storageInfo == ifs.storages.end()) {
1153         return {};
1154     }
1155     return normalizePathToStorageLocked(ifs, storageInfo, path);
1156 }
1157 
makeFile(StorageId storage,std::string_view path,int mode,FileId id,incfs::NewFileParams params,std::span<const uint8_t> data)1158 int IncrementalService::makeFile(StorageId storage, std::string_view path, int mode, FileId id,
1159                                  incfs::NewFileParams params, std::span<const uint8_t> data) {
1160     const auto ifs = getIfs(storage);
1161     if (!ifs) {
1162         return -EINVAL;
1163     }
1164     if (data.size() > params.size) {
1165         LOG(ERROR) << "Bad data size - bigger than file size";
1166         return -EINVAL;
1167     }
1168     if (!data.empty() && data.size() != params.size) {
1169         // Writing a page is an irreversible operation, and it can't be updated with additional
1170         // data later. Check that the last written page is complete, or we may break the file.
1171         if (!isPageAligned(data.size())) {
1172             LOG(ERROR) << "Bad data size - tried to write half a page?";
1173             return -EINVAL;
1174         }
1175     }
1176     const std::string normPath = normalizePathToStorage(*ifs, storage, path);
1177     if (normPath.empty()) {
1178         LOG(ERROR) << "Internal error: storageId " << storage << " failed to normalize: " << path;
1179         return -EINVAL;
1180     }
1181     if (auto err = mIncFs->makeFile(ifs->control, normPath, mode, id, params); err) {
1182         LOG(ERROR) << "Internal error: storageId " << storage << " failed to makeFile [" << normPath
1183                    << "]: " << err;
1184         return err;
1185     }
1186     if (params.size > 0) {
1187         if (auto err = mIncFs->reserveSpace(ifs->control, id, params.size)) {
1188             if (err != -EOPNOTSUPP) {
1189                 LOG(ERROR) << "Failed to reserve space for a new file: " << err;
1190                 (void)mIncFs->unlink(ifs->control, normPath);
1191                 return err;
1192             } else {
1193                 LOG(WARNING) << "Reserving space for backing file isn't supported, "
1194                                 "may run out of disk later";
1195             }
1196         }
1197         if (!data.empty()) {
1198             if (auto err = setFileContent(ifs, id, path, data); err) {
1199                 (void)mIncFs->unlink(ifs->control, normPath);
1200                 return err;
1201             }
1202         }
1203     }
1204     return 0;
1205 }
1206 
makeDir(StorageId storageId,std::string_view path,int mode)1207 int IncrementalService::makeDir(StorageId storageId, std::string_view path, int mode) {
1208     if (auto ifs = getIfs(storageId)) {
1209         std::string normPath = normalizePathToStorage(*ifs, storageId, path);
1210         if (normPath.empty()) {
1211             return -EINVAL;
1212         }
1213         return mIncFs->makeDir(ifs->control, normPath, mode);
1214     }
1215     return -EINVAL;
1216 }
1217 
makeDirs(StorageId storageId,std::string_view path,int mode)1218 int IncrementalService::makeDirs(StorageId storageId, std::string_view path, int mode) {
1219     const auto ifs = getIfs(storageId);
1220     if (!ifs) {
1221         return -EINVAL;
1222     }
1223     return makeDirs(*ifs, storageId, path, mode);
1224 }
1225 
makeDirs(const IncFsMount & ifs,StorageId storageId,std::string_view path,int mode)1226 int IncrementalService::makeDirs(const IncFsMount& ifs, StorageId storageId, std::string_view path,
1227                                  int mode) {
1228     std::string normPath = normalizePathToStorage(ifs, storageId, path);
1229     if (normPath.empty()) {
1230         return -EINVAL;
1231     }
1232     return mIncFs->makeDirs(ifs.control, normPath, mode);
1233 }
1234 
link(StorageId sourceStorageId,std::string_view oldPath,StorageId destStorageId,std::string_view newPath)1235 int IncrementalService::link(StorageId sourceStorageId, std::string_view oldPath,
1236                              StorageId destStorageId, std::string_view newPath) {
1237     std::unique_lock l(mLock);
1238     auto ifsSrc = getIfsLocked(sourceStorageId);
1239     if (!ifsSrc) {
1240         return -EINVAL;
1241     }
1242     if (sourceStorageId != destStorageId && getIfsLocked(destStorageId) != ifsSrc) {
1243         return -EINVAL;
1244     }
1245     l.unlock();
1246     std::string normOldPath = normalizePathToStorage(*ifsSrc, sourceStorageId, oldPath);
1247     std::string normNewPath = normalizePathToStorage(*ifsSrc, destStorageId, newPath);
1248     if (normOldPath.empty() || normNewPath.empty()) {
1249         LOG(ERROR) << "Invalid paths in link(): " << normOldPath << " | " << normNewPath;
1250         return -EINVAL;
1251     }
1252     if (auto err = mIncFs->link(ifsSrc->control, normOldPath, normNewPath); err < 0) {
1253         PLOG(ERROR) << "Failed to link " << oldPath << "[" << normOldPath << "]"
1254                     << " to " << newPath << "[" << normNewPath << "]";
1255         return err;
1256     }
1257     return 0;
1258 }
1259 
unlink(StorageId storage,std::string_view path)1260 int IncrementalService::unlink(StorageId storage, std::string_view path) {
1261     if (auto ifs = getIfs(storage)) {
1262         std::string normOldPath = normalizePathToStorage(*ifs, storage, path);
1263         return mIncFs->unlink(ifs->control, normOldPath);
1264     }
1265     return -EINVAL;
1266 }
1267 
addBindMount(IncFsMount & ifs,StorageId storage,std::string_view storageRoot,std::string && source,std::string && target,BindKind kind,std::unique_lock<std::mutex> & mainLock)1268 int IncrementalService::addBindMount(IncFsMount& ifs, StorageId storage,
1269                                      std::string_view storageRoot, std::string&& source,
1270                                      std::string&& target, BindKind kind,
1271                                      std::unique_lock<std::mutex>& mainLock) {
1272     if (!isValidMountTarget(target)) {
1273         LOG(ERROR) << __func__ << ": invalid mount target " << target;
1274         return -EINVAL;
1275     }
1276 
1277     std::string mdFileName;
1278     std::string metadataFullPath;
1279     if (kind != BindKind::Temporary) {
1280         metadata::BindPoint bp;
1281         bp.set_storage_id(storage);
1282         bp.set_allocated_dest_path(&target);
1283         bp.set_allocated_source_subdir(&source);
1284         const auto metadata = bp.SerializeAsString();
1285         bp.release_dest_path();
1286         bp.release_source_subdir();
1287         mdFileName = makeBindMdName();
1288         metadataFullPath = path::join(ifs.root, constants().mount, mdFileName);
1289         auto node = mIncFs->makeFile(ifs.control, metadataFullPath, 0444, idFromMetadata(metadata),
1290                                      {.metadata = {metadata.data(), (IncFsSize)metadata.size()}});
1291         if (node) {
1292             LOG(ERROR) << __func__ << ": couldn't create a mount node " << mdFileName;
1293             return int(node);
1294         }
1295     }
1296 
1297     const auto res = addBindMountWithMd(ifs, storage, std::move(mdFileName), std::move(source),
1298                                         std::move(target), kind, mainLock);
1299     if (res) {
1300         mIncFs->unlink(ifs.control, metadataFullPath);
1301     }
1302     return res;
1303 }
1304 
addBindMountWithMd(IncrementalService::IncFsMount & ifs,StorageId storage,std::string && metadataName,std::string && source,std::string && target,BindKind kind,std::unique_lock<std::mutex> & mainLock)1305 int IncrementalService::addBindMountWithMd(IncrementalService::IncFsMount& ifs, StorageId storage,
1306                                            std::string&& metadataName, std::string&& source,
1307                                            std::string&& target, BindKind kind,
1308                                            std::unique_lock<std::mutex>& mainLock) {
1309     {
1310         std::lock_guard l(mMountOperationLock);
1311         const auto status = mVold->bindMount(source, target);
1312         if (!status.isOk()) {
1313             LOG(ERROR) << "Calling Vold::bindMount() failed: " << status.toString8();
1314             return status.exceptionCode() == binder::Status::EX_SERVICE_SPECIFIC
1315                     ? status.serviceSpecificErrorCode() > 0 ? -status.serviceSpecificErrorCode()
1316                                                             : status.serviceSpecificErrorCode() == 0
1317                                     ? -EFAULT
1318                                     : status.serviceSpecificErrorCode()
1319                     : -EIO;
1320         }
1321     }
1322 
1323     if (!mainLock.owns_lock()) {
1324         mainLock.lock();
1325     }
1326     std::lock_guard l(ifs.lock);
1327     addBindMountRecordLocked(ifs, storage, std::move(metadataName), std::move(source),
1328                              std::move(target), kind);
1329     return 0;
1330 }
1331 
addBindMountRecordLocked(IncFsMount & ifs,StorageId storage,std::string && metadataName,std::string && source,std::string && target,BindKind kind)1332 void IncrementalService::addBindMountRecordLocked(IncFsMount& ifs, StorageId storage,
1333                                                   std::string&& metadataName, std::string&& source,
1334                                                   std::string&& target, BindKind kind) {
1335     const auto [it, _] =
1336             ifs.bindPoints.insert_or_assign(target,
1337                                             IncFsMount::Bind{storage, std::move(metadataName),
1338                                                              std::move(source), kind});
1339     mBindsByPath[std::move(target)] = it;
1340 }
1341 
getMetadata(StorageId storage,std::string_view path) const1342 RawMetadata IncrementalService::getMetadata(StorageId storage, std::string_view path) const {
1343     const auto ifs = getIfs(storage);
1344     if (!ifs) {
1345         return {};
1346     }
1347     const auto normPath = normalizePathToStorage(*ifs, storage, path);
1348     if (normPath.empty()) {
1349         return {};
1350     }
1351     return mIncFs->getMetadata(ifs->control, normPath);
1352 }
1353 
getMetadata(StorageId storage,FileId node) const1354 RawMetadata IncrementalService::getMetadata(StorageId storage, FileId node) const {
1355     const auto ifs = getIfs(storage);
1356     if (!ifs) {
1357         return {};
1358     }
1359     return mIncFs->getMetadata(ifs->control, node);
1360 }
1361 
setUidReadTimeouts(StorageId storage,std::vector<PerUidReadTimeouts> && perUidReadTimeouts)1362 void IncrementalService::setUidReadTimeouts(StorageId storage,
1363                                             std::vector<PerUidReadTimeouts>&& perUidReadTimeouts) {
1364     using microseconds = std::chrono::microseconds;
1365     using milliseconds = std::chrono::milliseconds;
1366 
1367     auto maxPendingTimeUs = microseconds(0);
1368     for (const auto& timeouts : perUidReadTimeouts) {
1369         maxPendingTimeUs = std::max(maxPendingTimeUs, microseconds(timeouts.maxPendingTimeUs));
1370     }
1371     if (maxPendingTimeUs < Constants::minPerUidTimeout) {
1372         LOG(ERROR) << "Skip setting read timeouts (maxPendingTime < Constants::minPerUidTimeout): "
1373                    << duration_cast<milliseconds>(maxPendingTimeUs).count() << "ms < "
1374                    << Constants::minPerUidTimeout.count() << "ms";
1375         return;
1376     }
1377 
1378     const auto ifs = getIfs(storage);
1379     if (!ifs) {
1380         LOG(ERROR) << "Setting read timeouts failed: invalid storage id: " << storage;
1381         return;
1382     }
1383 
1384     if (auto err = mIncFs->setUidReadTimeouts(ifs->control, perUidReadTimeouts); err < 0) {
1385         LOG(ERROR) << "Setting read timeouts failed: " << -err;
1386         return;
1387     }
1388 
1389     const auto timeout = Clock::now() + maxPendingTimeUs - Constants::perUidTimeoutOffset;
1390     addIfsStateCallback(storage, [this, timeout](StorageId storageId, IfsState state) -> bool {
1391         if (checkUidReadTimeouts(storageId, state, timeout)) {
1392             return true;
1393         }
1394         clearUidReadTimeouts(storageId);
1395         return false;
1396     });
1397 }
1398 
clearUidReadTimeouts(StorageId storage)1399 void IncrementalService::clearUidReadTimeouts(StorageId storage) {
1400     const auto ifs = getIfs(storage);
1401     if (!ifs) {
1402         return;
1403     }
1404     mIncFs->setUidReadTimeouts(ifs->control, {});
1405 }
1406 
checkUidReadTimeouts(StorageId storage,IfsState state,Clock::time_point timeLimit)1407 bool IncrementalService::checkUidReadTimeouts(StorageId storage, IfsState state,
1408                                               Clock::time_point timeLimit) {
1409     if (Clock::now() >= timeLimit) {
1410         // Reached maximum timeout.
1411         return false;
1412     }
1413     if (state.error) {
1414         // Something is wrong, abort.
1415         return false;
1416     }
1417 
1418     // Still loading?
1419     if (state.fullyLoaded && !state.readLogsEnabled) {
1420         return false;
1421     }
1422 
1423     const auto timeLeft = timeLimit - Clock::now();
1424     if (timeLeft < Constants::progressUpdateInterval) {
1425         // Don't bother.
1426         return false;
1427     }
1428 
1429     return true;
1430 }
1431 
adoptMountedInstances()1432 std::unordered_set<std::string_view> IncrementalService::adoptMountedInstances() {
1433     std::unordered_set<std::string_view> mountedRootNames;
1434     mIncFs->listExistingMounts([this, &mountedRootNames](auto root, auto backingDir, auto binds) {
1435         LOG(INFO) << "Existing mount: " << backingDir << "->" << root;
1436         for (auto [source, target] : binds) {
1437             LOG(INFO) << "  bind: '" << source << "'->'" << target << "'";
1438             LOG(INFO) << "         " << path::join(root, source);
1439         }
1440 
1441         // Ensure it's a kind of a mount that's managed by IncrementalService
1442         if (path::basename(root) != constants().mount ||
1443             path::basename(backingDir) != constants().backing) {
1444             return;
1445         }
1446         const auto expectedRoot = path::dirname(root);
1447         if (path::dirname(backingDir) != expectedRoot) {
1448             return;
1449         }
1450         if (path::dirname(expectedRoot) != mIncrementalDir) {
1451             return;
1452         }
1453         if (!path::basename(expectedRoot).starts_with(constants().mountKeyPrefix)) {
1454             return;
1455         }
1456 
1457         LOG(INFO) << "Looks like an IncrementalService-owned: " << expectedRoot;
1458 
1459         // make sure we clean up the mount if it happens to be a bad one.
1460         // Note: unmounting needs to run first, so the cleanup object is created _last_.
1461         auto cleanupFiles = makeCleanup([&]() {
1462             LOG(INFO) << "Failed to adopt existing mount, deleting files: " << expectedRoot;
1463             IncFsMount::cleanupFilesystem(expectedRoot);
1464         });
1465         auto cleanupMounts = makeCleanup([&]() {
1466             LOG(INFO) << "Failed to adopt existing mount, cleaning up: " << expectedRoot;
1467             for (auto&& [_, target] : binds) {
1468                 mVold->unmountIncFs(std::string(target));
1469             }
1470             mVold->unmountIncFs(std::string(root));
1471         });
1472 
1473         auto control = mIncFs->openMount(root);
1474         if (!control) {
1475             LOG(INFO) << "failed to open mount " << root;
1476             return;
1477         }
1478 
1479         auto mountRecord =
1480                 parseFromIncfs<metadata::Mount>(mIncFs.get(), control,
1481                                                 path::join(root, constants().infoMdName));
1482         if (!mountRecord.has_loader() || !mountRecord.has_storage()) {
1483             LOG(ERROR) << "Bad mount metadata in mount at " << expectedRoot;
1484             return;
1485         }
1486 
1487         auto mountId = mountRecord.storage().id();
1488         mNextId = std::max(mNextId, mountId + 1);
1489 
1490         DataLoaderParamsParcel dataLoaderParams;
1491         {
1492             const auto& loader = mountRecord.loader();
1493             dataLoaderParams.type = (content::pm::DataLoaderType)loader.type();
1494             dataLoaderParams.packageName = loader.package_name();
1495             dataLoaderParams.className = loader.class_name();
1496             dataLoaderParams.arguments = loader.arguments();
1497         }
1498 
1499         // Not way to obtain a real sysfs key at this point - metrics will stop working after "soft"
1500         // reboot.
1501         std::string metricsKey{};
1502         auto ifs = std::make_shared<IncFsMount>(std::string(expectedRoot), std::move(metricsKey),
1503                                                 mountId, std::move(control), *this);
1504         (void)cleanupFiles.release(); // ifs will take care of that now
1505 
1506         // Check if marker file present.
1507         if (checkReadLogsDisabledMarker(root)) {
1508             ifs->disallowReadLogs();
1509         }
1510 
1511         std::vector<std::pair<std::string, metadata::BindPoint>> permanentBindPoints;
1512         auto d = openDir(root);
1513         while (auto e = ::readdir(d.get())) {
1514             if (e->d_type == DT_REG) {
1515                 auto name = std::string_view(e->d_name);
1516                 if (name.starts_with(constants().mountpointMdPrefix)) {
1517                     permanentBindPoints
1518                             .emplace_back(name,
1519                                           parseFromIncfs<metadata::BindPoint>(mIncFs.get(),
1520                                                                               ifs->control,
1521                                                                               path::join(root,
1522                                                                                          name)));
1523                     if (permanentBindPoints.back().second.dest_path().empty() ||
1524                         permanentBindPoints.back().second.source_subdir().empty()) {
1525                         permanentBindPoints.pop_back();
1526                         mIncFs->unlink(ifs->control, path::join(root, name));
1527                     } else {
1528                         LOG(INFO) << "Permanent bind record: '"
1529                                   << permanentBindPoints.back().second.source_subdir() << "'->'"
1530                                   << permanentBindPoints.back().second.dest_path() << "'";
1531                     }
1532                 }
1533             } else if (e->d_type == DT_DIR) {
1534                 if (e->d_name == "."sv || e->d_name == ".."sv) {
1535                     continue;
1536                 }
1537                 auto name = std::string_view(e->d_name);
1538                 if (name.starts_with(constants().storagePrefix)) {
1539                     int storageId;
1540                     const auto res =
1541                             std::from_chars(name.data() + constants().storagePrefix.size() + 1,
1542                                             name.data() + name.size(), storageId);
1543                     if (res.ec != std::errc{} || *res.ptr != '_') {
1544                         LOG(WARNING) << "Ignoring storage with invalid name '" << name
1545                                      << "' for mount " << expectedRoot;
1546                         continue;
1547                     }
1548                     auto [_, inserted] = mMounts.try_emplace(storageId, ifs);
1549                     if (!inserted) {
1550                         LOG(WARNING) << "Ignoring storage with duplicate id " << storageId
1551                                      << " for mount " << expectedRoot;
1552                         continue;
1553                     }
1554                     ifs->storages.insert_or_assign(storageId,
1555                                                    IncFsMount::Storage{path::join(root, name)});
1556                     mNextId = std::max(mNextId, storageId + 1);
1557                 }
1558             }
1559         }
1560 
1561         if (ifs->storages.empty()) {
1562             LOG(WARNING) << "No valid storages in mount " << root;
1563             return;
1564         }
1565 
1566         // now match the mounted directories with what we expect to have in the metadata
1567         {
1568             std::unique_lock l(mLock, std::defer_lock);
1569             for (auto&& [metadataFile, bindRecord] : permanentBindPoints) {
1570                 auto mountedIt = std::find_if(binds.begin(), binds.end(),
1571                                               [&, bindRecord = bindRecord](auto&& bind) {
1572                                                   return bind.second == bindRecord.dest_path() &&
1573                                                           path::join(root, bind.first) ==
1574                                                           bindRecord.source_subdir();
1575                                               });
1576                 if (mountedIt != binds.end()) {
1577                     LOG(INFO) << "Matched permanent bound " << bindRecord.source_subdir()
1578                               << " to mount " << mountedIt->first;
1579                     addBindMountRecordLocked(*ifs, bindRecord.storage_id(), std::move(metadataFile),
1580                                              std::move(*bindRecord.mutable_source_subdir()),
1581                                              std::move(*bindRecord.mutable_dest_path()),
1582                                              BindKind::Permanent);
1583                     if (mountedIt != binds.end() - 1) {
1584                         std::iter_swap(mountedIt, binds.end() - 1);
1585                     }
1586                     binds = binds.first(binds.size() - 1);
1587                 } else {
1588                     LOG(INFO) << "Didn't match permanent bound " << bindRecord.source_subdir()
1589                               << ", mounting";
1590                     // doesn't exist - try mounting back
1591                     if (addBindMountWithMd(*ifs, bindRecord.storage_id(), std::move(metadataFile),
1592                                            std::move(*bindRecord.mutable_source_subdir()),
1593                                            std::move(*bindRecord.mutable_dest_path()),
1594                                            BindKind::Permanent, l)) {
1595                         mIncFs->unlink(ifs->control, metadataFile);
1596                     }
1597                 }
1598             }
1599         }
1600 
1601         // if anything stays in |binds| those are probably temporary binds; system restarted since
1602         // they were mounted - so let's unmount them all.
1603         for (auto&& [source, target] : binds) {
1604             if (source.empty()) {
1605                 continue;
1606             }
1607             mVold->unmountIncFs(std::string(target));
1608         }
1609         (void)cleanupMounts.release(); // ifs now manages everything
1610 
1611         if (ifs->bindPoints.empty()) {
1612             LOG(WARNING) << "No valid bind points for mount " << expectedRoot;
1613             deleteStorage(*ifs);
1614             return;
1615         }
1616 
1617         prepareDataLoaderLocked(*ifs, std::move(dataLoaderParams));
1618         CHECK(ifs->dataLoaderStub);
1619 
1620         mountedRootNames.insert(path::basename(ifs->root));
1621 
1622         // not locking here at all: we're still in the constructor, no other calls can happen
1623         mMounts[ifs->mountId] = std::move(ifs);
1624     });
1625 
1626     return mountedRootNames;
1627 }
1628 
mountExistingImages(const std::unordered_set<std::string_view> & mountedRootNames)1629 void IncrementalService::mountExistingImages(
1630         const std::unordered_set<std::string_view>& mountedRootNames) {
1631     auto dir = openDir(mIncrementalDir);
1632     if (!dir) {
1633         PLOG(WARNING) << "Couldn't open the root incremental dir " << mIncrementalDir;
1634         return;
1635     }
1636     while (auto entry = ::readdir(dir.get())) {
1637         if (entry->d_type != DT_DIR) {
1638             continue;
1639         }
1640         std::string_view name = entry->d_name;
1641         if (!name.starts_with(constants().mountKeyPrefix)) {
1642             continue;
1643         }
1644         if (mountedRootNames.find(name) != mountedRootNames.end()) {
1645             continue;
1646         }
1647         const auto root = path::join(mIncrementalDir, name);
1648         if (!mountExistingImage(root)) {
1649             IncFsMount::cleanupFilesystem(root);
1650         }
1651     }
1652 }
1653 
mountExistingImage(std::string_view root)1654 bool IncrementalService::mountExistingImage(std::string_view root) {
1655     auto mountTarget = path::join(root, constants().mount);
1656     const auto backing = path::join(root, constants().backing);
1657     std::string mountKey(path::basename(path::dirname(mountTarget)));
1658 
1659     IncrementalFileSystemControlParcel controlParcel;
1660     auto metricsKey = makeUniqueName(mountKey);
1661     auto status = mVold->mountIncFs(backing, mountTarget, 0, metricsKey, &controlParcel);
1662     if (!status.isOk()) {
1663         LOG(ERROR) << "Vold::mountIncFs() failed: " << status.toString8();
1664         return false;
1665     }
1666 
1667     int cmd = controlParcel.cmd.release().release();
1668     int pendingReads = controlParcel.pendingReads.release().release();
1669     int logs = controlParcel.log.release().release();
1670     int blocksWritten =
1671             controlParcel.blocksWritten ? controlParcel.blocksWritten->release().release() : -1;
1672     IncFsMount::Control control = mIncFs->createControl(cmd, pendingReads, logs, blocksWritten);
1673 
1674     auto ifs = std::make_shared<IncFsMount>(std::string(root), std::move(metricsKey), -1,
1675                                             std::move(control), *this);
1676 
1677     auto mount = parseFromIncfs<metadata::Mount>(mIncFs.get(), ifs->control,
1678                                                  path::join(mountTarget, constants().infoMdName));
1679     if (!mount.has_loader() || !mount.has_storage()) {
1680         LOG(ERROR) << "Bad mount metadata in mount at " << root;
1681         return false;
1682     }
1683 
1684     ifs->mountId = mount.storage().id();
1685     mNextId = std::max(mNextId, ifs->mountId + 1);
1686 
1687     // Check if marker file present.
1688     if (checkReadLogsDisabledMarker(mountTarget)) {
1689         ifs->disallowReadLogs();
1690     }
1691 
1692     // DataLoader params
1693     DataLoaderParamsParcel dataLoaderParams;
1694     {
1695         const auto& loader = mount.loader();
1696         dataLoaderParams.type = (content::pm::DataLoaderType)loader.type();
1697         dataLoaderParams.packageName = loader.package_name();
1698         dataLoaderParams.className = loader.class_name();
1699         dataLoaderParams.arguments = loader.arguments();
1700     }
1701 
1702     prepareDataLoaderLocked(*ifs, std::move(dataLoaderParams));
1703     CHECK(ifs->dataLoaderStub);
1704 
1705     std::vector<std::pair<std::string, metadata::BindPoint>> bindPoints;
1706     auto d = openDir(mountTarget);
1707     while (auto e = ::readdir(d.get())) {
1708         if (e->d_type == DT_REG) {
1709             auto name = std::string_view(e->d_name);
1710             if (name.starts_with(constants().mountpointMdPrefix)) {
1711                 bindPoints.emplace_back(name,
1712                                         parseFromIncfs<metadata::BindPoint>(mIncFs.get(),
1713                                                                             ifs->control,
1714                                                                             path::join(mountTarget,
1715                                                                                        name)));
1716                 if (bindPoints.back().second.dest_path().empty() ||
1717                     bindPoints.back().second.source_subdir().empty()) {
1718                     bindPoints.pop_back();
1719                     mIncFs->unlink(ifs->control, path::join(ifs->root, constants().mount, name));
1720                 }
1721             }
1722         } else if (e->d_type == DT_DIR) {
1723             if (e->d_name == "."sv || e->d_name == ".."sv) {
1724                 continue;
1725             }
1726             auto name = std::string_view(e->d_name);
1727             if (name.starts_with(constants().storagePrefix)) {
1728                 int storageId;
1729                 const auto res = std::from_chars(name.data() + constants().storagePrefix.size() + 1,
1730                                                  name.data() + name.size(), storageId);
1731                 if (res.ec != std::errc{} || *res.ptr != '_') {
1732                     LOG(WARNING) << "Ignoring storage with invalid name '" << name << "' for mount "
1733                                  << root;
1734                     continue;
1735                 }
1736                 auto [_, inserted] = mMounts.try_emplace(storageId, ifs);
1737                 if (!inserted) {
1738                     LOG(WARNING) << "Ignoring storage with duplicate id " << storageId
1739                                  << " for mount " << root;
1740                     continue;
1741                 }
1742                 ifs->storages.insert_or_assign(storageId,
1743                                                IncFsMount::Storage{
1744                                                        path::join(root, constants().mount, name)});
1745                 mNextId = std::max(mNextId, storageId + 1);
1746             }
1747         }
1748     }
1749 
1750     if (ifs->storages.empty()) {
1751         LOG(WARNING) << "No valid storages in mount " << root;
1752         return false;
1753     }
1754 
1755     int bindCount = 0;
1756     {
1757         std::unique_lock l(mLock, std::defer_lock);
1758         for (auto&& bp : bindPoints) {
1759             bindCount += !addBindMountWithMd(*ifs, bp.second.storage_id(), std::move(bp.first),
1760                                              std::move(*bp.second.mutable_source_subdir()),
1761                                              std::move(*bp.second.mutable_dest_path()),
1762                                              BindKind::Permanent, l);
1763         }
1764     }
1765 
1766     if (bindCount == 0) {
1767         LOG(WARNING) << "No valid bind points for mount " << root;
1768         deleteStorage(*ifs);
1769         return false;
1770     }
1771 
1772     // not locking here at all: we're still in the constructor, no other calls can happen
1773     mMounts[ifs->mountId] = std::move(ifs);
1774     return true;
1775 }
1776 
runCmdLooper()1777 void IncrementalService::runCmdLooper() {
1778     constexpr auto kTimeoutMsecs = -1;
1779     while (mRunning.load(std::memory_order_relaxed)) {
1780         mLooper->pollAll(kTimeoutMsecs);
1781     }
1782 }
1783 
trimReservedSpaceV1(const IncFsMount & ifs)1784 void IncrementalService::trimReservedSpaceV1(const IncFsMount& ifs) {
1785     mIncFs->forEachFile(ifs.control, [this](auto&& control, auto&& fileId) {
1786         if (mIncFs->isFileFullyLoaded(control, fileId) == incfs::LoadingState::Full) {
1787             mIncFs->reserveSpace(control, fileId, -1);
1788         }
1789         return true;
1790     });
1791 }
1792 
prepareDataLoaderLocked(IncFsMount & ifs,DataLoaderParamsParcel && params,DataLoaderStatusListener && statusListener,const StorageHealthCheckParams & healthCheckParams,StorageHealthListener && healthListener)1793 void IncrementalService::prepareDataLoaderLocked(IncFsMount& ifs, DataLoaderParamsParcel&& params,
1794                                                  DataLoaderStatusListener&& statusListener,
1795                                                  const StorageHealthCheckParams& healthCheckParams,
1796                                                  StorageHealthListener&& healthListener) {
1797     FileSystemControlParcel fsControlParcel;
1798     fsControlParcel.incremental = std::make_optional<IncrementalFileSystemControlParcel>();
1799     fsControlParcel.incremental->cmd.reset(dup(ifs.control.cmd()));
1800     fsControlParcel.incremental->pendingReads.reset(dup(ifs.control.pendingReads()));
1801     fsControlParcel.incremental->log.reset(dup(ifs.control.logs()));
1802     if (ifs.control.blocksWritten() >= 0) {
1803         fsControlParcel.incremental->blocksWritten.emplace(dup(ifs.control.blocksWritten()));
1804     }
1805     fsControlParcel.service = new IncrementalServiceConnector(*this, ifs.mountId);
1806 
1807     ifs.dataLoaderStub =
1808             new DataLoaderStub(*this, ifs.mountId, std::move(params), std::move(fsControlParcel),
1809                                std::move(statusListener), healthCheckParams,
1810                                std::move(healthListener), path::join(ifs.root, constants().mount));
1811 
1812     // pre-v2 IncFS doesn't do automatic reserved space trimming - need to run it manually
1813     if (!(mIncFs->features() & incfs::Features::v2)) {
1814         addIfsStateCallback(ifs.mountId, [this](StorageId storageId, IfsState state) -> bool {
1815             if (!state.fullyLoaded) {
1816                 return true;
1817             }
1818 
1819             const auto ifs = getIfs(storageId);
1820             if (!ifs) {
1821                 return false;
1822             }
1823             trimReservedSpaceV1(*ifs);
1824             return false;
1825         });
1826     }
1827 
1828     addIfsStateCallback(ifs.mountId, [this](StorageId storageId, IfsState state) -> bool {
1829         if (!state.fullyLoaded || state.readLogsEnabled) {
1830             return true;
1831         }
1832 
1833         DataLoaderStubPtr dataLoaderStub;
1834         {
1835             const auto ifs = getIfs(storageId);
1836             if (!ifs) {
1837                 return false;
1838             }
1839 
1840             std::unique_lock l(ifs->lock);
1841             dataLoaderStub = std::exchange(ifs->dataLoaderStub, nullptr);
1842         }
1843 
1844         if (dataLoaderStub) {
1845             dataLoaderStub->cleanupResources();
1846         }
1847 
1848         return false;
1849     });
1850 }
1851 
1852 template <class Duration>
castToMs(Duration d)1853 static constexpr auto castToMs(Duration d) {
1854     return std::chrono::duration_cast<std::chrono::milliseconds>(d);
1855 }
1856 
1857 // Extract lib files from zip, create new files in incfs and write data to them
1858 // Lib files should be placed next to the APK file in the following matter:
1859 // Example:
1860 // /path/to/base.apk
1861 // /path/to/lib/arm/first.so
1862 // /path/to/lib/arm/second.so
configureNativeBinaries(StorageId storage,std::string_view apkFullPath,std::string_view libDirRelativePath,std::string_view abi,bool extractNativeLibs)1863 bool IncrementalService::configureNativeBinaries(StorageId storage, std::string_view apkFullPath,
1864                                                  std::string_view libDirRelativePath,
1865                                                  std::string_view abi, bool extractNativeLibs) {
1866     auto start = Clock::now();
1867 
1868     const auto ifs = getIfs(storage);
1869     if (!ifs) {
1870         LOG(ERROR) << "Invalid storage " << storage;
1871         return false;
1872     }
1873 
1874     const auto targetLibPathRelativeToStorage =
1875             path::join(path::dirname(normalizePathToStorage(*ifs, storage, apkFullPath)),
1876                        libDirRelativePath);
1877 
1878     // First prepare target directories if they don't exist yet
1879     if (auto res = makeDirs(*ifs, storage, targetLibPathRelativeToStorage, 0755)) {
1880         LOG(ERROR) << "Failed to prepare target lib directory " << targetLibPathRelativeToStorage
1881                    << " errno: " << res;
1882         return false;
1883     }
1884 
1885     auto mkDirsTs = Clock::now();
1886     ZipArchiveHandle zipFileHandle;
1887     if (OpenArchive(path::c_str(apkFullPath), &zipFileHandle)) {
1888         LOG(ERROR) << "Failed to open zip file at " << apkFullPath;
1889         return false;
1890     }
1891 
1892     // Need a shared pointer: will be passing it into all unpacking jobs.
1893     std::shared_ptr<ZipArchive> zipFile(zipFileHandle, [](ZipArchiveHandle h) { CloseArchive(h); });
1894     void* cookie = nullptr;
1895     const auto libFilePrefix = path::join(constants().libDir, abi) += "/";
1896     if (StartIteration(zipFile.get(), &cookie, libFilePrefix, constants().libSuffix)) {
1897         LOG(ERROR) << "Failed to start zip iteration for " << apkFullPath;
1898         return false;
1899     }
1900     auto endIteration = [](void* cookie) { EndIteration(cookie); };
1901     auto iterationCleaner = std::unique_ptr<void, decltype(endIteration)>(cookie, endIteration);
1902 
1903     auto openZipTs = Clock::now();
1904 
1905     auto mapFiles = (mIncFs->features() & incfs::Features::v2);
1906     incfs::FileId sourceId;
1907     if (mapFiles) {
1908         sourceId = mIncFs->getFileId(ifs->control, apkFullPath);
1909         if (!incfs::isValidFileId(sourceId)) {
1910             LOG(WARNING) << "Error getting IncFS file ID for apk path '" << apkFullPath
1911                          << "', mapping disabled";
1912             mapFiles = false;
1913         }
1914     }
1915 
1916     std::vector<Job> jobQueue;
1917     ZipEntry entry;
1918     std::string_view fileName;
1919     while (!Next(cookie, &entry, &fileName)) {
1920         if (fileName.empty()) {
1921             continue;
1922         }
1923 
1924         const auto entryUncompressed = entry.method == kCompressStored;
1925         const auto entryPageAligned = isPageAligned(entry.offset);
1926 
1927         if (!extractNativeLibs) {
1928             // ensure the file is properly aligned and unpacked
1929             if (!entryUncompressed) {
1930                 LOG(WARNING) << "Library " << fileName << " must be uncompressed to mmap it";
1931                 return false;
1932             }
1933             if (!entryPageAligned) {
1934                 LOG(WARNING) << "Library " << fileName
1935                              << " must be page-aligned to mmap it, offset = 0x" << std::hex
1936                              << entry.offset;
1937                 return false;
1938             }
1939             continue;
1940         }
1941 
1942         auto startFileTs = Clock::now();
1943 
1944         const auto libName = path::basename(fileName);
1945         auto targetLibPath = path::join(targetLibPathRelativeToStorage, libName);
1946         const auto targetLibPathAbsolute = normalizePathToStorage(*ifs, storage, targetLibPath);
1947         // If the extract file already exists, skip
1948         if (access(targetLibPathAbsolute.c_str(), F_OK) == 0) {
1949             if (perfLoggingEnabled()) {
1950                 LOG(INFO) << "incfs: Native lib file already exists: " << targetLibPath
1951                           << "; skipping extraction, spent "
1952                           << elapsedMcs(startFileTs, Clock::now()) << "mcs";
1953             }
1954             continue;
1955         }
1956 
1957         if (mapFiles && entryUncompressed && entryPageAligned && entry.uncompressed_length > 0) {
1958             incfs::NewMappedFileParams mappedFileParams = {
1959                     .sourceId = sourceId,
1960                     .sourceOffset = entry.offset,
1961                     .size = entry.uncompressed_length,
1962             };
1963 
1964             if (auto res = mIncFs->makeMappedFile(ifs->control, targetLibPathAbsolute, 0755,
1965                                                   mappedFileParams);
1966                 res == 0) {
1967                 if (perfLoggingEnabled()) {
1968                     auto doneTs = Clock::now();
1969                     LOG(INFO) << "incfs: Mapped " << libName << ": "
1970                               << elapsedMcs(startFileTs, doneTs) << "mcs";
1971                 }
1972                 continue;
1973             } else {
1974                 LOG(WARNING) << "Failed to map file for: '" << targetLibPath << "' errno: " << res
1975                              << "; falling back to full extraction";
1976             }
1977         }
1978 
1979         // Create new lib file without signature info
1980         incfs::NewFileParams libFileParams = {
1981                 .size = entry.uncompressed_length,
1982                 .signature = {},
1983                 // Metadata of the new lib file is its relative path
1984                 .metadata = {targetLibPath.c_str(), (IncFsSize)targetLibPath.size()},
1985         };
1986         incfs::FileId libFileId = idFromMetadata(targetLibPath);
1987         if (auto res = mIncFs->makeFile(ifs->control, targetLibPathAbsolute, 0755, libFileId,
1988                                         libFileParams)) {
1989             LOG(ERROR) << "Failed to make file for: " << targetLibPath << " errno: " << res;
1990             // If one lib file fails to be created, abort others as well
1991             return false;
1992         }
1993 
1994         auto makeFileTs = Clock::now();
1995 
1996         // If it is a zero-byte file, skip data writing
1997         if (entry.uncompressed_length == 0) {
1998             if (perfLoggingEnabled()) {
1999                 LOG(INFO) << "incfs: Extracted " << libName
2000                           << "(0 bytes): " << elapsedMcs(startFileTs, makeFileTs) << "mcs";
2001             }
2002             continue;
2003         }
2004 
2005         jobQueue.emplace_back([this, zipFile, entry, ifs = std::weak_ptr<IncFsMount>(ifs),
2006                                libFileId, libPath = std::move(targetLibPath),
2007                                makeFileTs]() mutable {
2008             extractZipFile(ifs.lock(), zipFile.get(), entry, libFileId, libPath, makeFileTs);
2009         });
2010 
2011         if (perfLoggingEnabled()) {
2012             auto prepareJobTs = Clock::now();
2013             LOG(INFO) << "incfs: Processed " << libName << ": "
2014                       << elapsedMcs(startFileTs, prepareJobTs)
2015                       << "mcs, make file: " << elapsedMcs(startFileTs, makeFileTs)
2016                       << " prepare job: " << elapsedMcs(makeFileTs, prepareJobTs);
2017         }
2018     }
2019 
2020     auto processedTs = Clock::now();
2021 
2022     if (!jobQueue.empty()) {
2023         {
2024             std::lock_guard lock(mJobMutex);
2025             if (mRunning) {
2026                 auto& existingJobs = mJobQueue[ifs->mountId];
2027                 if (existingJobs.empty()) {
2028                     existingJobs = std::move(jobQueue);
2029                 } else {
2030                     existingJobs.insert(existingJobs.end(), std::move_iterator(jobQueue.begin()),
2031                                         std::move_iterator(jobQueue.end()));
2032                 }
2033             }
2034         }
2035         mJobCondition.notify_all();
2036     }
2037 
2038     if (perfLoggingEnabled()) {
2039         auto end = Clock::now();
2040         LOG(INFO) << "incfs: configureNativeBinaries complete in " << elapsedMcs(start, end)
2041                   << "mcs, make dirs: " << elapsedMcs(start, mkDirsTs)
2042                   << " open zip: " << elapsedMcs(mkDirsTs, openZipTs)
2043                   << " make files: " << elapsedMcs(openZipTs, processedTs)
2044                   << " schedule jobs: " << elapsedMcs(processedTs, end);
2045     }
2046 
2047     return true;
2048 }
2049 
extractZipFile(const IfsMountPtr & ifs,ZipArchiveHandle zipFile,ZipEntry & entry,const incfs::FileId & libFileId,std::string_view debugLibPath,Clock::time_point scheduledTs)2050 void IncrementalService::extractZipFile(const IfsMountPtr& ifs, ZipArchiveHandle zipFile,
2051                                         ZipEntry& entry, const incfs::FileId& libFileId,
2052                                         std::string_view debugLibPath,
2053                                         Clock::time_point scheduledTs) {
2054     if (!ifs) {
2055         LOG(INFO) << "Skipping zip file " << debugLibPath << " extraction for an expired mount";
2056         return;
2057     }
2058 
2059     auto startedTs = Clock::now();
2060 
2061     // Write extracted data to new file
2062     // NOTE: don't zero-initialize memory, it may take a while for nothing
2063     auto libData = std::unique_ptr<uint8_t[]>(new uint8_t[entry.uncompressed_length]);
2064     if (ExtractToMemory(zipFile, &entry, libData.get(), entry.uncompressed_length)) {
2065         LOG(ERROR) << "Failed to extract native lib zip entry: " << path::basename(debugLibPath);
2066         return;
2067     }
2068 
2069     auto extractFileTs = Clock::now();
2070 
2071     if (setFileContent(ifs, libFileId, debugLibPath,
2072                        std::span(libData.get(), entry.uncompressed_length))) {
2073         return;
2074     }
2075 
2076     if (perfLoggingEnabled()) {
2077         auto endFileTs = Clock::now();
2078         LOG(INFO) << "incfs: Extracted " << path::basename(debugLibPath) << "("
2079                   << entry.compressed_length << " -> " << entry.uncompressed_length
2080                   << " bytes): " << elapsedMcs(startedTs, endFileTs)
2081                   << "mcs, scheduling delay: " << elapsedMcs(scheduledTs, startedTs)
2082                   << " extract: " << elapsedMcs(startedTs, extractFileTs)
2083                   << " open/prepare/write: " << elapsedMcs(extractFileTs, endFileTs);
2084     }
2085 }
2086 
waitForNativeBinariesExtraction(StorageId storage)2087 bool IncrementalService::waitForNativeBinariesExtraction(StorageId storage) {
2088     struct WaitPrinter {
2089         const Clock::time_point startTs = Clock::now();
2090         ~WaitPrinter() noexcept {
2091             if (perfLoggingEnabled()) {
2092                 const auto endTs = Clock::now();
2093                 LOG(INFO) << "incfs: waitForNativeBinariesExtraction() complete in "
2094                           << elapsedMcs(startTs, endTs) << "mcs";
2095             }
2096         }
2097     } waitPrinter;
2098 
2099     MountId mount;
2100     {
2101         auto ifs = getIfs(storage);
2102         if (!ifs) {
2103             return true;
2104         }
2105         mount = ifs->mountId;
2106     }
2107 
2108     std::unique_lock lock(mJobMutex);
2109     mJobCondition.wait(lock, [this, mount] {
2110         return !mRunning ||
2111                 (mPendingJobsMount != mount && mJobQueue.find(mount) == mJobQueue.end());
2112     });
2113     return mRunning;
2114 }
2115 
setFileContent(const IfsMountPtr & ifs,const incfs::FileId & fileId,std::string_view debugFilePath,std::span<const uint8_t> data) const2116 int IncrementalService::setFileContent(const IfsMountPtr& ifs, const incfs::FileId& fileId,
2117                                        std::string_view debugFilePath,
2118                                        std::span<const uint8_t> data) const {
2119     auto startTs = Clock::now();
2120 
2121     const auto writeFd = mIncFs->openForSpecialOps(ifs->control, fileId);
2122     if (!writeFd.ok()) {
2123         LOG(ERROR) << "Failed to open write fd for: " << debugFilePath
2124                    << " errno: " << writeFd.get();
2125         return writeFd.get();
2126     }
2127 
2128     const auto dataLength = data.size();
2129 
2130     auto openFileTs = Clock::now();
2131     const int numBlocks = (data.size() + constants().blockSize - 1) / constants().blockSize;
2132     std::vector<IncFsDataBlock> instructions(numBlocks);
2133     for (int i = 0; i < numBlocks; i++) {
2134         const auto blockSize = std::min<long>(constants().blockSize, data.size());
2135         instructions[i] = IncFsDataBlock{
2136                 .fileFd = writeFd.get(),
2137                 .pageIndex = static_cast<IncFsBlockIndex>(i),
2138                 .compression = INCFS_COMPRESSION_KIND_NONE,
2139                 .kind = INCFS_BLOCK_KIND_DATA,
2140                 .dataSize = static_cast<uint32_t>(blockSize),
2141                 .data = reinterpret_cast<const char*>(data.data()),
2142         };
2143         data = data.subspan(blockSize);
2144     }
2145     auto prepareInstsTs = Clock::now();
2146 
2147     size_t res = mIncFs->writeBlocks(instructions);
2148     if (res != instructions.size()) {
2149         LOG(ERROR) << "Failed to write data into: " << debugFilePath;
2150         return res;
2151     }
2152 
2153     if (perfLoggingEnabled()) {
2154         auto endTs = Clock::now();
2155         LOG(INFO) << "incfs: Set file content " << debugFilePath << "(" << dataLength
2156                   << " bytes): " << elapsedMcs(startTs, endTs)
2157                   << "mcs, open: " << elapsedMcs(startTs, openFileTs)
2158                   << " prepare: " << elapsedMcs(openFileTs, prepareInstsTs)
2159                   << " write: " << elapsedMcs(prepareInstsTs, endTs);
2160     }
2161 
2162     return 0;
2163 }
2164 
isFileFullyLoaded(StorageId storage,std::string_view filePath) const2165 incfs::LoadingState IncrementalService::isFileFullyLoaded(StorageId storage,
2166                                                           std::string_view filePath) const {
2167     std::unique_lock l(mLock);
2168     const auto ifs = getIfsLocked(storage);
2169     if (!ifs) {
2170         LOG(ERROR) << "isFileFullyLoaded failed, invalid storageId: " << storage;
2171         return incfs::LoadingState(-EINVAL);
2172     }
2173     const auto storageInfo = ifs->storages.find(storage);
2174     if (storageInfo == ifs->storages.end()) {
2175         LOG(ERROR) << "isFileFullyLoaded failed, no storage: " << storage;
2176         return incfs::LoadingState(-EINVAL);
2177     }
2178     l.unlock();
2179     return mIncFs->isFileFullyLoaded(ifs->control, filePath);
2180 }
2181 
isMountFullyLoaded(StorageId storage) const2182 incfs::LoadingState IncrementalService::isMountFullyLoaded(StorageId storage) const {
2183     const auto ifs = getIfs(storage);
2184     if (!ifs) {
2185         LOG(ERROR) << "isMountFullyLoaded failed, invalid storageId: " << storage;
2186         return incfs::LoadingState(-EINVAL);
2187     }
2188     return mIncFs->isEverythingFullyLoaded(ifs->control);
2189 }
2190 
getLoadingProgress(StorageId storage) const2191 IncrementalService::LoadingProgress IncrementalService::getLoadingProgress(
2192         StorageId storage) const {
2193     std::unique_lock l(mLock);
2194     const auto ifs = getIfsLocked(storage);
2195     if (!ifs) {
2196         LOG(ERROR) << "getLoadingProgress failed, invalid storageId: " << storage;
2197         return {-EINVAL, -EINVAL};
2198     }
2199     const auto storageInfo = ifs->storages.find(storage);
2200     if (storageInfo == ifs->storages.end()) {
2201         LOG(ERROR) << "getLoadingProgress failed, no storage: " << storage;
2202         return {-EINVAL, -EINVAL};
2203     }
2204     l.unlock();
2205     return getLoadingProgressFromPath(*ifs, storageInfo->second.name);
2206 }
2207 
getLoadingProgressFromPath(const IncFsMount & ifs,std::string_view storagePath) const2208 IncrementalService::LoadingProgress IncrementalService::getLoadingProgressFromPath(
2209         const IncFsMount& ifs, std::string_view storagePath) const {
2210     ssize_t totalBlocks = 0, filledBlocks = 0, error = 0;
2211     mFs->listFilesRecursive(storagePath, [&, this](auto filePath) {
2212         const auto [filledBlocksCount, totalBlocksCount] =
2213                 mIncFs->countFilledBlocks(ifs.control, filePath);
2214         if (filledBlocksCount == -EOPNOTSUPP || filledBlocksCount == -ENOTSUP ||
2215             filledBlocksCount == -ENOENT) {
2216             // a kind of a file that's not really being loaded, e.g. a mapped range
2217             // an older IncFS used to return ENOENT in this case, so handle it the same way
2218             return true;
2219         }
2220         if (filledBlocksCount < 0) {
2221             LOG(ERROR) << "getLoadingProgress failed to get filled blocks count for: " << filePath
2222                        << ", errno: " << filledBlocksCount;
2223             error = filledBlocksCount;
2224             return false;
2225         }
2226         totalBlocks += totalBlocksCount;
2227         filledBlocks += filledBlocksCount;
2228         return true;
2229     });
2230 
2231     return error ? LoadingProgress{error, error} : LoadingProgress{filledBlocks, totalBlocks};
2232 }
2233 
updateLoadingProgress(StorageId storage,StorageLoadingProgressListener && progressListener)2234 bool IncrementalService::updateLoadingProgress(StorageId storage,
2235                                                StorageLoadingProgressListener&& progressListener) {
2236     const auto progress = getLoadingProgress(storage);
2237     if (progress.isError()) {
2238         // Failed to get progress from incfs, abort.
2239         return false;
2240     }
2241     progressListener->onStorageLoadingProgressChanged(storage, progress.getProgress());
2242     if (progress.fullyLoaded()) {
2243         // Stop updating progress once it is fully loaded
2244         return true;
2245     }
2246     addTimedJob(*mProgressUpdateJobQueue, storage,
2247                 Constants::progressUpdateInterval /* repeat after 1s */,
2248                 [storage, progressListener = std::move(progressListener), this]() mutable {
2249                     updateLoadingProgress(storage, std::move(progressListener));
2250                 });
2251     return true;
2252 }
2253 
registerLoadingProgressListener(StorageId storage,StorageLoadingProgressListener progressListener)2254 bool IncrementalService::registerLoadingProgressListener(
2255         StorageId storage, StorageLoadingProgressListener progressListener) {
2256     return updateLoadingProgress(storage, std::move(progressListener));
2257 }
2258 
unregisterLoadingProgressListener(StorageId storage)2259 bool IncrementalService::unregisterLoadingProgressListener(StorageId storage) {
2260     return removeTimedJobs(*mProgressUpdateJobQueue, storage);
2261 }
2262 
perfLoggingEnabled()2263 bool IncrementalService::perfLoggingEnabled() {
2264     static const bool enabled = base::GetBoolProperty("incremental.perflogging", false);
2265     return enabled;
2266 }
2267 
runJobProcessing()2268 void IncrementalService::runJobProcessing() {
2269     for (;;) {
2270         std::unique_lock lock(mJobMutex);
2271         mJobCondition.wait(lock, [this]() { return !mRunning || !mJobQueue.empty(); });
2272         if (!mRunning) {
2273             return;
2274         }
2275 
2276         auto it = mJobQueue.begin();
2277         mPendingJobsMount = it->first;
2278         auto queue = std::move(it->second);
2279         mJobQueue.erase(it);
2280         lock.unlock();
2281 
2282         for (auto&& job : queue) {
2283             job();
2284         }
2285 
2286         lock.lock();
2287         mPendingJobsMount = kInvalidStorageId;
2288         lock.unlock();
2289         mJobCondition.notify_all();
2290     }
2291 }
2292 
registerAppOpsCallback(const std::string & packageName)2293 void IncrementalService::registerAppOpsCallback(const std::string& packageName) {
2294     sp<IAppOpsCallback> listener;
2295     {
2296         std::unique_lock lock{mCallbacksLock};
2297         auto& cb = mCallbackRegistered[packageName];
2298         if (cb) {
2299             return;
2300         }
2301         cb = new AppOpsListener(*this, packageName);
2302         listener = cb;
2303     }
2304 
2305     mAppOpsManager->startWatchingMode(AppOpsManager::OP_GET_USAGE_STATS,
2306                                       String16(packageName.c_str()), listener);
2307 }
2308 
unregisterAppOpsCallback(const std::string & packageName)2309 bool IncrementalService::unregisterAppOpsCallback(const std::string& packageName) {
2310     sp<IAppOpsCallback> listener;
2311     {
2312         std::unique_lock lock{mCallbacksLock};
2313         auto found = mCallbackRegistered.find(packageName);
2314         if (found == mCallbackRegistered.end()) {
2315             return false;
2316         }
2317         listener = found->second;
2318         mCallbackRegistered.erase(found);
2319     }
2320 
2321     mAppOpsManager->stopWatchingMode(listener);
2322     return true;
2323 }
2324 
onAppOpChanged(const std::string & packageName)2325 void IncrementalService::onAppOpChanged(const std::string& packageName) {
2326     if (!unregisterAppOpsCallback(packageName)) {
2327         return;
2328     }
2329 
2330     std::vector<IfsMountPtr> affected;
2331     {
2332         std::lock_guard l(mLock);
2333         affected.reserve(mMounts.size());
2334         for (auto&& [id, ifs] : mMounts) {
2335             std::unique_lock ll(ifs->lock);
2336             if (ifs->mountId == id && ifs->dataLoaderStub &&
2337                 ifs->dataLoaderStub->params().packageName == packageName) {
2338                 affected.push_back(ifs);
2339             }
2340         }
2341     }
2342     for (auto&& ifs : affected) {
2343         std::unique_lock ll(ifs->lock);
2344         disableReadLogsLocked(*ifs);
2345     }
2346 }
2347 
addTimedJob(TimedQueueWrapper & timedQueue,MountId id,Milliseconds after,Job what)2348 bool IncrementalService::addTimedJob(TimedQueueWrapper& timedQueue, MountId id, Milliseconds after,
2349                                      Job what) {
2350     if (id == kInvalidStorageId) {
2351         return false;
2352     }
2353     timedQueue.addJob(id, after, std::move(what));
2354     return true;
2355 }
2356 
removeTimedJobs(TimedQueueWrapper & timedQueue,MountId id)2357 bool IncrementalService::removeTimedJobs(TimedQueueWrapper& timedQueue, MountId id) {
2358     if (id == kInvalidStorageId) {
2359         return false;
2360     }
2361     timedQueue.removeJobs(id);
2362     return true;
2363 }
2364 
addIfsStateCallback(StorageId storageId,IfsStateCallback callback)2365 void IncrementalService::addIfsStateCallback(StorageId storageId, IfsStateCallback callback) {
2366     bool wasEmpty;
2367     {
2368         std::lock_guard l(mIfsStateCallbacksLock);
2369         wasEmpty = mIfsStateCallbacks.empty();
2370         mIfsStateCallbacks[storageId].emplace_back(std::move(callback));
2371     }
2372     if (wasEmpty) {
2373         addTimedJob(*mTimedQueue, kAllStoragesId, Constants::progressUpdateInterval,
2374                     [this]() { processIfsStateCallbacks(); });
2375     }
2376 }
2377 
processIfsStateCallbacks()2378 void IncrementalService::processIfsStateCallbacks() {
2379     StorageId storageId = kInvalidStorageId;
2380     std::vector<IfsStateCallback> local;
2381     while (true) {
2382         {
2383             std::lock_guard l(mIfsStateCallbacksLock);
2384             if (mIfsStateCallbacks.empty()) {
2385                 return;
2386             }
2387             IfsStateCallbacks::iterator it;
2388             if (storageId == kInvalidStorageId) {
2389                 // First entry, initialize the |it|.
2390                 it = mIfsStateCallbacks.begin();
2391             } else {
2392                 // Subsequent entries, update the |storageId|, and shift to the new one (not that
2393                 // it guarantees much about updated items, but at least the loop will finish).
2394                 it = mIfsStateCallbacks.lower_bound(storageId);
2395                 if (it == mIfsStateCallbacks.end()) {
2396                     // Nothing else left, too bad.
2397                     break;
2398                 }
2399                 if (it->first != storageId) {
2400                     local.clear(); // Was removed during processing, forget the old callbacks.
2401                 } else {
2402                     // Put the 'surviving' callbacks back into the map and advance the position.
2403                     auto& callbacks = it->second;
2404                     if (callbacks.empty()) {
2405                         std::swap(callbacks, local);
2406                     } else {
2407                         callbacks.insert(callbacks.end(), std::move_iterator(local.begin()),
2408                                          std::move_iterator(local.end()));
2409                         local.clear();
2410                     }
2411                     if (callbacks.empty()) {
2412                         it = mIfsStateCallbacks.erase(it);
2413                         if (mIfsStateCallbacks.empty()) {
2414                             return;
2415                         }
2416                     } else {
2417                         ++it;
2418                     }
2419                 }
2420             }
2421 
2422             if (it == mIfsStateCallbacks.end()) {
2423                 break;
2424             }
2425 
2426             storageId = it->first;
2427             auto& callbacks = it->second;
2428             if (callbacks.empty()) {
2429                 // Invalid case, one extra lookup should be ok.
2430                 continue;
2431             }
2432             std::swap(callbacks, local);
2433         }
2434 
2435         processIfsStateCallbacks(storageId, local);
2436     }
2437 
2438     addTimedJob(*mTimedQueue, kAllStoragesId, Constants::progressUpdateInterval,
2439                 [this]() { processIfsStateCallbacks(); });
2440 }
2441 
processIfsStateCallbacks(StorageId storageId,std::vector<IfsStateCallback> & callbacks)2442 void IncrementalService::processIfsStateCallbacks(StorageId storageId,
2443                                                   std::vector<IfsStateCallback>& callbacks) {
2444     const auto state = isMountFullyLoaded(storageId);
2445     IfsState storageState = {};
2446     storageState.error = int(state) < 0;
2447     storageState.fullyLoaded = state == incfs::LoadingState::Full;
2448     if (storageState.fullyLoaded) {
2449         const auto ifs = getIfs(storageId);
2450         storageState.readLogsEnabled = ifs && ifs->readLogsEnabled();
2451     }
2452 
2453     for (auto cur = callbacks.begin(); cur != callbacks.end();) {
2454         if ((*cur)(storageId, storageState)) {
2455             ++cur;
2456         } else {
2457             cur = callbacks.erase(cur);
2458         }
2459     }
2460 }
2461 
removeIfsStateCallbacks(StorageId storageId)2462 void IncrementalService::removeIfsStateCallbacks(StorageId storageId) {
2463     std::lock_guard l(mIfsStateCallbacksLock);
2464     mIfsStateCallbacks.erase(storageId);
2465 }
2466 
getMetrics(StorageId storageId,android::os::PersistableBundle * result)2467 void IncrementalService::getMetrics(StorageId storageId, android::os::PersistableBundle* result) {
2468     const auto ifs = getIfs(storageId);
2469     if (!ifs) {
2470         LOG(ERROR) << "getMetrics failed, invalid storageId: " << storageId;
2471         return;
2472     }
2473     const auto& kMetricsReadLogsEnabled =
2474             os::incremental::BnIncrementalService::METRICS_READ_LOGS_ENABLED();
2475     result->putBoolean(String16(kMetricsReadLogsEnabled.c_str()), ifs->readLogsEnabled() != 0);
2476     const auto incfsMetrics = mIncFs->getMetrics(ifs->metricsKey);
2477     if (incfsMetrics) {
2478         const auto& kMetricsTotalDelayedReads =
2479                 os::incremental::BnIncrementalService::METRICS_TOTAL_DELAYED_READS();
2480         const auto totalDelayedReads =
2481                 incfsMetrics->readsDelayedMin + incfsMetrics->readsDelayedPending;
2482         result->putInt(String16(kMetricsTotalDelayedReads.c_str()), totalDelayedReads);
2483         const auto& kMetricsTotalFailedReads =
2484                 os::incremental::BnIncrementalService::METRICS_TOTAL_FAILED_READS();
2485         const auto totalFailedReads = incfsMetrics->readsFailedTimedOut +
2486                 incfsMetrics->readsFailedHashVerification + incfsMetrics->readsFailedOther;
2487         result->putInt(String16(kMetricsTotalFailedReads.c_str()), totalFailedReads);
2488         const auto& kMetricsTotalDelayedReadsMillis =
2489                 os::incremental::BnIncrementalService::METRICS_TOTAL_DELAYED_READS_MILLIS();
2490         const int64_t totalDelayedReadsMillis =
2491                 (incfsMetrics->readsDelayedMinUs + incfsMetrics->readsDelayedPendingUs) / 1000;
2492         result->putLong(String16(kMetricsTotalDelayedReadsMillis.c_str()), totalDelayedReadsMillis);
2493     }
2494     const auto lastReadError = mIncFs->getLastReadError(ifs->control);
2495     if (lastReadError && lastReadError->timestampUs != 0) {
2496         const auto& kMetricsMillisSinceLastReadError =
2497                 os::incremental::BnIncrementalService::METRICS_MILLIS_SINCE_LAST_READ_ERROR();
2498         result->putLong(String16(kMetricsMillisSinceLastReadError.c_str()),
2499                         (int64_t)elapsedUsSinceMonoTs(lastReadError->timestampUs) / 1000);
2500         const auto& kMetricsLastReadErrorNo =
2501                 os::incremental::BnIncrementalService::METRICS_LAST_READ_ERROR_NUMBER();
2502         result->putInt(String16(kMetricsLastReadErrorNo.c_str()), lastReadError->errorNo);
2503         const auto& kMetricsLastReadUid =
2504                 os::incremental::BnIncrementalService::METRICS_LAST_READ_ERROR_UID();
2505         result->putInt(String16(kMetricsLastReadUid.c_str()), lastReadError->uid);
2506     }
2507     std::unique_lock l(ifs->lock);
2508     if (!ifs->dataLoaderStub) {
2509         return;
2510     }
2511     ifs->dataLoaderStub->getMetrics(result);
2512 }
2513 
DataLoaderStub(IncrementalService & service,MountId id,DataLoaderParamsParcel && params,FileSystemControlParcel && control,DataLoaderStatusListener && statusListener,const StorageHealthCheckParams & healthCheckParams,StorageHealthListener && healthListener,std::string && healthPath)2514 IncrementalService::DataLoaderStub::DataLoaderStub(
2515         IncrementalService& service, MountId id, DataLoaderParamsParcel&& params,
2516         FileSystemControlParcel&& control, DataLoaderStatusListener&& statusListener,
2517         const StorageHealthCheckParams& healthCheckParams, StorageHealthListener&& healthListener,
2518         std::string&& healthPath)
2519       : mService(service),
2520         mId(id),
2521         mParams(std::move(params)),
2522         mControl(std::move(control)),
2523         mStatusListener(std::move(statusListener)),
2524         mHealthListener(std::move(healthListener)),
2525         mHealthPath(std::move(healthPath)),
2526         mHealthCheckParams(healthCheckParams) {
2527     if (mHealthListener && !isHealthParamsValid()) {
2528         mHealthListener = {};
2529     }
2530     if (!mHealthListener) {
2531         // Disable advanced health check statuses.
2532         mHealthCheckParams.blockedTimeoutMs = -1;
2533     }
2534     updateHealthStatus();
2535 }
2536 
~DataLoaderStub()2537 IncrementalService::DataLoaderStub::~DataLoaderStub() {
2538     if (isValid()) {
2539         cleanupResources();
2540     }
2541 }
2542 
cleanupResources()2543 void IncrementalService::DataLoaderStub::cleanupResources() {
2544     auto now = Clock::now();
2545     {
2546         std::unique_lock lock(mMutex);
2547         mHealthPath.clear();
2548         unregisterFromPendingReads();
2549         resetHealthControl();
2550         mService.removeTimedJobs(*mService.mTimedQueue, mId);
2551     }
2552     mService.removeIfsStateCallbacks(mId);
2553 
2554     requestDestroy();
2555 
2556     {
2557         std::unique_lock lock(mMutex);
2558         mParams = {};
2559         mControl = {};
2560         mHealthControl = {};
2561         mHealthListener = {};
2562         mStatusCondition.wait_until(lock, now + Constants::destroyTimeout, [this] {
2563             return mCurrentStatus == IDataLoaderStatusListener::DATA_LOADER_DESTROYED;
2564         });
2565         mStatusListener = {};
2566         mId = kInvalidStorageId;
2567     }
2568 }
2569 
getDataLoader()2570 sp<content::pm::IDataLoader> IncrementalService::DataLoaderStub::getDataLoader() {
2571     sp<IDataLoader> dataloader;
2572     auto status = mService.mDataLoaderManager->getDataLoader(id(), &dataloader);
2573     if (!status.isOk()) {
2574         LOG(ERROR) << "Failed to get dataloader: " << status.toString8();
2575         return {};
2576     }
2577     if (!dataloader) {
2578         LOG(ERROR) << "DataLoader is null: " << status.toString8();
2579         return {};
2580     }
2581     return dataloader;
2582 }
2583 
isSystemDataLoader() const2584 bool IncrementalService::DataLoaderStub::isSystemDataLoader() const {
2585     return (params().packageName == Constants::systemPackage);
2586 }
2587 
requestCreate()2588 bool IncrementalService::DataLoaderStub::requestCreate() {
2589     return setTargetStatus(IDataLoaderStatusListener::DATA_LOADER_CREATED);
2590 }
2591 
requestStart()2592 bool IncrementalService::DataLoaderStub::requestStart() {
2593     return setTargetStatus(IDataLoaderStatusListener::DATA_LOADER_STARTED);
2594 }
2595 
requestDestroy()2596 bool IncrementalService::DataLoaderStub::requestDestroy() {
2597     return setTargetStatus(IDataLoaderStatusListener::DATA_LOADER_DESTROYED);
2598 }
2599 
setTargetStatus(int newStatus)2600 bool IncrementalService::DataLoaderStub::setTargetStatus(int newStatus) {
2601     {
2602         std::unique_lock lock(mMutex);
2603         setTargetStatusLocked(newStatus);
2604     }
2605     return fsmStep();
2606 }
2607 
setTargetStatusLocked(int status)2608 void IncrementalService::DataLoaderStub::setTargetStatusLocked(int status) {
2609     auto oldStatus = mTargetStatus;
2610     mTargetStatus = status;
2611     mTargetStatusTs = Clock::now();
2612     LOG(DEBUG) << "Target status update for DataLoader " << id() << ": " << oldStatus << " -> "
2613                << status << " (current " << mCurrentStatus << ")";
2614 }
2615 
needToBind()2616 std::optional<Milliseconds> IncrementalService::DataLoaderStub::needToBind() {
2617     std::unique_lock lock(mMutex);
2618 
2619     const auto now = mService.mClock->now();
2620     const bool healthy = (mPreviousBindDelay == 0ms);
2621 
2622     if (mCurrentStatus == IDataLoaderStatusListener::DATA_LOADER_BINDING &&
2623         now - mCurrentStatusTs <= Constants::bindingTimeout) {
2624         LOG(INFO) << "Binding still in progress. "
2625                   << (healthy ? "The DL is healthy/freshly bound, ok to retry for a few times."
2626                               : "Already unhealthy, don't do anything.")
2627                   << " for storage " << mId;
2628         // Binding still in progress.
2629         if (!healthy) {
2630             // Already unhealthy, don't do anything.
2631             return {};
2632         }
2633         // The DL is healthy/freshly bound, ok to retry for a few times.
2634         if (now - mPreviousBindTs <= Constants::bindGracePeriod) {
2635             // Still within grace period.
2636             if (now - mCurrentStatusTs >= Constants::bindRetryInterval) {
2637                 // Retry interval passed, retrying.
2638                 mCurrentStatusTs = now;
2639                 mPreviousBindDelay = 0ms;
2640                 return 0ms;
2641             }
2642             return {};
2643         }
2644         // fallthrough, mark as unhealthy, and retry with delay
2645     }
2646 
2647     const auto previousBindTs = mPreviousBindTs;
2648     mPreviousBindTs = now;
2649 
2650     const auto nonCrashingInterval =
2651             std::max(castToMs(now - previousBindTs - mPreviousBindDelay), 100ms);
2652     if (previousBindTs.time_since_epoch() == Clock::duration::zero() ||
2653         nonCrashingInterval > Constants::healthyDataLoaderUptime) {
2654         mPreviousBindDelay = 0ms;
2655         return 0ms;
2656     }
2657 
2658     constexpr auto minBindDelayMs = castToMs(Constants::minBindDelay);
2659     constexpr auto maxBindDelayMs = castToMs(Constants::maxBindDelay);
2660 
2661     const auto bindDelayMs =
2662             std::min(std::max(mPreviousBindDelay * Constants::bindDelayMultiplier, minBindDelayMs),
2663                      maxBindDelayMs)
2664                     .count();
2665     const auto bindDelayJitterRangeMs = bindDelayMs / Constants::bindDelayJitterDivider;
2666     // rand() is enough, not worth maintaining a full-blown <rand> object for delay jitter
2667     const auto bindDelayJitterMs = rand() % (bindDelayJitterRangeMs * 2) - // NOLINT
2668             bindDelayJitterRangeMs;
2669     mPreviousBindDelay = std::chrono::milliseconds(bindDelayMs + bindDelayJitterMs);
2670     return mPreviousBindDelay;
2671 }
2672 
bind()2673 bool IncrementalService::DataLoaderStub::bind() {
2674     const auto maybeBindDelay = needToBind();
2675     if (!maybeBindDelay) {
2676         LOG(DEBUG) << "Skipping bind to " << mParams.packageName << " because of pending bind.";
2677         return true;
2678     }
2679     const auto bindDelay = *maybeBindDelay;
2680     if (bindDelay > 1s) {
2681         LOG(INFO) << "Delaying bind to " << mParams.packageName << " by "
2682                   << bindDelay.count() / 1000 << "s"
2683                   << " for storage " << mId;
2684     }
2685 
2686     bool result = false;
2687     auto status = mService.mDataLoaderManager->bindToDataLoader(id(), mParams, bindDelay.count(),
2688                                                                 this, &result);
2689     if (!status.isOk() || !result) {
2690         const bool healthy = (bindDelay == 0ms);
2691         LOG(ERROR) << "Failed to bind a data loader for mount " << id()
2692                    << (healthy ? ", retrying." : "");
2693 
2694         // Internal error, retry for healthy/new DLs.
2695         // Let needToBind migrate it to unhealthy after too many retries.
2696         if (healthy) {
2697             if (mService.addTimedJob(*mService.mTimedQueue, id(), Constants::bindRetryInterval,
2698                                      [this]() { fsmStep(); })) {
2699                 // Mark as binding so that we know it's not the DL's fault.
2700                 setCurrentStatus(IDataLoaderStatusListener::DATA_LOADER_BINDING);
2701                 return true;
2702             }
2703         }
2704 
2705         return false;
2706     }
2707     return true;
2708 }
2709 
create()2710 bool IncrementalService::DataLoaderStub::create() {
2711     auto dataloader = getDataLoader();
2712     if (!dataloader) {
2713         return false;
2714     }
2715     auto status = dataloader->create(id(), mParams, mControl, this);
2716     if (!status.isOk()) {
2717         LOG(ERROR) << "Failed to create DataLoader: " << status.toString8();
2718         return false;
2719     }
2720     return true;
2721 }
2722 
start()2723 bool IncrementalService::DataLoaderStub::start() {
2724     auto dataloader = getDataLoader();
2725     if (!dataloader) {
2726         return false;
2727     }
2728     auto status = dataloader->start(id());
2729     if (!status.isOk()) {
2730         LOG(ERROR) << "Failed to start DataLoader: " << status.toString8();
2731         return false;
2732     }
2733     return true;
2734 }
2735 
destroy()2736 bool IncrementalService::DataLoaderStub::destroy() {
2737     return mService.mDataLoaderManager->unbindFromDataLoader(id()).isOk();
2738 }
2739 
fsmStep()2740 bool IncrementalService::DataLoaderStub::fsmStep() {
2741     if (!isValid()) {
2742         return false;
2743     }
2744 
2745     int currentStatus;
2746     int targetStatus;
2747     {
2748         std::unique_lock lock(mMutex);
2749         currentStatus = mCurrentStatus;
2750         targetStatus = mTargetStatus;
2751     }
2752 
2753     LOG(DEBUG) << "fsmStep: " << id() << ": " << currentStatus << " -> " << targetStatus;
2754 
2755     if (currentStatus == targetStatus) {
2756         return true;
2757     }
2758 
2759     switch (targetStatus) {
2760         case IDataLoaderStatusListener::DATA_LOADER_DESTROYED: {
2761             switch (currentStatus) {
2762                 case IDataLoaderStatusListener::DATA_LOADER_UNAVAILABLE:
2763                 case IDataLoaderStatusListener::DATA_LOADER_UNRECOVERABLE:
2764                     destroy();
2765                     // DataLoader is broken, just assume it's destroyed.
2766                     compareAndSetCurrentStatus(currentStatus,
2767                                                IDataLoaderStatusListener::DATA_LOADER_DESTROYED);
2768                     return true;
2769                 case IDataLoaderStatusListener::DATA_LOADER_BINDING:
2770                     compareAndSetCurrentStatus(currentStatus,
2771                                                IDataLoaderStatusListener::DATA_LOADER_DESTROYED);
2772                     return true;
2773                 default:
2774                     return destroy();
2775             }
2776             break;
2777         }
2778         case IDataLoaderStatusListener::DATA_LOADER_STARTED: {
2779             switch (currentStatus) {
2780                 case IDataLoaderStatusListener::DATA_LOADER_CREATED:
2781                 case IDataLoaderStatusListener::DATA_LOADER_STOPPED:
2782                     return start();
2783             }
2784             [[fallthrough]];
2785         }
2786         case IDataLoaderStatusListener::DATA_LOADER_CREATED:
2787             switch (currentStatus) {
2788                 case IDataLoaderStatusListener::DATA_LOADER_UNAVAILABLE:
2789                 case IDataLoaderStatusListener::DATA_LOADER_UNRECOVERABLE:
2790                     // Before binding need to make sure we are unbound.
2791                     // Otherwise we'll get stuck binding.
2792                     destroy();
2793                     // DataLoader is broken, just assume it's destroyed.
2794                     compareAndSetCurrentStatus(currentStatus,
2795                                                IDataLoaderStatusListener::DATA_LOADER_DESTROYED);
2796                     return true;
2797                 case IDataLoaderStatusListener::DATA_LOADER_DESTROYED:
2798                 case IDataLoaderStatusListener::DATA_LOADER_BINDING:
2799                     return bind();
2800                 case IDataLoaderStatusListener::DATA_LOADER_BOUND:
2801                     return create();
2802             }
2803             break;
2804         default:
2805             LOG(ERROR) << "Invalid target status: " << targetStatus
2806                        << ", current status: " << currentStatus;
2807             break;
2808     }
2809     return false;
2810 }
2811 
onStatusChanged(MountId mountId,int newStatus)2812 binder::Status IncrementalService::DataLoaderStub::onStatusChanged(MountId mountId, int newStatus) {
2813     if (!isValid()) {
2814         return binder::Status::
2815                 fromServiceSpecificError(-EINVAL, "onStatusChange came to invalid DataLoaderStub");
2816     }
2817     if (id() != mountId) {
2818         LOG(ERROR) << "onStatusChanged: mount ID mismatch: expected " << id()
2819                    << ", but got: " << mountId;
2820         return binder::Status::fromServiceSpecificError(-EPERM, "Mount ID mismatch.");
2821     }
2822     if (newStatus == IDataLoaderStatusListener::DATA_LOADER_UNAVAILABLE ||
2823         newStatus == IDataLoaderStatusListener::DATA_LOADER_UNRECOVERABLE) {
2824         // User-provided status, let's postpone the handling to avoid possible deadlocks.
2825         mService.addTimedJob(*mService.mTimedQueue, id(), Constants::userStatusDelay,
2826                              [this, newStatus]() { setCurrentStatus(newStatus); });
2827         return binder::Status::ok();
2828     }
2829 
2830     setCurrentStatus(newStatus);
2831     return binder::Status::ok();
2832 }
2833 
setCurrentStatus(int newStatus)2834 void IncrementalService::DataLoaderStub::setCurrentStatus(int newStatus) {
2835     compareAndSetCurrentStatus(Constants::anyStatus, newStatus);
2836 }
2837 
compareAndSetCurrentStatus(int expectedStatus,int newStatus)2838 void IncrementalService::DataLoaderStub::compareAndSetCurrentStatus(int expectedStatus,
2839                                                                     int newStatus) {
2840     int oldStatus, oldTargetStatus, newTargetStatus;
2841     DataLoaderStatusListener listener;
2842     {
2843         std::unique_lock lock(mMutex);
2844         if (mCurrentStatus == newStatus) {
2845             return;
2846         }
2847         if (expectedStatus != Constants::anyStatus && expectedStatus != mCurrentStatus) {
2848             return;
2849         }
2850 
2851         oldStatus = mCurrentStatus;
2852         oldTargetStatus = mTargetStatus;
2853         listener = mStatusListener;
2854 
2855         // Change the status.
2856         mCurrentStatus = newStatus;
2857         mCurrentStatusTs = mService.mClock->now();
2858 
2859         switch (mCurrentStatus) {
2860             case IDataLoaderStatusListener::DATA_LOADER_UNAVAILABLE:
2861                 // Unavailable, retry.
2862                 setTargetStatusLocked(IDataLoaderStatusListener::DATA_LOADER_STARTED);
2863                 break;
2864             case IDataLoaderStatusListener::DATA_LOADER_UNRECOVERABLE:
2865                 // Unrecoverable, just unbind.
2866                 setTargetStatusLocked(IDataLoaderStatusListener::DATA_LOADER_DESTROYED);
2867                 break;
2868             default:
2869                 break;
2870         }
2871 
2872         newTargetStatus = mTargetStatus;
2873     }
2874 
2875     LOG(DEBUG) << "Current status update for DataLoader " << id() << ": " << oldStatus << " -> "
2876                << newStatus << " (target " << oldTargetStatus << " -> " << newTargetStatus << ")";
2877 
2878     if (listener) {
2879         listener->onStatusChanged(id(), newStatus);
2880     }
2881 
2882     fsmStep();
2883 
2884     mStatusCondition.notify_all();
2885 }
2886 
isHealthParamsValid() const2887 bool IncrementalService::DataLoaderStub::isHealthParamsValid() const {
2888     return mHealthCheckParams.blockedTimeoutMs > 0 &&
2889             mHealthCheckParams.blockedTimeoutMs < mHealthCheckParams.unhealthyTimeoutMs;
2890 }
2891 
onHealthStatus(const StorageHealthListener & healthListener,int healthStatus)2892 void IncrementalService::DataLoaderStub::onHealthStatus(const StorageHealthListener& healthListener,
2893                                                         int healthStatus) {
2894     LOG(DEBUG) << id() << ": healthStatus: " << healthStatus;
2895     if (healthListener) {
2896         healthListener->onHealthStatus(id(), healthStatus);
2897     }
2898     mHealthStatus = healthStatus;
2899 }
2900 
updateHealthStatus(bool baseline)2901 void IncrementalService::DataLoaderStub::updateHealthStatus(bool baseline) {
2902     LOG(DEBUG) << id() << ": updateHealthStatus" << (baseline ? " (baseline)" : "");
2903 
2904     int healthStatusToReport = -1;
2905     StorageHealthListener healthListener;
2906 
2907     {
2908         std::unique_lock lock(mMutex);
2909         unregisterFromPendingReads();
2910 
2911         healthListener = mHealthListener;
2912 
2913         // Healthcheck depends on timestamp of the oldest pending read.
2914         // To get it, we need to re-open a pendingReads FD to get a full list of reads.
2915         // Additionally we need to re-register for epoll with fresh FDs in case there are no
2916         // reads.
2917         const auto now = Clock::now();
2918         const auto kernelTsUs = getOldestPendingReadTs();
2919         if (baseline) {
2920             // Updating baseline only on looper/epoll callback, i.e. on new set of pending
2921             // reads.
2922             mHealthBase = {now, kernelTsUs};
2923         }
2924 
2925         if (kernelTsUs == kMaxBootClockTsUs || mHealthBase.kernelTsUs == kMaxBootClockTsUs ||
2926             mHealthBase.userTs > now) {
2927             LOG(DEBUG) << id() << ": No pending reads or invalid base, report Ok and wait.";
2928             registerForPendingReads();
2929             healthStatusToReport = IStorageHealthListener::HEALTH_STATUS_OK;
2930             lock.unlock();
2931             onHealthStatus(healthListener, healthStatusToReport);
2932             return;
2933         }
2934 
2935         resetHealthControl();
2936 
2937         // Always make sure the data loader is started.
2938         setTargetStatusLocked(IDataLoaderStatusListener::DATA_LOADER_STARTED);
2939 
2940         // Skip any further processing if health check params are invalid.
2941         if (!isHealthParamsValid()) {
2942             LOG(DEBUG) << id()
2943                        << ": Skip any further processing if health check params are invalid.";
2944             healthStatusToReport = IStorageHealthListener::HEALTH_STATUS_READS_PENDING;
2945             lock.unlock();
2946             onHealthStatus(healthListener, healthStatusToReport);
2947             // Triggering data loader start. This is a one-time action.
2948             fsmStep();
2949             return;
2950         }
2951 
2952         // Don't schedule timer job less than 500ms in advance.
2953         static constexpr auto kTolerance = 500ms;
2954 
2955         const auto blockedTimeout = std::chrono::milliseconds(mHealthCheckParams.blockedTimeoutMs);
2956         const auto unhealthyTimeout =
2957                 std::chrono::milliseconds(mHealthCheckParams.unhealthyTimeoutMs);
2958         const auto unhealthyMonitoring =
2959                 std::max(1000ms,
2960                          std::chrono::milliseconds(mHealthCheckParams.unhealthyMonitoringMs));
2961 
2962         const auto delta = elapsedMsSinceKernelTs(now, kernelTsUs);
2963 
2964         Milliseconds checkBackAfter;
2965         if (delta + kTolerance < blockedTimeout) {
2966             LOG(DEBUG) << id() << ": Report reads pending and wait for blocked status.";
2967             checkBackAfter = blockedTimeout - delta;
2968             healthStatusToReport = IStorageHealthListener::HEALTH_STATUS_READS_PENDING;
2969         } else if (delta + kTolerance < unhealthyTimeout) {
2970             LOG(DEBUG) << id() << ": Report blocked and wait for unhealthy.";
2971             checkBackAfter = unhealthyTimeout - delta;
2972             healthStatusToReport = IStorageHealthListener::HEALTH_STATUS_BLOCKED;
2973         } else {
2974             LOG(DEBUG) << id() << ": Report unhealthy and continue monitoring.";
2975             checkBackAfter = unhealthyMonitoring;
2976             healthStatusToReport = IStorageHealthListener::HEALTH_STATUS_UNHEALTHY;
2977         }
2978         LOG(DEBUG) << id() << ": updateHealthStatus in " << double(checkBackAfter.count()) / 1000.0
2979                    << "secs";
2980         mService.addTimedJob(*mService.mTimedQueue, id(), checkBackAfter,
2981                              [this]() { updateHealthStatus(); });
2982     }
2983 
2984     // With kTolerance we are expecting these to execute before the next update.
2985     if (healthStatusToReport != -1) {
2986         onHealthStatus(healthListener, healthStatusToReport);
2987     }
2988 
2989     fsmStep();
2990 }
2991 
elapsedMsSinceKernelTs(TimePoint now,BootClockTsUs kernelTsUs)2992 Milliseconds IncrementalService::DataLoaderStub::elapsedMsSinceKernelTs(TimePoint now,
2993                                                                         BootClockTsUs kernelTsUs) {
2994     const auto kernelDeltaUs = kernelTsUs - mHealthBase.kernelTsUs;
2995     const auto userTs = mHealthBase.userTs + std::chrono::microseconds(kernelDeltaUs);
2996     return std::chrono::duration_cast<Milliseconds>(now - userTs);
2997 }
2998 
initializeHealthControl()2999 const incfs::UniqueControl& IncrementalService::DataLoaderStub::initializeHealthControl() {
3000     if (mHealthPath.empty()) {
3001         resetHealthControl();
3002         return mHealthControl;
3003     }
3004     if (mHealthControl.pendingReads() < 0) {
3005         mHealthControl = mService.mIncFs->openMount(mHealthPath);
3006     }
3007     if (mHealthControl.pendingReads() < 0) {
3008         LOG(ERROR) << "Failed to open health control for: " << id() << ", path: " << mHealthPath
3009                    << "(" << mHealthControl.cmd() << ":" << mHealthControl.pendingReads() << ":"
3010                    << mHealthControl.logs() << ")";
3011     }
3012     return mHealthControl;
3013 }
3014 
resetHealthControl()3015 void IncrementalService::DataLoaderStub::resetHealthControl() {
3016     mHealthControl = {};
3017 }
3018 
getOldestPendingReadTs()3019 BootClockTsUs IncrementalService::DataLoaderStub::getOldestPendingReadTs() {
3020     auto result = kMaxBootClockTsUs;
3021 
3022     const auto& control = initializeHealthControl();
3023     if (control.pendingReads() < 0) {
3024         return result;
3025     }
3026 
3027     if (mService.mIncFs->waitForPendingReads(control, 0ms, &mLastPendingReads) !=
3028                 android::incfs::WaitResult::HaveData ||
3029         mLastPendingReads.empty()) {
3030         // Clear previous pending reads
3031         mLastPendingReads.clear();
3032         return result;
3033     }
3034 
3035     LOG(DEBUG) << id() << ": pendingReads: fd(" << control.pendingReads() << "), count("
3036                << mLastPendingReads.size() << "), block: " << mLastPendingReads.front().block
3037                << ", time: " << mLastPendingReads.front().bootClockTsUs
3038                << ", uid: " << mLastPendingReads.front().uid;
3039 
3040     return getOldestTsFromLastPendingReads();
3041 }
3042 
registerForPendingReads()3043 void IncrementalService::DataLoaderStub::registerForPendingReads() {
3044     const auto pendingReadsFd = mHealthControl.pendingReads();
3045     if (pendingReadsFd < 0) {
3046         return;
3047     }
3048 
3049     LOG(DEBUG) << id() << ": addFd(pendingReadsFd): " << pendingReadsFd;
3050 
3051     mService.mLooper->addFd(
3052             pendingReadsFd, android::Looper::POLL_CALLBACK, android::Looper::EVENT_INPUT,
3053             [](int, int, void* data) -> int {
3054                 auto self = (DataLoaderStub*)data;
3055                 self->updateHealthStatus(/*baseline=*/true);
3056                 return 0;
3057             },
3058             this);
3059     mService.mLooper->wake();
3060 }
3061 
getOldestTsFromLastPendingReads()3062 BootClockTsUs IncrementalService::DataLoaderStub::getOldestTsFromLastPendingReads() {
3063     auto result = kMaxBootClockTsUs;
3064     for (auto&& pendingRead : mLastPendingReads) {
3065         result = std::min(result, pendingRead.bootClockTsUs);
3066     }
3067     return result;
3068 }
3069 
getMetrics(android::os::PersistableBundle * result)3070 void IncrementalService::DataLoaderStub::getMetrics(android::os::PersistableBundle* result) {
3071     const auto duration = elapsedMsSinceOldestPendingRead();
3072     if (duration >= 0) {
3073         const auto& kMetricsMillisSinceOldestPendingRead =
3074                 os::incremental::BnIncrementalService::METRICS_MILLIS_SINCE_OLDEST_PENDING_READ();
3075         result->putLong(String16(kMetricsMillisSinceOldestPendingRead.c_str()), duration);
3076     }
3077     const auto& kMetricsStorageHealthStatusCode =
3078             os::incremental::BnIncrementalService::METRICS_STORAGE_HEALTH_STATUS_CODE();
3079     result->putInt(String16(kMetricsStorageHealthStatusCode.c_str()), mHealthStatus);
3080     const auto& kMetricsDataLoaderStatusCode =
3081             os::incremental::BnIncrementalService::METRICS_DATA_LOADER_STATUS_CODE();
3082     result->putInt(String16(kMetricsDataLoaderStatusCode.c_str()), mCurrentStatus);
3083     const auto& kMetricsMillisSinceLastDataLoaderBind =
3084             os::incremental::BnIncrementalService::METRICS_MILLIS_SINCE_LAST_DATA_LOADER_BIND();
3085     result->putLong(String16(kMetricsMillisSinceLastDataLoaderBind.c_str()),
3086                     elapsedMcs(mPreviousBindTs, mService.mClock->now()) / 1000);
3087     const auto& kMetricsDataLoaderBindDelayMillis =
3088             os::incremental::BnIncrementalService::METRICS_DATA_LOADER_BIND_DELAY_MILLIS();
3089     result->putLong(String16(kMetricsDataLoaderBindDelayMillis.c_str()),
3090                     mPreviousBindDelay.count());
3091 }
3092 
elapsedMsSinceOldestPendingRead()3093 long IncrementalService::DataLoaderStub::elapsedMsSinceOldestPendingRead() {
3094     const auto oldestPendingReadKernelTs = getOldestTsFromLastPendingReads();
3095     if (oldestPendingReadKernelTs == kMaxBootClockTsUs) {
3096         return 0;
3097     }
3098     return elapsedMsSinceKernelTs(Clock::now(), oldestPendingReadKernelTs).count();
3099 }
3100 
unregisterFromPendingReads()3101 void IncrementalService::DataLoaderStub::unregisterFromPendingReads() {
3102     const auto pendingReadsFd = mHealthControl.pendingReads();
3103     if (pendingReadsFd < 0) {
3104         return;
3105     }
3106 
3107     LOG(DEBUG) << id() << ": removeFd(pendingReadsFd): " << pendingReadsFd;
3108 
3109     mService.mLooper->removeFd(pendingReadsFd);
3110     mService.mLooper->wake();
3111 }
3112 
setHealthListener(const StorageHealthCheckParams & healthCheckParams,StorageHealthListener && healthListener)3113 void IncrementalService::DataLoaderStub::setHealthListener(
3114         const StorageHealthCheckParams& healthCheckParams, StorageHealthListener&& healthListener) {
3115     std::lock_guard lock(mMutex);
3116     mHealthCheckParams = healthCheckParams;
3117     mHealthListener = std::move(healthListener);
3118     if (!mHealthListener) {
3119         mHealthCheckParams.blockedTimeoutMs = -1;
3120     }
3121 }
3122 
toHexString(const RawMetadata & metadata)3123 static std::string toHexString(const RawMetadata& metadata) {
3124     int n = metadata.size();
3125     std::string res(n * 2, '\0');
3126     // Same as incfs::toString(fileId)
3127     static constexpr char kHexChar[] = "0123456789abcdef";
3128     for (int i = 0; i < n; ++i) {
3129         res[i * 2] = kHexChar[(metadata[i] & 0xf0) >> 4];
3130         res[i * 2 + 1] = kHexChar[(metadata[i] & 0x0f)];
3131     }
3132     return res;
3133 }
3134 
onDump(int fd)3135 void IncrementalService::DataLoaderStub::onDump(int fd) {
3136     dprintf(fd, "    dataLoader: {\n");
3137     dprintf(fd, "      currentStatus: %d\n", mCurrentStatus);
3138     dprintf(fd, "      currentStatusTs: %lldmcs\n",
3139             (long long)(elapsedMcs(mCurrentStatusTs, Clock::now())));
3140     dprintf(fd, "      targetStatus: %d\n", mTargetStatus);
3141     dprintf(fd, "      targetStatusTs: %lldmcs\n",
3142             (long long)(elapsedMcs(mTargetStatusTs, Clock::now())));
3143     dprintf(fd, "      health: {\n");
3144     dprintf(fd, "        path: %s\n", mHealthPath.c_str());
3145     dprintf(fd, "        base: %lldmcs (%lld)\n",
3146             (long long)(elapsedMcs(mHealthBase.userTs, Clock::now())),
3147             (long long)mHealthBase.kernelTsUs);
3148     dprintf(fd, "        blockedTimeoutMs: %d\n", int(mHealthCheckParams.blockedTimeoutMs));
3149     dprintf(fd, "        unhealthyTimeoutMs: %d\n", int(mHealthCheckParams.unhealthyTimeoutMs));
3150     dprintf(fd, "        unhealthyMonitoringMs: %d\n",
3151             int(mHealthCheckParams.unhealthyMonitoringMs));
3152     dprintf(fd, "        lastPendingReads: \n");
3153     const auto control = mService.mIncFs->openMount(mHealthPath);
3154     for (auto&& pendingRead : mLastPendingReads) {
3155         dprintf(fd, "          fileId: %s\n", IncFsWrapper::toString(pendingRead.id).c_str());
3156         const auto metadata = mService.mIncFs->getMetadata(control, pendingRead.id);
3157         dprintf(fd, "          metadataHex: %s\n", toHexString(metadata).c_str());
3158         dprintf(fd, "          blockIndex: %d\n", pendingRead.block);
3159         dprintf(fd, "          bootClockTsUs: %lld\n", (long long)pendingRead.bootClockTsUs);
3160     }
3161     dprintf(fd, "        bind: %llds ago (delay: %llds)\n",
3162             (long long)(elapsedMcs(mPreviousBindTs, mService.mClock->now()) / 1000000),
3163             (long long)(mPreviousBindDelay.count() / 1000));
3164     dprintf(fd, "      }\n");
3165     const auto& params = mParams;
3166     dprintf(fd, "      dataLoaderParams: {\n");
3167     dprintf(fd, "        type: %s\n", toString(params.type).c_str());
3168     dprintf(fd, "        packageName: %s\n", params.packageName.c_str());
3169     dprintf(fd, "        className: %s\n", params.className.c_str());
3170     dprintf(fd, "        arguments: %s\n", params.arguments.c_str());
3171     dprintf(fd, "      }\n");
3172     dprintf(fd, "    }\n");
3173 }
3174 
opChanged(int32_t,const String16 &)3175 void IncrementalService::AppOpsListener::opChanged(int32_t, const String16&) {
3176     incrementalService.onAppOpChanged(packageName);
3177 }
3178 
setStorageParams(bool enableReadLogs,int32_t * _aidl_return)3179 binder::Status IncrementalService::IncrementalServiceConnector::setStorageParams(
3180         bool enableReadLogs, int32_t* _aidl_return) {
3181     *_aidl_return = incrementalService.setStorageParams(storage, enableReadLogs);
3182     return binder::Status::ok();
3183 }
3184 
idFromMetadata(std::span<const uint8_t> metadata)3185 FileId IncrementalService::idFromMetadata(std::span<const uint8_t> metadata) {
3186     return IncFs_FileIdFromMetadata({(const char*)metadata.data(), metadata.size()});
3187 }
3188 
3189 } // namespace android::incremental
3190