Refactor of use of fstab in advance of fix.

Test: Ensure device still boots.
Bug: 65737446
Change-Id: Ie466db9f5d8c77656cc525c0d49fe6a3cce154f1
diff --git a/Android.mk b/Android.mk
index 4d812b7..dae747e 100644
--- a/Android.mk
+++ b/Android.mk
@@ -12,7 +12,7 @@
 	Devmapper.cpp \
 	CheckBattery.cpp \
 	Ext4Crypt.cpp \
-	VoldUtil.c \
+	VoldUtil.cpp \
 	cryptfs.cpp \
 	model/Disk.cpp \
 	model/VolumeBase.cpp \
diff --git a/MetadataCrypt.cpp b/MetadataCrypt.cpp
index 8311813..24047f9 100644
--- a/MetadataCrypt.cpp
+++ b/MetadataCrypt.cpp
@@ -42,7 +42,6 @@
 #include "Utils.h"
 #include "VoldUtil.h"
 
-extern struct fstab *fstab;
 #define DM_CRYPT_BUF_SIZE 4096
 #define TABLE_LOAD_RETRIES 10
 #define DEFAULT_KEY_TARGET_TYPE "default-key"
@@ -58,7 +57,7 @@
         PLOG(ERROR) << "Failed to setexeccon";
         return false;
     }
