Add length parameter to format /data f2fs filesystem

With metadata encryption, if partition is wiped (or) cryptfs failed ,
it will do format without length which make partition size is grown
large to occupy remaining space instead of restricting the Android
defined partition size.

Bug: 343159184
Test: Add length flag in fstab && fastboot erase userdata &&
      fastboot reboot && df -h /data

Change-Id: Iad041e960c9337ab1b9d51b115db3c5f3f1f75e2
Signed-off-by: Ashok Mutyala <quic_amutyala@quicinc.com>
diff --git a/MetadataCrypt.cpp b/MetadataCrypt.cpp
index a1106fd..38dd112 100644
--- a/MetadataCrypt.cpp
+++ b/MetadataCrypt.cpp
@@ -247,10 +247,12 @@
 bool fscrypt_mount_metadata_encrypted(const std::string& blk_device, const std::string& mount_point,
                                       bool needs_encrypt, bool should_format,
                                       const std::string& fs_type, bool is_zoned,
-                                      const std::vector<std::string>& user_devices) {
+                                      const std::vector<std::string>& user_devices,
+                                      int64_t length) {
     LOG(DEBUG) << "fscrypt_mount_metadata_encrypted: " << mount_point
                << " encrypt: " << needs_encrypt << " format: " << should_format << " with "
-               << fs_type << " block device: " << blk_device << " with zoned " << is_zoned;
+               << fs_type << " block device: " << blk_device << " with zoned " << is_zoned
+               << " length: " << length;
 
     for (auto& device : user_devices) {
         LOG(DEBUG) << " - user devices: " << device;
@@ -338,7 +340,7 @@
             if (fs_type == "ext4") {
                 error = ext4::Format(crypto_blkdev, 0, mount_point);
             } else if (fs_type == "f2fs") {
-                error = f2fs::Format(crypto_blkdev, is_zoned, crypto_user_blkdev);
+                error = f2fs::Format(crypto_blkdev, is_zoned, crypto_user_blkdev, length);
             } else {
                 LOG(ERROR) << "Unknown filesystem type: " << fs_type;
                 return false;
diff --git a/MetadataCrypt.h b/MetadataCrypt.h
index 2c07a14..a091443 100644
--- a/MetadataCrypt.h
+++ b/MetadataCrypt.h
@@ -29,7 +29,7 @@
 bool fscrypt_mount_metadata_encrypted(const std::string& block_device,
                                       const std::string& mount_point, bool needs_encrypt,
                                       bool should_format, const std::string& fs_type, bool is_zoned,
-                                      const std::vector<std::string>& user_devices);
+                                      const std::vector<std::string>& user_devices, int64_t length);
 
 bool defaultkey_volume_keygen(KeyGeneration* gen);
 
diff --git a/VoldNativeService.cpp b/VoldNativeService.cpp
index a70639c..98dec66 100644
--- a/VoldNativeService.cpp
+++ b/VoldNativeService.cpp
@@ -594,18 +594,19 @@
     ACQUIRE_LOCK;
 
     return translateBool(fscrypt_mount_metadata_encrypted(blkDevice, mountPoint, false, false,
-                                                          "null", isZoned, userDevices));
+                                                          "null", isZoned, userDevices, 0));
 }
 
 binder::Status VoldNativeService::encryptFstab(const std::string& blkDevice,
                                                const std::string& mountPoint, bool shouldFormat,
                                                const std::string& fsType, bool isZoned,
-                                               const std::vector<std::string>& userDevices) {
+                                               const std::vector<std::string>& userDevices,
+                                               int64_t length) {
     ENFORCE_SYSTEM_OR_ROOT;
     ACQUIRE_LOCK;
 
     return translateBool(fscrypt_mount_metadata_encrypted(blkDevice, mountPoint, true, shouldFormat,
-                                                          fsType, isZoned, userDevices));
+                                                          fsType, isZoned, userDevices, length));
 }
 
 binder::Status VoldNativeService::setStorageBindingSeed(const std::vector<uint8_t>& seed) {
diff --git a/VoldNativeService.h b/VoldNativeService.h
index 619c720..bd37ac7 100644
--- a/VoldNativeService.h
+++ b/VoldNativeService.h
@@ -107,9 +107,10 @@
     binder::Status initUser0();
     binder::Status mountFstab(const std::string& blkDevice, const std::string& mountPoint,
                               bool isZoned, const std::vector<std::string>& userDevices);
+
     binder::Status encryptFstab(const std::string& blkDevice, const std::string& mountPoint,
                                 bool shouldFormat, const std::string& fsType, bool isZoned,
-                                const std::vector<std::string>& userDevices);
+                                const std::vector<std::string>& userDevices, int64_t length);
 
     binder::Status setStorageBindingSeed(const std::vector<uint8_t>& seed);
 
