storage: Un-split NS filesystem initialization

Right now it's hard to extract ns initialization into its own function
because it's left partially-initialized while initializing TDP.

Bug: 332762065
Change-Id: I1732ec8a71022a65e1466932b960c8ec15542a61
diff --git a/aidl_service.cpp b/aidl_service.cpp
index 36edb4b..49b8b0d 100644
--- a/aidl_service.cpp
+++ b/aidl_service.cpp
@@ -743,8 +743,8 @@
     storage_service::StorageService& service = self->inner->service;
     service.TryActivateFilesystem(block_devices, STORAGE_TP);
     service.TryActivateFilesystem(block_devices, STORAGE_TDEA);
-    service.TryActivateFilesystem(block_devices, STORAGE_TDP);
     service.TryActivateFilesystem(block_devices, STORAGE_TD);
+    service.TryActivateFilesystem(block_devices, STORAGE_TDP);
     service.TryActivateFilesystem(block_devices, STORAGE_NSP);
 }
 
@@ -752,8 +752,8 @@
                           struct block_device_tipc* block_devices) {
     storage_service::StorageService& service = self->inner->service;
     service.DeactivateFilesystem(block_devices, STORAGE_NSP);
-    service.DeactivateFilesystem(block_devices, STORAGE_TD);
     service.DeactivateFilesystem(block_devices, STORAGE_TDP);
+    service.DeactivateFilesystem(block_devices, STORAGE_TD);
     service.DeactivateFilesystem(block_devices, STORAGE_TDEA);
     service.DeactivateFilesystem(block_devices, STORAGE_TP);
 }
\ No newline at end of file
diff --git a/block_device_tipc.c b/block_device_tipc.c
index e3eae2e..357b603 100644
--- a/block_device_tipc.c
+++ b/block_device_tipc.c
@@ -603,6 +603,36 @@
         goto err_get_td_max_size;
     }
 
+    /* Request empty file system if file is empty */
+    ret = ns_read_pos(state->ipc_handle, state->dev_ns.ns_handle, 0, &probe,
+                      sizeof(probe));
+    if (ret < (int)sizeof(probe)) {
+        ns_init_flags |= FS_INIT_FLAGS_DO_CLEAR;
+    }
+
+    block_device_tipc_init_dev_rpmb(&state->dev_ns_rpmb, state->rpmb_state,
+                                    partitions.ns.start,
+                                    partitions.ns.block_count, true);
+
+#if STORAGE_NS_RECOVERY_CLEAR_ALLOWED
+    ns_init_flags |= FS_INIT_FLAGS_RECOVERY_CLEAR_ALLOWED;
+#endif
+
+    /*
+     * This must be false if STORAGE_NS_ALTERNATE_SUPERBLOCK_ALLOWED is
+     * false.
+     */
+    if (alternate_data_partition) {
+        ns_init_flags |= FS_INIT_FLAGS_ALTERNATE_DATA;
+    }
+
+    ret = fs_init(&state->tr_state_ns, file_system_id_td, fs_key,
+                  &state->dev_ns.dev, &state->dev_ns_rpmb.dev, ns_init_flags);
+    if (ret < 0) {
+        SS_ERR("%s: failed to initialize TD: %d\n", __func__, ret);
+        goto err_init_fs_ns_tr_state;
+    }
+
 #if HAS_FS_TDP
     block_device_tipc_init_dev_ns(&state->dev_ns_tdp, state->ipc_handle, false);
 
@@ -656,36 +686,6 @@
 #endif
 #endif
 
-    /* Request empty file system if file is empty */
-    ret = ns_read_pos(state->ipc_handle, state->dev_ns.ns_handle, 0, &probe,
-                      sizeof(probe));
-    if (ret < (int)sizeof(probe)) {
-        ns_init_flags |= FS_INIT_FLAGS_DO_CLEAR;
-    }
-
-    block_device_tipc_init_dev_rpmb(&state->dev_ns_rpmb, state->rpmb_state,
-                                    partitions.ns.start,
-                                    partitions.ns.block_count, true);
-
-#if STORAGE_NS_RECOVERY_CLEAR_ALLOWED
-    ns_init_flags |= FS_INIT_FLAGS_RECOVERY_CLEAR_ALLOWED;
-#endif
-
-    /*
-     * This must be false if STORAGE_NS_ALTERNATE_SUPERBLOCK_ALLOWED is
-     * false.
-     */
-    if (alternate_data_partition) {
-        ns_init_flags |= FS_INIT_FLAGS_ALTERNATE_DATA;
-    }
-
-    ret = fs_init(&state->tr_state_ns, file_system_id_td, fs_key,
-                  &state->dev_ns.dev, &state->dev_ns_rpmb.dev, ns_init_flags);
-    if (ret < 0) {
-        SS_ERR("%s: failed to initialize TD: %d\n", __func__, ret);
-        goto err_init_fs_ns_tr_state;
-    }
-
 #if HAS_FS_NSP
     block_device_tipc_init_dev_ns(&state->dev_ns_nsp, state->ipc_handle, false);
 
@@ -739,9 +739,6 @@
     ns_close_file(state->ipc_handle, state->dev_ns_nsp.ns_handle);
 err_open_nsp:
 #endif
-    fs_destroy(&state->tr_state_ns);
-err_init_fs_ns_tr_state:
-    block_cache_dev_destroy(&state->dev_ns.dev);
 #if HAS_FS_TDP
     fs_destroy(&state->tr_state_ns_tdp);
 err_init_fs_ns_tdp_tr_state:
@@ -750,6 +747,9 @@
     ns_close_file(state->ipc_handle, state->dev_ns_tdp.ns_handle);
 err_open_tdp:
 #endif
