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 #define on_build_free_nids(nmi) mutex_is_locked(&nm_i->build_lock)
26 static struct kmem_cache *nat_entry_slab;
27 static struct kmem_cache *free_nid_slab;
29 bool available_free_memory(struct f2fs_sb_info *sbi, int type)
31 struct f2fs_nm_info *nm_i = NM_I(sbi);
33 unsigned long mem_size = 0;
37 /* give 25%, 25%, 50% memory for each components respectively */
38 if (type == FREE_NIDS) {
39 mem_size = (nm_i->fcnt * sizeof(struct free_nid)) >> 12;
40 res = mem_size < ((val.totalram * nm_i->ram_thresh / 100) >> 2);
41 } else if (type == NAT_ENTRIES) {
42 mem_size = (nm_i->nat_cnt * sizeof(struct nat_entry)) >> 12;
43 res = mem_size < ((val.totalram * nm_i->ram_thresh / 100) >> 2);
44 } else if (type == DIRTY_DENTS) {
45 if (sbi->sb->s_bdi->dirty_exceeded)
47 mem_size = get_pages(sbi, F2FS_DIRTY_DENTS);
48 res = mem_size < ((val.totalram * nm_i->ram_thresh / 100) >> 1);
53 static void clear_node_page_dirty(struct page *page)
55 struct address_space *mapping = page->mapping;
56 struct f2fs_sb_info *sbi = F2FS_SB(mapping->host->i_sb);
57 unsigned int long flags;
59 if (PageDirty(page)) {
60 spin_lock_irqsave(&mapping->tree_lock, flags);
61 radix_tree_tag_clear(&mapping->page_tree,
64 spin_unlock_irqrestore(&mapping->tree_lock, flags);
66 clear_page_dirty_for_io(page);
67 dec_page_count(sbi, F2FS_DIRTY_NODES);
69 ClearPageUptodate(page);
72 static struct page *get_current_nat_page(struct f2fs_sb_info *sbi, nid_t nid)
74 pgoff_t index = current_nat_addr(sbi, nid);
75 return get_meta_page(sbi, index);
78 static struct page *get_next_nat_page(struct f2fs_sb_info *sbi, nid_t nid)
80 struct page *src_page;
81 struct page *dst_page;
86 struct f2fs_nm_info *nm_i = NM_I(sbi);
88 src_off = current_nat_addr(sbi, nid);
89 dst_off = next_nat_addr(sbi, src_off);
91 /* get current nat block page with lock */
92 src_page = get_meta_page(sbi, src_off);
94 /* Dirty src_page means that it is already the new target NAT page. */
95 if (PageDirty(src_page))
98 dst_page = grab_meta_page(sbi, dst_off);
100 src_addr = page_address(src_page);
101 dst_addr = page_address(dst_page);
102 memcpy(dst_addr, src_addr, PAGE_CACHE_SIZE);
103 set_page_dirty(dst_page);
104 f2fs_put_page(src_page, 1);
106 set_to_next_nat(nm_i, nid);
111 static struct nat_entry *__lookup_nat_cache(struct f2fs_nm_info *nm_i, nid_t n)
113 return radix_tree_lookup(&nm_i->nat_root, n);
116 static unsigned int __gang_lookup_nat_cache(struct f2fs_nm_info *nm_i,
117 nid_t start, unsigned int nr, struct nat_entry **ep)
119 return radix_tree_gang_lookup(&nm_i->nat_root, (void **)ep, start, nr);
122 static void __del_from_nat_cache(struct f2fs_nm_info *nm_i, struct nat_entry *e)
125 radix_tree_delete(&nm_i->nat_root, nat_get_nid(e));
127 kmem_cache_free(nat_entry_slab, e);
130 int is_checkpointed_node(struct f2fs_sb_info *sbi, nid_t nid)
132 struct f2fs_nm_info *nm_i = NM_I(sbi);
136 read_lock(&nm_i->nat_tree_lock);
137 e = __lookup_nat_cache(nm_i, nid);
138 if (e && !e->checkpointed)
140 read_unlock(&nm_i->nat_tree_lock);
144 bool fsync_mark_done(struct f2fs_sb_info *sbi, nid_t nid)
146 struct f2fs_nm_info *nm_i = NM_I(sbi);
148 bool fsync_done = false;
150 read_lock(&nm_i->nat_tree_lock);
151 e = __lookup_nat_cache(nm_i, nid);
153 fsync_done = e->fsync_done;
154 read_unlock(&nm_i->nat_tree_lock);
158 void fsync_mark_clear(struct f2fs_sb_info *sbi, nid_t nid)
160 struct f2fs_nm_info *nm_i = NM_I(sbi);
163 write_lock(&nm_i->nat_tree_lock);
164 e = __lookup_nat_cache(nm_i, nid);
166 e->fsync_done = false;
167 write_unlock(&nm_i->nat_tree_lock);
170 static struct nat_entry *grab_nat_entry(struct f2fs_nm_info *nm_i, nid_t nid)
172 struct nat_entry *new;
174 new = kmem_cache_alloc(nat_entry_slab, GFP_ATOMIC);
177 if (radix_tree_insert(&nm_i->nat_root, nid, new)) {
178 kmem_cache_free(nat_entry_slab, new);
181 memset(new, 0, sizeof(struct nat_entry));
182 nat_set_nid(new, nid);
183 new->checkpointed = true;
184 list_add_tail(&new->list, &nm_i->nat_entries);
189 static void cache_nat_entry(struct f2fs_nm_info *nm_i, nid_t nid,
190 struct f2fs_nat_entry *ne)
194 write_lock(&nm_i->nat_tree_lock);
195 e = __lookup_nat_cache(nm_i, nid);
197 e = grab_nat_entry(nm_i, nid);
199 write_unlock(&nm_i->nat_tree_lock);
202 node_info_from_raw_nat(&e->ni, ne);
204 write_unlock(&nm_i->nat_tree_lock);
207 static void set_node_addr(struct f2fs_sb_info *sbi, struct node_info *ni,
208 block_t new_blkaddr, bool fsync_done)
210 struct f2fs_nm_info *nm_i = NM_I(sbi);
213 write_lock(&nm_i->nat_tree_lock);
214 e = __lookup_nat_cache(nm_i, ni->nid);
216 e = grab_nat_entry(nm_i, ni->nid);
218 write_unlock(&nm_i->nat_tree_lock);
222 f2fs_bug_on(ni->blk_addr == NEW_ADDR);
223 } else if (new_blkaddr == NEW_ADDR) {
225 * when nid is reallocated,
226 * previous nat entry can be remained in nat cache.
227 * So, reinitialize it with new information.
230 f2fs_bug_on(ni->blk_addr != NULL_ADDR);
234 f2fs_bug_on(nat_get_blkaddr(e) != ni->blk_addr);
235 f2fs_bug_on(nat_get_blkaddr(e) == NULL_ADDR &&
236 new_blkaddr == NULL_ADDR);
237 f2fs_bug_on(nat_get_blkaddr(e) == NEW_ADDR &&
238 new_blkaddr == NEW_ADDR);
239 f2fs_bug_on(nat_get_blkaddr(e) != NEW_ADDR &&
240 nat_get_blkaddr(e) != NULL_ADDR &&
241 new_blkaddr == NEW_ADDR);
243 /* increament version no as node is removed */
244 if (nat_get_blkaddr(e) != NEW_ADDR && new_blkaddr == NULL_ADDR) {
245 unsigned char version = nat_get_version(e);
246 nat_set_version(e, inc_node_version(version));
250 nat_set_blkaddr(e, new_blkaddr);
251 __set_nat_cache_dirty(nm_i, e);
253 /* update fsync_mark if its inode nat entry is still alive */
254 e = __lookup_nat_cache(nm_i, ni->ino);
256 e->fsync_done = fsync_done;
257 write_unlock(&nm_i->nat_tree_lock);
260 int try_to_free_nats(struct f2fs_sb_info *sbi, int nr_shrink)
262 struct f2fs_nm_info *nm_i = NM_I(sbi);
264 if (available_free_memory(sbi, NAT_ENTRIES))
267 write_lock(&nm_i->nat_tree_lock);
268 while (nr_shrink && !list_empty(&nm_i->nat_entries)) {
269 struct nat_entry *ne;
270 ne = list_first_entry(&nm_i->nat_entries,
271 struct nat_entry, list);
272 __del_from_nat_cache(nm_i, ne);
275 write_unlock(&nm_i->nat_tree_lock);
280 * This function returns always success
282 void get_node_info(struct f2fs_sb_info *sbi, nid_t nid, struct node_info *ni)
284 struct f2fs_nm_info *nm_i = NM_I(sbi);
285 struct curseg_info *curseg = CURSEG_I(sbi, CURSEG_HOT_DATA);
286 struct f2fs_summary_block *sum = curseg->sum_blk;
287 nid_t start_nid = START_NID(nid);
288 struct f2fs_nat_block *nat_blk;
289 struct page *page = NULL;
290 struct f2fs_nat_entry ne;
294 memset(&ne, 0, sizeof(struct f2fs_nat_entry));
297 /* Check nat cache */
298 read_lock(&nm_i->nat_tree_lock);
299 e = __lookup_nat_cache(nm_i, nid);
301 ni->ino = nat_get_ino(e);
302 ni->blk_addr = nat_get_blkaddr(e);
303 ni->version = nat_get_version(e);
305 read_unlock(&nm_i->nat_tree_lock);
309 /* Check current segment summary */
310 mutex_lock(&curseg->curseg_mutex);
311 i = lookup_journal_in_cursum(sum, NAT_JOURNAL, nid, 0);
313 ne = nat_in_journal(sum, i);
314 node_info_from_raw_nat(ni, &ne);
316 mutex_unlock(&curseg->curseg_mutex);
320 /* Fill node_info from nat page */
321 page = get_current_nat_page(sbi, start_nid);
322 nat_blk = (struct f2fs_nat_block *)page_address(page);
323 ne = nat_blk->entries[nid - start_nid];
324 node_info_from_raw_nat(ni, &ne);
325 f2fs_put_page(page, 1);
327 /* cache nat entry */
328 cache_nat_entry(NM_I(sbi), nid, &ne);
332 * The maximum depth is four.
333 * Offset[0] will have raw inode offset.
335 static int get_node_path(struct f2fs_inode_info *fi, long block,
336 int offset[4], unsigned int noffset[4])
338 const long direct_index = ADDRS_PER_INODE(fi);
339 const long direct_blks = ADDRS_PER_BLOCK;
340 const long dptrs_per_blk = NIDS_PER_BLOCK;
341 const long indirect_blks = ADDRS_PER_BLOCK * NIDS_PER_BLOCK;
342 const long dindirect_blks = indirect_blks * NIDS_PER_BLOCK;
348 if (block < direct_index) {
352 block -= direct_index;
353 if (block < direct_blks) {
354 offset[n++] = NODE_DIR1_BLOCK;
360 block -= direct_blks;
361 if (block < direct_blks) {
362 offset[n++] = NODE_DIR2_BLOCK;
368 block -= direct_blks;
369 if (block < indirect_blks) {
370 offset[n++] = NODE_IND1_BLOCK;
372 offset[n++] = block / direct_blks;
373 noffset[n] = 4 + offset[n - 1];
374 offset[n] = block % direct_blks;
378 block -= indirect_blks;
379 if (block < indirect_blks) {
380 offset[n++] = NODE_IND2_BLOCK;
381 noffset[n] = 4 + dptrs_per_blk;
382 offset[n++] = block / direct_blks;
383 noffset[n] = 5 + dptrs_per_blk + offset[n - 1];
384 offset[n] = block % direct_blks;
388 block -= indirect_blks;
389 if (block < dindirect_blks) {
390 offset[n++] = NODE_DIND_BLOCK;
391 noffset[n] = 5 + (dptrs_per_blk * 2);
392 offset[n++] = block / indirect_blks;
393 noffset[n] = 6 + (dptrs_per_blk * 2) +
394 offset[n - 1] * (dptrs_per_blk + 1);
395 offset[n++] = (block / direct_blks) % dptrs_per_blk;
396 noffset[n] = 7 + (dptrs_per_blk * 2) +
397 offset[n - 2] * (dptrs_per_blk + 1) +
399 offset[n] = block % direct_blks;
410 * Caller should call f2fs_put_dnode(dn).
411 * Also, it should grab and release a rwsem by calling f2fs_lock_op() and
412 * f2fs_unlock_op() only if ro is not set RDONLY_NODE.
413 * In the case of RDONLY_NODE, we don't need to care about mutex.
415 int get_dnode_of_data(struct dnode_of_data *dn, pgoff_t index, int mode)
417 struct f2fs_sb_info *sbi = F2FS_SB(dn->inode->i_sb);
418 struct page *npage[4];
421 unsigned int noffset[4];
426 level = get_node_path(F2FS_I(dn->inode), index, offset, noffset);
428 nids[0] = dn->inode->i_ino;
429 npage[0] = dn->inode_page;
432 npage[0] = get_node_page(sbi, nids[0]);
433 if (IS_ERR(npage[0]))
434 return PTR_ERR(npage[0]);
438 nids[1] = get_nid(parent, offset[0], true);
439 dn->inode_page = npage[0];
440 dn->inode_page_locked = true;
442 /* get indirect or direct nodes */
443 for (i = 1; i <= level; i++) {
446 if (!nids[i] && mode == ALLOC_NODE) {
448 if (!alloc_nid(sbi, &(nids[i]))) {
454 npage[i] = new_node_page(dn, noffset[i], NULL);
455 if (IS_ERR(npage[i])) {
456 alloc_nid_failed(sbi, nids[i]);
457 err = PTR_ERR(npage[i]);
461 set_nid(parent, offset[i - 1], nids[i], i == 1);
462 alloc_nid_done(sbi, nids[i]);
464 } else if (mode == LOOKUP_NODE_RA && i == level && level > 1) {
465 npage[i] = get_node_page_ra(parent, offset[i - 1]);
466 if (IS_ERR(npage[i])) {
467 err = PTR_ERR(npage[i]);
473 dn->inode_page_locked = false;
476 f2fs_put_page(parent, 1);
480 npage[i] = get_node_page(sbi, nids[i]);
481 if (IS_ERR(npage[i])) {
482 err = PTR_ERR(npage[i]);
483 f2fs_put_page(npage[0], 0);
489 nids[i + 1] = get_nid(parent, offset[i], false);
492 dn->nid = nids[level];
493 dn->ofs_in_node = offset[level];
494 dn->node_page = npage[level];
495 dn->data_blkaddr = datablock_addr(dn->node_page, dn->ofs_in_node);
499 f2fs_put_page(parent, 1);
501 f2fs_put_page(npage[0], 0);
503 dn->inode_page = NULL;
504 dn->node_page = NULL;
508 static void truncate_node(struct dnode_of_data *dn)
510 struct f2fs_sb_info *sbi = F2FS_SB(dn->inode->i_sb);
513 get_node_info(sbi, dn->nid, &ni);
514 if (dn->inode->i_blocks == 0) {
515 f2fs_bug_on(ni.blk_addr != NULL_ADDR);
518 f2fs_bug_on(ni.blk_addr == NULL_ADDR);
520 /* Deallocate node address */
521 invalidate_blocks(sbi, ni.blk_addr);
522 dec_valid_node_count(sbi, dn->inode);
523 set_node_addr(sbi, &ni, NULL_ADDR, false);
525 if (dn->nid == dn->inode->i_ino) {
526 remove_orphan_inode(sbi, dn->nid);
527 dec_valid_inode_count(sbi);
532 clear_node_page_dirty(dn->node_page);
533 F2FS_SET_SB_DIRT(sbi);
535 f2fs_put_page(dn->node_page, 1);
537 invalidate_mapping_pages(NODE_MAPPING(sbi),
538 dn->node_page->index, dn->node_page->index);
540 dn->node_page = NULL;
541 trace_f2fs_truncate_node(dn->inode, dn->nid, ni.blk_addr);
544 static int truncate_dnode(struct dnode_of_data *dn)
546 struct f2fs_sb_info *sbi = F2FS_SB(dn->inode->i_sb);
552 /* get direct node */
553 page = get_node_page(sbi, dn->nid);
554 if (IS_ERR(page) && PTR_ERR(page) == -ENOENT)
556 else if (IS_ERR(page))
557 return PTR_ERR(page);
559 /* Make dnode_of_data for parameter */
560 dn->node_page = page;
562 truncate_data_blocks(dn);
567 static int truncate_nodes(struct dnode_of_data *dn, unsigned int nofs,
570 struct f2fs_sb_info *sbi = F2FS_SB(dn->inode->i_sb);
571 struct dnode_of_data rdn = *dn;
573 struct f2fs_node *rn;
575 unsigned int child_nofs;
580 return NIDS_PER_BLOCK + 1;
582 trace_f2fs_truncate_nodes_enter(dn->inode, dn->nid, dn->data_blkaddr);
584 page = get_node_page(sbi, dn->nid);
586 trace_f2fs_truncate_nodes_exit(dn->inode, PTR_ERR(page));
587 return PTR_ERR(page);
590 rn = F2FS_NODE(page);
592 for (i = ofs; i < NIDS_PER_BLOCK; i++, freed++) {
593 child_nid = le32_to_cpu(rn->in.nid[i]);
597 ret = truncate_dnode(&rdn);
600 set_nid(page, i, 0, false);
603 child_nofs = nofs + ofs * (NIDS_PER_BLOCK + 1) + 1;
604 for (i = ofs; i < NIDS_PER_BLOCK; i++) {
605 child_nid = le32_to_cpu(rn->in.nid[i]);
606 if (child_nid == 0) {
607 child_nofs += NIDS_PER_BLOCK + 1;
611 ret = truncate_nodes(&rdn, child_nofs, 0, depth - 1);
612 if (ret == (NIDS_PER_BLOCK + 1)) {
613 set_nid(page, i, 0, false);
615 } else if (ret < 0 && ret != -ENOENT) {
623 /* remove current indirect node */
624 dn->node_page = page;
628 f2fs_put_page(page, 1);
630 trace_f2fs_truncate_nodes_exit(dn->inode, freed);
634 f2fs_put_page(page, 1);
635 trace_f2fs_truncate_nodes_exit(dn->inode, ret);
639 static int truncate_partial_nodes(struct dnode_of_data *dn,
640 struct f2fs_inode *ri, int *offset, int depth)
642 struct f2fs_sb_info *sbi = F2FS_SB(dn->inode->i_sb);
643 struct page *pages[2];
650 nid[0] = le32_to_cpu(ri->i_nid[offset[0] - NODE_DIR1_BLOCK]);
654 /* get indirect nodes in the path */
655 for (i = 0; i < idx + 1; i++) {
656 /* refernece count'll be increased */
657 pages[i] = get_node_page(sbi, nid[i]);
658 if (IS_ERR(pages[i])) {
659 err = PTR_ERR(pages[i]);
663 nid[i + 1] = get_nid(pages[i], offset[i + 1], false);
666 /* free direct nodes linked to a partial indirect node */
667 for (i = offset[idx + 1]; i < NIDS_PER_BLOCK; i++) {
668 child_nid = get_nid(pages[idx], i, false);
672 err = truncate_dnode(dn);
675 set_nid(pages[idx], i, 0, false);
678 if (offset[idx + 1] == 0) {
679 dn->node_page = pages[idx];
683 f2fs_put_page(pages[idx], 1);
689 for (i = idx; i >= 0; i--)
690 f2fs_put_page(pages[i], 1);
692 trace_f2fs_truncate_partial_nodes(dn->inode, nid, depth, err);
698 * All the block addresses of data and nodes should be nullified.
700 int truncate_inode_blocks(struct inode *inode, pgoff_t from)
702 struct f2fs_sb_info *sbi = F2FS_SB(inode->i_sb);
703 int err = 0, cont = 1;
704 int level, offset[4], noffset[4];
705 unsigned int nofs = 0;
706 struct f2fs_inode *ri;
707 struct dnode_of_data dn;
710 trace_f2fs_truncate_inode_blocks_enter(inode, from);
712 level = get_node_path(F2FS_I(inode), from, offset, noffset);
714 page = get_node_page(sbi, inode->i_ino);
716 trace_f2fs_truncate_inode_blocks_exit(inode, PTR_ERR(page));
717 return PTR_ERR(page);
720 set_new_dnode(&dn, inode, page, NULL, 0);
723 ri = F2FS_INODE(page);
731 if (!offset[level - 1])
733 err = truncate_partial_nodes(&dn, ri, offset, level);
734 if (err < 0 && err != -ENOENT)
736 nofs += 1 + NIDS_PER_BLOCK;
739 nofs = 5 + 2 * NIDS_PER_BLOCK;
740 if (!offset[level - 1])
742 err = truncate_partial_nodes(&dn, ri, offset, level);
743 if (err < 0 && err != -ENOENT)
752 dn.nid = le32_to_cpu(ri->i_nid[offset[0] - NODE_DIR1_BLOCK]);
754 case NODE_DIR1_BLOCK:
755 case NODE_DIR2_BLOCK:
756 err = truncate_dnode(&dn);
759 case NODE_IND1_BLOCK:
760 case NODE_IND2_BLOCK:
761 err = truncate_nodes(&dn, nofs, offset[1], 2);
764 case NODE_DIND_BLOCK:
765 err = truncate_nodes(&dn, nofs, offset[1], 3);
772 if (err < 0 && err != -ENOENT)
774 if (offset[1] == 0 &&
775 ri->i_nid[offset[0] - NODE_DIR1_BLOCK]) {
777 if (unlikely(page->mapping != NODE_MAPPING(sbi))) {
778 f2fs_put_page(page, 1);
781 f2fs_wait_on_page_writeback(page, NODE);
782 ri->i_nid[offset[0] - NODE_DIR1_BLOCK] = 0;
783 set_page_dirty(page);
791 f2fs_put_page(page, 0);
792 trace_f2fs_truncate_inode_blocks_exit(inode, err);
793 return err > 0 ? 0 : err;
796 int truncate_xattr_node(struct inode *inode, struct page *page)
798 struct f2fs_sb_info *sbi = F2FS_SB(inode->i_sb);
799 nid_t nid = F2FS_I(inode)->i_xattr_nid;
800 struct dnode_of_data dn;
806 npage = get_node_page(sbi, nid);
808 return PTR_ERR(npage);
810 F2FS_I(inode)->i_xattr_nid = 0;
812 /* need to do checkpoint during fsync */
813 F2FS_I(inode)->xattr_ver = cur_cp_version(F2FS_CKPT(sbi));
815 set_new_dnode(&dn, inode, page, npage, nid);
818 dn.inode_page_locked = true;
824 * Caller should grab and release a rwsem by calling f2fs_lock_op() and
827 void remove_inode_page(struct inode *inode)
829 struct f2fs_sb_info *sbi = F2FS_SB(inode->i_sb);
831 nid_t ino = inode->i_ino;
832 struct dnode_of_data dn;
834 page = get_node_page(sbi, ino);
838 if (truncate_xattr_node(inode, page)) {
839 f2fs_put_page(page, 1);
842 /* 0 is possible, after f2fs_new_inode() is failed */
843 f2fs_bug_on(inode->i_blocks != 0 && inode->i_blocks != 1);
844 set_new_dnode(&dn, inode, page, page, ino);
848 struct page *new_inode_page(struct inode *inode, const struct qstr *name)
850 struct dnode_of_data dn;
852 /* allocate inode page for new inode */
853 set_new_dnode(&dn, inode, NULL, NULL, inode->i_ino);
855 /* caller should f2fs_put_page(page, 1); */
856 return new_node_page(&dn, 0, NULL);
859 struct page *new_node_page(struct dnode_of_data *dn,
860 unsigned int ofs, struct page *ipage)
862 struct f2fs_sb_info *sbi = F2FS_SB(dn->inode->i_sb);
863 struct node_info old_ni, new_ni;
867 if (unlikely(is_inode_flag_set(F2FS_I(dn->inode), FI_NO_ALLOC)))
868 return ERR_PTR(-EPERM);
870 page = grab_cache_page(NODE_MAPPING(sbi), dn->nid);
872 return ERR_PTR(-ENOMEM);
874 if (unlikely(!inc_valid_node_count(sbi, dn->inode))) {
879 get_node_info(sbi, dn->nid, &old_ni);
881 /* Reinitialize old_ni with new node page */
882 f2fs_bug_on(old_ni.blk_addr != NULL_ADDR);
884 new_ni.ino = dn->inode->i_ino;
885 set_node_addr(sbi, &new_ni, NEW_ADDR, false);
887 f2fs_wait_on_page_writeback(page, NODE);
888 fill_node_footer(page, dn->nid, dn->inode->i_ino, ofs, true);
889 set_cold_node(dn->inode, page);
890 SetPageUptodate(page);
891 set_page_dirty(page);
893 if (f2fs_has_xattr_block(ofs))
894 F2FS_I(dn->inode)->i_xattr_nid = dn->nid;
896 dn->node_page = page;
898 update_inode(dn->inode, ipage);
902 inc_valid_inode_count(sbi);
907 clear_node_page_dirty(page);
908 f2fs_put_page(page, 1);
913 * Caller should do after getting the following values.
914 * 0: f2fs_put_page(page, 0)
915 * LOCKED_PAGE: f2fs_put_page(page, 1)
918 static int read_node_page(struct page *page, int rw)
920 struct f2fs_sb_info *sbi = F2FS_SB(page->mapping->host->i_sb);
923 get_node_info(sbi, page->index, &ni);
925 if (unlikely(ni.blk_addr == NULL_ADDR)) {
926 f2fs_put_page(page, 1);
930 if (PageUptodate(page))
933 return f2fs_submit_page_bio(sbi, page, ni.blk_addr, rw);
937 * Readahead a node page
939 void ra_node_page(struct f2fs_sb_info *sbi, nid_t nid)
944 apage = find_get_page(NODE_MAPPING(sbi), nid);
945 if (apage && PageUptodate(apage)) {
946 f2fs_put_page(apage, 0);
949 f2fs_put_page(apage, 0);
951 apage = grab_cache_page(NODE_MAPPING(sbi), nid);
955 err = read_node_page(apage, READA);
957 f2fs_put_page(apage, 0);
958 else if (err == LOCKED_PAGE)
959 f2fs_put_page(apage, 1);
962 struct page *get_node_page(struct f2fs_sb_info *sbi, pgoff_t nid)
967 page = grab_cache_page(NODE_MAPPING(sbi), nid);
969 return ERR_PTR(-ENOMEM);
971 err = read_node_page(page, READ_SYNC);
974 else if (err == LOCKED_PAGE)
978 if (unlikely(!PageUptodate(page) || nid != nid_of_node(page))) {
979 f2fs_put_page(page, 1);
980 return ERR_PTR(-EIO);
982 if (unlikely(page->mapping != NODE_MAPPING(sbi))) {
983 f2fs_put_page(page, 1);
991 * Return a locked page for the desired node page.
992 * And, readahead MAX_RA_NODE number of node pages.
994 struct page *get_node_page_ra(struct page *parent, int start)
996 struct f2fs_sb_info *sbi = F2FS_SB(parent->mapping->host->i_sb);
997 struct blk_plug plug;
1002 /* First, try getting the desired direct node. */
1003 nid = get_nid(parent, start, false);
1005 return ERR_PTR(-ENOENT);
1007 page = grab_cache_page(NODE_MAPPING(sbi), nid);
1009 return ERR_PTR(-ENOMEM);
1011 err = read_node_page(page, READ_SYNC);
1013 return ERR_PTR(err);
1014 else if (err == LOCKED_PAGE)
1017 blk_start_plug(&plug);
1019 /* Then, try readahead for siblings of the desired node */
1020 end = start + MAX_RA_NODE;
1021 end = min(end, NIDS_PER_BLOCK);
1022 for (i = start + 1; i < end; i++) {
1023 nid = get_nid(parent, i, false);
1026 ra_node_page(sbi, nid);
1029 blk_finish_plug(&plug);
1032 if (unlikely(page->mapping != NODE_MAPPING(sbi))) {
1033 f2fs_put_page(page, 1);
1037 if (unlikely(!PageUptodate(page))) {
1038 f2fs_put_page(page, 1);
1039 return ERR_PTR(-EIO);
1044 void sync_inode_page(struct dnode_of_data *dn)
1046 if (IS_INODE(dn->node_page) || dn->inode_page == dn->node_page) {
1047 update_inode(dn->inode, dn->node_page);
1048 } else if (dn->inode_page) {
1049 if (!dn->inode_page_locked)
1050 lock_page(dn->inode_page);
1051 update_inode(dn->inode, dn->inode_page);
1052 if (!dn->inode_page_locked)
1053 unlock_page(dn->inode_page);
1055 update_inode_page(dn->inode);
1059 int sync_node_pages(struct f2fs_sb_info *sbi, nid_t ino,
1060 struct writeback_control *wbc)
1063 struct pagevec pvec;
1064 int step = ino ? 2 : 0;
1065 int nwritten = 0, wrote = 0;
1067 pagevec_init(&pvec, 0);
1073 while (index <= end) {
1075 nr_pages = pagevec_lookup_tag(&pvec, NODE_MAPPING(sbi), &index,
1076 PAGECACHE_TAG_DIRTY,
1077 min(end - index, (pgoff_t)PAGEVEC_SIZE-1) + 1);
1081 for (i = 0; i < nr_pages; i++) {
1082 struct page *page = pvec.pages[i];
1085 * flushing sequence with step:
1090 if (step == 0 && IS_DNODE(page))
1092 if (step == 1 && (!IS_DNODE(page) ||
1093 is_cold_node(page)))
1095 if (step == 2 && (!IS_DNODE(page) ||
1096 !is_cold_node(page)))
1101 * we should not skip writing node pages.
1103 if (ino && ino_of_node(page) == ino)
1105 else if (!trylock_page(page))
1108 if (unlikely(page->mapping != NODE_MAPPING(sbi))) {
1113 if (ino && ino_of_node(page) != ino)
1114 goto continue_unlock;
1116 if (!PageDirty(page)) {
1117 /* someone wrote it for us */
1118 goto continue_unlock;
1121 if (!clear_page_dirty_for_io(page))
1122 goto continue_unlock;
1124 /* called by fsync() */
1125 if (ino && IS_DNODE(page)) {
1126 int mark = !is_checkpointed_node(sbi, ino);
1127 set_fsync_mark(page, 1);
1129 set_dentry_mark(page, mark);
1132 set_fsync_mark(page, 0);
1133 set_dentry_mark(page, 0);
1135 NODE_MAPPING(sbi)->a_ops->writepage(page, wbc);
1138 if (--wbc->nr_to_write == 0)
1141 pagevec_release(&pvec);
1144 if (wbc->nr_to_write == 0) {
1156 f2fs_submit_merged_bio(sbi, NODE, WRITE);
1160 int wait_on_node_pages_writeback(struct f2fs_sb_info *sbi, nid_t ino)
1162 pgoff_t index = 0, end = LONG_MAX;
1163 struct pagevec pvec;
1164 int ret2 = 0, ret = 0;
1166 pagevec_init(&pvec, 0);
1168 while (index <= end) {
1170 nr_pages = pagevec_lookup_tag(&pvec, NODE_MAPPING(sbi), &index,
1171 PAGECACHE_TAG_WRITEBACK,
1172 min(end - index, (pgoff_t)PAGEVEC_SIZE-1) + 1);
1176 for (i = 0; i < nr_pages; i++) {
1177 struct page *page = pvec.pages[i];
1179 /* until radix tree lookup accepts end_index */
1180 if (unlikely(page->index > end))
1183 if (ino && ino_of_node(page) == ino) {
1184 f2fs_wait_on_page_writeback(page, NODE);
1185 if (TestClearPageError(page))
1189 pagevec_release(&pvec);
1193 if (unlikely(test_and_clear_bit(AS_ENOSPC, &NODE_MAPPING(sbi)->flags)))
1195 if (unlikely(test_and_clear_bit(AS_EIO, &NODE_MAPPING(sbi)->flags)))
1202 static int f2fs_write_node_page(struct page *page,
1203 struct writeback_control *wbc)
1205 struct f2fs_sb_info *sbi = F2FS_SB(page->mapping->host->i_sb);
1208 struct node_info ni;
1209 struct f2fs_io_info fio = {
1211 .rw = (wbc->sync_mode == WB_SYNC_ALL) ? WRITE_SYNC : WRITE,
1214 trace_f2fs_writepage(page, NODE);
1216 if (unlikely(sbi->por_doing))
1219 f2fs_wait_on_page_writeback(page, NODE);
1221 /* get old block addr of this node page */
1222 nid = nid_of_node(page);
1223 f2fs_bug_on(page->index != nid);
1225 get_node_info(sbi, nid, &ni);
1227 /* This page is already truncated */
1228 if (unlikely(ni.blk_addr == NULL_ADDR)) {
1229 dec_page_count(sbi, F2FS_DIRTY_NODES);
1234 if (wbc->for_reclaim)
1237 mutex_lock(&sbi->node_write);
1238 set_page_writeback(page);
1239 write_node_page(sbi, page, &fio, nid, ni.blk_addr, &new_addr);
1240 set_node_addr(sbi, &ni, new_addr, is_fsync_dnode(page));
1241 dec_page_count(sbi, F2FS_DIRTY_NODES);
1242 mutex_unlock(&sbi->node_write);
1247 redirty_page_for_writepage(wbc, page);
1248 return AOP_WRITEPAGE_ACTIVATE;
1251 static int f2fs_write_node_pages(struct address_space *mapping,
1252 struct writeback_control *wbc)
1254 struct f2fs_sb_info *sbi = F2FS_SB(mapping->host->i_sb);
1257 trace_f2fs_writepages(mapping->host, wbc, NODE);
1259 /* balancing f2fs's metadata in background */
1260 f2fs_balance_fs_bg(sbi);
1262 /* collect a number of dirty node pages and write together */
1263 if (get_pages(sbi, F2FS_DIRTY_NODES) < nr_pages_to_skip(sbi, NODE))
1266 diff = nr_pages_to_write(sbi, NODE, wbc);
1267 wbc->sync_mode = WB_SYNC_NONE;
1268 sync_node_pages(sbi, 0, wbc);
1269 wbc->nr_to_write = max((long)0, wbc->nr_to_write - diff);
1273 wbc->pages_skipped += get_pages(sbi, F2FS_DIRTY_NODES);
1277 static int f2fs_set_node_page_dirty(struct page *page)
1279 struct address_space *mapping = page->mapping;
1280 struct f2fs_sb_info *sbi = F2FS_SB(mapping->host->i_sb);
1282 trace_f2fs_set_page_dirty(page, NODE);
1284 SetPageUptodate(page);
1285 if (!PageDirty(page)) {
1286 __set_page_dirty_nobuffers(page);
1287 inc_page_count(sbi, F2FS_DIRTY_NODES);
1288 SetPagePrivate(page);
1294 static void f2fs_invalidate_node_page(struct page *page, unsigned int offset,
1295 unsigned int length)
1297 struct inode *inode = page->mapping->host;
1298 struct f2fs_sb_info *sbi = F2FS_SB(inode->i_sb);
1299 if (PageDirty(page))
1300 dec_page_count(sbi, F2FS_DIRTY_NODES);
1301 ClearPagePrivate(page);
1304 static int f2fs_release_node_page(struct page *page, gfp_t wait)
1306 ClearPagePrivate(page);
1311 * Structure of the f2fs node operations
1313 const struct address_space_operations f2fs_node_aops = {
1314 .writepage = f2fs_write_node_page,
1315 .writepages = f2fs_write_node_pages,
1316 .set_page_dirty = f2fs_set_node_page_dirty,
1317 .invalidatepage = f2fs_invalidate_node_page,
1318 .releasepage = f2fs_release_node_page,
1321 static struct free_nid *__lookup_free_nid_list(struct f2fs_nm_info *nm_i,
1324 return radix_tree_lookup(&nm_i->free_nid_root, n);
1327 static void __del_from_free_nid_list(struct f2fs_nm_info *nm_i,
1331 radix_tree_delete(&nm_i->free_nid_root, i->nid);
1334 static int add_free_nid(struct f2fs_sb_info *sbi, nid_t nid, bool build)
1336 struct f2fs_nm_info *nm_i = NM_I(sbi);
1338 struct nat_entry *ne;
1339 bool allocated = false;
1341 if (!available_free_memory(sbi, FREE_NIDS))
1344 /* 0 nid should not be used */
1345 if (unlikely(nid == 0))
1349 /* do not add allocated nids */
1350 read_lock(&nm_i->nat_tree_lock);
1351 ne = __lookup_nat_cache(nm_i, nid);
1353 (!ne->checkpointed || nat_get_blkaddr(ne) != NULL_ADDR))
1355 read_unlock(&nm_i->nat_tree_lock);
1360 i = f2fs_kmem_cache_alloc(free_nid_slab, GFP_NOFS);
1364 spin_lock(&nm_i->free_nid_list_lock);
1365 if (radix_tree_insert(&nm_i->free_nid_root, i->nid, i)) {
1366 spin_unlock(&nm_i->free_nid_list_lock);
1367 kmem_cache_free(free_nid_slab, i);
1370 list_add_tail(&i->list, &nm_i->free_nid_list);
1372 spin_unlock(&nm_i->free_nid_list_lock);
1376 static void remove_free_nid(struct f2fs_nm_info *nm_i, nid_t nid)
1379 bool need_free = false;
1381 spin_lock(&nm_i->free_nid_list_lock);
1382 i = __lookup_free_nid_list(nm_i, nid);
1383 if (i && i->state == NID_NEW) {
1384 __del_from_free_nid_list(nm_i, i);
1388 spin_unlock(&nm_i->free_nid_list_lock);
1391 kmem_cache_free(free_nid_slab, i);
1394 static void scan_nat_page(struct f2fs_sb_info *sbi,
1395 struct page *nat_page, nid_t start_nid)
1397 struct f2fs_nm_info *nm_i = NM_I(sbi);
1398 struct f2fs_nat_block *nat_blk = page_address(nat_page);
1402 i = start_nid % NAT_ENTRY_PER_BLOCK;
1404 for (; i < NAT_ENTRY_PER_BLOCK; i++, start_nid++) {
1406 if (unlikely(start_nid >= nm_i->max_nid))
1409 blk_addr = le32_to_cpu(nat_blk->entries[i].block_addr);
1410 f2fs_bug_on(blk_addr == NEW_ADDR);
1411 if (blk_addr == NULL_ADDR) {
1412 if (add_free_nid(sbi, start_nid, true) < 0)
1418 static void build_free_nids(struct f2fs_sb_info *sbi)
1420 struct f2fs_nm_info *nm_i = NM_I(sbi);
1421 struct curseg_info *curseg = CURSEG_I(sbi, CURSEG_HOT_DATA);
1422 struct f2fs_summary_block *sum = curseg->sum_blk;
1424 nid_t nid = nm_i->next_scan_nid;
1426 /* Enough entries */
1427 if (nm_i->fcnt > NAT_ENTRY_PER_BLOCK)
1430 /* readahead nat pages to be scanned */
1431 ra_meta_pages(sbi, NAT_BLOCK_OFFSET(nid), FREE_NID_PAGES, META_NAT);
1434 struct page *page = get_current_nat_page(sbi, nid);
1436 scan_nat_page(sbi, page, nid);
1437 f2fs_put_page(page, 1);
1439 nid += (NAT_ENTRY_PER_BLOCK - (nid % NAT_ENTRY_PER_BLOCK));
1440 if (unlikely(nid >= nm_i->max_nid))
1443 if (i++ == FREE_NID_PAGES)
1447 /* go to the next free nat pages to find free nids abundantly */
1448 nm_i->next_scan_nid = nid;
1450 /* find free nids from current sum_pages */
1451 mutex_lock(&curseg->curseg_mutex);
1452 for (i = 0; i < nats_in_cursum(sum); i++) {
1453 block_t addr = le32_to_cpu(nat_in_journal(sum, i).block_addr);
1454 nid = le32_to_cpu(nid_in_journal(sum, i));
1455 if (addr == NULL_ADDR)
1456 add_free_nid(sbi, nid, true);
1458 remove_free_nid(nm_i, nid);
1460 mutex_unlock(&curseg->curseg_mutex);
1464 * If this function returns success, caller can obtain a new nid
1465 * from second parameter of this function.
1466 * The returned nid could be used ino as well as nid when inode is created.
1468 bool alloc_nid(struct f2fs_sb_info *sbi, nid_t *nid)
1470 struct f2fs_nm_info *nm_i = NM_I(sbi);
1471 struct free_nid *i = NULL;
1473 if (unlikely(sbi->total_valid_node_count + 1 > nm_i->available_nids))
1476 spin_lock(&nm_i->free_nid_list_lock);
1478 /* We should not use stale free nids created by build_free_nids */
1479 if (nm_i->fcnt && !on_build_free_nids(nm_i)) {
1480 f2fs_bug_on(list_empty(&nm_i->free_nid_list));
1481 list_for_each_entry(i, &nm_i->free_nid_list, list)
1482 if (i->state == NID_NEW)
1485 f2fs_bug_on(i->state != NID_NEW);
1487 i->state = NID_ALLOC;
1489 spin_unlock(&nm_i->free_nid_list_lock);
1492 spin_unlock(&nm_i->free_nid_list_lock);
1494 /* Let's scan nat pages and its caches to get free nids */
1495 mutex_lock(&nm_i->build_lock);
1496 build_free_nids(sbi);
1497 mutex_unlock(&nm_i->build_lock);
1502 * alloc_nid() should be called prior to this function.
1504 void alloc_nid_done(struct f2fs_sb_info *sbi, nid_t nid)
1506 struct f2fs_nm_info *nm_i = NM_I(sbi);
1509 spin_lock(&nm_i->free_nid_list_lock);
1510 i = __lookup_free_nid_list(nm_i, nid);
1511 f2fs_bug_on(!i || i->state != NID_ALLOC);
1512 __del_from_free_nid_list(nm_i, i);
1513 spin_unlock(&nm_i->free_nid_list_lock);
1515 kmem_cache_free(free_nid_slab, i);
1519 * alloc_nid() should be called prior to this function.
1521 void alloc_nid_failed(struct f2fs_sb_info *sbi, nid_t nid)
1523 struct f2fs_nm_info *nm_i = NM_I(sbi);
1525 bool need_free = false;
1530 spin_lock(&nm_i->free_nid_list_lock);
1531 i = __lookup_free_nid_list(nm_i, nid);
1532 f2fs_bug_on(!i || i->state != NID_ALLOC);
1533 if (!available_free_memory(sbi, FREE_NIDS)) {
1534 __del_from_free_nid_list(nm_i, i);
1540 spin_unlock(&nm_i->free_nid_list_lock);
1543 kmem_cache_free(free_nid_slab, i);
1546 void recover_node_page(struct f2fs_sb_info *sbi, struct page *page,
1547 struct f2fs_summary *sum, struct node_info *ni,
1548 block_t new_blkaddr)
1550 rewrite_node_page(sbi, page, sum, ni->blk_addr, new_blkaddr);
1551 set_node_addr(sbi, ni, new_blkaddr, false);
1552 clear_node_page_dirty(page);
1555 static void recover_inline_xattr(struct inode *inode, struct page *page)
1557 struct f2fs_sb_info *sbi = F2FS_SB(inode->i_sb);
1558 void *src_addr, *dst_addr;
1561 struct f2fs_inode *ri;
1563 if (!f2fs_has_inline_xattr(inode))
1566 if (!IS_INODE(page))
1569 ri = F2FS_INODE(page);
1570 if (!(ri->i_inline & F2FS_INLINE_XATTR))
1573 ipage = get_node_page(sbi, inode->i_ino);
1574 f2fs_bug_on(IS_ERR(ipage));
1576 dst_addr = inline_xattr_addr(ipage);
1577 src_addr = inline_xattr_addr(page);
1578 inline_size = inline_xattr_size(inode);
1580 f2fs_wait_on_page_writeback(ipage, NODE);
1581 memcpy(dst_addr, src_addr, inline_size);
1583 update_inode(inode, ipage);
1584 f2fs_put_page(ipage, 1);
1587 bool recover_xattr_data(struct inode *inode, struct page *page, block_t blkaddr)
1589 struct f2fs_sb_info *sbi = F2FS_SB(inode->i_sb);
1590 nid_t prev_xnid = F2FS_I(inode)->i_xattr_nid;
1591 nid_t new_xnid = nid_of_node(page);
1592 struct node_info ni;
1594 recover_inline_xattr(inode, page);
1596 if (!f2fs_has_xattr_block(ofs_of_node(page)))
1599 /* 1: invalidate the previous xattr nid */
1603 /* Deallocate node address */
1604 get_node_info(sbi, prev_xnid, &ni);
1605 f2fs_bug_on(ni.blk_addr == NULL_ADDR);
1606 invalidate_blocks(sbi, ni.blk_addr);
1607 dec_valid_node_count(sbi, inode);
1608 set_node_addr(sbi, &ni, NULL_ADDR, false);
1611 /* 2: allocate new xattr nid */
1612 if (unlikely(!inc_valid_node_count(sbi, inode)))
1615 remove_free_nid(NM_I(sbi), new_xnid);
1616 get_node_info(sbi, new_xnid, &ni);
1617 ni.ino = inode->i_ino;
1618 set_node_addr(sbi, &ni, NEW_ADDR, false);
1619 F2FS_I(inode)->i_xattr_nid = new_xnid;
1621 /* 3: update xattr blkaddr */
1622 refresh_sit_entry(sbi, NEW_ADDR, blkaddr);
1623 set_node_addr(sbi, &ni, blkaddr, false);
1625 update_inode_page(inode);
1629 int recover_inode_page(struct f2fs_sb_info *sbi, struct page *page)
1631 struct f2fs_inode *src, *dst;
1632 nid_t ino = ino_of_node(page);
1633 struct node_info old_ni, new_ni;
1636 get_node_info(sbi, ino, &old_ni);
1638 if (unlikely(old_ni.blk_addr != NULL_ADDR))
1641 ipage = grab_cache_page(NODE_MAPPING(sbi), ino);
1645 /* Should not use this inode from free nid list */
1646 remove_free_nid(NM_I(sbi), ino);
1648 SetPageUptodate(ipage);
1649 fill_node_footer(ipage, ino, ino, 0, true);
1651 src = F2FS_INODE(page);
1652 dst = F2FS_INODE(ipage);
1654 memcpy(dst, src, (unsigned long)&src->i_ext - (unsigned long)src);
1656 dst->i_blocks = cpu_to_le64(1);
1657 dst->i_links = cpu_to_le32(1);
1658 dst->i_xattr_nid = 0;
1663 if (unlikely(!inc_valid_node_count(sbi, NULL)))
1665 set_node_addr(sbi, &new_ni, NEW_ADDR, false);
1666 inc_valid_inode_count(sbi);
1667 f2fs_put_page(ipage, 1);
1672 * ra_sum_pages() merge contiguous pages into one bio and submit.
1673 * these pre-readed pages are alloced in bd_inode's mapping tree.
1675 static int ra_sum_pages(struct f2fs_sb_info *sbi, struct page **pages,
1676 int start, int nrpages)
1678 struct inode *inode = sbi->sb->s_bdev->bd_inode;
1679 struct address_space *mapping = inode->i_mapping;
1680 int i, page_idx = start;
1681 struct f2fs_io_info fio = {
1683 .rw = READ_SYNC | REQ_META | REQ_PRIO
1686 for (i = 0; page_idx < start + nrpages; page_idx++, i++) {
1687 /* alloc page in bd_inode for reading node summary info */
1688 pages[i] = grab_cache_page(mapping, page_idx);
1691 f2fs_submit_page_mbio(sbi, pages[i], page_idx, &fio);
1694 f2fs_submit_merged_bio(sbi, META, READ);
1698 int restore_node_summary(struct f2fs_sb_info *sbi,
1699 unsigned int segno, struct f2fs_summary_block *sum)
1701 struct f2fs_node *rn;
1702 struct f2fs_summary *sum_entry;
1703 struct inode *inode = sbi->sb->s_bdev->bd_inode;
1705 int bio_blocks = MAX_BIO_BLOCKS(max_hw_blocks(sbi));
1706 struct page *pages[bio_blocks];
1707 int i, idx, last_offset, nrpages, err = 0;
1709 /* scan the node segment */
1710 last_offset = sbi->blocks_per_seg;
1711 addr = START_BLOCK(sbi, segno);
1712 sum_entry = &sum->entries[0];
1714 for (i = 0; !err && i < last_offset; i += nrpages, addr += nrpages) {
1715 nrpages = min(last_offset - i, bio_blocks);
1717 /* read ahead node pages */
1718 nrpages = ra_sum_pages(sbi, pages, addr, nrpages);
1722 for (idx = 0; idx < nrpages; idx++) {
1726 lock_page(pages[idx]);
1727 if (unlikely(!PageUptodate(pages[idx]))) {
1730 rn = F2FS_NODE(pages[idx]);
1731 sum_entry->nid = rn->footer.nid;
1732 sum_entry->version = 0;
1733 sum_entry->ofs_in_node = 0;
1736 unlock_page(pages[idx]);
1738 page_cache_release(pages[idx]);
1741 invalidate_mapping_pages(inode->i_mapping, addr,
1747 static bool flush_nats_in_journal(struct f2fs_sb_info *sbi)
1749 struct f2fs_nm_info *nm_i = NM_I(sbi);
1750 struct curseg_info *curseg = CURSEG_I(sbi, CURSEG_HOT_DATA);
1751 struct f2fs_summary_block *sum = curseg->sum_blk;
1754 mutex_lock(&curseg->curseg_mutex);
1756 if (nats_in_cursum(sum) < NAT_JOURNAL_ENTRIES) {
1757 mutex_unlock(&curseg->curseg_mutex);
1761 for (i = 0; i < nats_in_cursum(sum); i++) {
1762 struct nat_entry *ne;
1763 struct f2fs_nat_entry raw_ne;
1764 nid_t nid = le32_to_cpu(nid_in_journal(sum, i));
1766 raw_ne = nat_in_journal(sum, i);
1768 write_lock(&nm_i->nat_tree_lock);
1769 ne = __lookup_nat_cache(nm_i, nid);
1771 __set_nat_cache_dirty(nm_i, ne);
1772 write_unlock(&nm_i->nat_tree_lock);
1775 ne = grab_nat_entry(nm_i, nid);
1777 write_unlock(&nm_i->nat_tree_lock);
1780 node_info_from_raw_nat(&ne->ni, &raw_ne);
1781 __set_nat_cache_dirty(nm_i, ne);
1782 write_unlock(&nm_i->nat_tree_lock);
1784 update_nats_in_cursum(sum, -i);
1785 mutex_unlock(&curseg->curseg_mutex);
1790 * This function is called during the checkpointing process.
1792 void flush_nat_entries(struct f2fs_sb_info *sbi)
1794 struct f2fs_nm_info *nm_i = NM_I(sbi);
1795 struct curseg_info *curseg = CURSEG_I(sbi, CURSEG_HOT_DATA);
1796 struct f2fs_summary_block *sum = curseg->sum_blk;
1797 struct nat_entry *ne, *cur;
1798 struct page *page = NULL;
1799 struct f2fs_nat_block *nat_blk = NULL;
1800 nid_t start_nid = 0, end_nid = 0;
1803 flushed = flush_nats_in_journal(sbi);
1806 mutex_lock(&curseg->curseg_mutex);
1808 /* 1) flush dirty nat caches */
1809 list_for_each_entry_safe(ne, cur, &nm_i->dirty_nat_entries, list) {
1811 struct f2fs_nat_entry raw_ne;
1814 if (nat_get_blkaddr(ne) == NEW_ADDR)
1817 nid = nat_get_nid(ne);
1822 /* if there is room for nat enries in curseg->sumpage */
1823 offset = lookup_journal_in_cursum(sum, NAT_JOURNAL, nid, 1);
1825 raw_ne = nat_in_journal(sum, offset);
1829 if (!page || (start_nid > nid || nid > end_nid)) {
1831 f2fs_put_page(page, 1);
1834 start_nid = START_NID(nid);
1835 end_nid = start_nid + NAT_ENTRY_PER_BLOCK - 1;
1838 * get nat block with dirty flag, increased reference
1839 * count, mapped and lock
1841 page = get_next_nat_page(sbi, start_nid);
1842 nat_blk = page_address(page);
1845 f2fs_bug_on(!nat_blk);
1846 raw_ne = nat_blk->entries[nid - start_nid];
1848 raw_nat_from_node_info(&raw_ne, &ne->ni);
1851 nat_blk->entries[nid - start_nid] = raw_ne;
1853 nat_in_journal(sum, offset) = raw_ne;
1854 nid_in_journal(sum, offset) = cpu_to_le32(nid);
1857 if (nat_get_blkaddr(ne) == NULL_ADDR &&
1858 add_free_nid(sbi, nid, false) <= 0) {
1859 write_lock(&nm_i->nat_tree_lock);
1860 __del_from_nat_cache(nm_i, ne);
1861 write_unlock(&nm_i->nat_tree_lock);
1863 write_lock(&nm_i->nat_tree_lock);
1864 __clear_nat_cache_dirty(nm_i, ne);
1865 write_unlock(&nm_i->nat_tree_lock);
1869 mutex_unlock(&curseg->curseg_mutex);
1870 f2fs_put_page(page, 1);
1873 static int init_node_manager(struct f2fs_sb_info *sbi)
1875 struct f2fs_super_block *sb_raw = F2FS_RAW_SUPER(sbi);
1876 struct f2fs_nm_info *nm_i = NM_I(sbi);
1877 unsigned char *version_bitmap;
1878 unsigned int nat_segs, nat_blocks;
1880 nm_i->nat_blkaddr = le32_to_cpu(sb_raw->nat_blkaddr);
1882 /* segment_count_nat includes pair segment so divide to 2. */
1883 nat_segs = le32_to_cpu(sb_raw->segment_count_nat) >> 1;
1884 nat_blocks = nat_segs << le32_to_cpu(sb_raw->log_blocks_per_seg);
1886 nm_i->max_nid = NAT_ENTRY_PER_BLOCK * nat_blocks;
1888 /* not used nids: 0, node, meta, (and root counted as valid node) */
1889 nm_i->available_nids = nm_i->max_nid - 3;
1892 nm_i->ram_thresh = DEF_RAM_THRESHOLD;
1894 INIT_RADIX_TREE(&nm_i->free_nid_root, GFP_ATOMIC);
1895 INIT_LIST_HEAD(&nm_i->free_nid_list);
1896 INIT_RADIX_TREE(&nm_i->nat_root, GFP_ATOMIC);
1897 INIT_LIST_HEAD(&nm_i->nat_entries);
1898 INIT_LIST_HEAD(&nm_i->dirty_nat_entries);
1900 mutex_init(&nm_i->build_lock);
1901 spin_lock_init(&nm_i->free_nid_list_lock);
1902 rwlock_init(&nm_i->nat_tree_lock);
1904 nm_i->next_scan_nid = le32_to_cpu(sbi->ckpt->next_free_nid);
1905 nm_i->bitmap_size = __bitmap_size(sbi, NAT_BITMAP);
1906 version_bitmap = __bitmap_ptr(sbi, NAT_BITMAP);
1907 if (!version_bitmap)
1910 nm_i->nat_bitmap = kmemdup(version_bitmap, nm_i->bitmap_size,
1912 if (!nm_i->nat_bitmap)
1917 int build_node_manager(struct f2fs_sb_info *sbi)
1921 sbi->nm_info = kzalloc(sizeof(struct f2fs_nm_info), GFP_KERNEL);
1925 err = init_node_manager(sbi);
1929 build_free_nids(sbi);
1933 void destroy_node_manager(struct f2fs_sb_info *sbi)
1935 struct f2fs_nm_info *nm_i = NM_I(sbi);
1936 struct free_nid *i, *next_i;
1937 struct nat_entry *natvec[NATVEC_SIZE];
1944 /* destroy free nid list */
1945 spin_lock(&nm_i->free_nid_list_lock);
1946 list_for_each_entry_safe(i, next_i, &nm_i->free_nid_list, list) {
1947 f2fs_bug_on(i->state == NID_ALLOC);
1948 __del_from_free_nid_list(nm_i, i);
1950 spin_unlock(&nm_i->free_nid_list_lock);
1951 kmem_cache_free(free_nid_slab, i);
1952 spin_lock(&nm_i->free_nid_list_lock);
1954 f2fs_bug_on(nm_i->fcnt);
1955 spin_unlock(&nm_i->free_nid_list_lock);
1957 /* destroy nat cache */
1958 write_lock(&nm_i->nat_tree_lock);
1959 while ((found = __gang_lookup_nat_cache(nm_i,
1960 nid, NATVEC_SIZE, natvec))) {
1962 nid = nat_get_nid(natvec[found - 1]) + 1;
1963 for (idx = 0; idx < found; idx++)
1964 __del_from_nat_cache(nm_i, natvec[idx]);
1966 f2fs_bug_on(nm_i->nat_cnt);
1967 write_unlock(&nm_i->nat_tree_lock);
1969 kfree(nm_i->nat_bitmap);
1970 sbi->nm_info = NULL;
1974 int __init create_node_manager_caches(void)
1976 nat_entry_slab = f2fs_kmem_cache_create("nat_entry",
1977 sizeof(struct nat_entry));
1978 if (!nat_entry_slab)
1981 free_nid_slab = f2fs_kmem_cache_create("free_nid",
1982 sizeof(struct free_nid));
1983 if (!free_nid_slab) {
1984 kmem_cache_destroy(nat_entry_slab);
1990 void destroy_node_manager_caches(void)
1992 kmem_cache_destroy(free_nid_slab);
1993 kmem_cache_destroy(nat_entry_slab);