1 // SPDX-License-Identifier: GPL-2.0
5 * Copyright (c) 2012 Samsung Electronics Co., Ltd.
6 * http://www.samsung.com/
9 #include <linux/f2fs_fs.h>
10 #include <linux/mpage.h>
11 #include <linux/sched/mm.h>
12 #include <linux/blkdev.h>
13 #include <linux/pagevec.h>
14 #include <linux/swap.h>
21 #include <trace/events/f2fs.h>
23 #define on_f2fs_build_free_nids(nmi) mutex_is_locked(&(nm_i)->build_lock)
25 static struct kmem_cache *nat_entry_slab;
26 static struct kmem_cache *free_nid_slab;
27 static struct kmem_cache *nat_entry_set_slab;
28 static struct kmem_cache *fsync_node_entry_slab;
31 * Check whether the given nid is within node id range.
33 int f2fs_check_nid_range(struct f2fs_sb_info *sbi, nid_t nid)
35 if (unlikely(nid < F2FS_ROOT_INO(sbi) || nid >= NM_I(sbi)->max_nid)) {
36 set_sbi_flag(sbi, SBI_NEED_FSCK);
37 f2fs_warn(sbi, "%s: out-of-range nid=%x, run fsck to fix.",
39 f2fs_handle_error(sbi, ERROR_CORRUPTED_INODE);
45 bool f2fs_available_free_memory(struct f2fs_sb_info *sbi, int type)
47 struct f2fs_nm_info *nm_i = NM_I(sbi);
48 struct discard_cmd_control *dcc = SM_I(sbi)->dcc_info;
50 unsigned long avail_ram;
51 unsigned long mem_size = 0;
59 /* only uses low memory */
60 avail_ram = val.totalram - val.totalhigh;
63 * give 25%, 25%, 50%, 50%, 50% memory for each components respectively
65 if (type == FREE_NIDS) {
66 mem_size = (nm_i->nid_cnt[FREE_NID] *
67 sizeof(struct free_nid)) >> PAGE_SHIFT;
68 res = mem_size < ((avail_ram * nm_i->ram_thresh / 100) >> 2);
69 } else if (type == NAT_ENTRIES) {
70 mem_size = (nm_i->nat_cnt[TOTAL_NAT] *
71 sizeof(struct nat_entry)) >> PAGE_SHIFT;
72 res = mem_size < ((avail_ram * nm_i->ram_thresh / 100) >> 2);
73 if (excess_cached_nats(sbi))
75 } else if (type == DIRTY_DENTS) {
76 if (sbi->sb->s_bdi->wb.dirty_exceeded)
78 mem_size = get_pages(sbi, F2FS_DIRTY_DENTS);
79 res = mem_size < ((avail_ram * nm_i->ram_thresh / 100) >> 1);
80 } else if (type == INO_ENTRIES) {
83 for (i = 0; i < MAX_INO_ENTRY; i++)
84 mem_size += sbi->im[i].ino_num *
85 sizeof(struct ino_entry);
86 mem_size >>= PAGE_SHIFT;
87 res = mem_size < ((avail_ram * nm_i->ram_thresh / 100) >> 1);
88 } else if (type == READ_EXTENT_CACHE) {
89 struct extent_tree_info *eti = &sbi->extent_tree[EX_READ];
91 mem_size = (atomic_read(&eti->total_ext_tree) *
92 sizeof(struct extent_tree) +
93 atomic_read(&eti->total_ext_node) *
94 sizeof(struct extent_node)) >> PAGE_SHIFT;
95 res = mem_size < ((avail_ram * nm_i->ram_thresh / 100) >> 1);
96 } else if (type == DISCARD_CACHE) {
97 mem_size = (atomic_read(&dcc->discard_cmd_cnt) *
98 sizeof(struct discard_cmd)) >> PAGE_SHIFT;
99 res = mem_size < (avail_ram * nm_i->ram_thresh / 100);
100 } else if (type == COMPRESS_PAGE) {
101 #ifdef CONFIG_F2FS_FS_COMPRESSION
102 unsigned long free_ram = val.freeram;
105 * free memory is lower than watermark or cached page count
106 * exceed threshold, deny caching compress page.
108 res = (free_ram > avail_ram * sbi->compress_watermark / 100) &&
109 (COMPRESS_MAPPING(sbi)->nrpages <
110 free_ram * sbi->compress_percent / 100);
115 if (!sbi->sb->s_bdi->wb.dirty_exceeded)
121 static void clear_node_page_dirty(struct page *page)
123 if (PageDirty(page)) {
124 f2fs_clear_page_cache_dirty_tag(page);
125 clear_page_dirty_for_io(page);
126 dec_page_count(F2FS_P_SB(page), F2FS_DIRTY_NODES);
128 ClearPageUptodate(page);
131 static struct page *get_current_nat_page(struct f2fs_sb_info *sbi, nid_t nid)
133 return f2fs_get_meta_page_retry(sbi, current_nat_addr(sbi, nid));
136 static struct page *get_next_nat_page(struct f2fs_sb_info *sbi, nid_t nid)
138 struct page *src_page;
139 struct page *dst_page;
143 struct f2fs_nm_info *nm_i = NM_I(sbi);
145 dst_off = next_nat_addr(sbi, current_nat_addr(sbi, nid));
147 /* get current nat block page with lock */
148 src_page = get_current_nat_page(sbi, nid);
149 if (IS_ERR(src_page))
151 dst_page = f2fs_grab_meta_page(sbi, dst_off);
152 f2fs_bug_on(sbi, PageDirty(src_page));
154 src_addr = page_address(src_page);
155 dst_addr = page_address(dst_page);
156 memcpy(dst_addr, src_addr, PAGE_SIZE);
157 set_page_dirty(dst_page);
158 f2fs_put_page(src_page, 1);
160 set_to_next_nat(nm_i, nid);
165 static struct nat_entry *__alloc_nat_entry(struct f2fs_sb_info *sbi,
166 nid_t nid, bool no_fail)
168 struct nat_entry *new;
170 new = f2fs_kmem_cache_alloc(nat_entry_slab,
171 GFP_F2FS_ZERO, no_fail, sbi);
173 nat_set_nid(new, nid);
179 static void __free_nat_entry(struct nat_entry *e)
181 kmem_cache_free(nat_entry_slab, e);
184 /* must be locked by nat_tree_lock */
185 static struct nat_entry *__init_nat_entry(struct f2fs_nm_info *nm_i,
186 struct nat_entry *ne, struct f2fs_nat_entry *raw_ne, bool no_fail)
189 f2fs_radix_tree_insert(&nm_i->nat_root, nat_get_nid(ne), ne);
190 else if (radix_tree_insert(&nm_i->nat_root, nat_get_nid(ne), ne))
194 node_info_from_raw_nat(&ne->ni, raw_ne);
196 spin_lock(&nm_i->nat_list_lock);
197 list_add_tail(&ne->list, &nm_i->nat_entries);
198 spin_unlock(&nm_i->nat_list_lock);
200 nm_i->nat_cnt[TOTAL_NAT]++;
201 nm_i->nat_cnt[RECLAIMABLE_NAT]++;
205 static struct nat_entry *__lookup_nat_cache(struct f2fs_nm_info *nm_i, nid_t n)
207 struct nat_entry *ne;
209 ne = radix_tree_lookup(&nm_i->nat_root, n);
211 /* for recent accessed nat entry, move it to tail of lru list */
212 if (ne && !get_nat_flag(ne, IS_DIRTY)) {
213 spin_lock(&nm_i->nat_list_lock);
214 if (!list_empty(&ne->list))
215 list_move_tail(&ne->list, &nm_i->nat_entries);
216 spin_unlock(&nm_i->nat_list_lock);
222 static unsigned int __gang_lookup_nat_cache(struct f2fs_nm_info *nm_i,
223 nid_t start, unsigned int nr, struct nat_entry **ep)
225 return radix_tree_gang_lookup(&nm_i->nat_root, (void **)ep, start, nr);
228 static void __del_from_nat_cache(struct f2fs_nm_info *nm_i, struct nat_entry *e)
230 radix_tree_delete(&nm_i->nat_root, nat_get_nid(e));
231 nm_i->nat_cnt[TOTAL_NAT]--;
232 nm_i->nat_cnt[RECLAIMABLE_NAT]--;
236 static struct nat_entry_set *__grab_nat_entry_set(struct f2fs_nm_info *nm_i,
237 struct nat_entry *ne)
239 nid_t set = NAT_BLOCK_OFFSET(ne->ni.nid);
240 struct nat_entry_set *head;
242 head = radix_tree_lookup(&nm_i->nat_set_root, set);
244 head = f2fs_kmem_cache_alloc(nat_entry_set_slab,
245 GFP_NOFS, true, NULL);
247 INIT_LIST_HEAD(&head->entry_list);
248 INIT_LIST_HEAD(&head->set_list);
251 f2fs_radix_tree_insert(&nm_i->nat_set_root, set, head);
256 static void __set_nat_cache_dirty(struct f2fs_nm_info *nm_i,
257 struct nat_entry *ne)
259 struct nat_entry_set *head;
260 bool new_ne = nat_get_blkaddr(ne) == NEW_ADDR;
263 head = __grab_nat_entry_set(nm_i, ne);
266 * update entry_cnt in below condition:
267 * 1. update NEW_ADDR to valid block address;
268 * 2. update old block address to new one;
270 if (!new_ne && (get_nat_flag(ne, IS_PREALLOC) ||
271 !get_nat_flag(ne, IS_DIRTY)))
274 set_nat_flag(ne, IS_PREALLOC, new_ne);
276 if (get_nat_flag(ne, IS_DIRTY))
279 nm_i->nat_cnt[DIRTY_NAT]++;
280 nm_i->nat_cnt[RECLAIMABLE_NAT]--;
281 set_nat_flag(ne, IS_DIRTY, true);
283 spin_lock(&nm_i->nat_list_lock);
285 list_del_init(&ne->list);
287 list_move_tail(&ne->list, &head->entry_list);
288 spin_unlock(&nm_i->nat_list_lock);
291 static void __clear_nat_cache_dirty(struct f2fs_nm_info *nm_i,
292 struct nat_entry_set *set, struct nat_entry *ne)
294 spin_lock(&nm_i->nat_list_lock);
295 list_move_tail(&ne->list, &nm_i->nat_entries);
296 spin_unlock(&nm_i->nat_list_lock);
298 set_nat_flag(ne, IS_DIRTY, false);
300 nm_i->nat_cnt[DIRTY_NAT]--;
301 nm_i->nat_cnt[RECLAIMABLE_NAT]++;
304 static unsigned int __gang_lookup_nat_set(struct f2fs_nm_info *nm_i,
305 nid_t start, unsigned int nr, struct nat_entry_set **ep)
307 return radix_tree_gang_lookup(&nm_i->nat_set_root, (void **)ep,
311 bool f2fs_in_warm_node_list(struct f2fs_sb_info *sbi, struct page *page)
313 return NODE_MAPPING(sbi) == page->mapping &&
314 IS_DNODE(page) && is_cold_node(page);
317 void f2fs_init_fsync_node_info(struct f2fs_sb_info *sbi)
319 spin_lock_init(&sbi->fsync_node_lock);
320 INIT_LIST_HEAD(&sbi->fsync_node_list);
321 sbi->fsync_seg_id = 0;
322 sbi->fsync_node_num = 0;
325 static unsigned int f2fs_add_fsync_node_entry(struct f2fs_sb_info *sbi,
328 struct fsync_node_entry *fn;
332 fn = f2fs_kmem_cache_alloc(fsync_node_entry_slab,
333 GFP_NOFS, true, NULL);
337 INIT_LIST_HEAD(&fn->list);
339 spin_lock_irqsave(&sbi->fsync_node_lock, flags);
340 list_add_tail(&fn->list, &sbi->fsync_node_list);
341 fn->seq_id = sbi->fsync_seg_id++;
343 sbi->fsync_node_num++;
344 spin_unlock_irqrestore(&sbi->fsync_node_lock, flags);
349 void f2fs_del_fsync_node_entry(struct f2fs_sb_info *sbi, struct page *page)
351 struct fsync_node_entry *fn;
354 spin_lock_irqsave(&sbi->fsync_node_lock, flags);
355 list_for_each_entry(fn, &sbi->fsync_node_list, list) {
356 if (fn->page == page) {
358 sbi->fsync_node_num--;
359 spin_unlock_irqrestore(&sbi->fsync_node_lock, flags);
360 kmem_cache_free(fsync_node_entry_slab, fn);
365 spin_unlock_irqrestore(&sbi->fsync_node_lock, flags);
369 void f2fs_reset_fsync_node_info(struct f2fs_sb_info *sbi)
373 spin_lock_irqsave(&sbi->fsync_node_lock, flags);
374 sbi->fsync_seg_id = 0;
375 spin_unlock_irqrestore(&sbi->fsync_node_lock, flags);
378 int f2fs_need_dentry_mark(struct f2fs_sb_info *sbi, nid_t nid)
380 struct f2fs_nm_info *nm_i = NM_I(sbi);
384 f2fs_down_read(&nm_i->nat_tree_lock);
385 e = __lookup_nat_cache(nm_i, nid);
387 if (!get_nat_flag(e, IS_CHECKPOINTED) &&
388 !get_nat_flag(e, HAS_FSYNCED_INODE))
391 f2fs_up_read(&nm_i->nat_tree_lock);
395 bool f2fs_is_checkpointed_node(struct f2fs_sb_info *sbi, nid_t nid)
397 struct f2fs_nm_info *nm_i = NM_I(sbi);
401 f2fs_down_read(&nm_i->nat_tree_lock);
402 e = __lookup_nat_cache(nm_i, nid);
403 if (e && !get_nat_flag(e, IS_CHECKPOINTED))
405 f2fs_up_read(&nm_i->nat_tree_lock);
409 bool f2fs_need_inode_block_update(struct f2fs_sb_info *sbi, nid_t ino)
411 struct f2fs_nm_info *nm_i = NM_I(sbi);
413 bool need_update = true;
415 f2fs_down_read(&nm_i->nat_tree_lock);
416 e = __lookup_nat_cache(nm_i, ino);
417 if (e && get_nat_flag(e, HAS_LAST_FSYNC) &&
418 (get_nat_flag(e, IS_CHECKPOINTED) ||
419 get_nat_flag(e, HAS_FSYNCED_INODE)))
421 f2fs_up_read(&nm_i->nat_tree_lock);
425 /* must be locked by nat_tree_lock */
426 static void cache_nat_entry(struct f2fs_sb_info *sbi, nid_t nid,
427 struct f2fs_nat_entry *ne)
429 struct f2fs_nm_info *nm_i = NM_I(sbi);
430 struct nat_entry *new, *e;
432 /* Let's mitigate lock contention of nat_tree_lock during checkpoint */
433 if (f2fs_rwsem_is_locked(&sbi->cp_global_sem))
436 new = __alloc_nat_entry(sbi, nid, false);
440 f2fs_down_write(&nm_i->nat_tree_lock);
441 e = __lookup_nat_cache(nm_i, nid);
443 e = __init_nat_entry(nm_i, new, ne, false);
445 f2fs_bug_on(sbi, nat_get_ino(e) != le32_to_cpu(ne->ino) ||
446 nat_get_blkaddr(e) !=
447 le32_to_cpu(ne->block_addr) ||
448 nat_get_version(e) != ne->version);
449 f2fs_up_write(&nm_i->nat_tree_lock);
451 __free_nat_entry(new);
454 static void set_node_addr(struct f2fs_sb_info *sbi, struct node_info *ni,
455 block_t new_blkaddr, bool fsync_done)
457 struct f2fs_nm_info *nm_i = NM_I(sbi);
459 struct nat_entry *new = __alloc_nat_entry(sbi, ni->nid, true);
461 f2fs_down_write(&nm_i->nat_tree_lock);
462 e = __lookup_nat_cache(nm_i, ni->nid);
464 e = __init_nat_entry(nm_i, new, NULL, true);
465 copy_node_info(&e->ni, ni);
466 f2fs_bug_on(sbi, ni->blk_addr == NEW_ADDR);
467 } else if (new_blkaddr == NEW_ADDR) {
469 * when nid is reallocated,
470 * previous nat entry can be remained in nat cache.
471 * So, reinitialize it with new information.
473 copy_node_info(&e->ni, ni);
474 f2fs_bug_on(sbi, ni->blk_addr != NULL_ADDR);
476 /* let's free early to reduce memory consumption */
478 __free_nat_entry(new);
481 f2fs_bug_on(sbi, nat_get_blkaddr(e) != ni->blk_addr);
482 f2fs_bug_on(sbi, nat_get_blkaddr(e) == NULL_ADDR &&
483 new_blkaddr == NULL_ADDR);
484 f2fs_bug_on(sbi, nat_get_blkaddr(e) == NEW_ADDR &&
485 new_blkaddr == NEW_ADDR);
486 f2fs_bug_on(sbi, __is_valid_data_blkaddr(nat_get_blkaddr(e)) &&
487 new_blkaddr == NEW_ADDR);
489 /* increment version no as node is removed */
490 if (nat_get_blkaddr(e) != NEW_ADDR && new_blkaddr == NULL_ADDR) {
491 unsigned char version = nat_get_version(e);
493 nat_set_version(e, inc_node_version(version));
497 nat_set_blkaddr(e, new_blkaddr);
498 if (!__is_valid_data_blkaddr(new_blkaddr))
499 set_nat_flag(e, IS_CHECKPOINTED, false);
500 __set_nat_cache_dirty(nm_i, e);
502 /* update fsync_mark if its inode nat entry is still alive */
503 if (ni->nid != ni->ino)
504 e = __lookup_nat_cache(nm_i, ni->ino);
506 if (fsync_done && ni->nid == ni->ino)
507 set_nat_flag(e, HAS_FSYNCED_INODE, true);
508 set_nat_flag(e, HAS_LAST_FSYNC, fsync_done);
510 f2fs_up_write(&nm_i->nat_tree_lock);
513 int f2fs_try_to_free_nats(struct f2fs_sb_info *sbi, int nr_shrink)
515 struct f2fs_nm_info *nm_i = NM_I(sbi);
518 if (!f2fs_down_write_trylock(&nm_i->nat_tree_lock))
521 spin_lock(&nm_i->nat_list_lock);
523 struct nat_entry *ne;
525 if (list_empty(&nm_i->nat_entries))
528 ne = list_first_entry(&nm_i->nat_entries,
529 struct nat_entry, list);
531 spin_unlock(&nm_i->nat_list_lock);
533 __del_from_nat_cache(nm_i, ne);
536 spin_lock(&nm_i->nat_list_lock);
538 spin_unlock(&nm_i->nat_list_lock);
540 f2fs_up_write(&nm_i->nat_tree_lock);
541 return nr - nr_shrink;
544 int f2fs_get_node_info(struct f2fs_sb_info *sbi, nid_t nid,
545 struct node_info *ni, bool checkpoint_context)
547 struct f2fs_nm_info *nm_i = NM_I(sbi);
548 struct curseg_info *curseg = CURSEG_I(sbi, CURSEG_HOT_DATA);
549 struct f2fs_journal *journal = curseg->journal;
550 nid_t start_nid = START_NID(nid);
551 struct f2fs_nat_block *nat_blk;
552 struct page *page = NULL;
553 struct f2fs_nat_entry ne;
561 /* Check nat cache */
562 f2fs_down_read(&nm_i->nat_tree_lock);
563 e = __lookup_nat_cache(nm_i, nid);
565 ni->ino = nat_get_ino(e);
566 ni->blk_addr = nat_get_blkaddr(e);
567 ni->version = nat_get_version(e);
568 f2fs_up_read(&nm_i->nat_tree_lock);
573 * Check current segment summary by trying to grab journal_rwsem first.
574 * This sem is on the critical path on the checkpoint requiring the above
575 * nat_tree_lock. Therefore, we should retry, if we failed to grab here
576 * while not bothering checkpoint.
578 if (!f2fs_rwsem_is_locked(&sbi->cp_global_sem) || checkpoint_context) {
579 down_read(&curseg->journal_rwsem);
580 } else if (f2fs_rwsem_is_contended(&nm_i->nat_tree_lock) ||
581 !down_read_trylock(&curseg->journal_rwsem)) {
582 f2fs_up_read(&nm_i->nat_tree_lock);
586 i = f2fs_lookup_journal_in_cursum(journal, NAT_JOURNAL, nid, 0);
588 ne = nat_in_journal(journal, i);
589 node_info_from_raw_nat(ni, &ne);
591 up_read(&curseg->journal_rwsem);
593 f2fs_up_read(&nm_i->nat_tree_lock);
597 /* Fill node_info from nat page */
598 index = current_nat_addr(sbi, nid);
599 f2fs_up_read(&nm_i->nat_tree_lock);
601 page = f2fs_get_meta_page(sbi, index);
603 return PTR_ERR(page);
605 nat_blk = (struct f2fs_nat_block *)page_address(page);
606 ne = nat_blk->entries[nid - start_nid];
607 node_info_from_raw_nat(ni, &ne);
608 f2fs_put_page(page, 1);
610 blkaddr = le32_to_cpu(ne.block_addr);
611 if (__is_valid_data_blkaddr(blkaddr) &&
612 !f2fs_is_valid_blkaddr(sbi, blkaddr, DATA_GENERIC_ENHANCE))
615 /* cache nat entry */
616 cache_nat_entry(sbi, nid, &ne);
621 * readahead MAX_RA_NODE number of node pages.
623 static void f2fs_ra_node_pages(struct page *parent, int start, int n)
625 struct f2fs_sb_info *sbi = F2FS_P_SB(parent);
626 struct blk_plug plug;
630 blk_start_plug(&plug);
632 /* Then, try readahead for siblings of the desired node */
634 end = min(end, NIDS_PER_BLOCK);
635 for (i = start; i < end; i++) {
636 nid = get_nid(parent, i, false);
637 f2fs_ra_node_page(sbi, nid);
640 blk_finish_plug(&plug);
643 pgoff_t f2fs_get_next_page_offset(struct dnode_of_data *dn, pgoff_t pgofs)
645 const long direct_index = ADDRS_PER_INODE(dn->inode);
646 const long direct_blks = ADDRS_PER_BLOCK(dn->inode);
647 const long indirect_blks = ADDRS_PER_BLOCK(dn->inode) * NIDS_PER_BLOCK;
648 unsigned int skipped_unit = ADDRS_PER_BLOCK(dn->inode);
649 int cur_level = dn->cur_level;
650 int max_level = dn->max_level;
656 while (max_level-- > cur_level)
657 skipped_unit *= NIDS_PER_BLOCK;
659 switch (dn->max_level) {
661 base += 2 * indirect_blks;
664 base += 2 * direct_blks;
667 base += direct_index;
670 f2fs_bug_on(F2FS_I_SB(dn->inode), 1);
673 return ((pgofs - base) / skipped_unit + 1) * skipped_unit + base;
677 * The maximum depth is four.
678 * Offset[0] will have raw inode offset.
680 static int get_node_path(struct inode *inode, long block,
681 int offset[4], unsigned int noffset[4])
683 const long direct_index = ADDRS_PER_INODE(inode);
684 const long direct_blks = ADDRS_PER_BLOCK(inode);
685 const long dptrs_per_blk = NIDS_PER_BLOCK;
686 const long indirect_blks = ADDRS_PER_BLOCK(inode) * NIDS_PER_BLOCK;
687 const long dindirect_blks = indirect_blks * NIDS_PER_BLOCK;
693 if (block < direct_index) {
697 block -= direct_index;
698 if (block < direct_blks) {
699 offset[n++] = NODE_DIR1_BLOCK;
705 block -= direct_blks;
706 if (block < direct_blks) {
707 offset[n++] = NODE_DIR2_BLOCK;
713 block -= direct_blks;
714 if (block < indirect_blks) {
715 offset[n++] = NODE_IND1_BLOCK;
717 offset[n++] = block / direct_blks;
718 noffset[n] = 4 + offset[n - 1];
719 offset[n] = block % direct_blks;
723 block -= indirect_blks;
724 if (block < indirect_blks) {
725 offset[n++] = NODE_IND2_BLOCK;
726 noffset[n] = 4 + dptrs_per_blk;
727 offset[n++] = block / direct_blks;
728 noffset[n] = 5 + dptrs_per_blk + offset[n - 1];
729 offset[n] = block % direct_blks;
733 block -= indirect_blks;
734 if (block < dindirect_blks) {
735 offset[n++] = NODE_DIND_BLOCK;
736 noffset[n] = 5 + (dptrs_per_blk * 2);
737 offset[n++] = block / indirect_blks;
738 noffset[n] = 6 + (dptrs_per_blk * 2) +
739 offset[n - 1] * (dptrs_per_blk + 1);
740 offset[n++] = (block / direct_blks) % dptrs_per_blk;
741 noffset[n] = 7 + (dptrs_per_blk * 2) +
742 offset[n - 2] * (dptrs_per_blk + 1) +
744 offset[n] = block % direct_blks;
755 * Caller should call f2fs_put_dnode(dn).
756 * Also, it should grab and release a rwsem by calling f2fs_lock_op() and
757 * f2fs_unlock_op() only if mode is set with ALLOC_NODE.
759 int f2fs_get_dnode_of_data(struct dnode_of_data *dn, pgoff_t index, int mode)
761 struct f2fs_sb_info *sbi = F2FS_I_SB(dn->inode);
762 struct page *npage[4];
763 struct page *parent = NULL;
765 unsigned int noffset[4];
770 level = get_node_path(dn->inode, index, offset, noffset);
774 nids[0] = dn->inode->i_ino;
775 npage[0] = dn->inode_page;
778 npage[0] = f2fs_get_node_page(sbi, nids[0]);
779 if (IS_ERR(npage[0]))
780 return PTR_ERR(npage[0]);
783 /* if inline_data is set, should not report any block indices */
784 if (f2fs_has_inline_data(dn->inode) && index) {
786 f2fs_put_page(npage[0], 1);
792 nids[1] = get_nid(parent, offset[0], true);
793 dn->inode_page = npage[0];
794 dn->inode_page_locked = true;
796 /* get indirect or direct nodes */
797 for (i = 1; i <= level; i++) {
800 if (!nids[i] && mode == ALLOC_NODE) {
802 if (!f2fs_alloc_nid(sbi, &(nids[i]))) {
808 npage[i] = f2fs_new_node_page(dn, noffset[i]);
809 if (IS_ERR(npage[i])) {
810 f2fs_alloc_nid_failed(sbi, nids[i]);
811 err = PTR_ERR(npage[i]);
815 set_nid(parent, offset[i - 1], nids[i], i == 1);
816 f2fs_alloc_nid_done(sbi, nids[i]);
818 } else if (mode == LOOKUP_NODE_RA && i == level && level > 1) {
819 npage[i] = f2fs_get_node_page_ra(parent, offset[i - 1]);
820 if (IS_ERR(npage[i])) {
821 err = PTR_ERR(npage[i]);
827 dn->inode_page_locked = false;
830 f2fs_put_page(parent, 1);
834 npage[i] = f2fs_get_node_page(sbi, nids[i]);
835 if (IS_ERR(npage[i])) {
836 err = PTR_ERR(npage[i]);
837 f2fs_put_page(npage[0], 0);
843 nids[i + 1] = get_nid(parent, offset[i], false);
846 dn->nid = nids[level];
847 dn->ofs_in_node = offset[level];
848 dn->node_page = npage[level];
849 dn->data_blkaddr = f2fs_data_blkaddr(dn);
851 if (is_inode_flag_set(dn->inode, FI_COMPRESSED_FILE) &&
852 f2fs_sb_has_readonly(sbi)) {
853 unsigned int cluster_size = F2FS_I(dn->inode)->i_cluster_size;
854 unsigned int ofs_in_node = dn->ofs_in_node;
855 pgoff_t fofs = index;
859 /* should align fofs and ofs_in_node to cluster_size */
860 if (fofs % cluster_size) {
861 fofs = round_down(fofs, cluster_size);
862 ofs_in_node = round_down(ofs_in_node, cluster_size);
865 c_len = f2fs_cluster_blocks_are_contiguous(dn, ofs_in_node);
869 blkaddr = data_blkaddr(dn->inode, dn->node_page, ofs_in_node);
870 if (blkaddr == COMPRESS_ADDR)
871 blkaddr = data_blkaddr(dn->inode, dn->node_page,
874 f2fs_update_read_extent_tree_range_compressed(dn->inode,
875 fofs, blkaddr, cluster_size, c_len);
881 f2fs_put_page(parent, 1);
883 f2fs_put_page(npage[0], 0);
885 dn->inode_page = NULL;
886 dn->node_page = NULL;
887 if (err == -ENOENT) {
889 dn->max_level = level;
890 dn->ofs_in_node = offset[level];
895 static int truncate_node(struct dnode_of_data *dn)
897 struct f2fs_sb_info *sbi = F2FS_I_SB(dn->inode);
902 err = f2fs_get_node_info(sbi, dn->nid, &ni, false);
906 /* Deallocate node address */
907 f2fs_invalidate_blocks(sbi, ni.blk_addr);
908 dec_valid_node_count(sbi, dn->inode, dn->nid == dn->inode->i_ino);
909 set_node_addr(sbi, &ni, NULL_ADDR, false);
911 if (dn->nid == dn->inode->i_ino) {
912 f2fs_remove_orphan_inode(sbi, dn->nid);
913 dec_valid_inode_count(sbi);
914 f2fs_inode_synced(dn->inode);
917 clear_node_page_dirty(dn->node_page);
918 set_sbi_flag(sbi, SBI_IS_DIRTY);
920 index = dn->node_page->index;
921 f2fs_put_page(dn->node_page, 1);
923 invalidate_mapping_pages(NODE_MAPPING(sbi),
926 dn->node_page = NULL;
927 trace_f2fs_truncate_node(dn->inode, dn->nid, ni.blk_addr);
932 static int truncate_dnode(struct dnode_of_data *dn)
940 /* get direct node */
941 page = f2fs_get_node_page(F2FS_I_SB(dn->inode), dn->nid);
942 if (PTR_ERR(page) == -ENOENT)
944 else if (IS_ERR(page))
945 return PTR_ERR(page);
947 /* Make dnode_of_data for parameter */
948 dn->node_page = page;
950 f2fs_truncate_data_blocks(dn);
951 err = truncate_node(dn);
953 f2fs_put_page(page, 1);
960 static int truncate_nodes(struct dnode_of_data *dn, unsigned int nofs,
963 struct dnode_of_data rdn = *dn;
965 struct f2fs_node *rn;
967 unsigned int child_nofs;
972 return NIDS_PER_BLOCK + 1;
974 trace_f2fs_truncate_nodes_enter(dn->inode, dn->nid, dn->data_blkaddr);
976 page = f2fs_get_node_page(F2FS_I_SB(dn->inode), dn->nid);
978 trace_f2fs_truncate_nodes_exit(dn->inode, PTR_ERR(page));
979 return PTR_ERR(page);
982 f2fs_ra_node_pages(page, ofs, NIDS_PER_BLOCK);
984 rn = F2FS_NODE(page);
986 for (i = ofs; i < NIDS_PER_BLOCK; i++, freed++) {
987 child_nid = le32_to_cpu(rn->in.nid[i]);
991 ret = truncate_dnode(&rdn);
994 if (set_nid(page, i, 0, false))
995 dn->node_changed = true;
998 child_nofs = nofs + ofs * (NIDS_PER_BLOCK + 1) + 1;
999 for (i = ofs; i < NIDS_PER_BLOCK; i++) {
1000 child_nid = le32_to_cpu(rn->in.nid[i]);
1001 if (child_nid == 0) {
1002 child_nofs += NIDS_PER_BLOCK + 1;
1005 rdn.nid = child_nid;
1006 ret = truncate_nodes(&rdn, child_nofs, 0, depth - 1);
1007 if (ret == (NIDS_PER_BLOCK + 1)) {
1008 if (set_nid(page, i, 0, false))
1009 dn->node_changed = true;
1011 } else if (ret < 0 && ret != -ENOENT) {
1019 /* remove current indirect node */
1020 dn->node_page = page;
1021 ret = truncate_node(dn);
1026 f2fs_put_page(page, 1);
1028 trace_f2fs_truncate_nodes_exit(dn->inode, freed);
1032 f2fs_put_page(page, 1);
1033 trace_f2fs_truncate_nodes_exit(dn->inode, ret);
1037 static int truncate_partial_nodes(struct dnode_of_data *dn,
1038 struct f2fs_inode *ri, int *offset, int depth)
1040 struct page *pages[2];
1045 int idx = depth - 2;
1047 nid[0] = le32_to_cpu(ri->i_nid[offset[0] - NODE_DIR1_BLOCK]);
1051 /* get indirect nodes in the path */
1052 for (i = 0; i < idx + 1; i++) {
1053 /* reference count'll be increased */
1054 pages[i] = f2fs_get_node_page(F2FS_I_SB(dn->inode), nid[i]);
1055 if (IS_ERR(pages[i])) {
1056 err = PTR_ERR(pages[i]);
1060 nid[i + 1] = get_nid(pages[i], offset[i + 1], false);
1063 f2fs_ra_node_pages(pages[idx], offset[idx + 1], NIDS_PER_BLOCK);
1065 /* free direct nodes linked to a partial indirect node */
1066 for (i = offset[idx + 1]; i < NIDS_PER_BLOCK; i++) {
1067 child_nid = get_nid(pages[idx], i, false);
1070 dn->nid = child_nid;
1071 err = truncate_dnode(dn);
1074 if (set_nid(pages[idx], i, 0, false))
1075 dn->node_changed = true;
1078 if (offset[idx + 1] == 0) {
1079 dn->node_page = pages[idx];
1081 err = truncate_node(dn);
1085 f2fs_put_page(pages[idx], 1);
1088 offset[idx + 1] = 0;
1091 for (i = idx; i >= 0; i--)
1092 f2fs_put_page(pages[i], 1);
1094 trace_f2fs_truncate_partial_nodes(dn->inode, nid, depth, err);
1100 * All the block addresses of data and nodes should be nullified.
1102 int f2fs_truncate_inode_blocks(struct inode *inode, pgoff_t from)
1104 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
1105 int err = 0, cont = 1;
1106 int level, offset[4], noffset[4];
1107 unsigned int nofs = 0;
1108 struct f2fs_inode *ri;
1109 struct dnode_of_data dn;
1112 trace_f2fs_truncate_inode_blocks_enter(inode, from);
1114 level = get_node_path(inode, from, offset, noffset);
1116 trace_f2fs_truncate_inode_blocks_exit(inode, level);
1120 page = f2fs_get_node_page(sbi, inode->i_ino);
1122 trace_f2fs_truncate_inode_blocks_exit(inode, PTR_ERR(page));
1123 return PTR_ERR(page);
1126 set_new_dnode(&dn, inode, page, NULL, 0);
1129 ri = F2FS_INODE(page);
1137 if (!offset[level - 1])
1139 err = truncate_partial_nodes(&dn, ri, offset, level);
1140 if (err < 0 && err != -ENOENT)
1142 nofs += 1 + NIDS_PER_BLOCK;
1145 nofs = 5 + 2 * NIDS_PER_BLOCK;
1146 if (!offset[level - 1])
1148 err = truncate_partial_nodes(&dn, ri, offset, level);
1149 if (err < 0 && err != -ENOENT)
1158 dn.nid = le32_to_cpu(ri->i_nid[offset[0] - NODE_DIR1_BLOCK]);
1159 switch (offset[0]) {
1160 case NODE_DIR1_BLOCK:
1161 case NODE_DIR2_BLOCK:
1162 err = truncate_dnode(&dn);
1165 case NODE_IND1_BLOCK:
1166 case NODE_IND2_BLOCK:
1167 err = truncate_nodes(&dn, nofs, offset[1], 2);
1170 case NODE_DIND_BLOCK:
1171 err = truncate_nodes(&dn, nofs, offset[1], 3);
1178 if (err < 0 && err != -ENOENT)
1180 if (offset[1] == 0 &&
1181 ri->i_nid[offset[0] - NODE_DIR1_BLOCK]) {
1183 BUG_ON(page->mapping != NODE_MAPPING(sbi));
1184 f2fs_wait_on_page_writeback(page, NODE, true, true);
1185 ri->i_nid[offset[0] - NODE_DIR1_BLOCK] = 0;
1186 set_page_dirty(page);
1194 f2fs_put_page(page, 0);
1195 trace_f2fs_truncate_inode_blocks_exit(inode, err);
1196 return err > 0 ? 0 : err;
1199 /* caller must lock inode page */
1200 int f2fs_truncate_xattr_node(struct inode *inode)
1202 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
1203 nid_t nid = F2FS_I(inode)->i_xattr_nid;
1204 struct dnode_of_data dn;
1211 npage = f2fs_get_node_page(sbi, nid);
1213 return PTR_ERR(npage);
1215 set_new_dnode(&dn, inode, NULL, npage, nid);
1216 err = truncate_node(&dn);
1218 f2fs_put_page(npage, 1);
1222 f2fs_i_xnid_write(inode, 0);
1228 * Caller should grab and release a rwsem by calling f2fs_lock_op() and
1231 int f2fs_remove_inode_page(struct inode *inode)
1233 struct dnode_of_data dn;
1236 set_new_dnode(&dn, inode, NULL, NULL, inode->i_ino);
1237 err = f2fs_get_dnode_of_data(&dn, 0, LOOKUP_NODE);
1241 err = f2fs_truncate_xattr_node(inode);
1243 f2fs_put_dnode(&dn);
1247 /* remove potential inline_data blocks */
1248 if (S_ISREG(inode->i_mode) || S_ISDIR(inode->i_mode) ||
1249 S_ISLNK(inode->i_mode))
1250 f2fs_truncate_data_blocks_range(&dn, 1);
1252 /* 0 is possible, after f2fs_new_inode() has failed */
1253 if (unlikely(f2fs_cp_error(F2FS_I_SB(inode)))) {
1254 f2fs_put_dnode(&dn);
1258 if (unlikely(inode->i_blocks != 0 && inode->i_blocks != 8)) {
1259 f2fs_warn(F2FS_I_SB(inode),
1260 "f2fs_remove_inode_page: inconsistent i_blocks, ino:%lu, iblocks:%llu",
1261 inode->i_ino, (unsigned long long)inode->i_blocks);
1262 set_sbi_flag(F2FS_I_SB(inode), SBI_NEED_FSCK);
1265 /* will put inode & node pages */
1266 err = truncate_node(&dn);
1268 f2fs_put_dnode(&dn);
1274 struct page *f2fs_new_inode_page(struct inode *inode)
1276 struct dnode_of_data dn;
1278 /* allocate inode page for new inode */
1279 set_new_dnode(&dn, inode, NULL, NULL, inode->i_ino);
1281 /* caller should f2fs_put_page(page, 1); */
1282 return f2fs_new_node_page(&dn, 0);
1285 struct page *f2fs_new_node_page(struct dnode_of_data *dn, unsigned int ofs)
1287 struct f2fs_sb_info *sbi = F2FS_I_SB(dn->inode);
1288 struct node_info new_ni;
1292 if (unlikely(is_inode_flag_set(dn->inode, FI_NO_ALLOC)))
1293 return ERR_PTR(-EPERM);
1295 page = f2fs_grab_cache_page(NODE_MAPPING(sbi), dn->nid, false);
1297 return ERR_PTR(-ENOMEM);
1299 if (unlikely((err = inc_valid_node_count(sbi, dn->inode, !ofs))))
1302 #ifdef CONFIG_F2FS_CHECK_FS
1303 err = f2fs_get_node_info(sbi, dn->nid, &new_ni, false);
1305 dec_valid_node_count(sbi, dn->inode, !ofs);
1308 if (unlikely(new_ni.blk_addr != NULL_ADDR)) {
1309 err = -EFSCORRUPTED;
1310 set_sbi_flag(sbi, SBI_NEED_FSCK);
1311 f2fs_handle_error(sbi, ERROR_INVALID_BLKADDR);
1315 new_ni.nid = dn->nid;
1316 new_ni.ino = dn->inode->i_ino;
1317 new_ni.blk_addr = NULL_ADDR;
1320 set_node_addr(sbi, &new_ni, NEW_ADDR, false);
1322 f2fs_wait_on_page_writeback(page, NODE, true, true);
1323 fill_node_footer(page, dn->nid, dn->inode->i_ino, ofs, true);
1324 set_cold_node(page, S_ISDIR(dn->inode->i_mode));
1325 if (!PageUptodate(page))
1326 SetPageUptodate(page);
1327 if (set_page_dirty(page))
1328 dn->node_changed = true;
1330 if (f2fs_has_xattr_block(ofs))
1331 f2fs_i_xnid_write(dn->inode, dn->nid);
1334 inc_valid_inode_count(sbi);
1338 clear_node_page_dirty(page);
1339 f2fs_put_page(page, 1);
1340 return ERR_PTR(err);
1344 * Caller should do after getting the following values.
1345 * 0: f2fs_put_page(page, 0)
1346 * LOCKED_PAGE or error: f2fs_put_page(page, 1)
1348 static int read_node_page(struct page *page, blk_opf_t op_flags)
1350 struct f2fs_sb_info *sbi = F2FS_P_SB(page);
1351 struct node_info ni;
1352 struct f2fs_io_info fio = {
1356 .op_flags = op_flags,
1358 .encrypted_page = NULL,
1362 if (PageUptodate(page)) {
1363 if (!f2fs_inode_chksum_verify(sbi, page)) {
1364 ClearPageUptodate(page);
1370 err = f2fs_get_node_info(sbi, page->index, &ni, false);
1374 /* NEW_ADDR can be seen, after cp_error drops some dirty node pages */
1375 if (unlikely(ni.blk_addr == NULL_ADDR || ni.blk_addr == NEW_ADDR)) {
1376 ClearPageUptodate(page);
1380 fio.new_blkaddr = fio.old_blkaddr = ni.blk_addr;
1382 err = f2fs_submit_page_bio(&fio);
1385 f2fs_update_iostat(sbi, NULL, FS_NODE_READ_IO, F2FS_BLKSIZE);
1391 * Readahead a node page
1393 void f2fs_ra_node_page(struct f2fs_sb_info *sbi, nid_t nid)
1400 if (f2fs_check_nid_range(sbi, nid))
1403 apage = xa_load(&NODE_MAPPING(sbi)->i_pages, nid);
1407 apage = f2fs_grab_cache_page(NODE_MAPPING(sbi), nid, false);
1411 err = read_node_page(apage, REQ_RAHEAD);
1412 f2fs_put_page(apage, err ? 1 : 0);
1415 static struct page *__get_node_page(struct f2fs_sb_info *sbi, pgoff_t nid,
1416 struct page *parent, int start)
1422 return ERR_PTR(-ENOENT);
1423 if (f2fs_check_nid_range(sbi, nid))
1424 return ERR_PTR(-EINVAL);
1426 page = f2fs_grab_cache_page(NODE_MAPPING(sbi), nid, false);
1428 return ERR_PTR(-ENOMEM);
1430 err = read_node_page(page, 0);
1433 } else if (err == LOCKED_PAGE) {
1439 f2fs_ra_node_pages(parent, start + 1, MAX_RA_NODE);
1443 if (unlikely(page->mapping != NODE_MAPPING(sbi))) {
1444 f2fs_put_page(page, 1);
1448 if (unlikely(!PageUptodate(page))) {
1453 if (!f2fs_inode_chksum_verify(sbi, page)) {
1458 if (likely(nid == nid_of_node(page)))
1461 f2fs_warn(sbi, "inconsistent node block, nid:%lu, node_footer[nid:%u,ino:%u,ofs:%u,cpver:%llu,blkaddr:%u]",
1462 nid, nid_of_node(page), ino_of_node(page),
1463 ofs_of_node(page), cpver_of_node(page),
1464 next_blkaddr_of_node(page));
1465 set_sbi_flag(sbi, SBI_NEED_FSCK);
1466 f2fs_handle_error(sbi, ERROR_INCONSISTENT_FOOTER);
1467 err = -EFSCORRUPTED;
1469 ClearPageUptodate(page);
1471 /* ENOENT comes from read_node_page which is not an error. */
1473 f2fs_handle_page_eio(sbi, page->index, NODE);
1474 f2fs_put_page(page, 1);
1475 return ERR_PTR(err);
1478 struct page *f2fs_get_node_page(struct f2fs_sb_info *sbi, pgoff_t nid)
1480 return __get_node_page(sbi, nid, NULL, 0);
1483 struct page *f2fs_get_node_page_ra(struct page *parent, int start)
1485 struct f2fs_sb_info *sbi = F2FS_P_SB(parent);
1486 nid_t nid = get_nid(parent, start, false);
1488 return __get_node_page(sbi, nid, parent, start);
1491 static void flush_inline_data(struct f2fs_sb_info *sbi, nid_t ino)
1493 struct inode *inode;
1497 /* should flush inline_data before evict_inode */
1498 inode = ilookup(sbi->sb, ino);
1502 page = f2fs_pagecache_get_page(inode->i_mapping, 0,
1503 FGP_LOCK|FGP_NOWAIT, 0);
1507 if (!PageUptodate(page))
1510 if (!PageDirty(page))
1513 if (!clear_page_dirty_for_io(page))
1516 ret = f2fs_write_inline_data(inode, page);
1517 inode_dec_dirty_pages(inode);
1518 f2fs_remove_dirty_inode(inode);
1520 set_page_dirty(page);
1522 f2fs_put_page(page, 1);
1527 static struct page *last_fsync_dnode(struct f2fs_sb_info *sbi, nid_t ino)
1530 struct pagevec pvec;
1531 struct page *last_page = NULL;
1534 pagevec_init(&pvec);
1537 while ((nr_pages = pagevec_lookup_tag(&pvec, NODE_MAPPING(sbi), &index,
1538 PAGECACHE_TAG_DIRTY))) {
1541 for (i = 0; i < nr_pages; i++) {
1542 struct page *page = pvec.pages[i];
1544 if (unlikely(f2fs_cp_error(sbi))) {
1545 f2fs_put_page(last_page, 0);
1546 pagevec_release(&pvec);
1547 return ERR_PTR(-EIO);
1550 if (!IS_DNODE(page) || !is_cold_node(page))
1552 if (ino_of_node(page) != ino)
1557 if (unlikely(page->mapping != NODE_MAPPING(sbi))) {
1562 if (ino_of_node(page) != ino)
1563 goto continue_unlock;
1565 if (!PageDirty(page)) {
1566 /* someone wrote it for us */
1567 goto continue_unlock;
1571 f2fs_put_page(last_page, 0);
1577 pagevec_release(&pvec);
1583 static int __write_node_page(struct page *page, bool atomic, bool *submitted,
1584 struct writeback_control *wbc, bool do_balance,
1585 enum iostat_type io_type, unsigned int *seq_id)
1587 struct f2fs_sb_info *sbi = F2FS_P_SB(page);
1589 struct node_info ni;
1590 struct f2fs_io_info fio = {
1592 .ino = ino_of_node(page),
1595 .op_flags = wbc_to_write_flags(wbc),
1597 .encrypted_page = NULL,
1604 trace_f2fs_writepage(page, NODE);
1606 if (unlikely(f2fs_cp_error(sbi))) {
1607 ClearPageUptodate(page);
1608 dec_page_count(sbi, F2FS_DIRTY_NODES);
1613 if (unlikely(is_sbi_flag_set(sbi, SBI_POR_DOING)))
1616 if (!is_sbi_flag_set(sbi, SBI_CP_DISABLED) &&
1617 wbc->sync_mode == WB_SYNC_NONE &&
1618 IS_DNODE(page) && is_cold_node(page))
1621 /* get old block addr of this node page */
1622 nid = nid_of_node(page);
1623 f2fs_bug_on(sbi, page->index != nid);
1625 if (f2fs_get_node_info(sbi, nid, &ni, !do_balance))
1628 if (wbc->for_reclaim) {
1629 if (!f2fs_down_read_trylock(&sbi->node_write))
1632 f2fs_down_read(&sbi->node_write);
1635 /* This page is already truncated */
1636 if (unlikely(ni.blk_addr == NULL_ADDR)) {
1637 ClearPageUptodate(page);
1638 dec_page_count(sbi, F2FS_DIRTY_NODES);
1639 f2fs_up_read(&sbi->node_write);
1644 if (__is_valid_data_blkaddr(ni.blk_addr) &&
1645 !f2fs_is_valid_blkaddr(sbi, ni.blk_addr,
1646 DATA_GENERIC_ENHANCE)) {
1647 f2fs_up_read(&sbi->node_write);
1651 if (atomic && !test_opt(sbi, NOBARRIER) && !f2fs_sb_has_blkzoned(sbi))
1652 fio.op_flags |= REQ_PREFLUSH | REQ_FUA;
1654 /* should add to global list before clearing PAGECACHE status */
1655 if (f2fs_in_warm_node_list(sbi, page)) {
1656 seq = f2fs_add_fsync_node_entry(sbi, page);
1661 set_page_writeback(page);
1662 ClearPageError(page);
1664 fio.old_blkaddr = ni.blk_addr;
1665 f2fs_do_write_node_page(nid, &fio);
1666 set_node_addr(sbi, &ni, fio.new_blkaddr, is_fsync_dnode(page));
1667 dec_page_count(sbi, F2FS_DIRTY_NODES);
1668 f2fs_up_read(&sbi->node_write);
1670 if (wbc->for_reclaim) {
1671 f2fs_submit_merged_write_cond(sbi, NULL, page, 0, NODE);
1677 if (unlikely(f2fs_cp_error(sbi))) {
1678 f2fs_submit_merged_write(sbi, NODE);
1682 *submitted = fio.submitted;
1685 f2fs_balance_fs(sbi, false);
1689 redirty_page_for_writepage(wbc, page);
1690 return AOP_WRITEPAGE_ACTIVATE;
1693 int f2fs_move_node_page(struct page *node_page, int gc_type)
1697 if (gc_type == FG_GC) {
1698 struct writeback_control wbc = {
1699 .sync_mode = WB_SYNC_ALL,
1704 f2fs_wait_on_page_writeback(node_page, NODE, true, true);
1706 set_page_dirty(node_page);
1708 if (!clear_page_dirty_for_io(node_page)) {
1713 if (__write_node_page(node_page, false, NULL,
1714 &wbc, false, FS_GC_NODE_IO, NULL)) {
1716 unlock_page(node_page);
1720 /* set page dirty and write it */
1721 if (!PageWriteback(node_page))
1722 set_page_dirty(node_page);
1725 unlock_page(node_page);
1727 f2fs_put_page(node_page, 0);
1731 static int f2fs_write_node_page(struct page *page,
1732 struct writeback_control *wbc)
1734 return __write_node_page(page, false, NULL, wbc, false,
1738 int f2fs_fsync_node_pages(struct f2fs_sb_info *sbi, struct inode *inode,
1739 struct writeback_control *wbc, bool atomic,
1740 unsigned int *seq_id)
1743 struct pagevec pvec;
1745 struct page *last_page = NULL;
1746 bool marked = false;
1747 nid_t ino = inode->i_ino;
1752 last_page = last_fsync_dnode(sbi, ino);
1753 if (IS_ERR_OR_NULL(last_page))
1754 return PTR_ERR_OR_ZERO(last_page);
1757 pagevec_init(&pvec);
1760 while ((nr_pages = pagevec_lookup_tag(&pvec, NODE_MAPPING(sbi), &index,
1761 PAGECACHE_TAG_DIRTY))) {
1764 for (i = 0; i < nr_pages; i++) {
1765 struct page *page = pvec.pages[i];
1766 bool submitted = false;
1768 if (unlikely(f2fs_cp_error(sbi))) {
1769 f2fs_put_page(last_page, 0);
1770 pagevec_release(&pvec);
1775 if (!IS_DNODE(page) || !is_cold_node(page))
1777 if (ino_of_node(page) != ino)
1782 if (unlikely(page->mapping != NODE_MAPPING(sbi))) {
1787 if (ino_of_node(page) != ino)
1788 goto continue_unlock;
1790 if (!PageDirty(page) && page != last_page) {
1791 /* someone wrote it for us */
1792 goto continue_unlock;
1795 f2fs_wait_on_page_writeback(page, NODE, true, true);
1797 set_fsync_mark(page, 0);
1798 set_dentry_mark(page, 0);
1800 if (!atomic || page == last_page) {
1801 set_fsync_mark(page, 1);
1802 percpu_counter_inc(&sbi->rf_node_block_count);
1803 if (IS_INODE(page)) {
1804 if (is_inode_flag_set(inode,
1806 f2fs_update_inode(inode, page);
1807 set_dentry_mark(page,
1808 f2fs_need_dentry_mark(sbi, ino));
1810 /* may be written by other thread */
1811 if (!PageDirty(page))
1812 set_page_dirty(page);
1815 if (!clear_page_dirty_for_io(page))
1816 goto continue_unlock;
1818 ret = __write_node_page(page, atomic &&
1820 &submitted, wbc, true,
1821 FS_NODE_IO, seq_id);
1824 f2fs_put_page(last_page, 0);
1826 } else if (submitted) {
1830 if (page == last_page) {
1831 f2fs_put_page(page, 0);
1836 pagevec_release(&pvec);
1842 if (!ret && atomic && !marked) {
1843 f2fs_debug(sbi, "Retry to write fsync mark: ino=%u, idx=%lx",
1844 ino, last_page->index);
1845 lock_page(last_page);
1846 f2fs_wait_on_page_writeback(last_page, NODE, true, true);
1847 set_page_dirty(last_page);
1848 unlock_page(last_page);
1853 f2fs_submit_merged_write_cond(sbi, NULL, NULL, ino, NODE);
1854 return ret ? -EIO : 0;
1857 static int f2fs_match_ino(struct inode *inode, unsigned long ino, void *data)
1859 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
1862 if (inode->i_ino != ino)
1865 if (!is_inode_flag_set(inode, FI_DIRTY_INODE))
1868 spin_lock(&sbi->inode_lock[DIRTY_META]);
1869 clean = list_empty(&F2FS_I(inode)->gdirty_list);
1870 spin_unlock(&sbi->inode_lock[DIRTY_META]);
1875 inode = igrab(inode);
1881 static bool flush_dirty_inode(struct page *page)
1883 struct f2fs_sb_info *sbi = F2FS_P_SB(page);
1884 struct inode *inode;
1885 nid_t ino = ino_of_node(page);
1887 inode = find_inode_nowait(sbi->sb, ino, f2fs_match_ino, NULL);
1891 f2fs_update_inode(inode, page);
1898 void f2fs_flush_inline_data(struct f2fs_sb_info *sbi)
1901 struct pagevec pvec;
1904 pagevec_init(&pvec);
1906 while ((nr_pages = pagevec_lookup_tag(&pvec,
1907 NODE_MAPPING(sbi), &index, PAGECACHE_TAG_DIRTY))) {
1910 for (i = 0; i < nr_pages; i++) {
1911 struct page *page = pvec.pages[i];
1913 if (!IS_DNODE(page))
1918 if (unlikely(page->mapping != NODE_MAPPING(sbi))) {
1924 if (!PageDirty(page)) {
1925 /* someone wrote it for us */
1926 goto continue_unlock;
1929 /* flush inline_data, if it's async context. */
1930 if (page_private_inline(page)) {
1931 clear_page_private_inline(page);
1933 flush_inline_data(sbi, ino_of_node(page));
1938 pagevec_release(&pvec);
1943 int f2fs_sync_node_pages(struct f2fs_sb_info *sbi,
1944 struct writeback_control *wbc,
1945 bool do_balance, enum iostat_type io_type)
1948 struct pagevec pvec;
1952 int nr_pages, done = 0;
1954 pagevec_init(&pvec);
1959 while (!done && (nr_pages = pagevec_lookup_tag(&pvec,
1960 NODE_MAPPING(sbi), &index, PAGECACHE_TAG_DIRTY))) {
1963 for (i = 0; i < nr_pages; i++) {
1964 struct page *page = pvec.pages[i];
1965 bool submitted = false;
1967 /* give a priority to WB_SYNC threads */
1968 if (atomic_read(&sbi->wb_sync_req[NODE]) &&
1969 wbc->sync_mode == WB_SYNC_NONE) {
1975 * flushing sequence with step:
1980 if (step == 0 && IS_DNODE(page))
1982 if (step == 1 && (!IS_DNODE(page) ||
1983 is_cold_node(page)))
1985 if (step == 2 && (!IS_DNODE(page) ||
1986 !is_cold_node(page)))
1989 if (wbc->sync_mode == WB_SYNC_ALL)
1991 else if (!trylock_page(page))
1994 if (unlikely(page->mapping != NODE_MAPPING(sbi))) {
2000 if (!PageDirty(page)) {
2001 /* someone wrote it for us */
2002 goto continue_unlock;
2005 /* flush inline_data/inode, if it's async context. */
2009 /* flush inline_data */
2010 if (page_private_inline(page)) {
2011 clear_page_private_inline(page);
2013 flush_inline_data(sbi, ino_of_node(page));
2017 /* flush dirty inode */
2018 if (IS_INODE(page) && flush_dirty_inode(page))
2021 f2fs_wait_on_page_writeback(page, NODE, true, true);
2023 if (!clear_page_dirty_for_io(page))
2024 goto continue_unlock;
2026 set_fsync_mark(page, 0);
2027 set_dentry_mark(page, 0);
2029 ret = __write_node_page(page, false, &submitted,
2030 wbc, do_balance, io_type, NULL);
2036 if (--wbc->nr_to_write == 0)
2039 pagevec_release(&pvec);
2042 if (wbc->nr_to_write == 0) {
2049 if (!is_sbi_flag_set(sbi, SBI_CP_DISABLED) &&
2050 wbc->sync_mode == WB_SYNC_NONE && step == 1)
2057 f2fs_submit_merged_write(sbi, NODE);
2059 if (unlikely(f2fs_cp_error(sbi)))
2064 int f2fs_wait_on_node_pages_writeback(struct f2fs_sb_info *sbi,
2065 unsigned int seq_id)
2067 struct fsync_node_entry *fn;
2069 struct list_head *head = &sbi->fsync_node_list;
2070 unsigned long flags;
2071 unsigned int cur_seq_id = 0;
2074 while (seq_id && cur_seq_id < seq_id) {
2075 spin_lock_irqsave(&sbi->fsync_node_lock, flags);
2076 if (list_empty(head)) {
2077 spin_unlock_irqrestore(&sbi->fsync_node_lock, flags);
2080 fn = list_first_entry(head, struct fsync_node_entry, list);
2081 if (fn->seq_id > seq_id) {
2082 spin_unlock_irqrestore(&sbi->fsync_node_lock, flags);
2085 cur_seq_id = fn->seq_id;
2088 spin_unlock_irqrestore(&sbi->fsync_node_lock, flags);
2090 f2fs_wait_on_page_writeback(page, NODE, true, false);
2091 if (TestClearPageError(page))
2100 ret2 = filemap_check_errors(NODE_MAPPING(sbi));
2107 static int f2fs_write_node_pages(struct address_space *mapping,
2108 struct writeback_control *wbc)
2110 struct f2fs_sb_info *sbi = F2FS_M_SB(mapping);
2111 struct blk_plug plug;
2114 if (unlikely(is_sbi_flag_set(sbi, SBI_POR_DOING)))
2117 /* balancing f2fs's metadata in background */
2118 f2fs_balance_fs_bg(sbi, true);
2120 /* collect a number of dirty node pages and write together */
2121 if (wbc->sync_mode != WB_SYNC_ALL &&
2122 get_pages(sbi, F2FS_DIRTY_NODES) <
2123 nr_pages_to_skip(sbi, NODE))
2126 if (wbc->sync_mode == WB_SYNC_ALL)
2127 atomic_inc(&sbi->wb_sync_req[NODE]);
2128 else if (atomic_read(&sbi->wb_sync_req[NODE])) {
2129 /* to avoid potential deadlock */
2131 blk_finish_plug(current->plug);
2135 trace_f2fs_writepages(mapping->host, wbc, NODE);
2137 diff = nr_pages_to_write(sbi, NODE, wbc);
2138 blk_start_plug(&plug);
2139 f2fs_sync_node_pages(sbi, wbc, true, FS_NODE_IO);
2140 blk_finish_plug(&plug);
2141 wbc->nr_to_write = max((long)0, wbc->nr_to_write - diff);
2143 if (wbc->sync_mode == WB_SYNC_ALL)
2144 atomic_dec(&sbi->wb_sync_req[NODE]);
2148 wbc->pages_skipped += get_pages(sbi, F2FS_DIRTY_NODES);
2149 trace_f2fs_writepages(mapping->host, wbc, NODE);
2153 static bool f2fs_dirty_node_folio(struct address_space *mapping,
2154 struct folio *folio)
2156 trace_f2fs_set_page_dirty(&folio->page, NODE);
2158 if (!folio_test_uptodate(folio))
2159 folio_mark_uptodate(folio);
2160 #ifdef CONFIG_F2FS_CHECK_FS
2161 if (IS_INODE(&folio->page))
2162 f2fs_inode_chksum_set(F2FS_M_SB(mapping), &folio->page);
2164 if (filemap_dirty_folio(mapping, folio)) {
2165 inc_page_count(F2FS_M_SB(mapping), F2FS_DIRTY_NODES);
2166 set_page_private_reference(&folio->page);
2173 * Structure of the f2fs node operations
2175 const struct address_space_operations f2fs_node_aops = {
2176 .writepage = f2fs_write_node_page,
2177 .writepages = f2fs_write_node_pages,
2178 .dirty_folio = f2fs_dirty_node_folio,
2179 .invalidate_folio = f2fs_invalidate_folio,
2180 .release_folio = f2fs_release_folio,
2181 .migrate_folio = filemap_migrate_folio,
2184 static struct free_nid *__lookup_free_nid_list(struct f2fs_nm_info *nm_i,
2187 return radix_tree_lookup(&nm_i->free_nid_root, n);
2190 static int __insert_free_nid(struct f2fs_sb_info *sbi,
2193 struct f2fs_nm_info *nm_i = NM_I(sbi);
2194 int err = radix_tree_insert(&nm_i->free_nid_root, i->nid, i);
2199 nm_i->nid_cnt[FREE_NID]++;
2200 list_add_tail(&i->list, &nm_i->free_nid_list);
2204 static void __remove_free_nid(struct f2fs_sb_info *sbi,
2205 struct free_nid *i, enum nid_state state)
2207 struct f2fs_nm_info *nm_i = NM_I(sbi);
2209 f2fs_bug_on(sbi, state != i->state);
2210 nm_i->nid_cnt[state]--;
2211 if (state == FREE_NID)
2213 radix_tree_delete(&nm_i->free_nid_root, i->nid);
2216 static void __move_free_nid(struct f2fs_sb_info *sbi, struct free_nid *i,
2217 enum nid_state org_state, enum nid_state dst_state)
2219 struct f2fs_nm_info *nm_i = NM_I(sbi);
2221 f2fs_bug_on(sbi, org_state != i->state);
2222 i->state = dst_state;
2223 nm_i->nid_cnt[org_state]--;
2224 nm_i->nid_cnt[dst_state]++;
2226 switch (dst_state) {
2231 list_add_tail(&i->list, &nm_i->free_nid_list);
2238 bool f2fs_nat_bitmap_enabled(struct f2fs_sb_info *sbi)
2240 struct f2fs_nm_info *nm_i = NM_I(sbi);
2244 f2fs_down_read(&nm_i->nat_tree_lock);
2245 for (i = 0; i < nm_i->nat_blocks; i++) {
2246 if (!test_bit_le(i, nm_i->nat_block_bitmap)) {
2251 f2fs_up_read(&nm_i->nat_tree_lock);
2256 static void update_free_nid_bitmap(struct f2fs_sb_info *sbi, nid_t nid,
2257 bool set, bool build)
2259 struct f2fs_nm_info *nm_i = NM_I(sbi);
2260 unsigned int nat_ofs = NAT_BLOCK_OFFSET(nid);
2261 unsigned int nid_ofs = nid - START_NID(nid);
2263 if (!test_bit_le(nat_ofs, nm_i->nat_block_bitmap))
2267 if (test_bit_le(nid_ofs, nm_i->free_nid_bitmap[nat_ofs]))
2269 __set_bit_le(nid_ofs, nm_i->free_nid_bitmap[nat_ofs]);
2270 nm_i->free_nid_count[nat_ofs]++;
2272 if (!test_bit_le(nid_ofs, nm_i->free_nid_bitmap[nat_ofs]))
2274 __clear_bit_le(nid_ofs, nm_i->free_nid_bitmap[nat_ofs]);
2276 nm_i->free_nid_count[nat_ofs]--;
2280 /* return if the nid is recognized as free */
2281 static bool add_free_nid(struct f2fs_sb_info *sbi,
2282 nid_t nid, bool build, bool update)
2284 struct f2fs_nm_info *nm_i = NM_I(sbi);
2285 struct free_nid *i, *e;
2286 struct nat_entry *ne;
2290 /* 0 nid should not be used */
2291 if (unlikely(nid == 0))
2294 if (unlikely(f2fs_check_nid_range(sbi, nid)))
2297 i = f2fs_kmem_cache_alloc(free_nid_slab, GFP_NOFS, true, NULL);
2299 i->state = FREE_NID;
2301 radix_tree_preload(GFP_NOFS | __GFP_NOFAIL);
2303 spin_lock(&nm_i->nid_list_lock);
2311 * - __insert_nid_to_list(PREALLOC_NID)
2312 * - f2fs_balance_fs_bg
2313 * - f2fs_build_free_nids
2314 * - __f2fs_build_free_nids
2317 * - __lookup_nat_cache
2319 * - f2fs_init_inode_metadata
2320 * - f2fs_new_inode_page
2321 * - f2fs_new_node_page
2323 * - f2fs_alloc_nid_done
2324 * - __remove_nid_from_list(PREALLOC_NID)
2325 * - __insert_nid_to_list(FREE_NID)
2327 ne = __lookup_nat_cache(nm_i, nid);
2328 if (ne && (!get_nat_flag(ne, IS_CHECKPOINTED) ||
2329 nat_get_blkaddr(ne) != NULL_ADDR))
2332 e = __lookup_free_nid_list(nm_i, nid);
2334 if (e->state == FREE_NID)
2340 err = __insert_free_nid(sbi, i);
2343 update_free_nid_bitmap(sbi, nid, ret, build);
2345 nm_i->available_nids++;
2347 spin_unlock(&nm_i->nid_list_lock);
2348 radix_tree_preload_end();
2351 kmem_cache_free(free_nid_slab, i);
2355 static void remove_free_nid(struct f2fs_sb_info *sbi, nid_t nid)
2357 struct f2fs_nm_info *nm_i = NM_I(sbi);
2359 bool need_free = false;
2361 spin_lock(&nm_i->nid_list_lock);
2362 i = __lookup_free_nid_list(nm_i, nid);
2363 if (i && i->state == FREE_NID) {
2364 __remove_free_nid(sbi, i, FREE_NID);
2367 spin_unlock(&nm_i->nid_list_lock);
2370 kmem_cache_free(free_nid_slab, i);
2373 static int scan_nat_page(struct f2fs_sb_info *sbi,
2374 struct page *nat_page, nid_t start_nid)
2376 struct f2fs_nm_info *nm_i = NM_I(sbi);
2377 struct f2fs_nat_block *nat_blk = page_address(nat_page);
2379 unsigned int nat_ofs = NAT_BLOCK_OFFSET(start_nid);
2382 __set_bit_le(nat_ofs, nm_i->nat_block_bitmap);
2384 i = start_nid % NAT_ENTRY_PER_BLOCK;
2386 for (; i < NAT_ENTRY_PER_BLOCK; i++, start_nid++) {
2387 if (unlikely(start_nid >= nm_i->max_nid))
2390 blk_addr = le32_to_cpu(nat_blk->entries[i].block_addr);
2392 if (blk_addr == NEW_ADDR)
2395 if (blk_addr == NULL_ADDR) {
2396 add_free_nid(sbi, start_nid, true, true);
2398 spin_lock(&NM_I(sbi)->nid_list_lock);
2399 update_free_nid_bitmap(sbi, start_nid, false, true);
2400 spin_unlock(&NM_I(sbi)->nid_list_lock);
2407 static void scan_curseg_cache(struct f2fs_sb_info *sbi)
2409 struct curseg_info *curseg = CURSEG_I(sbi, CURSEG_HOT_DATA);
2410 struct f2fs_journal *journal = curseg->journal;
2413 down_read(&curseg->journal_rwsem);
2414 for (i = 0; i < nats_in_cursum(journal); i++) {
2418 addr = le32_to_cpu(nat_in_journal(journal, i).block_addr);
2419 nid = le32_to_cpu(nid_in_journal(journal, i));
2420 if (addr == NULL_ADDR)
2421 add_free_nid(sbi, nid, true, false);
2423 remove_free_nid(sbi, nid);
2425 up_read(&curseg->journal_rwsem);
2428 static void scan_free_nid_bits(struct f2fs_sb_info *sbi)
2430 struct f2fs_nm_info *nm_i = NM_I(sbi);
2431 unsigned int i, idx;
2434 f2fs_down_read(&nm_i->nat_tree_lock);
2436 for (i = 0; i < nm_i->nat_blocks; i++) {
2437 if (!test_bit_le(i, nm_i->nat_block_bitmap))
2439 if (!nm_i->free_nid_count[i])
2441 for (idx = 0; idx < NAT_ENTRY_PER_BLOCK; idx++) {
2442 idx = find_next_bit_le(nm_i->free_nid_bitmap[i],
2443 NAT_ENTRY_PER_BLOCK, idx);
2444 if (idx >= NAT_ENTRY_PER_BLOCK)
2447 nid = i * NAT_ENTRY_PER_BLOCK + idx;
2448 add_free_nid(sbi, nid, true, false);
2450 if (nm_i->nid_cnt[FREE_NID] >= MAX_FREE_NIDS)
2455 scan_curseg_cache(sbi);
2457 f2fs_up_read(&nm_i->nat_tree_lock);
2460 static int __f2fs_build_free_nids(struct f2fs_sb_info *sbi,
2461 bool sync, bool mount)
2463 struct f2fs_nm_info *nm_i = NM_I(sbi);
2465 nid_t nid = nm_i->next_scan_nid;
2467 if (unlikely(nid >= nm_i->max_nid))
2470 if (unlikely(nid % NAT_ENTRY_PER_BLOCK))
2471 nid = NAT_BLOCK_OFFSET(nid) * NAT_ENTRY_PER_BLOCK;
2473 /* Enough entries */
2474 if (nm_i->nid_cnt[FREE_NID] >= NAT_ENTRY_PER_BLOCK)
2477 if (!sync && !f2fs_available_free_memory(sbi, FREE_NIDS))
2481 /* try to find free nids in free_nid_bitmap */
2482 scan_free_nid_bits(sbi);
2484 if (nm_i->nid_cnt[FREE_NID] >= NAT_ENTRY_PER_BLOCK)
2488 /* readahead nat pages to be scanned */
2489 f2fs_ra_meta_pages(sbi, NAT_BLOCK_OFFSET(nid), FREE_NID_PAGES,
2492 f2fs_down_read(&nm_i->nat_tree_lock);
2495 if (!test_bit_le(NAT_BLOCK_OFFSET(nid),
2496 nm_i->nat_block_bitmap)) {
2497 struct page *page = get_current_nat_page(sbi, nid);
2500 ret = PTR_ERR(page);
2502 ret = scan_nat_page(sbi, page, nid);
2503 f2fs_put_page(page, 1);
2507 f2fs_up_read(&nm_i->nat_tree_lock);
2508 f2fs_err(sbi, "NAT is corrupt, run fsck to fix it");
2513 nid += (NAT_ENTRY_PER_BLOCK - (nid % NAT_ENTRY_PER_BLOCK));
2514 if (unlikely(nid >= nm_i->max_nid))
2517 if (++i >= FREE_NID_PAGES)
2521 /* go to the next free nat pages to find free nids abundantly */
2522 nm_i->next_scan_nid = nid;
2524 /* find free nids from current sum_pages */
2525 scan_curseg_cache(sbi);
2527 f2fs_up_read(&nm_i->nat_tree_lock);
2529 f2fs_ra_meta_pages(sbi, NAT_BLOCK_OFFSET(nm_i->next_scan_nid),
2530 nm_i->ra_nid_pages, META_NAT, false);
2535 int f2fs_build_free_nids(struct f2fs_sb_info *sbi, bool sync, bool mount)
2539 mutex_lock(&NM_I(sbi)->build_lock);
2540 ret = __f2fs_build_free_nids(sbi, sync, mount);
2541 mutex_unlock(&NM_I(sbi)->build_lock);
2547 * If this function returns success, caller can obtain a new nid
2548 * from second parameter of this function.
2549 * The returned nid could be used ino as well as nid when inode is created.
2551 bool f2fs_alloc_nid(struct f2fs_sb_info *sbi, nid_t *nid)
2553 struct f2fs_nm_info *nm_i = NM_I(sbi);
2554 struct free_nid *i = NULL;
2556 if (time_to_inject(sbi, FAULT_ALLOC_NID)) {
2557 f2fs_show_injection_info(sbi, FAULT_ALLOC_NID);
2561 spin_lock(&nm_i->nid_list_lock);
2563 if (unlikely(nm_i->available_nids == 0)) {
2564 spin_unlock(&nm_i->nid_list_lock);
2568 /* We should not use stale free nids created by f2fs_build_free_nids */
2569 if (nm_i->nid_cnt[FREE_NID] && !on_f2fs_build_free_nids(nm_i)) {
2570 f2fs_bug_on(sbi, list_empty(&nm_i->free_nid_list));
2571 i = list_first_entry(&nm_i->free_nid_list,
2572 struct free_nid, list);
2575 __move_free_nid(sbi, i, FREE_NID, PREALLOC_NID);
2576 nm_i->available_nids--;
2578 update_free_nid_bitmap(sbi, *nid, false, false);
2580 spin_unlock(&nm_i->nid_list_lock);
2583 spin_unlock(&nm_i->nid_list_lock);
2585 /* Let's scan nat pages and its caches to get free nids */
2586 if (!f2fs_build_free_nids(sbi, true, false))
2592 * f2fs_alloc_nid() should be called prior to this function.
2594 void f2fs_alloc_nid_done(struct f2fs_sb_info *sbi, nid_t nid)
2596 struct f2fs_nm_info *nm_i = NM_I(sbi);
2599 spin_lock(&nm_i->nid_list_lock);
2600 i = __lookup_free_nid_list(nm_i, nid);
2601 f2fs_bug_on(sbi, !i);
2602 __remove_free_nid(sbi, i, PREALLOC_NID);
2603 spin_unlock(&nm_i->nid_list_lock);
2605 kmem_cache_free(free_nid_slab, i);
2609 * f2fs_alloc_nid() should be called prior to this function.
2611 void f2fs_alloc_nid_failed(struct f2fs_sb_info *sbi, nid_t nid)
2613 struct f2fs_nm_info *nm_i = NM_I(sbi);
2615 bool need_free = false;
2620 spin_lock(&nm_i->nid_list_lock);
2621 i = __lookup_free_nid_list(nm_i, nid);
2622 f2fs_bug_on(sbi, !i);
2624 if (!f2fs_available_free_memory(sbi, FREE_NIDS)) {
2625 __remove_free_nid(sbi, i, PREALLOC_NID);
2628 __move_free_nid(sbi, i, PREALLOC_NID, FREE_NID);
2631 nm_i->available_nids++;
2633 update_free_nid_bitmap(sbi, nid, true, false);
2635 spin_unlock(&nm_i->nid_list_lock);
2638 kmem_cache_free(free_nid_slab, i);
2641 int f2fs_try_to_free_nids(struct f2fs_sb_info *sbi, int nr_shrink)
2643 struct f2fs_nm_info *nm_i = NM_I(sbi);
2646 if (nm_i->nid_cnt[FREE_NID] <= MAX_FREE_NIDS)
2649 if (!mutex_trylock(&nm_i->build_lock))
2652 while (nr_shrink && nm_i->nid_cnt[FREE_NID] > MAX_FREE_NIDS) {
2653 struct free_nid *i, *next;
2654 unsigned int batch = SHRINK_NID_BATCH_SIZE;
2656 spin_lock(&nm_i->nid_list_lock);
2657 list_for_each_entry_safe(i, next, &nm_i->free_nid_list, list) {
2658 if (!nr_shrink || !batch ||
2659 nm_i->nid_cnt[FREE_NID] <= MAX_FREE_NIDS)
2661 __remove_free_nid(sbi, i, FREE_NID);
2662 kmem_cache_free(free_nid_slab, i);
2666 spin_unlock(&nm_i->nid_list_lock);
2669 mutex_unlock(&nm_i->build_lock);
2671 return nr - nr_shrink;
2674 int f2fs_recover_inline_xattr(struct inode *inode, struct page *page)
2676 void *src_addr, *dst_addr;
2679 struct f2fs_inode *ri;
2681 ipage = f2fs_get_node_page(F2FS_I_SB(inode), inode->i_ino);
2683 return PTR_ERR(ipage);
2685 ri = F2FS_INODE(page);
2686 if (ri->i_inline & F2FS_INLINE_XATTR) {
2687 if (!f2fs_has_inline_xattr(inode)) {
2688 set_inode_flag(inode, FI_INLINE_XATTR);
2689 stat_inc_inline_xattr(inode);
2692 if (f2fs_has_inline_xattr(inode)) {
2693 stat_dec_inline_xattr(inode);
2694 clear_inode_flag(inode, FI_INLINE_XATTR);
2699 dst_addr = inline_xattr_addr(inode, ipage);
2700 src_addr = inline_xattr_addr(inode, page);
2701 inline_size = inline_xattr_size(inode);
2703 f2fs_wait_on_page_writeback(ipage, NODE, true, true);
2704 memcpy(dst_addr, src_addr, inline_size);
2706 f2fs_update_inode(inode, ipage);
2707 f2fs_put_page(ipage, 1);
2711 int f2fs_recover_xattr_data(struct inode *inode, struct page *page)
2713 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
2714 nid_t prev_xnid = F2FS_I(inode)->i_xattr_nid;
2716 struct dnode_of_data dn;
2717 struct node_info ni;
2724 /* 1: invalidate the previous xattr nid */
2725 err = f2fs_get_node_info(sbi, prev_xnid, &ni, false);
2729 f2fs_invalidate_blocks(sbi, ni.blk_addr);
2730 dec_valid_node_count(sbi, inode, false);
2731 set_node_addr(sbi, &ni, NULL_ADDR, false);
2734 /* 2: update xattr nid in inode */
2735 if (!f2fs_alloc_nid(sbi, &new_xnid))
2738 set_new_dnode(&dn, inode, NULL, NULL, new_xnid);
2739 xpage = f2fs_new_node_page(&dn, XATTR_NODE_OFFSET);
2740 if (IS_ERR(xpage)) {
2741 f2fs_alloc_nid_failed(sbi, new_xnid);
2742 return PTR_ERR(xpage);
2745 f2fs_alloc_nid_done(sbi, new_xnid);
2746 f2fs_update_inode_page(inode);
2748 /* 3: update and set xattr node page dirty */
2750 memcpy(F2FS_NODE(xpage), F2FS_NODE(page),
2751 VALID_XATTR_BLOCK_SIZE);
2752 set_page_dirty(xpage);
2754 f2fs_put_page(xpage, 1);
2759 int f2fs_recover_inode_page(struct f2fs_sb_info *sbi, struct page *page)
2761 struct f2fs_inode *src, *dst;
2762 nid_t ino = ino_of_node(page);
2763 struct node_info old_ni, new_ni;
2767 err = f2fs_get_node_info(sbi, ino, &old_ni, false);
2771 if (unlikely(old_ni.blk_addr != NULL_ADDR))
2774 ipage = f2fs_grab_cache_page(NODE_MAPPING(sbi), ino, false);
2776 memalloc_retry_wait(GFP_NOFS);
2780 /* Should not use this inode from free nid list */
2781 remove_free_nid(sbi, ino);
2783 if (!PageUptodate(ipage))
2784 SetPageUptodate(ipage);
2785 fill_node_footer(ipage, ino, ino, 0, true);
2786 set_cold_node(ipage, false);
2788 src = F2FS_INODE(page);
2789 dst = F2FS_INODE(ipage);
2791 memcpy(dst, src, offsetof(struct f2fs_inode, i_ext));
2793 dst->i_blocks = cpu_to_le64(1);
2794 dst->i_links = cpu_to_le32(1);
2795 dst->i_xattr_nid = 0;
2796 dst->i_inline = src->i_inline & (F2FS_INLINE_XATTR | F2FS_EXTRA_ATTR);
2797 if (dst->i_inline & F2FS_EXTRA_ATTR) {
2798 dst->i_extra_isize = src->i_extra_isize;
2800 if (f2fs_sb_has_flexible_inline_xattr(sbi) &&
2801 F2FS_FITS_IN_INODE(src, le16_to_cpu(src->i_extra_isize),
2802 i_inline_xattr_size))
2803 dst->i_inline_xattr_size = src->i_inline_xattr_size;
2805 if (f2fs_sb_has_project_quota(sbi) &&
2806 F2FS_FITS_IN_INODE(src, le16_to_cpu(src->i_extra_isize),
2808 dst->i_projid = src->i_projid;
2810 if (f2fs_sb_has_inode_crtime(sbi) &&
2811 F2FS_FITS_IN_INODE(src, le16_to_cpu(src->i_extra_isize),
2813 dst->i_crtime = src->i_crtime;
2814 dst->i_crtime_nsec = src->i_crtime_nsec;
2821 if (unlikely(inc_valid_node_count(sbi, NULL, true)))
2823 set_node_addr(sbi, &new_ni, NEW_ADDR, false);
2824 inc_valid_inode_count(sbi);
2825 set_page_dirty(ipage);
2826 f2fs_put_page(ipage, 1);
2830 int f2fs_restore_node_summary(struct f2fs_sb_info *sbi,
2831 unsigned int segno, struct f2fs_summary_block *sum)
2833 struct f2fs_node *rn;
2834 struct f2fs_summary *sum_entry;
2836 int i, idx, last_offset, nrpages;
2838 /* scan the node segment */
2839 last_offset = sbi->blocks_per_seg;
2840 addr = START_BLOCK(sbi, segno);
2841 sum_entry = &sum->entries[0];
2843 for (i = 0; i < last_offset; i += nrpages, addr += nrpages) {
2844 nrpages = bio_max_segs(last_offset - i);
2846 /* readahead node pages */
2847 f2fs_ra_meta_pages(sbi, addr, nrpages, META_POR, true);
2849 for (idx = addr; idx < addr + nrpages; idx++) {
2850 struct page *page = f2fs_get_tmp_page(sbi, idx);
2853 return PTR_ERR(page);
2855 rn = F2FS_NODE(page);
2856 sum_entry->nid = rn->footer.nid;
2857 sum_entry->version = 0;
2858 sum_entry->ofs_in_node = 0;
2860 f2fs_put_page(page, 1);
2863 invalidate_mapping_pages(META_MAPPING(sbi), addr,
2869 static void remove_nats_in_journal(struct f2fs_sb_info *sbi)
2871 struct f2fs_nm_info *nm_i = NM_I(sbi);
2872 struct curseg_info *curseg = CURSEG_I(sbi, CURSEG_HOT_DATA);
2873 struct f2fs_journal *journal = curseg->journal;
2876 down_write(&curseg->journal_rwsem);
2877 for (i = 0; i < nats_in_cursum(journal); i++) {
2878 struct nat_entry *ne;
2879 struct f2fs_nat_entry raw_ne;
2880 nid_t nid = le32_to_cpu(nid_in_journal(journal, i));
2882 if (f2fs_check_nid_range(sbi, nid))
2885 raw_ne = nat_in_journal(journal, i);
2887 ne = __lookup_nat_cache(nm_i, nid);
2889 ne = __alloc_nat_entry(sbi, nid, true);
2890 __init_nat_entry(nm_i, ne, &raw_ne, true);
2894 * if a free nat in journal has not been used after last
2895 * checkpoint, we should remove it from available nids,
2896 * since later we will add it again.
2898 if (!get_nat_flag(ne, IS_DIRTY) &&
2899 le32_to_cpu(raw_ne.block_addr) == NULL_ADDR) {
2900 spin_lock(&nm_i->nid_list_lock);
2901 nm_i->available_nids--;
2902 spin_unlock(&nm_i->nid_list_lock);
2905 __set_nat_cache_dirty(nm_i, ne);
2907 update_nats_in_cursum(journal, -i);
2908 up_write(&curseg->journal_rwsem);
2911 static void __adjust_nat_entry_set(struct nat_entry_set *nes,
2912 struct list_head *head, int max)
2914 struct nat_entry_set *cur;
2916 if (nes->entry_cnt >= max)
2919 list_for_each_entry(cur, head, set_list) {
2920 if (cur->entry_cnt >= nes->entry_cnt) {
2921 list_add(&nes->set_list, cur->set_list.prev);
2926 list_add_tail(&nes->set_list, head);
2929 static void __update_nat_bits(struct f2fs_nm_info *nm_i, unsigned int nat_ofs,
2933 __set_bit_le(nat_ofs, nm_i->empty_nat_bits);
2934 __clear_bit_le(nat_ofs, nm_i->full_nat_bits);
2938 __clear_bit_le(nat_ofs, nm_i->empty_nat_bits);
2939 if (valid == NAT_ENTRY_PER_BLOCK)
2940 __set_bit_le(nat_ofs, nm_i->full_nat_bits);
2942 __clear_bit_le(nat_ofs, nm_i->full_nat_bits);
2945 static void update_nat_bits(struct f2fs_sb_info *sbi, nid_t start_nid,
2948 struct f2fs_nm_info *nm_i = NM_I(sbi);
2949 unsigned int nat_index = start_nid / NAT_ENTRY_PER_BLOCK;
2950 struct f2fs_nat_block *nat_blk = page_address(page);
2954 if (!is_set_ckpt_flags(sbi, CP_NAT_BITS_FLAG))
2957 if (nat_index == 0) {
2961 for (; i < NAT_ENTRY_PER_BLOCK; i++) {
2962 if (le32_to_cpu(nat_blk->entries[i].block_addr) != NULL_ADDR)
2966 __update_nat_bits(nm_i, nat_index, valid);
2969 void f2fs_enable_nat_bits(struct f2fs_sb_info *sbi)
2971 struct f2fs_nm_info *nm_i = NM_I(sbi);
2972 unsigned int nat_ofs;
2974 f2fs_down_read(&nm_i->nat_tree_lock);
2976 for (nat_ofs = 0; nat_ofs < nm_i->nat_blocks; nat_ofs++) {
2977 unsigned int valid = 0, nid_ofs = 0;
2979 /* handle nid zero due to it should never be used */
2980 if (unlikely(nat_ofs == 0)) {
2985 for (; nid_ofs < NAT_ENTRY_PER_BLOCK; nid_ofs++) {
2986 if (!test_bit_le(nid_ofs,
2987 nm_i->free_nid_bitmap[nat_ofs]))
2991 __update_nat_bits(nm_i, nat_ofs, valid);
2994 f2fs_up_read(&nm_i->nat_tree_lock);
2997 static int __flush_nat_entry_set(struct f2fs_sb_info *sbi,
2998 struct nat_entry_set *set, struct cp_control *cpc)
3000 struct curseg_info *curseg = CURSEG_I(sbi, CURSEG_HOT_DATA);
3001 struct f2fs_journal *journal = curseg->journal;
3002 nid_t start_nid = set->set * NAT_ENTRY_PER_BLOCK;
3003 bool to_journal = true;
3004 struct f2fs_nat_block *nat_blk;
3005 struct nat_entry *ne, *cur;
3006 struct page *page = NULL;
3009 * there are two steps to flush nat entries:
3010 * #1, flush nat entries to journal in current hot data summary block.
3011 * #2, flush nat entries to nat page.
3013 if ((cpc->reason & CP_UMOUNT) ||
3014 !__has_cursum_space(journal, set->entry_cnt, NAT_JOURNAL))
3018 down_write(&curseg->journal_rwsem);
3020 page = get_next_nat_page(sbi, start_nid);
3022 return PTR_ERR(page);
3024 nat_blk = page_address(page);
3025 f2fs_bug_on(sbi, !nat_blk);
3028 /* flush dirty nats in nat entry set */
3029 list_for_each_entry_safe(ne, cur, &set->entry_list, list) {
3030 struct f2fs_nat_entry *raw_ne;
3031 nid_t nid = nat_get_nid(ne);
3034 f2fs_bug_on(sbi, nat_get_blkaddr(ne) == NEW_ADDR);
3037 offset = f2fs_lookup_journal_in_cursum(journal,
3038 NAT_JOURNAL, nid, 1);
3039 f2fs_bug_on(sbi, offset < 0);
3040 raw_ne = &nat_in_journal(journal, offset);
3041 nid_in_journal(journal, offset) = cpu_to_le32(nid);
3043 raw_ne = &nat_blk->entries[nid - start_nid];
3045 raw_nat_from_node_info(raw_ne, &ne->ni);
3047 __clear_nat_cache_dirty(NM_I(sbi), set, ne);
3048 if (nat_get_blkaddr(ne) == NULL_ADDR) {
3049 add_free_nid(sbi, nid, false, true);
3051 spin_lock(&NM_I(sbi)->nid_list_lock);
3052 update_free_nid_bitmap(sbi, nid, false, false);
3053 spin_unlock(&NM_I(sbi)->nid_list_lock);
3058 up_write(&curseg->journal_rwsem);
3060 update_nat_bits(sbi, start_nid, page);
3061 f2fs_put_page(page, 1);
3064 /* Allow dirty nats by node block allocation in write_begin */
3065 if (!set->entry_cnt) {
3066 radix_tree_delete(&NM_I(sbi)->nat_set_root, set->set);
3067 kmem_cache_free(nat_entry_set_slab, set);
3073 * This function is called during the checkpointing process.
3075 int f2fs_flush_nat_entries(struct f2fs_sb_info *sbi, struct cp_control *cpc)
3077 struct f2fs_nm_info *nm_i = NM_I(sbi);
3078 struct curseg_info *curseg = CURSEG_I(sbi, CURSEG_HOT_DATA);
3079 struct f2fs_journal *journal = curseg->journal;
3080 struct nat_entry_set *setvec[SETVEC_SIZE];
3081 struct nat_entry_set *set, *tmp;
3088 * during unmount, let's flush nat_bits before checking
3089 * nat_cnt[DIRTY_NAT].
3091 if (cpc->reason & CP_UMOUNT) {
3092 f2fs_down_write(&nm_i->nat_tree_lock);
3093 remove_nats_in_journal(sbi);
3094 f2fs_up_write(&nm_i->nat_tree_lock);
3097 if (!nm_i->nat_cnt[DIRTY_NAT])
3100 f2fs_down_write(&nm_i->nat_tree_lock);
3103 * if there are no enough space in journal to store dirty nat
3104 * entries, remove all entries from journal and merge them
3105 * into nat entry set.
3107 if (cpc->reason & CP_UMOUNT ||
3108 !__has_cursum_space(journal,
3109 nm_i->nat_cnt[DIRTY_NAT], NAT_JOURNAL))
3110 remove_nats_in_journal(sbi);
3112 while ((found = __gang_lookup_nat_set(nm_i,
3113 set_idx, SETVEC_SIZE, setvec))) {
3116 set_idx = setvec[found - 1]->set + 1;
3117 for (idx = 0; idx < found; idx++)
3118 __adjust_nat_entry_set(setvec[idx], &sets,
3119 MAX_NAT_JENTRIES(journal));
3122 /* flush dirty nats in nat entry set */
3123 list_for_each_entry_safe(set, tmp, &sets, set_list) {
3124 err = __flush_nat_entry_set(sbi, set, cpc);
3129 f2fs_up_write(&nm_i->nat_tree_lock);
3130 /* Allow dirty nats by node block allocation in write_begin */
3135 static int __get_nat_bitmaps(struct f2fs_sb_info *sbi)
3137 struct f2fs_checkpoint *ckpt = F2FS_CKPT(sbi);
3138 struct f2fs_nm_info *nm_i = NM_I(sbi);
3139 unsigned int nat_bits_bytes = nm_i->nat_blocks / BITS_PER_BYTE;
3141 __u64 cp_ver = cur_cp_version(ckpt);
3142 block_t nat_bits_addr;
3144 nm_i->nat_bits_blocks = F2FS_BLK_ALIGN((nat_bits_bytes << 1) + 8);
3145 nm_i->nat_bits = f2fs_kvzalloc(sbi,
3146 nm_i->nat_bits_blocks << F2FS_BLKSIZE_BITS, GFP_KERNEL);
3147 if (!nm_i->nat_bits)
3150 nm_i->full_nat_bits = nm_i->nat_bits + 8;
3151 nm_i->empty_nat_bits = nm_i->full_nat_bits + nat_bits_bytes;
3153 if (!is_set_ckpt_flags(sbi, CP_NAT_BITS_FLAG))
3156 nat_bits_addr = __start_cp_addr(sbi) + sbi->blocks_per_seg -
3157 nm_i->nat_bits_blocks;
3158 for (i = 0; i < nm_i->nat_bits_blocks; i++) {
3161 page = f2fs_get_meta_page(sbi, nat_bits_addr++);
3163 return PTR_ERR(page);
3165 memcpy(nm_i->nat_bits + (i << F2FS_BLKSIZE_BITS),
3166 page_address(page), F2FS_BLKSIZE);
3167 f2fs_put_page(page, 1);
3170 cp_ver |= (cur_cp_crc(ckpt) << 32);
3171 if (cpu_to_le64(cp_ver) != *(__le64 *)nm_i->nat_bits) {
3172 clear_ckpt_flags(sbi, CP_NAT_BITS_FLAG);
3173 f2fs_notice(sbi, "Disable nat_bits due to incorrect cp_ver (%llu, %llu)",
3174 cp_ver, le64_to_cpu(*(__le64 *)nm_i->nat_bits));
3178 f2fs_notice(sbi, "Found nat_bits in checkpoint");
3182 static inline void load_free_nid_bitmap(struct f2fs_sb_info *sbi)
3184 struct f2fs_nm_info *nm_i = NM_I(sbi);
3186 nid_t nid, last_nid;
3188 if (!is_set_ckpt_flags(sbi, CP_NAT_BITS_FLAG))
3191 for (i = 0; i < nm_i->nat_blocks; i++) {
3192 i = find_next_bit_le(nm_i->empty_nat_bits, nm_i->nat_blocks, i);
3193 if (i >= nm_i->nat_blocks)
3196 __set_bit_le(i, nm_i->nat_block_bitmap);
3198 nid = i * NAT_ENTRY_PER_BLOCK;
3199 last_nid = nid + NAT_ENTRY_PER_BLOCK;
3201 spin_lock(&NM_I(sbi)->nid_list_lock);
3202 for (; nid < last_nid; nid++)
3203 update_free_nid_bitmap(sbi, nid, true, true);
3204 spin_unlock(&NM_I(sbi)->nid_list_lock);
3207 for (i = 0; i < nm_i->nat_blocks; i++) {
3208 i = find_next_bit_le(nm_i->full_nat_bits, nm_i->nat_blocks, i);
3209 if (i >= nm_i->nat_blocks)
3212 __set_bit_le(i, nm_i->nat_block_bitmap);
3216 static int init_node_manager(struct f2fs_sb_info *sbi)
3218 struct f2fs_super_block *sb_raw = F2FS_RAW_SUPER(sbi);
3219 struct f2fs_nm_info *nm_i = NM_I(sbi);
3220 unsigned char *version_bitmap;
3221 unsigned int nat_segs;
3224 nm_i->nat_blkaddr = le32_to_cpu(sb_raw->nat_blkaddr);
3226 /* segment_count_nat includes pair segment so divide to 2. */
3227 nat_segs = le32_to_cpu(sb_raw->segment_count_nat) >> 1;
3228 nm_i->nat_blocks = nat_segs << le32_to_cpu(sb_raw->log_blocks_per_seg);
3229 nm_i->max_nid = NAT_ENTRY_PER_BLOCK * nm_i->nat_blocks;
3231 /* not used nids: 0, node, meta, (and root counted as valid node) */
3232 nm_i->available_nids = nm_i->max_nid - sbi->total_valid_node_count -
3233 F2FS_RESERVED_NODE_NUM;
3234 nm_i->nid_cnt[FREE_NID] = 0;
3235 nm_i->nid_cnt[PREALLOC_NID] = 0;
3236 nm_i->ram_thresh = DEF_RAM_THRESHOLD;
3237 nm_i->ra_nid_pages = DEF_RA_NID_PAGES;
3238 nm_i->dirty_nats_ratio = DEF_DIRTY_NAT_RATIO_THRESHOLD;
3239 nm_i->max_rf_node_blocks = DEF_RF_NODE_BLOCKS;
3241 INIT_RADIX_TREE(&nm_i->free_nid_root, GFP_ATOMIC);
3242 INIT_LIST_HEAD(&nm_i->free_nid_list);
3243 INIT_RADIX_TREE(&nm_i->nat_root, GFP_NOIO);
3244 INIT_RADIX_TREE(&nm_i->nat_set_root, GFP_NOIO);
3245 INIT_LIST_HEAD(&nm_i->nat_entries);
3246 spin_lock_init(&nm_i->nat_list_lock);
3248 mutex_init(&nm_i->build_lock);
3249 spin_lock_init(&nm_i->nid_list_lock);
3250 init_f2fs_rwsem(&nm_i->nat_tree_lock);
3252 nm_i->next_scan_nid = le32_to_cpu(sbi->ckpt->next_free_nid);
3253 nm_i->bitmap_size = __bitmap_size(sbi, NAT_BITMAP);
3254 version_bitmap = __bitmap_ptr(sbi, NAT_BITMAP);
3255 nm_i->nat_bitmap = kmemdup(version_bitmap, nm_i->bitmap_size,
3257 if (!nm_i->nat_bitmap)
3260 err = __get_nat_bitmaps(sbi);
3264 #ifdef CONFIG_F2FS_CHECK_FS
3265 nm_i->nat_bitmap_mir = kmemdup(version_bitmap, nm_i->bitmap_size,
3267 if (!nm_i->nat_bitmap_mir)
3274 static int init_free_nid_cache(struct f2fs_sb_info *sbi)
3276 struct f2fs_nm_info *nm_i = NM_I(sbi);
3279 nm_i->free_nid_bitmap =
3280 f2fs_kvzalloc(sbi, array_size(sizeof(unsigned char *),
3283 if (!nm_i->free_nid_bitmap)
3286 for (i = 0; i < nm_i->nat_blocks; i++) {
3287 nm_i->free_nid_bitmap[i] = f2fs_kvzalloc(sbi,
3288 f2fs_bitmap_size(NAT_ENTRY_PER_BLOCK), GFP_KERNEL);
3289 if (!nm_i->free_nid_bitmap[i])
3293 nm_i->nat_block_bitmap = f2fs_kvzalloc(sbi, nm_i->nat_blocks / 8,
3295 if (!nm_i->nat_block_bitmap)
3298 nm_i->free_nid_count =
3299 f2fs_kvzalloc(sbi, array_size(sizeof(unsigned short),
3302 if (!nm_i->free_nid_count)
3307 int f2fs_build_node_manager(struct f2fs_sb_info *sbi)
3311 sbi->nm_info = f2fs_kzalloc(sbi, sizeof(struct f2fs_nm_info),
3316 err = init_node_manager(sbi);
3320 err = init_free_nid_cache(sbi);
3324 /* load free nid status from nat_bits table */
3325 load_free_nid_bitmap(sbi);
3327 return f2fs_build_free_nids(sbi, true, true);
3330 void f2fs_destroy_node_manager(struct f2fs_sb_info *sbi)
3332 struct f2fs_nm_info *nm_i = NM_I(sbi);
3333 struct free_nid *i, *next_i;
3334 struct nat_entry *natvec[NATVEC_SIZE];
3335 struct nat_entry_set *setvec[SETVEC_SIZE];
3342 /* destroy free nid list */
3343 spin_lock(&nm_i->nid_list_lock);
3344 list_for_each_entry_safe(i, next_i, &nm_i->free_nid_list, list) {
3345 __remove_free_nid(sbi, i, FREE_NID);
3346 spin_unlock(&nm_i->nid_list_lock);
3347 kmem_cache_free(free_nid_slab, i);
3348 spin_lock(&nm_i->nid_list_lock);
3350 f2fs_bug_on(sbi, nm_i->nid_cnt[FREE_NID]);
3351 f2fs_bug_on(sbi, nm_i->nid_cnt[PREALLOC_NID]);
3352 f2fs_bug_on(sbi, !list_empty(&nm_i->free_nid_list));
3353 spin_unlock(&nm_i->nid_list_lock);
3355 /* destroy nat cache */
3356 f2fs_down_write(&nm_i->nat_tree_lock);
3357 while ((found = __gang_lookup_nat_cache(nm_i,
3358 nid, NATVEC_SIZE, natvec))) {
3361 nid = nat_get_nid(natvec[found - 1]) + 1;
3362 for (idx = 0; idx < found; idx++) {
3363 spin_lock(&nm_i->nat_list_lock);
3364 list_del(&natvec[idx]->list);
3365 spin_unlock(&nm_i->nat_list_lock);
3367 __del_from_nat_cache(nm_i, natvec[idx]);
3370 f2fs_bug_on(sbi, nm_i->nat_cnt[TOTAL_NAT]);
3372 /* destroy nat set cache */
3374 while ((found = __gang_lookup_nat_set(nm_i,
3375 nid, SETVEC_SIZE, setvec))) {
3378 nid = setvec[found - 1]->set + 1;
3379 for (idx = 0; idx < found; idx++) {
3380 /* entry_cnt is not zero, when cp_error was occurred */
3381 f2fs_bug_on(sbi, !list_empty(&setvec[idx]->entry_list));
3382 radix_tree_delete(&nm_i->nat_set_root, setvec[idx]->set);
3383 kmem_cache_free(nat_entry_set_slab, setvec[idx]);
3386 f2fs_up_write(&nm_i->nat_tree_lock);
3388 kvfree(nm_i->nat_block_bitmap);
3389 if (nm_i->free_nid_bitmap) {
3392 for (i = 0; i < nm_i->nat_blocks; i++)
3393 kvfree(nm_i->free_nid_bitmap[i]);
3394 kvfree(nm_i->free_nid_bitmap);
3396 kvfree(nm_i->free_nid_count);
3398 kvfree(nm_i->nat_bitmap);
3399 kvfree(nm_i->nat_bits);
3400 #ifdef CONFIG_F2FS_CHECK_FS
3401 kvfree(nm_i->nat_bitmap_mir);
3403 sbi->nm_info = NULL;
3407 int __init f2fs_create_node_manager_caches(void)
3409 nat_entry_slab = f2fs_kmem_cache_create("f2fs_nat_entry",
3410 sizeof(struct nat_entry));
3411 if (!nat_entry_slab)
3414 free_nid_slab = f2fs_kmem_cache_create("f2fs_free_nid",
3415 sizeof(struct free_nid));
3417 goto destroy_nat_entry;
3419 nat_entry_set_slab = f2fs_kmem_cache_create("f2fs_nat_entry_set",
3420 sizeof(struct nat_entry_set));
3421 if (!nat_entry_set_slab)
3422 goto destroy_free_nid;
3424 fsync_node_entry_slab = f2fs_kmem_cache_create("f2fs_fsync_node_entry",
3425 sizeof(struct fsync_node_entry));
3426 if (!fsync_node_entry_slab)
3427 goto destroy_nat_entry_set;
3430 destroy_nat_entry_set:
3431 kmem_cache_destroy(nat_entry_set_slab);
3433 kmem_cache_destroy(free_nid_slab);
3435 kmem_cache_destroy(nat_entry_slab);
3440 void f2fs_destroy_node_manager_caches(void)
3442 kmem_cache_destroy(fsync_node_entry_slab);
3443 kmem_cache_destroy(nat_entry_set_slab);
3444 kmem_cache_destroy(free_nid_slab);
3445 kmem_cache_destroy(nat_entry_slab);