Skip to content

Commit 49d2f65

Browse files
chaseyujfvogel
authored andcommitted
Revert "f2fs: rebuild nat_bits during umount"
[ Upstream commit 19426c4988aa85298c1b4caf2889d37ec5c80fea ] This reverts commit 94c821f. It reports that there is potential corruption in node footer, the most suspious feature is nat_bits, let's revert recovery related code. Signed-off-by: Chao Yu <[email protected]> Signed-off-by: Jaegeuk Kim <[email protected]> Signed-off-by: Sasha Levin <[email protected]> (cherry picked from commit aa39d45071ec18fc9592e9458556d7b46187eab8) Signed-off-by: Jack Vogel <[email protected]>
1 parent 2df15be commit 49d2f65

File tree

3 files changed

+59
-95
lines changed

3 files changed

+59
-95
lines changed

fs/f2fs/checkpoint.c

Lines changed: 6 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1344,21 +1344,13 @@ static void update_ckpt_flags(struct f2fs_sb_info *sbi, struct cp_control *cpc)
13441344
struct f2fs_checkpoint *ckpt = F2FS_CKPT(sbi);
13451345
unsigned long flags;
13461346

1347-
if (cpc->reason & CP_UMOUNT) {
1348-
if (le32_to_cpu(ckpt->cp_pack_total_block_count) +
1349-
NM_I(sbi)->nat_bits_blocks > BLKS_PER_SEG(sbi)) {
1350-
clear_ckpt_flags(sbi, CP_NAT_BITS_FLAG);
1351-
f2fs_notice(sbi, "Disable nat_bits due to no space");
1352-
} else if (!is_set_ckpt_flags(sbi, CP_NAT_BITS_FLAG) &&
1353-
f2fs_nat_bitmap_enabled(sbi)) {
1354-
f2fs_enable_nat_bits(sbi);
1355-
set_ckpt_flags(sbi, CP_NAT_BITS_FLAG);
1356-
f2fs_notice(sbi, "Rebuild and enable nat_bits");
1357-
}
1358-
}
1359-
13601347
spin_lock_irqsave(&sbi->cp_lock, flags);
13611348

1349+
if ((cpc->reason & CP_UMOUNT) &&
1350+
le32_to_cpu(ckpt->cp_pack_total_block_count) >
1351+
sbi->blocks_per_seg - NM_I(sbi)->nat_bits_blocks)
1352+
disable_nat_bits(sbi, false);
1353+
13621354
if (cpc->reason & CP_TRIMMED)
13631355
__set_ckpt_flags(ckpt, CP_TRIMMED_FLAG);
13641356
else
@@ -1541,8 +1533,7 @@ static int do_checkpoint(struct f2fs_sb_info *sbi, struct cp_control *cpc)
15411533
start_blk = __start_cp_next_addr(sbi);
15421534

