Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 31d0b704 authored by Jeff Sharkey's avatar Jeff Sharkey
Browse files

Add 'fstrim' command for use from shell.

Test: builds, boots, new command works
Bug: 20948199
Change-Id: If7b122a6c98a4ce2a2f38e545015a22decd1b516
parent 2c0dc3e0
Loading
Loading
Loading
Loading
+8 −1
Original line number Diff line number Diff line
@@ -92,6 +92,8 @@ public final class Sm {
            runSetEmulateFbe();
        } else if ("get-fbe-mode".equals(op)) {
            runGetFbeMode();
        } else if ("fstrim".equals(op)) {
            runFstrim();
        } else {
            throw new IllegalArgumentException();
        }
@@ -219,6 +221,10 @@ public final class Sm {
        }
    }

    public void runFstrim() throws RemoteException {
        mSm.fstrim(0);
    }

    private String nextArg() {
        if (mNextArg >= mArgs.length) {
            return null;
@@ -240,6 +246,7 @@ public final class Sm {
        System.err.println("       sm unmount VOLUME");
        System.err.println("       sm format VOLUME");
        System.err.println("       sm benchmark VOLUME");
        System.err.println("       sm fstrim");
        System.err.println("");
        System.err.println("       sm forget [UUID|all]");
        System.err.println("");
+2 −1
Original line number Diff line number Diff line
@@ -288,4 +288,5 @@ interface IStorageManager {
    ParcelFileDescriptor mountAppFuse(in String name) = 69;
    void addUserKeyAuth(int userId, int serialNumber, in byte[] token, in byte[] secret) = 70;
    void fixateNewestUserKeyAuth(int userId) = 71;
    void fstrim(int flags) = 72;
}
+5 −0
Original line number Diff line number Diff line
@@ -134,6 +134,11 @@ public class StorageManager {
    /** {@hide} */
    public static final int FLAG_INCLUDE_INVISIBLE = 1 << 10;

    /** {@hide} */
    public static final int FSTRIM_FLAG_DEEP = 1 << 0;
    /** {@hide} */
    public static final int FSTRIM_FLAG_BENCHMARK = 1 << 1;

    /** @hide The volume is not encrypted. */
    public static final int ENCRYPTION_STATE_NONE = 1;

+24 −8
Original line number Diff line number Diff line
@@ -644,14 +644,8 @@ class StorageManagerService extends IStorageManager.Stub
                        Slog.e(TAG, "Unable to record last fstrim!");
                    }

                    final boolean shouldBenchmark = shouldBenchmark();
                    try {
                        // This method must be run on the main (handler) thread,
                        // so it is safe to directly call into vold.
                        mConnector.execute("fstrim", shouldBenchmark ? "dotrimbench" : "dotrim");
                    } catch (NativeDaemonConnectorException ndce) {
                        Slog.e(TAG, "Failed to run fstrim!");
                    }
                    final int flags = shouldBenchmark() ? StorageManager.FSTRIM_FLAG_BENCHMARK : 0;
                    fstrim(flags);

                    // invoke the completion callback, if any
                    // TODO: fstrim is non-blocking, so remove this useless callback
@@ -1956,6 +1950,28 @@ class StorageManagerService extends IStorageManager.Stub
        }
    }

    @Override
    public void fstrim(int flags) {
        enforcePermission(android.Manifest.permission.MOUNT_FORMAT_FILESYSTEMS);
        waitForReady();

        String cmd;
        if ((flags & StorageManager.FSTRIM_FLAG_DEEP) != 0) {
            cmd = "dodtrim";
        } else {
            cmd = "dotrim";
        }
        if ((flags & StorageManager.FSTRIM_FLAG_BENCHMARK) != 0) {
            cmd += "bench";
        }

        try {
            mConnector.execute("fstrim", cmd);
        } catch (NativeDaemonConnectorException e) {
            Slog.e(TAG, "Failed to run fstrim: " + e);
        }
    }

    private void remountUidExternalStorage(int uid, int mode) {
        waitForReady();