Per package read timeouts.

Bug: 162345970
Test: atest PackageManagerShellCommandTest PackageManagerShellCommandIncrementalTest IncrementalServiceTest PackageManagerServiceTest

Change-Id: I2599db1ed8827fff16387c11254a5d607f27ea46
diff --git a/services/incremental/BinderIncrementalService.cpp b/services/incremental/BinderIncrementalService.cpp
index a31aac9..d224428 100644
--- a/services/incremental/BinderIncrementalService.cpp
+++ b/services/incremental/BinderIncrementalService.cpp
@@ -122,13 +122,14 @@
         const ::android::sp<::android::content::pm::IDataLoaderStatusListener>& statusListener,
         const ::android::os::incremental::StorageHealthCheckParams& healthCheckParams,
         const ::android::sp<::android::os::incremental::IStorageHealthListener>& healthListener,
+        const ::std::vector<::android::os::incremental::PerUidReadTimeouts>& perUidReadTimeouts,
         int32_t* _aidl_return) {
     *_aidl_return =
             mImpl.createStorage(path, const_cast<content::pm::DataLoaderParamsParcel&&>(params),
                                 android::incremental::IncrementalService::CreateOptions(createMode),
                                 statusListener,
                                 const_cast<StorageHealthCheckParams&&>(healthCheckParams),
-                                healthListener);
+                                healthListener, perUidReadTimeouts);
     return ok();
 }
 
@@ -164,8 +165,8 @@
     return ok();
 }
 
-binder::Status BinderIncrementalService::disableReadLogs(int32_t storageId) {
-    mImpl.disableReadLogs(storageId);
+binder::Status BinderIncrementalService::disallowReadLogs(int32_t storageId) {
+    mImpl.disallowReadLogs(storageId);
     return ok();
 }
 
@@ -254,7 +255,7 @@
 
 binder::Status BinderIncrementalService::getLoadingProgress(int32_t storageId,
                                                             float* _aidl_return) {
-    *_aidl_return = mImpl.getLoadingProgress(storageId);
+    *_aidl_return = mImpl.getLoadingProgress(storageId).getProgress();
     return ok();
 }
 
diff --git a/services/incremental/BinderIncrementalService.h b/services/incremental/BinderIncrementalService.h
index 8afa0f7..9a4537a 100644
--- a/services/incremental/BinderIncrementalService.h
+++ b/services/incremental/BinderIncrementalService.h
@@ -45,6 +45,7 @@
             const ::android::sp<::android::content::pm::IDataLoaderStatusListener>& statusListener,
             const ::android::os::incremental::StorageHealthCheckParams& healthCheckParams,
             const ::android::sp<IStorageHealthListener>& healthListener,
+            const ::std::vector<::android::os::incremental::PerUidReadTimeouts>& perUidReadTimeouts,
             int32_t* _aidl_return) final;
     binder::Status createLinkedStorage(const std::string& path, int32_t otherStorageId,
                                        int32_t createMode, int32_t* _aidl_return) final;
@@ -77,7 +78,7 @@
                                    std::vector<uint8_t>* _aidl_return) final;
     binder::Status startLoading(int32_t storageId, bool* _aidl_return) final;
     binder::Status deleteStorage(int32_t storageId) final;
-    binder::Status disableReadLogs(int32_t storageId) final;
+    binder::Status disallowReadLogs(int32_t storageId) final;
     binder::Status configureNativeBinaries(int32_t storageId, const std::string& apkFullPath,
                                            const std::string& libDirRelativePath,
                                            const std::string& abi, bool extractNativeLibs,
diff --git a/services/incremental/IncrementalService.cpp b/services/incremental/IncrementalService.cpp
index eb6b325..45c9ad9 100644
--- a/services/incremental/IncrementalService.cpp
+++ b/services/incremental/IncrementalService.cpp
@@ -63,6 +63,10 @@
     static constexpr auto libSuffix = ".so"sv;
     static constexpr auto blockSize = 4096;
     static constexpr auto systemPackage = "android"sv;
+
+    static constexpr auto progressUpdateInterval = 1000ms;
+    static constexpr auto perUidTimeoutOffset = progressUpdateInterval * 2;
+    static constexpr auto minPerUidTimeout = progressUpdateInterval * 3;
 };
 
 static const Constants& constants() {
@@ -350,7 +354,8 @@
             dprintf(fd, "    storages (%d): {\n", int(mnt.storages.size()));
             for (auto&& [storageId, storage] : mnt.storages) {
                 dprintf(fd, "      [%d] -> [%s] (%d %% loaded) \n", storageId, storage.name.c_str(),
-                        (int)(getLoadingProgressFromPath(mnt, storage.name.c_str()) * 100));
+                        (int)(getLoadingProgressFromPath(mnt, storage.name.c_str()).getProgress() *
+                              100));
             }
             dprintf(fd, "    }\n");
 
@@ -419,12 +424,11 @@
     }
 }
 
