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

  Add default initializers for struct member variables (segment.h)
  (comment: #53 https://fuchsia-review.googlesource.com/c/third_party/f2fs/+/511839)

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

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

  Change #define to inline function (segment.h, segment.cc, node.cc, recovery.cc)
  (comment: #749 https://fuchsia-review.googlesource.com/c/third_party/f2fs/+/511842)

  Change enum to enum class (segment.h, segment.cc)
  (comment: #429 https://fuchsia-review.googlesource.com/c/third_party/f2fs/+/511841)

  Change C style cast to static_cast (segment.cc)
  (comment: #571 https://fuchsia-review.googlesource.com/c/third_party/f2fs/+/520880)

  Change #define to constexpr (segment.cc)
  (comment: #9, #128 https://fuchsia-review.googlesource.com/c/third_party/f2fs/+/511840)

  Change error return using zx_status_t (segment.h, segment.cc)
  (comment: #24, #106 https://fuchsia-review.googlesource.com/c/third_party/f2fs/+/520882)

  Apply C++ Style (segment.cc)

Change-Id: I2822d3d3375aabf537141d0e3ac53402ef702259
Reviewed-on: https://fuchsia-review.googlesource.com/c/third_party/f2fs/+/530805
Reviewed-by: Brett Wilson <brettw@google.com>
diff --git a/node.cc b/node.cc
index e5cb9bc..29a0a0b 100644
--- a/node.cc
+++ b/node.cc
@@ -1812,7 +1812,7 @@
 
   /* scan the node segment */
   last_offset = sbi.blocks_per_seg;
-  addr = START_BLOCK(&sbi, segno);
+  addr = StartBlock(&sbi, segno);
   sum_entry = &sum->entries[0];
 
 #if 0  // porting needed
diff --git a/recovery.cc b/recovery.cc
index 57cabc7..1c86ddb 100644
--- a/recovery.cc
+++ b/recovery.cc
@@ -99,7 +99,7 @@
 
   /* get node pages in the current segment */
   curseg = SegMgr::CURSEG_I(&sbi, CURSEG_WARM_NODE);
-  blkaddr = START_BLOCK(&sbi, curseg->segno) + curseg->next_blkoff;
+  blkaddr = StartBlock(&sbi, curseg->segno) + curseg->next_blkoff;
 
   /* read node page */
   page = GrabCachePage(nullptr, F2FS_NODE_INO(sbi_), blkaddr);
@@ -205,8 +205,8 @@
 void F2fs::CheckIndexInPrevNodes(block_t blkaddr) {
   f2fs_sb_info &sbi = SbInfo();
   seg_entry *sentry;
-  uint32_t segno = GET_SEGNO(&sbi, blkaddr);
-  uint16_t blkoff = GET_SEGOFF_FROM_SEG0(&sbi, blkaddr) & (sbi.blocks_per_seg - 1);
+  uint32_t segno = GetSegNo(&sbi, blkaddr);
+  uint16_t blkoff = GetSegOffFromSeg0(&sbi, blkaddr) & (sbi.blocks_per_seg - 1);
   f2fs_summary sum;
   nid_t ino;
   void *kaddr;
@@ -335,7 +335,7 @@
 
   /* get node pages in the current segment */
   curseg = SegMgr::CURSEG_I(&sbi, type);
-  blkaddr = NEXT_FREE_BLKADDR(&sbi, curseg);
+  blkaddr = NextFreeBlkAddr(&sbi, curseg);
 
   /* read node page */
   page = GrabCachePage(nullptr, F2FS_NODE_INO(sbi_), blkaddr);
diff --git a/segment.cc b/segment.cc
index 97f4905..7fdf074 100644
--- a/segment.cc
+++ b/segment.cc
@@ -14,30 +14,30 @@
 /*
  * inline functions
  */
-inline struct seg_entry *SegMgr::GetSegEntry(unsigned int segno) {
+inline seg_entry *SegMgr::GetSegEntry(uint32_t segno) {
   f2fs_sb_info &sbi = fs_->SbInfo();
-  struct sit_info *sit_i = SIT_I(&sbi);
+  sit_info *sit_i = SIT_I(&sbi);
   return &sit_i->sentries[segno];
 }
 
-inline struct sec_entry *SegMgr::GetSecEntry(unsigned int segno) {
+inline sec_entry *SegMgr::GetSecEntry(uint32_t segno) {
   f2fs_sb_info &sbi = fs_->SbInfo();
-  struct sit_info *sit_i = SIT_I(&sbi);
-  return &sit_i->sec_entries[GET_SECNO(&sbi, segno)];
+  sit_info *sit_i = SIT_I(&sbi);
+  return &sit_i->sec_entries[GetSecNo(&sbi, segno)];
 }
 
-inline unsigned int SegMgr::GetValidBlocks(unsigned int segno, int section) {
+inline uint32_t SegMgr::GetValidBlocks(uint32_t segno, int section) {
   /*
    * In order to get # of valid blocks in a section instantly from many
    * segments, f2fs manages two counting structures separately.
    */
-  if (section > 1)
+  if (section > 1) {
     return GetSecEntry(segno)->valid_blocks;
-  else
-    return GetSegEntry(segno)->valid_blocks;
+  }
+  return GetSegEntry(segno)->valid_blocks;
 }
 
-inline void SegMgr::SegInfoFromRawSit(struct seg_entry *se, struct f2fs_sit_entry *rs) {
+inline void SegMgr::SegInfoFromRawSit(seg_entry *se, f2fs_sit_entry *rs) {
   se->valid_blocks = GET_SIT_VBLOCKS(rs);
   se->ckpt_valid_blocks = GET_SIT_VBLOCKS(rs);
   memcpy(se->cur_valid_map, rs->valid_map, SIT_VBLOCK_MAP_SIZE);
@@ -46,8 +46,8 @@
   se->mtime = LeToCpu(uint64_t{rs->mtime});
 }
 
-inline void SegMgr::SegInfoToRawSit(struct seg_entry *se, struct f2fs_sit_entry *rs) {
-  unsigned short raw_vblocks = (se->type << SIT_VBLOCKS_SHIFT) | se->valid_blocks;
+inline void SegMgr::SegInfoToRawSit(seg_entry *se, f2fs_sit_entry *rs) {
+  uint16_t raw_vblocks = (se->type << SIT_VBLOCKS_SHIFT) | se->valid_blocks;
   rs->vblocks = CpuToLe(raw_vblocks);
   memcpy(rs->valid_map, se->cur_valid_map, SIT_VBLOCK_MAP_SIZE);
   memcpy(se->ckpt_valid_map, rs->valid_map, SIT_VBLOCK_MAP_SIZE);
@@ -55,27 +55,27 @@
   rs->mtime = CpuToLe(static_cast<uint64_t>(se->mtime));
 }
 
-inline unsigned int SegMgr::FindNextInuse(struct free_segmap_info *free_i, unsigned int max,
-                                          unsigned int segno) {
-  unsigned int ret;
+inline uint32_t SegMgr::FindNextInuse(free_segmap_info *free_i, uint32_t max,
+                                          uint32_t segno) {
+  uint32_t ret;
   ReadLock(&free_i->segmap_lock);
   ret = find_next_bit_le(free_i->free_segmap, max, segno);
   ReadUnlock(&free_i->segmap_lock);
   return ret;
 }
 
-inline void SegMgr::__SetFree(unsigned int segno) {
+inline void SegMgr::__SetFree(uint32_t segno) {
   f2fs_sb_info &sbi = fs_->SbInfo();
-  struct free_segmap_info *free_i = FREE_I(&sbi);
-  unsigned int secno = segno / sbi.segs_per_sec;
-  unsigned int start_segno = secno * sbi.segs_per_sec;
-  unsigned int next;
+  free_segmap_info *free_i = FREE_I(&sbi);
+  uint32_t secno = segno / sbi.segs_per_sec;
+  uint32_t start_segno = secno * sbi.segs_per_sec;
+  uint32_t next;
 
   WriteLock(&free_i->segmap_lock);
   clear_bit(segno, free_i->free_segmap);
   free_i->free_segments++;
 
-  next = find_next_bit_le(free_i->free_segmap, TOTAL_SEGS(&sbi), start_segno);
+  next = find_next_bit_le(free_i->free_segmap, TotalSegs(&sbi), start_segno);
   if (next >= start_segno + sbi.segs_per_sec) {
     clear_bit(secno, free_i->free_secmap);
     free_i->free_sections++;
@@ -83,28 +83,28 @@
   WriteUnlock(&free_i->segmap_lock);
 }
 
-inline void SegMgr::__SetInuse(unsigned int segno) {
+inline void SegMgr::__SetInuse(uint32_t segno) {
   f2fs_sb_info &sbi = fs_->SbInfo();
-  struct free_segmap_info *free_i = FREE_I(&sbi);
-  unsigned int secno = segno / sbi.segs_per_sec;
+  free_segmap_info *free_i = FREE_I(&sbi);
+  uint32_t secno = segno / sbi.segs_per_sec;
   set_bit(segno, free_i->free_segmap);
   free_i->free_segments--;
   if (!test_and_set_bit(secno, free_i->free_secmap))
     free_i->free_sections--;
 }
 
-inline void SegMgr::__SetTestAndFree(unsigned int segno) {
+inline void SegMgr::__SetTestAndFree(uint32_t segno) {
   f2fs_sb_info &sbi = fs_->SbInfo();
-  struct free_segmap_info *free_i = FREE_I(&sbi);
-  unsigned int secno = segno / sbi.segs_per_sec;
-  unsigned int start_segno = secno * sbi.segs_per_sec;
-  unsigned int next;
+  free_segmap_info *free_i = FREE_I(&sbi);
+  uint32_t secno = segno / sbi.segs_per_sec;
+  uint32_t start_segno = secno * sbi.segs_per_sec;
+  uint32_t next;
 
   WriteLock(&free_i->segmap_lock);
   if (test_and_clear_bit(segno, free_i->free_segmap)) {
     free_i->free_segments++;
 
-    next = find_next_bit_le(free_i->free_segmap, TOTAL_SEGS(&sbi), start_segno);
+    next = find_next_bit_le(free_i->free_segmap, TotalSegs(&sbi), start_segno);
     if (next >= start_segno + sbi.segs_per_sec) {
       if (test_and_clear_bit(secno, free_i->free_secmap))
         free_i->free_sections++;
@@ -113,10 +113,10 @@
   WriteUnlock(&free_i->segmap_lock);
 }
 
-inline void SegMgr::__SetTestAndInuse(unsigned int segno) {
+inline void SegMgr::__SetTestAndInuse(uint32_t segno) {
   f2fs_sb_info &sbi = fs_->SbInfo();
-  struct free_segmap_info *free_i = FREE_I(&sbi);
-  unsigned int secno = segno / sbi.segs_per_sec;
+  free_segmap_info *free_i = FREE_I(&sbi);
+  uint32_t secno = segno / sbi.segs_per_sec;
   WriteLock(&free_i->segmap_lock);
   if (!test_and_set_bit(segno, free_i->free_segmap)) {
     free_i->free_segments--;
@@ -128,14 +128,14 @@
 
 void SegMgr::GetSitBitmap(void *dst_addr) {
   f2fs_sb_info &sbi = fs_->SbInfo();
-  struct sit_info *sit_i = SIT_I(&sbi);
+  sit_info *sit_i = SIT_I(&sbi);
   memcpy(dst_addr, sit_i->sit_bitmap, sit_i->bitmap_size);
 }
 
 #if 0  // porting needed
 inline block_t SegMgr::WrittenBlockCount() {
   f2fs_sb_info &sbi = fs_->SbInfo();
-  struct sit_info *sit_i = SIT_I(&sbi);
+  sit_info *sit_i = SIT_I(&sbi);
   block_t vblocks;
 
   mtx_lock(&sit_i->sentry_lock);
@@ -146,10 +146,10 @@
 }
 #endif
 
-unsigned int SegMgr::FreeSegments() {
+uint32_t SegMgr::FreeSegments() {
   f2fs_sb_info &sbi = fs_->SbInfo();
-  struct free_segmap_info *free_i = FREE_I(&sbi);
-  unsigned int free_segs;
+  free_segmap_info *free_i = FREE_I(&sbi);
+  uint32_t free_segs;
 
   ReadLock(&free_i->segmap_lock);
   free_segs = free_i->free_segments;
@@ -163,10 +163,10 @@
   return SM_I(&sbi)->reserved_segments;
 }
 
-inline unsigned int SegMgr::FreeSections() {
+inline uint32_t SegMgr::FreeSections() {
   f2fs_sb_info &sbi = fs_->SbInfo();
-  struct free_segmap_info *free_i = FREE_I(&sbi);
-  unsigned int free_secs;
+  free_segmap_info *free_i = FREE_I(&sbi);
+  uint32_t free_secs;
 
   ReadLock(&free_i->segmap_lock);
   free_secs = free_i->free_sections;
@@ -175,16 +175,19 @@
   return free_secs;
 }
 
-inline unsigned int SegMgr::PrefreeSegments() {
+inline uint32_t SegMgr::PrefreeSegments() {
   f2fs_sb_info &sbi = fs_->SbInfo();
-  return DIRTY_I(&sbi)->nr_dirty[PRE];
+  return DIRTY_I(&sbi)->nr_dirty[static_cast<int>(DirtyType::kPre)];
 }
 
-inline unsigned int SegMgr::DirtySegments() {
+inline uint32_t SegMgr::DirtySegments() {
   f2fs_sb_info &sbi = fs_->SbInfo();
-  return DIRTY_I(&sbi)->nr_dirty[DIRTY_HOT_DATA] + DIRTY_I(&sbi)->nr_dirty[DIRTY_WARM_DATA] +
-         DIRTY_I(&sbi)->nr_dirty[DIRTY_COLD_DATA] + DIRTY_I(&sbi)->nr_dirty[DIRTY_HOT_NODE] +
-         DIRTY_I(&sbi)->nr_dirty[DIRTY_WARM_NODE] + DIRTY_I(&sbi)->nr_dirty[DIRTY_COLD_NODE];
+  return DIRTY_I(&sbi)->nr_dirty[static_cast<int>(DirtyType::kDirtyHotData)] +
+        DIRTY_I(&sbi)->nr_dirty[static_cast<int>(DirtyType::kDirtyWarmData)] +
+        DIRTY_I(&sbi)->nr_dirty[static_cast<int>(DirtyType::kDirtyColdData)] +
+        DIRTY_I(&sbi)->nr_dirty[static_cast<int>(DirtyType::kDirtyHotNode)] +
+        DIRTY_I(&sbi)->nr_dirty[static_cast<int>(DirtyType::kDirtyWarmNode)] +
+        DIRTY_I(&sbi)->nr_dirty[static_cast<int>(DirtyType::kDirtyColdNode)];
 }
 
 inline int SegMgr::OverprovisionSegments() {
@@ -194,68 +197,69 @@
 
 inline int SegMgr::OverprovisionSections() {
   f2fs_sb_info &sbi = fs_->SbInfo();
-  return ((unsigned int)OverprovisionSegments()) / sbi.segs_per_sec;
+  return (static_cast<uint32_t>(OverprovisionSegments())) / sbi.segs_per_sec;
 }
 
 inline int SegMgr::ReservedSections() {
   f2fs_sb_info &sbi = fs_->SbInfo();
-  return ((unsigned int)ReservedSegments()) / sbi.segs_per_sec;
+  return (static_cast<uint32_t>(ReservedSegments())) / sbi.segs_per_sec;
 }
 
-inline bool SegMgr::NeedSSR() { return (FreeSections() < (unsigned int)OverprovisionSections()); }
+inline bool SegMgr::NeedSSR() { return (FreeSections() < static_cast<uint32_t>(OverprovisionSections())); }
 
 inline int SegMgr::GetSsrSegment(int type) {
   f2fs_sb_info &sbi = fs_->SbInfo();
-  struct curseg_info *curseg = CURSEG_I(&sbi, type);
-  return DIRTY_I(&sbi)->v_ops->get_victim(&sbi, &(curseg)->next_segno, BG_GC, type, SSR);
+  curseg_info *curseg = CURSEG_I(&sbi, type);
+  return DIRTY_I(&sbi)->v_ops->get_victim(&sbi, &(curseg)->next_segno, static_cast<int>(GcType::kBgGc), type, static_cast<uint8_t>(AllocMode::kLFS));
 }
 
 inline bool SegMgr::HasNotEnoughFreeSecs() {
-  return FreeSections() <= (unsigned int)ReservedSections();
+  return FreeSections() <= static_cast<uint32_t>(ReservedSections());
 }
 
-inline int SegMgr::Utilization() {
+inline uint32_t SegMgr::Utilization() {
   f2fs_sb_info &sbi = fs_->SbInfo();
-  return (long int)valid_user_blocks(&sbi) * 100 / (long int)sbi.user_block_count;
+  return static_cast<uint32_t>(static_cast<int64_t>(valid_user_blocks(&sbi)) * 100 /
+            static_cast<int64_t>(sbi.user_block_count));
 }
 
 /*
  * Sometimes f2fs may be better to drop out-of-place update policy.
- * So, if fs utilization is over MIN_IPU_UTIL, then f2fs tries to write
+ * So, if fs utilization is over kMinIpuUtil, then f2fs tries to write
  * data in the original place likewise other traditional file systems.
  * But, currently set 100 in percentage, which means it is disabled.
  * See below need_inplace_update().
  */
-#define MIN_IPU_UTIL 50
+constexpr uint32_t kMinIpuUtil = 50;
 bool SegMgr::NeedInplaceUpdate(VnodeF2fs *vnode) {
   if (S_ISDIR(vnode->i_mode_))
     return false;
-  if (/*NeedSSR() &&*/ Utilization() > MIN_IPU_UTIL)
+  if (/*NeedSSR() &&*/ Utilization() > kMinIpuUtil)
     return true;
   return false;
 }
 
-unsigned int SegMgr::CursegSegno(int type) {
+uint32_t SegMgr::CursegSegno(int type) {
   f2fs_sb_info &sbi = fs_->SbInfo();
-  struct curseg_info *curseg = CURSEG_I(&sbi, type);
+  curseg_info *curseg = CURSEG_I(&sbi, type);
   return curseg->segno;
 }
 
-unsigned char SegMgr::CursegAllocType(int type) {
+uint8_t SegMgr::CursegAllocType(int type) {
   f2fs_sb_info &sbi = fs_->SbInfo();
-  struct curseg_info *curseg = CURSEG_I(&sbi, type);
+  curseg_info *curseg = CURSEG_I(&sbi, type);
   return curseg->alloc_type;
 }
 
-inline unsigned short SegMgr::CursegBlkoff(int type) {
+inline uint16_t SegMgr::CursegBlkoff(int type) {
   f2fs_sb_info &sbi = fs_->SbInfo();
-  struct curseg_info *curseg = CURSEG_I(&sbi, type);
+  curseg_info *curseg = CURSEG_I(&sbi, type);
   return curseg->next_blkoff;
 }
 
-inline void SegMgr::CheckSegRange(unsigned int segno) {
+inline void SegMgr::CheckSegRange(uint32_t segno) {
   f2fs_sb_info &sbi = fs_->SbInfo();
-  [[maybe_unused]] unsigned int end_segno = SM_I(&sbi)->segment_count - 1;
+  [[maybe_unused]] uint32_t end_segno = SM_I(&sbi)->segment_count - 1;
   ZX_ASSERT(!(segno > end_segno));
 }
 
@@ -266,7 +270,7 @@
  */
 inline void SegMgr::VerifyBlockAddr(block_t blk_addr) {
   f2fs_sb_info &sbi = fs_->SbInfo();
-  struct f2fs_sm_info *sm_info = SM_I(&sbi);
+  f2fs_sm_info *sm_info = SM_I(&sbi);
   block_t total_blks = sm_info->segment_count << sbi.log_blocks_per_seg;
   [[maybe_unused]] block_t start_addr = sm_info->seg0_blkaddr;
   [[maybe_unused]] block_t end_addr = start_addr + total_blks - 1;
@@ -278,12 +282,12 @@
 /*
  * Summary block is always treated as invalid block
  */
-inline void SegMgr::CheckBlockCount(int segno, struct f2fs_sit_entry *raw_sit) {
+inline void SegMgr::CheckBlockCount(int segno, f2fs_sit_entry *raw_sit) {
   f2fs_sb_info &sbi = fs_->SbInfo();
-  struct f2fs_sm_info *sm_info = SM_I(&sbi);
-  unsigned int end_segno = sm_info->segment_count - 1;
+  f2fs_sm_info *sm_info = SM_I(&sbi);
+  uint32_t end_segno = sm_info->segment_count - 1;
   int valid_blocks = 0;
-  unsigned int i;
+  uint32_t i;
 
   /* check segment usage */
   ZX_ASSERT(!(GET_SIT_VBLOCKS(raw_sit) > sbi.blocks_per_seg));
@@ -292,22 +296,23 @@
   ZX_ASSERT(!(segno > (int)end_segno));
 
   /* check bitmap with valid block count */
-  for (i = 0; i < sbi.blocks_per_seg; i++)
-    if (f2fs_test_bit(i, (char *)raw_sit->valid_map))
+  for (i = 0; i < sbi.blocks_per_seg; i++) {
+    if (f2fs_test_bit(i, reinterpret_cast<char *>(raw_sit->valid_map)))
       valid_blocks++;
+  }
   ZX_ASSERT(GET_SIT_VBLOCKS(raw_sit) == valid_blocks);
 }
 
-inline pgoff_t SegMgr::CurrentSitAddr(unsigned int start) {
+inline pgoff_t SegMgr::CurrentSitAddr(uint32_t start) {
   f2fs_sb_info &sbi = fs_->SbInfo();
-  struct sit_info *sit_i = SIT_I(&sbi);
-  unsigned int offset = SIT_BLOCK_OFFSET(sit_i, start);
+  sit_info *sit_i = SIT_I(&sbi);
+  uint32_t offset = SitBlockOffset(sit_i, start);
   block_t blk_addr = sit_i->sit_base_addr + offset;
 
   CheckSegRange(start);
 
   /* calculate sit block address */
-  if (f2fs_test_bit(offset, (char *)sit_i->sit_bitmap))
+  if (f2fs_test_bit(offset, sit_i->sit_bitmap))
     blk_addr += sit_i->sit_blocks;
 
   return blk_addr;
@@ -315,7 +320,7 @@
 
 inline pgoff_t SegMgr::NextSitAddr(pgoff_t block_addr) {
   f2fs_sb_info &sbi = fs_->SbInfo();
-  struct sit_info *sit_i = SIT_I(&sbi);
+  sit_info *sit_i = SIT_I(&sbi);
   block_addr -= sit_i->sit_base_addr;
   if (block_addr < sit_i->sit_blocks)
     block_addr += sit_i->sit_blocks;
@@ -325,26 +330,27 @@
   return block_addr + sit_i->sit_base_addr;
 }
 
-inline void SegMgr::SetToNextSit(struct sit_info *sit_i, unsigned int start) {
-  unsigned int block_off = SIT_BLOCK_OFFSET(sit_i, start);
+inline void SegMgr::SetToNextSit(sit_info *sit_i, uint32_t start) {
+  uint32_t block_off = SitBlockOffset(sit_i, start);
 
-  if (f2fs_test_bit(block_off, sit_i->sit_bitmap))
+  if (f2fs_test_bit(block_off, sit_i->sit_bitmap)) {
     f2fs_clear_bit(block_off, sit_i->sit_bitmap);
-  else
+  } else {
     f2fs_set_bit(block_off, sit_i->sit_bitmap);
+  }
 }
 
-inline unsigned long long SegMgr::GetMtime() {
+inline uint64_t SegMgr::GetMtime() {
   auto cur_time = time(nullptr);
   f2fs_sb_info &sbi = fs_->SbInfo();
-  struct sit_info *sit_i = SIT_I(&sbi);
+  sit_info *sit_i = SIT_I(&sbi);
 
   return sit_i->elapsed_time + cur_time - sit_i->mounted_time;
   return 0;
 }
 
-inline void SegMgr::SetSummary(struct f2fs_summary *sum, nid_t nid, unsigned int ofs_in_node,
-                               unsigned char version) {
+inline void SegMgr::SetSummary(f2fs_summary *sum, nid_t nid, uint32_t ofs_in_node,
+                               uint8_t version) {
   sum->nid = CpuToLe(nid);
   sum->ofs_in_node = CpuToLe(ofs_in_node);
   sum->version = version;
@@ -369,7 +375,7 @@
 int SegMgr::NeedToFlush() {
   f2fs_sb_info &sbi = fs_->SbInfo();
 
-  unsigned int pages_per_sec = (1 << sbi.log_blocks_per_seg) * sbi.segs_per_sec;
+  uint32_t pages_per_sec = (1 << sbi.log_blocks_per_seg) * sbi.segs_per_sec;
   int node_secs =
       ((get_pages(&sbi, F2FS_DIRTY_NODES) + pages_per_sec - 1) >> sbi.log_blocks_per_seg) /
       sbi.segs_per_sec;
@@ -380,7 +386,7 @@
   if (sbi.por_doing)
     return 0;
 
-  if (FreeSections() <= (unsigned int)(node_secs + 2 * dent_secs + ReservedSections()))
+  if (FreeSections() <= static_cast<uint32_t>(node_secs + 2 * dent_secs + ReservedSections()))
     return 1;
   return 0;
 }
@@ -391,7 +397,7 @@
  */
 void SegMgr::F2fsBalanceFs() {
   f2fs_sb_info &sbi = fs_->SbInfo();
-  struct WritebackControl wbc = {
+  WritebackControl wbc = {
 #if 0  // porting needed
       // .nr_to_write = LONG_MAX,
       // .sync_mode = WB_SYNC_ALL,
@@ -421,39 +427,39 @@
   }
 }
 
-void SegMgr::__LocateDirtySegment(unsigned int segno, enum dirty_type dirty_type) {
+void SegMgr::__LocateDirtySegment(uint32_t segno, DirtyType dirty_type) {
   f2fs_sb_info &sbi = fs_->SbInfo();
-  struct dirty_seglist_info *dirty_i = DIRTY_I(&sbi);
+  dirty_seglist_info *dirty_i = DIRTY_I(&sbi);
 
   /* need not be added */
-  if (IS_CURSEG(&sbi, segno))
+  if (IsCurSeg(&sbi, segno))
     return;
 
-  if (!test_and_set_bit(segno, dirty_i->dirty_segmap[dirty_type]))
-    dirty_i->nr_dirty[dirty_type]++;
+  if (!test_and_set_bit(segno, dirty_i->dirty_segmap[static_cast<int>(dirty_type)]))
+    dirty_i->nr_dirty[static_cast<int>(dirty_type)]++;
 
-  if (dirty_type == DIRTY) {
-    struct seg_entry *sentry = GetSegEntry(segno);
-    dirty_type = static_cast<enum dirty_type>(sentry->type);
-    if (!test_and_set_bit(segno, dirty_i->dirty_segmap[dirty_type]))
-      dirty_i->nr_dirty[dirty_type]++;
+  if (dirty_type == DirtyType::kDirty) {
+    seg_entry *sentry = GetSegEntry(segno);
+    dirty_type = static_cast<DirtyType>(sentry->type);
+    if (!test_and_set_bit(segno, dirty_i->dirty_segmap[static_cast<int>(dirty_type)]))
+      dirty_i->nr_dirty[static_cast<int>(dirty_type)]++;
   }
 }
 
-void SegMgr::__RemoveDirtySegment(unsigned int segno, enum dirty_type dirty_type) {
+void SegMgr::__RemoveDirtySegment(uint32_t segno, DirtyType dirty_type) {
   f2fs_sb_info &sbi = fs_->SbInfo();
-  struct dirty_seglist_info *dirty_i = DIRTY_I(&sbi);
+  dirty_seglist_info *dirty_i = DIRTY_I(&sbi);
 
-  if (test_and_clear_bit(segno, dirty_i->dirty_segmap[dirty_type]))
-    dirty_i->nr_dirty[dirty_type]--;
+  if (test_and_clear_bit(segno, dirty_i->dirty_segmap[static_cast<int>(dirty_type)]))
+    dirty_i->nr_dirty[static_cast<int>(dirty_type)]--;
 
-  if (dirty_type == DIRTY) {
-    struct seg_entry *sentry = GetSegEntry(segno);
-    dirty_type = static_cast<enum dirty_type>(sentry->type);
-    if (test_and_clear_bit(segno, dirty_i->dirty_segmap[dirty_type]))
-      dirty_i->nr_dirty[dirty_type]--;
-    clear_bit(segno, dirty_i->victim_segmap[FG_GC]);
-    clear_bit(segno, dirty_i->victim_segmap[BG_GC]);
+  if (dirty_type == DirtyType::kDirty) {
+    seg_entry *sentry = GetSegEntry(segno);
+    dirty_type = static_cast<DirtyType>(sentry->type);
+    if (test_and_clear_bit(segno, dirty_i->dirty_segmap[static_cast<int>(dirty_type)]))
+      dirty_i->nr_dirty[static_cast<int>(dirty_type)]--;
+    clear_bit(segno, dirty_i->victim_segmap[static_cast<int>(GcType::kFgGc)]);
+    clear_bit(segno, dirty_i->victim_segmap[static_cast<int>(GcType::kBgGc)]);
   }
 }
 
@@ -462,12 +468,12 @@
  * Adding dirty entry into seglist is not critical operation.
  * If a given segment is one of current working segments, it won't be added.
  */
-void SegMgr::LocateDirtySegment(unsigned int segno) {
+void SegMgr::LocateDirtySegment(uint32_t segno) {
   f2fs_sb_info &sbi = fs_->SbInfo();
-  struct dirty_seglist_info *dirty_i = DIRTY_I(&sbi);
-  unsigned short valid_blocks;
+  dirty_seglist_info *dirty_i = DIRTY_I(&sbi);
+  uint16_t valid_blocks;
 
-  if (segno == NULL_SEGNO || IS_CURSEG(&sbi, segno))
+  if (segno == kNullSegNo || IsCurSeg(&sbi, segno))
     return;
 
   mtx_lock(&dirty_i->seglist_lock);
@@ -475,17 +481,16 @@
   valid_blocks = GetValidBlocks(segno, 0);
 
   if (valid_blocks == 0) {
-    __LocateDirtySegment(segno, PRE);
-    __RemoveDirtySegment(segno, DIRTY);
+    __LocateDirtySegment(segno, DirtyType::kPre);
+    __RemoveDirtySegment(segno, DirtyType::kDirty);
   } else if (valid_blocks < sbi.blocks_per_seg) {
-    __LocateDirtySegment(segno, DIRTY);
+    __LocateDirtySegment(segno, DirtyType::kDirty);
   } else {
     /* Recovery routine with SSR needs this */
-    __RemoveDirtySegment(segno, DIRTY);
+    __RemoveDirtySegment(segno, DirtyType::kDirty);
   }
 
   mtx_unlock(&dirty_i->seglist_lock);
-  return;
 }
 
 /**
@@ -493,13 +498,13 @@
  */
 void SegMgr::SetPrefreeAsFreeSegments() {
   f2fs_sb_info &sbi = fs_->SbInfo();
-  struct dirty_seglist_info *dirty_i = DIRTY_I(&sbi);
-  unsigned int segno, offset = 0;
-  unsigned int total_segs = TOTAL_SEGS(&sbi);
+  dirty_seglist_info *dirty_i = DIRTY_I(&sbi);
+  uint32_t segno, offset = 0;
+  uint32_t total_segs = TotalSegs(&sbi);
 
   mtx_lock(&dirty_i->seglist_lock);
   while (true) {
-    segno = find_next_bit_le(dirty_i->dirty_segmap[PRE], total_segs, offset);
+    segno = find_next_bit_le(dirty_i->dirty_segmap[static_cast<int>(DirtyType::kPre)], total_segs, offset);
     if (segno >= total_segs)
       break;
     __SetTestAndFree(segno);
@@ -510,25 +515,25 @@
 
 void SegMgr::ClearPrefreeSegments() {
   f2fs_sb_info &sbi = fs_->SbInfo();
-  struct dirty_seglist_info *dirty_i = DIRTY_I(&sbi);
-  unsigned int segno, offset = 0;
-  unsigned int total_segs = TOTAL_SEGS(&sbi);
+  dirty_seglist_info *dirty_i = DIRTY_I(&sbi);
+  uint32_t segno, offset = 0;
+  uint32_t total_segs = TotalSegs(&sbi);
 
   mtx_lock(&dirty_i->seglist_lock);
   while (true) {
-    segno = find_next_bit_le(dirty_i->dirty_segmap[PRE], total_segs, offset);
+    segno = find_next_bit_le(dirty_i->dirty_segmap[static_cast<int>(DirtyType::kPre)], total_segs, offset);
     if (segno >= total_segs)
       break;
 
     offset = segno + 1;
-    if (test_and_clear_bit(segno, dirty_i->dirty_segmap[PRE]))
-      dirty_i->nr_dirty[PRE]--;
+    if (test_and_clear_bit(segno, dirty_i->dirty_segmap[static_cast<int>(DirtyType::kPre)]))
+      dirty_i->nr_dirty[static_cast<int>(DirtyType::kPre)]--;
 
 #if 0  // porting needed (Trim)
     /* Let's use trim */
     // if (test_opt(sbi, DISCARD))
     //	blkdev_issue_discard(sbi->sb->s_bdev,
-    //			START_BLOCK(sbi, segno) <<
+    //			StartBlock(sbi, segno) <<
     //			sbi->log_sectors_per_block,
     //			1 << (sbi->log_sectors_per_block +
     //				sbi->log_blocks_per_seg),
@@ -538,15 +543,15 @@
   mtx_unlock(&dirty_i->seglist_lock);
 }
 
-void SegMgr::__MarkSitEntryDirty(unsigned int segno) {
+void SegMgr::__MarkSitEntryDirty(uint32_t segno) {
   f2fs_sb_info &sbi = fs_->SbInfo();
-  struct sit_info *sit_i = SIT_I(&sbi);
+  sit_info *sit_i = SIT_I(&sbi);
   if (!test_and_set_bit_le(segno, sit_i->dirty_sentries_bitmap))
     sit_i->dirty_sentries++;
 }
 
-void SegMgr::__SetSitEntryType(int type, unsigned int segno, int modified) {
-  struct seg_entry *se = GetSegEntry(segno);
+void SegMgr::__SetSitEntryType(int type, uint32_t segno, int modified) {
+  seg_entry *se = GetSegEntry(segno);
   se->type = type;
   if (modified)
     __MarkSitEntryDirty(segno);
@@ -554,17 +559,17 @@
 
 void SegMgr::UpdateSitEntry(block_t blkaddr, int del) {
   f2fs_sb_info &sbi = fs_->SbInfo();
-  struct seg_entry *se;
-  unsigned int segno, offset;
-  long int new_vblocks;
+  seg_entry *se;
+  uint32_t segno, offset;
+  int64_t new_vblocks;
 
-  segno = GET_SEGNO(&sbi, blkaddr);
+  segno = GetSegNo(&sbi, blkaddr);
 
   se = GetSegEntry(segno);
   new_vblocks = se->valid_blocks + del;
-  offset = GET_SEGOFF_FROM_SEG0(&sbi, blkaddr) & (sbi.blocks_per_seg - 1);
+  offset = GetSegOffFromSeg0(&sbi, blkaddr) & (sbi.blocks_per_seg - 1);
 
-  ZX_ASSERT(!((new_vblocks >> (sizeof(unsigned short) << 3) || (new_vblocks > sbi.blocks_per_seg))));
+  ZX_ASSERT(!((new_vblocks >> (sizeof(uint16_t) << 3) || (new_vblocks > sbi.blocks_per_seg))));
 
   se->valid_blocks = new_vblocks;
   se->mtime = GetMtime();
@@ -593,14 +598,14 @@
 void SegMgr::RefreshSitEntry(block_t old_blkaddr, block_t new_blkaddr) {
   f2fs_sb_info &sbi = fs_->SbInfo();
   UpdateSitEntry(new_blkaddr, 1);
-  if (GET_SEGNO(&sbi, old_blkaddr) != NULL_SEGNO)
+  if (GetSegNo(&sbi, old_blkaddr) != kNullSegNo)
     UpdateSitEntry(old_blkaddr, -1);
 }
 
 void SegMgr::InvalidateBlocks(block_t addr) {
   f2fs_sb_info &sbi = fs_->SbInfo();
-  unsigned int segno = GET_SEGNO(&sbi, addr);
-  struct sit_info *sit_i = SIT_I(&sbi);
+  uint32_t segno = GetSegNo(&sbi, addr);
+  sit_info *sit_i = SIT_I(&sbi);
 
   ZX_ASSERT(addr != NULL_ADDR);
   if (addr == NEW_ADDR)
@@ -620,12 +625,12 @@
 /**
  * This function should be resided under the curseg_mutex lock
  */
-void SegMgr::__AddSumEntry(int type, struct f2fs_summary *sum, unsigned short offset) {
+void SegMgr::__AddSumEntry(int type, f2fs_summary *sum, uint16_t offset) {
   f2fs_sb_info &sbi = fs_->SbInfo();
-  struct curseg_info *curseg = CURSEG_I(&sbi, type);
-  char *addr = (char *)curseg->sum_blk;
-  (addr) += offset * sizeof(struct f2fs_summary);
-  memcpy(addr, sum, sizeof(struct f2fs_summary));
+  curseg_info *curseg = CURSEG_I(&sbi, type);
+  char *addr = reinterpret_cast<char *>(curseg->sum_blk);
+  (addr) += offset * sizeof(f2fs_summary);
+  memcpy(addr, sum, sizeof(f2fs_summary));
 }
 
 /**
@@ -638,31 +643,33 @@
   int i, sum_space;
 
   for (i = CURSEG_HOT_DATA; i <= CURSEG_COLD_DATA; i++) {
-    if (sbi.ckpt->alloc_type[i] == SSR)
+    if (sbi.ckpt->alloc_type[i] == static_cast<uint8_t>(AllocMode::kSSR)) {
       valid_sum_count += sbi.blocks_per_seg;
-    else
+    } else {
       valid_sum_count += CursegBlkoff(i);
+    }
   }
 
-  total_size_bytes = valid_sum_count * (SUMMARY_SIZE + 1) + sizeof(struct nat_journal) + 2 +
-                     sizeof(struct sit_journal) + 2;
+  total_size_bytes = valid_sum_count * (SUMMARY_SIZE + 1) + sizeof(nat_journal) + 2 +
+                     sizeof(sit_journal) + 2;
   sum_space = kPageCacheSize - SUM_FOOTER_SIZE;
-  if (total_size_bytes < sum_space)
+  if (total_size_bytes < sum_space) {
     return 1;
-  else if (total_size_bytes < 2 * sum_space)
+  } else if (total_size_bytes < 2 * sum_space) {
     return 2;
+  }
   return 3;
 }
 
 /**
  * Caller should put this summary page
  */
-Page *SegMgr::GetSumPage(unsigned int segno) {
+Page *SegMgr::GetSumPage(uint32_t segno) {
   f2fs_sb_info &sbi = fs_->SbInfo();
-  return fs_->GetMetaPage(GET_SUM_BLOCK(&sbi, segno));
+  return fs_->GetMetaPage(GetSumBlock(&sbi, segno));
 }
 
-void SegMgr::WriteSumPage(struct f2fs_summary_block *sum_blk, block_t blk_addr) {
+void SegMgr::WriteSumPage(f2fs_summary_block *sum_blk, block_t blk_addr) {
   Page *page = fs_->GrabMetaPage(blk_addr);
   void *kaddr = PageAddress(page);
   memcpy(kaddr, sum_blk, kPageCacheSize);
@@ -673,11 +680,11 @@
   F2fsPutPage(page, 1);
 }
 
-unsigned int SegMgr::CheckPrefreeSegments(int ofs_unit, int type) {
+uint32_t SegMgr::CheckPrefreeSegments(int ofs_unit, int type) {
   f2fs_sb_info &sbi = fs_->SbInfo();
-  struct dirty_seglist_info *dirty_i = DIRTY_I(&sbi);
-  unsigned long *prefree_segmap = dirty_i->dirty_segmap[PRE];
-  unsigned int segno, next_segno, i;
+  dirty_seglist_info *dirty_i = DIRTY_I(&sbi);
+  uint64_t *prefree_segmap = dirty_i->dirty_segmap[static_cast<int>(DirtyType::kPre)];
+  uint32_t segno, next_segno, i;
   int ofs = 0;
 
   /*
@@ -685,49 +692,50 @@
    * we should not reuse prefree segments.
    */
   if (HasNotEnoughFreeSecs())
-    return NULL_SEGNO;
+    return kNullSegNo;
 
   /*
    * NODE page should not reuse prefree segment,
    * since those information is used for SPOR.
    */
-  if (IS_NODESEG(type))
-    return NULL_SEGNO;
+  if (IsNodeSeg(type))
+    return kNullSegNo;
 next:
-  segno = find_next_bit_le(prefree_segmap, TOTAL_SEGS(&sbi), ofs++);
+  segno = find_next_bit_le(prefree_segmap, TotalSegs(&sbi), ofs++);
   ofs = ((segno / ofs_unit) * ofs_unit) + ofs_unit;
-  if (segno < TOTAL_SEGS(&sbi)) {
+  if (segno < TotalSegs(&sbi)) {
     /* skip intermediate segments in a section */
     if (segno % ofs_unit)
       goto next;
 
     /* skip if whole section is not prefree */
-    next_segno = find_next_zero_bit(prefree_segmap, TOTAL_SEGS(&sbi), segno + 1);
-    if (next_segno - segno < static_cast<unsigned int>(ofs_unit))
+    next_segno = find_next_zero_bit(prefree_segmap, TotalSegs(&sbi), segno + 1);
+    if (next_segno - segno < static_cast<uint32_t>(ofs_unit))
       goto next;
 
     /* skip if whole section was not free at the last checkpoint */
-    for (i = 0; i < static_cast<unsigned int>(ofs_unit); i++)
+    for (i = 0; i < static_cast<uint32_t>(ofs_unit); i++) {
       if (GetSegEntry(segno)->ckpt_valid_blocks)
         goto next;
+    }
     return segno;
   }
-  return NULL_SEGNO;
+  return kNullSegNo;
 }
 
 /**
  * Find a new segment from the free segments bitmap to right order
  * This function should be returned with success, otherwise BUG
  */
-void SegMgr::GetNewSegment(unsigned int *newseg, bool new_sec, int dir) {
+void SegMgr::GetNewSegment(uint32_t *newseg, bool new_sec, int dir) {
   f2fs_sb_info &sbi = fs_->SbInfo();
-  struct free_segmap_info *free_i = FREE_I(&sbi);
-  unsigned int total_secs = sbi.total_sections;
-  unsigned int segno, secno, zoneno;
-  unsigned int total_zones = sbi.total_sections / sbi.secs_per_zone;
-  unsigned int hint = *newseg / sbi.segs_per_sec;
-  unsigned int old_zoneno = GET_ZONENO_FROM_SEGNO(&sbi, *newseg);
-  unsigned int left_start = hint;
+  free_segmap_info *free_i = FREE_I(&sbi);
+  uint32_t total_secs = sbi.total_sections;
+  uint32_t segno, secno, zoneno;
+  uint32_t total_zones = sbi.total_sections / sbi.secs_per_zone;
+  uint32_t hint = *newseg / sbi.segs_per_sec;
+  uint32_t old_zoneno = GetZoneNoFromSegNo(&sbi, *newseg);
+  uint32_t left_start = hint;
   bool init = true;
   int go_left = 0;
   int i;
@@ -735,14 +743,14 @@
   WriteLock(&free_i->segmap_lock);
 
   if (!new_sec && ((*newseg + 1) % sbi.segs_per_sec)) {
-    segno = find_next_zero_bit(free_i->free_segmap, TOTAL_SEGS(&sbi), *newseg + 1);
-    if (segno < TOTAL_SEGS(&sbi))
+    segno = find_next_zero_bit(free_i->free_segmap, TotalSegs(&sbi), *newseg + 1);
+    if (segno < TotalSegs(&sbi))
       goto got_it;
   }
 find_other_zone:
   secno = find_next_zero_bit(free_i->free_secmap, total_secs, hint);
   if (secno >= total_secs) {
-    if (dir == ALLOC_RIGHT) {
+    if (dir == static_cast<int>(AllocDirection::kAllocRight)) {
       secno = find_next_zero_bit(free_i->free_secmap, total_secs, 0);
       ZX_ASSERT(!(secno >= total_secs));
     } else {
@@ -775,24 +783,26 @@
     goto got_it;
   if (zoneno == old_zoneno)
     goto got_it;
-  if (dir == ALLOC_LEFT) {
+  if (dir == static_cast<int>(AllocDirection::kAllocLeft)) {
     if (!go_left && zoneno + 1 >= total_zones)
       goto got_it;
     if (go_left && zoneno == 0)
       goto got_it;
   }
-  for (i = 0; i < NR_CURSEG_TYPE; i++)
+  for (i = 0; i < NR_CURSEG_TYPE; i++) {
     if (CURSEG_I(&sbi, i)->zone == zoneno)
       break;
+    }
 
   if (i < NR_CURSEG_TYPE) {
     /* zone is in user, try another */
-    if (go_left)
+    if (go_left) {
       hint = zoneno * sbi.secs_per_zone - 1;
-    else if (zoneno + 1 >= total_zones)
+    } else if (zoneno + 1 >= total_zones) {
       hint = 0;
-    else
+    } else {
       hint = (zoneno + 1) * sbi.secs_per_zone;
+    }
     init = false;
     goto find_other_zone;
   }
@@ -806,19 +816,19 @@
 
 void SegMgr::ResetCurseg(int type, int modified) {
   f2fs_sb_info &sbi = fs_->SbInfo();
-  struct curseg_info *curseg = CURSEG_I(&sbi, type);
-  struct summary_footer *sum_footer;
+  curseg_info *curseg = CURSEG_I(&sbi, type);
+  summary_footer *sum_footer;
 
   curseg->segno = curseg->next_segno;
-  curseg->zone = GET_ZONENO_FROM_SEGNO(&sbi, curseg->segno);
+  curseg->zone = GetZoneNoFromSegNo(&sbi, curseg->segno);
   curseg->next_blkoff = 0;
-  curseg->next_segno = NULL_SEGNO;
+  curseg->next_segno = kNullSegNo;
 
   sum_footer = &(curseg->sum_blk->footer);
-  memset(sum_footer, 0, sizeof(struct summary_footer));
-  if (IS_DATASEG(type))
+  memset(sum_footer, 0, sizeof(summary_footer));
+  if (IsDataSeg(type))
     SET_SUM_TYPE(sum_footer, SUM_TYPE_DATA);
-  if (IS_NODESEG(type))
+  if (IsNodeSeg(type))
     SET_SUM_TYPE(sum_footer, SUM_TYPE_NODE);
   __SetSitEntryType(type, curseg->segno, modified);
 }
@@ -829,30 +839,30 @@
  */
 void SegMgr::NewCurseg(int type, bool new_sec) {
   f2fs_sb_info &sbi = fs_->SbInfo();
-  struct curseg_info *curseg = CURSEG_I(&sbi, type);
-  unsigned int segno = curseg->segno;
-  int dir = ALLOC_LEFT;
+  curseg_info *curseg = CURSEG_I(&sbi, type);
+  uint32_t segno = curseg->segno;
+  int dir = static_cast<int>(AllocDirection::kAllocLeft);
 
-  WriteSumPage(curseg->sum_blk, GET_SUM_BLOCK(&sbi, curseg->segno));
+  WriteSumPage(curseg->sum_blk, GetSumBlock(&sbi, curseg->segno));
   if (type == CURSEG_WARM_DATA || type == CURSEG_COLD_DATA)
-    dir = ALLOC_RIGHT;
+    dir = static_cast<int>(AllocDirection::kAllocRight);
 
   if (test_opt(&sbi, NOHEAP))
-    dir = ALLOC_RIGHT;
+    dir = static_cast<int>(AllocDirection::kAllocRight);
 
   GetNewSegment(&segno, new_sec, dir);
   curseg->next_segno = segno;
   ResetCurseg(type, 1);
-  curseg->alloc_type = LFS;
+  curseg->alloc_type = static_cast<uint8_t>(AllocMode::kLFS);
 }
 
-void SegMgr::__NextFreeBlkoff(struct curseg_info *seg, block_t start) {
+void SegMgr::__NextFreeBlkoff(curseg_info *seg, block_t start) {
   f2fs_sb_info &sbi = fs_->SbInfo();
-  struct seg_entry *se = GetSegEntry(seg->segno);
+  seg_entry *se = GetSegEntry(seg->segno);
   block_t ofs;
   for (ofs = start; ofs < sbi.blocks_per_seg; ofs++) {
-    if (!f2fs_test_bit(ofs, (char *)se->ckpt_valid_map) &&
-        !f2fs_test_bit(ofs, (char *)se->cur_valid_map))
+    if (!f2fs_test_bit(ofs, reinterpret_cast<char *>(se->ckpt_valid_map)) &&
+        !f2fs_test_bit(ofs, reinterpret_cast<char *>(se->cur_valid_map)))
       break;
   }
   seg->next_blkoff = ofs;
@@ -863,8 +873,8 @@
  * by increasing the current block offset. However, if a segment is written by
  * SSR manner, next block offset obtained by calling __next_free_blkoff
  */
-void SegMgr::__RefreshNextBlkoff(struct curseg_info *seg) {
-  if (seg->alloc_type == SSR) {
+void SegMgr::__RefreshNextBlkoff(curseg_info *seg) {
+  if (seg->alloc_type == static_cast<uint8_t>(AllocMode::kSSR)) {
     __NextFreeBlkoff(seg, seg->next_blkoff + 1);
   } else {
     seg->next_blkoff++;
@@ -877,27 +887,27 @@
  */
 void SegMgr::ChangeCurseg(int type, bool reuse) {
   f2fs_sb_info &sbi = fs_->SbInfo();
-  struct dirty_seglist_info *dirty_i = DIRTY_I(&sbi);
-  struct curseg_info *curseg = CURSEG_I(&sbi, type);
-  unsigned int new_segno = curseg->next_segno;
-  struct f2fs_summary_block *sum_node;
-  struct Page *sum_page;
+  dirty_seglist_info *dirty_i = DIRTY_I(&sbi);
+  curseg_info *curseg = CURSEG_I(&sbi, type);
+  uint32_t new_segno = curseg->next_segno;
+  f2fs_summary_block *sum_node;
+  Page *sum_page;
 
-  WriteSumPage(curseg->sum_blk, GET_SUM_BLOCK(&sbi, curseg->segno));
+  WriteSumPage(curseg->sum_blk, GetSumBlock(&sbi, curseg->segno));
   __SetTestAndInuse(new_segno);
 
   mtx_lock(&dirty_i->seglist_lock);
-  __RemoveDirtySegment(new_segno, PRE);
-  __RemoveDirtySegment(new_segno, DIRTY);
+  __RemoveDirtySegment(new_segno, DirtyType::kPre);
+  __RemoveDirtySegment(new_segno, DirtyType::kDirty);
   mtx_unlock(&dirty_i->seglist_lock);
 
   ResetCurseg(type, 1);
-  curseg->alloc_type = SSR;
+  curseg->alloc_type = static_cast<uint8_t>(AllocMode::kLFS);
   __NextFreeBlkoff(curseg, 0);
 
   if (reuse) {
     sum_page = GetSumPage(new_segno);
-    sum_node = (struct f2fs_summary_block *)PageAddress(sum_page);
+    sum_node = static_cast<f2fs_summary_block *>(PageAddress(sum_page));
     memcpy(curseg->sum_blk, sum_node, SUM_ENTRY_SIZE);
     F2fsPutPage(sum_page, 1);
   }
@@ -909,8 +919,8 @@
  */
 void SegMgr::AllocateSegmentByDefault(int type, bool force) {
   f2fs_sb_info &sbi = fs_->SbInfo();
-  struct curseg_info *curseg = CURSEG_I(&sbi, type);
-  // unsigned int ofs_unit;
+  curseg_info *curseg = CURSEG_I(&sbi, type);
+  // uint32_t ofs_unit;
 
   if (force) {
     NewCurseg(type, true);
@@ -921,7 +931,7 @@
   // ofs_unit = NeedSSR() ? 1 : sbi.segs_per_sec;
   // curseg->next_segno = CheckPrefreeSegments(ofs_unit, type);
 
-  // if (curseg->next_segno != NULL_SEGNO)
+  // if (curseg->next_segno != kNullSegNo)
   // 	ChangeCurseg(type, false);
   // else
 
@@ -945,7 +955,7 @@
             << ", FreeSections()=" << FreeSections()
             << ", PrefreeSegments()=" << PrefreeSegments()
             << ", DirtySegments()=" << DirtySegments()
-            << ", TOTAL_SEGS=" << TOTAL_SEGS(&sbi)
+            << ", TotalSegs=" << TotalSegs(&sbi)
             << ", Utilization()=" << Utilization()
             << std::endl;
 #endif
@@ -953,8 +963,8 @@
 
 void SegMgr::AllocateNewSegments() {
   f2fs_sb_info &sbi = fs_->SbInfo();
-  struct curseg_info *curseg;
-  unsigned int old_curseg;
+  curseg_info *curseg;
+  uint32_t old_curseg;
   int i;
 
   for (i = CURSEG_HOT_DATA; i <= CURSEG_COLD_DATA; i++) {
@@ -967,20 +977,20 @@
 
 #if 0  // porting needed
 /*
-const struct segment_allocation default_salloc_ops = {
+const segment_allocation default_salloc_ops = {
         .allocate_segment = AllocateSegmentByDefault,
 };
 */
 #endif
 
 #if 0  // porting needed (bio)
-void SegMgr::F2fsEndIoWrite(struct bio *bio, int err) {
+void SegMgr::F2fsEndIoWrite(bio *bio, int err) {
   // const int uptodate = test_bit(BIO_UPTODATE, &bio->bi_flags);
-  // struct bio_vec *bvec = bio->bi_io_vec + bio->bi_vcnt - 1;
-  // struct bio_private *p = bio->bi_private;
+  // bio_vec *bvec = bio->bi_io_vec + bio->bi_vcnt - 1;
+  // bio_private *p = bio->bi_private;
 
   // do {
-  // 	struct page *page = bvec->bv_page;
+  // 	page *page = bvec->bv_page;
 
   // 	if (--bvec >= bio->bi_io_vec)
   // 		prefetchw(&bvec->bv_page->flags);
@@ -1003,9 +1013,9 @@
 #endif
 
 #if 0  // porting needed (bio)
-struct bio *SegMgr::F2fsBioAlloc(struct block_device *bdev, sector_t first_sector, int nr_vecs,
+bio *SegMgr::F2fsBioAlloc(block_device *bdev, sector_t first_sector, int nr_vecs,
                                  gfp_t gfp_flags) {
-  // 	struct bio *bio;
+  // 	bio *bio;
   // repeat:
   // 	/* allocate new bio */
   // 	bio = bio_alloc(gfp_flags, nr_vecs);
@@ -1018,7 +1028,7 @@
   // 		bio->bi_bdev = bdev;
   // 		bio->bi_sector = first_sector;
   // retry:
-  // 		bio->bi_private = kmalloc(sizeof(struct bio_private),
+  // 		bio->bi_private = kmalloc(sizeof(bio_private),
   // 						GFP_NOFS | __GFP_HIGH);
   // 		if (!bio->bi_private) {
   // 			cond_resched();
@@ -1043,7 +1053,7 @@
   // 	rw = kWriteFlushFua;
 
   // if (sbi->bio[btype]) {
-  // 	struct bio_private *p = sbi->bio[btype]->bi_private;
+  // 	bio_private *p = sbi->bio[btype]->bi_private;
   // 	p->sbi = sbi;
   // 	sbi->bio[btype]->bi_end_io = f2fs_end_io_write;
   // 	if (type == META_FLUSH) {
@@ -1078,7 +1088,7 @@
 #if 0  // porting needed (bio)
   //	fs_->bc_->Sync();
 
-  // 	struct block_device *bdev = sbi->sb->s_bdev;
+  // 	block_device *bdev = sbi->sb->s_bdev;
 
   // 	verify_block_addr(sbi, blk_addr);
 
@@ -1108,7 +1118,7 @@
 
 bool SegMgr::__HasCursegSpace(int type) {
   f2fs_sb_info &sbi = fs_->SbInfo();
-  struct curseg_info *curseg = CURSEG_I(&sbi, type);
+  curseg_info *curseg = CURSEG_I(&sbi, type);
   if (curseg->next_blkoff < sbi.blocks_per_seg) {
     return true;
   }
@@ -1116,25 +1126,28 @@
 }
 
 int SegMgr::__GetSegmentType2(Page *page, enum page_type p_type) {
-  if (p_type == DATA)
+  if (p_type == DATA) {
     return CURSEG_HOT_DATA;
-  else
+  } else {
     return CURSEG_HOT_NODE;
+  }
 }
 
 int SegMgr::__GetSegmentType4(Page *page, enum page_type p_type) {
   if (p_type == DATA) {
     VnodeF2fs *vnode = static_cast<f2fs::VnodeF2fs *>(page->host);
 
-    if (S_ISDIR(vnode->i_mode_))
+    if (S_ISDIR(vnode->i_mode_)) {
       return CURSEG_HOT_DATA;
-    else
+    } else {
       return CURSEG_COLD_DATA;
+    }
   } else {
-    if (fs_->Nodemgr().IS_DNODE(page) && !NodeMgr::IsColdNode(page))
+    if (fs_->Nodemgr().IS_DNODE(page) && !NodeMgr::IsColdNode(page)) {
       return CURSEG_HOT_NODE;
-    else
+    } else {
       return CURSEG_COLD_NODE;
+    }
     return 0;
   }
   return 0;
@@ -1144,17 +1157,19 @@
   if (p_type == DATA) {
     VnodeF2fs *vnode = static_cast<f2fs::VnodeF2fs *>(page->host);
 
-    if (S_ISDIR(vnode->i_mode_))
+    if (S_ISDIR(vnode->i_mode_)) {
       return CURSEG_HOT_DATA;
-    else if (NodeMgr::IsColdData(page) || NodeMgr::IsColdFile(vnode))
+    } else if (NodeMgr::IsColdData(page) || NodeMgr::IsColdFile(vnode)) {
       return CURSEG_COLD_DATA;
-    else
+    } else {
       return CURSEG_WARM_DATA;
+    }
   } else {
-    if (fs_->Nodemgr().IS_DNODE(page))
+    if (fs_->Nodemgr().IS_DNODE(page)) {
       return NodeMgr::IsColdNode(page) ? CURSEG_WARM_NODE : CURSEG_HOT_NODE;
-    else
+    } else {
       return CURSEG_COLD_NODE;
+    }
   }
   return 0;
 }
@@ -1174,18 +1189,18 @@
 }
 
 void SegMgr::DoWritePage(Page *page, block_t old_blkaddr, block_t *new_blkaddr,
-                         struct f2fs_summary *sum, enum page_type p_type) {
+                         f2fs_summary *sum, enum page_type p_type) {
   f2fs_sb_info &sbi = fs_->SbInfo();
-  struct sit_info *sit_i = SIT_I(&sbi);
-  struct curseg_info *curseg;
-  unsigned int old_cursegno;
+  sit_info *sit_i = SIT_I(&sbi);
+  curseg_info *curseg;
+  uint32_t old_cursegno;
   int type;
 
   type = __GetSegmentType(page, p_type);
   curseg = CURSEG_I(&sbi, type);
 
   mtx_lock(&curseg->curseg_mutex);
-  *new_blkaddr = NEXT_FREE_BLKADDR(&sbi, curseg);
+  *new_blkaddr = NextFreeBlkAddr(&sbi, curseg);
   old_cursegno = curseg->segno;
 
   /*
@@ -1213,11 +1228,11 @@
   }
 
   LocateDirtySegment(old_cursegno);
-  LocateDirtySegment(GET_SEGNO(&sbi, old_blkaddr));
+  LocateDirtySegment(GetSegNo(&sbi, old_blkaddr));
   mtx_unlock(&sit_i->sentry_lock);
 
   if (p_type == NODE)
-    fs_->Nodemgr().FillNodeFooterBlkaddr(page, NEXT_FREE_BLKADDR(&sbi, curseg));
+    fs_->Nodemgr().FillNodeFooterBlkaddr(page, NextFreeBlkAddr(&sbi, curseg));
 
   /* writeout dirty page into bdev */
   SubmitWritePage(page, *new_blkaddr, p_type);
@@ -1225,7 +1240,7 @@
   mtx_unlock(&curseg->curseg_mutex);
 }
 
-zx_status_t SegMgr::WriteMetaPage(Page *page, struct WritebackControl *wbc) {
+zx_status_t SegMgr::WriteMetaPage(Page *page, WritebackControl *wbc) {
 #if 0  // porting needed
   // if (wbc && wbc->for_reclaim)
   // 	return kAopWritepageActivate;
@@ -1235,17 +1250,17 @@
   return ZX_OK;
 }
 
-void SegMgr::WriteNodePage(Page *page, unsigned int nid, block_t old_blkaddr,
+void SegMgr::WriteNodePage(Page *page, uint32_t nid, block_t old_blkaddr,
                            block_t *new_blkaddr) {
-  struct f2fs_summary sum;
+  f2fs_summary sum;
   SetSummary(&sum, nid, 0, 0);
   DoWritePage(page, old_blkaddr, new_blkaddr, &sum, NODE);
 }
 
-void SegMgr::WriteDataPage(VnodeF2fs *vnode, Page *page, struct dnode_of_data *dn,
+void SegMgr::WriteDataPage(VnodeF2fs *vnode, Page *page, dnode_of_data *dn,
                            block_t old_blkaddr, block_t *new_blkaddr) {
-  struct f2fs_summary sum;
-  struct node_info ni;
+  f2fs_summary sum;
+  node_info ni;
 
   ZX_ASSERT(old_blkaddr != NULL_ADDR);
   fs_->Nodemgr().GetNodeInfo(dn->nid, &ni);
@@ -1258,20 +1273,20 @@
   SubmitWritePage(page, old_blk_addr, DATA);
 }
 
-void SegMgr::RecoverDataPage(Page *page, struct f2fs_summary *sum, block_t old_blkaddr,
+void SegMgr::RecoverDataPage(Page *page, f2fs_summary *sum, block_t old_blkaddr,
                              block_t new_blkaddr) {
   f2fs_sb_info &sbi = fs_->SbInfo();
-  struct sit_info *sit_i = SIT_I(&sbi);
-  struct curseg_info *curseg;
-  unsigned int segno, old_cursegno;
-  struct seg_entry *se;
+  sit_info *sit_i = SIT_I(&sbi);
+  curseg_info *curseg;
+  uint32_t segno, old_cursegno;
+  seg_entry *se;
   int type;
 
-  segno = GET_SEGNO(&sbi, new_blkaddr);
+  segno = GetSegNo(&sbi, new_blkaddr);
   se = GetSegEntry(segno);
   type = se->type;
 
-  if (se->valid_blocks == 0 && !IS_CURSEG(&sbi, segno)) {
+  if (se->valid_blocks == 0 && !IsCurSeg(&sbi, segno)) {
     if (old_blkaddr == NULL_ADDR) {
       type = CURSEG_COLD_DATA;
     } else {
@@ -1291,34 +1306,34 @@
     ChangeCurseg(type, true);
   }
 
-  curseg->next_blkoff = GET_SEGOFF_FROM_SEG0(&sbi, new_blkaddr) & (sbi.blocks_per_seg - 1);
+  curseg->next_blkoff = GetSegOffFromSeg0(&sbi, new_blkaddr) & (sbi.blocks_per_seg - 1);
   __AddSumEntry(type, sum, curseg->next_blkoff);
 
   RefreshSitEntry(old_blkaddr, new_blkaddr);
 
   LocateDirtySegment(old_cursegno);
-  LocateDirtySegment(GET_SEGNO(&sbi, old_blkaddr));
+  LocateDirtySegment(GetSegNo(&sbi, old_blkaddr));
 
   mtx_unlock(&sit_i->sentry_lock);
   mtx_unlock(&curseg->curseg_mutex);
 }
 
-void SegMgr::RewriteNodePage(Page *page, struct f2fs_summary *sum, block_t old_blkaddr,
+void SegMgr::RewriteNodePage(Page *page, f2fs_summary *sum, block_t old_blkaddr,
                              block_t new_blkaddr) {
   f2fs_sb_info &sbi = fs_->SbInfo();
-  struct sit_info *sit_i = SIT_I(&sbi);
+  sit_info *sit_i = SIT_I(&sbi);
   int type = CURSEG_WARM_NODE;
-  struct curseg_info *curseg;
-  unsigned int segno, old_cursegno;
+  curseg_info *curseg;
+  uint32_t segno, old_cursegno;
   block_t next_blkaddr = NodeMgr::NextBlkaddrOfNode(page);
-  unsigned int next_segno = GET_SEGNO(&sbi, next_blkaddr);
+  uint32_t next_segno = GetSegNo(&sbi, next_blkaddr);
 
   curseg = CURSEG_I(&sbi, type);
 
   mtx_lock(&curseg->curseg_mutex);
   mtx_lock(&sit_i->sentry_lock);
 
-  segno = GET_SEGNO(&sbi, new_blkaddr);
+  segno = GetSegNo(&sbi, new_blkaddr);
   old_cursegno = curseg->segno;
 
   /* change the current segment */
@@ -1326,7 +1341,7 @@
     curseg->next_segno = segno;
     ChangeCurseg(type, true);
   }
-  curseg->next_blkoff = GET_SEGOFF_FROM_SEG0(&sbi, new_blkaddr) & (sbi.blocks_per_seg - 1);
+  curseg->next_blkoff = GetSegOffFromSeg0(&sbi, new_blkaddr) & (sbi.blocks_per_seg - 1);
   __AddSumEntry(type, sum, curseg->next_blkoff);
 
   /* change the current log to the next block addr in advance */
@@ -1334,7 +1349,7 @@
     curseg->next_segno = next_segno;
     ChangeCurseg(type, true);
   }
-  curseg->next_blkoff = GET_SEGOFF_FROM_SEG0(&sbi, next_blkaddr) & (sbi.blocks_per_seg - 1);
+  curseg->next_blkoff = GetSegOffFromSeg0(&sbi, next_blkaddr) & (sbi.blocks_per_seg - 1);
 
   /* rewrite node page */
   SetPageWriteback(page);
@@ -1345,7 +1360,7 @@
   RefreshSitEntry(old_blkaddr, new_blkaddr);
 
   LocateDirtySegment(old_cursegno);
-  LocateDirtySegment(GET_SEGNO(&sbi, old_blkaddr));
+  LocateDirtySegment(GetSegNo(&sbi, old_blkaddr));
 
   mtx_unlock(&sit_i->sentry_lock);
   mtx_unlock(&curseg->curseg_mutex);
@@ -1353,9 +1368,9 @@
 
 int SegMgr::ReadCompactedSummaries() {
   f2fs_sb_info &sbi = fs_->SbInfo();
-  struct f2fs_checkpoint *ckpt = F2FS_CKPT(&sbi);
-  struct curseg_info *seg_i;
-  unsigned char *kaddr;
+  f2fs_checkpoint *ckpt = F2FS_CKPT(&sbi);
+  curseg_info *seg_i;
+  uint8_t *kaddr;
   Page *page;
   block_t start;
   int i, j, offset;
@@ -1363,7 +1378,7 @@
   start = StartSumBlock();
 
   page = fs_->GetMetaPage(start++);
-  kaddr = static_cast<unsigned char *>(PageAddress(page));
+  kaddr = static_cast<uint8_t *>(PageAddress(page));
 
   /* Step 1: restore nat cache */
   seg_i = CURSEG_I(&sbi, CURSEG_HOT_DATA);
@@ -1376,8 +1391,8 @@
 
   /* Step 3: restore summary entries */
   for (i = CURSEG_HOT_DATA; i <= CURSEG_COLD_DATA; i++) {
-    unsigned short blk_off;
-    unsigned int segno;
+    uint16_t blk_off;
+    uint32_t segno;
 
     seg_i = CURSEG_I(&sbi, i);
     segno = LeToCpu(ckpt->cur_data_segno[i]);
@@ -1387,12 +1402,12 @@
     seg_i->alloc_type = ckpt->alloc_type[i];
     seg_i->next_blkoff = blk_off;
 
-    if (seg_i->alloc_type == SSR)
+    if (seg_i->alloc_type == static_cast<uint8_t>(AllocMode::kLFS))
       blk_off = sbi.blocks_per_seg;
 
     for (j = 0; j < blk_off; j++) {
-      struct f2fs_summary *s;
-      s = reinterpret_cast<struct f2fs_summary *>(kaddr + offset);
+      f2fs_summary *s;
+      s = reinterpret_cast<f2fs_summary *>(kaddr + offset);
       seg_i->sum_blk->entries[j] = *s;
       offset += SUMMARY_SIZE;
       if (offset + SUMMARY_SIZE <= kPageCacheSize - SUM_FOOTER_SIZE)
@@ -1402,7 +1417,7 @@
       page = nullptr;
 
       page = fs_->GetMetaPage(start++);
-      kaddr = static_cast<unsigned char *>(PageAddress(page));
+      kaddr = static_cast<uint8_t *>(PageAddress(page));
       offset = 0;
     }
   }
@@ -1412,16 +1427,16 @@
 
 int SegMgr::ReadNormalSummaries(int type) {
   f2fs_sb_info &sbi = fs_->SbInfo();
-  struct f2fs_checkpoint *ckpt = F2FS_CKPT(&sbi);
-  struct f2fs_summary_block *sum;
-  struct curseg_info *curseg;
+  f2fs_checkpoint *ckpt = F2FS_CKPT(&sbi);
+  f2fs_summary_block *sum;
+  curseg_info *curseg;
   Page *new_page;
-  unsigned short blk_off;
-  unsigned int segno = 0;
+  uint16_t blk_off;
+  uint32_t segno = 0;
   block_t blk_addr = 0;
 
   /* get segment number and block addr */
-  if (IS_DATASEG(type)) {
+  if (IsDataSeg(type)) {
     segno = LeToCpu(ckpt->cur_data_segno[type]);
     blk_off = LeToCpu(ckpt->cur_data_blkoff[type - CURSEG_HOT_DATA]);
     if (ckpt->ckpt_flags & CP_UMOUNT_FLAG) {
@@ -1434,16 +1449,16 @@
     if (ckpt->ckpt_flags & CP_UMOUNT_FLAG) {
       blk_addr = SumBlkAddr(NR_CURSEG_NODE_TYPE, type - CURSEG_HOT_NODE);
     } else
-      blk_addr = GET_SUM_BLOCK(&sbi, segno);
+      blk_addr = GetSumBlock(&sbi, segno);
   }
 
   new_page = fs_->GetMetaPage(blk_addr);
-  sum = (struct f2fs_summary_block *)PageAddress(new_page);
+  sum = static_cast<f2fs_summary_block *>(PageAddress(new_page));
 
-  if (IS_NODESEG(type)) {
+  if (IsNodeSeg(type)) {
     if (ckpt->ckpt_flags & CP_UMOUNT_FLAG) {
-      struct f2fs_summary *ns = &sum->entries[0];
-      unsigned int i;
+      f2fs_summary *ns = &sum->entries[0];
+      uint32_t i;
       for (i = 0; i < sbi.blocks_per_seg; i++, ns++) {
         ns->version = 0;
         ns->ofs_in_node = 0;
@@ -1470,35 +1485,35 @@
   return 0;
 }
 
-int SegMgr::RestoreCursegSummaries() {
+zx_status_t SegMgr::RestoreCursegSummaries() {
   f2fs_sb_info &sbi = fs_->SbInfo();
   int type = CURSEG_HOT_DATA;
 
   if (sbi.ckpt->ckpt_flags & CP_COMPACT_SUM_FLAG) {
     /* restore for compacted data summary */
     if (ReadCompactedSummaries())
-      return -EINVAL;
+      return ZX_ERR_INVALID_ARGS;
     type = CURSEG_HOT_NODE;
   }
 
   for (; type <= CURSEG_COLD_NODE; type++) {
     if (ReadNormalSummaries(type))
-      return -EINVAL;
+      return ZX_ERR_INVALID_ARGS;
   }
-  return 0;
+  return ZX_OK;
 }
 
 void SegMgr::WriteCompactedSummaries(block_t blkaddr) {
   f2fs_sb_info &sbi = fs_->SbInfo();
   Page *page;
-  unsigned char *kaddr;
-  struct f2fs_summary *summary;
-  struct curseg_info *seg_i;
+  uint8_t *kaddr;
+  f2fs_summary *summary;
+  curseg_info *seg_i;
   int written_size = 0;
   int i, j;
 
   page = fs_->GrabMetaPage(blkaddr++);
-  kaddr = (unsigned char *)PageAddress(page);
+  kaddr = static_cast<uint8_t *>(PageAddress(page));
 
   /* Step 1: write nat cache */
   seg_i = CURSEG_I(&sbi, CURSEG_HOT_DATA);
@@ -1515,20 +1530,21 @@
 
   /* Step 3: write summary entries */
   for (i = CURSEG_HOT_DATA; i <= CURSEG_COLD_DATA; i++) {
-    unsigned short blkoff;
+    uint16_t blkoff;
     seg_i = CURSEG_I(&sbi, i);
-    if (sbi.ckpt->alloc_type[i] == SSR)
+    if (sbi.ckpt->alloc_type[i] == static_cast<uint8_t>(AllocMode::kLFS)) {
       blkoff = sbi.blocks_per_seg;
-    else
+    } else {
       blkoff = CursegBlkoff(i);
+    }
 
     for (j = 0; j < blkoff; j++) {
       if (!page) {
         page = fs_->GrabMetaPage(blkaddr++);
-        kaddr = (unsigned char *)PageAddress(page);
+        kaddr = static_cast<uint8_t *>(PageAddress(page));
         written_size = 0;
       }
-      summary = (struct f2fs_summary *)(kaddr + written_size);
+      summary = reinterpret_cast<f2fs_summary *>(kaddr + written_size);
       *summary = seg_i->sum_blk->entries[j];
       written_size += SUMMARY_SIZE;
 #if 0  // porting needed
@@ -1551,13 +1567,14 @@
   f2fs_sb_info &sbi = fs_->SbInfo();
   int i, end;
 
-  if (IS_DATASEG(type))
+  if (IsDataSeg(type)) {
     end = type + NR_CURSEG_DATA_TYPE;
-  else
+  } else {
     end = type + NR_CURSEG_NODE_TYPE;
+  }
 
   for (i = type; i < end; i++) {
-    struct curseg_info *sum = CURSEG_I(&sbi, i);
+    curseg_info *sum = CURSEG_I(&sbi, i);
     mtx_lock(&sum->curseg_mutex);
     WriteSumPage(sum->sum_blk, blkaddr + (i - type));
     mtx_unlock(&sum->curseg_mutex);
@@ -1566,10 +1583,11 @@
 
 void SegMgr::WriteDataSummaries(block_t start_blk) {
   f2fs_sb_info &sbi = fs_->SbInfo();
-  if (sbi.ckpt->ckpt_flags & CP_COMPACT_SUM_FLAG)
+  if (sbi.ckpt->ckpt_flags & CP_COMPACT_SUM_FLAG) {
     WriteCompactedSummaries(start_blk);
-  else
+  } else {
     WriteNormalSummaries(start_blk, CURSEG_HOT_DATA);
+  }
 }
 
 void SegMgr::WriteNodeSummaries(block_t start_blk) {
@@ -1578,7 +1596,7 @@
     WriteNormalSummaries(start_blk, CURSEG_HOT_NODE);
 }
 
-int SegMgr::LookupJournalInCursum(struct f2fs_summary_block *sum, int type, unsigned int val,
+int SegMgr::LookupJournalInCursum(f2fs_summary_block *sum, int type, uint32_t val,
                                   int alloc) {
   int i;
 
@@ -1600,10 +1618,10 @@
   return -1;
 }
 
-Page *SegMgr::GetCurrentSitPage(unsigned int segno) {
+Page *SegMgr::GetCurrentSitPage(uint32_t segno) {
   f2fs_sb_info &sbi = fs_->SbInfo();
-  struct sit_info *sit_i = SIT_I(&sbi);
-  unsigned int offset = SIT_BLOCK_OFFSET(sit_i, segno);
+  sit_info *sit_i = SIT_I(&sbi);
+  uint32_t offset = SitBlockOffset(sit_i, segno);
   block_t blk_addr = sit_i->sit_base_addr + offset;
 
   CheckSegRange(segno);
@@ -1615,9 +1633,9 @@
   return fs_->GetMetaPage(blk_addr);
 }
 
-Page *SegMgr::GetNextSitPage(unsigned int start) {
+Page *SegMgr::GetNextSitPage(uint32_t start) {
   f2fs_sb_info &sbi = fs_->SbInfo();
-  struct sit_info *sit_i = SIT_I(&sbi);
+  sit_info *sit_i = SIT_I(&sbi);
   Page *src_page, *dst_page;
   pgoff_t src_off, dst_off;
   void *src_addr, *dst_addr;
@@ -1646,8 +1664,8 @@
 
 bool SegMgr::FlushSitsInJournal() {
   f2fs_sb_info &sbi = fs_->SbInfo();
-  struct curseg_info *curseg = CURSEG_I(&sbi, CURSEG_COLD_DATA);
-  struct f2fs_summary_block *sum = curseg->sum_blk;
+  curseg_info *curseg = CURSEG_I(&sbi, CURSEG_COLD_DATA);
+  f2fs_summary_block *sum = curseg->sum_blk;
   int i;
 
   /*
@@ -1657,14 +1675,14 @@
    */
   if (sits_in_cursum(sum) >= SIT_JOURNAL_ENTRIES) {
     for (i = sits_in_cursum(sum) - 1; i >= 0; i--) {
-      unsigned int segno;
+      uint32_t segno;
       segno = LeToCpu(segno_in_journal(sum, i));
       __MarkSitEntryDirty(segno);
     }
     update_sits_in_cursum(sum, -sits_in_cursum(sum));
-    return 1;
+    return true;
   }
-  return 0;
+  return false;
 }
 
 /**
@@ -1673,15 +1691,15 @@
  */
 void SegMgr::FlushSitEntries() {
   f2fs_sb_info &sbi = fs_->SbInfo();
-  struct sit_info *sit_i = SIT_I(&sbi);
-  unsigned long *bitmap = sit_i->dirty_sentries_bitmap;
-  struct curseg_info *curseg = CURSEG_I(&sbi, CURSEG_COLD_DATA);
-  struct f2fs_summary_block *sum = curseg->sum_blk;
-  unsigned long nsegs = TOTAL_SEGS(&sbi);
+  sit_info *sit_i = SIT_I(&sbi);
+  uint64_t *bitmap = sit_i->dirty_sentries_bitmap;
+  curseg_info *curseg = CURSEG_I(&sbi, CURSEG_COLD_DATA);
+  f2fs_summary_block *sum = curseg->sum_blk;
+  uint64_t nsegs = TotalSegs(&sbi);
   Page *page = nullptr;
-  struct f2fs_sit_block *raw_sit = nullptr;
-  unsigned int start = 0, end = 0;
-  unsigned int segno = -1;
+  f2fs_sit_block *raw_sit = nullptr;
+  uint32_t start = 0, end = 0;
+  uint32_t segno = -1;
   bool flushed;
 
   mtx_lock(&curseg->curseg_mutex);
@@ -1694,10 +1712,10 @@
   flushed = FlushSitsInJournal();
 
   while ((segno = find_next_bit_le(bitmap, nsegs, segno + 1)) < nsegs) {
-    struct seg_entry *se = GetSegEntry(segno);
+    seg_entry *se = GetSegEntry(segno);
     int sit_offset, offset;
 
-    sit_offset = SIT_ENTRY_OFFSET(sit_i, segno);
+    sit_offset = SitEntryOffset(sit_i, segno);
 
     if (flushed)
       goto to_sit_page;
@@ -1717,12 +1735,12 @@
         page = nullptr;
       }
 
-      start = START_SEGNO(sit_i, segno);
+      start = StartSegNo(sit_i, segno);
       end = start + SIT_ENTRY_PER_BLOCK - 1;
 
       /* read sit block that will be updated */
       page = GetNextSitPage(start);
-      raw_sit = (struct f2fs_sit_block *)PageAddress(page);
+      raw_sit = static_cast<f2fs_sit_block *>(PageAddress(page));
     }
 
     /* udpate entry in SIT block */
@@ -1747,49 +1765,49 @@
 /*
  * Build
  */
-int SegMgr::BuildSitInfo() {
+zx_status_t SegMgr::BuildSitInfo() {
   f2fs_sb_info &sbi = fs_->SbInfo();
-  struct f2fs_super_block *raw_super = F2FS_RAW_SUPER(&sbi);
-  struct f2fs_checkpoint *ckpt = F2FS_CKPT(&sbi);
-  struct sit_info *sit_i;
-  unsigned int sit_segs, start;
+  f2fs_super_block *raw_super = F2FS_RAW_SUPER(&sbi);
+  f2fs_checkpoint *ckpt = F2FS_CKPT(&sbi);
+  sit_info *sit_i;
+  uint32_t sit_segs, start;
   char *src_bitmap, *dst_bitmap;
-  unsigned int bitmap_size;
+  uint32_t bitmap_size;
 
   /* allocate memory for SIT information */
-  sit_i = static_cast<sit_info *>(malloc(sizeof(struct sit_info)));
-  memset(sit_i, 0, sizeof(struct sit_info));
+  sit_i = static_cast<sit_info *>(malloc(sizeof(sit_info)));
+  memset(sit_i, 0, sizeof(sit_info));
   if (!sit_i)
-    return -ENOMEM;
+    return ZX_ERR_NO_MEMORY;
 
   SM_I(&sbi)->sit_info = sit_i;
 
-  sit_i->sentries = static_cast<seg_entry *>(calloc(TOTAL_SEGS(&sbi), sizeof(struct seg_entry)));
+  sit_i->sentries = static_cast<seg_entry *>(calloc(TotalSegs(&sbi), sizeof(seg_entry)));
   if (!sit_i->sentries)
-    return -ENOMEM;
+    return ZX_ERR_NO_MEMORY;
 
-  bitmap_size = f2fs_bitmap_size(TOTAL_SEGS(&sbi));
-  sit_i->dirty_sentries_bitmap = static_cast<unsigned long *>(malloc(bitmap_size));
+  bitmap_size = BitmapSize(TotalSegs(&sbi));
+  sit_i->dirty_sentries_bitmap = static_cast<uint64_t *>(malloc(bitmap_size));
   memset(sit_i->dirty_sentries_bitmap, 0, bitmap_size);
   if (!sit_i->dirty_sentries_bitmap)
-    return -ENOMEM;
+    return ZX_ERR_NO_MEMORY;
 
-  for (start = 0; start < TOTAL_SEGS(&sbi); start++) {
+  for (start = 0; start < TotalSegs(&sbi); start++) {
     sit_i->sentries[start].cur_valid_map =
-        static_cast<unsigned char *>(malloc(SIT_VBLOCK_MAP_SIZE));
+        static_cast<uint8_t *>(malloc(SIT_VBLOCK_MAP_SIZE));
     memset(sit_i->sentries[start].cur_valid_map, 0, SIT_VBLOCK_MAP_SIZE);
     sit_i->sentries[start].ckpt_valid_map =
-        static_cast<unsigned char *>(malloc(SIT_VBLOCK_MAP_SIZE));
+        static_cast<uint8_t *>(malloc(SIT_VBLOCK_MAP_SIZE));
     memset(sit_i->sentries[start].ckpt_valid_map, 0, SIT_VBLOCK_MAP_SIZE);
     if (!sit_i->sentries[start].cur_valid_map || !sit_i->sentries[start].ckpt_valid_map)
-      return -ENOMEM;
+      return ZX_ERR_NO_MEMORY;
   }
 
   if (sbi.segs_per_sec > 1) {
     sit_i->sec_entries =
-        static_cast<sec_entry *>(calloc(sbi.total_sections, sizeof(struct sec_entry)));
+        static_cast<sec_entry *>(calloc(sbi.total_sections, sizeof(sec_entry)));
     if (!sit_i->sec_entries)
-      return -ENOMEM;
+      return ZX_ERR_NO_MEMORY;
   }
 
   /* get information related with SIT */
@@ -1797,12 +1815,12 @@
 
   /* setup SIT bitmap from ckeckpoint pack */
   bitmap_size = __bitmap_size(&sbi, SIT_BITMAP);
-  src_bitmap = (char *)__bitmap_ptr(&sbi, SIT_BITMAP);
+  src_bitmap = static_cast<char *>(__bitmap_ptr(&sbi, SIT_BITMAP));
 
   dst_bitmap = static_cast<char *>(malloc(bitmap_size));
   memset(dst_bitmap, 0, bitmap_size);
   if (!dst_bitmap)
-    return -ENOMEM;
+    return ZX_ERR_NO_MEMORY;
   memcpy(dst_bitmap, src_bitmap, bitmap_size);
 
 #if 0  // porting needed
@@ -1821,55 +1839,55 @@
   sit_i->elapsed_time = LeToCpu(sbi.ckpt->elapsed_time);
   sit_i->mounted_time = cur_time;
   mtx_init(&sit_i->sentry_lock, mtx_plain);
-  return 0;
+  return ZX_OK;
 }
 
-int SegMgr::BuildFreeSegmap() {
+zx_status_t SegMgr::BuildFreeSegmap() {
   f2fs_sb_info &sbi = fs_->SbInfo();
-  struct f2fs_sm_info *sm_info = SM_I(&sbi);
-  struct free_segmap_info *free_i;
-  unsigned int bitmap_size, sec_bitmap_size;
+  f2fs_sm_info *sm_info = SM_I(&sbi);
+  free_segmap_info *free_i;
+  uint32_t bitmap_size, sec_bitmap_size;
 
   /* allocate memory for free segmap information */
-  free_i = static_cast<struct free_segmap_info *>(malloc(sizeof(struct free_segmap_info)));
-  memset(free_i, 0, sizeof(struct free_segmap_info));
+  free_i = static_cast<free_segmap_info *>(malloc(sizeof(free_segmap_info)));
+  memset(free_i, 0, sizeof(free_segmap_info));
 
   if (!free_i)
-    return -ENOMEM;
+    return ZX_ERR_NO_MEMORY;
 
   SM_I(&sbi)->free_info = free_i;
 
-  bitmap_size = f2fs_bitmap_size(TOTAL_SEGS(&sbi));
-  free_i->free_segmap = static_cast<unsigned long *>(malloc(bitmap_size));
+  bitmap_size = BitmapSize(TotalSegs(&sbi));
+  free_i->free_segmap = static_cast<uint64_t *>(malloc(bitmap_size));
   if (!free_i->free_segmap)
-    return -ENOMEM;
+    return ZX_ERR_NO_MEMORY;
 
-  sec_bitmap_size = f2fs_bitmap_size(sbi.total_sections);
-  free_i->free_secmap = static_cast<unsigned long *>(malloc(sec_bitmap_size));
+  sec_bitmap_size = BitmapSize(sbi.total_sections);
+  free_i->free_secmap = static_cast<uint64_t *>(malloc(sec_bitmap_size));
   if (!free_i->free_secmap)
-    return -ENOMEM;
+    return ZX_ERR_NO_MEMORY;
 
   /* set all segments as dirty temporarily */
   memset(free_i->free_segmap, 0xff, bitmap_size);
   memset(free_i->free_secmap, 0xff, sec_bitmap_size);
 
   /* init free segmap information */
-  free_i->start_segno = (unsigned int)GET_SEGNO_FROM_SEG0(&sbi, sm_info->main_blkaddr);
+  free_i->start_segno = GetSegNoFromSeg0(&sbi, sm_info->main_blkaddr);
   free_i->free_segments = 0;
   free_i->free_sections = 0;
 
   RwlockInit(&free_i->segmap_lock);
-  return 0;
+  return ZX_OK;
 }
 
-int SegMgr::BuildCurseg() {
+zx_status_t SegMgr::BuildCurseg() {
   f2fs_sb_info &sbi = fs_->SbInfo();
   curseg_info *array = nullptr;
   int i;
   array = static_cast<curseg_info *>(calloc(NR_CURSEG_TYPE, sizeof(*array)));
 
   if (!array)
-    return -ENOMEM;
+    return ZX_ERR_NO_MEMORY;
 
   SM_I(&sbi)->curseg_array = array;
 
@@ -1878,8 +1896,8 @@
     array[i].sum_blk = static_cast<f2fs_summary_block *>(malloc(kPageCacheSize));
     memset(array[i].sum_blk, 0, kPageCacheSize);
     if (!array[i].sum_blk)
-      return -ENOMEM;
-    array[i].segno = NULL_SEGNO;
+      return ZX_ERR_NO_MEMORY;
+    array[i].segno = kNullSegNo;
     array[i].next_blkoff = 0;
   }
   return RestoreCursegSummaries();
@@ -1887,15 +1905,15 @@
 
 void SegMgr::BuildSitEntries() {
   f2fs_sb_info &sbi = fs_->SbInfo();
-  struct sit_info *sit_i = SIT_I(&sbi);
-  struct curseg_info *curseg = CURSEG_I(&sbi, CURSEG_COLD_DATA);
-  struct f2fs_summary_block *sum = curseg->sum_blk;
-  unsigned int start;
+  sit_info *sit_i = SIT_I(&sbi);
+  curseg_info *curseg = CURSEG_I(&sbi, CURSEG_COLD_DATA);
+  f2fs_summary_block *sum = curseg->sum_blk;
+  uint32_t start;
 
-  for (start = 0; start < TOTAL_SEGS(&sbi); start++) {
-    struct seg_entry *se = &sit_i->sentries[start];
-    struct f2fs_sit_block *sit_blk;
-    struct f2fs_sit_entry sit;
+  for (start = 0; start < TotalSegs(&sbi); start++) {
+    seg_entry *se = &sit_i->sentries[start];
+    f2fs_sit_block *sit_blk;
+    f2fs_sit_entry sit;
     Page *page;
     int i;
 
@@ -1909,14 +1927,14 @@
     }
     mtx_unlock(&curseg->curseg_mutex);
     page = GetCurrentSitPage(start);
-    sit_blk = (struct f2fs_sit_block *)PageAddress(page);
-    sit = sit_blk->entries[SIT_ENTRY_OFFSET(sit_i, start)];
+    sit_blk = static_cast<f2fs_sit_block *>(PageAddress(page));
+    sit = sit_blk->entries[SitEntryOffset(sit_i, start)];
     F2fsPutPage(page, 1);
   got_it:
     CheckBlockCount(start, &sit);
     SegInfoFromRawSit(se, &sit);
     if (sbi.segs_per_sec > 1) {
-      struct sec_entry *e = GetSecEntry(start);
+      sec_entry *e = GetSecEntry(start);
       e->valid_blocks += se->valid_blocks;
     }
   }
@@ -1924,34 +1942,34 @@
 
 void SegMgr::InitFreeSegmap() {
   f2fs_sb_info &sbi = fs_->SbInfo();
-  unsigned int start;
+  uint32_t start;
   int type;
 
-  for (start = 0; start < TOTAL_SEGS(&sbi); start++) {
-    struct seg_entry *sentry = GetSegEntry(start);
+  for (start = 0; start < TotalSegs(&sbi); start++) {
+    seg_entry *sentry = GetSegEntry(start);
     if (!sentry->valid_blocks)
       __SetFree(start);
   }
 
   /* set use the current segments */
   for (type = CURSEG_HOT_DATA; type <= CURSEG_COLD_NODE; type++) {
-    struct curseg_info *curseg_t = CURSEG_I(&sbi, type);
+    curseg_info *curseg_t = CURSEG_I(&sbi, type);
     __SetTestAndInuse(curseg_t->segno);
   }
 }
 
 void SegMgr::InitDirtySegmap() {
   f2fs_sb_info &sbi = fs_->SbInfo();
-  struct dirty_seglist_info *dirty_i = DIRTY_I(&sbi);
-  struct free_segmap_info *free_i = FREE_I(&sbi);
-  unsigned int segno = 0, offset = 0;
-  unsigned short valid_blocks;
+  dirty_seglist_info *dirty_i = DIRTY_I(&sbi);
+  free_segmap_info *free_i = FREE_I(&sbi);
+  uint32_t segno = 0, offset = 0;
+  uint16_t valid_blocks;
   int full_block_cnt = 0, dirty_block_cnt = 0;
 
-  while (segno < TOTAL_SEGS(&sbi)) {
+  while (segno < TotalSegs(&sbi)) {
     /* find dirty segment based on free segmap */
-    segno = FindNextInuse(free_i, TOTAL_SEGS(&sbi), offset);
-    if (segno >= TOTAL_SEGS(&sbi))
+    segno = FindNextInuse(free_i, TotalSegs(&sbi), offset);
+    if (segno >= TotalSegs(&sbi))
       break;
     offset = segno + 1;
     valid_blocks = GetValidBlocks(segno, 0);
@@ -1960,7 +1978,7 @@
       continue;
     }
     mtx_lock(&dirty_i->seglist_lock);
-    __LocateDirtySegment(segno, DIRTY);
+    __LocateDirtySegment(segno, DirtyType::kDirty);
     dirty_block_cnt++;
     mtx_unlock(&dirty_i->seglist_lock);
   }
@@ -1971,42 +1989,42 @@
 #endif
 }
 
-int SegMgr::InitVictimSegmap() {
+zx_status_t SegMgr::InitVictimSegmap() {
   f2fs_sb_info &sbi = fs_->SbInfo();
-  struct dirty_seglist_info *dirty_i = DIRTY_I(&sbi);
-  unsigned int bitmap_size = f2fs_bitmap_size(TOTAL_SEGS(&sbi));
+  dirty_seglist_info *dirty_i = DIRTY_I(&sbi);
+  uint32_t bitmap_size = BitmapSize(TotalSegs(&sbi));
 
-  dirty_i->victim_segmap[FG_GC] = static_cast<unsigned long *>(malloc(bitmap_size));
-  memset(dirty_i->victim_segmap[FG_GC], 0, bitmap_size);
-  dirty_i->victim_segmap[BG_GC] = static_cast<unsigned long *>(malloc(bitmap_size));
-  memset(dirty_i->victim_segmap[BG_GC], 0, bitmap_size);
+  dirty_i->victim_segmap[static_cast<int>(GcType::kFgGc)] = static_cast<uint64_t *>(malloc(bitmap_size));
+  memset(dirty_i->victim_segmap[static_cast<int>(GcType::kFgGc)], 0, bitmap_size);
+  dirty_i->victim_segmap[static_cast<int>(GcType::kBgGc)] = static_cast<uint64_t *>(malloc(bitmap_size));
+  memset(dirty_i->victim_segmap[static_cast<int>(GcType::kBgGc)], 0, bitmap_size);
 
-  if (!dirty_i->victim_segmap[FG_GC] || !dirty_i->victim_segmap[BG_GC])
-    return -ENOMEM;
-  return 0;
+  if (!dirty_i->victim_segmap[static_cast<int>(GcType::kFgGc)] || !dirty_i->victim_segmap[static_cast<int>(GcType::kBgGc)])
+    return ZX_ERR_NO_MEMORY;
+  return ZX_OK;
 }
 
-int SegMgr::BuildDirtySegmap() {
+zx_status_t SegMgr::BuildDirtySegmap() {
   f2fs_sb_info &sbi = fs_->SbInfo();
-  struct dirty_seglist_info *dirty_i;
-  unsigned int bitmap_size, i;
+  dirty_seglist_info *dirty_i;
+  uint32_t bitmap_size, i;
 
-  dirty_i = static_cast<struct dirty_seglist_info *>(malloc(sizeof(struct dirty_seglist_info)));
-  memset(dirty_i, 0, sizeof(struct dirty_seglist_info));
+  dirty_i = static_cast<dirty_seglist_info *>(malloc(sizeof(dirty_seglist_info)));
+  memset(dirty_i, 0, sizeof(dirty_seglist_info));
   if (!dirty_i)
-    return -ENOMEM;
+    return ZX_ERR_NO_MEMORY;
 
   SM_I(&sbi)->dirty_info = dirty_i;
   mtx_init(&dirty_i->seglist_lock, mtx_plain);
 
-  bitmap_size = f2fs_bitmap_size(TOTAL_SEGS(&sbi));
+  bitmap_size = BitmapSize(TotalSegs(&sbi));
 
-  for (i = 0; i < NR_DIRTY_TYPE; i++) {
-    dirty_i->dirty_segmap[i] = static_cast<unsigned long *>(malloc(bitmap_size));
+  for (i = 0; i < static_cast<int>(DirtyType::kNrDirtytype); i++) {
+    dirty_i->dirty_segmap[i] = static_cast<uint64_t *>(malloc(bitmap_size));
     memset(dirty_i->dirty_segmap[i], 0, bitmap_size);
     dirty_i->nr_dirty[i] = 0;
     if (!dirty_i->dirty_segmap[i])
-      return -ENOMEM;
+      return ZX_ERR_NO_MEMORY;
   }
 
   InitDirtySegmap();
@@ -2018,16 +2036,16 @@
  */
 void SegMgr::InitMinMaxMtime() {
   f2fs_sb_info &sbi = fs_->SbInfo();
-  struct sit_info *sit_i = SIT_I(&sbi);
-  unsigned int segno;
+  sit_info *sit_i = SIT_I(&sbi);
+  uint32_t segno;
 
   mtx_lock(&sit_i->sentry_lock);
 
   sit_i->min_mtime = LLONG_MAX;
 
-  for (segno = 0; segno < TOTAL_SEGS(&sbi); segno += sbi.segs_per_sec) {
-    unsigned int i;
-    unsigned long long mtime = 0;
+  for (segno = 0; segno < TotalSegs(&sbi); segno += sbi.segs_per_sec) {
+    uint32_t i;
+    uint64_t mtime = 0;
 
     for (i = 0; i < sbi.segs_per_sec; i++)
       mtime += GetSegEntry(segno + i)->mtime;
@@ -2043,10 +2061,10 @@
 
 zx_status_t SegMgr::BuildSegmentManager() {
   f2fs_sb_info &sbi = fs_->SbInfo();
-  struct f2fs_super_block *raw_super = F2FS_RAW_SUPER(&sbi);
-  struct f2fs_checkpoint *ckpt = F2FS_CKPT(&sbi);
-  struct f2fs_sm_info *sm_info = nullptr;
-  int err;
+  f2fs_super_block *raw_super = F2FS_RAW_SUPER(&sbi);
+  f2fs_checkpoint *ckpt = F2FS_CKPT(&sbi);
+  f2fs_sm_info *sm_info = nullptr;
+  zx_status_t err = 0;
 
   sm_info = new f2fs_sm_info;
   if (!sm_info)
@@ -2086,7 +2104,7 @@
     return err;
 
 #ifdef F2FS_BU_DEBUG
-  std::cout << "SegMgr::BuildSegmentManager(), TOTAL_SEGS(&sbi)=" << TOTAL_SEGS(&sbi)
+  std::cout << "SegMgr::BuildSegmentManager(), TotalSegs(&sbi)=" << TotalSegs(&sbi)
             << std::endl;
   std::cout << "SegMgr::BuildSegmentManager(), ReservedSections()=" << ReservedSections()
             << std::endl;
@@ -2098,51 +2116,51 @@
   return ZX_OK;
 }
 
-void SegMgr::DiscardDirtySegmap(enum dirty_type dirty_type) {
+void SegMgr::DiscardDirtySegmap(DirtyType dirty_type) {
   f2fs_sb_info &sbi = fs_->SbInfo();
-  struct dirty_seglist_info *dirty_i = DIRTY_I(&sbi);
+  dirty_seglist_info *dirty_i = DIRTY_I(&sbi);
 
   mtx_lock(&dirty_i->seglist_lock);
-  free(dirty_i->dirty_segmap[dirty_type]);
-  dirty_i->nr_dirty[dirty_type] = 0;
+  free(dirty_i->dirty_segmap[static_cast<int>(dirty_type)]);
+  dirty_i->nr_dirty[static_cast<int>(dirty_type)] = 0;
   mtx_unlock(&dirty_i->seglist_lock);
 }
 
 void SegMgr::ResetVictimSegmap() {
   f2fs_sb_info &sbi = fs_->SbInfo();
-  unsigned int bitmap_size = f2fs_bitmap_size(TOTAL_SEGS(&sbi));
-  memset(DIRTY_I(&sbi)->victim_segmap[FG_GC], 0, bitmap_size);
+  uint32_t bitmap_size = BitmapSize(TotalSegs(&sbi));
+  memset(DIRTY_I(&sbi)->victim_segmap[static_cast<int>(GcType::kFgGc)], 0, bitmap_size);
 }
 
 void SegMgr::DestroyVictimSegmap() {
   f2fs_sb_info &sbi = fs_->SbInfo();
-  struct dirty_seglist_info *dirty_i = DIRTY_I(&sbi);
+  dirty_seglist_info *dirty_i = DIRTY_I(&sbi);
 
-  free(dirty_i->victim_segmap[FG_GC]);
-  free(dirty_i->victim_segmap[BG_GC]);
+  free(dirty_i->victim_segmap[static_cast<int>(GcType::kFgGc)]);
+  free(dirty_i->victim_segmap[static_cast<int>(GcType::kBgGc)]);
 }
 
 void SegMgr::DestroyDirtySegmap() {
   f2fs_sb_info &sbi = fs_->SbInfo();
-  struct dirty_seglist_info *dirty_i = DIRTY_I(&sbi);
+  dirty_seglist_info *dirty_i = DIRTY_I(&sbi);
   int i;
 
   if (!dirty_i)
     return;
 
   /* discard pre-free/dirty segments list */
-  for (i = 0; i < NR_DIRTY_TYPE; i++)
-    DiscardDirtySegmap((dirty_type)i);
+  for (i = 0; i < static_cast<int>(DirtyType::kNrDirtytype); i++)
+    DiscardDirtySegmap(static_cast<DirtyType>(i));
 
   DestroyVictimSegmap();
-  SM_I(&sbi)->dirty_info = NULL;
+  SM_I(&sbi)->dirty_info = nullptr;
   free(dirty_i);
 }
 
 // TODO: destroy_curseg
 void SegMgr::DestroyCurseg() {
   f2fs_sb_info &sbi = fs_->SbInfo();
-  struct curseg_info *array = SM_I(&sbi)->curseg_array;
+  curseg_info *array = SM_I(&sbi)->curseg_array;
   int i;
 
   if (!array)
@@ -2155,7 +2173,7 @@
 
 void SegMgr::DestroyFreeSegmap() {
   f2fs_sb_info &sbi = fs_->SbInfo();
-  struct free_segmap_info *free_i = SM_I(&sbi)->free_info;
+  free_segmap_info *free_i = SM_I(&sbi)->free_info;
   if (!free_i)
     return;
   SM_I(&sbi)->free_info = nullptr;
@@ -2166,14 +2184,14 @@
 
 void SegMgr::DestroySitInfo() {
   f2fs_sb_info &sbi = fs_->SbInfo();
-  struct sit_info *sit_i = SIT_I(&sbi);
-  unsigned int start;
+  sit_info *sit_i = SIT_I(&sbi);
+  uint32_t start;
 
   if (!sit_i)
     return;
 
   if (sit_i->sentries) {
-    for (start = 0; start < TOTAL_SEGS(&sbi); start++) {
+    for (start = 0; start < TotalSegs(&sbi); start++) {
       free(sit_i->sentries[start].cur_valid_map);
       free(sit_i->sentries[start].ckpt_valid_map);
     }
@@ -2189,7 +2207,7 @@
 
 void SegMgr::DestroySegmentManager() {
   f2fs_sb_info &sbi = fs_->SbInfo();
-  struct f2fs_sm_info *sm_info = SM_I(&sbi);
+  f2fs_sm_info *sm_info = SM_I(&sbi);
 
 	DestroyDirtySegmap();
   DestroyCurseg();
diff --git a/segment.h b/segment.h
index a2dbcb5..e68c89c 100644
--- a/segment.h
+++ b/segment.h
@@ -9,61 +9,12 @@
 namespace f2fs {
 
 /* constant macro */
-#define NULL_SEGNO ((unsigned int)(~0))
-
-/* V: Logical segment # in volume, R: Relative segment # in main area */
-#define GET_L2R_SEGNO(free_i, segno) (segno - free_i->start_segno)
-#define GET_R2L_SEGNO(free_i, segno) (segno + free_i->start_segno)
-
-#define IS_DATASEG(t) ((t == CURSEG_HOT_DATA) || (t == CURSEG_COLD_DATA) || (t == CURSEG_WARM_DATA))
-
-#define IS_NODESEG(t) ((t == CURSEG_HOT_NODE) || (t == CURSEG_COLD_NODE) || (t == CURSEG_WARM_NODE))
-
-#define IS_CURSEG(sbi, segno)                           \
-  ((segno == CURSEG_I(sbi, CURSEG_HOT_DATA)->segno) ||  \
-   (segno == CURSEG_I(sbi, CURSEG_WARM_DATA)->segno) || \
-   (segno == CURSEG_I(sbi, CURSEG_COLD_DATA)->segno) || \
-   (segno == CURSEG_I(sbi, CURSEG_HOT_NODE)->segno) ||  \
-   (segno == CURSEG_I(sbi, CURSEG_WARM_NODE)->segno) || \
-   (segno == CURSEG_I(sbi, CURSEG_COLD_NODE)->segno))
-
-#define IS_CURSEC(sbi, secno)                                                 \
-  ((secno == CURSEG_I(sbi, CURSEG_HOT_DATA)->segno / (sbi)->segs_per_sec) ||  \
-   (secno == CURSEG_I(sbi, CURSEG_WARM_DATA)->segno / (sbi)->segs_per_sec) || \
-   (secno == CURSEG_I(sbi, CURSEG_COLD_DATA)->segno / (sbi)->segs_per_sec) || \
-   (secno == CURSEG_I(sbi, CURSEG_HOT_NODE)->segno / (sbi)->segs_per_sec) ||  \
-   (secno == CURSEG_I(sbi, CURSEG_WARM_NODE)->segno / (sbi)->segs_per_sec) || \
-   (secno == CURSEG_I(sbi, CURSEG_COLD_NODE)->segno / (sbi)->segs_per_sec))
-
-#define START_BLOCK(sbi, segno) \
-  (SM_I(sbi)->seg0_blkaddr + (GET_R2L_SEGNO(FREE_I(sbi), segno) << (sbi)->log_blocks_per_seg))
-#define NEXT_FREE_BLKADDR(sbi, curseg) (START_BLOCK(sbi, curseg->segno) + curseg->next_blkoff)
-
-#define MAIN_BASE_BLOCK(sbi) (SM_I(sbi)->main_blkaddr)
-
-#define GET_SEGOFF_FROM_SEG0(sbi, blk_addr) ((blk_addr)-SM_I(sbi)->seg0_blkaddr)
-#define GET_SEGNO_FROM_SEG0(sbi, blk_addr) \
-  (GET_SEGOFF_FROM_SEG0(sbi, blk_addr) >> (sbi)->log_blocks_per_seg)
-#define GET_SEGNO(sbi, blk_addr)                       \
-  (((blk_addr == NULL_ADDR) || (blk_addr == NEW_ADDR)) \
-       ? NULL_SEGNO                                    \
-       : GET_L2R_SEGNO(FREE_I(sbi), GET_SEGNO_FROM_SEG0(sbi, blk_addr)))
-#define GET_SECNO(sbi, segno) ((segno) / (sbi)->segs_per_sec)
-#define GET_ZONENO_FROM_SEGNO(sbi, segno) ((segno / (sbi)->segs_per_sec) / (sbi)->secs_per_zone)
-
-#define GET_SUM_BLOCK(sbi, segno) (((sbi)->sm_info->ssa_blkaddr) + segno)
-
-#define SIT_ENTRY_OFFSET(sit_i, segno) (segno % (sit_i)->sents_per_block)
-#define SIT_BLOCK_OFFSET(sit_i, segno) (segno / SIT_ENTRY_PER_BLOCK)
-#define START_SEGNO(sit_i, segno) (SIT_BLOCK_OFFSET(sit_i, segno) * SIT_ENTRY_PER_BLOCK)
-#define f2fs_bitmap_size(nr) (BitsToLongs(nr) * sizeof(unsigned long))
-#define TOTAL_SEGS(sbi) (SM_I(sbi)->main_segments)
+constexpr uint32_t kNullSegNo = (uint32_t)(~0);
 
 /* during checkpoint, bio_private is used to synchronize the last bio */
 struct bio_private {
-  ;
-  bool is_sync;
-  void *wait;
+  bool is_sync = false;
+  void *wait = nullptr;
 };
 
 /*
@@ -71,129 +22,176 @@
  * RIGHT means allocating new sections towards the end of volume.
  * LEFT means the opposite direction.
  */
-enum { ALLOC_RIGHT = 0, ALLOC_LEFT };
+enum class AllocDirection { kAllocRight = 0, kAllocLeft, };
 
 /*
  * In the victim_sel_policy->alloc_mode, there are two block allocation modes.
  * LFS writes data sequentially with cleaning operations.
  * SSR (Slack Space Recycle) reuses obsolete space without cleaning operations.
  */
-enum { LFS = 0, SSR };
+enum class AllocMode { kLFS = 0, kSSR };
 
 /*
  * In the victim_sel_policy->gc_mode, there are two gc, aka cleaning, modes.
  * GC_CB is based on cost-benefit algorithm.
  * GC_GREEDY is based on greedy algorithm.
  */
-enum { GC_CB = 0, GC_GREEDY };
+enum class GcAlgorithm { kGcCb = 0, kGcGreedy };
 
 /*
  * BG_GC means the background cleaning job.
  * FG_GC means the on-demand cleaning job.
  */
-enum { BG_GC = 0, FG_GC };
+enum class GcType { kBgGc = 0, kFgGc };
 
 /* for a function parameter to select a victim segment */
 struct victim_sel_policy {
-  int alloc_mode;              /* LFS or SSR */
-  int gc_mode;                 /* GC_CB or GC_GREEDY */
-  unsigned long *dirty_segmap; /* dirty segment bitmap */
-  unsigned int offset;         /* last scanned bitmap offset */
-  unsigned int ofs_unit;       /* bitmap search unit */
-  unsigned int min_cost;       /* minimum cost */
-  unsigned int min_segno;      /* segment # having min. cost */
+  int alloc_mode = 0;              /* LFS or SSR */
+  int gc_mode = 0;                 /* GC_CB or GC_GREEDY */
+  uint64_t *dirty_segmap = nullptr; /* dirty segment bitmap */
+  uint32_t offset = 0;         /* last scanned bitmap offset */
+  uint32_t ofs_unit = 0;       /* bitmap search unit */
+  uint32_t min_cost = 0;       /* minimum cost */
+  uint32_t min_segno = 0;      /* segment # having min. cost */
 };
 
 struct seg_entry {
-  unsigned short valid_blocks;  /* # of valid blocks */
-  unsigned char *cur_valid_map; /* validity bitmap of blocks */
+  uint16_t valid_blocks = 0;  /* # of valid blocks */
+  uint8_t *cur_valid_map = nullptr; /* validity bitmap of blocks */
   /*
    * # of valid blocks and the validity bitmap stored in the the last
    * checkpoint pack. This information is used by the SSR mode.
    */
-  unsigned short ckpt_valid_blocks;
-  unsigned char *ckpt_valid_map;
-  unsigned char type;       /* segment type like CURSEG_XXX_TYPE */
-  unsigned long long mtime; /* modification time of the segment */
+  uint16_t ckpt_valid_blocks = 0;
+  uint8_t *ckpt_valid_map = nullptr;
+  uint8_t type = 0;       /* segment type like CURSEG_XXX_TYPE */
+  uint64_t mtime = 0; /* modification time of the segment */
 };
 
 struct sec_entry {
-  unsigned int valid_blocks; /* # of valid blocks in a section */
+  uint32_t valid_blocks = 0; /* # of valid blocks in a section */
 };
 
 struct segment_allocation {
-  void (*allocate_segment)(struct f2fs_sb_info *, int, bool);
+  void (*allocate_segment)(f2fs_sb_info *, int, bool) = nullptr;
 };
 
 struct sit_info {
-  const struct segment_allocation *s_ops;
+  const segment_allocation *s_ops = nullptr;
 
-  block_t sit_base_addr;        /* start block address of SIT area */
-  block_t sit_blocks;           /* # of blocks used by SIT area */
-  block_t written_valid_blocks; /* # of valid blocks in main area */
-  char *sit_bitmap;             /* SIT bitmap pointer */
-  unsigned int bitmap_size;     /* SIT bitmap size */
+  block_t sit_base_addr = 0;        /* start block address of SIT area */
+  block_t sit_blocks = 0;           /* # of blocks used by SIT area */
+  block_t written_valid_blocks = 0; /* # of valid blocks in main area */
+  char *sit_bitmap = nullptr;             /* SIT bitmap pointer */
+  uint32_t bitmap_size = 0;     /* SIT bitmap size */
 
-  unsigned long *dirty_sentries_bitmap; /* bitmap for dirty sentries */
-  unsigned int dirty_sentries;          /* # of dirty sentries */
-  unsigned int sents_per_block;         /* # of SIT entries per block */
+  uint64_t *dirty_sentries_bitmap = nullptr; /* bitmap for dirty sentries */
+  uint32_t dirty_sentries = 0;          /* # of dirty sentries */
+  uint32_t sents_per_block = 0;         /* # of SIT entries per block */
   mtx_t sentry_lock;                    /* to protect SIT cache */
-  struct seg_entry *sentries;           /* SIT segment-level cache */
-  struct sec_entry *sec_entries;        /* SIT section-level cache */
+  seg_entry *sentries = nullptr;           /* SIT segment-level cache */
+  sec_entry *sec_entries = nullptr;        /* SIT section-level cache */
 
   /* for cost-benefit algorithm in cleaning procedure */
-  unsigned long long elapsed_time; /* elapsed time after mount */
-  unsigned long long mounted_time; /* mount time */
-  unsigned long long min_mtime;    /* min. modification time */
-  unsigned long long max_mtime;    /* max. modification time */
+  uint64_t elapsed_time = 0; /* elapsed time after mount */
+  uint64_t mounted_time = 0; /* mount time */
+  uint64_t min_mtime = 0;    /* min. modification time */
+  uint64_t max_mtime = 0;    /* max. modification time */
 };
 
 struct free_segmap_info {
-  unsigned int start_segno;   /* start segment number logically */
-  unsigned int free_segments; /* # of free segments */
-  unsigned int free_sections; /* # of free sections */
+  uint32_t start_segno = 0;   /* start segment number logically */
+  uint32_t free_segments = 0; /* # of free segments */
+  uint32_t free_sections = 0; /* # of free sections */
   rwlock_t segmap_lock;       /* free segmap lock */
-  unsigned long *free_segmap; /* free segment bitmap */
-  unsigned long *free_secmap; /* free section bitmap */
+  uint64_t *free_segmap = nullptr; /* free segment bitmap */
+  uint64_t *free_secmap = nullptr; /* free section bitmap */
 };
 
 /* Notice: The order of dirty type is same with CURSEG_XXX in f2fs.h */
-enum dirty_type {
-  DIRTY_HOT_DATA,  /* dirty segments assigned as hot data logs */
-  DIRTY_WARM_DATA, /* dirty segments assigned as warm data logs */
-  DIRTY_COLD_DATA, /* dirty segments assigned as cold data logs */
-  DIRTY_HOT_NODE,  /* dirty segments assigned as hot node logs */
-  DIRTY_WARM_NODE, /* dirty segments assigned as warm node logs */
-  DIRTY_COLD_NODE, /* dirty segments assigned as cold node logs */
-  DIRTY,           /* to count # of dirty segments */
-  PRE,             /* to count # of entirely obsolete segments */
-  NR_DIRTY_TYPE
+enum class DirtyType {
+  kDirtyHotData = 0,  /* dirty segments assigned as hot data logs */
+  kDirtyWarmData, /* dirty segments assigned as warm data logs */
+  kDirtyColdData, /* dirty segments assigned as cold data logs */
+  kDirtyHotNode,  /* dirty segments assigned as hot node logs */
+  kDirtyWarmNode, /* dirty segments assigned as warm node logs */
+  kDirtyColdNode, /* dirty segments assigned as cold node logs */
+  kDirty,           /* to count # of dirty segments */
+  kPre,             /* to count # of entirely obsolete segments */
+  kNrDirtytype
 };
 
 struct dirty_seglist_info {
-  const struct victim_selection *v_ops; /* victim selction operation */
-  unsigned long *dirty_segmap[NR_DIRTY_TYPE];
+  const struct victim_selection *v_ops = nullptr; /* victim selction operation */
+  uint64_t *dirty_segmap[static_cast<int>(DirtyType::kNrDirtytype)] = {};
   mtx_t seglist_lock;              /* lock for segment bitmaps */
-  int nr_dirty[NR_DIRTY_TYPE];     /* # of dirty segments */
-  unsigned long *victim_segmap[2]; /* BG_GC, FG_GC */
+  int nr_dirty[static_cast<int>(DirtyType::kNrDirtytype)] = {};     /* # of dirty segments */
+  uint64_t *victim_segmap[2] = {}; /* BG_GC, FG_GC */
 };
 
 /* victim selection function for cleaning and SSR */
 struct victim_selection {
-  int (*get_victim)(struct f2fs_sb_info *, unsigned int *, int, int, char);
+  int (*get_victim)(f2fs_sb_info *, uint32_t *, int, int, char) = nullptr;
 };
 
 /* for active log information */
 struct curseg_info {
   mtx_t curseg_mutex;                 /* lock for consistency */
-  struct f2fs_summary_block *sum_blk; /* cached summary block */
-  unsigned char alloc_type;           /* current allocation type */
-  unsigned int segno;                 /* current segment number */
-  unsigned short next_blkoff;         /* next block offset to write */
-  unsigned int zone;                  /* current zone number */
-  unsigned int next_segno;            /* preallocated segment */
+  f2fs_summary_block *sum_blk = nullptr; /* cached summary block */
+  uint8_t alloc_type = 0;           /* current allocation type */
+  uint32_t segno = 0;                 /* current segment number */
+  uint16_t next_blkoff = 0;         /* next block offset to write */
+  uint32_t zone = 0;                  /* current zone number */
+  uint32_t next_segno = 0;            /* preallocated segment */
 };
 
+/* V: Logical segment # in volume, R: Relative segment # in main area */
+inline uint32_t GetL2RSegNo(free_segmap_info *free_i, uint32_t segno) { return (segno - free_i->start_segno); }
+inline uint32_t GetR2LSegNo(free_segmap_info *free_i, uint32_t segno) { return (segno + free_i->start_segno); }
+
+inline uint32_t IsDataSeg(uint32_t t) {
+  return ((t == CURSEG_HOT_DATA) || (t == CURSEG_COLD_DATA) || (t == CURSEG_WARM_DATA));
+}
+
+inline uint32_t IsNodeSeg(uint32_t t) {
+  return ((t == CURSEG_HOT_NODE) || (t == CURSEG_COLD_NODE) || (t == CURSEG_WARM_NODE));
+}
+
+inline block_t StartBlock(f2fs_sb_info *sbi, uint32_t segno) {
+  return (SM_I(sbi)->seg0_blkaddr + (GetR2LSegNo(FREE_I(sbi), segno) << (sbi)->log_blocks_per_seg));
+}
+inline block_t NextFreeBlkAddr(f2fs_sb_info *sbi, curseg_info *curseg) {
+  return (StartBlock(sbi, curseg->segno) + curseg->next_blkoff);
+}
+
+inline block_t MainBaseBlock(f2fs_sb_info *sbi) { return SM_I(sbi)->main_blkaddr;}
+
+inline block_t GetSegOffFromSeg0(f2fs_sb_info *sbi, block_t blk_addr) { return blk_addr - SM_I(sbi)->seg0_blkaddr; }
+inline uint32_t GetSegNoFromSeg0(f2fs_sb_info *sbi, block_t blk_addr) {
+  return GetSegOffFromSeg0(sbi, blk_addr) >> sbi->log_blocks_per_seg;
+}
+
+inline uint32_t GetSegNo(f2fs_sb_info *sbi, block_t blk_addr) {
+  return ((blk_addr == NULL_ADDR) || (blk_addr == NEW_ADDR)) ? kNullSegNo
+       : GetL2RSegNo(FREE_I(sbi), GetSegNoFromSeg0(sbi, blk_addr));
+}
+
+inline uint32_t GetSecNo(f2fs_sb_info *sbi, uint32_t segno) { return segno / sbi->segs_per_sec; }
+
+inline uint32_t GetZoneNoFromSegNo(f2fs_sb_info *sbi, uint32_t segno) {
+  return (segno / sbi->segs_per_sec) / sbi->secs_per_zone;
+}
+
+inline block_t GetSumBlock(f2fs_sb_info *sbi, uint32_t segno) {
+  return (sbi->sm_info->ssa_blkaddr) + segno;
+}
+
+inline uint32_t SitEntryOffset(sit_info *sit_i, uint32_t segno) { return segno % sit_i->sents_per_block; }
+inline uint32_t SitBlockOffset(sit_info *sit_i, uint32_t segno) { return segno / SIT_ENTRY_PER_BLOCK; }
+inline uint32_t StartSegNo(sit_info *sit_i, uint32_t segno) { return SitBlockOffset(sit_i, segno) * SIT_ENTRY_PER_BLOCK; }
+inline uint32_t BitmapSize(uint32_t nr) { return BitsToLongs(nr) * sizeof(uint64_t); }
+inline uint32_t TotalSegs(f2fs_sb_info *sbi) { return SM_I(sbi)->main_segments; }
+
 class SegMgr {
  public:
   // Not copyable or moveable
@@ -209,14 +207,14 @@
   ~SegMgr() = default;
 
   // Static functions
-  static struct curseg_info *CURSEG_I(f2fs_sb_info *sbi, int type);
-  static int LookupJournalInCursum(struct f2fs_summary_block *sum, int type, unsigned int val,
+  static curseg_info *CURSEG_I(f2fs_sb_info *sbi, int type);
+  static int LookupJournalInCursum(f2fs_summary_block *sum, int type, uint32_t val,
                                    int alloc);
 
   // Public functions
   zx_status_t BuildSegmentManager();
   void DestroySegmentManager();
-  void RewriteNodePage(Page *page, struct f2fs_summary *sum, block_t old_blkaddr,
+  void RewriteNodePage(Page *page, f2fs_summary *sum, block_t old_blkaddr,
                        block_t new_blkaddr);
 
  private:
@@ -224,73 +222,73 @@
 
  public:
   // Inline functions
-  struct seg_entry *GetSegEntry(unsigned int segno);
-  struct sec_entry *GetSecEntry(unsigned int segno);
-  unsigned int GetValidBlocks(unsigned int segno, int section);
-  void SegInfoFromRawSit(struct seg_entry *se, struct f2fs_sit_entry *rs);
-  void SegInfoToRawSit(struct seg_entry *se, struct f2fs_sit_entry *rs);
-  unsigned int FindNextInuse(struct free_segmap_info *free_i, unsigned int max, unsigned int segno);
-  void __SetFree(unsigned int segno);
-  void __SetInuse(unsigned int segno);
-  void __SetTestAndFree(unsigned int segno);
-  void __SetTestAndInuse(unsigned int segno);
+  seg_entry *GetSegEntry(uint32_t segno);
+  sec_entry *GetSecEntry(uint32_t segno);
+  uint32_t GetValidBlocks(uint32_t segno, int section);
+  void SegInfoFromRawSit(seg_entry *se, f2fs_sit_entry *rs);
+  void SegInfoToRawSit(seg_entry *se, f2fs_sit_entry *rs);
+  uint32_t FindNextInuse(free_segmap_info *free_i, uint32_t max, uint32_t segno);
+  void __SetFree(uint32_t segno);
+  void __SetInuse(uint32_t segno);
+  void __SetTestAndFree(uint32_t segno);
+  void __SetTestAndInuse(uint32_t segno);
   void GetSitBitmap(void *dst_addr);
 #if 0  // porting needed
   block_t WrittenBlockCount();
 #endif
-  unsigned int FreeSegments();
+  uint32_t FreeSegments();
   int ReservedSegments();
-  unsigned int FreeSections();
-  unsigned int PrefreeSegments();
-  unsigned int DirtySegments();
+  uint32_t FreeSections();
+  uint32_t PrefreeSegments();
+  uint32_t DirtySegments();
   int OverprovisionSegments();
   int OverprovisionSections();
   int ReservedSections();
   bool NeedSSR();
   int GetSsrSegment(int type);
   bool HasNotEnoughFreeSecs();
-  int Utilization();
+  uint32_t Utilization();
   bool NeedInplaceUpdate(VnodeF2fs *vnode);
-  unsigned int CursegSegno(int type);
-  unsigned char CursegAllocType(int type);
-  unsigned short CursegBlkoff(int type);
-  void CheckSegRange(unsigned int segno);
+  uint32_t CursegSegno(int type);
+  uint8_t CursegAllocType(int type);
+  uint16_t CursegBlkoff(int type);
+  void CheckSegRange(uint32_t segno);
   #if 0  // porting needed
   void VerifyBlockAddr(block_t blk_addr);
   #endif
-  void CheckBlockCount(int segno, struct f2fs_sit_entry *raw_sit);
-  pgoff_t CurrentSitAddr(unsigned int start);
+  void CheckBlockCount(int segno, f2fs_sit_entry *raw_sit);
+  pgoff_t CurrentSitAddr(uint32_t start);
   pgoff_t NextSitAddr(pgoff_t block_addr);
-  void SetToNextSit(struct sit_info *sit_i, unsigned int start);
-  unsigned long long GetMtime();
-  void SetSummary(struct f2fs_summary *sum, nid_t nid, unsigned int ofs_in_node,
-                  unsigned char version);
+  void SetToNextSit(sit_info *sit_i, uint32_t start);
+  uint64_t GetMtime();
+  void SetSummary(f2fs_summary *sum, nid_t nid, uint32_t ofs_in_node,
+                  uint8_t version);
   block_t StartSumBlock();
   block_t SumBlkAddr(int base, int type);
 
   // Functions
   int NeedToFlush();
   void F2fsBalanceFs();
-  void __LocateDirtySegment(unsigned int segno, enum dirty_type dirty_type);
-  void __RemoveDirtySegment(unsigned int segno, enum dirty_type dirty_type);
-  void LocateDirtySegment(unsigned int segno);
+  void __LocateDirtySegment(uint32_t segno, enum DirtyType dirty_type);
+  void __RemoveDirtySegment(uint32_t segno, enum DirtyType dirty_type);
+  void LocateDirtySegment(uint32_t segno);
   void SetPrefreeAsFreeSegments();
   void ClearPrefreeSegments();
-  void __MarkSitEntryDirty(unsigned int segno);
-  void __SetSitEntryType(int type, unsigned int segno, int modified);
+  void __MarkSitEntryDirty(uint32_t segno);
+  void __SetSitEntryType(int type, uint32_t segno, int modified);
   void UpdateSitEntry(block_t blkaddr, int del);
   void RefreshSitEntry(block_t old_blkaddr, block_t new_blkaddr);
   void InvalidateBlocks(block_t addr);
-  void __AddSumEntry(int type, struct f2fs_summary *sum, unsigned short offset);
+  void __AddSumEntry(int type, f2fs_summary *sum, uint16_t offset);
   int NpagesForSummaryFlush();
-  Page *GetSumPage(unsigned int segno);
-  void WriteSumPage(struct f2fs_summary_block *sum_blk, block_t blk_addr);
-  unsigned int CheckPrefreeSegments(int ofs_unit, int type);
-  void GetNewSegment(unsigned int *newseg, bool new_sec, int dir);
+  Page *GetSumPage(uint32_t segno);
+  void WriteSumPage(f2fs_summary_block *sum_blk, block_t blk_addr);
+  uint32_t CheckPrefreeSegments(int ofs_unit, int type);
+  void GetNewSegment(uint32_t *newseg, bool new_sec, int dir);
   void ResetCurseg(int type, int modified);
   void NewCurseg(int type, bool new_sec);
-  void __NextFreeBlkoff(struct curseg_info *seg, block_t start);
-  void __RefreshNextBlkoff(struct curseg_info *seg);
+  void __NextFreeBlkoff(curseg_info *seg, block_t start);
+  void __RefreshNextBlkoff(curseg_info *seg);
   void ChangeCurseg(int type, bool reuse);
   void AllocateSegmentByDefault(int type, bool force);
   void AllocateNewSegments();
@@ -302,8 +300,8 @@
 #endif
 
 #if 0  // porting needed
-  void F2fsEndIoWrite(struct bio *bio, int err);
-  struct bio *F2fsBioAlloc(struct block_device *bdev, sector_t first_sector, int nr_vecs,
+  void F2fsEndIoWrite(bio *bio, int err);
+  bio *F2fsBioAlloc(block_device *bdev, sector_t first_sector, int nr_vecs,
                            gfp_t gfp_flags);
   void DoSubmitBio(enum page_type type, bool sync);
 #endif
@@ -314,14 +312,14 @@
   int __GetSegmentType4(Page *page, enum page_type p_type);
   int __GetSegmentType6(Page *page, enum page_type p_type);
   int __GetSegmentType(Page *page, enum page_type p_type);
-  void DoWritePage(Page *page, block_t old_blkaddr, block_t *new_blkaddr, struct f2fs_summary *sum,
+  void DoWritePage(Page *page, block_t old_blkaddr, block_t *new_blkaddr, f2fs_summary *sum,
                    enum page_type p_type);
-  zx_status_t WriteMetaPage(Page *page, struct WritebackControl *wbc);
-  void WriteNodePage(Page *page, unsigned int nid, block_t old_blkaddr, block_t *new_blkaddr);
-  void WriteDataPage(VnodeF2fs *vnode, Page *page, struct dnode_of_data *dn, block_t old_blkaddr,
+  zx_status_t WriteMetaPage(Page *page, WritebackControl *wbc);
+  void WriteNodePage(Page *page, uint32_t nid, block_t old_blkaddr, block_t *new_blkaddr);
+  void WriteDataPage(VnodeF2fs *vnode, Page *page, dnode_of_data *dn, block_t old_blkaddr,
                      block_t *new_blkaddr);
   void RewriteDataPage(Page *page, block_t old_blk_addr);
-  void RecoverDataPage(Page *page, struct f2fs_summary *sum, block_t old_blkaddr,
+  void RecoverDataPage(Page *page, f2fs_summary *sum, block_t old_blkaddr,
                        block_t new_blkaddr);
 
   int ReadCompactedSummaries();
@@ -332,25 +330,25 @@
   void WriteDataSummaries(block_t start_blk);
   void WriteNodeSummaries(block_t start_blk);
 
-  Page *GetCurrentSitPage(unsigned int segno);
-  Page *GetNextSitPage(unsigned int start);
+  Page *GetCurrentSitPage(uint32_t segno);
+  Page *GetNextSitPage(uint32_t start);
   bool FlushSitsInJournal();
   void FlushSitEntries();
 
   //////////////////////////////////////////// BUILD
   ///////////////////////////////////////////////////////////
 
-  int BuildSitInfo();
-  int BuildFreeSegmap();
-  int BuildCurseg();
+  zx_status_t BuildSitInfo();
+  zx_status_t BuildFreeSegmap();
+  zx_status_t BuildCurseg();
   void BuildSitEntries();
   void InitFreeSegmap();
   void InitDirtySegmap();
-  int InitVictimSegmap();
-  int BuildDirtySegmap();
+  zx_status_t InitVictimSegmap();
+  zx_status_t BuildDirtySegmap();
   void InitMinMaxMtime();
 
-  void DiscardDirtySegmap(enum dirty_type dirty_type);
+  void DiscardDirtySegmap(enum DirtyType dirty_type);
   void ResetVictimSegmap();
   void DestroyVictimSegmap();
   void DestroyDirtySegmap();
@@ -360,8 +358,26 @@
   void DestroySitInfo();
 };
 
-inline struct curseg_info *SegMgr::CURSEG_I(f2fs_sb_info *sbi, int type) {
-  return (struct curseg_info *)(SM_I(sbi)->curseg_array + type);
+inline curseg_info *SegMgr::CURSEG_I(f2fs_sb_info *sbi, int type) {
+  return (curseg_info *)(SM_I(sbi)->curseg_array + type);
+}
+
+inline bool IsCurSeg(f2fs_sb_info *sbi, uint32_t segno) {
+  return ((segno == SegMgr::CURSEG_I(sbi, CURSEG_HOT_DATA)->segno) ||
+   (segno == SegMgr::CURSEG_I(sbi, CURSEG_WARM_DATA)->segno) ||
+   (segno == SegMgr::CURSEG_I(sbi, CURSEG_COLD_DATA)->segno) ||
+   (segno == SegMgr::CURSEG_I(sbi, CURSEG_HOT_NODE)->segno) ||
+   (segno == SegMgr::CURSEG_I(sbi, CURSEG_WARM_NODE)->segno) ||
+   (segno == SegMgr::CURSEG_I(sbi, CURSEG_COLD_NODE)->segno));
+}
+
+inline bool IsCurSec(f2fs_sb_info *sbi, uint32_t secno) {
+  return ((secno == SegMgr::CURSEG_I(sbi, CURSEG_HOT_DATA)->segno / (sbi)->segs_per_sec) ||
+   (secno == SegMgr::CURSEG_I(sbi, CURSEG_WARM_DATA)->segno / (sbi)->segs_per_sec) ||
+   (secno == SegMgr::CURSEG_I(sbi, CURSEG_COLD_DATA)->segno / (sbi)->segs_per_sec) ||
+   (secno == SegMgr::CURSEG_I(sbi, CURSEG_HOT_NODE)->segno / (sbi)->segs_per_sec) ||
+   (secno == SegMgr::CURSEG_I(sbi, CURSEG_WARM_NODE)->segno / (sbi)->segs_per_sec) ||
+   (secno == SegMgr::CURSEG_I(sbi, CURSEG_COLD_NODE)->segno / (sbi)->segs_per_sec));
 }
 
 }  // namespace f2fs