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 mem_size
= get_pages(sbi
, F2FS_DIRTY_DENTS
);
46 res
= mem_size
< ((val
.totalram
* nm_i
->ram_thresh
/ 100) >> 1);
51 static void clear_node_page_dirty(struct page
*page
)
53 struct address_space
*mapping
= page
->mapping
;
54 struct f2fs_sb_info
*sbi
= F2FS_SB(mapping
->host
->i_sb
);
55 unsigned int long flags
;
57 if (PageDirty(page
)) {
58 spin_lock_irqsave(&mapping
->tree_lock
, flags
);
59 radix_tree_tag_clear(&mapping
->page_tree
,
62 spin_unlock_irqrestore(&mapping
->tree_lock
, flags
);
64 clear_page_dirty_for_io(page
);
65 dec_page_count(sbi
, F2FS_DIRTY_NODES
);
67 ClearPageUptodate(page
);
70 static struct page
*get_current_nat_page(struct f2fs_sb_info
*sbi
, nid_t nid
)
72 pgoff_t index
= current_nat_addr(sbi
, nid
);
73 return get_meta_page(sbi
, index
);
76 static struct page
*get_next_nat_page(struct f2fs_sb_info
*sbi
, nid_t nid
)
78 struct page
*src_page
;
79 struct page
*dst_page
;
84 struct f2fs_nm_info
*nm_i
= NM_I(sbi
);
86 src_off
= current_nat_addr(sbi
, nid
);
87 dst_off
= next_nat_addr(sbi
, src_off
);
89 /* get current nat block page with lock */
90 src_page
= get_meta_page(sbi
, src_off
);
92 /* Dirty src_page means that it is already the new target NAT page. */
93 if (PageDirty(src_page
))
96 dst_page
= grab_meta_page(sbi
, dst_off
);
98 src_addr
= page_address(src_page
);
99 dst_addr
= page_address(dst_page
);
100 memcpy(dst_addr
, src_addr
, PAGE_CACHE_SIZE
);
101 set_page_dirty(dst_page
);
102 f2fs_put_page(src_page
, 1);
104 set_to_next_nat(nm_i
, nid
);
109 static struct nat_entry
*__lookup_nat_cache(struct f2fs_nm_info
*nm_i
, nid_t n
)
111 return radix_tree_lookup(&nm_i
->nat_root
, n
);
114 static unsigned int __gang_lookup_nat_cache(struct f2fs_nm_info
*nm_i
,
115 nid_t start
, unsigned int nr
, struct nat_entry
**ep
)
117 return radix_tree_gang_lookup(&nm_i
->nat_root
, (void **)ep
, start
, nr
);
120 static void __del_from_nat_cache(struct f2fs_nm_info
*nm_i
, struct nat_entry
*e
)
123 radix_tree_delete(&nm_i
->nat_root
, nat_get_nid(e
));
125 kmem_cache_free(nat_entry_slab
, e
);
128 int is_checkpointed_node(struct f2fs_sb_info
*sbi
, nid_t nid
)
130 struct f2fs_nm_info
*nm_i
= NM_I(sbi
);
134 read_lock(&nm_i
->nat_tree_lock
);
135 e
= __lookup_nat_cache(nm_i
, nid
);
136 if (e
&& !e
->checkpointed
)
138 read_unlock(&nm_i
->nat_tree_lock
);
142 bool fsync_mark_done(struct f2fs_sb_info
*sbi
, nid_t nid
)
144 struct f2fs_nm_info
*nm_i
= NM_I(sbi
);
146 bool fsync_done
= false;
148 read_lock(&nm_i
->nat_tree_lock
);
149 e
= __lookup_nat_cache(nm_i
, nid
);
151 fsync_done
= e
->fsync_done
;
152 read_unlock(&nm_i
->nat_tree_lock
);
156 void fsync_mark_clear(struct f2fs_sb_info
*sbi
, nid_t nid
)
158 struct f2fs_nm_info
*nm_i
= NM_I(sbi
);
161 write_lock(&nm_i
->nat_tree_lock
);
162 e
= __lookup_nat_cache(nm_i
, nid
);
164 e
->fsync_done
= false;
165 write_unlock(&nm_i
->nat_tree_lock
);
168 static struct nat_entry
*grab_nat_entry(struct f2fs_nm_info
*nm_i
, nid_t nid
)
170 struct nat_entry
*new;
172 new = kmem_cache_alloc(nat_entry_slab
, GFP_ATOMIC
);
175 if (radix_tree_insert(&nm_i
->nat_root
, nid
, new)) {
176 kmem_cache_free(nat_entry_slab
, new);
179 memset(new, 0, sizeof(struct nat_entry
));
180 nat_set_nid(new, nid
);
181 new->checkpointed
= true;
182 list_add_tail(&new->list
, &nm_i
->nat_entries
);
187 static void cache_nat_entry(struct f2fs_nm_info
*nm_i
, nid_t nid
,
188 struct f2fs_nat_entry
*ne
)
192 write_lock(&nm_i
->nat_tree_lock
);
193 e
= __lookup_nat_cache(nm_i
, nid
);
195 e
= grab_nat_entry(nm_i
, nid
);
197 write_unlock(&nm_i
->nat_tree_lock
);
200 node_info_from_raw_nat(&e
->ni
, ne
);
202 write_unlock(&nm_i
->nat_tree_lock
);
205 static void set_node_addr(struct f2fs_sb_info
*sbi
, struct node_info
*ni
,
206 block_t new_blkaddr
, bool fsync_done
)
208 struct f2fs_nm_info
*nm_i
= NM_I(sbi
);
211 write_lock(&nm_i
->nat_tree_lock
);
212 e
= __lookup_nat_cache(nm_i
, ni
->nid
);
214 e
= grab_nat_entry(nm_i
, ni
->nid
);
216 write_unlock(&nm_i
->nat_tree_lock
);
220 f2fs_bug_on(ni
->blk_addr
== NEW_ADDR
);
221 } else if (new_blkaddr
== NEW_ADDR
) {
223 * when nid is reallocated,
224 * previous nat entry can be remained in nat cache.
225 * So, reinitialize it with new information.
228 f2fs_bug_on(ni
->blk_addr
!= NULL_ADDR
);
232 f2fs_bug_on(nat_get_blkaddr(e
) != ni
->blk_addr
);
233 f2fs_bug_on(nat_get_blkaddr(e
) == NULL_ADDR
&&
234 new_blkaddr
== NULL_ADDR
);
235 f2fs_bug_on(nat_get_blkaddr(e
) == NEW_ADDR
&&
236 new_blkaddr
== NEW_ADDR
);
237 f2fs_bug_on(nat_get_blkaddr(e
) != NEW_ADDR
&&
238 nat_get_blkaddr(e
) != NULL_ADDR
&&
239 new_blkaddr
== NEW_ADDR
);
241 /* increament version no as node is removed */
242 if (nat_get_blkaddr(e
) != NEW_ADDR
&& new_blkaddr
== NULL_ADDR
) {
243 unsigned char version
= nat_get_version(e
);
244 nat_set_version(e
, inc_node_version(version
));
248 nat_set_blkaddr(e
, new_blkaddr
);
249 __set_nat_cache_dirty(nm_i
, e
);
251 /* update fsync_mark if its inode nat entry is still alive */
252 e
= __lookup_nat_cache(nm_i
, ni
->ino
);
254 e
->fsync_done
= fsync_done
;
255 write_unlock(&nm_i
->nat_tree_lock
);
258 int try_to_free_nats(struct f2fs_sb_info
*sbi
, int nr_shrink
)
260 struct f2fs_nm_info
*nm_i
= NM_I(sbi
);
262 if (available_free_memory(sbi
, NAT_ENTRIES
))
265 write_lock(&nm_i
->nat_tree_lock
);
266 while (nr_shrink
&& !list_empty(&nm_i
->nat_entries
)) {
267 struct nat_entry
*ne
;
268 ne
= list_first_entry(&nm_i
->nat_entries
,
269 struct nat_entry
, list
);
270 __del_from_nat_cache(nm_i
, ne
);
273 write_unlock(&nm_i
->nat_tree_lock
);
278 * This function returns always success
280 void get_node_info(struct f2fs_sb_info
*sbi
, nid_t nid
, struct node_info
*ni
)
282 struct f2fs_nm_info
*nm_i
= NM_I(sbi
);
283 struct curseg_info
*curseg
= CURSEG_I(sbi
, CURSEG_HOT_DATA
);
284 struct f2fs_summary_block
*sum
= curseg
->sum_blk
;
285 nid_t start_nid
= START_NID(nid
);
286 struct f2fs_nat_block
*nat_blk
;
287 struct page
*page
= NULL
;
288 struct f2fs_nat_entry ne
;
292 memset(&ne
, 0, sizeof(struct f2fs_nat_entry
));
295 /* Check nat cache */
296 read_lock(&nm_i
->nat_tree_lock
);
297 e
= __lookup_nat_cache(nm_i
, nid
);
299 ni
->ino
= nat_get_ino(e
);
300 ni
->blk_addr
= nat_get_blkaddr(e
);
301 ni
->version
= nat_get_version(e
);
303 read_unlock(&nm_i
->nat_tree_lock
);
307 /* Check current segment summary */
308 mutex_lock(&curseg
->curseg_mutex
);
309 i
= lookup_journal_in_cursum(sum
, NAT_JOURNAL
, nid
, 0);
311 ne
= nat_in_journal(sum
, i
);
312 node_info_from_raw_nat(ni
, &ne
);
314 mutex_unlock(&curseg
->curseg_mutex
);
318 /* Fill node_info from nat page */
319 page
= get_current_nat_page(sbi
, start_nid
);
320 nat_blk
= (struct f2fs_nat_block
*)page_address(page
);
321 ne
= nat_blk
->entries
[nid
- start_nid
];
322 node_info_from_raw_nat(ni
, &ne
);
323 f2fs_put_page(page
, 1);
325 /* cache nat entry */
326 cache_nat_entry(NM_I(sbi
), nid
, &ne
);
330 * The maximum depth is four.
331 * Offset[0] will have raw inode offset.
333 static int get_node_path(struct f2fs_inode_info
*fi
, long block
,
334 int offset
[4], unsigned int noffset
[4])
336 const long direct_index
= ADDRS_PER_INODE(fi
);
337 const long direct_blks
= ADDRS_PER_BLOCK
;
338 const long dptrs_per_blk
= NIDS_PER_BLOCK
;
339 const long indirect_blks
= ADDRS_PER_BLOCK
* NIDS_PER_BLOCK
;
340 const long dindirect_blks
= indirect_blks
* NIDS_PER_BLOCK
;
346 if (block
< direct_index
) {
350 block
-= direct_index
;
351 if (block
< direct_blks
) {
352 offset
[n
++] = NODE_DIR1_BLOCK
;
358 block
-= direct_blks
;
359 if (block
< direct_blks
) {
360 offset
[n
++] = NODE_DIR2_BLOCK
;
366 block
-= direct_blks
;
367 if (block
< indirect_blks
) {
368 offset
[n
++] = NODE_IND1_BLOCK
;
370 offset
[n
++] = block
/ direct_blks
;
371 noffset
[n
] = 4 + offset
[n
- 1];
372 offset
[n
] = block
% direct_blks
;
376 block
-= indirect_blks
;
377 if (block
< indirect_blks
) {
378 offset
[n
++] = NODE_IND2_BLOCK
;
379 noffset
[n
] = 4 + dptrs_per_blk
;
380 offset
[n
++] = block
/ direct_blks
;
381 noffset
[n
] = 5 + dptrs_per_blk
+ offset
[n
- 1];
382 offset
[n
] = block
% direct_blks
;
386 block
-= indirect_blks
;
387 if (block
< dindirect_blks
) {
388 offset
[n
++] = NODE_DIND_BLOCK
;
389 noffset
[n
] = 5 + (dptrs_per_blk
* 2);
390 offset
[n
++] = block
/ indirect_blks
;
391 noffset
[n
] = 6 + (dptrs_per_blk
* 2) +
392 offset
[n
- 1] * (dptrs_per_blk
+ 1);
393 offset
[n
++] = (block
/ direct_blks
) % dptrs_per_blk
;
394 noffset
[n
] = 7 + (dptrs_per_blk
* 2) +
395 offset
[n
- 2] * (dptrs_per_blk
+ 1) +
397 offset
[n
] = block
% direct_blks
;
408 * Caller should call f2fs_put_dnode(dn).
409 * Also, it should grab and release a rwsem by calling f2fs_lock_op() and
410 * f2fs_unlock_op() only if ro is not set RDONLY_NODE.
411 * In the case of RDONLY_NODE, we don't need to care about mutex.
413 int get_dnode_of_data(struct dnode_of_data
*dn
, pgoff_t index
, int mode
)
415 struct f2fs_sb_info
*sbi
= F2FS_SB(dn
->inode
->i_sb
);
416 struct page
*npage
[4];
419 unsigned int noffset
[4];
424 level
= get_node_path(F2FS_I(dn
->inode
), index
, offset
, noffset
);
426 nids
[0] = dn
->inode
->i_ino
;
427 npage
[0] = dn
->inode_page
;
430 npage
[0] = get_node_page(sbi
, nids
[0]);
431 if (IS_ERR(npage
[0]))
432 return PTR_ERR(npage
[0]);
436 nids
[1] = get_nid(parent
, offset
[0], true);
437 dn
->inode_page
= npage
[0];
438 dn
->inode_page_locked
= true;
440 /* get indirect or direct nodes */
441 for (i
= 1; i
<= level
; i
++) {
444 if (!nids
[i
] && mode
== ALLOC_NODE
) {
446 if (!alloc_nid(sbi
, &(nids
[i
]))) {
452 npage
[i
] = new_node_page(dn
, noffset
[i
], NULL
);
453 if (IS_ERR(npage
[i
])) {
454 alloc_nid_failed(sbi
, nids
[i
]);
455 err
= PTR_ERR(npage
[i
]);
459 set_nid(parent
, offset
[i
- 1], nids
[i
], i
== 1);
460 alloc_nid_done(sbi
, nids
[i
]);
462 } else if (mode
== LOOKUP_NODE_RA
&& i
== level
&& level
> 1) {
463 npage
[i
] = get_node_page_ra(parent
, offset
[i
- 1]);
464 if (IS_ERR(npage
[i
])) {
465 err
= PTR_ERR(npage
[i
]);
471 dn
->inode_page_locked
= false;
474 f2fs_put_page(parent
, 1);
478 npage
[i
] = get_node_page(sbi
, nids
[i
]);
479 if (IS_ERR(npage
[i
])) {
480 err
= PTR_ERR(npage
[i
]);
481 f2fs_put_page(npage
[0], 0);
487 nids
[i
+ 1] = get_nid(parent
, offset
[i
], false);
490 dn
->nid
= nids
[level
];
491 dn
->ofs_in_node
= offset
[level
];
492 dn
->node_page
= npage
[level
];
493 dn
->data_blkaddr
= datablock_addr(dn
->node_page
, dn
->ofs_in_node
);
497 f2fs_put_page(parent
, 1);
499 f2fs_put_page(npage
[0], 0);
501 dn
->inode_page
= NULL
;
502 dn
->node_page
= NULL
;
506 static void truncate_node(struct dnode_of_data
*dn
)
508 struct f2fs_sb_info
*sbi
= F2FS_SB(dn
->inode
->i_sb
);
511 get_node_info(sbi
, dn
->nid
, &ni
);
512 if (dn
->inode
->i_blocks
== 0) {
513 f2fs_bug_on(ni
.blk_addr
!= NULL_ADDR
);
516 f2fs_bug_on(ni
.blk_addr
== NULL_ADDR
);
518 /* Deallocate node address */
519 invalidate_blocks(sbi
, ni
.blk_addr
);
520 dec_valid_node_count(sbi
, dn
->inode
);
521 set_node_addr(sbi
, &ni
, NULL_ADDR
, false);
523 if (dn
->nid
== dn
->inode
->i_ino
) {
524 remove_orphan_inode(sbi
, dn
->nid
);
525 dec_valid_inode_count(sbi
);
530 clear_node_page_dirty(dn
->node_page
);
531 F2FS_SET_SB_DIRT(sbi
);
533 f2fs_put_page(dn
->node_page
, 1);
535 invalidate_mapping_pages(NODE_MAPPING(sbi
),
536 dn
->node_page
->index
, dn
->node_page
->index
);
538 dn
->node_page
= NULL
;
539 trace_f2fs_truncate_node(dn
->inode
, dn
->nid
, ni
.blk_addr
);
542 static int truncate_dnode(struct dnode_of_data
*dn
)
544 struct f2fs_sb_info
*sbi
= F2FS_SB(dn
->inode
->i_sb
);
550 /* get direct node */
551 page
= get_node_page(sbi
, dn
->nid
);
552 if (IS_ERR(page
) && PTR_ERR(page
) == -ENOENT
)
554 else if (IS_ERR(page
))
555 return PTR_ERR(page
);
557 /* Make dnode_of_data for parameter */
558 dn
->node_page
= page
;
560 truncate_data_blocks(dn
);
565 static int truncate_nodes(struct dnode_of_data
*dn
, unsigned int nofs
,
568 struct f2fs_sb_info
*sbi
= F2FS_SB(dn
->inode
->i_sb
);
569 struct dnode_of_data rdn
= *dn
;
571 struct f2fs_node
*rn
;
573 unsigned int child_nofs
;
578 return NIDS_PER_BLOCK
+ 1;
580 trace_f2fs_truncate_nodes_enter(dn
->inode
, dn
->nid
, dn
->data_blkaddr
);
582 page
= get_node_page(sbi
, dn
->nid
);
584 trace_f2fs_truncate_nodes_exit(dn
->inode
, PTR_ERR(page
));
585 return PTR_ERR(page
);
588 rn
= F2FS_NODE(page
);
590 for (i
= ofs
; i
< NIDS_PER_BLOCK
; i
++, freed
++) {
591 child_nid
= le32_to_cpu(rn
->in
.nid
[i
]);
595 ret
= truncate_dnode(&rdn
);
598 set_nid(page
, i
, 0, false);
601 child_nofs
= nofs
+ ofs
* (NIDS_PER_BLOCK
+ 1) + 1;
602 for (i
= ofs
; i
< NIDS_PER_BLOCK
; i
++) {
603 child_nid
= le32_to_cpu(rn
->in
.nid
[i
]);
604 if (child_nid
== 0) {
605 child_nofs
+= NIDS_PER_BLOCK
+ 1;
609 ret
= truncate_nodes(&rdn
, child_nofs
, 0, depth
- 1);
610 if (ret
== (NIDS_PER_BLOCK
+ 1)) {
611 set_nid(page
, i
, 0, false);
613 } else if (ret
< 0 && ret
!= -ENOENT
) {
621 /* remove current indirect node */
622 dn
->node_page
= page
;
626 f2fs_put_page(page
, 1);
628 trace_f2fs_truncate_nodes_exit(dn
->inode
, freed
);
632 f2fs_put_page(page
, 1);
633 trace_f2fs_truncate_nodes_exit(dn
->inode
, ret
);
637 static int truncate_partial_nodes(struct dnode_of_data
*dn
,
638 struct f2fs_inode
*ri
, int *offset
, int depth
)
640 struct f2fs_sb_info
*sbi
= F2FS_SB(dn
->inode
->i_sb
);
641 struct page
*pages
[2];
648 nid
[0] = le32_to_cpu(ri
->i_nid
[offset
[0] - NODE_DIR1_BLOCK
]);
652 /* get indirect nodes in the path */
653 for (i
= 0; i
< idx
+ 1; i
++) {
654 /* refernece count'll be increased */
655 pages
[i
] = get_node_page(sbi
, nid
[i
]);
656 if (IS_ERR(pages
[i
])) {
657 err
= PTR_ERR(pages
[i
]);
661 nid
[i
+ 1] = get_nid(pages
[i
], offset
[i
+ 1], false);
664 /* free direct nodes linked to a partial indirect node */
665 for (i
= offset
[idx
+ 1]; i
< NIDS_PER_BLOCK
; i
++) {
666 child_nid
= get_nid(pages
[idx
], i
, false);
670 err
= truncate_dnode(dn
);
673 set_nid(pages
[idx
], i
, 0, false);
676 if (offset
[idx
+ 1] == 0) {
677 dn
->node_page
= pages
[idx
];
681 f2fs_put_page(pages
[idx
], 1);
687 for (i
= idx
; i
>= 0; i
--)
688 f2fs_put_page(pages
[i
], 1);
690 trace_f2fs_truncate_partial_nodes(dn
->inode
, nid
, depth
, err
);
696 * All the block addresses of data and nodes should be nullified.
698 int truncate_inode_blocks(struct inode
*inode
, pgoff_t from
)
700 struct f2fs_sb_info
*sbi
= F2FS_SB(inode
->i_sb
);
701 int err
= 0, cont
= 1;
702 int level
, offset
[4], noffset
[4];
703 unsigned int nofs
= 0;
704 struct f2fs_inode
*ri
;
705 struct dnode_of_data dn
;
708 trace_f2fs_truncate_inode_blocks_enter(inode
, from
);
710 level
= get_node_path(F2FS_I(inode
), from
, offset
, noffset
);
712 page
= get_node_page(sbi
, inode
->i_ino
);
714 trace_f2fs_truncate_inode_blocks_exit(inode
, PTR_ERR(page
));
715 return PTR_ERR(page
);
718 set_new_dnode(&dn
, inode
, page
, NULL
, 0);
721 ri
= F2FS_INODE(page
);
729 if (!offset
[level
- 1])
731 err
= truncate_partial_nodes(&dn
, ri
, offset
, level
);
732 if (err
< 0 && err
!= -ENOENT
)
734 nofs
+= 1 + NIDS_PER_BLOCK
;
737 nofs
= 5 + 2 * NIDS_PER_BLOCK
;
738 if (!offset
[level
- 1])
740 err
= truncate_partial_nodes(&dn
, ri
, offset
, level
);
741 if (err
< 0 && err
!= -ENOENT
)
750 dn
.nid
= le32_to_cpu(ri
->i_nid
[offset
[0] - NODE_DIR1_BLOCK
]);
752 case NODE_DIR1_BLOCK
:
753 case NODE_DIR2_BLOCK
:
754 err
= truncate_dnode(&dn
);
757 case NODE_IND1_BLOCK
:
758 case NODE_IND2_BLOCK
:
759 err
= truncate_nodes(&dn
, nofs
, offset
[1], 2);
762 case NODE_DIND_BLOCK
:
763 err
= truncate_nodes(&dn
, nofs
, offset
[1], 3);
770 if (err
< 0 && err
!= -ENOENT
)
772 if (offset
[1] == 0 &&
773 ri
->i_nid
[offset
[0] - NODE_DIR1_BLOCK
]) {
775 if (unlikely(page
->mapping
!= NODE_MAPPING(sbi
))) {
776 f2fs_put_page(page
, 1);
779 f2fs_wait_on_page_writeback(page
, NODE
);
780 ri
->i_nid
[offset
[0] - NODE_DIR1_BLOCK
] = 0;
781 set_page_dirty(page
);
789 f2fs_put_page(page
, 0);
790 trace_f2fs_truncate_inode_blocks_exit(inode
, err
);
791 return err
> 0 ? 0 : err
;
794 int truncate_xattr_node(struct inode
*inode
, struct page
*page
)
796 struct f2fs_sb_info
*sbi
= F2FS_SB(inode
->i_sb
);
797 nid_t nid
= F2FS_I(inode
)->i_xattr_nid
;
798 struct dnode_of_data dn
;
804 npage
= get_node_page(sbi
, nid
);
806 return PTR_ERR(npage
);
808 F2FS_I(inode
)->i_xattr_nid
= 0;
810 /* need to do checkpoint during fsync */
811 F2FS_I(inode
)->xattr_ver
= cur_cp_version(F2FS_CKPT(sbi
));
813 set_new_dnode(&dn
, inode
, page
, npage
, nid
);
816 dn
.inode_page_locked
= true;
822 * Caller should grab and release a rwsem by calling f2fs_lock_op() and
825 void remove_inode_page(struct inode
*inode
)
827 struct f2fs_sb_info
*sbi
= F2FS_SB(inode
->i_sb
);
829 nid_t ino
= inode
->i_ino
;
830 struct dnode_of_data dn
;
832 page
= get_node_page(sbi
, ino
);
836 if (truncate_xattr_node(inode
, page
)) {
837 f2fs_put_page(page
, 1);
840 /* 0 is possible, after f2fs_new_inode() is failed */
841 f2fs_bug_on(inode
->i_blocks
!= 0 && inode
->i_blocks
!= 1);
842 set_new_dnode(&dn
, inode
, page
, page
, ino
);
846 struct page
*new_inode_page(struct inode
*inode
, const struct qstr
*name
)
848 struct dnode_of_data dn
;
850 /* allocate inode page for new inode */
851 set_new_dnode(&dn
, inode
, NULL
, NULL
, inode
->i_ino
);
853 /* caller should f2fs_put_page(page, 1); */
854 return new_node_page(&dn
, 0, NULL
);
857 struct page
*new_node_page(struct dnode_of_data
*dn
,
858 unsigned int ofs
, struct page
*ipage
)
860 struct f2fs_sb_info
*sbi
= F2FS_SB(dn
->inode
->i_sb
);
861 struct node_info old_ni
, new_ni
;
865 if (unlikely(is_inode_flag_set(F2FS_I(dn
->inode
), FI_NO_ALLOC
)))
866 return ERR_PTR(-EPERM
);
868 page
= grab_cache_page(NODE_MAPPING(sbi
), dn
->nid
);
870 return ERR_PTR(-ENOMEM
);
872 if (unlikely(!inc_valid_node_count(sbi
, dn
->inode
))) {
877 get_node_info(sbi
, dn
->nid
, &old_ni
);
879 /* Reinitialize old_ni with new node page */
880 f2fs_bug_on(old_ni
.blk_addr
!= NULL_ADDR
);
882 new_ni
.ino
= dn
->inode
->i_ino
;
883 set_node_addr(sbi
, &new_ni
, NEW_ADDR
, false);
885 f2fs_wait_on_page_writeback(page
, NODE
);
886 fill_node_footer(page
, dn
->nid
, dn
->inode
->i_ino
, ofs
, true);
887 set_cold_node(dn
->inode
, page
);
888 SetPageUptodate(page
);
889 set_page_dirty(page
);
891 if (f2fs_has_xattr_block(ofs
))
892 F2FS_I(dn
->inode
)->i_xattr_nid
= dn
->nid
;
894 dn
->node_page
= page
;
896 update_inode(dn
->inode
, ipage
);
900 inc_valid_inode_count(sbi
);
905 clear_node_page_dirty(page
);
906 f2fs_put_page(page
, 1);
911 * Caller should do after getting the following values.
912 * 0: f2fs_put_page(page, 0)
913 * LOCKED_PAGE: f2fs_put_page(page, 1)
916 static int read_node_page(struct page
*page
, int rw
)
918 struct f2fs_sb_info
*sbi
= F2FS_SB(page
->mapping
->host
->i_sb
);
921 get_node_info(sbi
, page
->index
, &ni
);
923 if (unlikely(ni
.blk_addr
== NULL_ADDR
)) {
924 f2fs_put_page(page
, 1);
928 if (PageUptodate(page
))
931 return f2fs_submit_page_bio(sbi
, page
, ni
.blk_addr
, rw
);
935 * Readahead a node page
937 void ra_node_page(struct f2fs_sb_info
*sbi
, nid_t nid
)
942 apage
= find_get_page(NODE_MAPPING(sbi
), nid
);
943 if (apage
&& PageUptodate(apage
)) {
944 f2fs_put_page(apage
, 0);
947 f2fs_put_page(apage
, 0);
949 apage
= grab_cache_page(NODE_MAPPING(sbi
), nid
);
953 err
= read_node_page(apage
, READA
);
955 f2fs_put_page(apage
, 0);
956 else if (err
== LOCKED_PAGE
)
957 f2fs_put_page(apage
, 1);
960 struct page
*get_node_page(struct f2fs_sb_info
*sbi
, pgoff_t nid
)
965 page
= grab_cache_page(NODE_MAPPING(sbi
), nid
);
967 return ERR_PTR(-ENOMEM
);
969 err
= read_node_page(page
, READ_SYNC
);
972 else if (err
== LOCKED_PAGE
)
976 if (unlikely(!PageUptodate(page
) || nid
!= nid_of_node(page
))) {
977 f2fs_put_page(page
, 1);
978 return ERR_PTR(-EIO
);
980 if (unlikely(page
->mapping
!= NODE_MAPPING(sbi
))) {
981 f2fs_put_page(page
, 1);
989 * Return a locked page for the desired node page.
990 * And, readahead MAX_RA_NODE number of node pages.
992 struct page
*get_node_page_ra(struct page
*parent
, int start
)
994 struct f2fs_sb_info
*sbi
= F2FS_SB(parent
->mapping
->host
->i_sb
);
995 struct blk_plug plug
;
1000 /* First, try getting the desired direct node. */
1001 nid
= get_nid(parent
, start
, false);
1003 return ERR_PTR(-ENOENT
);
1005 page
= grab_cache_page(NODE_MAPPING(sbi
), nid
);
1007 return ERR_PTR(-ENOMEM
);
1009 err
= read_node_page(page
, READ_SYNC
);
1011 return ERR_PTR(err
);
1012 else if (err
== LOCKED_PAGE
)
1015 blk_start_plug(&plug
);
1017 /* Then, try readahead for siblings of the desired node */
1018 end
= start
+ MAX_RA_NODE
;
1019 end
= min(end
, NIDS_PER_BLOCK
);
1020 for (i
= start
+ 1; i
< end
; i
++) {
1021 nid
= get_nid(parent
, i
, false);
1024 ra_node_page(sbi
, nid
);
1027 blk_finish_plug(&plug
);
1030 if (unlikely(page
->mapping
!= NODE_MAPPING(sbi
))) {
1031 f2fs_put_page(page
, 1);
1035 if (unlikely(!PageUptodate(page
))) {
1036 f2fs_put_page(page
, 1);
1037 return ERR_PTR(-EIO
);
1042 void sync_inode_page(struct dnode_of_data
*dn
)
1044 if (IS_INODE(dn
->node_page
) || dn
->inode_page
== dn
->node_page
) {
1045 update_inode(dn
->inode
, dn
->node_page
);
1046 } else if (dn
->inode_page
) {
1047 if (!dn
->inode_page_locked
)
1048 lock_page(dn
->inode_page
);
1049 update_inode(dn
->inode
, dn
->inode_page
);
1050 if (!dn
->inode_page_locked
)
1051 unlock_page(dn
->inode_page
);
1053 update_inode_page(dn
->inode
);
1057 int sync_node_pages(struct f2fs_sb_info
*sbi
, nid_t ino
,
1058 struct writeback_control
*wbc
)
1061 struct pagevec pvec
;
1062 int step
= ino
? 2 : 0;
1063 int nwritten
= 0, wrote
= 0;
1065 pagevec_init(&pvec
, 0);
1071 while (index
<= end
) {
1073 nr_pages
= pagevec_lookup_tag(&pvec
, NODE_MAPPING(sbi
), &index
,
1074 PAGECACHE_TAG_DIRTY
,
1075 min(end
- index
, (pgoff_t
)PAGEVEC_SIZE
-1) + 1);
1079 for (i
= 0; i
< nr_pages
; i
++) {
1080 struct page
*page
= pvec
.pages
[i
];
1083 * flushing sequence with step:
1088 if (step
== 0 && IS_DNODE(page
))
1090 if (step
== 1 && (!IS_DNODE(page
) ||
1091 is_cold_node(page
)))
1093 if (step
== 2 && (!IS_DNODE(page
) ||
1094 !is_cold_node(page
)))
1099 * we should not skip writing node pages.
1101 if (ino
&& ino_of_node(page
) == ino
)
1103 else if (!trylock_page(page
))
1106 if (unlikely(page
->mapping
!= NODE_MAPPING(sbi
))) {
1111 if (ino
&& ino_of_node(page
) != ino
)
1112 goto continue_unlock
;
1114 if (!PageDirty(page
)) {
1115 /* someone wrote it for us */
1116 goto continue_unlock
;
1119 if (!clear_page_dirty_for_io(page
))
1120 goto continue_unlock
;
1122 /* called by fsync() */
1123 if (ino
&& IS_DNODE(page
)) {
1124 int mark
= !is_checkpointed_node(sbi
, ino
);
1125 set_fsync_mark(page
, 1);
1127 set_dentry_mark(page
, mark
);
1130 set_fsync_mark(page
, 0);
1131 set_dentry_mark(page
, 0);
1133 NODE_MAPPING(sbi
)->a_ops
->writepage(page
, wbc
);
1136 if (--wbc
->nr_to_write
== 0)
1139 pagevec_release(&pvec
);
1142 if (wbc
->nr_to_write
== 0) {
1154 f2fs_submit_merged_bio(sbi
, NODE
, WRITE
);
1158 int wait_on_node_pages_writeback(struct f2fs_sb_info
*sbi
, nid_t ino
)
1160 pgoff_t index
= 0, end
= LONG_MAX
;
1161 struct pagevec pvec
;
1162 int ret2
= 0, ret
= 0;
1164 pagevec_init(&pvec
, 0);
1166 while (index
<= end
) {
1168 nr_pages
= pagevec_lookup_tag(&pvec
, NODE_MAPPING(sbi
), &index
,
1169 PAGECACHE_TAG_WRITEBACK
,
1170 min(end
- index
, (pgoff_t
)PAGEVEC_SIZE
-1) + 1);
1174 for (i
= 0; i
< nr_pages
; i
++) {
1175 struct page
*page
= pvec
.pages
[i
];
1177 /* until radix tree lookup accepts end_index */
1178 if (unlikely(page
->index
> end
))
1181 if (ino
&& ino_of_node(page
) == ino
) {
1182 f2fs_wait_on_page_writeback(page
, NODE
);
1183 if (TestClearPageError(page
))
1187 pagevec_release(&pvec
);
1191 if (unlikely(test_and_clear_bit(AS_ENOSPC
, &NODE_MAPPING(sbi
)->flags
)))
1193 if (unlikely(test_and_clear_bit(AS_EIO
, &NODE_MAPPING(sbi
)->flags
)))
1200 static int f2fs_write_node_page(struct page
*page
,
1201 struct writeback_control
*wbc
)
1203 struct f2fs_sb_info
*sbi
= F2FS_SB(page
->mapping
->host
->i_sb
);
1206 struct node_info ni
;
1207 struct f2fs_io_info fio
= {
1209 .rw
= (wbc
->sync_mode
== WB_SYNC_ALL
) ? WRITE_SYNC
: WRITE
,
1212 trace_f2fs_writepage(page
, NODE
);
1214 if (unlikely(sbi
->por_doing
))
1217 f2fs_wait_on_page_writeback(page
, NODE
);
1219 /* get old block addr of this node page */
1220 nid
= nid_of_node(page
);
1221 f2fs_bug_on(page
->index
!= nid
);
1223 get_node_info(sbi
, nid
, &ni
);
1225 /* This page is already truncated */
1226 if (unlikely(ni
.blk_addr
== NULL_ADDR
)) {
1227 dec_page_count(sbi
, F2FS_DIRTY_NODES
);
1232 if (wbc
->for_reclaim
)
1235 mutex_lock(&sbi
->node_write
);
1236 set_page_writeback(page
);
1237 write_node_page(sbi
, page
, &fio
, nid
, ni
.blk_addr
, &new_addr
);
1238 set_node_addr(sbi
, &ni
, new_addr
, is_fsync_dnode(page
));
1239 dec_page_count(sbi
, F2FS_DIRTY_NODES
);
1240 mutex_unlock(&sbi
->node_write
);
1245 redirty_page_for_writepage(wbc
, page
);
1246 return AOP_WRITEPAGE_ACTIVATE
;
1249 static int f2fs_write_node_pages(struct address_space
*mapping
,
1250 struct writeback_control
*wbc
)
1252 struct f2fs_sb_info
*sbi
= F2FS_SB(mapping
->host
->i_sb
);
1255 trace_f2fs_writepages(mapping
->host
, wbc
, NODE
);
1257 /* balancing f2fs's metadata in background */
1258 f2fs_balance_fs_bg(sbi
);
1260 /* collect a number of dirty node pages and write together */
1261 if (get_pages(sbi
, F2FS_DIRTY_NODES
) < nr_pages_to_skip(sbi
, NODE
))
1264 diff
= nr_pages_to_write(sbi
, NODE
, wbc
);
1265 wbc
->sync_mode
= WB_SYNC_NONE
;
1266 sync_node_pages(sbi
, 0, wbc
);
1267 wbc
->nr_to_write
= max((long)0, wbc
->nr_to_write
- diff
);
1271 wbc
->pages_skipped
+= get_pages(sbi
, F2FS_DIRTY_NODES
);
1275 static int f2fs_set_node_page_dirty(struct page
*page
)
1277 struct address_space
*mapping
= page
->mapping
;
1278 struct f2fs_sb_info
*sbi
= F2FS_SB(mapping
->host
->i_sb
);
1280 trace_f2fs_set_page_dirty(page
, NODE
);
1282 SetPageUptodate(page
);
1283 if (!PageDirty(page
)) {
1284 __set_page_dirty_nobuffers(page
);
1285 inc_page_count(sbi
, F2FS_DIRTY_NODES
);
1286 SetPagePrivate(page
);
1292 static void f2fs_invalidate_node_page(struct page
*page
, unsigned int offset
,
1293 unsigned int length
)
1295 struct inode
*inode
= page
->mapping
->host
;
1296 struct f2fs_sb_info
*sbi
= F2FS_SB(inode
->i_sb
);
1297 if (PageDirty(page
))
1298 dec_page_count(sbi
, F2FS_DIRTY_NODES
);
1299 ClearPagePrivate(page
);
1302 static int f2fs_release_node_page(struct page
*page
, gfp_t wait
)
1304 ClearPagePrivate(page
);
1309 * Structure of the f2fs node operations
1311 const struct address_space_operations f2fs_node_aops
= {
1312 .writepage
= f2fs_write_node_page
,
1313 .writepages
= f2fs_write_node_pages
,
1314 .set_page_dirty
= f2fs_set_node_page_dirty
,
1315 .invalidatepage
= f2fs_invalidate_node_page
,
1316 .releasepage
= f2fs_release_node_page
,
1319 static struct free_nid
*__lookup_free_nid_list(struct f2fs_nm_info
*nm_i
,
1322 return radix_tree_lookup(&nm_i
->free_nid_root
, n
);
1325 static void __del_from_free_nid_list(struct f2fs_nm_info
*nm_i
,
1329 radix_tree_delete(&nm_i
->free_nid_root
, i
->nid
);
1332 static int add_free_nid(struct f2fs_sb_info
*sbi
, nid_t nid
, bool build
)
1334 struct f2fs_nm_info
*nm_i
= NM_I(sbi
);
1336 struct nat_entry
*ne
;
1337 bool allocated
= false;
1339 if (!available_free_memory(sbi
, FREE_NIDS
))
1342 /* 0 nid should not be used */
1343 if (unlikely(nid
== 0))
1347 /* do not add allocated nids */
1348 read_lock(&nm_i
->nat_tree_lock
);
1349 ne
= __lookup_nat_cache(nm_i
, nid
);
1351 (!ne
->checkpointed
|| nat_get_blkaddr(ne
) != NULL_ADDR
))
1353 read_unlock(&nm_i
->nat_tree_lock
);
1358 i
= f2fs_kmem_cache_alloc(free_nid_slab
, GFP_NOFS
);
1362 spin_lock(&nm_i
->free_nid_list_lock
);
1363 if (radix_tree_insert(&nm_i
->free_nid_root
, i
->nid
, i
)) {
1364 spin_unlock(&nm_i
->free_nid_list_lock
);
1365 kmem_cache_free(free_nid_slab
, i
);
1368 list_add_tail(&i
->list
, &nm_i
->free_nid_list
);
1370 spin_unlock(&nm_i
->free_nid_list_lock
);
1374 static void remove_free_nid(struct f2fs_nm_info
*nm_i
, nid_t nid
)
1377 bool need_free
= false;
1379 spin_lock(&nm_i
->free_nid_list_lock
);
1380 i
= __lookup_free_nid_list(nm_i
, nid
);
1381 if (i
&& i
->state
== NID_NEW
) {
1382 __del_from_free_nid_list(nm_i
, i
);
1386 spin_unlock(&nm_i
->free_nid_list_lock
);
1389 kmem_cache_free(free_nid_slab
, i
);
1392 static void scan_nat_page(struct f2fs_sb_info
*sbi
,
1393 struct page
*nat_page
, nid_t start_nid
)
1395 struct f2fs_nm_info
*nm_i
= NM_I(sbi
);
1396 struct f2fs_nat_block
*nat_blk
= page_address(nat_page
);
1400 i
= start_nid
% NAT_ENTRY_PER_BLOCK
;
1402 for (; i
< NAT_ENTRY_PER_BLOCK
; i
++, start_nid
++) {
1404 if (unlikely(start_nid
>= nm_i
->max_nid
))
1407 blk_addr
= le32_to_cpu(nat_blk
->entries
[i
].block_addr
);
1408 f2fs_bug_on(blk_addr
== NEW_ADDR
);
1409 if (blk_addr
== NULL_ADDR
) {
1410 if (add_free_nid(sbi
, start_nid
, true) < 0)
1416 static void build_free_nids(struct f2fs_sb_info
*sbi
)
1418 struct f2fs_nm_info
*nm_i
= NM_I(sbi
);
1419 struct curseg_info
*curseg
= CURSEG_I(sbi
, CURSEG_HOT_DATA
);
1420 struct f2fs_summary_block
*sum
= curseg
->sum_blk
;
1422 nid_t nid
= nm_i
->next_scan_nid
;
1424 /* Enough entries */
1425 if (nm_i
->fcnt
> NAT_ENTRY_PER_BLOCK
)
1428 /* readahead nat pages to be scanned */
1429 ra_meta_pages(sbi
, NAT_BLOCK_OFFSET(nid
), FREE_NID_PAGES
, META_NAT
);
1432 struct page
*page
= get_current_nat_page(sbi
, nid
);
1434 scan_nat_page(sbi
, page
, nid
);
1435 f2fs_put_page(page
, 1);
1437 nid
+= (NAT_ENTRY_PER_BLOCK
- (nid
% NAT_ENTRY_PER_BLOCK
));
1438 if (unlikely(nid
>= nm_i
->max_nid
))
1441 if (i
++ == FREE_NID_PAGES
)
1445 /* go to the next free nat pages to find free nids abundantly */
1446 nm_i
->next_scan_nid
= nid
;
1448 /* find free nids from current sum_pages */
1449 mutex_lock(&curseg
->curseg_mutex
);
1450 for (i
= 0; i
< nats_in_cursum(sum
); i
++) {
1451 block_t addr
= le32_to_cpu(nat_in_journal(sum
, i
).block_addr
);
1452 nid
= le32_to_cpu(nid_in_journal(sum
, i
));
1453 if (addr
== NULL_ADDR
)
1454 add_free_nid(sbi
, nid
, true);
1456 remove_free_nid(nm_i
, nid
);
1458 mutex_unlock(&curseg
->curseg_mutex
);
1462 * If this function returns success, caller can obtain a new nid
1463 * from second parameter of this function.
1464 * The returned nid could be used ino as well as nid when inode is created.
1466 bool alloc_nid(struct f2fs_sb_info
*sbi
, nid_t
*nid
)
1468 struct f2fs_nm_info
*nm_i
= NM_I(sbi
);
1469 struct free_nid
*i
= NULL
;
1471 if (unlikely(sbi
->total_valid_node_count
+ 1 > nm_i
->available_nids
))
1474 spin_lock(&nm_i
->free_nid_list_lock
);
1476 /* We should not use stale free nids created by build_free_nids */
1477 if (nm_i
->fcnt
&& !on_build_free_nids(nm_i
)) {
1478 f2fs_bug_on(list_empty(&nm_i
->free_nid_list
));
1479 list_for_each_entry(i
, &nm_i
->free_nid_list
, list
)
1480 if (i
->state
== NID_NEW
)
1483 f2fs_bug_on(i
->state
!= NID_NEW
);
1485 i
->state
= NID_ALLOC
;
1487 spin_unlock(&nm_i
->free_nid_list_lock
);
1490 spin_unlock(&nm_i
->free_nid_list_lock
);
1492 /* Let's scan nat pages and its caches to get free nids */
1493 mutex_lock(&nm_i
->build_lock
);
1494 build_free_nids(sbi
);
1495 mutex_unlock(&nm_i
->build_lock
);
1500 * alloc_nid() should be called prior to this function.
1502 void alloc_nid_done(struct f2fs_sb_info
*sbi
, nid_t nid
)
1504 struct f2fs_nm_info
*nm_i
= NM_I(sbi
);
1507 spin_lock(&nm_i
->free_nid_list_lock
);
1508 i
= __lookup_free_nid_list(nm_i
, nid
);
1509 f2fs_bug_on(!i
|| i
->state
!= NID_ALLOC
);
1510 __del_from_free_nid_list(nm_i
, i
);
1511 spin_unlock(&nm_i
->free_nid_list_lock
);
1513 kmem_cache_free(free_nid_slab
, i
);
1517 * alloc_nid() should be called prior to this function.
1519 void alloc_nid_failed(struct f2fs_sb_info
*sbi
, nid_t nid
)
1521 struct f2fs_nm_info
*nm_i
= NM_I(sbi
);
1523 bool need_free
= false;
1528 spin_lock(&nm_i
->free_nid_list_lock
);
1529 i
= __lookup_free_nid_list(nm_i
, nid
);
1530 f2fs_bug_on(!i
|| i
->state
!= NID_ALLOC
);
1531 if (!available_free_memory(sbi
, FREE_NIDS
)) {
1532 __del_from_free_nid_list(nm_i
, i
);
1538 spin_unlock(&nm_i
->free_nid_list_lock
);
1541 kmem_cache_free(free_nid_slab
, i
);
1544 void recover_node_page(struct f2fs_sb_info
*sbi
, struct page
*page
,
1545 struct f2fs_summary
*sum
, struct node_info
*ni
,
1546 block_t new_blkaddr
)
1548 rewrite_node_page(sbi
, page
, sum
, ni
->blk_addr
, new_blkaddr
);
1549 set_node_addr(sbi
, ni
, new_blkaddr
, false);
1550 clear_node_page_dirty(page
);
1553 static void recover_inline_xattr(struct inode
*inode
, struct page
*page
)
1555 struct f2fs_sb_info
*sbi
= F2FS_SB(inode
->i_sb
);
1556 void *src_addr
, *dst_addr
;
1559 struct f2fs_inode
*ri
;
1561 if (!f2fs_has_inline_xattr(inode
))
1564 if (!IS_INODE(page
))
1567 ri
= F2FS_INODE(page
);
1568 if (!(ri
->i_inline
& F2FS_INLINE_XATTR
))
1571 ipage
= get_node_page(sbi
, inode
->i_ino
);
1572 f2fs_bug_on(IS_ERR(ipage
));
1574 dst_addr
= inline_xattr_addr(ipage
);
1575 src_addr
= inline_xattr_addr(page
);
1576 inline_size
= inline_xattr_size(inode
);
1578 f2fs_wait_on_page_writeback(ipage
, NODE
);
1579 memcpy(dst_addr
, src_addr
, inline_size
);
1581 update_inode(inode
, ipage
);
1582 f2fs_put_page(ipage
, 1);
1585 bool recover_xattr_data(struct inode
*inode
, struct page
*page
, block_t blkaddr
)
1587 struct f2fs_sb_info
*sbi
= F2FS_SB(inode
->i_sb
);
1588 nid_t prev_xnid
= F2FS_I(inode
)->i_xattr_nid
;
1589 nid_t new_xnid
= nid_of_node(page
);
1590 struct node_info ni
;
1592 recover_inline_xattr(inode
, page
);
1594 if (!f2fs_has_xattr_block(ofs_of_node(page
)))
1597 /* 1: invalidate the previous xattr nid */
1601 /* Deallocate node address */
1602 get_node_info(sbi
, prev_xnid
, &ni
);
1603 f2fs_bug_on(ni
.blk_addr
== NULL_ADDR
);
1604 invalidate_blocks(sbi
, ni
.blk_addr
);
1605 dec_valid_node_count(sbi
, inode
);
1606 set_node_addr(sbi
, &ni
, NULL_ADDR
, false);
1609 /* 2: allocate new xattr nid */
1610 if (unlikely(!inc_valid_node_count(sbi
, inode
)))
1613 remove_free_nid(NM_I(sbi
), new_xnid
);
1614 get_node_info(sbi
, new_xnid
, &ni
);
1615 ni
.ino
= inode
->i_ino
;
1616 set_node_addr(sbi
, &ni
, NEW_ADDR
, false);
1617 F2FS_I(inode
)->i_xattr_nid
= new_xnid
;
1619 /* 3: update xattr blkaddr */
1620 refresh_sit_entry(sbi
, NEW_ADDR
, blkaddr
);
1621 set_node_addr(sbi
, &ni
, blkaddr
, false);
1623 update_inode_page(inode
);
1627 int recover_inode_page(struct f2fs_sb_info
*sbi
, struct page
*page
)
1629 struct f2fs_inode
*src
, *dst
;
1630 nid_t ino
= ino_of_node(page
);
1631 struct node_info old_ni
, new_ni
;
1634 get_node_info(sbi
, ino
, &old_ni
);
1636 if (unlikely(old_ni
.blk_addr
!= NULL_ADDR
))
1639 ipage
= grab_cache_page(NODE_MAPPING(sbi
), ino
);
1643 /* Should not use this inode from free nid list */
1644 remove_free_nid(NM_I(sbi
), ino
);
1646 SetPageUptodate(ipage
);
1647 fill_node_footer(ipage
, ino
, ino
, 0, true);
1649 src
= F2FS_INODE(page
);
1650 dst
= F2FS_INODE(ipage
);
1652 memcpy(dst
, src
, (unsigned long)&src
->i_ext
- (unsigned long)src
);
1654 dst
->i_blocks
= cpu_to_le64(1);
1655 dst
->i_links
= cpu_to_le32(1);
1656 dst
->i_xattr_nid
= 0;
1661 if (unlikely(!inc_valid_node_count(sbi
, NULL
)))
1663 set_node_addr(sbi
, &new_ni
, NEW_ADDR
, false);
1664 inc_valid_inode_count(sbi
);
1665 f2fs_put_page(ipage
, 1);
1670 * ra_sum_pages() merge contiguous pages into one bio and submit.
1671 * these pre-readed pages are alloced in bd_inode's mapping tree.
1673 static int ra_sum_pages(struct f2fs_sb_info
*sbi
, struct page
**pages
,
1674 int start
, int nrpages
)
1676 struct inode
*inode
= sbi
->sb
->s_bdev
->bd_inode
;
1677 struct address_space
*mapping
= inode
->i_mapping
;
1678 int i
, page_idx
= start
;
1679 struct f2fs_io_info fio
= {
1681 .rw
= READ_SYNC
| REQ_META
| REQ_PRIO
1684 for (i
= 0; page_idx
< start
+ nrpages
; page_idx
++, i
++) {
1685 /* alloc page in bd_inode for reading node summary info */
1686 pages
[i
] = grab_cache_page(mapping
, page_idx
);
1689 f2fs_submit_page_mbio(sbi
, pages
[i
], page_idx
, &fio
);
1692 f2fs_submit_merged_bio(sbi
, META
, READ
);
1696 int restore_node_summary(struct f2fs_sb_info
*sbi
,
1697 unsigned int segno
, struct f2fs_summary_block
*sum
)
1699 struct f2fs_node
*rn
;
1700 struct f2fs_summary
*sum_entry
;
1701 struct inode
*inode
= sbi
->sb
->s_bdev
->bd_inode
;
1703 int bio_blocks
= MAX_BIO_BLOCKS(max_hw_blocks(sbi
));
1704 struct page
*pages
[bio_blocks
];
1705 int i
, idx
, last_offset
, nrpages
, err
= 0;
1707 /* scan the node segment */
1708 last_offset
= sbi
->blocks_per_seg
;
1709 addr
= START_BLOCK(sbi
, segno
);
1710 sum_entry
= &sum
->entries
[0];
1712 for (i
= 0; !err
&& i
< last_offset
; i
+= nrpages
, addr
+= nrpages
) {
1713 nrpages
= min(last_offset
- i
, bio_blocks
);
1715 /* read ahead node pages */
1716 nrpages
= ra_sum_pages(sbi
, pages
, addr
, nrpages
);
1720 for (idx
= 0; idx
< nrpages
; idx
++) {
1724 lock_page(pages
[idx
]);
1725 if (unlikely(!PageUptodate(pages
[idx
]))) {
1728 rn
= F2FS_NODE(pages
[idx
]);
1729 sum_entry
->nid
= rn
->footer
.nid
;
1730 sum_entry
->version
= 0;
1731 sum_entry
->ofs_in_node
= 0;
1734 unlock_page(pages
[idx
]);
1736 page_cache_release(pages
[idx
]);
1739 invalidate_mapping_pages(inode
->i_mapping
, addr
,
1745 static bool flush_nats_in_journal(struct f2fs_sb_info
*sbi
)
1747 struct f2fs_nm_info
*nm_i
= NM_I(sbi
);
1748 struct curseg_info
*curseg
= CURSEG_I(sbi
, CURSEG_HOT_DATA
);
1749 struct f2fs_summary_block
*sum
= curseg
->sum_blk
;
1752 mutex_lock(&curseg
->curseg_mutex
);
1754 if (nats_in_cursum(sum
) < NAT_JOURNAL_ENTRIES
) {
1755 mutex_unlock(&curseg
->curseg_mutex
);
1759 for (i
= 0; i
< nats_in_cursum(sum
); i
++) {
1760 struct nat_entry
*ne
;
1761 struct f2fs_nat_entry raw_ne
;
1762 nid_t nid
= le32_to_cpu(nid_in_journal(sum
, i
));
1764 raw_ne
= nat_in_journal(sum
, i
);
1766 write_lock(&nm_i
->nat_tree_lock
);
1767 ne
= __lookup_nat_cache(nm_i
, nid
);
1769 __set_nat_cache_dirty(nm_i
, ne
);
1770 write_unlock(&nm_i
->nat_tree_lock
);
1773 ne
= grab_nat_entry(nm_i
, nid
);
1775 write_unlock(&nm_i
->nat_tree_lock
);
1778 node_info_from_raw_nat(&ne
->ni
, &raw_ne
);
1779 __set_nat_cache_dirty(nm_i
, ne
);
1780 write_unlock(&nm_i
->nat_tree_lock
);
1782 update_nats_in_cursum(sum
, -i
);
1783 mutex_unlock(&curseg
->curseg_mutex
);
1788 * This function is called during the checkpointing process.
1790 void flush_nat_entries(struct f2fs_sb_info
*sbi
)
1792 struct f2fs_nm_info
*nm_i
= NM_I(sbi
);
1793 struct curseg_info
*curseg
= CURSEG_I(sbi
, CURSEG_HOT_DATA
);
1794 struct f2fs_summary_block
*sum
= curseg
->sum_blk
;
1795 struct nat_entry
*ne
, *cur
;
1796 struct page
*page
= NULL
;
1797 struct f2fs_nat_block
*nat_blk
= NULL
;
1798 nid_t start_nid
= 0, end_nid
= 0;
1801 flushed
= flush_nats_in_journal(sbi
);
1804 mutex_lock(&curseg
->curseg_mutex
);
1806 /* 1) flush dirty nat caches */
1807 list_for_each_entry_safe(ne
, cur
, &nm_i
->dirty_nat_entries
, list
) {
1809 struct f2fs_nat_entry raw_ne
;
1812 if (nat_get_blkaddr(ne
) == NEW_ADDR
)
1815 nid
= nat_get_nid(ne
);
1820 /* if there is room for nat enries in curseg->sumpage */
1821 offset
= lookup_journal_in_cursum(sum
, NAT_JOURNAL
, nid
, 1);
1823 raw_ne
= nat_in_journal(sum
, offset
);
1827 if (!page
|| (start_nid
> nid
|| nid
> end_nid
)) {
1829 f2fs_put_page(page
, 1);
1832 start_nid
= START_NID(nid
);
1833 end_nid
= start_nid
+ NAT_ENTRY_PER_BLOCK
- 1;
1836 * get nat block with dirty flag, increased reference
1837 * count, mapped and lock
1839 page
= get_next_nat_page(sbi
, start_nid
);
1840 nat_blk
= page_address(page
);
1843 f2fs_bug_on(!nat_blk
);
1844 raw_ne
= nat_blk
->entries
[nid
- start_nid
];
1846 raw_nat_from_node_info(&raw_ne
, &ne
->ni
);
1849 nat_blk
->entries
[nid
- start_nid
] = raw_ne
;
1851 nat_in_journal(sum
, offset
) = raw_ne
;
1852 nid_in_journal(sum
, offset
) = cpu_to_le32(nid
);
1855 if (nat_get_blkaddr(ne
) == NULL_ADDR
&&
1856 add_free_nid(sbi
, nid
, false) <= 0) {
1857 write_lock(&nm_i
->nat_tree_lock
);
1858 __del_from_nat_cache(nm_i
, ne
);
1859 write_unlock(&nm_i
->nat_tree_lock
);
1861 write_lock(&nm_i
->nat_tree_lock
);
1862 __clear_nat_cache_dirty(nm_i
, ne
);
1863 write_unlock(&nm_i
->nat_tree_lock
);
1867 mutex_unlock(&curseg
->curseg_mutex
);
1868 f2fs_put_page(page
, 1);
1871 static int init_node_manager(struct f2fs_sb_info
*sbi
)
1873 struct f2fs_super_block
*sb_raw
= F2FS_RAW_SUPER(sbi
);
1874 struct f2fs_nm_info
*nm_i
= NM_I(sbi
);
1875 unsigned char *version_bitmap
;
1876 unsigned int nat_segs
, nat_blocks
;
1878 nm_i
->nat_blkaddr
= le32_to_cpu(sb_raw
->nat_blkaddr
);
1880 /* segment_count_nat includes pair segment so divide to 2. */
1881 nat_segs
= le32_to_cpu(sb_raw
->segment_count_nat
) >> 1;
1882 nat_blocks
= nat_segs
<< le32_to_cpu(sb_raw
->log_blocks_per_seg
);
1884 nm_i
->max_nid
= NAT_ENTRY_PER_BLOCK
* nat_blocks
;
1886 /* not used nids: 0, node, meta, (and root counted as valid node) */
1887 nm_i
->available_nids
= nm_i
->max_nid
- 3;
1890 nm_i
->ram_thresh
= DEF_RAM_THRESHOLD
;
1892 INIT_RADIX_TREE(&nm_i
->free_nid_root
, GFP_ATOMIC
);
1893 INIT_LIST_HEAD(&nm_i
->free_nid_list
);
1894 INIT_RADIX_TREE(&nm_i
->nat_root
, GFP_ATOMIC
);
1895 INIT_LIST_HEAD(&nm_i
->nat_entries
);
1896 INIT_LIST_HEAD(&nm_i
->dirty_nat_entries
);
1898 mutex_init(&nm_i
->build_lock
);
1899 spin_lock_init(&nm_i
->free_nid_list_lock
);
1900 rwlock_init(&nm_i
->nat_tree_lock
);
1902 nm_i
->next_scan_nid
= le32_to_cpu(sbi
->ckpt
->next_free_nid
);
1903 nm_i
->bitmap_size
= __bitmap_size(sbi
, NAT_BITMAP
);
1904 version_bitmap
= __bitmap_ptr(sbi
, NAT_BITMAP
);
1905 if (!version_bitmap
)
1908 nm_i
->nat_bitmap
= kmemdup(version_bitmap
, nm_i
->bitmap_size
,
1910 if (!nm_i
->nat_bitmap
)
1915 int build_node_manager(struct f2fs_sb_info
*sbi
)
1919 sbi
->nm_info
= kzalloc(sizeof(struct f2fs_nm_info
), GFP_KERNEL
);
1923 err
= init_node_manager(sbi
);
1927 build_free_nids(sbi
);
1931 void destroy_node_manager(struct f2fs_sb_info
*sbi
)
1933 struct f2fs_nm_info
*nm_i
= NM_I(sbi
);
1934 struct free_nid
*i
, *next_i
;
1935 struct nat_entry
*natvec
[NATVEC_SIZE
];
1942 /* destroy free nid list */
1943 spin_lock(&nm_i
->free_nid_list_lock
);
1944 list_for_each_entry_safe(i
, next_i
, &nm_i
->free_nid_list
, list
) {
1945 f2fs_bug_on(i
->state
== NID_ALLOC
);
1946 __del_from_free_nid_list(nm_i
, i
);
1948 spin_unlock(&nm_i
->free_nid_list_lock
);
1949 kmem_cache_free(free_nid_slab
, i
);
1950 spin_lock(&nm_i
->free_nid_list_lock
);
1952 f2fs_bug_on(nm_i
->fcnt
);
1953 spin_unlock(&nm_i
->free_nid_list_lock
);
1955 /* destroy nat cache */
1956 write_lock(&nm_i
->nat_tree_lock
);
1957 while ((found
= __gang_lookup_nat_cache(nm_i
,
1958 nid
, NATVEC_SIZE
, natvec
))) {
1960 nid
= nat_get_nid(natvec
[found
- 1]) + 1;
1961 for (idx
= 0; idx
< found
; idx
++)
1962 __del_from_nat_cache(nm_i
, natvec
[idx
]);
1964 f2fs_bug_on(nm_i
->nat_cnt
);
1965 write_unlock(&nm_i
->nat_tree_lock
);
1967 kfree(nm_i
->nat_bitmap
);
1968 sbi
->nm_info
= NULL
;
1972 int __init
create_node_manager_caches(void)
1974 nat_entry_slab
= f2fs_kmem_cache_create("nat_entry",
1975 sizeof(struct nat_entry
));
1976 if (!nat_entry_slab
)
1979 free_nid_slab
= f2fs_kmem_cache_create("free_nid",
1980 sizeof(struct free_nid
));
1981 if (!free_nid_slab
) {
1982 kmem_cache_destroy(nat_entry_slab
);
1988 void destroy_node_manager_caches(void)
1990 kmem_cache_destroy(free_nid_slab
);
1991 kmem_cache_destroy(nat_entry_slab
);