-StorageId IncrementalService::createStorage(std::string_view mountPoint,
-                                            content::pm::DataLoaderParamsParcel&& dataLoaderParams,
-                                            CreateOptions options,
-                                            const DataLoaderStatusListener& statusListener,
-                                            StorageHealthCheckParams&& healthCheckParams,
-                                            const StorageHealthListener& healthListener) {
+StorageId IncrementalService::createStorage(
+        std::string_view mountPoint, content::pm::DataLoaderParamsParcel&& dataLoaderParams,
+        CreateOptions options, const DataLoaderStatusListener& statusListener,
+        StorageHealthCheckParams&& healthCheckParams, const StorageHealthListener& healthListener,
+        const std::vector<PerUidReadTimeouts>& perUidReadTimeouts) {
     LOG(INFO) << "createStorage: " << mountPoint << " | " << int(options);
     if (!path::isAbsolute(mountPoint)) {
         LOG(ERROR) << "path is not absolute: " << mountPoint;
@@ -553,13 +557,14 @@
     if (auto err = addBindMount(*ifs, storageIt->first, storageIt->second.name,
                                 std::string(storageIt->second.name), std::move(mountNorm), bk, l);
         err < 0) {
-        LOG(ERROR) << "adding bind mount failed: " << -err;
+        LOG(ERROR) << "Adding bind mount failed: " << -err;
         return kInvalidStorageId;
     }
 
     // Done here as well, all data structures are in good state.
     secondCleanupOnFailure.release();
 
+    // DataLoader.
     auto dataLoaderStub = prepareDataLoader(*ifs, std::move(dataLoaderParams), &statusListener,
                                             std::move(healthCheckParams), &healthListener);
     CHECK(dataLoaderStub);
@@ -567,6 +572,11 @@
     mountIt->second = std::move(ifs);
     l.unlock();
 
+    // Per Uid timeouts.
+    if (!perUidReadTimeouts.empty()) {
+        setUidReadTimeouts(mountId, perUidReadTimeouts);
+    }
+
     if (mSystemReady.load(std::memory_order_relaxed) && !dataLoaderStub->requestCreate()) {
         // failed to create data loader
         LOG(ERROR) << "initializeDataLoader() failed";
@@ -634,17 +644,17 @@
     return it->second->second.storage;
 }
 
-void IncrementalService::disableReadLogs(StorageId storageId) {
+void IncrementalService::disallowReadLogs(StorageId storageId) {
     std::unique_lock l(mLock);
     const auto ifs = getIfsLocked(storageId);
     if (!ifs) {
-        LOG(ERROR) << "disableReadLogs failed, invalid storageId: " << storageId;
+        LOG(ERROR) << "disallowReadLogs failed, invalid storageId: " << storageId;
         return;
     }
-    if (!ifs->readLogsEnabled()) {
+    if (!ifs->readLogsAllowed()) {
         return;
     }
-    ifs->disableReadLogs();
+    ifs->disallowReadLogs();
     l.unlock();
 
     const auto metadata = constants().readLogsDisabledMarkerName;
@@ -669,7 +679,7 @@
 
     const auto& params = ifs->dataLoaderStub->params();
     if (enableReadLogs) {
-        if (!ifs->readLogsEnabled()) {
+        if (!ifs->readLogsAllowed()) {
             LOG(ERROR) << "setStorageParams failed, readlogs disabled for storageId: " << storageId;
             return -EPERM;
         }
@@ -704,7 +714,12 @@
     }
 
     std::lock_guard l(mMountOperationLock);
-    return mVold->setIncFsMountOptions(control, enableReadLogs);
+    const auto status = mVold->setIncFsMountOptions(control, enableReadLogs);
+    if (status.isOk()) {
+        // Store enabled state.
+        ifs.setReadLogsEnabled(enableReadLogs);
+    }
+    return status;
 }
 
 void IncrementalService::deleteStorage(StorageId storageId) {
@@ -1052,6 +1067,74 @@
     return true;
 }
 
+void IncrementalService::setUidReadTimeouts(
+        StorageId storage, const std::vector<PerUidReadTimeouts>& perUidReadTimeouts) {
+    using microseconds = std::chrono::microseconds;
+    using milliseconds = std::chrono::milliseconds;
+
+    auto maxPendingTimeUs = microseconds(0);
+    for (const auto& timeouts : perUidReadTimeouts) {
+        maxPendingTimeUs = std::max(maxPendingTimeUs, microseconds(timeouts.maxPendingTimeUs));
+    }
+    if (maxPendingTimeUs < Constants::minPerUidTimeout) {
+        return;
+    }
+
+    const auto ifs = getIfs(storage);
+    if (!ifs) {
+        return;
+    }
+
+    if (auto err = mIncFs->setUidReadTimeouts(ifs->control, perUidReadTimeouts); err < 0) {
+        LOG(ERROR) << "Setting read timeouts failed: " << -err;
+        return;
+    }
+
+    const auto timeout = std::chrono::duration_cast<milliseconds>(maxPendingTimeUs) -
+            Constants::perUidTimeoutOffset;
+    updateUidReadTimeouts(storage, Clock::now() + timeout);
+}
+
+void IncrementalService::clearUidReadTimeouts(StorageId storage) {
+    const auto ifs = getIfs(storage);
+    if (!ifs) {
+        return;
+    }
+
+    mIncFs->setUidReadTimeouts(ifs->control, {});
+}
+
+void IncrementalService::updateUidReadTimeouts(StorageId storage, Clock::time_point timeLimit) {
+    // Reached maximum timeout.
+    if (Clock::now() >= timeLimit) {
+        return clearUidReadTimeouts(storage);
+    }
+
+    // Still loading?
+    const auto progress = getLoadingProgress(storage);
+    if (progress.isError()) {
+        // Something is wrong, abort.
+        return clearUidReadTimeouts(storage);
+    }
+
+    if (progress.started() && progress.fullyLoaded()) {
+        // Fully loaded, check readLogs collection.
+        const auto ifs = getIfs(storage);
+        if (!ifs->readLogsEnabled()) {
+            return clearUidReadTimeouts(storage);
+        }
+    }
+
+    const auto timeLeft = timeLimit - Clock::now();
+    if (timeLeft < Constants::progressUpdateInterval) {
+        // Don't bother.
+        return clearUidReadTimeouts(storage);
+    }
+
+    addTimedJob(*mTimedQueue, storage, Constants::progressUpdateInterval,
+                [this, storage, timeLimit]() { updateUidReadTimeouts(storage, timeLimit); });
+}
+
 std::unordered_set<std::string_view> IncrementalService::adoptMountedInstances() {
     std::unordered_set<std::string_view> mountedRootNames;
     mIncFs->listExistingMounts([this, &mountedRootNames](auto root, auto backingDir, auto binds) {
@@ -1125,7 +1208,7 @@
 
         // Check if marker file present.
         if (checkReadLogsDisabledMarker(root)) {
-            ifs->disableReadLogs();
+            ifs->disallowReadLogs();
         }
 
         std::vector<std::pair<std::string, metadata::BindPoint>> permanentBindPoints;
@@ -1301,7 +1384,7 @@
 
     // Check if marker file present.
     if (checkReadLogsDisabledMarker(mountTarget)) {
-        ifs->disableReadLogs();
+        ifs->disallowReadLogs();
     }
 
     // DataLoader params
@@ -1705,7 +1788,7 @@
     return 0;
 }
 
-int IncrementalService::isFileFullyLoaded(StorageId storage, const std::string& path) const {
+int IncrementalService::isFileFullyLoaded(StorageId storage, std::string_view filePath) const {
     std::unique_lock l(mLock);
     const auto ifs = getIfsLocked(storage);
     if (!ifs) {
@@ -1718,7 +1801,7 @@
         return -EINVAL;
     }
     l.unlock();
-    return isFileFullyLoadedFromPath(*ifs, path);
+    return isFileFullyLoadedFromPath(*ifs, filePath);
 }
 
 int IncrementalService::isFileFullyLoadedFromPath(const IncFsMount& ifs,
@@ -1736,25 +1819,26 @@
     return totalBlocks - filledBlocks;
 }
 
-float IncrementalService::getLoadingProgress(StorageId storage) const {
+IncrementalService::LoadingProgress IncrementalService::getLoadingProgress(
+        StorageId storage) const {
     std::unique_lock l(mLock);
     const auto ifs = getIfsLocked(storage);
     if (!ifs) {
         LOG(ERROR) << "getLoadingProgress failed, invalid storageId: " << storage;
-        return -EINVAL;
+        return {-EINVAL, -EINVAL};
     }
     const auto storageInfo = ifs->storages.find(storage);
     if (storageInfo == ifs->storages.end()) {
         LOG(ERROR) << "getLoadingProgress failed, no storage: " << storage;
-        return -EINVAL;
+        return {-EINVAL, -EINVAL};
     }
     l.unlock();
     return getLoadingProgressFromPath(*ifs, storageInfo->second.name);
 }
 
-float IncrementalService::getLoadingProgressFromPath(const IncFsMount& ifs,
-                                                     std::string_view storagePath) const {
-    size_t totalBlocks = 0, filledBlocks = 0;
+IncrementalService::LoadingProgress IncrementalService::getLoadingProgressFromPath(
+        const IncFsMount& ifs, std::string_view storagePath) const {
+    ssize_t totalBlocks = 0, filledBlocks = 0;
     const auto filePaths = mFs->listFilesRecursive(storagePath);
     for (const auto& filePath : filePaths) {
         const auto [filledBlocksCount, totalBlocksCount] =
@@ -1762,33 +1846,29 @@
         if (filledBlocksCount < 0) {
             LOG(ERROR) << "getLoadingProgress failed to get filled blocks count for: " << filePath
                        << " errno: " << filledBlocksCount;
-            return filledBlocksCount;
+            return {filledBlocksCount, filledBlocksCount};
         }
         totalBlocks += totalBlocksCount;
         filledBlocks += filledBlocksCount;
     }
 
-    if (totalBlocks == 0) {
-        // No file in the storage or files are empty; regarded as fully loaded
-        return 1;
-    }
-    return (float)filledBlocks / (float)totalBlocks;
+    return {filledBlocks, totalBlocks};
 }
 
 bool IncrementalService::updateLoadingProgress(
         StorageId storage, const StorageLoadingProgressListener& progressListener) {
     const auto progress = getLoadingProgress(storage);
-    if (progress < 0) {
+    if (progress.isError()) {
         // Failed to get progress from incfs, abort.
         return false;
     }
-    progressListener->onStorageLoadingProgressChanged(storage, progress);
-    if (progress > 1 - 0.001f) {
+    progressListener->onStorageLoadingProgressChanged(storage, progress.getProgress());
+    if (progress.fullyLoaded()) {
         // Stop updating progress once it is fully loaded
         return true;
     }
-    static constexpr auto kProgressUpdateInterval = 1000ms;
-    addTimedJob(*mProgressUpdateJobQueue, storage, kProgressUpdateInterval /* repeat after 1s */,
+    addTimedJob(*mProgressUpdateJobQueue, storage,
+                Constants::progressUpdateInterval /* repeat after 1s */,
                 [storage, progressListener, this]() {
                     updateLoadingProgress(storage, progressListener);
                 });
diff --git a/services/incremental/IncrementalService.h b/services/incremental/IncrementalService.h
index eb69470..3066121 100644
--- a/services/incremental/IncrementalService.h
+++ b/services/incremental/IncrementalService.h
@@ -23,6 +23,7 @@
 #include <android/os/incremental/BnIncrementalServiceConnector.h>
 #include <android/os/incremental/BnStorageHealthListener.h>
 #include <android/os/incremental/BnStorageLoadingProgressListener.h>
+#include <android/os/incremental/PerUidReadTimeouts.h>
 #include <android/os/incremental/StorageHealthCheckParams.h>
 #include <binder/IAppOpsCallback.h>
 #include <utils/String16.h>
@@ -69,6 +70,8 @@
 using IStorageLoadingProgressListener = ::android::os::incremental::IStorageLoadingProgressListener;
 using StorageLoadingProgressListener = ::android::sp<IStorageLoadingProgressListener>;
 
+using PerUidReadTimeouts = ::android::os::incremental::PerUidReadTimeouts;
+
 class IncrementalService final {
 public:
     explicit IncrementalService(ServiceManagerWrapper&& sm, std::string_view rootDir);
@@ -98,7 +101,23 @@
     };
 
     enum StorageFlags {
-        ReadLogsEnabled = 1,
+        ReadLogsAllowed = 1 << 0,
+        ReadLogsEnabled = 1 << 1,
+    };
+
+    struct LoadingProgress {
+        ssize_t filledBlocks;
+        ssize_t totalBlocks;
+
+        bool isError() const { return totalBlocks < 0; }
+        bool started() const { return totalBlocks > 0; }
+        bool fullyLoaded() const { return !isError() && (totalBlocks == filledBlocks); }
+
+        float getProgress() const {
+            return totalBlocks < 0
+                    ? totalBlocks
+                    : totalBlocks > 0 ? double(filledBlocks) / double(totalBlocks) : 1.f;
+        }
     };
 
     static FileId idFromMetadata(std::span<const uint8_t> metadata);
@@ -114,7 +133,8 @@
                             content::pm::DataLoaderParamsParcel&& dataLoaderParams,
                             CreateOptions options, const DataLoaderStatusListener& statusListener,
                             StorageHealthCheckParams&& healthCheckParams,
-                            const StorageHealthListener& healthListener);
+                            const StorageHealthListener& healthListener,
+                            const std::vector<PerUidReadTimeouts>& perUidReadTimeouts);
     StorageId createLinkedStorage(std::string_view mountPoint, StorageId linkedStorage,
                                   CreateOptions options = CreateOptions::Default);
     StorageId openStorage(std::string_view path);
@@ -123,7 +143,7 @@
     int unbind(StorageId storage, std::string_view target);
     void deleteStorage(StorageId storage);
 
-    void disableReadLogs(StorageId storage);
+    void disallowReadLogs(StorageId storage);
     int setStorageParams(StorageId storage, bool enableReadLogs);
 
     int makeFile(StorageId storage, std::string_view path, int mode, FileId id,
@@ -135,8 +155,8 @@
              std::string_view newPath);
     int unlink(StorageId storage, std::string_view path);
 
-    int isFileFullyLoaded(StorageId storage, const std::string& path) const;
-    float getLoadingProgress(StorageId storage) const;
+    int isFileFullyLoaded(StorageId storage, std::string_view filePath) const;
+    LoadingProgress getLoadingProgress(StorageId storage) const;
     bool registerLoadingProgressListener(StorageId storage,
                                          const StorageLoadingProgressListener& progressListener);
     bool unregisterLoadingProgressListener(StorageId storage);
@@ -282,7 +302,7 @@
         const std::string root;
         Control control;
         /*const*/ MountId mountId;
-        int32_t flags = StorageFlags::ReadLogsEnabled;
+        int32_t flags = StorageFlags::ReadLogsAllowed;
         StorageMap storages;
         BindMap bindPoints;
         DataLoaderStubPtr dataLoaderStub;
@@ -301,7 +321,15 @@
 
         StorageMap::iterator makeStorage(StorageId id);
 
-        void disableReadLogs() { flags &= ~StorageFlags::ReadLogsEnabled; }
+        void disallowReadLogs() { flags &= ~StorageFlags::ReadLogsAllowed; }
+        int32_t readLogsAllowed() const { return (flags & StorageFlags::ReadLogsAllowed); }
+
+        void setReadLogsEnabled(bool value) {
+            if (value)
+                flags |= StorageFlags::ReadLogsEnabled;
+            else
+                flags &= ~StorageFlags::ReadLogsEnabled;
+        }
         int32_t readLogsEnabled() const { return (flags & StorageFlags::ReadLogsEnabled); }
 
         static void cleanupFilesystem(std::string_view root);
@@ -313,6 +341,11 @@
 
     static bool perfLoggingEnabled();
 
+    void setUidReadTimeouts(StorageId storage,
+                            const std::vector<PerUidReadTimeouts>& perUidReadTimeouts);
+    void clearUidReadTimeouts(StorageId storage);
+    void updateUidReadTimeouts(StorageId storage, Clock::time_point timeLimit);
+
     std::unordered_set<std::string_view> adoptMountedInstances();
     void mountExistingImages(const std::unordered_set<std::string_view>& mountedRootNames);
     bool mountExistingImage(std::string_view root);
@@ -355,7 +388,7 @@
     binder::Status applyStorageParams(IncFsMount& ifs, bool enableReadLogs);
 
     int isFileFullyLoadedFromPath(const IncFsMount& ifs, std::string_view filePath) const;
-    float getLoadingProgressFromPath(const IncFsMount& ifs, std::string_view path) const;
+    LoadingProgress getLoadingProgressFromPath(const IncFsMount& ifs, std::string_view path) const;
 
     int setFileContent(const IfsMountPtr& ifs, const incfs::FileId& fileId,
                        std::string_view debugFilePath, std::span<const uint8_t> data) const;
diff --git a/services/incremental/ServiceWrappers.cpp b/services/incremental/ServiceWrappers.cpp
index dfe9684..b1521b0 100644
--- a/services/incremental/ServiceWrappers.cpp
+++ b/services/incremental/ServiceWrappers.cpp
@@ -206,6 +206,11 @@
                                    std::vector<incfs::ReadInfo>* pendingReadsBuffer) const final {
         return incfs::waitForPendingReads(control, timeout, pendingReadsBuffer);
     }
+    ErrorCode setUidReadTimeouts(const Control& control,
+                                 const std::vector<android::os::incremental::PerUidReadTimeouts>&
+                                         perUidReadTimeouts) const final {
+        return -ENOTSUP;
+    }
 };
 
 static JNIEnv* getOrAttachJniEnv(JavaVM* jvm);
diff --git a/services/incremental/ServiceWrappers.h b/services/incremental/ServiceWrappers.h
index f2d0073..fad8d67 100644
--- a/services/incremental/ServiceWrappers.h
+++ b/services/incremental/ServiceWrappers.h
@@ -21,6 +21,7 @@
 #include <android/content/pm/FileSystemControlParcel.h>
 #include <android/content/pm/IDataLoader.h>
 #include <android/content/pm/IDataLoaderStatusListener.h>
+#include <android/os/incremental/PerUidReadTimeouts.h>
 #include <binder/IAppOpsCallback.h>
 #include <binder/IServiceManager.h>
 #include <binder/Status.h>
@@ -103,6 +104,10 @@
     virtual WaitResult waitForPendingReads(
             const Control& control, std::chrono::milliseconds timeout,
             std::vector<incfs::ReadInfo>* pendingReadsBuffer) const = 0;
+    virtual ErrorCode setUidReadTimeouts(
+            const Control& control,
+            const std::vector<::android::os::incremental::PerUidReadTimeouts>& perUidReadTimeouts)
+            const = 0;
 };
 
 class AppOpsManagerWrapper {
diff --git a/services/incremental/test/IncrementalServiceTest.cpp b/services/incremental/test/IncrementalServiceTest.cpp
index 9b8cf40..02eaa2f 100644
--- a/services/incremental/test/IncrementalServiceTest.cpp
+++ b/services/incremental/test/IncrementalServiceTest.cpp
@@ -42,6 +42,7 @@
 
 using namespace android::incfs;
 using namespace android::content::pm;
+using PerUidReadTimeouts = android::os::incremental::PerUidReadTimeouts;
 
 namespace android::os::incremental {
 
@@ -307,6 +308,9 @@
     MOCK_CONST_METHOD3(waitForPendingReads,
                        WaitResult(const Control& control, std::chrono::milliseconds timeout,
                                   std::vector<incfs::ReadInfo>* pendingReadsBuffer));
+    MOCK_CONST_METHOD2(setUidReadTimeouts,
+                       ErrorCode(const Control& control,
+                                 const std::vector<PerUidReadTimeouts>& perUidReadTimeouts));
 
     MockIncFs() { ON_CALL(*this, listExistingMounts(_)).WillByDefault(Return()); }
 
@@ -665,7 +669,7 @@
     TemporaryDir tempDir;
     int storageId = mIncrementalService->createStorage(tempDir.path, std::move(mDataLoaderParcel),
                                                        IncrementalService::CreateOptions::CreateNew,
-                                                       {}, {}, {});
+                                                       {}, {}, {}, {});
     ASSERT_LT(storageId, 0);
 }
 
@@ -676,7 +680,7 @@
     TemporaryDir tempDir;
     int storageId = mIncrementalService->createStorage(tempDir.path, std::move(mDataLoaderParcel),
                                                        IncrementalService::CreateOptions::CreateNew,
-                                                       {}, {}, {});
+                                                       {}, {}, {}, {});
     ASSERT_LT(storageId, 0);
 }
 
@@ -689,7 +693,7 @@
     TemporaryDir tempDir;
     int storageId = mIncrementalService->createStorage(tempDir.path, std::move(mDataLoaderParcel),
                                                        IncrementalService::CreateOptions::CreateNew,
-                                                       {}, {}, {});
+                                                       {}, {}, {}, {});
     ASSERT_LT(storageId, 0);
 }
 
@@ -703,7 +707,7 @@
     TemporaryDir tempDir;
     int storageId = mIncrementalService->createStorage(tempDir.path, std::move(mDataLoaderParcel),
                                                        IncrementalService::CreateOptions::CreateNew,
-                                                       {}, {}, {});
+                                                       {}, {}, {}, {});
     ASSERT_LT(storageId, 0);
 }
 
@@ -721,7 +725,7 @@
     TemporaryDir tempDir;
     int storageId = mIncrementalService->createStorage(tempDir.path, std::move(mDataLoaderParcel),
                                                        IncrementalService::CreateOptions::CreateNew,
-                                                       {}, {}, {});
+                                                       {}, {}, {}, {});
     ASSERT_LT(storageId, 0);
 }
 
@@ -735,7 +739,7 @@
     TemporaryDir tempDir;
     int storageId = mIncrementalService->createStorage(tempDir.path, std::move(mDataLoaderParcel),
                                                        IncrementalService::CreateOptions::CreateNew,
-                                                       {}, {}, {});
+                                                       {}, {}, {}, {});
     ASSERT_GE(storageId, 0);
     mIncrementalService->deleteStorage(storageId);
 }
