Skip to content

Commit 9a4ffdf

Browse files
chaseyuJaegeuk Kim
authored andcommitted
f2fs: obsolete ALLOC_NID_LIST list
As Fan Li reported, there is no user traversing nid_list[ALLOC_NID_LIST] which is used for tracking preallocated nids. Let's drop it, and only track preallocated nids in free_nid_root radix-tree. Reported-by: Fan Li <fanofcode.li@samsung.com> Signed-off-by: Chao Yu <yuchao0@huawei.com> Signed-off-by: Jaegeuk Kim <jaegeuk@kernel.org>
1 parent 71ad682 commit 9a4ffdf

5 files changed

Lines changed: 64 additions & 73 deletions

File tree

‎fs/f2fs/debug.c‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -98,9 +98,9 @@ static void update_general_status(struct f2fs_sb_info *sbi)
9898
si->dirty_nats = NM_I(sbi)->dirty_nat_cnt;
9999
si->sits = MAIN_SEGS(sbi);
100100
si->dirty_sits = SIT_I(sbi)->dirty_sentries;
101-
si->free_nids = NM_I(sbi)->nid_cnt[FREE_NID_LIST];
101+
si->free_nids = NM_I(sbi)->nid_cnt[FREE_NID];
102102
si->avail_nids = NM_I(sbi)->available_nids;
103-
si->alloc_nids = NM_I(sbi)->nid_cnt[ALLOC_NID_LIST];
103+
si->alloc_nids = NM_I(sbi)->nid_cnt[PREALLOC_NID];
104104
si->bg_gc = sbi->bg_gc;
105105
si->util_free = (int)(free_user_blocks(sbi) >> sbi->log_blocks_per_seg)
106106
* 100 / (int)(sbi->user_block_count >> sbi->log_blocks_per_seg)
@@ -233,8 +233,8 @@ static void update_mem_info(struct f2fs_sb_info *sbi)
233233
}
234234

235235
/* free nids */
236-
si->cache_mem += (NM_I(sbi)->nid_cnt[FREE_NID_LIST] +
237-
NM_I(sbi)->nid_cnt[ALLOC_NID_LIST]) *
236+
si->cache_mem += (NM_I(sbi)->nid_cnt[FREE_NID] +
237+
NM_I(sbi)->nid_cnt[PREALLOC_NID]) *
238238
sizeof(struct free_nid);
239239
si->cache_mem += NM_I(sbi)->nat_cnt * sizeof(struct nat_entry);
240240
si->cache_mem += NM_I(sbi)->dirty_nat_cnt *

‎fs/f2fs/f2fs.h‎

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -666,10 +666,13 @@ static inline void __try_update_largest_extent(struct inode *inode,
666666
}
667667
}
668668

