Merge "Checking LOADER_USAGE_STATS before enabling read logs." into rvc-dev am: 6ce90ced12

Change-Id: I9b04f7a6201cf148c310dc7c1598c40076839f64
diff --git a/VoldNativeService.cpp b/VoldNativeService.cpp
index 9bf62f3..8dcc6e5 100644
--- a/VoldNativeService.cpp
+++ b/VoldNativeService.cpp
@@ -53,6 +53,7 @@
 namespace {
 
 constexpr const char* kDump = "android.permission.DUMP";
+constexpr const char* kDataUsageStats = "android.permission.LOADER_USAGE_STATS";
 
 static binder::Status error(const std::string& msg) {
     PLOG(ERROR) << msg;
@@ -893,7 +894,8 @@
     auto control = IncFs_Mount(backingPath.c_str(), targetDir.c_str(),
                                {.flags = IncFsMountFlags(flags),
                                 .defaultReadTimeoutMs = INCFS_DEFAULT_READ_TIMEOUT_MS,
-                                .readLogBufferPages = 4});
+                                // Mount with read logs disabled.
+                                .readLogBufferPages = 0});
     if (control == nullptr) {
         return translate(-1);
     }
@@ -915,6 +917,28 @@
     return translate(IncFs_Unmount(dir.c_str()));
 }
 
+binder::Status VoldNativeService::setIncFsMountOptions(
+        const ::android::os::incremental::IncrementalFileSystemControlParcel& control,
+        bool enableReadLogs) {
+    auto status = CheckPermission(kDataUsageStats);
+    if (!status.isOk()) {
+        return status;
+    }
+
+    auto incfsControl = IncFs_CreateControl(dup(control.cmd.get()), dup(control.pendingReads.get()),
+                                            dup(control.log.get()));
+    if (auto error = IncFs_SetOptions(
+                incfsControl,
+                {.defaultReadTimeoutMs = INCFS_DEFAULT_READ_TIMEOUT_MS,
+                 .readLogBufferPages = enableReadLogs ? INCFS_DEFAULT_PAGE_READ_BUFFER_PAGES : 0});
+        error < 0) {
+        status = binder::Status::fromServiceSpecificError(error);
+    }
+    IncFs_DeleteControl(incfsControl);
+
+    return status;
+}
+
 binder::Status VoldNativeService::bindMount(const std::string& sourceDir,
                                             const std::string& targetDir) {
     ENFORCE_SYSTEM_OR_ROOT;
diff --git a/VoldNativeService.h b/VoldNativeService.h
index 9cd0435..4bf7aa0 100644
--- a/VoldNativeService.h
+++ b/VoldNativeService.h
@@ -154,6 +154,9 @@
             const std::string& backingPath, const std::string& targetDir, int32_t flags,
             ::android::os::incremental::IncrementalFileSystemControlParcel* _aidl_return) override;
     binder::Status unmountIncFs(const std::string& dir) override;
+    binder::Status setIncFsMountOptions(
+            const ::android::os::incremental::IncrementalFileSystemControlParcel& control,
+            bool enableReadLogs) override;
     binder::Status bindMount(const std::string& sourceDir, const std::string& targetDir) override;
 };
 
diff --git a/binder/android/os/IVold.aidl b/binder/android/os/IVold.aidl
index 1d5657f..68e2ba9 100644
--- a/binder/android/os/IVold.aidl
+++ b/binder/android/os/IVold.aidl
@@ -135,6 +135,7 @@
     boolean incFsEnabled();
     IncrementalFileSystemControlParcel mountIncFs(@utf8InCpp String backingPath, @utf8InCpp String targetDir, int flags);
     void unmountIncFs(@utf8InCpp String dir);
+    void setIncFsMountOptions(in IncrementalFileSystemControlParcel control, boolean enableReadLogs);
     void bindMount(@utf8InCpp String sourceDir, @utf8InCpp String targetDir);
 
     const int ENCRYPTION_FLAG_NO_UI = 4;