@@ -750,7 +754,7 @@
     TemporaryDir tempDir;
     int storageId = mIncrementalService->createStorage(tempDir.path, std::move(mDataLoaderParcel),
                                                        IncrementalService::CreateOptions::CreateNew,
-                                                       {}, {}, {});
+                                                       {}, {}, {}, {});
     ASSERT_GE(storageId, 0);
     // Simulated crash/other connection breakage.
     mDataLoaderManager->setDataLoaderStatusDestroyed();
@@ -767,7 +771,7 @@
     TemporaryDir tempDir;
     int storageId = mIncrementalService->createStorage(tempDir.path, std::move(mDataLoaderParcel),
                                                        IncrementalService::CreateOptions::CreateNew,
-                                                       {}, {}, {});
+                                                       {}, {}, {}, {});
     ASSERT_GE(storageId, 0);
     mDataLoaderManager->setDataLoaderStatusCreated();
     ASSERT_TRUE(mIncrementalService->startLoading(storageId));
@@ -785,7 +789,7 @@
     TemporaryDir tempDir;
     int storageId = mIncrementalService->createStorage(tempDir.path, std::move(mDataLoaderParcel),
                                                        IncrementalService::CreateOptions::CreateNew,
-                                                       {}, {}, {});
+                                                       {}, {}, {}, {});
     ASSERT_GE(storageId, 0);
     ASSERT_TRUE(mIncrementalService->startLoading(storageId));
     mDataLoaderManager->setDataLoaderStatusCreated();
