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);
95 struct f2fs_io_info fio = {
98 .rw_flag = REQ_META | REQ_PRIO
102 for (i = 0; i < FREE_NID_PAGES; i++, nid += NAT_ENTRY_PER_BLOCK) {
103 if (unlikely(nid >= nm_i->max_nid))
105 index = current_nat_addr(sbi, nid);
107 page = grab_cache_page(mapping, index);
110 if (PageUptodate(page)) {
111 mark_page_accessed(page);
112 f2fs_put_page(page, 1);
115 f2fs_submit_page_mbio(sbi, page, index, &fio);
116 mark_page_accessed(page);
117 f2fs_put_page(page, 0);
119 f2fs_submit_merged_bio(sbi, META, READ);
122 static struct nat_entry *__lookup_nat_cache(struct f2fs_nm_info *nm_i, nid_t n)
124 return radix_tree_lookup(&nm_i->nat_root, n);
127 static unsigned int __gang_lookup_nat_cache(struct f2fs_nm_info *nm_i,
128 nid_t start, unsigned int nr, struct nat_entry **ep)
130 return radix_tree_gang_lookup(&nm_i->nat_root, (void **)ep, start, nr);
133 static void __del_from_nat_cache(struct f2fs_nm_info *nm_i, struct nat_entry *e)
136 radix_tree_delete(&nm_i->nat_root, nat_get_nid(e));
138 kmem_cache_free(nat_entry_slab, e);
141 int is_checkpointed_node(struct f2fs_sb_info *sbi, nid_t nid)
143 struct f2fs_nm_info *nm_i = NM_I(sbi);
147 read_lock(&nm_i->nat_tree_lock);
148 e = __lookup_nat_cache(nm_i, nid);
149 if (e && !e->checkpointed)
151 read_unlock(&nm_i->nat_tree_lock);
155 static struct nat_entry *grab_nat_entry(struct f2fs_nm_info *nm_i, nid_t nid)
157 struct nat_entry *new;
159 new = kmem_cache_alloc(nat_entry_slab, GFP_ATOMIC);
162 if (radix_tree_insert(&nm_i->nat_root, nid, new)) {
163 kmem_cache_free(nat_entry_slab, new);
166 memset(new, 0, sizeof(struct nat_entry));
167 nat_set_nid(new, nid);
168 list_add_tail(&new->list, &nm_i->nat_entries);
173 static void cache_nat_entry(struct f2fs_nm_info *nm_i, nid_t nid,
174 struct f2fs_nat_entry *ne)
178 write_lock(&nm_i->nat_tree_lock);
179 e = __lookup_nat_cache(nm_i, nid);
181 e = grab_nat_entry(nm_i, nid);
183 write_unlock(&nm_i->nat_tree_lock);
186 nat_set_blkaddr(e, le32_to_cpu(ne->block_addr));
187 nat_set_ino(e, le32_to_cpu(ne->ino));
188 nat_set_version(e, ne->version);
189 e->checkpointed = true;
191 write_unlock(&nm_i->nat_tree_lock);
194 static void set_node_addr(struct f2fs_sb_info *sbi, struct node_info *ni,
197 struct f2fs_nm_info *nm_i = NM_I(sbi);
200 write_lock(&nm_i->nat_tree_lock);
201 e = __lookup_nat_cache(nm_i, ni->nid);
203 e = grab_nat_entry(nm_i, ni->nid);
205 write_unlock(&nm_i->nat_tree_lock);
209 e->checkpointed = true;
210 f2fs_bug_on(ni->blk_addr == NEW_ADDR);
211 } else if (new_blkaddr == NEW_ADDR) {
213 * when nid is reallocated,
214 * previous nat entry can be remained in nat cache.
215 * So, reinitialize it with new information.
218 f2fs_bug_on(ni->blk_addr != NULL_ADDR);
221 if (new_blkaddr == NEW_ADDR)
222 e->checkpointed = false;
225 f2fs_bug_on(nat_get_blkaddr(e) != ni->blk_addr);
226 f2fs_bug_on(nat_get_blkaddr(e) == NULL_ADDR &&
227 new_blkaddr == NULL_ADDR);
228 f2fs_bug_on(nat_get_blkaddr(e) == NEW_ADDR &&
229 new_blkaddr == NEW_ADDR);
230 f2fs_bug_on(nat_get_blkaddr(e) != NEW_ADDR &&
231 nat_get_blkaddr(e) != NULL_ADDR &&
232 new_blkaddr == NEW_ADDR);
234 /* increament version no as node is removed */
235 if (nat_get_blkaddr(e) != NEW_ADDR && new_blkaddr == NULL_ADDR) {
236 unsigned char version = nat_get_version(e);
237 nat_set_version(e, inc_node_version(version));
241 nat_set_blkaddr(e, new_blkaddr);
242 __set_nat_cache_dirty(nm_i, e);
243 write_unlock(&nm_i->nat_tree_lock);
246 int try_to_free_nats(struct f2fs_sb_info *sbi, int nr_shrink)
248 struct f2fs_nm_info *nm_i = NM_I(sbi);
250 if (nm_i->nat_cnt <= NM_WOUT_THRESHOLD)
253 write_lock(&nm_i->nat_tree_lock);
254 while (nr_shrink && !list_empty(&nm_i->nat_entries)) {
255 struct nat_entry *ne;
256 ne = list_first_entry(&nm_i->nat_entries,
257 struct nat_entry, list);
258 __del_from_nat_cache(nm_i, ne);
261 write_unlock(&nm_i->nat_tree_lock);
266 * This function returns always success
268 void get_node_info(struct f2fs_sb_info *sbi, nid_t nid, struct node_info *ni)
270 struct f2fs_nm_info *nm_i = NM_I(sbi);
271 struct curseg_info *curseg = CURSEG_I(sbi, CURSEG_HOT_DATA);
272 struct f2fs_summary_block *sum = curseg->sum_blk;
273 nid_t start_nid = START_NID(nid);
274 struct f2fs_nat_block *nat_blk;
275 struct page *page = NULL;
276 struct f2fs_nat_entry ne;
280 memset(&ne, 0, sizeof(struct f2fs_nat_entry));
283 /* Check nat cache */
284 read_lock(&nm_i->nat_tree_lock);
285 e = __lookup_nat_cache(nm_i, nid);
287 ni->ino = nat_get_ino(e);
288 ni->blk_addr = nat_get_blkaddr(e);
289 ni->version = nat_get_version(e);
291 read_unlock(&nm_i->nat_tree_lock);
295 /* Check current segment summary */
296 mutex_lock(&curseg->curseg_mutex);
297 i = lookup_journal_in_cursum(sum, NAT_JOURNAL, nid, 0);
299 ne = nat_in_journal(sum, i);
300 node_info_from_raw_nat(ni, &ne);
302 mutex_unlock(&curseg->curseg_mutex);
306 /* Fill node_info from nat page */
307 page = get_current_nat_page(sbi, start_nid);
308 nat_blk = (struct f2fs_nat_block *)page_address(page);
309 ne = nat_blk->entries[nid - start_nid];
310 node_info_from_raw_nat(ni, &ne);
311 f2fs_put_page(page, 1);
313 /* cache nat entry */
314 cache_nat_entry(NM_I(sbi), nid, &ne);
318 * The maximum depth is four.
319 * Offset[0] will have raw inode offset.
321 static int get_node_path(struct f2fs_inode_info *fi, long block,
322 int offset[4], unsigned int noffset[4])
324 const long direct_index = ADDRS_PER_INODE(fi);
325 const long direct_blks = ADDRS_PER_BLOCK;
326 const long dptrs_per_blk = NIDS_PER_BLOCK;
327 const long indirect_blks = ADDRS_PER_BLOCK * NIDS_PER_BLOCK;
328 const long dindirect_blks = indirect_blks * NIDS_PER_BLOCK;
334 if (block < direct_index) {
338 block -= direct_index;
339 if (block < direct_blks) {
340 offset[n++] = NODE_DIR1_BLOCK;
346 block -= direct_blks;
347 if (block < direct_blks) {
348 offset[n++] = NODE_DIR2_BLOCK;
354 block -= direct_blks;
355 if (block < indirect_blks) {
356 offset[n++] = NODE_IND1_BLOCK;
358 offset[n++] = block / direct_blks;
359 noffset[n] = 4 + offset[n - 1];
360 offset[n] = block % direct_blks;
364 block -= indirect_blks;
365 if (block < indirect_blks) {
366 offset[n++] = NODE_IND2_BLOCK;
367 noffset[n] = 4 + dptrs_per_blk;
368 offset[n++] = block / direct_blks;
369 noffset[n] = 5 + dptrs_per_blk + offset[n - 1];
370 offset[n] = block % direct_blks;
374 block -= indirect_blks;
375 if (block < dindirect_blks) {
376 offset[n++] = NODE_DIND_BLOCK;
377 noffset[n] = 5 + (dptrs_per_blk * 2);
378 offset[n++] = block / indirect_blks;
379 noffset[n] = 6 + (dptrs_per_blk * 2) +
380 offset[n - 1] * (dptrs_per_blk + 1);
381 offset[n++] = (block / direct_blks) % dptrs_per_blk;
382 noffset[n] = 7 + (dptrs_per_blk * 2) +
383 offset[n - 2] * (dptrs_per_blk + 1) +
385 offset[n] = block % direct_blks;
396 * Caller should call f2fs_put_dnode(dn).
397 * Also, it should grab and release a mutex by calling mutex_lock_op() and
398 * mutex_unlock_op() only if ro is not set RDONLY_NODE.
399 * In the case of RDONLY_NODE, we don't need to care about mutex.
401 int get_dnode_of_data(struct dnode_of_data *dn, pgoff_t index, int mode)
403 struct f2fs_sb_info *sbi = F2FS_SB(dn->inode->i_sb);
404 struct page *npage[4];
407 unsigned int noffset[4];
412 level = get_node_path(F2FS_I(dn->inode), index, offset, noffset);
414 nids[0] = dn->inode->i_ino;
415 npage[0] = dn->inode_page;
418 npage[0] = get_node_page(sbi, nids[0]);
419 if (IS_ERR(npage[0]))
420 return PTR_ERR(npage[0]);
424 nids[1] = get_nid(parent, offset[0], true);
425 dn->inode_page = npage[0];
426 dn->inode_page_locked = true;
428 /* get indirect or direct nodes */
429 for (i = 1; i <= level; i++) {
432 if (!nids[i] && mode == ALLOC_NODE) {
434 if (!alloc_nid(sbi, &(nids[i]))) {
440 npage[i] = new_node_page(dn, noffset[i], NULL);
441 if (IS_ERR(npage[i])) {
442 alloc_nid_failed(sbi, nids[i]);
443 err = PTR_ERR(npage[i]);
447 set_nid(parent, offset[i - 1], nids[i], i == 1);
448 alloc_nid_done(sbi, nids[i]);
450 } else if (mode == LOOKUP_NODE_RA && i == level && level > 1) {
451 npage[i] = get_node_page_ra(parent, offset[i - 1]);
452 if (IS_ERR(npage[i])) {
453 err = PTR_ERR(npage[i]);
459 dn->inode_page_locked = false;
462 f2fs_put_page(parent, 1);
466 npage[i] = get_node_page(sbi, nids[i]);
467 if (IS_ERR(npage[i])) {
468 err = PTR_ERR(npage[i]);
469 f2fs_put_page(npage[0], 0);
475 nids[i + 1] = get_nid(parent, offset[i], false);
478 dn->nid = nids[level];
479 dn->ofs_in_node = offset[level];
480 dn->node_page = npage[level];
481 dn->data_blkaddr = datablock_addr(dn->node_page, dn->ofs_in_node);
485 f2fs_put_page(parent, 1);
487 f2fs_put_page(npage[0], 0);
489 dn->inode_page = NULL;
490 dn->node_page = NULL;
494 static void truncate_node(struct dnode_of_data *dn)
496 struct f2fs_sb_info *sbi = F2FS_SB(dn->inode->i_sb);
499 get_node_info(sbi, dn->nid, &ni);
500 if (dn->inode->i_blocks == 0) {
501 f2fs_bug_on(ni.blk_addr != NULL_ADDR);
504 f2fs_bug_on(ni.blk_addr == NULL_ADDR);
506 /* Deallocate node address */
507 invalidate_blocks(sbi, ni.blk_addr);
508 dec_valid_node_count(sbi, dn->inode);
509 set_node_addr(sbi, &ni, NULL_ADDR);
511 if (dn->nid == dn->inode->i_ino) {
512 remove_orphan_inode(sbi, dn->nid);
513 dec_valid_inode_count(sbi);
518 clear_node_page_dirty(dn->node_page);
519 F2FS_SET_SB_DIRT(sbi);
521 f2fs_put_page(dn->node_page, 1);
522 dn->node_page = NULL;
523 trace_f2fs_truncate_node(dn->inode, dn->nid, ni.blk_addr);
526 static int truncate_dnode(struct dnode_of_data *dn)
528 struct f2fs_sb_info *sbi = F2FS_SB(dn->inode->i_sb);
534 /* get direct node */
535 page = get_node_page(sbi, dn->nid);
536 if (IS_ERR(page) && PTR_ERR(page) == -ENOENT)
538 else if (IS_ERR(page))
539 return PTR_ERR(page);
541 /* Make dnode_of_data for parameter */
542 dn->node_page = page;
544 truncate_data_blocks(dn);
549 static int truncate_nodes(struct dnode_of_data *dn, unsigned int nofs,
552 struct f2fs_sb_info *sbi = F2FS_SB(dn->inode->i_sb);
553 struct dnode_of_data rdn = *dn;
555 struct f2fs_node *rn;
557 unsigned int child_nofs;
562 return NIDS_PER_BLOCK + 1;
564 trace_f2fs_truncate_nodes_enter(dn->inode, dn->nid, dn->data_blkaddr);
566 page = get_node_page(sbi, dn->nid);
568 trace_f2fs_truncate_nodes_exit(dn->inode, PTR_ERR(page));
569 return PTR_ERR(page);
572 rn = F2FS_NODE(page);
574 for (i = ofs; i < NIDS_PER_BLOCK; i++, freed++) {
575 child_nid = le32_to_cpu(rn->in.nid[i]);
579 ret = truncate_dnode(&rdn);
582 set_nid(page, i, 0, false);
585 child_nofs = nofs + ofs * (NIDS_PER_BLOCK + 1) + 1;
586 for (i = ofs; i < NIDS_PER_BLOCK; i++) {
587 child_nid = le32_to_cpu(rn->in.nid[i]);
588 if (child_nid == 0) {
589 child_nofs += NIDS_PER_BLOCK + 1;
593 ret = truncate_nodes(&rdn, child_nofs, 0, depth - 1);
594 if (ret == (NIDS_PER_BLOCK + 1)) {
595 set_nid(page, i, 0, false);
597 } else if (ret < 0 && ret != -ENOENT) {
605 /* remove current indirect node */
606 dn->node_page = page;
610 f2fs_put_page(page, 1);
612 trace_f2fs_truncate_nodes_exit(dn->inode, freed);
616 f2fs_put_page(page, 1);
617 trace_f2fs_truncate_nodes_exit(dn->inode, ret);
621 static int truncate_partial_nodes(struct dnode_of_data *dn,
622 struct f2fs_inode *ri, int *offset, int depth)
624 struct f2fs_sb_info *sbi = F2FS_SB(dn->inode->i_sb);
625 struct page *pages[2];
632 nid[0] = le32_to_cpu(ri->i_nid[offset[0] - NODE_DIR1_BLOCK]);
636 /* get indirect nodes in the path */
637 for (i = 0; i < depth - 1; i++) {
638 /* refernece count'll be increased */
639 pages[i] = get_node_page(sbi, nid[i]);
640 if (IS_ERR(pages[i])) {
642 err = PTR_ERR(pages[i]);
645 nid[i + 1] = get_nid(pages[i], offset[i + 1], false);
648 /* free direct nodes linked to a partial indirect node */
649 for (i = offset[depth - 1]; i < NIDS_PER_BLOCK; i++) {
650 child_nid = get_nid(pages[idx], i, false);
654 err = truncate_dnode(dn);
657 set_nid(pages[idx], i, 0, false);
660 if (offset[depth - 1] == 0) {
661 dn->node_page = pages[idx];
665 f2fs_put_page(pages[idx], 1);
668 offset[depth - 1] = 0;
670 for (i = depth - 3; i >= 0; i--)
671 f2fs_put_page(pages[i], 1);
673 trace_f2fs_truncate_partial_nodes(dn->inode, nid, depth, err);
679 * All the block addresses of data and nodes should be nullified.
681 int truncate_inode_blocks(struct inode *inode, pgoff_t from)
683 struct f2fs_sb_info *sbi = F2FS_SB(inode->i_sb);
684 struct address_space *node_mapping = sbi->node_inode->i_mapping;
685 int err = 0, cont = 1;
686 int level, offset[4], noffset[4];
687 unsigned int nofs = 0;
688 struct f2fs_node *rn;
689 struct dnode_of_data dn;
692 trace_f2fs_truncate_inode_blocks_enter(inode, from);
694 level = get_node_path(F2FS_I(inode), from, offset, noffset);
696 page = get_node_page(sbi, inode->i_ino);
698 trace_f2fs_truncate_inode_blocks_exit(inode, PTR_ERR(page));
699 return PTR_ERR(page);
702 set_new_dnode(&dn, inode, page, NULL, 0);
705 rn = F2FS_NODE(page);
713 if (!offset[level - 1])
715 err = truncate_partial_nodes(&dn, &rn->i, offset, level);
716 if (err < 0 && err != -ENOENT)
718 nofs += 1 + NIDS_PER_BLOCK;
721 nofs = 5 + 2 * NIDS_PER_BLOCK;
722 if (!offset[level - 1])
724 err = truncate_partial_nodes(&dn, &rn->i, offset, level);
725 if (err < 0 && err != -ENOENT)
734 dn.nid = le32_to_cpu(rn->i.i_nid[offset[0] - NODE_DIR1_BLOCK]);
736 case NODE_DIR1_BLOCK:
737 case NODE_DIR2_BLOCK:
738 err = truncate_dnode(&dn);
741 case NODE_IND1_BLOCK:
742 case NODE_IND2_BLOCK:
743 err = truncate_nodes(&dn, nofs, offset[1], 2);
746 case NODE_DIND_BLOCK:
747 err = truncate_nodes(&dn, nofs, offset[1], 3);
754 if (err < 0 && err != -ENOENT)
756 if (offset[1] == 0 &&
757 rn->i.i_nid[offset[0] - NODE_DIR1_BLOCK]) {
759 if (unlikely(page->mapping != node_mapping)) {
760 f2fs_put_page(page, 1);
763 wait_on_page_writeback(page);
764 rn->i.i_nid[offset[0] - NODE_DIR1_BLOCK] = 0;
765 set_page_dirty(page);
773 f2fs_put_page(page, 0);
774 trace_f2fs_truncate_inode_blocks_exit(inode, err);
775 return err > 0 ? 0 : err;
778 int truncate_xattr_node(struct inode *inode, struct page *page)
780 struct f2fs_sb_info *sbi = F2FS_SB(inode->i_sb);
781 nid_t nid = F2FS_I(inode)->i_xattr_nid;
782 struct dnode_of_data dn;
788 npage = get_node_page(sbi, nid);
790 return PTR_ERR(npage);
792 F2FS_I(inode)->i_xattr_nid = 0;
794 /* need to do checkpoint during fsync */
795 F2FS_I(inode)->xattr_ver = cur_cp_version(F2FS_CKPT(sbi));
797 set_new_dnode(&dn, inode, page, npage, nid);
800 dn.inode_page_locked = true;
806 * Caller should grab and release a mutex by calling mutex_lock_op() and
809 void remove_inode_page(struct inode *inode)
811 struct f2fs_sb_info *sbi = F2FS_SB(inode->i_sb);
813 nid_t ino = inode->i_ino;
814 struct dnode_of_data dn;
816 page = get_node_page(sbi, ino);
820 if (truncate_xattr_node(inode, page)) {
821 f2fs_put_page(page, 1);
824 /* 0 is possible, after f2fs_new_inode() is failed */
825 f2fs_bug_on(inode->i_blocks != 0 && inode->i_blocks != 1);
826 set_new_dnode(&dn, inode, page, page, ino);
830 struct page *new_inode_page(struct inode *inode, const struct qstr *name)
832 struct dnode_of_data dn;
834 /* allocate inode page for new inode */
835 set_new_dnode(&dn, inode, NULL, NULL, inode->i_ino);
837 /* caller should f2fs_put_page(page, 1); */
838 return new_node_page(&dn, 0, NULL);
841 struct page *new_node_page(struct dnode_of_data *dn,
842 unsigned int ofs, struct page *ipage)
844 struct f2fs_sb_info *sbi = F2FS_SB(dn->inode->i_sb);
845 struct address_space *mapping = sbi->node_inode->i_mapping;
846 struct node_info old_ni, new_ni;
850 if (unlikely(is_inode_flag_set(F2FS_I(dn->inode), FI_NO_ALLOC)))
851 return ERR_PTR(-EPERM);
853 page = grab_cache_page(mapping, dn->nid);
855 return ERR_PTR(-ENOMEM);
857 if (unlikely(!inc_valid_node_count(sbi, dn->inode))) {
862 get_node_info(sbi, dn->nid, &old_ni);
864 /* Reinitialize old_ni with new node page */
865 f2fs_bug_on(old_ni.blk_addr != NULL_ADDR);
867 new_ni.ino = dn->inode->i_ino;
868 set_node_addr(sbi, &new_ni, NEW_ADDR);
870 fill_node_footer(page, dn->nid, dn->inode->i_ino, ofs, true);
871 set_cold_node(dn->inode, page);
872 SetPageUptodate(page);
873 set_page_dirty(page);
875 if (ofs == XATTR_NODE_OFFSET)
876 F2FS_I(dn->inode)->i_xattr_nid = dn->nid;
878 dn->node_page = page;
880 update_inode(dn->inode, ipage);
884 inc_valid_inode_count(sbi);
889 clear_node_page_dirty(page);
890 f2fs_put_page(page, 1);
895 * Caller should do after getting the following values.
896 * 0: f2fs_put_page(page, 0)
897 * LOCKED_PAGE: f2fs_put_page(page, 1)
900 static int read_node_page(struct page *page, int rw)
902 struct f2fs_sb_info *sbi = F2FS_SB(page->mapping->host->i_sb);
905 get_node_info(sbi, page->index, &ni);
907 if (unlikely(ni.blk_addr == NULL_ADDR)) {
908 f2fs_put_page(page, 1);
912 if (PageUptodate(page))
915 return f2fs_submit_page_bio(sbi, page, ni.blk_addr, rw);
919 * Readahead a node page
921 void ra_node_page(struct f2fs_sb_info *sbi, nid_t nid)
923 struct address_space *mapping = sbi->node_inode->i_mapping;
927 apage = find_get_page(mapping, nid);
928 if (apage && PageUptodate(apage)) {
929 f2fs_put_page(apage, 0);
932 f2fs_put_page(apage, 0);
934 apage = grab_cache_page(mapping, nid);
938 err = read_node_page(apage, READA);
940 f2fs_put_page(apage, 0);
941 else if (err == LOCKED_PAGE)
942 f2fs_put_page(apage, 1);
945 struct page *get_node_page(struct f2fs_sb_info *sbi, pgoff_t nid)
947 struct address_space *mapping = sbi->node_inode->i_mapping;
951 page = grab_cache_page(mapping, nid);
953 return ERR_PTR(-ENOMEM);
955 err = read_node_page(page, READ_SYNC);
958 else if (err == LOCKED_PAGE)
962 if (unlikely(!PageUptodate(page))) {
963 f2fs_put_page(page, 1);
964 return ERR_PTR(-EIO);
966 if (unlikely(page->mapping != mapping)) {
967 f2fs_put_page(page, 1);
971 f2fs_bug_on(nid != nid_of_node(page));
972 mark_page_accessed(page);
977 * Return a locked page for the desired node page.
978 * And, readahead MAX_RA_NODE number of node pages.
980 struct page *get_node_page_ra(struct page *parent, int start)
982 struct f2fs_sb_info *sbi = F2FS_SB(parent->mapping->host->i_sb);
983 struct address_space *mapping = sbi->node_inode->i_mapping;
984 struct blk_plug plug;
989 /* First, try getting the desired direct node. */
990 nid = get_nid(parent, start, false);
992 return ERR_PTR(-ENOENT);
994 page = grab_cache_page(mapping, nid);
996 return ERR_PTR(-ENOMEM);
998 err = read_node_page(page, READ_SYNC);
1000 return ERR_PTR(err);
1001 else if (err == LOCKED_PAGE)
1004 blk_start_plug(&plug);
1006 /* Then, try readahead for siblings of the desired node */
1007 end = start + MAX_RA_NODE;
1008 end = min(end, NIDS_PER_BLOCK);
1009 for (i = start + 1; i < end; i++) {
1010 nid = get_nid(parent, i, false);
1013 ra_node_page(sbi, nid);
1016 blk_finish_plug(&plug);
1019 if (unlikely(page->mapping != mapping)) {
1020 f2fs_put_page(page, 1);
1024 if (unlikely(!PageUptodate(page))) {
1025 f2fs_put_page(page, 1);
1026 return ERR_PTR(-EIO);
1028 mark_page_accessed(page);
1032 void sync_inode_page(struct dnode_of_data *dn)
1034 if (IS_INODE(dn->node_page) || dn->inode_page == dn->node_page) {
1035 update_inode(dn->inode, dn->node_page);
1036 } else if (dn->inode_page) {
1037 if (!dn->inode_page_locked)
1038 lock_page(dn->inode_page);
1039 update_inode(dn->inode, dn->inode_page);
1040 if (!dn->inode_page_locked)
1041 unlock_page(dn->inode_page);
1043 update_inode_page(dn->inode);
1047 int sync_node_pages(struct f2fs_sb_info *sbi, nid_t ino,
1048 struct writeback_control *wbc)
1050 struct address_space *mapping = sbi->node_inode->i_mapping;
1052 struct pagevec pvec;
1053 int step = ino ? 2 : 0;
1054 int nwritten = 0, wrote = 0;
1056 pagevec_init(&pvec, 0);
1062 while (index <= end) {
1064 nr_pages = pagevec_lookup_tag(&pvec, mapping, &index,
1065 PAGECACHE_TAG_DIRTY,
1066 min(end - index, (pgoff_t)PAGEVEC_SIZE-1) + 1);
1070 for (i = 0; i < nr_pages; i++) {
1071 struct page *page = pvec.pages[i];
1074 * flushing sequence with step:
1079 if (step == 0 && IS_DNODE(page))
1081 if (step == 1 && (!IS_DNODE(page) ||
1082 is_cold_node(page)))
1084 if (step == 2 && (!IS_DNODE(page) ||
1085 !is_cold_node(page)))
1090 * we should not skip writing node pages.
1092 if (ino && ino_of_node(page) == ino)
1094 else if (!trylock_page(page))
1097 if (unlikely(page->mapping != mapping)) {
1102 if (ino && ino_of_node(page) != ino)
1103 goto continue_unlock;
1105 if (!PageDirty(page)) {
1106 /* someone wrote it for us */
1107 goto continue_unlock;
1110 if (!clear_page_dirty_for_io(page))
1111 goto continue_unlock;
1113 /* called by fsync() */
1114 if (ino && IS_DNODE(page)) {
1115 int mark = !is_checkpointed_node(sbi, ino);
1116 set_fsync_mark(page, 1);
1118 set_dentry_mark(page, mark);
1121 set_fsync_mark(page, 0);
1122 set_dentry_mark(page, 0);
1124 mapping->a_ops->writepage(page, wbc);
1127 if (--wbc->nr_to_write == 0)
1130 pagevec_release(&pvec);
1133 if (wbc->nr_to_write == 0) {
1145 f2fs_submit_merged_bio(sbi, NODE, WRITE);
1149 int wait_on_node_pages_writeback(struct f2fs_sb_info *sbi, nid_t ino)
1151 struct address_space *mapping = sbi->node_inode->i_mapping;
1152 pgoff_t index = 0, end = LONG_MAX;
1153 struct pagevec pvec;
1155 int ret2 = 0, ret = 0;
1157 pagevec_init(&pvec, 0);
1158 while ((index <= end) &&
1159 (nr_pages = pagevec_lookup_tag(&pvec, mapping, &index,
1160 PAGECACHE_TAG_WRITEBACK,
1161 min(end - index, (pgoff_t)PAGEVEC_SIZE-1) + 1)) != 0) {
1164 for (i = 0; i < nr_pages; i++) {
1165 struct page *page = pvec.pages[i];
1167 /* until radix tree lookup accepts end_index */
1168 if (unlikely(page->index > end))
1171 if (ino && ino_of_node(page) == ino) {
1172 wait_on_page_writeback(page);
1173 if (TestClearPageError(page))
1177 pagevec_release(&pvec);
1181 if (unlikely(test_and_clear_bit(AS_ENOSPC, &mapping->flags)))
1183 if (unlikely(test_and_clear_bit(AS_EIO, &mapping->flags)))
1190 static int f2fs_write_node_page(struct page *page,
1191 struct writeback_control *wbc)
1193 struct f2fs_sb_info *sbi = F2FS_SB(page->mapping->host->i_sb);
1196 struct node_info ni;
1198 if (unlikely(sbi->por_doing))
1201 wait_on_page_writeback(page);
1203 /* get old block addr of this node page */
1204 nid = nid_of_node(page);
1205 f2fs_bug_on(page->index != nid);
1207 get_node_info(sbi, nid, &ni);
1209 /* This page is already truncated */
1210 if (unlikely(ni.blk_addr == NULL_ADDR)) {
1211 dec_page_count(sbi, F2FS_DIRTY_NODES);
1216 if (wbc->for_reclaim)
1219 mutex_lock(&sbi->node_write);
1220 set_page_writeback(page);
1221 write_node_page(sbi, page, nid, ni.blk_addr, &new_addr);
1222 set_node_addr(sbi, &ni, new_addr);
1223 dec_page_count(sbi, F2FS_DIRTY_NODES);
1224 mutex_unlock(&sbi->node_write);
1229 dec_page_count(sbi, F2FS_DIRTY_NODES);
1230 wbc->pages_skipped++;
1231 set_page_dirty(page);
1232 return AOP_WRITEPAGE_ACTIVATE;
1236 * It is very important to gather dirty pages and write at once, so that we can
1237 * submit a big bio without interfering other data writes.
1238 * Be default, 512 pages (2MB) * 3 node types, is more reasonable.
1240 #define COLLECT_DIRTY_NODES 1536
1241 static int f2fs_write_node_pages(struct address_space *mapping,
1242 struct writeback_control *wbc)
1244 struct f2fs_sb_info *sbi = F2FS_SB(mapping->host->i_sb);
1245 long nr_to_write = wbc->nr_to_write;
1247 /* balancing f2fs's metadata in background */
1248 f2fs_balance_fs_bg(sbi);
1250 /* collect a number of dirty node pages and write together */
1251 if (get_pages(sbi, F2FS_DIRTY_NODES) < COLLECT_DIRTY_NODES)
1254 /* if mounting is failed, skip writing node pages */
1255 wbc->nr_to_write = 3 * max_hw_blocks(sbi);
1256 sync_node_pages(sbi, 0, wbc);
1257 wbc->nr_to_write = nr_to_write - (3 * max_hw_blocks(sbi) -
1262 static int f2fs_set_node_page_dirty(struct page *page)
1264 struct address_space *mapping = page->mapping;
1265 struct f2fs_sb_info *sbi = F2FS_SB(mapping->host->i_sb);
1267 trace_f2fs_set_page_dirty(page, NODE);
1269 SetPageUptodate(page);
1270 if (!PageDirty(page)) {
1271 __set_page_dirty_nobuffers(page);
1272 inc_page_count(sbi, F2FS_DIRTY_NODES);
1273 SetPagePrivate(page);
1279 static void f2fs_invalidate_node_page(struct page *page, unsigned int offset,
1280 unsigned int length)
1282 struct inode *inode = page->mapping->host;
1283 struct f2fs_sb_info *sbi = F2FS_SB(inode->i_sb);
1284 if (PageDirty(page))
1285 dec_page_count(sbi, F2FS_DIRTY_NODES);
1286 ClearPagePrivate(page);
1289 static int f2fs_release_node_page(struct page *page, gfp_t wait)
1291 ClearPagePrivate(page);
1296 * Structure of the f2fs node operations
1298 const struct address_space_operations f2fs_node_aops = {
1299 .writepage = f2fs_write_node_page,
1300 .writepages = f2fs_write_node_pages,
1301 .set_page_dirty = f2fs_set_node_page_dirty,
1302 .invalidatepage = f2fs_invalidate_node_page,
1303 .releasepage = f2fs_release_node_page,
1306 static struct free_nid *__lookup_free_nid_list(nid_t n, struct list_head *head)
1308 struct list_head *this;
1310 list_for_each(this, head) {
1311 i = list_entry(this, struct free_nid, list);
1318 static void __del_from_free_nid_list(struct free_nid *i)
1321 kmem_cache_free(free_nid_slab, i);
1324 static int add_free_nid(struct f2fs_nm_info *nm_i, nid_t nid, bool build)
1327 struct nat_entry *ne;
1328 bool allocated = false;
1330 if (nm_i->fcnt > 2 * MAX_FREE_NIDS)
1333 /* 0 nid should not be used */
1334 if (unlikely(nid == 0))
1338 /* do not add allocated nids */
1339 read_lock(&nm_i->nat_tree_lock);
1340 ne = __lookup_nat_cache(nm_i, nid);
1341 if (ne && nat_get_blkaddr(ne) != NULL_ADDR)
1343 read_unlock(&nm_i->nat_tree_lock);
1348 i = f2fs_kmem_cache_alloc(free_nid_slab, GFP_NOFS);
1352 spin_lock(&nm_i->free_nid_list_lock);
1353 if (__lookup_free_nid_list(nid, &nm_i->free_nid_list)) {
1354 spin_unlock(&nm_i->free_nid_list_lock);
1355 kmem_cache_free(free_nid_slab, i);
1358 list_add_tail(&i->list, &nm_i->free_nid_list);
1360 spin_unlock(&nm_i->free_nid_list_lock);
1364 static void remove_free_nid(struct f2fs_nm_info *nm_i, nid_t nid)
1367 spin_lock(&nm_i->free_nid_list_lock);
1368 i = __lookup_free_nid_list(nid, &nm_i->free_nid_list);
1369 if (i && i->state == NID_NEW) {
1370 __del_from_free_nid_list(i);
1373 spin_unlock(&nm_i->free_nid_list_lock);
1376 static void scan_nat_page(struct f2fs_nm_info *nm_i,
1377 struct page *nat_page, nid_t start_nid)
1379 struct f2fs_nat_block *nat_blk = page_address(nat_page);
1383 i = start_nid % NAT_ENTRY_PER_BLOCK;
1385 for (; i < NAT_ENTRY_PER_BLOCK; i++, start_nid++) {
1387 if (unlikely(start_nid >= nm_i->max_nid))
1390 blk_addr = le32_to_cpu(nat_blk->entries[i].block_addr);
1391 f2fs_bug_on(blk_addr == NEW_ADDR);
1392 if (blk_addr == NULL_ADDR) {
1393 if (add_free_nid(nm_i, start_nid, true) < 0)
1399 static void build_free_nids(struct f2fs_sb_info *sbi)
1401 struct f2fs_nm_info *nm_i = NM_I(sbi);
1402 struct curseg_info *curseg = CURSEG_I(sbi, CURSEG_HOT_DATA);
1403 struct f2fs_summary_block *sum = curseg->sum_blk;
1405 nid_t nid = nm_i->next_scan_nid;
1407 /* Enough entries */
1408 if (nm_i->fcnt > NAT_ENTRY_PER_BLOCK)
1411 /* readahead nat pages to be scanned */
1412 ra_nat_pages(sbi, nid);
1415 struct page *page = get_current_nat_page(sbi, nid);
1417 scan_nat_page(nm_i, page, nid);
1418 f2fs_put_page(page, 1);
1420 nid += (NAT_ENTRY_PER_BLOCK - (nid % NAT_ENTRY_PER_BLOCK));
1421 if (unlikely(nid >= nm_i->max_nid))
1424 if (i++ == FREE_NID_PAGES)
1428 /* go to the next free nat pages to find free nids abundantly */
1429 nm_i->next_scan_nid = nid;
1431 /* find free nids from current sum_pages */
1432 mutex_lock(&curseg->curseg_mutex);
1433 for (i = 0; i < nats_in_cursum(sum); i++) {
1434 block_t addr = le32_to_cpu(nat_in_journal(sum, i).block_addr);
1435 nid = le32_to_cpu(nid_in_journal(sum, i));
1436 if (addr == NULL_ADDR)
1437 add_free_nid(nm_i, nid, true);
1439 remove_free_nid(nm_i, nid);
1441 mutex_unlock(&curseg->curseg_mutex);
1445 * If this function returns success, caller can obtain a new nid
1446 * from second parameter of this function.
1447 * The returned nid could be used ino as well as nid when inode is created.
1449 bool alloc_nid(struct f2fs_sb_info *sbi, nid_t *nid)
1451 struct f2fs_nm_info *nm_i = NM_I(sbi);
1452 struct free_nid *i = NULL;
1453 struct list_head *this;
1455 if (unlikely(sbi->total_valid_node_count + 1 >= nm_i->max_nid))
1458 spin_lock(&nm_i->free_nid_list_lock);
1460 /* We should not use stale free nids created by build_free_nids */
1461 if (nm_i->fcnt && !sbi->on_build_free_nids) {
1462 f2fs_bug_on(list_empty(&nm_i->free_nid_list));
1463 list_for_each(this, &nm_i->free_nid_list) {
1464 i = list_entry(this, struct free_nid, list);
1465 if (i->state == NID_NEW)
1469 f2fs_bug_on(i->state != NID_NEW);
1471 i->state = NID_ALLOC;
1473 spin_unlock(&nm_i->free_nid_list_lock);
1476 spin_unlock(&nm_i->free_nid_list_lock);
1478 /* Let's scan nat pages and its caches to get free nids */
1479 mutex_lock(&nm_i->build_lock);
1480 sbi->on_build_free_nids = true;
1481 build_free_nids(sbi);
1482 sbi->on_build_free_nids = false;
1483 mutex_unlock(&nm_i->build_lock);
1488 * alloc_nid() should be called prior to this function.
1490 void alloc_nid_done(struct f2fs_sb_info *sbi, nid_t nid)
1492 struct f2fs_nm_info *nm_i = NM_I(sbi);
1495 spin_lock(&nm_i->free_nid_list_lock);
1496 i = __lookup_free_nid_list(nid, &nm_i->free_nid_list);
1497 f2fs_bug_on(!i || i->state != NID_ALLOC);
1498 __del_from_free_nid_list(i);
1499 spin_unlock(&nm_i->free_nid_list_lock);
1503 * alloc_nid() should be called prior to this function.
1505 void alloc_nid_failed(struct f2fs_sb_info *sbi, nid_t nid)
1507 struct f2fs_nm_info *nm_i = NM_I(sbi);
1513 spin_lock(&nm_i->free_nid_list_lock);
1514 i = __lookup_free_nid_list(nid, &nm_i->free_nid_list);
1515 f2fs_bug_on(!i || i->state != NID_ALLOC);
1516 if (nm_i->fcnt > 2 * MAX_FREE_NIDS) {
1517 __del_from_free_nid_list(i);
1522 spin_unlock(&nm_i->free_nid_list_lock);
1525 void recover_node_page(struct f2fs_sb_info *sbi, struct page *page,
1526 struct f2fs_summary *sum, struct node_info *ni,
1527 block_t new_blkaddr)
1529 rewrite_node_page(sbi, page, sum, ni->blk_addr, new_blkaddr);
1530 set_node_addr(sbi, ni, new_blkaddr);
1531 clear_node_page_dirty(page);
1534 int recover_inode_page(struct f2fs_sb_info *sbi, struct page *page)
1536 struct address_space *mapping = sbi->node_inode->i_mapping;
1537 struct f2fs_node *src, *dst;
1538 nid_t ino = ino_of_node(page);
1539 struct node_info old_ni, new_ni;
1542 ipage = grab_cache_page(mapping, ino);
1546 /* Should not use this inode from free nid list */
1547 remove_free_nid(NM_I(sbi), ino);
1549 get_node_info(sbi, ino, &old_ni);
1550 SetPageUptodate(ipage);
1551 fill_node_footer(ipage, ino, ino, 0, true);
1553 src = F2FS_NODE(page);
1554 dst = F2FS_NODE(ipage);
1556 memcpy(dst, src, (unsigned long)&src->i.i_ext - (unsigned long)&src->i);
1558 dst->i.i_blocks = cpu_to_le64(1);
1559 dst->i.i_links = cpu_to_le32(1);
1560 dst->i.i_xattr_nid = 0;
1565 if (unlikely(!inc_valid_node_count(sbi, NULL)))
1567 set_node_addr(sbi, &new_ni, NEW_ADDR);
1568 inc_valid_inode_count(sbi);
1569 f2fs_put_page(ipage, 1);
1574 * ra_sum_pages() merge contiguous pages into one bio and submit.
1575 * these pre-readed pages are linked in pages list.
1577 static int ra_sum_pages(struct f2fs_sb_info *sbi, struct list_head *pages,
1578 int start, int nrpages)
1581 int page_idx = start;
1582 struct f2fs_io_info fio = {
1585 .rw_flag = REQ_META | REQ_PRIO
1588 for (; page_idx < start + nrpages; page_idx++) {
1589 /* alloc temporal page for read node summary info*/
1590 page = alloc_page(GFP_F2FS_ZERO);
1593 list_for_each_entry_safe(page, tmp, pages, lru) {
1594 list_del(&page->lru);
1596 __free_pages(page, 0);
1602 page->index = page_idx;
1603 list_add_tail(&page->lru, pages);
1606 list_for_each_entry(page, pages, lru)
1607 f2fs_submit_page_mbio(sbi, page, page->index, &fio);
1609 f2fs_submit_merged_bio(sbi, META, READ);
1613 int restore_node_summary(struct f2fs_sb_info *sbi,
1614 unsigned int segno, struct f2fs_summary_block *sum)
1616 struct f2fs_node *rn;
1617 struct f2fs_summary *sum_entry;
1618 struct page *page, *tmp;
1620 int bio_blocks = MAX_BIO_BLOCKS(max_hw_blocks(sbi));
1621 int i, last_offset, nrpages, err = 0;
1622 LIST_HEAD(page_list);
1624 /* scan the node segment */
1625 last_offset = sbi->blocks_per_seg;
1626 addr = START_BLOCK(sbi, segno);
1627 sum_entry = &sum->entries[0];
1629 for (i = 0; i < last_offset; i += nrpages, addr += nrpages) {
1630 nrpages = min(last_offset - i, bio_blocks);
1632 /* read ahead node pages */
1633 err = ra_sum_pages(sbi, &page_list, addr, nrpages);
1637 list_for_each_entry_safe(page, tmp, &page_list, lru) {
1640 if (unlikely(!PageUptodate(page))) {
1643 rn = F2FS_NODE(page);
1644 sum_entry->nid = rn->footer.nid;
1645 sum_entry->version = 0;
1646 sum_entry->ofs_in_node = 0;
1650 list_del(&page->lru);
1652 __free_pages(page, 0);
1658 static bool flush_nats_in_journal(struct f2fs_sb_info *sbi)
1660 struct f2fs_nm_info *nm_i = NM_I(sbi);
1661 struct curseg_info *curseg = CURSEG_I(sbi, CURSEG_HOT_DATA);
1662 struct f2fs_summary_block *sum = curseg->sum_blk;
1665 mutex_lock(&curseg->curseg_mutex);
1667 if (nats_in_cursum(sum) < NAT_JOURNAL_ENTRIES) {
1668 mutex_unlock(&curseg->curseg_mutex);
1672 for (i = 0; i < nats_in_cursum(sum); i++) {
1673 struct nat_entry *ne;
1674 struct f2fs_nat_entry raw_ne;
1675 nid_t nid = le32_to_cpu(nid_in_journal(sum, i));
1677 raw_ne = nat_in_journal(sum, i);
1679 write_lock(&nm_i->nat_tree_lock);
1680 ne = __lookup_nat_cache(nm_i, nid);
1682 __set_nat_cache_dirty(nm_i, ne);
1683 write_unlock(&nm_i->nat_tree_lock);
1686 ne = grab_nat_entry(nm_i, nid);
1688 write_unlock(&nm_i->nat_tree_lock);
1691 nat_set_blkaddr(ne, le32_to_cpu(raw_ne.block_addr));
1692 nat_set_ino(ne, le32_to_cpu(raw_ne.ino));
1693 nat_set_version(ne, raw_ne.version);
1694 __set_nat_cache_dirty(nm_i, ne);
1695 write_unlock(&nm_i->nat_tree_lock);
1697 update_nats_in_cursum(sum, -i);
1698 mutex_unlock(&curseg->curseg_mutex);
1703 * This function is called during the checkpointing process.
1705 void flush_nat_entries(struct f2fs_sb_info *sbi)
1707 struct f2fs_nm_info *nm_i = NM_I(sbi);
1708 struct curseg_info *curseg = CURSEG_I(sbi, CURSEG_HOT_DATA);
1709 struct f2fs_summary_block *sum = curseg->sum_blk;
1710 struct list_head *cur, *n;
1711 struct page *page = NULL;
1712 struct f2fs_nat_block *nat_blk = NULL;
1713 nid_t start_nid = 0, end_nid = 0;
1716 flushed = flush_nats_in_journal(sbi);
1719 mutex_lock(&curseg->curseg_mutex);
1721 /* 1) flush dirty nat caches */
1722 list_for_each_safe(cur, n, &nm_i->dirty_nat_entries) {
1723 struct nat_entry *ne;
1725 struct f2fs_nat_entry raw_ne;
1727 block_t new_blkaddr;
1729 ne = list_entry(cur, struct nat_entry, list);
1730 nid = nat_get_nid(ne);
1732 if (nat_get_blkaddr(ne) == NEW_ADDR)
1737 /* if there is room for nat enries in curseg->sumpage */
1738 offset = lookup_journal_in_cursum(sum, NAT_JOURNAL, nid, 1);
1740 raw_ne = nat_in_journal(sum, offset);
1744 if (!page || (start_nid > nid || nid > end_nid)) {
1746 f2fs_put_page(page, 1);
1749 start_nid = START_NID(nid);
1750 end_nid = start_nid + NAT_ENTRY_PER_BLOCK - 1;
1753 * get nat block with dirty flag, increased reference
1754 * count, mapped and lock
1756 page = get_next_nat_page(sbi, start_nid);
1757 nat_blk = page_address(page);
1760 f2fs_bug_on(!nat_blk);
1761 raw_ne = nat_blk->entries[nid - start_nid];
1763 new_blkaddr = nat_get_blkaddr(ne);
1765 raw_ne.ino = cpu_to_le32(nat_get_ino(ne));
1766 raw_ne.block_addr = cpu_to_le32(new_blkaddr);
1767 raw_ne.version = nat_get_version(ne);
1770 nat_blk->entries[nid - start_nid] = raw_ne;
1772 nat_in_journal(sum, offset) = raw_ne;
1773 nid_in_journal(sum, offset) = cpu_to_le32(nid);
1776 if (nat_get_blkaddr(ne) == NULL_ADDR &&
1777 add_free_nid(NM_I(sbi), nid, false) <= 0) {
1778 write_lock(&nm_i->nat_tree_lock);
1779 __del_from_nat_cache(nm_i, ne);
1780 write_unlock(&nm_i->nat_tree_lock);
1782 write_lock(&nm_i->nat_tree_lock);
1783 __clear_nat_cache_dirty(nm_i, ne);
1784 ne->checkpointed = true;
1785 write_unlock(&nm_i->nat_tree_lock);
1789 mutex_unlock(&curseg->curseg_mutex);
1790 f2fs_put_page(page, 1);
1792 /* 2) shrink nat caches if necessary */
1793 try_to_free_nats(sbi, nm_i->nat_cnt - NM_WOUT_THRESHOLD);
1796 static int init_node_manager(struct f2fs_sb_info *sbi)
1798 struct f2fs_super_block *sb_raw = F2FS_RAW_SUPER(sbi);
1799 struct f2fs_nm_info *nm_i = NM_I(sbi);
1800 unsigned char *version_bitmap;
1801 unsigned int nat_segs, nat_blocks;
1803 nm_i->nat_blkaddr = le32_to_cpu(sb_raw->nat_blkaddr);
1805 /* segment_count_nat includes pair segment so divide to 2. */
1806 nat_segs = le32_to_cpu(sb_raw->segment_count_nat) >> 1;
1807 nat_blocks = nat_segs << le32_to_cpu(sb_raw->log_blocks_per_seg);
1808 nm_i->max_nid = NAT_ENTRY_PER_BLOCK * nat_blocks;
1812 INIT_LIST_HEAD(&nm_i->free_nid_list);
1813 INIT_RADIX_TREE(&nm_i->nat_root, GFP_ATOMIC);
1814 INIT_LIST_HEAD(&nm_i->nat_entries);
1815 INIT_LIST_HEAD(&nm_i->dirty_nat_entries);
1817 mutex_init(&nm_i->build_lock);
1818 spin_lock_init(&nm_i->free_nid_list_lock);
1819 rwlock_init(&nm_i->nat_tree_lock);
1821 nm_i->next_scan_nid = le32_to_cpu(sbi->ckpt->next_free_nid);
1822 nm_i->bitmap_size = __bitmap_size(sbi, NAT_BITMAP);
1823 version_bitmap = __bitmap_ptr(sbi, NAT_BITMAP);
1824 if (!version_bitmap)
1827 nm_i->nat_bitmap = kmemdup(version_bitmap, nm_i->bitmap_size,
1829 if (!nm_i->nat_bitmap)
1834 int build_node_manager(struct f2fs_sb_info *sbi)
1838 sbi->nm_info = kzalloc(sizeof(struct f2fs_nm_info), GFP_KERNEL);
1842 err = init_node_manager(sbi);
1846 build_free_nids(sbi);
1850 void destroy_node_manager(struct f2fs_sb_info *sbi)
1852 struct f2fs_nm_info *nm_i = NM_I(sbi);
1853 struct free_nid *i, *next_i;
1854 struct nat_entry *natvec[NATVEC_SIZE];
1861 /* destroy free nid list */
1862 spin_lock(&nm_i->free_nid_list_lock);
1863 list_for_each_entry_safe(i, next_i, &nm_i->free_nid_list, list) {
1864 f2fs_bug_on(i->state == NID_ALLOC);
1865 __del_from_free_nid_list(i);
1868 f2fs_bug_on(nm_i->fcnt);
1869 spin_unlock(&nm_i->free_nid_list_lock);
1871 /* destroy nat cache */
1872 write_lock(&nm_i->nat_tree_lock);
1873 while ((found = __gang_lookup_nat_cache(nm_i,
1874 nid, NATVEC_SIZE, natvec))) {
1876 for (idx = 0; idx < found; idx++) {
1877 struct nat_entry *e = natvec[idx];
1878 nid = nat_get_nid(e) + 1;
1879 __del_from_nat_cache(nm_i, e);
1882 f2fs_bug_on(nm_i->nat_cnt);
1883 write_unlock(&nm_i->nat_tree_lock);
1885 kfree(nm_i->nat_bitmap);
1886 sbi->nm_info = NULL;
1890 int __init create_node_manager_caches(void)
1892 nat_entry_slab = f2fs_kmem_cache_create("nat_entry",
1893 sizeof(struct nat_entry), NULL);
1894 if (!nat_entry_slab)
1897 free_nid_slab = f2fs_kmem_cache_create("free_nid",
1898 sizeof(struct free_nid), NULL);
1899 if (!free_nid_slab) {
1900 kmem_cache_destroy(nat_entry_slab);
1906 void destroy_node_manager_caches(void)
1908 kmem_cache_destroy(free_nid_slab);
1909 kmem_cache_destroy(nat_entry_slab);