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>
23 static struct kmem_cache *nat_entry_slab;
24 static struct kmem_cache *free_nid_slab;
26 static void clear_node_page_dirty(struct page *page)
28 struct address_space *mapping = page->mapping;
29 struct f2fs_sb_info *sbi = F2FS_SB(mapping->host->i_sb);
30 unsigned int long flags;
32 if (PageDirty(page)) {
33 spin_lock_irqsave(&mapping->tree_lock, flags);
34 radix_tree_tag_clear(&mapping->page_tree,
37 spin_unlock_irqrestore(&mapping->tree_lock, flags);
39 clear_page_dirty_for_io(page);
40 dec_page_count(sbi, F2FS_DIRTY_NODES);
42 ClearPageUptodate(page);
45 static struct page *get_current_nat_page(struct f2fs_sb_info *sbi, nid_t nid)
47 pgoff_t index = current_nat_addr(sbi, nid);
48 return get_meta_page(sbi, index);
51 static struct page *get_next_nat_page(struct f2fs_sb_info *sbi, nid_t nid)
53 struct page *src_page;
54 struct page *dst_page;
59 struct f2fs_nm_info *nm_i = NM_I(sbi);
61 src_off = current_nat_addr(sbi, nid);
62 dst_off = next_nat_addr(sbi, src_off);
64 /* get current nat block page with lock */
65 src_page = get_meta_page(sbi, src_off);
67 /* Dirty src_page means that it is already the new target NAT page. */
68 if (PageDirty(src_page))
71 dst_page = grab_meta_page(sbi, dst_off);
73 src_addr = page_address(src_page);
74 dst_addr = page_address(dst_page);
75 memcpy(dst_addr, src_addr, PAGE_CACHE_SIZE);
76 set_page_dirty(dst_page);
77 f2fs_put_page(src_page, 1);
79 set_to_next_nat(nm_i, nid);
87 static void ra_nat_pages(struct f2fs_sb_info *sbi, int nid)
89 struct address_space *mapping = sbi->meta_inode->i_mapping;
90 struct f2fs_nm_info *nm_i = NM_I(sbi);
95 for (i = 0; i < FREE_NID_PAGES; i++, nid += NAT_ENTRY_PER_BLOCK) {
96 if (nid >= nm_i->max_nid)
98 index = current_nat_addr(sbi, nid);
100 page = grab_cache_page(mapping, index);
103 if (f2fs_readpage(sbi, page, index, READ)) {
104 f2fs_put_page(page, 1);
107 page_cache_release(page);
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 static struct nat_entry *grab_nat_entry(struct f2fs_nm_info *nm_i, nid_t nid)
146 struct nat_entry *new;
148 new = kmem_cache_alloc(nat_entry_slab, GFP_ATOMIC);
151 if (radix_tree_insert(&nm_i->nat_root, nid, new)) {
152 kmem_cache_free(nat_entry_slab, new);
155 memset(new, 0, sizeof(struct nat_entry));
156 nat_set_nid(new, nid);
157 list_add_tail(&new->list, &nm_i->nat_entries);
162 static void cache_nat_entry(struct f2fs_nm_info *nm_i, nid_t nid,
163 struct f2fs_nat_entry *ne)
167 write_lock(&nm_i->nat_tree_lock);
168 e = __lookup_nat_cache(nm_i, nid);
170 e = grab_nat_entry(nm_i, nid);
172 write_unlock(&nm_i->nat_tree_lock);
175 nat_set_blkaddr(e, le32_to_cpu(ne->block_addr));
176 nat_set_ino(e, le32_to_cpu(ne->ino));
177 nat_set_version(e, ne->version);
178 e->checkpointed = true;
180 write_unlock(&nm_i->nat_tree_lock);
183 static void set_node_addr(struct f2fs_sb_info *sbi, struct node_info *ni,
186 struct f2fs_nm_info *nm_i = NM_I(sbi);
189 write_lock(&nm_i->nat_tree_lock);
190 e = __lookup_nat_cache(nm_i, ni->nid);
192 e = grab_nat_entry(nm_i, ni->nid);
194 write_unlock(&nm_i->nat_tree_lock);
198 e->checkpointed = true;
199 BUG_ON(ni->blk_addr == NEW_ADDR);
200 } else if (new_blkaddr == NEW_ADDR) {
202 * when nid is reallocated,
203 * previous nat entry can be remained in nat cache.
204 * So, reinitialize it with new information.
207 BUG_ON(ni->blk_addr != NULL_ADDR);
210 if (new_blkaddr == NEW_ADDR)
211 e->checkpointed = false;
214 BUG_ON(nat_get_blkaddr(e) != ni->blk_addr);
215 BUG_ON(nat_get_blkaddr(e) == NULL_ADDR &&
216 new_blkaddr == NULL_ADDR);
217 BUG_ON(nat_get_blkaddr(e) == NEW_ADDR &&
218 new_blkaddr == NEW_ADDR);
219 BUG_ON(nat_get_blkaddr(e) != NEW_ADDR &&
220 nat_get_blkaddr(e) != NULL_ADDR &&
221 new_blkaddr == NEW_ADDR);
223 /* increament version no as node is removed */
224 if (nat_get_blkaddr(e) != NEW_ADDR && new_blkaddr == NULL_ADDR) {
225 unsigned char version = nat_get_version(e);
226 nat_set_version(e, inc_node_version(version));
230 nat_set_blkaddr(e, new_blkaddr);
231 __set_nat_cache_dirty(nm_i, e);
232 write_unlock(&nm_i->nat_tree_lock);
235 static int try_to_free_nats(struct f2fs_sb_info *sbi, int nr_shrink)
237 struct f2fs_nm_info *nm_i = NM_I(sbi);
239 if (nm_i->nat_cnt < 2 * NM_WOUT_THRESHOLD)
242 write_lock(&nm_i->nat_tree_lock);
243 while (nr_shrink && !list_empty(&nm_i->nat_entries)) {
244 struct nat_entry *ne;
245 ne = list_first_entry(&nm_i->nat_entries,
246 struct nat_entry, list);
247 __del_from_nat_cache(nm_i, ne);
250 write_unlock(&nm_i->nat_tree_lock);
255 * This function returns always success
257 void get_node_info(struct f2fs_sb_info *sbi, nid_t nid, struct node_info *ni)
259 struct f2fs_nm_info *nm_i = NM_I(sbi);
260 struct curseg_info *curseg = CURSEG_I(sbi, CURSEG_HOT_DATA);
261 struct f2fs_summary_block *sum = curseg->sum_blk;
262 nid_t start_nid = START_NID(nid);
263 struct f2fs_nat_block *nat_blk;
264 struct page *page = NULL;
265 struct f2fs_nat_entry ne;
269 memset(&ne, 0, sizeof(struct f2fs_nat_entry));
272 /* Check nat cache */
273 read_lock(&nm_i->nat_tree_lock);
274 e = __lookup_nat_cache(nm_i, nid);
276 ni->ino = nat_get_ino(e);
277 ni->blk_addr = nat_get_blkaddr(e);
278 ni->version = nat_get_version(e);
280 read_unlock(&nm_i->nat_tree_lock);
284 /* Check current segment summary */
285 mutex_lock(&curseg->curseg_mutex);
286 i = lookup_journal_in_cursum(sum, NAT_JOURNAL, nid, 0);
288 ne = nat_in_journal(sum, i);
289 node_info_from_raw_nat(ni, &ne);
291 mutex_unlock(&curseg->curseg_mutex);
295 /* Fill node_info from nat page */
296 page = get_current_nat_page(sbi, start_nid);
297 nat_blk = (struct f2fs_nat_block *)page_address(page);
298 ne = nat_blk->entries[nid - start_nid];
299 node_info_from_raw_nat(ni, &ne);
300 f2fs_put_page(page, 1);
302 /* cache nat entry */
303 cache_nat_entry(NM_I(sbi), nid, &ne);
307 * The maximum depth is four.
308 * Offset[0] will have raw inode offset.
310 static int get_node_path(long block, int offset[4], unsigned int noffset[4])
312 const long direct_index = ADDRS_PER_INODE;
313 const long direct_blks = ADDRS_PER_BLOCK;
314 const long dptrs_per_blk = NIDS_PER_BLOCK;
315 const long indirect_blks = ADDRS_PER_BLOCK * NIDS_PER_BLOCK;
316 const long dindirect_blks = indirect_blks * NIDS_PER_BLOCK;
322 if (block < direct_index) {
327 block -= direct_index;
328 if (block < direct_blks) {
329 offset[n++] = NODE_DIR1_BLOCK;
335 block -= direct_blks;
336 if (block < direct_blks) {
337 offset[n++] = NODE_DIR2_BLOCK;
343 block -= direct_blks;
344 if (block < indirect_blks) {
345 offset[n++] = NODE_IND1_BLOCK;
347 offset[n++] = block / direct_blks;
348 noffset[n] = 4 + offset[n - 1];
349 offset[n++] = block % direct_blks;
353 block -= indirect_blks;
354 if (block < indirect_blks) {
355 offset[n++] = NODE_IND2_BLOCK;
356 noffset[n] = 4 + dptrs_per_blk;
357 offset[n++] = block / direct_blks;
358 noffset[n] = 5 + dptrs_per_blk + offset[n - 1];
359 offset[n++] = block % direct_blks;
363 block -= indirect_blks;
364 if (block < dindirect_blks) {
365 offset[n++] = NODE_DIND_BLOCK;
366 noffset[n] = 5 + (dptrs_per_blk * 2);
367 offset[n++] = block / indirect_blks;
368 noffset[n] = 6 + (dptrs_per_blk * 2) +
369 offset[n - 1] * (dptrs_per_blk + 1);
370 offset[n++] = (block / direct_blks) % dptrs_per_blk;
371 noffset[n] = 7 + (dptrs_per_blk * 2) +
372 offset[n - 2] * (dptrs_per_blk + 1) +
374 offset[n++] = block % direct_blks;
385 * Caller should call f2fs_put_dnode(dn).
387 int get_dnode_of_data(struct dnode_of_data *dn, pgoff_t index, int ro)
389 struct f2fs_sb_info *sbi = F2FS_SB(dn->inode->i_sb);
390 struct page *npage[4];
393 unsigned int noffset[4];
398 level = get_node_path(index, offset, noffset);
400 nids[0] = dn->inode->i_ino;
401 npage[0] = get_node_page(sbi, nids[0]);
402 if (IS_ERR(npage[0]))
403 return PTR_ERR(npage[0]);
406 nids[1] = get_nid(parent, offset[0], true);
407 dn->inode_page = npage[0];
408 dn->inode_page_locked = true;
410 /* get indirect or direct nodes */
411 for (i = 1; i <= level; i++) {
414 if (!nids[i] && !ro) {
415 mutex_lock_op(sbi, NODE_NEW);
418 if (!alloc_nid(sbi, &(nids[i]))) {
419 mutex_unlock_op(sbi, NODE_NEW);
425 npage[i] = new_node_page(dn, noffset[i]);
426 if (IS_ERR(npage[i])) {
427 alloc_nid_failed(sbi, nids[i]);
428 mutex_unlock_op(sbi, NODE_NEW);
429 err = PTR_ERR(npage[i]);
433 set_nid(parent, offset[i - 1], nids[i], i == 1);
434 alloc_nid_done(sbi, nids[i]);
435 mutex_unlock_op(sbi, NODE_NEW);
437 } else if (ro && i == level && level > 1) {
438 npage[i] = get_node_page_ra(parent, offset[i - 1]);
439 if (IS_ERR(npage[i])) {
440 err = PTR_ERR(npage[i]);
446 dn->inode_page_locked = false;
449 f2fs_put_page(parent, 1);
453 npage[i] = get_node_page(sbi, nids[i]);
454 if (IS_ERR(npage[i])) {
455 err = PTR_ERR(npage[i]);
456 f2fs_put_page(npage[0], 0);
462 nids[i + 1] = get_nid(parent, offset[i], false);
465 dn->nid = nids[level];
466 dn->ofs_in_node = offset[level];
467 dn->node_page = npage[level];
468 dn->data_blkaddr = datablock_addr(dn->node_page, dn->ofs_in_node);
472 f2fs_put_page(parent, 1);
474 f2fs_put_page(npage[0], 0);
476 dn->inode_page = NULL;
477 dn->node_page = NULL;
481 static void truncate_node(struct dnode_of_data *dn)
483 struct f2fs_sb_info *sbi = F2FS_SB(dn->inode->i_sb);
486 get_node_info(sbi, dn->nid, &ni);
487 if (dn->inode->i_blocks == 0) {
488 BUG_ON(ni.blk_addr != NULL_ADDR);
491 BUG_ON(ni.blk_addr == NULL_ADDR);
493 /* Deallocate node address */
494 invalidate_blocks(sbi, ni.blk_addr);
495 dec_valid_node_count(sbi, dn->inode, 1);
496 set_node_addr(sbi, &ni, NULL_ADDR);
498 if (dn->nid == dn->inode->i_ino) {
499 remove_orphan_inode(sbi, dn->nid);
500 dec_valid_inode_count(sbi);
505 clear_node_page_dirty(dn->node_page);
506 F2FS_SET_SB_DIRT(sbi);
508 f2fs_put_page(dn->node_page, 1);
509 dn->node_page = NULL;
512 static int truncate_dnode(struct dnode_of_data *dn)
514 struct f2fs_sb_info *sbi = F2FS_SB(dn->inode->i_sb);
520 /* get direct node */
521 page = get_node_page(sbi, dn->nid);
522 if (IS_ERR(page) && PTR_ERR(page) == -ENOENT)
524 else if (IS_ERR(page))
525 return PTR_ERR(page);
527 /* Make dnode_of_data for parameter */
528 dn->node_page = page;
530 truncate_data_blocks(dn);
535 static int truncate_nodes(struct dnode_of_data *dn, unsigned int nofs,
538 struct f2fs_sb_info *sbi = F2FS_SB(dn->inode->i_sb);
539 struct dnode_of_data rdn = *dn;
541 struct f2fs_node *rn;
543 unsigned int child_nofs;
548 return NIDS_PER_BLOCK + 1;
550 page = get_node_page(sbi, dn->nid);
552 return PTR_ERR(page);
554 rn = (struct f2fs_node *)page_address(page);
556 for (i = ofs; i < NIDS_PER_BLOCK; i++, freed++) {
557 child_nid = le32_to_cpu(rn->in.nid[i]);
561 ret = truncate_dnode(&rdn);
564 set_nid(page, i, 0, false);
567 child_nofs = nofs + ofs * (NIDS_PER_BLOCK + 1) + 1;
568 for (i = ofs; i < NIDS_PER_BLOCK; i++) {
569 child_nid = le32_to_cpu(rn->in.nid[i]);
570 if (child_nid == 0) {
571 child_nofs += NIDS_PER_BLOCK + 1;
575 ret = truncate_nodes(&rdn, child_nofs, 0, depth - 1);
576 if (ret == (NIDS_PER_BLOCK + 1)) {
577 set_nid(page, i, 0, false);
579 } else if (ret < 0 && ret != -ENOENT) {
587 /* remove current indirect node */
588 dn->node_page = page;
592 f2fs_put_page(page, 1);
597 f2fs_put_page(page, 1);
601 static int truncate_partial_nodes(struct dnode_of_data *dn,
602 struct f2fs_inode *ri, int *offset, int depth)
604 struct f2fs_sb_info *sbi = F2FS_SB(dn->inode->i_sb);
605 struct page *pages[2];
612 nid[0] = le32_to_cpu(ri->i_nid[offset[0] - NODE_DIR1_BLOCK]);
616 /* get indirect nodes in the path */
617 for (i = 0; i < depth - 1; i++) {
618 /* refernece count'll be increased */
619 pages[i] = get_node_page(sbi, nid[i]);
620 if (IS_ERR(pages[i])) {
622 err = PTR_ERR(pages[i]);
625 nid[i + 1] = get_nid(pages[i], offset[i + 1], false);
628 /* free direct nodes linked to a partial indirect node */
629 for (i = offset[depth - 1]; i < NIDS_PER_BLOCK; i++) {
630 child_nid = get_nid(pages[idx], i, false);
634 err = truncate_dnode(dn);
637 set_nid(pages[idx], i, 0, false);
640 if (offset[depth - 1] == 0) {
641 dn->node_page = pages[idx];
645 f2fs_put_page(pages[idx], 1);
648 offset[depth - 1] = 0;
650 for (i = depth - 3; i >= 0; i--)
651 f2fs_put_page(pages[i], 1);
656 * All the block addresses of data and nodes should be nullified.
658 int truncate_inode_blocks(struct inode *inode, pgoff_t from)
660 struct f2fs_sb_info *sbi = F2FS_SB(inode->i_sb);
661 int err = 0, cont = 1;
662 int level, offset[4], noffset[4];
664 struct f2fs_node *rn;
665 struct dnode_of_data dn;
668 level = get_node_path(from, offset, noffset);
670 page = get_node_page(sbi, inode->i_ino);
672 return PTR_ERR(page);
674 set_new_dnode(&dn, inode, page, NULL, 0);
677 rn = page_address(page);
685 if (!offset[level - 1])
687 err = truncate_partial_nodes(&dn, &rn->i, offset, level);
688 if (err < 0 && err != -ENOENT)
690 nofs += 1 + NIDS_PER_BLOCK;
693 nofs = 5 + 2 * NIDS_PER_BLOCK;
694 if (!offset[level - 1])
696 err = truncate_partial_nodes(&dn, &rn->i, offset, level);
697 if (err < 0 && err != -ENOENT)
706 dn.nid = le32_to_cpu(rn->i.i_nid[offset[0] - NODE_DIR1_BLOCK]);
708 case NODE_DIR1_BLOCK:
709 case NODE_DIR2_BLOCK:
710 err = truncate_dnode(&dn);
713 case NODE_IND1_BLOCK:
714 case NODE_IND2_BLOCK:
715 err = truncate_nodes(&dn, nofs, offset[1], 2);
718 case NODE_DIND_BLOCK:
719 err = truncate_nodes(&dn, nofs, offset[1], 3);
726 if (err < 0 && err != -ENOENT)
728 if (offset[1] == 0 &&
729 rn->i.i_nid[offset[0] - NODE_DIR1_BLOCK]) {
731 wait_on_page_writeback(page);
732 rn->i.i_nid[offset[0] - NODE_DIR1_BLOCK] = 0;
733 set_page_dirty(page);
741 f2fs_put_page(page, 0);
742 return err > 0 ? 0 : err;
745 int remove_inode_page(struct inode *inode)
747 struct f2fs_sb_info *sbi = F2FS_SB(inode->i_sb);
749 nid_t ino = inode->i_ino;
750 struct dnode_of_data dn;
752 mutex_lock_op(sbi, NODE_TRUNC);
753 page = get_node_page(sbi, ino);
755 mutex_unlock_op(sbi, NODE_TRUNC);
756 return PTR_ERR(page);
759 if (F2FS_I(inode)->i_xattr_nid) {
760 nid_t nid = F2FS_I(inode)->i_xattr_nid;
761 struct page *npage = get_node_page(sbi, nid);
764 mutex_unlock_op(sbi, NODE_TRUNC);
765 return PTR_ERR(npage);
768 F2FS_I(inode)->i_xattr_nid = 0;
769 set_new_dnode(&dn, inode, page, npage, nid);
770 dn.inode_page_locked = 1;
774 /* 0 is possible, after f2fs_new_inode() is failed */
775 BUG_ON(inode->i_blocks != 0 && inode->i_blocks != 1);
776 set_new_dnode(&dn, inode, page, page, ino);
779 mutex_unlock_op(sbi, NODE_TRUNC);
783 int new_inode_page(struct inode *inode, struct dentry *dentry)
785 struct f2fs_sb_info *sbi = F2FS_SB(inode->i_sb);
787 struct dnode_of_data dn;
789 /* allocate inode page for new inode */
790 set_new_dnode(&dn, inode, NULL, NULL, inode->i_ino);
791 mutex_lock_op(sbi, NODE_NEW);
792 page = new_node_page(&dn, 0);
793 init_dent_inode(dentry, page);
794 mutex_unlock_op(sbi, NODE_NEW);
796 return PTR_ERR(page);
797 f2fs_put_page(page, 1);
801 struct page *new_node_page(struct dnode_of_data *dn, unsigned int ofs)
803 struct f2fs_sb_info *sbi = F2FS_SB(dn->inode->i_sb);
804 struct address_space *mapping = sbi->node_inode->i_mapping;
805 struct node_info old_ni, new_ni;
809 if (is_inode_flag_set(F2FS_I(dn->inode), FI_NO_ALLOC))
810 return ERR_PTR(-EPERM);
812 page = grab_cache_page(mapping, dn->nid);
814 return ERR_PTR(-ENOMEM);
816 get_node_info(sbi, dn->nid, &old_ni);
818 SetPageUptodate(page);
819 fill_node_footer(page, dn->nid, dn->inode->i_ino, ofs, true);
821 /* Reinitialize old_ni with new node page */
822 BUG_ON(old_ni.blk_addr != NULL_ADDR);
824 new_ni.ino = dn->inode->i_ino;
826 if (!inc_valid_node_count(sbi, dn->inode, 1)) {
830 set_node_addr(sbi, &new_ni, NEW_ADDR);
831 set_cold_node(dn->inode, page);
833 dn->node_page = page;
835 set_page_dirty(page);
837 inc_valid_inode_count(sbi);
842 clear_node_page_dirty(page);
843 f2fs_put_page(page, 1);
847 static int read_node_page(struct page *page, int type)
849 struct f2fs_sb_info *sbi = F2FS_SB(page->mapping->host->i_sb);
852 get_node_info(sbi, page->index, &ni);
854 if (ni.blk_addr == NULL_ADDR)
856 return f2fs_readpage(sbi, page, ni.blk_addr, type);
860 * Readahead a node page
862 void ra_node_page(struct f2fs_sb_info *sbi, nid_t nid)
864 struct address_space *mapping = sbi->node_inode->i_mapping;
867 apage = find_get_page(mapping, nid);
868 if (apage && PageUptodate(apage))
870 f2fs_put_page(apage, 0);
872 apage = grab_cache_page(mapping, nid);
876 if (read_node_page(apage, READA))
879 page_cache_release(apage);
885 page_cache_release(apage);
888 struct page *get_node_page(struct f2fs_sb_info *sbi, pgoff_t nid)
892 struct address_space *mapping = sbi->node_inode->i_mapping;
894 page = grab_cache_page(mapping, nid);
896 return ERR_PTR(-ENOMEM);
898 err = read_node_page(page, READ_SYNC);
900 f2fs_put_page(page, 1);
904 BUG_ON(nid != nid_of_node(page));
905 mark_page_accessed(page);
910 * Return a locked page for the desired node page.
911 * And, readahead MAX_RA_NODE number of node pages.
913 struct page *get_node_page_ra(struct page *parent, int start)
915 struct f2fs_sb_info *sbi = F2FS_SB(parent->mapping->host->i_sb);
916 struct address_space *mapping = sbi->node_inode->i_mapping;
922 /* First, try getting the desired direct node. */
923 nid = get_nid(parent, start, false);
925 return ERR_PTR(-ENOENT);
927 page = find_get_page(mapping, nid);
928 if (page && PageUptodate(page))
930 f2fs_put_page(page, 0);
933 page = grab_cache_page(mapping, nid);
935 return ERR_PTR(-ENOMEM);
937 err = read_node_page(page, READA);
939 f2fs_put_page(page, 1);
943 /* Then, try readahead for siblings of the desired node */
944 end = start + MAX_RA_NODE;
945 end = min(end, NIDS_PER_BLOCK);
946 for (i = start + 1; i < end; i++) {
947 nid = get_nid(parent, i, false);
950 ra_node_page(sbi, nid);
955 if (PageError(page)) {
956 f2fs_put_page(page, 1);
957 return ERR_PTR(-EIO);
960 /* Has the page been truncated? */
961 if (page->mapping != mapping) {
962 f2fs_put_page(page, 1);
968 void sync_inode_page(struct dnode_of_data *dn)
970 if (IS_INODE(dn->node_page) || dn->inode_page == dn->node_page) {
971 update_inode(dn->inode, dn->node_page);
972 } else if (dn->inode_page) {
973 if (!dn->inode_page_locked)
974 lock_page(dn->inode_page);
975 update_inode(dn->inode, dn->inode_page);
976 if (!dn->inode_page_locked)
977 unlock_page(dn->inode_page);
979 f2fs_write_inode(dn->inode, NULL);
983 int sync_node_pages(struct f2fs_sb_info *sbi, nid_t ino,
984 struct writeback_control *wbc)
986 struct address_space *mapping = sbi->node_inode->i_mapping;
989 int step = ino ? 2 : 0;
990 int nwritten = 0, wrote = 0;
992 pagevec_init(&pvec, 0);
998 while (index <= end) {
1000 nr_pages = pagevec_lookup_tag(&pvec, mapping, &index,
1001 PAGECACHE_TAG_DIRTY,
1002 min(end - index, (pgoff_t)PAGEVEC_SIZE-1) + 1);
1006 for (i = 0; i < nr_pages; i++) {
1007 struct page *page = pvec.pages[i];
1010 * flushing sequence with step:
1015 if (step == 0 && IS_DNODE(page))
1017 if (step == 1 && (!IS_DNODE(page) ||
1018 is_cold_node(page)))
1020 if (step == 2 && (!IS_DNODE(page) ||
1021 !is_cold_node(page)))
1026 * we should not skip writing node pages.
1028 if (ino && ino_of_node(page) == ino)
1030 else if (!trylock_page(page))
1033 if (unlikely(page->mapping != mapping)) {
1038 if (ino && ino_of_node(page) != ino)
1039 goto continue_unlock;
1041 if (!PageDirty(page)) {
1042 /* someone wrote it for us */
1043 goto continue_unlock;
1046 if (!clear_page_dirty_for_io(page))
1047 goto continue_unlock;
1049 /* called by fsync() */
1050 if (ino && IS_DNODE(page)) {
1051 int mark = !is_checkpointed_node(sbi, ino);
1052 set_fsync_mark(page, 1);
1054 set_dentry_mark(page, mark);
1057 set_fsync_mark(page, 0);
1058 set_dentry_mark(page, 0);
1060 mapping->a_ops->writepage(page, wbc);
1063 if (--wbc->nr_to_write == 0)
1066 pagevec_release(&pvec);
1069 if (wbc->nr_to_write == 0) {
1081 f2fs_submit_bio(sbi, NODE, wbc->sync_mode == WB_SYNC_ALL);
1086 static int f2fs_write_node_page(struct page *page,
1087 struct writeback_control *wbc)
1089 struct f2fs_sb_info *sbi = F2FS_SB(page->mapping->host->i_sb);
1092 struct node_info ni;
1094 if (wbc->for_reclaim) {
1095 dec_page_count(sbi, F2FS_DIRTY_NODES);
1096 wbc->pages_skipped++;
1097 set_page_dirty(page);
1098 return AOP_WRITEPAGE_ACTIVATE;
1101 wait_on_page_writeback(page);
1103 mutex_lock_op(sbi, NODE_WRITE);
1105 /* get old block addr of this node page */
1106 nid = nid_of_node(page);
1107 BUG_ON(page->index != nid);
1109 get_node_info(sbi, nid, &ni);
1111 /* This page is already truncated */
1112 if (ni.blk_addr == NULL_ADDR)
1115 set_page_writeback(page);
1117 /* insert node offset */
1118 write_node_page(sbi, page, nid, ni.blk_addr, &new_addr);
1119 set_node_addr(sbi, &ni, new_addr);
1120 dec_page_count(sbi, F2FS_DIRTY_NODES);
1122 mutex_unlock_op(sbi, NODE_WRITE);
1127 static int f2fs_write_node_pages(struct address_space *mapping,
1128 struct writeback_control *wbc)
1130 struct f2fs_sb_info *sbi = F2FS_SB(mapping->host->i_sb);
1131 struct block_device *bdev = sbi->sb->s_bdev;
1132 long nr_to_write = wbc->nr_to_write;
1134 if (wbc->for_kupdate)
1137 if (get_pages(sbi, F2FS_DIRTY_NODES) == 0)
1140 if (try_to_free_nats(sbi, NAT_ENTRY_PER_BLOCK)) {
1141 write_checkpoint(sbi, false, false);
1145 /* if mounting is failed, skip writing node pages */
1146 wbc->nr_to_write = bio_get_nr_vecs(bdev);
1147 sync_node_pages(sbi, 0, wbc);
1148 wbc->nr_to_write = nr_to_write -
1149 (bio_get_nr_vecs(bdev) - wbc->nr_to_write);
1153 static int f2fs_set_node_page_dirty(struct page *page)
1155 struct address_space *mapping = page->mapping;
1156 struct f2fs_sb_info *sbi = F2FS_SB(mapping->host->i_sb);
1158 SetPageUptodate(page);
1159 if (!PageDirty(page)) {
1160 __set_page_dirty_nobuffers(page);
1161 inc_page_count(sbi, F2FS_DIRTY_NODES);
1162 SetPagePrivate(page);
1168 static void f2fs_invalidate_node_page(struct page *page, unsigned long offset)
1170 struct inode *inode = page->mapping->host;
1171 struct f2fs_sb_info *sbi = F2FS_SB(inode->i_sb);
1172 if (PageDirty(page))
1173 dec_page_count(sbi, F2FS_DIRTY_NODES);
1174 ClearPagePrivate(page);
1177 static int f2fs_release_node_page(struct page *page, gfp_t wait)
1179 ClearPagePrivate(page);
1184 * Structure of the f2fs node operations
1186 const struct address_space_operations f2fs_node_aops = {
1187 .writepage = f2fs_write_node_page,
1188 .writepages = f2fs_write_node_pages,
1189 .set_page_dirty = f2fs_set_node_page_dirty,
1190 .invalidatepage = f2fs_invalidate_node_page,
1191 .releasepage = f2fs_release_node_page,
1194 static struct free_nid *__lookup_free_nid_list(nid_t n, struct list_head *head)
1196 struct list_head *this;
1197 struct free_nid *i = NULL;
1198 list_for_each(this, head) {
1199 i = list_entry(this, struct free_nid, list);
1207 static void __del_from_free_nid_list(struct free_nid *i)
1210 kmem_cache_free(free_nid_slab, i);
1213 static int add_free_nid(struct f2fs_nm_info *nm_i, nid_t nid)
1217 if (nm_i->fcnt > 2 * MAX_FREE_NIDS)
1220 i = kmem_cache_alloc(free_nid_slab, GFP_NOFS);
1228 spin_lock(&nm_i->free_nid_list_lock);
1229 if (__lookup_free_nid_list(nid, &nm_i->free_nid_list)) {
1230 spin_unlock(&nm_i->free_nid_list_lock);
1231 kmem_cache_free(free_nid_slab, i);
1234 list_add_tail(&i->list, &nm_i->free_nid_list);
1236 spin_unlock(&nm_i->free_nid_list_lock);
1240 static void remove_free_nid(struct f2fs_nm_info *nm_i, nid_t nid)
1243 spin_lock(&nm_i->free_nid_list_lock);
1244 i = __lookup_free_nid_list(nid, &nm_i->free_nid_list);
1245 if (i && i->state == NID_NEW) {
1246 __del_from_free_nid_list(i);
1249 spin_unlock(&nm_i->free_nid_list_lock);
1252 static int scan_nat_page(struct f2fs_nm_info *nm_i,
1253 struct page *nat_page, nid_t start_nid)
1255 struct f2fs_nat_block *nat_blk = page_address(nat_page);
1260 /* 0 nid should not be used */
1264 i = start_nid % NAT_ENTRY_PER_BLOCK;
1266 for (; i < NAT_ENTRY_PER_BLOCK; i++, start_nid++) {
1267 blk_addr = le32_to_cpu(nat_blk->entries[i].block_addr);
1268 BUG_ON(blk_addr == NEW_ADDR);
1269 if (blk_addr == NULL_ADDR)
1270 fcnt += add_free_nid(nm_i, start_nid);
1275 static void build_free_nids(struct f2fs_sb_info *sbi)
1277 struct free_nid *fnid, *next_fnid;
1278 struct f2fs_nm_info *nm_i = NM_I(sbi);
1279 struct curseg_info *curseg = CURSEG_I(sbi, CURSEG_HOT_DATA);
1280 struct f2fs_summary_block *sum = curseg->sum_blk;
1282 bool is_cycled = false;
1286 nid = nm_i->next_scan_nid;
1287 nm_i->init_scan_nid = nid;
1289 ra_nat_pages(sbi, nid);
1292 struct page *page = get_current_nat_page(sbi, nid);
1294 fcnt += scan_nat_page(nm_i, page, nid);
1295 f2fs_put_page(page, 1);
1297 nid += (NAT_ENTRY_PER_BLOCK - (nid % NAT_ENTRY_PER_BLOCK));
1299 if (nid >= nm_i->max_nid) {
1303 if (fcnt > MAX_FREE_NIDS)
1305 if (is_cycled && nm_i->init_scan_nid <= nid)
1309 nm_i->next_scan_nid = nid;
1311 /* find free nids from current sum_pages */
1312 mutex_lock(&curseg->curseg_mutex);
1313 for (i = 0; i < nats_in_cursum(sum); i++) {
1314 block_t addr = le32_to_cpu(nat_in_journal(sum, i).block_addr);
1315 nid = le32_to_cpu(nid_in_journal(sum, i));
1316 if (addr == NULL_ADDR)
1317 add_free_nid(nm_i, nid);
1319 remove_free_nid(nm_i, nid);
1321 mutex_unlock(&curseg->curseg_mutex);
1323 /* remove the free nids from current allocated nids */
1324 list_for_each_entry_safe(fnid, next_fnid, &nm_i->free_nid_list, list) {
1325 struct nat_entry *ne;
1327 read_lock(&nm_i->nat_tree_lock);
1328 ne = __lookup_nat_cache(nm_i, fnid->nid);
1329 if (ne && nat_get_blkaddr(ne) != NULL_ADDR)
1330 remove_free_nid(nm_i, fnid->nid);
1331 read_unlock(&nm_i->nat_tree_lock);
1336 * If this function returns success, caller can obtain a new nid
1337 * from second parameter of this function.
1338 * The returned nid could be used ino as well as nid when inode is created.
1340 bool alloc_nid(struct f2fs_sb_info *sbi, nid_t *nid)
1342 struct f2fs_nm_info *nm_i = NM_I(sbi);
1343 struct free_nid *i = NULL;
1344 struct list_head *this;
1346 mutex_lock(&nm_i->build_lock);
1348 /* scan NAT in order to build free nid list */
1349 build_free_nids(sbi);
1351 mutex_unlock(&nm_i->build_lock);
1355 mutex_unlock(&nm_i->build_lock);
1358 * We check fcnt again since previous check is racy as
1359 * we didn't hold free_nid_list_lock. So other thread
1360 * could consume all of free nids.
1362 spin_lock(&nm_i->free_nid_list_lock);
1364 spin_unlock(&nm_i->free_nid_list_lock);
1368 BUG_ON(list_empty(&nm_i->free_nid_list));
1369 list_for_each(this, &nm_i->free_nid_list) {
1370 i = list_entry(this, struct free_nid, list);
1371 if (i->state == NID_NEW)
1375 BUG_ON(i->state != NID_NEW);
1377 i->state = NID_ALLOC;
1379 spin_unlock(&nm_i->free_nid_list_lock);
1384 * alloc_nid() should be called prior to this function.
1386 void alloc_nid_done(struct f2fs_sb_info *sbi, nid_t nid)
1388 struct f2fs_nm_info *nm_i = NM_I(sbi);
1391 spin_lock(&nm_i->free_nid_list_lock);
1392 i = __lookup_free_nid_list(nid, &nm_i->free_nid_list);
1394 BUG_ON(i->state != NID_ALLOC);
1395 __del_from_free_nid_list(i);
1397 spin_unlock(&nm_i->free_nid_list_lock);
1401 * alloc_nid() should be called prior to this function.
1403 void alloc_nid_failed(struct f2fs_sb_info *sbi, nid_t nid)
1405 alloc_nid_done(sbi, nid);
1406 add_free_nid(NM_I(sbi), nid);
1409 void recover_node_page(struct f2fs_sb_info *sbi, struct page *page,
1410 struct f2fs_summary *sum, struct node_info *ni,
1411 block_t new_blkaddr)
1413 rewrite_node_page(sbi, page, sum, ni->blk_addr, new_blkaddr);
1414 set_node_addr(sbi, ni, new_blkaddr);
1415 clear_node_page_dirty(page);
1418 int recover_inode_page(struct f2fs_sb_info *sbi, struct page *page)
1420 struct address_space *mapping = sbi->node_inode->i_mapping;
1421 struct f2fs_node *src, *dst;
1422 nid_t ino = ino_of_node(page);
1423 struct node_info old_ni, new_ni;
1426 ipage = grab_cache_page(mapping, ino);
1430 /* Should not use this inode from free nid list */
1431 remove_free_nid(NM_I(sbi), ino);
1433 get_node_info(sbi, ino, &old_ni);
1434 SetPageUptodate(ipage);
1435 fill_node_footer(ipage, ino, ino, 0, true);
1437 src = (struct f2fs_node *)page_address(page);
1438 dst = (struct f2fs_node *)page_address(ipage);
1440 memcpy(dst, src, (unsigned long)&src->i.i_ext - (unsigned long)&src->i);
1442 dst->i.i_blocks = cpu_to_le64(1);
1443 dst->i.i_links = cpu_to_le32(1);
1444 dst->i.i_xattr_nid = 0;
1449 set_node_addr(sbi, &new_ni, NEW_ADDR);
1450 inc_valid_inode_count(sbi);
1452 f2fs_put_page(ipage, 1);
1456 int restore_node_summary(struct f2fs_sb_info *sbi,
1457 unsigned int segno, struct f2fs_summary_block *sum)
1459 struct f2fs_node *rn;
1460 struct f2fs_summary *sum_entry;
1465 /* alloc temporal page for read node */
1466 page = alloc_page(GFP_NOFS | __GFP_ZERO);
1468 return PTR_ERR(page);
1471 /* scan the node segment */
1472 last_offset = sbi->blocks_per_seg;
1473 addr = START_BLOCK(sbi, segno);
1474 sum_entry = &sum->entries[0];
1476 for (i = 0; i < last_offset; i++, sum_entry++) {
1477 if (f2fs_readpage(sbi, page, addr, READ_SYNC))
1480 rn = (struct f2fs_node *)page_address(page);
1481 sum_entry->nid = rn->footer.nid;
1482 sum_entry->version = 0;
1483 sum_entry->ofs_in_node = 0;
1487 * In order to read next node page,
1488 * we must clear PageUptodate flag.
1490 ClearPageUptodate(page);
1494 __free_pages(page, 0);
1498 static bool flush_nats_in_journal(struct f2fs_sb_info *sbi)
1500 struct f2fs_nm_info *nm_i = NM_I(sbi);
1501 struct curseg_info *curseg = CURSEG_I(sbi, CURSEG_HOT_DATA);
1502 struct f2fs_summary_block *sum = curseg->sum_blk;
1505 mutex_lock(&curseg->curseg_mutex);
1507 if (nats_in_cursum(sum) < NAT_JOURNAL_ENTRIES) {
1508 mutex_unlock(&curseg->curseg_mutex);
1512 for (i = 0; i < nats_in_cursum(sum); i++) {
1513 struct nat_entry *ne;
1514 struct f2fs_nat_entry raw_ne;
1515 nid_t nid = le32_to_cpu(nid_in_journal(sum, i));
1517 raw_ne = nat_in_journal(sum, i);
1519 write_lock(&nm_i->nat_tree_lock);
1520 ne = __lookup_nat_cache(nm_i, nid);
1522 __set_nat_cache_dirty(nm_i, ne);
1523 write_unlock(&nm_i->nat_tree_lock);
1526 ne = grab_nat_entry(nm_i, nid);
1528 write_unlock(&nm_i->nat_tree_lock);
1531 nat_set_blkaddr(ne, le32_to_cpu(raw_ne.block_addr));
1532 nat_set_ino(ne, le32_to_cpu(raw_ne.ino));
1533 nat_set_version(ne, raw_ne.version);
1534 __set_nat_cache_dirty(nm_i, ne);
1535 write_unlock(&nm_i->nat_tree_lock);
1537 update_nats_in_cursum(sum, -i);
1538 mutex_unlock(&curseg->curseg_mutex);
1543 * This function is called during the checkpointing process.
1545 void flush_nat_entries(struct f2fs_sb_info *sbi)
1547 struct f2fs_nm_info *nm_i = NM_I(sbi);
1548 struct curseg_info *curseg = CURSEG_I(sbi, CURSEG_HOT_DATA);
1549 struct f2fs_summary_block *sum = curseg->sum_blk;
1550 struct list_head *cur, *n;
1551 struct page *page = NULL;
1552 struct f2fs_nat_block *nat_blk = NULL;
1553 nid_t start_nid = 0, end_nid = 0;
1556 flushed = flush_nats_in_journal(sbi);
1559 mutex_lock(&curseg->curseg_mutex);
1561 /* 1) flush dirty nat caches */
1562 list_for_each_safe(cur, n, &nm_i->dirty_nat_entries) {
1563 struct nat_entry *ne;
1565 struct f2fs_nat_entry raw_ne;
1567 block_t new_blkaddr;
1569 ne = list_entry(cur, struct nat_entry, list);
1570 nid = nat_get_nid(ne);
1572 if (nat_get_blkaddr(ne) == NEW_ADDR)
1577 /* if there is room for nat enries in curseg->sumpage */
1578 offset = lookup_journal_in_cursum(sum, NAT_JOURNAL, nid, 1);
1580 raw_ne = nat_in_journal(sum, offset);
1584 if (!page || (start_nid > nid || nid > end_nid)) {
1586 f2fs_put_page(page, 1);
1589 start_nid = START_NID(nid);
1590 end_nid = start_nid + NAT_ENTRY_PER_BLOCK - 1;
1593 * get nat block with dirty flag, increased reference
1594 * count, mapped and lock
1596 page = get_next_nat_page(sbi, start_nid);
1597 nat_blk = page_address(page);
1601 raw_ne = nat_blk->entries[nid - start_nid];
1603 new_blkaddr = nat_get_blkaddr(ne);
1605 raw_ne.ino = cpu_to_le32(nat_get_ino(ne));
1606 raw_ne.block_addr = cpu_to_le32(new_blkaddr);
1607 raw_ne.version = nat_get_version(ne);
1610 nat_blk->entries[nid - start_nid] = raw_ne;
1612 nat_in_journal(sum, offset) = raw_ne;
1613 nid_in_journal(sum, offset) = cpu_to_le32(nid);
1616 if (nat_get_blkaddr(ne) == NULL_ADDR) {
1617 write_lock(&nm_i->nat_tree_lock);
1618 __del_from_nat_cache(nm_i, ne);
1619 write_unlock(&nm_i->nat_tree_lock);
1621 /* We can reuse this freed nid at this point */
1622 add_free_nid(NM_I(sbi), nid);
1624 write_lock(&nm_i->nat_tree_lock);
1625 __clear_nat_cache_dirty(nm_i, ne);
1626 ne->checkpointed = true;
1627 write_unlock(&nm_i->nat_tree_lock);
1631 mutex_unlock(&curseg->curseg_mutex);
1632 f2fs_put_page(page, 1);
1634 /* 2) shrink nat caches if necessary */
1635 try_to_free_nats(sbi, nm_i->nat_cnt - NM_WOUT_THRESHOLD);
1638 static int init_node_manager(struct f2fs_sb_info *sbi)
1640 struct f2fs_super_block *sb_raw = F2FS_RAW_SUPER(sbi);
1641 struct f2fs_nm_info *nm_i = NM_I(sbi);
1642 unsigned char *version_bitmap;
1643 unsigned int nat_segs, nat_blocks;
1645 nm_i->nat_blkaddr = le32_to_cpu(sb_raw->nat_blkaddr);
1647 /* segment_count_nat includes pair segment so divide to 2. */
1648 nat_segs = le32_to_cpu(sb_raw->segment_count_nat) >> 1;
1649 nat_blocks = nat_segs << le32_to_cpu(sb_raw->log_blocks_per_seg);
1650 nm_i->max_nid = NAT_ENTRY_PER_BLOCK * nat_blocks;
1654 INIT_LIST_HEAD(&nm_i->free_nid_list);
1655 INIT_RADIX_TREE(&nm_i->nat_root, GFP_ATOMIC);
1656 INIT_LIST_HEAD(&nm_i->nat_entries);
1657 INIT_LIST_HEAD(&nm_i->dirty_nat_entries);
1659 mutex_init(&nm_i->build_lock);
1660 spin_lock_init(&nm_i->free_nid_list_lock);
1661 rwlock_init(&nm_i->nat_tree_lock);
1663 nm_i->bitmap_size = __bitmap_size(sbi, NAT_BITMAP);
1664 nm_i->init_scan_nid = le32_to_cpu(sbi->ckpt->next_free_nid);
1665 nm_i->next_scan_nid = le32_to_cpu(sbi->ckpt->next_free_nid);
1667 nm_i->nat_bitmap = kzalloc(nm_i->bitmap_size, GFP_KERNEL);
1668 if (!nm_i->nat_bitmap)
1670 version_bitmap = __bitmap_ptr(sbi, NAT_BITMAP);
1671 if (!version_bitmap)
1674 /* copy version bitmap */
1675 memcpy(nm_i->nat_bitmap, version_bitmap, nm_i->bitmap_size);
1679 int build_node_manager(struct f2fs_sb_info *sbi)
1683 sbi->nm_info = kzalloc(sizeof(struct f2fs_nm_info), GFP_KERNEL);
1687 err = init_node_manager(sbi);
1691 build_free_nids(sbi);
1695 void destroy_node_manager(struct f2fs_sb_info *sbi)
1697 struct f2fs_nm_info *nm_i = NM_I(sbi);
1698 struct free_nid *i, *next_i;
1699 struct nat_entry *natvec[NATVEC_SIZE];
1706 /* destroy free nid list */
1707 spin_lock(&nm_i->free_nid_list_lock);
1708 list_for_each_entry_safe(i, next_i, &nm_i->free_nid_list, list) {
1709 BUG_ON(i->state == NID_ALLOC);
1710 __del_from_free_nid_list(i);
1714 spin_unlock(&nm_i->free_nid_list_lock);
1716 /* destroy nat cache */
1717 write_lock(&nm_i->nat_tree_lock);
1718 while ((found = __gang_lookup_nat_cache(nm_i,
1719 nid, NATVEC_SIZE, natvec))) {
1721 for (idx = 0; idx < found; idx++) {
1722 struct nat_entry *e = natvec[idx];
1723 nid = nat_get_nid(e) + 1;
1724 __del_from_nat_cache(nm_i, e);
1727 BUG_ON(nm_i->nat_cnt);
1728 write_unlock(&nm_i->nat_tree_lock);
1730 kfree(nm_i->nat_bitmap);
1731 sbi->nm_info = NULL;
1735 int create_node_manager_caches(void)
1737 nat_entry_slab = f2fs_kmem_cache_create("nat_entry",
1738 sizeof(struct nat_entry), NULL);
1739 if (!nat_entry_slab)
1742 free_nid_slab = f2fs_kmem_cache_create("free_nid",
1743 sizeof(struct free_nid), NULL);
1744 if (!free_nid_slab) {
1745 kmem_cache_destroy(nat_entry_slab);
1751 void destroy_node_manager_caches(void)
1753 kmem_cache_destroy(free_nid_slab);
1754 kmem_cache_destroy(nat_entry_slab);