@@ -802,7 +806,7 @@
     TemporaryDir tempDir;
     int storageId = mIncrementalService->createStorage(tempDir.path, std::move(mDataLoaderParcel),
                                                        IncrementalService::CreateOptions::CreateNew,
-                                                       {}, {}, {});
+                                                       {}, {}, {}, {});
     ASSERT_GE(storageId, 0);
     mDataLoaderManager->setDataLoaderStatusUnavailable();
 }
@@ -823,7 +827,7 @@
     TemporaryDir tempDir;
     int storageId = mIncrementalService->createStorage(tempDir.path, std::move(mDataLoaderParcel),
                                                        IncrementalService::CreateOptions::CreateNew,
-                                                       {}, {}, {});
+                                                       {}, {}, {}, {});
     ASSERT_GE(storageId, 0);
     mDataLoaderManager->setDataLoaderStatusUnavailable();
     ASSERT_NE(nullptr, mLooper->mCallback);
@@ -877,7 +881,7 @@
     TemporaryDir tempDir;
     int storageId = mIncrementalService->createStorage(tempDir.path, std::move(mDataLoaderParcel),
                                                        IncrementalService::CreateOptions::CreateNew,
-                                                       {}, std::move(params), listener);
+                                                       {}, std::move(params), listener, {});
     ASSERT_GE(storageId, 0);
 
     // Healthy state, registered for pending reads.
