[f2fs] code refactoring: mkfs.h, mkfs.cc

  Add default destructor (mkfs.h)
  Change ASCIIToUNICODE function name and Change output prameter position to last (mkfs.h, mkfs.cc)
  (comments on mkfs.h: #23, #35, #51 https://fuchsia-review.googlesource.com/c/third_party/f2fs/+/511845)

  Change multi line printf to one line (mkfs.cc)
  Change while roop to range-based for loop (mkfs.cc)
  (comments on mkfs.cc: #31, #173, #467 https://fuchsia-review.googlesource.com/c/third_party/f2fs/+/511846/6)

  Init struct member pointer to nullptr (mkfs.h)
  (comment: #53 https://fuchsia-review.googlesource.com/c/third_party/f2fs/+/511839)

  Change unsigned int style to uint32_t (mkfs.h, mkfs.cc)
  (comment: #142 https://fuchsia-review.googlesource.com/c/third_party/f2fs/+/511840)

  Change struct xxx to xxx (mkfs.h, mkfs.cc)
  (comment: #312 https://fuchsia-review.googlesource.com/c/third_party/f2fs/+/520881/2)

  Change int8_t err to zx_status_t (mkfs.h, mkfs.cc)
  (comment: #24, #106 https://fuchsia-review.googlesource.com/c/third_party/f2fs/+/520882)

  Change hard coded value to constexpr (mkfs.h, mkfs.cc)
  (comment: #9, #128 https://fuchsia-review.googlesource.com/c/third_party/f2fs/+/511840)

  Remove f2fs prefix (mkfs.h)
  (comment: #84 https://fuchsia-review.googlesource.com/c/third_party/f2fs/+/511841)

Change-Id: I1d3b82437f57c87105af2bc8d620eb6424f07d0e
Reviewed-on: https://fuchsia-review.googlesource.com/c/third_party/f2fs/+/530799
Reviewed-by: Brett Wilson <brettw@google.com>
diff --git a/mkfs.cc b/mkfs.cc
index 26ff2aa..5567a58 100644
--- a/mkfs.cc
+++ b/mkfs.cc
@@ -28,9 +28,9 @@
 
 namespace f2fs {
 
-const char *media_ext_lists[] = {"jpg", "gif", "png",  "avi", "divx", "mp4", "mp3", "3gp",
+const char *kMediaExtList[] = {"jpg", "gif", "png",  "avi", "divx", "mp4", "mp3", "3gp",
                                  "wmv", "wma", "mpeg", "mkv", "mov",  "asx", "asf", "wmx",
-                                 "svi", "wvx", "wm",   "mpg", "mpe",  "rm",  "ogg", nullptr};
+                                 "svi", "wvx", "wm",   "mpg", "mpe",  "rm",  "ogg"};
 
 F2fsMkfs::F2fsMkfs(std::unique_ptr<f2fs::Bcache> bc, const MkfsOptions &mkfs_options)
     : bc_(std::move(bc)), mkfs_options_(mkfs_options) {}
@@ -51,12 +51,12 @@
   // TODO: parse mkfs options
   f2fs_params.extension_list = extension_list;
 
-  F2fsInitGlobalParameters();
+  InitGlobalParameters();
 
-  if (F2fsGetDeviceInfo() < 0)
+  if (GetDeviceInfo() < 0)
     return -1;
 
-  if (F2fsFormatDevice() < 0)
+  if (FormatDevice() < 0)
     return -1;
 
   printf("Info: format successful\n");
@@ -64,7 +64,7 @@
   return ZX_OK;
 }
 
-void F2fsMkfs::ASCIIToUNICODE(uint16_t *out_buf, uint8_t *in_buf) {
+void F2fsMkfs::AsciiToUnicode(uint8_t *in_buf, uint16_t *out_buf) {
   uint8_t *pchTempPtr = in_buf;
   uint16_t *pwTempPtr = out_buf;
 
@@ -79,7 +79,7 @@
   *pwTempPtr = '\0';
 }
 
-void F2fsMkfs::F2fsInitGlobalParameters() {
+void F2fsMkfs::InitGlobalParameters() {
   static_assert(__BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__);
 
   f2fs_params.sector_size = DEFAULT_SECTOR_SIZE;
@@ -100,7 +100,7 @@
   f2fs_params.device_name = nullptr;
 }
 
-inline int F2fsMkfs::F2fsSetBit(unsigned int nr, unsigned char *addr) {
+inline int F2fsMkfs::F2fsSetBit(uint32_t nr, uint8_t *addr) {
   int mask;
   int ret;
 
@@ -140,7 +140,7 @@
 }
 #endif
 
-zx_status_t F2fsMkfs::F2fsGetDeviceInfo() {
+zx_status_t F2fsMkfs::GetDeviceInfo() {
   fuchsia_hardware_block_BlockInfo info;
 
   bc_->device()->BlockGetInfo(&info);
@@ -161,7 +161,6 @@
 }
 
 void F2fsMkfs::ConfigureExtensionList() {
-  const char **extlist = media_ext_lists;
   char *ext_str = f2fs_params.extension_list;
   char *ue;
   int name_len;
@@ -170,10 +169,9 @@
   super_block.extension_count = 0;
   memset(super_block.extension_list, 0, sizeof(super_block.extension_list));
 
-  while (*extlist) {
-    name_len = static_cast<int>(strlen(*extlist));
-    memcpy(super_block.extension_list[i++], *extlist, name_len);
-    extlist++;
+  for (const char *ext : kMediaExtList) {
+    name_len = static_cast<int>(strlen(ext));
+    memcpy(super_block.extension_list[i++], ext, name_len);
   }
   super_block.extension_count = i - 1;
 
@@ -231,7 +229,7 @@
   return status;
 }
 
-zx_status_t F2fsMkfs::F2fsPrepareSuperBlock() {
+zx_status_t F2fsMkfs::PrepareSuperBlock() {
   uint32_t blk_size_bytes;
   uint32_t log_sectorsize, log_sectors_per_block;
   uint32_t log_blocksize, log_blks_per_seg;
@@ -336,7 +334,7 @@
    * So the threshold is determined not to overflow one CP page
    */
   sit_bitmap_size = ((LeToCpu(super_block.segment_count_sit) / 2) << log_blks_per_seg) / 8;
-  max_nat_bitmap_size = 4096 - sizeof(struct f2fs_checkpoint) + 1 - sit_bitmap_size;
+  max_nat_bitmap_size = 4096 - sizeof(f2fs_checkpoint) + 1 - sit_bitmap_size;
   max_nat_segments = (max_nat_bitmap_size * 8) >> log_blks_per_seg;
 
   if (LeToCpu(super_block.segment_count_nat) > max_nat_segments)
@@ -384,16 +382,14 @@
       CpuToLe(LeToCpu(super_block.section_count) * f2fs_params.segs_per_sec);
 
   if ((LeToCpu(super_block.segment_count_main) - 2) < f2fs_params.reserved_segments) {
-    printf(
-        "Error: Device size is not sufficient for F2FS volume, \
-			more segment needed =%u",
+    printf("Error: Device size is not sufficient for F2FS volume, more segment needed =%u",
         f2fs_params.reserved_segments - (LeToCpu(super_block.segment_count_main) - 2));
     return ZX_ERR_NO_SPACE;
   }
 
   memcpy(super_block.uuid, uuid::Uuid::Generate().bytes(), 16);
 
-  ASCIIToUNICODE(super_block.volume_name, f2fs_params.vol_label);
+  AsciiToUnicode(f2fs_params.vol_label, super_block.volume_name);
 
   super_block.node_ino = CpuToLe(uint32_t{1});
   super_block.meta_ino = CpuToLe(uint32_t{2});
@@ -402,10 +398,7 @@
   total_zones = ((LeToCpu(super_block.segment_count_main) - 1) / f2fs_params.segs_per_sec) /
                 f2fs_params.secs_per_zone;
   if (total_zones <= 6) {
-    printf(
-        "\n\tError: %d zones: Need more zones \
-			by shrinking zone size\n",
-        total_zones);
+    printf("\n\tError: %d zones: Need more zones	by shrinking zone size\n", total_zones);
     return ZX_ERR_NO_SPACE;
   }
 
@@ -441,7 +434,7 @@
   return 0;
 }
 
-zx_status_t F2fsMkfs::F2fsInitSitArea() {
+zx_status_t F2fsMkfs::InitSitArea() {
   uint32_t blk_size_bytes;
   uint32_t seg_size_bytes;
   uint32_t index = 0;
@@ -463,9 +456,7 @@
   for (index = 0; index < (LeToCpu(super_block.segment_count_sit) / 2); index++) {
     ret = WriteToDisk(zero_buf, sit_seg_blk_offset, seg_size_bytes);
     if (ret < 0) {
-      printf(
-          "\n\tError: While zeroing out the sit area \
-					on disk!!!\n");
+      printf("\n\tError: While zeroing out the sit area on disk!!!\n");
       return ret;
     }
     sit_seg_blk_offset = sit_seg_blk_offset + seg_size_bytes;
@@ -475,7 +466,7 @@
   return ZX_OK;
 }
 
-zx_status_t F2fsMkfs::F2fsInitNatArea() {
+zx_status_t F2fsMkfs::InitNatArea() {
   uint32_t blk_size_bytes;
   uint32_t seg_size_bytes;
   uint32_t index = 0;
@@ -497,9 +488,7 @@
   for (index = 0; index < (LeToCpu(super_block.segment_count_nat) / 2); index++) {
     ret = WriteToDisk(nat_buf, nat_seg_blk_offset, seg_size_bytes);
     if (ret < 0) {
-      printf(
-          "\n\tError: While zeroing out the nat area \
-					on disk!!!\n");
+      printf("\n\tError: While zeroing out the nat area on disk!!!\n");
       return ret;
     }
     nat_seg_blk_offset = nat_seg_blk_offset + (2 * seg_size_bytes);
@@ -509,22 +498,22 @@
   return ZX_OK;
 }
 
-zx_status_t F2fsMkfs::F2fsWriteCheckPointPack() {
-  struct f2fs_checkpoint *ckp = nullptr;
-  struct f2fs_summary_block *sum = nullptr;
+zx_status_t F2fsMkfs::WriteCheckPointPack() {
+  f2fs_checkpoint *ckp = nullptr;
+  f2fs_summary_block *sum = nullptr;
   uint32_t blk_size_bytes;
   uint64_t cp_seg_blk_offset = 0;
   uint32_t crc = 0;
   zx_status_t ret;
   int i;
 
-  ckp = static_cast<struct f2fs_checkpoint *>(calloc(F2FS_BLKSIZE, 1));
+  ckp = static_cast<f2fs_checkpoint *>(calloc(F2FS_BLKSIZE, 1));
   if (ckp == nullptr) {
     printf("\n\tError: Calloc Failed for f2fs_checkpoint!!!\n");
     return ZX_ERR_NO_MEMORY;
   }
 
-  sum = static_cast<struct f2fs_summary_block *>(calloc(F2FS_BLKSIZE, 1));
+  sum = static_cast<f2fs_summary_block *>(calloc(F2FS_BLKSIZE, 1));
   if (sum == nullptr) {
     printf("\n\tError: Calloc Failed for summay_node!!!\n");
     return ZX_ERR_NO_MEMORY;
@@ -575,10 +564,10 @@
                                               << LeToCpu(super_block.log_blocks_per_seg)) /
                                              8);
 
-  ckp->checksum_offset = CpuToLe(uint32_t{4092}); //TODO use constexpr
+  ckp->checksum_offset = CpuToLe(uint32_t{kChecksumOffset});
 
   crc = F2fsCalCrc32(kF2fsSuperMagic, ckp, LeToCpu(ckp->checksum_offset));
-  *(reinterpret_cast<uint32_t *>(reinterpret_cast<unsigned char *>(ckp) +
+  *(reinterpret_cast<uint32_t *>(reinterpret_cast<uint8_t *>(ckp) +
                                  LeToCpu(ckp->checksum_offset))) = crc;
 
   blk_size_bytes = 1 << LeToCpu(super_block.log_blocksize);
@@ -591,7 +580,7 @@
   }
 
   /* 2. Prepare and write Segment summary for data blocks */
-  memset(sum, 0, sizeof(struct f2fs_summary_block));
+  memset(sum, 0, sizeof(f2fs_summary_block));
   SET_SUM_TYPE((&sum->footer), SUM_TYPE_DATA);
 
   sum->entries[0].nid = super_block.root_ino;
@@ -605,7 +594,7 @@
   }
 
   /* 3. Fill segment summary for data block to zero. */
-  memset(sum, 0, sizeof(struct f2fs_summary_block));
+  memset(sum, 0, sizeof(f2fs_summary_block));
   SET_SUM_TYPE((&sum->footer), SUM_TYPE_DATA);
 
   cp_seg_blk_offset += blk_size_bytes;
@@ -616,7 +605,7 @@
   }
 
   /* 4. Fill segment summary for data block to zero. */
-  memset(sum, 0, sizeof(struct f2fs_summary_block));
+  memset(sum, 0, sizeof(f2fs_summary_block));
   SET_SUM_TYPE((&sum->footer), SUM_TYPE_DATA);
 
   /* inode sit for root */
@@ -646,7 +635,7 @@
   }
 
   /* 5. Prepare and write Segment summary for node blocks */
-  memset(sum, 0, sizeof(struct f2fs_summary_block));
+  memset(sum, 0, sizeof(f2fs_summary_block));
   SET_SUM_TYPE((&sum->footer), SUM_TYPE_NODE);
 
   sum->entries[0].nid = super_block.root_ino;
@@ -660,7 +649,7 @@
   }
 
   /* 6. Fill segment summary for data block to zero. */
-  memset(sum, 0, sizeof(struct f2fs_summary_block));
+  memset(sum, 0, sizeof(f2fs_summary_block));
   SET_SUM_TYPE((&sum->footer), SUM_TYPE_NODE);
 
   cp_seg_blk_offset += blk_size_bytes;
@@ -671,7 +660,7 @@
   }
 
   /* 7. Fill segment summary for data block to zero. */
-  memset(sum, 0, sizeof(struct f2fs_summary_block));
+  memset(sum, 0, sizeof(f2fs_summary_block));
   SET_SUM_TYPE((&sum->footer), SUM_TYPE_NODE);
   cp_seg_blk_offset += blk_size_bytes;
   ret = WriteToDisk(sum, cp_seg_blk_offset, F2FS_BLKSIZE);
@@ -694,7 +683,7 @@
   ckp->checkpoint_ver = 0;
 
   crc = F2fsCalCrc32(kF2fsSuperMagic, ckp, LeToCpu(ckp->checksum_offset));
-  *(reinterpret_cast<uint32_t *>(reinterpret_cast<unsigned char *>(ckp) +
+  *(reinterpret_cast<uint32_t *>(reinterpret_cast<uint8_t *>(ckp) +
                                  LeToCpu(ckp->checksum_offset))) = crc;
 
   cp_seg_blk_offset =
@@ -710,7 +699,7 @@
   return ZX_OK;
 }
 
-zx_status_t F2fsMkfs::F2fsWriteSuperBlock() {
+zx_status_t F2fsMkfs::WriteSuperBlock() {
   uint32_t index = 0;
   uint8_t *zero_buff;
   zx_status_t ret;
@@ -722,10 +711,7 @@
   for (index = 0; index < 2; index++) {
     ret = WriteToDisk(zero_buff, index * F2FS_BLKSIZE, F2FS_BLKSIZE);
     if (ret < 0) {
-      printf(
-          "\n\tError: While while writing supe_blk \
-					on disk!!! index : %d\n",
-          index);
+      printf("\n\tError: While while writing supe_blk	on disk!!! index : %d\n", index);
       return ret;
     }
   }
@@ -734,14 +720,14 @@
   return ZX_OK;
 }
 
-zx_status_t F2fsMkfs::F2fsWriteRootInode() {
-  struct f2fs_node *raw_node = nullptr;
+zx_status_t F2fsMkfs::WriteRootInode() {
+  f2fs_node *raw_node = nullptr;
   uint32_t blk_size_bytes;
   uint64_t data_blk_nor;
   uint64_t main_area_node_seg_blk_offset = 0;
   zx_status_t ret;
 
-  raw_node = static_cast<struct f2fs_node *>(calloc(F2FS_BLKSIZE, 1));
+  raw_node = static_cast<f2fs_node *>(calloc(F2FS_BLKSIZE, 1));
   if (raw_node == nullptr) {
     printf("\n\tError: Calloc Failed for raw_node!!!\n");
     return ZX_ERR_NO_MEMORY;
@@ -788,12 +774,11 @@
 
   ret = WriteToDisk(raw_node, main_area_node_seg_blk_offset, F2FS_BLKSIZE);
   if (ret < 0) {
-    printf("\n\tError: While writing the raw_node to disk!!!, size = %lu\n",
-           sizeof(struct f2fs_node));
+    printf("\n\tError: While writing the raw_node to disk!!!, size = %lu\n",sizeof(f2fs_node));
     return ret;
   }
 
-  memset(raw_node, 0xff, sizeof(struct f2fs_node));
+  memset(raw_node, 0xff, sizeof(f2fs_node));
 
   ret = WriteToDisk(raw_node, main_area_node_seg_blk_offset + 4096, F2FS_BLKSIZE);
   if (ret < 0) {
@@ -804,13 +789,13 @@
   return ZX_OK;
 }
 
-zx_status_t F2fsMkfs::F2fsUpdateNatRoot() {
-  struct f2fs_nat_block *nat_blk = nullptr;
+zx_status_t F2fsMkfs::UpdateNatRoot() {
+  f2fs_nat_block *nat_blk = nullptr;
   uint32_t blk_size_bytes;
   uint64_t nat_seg_blk_offset = 0;
   zx_status_t ret;
 
-  nat_blk = static_cast<struct f2fs_nat_block *>(calloc(F2FS_BLKSIZE, 1));
+  nat_blk = static_cast<f2fs_nat_block *>(calloc(F2FS_BLKSIZE, 1));
   if (nat_blk == nullptr) {
     printf("\n\tError: Calloc Failed for nat_blk!!!\n");
     return ZX_ERR_NO_MEMORY;
@@ -844,13 +829,13 @@
   return ZX_OK;
 }
 
-zx_status_t F2fsMkfs::F2fsAddDefaultDentryRoot() {
-  struct f2fs_dentry_block *dent_blk = nullptr;
+zx_status_t F2fsMkfs::AddDefaultDentryRoot() {
+  f2fs_dentry_block *dent_blk = nullptr;
   uint32_t blk_size_bytes;
   uint64_t data_blk_offset = 0;
   zx_status_t ret;
 
-  dent_blk = static_cast<struct f2fs_dentry_block *>(calloc(F2FS_BLKSIZE, 1));
+  dent_blk = static_cast<f2fs_dentry_block *>(calloc(F2FS_BLKSIZE, 1));
   if (dent_blk == nullptr) {
     printf("\n\tError: Calloc Failed for dent_blk!!!\n");
     return ZX_ERR_NO_MEMORY;
@@ -885,22 +870,22 @@
   return ZX_OK;
 }
 
-zx_status_t F2fsMkfs::F2fsCreateRootDir() {
+zx_status_t F2fsMkfs::CreateRootDir() {
   int8_t err = 0;
 
-  err = F2fsWriteRootInode();
+  err = WriteRootInode();
   if (err < 0) {
     printf("\n\tError: Failed to write root inode!!!\n");
     goto exit;
   }
 
-  err = F2fsUpdateNatRoot();
+  err = UpdateNatRoot();
   if (err < 0) {
     printf("\n\tError: Failed to update NAT for root!!!\n");
     goto exit;
   }
 
-  err = F2fsAddDefaultDentryRoot();
+  err = AddDefaultDentryRoot();
   if (err < 0) {
     printf("\n\tError: Failed to add default dentries for root!!!\n");
     goto exit;
@@ -915,8 +900,8 @@
 #if 0  // porting needed
 // int F2fsMkfs::F2fsTrimDevice()
 // {
-//         unsigned long long range[2];
-//         struct stat stat_buf;
+//         uint64_t range[2];
+//         stat stat_buf;
 
 //         range[0] = 0;
 //         range[1] = f2fs_params.total_sectors * DEFAULT_SECTOR_SIZE;
@@ -937,10 +922,10 @@
 // }
 #endif
 
-int8_t F2fsMkfs::F2fsFormatDevice() {
-  int8_t err = 0;
+zx_status_t F2fsMkfs::FormatDevice() {
+  zx_status_t err = 0;
 
-  err = static_cast<int8_t>(F2fsPrepareSuperBlock());
+  err = PrepareSuperBlock();
   if (err < 0)
     goto exit;
 
@@ -952,31 +937,31 @@
   //   goto exit;
   // }
 #endif
-  err = F2fsInitSitArea();
+  err = InitSitArea();
   if (err < 0) {
     printf("\n\tError: Failed to Initialise the SIT AREA!!!\n");
     goto exit;
   }
 
-  err = F2fsInitNatArea();
+  err = InitNatArea();
   if (err < 0) {
     printf("\n\tError: Failed to Initialise the NAT AREA!!!\n");
     goto exit;
   }
 
-  err = F2fsCreateRootDir();
+  err = CreateRootDir();
   if (err < 0) {
     printf("\n\tError: Failed to create the root directory!!!\n");
     goto exit;
   }
 
-  err = F2fsWriteCheckPointPack();
+  err = WriteCheckPointPack();
   if (err < 0) {
     printf("\n\tError: Failed to write the check point pack!!!\n");
     goto exit;
   }
 
-  err = F2fsWriteSuperBlock();
+  err = WriteSuperBlock();
   if (err < 0) {
     printf("\n\tError: Failed to write the Super Block!!!\n");
     goto exit;
diff --git a/mkfs.h b/mkfs.h
index 0e459c4..c08a22f 100644
--- a/mkfs.h
+++ b/mkfs.h
@@ -7,6 +7,7 @@
 
 namespace f2fs {
 
+constexpr uint32_t kChecksumOffset = 4092;
 struct MkfsOptions {
   // TODO: Add mkfs options
   /*
@@ -20,19 +21,19 @@
           -e [extention list] e.g. "mp3,gif,mov"
    */
 
-  char* label;
+  char* label = nullptr;
   bool heap_based_allocation = false;
   uint32_t overprovision_ratio = 5;
   uint32_t num_of_seg_per_sec = 1;
   uint32_t num_of_sec_per_zone = 1;
-  char* extention_list;
+  char* extention_list = nullptr;
 };
 
 class F2fsMkfs {
  public:
   F2fsMkfs(std::unique_ptr<f2fs::Bcache> bc, const MkfsOptions& mkfs_options);
 
-  ~F2fsMkfs() {}
+  ~F2fsMkfs() = default;
 
   zx_status_t Mkfs();
 
@@ -41,31 +42,31 @@
   [[maybe_unused]] const MkfsOptions& mkfs_options_;
 
   // F2FS Parameter
-  struct f2fs_global_parameters f2fs_params;
-  struct f2fs_super_block super_block;
+  f2fs_global_parameters f2fs_params;
+  f2fs_super_block super_block;
 
-  void F2fsInitGlobalParameters();
-  zx_status_t F2fsGetDeviceInfo();
-  int8_t F2fsFormatDevice();
+  void InitGlobalParameters();
+  zx_status_t GetDeviceInfo();
+  zx_status_t FormatDevice();
 
-  void ASCIIToUNICODE(uint16_t* out_buf, uint8_t* in_buf);
+  void AsciiToUnicode(uint8_t* in_buf, uint16_t* out_buf);
 
-  inline int F2fsSetBit(unsigned int nr, unsigned char* addr);
+  inline int F2fsSetBit(uint32_t nr, uint8_t* addr);
   int8_t LogBase2(uint32_t num);
   void ConfigureExtensionList();
 
   zx_status_t WriteToDisk(void* buf, uint64_t offset, size_t length);
 
-  zx_status_t F2fsPrepareSuperBlock();
-  zx_status_t F2fsInitSitArea();
-  zx_status_t F2fsInitNatArea();
+  zx_status_t PrepareSuperBlock();
+  zx_status_t InitSitArea();
+  zx_status_t InitNatArea();
 
-  zx_status_t F2fsWriteCheckPointPack();
-  zx_status_t F2fsWriteSuperBlock();
-  zx_status_t F2fsWriteRootInode();
-  zx_status_t F2fsUpdateNatRoot();
-  zx_status_t F2fsAddDefaultDentryRoot();
-  zx_status_t F2fsCreateRootDir();
+  zx_status_t WriteCheckPointPack();
+  zx_status_t WriteSuperBlock();
+  zx_status_t WriteRootInode();
+  zx_status_t UpdateNatRoot();
+  zx_status_t AddDefaultDentryRoot();
+  zx_status_t CreateRootDir();
 
 
 #if 0  // porting needed