Merge "Checkpoint of better dynamic device support."
diff --git a/cryptfs.c b/cryptfs.c
index 46e6be7..5786210 100644
--- a/cryptfs.c
+++ b/cryptfs.c
@@ -58,7 +58,7 @@
 #include "CheckBattery.h"
 #include "Process.h"
 
-#include <hardware/keymaster.h>
+#include <hardware/keymaster0.h>
 
 #define UNUSED __attribute__((unused))
 
@@ -98,7 +98,7 @@
 static int  master_key_saved = 0;
 static struct crypt_persist_data *persist_data = NULL;
 
-static int keymaster_init(keymaster_device_t **keymaster_dev)
+static int keymaster_init(keymaster0_device_t **keymaster_dev)
 {
     int rc;
 
@@ -109,7 +109,7 @@
         goto out;
     }
 
-    rc = keymaster_open(mod, keymaster_dev);
+    rc = keymaster0_open(mod, keymaster_dev);
     if (rc) {
         ALOGE("could not open keymaster device in %s (%s)",
             KEYSTORE_HARDWARE_MODULE_ID, strerror(-rc));
@@ -126,7 +126,7 @@
 /* Should we use keymaster? */
 static int keymaster_check_compatibility()
 {
-    keymaster_device_t *keymaster_dev = 0;
+    keymaster0_device_t *keymaster_dev = 0;
     int rc = 0;
 
     if (keymaster_init(&keymaster_dev)) {
@@ -149,7 +149,7 @@
     }
 
 out:
-    keymaster_close(keymaster_dev);
+    keymaster0_close(keymaster_dev);
     return rc;
 }
 
@@ -157,7 +157,7 @@
 static int keymaster_create_key(struct crypt_mnt_ftr *ftr)
 {
     uint8_t* key = 0;
-    keymaster_device_t *keymaster_dev = 0;
+    keymaster0_device_t *keymaster_dev = 0;
 
     if (keymaster_init(&keymaster_dev)) {
         SLOGE("Failed to init keymaster");
@@ -189,7 +189,7 @@
     ftr->keymaster_blob_size = key_size;
 
 out:
-    keymaster_close(keymaster_dev);
+    keymaster0_close(keymaster_dev);
     free(key);
     return rc;
 }
@@ -202,7 +202,7 @@
                                  size_t *signature_size)
 {
     int rc = 0;
-    keymaster_device_t *keymaster_dev = 0;
+    keymaster0_device_t *keymaster_dev = 0;
     if (keymaster_init(&keymaster_dev)) {
         SLOGE("Failed to init keymaster");
         return -1;
@@ -262,7 +262,7 @@
                                   signature,
                                   signature_size);
 
-    keymaster_close(keymaster_dev);
+    keymaster0_close(keymaster_dev);
     return rc;
 }
 
diff --git a/fstrim.c b/fstrim.c
index 8a02c77..231d66e 100644
--- a/fstrim.c
+++ b/fstrim.c
@@ -62,12 +62,16 @@
     int fd;
     int ret = 0;
     struct fstrim_range range = { 0 };
-    struct stat sb;
     extern struct fstab *fstab;
     int deep_trim = !!thread_arg;
 
     SLOGI("Starting fstrim work...\n");
 
+    /* Get a wakelock as this may take a while, and we don't want the
+     * device to sleep on us.
+     */
+    acquire_wake_lock(PARTIAL_WAKE_LOCK, FSTRIM_WAKELOCK);
+
     /* Log the start time in the event log */
     LOG_EVENT_LONG(LOG_FSTRIM_START, get_boot_time_ms());
 
@@ -84,19 +88,11 @@
         if (fs_mgr_is_voldmanaged(&fstab->recs[i])) {
             continue; /* Should we trim fat32 filesystems? */
         }
-
-        if (stat(fstab->recs[i].mount_point, &sb) == -1) {
-            SLOGE("Cannot stat mount point %s\n", fstab->recs[i].mount_point);
-            ret = -1;
-            continue;
-        }
-        if (!S_ISDIR(sb.st_mode)) {
-            SLOGE("%s is not a directory\n", fstab->recs[i].mount_point);
-            ret = -1;
+        if (fs_mgr_is_notrim(&fstab->recs[i])) {
             continue;
         }
 
-        fd = open(fstab->recs[i].mount_point, O_RDONLY);
+        fd = open(fstab->recs[i].mount_point, O_RDONLY | O_DIRECTORY | O_CLOEXEC | O_NOFOLLOW);
         if (fd < 0) {
             SLOGE("Cannot open %s for FITRIM\n", fstab->recs[i].mount_point);
             ret = -1;
@@ -133,11 +129,6 @@
     pthread_t t;
     int ret;
 
-    /* Get a wakelock as this may take a while, and we don't want the
-     * device to sleep on us.
-     */
-    acquire_wake_lock(PARTIAL_WAKE_LOCK, FSTRIM_WAKELOCK);
-
     /* Depending on the emmc chip and size, this can take upwards
      * of a few minutes.  If done in the same thread as the caller
      * of this function, that would block vold from accepting any