+    fs_destroy(&state->tr_state_ns);
+err_init_fs_ns_tr_state:
+    block_cache_dev_destroy(&state->dev_ns.dev);
 err_get_td_max_size:
     ns_close_file(state->ipc_handle, state->dev_ns.ns_handle);
     fs_destroy(&state->tr_state_rpmb);
@@ -764,19 +764,19 @@
 
 void block_device_tipc_destroy(struct block_device_tipc* state) {
     if (block_device_tipc_has_ns(state)) {
-        fs_destroy(&state->tr_state_ns);
-        block_cache_dev_destroy(&state->dev_ns.dev);
-        ns_close_file(state->ipc_handle, state->dev_ns.ns_handle);
-#if HAS_FS_TDP
-        fs_destroy(&state->tr_state_ns_tdp);
-        block_cache_dev_destroy(&state->dev_ns_tdp.dev);
-        ns_close_file(state->ipc_handle, state->dev_ns_tdp.ns_handle);
-#endif
 #if HAS_FS_NSP
         fs_destroy(&state->tr_state_ns_nsp);
         block_cache_dev_destroy(&state->dev_ns_nsp.dev);
         ns_close_file(state->ipc_handle, state->dev_ns_nsp.ns_handle);
 #endif
+#if HAS_FS_TDP
+        fs_destroy(&state->tr_state_ns_tdp);
+        block_cache_dev_destroy(&state->dev_ns_tdp.dev);
+        ns_close_file(state->ipc_handle, state->dev_ns_tdp.ns_handle);
+#endif
+        fs_destroy(&state->tr_state_ns);
+        block_cache_dev_destroy(&state->dev_ns.dev);
+        ns_close_file(state->ipc_handle, state->dev_ns.ns_handle);
     }
 
     fs_destroy(&state->tr_state_rpmb);
@@ -791,6 +791,8 @@
         return self->ipc_handle != 0;
     case STORAGE_TDEA:
         return self->ipc_handle != 0;
+    case STORAGE_TD:
+        return block_device_tipc_has_ns(self) && self->dev_ns.ipc_handle != 0;
     case STORAGE_TDP:
 #if HAS_FS_TDP
         return block_device_tipc_has_ns(self) &&
@@ -798,8 +800,6 @@
 #else
         return block_device_tipc_fs_connected(self, STORAGE_TP);
 #endif
-    case STORAGE_TD:
-        return block_device_tipc_has_ns(self) && self->dev_ns.ipc_handle != 0;
     case STORAGE_NSP:
 #if HAS_FS_NSP
         return block_device_tipc_has_ns(self) &&
@@ -824,14 +824,14 @@
         return &self->tr_state_rpmb;
     case STORAGE_TDEA:
         return &self->tr_state_rpmb;
+    case STORAGE_TD:
+        return &self->tr_state_ns;
     case STORAGE_TDP:
 #if HAS_FS_TDP
         return &self->tr_state_ns_tdp;
 #else
         return block_device_tipc_get_fs(self, STORAGE_TP);
 #endif
-    case STORAGE_TD:
-        return &self->tr_state_ns;
     case STORAGE_NSP:
 #if HAS_FS_NSP
         return &self->tr_state_ns_nsp;
diff --git a/block_device_tipc.h b/block_device_tipc.h
index 900acd7..d4a1ecb 100644
--- a/block_device_tipc.h
+++ b/block_device_tipc.h
@@ -57,8 +57,8 @@
 enum storage_filesystem_type {
     STORAGE_TP,
     STORAGE_TDEA,
-    STORAGE_TDP,
     STORAGE_TD,
+    STORAGE_TDP,
     STORAGE_NSP,
     STORAGE_FILESYSTEMS_COUNT,
 };
@@ -100,6 +100,10 @@
  *                         @ipc_handle.
  * @dev_rpmb:              The rpmb block device backing @tr_state_rpmb.
  * @tr_state_rpmb:         Filesystem for rpmb (TP, TDEA).
+ * @dev_ns:                The rpmb block device containing the superblock for
+ *                         @tr_state_ns.
+ * @dev_ns_rpmb:           The rpmb block device backing @tr_state_ns.
+ * @tr_state_ns:           Filesystem for TD.
  * @dev_ns_tdp:            The ns block device backing @tr_state_ns_tdp. Only
  *                         present when $HAS_FS_TDP defined.
  * @dev_ns_tdp_rpmb:       The rpmb block device containing the superblock for
@@ -107,10 +111,6 @@
  *                         defined.
  * @tr_state_ns_tdp:       Filesystem for TDP. Only present when $HAS_FS_TDP
  *                         defined.
- * @dev_ns:                The rpmb block device containing the superblock for
- *                         @tr_state_ns.
- * @dev_ns_rpmb:           The rpmb block device backing @tr_state_ns.
- * @tr_state_ns:           Filesystem for TD.
  * @dev_ns_nsp:            The ns block device backing @tr_state_ns_nsp. Only
  *                         present when $HAS_FS_NSP defined.
  * @dev_ns_nsp_superblock: The ns block device containing the superblock for
@@ -126,16 +126,16 @@
     struct block_device_rpmb dev_rpmb;
     struct fs tr_state_rpmb;
 
+    struct block_device_ns dev_ns;
+    struct block_device_rpmb dev_ns_rpmb;
+    struct fs tr_state_ns;
+
 #if HAS_FS_TDP
     struct block_device_ns dev_ns_tdp;
     struct block_device_rpmb dev_ns_tdp_rpmb;
     struct fs tr_state_ns_tdp;
 #endif
 
-    struct block_device_ns dev_ns;
-    struct block_device_rpmb dev_ns_rpmb;
-    struct fs tr_state_ns;
-
 #if HAS_FS_NSP
     struct block_device_ns dev_ns_nsp;
     struct block_device_ns dev_ns_nsp_superblock;