15431535
/* write nat bits */
1544-
if ((cpc->reason & CP_UMOUNT) &&
1545-
is_set_ckpt_flags(sbi, CP_NAT_BITS_FLAG)) {
1536+
if (enabled_nat_bits(sbi, cpc)) {
15461537
__u64 cp_ver = cur_cp_version(ckpt);
15471538
block_t blk;
15481539

fs/f2fs/f2fs.h

Lines changed: 30 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2231,6 +2231,36 @@ static inline void f2fs_up_write(struct f2fs_rwsem *sem)
22312231
#endif
22322232
}
22332233

2234+
static inline void disable_nat_bits(struct f2fs_sb_info *sbi, bool lock)
2235+
{
2236+
unsigned long flags;
2237+
unsigned char *nat_bits;
2238+
2239+
/*
2240+
* In order to re-enable nat_bits we need to call fsck.f2fs by
2241+
* set_sbi_flag(sbi, SBI_NEED_FSCK). But it may give huge cost,
2242+
* so let's rely on regular fsck or unclean shutdown.
2243+
*/
2244+
2245+
if (lock)
2246+
spin_lock_irqsave(&sbi->cp_lock, flags);
2247+
__clear_ckpt_flags(F2FS_CKPT(sbi), CP_NAT_BITS_FLAG);
2248+
nat_bits = NM_I(sbi)->nat_bits;
2249+
NM_I(sbi)->nat_bits = NULL;
2250+
if (lock)
2251+
spin_unlock_irqrestore(&sbi->cp_lock, flags);
2252+
2253+
kvfree(nat_bits);
2254+
}
2255+
2256+
static inline bool enabled_nat_bits(struct f2fs_sb_info *sbi,
2257+
struct cp_control *cpc)
2258+
{
2259+
bool set = is_set_ckpt_flags(sbi, CP_NAT_BITS_FLAG);
2260+
2261+
return (cpc) ? (cpc->reason & CP_UMOUNT) && set : set;
2262+
}
2263+
22342264
static inline void f2fs_lock_op(struct f2fs_sb_info *sbi)
22352265
{
22362266
f2fs_down_read(&sbi->cp_rwsem);
@@ -3671,7 +3701,6 @@ int f2fs_truncate_inode_blocks(struct inode *inode, pgoff_t from);
36713701
int f2fs_truncate_xattr_node(struct inode *inode);
36723702
int f2fs_wait_on_node_pages_writeback(struct f2fs_sb_info *sbi,
36733703
unsigned int seq_id);
3674-
bool f2fs_nat_bitmap_enabled(struct f2fs_sb_info *sbi);
36753704
int f2fs_remove_inode_page(struct inode *inode);
36763705
struct page *f2fs_new_inode_page(struct inode *inode);
36773706
struct page *f2fs_new_node_page(struct dnode_of_data *dn, unsigned int ofs);
@@ -3696,7 +3725,6 @@ int f2fs_recover_xattr_data(struct inode *inode, struct page *page);
36963725
int f2fs_recover_inode_page(struct f2fs_sb_info *sbi, struct page *page);
36973726
int f2fs_restore_node_summary(struct f2fs_sb_info *sbi,
36983727
unsigned int segno, struct f2fs_summary_block *sum);
3699-
void f2fs_enable_nat_bits(struct f2fs_sb_info *sbi);
37003728
int f2fs_flush_nat_entries(struct f2fs_sb_info *sbi, struct cp_control *cpc);
37013729
int f2fs_build_node_manager(struct f2fs_sb_info *sbi);
37023730
void f2fs_destroy_node_manager(struct f2fs_sb_info *sbi);

fs/f2fs/node.c

Lines changed: 23 additions & 78 deletions
Original file line numberDiff line numberDiff line change
@@ -2277,24 +2277,6 @@ static void __move_free_nid(struct f2fs_sb_info *sbi, struct free_nid *i,
22772277
}
22782278
}
22792279

2280-
bool f2fs_nat_bitmap_enabled(struct f2fs_sb_info *sbi)
2281-
{
2282-
struct f2fs_nm_info *nm_i = NM_I(sbi);
2283-
unsigned int i;
2284-
bool ret = true;
2285-
2286-
f2fs_down_read(&nm_i->nat_tree_lock);
2287-
for (i = 0; i < nm_i->nat_blocks; i++) {
2288-
if (!test_bit_le(i, nm_i->nat_block_bitmap)) {
2289-
ret = false;
2290-
break;
2291-
}
2292-
}
2293-
f2fs_up_read(&nm_i->nat_tree_lock);
2294-
2295-
return ret;
2296-
}
2297-
22982280
static void update_free_nid_bitmap(struct f2fs_sb_info *sbi, nid_t nid,
22992281
bool set, bool build)
23002282
{
@@ -2973,23 +2955,7 @@ static void __adjust_nat_entry_set(struct nat_entry_set *nes,
29732955
list_add_tail(&nes->set_list, head);
29742956
}
29752957

2976-
static void __update_nat_bits(struct f2fs_nm_info *nm_i, unsigned int nat_ofs,
2977-
unsigned int valid)
2978-
{
2979-
if (valid == 0) {
2980-
__set_bit_le(nat_ofs, nm_i->empty_nat_bits);
2981-
__clear_bit_le(nat_ofs, nm_i->full_nat_bits);
2982-
return;
2983-
}
2984-
2985-
__clear_bit_le(nat_ofs, nm_i->empty_nat_bits);
2986-
if (valid == NAT_ENTRY_PER_BLOCK)
2987-
__set_bit_le(nat_ofs, nm_i->full_nat_bits);
2988-
else
2989-
__clear_bit_le(nat_ofs, nm_i->full_nat_bits);
2990-
}
2991-
2992-
static void update_nat_bits(struct f2fs_sb_info *sbi, nid_t start_nid,
2958+
static void __update_nat_bits(struct f2fs_sb_info *sbi, nid_t start_nid,
29932959
struct page *page)
29942960
{
29952961
struct f2fs_nm_info *nm_i = NM_I(sbi);
@@ -2998,7 +2964,7 @@ static void update_nat_bits(struct f2fs_sb_info *sbi, nid_t start_nid,
29982964
int valid = 0;
29992965
int i = 0;
30002966

3001-
if (!is_set_ckpt_flags(sbi, CP_NAT_BITS_FLAG))
2967+
if (!enabled_nat_bits(sbi, NULL))
30022968
return;
30032969

30042970
if (nat_index == 0) {
@@ -3009,36 +2975,17 @@ static void update_nat_bits(struct f2fs_sb_info *sbi, nid_t start_nid,
30092975
if (le32_to_cpu(nat_blk->entries[i].block_addr) != NULL_ADDR)
30102976
valid++;
30112977
}
3012-
3013-
__update_nat_bits(nm_i, nat_index, valid);
3014-
}
3015-
3016-
void f2fs_enable_nat_bits(struct f2fs_sb_info *sbi)
3017-
{
3018-
struct f2fs_nm_info *nm_i = NM_I(sbi);
3019-
unsigned int nat_ofs;
3020-
3021-
f2fs_down_read(&nm_i->nat_tree_lock);
3022-
3023-
for (nat_ofs = 0; nat_ofs < nm_i->nat_blocks; nat_ofs++) {
3024-
unsigned int valid = 0, nid_ofs = 0;
3025-
3026-
/* handle nid zero due to it should never be used */
3027-
if (unlikely(nat_ofs == 0)) {
3028-
valid = 1;
3029-
nid_ofs = 1;
3030-
}
3031-
3032-
for (; nid_ofs < NAT_ENTRY_PER_BLOCK; nid_ofs++) {
3033-
if (!test_bit_le(nid_ofs,
3034-
nm_i->free_nid_bitmap[nat_ofs]))
3035-
valid++;
3036-
}
3037-
3038-
__update_nat_bits(nm_i, nat_ofs, valid);
2978+
if (valid == 0) {
2979+
__set_bit_le(nat_index, nm_i->empty_nat_bits);
2980+
__clear_bit_le(nat_index, nm_i->full_nat_bits);
2981+
return;
30392982
}
30402983

3041-
f2fs_up_read(&nm_i->nat_tree_lock);
2984+
__clear_bit_le(nat_index, nm_i->empty_nat_bits);
2985+
if (valid == NAT_ENTRY_PER_BLOCK)
2986+
__set_bit_le(nat_index, nm_i->full_nat_bits);
2987+
else
2988+
__clear_bit_le(nat_index, nm_i->full_nat_bits);
30422989
}
30432990

30442991
static int __flush_nat_entry_set(struct f2fs_sb_info *sbi,
@@ -3057,7 +3004,7 @@ static int __flush_nat_entry_set(struct f2fs_sb_info *sbi,
30573004
* #1, flush nat entries to journal in current hot data summary block.
30583005
* #2, flush nat entries to nat page.
30593006
*/
3060-
if ((cpc->reason & CP_UMOUNT) ||
3007+
if (enabled_nat_bits(sbi, cpc) ||
30613008
!__has_cursum_space(journal, set->entry_cnt, NAT_JOURNAL))
30623009
to_journal = false;
30633010

@@ -3104,7 +3051,7 @@ static int __flush_nat_entry_set(struct f2fs_sb_info *sbi,
31043051
if (to_journal) {
31053052
up_write(&curseg->journal_rwsem);
31063053
} else {
3107-
update_nat_bits(sbi, start_nid, page);
3054+
__update_nat_bits(sbi, start_nid, page);
31083055
f2fs_put_page(page, 1);
31093056
}
31103057

@@ -3135,7 +3082,7 @@ int f2fs_flush_nat_entries(struct f2fs_sb_info *sbi, struct cp_control *cpc)
31353082
* during unmount, let's flush nat_bits before checking
31363083
* nat_cnt[DIRTY_NAT].
31373084
*/
3138-
if (cpc->reason & CP_UMOUNT) {
3085+
if (enabled_nat_bits(sbi, cpc)) {
31393086
f2fs_down_write(&nm_i->nat_tree_lock);
31403087
remove_nats_in_journal(sbi);
31413088
f2fs_up_write(&nm_i->nat_tree_lock);
@@ -3151,7 +3098,7 @@ int f2fs_flush_nat_entries(struct f2fs_sb_info *sbi, struct cp_control *cpc)
31513098
* entries, remove all entries from journal and merge them
31523099
* into nat entry set.
31533100
*/
3154-
if (cpc->reason & CP_UMOUNT ||
3101+
if (enabled_nat_bits(sbi, cpc) ||
31553102
!__has_cursum_space(journal,
31563103
nm_i->nat_cnt[DIRTY_NAT], NAT_JOURNAL))
31573104
remove_nats_in_journal(sbi);
@@ -3188,18 +3135,15 @@ static int __get_nat_bitmaps(struct f2fs_sb_info *sbi)
31883135
__u64 cp_ver = cur_cp_version(ckpt);
31893136
block_t nat_bits_addr;
31903137

3138+
if (!enabled_nat_bits(sbi, NULL))
3139+
return 0;
3140+
31913141
nm_i->nat_bits_blocks = F2FS_BLK_ALIGN((nat_bits_bytes << 1) + 8);
31923142
nm_i->nat_bits = f2fs_kvzalloc(sbi,
31933143
F2FS_BLK_TO_BYTES(nm_i->nat_bits_blocks), GFP_KERNEL);
31943144
if (!nm_i->nat_bits)
31953145
return -ENOMEM;
31963146

3197-
nm_i->full_nat_bits = nm_i->nat_bits + 8;
3198-
nm_i->empty_nat_bits = nm_i->full_nat_bits + nat_bits_bytes;
3199-
3200-
if (!is_set_ckpt_flags(sbi, CP_NAT_BITS_FLAG))
3201-
return 0;
3202-
32033147
nat_bits_addr = __start_cp_addr(sbi) + BLKS_PER_SEG(sbi) -
32043148
nm_i->nat_bits_blocks;
32053149
for (i = 0; i < nm_i->nat_bits_blocks; i++) {
@@ -3216,12 +3160,13 @@ static int __get_nat_bitmaps(struct f2fs_sb_info *sbi)
32163160

32173161
cp_ver |= (cur_cp_crc(ckpt) << 32);
32183162
if (cpu_to_le64(cp_ver) != *(__le64 *)nm_i->nat_bits) {
3219-
clear_ckpt_flags(sbi, CP_NAT_BITS_FLAG);
3220-
f2fs_notice(sbi, "Disable nat_bits due to incorrect cp_ver (%llu, %llu)",
3221-
cp_ver, le64_to_cpu(*(__le64 *)nm_i->nat_bits));
3163+
disable_nat_bits(sbi, true);
32223164
return 0;
32233165
}
32243166

3167+
nm_i->full_nat_bits = nm_i->nat_bits + 8;
3168+
nm_i->empty_nat_bits = nm_i->full_nat_bits + nat_bits_bytes;
3169+
32253170
f2fs_notice(sbi, "Found nat_bits in checkpoint");
32263171
return 0;
32273172
}
@@ -3232,7 +3177,7 @@ static inline void load_free_nid_bitmap(struct f2fs_sb_info *sbi)
32323177
unsigned int i = 0;
32333178
nid_t nid, last_nid;
32343179

3235-
if (!is_set_ckpt_flags(sbi, CP_NAT_BITS_FLAG))
3180+
if (!enabled_nat_bits(sbi, NULL))
32363181
return;
32373182

32383183
for (i = 0; i < nm_i->nat_blocks; i++) {

0 commit comments

Comments
 (0)