[f2fs] code refactoring: checkpoint.cc
Change struct xxx to xxx (checkpoint.cc)
(comment: #312 https://fuchsia-review.googlesource.com/c/third_party/f2fs/+/520881/2)
Change unsigned int style to uint32_t (checkpoint.cc, f2fs.h)
(comment: #142 https://fuchsia-review.googlesource.com/c/third_party/f2fs/+/511840)
Change C style cast to static_cast (checkpoint.cc)
(comment: #571 https://fuchsia-review.googlesource.com/c/third_party/f2fs/+/520880)
Change C style NULL to nullptr (checkpoint.cc)
Fix Statement should be inside braces warnning (checkpoint.cc)
Change-Id: I24c81eab2886e1a0f4181f500a03b36e61f30569
Reviewed-on: https://fuchsia-review.googlesource.com/c/third_party/f2fs/+/530800
Reviewed-by: Brett Wilson <brettw@google.com>
diff --git a/checkpoint.cc b/checkpoint.cc
index fd2922e..76806f3 100644
--- a/checkpoint.cc
+++ b/checkpoint.cc
@@ -37,7 +37,7 @@
#endif
goto repeat;
}
- if (VnodeF2fs::Readpage(this, (struct Page *)PageAddress(page), index, kReadSync)) {
+ if (VnodeF2fs::Readpage(this, static_cast<struct Page *>(PageAddress(page)), index, kReadSync)) {
F2fsPutPage(page, 1);
goto repeat;
}
@@ -49,7 +49,7 @@
return page;
}
-int F2fs::F2fsWriteMetaPage(Page *page, struct WritebackControl *wbc) {
+int F2fs::F2fsWriteMetaPage(Page *page, WritebackControl *wbc) {
int err;
WaitOnPageWriteback(page);
@@ -75,7 +75,7 @@
}
#if 0 // porting needed
-// int F2fs::F2fsWriteMetaPages(struct address_space *mapping, struct WritebackControl *wbc) {
+// int F2fs::F2fsWriteMetaPages(struct address_space *mapping, WritebackControl *wbc) {
// struct block_device *bdev = sbi_->sb->s_bdev;
// long written;
@@ -94,12 +94,12 @@
// }
#endif
-long F2fs::SyncMetaPages(enum page_type type, long nr_to_write) {
+int64_t F2fs::SyncMetaPages(enum page_type type, int64_t nr_to_write) {
#if 0 // porting needed
- // struct address_space *mapping = sbi->meta_inode->i_mapping;
+ // address_space *mapping = sbi->meta_inode->i_mapping;
// pgoff_t index = 0, end = LONG_MAX;
- // struct pagevec pvec;
- // long nwritten = 0;
+ // pagevec pvec;
+ // int64_t nwritten = 0;
// struct WritebackControl wbc = {
// .for_reclaim = 0,
// };
@@ -115,7 +115,7 @@
// break;
// for (i = 0; i < nr_pages; i++) {
- // struct page *page = pvec.pages[i];
+ // page *page = pvec.pages[i];
// lock_page(page);
// BUG_ON(page->mapping != mapping);
// BUG_ON(!PageDirty(page));
@@ -153,7 +153,7 @@
int F2fs::CheckOrphanSpace() {
f2fs_sb_info &sbi = SbInfo();
- unsigned int max_orphans;
+ uint32_t max_orphans;
int err = 0;
/*
@@ -173,17 +173,17 @@
void F2fs::AddOrphanInode(nid_t ino) {
f2fs_sb_info &sbi = SbInfo();
list_node_t *head, *this_node;
- struct orphan_inode_entry *new_entry = NULL, *orphan = NULL;
+ orphan_inode_entry *new_entry = nullptr, *orphan = nullptr;
mtx_lock(&sbi.orphan_inode_mutex);
head = &sbi.orphan_inode_list;
list_for_every(head, this_node) {
- orphan = containerof(this_node, struct orphan_inode_entry, list);
+ orphan = containerof(this_node, orphan_inode_entry, list);
if (orphan->ino == ino)
goto out;
if (orphan->ino > ino)
break;
- orphan = NULL;
+ orphan = nullptr;
}
retry:
#if 0 // porting needed
@@ -202,15 +202,16 @@
/* add new_oentry into list which is sorted by inode number */
if (orphan) {
- struct orphan_inode_entry *prev;
+ orphan_inode_entry *prev;
/* get previous entry */
- prev = containerof(orphan->list.prev, struct orphan_inode_entry, list);
- if (&prev->list != head)
+ prev = containerof(orphan->list.prev, orphan_inode_entry, list);
+ if (&prev->list != head) {
/* insert new orphan inode entry */
list_add(&prev->list, &new_entry->list);
- else
+ } else {
list_add(head, &new_entry->list);
+ }
} else {
list_add_tail(head, &new_entry->list);
}
@@ -222,12 +223,12 @@
void F2fs::RemoveOrphanInode(nid_t ino) {
f2fs_sb_info &sbi = SbInfo();
list_node_t *this_node, *next, *head;
- struct orphan_inode_entry *orphan;
+ orphan_inode_entry *orphan;
mtx_lock(&sbi.orphan_inode_mutex);
head = &sbi.orphan_inode_list;
list_for_every_safe(head, this_node, next) {
- orphan = containerof(this_node, struct orphan_inode_entry, list);
+ orphan = containerof(this_node, orphan_inode_entry, list);
if (orphan->ino == ino) {
list_delete(&orphan->list);
#if 0 // porting needed
@@ -266,9 +267,9 @@
for (i = 0; i < orphan_blkaddr; i++) {
Page *page = GetMetaPage(start_blk + i);
- struct f2fs_orphan_block *orphan_blk;
+ f2fs_orphan_block *orphan_blk;
- orphan_blk = (struct f2fs_orphan_block *)PageAddress(page);
+ orphan_blk = static_cast<f2fs_orphan_block *>(PageAddress(page));
for (j = 0; j < LeToCpu(orphan_blk->entry_count); j++) {
nid_t ino = LeToCpu(orphan_blk->ino[j]);
RecoverOrphanInode(ino);
@@ -284,23 +285,23 @@
void F2fs::WriteOrphanInodes(block_t start_blk) {
f2fs_sb_info &sbi = SbInfo();
list_node_t *head, *this_node, *next;
- struct f2fs_orphan_block *orphan_blk = NULL;
- Page *page = NULL;
- unsigned int nentries = 0;
- unsigned short index = 1;
- unsigned short orphan_blocks;
+ f2fs_orphan_block *orphan_blk = nullptr;
+ Page *page = nullptr;
+ uint32_t nentries = 0;
+ uint16_t index = 1;
+ uint16_t orphan_blocks;
orphan_blocks =
- (unsigned short)((sbi.n_orphans + (F2FS_ORPHANS_PER_BLOCK - 1)) / F2FS_ORPHANS_PER_BLOCK);
+ static_cast<uint16_t>((sbi.n_orphans + (F2FS_ORPHANS_PER_BLOCK - 1)) / F2FS_ORPHANS_PER_BLOCK);
mtx_lock(&sbi.orphan_inode_mutex);
head = &sbi.orphan_inode_list;
/* loop for each orphan inode entry and write them in Jornal block */
list_for_every_safe(head, this_node, next) {
- struct orphan_inode_entry *orphan;
+ orphan_inode_entry *orphan;
- orphan = containerof(this_node, struct orphan_inode_entry, list);
+ orphan = containerof(this_node, orphan_inode_entry, list);
if (nentries == F2FS_ORPHANS_PER_BLOCK) {
/*
@@ -320,13 +321,13 @@
index++;
start_blk++;
nentries = 0;
- page = NULL;
+ page = nullptr;
}
if (page)
goto page_exist;
page = GrabMetaPage(start_blk);
- orphan_blk = (struct f2fs_orphan_block *)PageAddress(page);
+ orphan_blk = static_cast<f2fs_orphan_block *>(PageAddress(page));
memset(orphan_blk, 0, sizeof(*orphan_blk));
page_exist:
orphan_blk->ino[nentries++] = CpuToLe(orphan->ino);
@@ -349,22 +350,22 @@
Page *F2fs::ValidateCheckpoint(block_t cp_addr, uint64_t *version) {
Page *cp_page_1, *cp_page_2;
- unsigned long blk_size = sbi_->blocksize;
- struct f2fs_checkpoint *cp_block;
- unsigned long long cur_version = 0, pre_version = 0;
- unsigned int crc = 0;
+ uint64_t blk_size = sbi_->blocksize;
+ f2fs_checkpoint *cp_block;
+ uint64_t cur_version = 0, pre_version = 0;
+ uint32_t crc = 0;
size_t crc_offset;
/* Read the 1st cp block in this CP pack */
cp_page_1 = GetMetaPage(cp_addr);
/* get the version number */
- cp_block = (struct f2fs_checkpoint *)PageAddress(cp_page_1);
+ cp_block = static_cast<f2fs_checkpoint *>(PageAddress(cp_page_1));
crc_offset = LeToCpu(cp_block->checksum_offset);
if (crc_offset >= blk_size)
goto invalid_cp1;
- crc = *(unsigned int *)((unsigned char *)cp_block + crc_offset);
+ crc = *reinterpret_cast<uint32_t *>(reinterpret_cast<uint8_t *>(cp_block) + crc_offset);
if (!F2fsCrcValid(crc, cp_block, crc_offset))
goto invalid_cp1;
@@ -374,12 +375,12 @@
cp_addr += LeToCpu(cp_block->cp_pack_total_block_count) - 1;
cp_page_2 = GetMetaPage(cp_addr);
- cp_block = (struct f2fs_checkpoint *)PageAddress(cp_page_2);
+ cp_block = static_cast<f2fs_checkpoint *>(PageAddress(cp_page_2));
crc_offset = LeToCpu(cp_block->checksum_offset);
if (crc_offset >= blk_size)
goto invalid_cp2;
- crc = *(unsigned int *)((unsigned char *)cp_block + crc_offset);
+ crc = *reinterpret_cast<uint32_t *>(reinterpret_cast<uint8_t *>(cp_block) + crc_offset);
if (!F2fsCrcValid(crc, cp_block, crc_offset))
goto invalid_cp2;
@@ -401,11 +402,11 @@
f2fs_checkpoint *cp_block;
f2fs_super_block &fsb = RawSb();
Page *cp1, *cp2, *cur_page;
- unsigned long blk_size = sbi_->blocksize;
+ uint64_t blk_size = sbi_->blocksize;
uint64_t cp1_version = 0, cp2_version = 0;
- unsigned long long cp_start_blk_no;
+ uint64_t cp_start_blk_no;
- sbi_->ckpt = (f2fs_checkpoint *)malloc(blk_size);
+ sbi_->ckpt = static_cast<f2fs_checkpoint *>(malloc(blk_size));
if (!sbi_->ckpt)
return -ENOMEM;
/*
@@ -420,10 +421,11 @@
cp2 = ValidateCheckpoint(cp_start_blk_no, &cp2_version);
if (cp1 && cp2) {
- if (VerAfter(cp2_version, cp1_version))
+ if (VerAfter(cp2_version, cp1_version)) {
cur_page = cp2;
- else
+ } else {
cur_page = cp1;
+ }
} else if (cp1) {
cur_page = cp1;
} else if (cp2) {
@@ -432,7 +434,7 @@
goto fail_no_cp;
}
- cp_block = (f2fs_checkpoint *)PageAddress(cur_page);
+ cp_block = static_cast<f2fs_checkpoint *>(PageAddress(cur_page));
memcpy(sbi_->ckpt, cp_block, blk_size);
#ifdef F2FS_BU_DEBUG
@@ -466,7 +468,7 @@
// void F2fs::SetDirtyDirPage(VnodeF2fs *vnode, Page *page) {
// f2fs_sb_info &sbi = SbInfo();
// list_node_t *head = &sbi.dir_inode_list;
-// struct dir_inode_entry *new_entry;
+// dir_inode_entry *new_entry;
// list_node_t *this_node;
// if (!S_ISDIR(vnode->i_mode_))
@@ -483,8 +485,8 @@
// SpinLock(&sbi.dir_inode_lock);
// list_for_every(head, this_node) {
-// struct dir_inode_entry *entry;
-// entry = containerof(this_node, struct dir_inode_entry, list);
+// dir_inode_entry *entry;
+// entry = containerof(this_node, dir_inode_entry, list);
// if (entry->vnode == vnode) {
// // kmem_cache_free(inode_entry_slab, new_entry);
// delete new_entry;
@@ -517,8 +519,8 @@
// goto out;
// list_for_every(head, this_node) {
-// struct dir_inode_entry *entry;
-// entry = containerof(this_node, struct dir_inode_entry, list);
+// dir_inode_entry *entry;
+// entry = containerof(this_node, dir_inode_entry, list);
// if (entry->vnode == vnode) {
// list_delete(&entry->list);
// // kmem_cache_free(inode_entry_slab, entry);
@@ -535,7 +537,7 @@
void F2fs::SyncDirtyDirInodes() {
f2fs_sb_info &sbi = SbInfo();
list_node_t *head = &sbi.dir_inode_list;
- struct dir_inode_entry *entry;
+ dir_inode_entry *entry;
fbl::RefPtr<VnodeF2fs> vnode;
retry:
@@ -544,8 +546,8 @@
SpinUnlock(&sbi.dir_inode_lock);
return;
}
- entry = containerof(head->next, struct dir_inode_entry, list);
- vnode.reset((VnodeF2fs *)Igrab(entry->vnode));
+ entry = containerof(head->next, dir_inode_entry, list);
+ vnode.reset(static_cast<VnodeF2fs *>(Igrab(entry->vnode)));
SpinUnlock(&sbi.dir_inode_lock);
if (vnode) {
#if 0 // porting needed
@@ -595,7 +597,7 @@
/* block all the operations */
for (t = DATA_NEW; t <= NODE_TRUNC; t++)
- mutex_lock_op(&sbi, (lock_type)t);
+ mutex_lock_op(&sbi, static_cast<lock_type>(t));
mtx_lock(&sbi.write_inode);
@@ -619,16 +621,16 @@
f2fs_sb_info &sbi = SbInfo();
int t;
for (t = NODE_WRITE; t >= RENAME; t--)
- mutex_unlock_op(&sbi, (lock_type)t);
+ mutex_unlock_op(&sbi, static_cast<lock_type>(t));
}
void F2fs::DoCheckpoint(bool is_umount) {
f2fs_sb_info &sbi = SbInfo();
- struct f2fs_checkpoint *ckpt = F2FS_CKPT(&sbi);
+ f2fs_checkpoint *ckpt = F2FS_CKPT(&sbi);
nid_t last_nid = 0;
block_t start_blk;
Page *cp_page;
- unsigned int data_sum_blocks, orphan_blocks;
+ uint32_t data_sum_blocks, orphan_blocks;
void *kaddr;
uint32_t crc32 = 0;
int i;
@@ -674,10 +676,11 @@
/* 2 cp + n data seg summary + orphan inode blocks */
data_sum_blocks = Segmgr().NpagesForSummaryFlush();
- if (data_sum_blocks < 3)
+ if (data_sum_blocks < 3) {
ckpt->ckpt_flags |= CP_COMPACT_SUM_FLAG;
- else
+ } else {
ckpt->ckpt_flags &= (~CP_COMPACT_SUM_FLAG);
+ }
orphan_blocks = (sbi.n_orphans + F2FS_ORPHANS_PER_BLOCK - 1) / F2FS_ORPHANS_PER_BLOCK;
ckpt->cp_pack_start_sum = 1 + orphan_blocks;
@@ -690,17 +693,19 @@
ckpt->ckpt_flags &= (~CP_UMOUNT_FLAG);
}
- if (sbi.n_orphans)
+ if (sbi.n_orphans) {
ckpt->ckpt_flags |= CP_ORPHAN_PRESENT_FLAG;
- else
+ } else {
ckpt->ckpt_flags &= (~CP_ORPHAN_PRESENT_FLAG);
+ }
/* update SIT/NAT bitmap */
Segmgr().GetSitBitmap(__bitmap_ptr(&sbi, SIT_BITMAP));
Nodemgr().GetNatBitmap(__bitmap_ptr(&sbi, NAT_BITMAP));
crc32 = F2fsCrc32(ckpt, LeToCpu(ckpt->checksum_offset));
- *(uint32_t *)((unsigned char *)ckpt + LeToCpu(ckpt->checksum_offset)) = CpuToLe(crc32);
+ *reinterpret_cast<uint32_t *>(reinterpret_cast<uint8_t *>(ckpt) +
+ LeToCpu(ckpt->checksum_offset)) = CpuToLe(crc32);
start_blk = __start_cp_addr(&sbi);
@@ -768,8 +773,8 @@
*/
void F2fs::WriteCheckpoint(bool blocked, bool is_umount) {
f2fs_sb_info &sbi = SbInfo();
- struct f2fs_checkpoint *ckpt = F2FS_CKPT(&sbi);
- unsigned long long ckpt_ver;
+ f2fs_checkpoint *ckpt = F2FS_CKPT(&sbi);
+ uint64_t ckpt_ver;
// TODO(unknown): Need to confirm if blocked is true
// if (!blocked) {
@@ -814,11 +819,11 @@
#if 0 // porting needed
// int F2fs::CreateCheckpointCaches() {
// orphan_entry_slab = f2fs_kmem_cache_create("f2fs_orphan_entry",
-// sizeof(struct orphan_inode_entry), NULL);
+// sizeof(orphan_inode_entry), nullptr);
// if (unlikely(!orphan_entry_slab))
// return -ENOMEM;
// inode_entry_slab = f2fs_kmem_cache_create("f2fs_dirty_dir_entry",
-// sizeof(struct dir_inode_entry), NULL);
+// sizeof(dir_inode_entry), nullptr);
// if (unlikely(!inode_entry_slab)) {
// kmem_cache_destroy(orphan_entry_slab);
// return -ENOMEM;
diff --git a/f2fs.h b/f2fs.h
index 55685f1..7b6d9da 100644
--- a/f2fs.h
+++ b/f2fs.h
@@ -147,7 +147,7 @@
#if 0 // porting needed
// int F2fsWriteMetaPages(address_space *mapping, WritebackControl *wbc);
#endif
- long SyncMetaPages(enum page_type type, long nr_to_write);
+ int64_t SyncMetaPages(enum page_type type, long nr_to_write);
#if 0 // porting needed
// int F2fsSetMetaPageDirty(Page *page);
#endif