@@ -972,7 +976,7 @@
     TemporaryDir tempDir;
     int storageId = mIncrementalService->createStorage(tempDir.path, std::move(mDataLoaderParcel),
                                                        IncrementalService::CreateOptions::CreateNew,
-                                                       {}, {}, {});
+                                                       {}, {}, {}, {});
     ASSERT_GE(storageId, 0);
     ASSERT_GE(mDataLoader->setStorageParams(true), 0);
 }
@@ -993,11 +997,11 @@
     TemporaryDir tempDir;
     int storageId = mIncrementalService->createStorage(tempDir.path, std::move(mDataLoaderParcel),
                                                        IncrementalService::CreateOptions::CreateNew,
-                                                       {}, {}, {});
+                                                       {}, {}, {}, {});
     ASSERT_GE(storageId, 0);
     ASSERT_GE(mDataLoader->setStorageParams(true), 0);
     // Now disable.
-    mIncrementalService->disableReadLogs(storageId);
+    mIncrementalService->disallowReadLogs(storageId);
     ASSERT_EQ(mDataLoader->setStorageParams(true), -EPERM);
 }
 
@@ -1019,7 +1023,7 @@
     TemporaryDir tempDir;
     int storageId = mIncrementalService->createStorage(tempDir.path, std::move(mDataLoaderParcel),
                                                        IncrementalService::CreateOptions::CreateNew,
-                                                       {}, {}, {});
+                                                       {}, {}, {}, {});
     ASSERT_GE(storageId, 0);
     ASSERT_GE(mDataLoader->setStorageParams(true), 0);
     ASSERT_NE(nullptr, mAppOpsManager->mStoredCallback.get());
