[incfs] Space trimming for IncFS v1

Run a manual timed job that trims all files one by one on the
old version of IncFS, where it didn't do it automatically.

Bug: 183435580
Fixes: 183436717
Test: atest libincfs-test service.incremental_test
Change-Id: I57885b2826e383814822c767802f837135fd8464
diff --git a/services/incremental/IncrementalService.cpp b/services/incremental/IncrementalService.cpp
index 9bb2f04..94f8e59 100644
--- a/services/incremental/IncrementalService.cpp
+++ b/services/incremental/IncrementalService.cpp
@@ -1086,17 +1086,14 @@
         return err;
     }
     if (params.size > 0) {
-        // Only v2+ incfs supports automatically trimming file over-reserved sizes
-        if (mIncFs->features() & incfs::Features::v2) {
-            if (auto err = mIncFs->reserveSpace(ifs->control, normPath, params.size)) {
-                if (err != -EOPNOTSUPP) {
-                    LOG(ERROR) << "Failed to reserve space for a new file: " << err;
-                    (void)mIncFs->unlink(ifs->control, normPath);
-                    return err;
-                } else {
-                    LOG(WARNING) << "Reserving space for backing file isn't supported, "
-                                    "may run out of disk later";
-                }
+        if (auto err = mIncFs->reserveSpace(ifs->control, id, params.size)) {
+            if (err != -EOPNOTSUPP) {
+                LOG(ERROR) << "Failed to reserve space for a new file: " << err;
+                (void)mIncFs->unlink(ifs->control, normPath);
+                return err;
+            } else {
+                LOG(WARNING) << "Reserving space for backing file isn't supported, "
+                                "may run out of disk later";
             }
         }
         if (!data.empty()) {
@@ -1680,6 +1677,15 @@
     }
 }
 
+void IncrementalService::trimReservedSpaceV1(const IncFsMount& ifs) {
+    mIncFs->forEachFile(ifs.control, [this](auto&& control, auto&& fileId) {
+        if (mIncFs->isFileFullyLoaded(control, fileId) == incfs::LoadingState::Full) {
+            mIncFs->reserveSpace(control, fileId, -1);
+        }
+        return true;
+    });
+}
+
 void IncrementalService::prepareDataLoaderLocked(IncFsMount& ifs, DataLoaderParamsParcel&& params,
                                                  DataLoaderStatusListener&& statusListener,
                                                  const StorageHealthCheckParams& healthCheckParams,
@@ -1699,6 +1705,22 @@
                                std::move(statusListener), healthCheckParams,
                                std::move(healthListener), path::join(ifs.root, constants().mount));
 
+    // pre-v2 IncFS doesn't do automatic reserved space trimming - need to run it manually
+    if (!(mIncFs->features() & incfs::Features::v2)) {
+        addIfsStateCallback(ifs.mountId, [this](StorageId storageId, IfsState state) -> bool {
+            if (!state.fullyLoaded) {
+                return true;
+            }
+
+            const auto ifs = getIfs(storageId);
+            if (!ifs) {
+                return false;
+            }
+            trimReservedSpaceV1(*ifs);
+            return false;
+        });
+    }
+
     addIfsStateCallback(ifs.mountId, [this](StorageId storageId, IfsState state) -> bool {
         if (!state.fullyLoaded || state.readLogsEnabled) {
             return true;