669-
enum nid_list {
670-
FREE_NID_LIST,
671-
ALLOC_NID_LIST,
672-
MAX_NID_LIST,
669+
/*
670+
* For free nid management
671+
*/
672+
enum nid_state {
673+
FREE_NID, /* newly added to free nid list */
674+
PREALLOC_NID, /* it is preallocated */
675+
MAX_NID_STATE,
673676
};
674677

675678
struct f2fs_nm_info {
@@ -692,8 +695,8 @@ struct f2fs_nm_info {
692695

693696
/* free node ids management */
694697
struct radix_tree_root free_nid_root;/* root of the free_nid cache */
695-
struct list_head nid_list[MAX_NID_LIST];/* lists for free nids */
696-
unsigned int nid_cnt[MAX_NID_LIST]; /* the number of free node id */
698+
struct list_head free_nid_list; /* list for free nids excluding preallocated nids */
699+
unsigned int nid_cnt[MAX_NID_STATE]; /* the number of free node id */
697700
spinlock_t nid_list_lock; /* protect nid lists ops */
698701
struct mutex build_lock; /* lock for build free nids */
699702
unsigned char (*free_nid_bitmap)[NAT_ENTRY_BITMAP_SIZE];

‎fs/f2fs/node.c‎

Lines changed: 47 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ bool available_free_memory(struct f2fs_sb_info *sbi, int type)
4646
* give 25%, 25%, 50%, 50%, 50% memory for each components respectively
4747
*/
4848
if (type == FREE_NIDS) {
49-
mem_size = (nm_i->nid_cnt[FREE_NID_LIST] *
49+
mem_size = (nm_i->nid_cnt[FREE_NID] *
5050
sizeof(struct free_nid)) >> PAGE_SHIFT;
5151
res = mem_size < ((avail_ram * nm_i->ram_thresh / 100) >> 2);
5252
} else if (type == NAT_ENTRIES) {
@@ -1757,8 +1757,8 @@ static struct free_nid *__lookup_free_nid_list(struct f2fs_nm_info *nm_i,
17571757
return radix_tree_lookup(&nm_i->free_nid_root, n);
17581758
}
17591759

1760-
static int __insert_nid_to_list(struct f2fs_sb_info *sbi,
1761-
struct free_nid *i, enum nid_list list, bool new)
1760+
static int __insert_free_nid(struct f2fs_sb_info *sbi,
1761+
struct free_nid *i, enum nid_state state, bool new)
17621762
{
17631763
struct f2fs_nm_info *nm_i = NM_I(sbi);
17641764

@@ -1768,22 +1768,22 @@ static int __insert_nid_to_list(struct f2fs_sb_info *sbi,
17681768
return err;
17691769
}
17701770

1771-
f2fs_bug_on(sbi, list == FREE_NID_LIST ? i->state != NID_NEW :
1772-
i->state != NID_ALLOC);
1773-
nm_i->nid_cnt[list]++;
1774-
list_add_tail(&i->list, &nm_i->nid_list[list]);
1771+
f2fs_bug_on(sbi, state != i->state);
1772+
nm_i->nid_cnt[state]++;
1773+
if (state == FREE_NID)
1774+
list_add_tail(&i->list, &nm_i->free_nid_list);
17751775
return 0;
17761776
}
17771777

1778-
static void __remove_nid_from_list(struct f2fs_sb_info *sbi,
1779-
struct free_nid *i, enum nid_list list, bool reuse)
1778+
static void __remove_free_nid(struct f2fs_sb_info *sbi,
1779+
struct free_nid *i, enum nid_state state, bool reuse)
17801780
{
17811781
struct f2fs_nm_info *nm_i = NM_I(sbi);
17821782

1783-
f2fs_bug_on(sbi, list == FREE_NID_LIST ? i->state != NID_NEW :
1784-
i->state != NID_ALLOC);
1785-
nm_i->nid_cnt[list]--;
1786-
list_del(&i->list);
1783+
f2fs_bug_on(sbi, state != i->state);
1784+
nm_i->nid_cnt[state]--;
1785+
if (state == FREE_NID)
1786+
list_del(&i->list);
17871787
if (!reuse)
17881788
radix_tree_delete(&nm_i->free_nid_root, i->nid);
17891789
}
@@ -1803,7 +1803,7 @@ static bool add_free_nid(struct f2fs_sb_info *sbi, nid_t nid, bool build)
18031803

18041804
i = f2fs_kmem_cache_alloc(free_nid_slab, GFP_NOFS);
18051805
i->nid = nid;
1806-
i->state = NID_NEW;
1806+
i->state = FREE_NID;
18071807

18081808
if (radix_tree_preload(GFP_NOFS))
18091809
goto err;
@@ -1816,7 +1816,7 @@ static bool add_free_nid(struct f2fs_sb_info *sbi, nid_t nid, bool build)
18161816
* - f2fs_create
18171817
* - f2fs_new_inode
18181818
* - alloc_nid
1819-
* - __insert_nid_to_list(ALLOC_NID_LIST)
1819+
* - __insert_nid_to_list(PREALLOC_NID)
18201820
* - f2fs_balance_fs_bg
18211821
* - build_free_nids
18221822
* - __build_free_nids
@@ -1829,8 +1829,8 @@ static bool add_free_nid(struct f2fs_sb_info *sbi, nid_t nid, bool build)
18291829
* - new_node_page
18301830
* - set_node_addr
18311831
* - alloc_nid_done
1832-
* - __remove_nid_from_list(ALLOC_NID_LIST)
1833-
* - __insert_nid_to_list(FREE_NID_LIST)
1832+
* - __remove_nid_from_list(PREALLOC_NID)
1833+
* - __insert_nid_to_list(FREE_NID)
18341834
*/
18351835
ne = __lookup_nat_cache(nm_i, nid);
18361836
if (ne && (!get_nat_flag(ne, IS_CHECKPOINTED) ||
@@ -1839,13 +1839,13 @@ static bool add_free_nid(struct f2fs_sb_info *sbi, nid_t nid, bool build)
18391839

18401840
e = __lookup_free_nid_list(nm_i, nid);
18411841
if (e) {
1842-
if (e->state == NID_NEW)
1842+
if (e->state == FREE_NID)
18431843
ret = true;
18441844
goto err_out;
18451845
}
18461846
}
18471847
ret = true;
1848-
err = __insert_nid_to_list(sbi, i, FREE_NID_LIST, true);
1848+
err = __insert_free_nid(sbi, i, FREE_NID, true);
18491849
err_out:
18501850
spin_unlock(&nm_i->nid_list_lock);
18511851
radix_tree_preload_end();
@@ -1863,8 +1863,8 @@ static void remove_free_nid(struct f2fs_sb_info *sbi, nid_t nid)
18631863

18641864
spin_lock(&nm_i->nid_list_lock);
18651865
i = __lookup_free_nid_list(nm_i, nid);
1866-
if (i && i->state == NID_NEW) {
1867-
__remove_nid_from_list(sbi, i, FREE_NID_LIST, false);
1866+
if (i && i->state == FREE_NID) {
1867+
__remove_free_nid(sbi, i, FREE_NID, false);
18681868
need_free = true;
18691869
}
18701870
spin_unlock(&nm_i->nid_list_lock);
@@ -1949,7 +1949,7 @@ static void scan_free_nid_bits(struct f2fs_sb_info *sbi)
19491949
nid = i * NAT_ENTRY_PER_BLOCK + idx;
19501950
add_free_nid(sbi, nid, true);
19511951

1952-
if (nm_i->nid_cnt[FREE_NID_LIST] >= MAX_FREE_NIDS)
1952+
if (nm_i->nid_cnt[FREE_NID] >= MAX_FREE_NIDS)
19531953
goto out;
19541954
}
19551955
}
@@ -1982,7 +1982,7 @@ static void __build_free_nids(struct f2fs_sb_info *sbi, bool sync, bool mount)
19821982
nid = 0;
19831983

19841984
/* Enough entries */
1985-
if (nm_i->nid_cnt[FREE_NID_LIST] >= NAT_ENTRY_PER_BLOCK)
1985+
if (nm_i->nid_cnt[FREE_NID] >= NAT_ENTRY_PER_BLOCK)
19861986
return;
19871987

19881988
if (!sync && !available_free_memory(sbi, FREE_NIDS))
@@ -1992,7 +1992,7 @@ static void __build_free_nids(struct f2fs_sb_info *sbi, bool sync, bool mount)
19921992
/* try to find free nids in free_nid_bitmap */
19931993
scan_free_nid_bits(sbi);
19941994

1995-
if (nm_i->nid_cnt[FREE_NID_LIST])
1995+
if (nm_i->nid_cnt[FREE_NID])
19961996
return;
19971997
}
19981998

@@ -2069,15 +2069,15 @@ bool alloc_nid(struct f2fs_sb_info *sbi, nid_t *nid)
20692069
}
20702070

20712071
/* We should not use stale free nids created by build_free_nids */
2072-
if (nm_i->nid_cnt[FREE_NID_LIST] && !on_build_free_nids(nm_i)) {
2073-
f2fs_bug_on(sbi, list_empty(&nm_i->nid_list[FREE_NID_LIST]));
2074-
i = list_first_entry(&nm_i->nid_list[FREE_NID_LIST],
2072+
if (nm_i->nid_cnt[FREE_NID] && !on_build_free_nids(nm_i)) {
2073+
f2fs_bug_on(sbi, list_empty(&nm_i->free_nid_list));
2074+
i = list_first_entry(&nm_i->free_nid_list,
20752075
struct free_nid, list);
20762076
*nid = i->nid;
20772077

2078-
__remove_nid_from_list(sbi, i, FREE_NID_LIST, true);
2079-
i->state = NID_ALLOC;
2080-
__insert_nid_to_list(sbi, i, ALLOC_NID_LIST, false);
2078+
__remove_free_nid(sbi, i, FREE_NID, true);
2079+
i->state = PREALLOC_NID;
2080+
__insert_free_nid(sbi, i, PREALLOC_NID, false);
20812081
nm_i->available_nids--;
20822082

20832083
update_free_nid_bitmap(sbi, *nid, false, false);
@@ -2103,7 +2103,7 @@ void alloc_nid_done(struct f2fs_sb_info *sbi, nid_t nid)
21032103
spin_lock(&nm_i->nid_list_lock);
21042104
i = __lookup_free_nid_list(nm_i, nid);
21052105
f2fs_bug_on(sbi, !i);
2106-
__remove_nid_from_list(sbi, i, ALLOC_NID_LIST, false);
2106+
__remove_free_nid(sbi, i, PREALLOC_NID, false);
21072107
spin_unlock(&nm_i->nid_list_lock);
21082108

21092109
kmem_cache_free(free_nid_slab, i);
@@ -2126,12 +2126,12 @@ void alloc_nid_failed(struct f2fs_sb_info *sbi, nid_t nid)
21262126
f2fs_bug_on(sbi, !i);
21272127

21282128
if (!available_free_memory(sbi, FREE_NIDS)) {
2129-
__remove_nid_from_list(sbi, i, ALLOC_NID_LIST, false);
2129+
__remove_free_nid(sbi, i, PREALLOC_NID, false);
21302130
need_free = true;
21312131
} else {
2132-
__remove_nid_from_list(sbi, i, ALLOC_NID_LIST, true);
2133-
i->state = NID_NEW;
2134-
__insert_nid_to_list(sbi, i, FREE_NID_LIST, false);
2132+
__remove_free_nid(sbi, i, PREALLOC_NID, true);
2133+
i->state = FREE_NID;
2134+
__insert_free_nid(sbi, i, FREE_NID, false);
21352135
}
21362136

21372137
nm_i->available_nids++;
@@ -2150,20 +2150,19 @@ int try_to_free_nids(struct f2fs_sb_info *sbi, int nr_shrink)
21502150
struct free_nid *i, *next;
21512151
int nr = nr_shrink;
21522152

2153-
if (nm_i->nid_cnt[FREE_NID_LIST] <= MAX_FREE_NIDS)
2153+
if (nm_i->nid_cnt[FREE_NID] <= MAX_FREE_NIDS)
21542154
return 0;
21552155

21562156
if (!mutex_trylock(&nm_i->build_lock))
21572157
return 0;
21582158

21592159
spin_lock(&nm_i->nid_list_lock);
2160-
list_for_each_entry_safe(i, next, &nm_i->nid_list[FREE_NID_LIST],
2161-
list) {
2160+
list_for_each_entry_safe(i, next, &nm_i->free_nid_list, list) {
21622161
if (nr_shrink <= 0 ||
2163-
nm_i->nid_cnt[FREE_NID_LIST] <= MAX_FREE_NIDS)
2162+
nm_i->nid_cnt[FREE_NID] <= MAX_FREE_NIDS)
21642163
break;
21652164

2166-
__remove_nid_from_list(sbi, i, FREE_NID_LIST, false);
2165+
__remove_free_nid(sbi, i, FREE_NID, false);
21672166
kmem_cache_free(free_nid_slab, i);
21682167
nr_shrink--;
21692168
}
@@ -2635,16 +2634,15 @@ static int init_node_manager(struct f2fs_sb_info *sbi)
26352634
/* not used nids: 0, node, meta, (and root counted as valid node) */
26362635
nm_i->available_nids = nm_i->max_nid - sbi->total_valid_node_count -
26372636
F2FS_RESERVED_NODE_NUM;
2638-
nm_i->nid_cnt[FREE_NID_LIST] = 0;
2639-
nm_i->nid_cnt[ALLOC_NID_LIST] = 0;
2637+
nm_i->nid_cnt[FREE_NID] = 0;
2638+
nm_i->nid_cnt[PREALLOC_NID] = 0;
26402639
nm_i->nat_cnt = 0;
26412640
nm_i->ram_thresh = DEF_RAM_THRESHOLD;
26422641
nm_i->ra_nid_pages = DEF_RA_NID_PAGES;
26432642
nm_i->dirty_nats_ratio = DEF_DIRTY_NAT_RATIO_THRESHOLD;
26442643

26452644
INIT_RADIX_TREE(&nm_i->free_nid_root, GFP_ATOMIC);
2646-
INIT_LIST_HEAD(&nm_i->nid_list[FREE_NID_LIST]);
2647-
INIT_LIST_HEAD(&nm_i->nid_list[ALLOC_NID_LIST]);
2645+
INIT_LIST_HEAD(&nm_i->free_nid_list);
26482646
INIT_RADIX_TREE(&nm_i->nat_root, GFP_NOIO);
26492647
INIT_RADIX_TREE(&nm_i->nat_set_root, GFP_NOIO);
26502648
INIT_LIST_HEAD(&nm_i->nat_entries);
@@ -2736,16 +2734,15 @@ void destroy_node_manager(struct f2fs_sb_info *sbi)
27362734

27372735
/* destroy free nid list */
27382736
spin_lock(&nm_i->nid_list_lock);
2739-
list_for_each_entry_safe(i, next_i, &nm_i->nid_list[FREE_NID_LIST],
2740-
list) {
2741-
__remove_nid_from_list(sbi, i, FREE_NID_LIST, false);
2737+
list_for_each_entry_safe(i, next_i, &nm_i->free_nid_list, list) {
2738+
__remove_free_nid(sbi, i, FREE_NID, false);
27422739
spin_unlock(&nm_i->nid_list_lock);
27432740
kmem_cache_free(free_nid_slab, i);
27442741
spin_lock(&nm_i->nid_list_lock);
27452742
}
2746-
f2fs_bug_on(sbi, nm_i->nid_cnt[FREE_NID_LIST]);
2747-
f2fs_bug_on(sbi, nm_i->nid_cnt[ALLOC_NID_LIST]);
2748-
f2fs_bug_on(sbi, !list_empty(&nm_i->nid_list[ALLOC_NID_LIST]));
2743+
f2fs_bug_on(sbi, nm_i->nid_cnt[FREE_NID]);
2744+
f2fs_bug_on(sbi, nm_i->nid_cnt[PREALLOC_NID]);
2745+
f2fs_bug_on(sbi, !list_empty(&nm_i->free_nid_list));
27492746
spin_unlock(&nm_i->nid_list_lock);
27502747

27512748
/* destroy nat cache */

‎fs/f2fs/node.h‎

Lines changed: 3 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -150,18 +150,10 @@ struct nat_entry_set {
150150
unsigned int entry_cnt; /* the # of nat entries in set */
151151
};
152152

153-
/*
154-
* For free nid mangement
155-
*/
156-
enum nid_state {
157-
NID_NEW, /* newly added to free nid list */
158-
NID_ALLOC /* it is allocated */
159-
};
160-
161153
struct free_nid {
162154
struct list_head list; /* for free node id list */
163155
nid_t nid; /* node id */
164-
int state; /* in use or not: NID_NEW or NID_ALLOC */
156+
int state; /* in use or not: FREE_NID or PREALLOC_NID */
165157
};
166158

167159
static inline void next_free_nid(struct f2fs_sb_info *sbi, nid_t *nid)
@@ -170,12 +162,11 @@ static inline void next_free_nid(struct f2fs_sb_info *sbi, nid_t *nid)
170162
struct free_nid *fnid;
171163

172164
spin_lock(&nm_i->nid_list_lock);
173-
if (nm_i->nid_cnt[FREE_NID_LIST] <= 0) {
165+
if (nm_i->nid_cnt[FREE_NID] <= 0) {
174166
spin_unlock(&nm_i->nid_list_lock);
175167
return;
176168
}
177-
fnid = list_first_entry(&nm_i->nid_list[FREE_NID_LIST],
178-
struct free_nid, list);
169+
fnid = list_first_entry(&nm_i->free_nid_list, struct free_nid, list);
179170
*nid = fnid->nid;
180171
spin_unlock(&nm_i->nid_list_lock);
181172
}

‎fs/f2fs/shrinker.c‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ static unsigned long __count_nat_entries(struct f2fs_sb_info *sbi)
2828

2929
static unsigned long __count_free_nids(struct f2fs_sb_info *sbi)
3030
{
31-
long count = NM_I(sbi)->nid_cnt[FREE_NID_LIST] - MAX_FREE_NIDS;
31+
long count = NM_I(sbi)->nid_cnt[FREE_NID] - MAX_FREE_NIDS;
3232

3333
return count > 0 ? count : 0;
3434
}

0 commit comments

Comments
 (0)