@@ -1038,7 +1042,7 @@
     TemporaryDir tempDir;
     int storageId = mIncrementalService->createStorage(tempDir.path, std::move(mDataLoaderParcel),
                                                        IncrementalService::CreateOptions::CreateNew,
-                                                       {}, {}, {});
+                                                       {}, {}, {}, {});
     ASSERT_GE(storageId, 0);
     ASSERT_LT(mDataLoader->setStorageParams(true), 0);
 }
@@ -1057,7 +1061,7 @@
     TemporaryDir tempDir;
     int storageId = mIncrementalService->createStorage(tempDir.path, std::move(mDataLoaderParcel),
                                                        IncrementalService::CreateOptions::CreateNew,
-                                                       {}, {}, {});
+                                                       {}, {}, {}, {});
     ASSERT_GE(storageId, 0);
     ASSERT_LT(mDataLoader->setStorageParams(true), 0);
 }
@@ -1066,7 +1070,7 @@
     TemporaryDir tempDir;
     int storageId = mIncrementalService->createStorage(tempDir.path, std::move(mDataLoaderParcel),
                                                        IncrementalService::CreateOptions::CreateNew,
-                                                       {}, {}, {});
+                                                       {}, {}, {}, {});
     std::string dir_path("test");
 
     // Expecting incfs to call makeDir on a path like:
@@ -1085,7 +1089,7 @@
     TemporaryDir tempDir;
     int storageId = mIncrementalService->createStorage(tempDir.path, std::move(mDataLoaderParcel),
                                                        IncrementalService::CreateOptions::CreateNew,
-                                                       {}, {}, {});
+                                                       {}, {}, {}, {});
     auto first = "first"sv;
     auto second = "second"sv;
     auto third = "third"sv;
@@ -1108,7 +1112,7 @@
     TemporaryDir tempDir;
     int storageId = mIncrementalService->createStorage(tempDir.path, std::move(mDataLoaderParcel),
                                                        IncrementalService::CreateOptions::CreateNew,
-                                                       {}, {}, {});
+                                                       {}, {}, {}, {});
     ASSERT_EQ(-1, mIncrementalService->isFileFullyLoaded(storageId, "base.apk"));
 }
 
@@ -1119,7 +1123,7 @@
     TemporaryDir tempDir;
     int storageId = mIncrementalService->createStorage(tempDir.path, std::move(mDataLoaderParcel),
                                                        IncrementalService::CreateOptions::CreateNew,
-                                                       {}, {}, {});
+                                                       {}, {}, {}, {});
     EXPECT_CALL(*mIncFs, countFilledBlocks(_, _)).Times(1);
     ASSERT_EQ(-1, mIncrementalService->isFileFullyLoaded(storageId, "base.apk"));
 }
