4 * Copyright (c) 2012 Samsung Electronics Co., Ltd.
5 * http://www.samsung.com/
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License version 2 as
9 * published by the Free Software Foundation.
12 #include <linux/f2fs_fs.h>
13 #include <linux/mpage.h>
14 #include <linux/backing-dev.h>
15 #include <linux/blkdev.h>
16 #include <linux/pagevec.h>
17 #include <linux/swap.h>
22 #include <trace/events/f2fs.h>
24 static struct kmem_cache *nat_entry_slab;
25 static struct kmem_cache *free_nid_slab;
27 static void clear_node_page_dirty(struct page *page)
29 struct address_space *mapping = page->mapping;
30 struct f2fs_sb_info *sbi = F2FS_SB(mapping->host->i_sb);
31 unsigned int long flags;
33 if (PageDirty(page)) {
34 spin_lock_irqsave(&mapping->tree_lock, flags);
35 radix_tree_tag_clear(&mapping->page_tree,
38 spin_unlock_irqrestore(&mapping->tree_lock, flags);
40 clear_page_dirty_for_io(page);
41 dec_page_count(sbi, F2FS_DIRTY_NODES);
43 ClearPageUptodate(page);
46 static struct page *get_current_nat_page(struct f2fs_sb_info *sbi, nid_t nid)
48 pgoff_t index = current_nat_addr(sbi, nid);
49 return get_meta_page(sbi, index);
52 static struct page *get_next_nat_page(struct f2fs_sb_info *sbi, nid_t nid)
54 struct page *src_page;
55 struct page *dst_page;
60 struct f2fs_nm_info *nm_i = NM_I(sbi);
62 src_off = current_nat_addr(sbi, nid);
63 dst_off = next_nat_addr(sbi, src_off);
65 /* get current nat block page with lock */
66 src_page = get_meta_page(sbi, src_off);
68 /* Dirty src_page means that it is already the new target NAT page. */
69 if (PageDirty(src_page))
72 dst_page = grab_meta_page(sbi, dst_off);
74 src_addr = page_address(src_page);
75 dst_addr = page_address(dst_page);
76 memcpy(dst_addr, src_addr, PAGE_CACHE_SIZE);
77 set_page_dirty(dst_page);
78 f2fs_put_page(src_page, 1);
80 set_to_next_nat(nm_i, nid);
88 static void ra_nat_pages(struct f2fs_sb_info *sbi, int nid)
90 struct address_space *mapping = sbi->meta_inode->i_mapping;
91 struct f2fs_nm_info *nm_i = NM_I(sbi);
97 blk_start_plug(&plug);
99 for (i = 0; i < FREE_NID_PAGES; i++, nid += NAT_ENTRY_PER_BLOCK) {
100 if (nid >= nm_i->max_nid)
102 index = current_nat_addr(sbi, nid);
104 page = grab_cache_page(mapping, index);
107 if (PageUptodate(page)) {
108 f2fs_put_page(page, 1);
111 if (f2fs_readpage(sbi, page, index, READ))
114 f2fs_put_page(page, 0);
116 blk_finish_plug(&plug);
119 static struct nat_entry *__lookup_nat_cache(struct f2fs_nm_info *nm_i, nid_t n)
121 return radix_tree_lookup(&nm_i->nat_root, n);
124 static unsigned int __gang_lookup_nat_cache(struct f2fs_nm_info *nm_i,
125 nid_t start, unsigned int nr, struct nat_entry **ep)
127 return radix_tree_gang_lookup(&nm_i->nat_root, (void **)ep, start, nr);
130 static void __del_from_nat_cache(struct f2fs_nm_info *nm_i, struct nat_entry *e)
133 radix_tree_delete(&nm_i->nat_root, nat_get_nid(e));
135 kmem_cache_free(nat_entry_slab, e);
138 int is_checkpointed_node(struct f2fs_sb_info *sbi, nid_t nid)
140 struct f2fs_nm_info *nm_i = NM_I(sbi);
144 read_lock(&nm_i->nat_tree_lock);
145 e = __lookup_nat_cache(nm_i, nid);
146 if (e && !e->checkpointed)
148 read_unlock(&nm_i->nat_tree_lock);
152 static struct nat_entry *grab_nat_entry(struct f2fs_nm_info *nm_i, nid_t nid)
154 struct nat_entry *new;
156 new = kmem_cache_alloc(nat_entry_slab, GFP_ATOMIC);
159 if (radix_tree_insert(&nm_i->nat_root, nid, new)) {
160 kmem_cache_free(nat_entry_slab, new);
163 memset(new, 0, sizeof(struct nat_entry));
164 nat_set_nid(new, nid);
165 list_add_tail(&new->list, &nm_i->nat_entries);
170 static void cache_nat_entry(struct f2fs_nm_info *nm_i, nid_t nid,
171 struct f2fs_nat_entry *ne)
175 write_lock(&nm_i->nat_tree_lock);
176 e = __lookup_nat_cache(nm_i, nid);
178 e = grab_nat_entry(nm_i, nid);
180 write_unlock(&nm_i->nat_tree_lock);
183 nat_set_blkaddr(e, le32_to_cpu(ne->block_addr));
184 nat_set_ino(e, le32_to_cpu(ne->ino));
185 nat_set_version(e, ne->version);
186 e->checkpointed = true;
188 write_unlock(&nm_i->nat_tree_lock);
191 static void set_node_addr(struct f2fs_sb_info *sbi, struct node_info *ni,
194 struct f2fs_nm_info *nm_i = NM_I(sbi);
197 write_lock(&nm_i->nat_tree_lock);
198 e = __lookup_nat_cache(nm_i, ni->nid);
200 e = grab_nat_entry(nm_i, ni->nid);
202 write_unlock(&nm_i->nat_tree_lock);
206 e->checkpointed = true;
207 f2fs_bug_on(ni->blk_addr == NEW_ADDR);
208 } else if (new_blkaddr == NEW_ADDR) {
210 * when nid is reallocated,
211 * previous nat entry can be remained in nat cache.
212 * So, reinitialize it with new information.
215 f2fs_bug_on(ni->blk_addr != NULL_ADDR);
218 if (new_blkaddr == NEW_ADDR)
219 e->checkpointed = false;
222 f2fs_bug_on(nat_get_blkaddr(e) != ni->blk_addr);
223 f2fs_bug_on(nat_get_blkaddr(e) == NULL_ADDR &&
224 new_blkaddr == NULL_ADDR);
225 f2fs_bug_on(nat_get_blkaddr(e) == NEW_ADDR &&
226 new_blkaddr == NEW_ADDR);
227 f2fs_bug_on(nat_get_blkaddr(e) != NEW_ADDR &&
228 nat_get_blkaddr(e) != NULL_ADDR &&
229 new_blkaddr == NEW_ADDR);
231 /* increament version no as node is removed */
232 if (nat_get_blkaddr(e) != NEW_ADDR && new_blkaddr == NULL_ADDR) {
233 unsigned char version = nat_get_version(e);
234 nat_set_version(e, inc_node_version(version));
238 nat_set_blkaddr(e, new_blkaddr);
239 __set_nat_cache_dirty(nm_i, e);
240 write_unlock(&nm_i->nat_tree_lock);
243 int try_to_free_nats(struct f2fs_sb_info *sbi, int nr_shrink)
245 struct f2fs_nm_info *nm_i = NM_I(sbi);
247 if (nm_i->nat_cnt <= NM_WOUT_THRESHOLD)
250 write_lock(&nm_i->nat_tree_lock);
251 while (nr_shrink && !list_empty(&nm_i->nat_entries)) {
252 struct nat_entry *ne;
253 ne = list_first_entry(&nm_i->nat_entries,
254 struct nat_entry, list);
255 __del_from_nat_cache(nm_i, ne);
258 write_unlock(&nm_i->nat_tree_lock);
263 * This function returns always success
265 void get_node_info(struct f2fs_sb_info *sbi, nid_t nid, struct node_info *ni)
267 struct f2fs_nm_info *nm_i = NM_I(sbi);
268 struct curseg_info *curseg = CURSEG_I(sbi, CURSEG_HOT_DATA);
269 struct f2fs_summary_block *sum = curseg->sum_blk;
270 nid_t start_nid = START_NID(nid);
271 struct f2fs_nat_block *nat_blk;
272 struct page *page = NULL;
273 struct f2fs_nat_entry ne;
277 memset(&ne, 0, sizeof(struct f2fs_nat_entry));
280 /* Check nat cache */
281 read_lock(&nm_i->nat_tree_lock);
282 e = __lookup_nat_cache(nm_i, nid);
284 ni->ino = nat_get_ino(e);
285 ni->blk_addr = nat_get_blkaddr(e);
286 ni->version = nat_get_version(e);
288 read_unlock(&nm_i->nat_tree_lock);
292 /* Check current segment summary */
293 mutex_lock(&curseg->curseg_mutex);
294 i = lookup_journal_in_cursum(sum, NAT_JOURNAL, nid, 0);
296 ne = nat_in_journal(sum, i);
297 node_info_from_raw_nat(ni, &ne);
299 mutex_unlock(&curseg->curseg_mutex);
303 /* Fill node_info from nat page */
304 page = get_current_nat_page(sbi, start_nid);
305 nat_blk = (struct f2fs_nat_block *)page_address(page);
306 ne = nat_blk->entries[nid - start_nid];
307 node_info_from_raw_nat(ni, &ne);
308 f2fs_put_page(page, 1);
310 /* cache nat entry */
311 cache_nat_entry(NM_I(sbi), nid, &ne);
315 * The maximum depth is four.
316 * Offset[0] will have raw inode offset.
318 static int get_node_path(struct f2fs_inode_info *fi, long block,
319 int offset[4], unsigned int noffset[4])
321 const long direct_index = ADDRS_PER_INODE(fi);
322 const long direct_blks = ADDRS_PER_BLOCK;
323 const long dptrs_per_blk = NIDS_PER_BLOCK;
324 const long indirect_blks = ADDRS_PER_BLOCK * NIDS_PER_BLOCK;
325 const long dindirect_blks = indirect_blks * NIDS_PER_BLOCK;
331 if (block < direct_index) {
335 block -= direct_index;
336 if (block < direct_blks) {
337 offset[n++] = NODE_DIR1_BLOCK;
343 block -= direct_blks;
344 if (block < direct_blks) {
345 offset[n++] = NODE_DIR2_BLOCK;
351 block -= direct_blks;
352 if (block < indirect_blks) {
353 offset[n++] = NODE_IND1_BLOCK;
355 offset[n++] = block / direct_blks;
356 noffset[n] = 4 + offset[n - 1];
357 offset[n] = block % direct_blks;
361 block -= indirect_blks;
362 if (block < indirect_blks) {
363 offset[n++] = NODE_IND2_BLOCK;
364 noffset[n] = 4 + dptrs_per_blk;
365 offset[n++] = block / direct_blks;
366 noffset[n] = 5 + dptrs_per_blk + offset[n - 1];
367 offset[n] = block % direct_blks;
371 block -= indirect_blks;
372 if (block < dindirect_blks) {
373 offset[n++] = NODE_DIND_BLOCK;
374 noffset[n] = 5 + (dptrs_per_blk * 2);
375 offset[n++] = block / indirect_blks;
376 noffset[n] = 6 + (dptrs_per_blk * 2) +
377 offset[n - 1] * (dptrs_per_blk + 1);
378 offset[n++] = (block / direct_blks) % dptrs_per_blk;
379 noffset[n] = 7 + (dptrs_per_blk * 2) +
380 offset[n - 2] * (dptrs_per_blk + 1) +
382 offset[n] = block % direct_blks;
393 * Caller should call f2fs_put_dnode(dn).
394 * Also, it should grab and release a mutex by calling mutex_lock_op() and
395 * mutex_unlock_op() only if ro is not set RDONLY_NODE.
396 * In the case of RDONLY_NODE, we don't need to care about mutex.
398 int get_dnode_of_data(struct dnode_of_data *dn, pgoff_t index, int mode)
400 struct f2fs_sb_info *sbi = F2FS_SB(dn->inode->i_sb);
401 struct page *npage[4];
404 unsigned int noffset[4];
409 level = get_node_path(F2FS_I(dn->inode), index, offset, noffset);
411 nids[0] = dn->inode->i_ino;
412 npage[0] = dn->inode_page;
415 npage[0] = get_node_page(sbi, nids[0]);
416 if (IS_ERR(npage[0]))
417 return PTR_ERR(npage[0]);
421 nids[1] = get_nid(parent, offset[0], true);
422 dn->inode_page = npage[0];
423 dn->inode_page_locked = true;
425 /* get indirect or direct nodes */
426 for (i = 1; i <= level; i++) {
429 if (!nids[i] && mode == ALLOC_NODE) {
431 if (!alloc_nid(sbi, &(nids[i]))) {
437 npage[i] = new_node_page(dn, noffset[i], NULL);
438 if (IS_ERR(npage[i])) {
439 alloc_nid_failed(sbi, nids[i]);
440 err = PTR_ERR(npage[i]);
444 set_nid(parent, offset[i - 1], nids[i], i == 1);
445 alloc_nid_done(sbi, nids[i]);
447 } else if (mode == LOOKUP_NODE_RA && i == level && level > 1) {
448 npage[i] = get_node_page_ra(parent, offset[i - 1]);
449 if (IS_ERR(npage[i])) {
450 err = PTR_ERR(npage[i]);
456 dn->inode_page_locked = false;
459 f2fs_put_page(parent, 1);
463 npage[i] = get_node_page(sbi, nids[i]);
464 if (IS_ERR(npage[i])) {
465 err = PTR_ERR(npage[i]);
466 f2fs_put_page(npage[0], 0);
472 nids[i + 1] = get_nid(parent, offset[i], false);
475 dn->nid = nids[level];
476 dn->ofs_in_node = offset[level];
477 dn->node_page = npage[level];
478 dn->data_blkaddr = datablock_addr(dn->node_page, dn->ofs_in_node);
482 f2fs_put_page(parent, 1);
484 f2fs_put_page(npage[0], 0);
486 dn->inode_page = NULL;
487 dn->node_page = NULL;
491 static void truncate_node(struct dnode_of_data *dn)
493 struct f2fs_sb_info *sbi = F2FS_SB(dn->inode->i_sb);
496 get_node_info(sbi, dn->nid, &ni);
497 if (dn->inode->i_blocks == 0) {
498 f2fs_bug_on(ni.blk_addr != NULL_ADDR);
501 f2fs_bug_on(ni.blk_addr == NULL_ADDR);
503 /* Deallocate node address */
504 invalidate_blocks(sbi, ni.blk_addr);
505 dec_valid_node_count(sbi, dn->inode);
506 set_node_addr(sbi, &ni, NULL_ADDR);
508 if (dn->nid == dn->inode->i_ino) {
509 remove_orphan_inode(sbi, dn->nid);
510 dec_valid_inode_count(sbi);
515 clear_node_page_dirty(dn->node_page);
516 F2FS_SET_SB_DIRT(sbi);
518 f2fs_put_page(dn->node_page, 1);
519 dn->node_page = NULL;
520 trace_f2fs_truncate_node(dn->inode, dn->nid, ni.blk_addr);
523 static int truncate_dnode(struct dnode_of_data *dn)
525 struct f2fs_sb_info *sbi = F2FS_SB(dn->inode->i_sb);
531 /* get direct node */
532 page = get_node_page(sbi, dn->nid);
533 if (IS_ERR(page) && PTR_ERR(page) == -ENOENT)
535 else if (IS_ERR(page))
536 return PTR_ERR(page);
538 /* Make dnode_of_data for parameter */
539 dn->node_page = page;
541 truncate_data_blocks(dn);
546 static int truncate_nodes(struct dnode_of_data *dn, unsigned int nofs,
549 struct f2fs_sb_info *sbi = F2FS_SB(dn->inode->i_sb);
550 struct dnode_of_data rdn = *dn;
552 struct f2fs_node *rn;
554 unsigned int child_nofs;
559 return NIDS_PER_BLOCK + 1;
561 trace_f2fs_truncate_nodes_enter(dn->inode, dn->nid, dn->data_blkaddr);
563 page = get_node_page(sbi, dn->nid);
565 trace_f2fs_truncate_nodes_exit(dn->inode, PTR_ERR(page));
566 return PTR_ERR(page);
569 rn = F2FS_NODE(page);
571 for (i = ofs; i < NIDS_PER_BLOCK; i++, freed++) {
572 child_nid = le32_to_cpu(rn->in.nid[i]);
576 ret = truncate_dnode(&rdn);
579 set_nid(page, i, 0, false);
582 child_nofs = nofs + ofs * (NIDS_PER_BLOCK + 1) + 1;
583 for (i = ofs; i < NIDS_PER_BLOCK; i++) {
584 child_nid = le32_to_cpu(rn->in.nid[i]);
585 if (child_nid == 0) {
586 child_nofs += NIDS_PER_BLOCK + 1;
590 ret = truncate_nodes(&rdn, child_nofs, 0, depth - 1);
591 if (ret == (NIDS_PER_BLOCK + 1)) {
592 set_nid(page, i, 0, false);
594 } else if (ret < 0 && ret != -ENOENT) {
602 /* remove current indirect node */
603 dn->node_page = page;
607 f2fs_put_page(page, 1);
609 trace_f2fs_truncate_nodes_exit(dn->inode, freed);
613 f2fs_put_page(page, 1);
614 trace_f2fs_truncate_nodes_exit(dn->inode, ret);
618 static int truncate_partial_nodes(struct dnode_of_data *dn,
619 struct f2fs_inode *ri, int *offset, int depth)
621 struct f2fs_sb_info *sbi = F2FS_SB(dn->inode->i_sb);
622 struct page *pages[2];
629 nid[0] = le32_to_cpu(ri->i_nid[offset[0] - NODE_DIR1_BLOCK]);
633 /* get indirect nodes in the path */
634 for (i = 0; i < depth - 1; i++) {
635 /* refernece count'll be increased */
636 pages[i] = get_node_page(sbi, nid[i]);
637 if (IS_ERR(pages[i])) {
639 err = PTR_ERR(pages[i]);
642 nid[i + 1] = get_nid(pages[i], offset[i + 1], false);
645 /* free direct nodes linked to a partial indirect node */
646 for (i = offset[depth - 1]; i < NIDS_PER_BLOCK; i++) {
647 child_nid = get_nid(pages[idx], i, false);
651 err = truncate_dnode(dn);
654 set_nid(pages[idx], i, 0, false);
657 if (offset[depth - 1] == 0) {
658 dn->node_page = pages[idx];
662 f2fs_put_page(pages[idx], 1);
665 offset[depth - 1] = 0;
667 for (i = depth - 3; i >= 0; i--)
668 f2fs_put_page(pages[i], 1);
670 trace_f2fs_truncate_partial_nodes(dn->inode, nid, depth, err);
676 * All the block addresses of data and nodes should be nullified.
678 int truncate_inode_blocks(struct inode *inode, pgoff_t from)
680 struct f2fs_sb_info *sbi = F2FS_SB(inode->i_sb);
681 struct address_space *node_mapping = sbi->node_inode->i_mapping;
682 int err = 0, cont = 1;
683 int level, offset[4], noffset[4];
684 unsigned int nofs = 0;
685 struct f2fs_node *rn;
686 struct dnode_of_data dn;
689 trace_f2fs_truncate_inode_blocks_enter(inode, from);
691 level = get_node_path(F2FS_I(inode), from, offset, noffset);
693 page = get_node_page(sbi, inode->i_ino);
695 trace_f2fs_truncate_inode_blocks_exit(inode, PTR_ERR(page));
696 return PTR_ERR(page);
699 set_new_dnode(&dn, inode, page, NULL, 0);
702 rn = F2FS_NODE(page);
710 if (!offset[level - 1])
712 err = truncate_partial_nodes(&dn, &rn->i, offset, level);
713 if (err < 0 && err != -ENOENT)
715 nofs += 1 + NIDS_PER_BLOCK;
718 nofs = 5 + 2 * NIDS_PER_BLOCK;
719 if (!offset[level - 1])
721 err = truncate_partial_nodes(&dn, &rn->i, offset, level);
722 if (err < 0 && err != -ENOENT)
731 dn.nid = le32_to_cpu(rn->i.i_nid[offset[0] - NODE_DIR1_BLOCK]);
733 case NODE_DIR1_BLOCK:
734 case NODE_DIR2_BLOCK:
735 err = truncate_dnode(&dn);
738 case NODE_IND1_BLOCK:
739 case NODE_IND2_BLOCK:
740 err = truncate_nodes(&dn, nofs, offset[1], 2);
743 case NODE_DIND_BLOCK:
744 err = truncate_nodes(&dn, nofs, offset[1], 3);
751 if (err < 0 && err != -ENOENT)
753 if (offset[1] == 0 &&
754 rn->i.i_nid[offset[0] - NODE_DIR1_BLOCK]) {
756 if (page->mapping != node_mapping) {
757 f2fs_put_page(page, 1);
760 wait_on_page_writeback(page);
761 rn->i.i_nid[offset[0] - NODE_DIR1_BLOCK] = 0;
762 set_page_dirty(page);
770 f2fs_put_page(page, 0);
771 trace_f2fs_truncate_inode_blocks_exit(inode, err);
772 return err > 0 ? 0 : err;
775 int truncate_xattr_node(struct inode *inode, struct page *page)
777 struct f2fs_sb_info *sbi = F2FS_SB(inode->i_sb);
778 nid_t nid = F2FS_I(inode)->i_xattr_nid;
779 struct dnode_of_data dn;
785 npage = get_node_page(sbi, nid);
787 return PTR_ERR(npage);
789 F2FS_I(inode)->i_xattr_nid = 0;
791 /* need to do checkpoint during fsync */
792 F2FS_I(inode)->xattr_ver = cur_cp_version(F2FS_CKPT(sbi));
794 set_new_dnode(&dn, inode, page, npage, nid);
797 dn.inode_page_locked = 1;
803 * Caller should grab and release a mutex by calling mutex_lock_op() and
806 void remove_inode_page(struct inode *inode)
808 struct f2fs_sb_info *sbi = F2FS_SB(inode->i_sb);
810 nid_t ino = inode->i_ino;
811 struct dnode_of_data dn;
813 page = get_node_page(sbi, ino);
817 if (truncate_xattr_node(inode, page)) {
818 f2fs_put_page(page, 1);
821 /* 0 is possible, after f2fs_new_inode() is failed */
822 f2fs_bug_on(inode->i_blocks != 0 && inode->i_blocks != 1);
823 set_new_dnode(&dn, inode, page, page, ino);
827 struct page *new_inode_page(struct inode *inode, const struct qstr *name)
829 struct dnode_of_data dn;
831 /* allocate inode page for new inode */
832 set_new_dnode(&dn, inode, NULL, NULL, inode->i_ino);
834 /* caller should f2fs_put_page(page, 1); */
835 return new_node_page(&dn, 0, NULL);
838 struct page *new_node_page(struct dnode_of_data *dn,
839 unsigned int ofs, struct page *ipage)
841 struct f2fs_sb_info *sbi = F2FS_SB(dn->inode->i_sb);
842 struct address_space *mapping = sbi->node_inode->i_mapping;
843 struct node_info old_ni, new_ni;
847 if (is_inode_flag_set(F2FS_I(dn->inode), FI_NO_ALLOC))
848 return ERR_PTR(-EPERM);
850 page = grab_cache_page(mapping, dn->nid);
852 return ERR_PTR(-ENOMEM);
854 if (!inc_valid_node_count(sbi, dn->inode)) {
859 get_node_info(sbi, dn->nid, &old_ni);
861 /* Reinitialize old_ni with new node page */
862 f2fs_bug_on(old_ni.blk_addr != NULL_ADDR);
864 new_ni.ino = dn->inode->i_ino;
865 set_node_addr(sbi, &new_ni, NEW_ADDR);
867 fill_node_footer(page, dn->nid, dn->inode->i_ino, ofs, true);
868 set_cold_node(dn->inode, page);
869 SetPageUptodate(page);
870 set_page_dirty(page);
872 if (ofs == XATTR_NODE_OFFSET)
873 F2FS_I(dn->inode)->i_xattr_nid = dn->nid;
875 dn->node_page = page;
877 update_inode(dn->inode, ipage);
881 inc_valid_inode_count(sbi);
886 clear_node_page_dirty(page);
887 f2fs_put_page(page, 1);
892 * Caller should do after getting the following values.
893 * 0: f2fs_put_page(page, 0)
894 * LOCKED_PAGE: f2fs_put_page(page, 1)
897 static int read_node_page(struct page *page, int type)
899 struct f2fs_sb_info *sbi = F2FS_SB(page->mapping->host->i_sb);
902 get_node_info(sbi, page->index, &ni);
904 if (ni.blk_addr == NULL_ADDR) {
905 f2fs_put_page(page, 1);
909 if (PageUptodate(page))
912 return f2fs_readpage(sbi, page, ni.blk_addr, type);
916 * Readahead a node page
918 void ra_node_page(struct f2fs_sb_info *sbi, nid_t nid)
920 struct address_space *mapping = sbi->node_inode->i_mapping;
924 apage = find_get_page(mapping, nid);
925 if (apage && PageUptodate(apage)) {
926 f2fs_put_page(apage, 0);
929 f2fs_put_page(apage, 0);
931 apage = grab_cache_page(mapping, nid);
935 err = read_node_page(apage, READA);
937 f2fs_put_page(apage, 0);
938 else if (err == LOCKED_PAGE)
939 f2fs_put_page(apage, 1);
942 struct page *get_node_page(struct f2fs_sb_info *sbi, pgoff_t nid)
944 struct address_space *mapping = sbi->node_inode->i_mapping;
948 page = grab_cache_page(mapping, nid);
950 return ERR_PTR(-ENOMEM);
952 err = read_node_page(page, READ_SYNC);
955 else if (err == LOCKED_PAGE)
959 if (!PageUptodate(page)) {
960 f2fs_put_page(page, 1);
961 return ERR_PTR(-EIO);
963 if (page->mapping != mapping) {
964 f2fs_put_page(page, 1);
968 f2fs_bug_on(nid != nid_of_node(page));
969 mark_page_accessed(page);
974 * Return a locked page for the desired node page.
975 * And, readahead MAX_RA_NODE number of node pages.
977 struct page *get_node_page_ra(struct page *parent, int start)
979 struct f2fs_sb_info *sbi = F2FS_SB(parent->mapping->host->i_sb);
980 struct address_space *mapping = sbi->node_inode->i_mapping;
981 struct blk_plug plug;
986 /* First, try getting the desired direct node. */
987 nid = get_nid(parent, start, false);
989 return ERR_PTR(-ENOENT);
991 page = grab_cache_page(mapping, nid);
993 return ERR_PTR(-ENOMEM);
995 err = read_node_page(page, READ_SYNC);
998 else if (err == LOCKED_PAGE)
1001 blk_start_plug(&plug);
1003 /* Then, try readahead for siblings of the desired node */
1004 end = start + MAX_RA_NODE;
1005 end = min(end, NIDS_PER_BLOCK);
1006 for (i = start + 1; i < end; i++) {
1007 nid = get_nid(parent, i, false);
1010 ra_node_page(sbi, nid);
1013 blk_finish_plug(&plug);
1016 if (page->mapping != mapping) {
1017 f2fs_put_page(page, 1);
1021 if (!PageUptodate(page)) {
1022 f2fs_put_page(page, 1);
1023 return ERR_PTR(-EIO);
1025 mark_page_accessed(page);
1029 void sync_inode_page(struct dnode_of_data *dn)
1031 if (IS_INODE(dn->node_page) || dn->inode_page == dn->node_page) {
1032 update_inode(dn->inode, dn->node_page);
1033 } else if (dn->inode_page) {
1034 if (!dn->inode_page_locked)
1035 lock_page(dn->inode_page);
1036 update_inode(dn->inode, dn->inode_page);
1037 if (!dn->inode_page_locked)
1038 unlock_page(dn->inode_page);
1040 update_inode_page(dn->inode);
1044 int sync_node_pages(struct f2fs_sb_info *sbi, nid_t ino,
1045 struct writeback_control *wbc)
1047 struct address_space *mapping = sbi->node_inode->i_mapping;
1049 struct pagevec pvec;
1050 int step = ino ? 2 : 0;
1051 int nwritten = 0, wrote = 0;
1053 pagevec_init(&pvec, 0);
1059 while (index <= end) {
1061 nr_pages = pagevec_lookup_tag(&pvec, mapping, &index,
1062 PAGECACHE_TAG_DIRTY,
1063 min(end - index, (pgoff_t)PAGEVEC_SIZE-1) + 1);
1067 for (i = 0; i < nr_pages; i++) {
1068 struct page *page = pvec.pages[i];
1071 * flushing sequence with step:
1076 if (step == 0 && IS_DNODE(page))
1078 if (step == 1 && (!IS_DNODE(page) ||
1079 is_cold_node(page)))
1081 if (step == 2 && (!IS_DNODE(page) ||
1082 !is_cold_node(page)))
1087 * we should not skip writing node pages.
1089 if (ino && ino_of_node(page) == ino)
1091 else if (!trylock_page(page))
1094 if (unlikely(page->mapping != mapping)) {
1099 if (ino && ino_of_node(page) != ino)
1100 goto continue_unlock;
1102 if (!PageDirty(page)) {
1103 /* someone wrote it for us */
1104 goto continue_unlock;
1107 if (!clear_page_dirty_for_io(page))
1108 goto continue_unlock;
1110 /* called by fsync() */
1111 if (ino && IS_DNODE(page)) {
1112 int mark = !is_checkpointed_node(sbi, ino);
1113 set_fsync_mark(page, 1);
1115 set_dentry_mark(page, mark);
1118 set_fsync_mark(page, 0);
1119 set_dentry_mark(page, 0);
1121 mapping->a_ops->writepage(page, wbc);
1124 if (--wbc->nr_to_write == 0)
1127 pagevec_release(&pvec);
1130 if (wbc->nr_to_write == 0) {
1142 f2fs_submit_bio(sbi, NODE, wbc->sync_mode == WB_SYNC_ALL);
1147 int wait_on_node_pages_writeback(struct f2fs_sb_info *sbi, nid_t ino)
1149 struct address_space *mapping = sbi->node_inode->i_mapping;
1150 pgoff_t index = 0, end = LONG_MAX;
1151 struct pagevec pvec;
1153 int ret2 = 0, ret = 0;
1155 pagevec_init(&pvec, 0);
1156 while ((index <= end) &&
1157 (nr_pages = pagevec_lookup_tag(&pvec, mapping, &index,
1158 PAGECACHE_TAG_WRITEBACK,
1159 min(end - index, (pgoff_t)PAGEVEC_SIZE-1) + 1)) != 0) {
1162 for (i = 0; i < nr_pages; i++) {
1163 struct page *page = pvec.pages[i];
1165 /* until radix tree lookup accepts end_index */
1166 if (page->index > end)
1169 if (ino && ino_of_node(page) == ino) {
1170 wait_on_page_writeback(page);
1171 if (TestClearPageError(page))
1175 pagevec_release(&pvec);
1179 if (test_and_clear_bit(AS_ENOSPC, &mapping->flags))
1181 if (test_and_clear_bit(AS_EIO, &mapping->flags))
1188 static int f2fs_write_node_page(struct page *page,
1189 struct writeback_control *wbc)
1191 struct f2fs_sb_info *sbi = F2FS_SB(page->mapping->host->i_sb);
1194 struct node_info ni;
1199 wait_on_page_writeback(page);
1201 /* get old block addr of this node page */
1202 nid = nid_of_node(page);
1203 f2fs_bug_on(page->index != nid);
1205 get_node_info(sbi, nid, &ni);
1207 /* This page is already truncated */
1208 if (ni.blk_addr == NULL_ADDR) {
1209 dec_page_count(sbi, F2FS_DIRTY_NODES);
1214 if (wbc->for_reclaim)
1217 mutex_lock(&sbi->node_write);
1218 set_page_writeback(page);
1219 write_node_page(sbi, page, nid, ni.blk_addr, &new_addr);
1220 set_node_addr(sbi, &ni, new_addr);
1221 dec_page_count(sbi, F2FS_DIRTY_NODES);
1222 mutex_unlock(&sbi->node_write);
1227 dec_page_count(sbi, F2FS_DIRTY_NODES);
1228 wbc->pages_skipped++;
1229 set_page_dirty(page);
1230 return AOP_WRITEPAGE_ACTIVATE;
1234 * It is very important to gather dirty pages and write at once, so that we can
1235 * submit a big bio without interfering other data writes.
1236 * Be default, 512 pages (2MB) * 3 node types, is more reasonable.
1238 #define COLLECT_DIRTY_NODES 1536
1239 static int f2fs_write_node_pages(struct address_space *mapping,
1240 struct writeback_control *wbc)
1242 struct f2fs_sb_info *sbi = F2FS_SB(mapping->host->i_sb);
1243 long nr_to_write = wbc->nr_to_write;
1245 /* balancing f2fs's metadata in background */
1246 f2fs_balance_fs_bg(sbi);
1248 /* collect a number of dirty node pages and write together */
1249 if (get_pages(sbi, F2FS_DIRTY_NODES) < COLLECT_DIRTY_NODES)
1252 /* if mounting is failed, skip writing node pages */
1253 wbc->nr_to_write = 3 * max_hw_blocks(sbi);
1254 sync_node_pages(sbi, 0, wbc);
1255 wbc->nr_to_write = nr_to_write - (3 * max_hw_blocks(sbi) -
1260 static int f2fs_set_node_page_dirty(struct page *page)
1262 struct address_space *mapping = page->mapping;
1263 struct f2fs_sb_info *sbi = F2FS_SB(mapping->host->i_sb);
1265 trace_f2fs_set_page_dirty(page, NODE);
1267 SetPageUptodate(page);
1268 if (!PageDirty(page)) {
1269 __set_page_dirty_nobuffers(page);
1270 inc_page_count(sbi, F2FS_DIRTY_NODES);
1271 SetPagePrivate(page);
1277 static void f2fs_invalidate_node_page(struct page *page, unsigned int offset,
1278 unsigned int length)
1280 struct inode *inode = page->mapping->host;
1281 struct f2fs_sb_info *sbi = F2FS_SB(inode->i_sb);
1282 if (PageDirty(page))
1283 dec_page_count(sbi, F2FS_DIRTY_NODES);
1284 ClearPagePrivate(page);
1287 static int f2fs_release_node_page(struct page *page, gfp_t wait)
1289 ClearPagePrivate(page);
1294 * Structure of the f2fs node operations
1296 const struct address_space_operations f2fs_node_aops = {
1297 .writepage = f2fs_write_node_page,
1298 .writepages = f2fs_write_node_pages,
1299 .set_page_dirty = f2fs_set_node_page_dirty,
1300 .invalidatepage = f2fs_invalidate_node_page,
1301 .releasepage = f2fs_release_node_page,
1304 static struct free_nid *__lookup_free_nid_list(nid_t n, struct list_head *head)
1306 struct list_head *this;
1308 list_for_each(this, head) {
1309 i = list_entry(this, struct free_nid, list);
1316 static void __del_from_free_nid_list(struct free_nid *i)
1319 kmem_cache_free(free_nid_slab, i);
1322 static int add_free_nid(struct f2fs_nm_info *nm_i, nid_t nid, bool build)
1325 struct nat_entry *ne;
1326 bool allocated = false;
1328 if (nm_i->fcnt > 2 * MAX_FREE_NIDS)
1331 /* 0 nid should not be used */
1336 /* do not add allocated nids */
1337 read_lock(&nm_i->nat_tree_lock);
1338 ne = __lookup_nat_cache(nm_i, nid);
1339 if (ne && nat_get_blkaddr(ne) != NULL_ADDR)
1341 read_unlock(&nm_i->nat_tree_lock);
1346 i = f2fs_kmem_cache_alloc(free_nid_slab, GFP_NOFS);
1350 spin_lock(&nm_i->free_nid_list_lock);
1351 if (__lookup_free_nid_list(nid, &nm_i->free_nid_list)) {
1352 spin_unlock(&nm_i->free_nid_list_lock);
1353 kmem_cache_free(free_nid_slab, i);
1356 list_add_tail(&i->list, &nm_i->free_nid_list);
1358 spin_unlock(&nm_i->free_nid_list_lock);
1362 static void remove_free_nid(struct f2fs_nm_info *nm_i, nid_t nid)
1365 spin_lock(&nm_i->free_nid_list_lock);
1366 i = __lookup_free_nid_list(nid, &nm_i->free_nid_list);
1367 if (i && i->state == NID_NEW) {
1368 __del_from_free_nid_list(i);
1371 spin_unlock(&nm_i->free_nid_list_lock);
1374 static void scan_nat_page(struct f2fs_nm_info *nm_i,
1375 struct page *nat_page, nid_t start_nid)
1377 struct f2fs_nat_block *nat_blk = page_address(nat_page);
1381 i = start_nid % NAT_ENTRY_PER_BLOCK;
1383 for (; i < NAT_ENTRY_PER_BLOCK; i++, start_nid++) {
1385 if (start_nid >= nm_i->max_nid)
1388 blk_addr = le32_to_cpu(nat_blk->entries[i].block_addr);
1389 f2fs_bug_on(blk_addr == NEW_ADDR);
1390 if (blk_addr == NULL_ADDR) {
1391 if (add_free_nid(nm_i, start_nid, true) < 0)
1397 static void build_free_nids(struct f2fs_sb_info *sbi)
1399 struct f2fs_nm_info *nm_i = NM_I(sbi);
1400 struct curseg_info *curseg = CURSEG_I(sbi, CURSEG_HOT_DATA);
1401 struct f2fs_summary_block *sum = curseg->sum_blk;
1403 nid_t nid = nm_i->next_scan_nid;
1405 /* Enough entries */
1406 if (nm_i->fcnt > NAT_ENTRY_PER_BLOCK)
1409 /* readahead nat pages to be scanned */
1410 ra_nat_pages(sbi, nid);
1413 struct page *page = get_current_nat_page(sbi, nid);
1415 scan_nat_page(nm_i, page, nid);
1416 f2fs_put_page(page, 1);
1418 nid += (NAT_ENTRY_PER_BLOCK - (nid % NAT_ENTRY_PER_BLOCK));
1419 if (nid >= nm_i->max_nid)
1422 if (i++ == FREE_NID_PAGES)
1426 /* go to the next free nat pages to find free nids abundantly */
1427 nm_i->next_scan_nid = nid;
1429 /* find free nids from current sum_pages */
1430 mutex_lock(&curseg->curseg_mutex);
1431 for (i = 0; i < nats_in_cursum(sum); i++) {
1432 block_t addr = le32_to_cpu(nat_in_journal(sum, i).block_addr);
1433 nid = le32_to_cpu(nid_in_journal(sum, i));
1434 if (addr == NULL_ADDR)
1435 add_free_nid(nm_i, nid, true);
1437 remove_free_nid(nm_i, nid);
1439 mutex_unlock(&curseg->curseg_mutex);
1443 * If this function returns success, caller can obtain a new nid
1444 * from second parameter of this function.
1445 * The returned nid could be used ino as well as nid when inode is created.
1447 bool alloc_nid(struct f2fs_sb_info *sbi, nid_t *nid)
1449 struct f2fs_nm_info *nm_i = NM_I(sbi);
1450 struct free_nid *i = NULL;
1451 struct list_head *this;
1453 if (sbi->total_valid_node_count + 1 >= nm_i->max_nid)
1456 spin_lock(&nm_i->free_nid_list_lock);
1458 /* We should not use stale free nids created by build_free_nids */
1459 if (nm_i->fcnt && !sbi->on_build_free_nids) {
1460 f2fs_bug_on(list_empty(&nm_i->free_nid_list));
1461 list_for_each(this, &nm_i->free_nid_list) {
1462 i = list_entry(this, struct free_nid, list);
1463 if (i->state == NID_NEW)
1467 f2fs_bug_on(i->state != NID_NEW);
1469 i->state = NID_ALLOC;
1471 spin_unlock(&nm_i->free_nid_list_lock);
1474 spin_unlock(&nm_i->free_nid_list_lock);
1476 /* Let's scan nat pages and its caches to get free nids */
1477 mutex_lock(&nm_i->build_lock);
1478 sbi->on_build_free_nids = true;
1479 build_free_nids(sbi);
1480 sbi->on_build_free_nids = false;
1481 mutex_unlock(&nm_i->build_lock);
1486 * alloc_nid() should be called prior to this function.
1488 void alloc_nid_done(struct f2fs_sb_info *sbi, nid_t nid)
1490 struct f2fs_nm_info *nm_i = NM_I(sbi);
1493 spin_lock(&nm_i->free_nid_list_lock);
1494 i = __lookup_free_nid_list(nid, &nm_i->free_nid_list);
1495 f2fs_bug_on(!i || i->state != NID_ALLOC);
1496 __del_from_free_nid_list(i);
1497 spin_unlock(&nm_i->free_nid_list_lock);
1501 * alloc_nid() should be called prior to this function.
1503 void alloc_nid_failed(struct f2fs_sb_info *sbi, nid_t nid)
1505 struct f2fs_nm_info *nm_i = NM_I(sbi);
1511 spin_lock(&nm_i->free_nid_list_lock);
1512 i = __lookup_free_nid_list(nid, &nm_i->free_nid_list);
1513 f2fs_bug_on(!i || i->state != NID_ALLOC);
1514 if (nm_i->fcnt > 2 * MAX_FREE_NIDS) {
1515 __del_from_free_nid_list(i);
1520 spin_unlock(&nm_i->free_nid_list_lock);
1523 void recover_node_page(struct f2fs_sb_info *sbi, struct page *page,
1524 struct f2fs_summary *sum, struct node_info *ni,
1525 block_t new_blkaddr)
1527 rewrite_node_page(sbi, page, sum, ni->blk_addr, new_blkaddr);
1528 set_node_addr(sbi, ni, new_blkaddr);
1529 clear_node_page_dirty(page);
1532 int recover_inode_page(struct f2fs_sb_info *sbi, struct page *page)
1534 struct address_space *mapping = sbi->node_inode->i_mapping;
1535 struct f2fs_node *src, *dst;
1536 nid_t ino = ino_of_node(page);
1537 struct node_info old_ni, new_ni;
1540 ipage = grab_cache_page(mapping, ino);
1544 /* Should not use this inode from free nid list */
1545 remove_free_nid(NM_I(sbi), ino);
1547 get_node_info(sbi, ino, &old_ni);
1548 SetPageUptodate(ipage);
1549 fill_node_footer(ipage, ino, ino, 0, true);
1551 src = F2FS_NODE(page);
1552 dst = F2FS_NODE(ipage);
1554 memcpy(dst, src, (unsigned long)&src->i.i_ext - (unsigned long)&src->i);
1556 dst->i.i_blocks = cpu_to_le64(1);
1557 dst->i.i_links = cpu_to_le32(1);
1558 dst->i.i_xattr_nid = 0;
1563 if (!inc_valid_node_count(sbi, NULL))
1565 set_node_addr(sbi, &new_ni, NEW_ADDR);
1566 inc_valid_inode_count(sbi);
1567 f2fs_put_page(ipage, 1);
1571 int restore_node_summary(struct f2fs_sb_info *sbi,
1572 unsigned int segno, struct f2fs_summary_block *sum)
1574 struct f2fs_node *rn;
1575 struct f2fs_summary *sum_entry;
1580 /* alloc temporal page for read node */
1581 page = alloc_page(GFP_NOFS | __GFP_ZERO);
1586 /* scan the node segment */
1587 last_offset = sbi->blocks_per_seg;
1588 addr = START_BLOCK(sbi, segno);
1589 sum_entry = &sum->entries[0];
1591 for (i = 0; i < last_offset; i++, sum_entry++) {
1593 * In order to read next node page,
1594 * we must clear PageUptodate flag.
1596 ClearPageUptodate(page);
1598 if (f2fs_readpage(sbi, page, addr, READ_SYNC))
1602 rn = F2FS_NODE(page);
1603 sum_entry->nid = rn->footer.nid;
1604 sum_entry->version = 0;
1605 sum_entry->ofs_in_node = 0;
1610 __free_pages(page, 0);
1614 static bool flush_nats_in_journal(struct f2fs_sb_info *sbi)
1616 struct f2fs_nm_info *nm_i = NM_I(sbi);
1617 struct curseg_info *curseg = CURSEG_I(sbi, CURSEG_HOT_DATA);
1618 struct f2fs_summary_block *sum = curseg->sum_blk;
1621 mutex_lock(&curseg->curseg_mutex);
1623 if (nats_in_cursum(sum) < NAT_JOURNAL_ENTRIES) {
1624 mutex_unlock(&curseg->curseg_mutex);
1628 for (i = 0; i < nats_in_cursum(sum); i++) {
1629 struct nat_entry *ne;
1630 struct f2fs_nat_entry raw_ne;
1631 nid_t nid = le32_to_cpu(nid_in_journal(sum, i));
1633 raw_ne = nat_in_journal(sum, i);
1635 write_lock(&nm_i->nat_tree_lock);
1636 ne = __lookup_nat_cache(nm_i, nid);
1638 __set_nat_cache_dirty(nm_i, ne);
1639 write_unlock(&nm_i->nat_tree_lock);
1642 ne = grab_nat_entry(nm_i, nid);
1644 write_unlock(&nm_i->nat_tree_lock);
1647 nat_set_blkaddr(ne, le32_to_cpu(raw_ne.block_addr));
1648 nat_set_ino(ne, le32_to_cpu(raw_ne.ino));
1649 nat_set_version(ne, raw_ne.version);
1650 __set_nat_cache_dirty(nm_i, ne);
1651 write_unlock(&nm_i->nat_tree_lock);
1653 update_nats_in_cursum(sum, -i);
1654 mutex_unlock(&curseg->curseg_mutex);
1659 * This function is called during the checkpointing process.
1661 void flush_nat_entries(struct f2fs_sb_info *sbi)
1663 struct f2fs_nm_info *nm_i = NM_I(sbi);
1664 struct curseg_info *curseg = CURSEG_I(sbi, CURSEG_HOT_DATA);
1665 struct f2fs_summary_block *sum = curseg->sum_blk;
1666 struct list_head *cur, *n;
1667 struct page *page = NULL;
1668 struct f2fs_nat_block *nat_blk = NULL;
1669 nid_t start_nid = 0, end_nid = 0;
1672 flushed = flush_nats_in_journal(sbi);
1675 mutex_lock(&curseg->curseg_mutex);
1677 /* 1) flush dirty nat caches */
1678 list_for_each_safe(cur, n, &nm_i->dirty_nat_entries) {
1679 struct nat_entry *ne;
1681 struct f2fs_nat_entry raw_ne;
1683 block_t new_blkaddr;
1685 ne = list_entry(cur, struct nat_entry, list);
1686 nid = nat_get_nid(ne);
1688 if (nat_get_blkaddr(ne) == NEW_ADDR)
1693 /* if there is room for nat enries in curseg->sumpage */
1694 offset = lookup_journal_in_cursum(sum, NAT_JOURNAL, nid, 1);
1696 raw_ne = nat_in_journal(sum, offset);
1700 if (!page || (start_nid > nid || nid > end_nid)) {
1702 f2fs_put_page(page, 1);
1705 start_nid = START_NID(nid);
1706 end_nid = start_nid + NAT_ENTRY_PER_BLOCK - 1;
1709 * get nat block with dirty flag, increased reference
1710 * count, mapped and lock
1712 page = get_next_nat_page(sbi, start_nid);
1713 nat_blk = page_address(page);
1716 f2fs_bug_on(!nat_blk);
1717 raw_ne = nat_blk->entries[nid - start_nid];
1719 new_blkaddr = nat_get_blkaddr(ne);
1721 raw_ne.ino = cpu_to_le32(nat_get_ino(ne));
1722 raw_ne.block_addr = cpu_to_le32(new_blkaddr);
1723 raw_ne.version = nat_get_version(ne);
1726 nat_blk->entries[nid - start_nid] = raw_ne;
1728 nat_in_journal(sum, offset) = raw_ne;
1729 nid_in_journal(sum, offset) = cpu_to_le32(nid);
1732 if (nat_get_blkaddr(ne) == NULL_ADDR &&
1733 add_free_nid(NM_I(sbi), nid, false) <= 0) {
1734 write_lock(&nm_i->nat_tree_lock);
1735 __del_from_nat_cache(nm_i, ne);
1736 write_unlock(&nm_i->nat_tree_lock);
1738 write_lock(&nm_i->nat_tree_lock);
1739 __clear_nat_cache_dirty(nm_i, ne);
1740 ne->checkpointed = true;
1741 write_unlock(&nm_i->nat_tree_lock);
1745 mutex_unlock(&curseg->curseg_mutex);
1746 f2fs_put_page(page, 1);
1748 /* 2) shrink nat caches if necessary */
1749 try_to_free_nats(sbi, nm_i->nat_cnt - NM_WOUT_THRESHOLD);
1752 static int init_node_manager(struct f2fs_sb_info *sbi)
1754 struct f2fs_super_block *sb_raw = F2FS_RAW_SUPER(sbi);
1755 struct f2fs_nm_info *nm_i = NM_I(sbi);
1756 unsigned char *version_bitmap;
1757 unsigned int nat_segs, nat_blocks;
1759 nm_i->nat_blkaddr = le32_to_cpu(sb_raw->nat_blkaddr);
1761 /* segment_count_nat includes pair segment so divide to 2. */
1762 nat_segs = le32_to_cpu(sb_raw->segment_count_nat) >> 1;
1763 nat_blocks = nat_segs << le32_to_cpu(sb_raw->log_blocks_per_seg);
1764 nm_i->max_nid = NAT_ENTRY_PER_BLOCK * nat_blocks;
1768 INIT_LIST_HEAD(&nm_i->free_nid_list);
1769 INIT_RADIX_TREE(&nm_i->nat_root, GFP_ATOMIC);
1770 INIT_LIST_HEAD(&nm_i->nat_entries);
1771 INIT_LIST_HEAD(&nm_i->dirty_nat_entries);
1773 mutex_init(&nm_i->build_lock);
1774 spin_lock_init(&nm_i->free_nid_list_lock);
1775 rwlock_init(&nm_i->nat_tree_lock);
1777 nm_i->next_scan_nid = le32_to_cpu(sbi->ckpt->next_free_nid);
1778 nm_i->bitmap_size = __bitmap_size(sbi, NAT_BITMAP);
1779 version_bitmap = __bitmap_ptr(sbi, NAT_BITMAP);
1780 if (!version_bitmap)
1783 nm_i->nat_bitmap = kmemdup(version_bitmap, nm_i->bitmap_size,
1785 if (!nm_i->nat_bitmap)
1790 int build_node_manager(struct f2fs_sb_info *sbi)
1794 sbi->nm_info = kzalloc(sizeof(struct f2fs_nm_info), GFP_KERNEL);
1798 err = init_node_manager(sbi);
1802 build_free_nids(sbi);
1806 void destroy_node_manager(struct f2fs_sb_info *sbi)
1808 struct f2fs_nm_info *nm_i = NM_I(sbi);
1809 struct free_nid *i, *next_i;
1810 struct nat_entry *natvec[NATVEC_SIZE];
1817 /* destroy free nid list */
1818 spin_lock(&nm_i->free_nid_list_lock);
1819 list_for_each_entry_safe(i, next_i, &nm_i->free_nid_list, list) {
1820 f2fs_bug_on(i->state == NID_ALLOC);
1821 __del_from_free_nid_list(i);
1824 f2fs_bug_on(nm_i->fcnt);
1825 spin_unlock(&nm_i->free_nid_list_lock);
1827 /* destroy nat cache */
1828 write_lock(&nm_i->nat_tree_lock);
1829 while ((found = __gang_lookup_nat_cache(nm_i,
1830 nid, NATVEC_SIZE, natvec))) {
1832 for (idx = 0; idx < found; idx++) {
1833 struct nat_entry *e = natvec[idx];
1834 nid = nat_get_nid(e) + 1;
1835 __del_from_nat_cache(nm_i, e);
1838 f2fs_bug_on(nm_i->nat_cnt);
1839 write_unlock(&nm_i->nat_tree_lock);
1841 kfree(nm_i->nat_bitmap);
1842 sbi->nm_info = NULL;
1846 int __init create_node_manager_caches(void)
1848 nat_entry_slab = f2fs_kmem_cache_create("nat_entry",
1849 sizeof(struct nat_entry), NULL);
1850 if (!nat_entry_slab)
1853 free_nid_slab = f2fs_kmem_cache_create("free_nid",
1854 sizeof(struct free_nid), NULL);
1855 if (!free_nid_slab) {
1856 kmem_cache_destroy(nat_entry_slab);
1862 void destroy_node_manager_caches(void)
1864 kmem_cache_destroy(free_nid_slab);
1865 kmem_cache_destroy(nat_entry_slab);