Add 'fstrim' command for use from shell.

Test: builds, boots, new command works
Bug: 20948199
Change-Id: If7b122a6c98a4ce2a2f38e545015a22decd1b516
This commit is contained in:
Jeff Sharkey
2016-11-21 14:16:53 -07:00
parent 2c0dc3e094
commit 31d0b7043d
4 changed files with 39 additions and 10 deletions

View File

@@ -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();
}
@@ -210,7 +212,7 @@ public final class Sm {
mSm.benchmark(volId);
}
public void runForget() throws RemoteException{
public void runForget() throws RemoteException {
final String fsUuid = nextArg();
if ("all".equals(fsUuid)) {
mSm.forgetAllVolumes();
@@ -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("");

View File

@@ -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;
}

View File

@@ -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;

View File

@@ -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();