@@ -1131,7 +1135,7 @@
     TemporaryDir tempDir;
     int storageId = mIncrementalService->createStorage(tempDir.path, std::move(mDataLoaderParcel),
                                                        IncrementalService::CreateOptions::CreateNew,
-                                                       {}, {}, {});
+                                                       {}, {}, {}, {});
     EXPECT_CALL(*mIncFs, countFilledBlocks(_, _)).Times(1);
     ASSERT_EQ(0, mIncrementalService->isFileFullyLoaded(storageId, "base.apk"));
 }
@@ -1143,7 +1147,7 @@
     TemporaryDir tempDir;
     int storageId = mIncrementalService->createStorage(tempDir.path, std::move(mDataLoaderParcel),
                                                        IncrementalService::CreateOptions::CreateNew,
-                                                       {}, {}, {});
+                                                       {}, {}, {}, {});
     EXPECT_CALL(*mIncFs, countFilledBlocks(_, _)).Times(1);
     ASSERT_EQ(0, mIncrementalService->isFileFullyLoaded(storageId, "base.apk"));
 }
@@ -1155,8 +1159,8 @@
     TemporaryDir tempDir;
     int storageId = mIncrementalService->createStorage(tempDir.path, std::move(mDataLoaderParcel),
                                                        IncrementalService::CreateOptions::CreateNew,
-                                                       {}, {}, {});
-    ASSERT_EQ(1, mIncrementalService->getLoadingProgress(storageId));
+                                                       {}, {}, {}, {});
+    ASSERT_EQ(1, mIncrementalService->getLoadingProgress(storageId).getProgress());
 }
 
 TEST_F(IncrementalServiceTest, testGetLoadingProgressFailsWithFailedRanges) {
@@ -1166,9 +1170,9 @@
     TemporaryDir tempDir;
     int storageId = mIncrementalService->createStorage(tempDir.path, std::move(mDataLoaderParcel),
                                                        IncrementalService::CreateOptions::CreateNew,
-                                                       {}, {}, {});
+                                                       {}, {}, {}, {});
     EXPECT_CALL(*mIncFs, countFilledBlocks(_, _)).Times(1);
-    ASSERT_EQ(-1, mIncrementalService->getLoadingProgress(storageId));
+    ASSERT_EQ(-1, mIncrementalService->getLoadingProgress(storageId).getProgress());
 }
 
 TEST_F(IncrementalServiceTest, testGetLoadingProgressSuccessWithEmptyRanges) {
@@ -1178,9 +1182,9 @@
     TemporaryDir tempDir;
     int storageId = mIncrementalService->createStorage(tempDir.path, std::move(mDataLoaderParcel),
                                                        IncrementalService::CreateOptions::CreateNew,
-                                                       {}, {}, {});
+                                                       {}, {}, {}, {});
     EXPECT_CALL(*mIncFs, countFilledBlocks(_, _)).Times(3);
-    ASSERT_EQ(1, mIncrementalService->getLoadingProgress(storageId));
+    ASSERT_EQ(1, mIncrementalService->getLoadingProgress(storageId).getProgress());
 }
 
 TEST_F(IncrementalServiceTest, testGetLoadingProgressSuccess) {
@@ -1190,9 +1194,9 @@
     TemporaryDir tempDir;
     int storageId = mIncrementalService->createStorage(tempDir.path, std::move(mDataLoaderParcel),
                                                        IncrementalService::CreateOptions::CreateNew,
-                                                       {}, {}, {});
+                                                       {}, {}, {}, {});
     EXPECT_CALL(*mIncFs, countFilledBlocks(_, _)).Times(3);
-    ASSERT_EQ(0.5, mIncrementalService->getLoadingProgress(storageId));
+    ASSERT_EQ(0.5, mIncrementalService->getLoadingProgress(storageId).getProgress());
 }
 
 TEST_F(IncrementalServiceTest, testRegisterLoadingProgressListenerSuccess) {
@@ -1202,7 +1206,7 @@
     TemporaryDir tempDir;
     int storageId = mIncrementalService->createStorage(tempDir.path, std::move(mDataLoaderParcel),
                                                        IncrementalService::CreateOptions::CreateNew,
-                                                       {}, {}, {});
+                                                       {}, {}, {}, {});
     sp<NiceMock<MockStorageLoadingProgressListener>> listener{
             new NiceMock<MockStorageLoadingProgressListener>};
     NiceMock<MockStorageLoadingProgressListener>* listenerMock = listener.get();
@@ -1227,7 +1231,7 @@
     TemporaryDir tempDir;
     int storageId = mIncrementalService->createStorage(tempDir.path, std::move(mDataLoaderParcel),
                                                        IncrementalService::CreateOptions::CreateNew,
-                                                       {}, {}, {});
+                                                       {}, {}, {}, {});
     sp<NiceMock<MockStorageLoadingProgressListener>> listener{
             new NiceMock<MockStorageLoadingProgressListener>};
     NiceMock<MockStorageLoadingProgressListener>* listenerMock = listener.get();
@@ -1242,9 +1246,10 @@
     NiceMock<MockStorageHealthListener>* newListenerMock = newListener.get();
 
     TemporaryDir tempDir;
-    int storageId = mIncrementalService->createStorage(tempDir.path, std::move(mDataLoaderParcel),
-                                                       IncrementalService::CreateOptions::CreateNew,
-                                                       {}, StorageHealthCheckParams{}, listener);
+    int storageId =
+            mIncrementalService->createStorage(tempDir.path, std::move(mDataLoaderParcel),
+                                               IncrementalService::CreateOptions::CreateNew, {},
+                                               StorageHealthCheckParams{}, listener, {});
     ASSERT_GE(storageId, 0);
     StorageHealthCheckParams newParams;
     newParams.blockedTimeoutMs = 10000;