-    auto mount_rc = fs_mgr_do_mount(fstab, const_cast<char*>(mount_point),
+    auto mount_rc = fs_mgr_do_mount(fstab_default, const_cast<char*>(mount_point),
                                     const_cast<char*>(blk_device), nullptr);
     if (setexeccon(nullptr)) {
         PLOG(ERROR) << "Failed to clear setexeccon";
@@ -73,7 +72,7 @@
 }
 
 static bool read_key(bool create_if_absent, KeyBuffer* key) {
-    auto data_rec = fs_mgr_get_crypt_entry(fstab);
+    auto data_rec = fs_mgr_get_crypt_entry(fstab_default);
     if (!data_rec) {
         LOG(ERROR) << "Failed to get data_rec";
         return false;
@@ -253,7 +252,7 @@
     LOG(DEBUG) << "e4crypt_mount_default_encrypted";
     KeyBuffer key;
     if (!read_key(false, &key)) return false;
-    auto data_rec = fs_mgr_get_crypt_entry(fstab);
+    auto data_rec = fs_mgr_get_crypt_entry(fstab_default);
     if (!data_rec) {
         LOG(ERROR) << "Failed to get data_rec";
         return false;
@@ -283,7 +282,7 @@
     KeyBuffer key_ref;
     if (!read_key(true, &key_ref)) return false;
 
-    auto data_rec = fs_mgr_get_crypt_entry(fstab);
+    auto data_rec = fs_mgr_get_crypt_entry(fstab_default);
     if (!data_rec) {
         LOG(ERROR) << "Failed to get data_rec";
         return false;
diff --git a/VoldUtil.c b/VoldUtil.cpp
similarity index 95%
rename from VoldUtil.c
rename to VoldUtil.cpp
index e5bc912..afe8b53 100644
--- a/VoldUtil.c
+++ b/VoldUtil.cpp
@@ -17,6 +17,8 @@
 #include <sys/ioctl.h>
 #include <linux/fs.h>
 
+struct fstab *fstab_default;
+
 void get_blkdev_size(int fd, unsigned long* nr_sec) {
   if ((ioctl(fd, BLKGETSIZE, nr_sec)) == -1) {
     *nr_sec = 0;
diff --git a/VoldUtil.h b/VoldUtil.h
index 5738382..fd66672 100644
--- a/VoldUtil.h
+++ b/VoldUtil.h
@@ -17,12 +17,13 @@
 #ifndef _VOLDUTIL_H
 #define _VOLDUTIL_H
 
+#include <fstab/fstab.h>
 #include <sys/cdefs.h>
 
+extern struct fstab *fstab_default;
+
 #define ARRAY_SIZE(a) (sizeof(a) / sizeof(*(a)))
 
-__BEGIN_DECLS
 void get_blkdev_size(int fd, unsigned long* nr_sec);
-__END_DECLS
 
 #endif
diff --git a/cryptfs.cpp b/cryptfs.cpp
index 132b31f..40b1e07 100644
--- a/cryptfs.cpp
+++ b/cryptfs.cpp
@@ -191,8 +191,6 @@
 static int password_expiry_time = 0;
 static const int password_max_age_seconds = 60;
 
-extern struct fstab *fstab;
-
 enum class RebootType {reboot, recovery, shutdown};
 static void cryptfs_reboot(RebootType rt)
 {
@@ -295,7 +293,7 @@
   int rc = -1;
 
   if (!cached_data) {
-    fs_mgr_get_crypt_info(fstab, key_loc, real_blkdev, sizeof(key_loc));
+    fs_mgr_get_crypt_info(fstab_default, key_loc, real_blkdev, sizeof(key_loc));
 
     if (!strcmp(key_loc, KEY_IN_FOOTER)) {
       if ( (fd = open(real_blkdev, O_RDWR|O_CLOEXEC)) < 0) {
@@ -1420,7 +1418,7 @@
         char ro_prop[PROPERTY_VALUE_MAX];
         property_get("ro.crypto.readonly", ro_prop, "");
         if (strlen(ro_prop) > 0 && std::stoi(ro_prop)) {
-            struct fstab_rec* rec = fs_mgr_get_entry_for_mount_point(fstab, DATA_MNT_POINT);
+            struct fstab_rec* rec = fs_mgr_get_entry_for_mount_point(fstab_default, DATA_MNT_POINT);
             rec->flags |= MS_RDONLY;
         }
 
@@ -1436,7 +1434,7 @@
             SLOGE("Failed to setexeccon");
             return -1;
         }
-        while ((mount_rc = fs_mgr_do_mount(fstab, DATA_MNT_POINT,
+        while ((mount_rc = fs_mgr_do_mount(fstab_default, DATA_MNT_POINT,
                                            crypto_blkdev, 0))
                != 0) {
             if (mount_rc == FS_MGR_DOMNT_BUSY) {
@@ -1517,7 +1515,7 @@
   }
 
   if (get_crypt_ftr_and_key(&crypt_ftr)) {
-    fs_mgr_get_crypt_info(fstab, key_loc, 0, sizeof(key_loc));
+    fs_mgr_get_crypt_info(fstab_default, key_loc, 0, sizeof(key_loc));
 
     /*
      * Only report this error if key_loc is a file and it exists.
@@ -1585,7 +1583,7 @@
     }
   }
 
-  fs_mgr_get_crypt_info(fstab, 0, real_blkdev, sizeof(real_blkdev));
+  fs_mgr_get_crypt_info(fstab_default, 0, real_blkdev, sizeof(real_blkdev));
 
   // Create crypto block device - all (non fatal) code paths
   // need it
@@ -1617,7 +1615,7 @@
     snprintf(tmp_mount_point, sizeof(tmp_mount_point), "%s/tmp_mnt",
              mount_point);
     mkdir(tmp_mount_point, 0755);
-    if (fs_mgr_do_mount(fstab, DATA_MNT_POINT, crypto_blkdev, tmp_mount_point)) {
+    if (fs_mgr_do_mount(fstab_default, DATA_MNT_POINT, crypto_blkdev, tmp_mount_point)) {
       SLOGE("Error temp mounting decrypted block device\n");
       delete_crypto_blk_dev(label);
 
@@ -2021,7 +2019,7 @@
     tot_encryption_size = crypt_ftr->fs_size;
 
     if (how == CRYPTO_ENABLE_WIPE) {
-        struct fstab_rec* rec = fs_mgr_get_entry_for_mount_point(fstab, DATA_MNT_POINT);
+        struct fstab_rec* rec = fs_mgr_get_entry_for_mount_point(fstab_default, DATA_MNT_POINT);
         int fs_type = get_fs_type(rec);
         if (fs_type < 0) {
             SLOGE("cryptfs_enable: unsupported fs type %s\n", rec->fs_type);
@@ -2121,8 +2119,8 @@
     }
 
     // TODO refactor fs_mgr_get_crypt_info to get both in one call
-    fs_mgr_get_crypt_info(fstab, key_loc, 0, sizeof(key_loc));
-    fs_mgr_get_crypt_info(fstab, 0, real_blkdev, sizeof(real_blkdev));
+    fs_mgr_get_crypt_info(fstab_default, key_loc, 0, sizeof(key_loc));
+    fs_mgr_get_crypt_info(fstab_default, 0, real_blkdev, sizeof(real_blkdev));
 
     /* Get the size of the real block device */
     fd = open(real_blkdev, O_RDONLY|O_CLOEXEC);
@@ -2870,7 +2868,7 @@
 
 int cryptfs_isConvertibleToFBE()
 {
-    struct fstab_rec* rec = fs_mgr_get_entry_for_mount_point(fstab, DATA_MNT_POINT);
+    struct fstab_rec* rec = fs_mgr_get_entry_for_mount_point(fstab_default, DATA_MNT_POINT);
     return fs_mgr_is_convertible_to_fbe(rec) ? 1 : 0;
 }
 
@@ -2943,6 +2941,6 @@
 void cryptfs_get_file_encryption_modes(const char **contents_mode_ret,
                                        const char **filenames_mode_ret)
 {
-    struct fstab_rec* rec = fs_mgr_get_entry_for_mount_point(fstab, DATA_MNT_POINT);
+    struct fstab_rec* rec = fs_mgr_get_entry_for_mount_point(fstab_default, DATA_MNT_POINT);
     fs_mgr_get_file_encryption_modes(rec, contents_mode_ret, filenames_mode_ret);
 }
diff --git a/main.cpp b/main.cpp
index f317f3d..1bb7639 100644
--- a/main.cpp
+++ b/main.cpp
@@ -20,6 +20,7 @@
 #include "VolumeManager.h"
 #include "NetlinkManager.h"
 #include "VoldNativeService.h"
+#include "VoldUtil.h"
 #include "cryptfs.h"
 #include "sehandle.h"
 
@@ -44,8 +45,6 @@
 static void coldboot(const char *path);
 static void parse_args(int argc, char** argv);
 
-struct fstab *fstab;
-
 struct selabel_handle *sehandle;
 
 using android::base::StringPrintf;
@@ -211,8 +210,8 @@
 static int process_config(VolumeManager *vm, bool* has_adoptable, bool* has_quota) {
     ATRACE_NAME("process_config");
 
-    fstab = fs_mgr_read_fstab_default();
-    if (!fstab) {
+    fstab_default = fs_mgr_read_fstab_default();
+    if (!fstab_default) {
         PLOG(ERROR) << "Failed to open default fstab";
         return -1;
     }
@@ -220,26 +219,27 @@
     /* Loop through entries looking for ones that vold manages */
     *has_adoptable = false;
     *has_quota = false;
-    for (int i = 0; i < fstab->num_entries; i++) {
-        if (fs_mgr_is_quota(&fstab->recs[i])) {
+    for (int i = 0; i < fstab_default->num_entries; i++) {
+        auto rec = &fstab_default->recs[i];
+        if (fs_mgr_is_quota(rec)) {
             *has_quota = true;
         }
 
-        if (fs_mgr_is_voldmanaged(&fstab->recs[i])) {
-            if (fs_mgr_is_nonremovable(&fstab->recs[i])) {
+        if (fs_mgr_is_voldmanaged(rec)) {
+            if (fs_mgr_is_nonremovable(rec)) {
                 LOG(WARNING) << "nonremovable no longer supported; ignoring volume";
                 continue;
             }
 
-            std::string sysPattern(fstab->recs[i].blk_device);
-            std::string nickname(fstab->recs[i].label);
+            std::string sysPattern(rec->blk_device);
+            std::string nickname(rec->label);
             int flags = 0;
 
-            if (fs_mgr_is_encryptable(&fstab->recs[i])) {
+            if (fs_mgr_is_encryptable(rec)) {
                 flags |= android::vold::Disk::Flags::kAdoptable;
                 *has_adoptable = true;
             }
-            if (fs_mgr_is_noemulatedsd(&fstab->recs[i])
+            if (fs_mgr_is_noemulatedsd(rec)
                     || property_get_bool("vold.debug.default_primary", false)) {
                 flags |= android::vold::Disk::Flags::kDefaultPrimary;
             }