diff --git a/binder/android/os/IVold.aidl b/binder/android/os/IVold.aidl
index 919369b..a8cce94 100644
--- a/binder/android/os/IVold.aidl
+++ b/binder/android/os/IVold.aidl
@@ -84,7 +84,7 @@
 
     void initUser0();
     void mountFstab(@utf8InCpp String blkDevice, @utf8InCpp String mountPoint, boolean isZoned, in @utf8InCpp String[] userDevices);
-    void encryptFstab(@utf8InCpp String blkDevice, @utf8InCpp String mountPoint, boolean shouldFormat, @utf8InCpp String fsType, boolean isZoned, in @utf8InCpp String[] userDevices);
+    void encryptFstab(@utf8InCpp String blkDevice, @utf8InCpp String mountPoint, boolean shouldFormat, @utf8InCpp String fsType, boolean isZoned, in @utf8InCpp String[] userDevices, long length);
 
     void setStorageBindingSeed(in byte[] seed);
 
diff --git a/fs/F2fs.cpp b/fs/F2fs.cpp
index 99afc32..3cdf574 100644
--- a/fs/F2fs.cpp
+++ b/fs/F2fs.cpp
@@ -72,8 +72,10 @@
 }
 
 status_t Format(const std::string& source, bool is_zoned,
-                const std::vector<std::string>& user_devices) {
+                const std::vector<std::string>& user_devices, int64_t length) {
     std::vector<char const*> cmd;
+    /* '-g android' parameter passed here which defaults the sector size to 4096 */
+    static constexpr int kSectorSize = 4096;
     cmd.emplace_back(kMkfsPath);
 
     cmd.emplace_back("-f");
@@ -110,6 +112,9 @@
 
     cmd.emplace_back(source.c_str());
 
+    if (length) {
+        cmd.emplace_back(std::to_string(length / kSectorSize).c_str());
+    }
     return logwrap_fork_execvp(cmd.size(), cmd.data(), nullptr, false, LOG_KLOG,
                              false, nullptr);
 }
diff --git a/fs/F2fs.h b/fs/F2fs.h
index a0218f2..7391310 100644
--- a/fs/F2fs.h
+++ b/fs/F2fs.h
@@ -31,7 +31,7 @@
 status_t Check(const std::string& source);
 status_t Mount(const std::string& source, const std::string& target);
 status_t Format(const std::string& source, const bool is_zoned,
-                const std::vector<std::string>& user_devices);
+                const std::vector<std::string>& user_devices, int64_t length = 0);
 
 }  // namespace f2fs
 }  // namespace vold
diff --git a/vdc.cpp b/vdc.cpp
index ee8cf9e..9764b1a 100644
--- a/vdc.cpp
+++ b/vdc.cpp
@@ -109,13 +109,15 @@
     if (isZoned == android::base::ParseBoolResult::kError) exit(EINVAL);
 
     std::vector<std::string> userDevices = {};
-    if (args[7] != "") {
-        userDevices = android::base::Split(args[7], " ");
+    int64_t length;
+    if (!android::base::ParseInt(args[7], &length)) exit(EINVAL);
+    if (args[8] != "") {
+        userDevices = android::base::Split(args[8], " ");
     }
-    checkStatus(args,
-                vold->encryptFstab(args[2], args[3],
-                                   shouldFormat == android::base::ParseBoolResult::kTrue, args[5],
-                                   isZoned == android::base::ParseBoolResult::kTrue, userDevices));
+    checkStatus(args, vold->encryptFstab(args[2], args[3],
+                                         shouldFormat == android::base::ParseBoolResult::kTrue,
+                                         args[5], isZoned == android::base::ParseBoolResult::kTrue,
+                                         userDevices, length));
 }
 
 int main(int argc, char** argv) {
@@ -162,7 +164,7 @@
         bindkeys(args, vold);
     } else if (args[0] == "cryptfs" && args[1] == "mountFstab" && args.size() == 6) {
         mountFstab(args, vold);
-    } else if (args[0] == "cryptfs" && args[1] == "encryptFstab" && args.size() == 8) {
+    } else if (args[0] == "cryptfs" && args[1] == "encryptFstab" && args.size() == 9) {
         encryptFstab(args, vold);
     } else if (args[0] == "checkpoint" && args[1] == "supportsCheckpoint" && args.size() == 2) {
         bool supported = false;