@@ -1313,4 +1318,123 @@
     mTimedQueue->clearJob(storageId);
 }
 
+static std::vector<PerUidReadTimeouts> createPerUidTimeouts(
+        std::initializer_list<std::tuple<int, int, int, int>> tuples) {
+    std::vector<PerUidReadTimeouts> result;
+    for (auto&& tuple : tuples) {
+        result.emplace_back();
+        auto& timeouts = result.back();
+        timeouts.uid = std::get<0>(tuple);
+        timeouts.minTimeUs = std::get<1>(tuple);
+        timeouts.minPendingTimeUs = std::get<2>(tuple);
+        timeouts.maxPendingTimeUs = std::get<3>(tuple);
+    }
+    return result;
+}
+
+static ErrorCode checkPerUidTimeouts(const Control& control,
+                                     const std::vector<PerUidReadTimeouts>& perUidReadTimeouts) {
+    std::vector<PerUidReadTimeouts> expected =
+            createPerUidTimeouts({{0, 1, 2, 3}, {1, 2, 3, 4}, {2, 3, 4, 100000000}});
+    EXPECT_EQ(expected, perUidReadTimeouts);
+    return 0;
+}
+
+static ErrorCode checkPerUidTimeoutsEmpty(
+        const Control& control, const std::vector<PerUidReadTimeouts>& perUidReadTimeouts) {
+    EXPECT_EQ(0u, perUidReadTimeouts.size());
+    return 0;
+}
+
+TEST_F(IncrementalServiceTest, testPerUidTimeoutsTooShort) {
+    EXPECT_CALL(*mDataLoaderManager, bindToDataLoader(_, _, _, _)).Times(1);
+    EXPECT_CALL(*mDataLoaderManager, unbindFromDataLoader(_)).Times(1);
+    EXPECT_CALL(*mDataLoader, create(_, _, _, _)).Times(1);
+    EXPECT_CALL(*mDataLoader, start(_)).Times(0);
+    EXPECT_CALL(*mDataLoader, destroy(_)).Times(1);
+    EXPECT_CALL(*mIncFs, setUidReadTimeouts(_, _)).Times(0);
+    EXPECT_CALL(*mTimedQueue, addJob(_, _, _)).Times(0);
+    EXPECT_CALL(*mVold, unmountIncFs(_)).Times(2);
+    TemporaryDir tempDir;
+    int storageId =
+            mIncrementalService->createStorage(tempDir.path, std::move(mDataLoaderParcel),
+                                               IncrementalService::CreateOptions::CreateNew, {}, {},
+                                               {},
+                                               createPerUidTimeouts(
+                                                       {{0, 1, 2, 3}, {1, 2, 3, 4}, {2, 3, 4, 5}}));
+    ASSERT_GE(storageId, 0);
+}
+
+TEST_F(IncrementalServiceTest, testPerUidTimeoutsSuccess) {
+    mVold->setIncFsMountOptionsSuccess();
+    mAppOpsManager->checkPermissionSuccess();
+    mFs->hasFiles();
+
+    EXPECT_CALL(*mIncFs, setUidReadTimeouts(_, _))
+            // First call.
+            .WillOnce(Invoke(&checkPerUidTimeouts))
+            // Fully loaded and no readlogs.
+            .WillOnce(Invoke(&checkPerUidTimeoutsEmpty));
+    EXPECT_CALL(*mTimedQueue, addJob(_, _, _)).Times(3);
+
+    // Empty storage.
+    mIncFs->countFilledBlocksEmpty();
+
+    TemporaryDir tempDir;
+    int storageId =
+            mIncrementalService->createStorage(tempDir.path, std::move(mDataLoaderParcel),
+                                               IncrementalService::CreateOptions::CreateNew, {}, {},
+                                               {},
+                                               createPerUidTimeouts({{0, 1, 2, 3},
+                                                                     {1, 2, 3, 4},
+                                                                     {2, 3, 4, 100000000}}));
+    ASSERT_GE(storageId, 0);
+
+    {
+        // Timed callback present -> 0 progress.
+        ASSERT_EQ(storageId, mTimedQueue->mId);
+        ASSERT_GE(mTimedQueue->mAfter, std::chrono::seconds(1));
+        const auto timedCallback = mTimedQueue->mWhat;
+        mTimedQueue->clearJob(storageId);
+
+        // Still loading.
+        mIncFs->countFilledBlocksSuccess();
+
+        // Call it again.
+        timedCallback();
+    }
+
+    {
+        // Still present -> 0.5 progress.
+        ASSERT_EQ(storageId, mTimedQueue->mId);
+        ASSERT_GE(mTimedQueue->mAfter, std::chrono::seconds(1));
+        const auto timedCallback = mTimedQueue->mWhat;
+        mTimedQueue->clearJob(storageId);
+
+        // Fully loaded but readlogs collection enabled.
+        mIncFs->countFilledBlocksFullyLoaded();
+        ASSERT_GE(mDataLoader->setStorageParams(true), 0);
+
+        // Call it again.
+        timedCallback();
+    }
+
+    {
+        // Still present -> fully loaded + readlogs.
+        ASSERT_EQ(storageId, mTimedQueue->mId);
+        ASSERT_GE(mTimedQueue->mAfter, std::chrono::seconds(1));
+        const auto timedCallback = mTimedQueue->mWhat;
+        mTimedQueue->clearJob(storageId);
+
+        // Now disable readlogs.
+        ASSERT_GE(mDataLoader->setStorageParams(false), 0);
+
+        // Call it again.
+        timedCallback();
+    }
+
+    // No callbacks anymore -> fully loaded and no readlogs.
+    ASSERT_EQ(mTimedQueue->mAfter, Milliseconds());
+}
+
 } // namespace android::os::incremental