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 #include <trace/events/f2fs.h>
25 #define on_build_free_nids(nmi) mutex_is_locked(&nm_i->build_lock)
27 static struct kmem_cache
*nat_entry_slab
;
28 static struct kmem_cache
*free_nid_slab
;
29 static struct kmem_cache
*nat_entry_set_slab
;
31 bool available_free_memory(struct f2fs_sb_info
*sbi
, int type
)
33 struct f2fs_nm_info
*nm_i
= NM_I(sbi
);
35 unsigned long avail_ram
;
36 unsigned long mem_size
= 0;
41 /* only uses low memory */
42 avail_ram
= val
.totalram
- val
.totalhigh
;
45 * give 25%, 25%, 50%, 50%, 50% memory for each components respectively
47 if (type
== FREE_NIDS
) {
48 mem_size
= (nm_i
->fcnt
* sizeof(struct free_nid
)) >>
50 res
= mem_size
< ((avail_ram
* nm_i
->ram_thresh
/ 100) >> 2);
51 } else if (type
== NAT_ENTRIES
) {
52 mem_size
= (nm_i
->nat_cnt
* sizeof(struct nat_entry
)) >>
54 res
= mem_size
< ((avail_ram
* nm_i
->ram_thresh
/ 100) >> 2);
55 } else if (type
== DIRTY_DENTS
) {
56 if (sbi
->sb
->s_bdi
->wb
.dirty_exceeded
)
58 mem_size
= get_pages(sbi
, F2FS_DIRTY_DENTS
);
59 res
= mem_size
< ((avail_ram
* nm_i
->ram_thresh
/ 100) >> 1);
60 } else if (type
== INO_ENTRIES
) {
63 for (i
= 0; i
<= UPDATE_INO
; i
++)
64 mem_size
+= (sbi
->im
[i
].ino_num
*
65 sizeof(struct ino_entry
)) >> PAGE_CACHE_SHIFT
;
66 res
= mem_size
< ((avail_ram
* nm_i
->ram_thresh
/ 100) >> 1);
67 } else if (type
== EXTENT_CACHE
) {
68 mem_size
= (sbi
->total_ext_tree
* sizeof(struct extent_tree
) +
69 atomic_read(&sbi
->total_ext_node
) *
70 sizeof(struct extent_node
)) >> PAGE_CACHE_SHIFT
;
71 res
= mem_size
< ((avail_ram
* nm_i
->ram_thresh
/ 100) >> 1);
73 if (sbi
->sb
->s_bdi
->wb
.dirty_exceeded
)
79 static void clear_node_page_dirty(struct page
*page
)
81 struct address_space
*mapping
= page
->mapping
;
82 unsigned int long flags
;
84 if (PageDirty(page
)) {
85 spin_lock_irqsave(&mapping
->tree_lock
, flags
);
86 radix_tree_tag_clear(&mapping
->page_tree
,
89 spin_unlock_irqrestore(&mapping
->tree_lock
, flags
);
91 clear_page_dirty_for_io(page
);
92 dec_page_count(F2FS_M_SB(mapping
), F2FS_DIRTY_NODES
);
94 ClearPageUptodate(page
);
97 static struct page
*get_current_nat_page(struct f2fs_sb_info
*sbi
, nid_t nid
)
99 pgoff_t index
= current_nat_addr(sbi
, nid
);
100 return get_meta_page(sbi
, index
);
103 static struct page
*get_next_nat_page(struct f2fs_sb_info
*sbi
, nid_t nid
)
105 struct page
*src_page
;
106 struct page
*dst_page
;
111 struct f2fs_nm_info
*nm_i
= NM_I(sbi
);
113 src_off
= current_nat_addr(sbi
, nid
);
114 dst_off
= next_nat_addr(sbi
, src_off
);
116 /* get current nat block page with lock */
117 src_page
= get_meta_page(sbi
, src_off
);
118 dst_page
= grab_meta_page(sbi
, dst_off
);
119 f2fs_bug_on(sbi
, PageDirty(src_page
));
121 src_addr
= page_address(src_page
);
122 dst_addr
= page_address(dst_page
);
123 memcpy(dst_addr
, src_addr
, PAGE_CACHE_SIZE
);
124 set_page_dirty(dst_page
);
125 f2fs_put_page(src_page
, 1);
127 set_to_next_nat(nm_i
, nid
);
132 static struct nat_entry
*__lookup_nat_cache(struct f2fs_nm_info
*nm_i
, nid_t n
)
134 return radix_tree_lookup(&nm_i
->nat_root
, n
);
137 static unsigned int __gang_lookup_nat_cache(struct f2fs_nm_info
*nm_i
,
138 nid_t start
, unsigned int nr
, struct nat_entry
**ep
)
140 return radix_tree_gang_lookup(&nm_i
->nat_root
, (void **)ep
, start
, nr
);
143 static void __del_from_nat_cache(struct f2fs_nm_info
*nm_i
, struct nat_entry
*e
)
146 radix_tree_delete(&nm_i
->nat_root
, nat_get_nid(e
));
148 kmem_cache_free(nat_entry_slab
, e
);
151 static void __set_nat_cache_dirty(struct f2fs_nm_info
*nm_i
,
152 struct nat_entry
*ne
)
154 nid_t set
= NAT_BLOCK_OFFSET(ne
->ni
.nid
);
155 struct nat_entry_set
*head
;
157 if (get_nat_flag(ne
, IS_DIRTY
))
160 head
= radix_tree_lookup(&nm_i
->nat_set_root
, set
);
162 head
= f2fs_kmem_cache_alloc(nat_entry_set_slab
, GFP_ATOMIC
);
164 INIT_LIST_HEAD(&head
->entry_list
);
165 INIT_LIST_HEAD(&head
->set_list
);
168 f2fs_radix_tree_insert(&nm_i
->nat_set_root
, set
, head
);
170 list_move_tail(&ne
->list
, &head
->entry_list
);
171 nm_i
->dirty_nat_cnt
++;
173 set_nat_flag(ne
, IS_DIRTY
, true);
176 static void __clear_nat_cache_dirty(struct f2fs_nm_info
*nm_i
,
177 struct nat_entry
*ne
)
179 nid_t set
= NAT_BLOCK_OFFSET(ne
->ni
.nid
);
180 struct nat_entry_set
*head
;
182 head
= radix_tree_lookup(&nm_i
->nat_set_root
, set
);
184 list_move_tail(&ne
->list
, &nm_i
->nat_entries
);
185 set_nat_flag(ne
, IS_DIRTY
, false);
187 nm_i
->dirty_nat_cnt
--;
191 static unsigned int __gang_lookup_nat_set(struct f2fs_nm_info
*nm_i
,
192 nid_t start
, unsigned int nr
, struct nat_entry_set
**ep
)
194 return radix_tree_gang_lookup(&nm_i
->nat_set_root
, (void **)ep
,
198 int need_dentry_mark(struct f2fs_sb_info
*sbi
, nid_t nid
)
200 struct f2fs_nm_info
*nm_i
= NM_I(sbi
);
204 down_read(&nm_i
->nat_tree_lock
);
205 e
= __lookup_nat_cache(nm_i
, nid
);
207 if (!get_nat_flag(e
, IS_CHECKPOINTED
) &&
208 !get_nat_flag(e
, HAS_FSYNCED_INODE
))
211 up_read(&nm_i
->nat_tree_lock
);
215 bool is_checkpointed_node(struct f2fs_sb_info
*sbi
, nid_t nid
)
217 struct f2fs_nm_info
*nm_i
= NM_I(sbi
);
221 down_read(&nm_i
->nat_tree_lock
);
222 e
= __lookup_nat_cache(nm_i
, nid
);
223 if (e
&& !get_nat_flag(e
, IS_CHECKPOINTED
))
225 up_read(&nm_i
->nat_tree_lock
);
229 bool need_inode_block_update(struct f2fs_sb_info
*sbi
, nid_t ino
)
231 struct f2fs_nm_info
*nm_i
= NM_I(sbi
);
233 bool need_update
= true;
235 down_read(&nm_i
->nat_tree_lock
);
236 e
= __lookup_nat_cache(nm_i
, ino
);
237 if (e
&& get_nat_flag(e
, HAS_LAST_FSYNC
) &&
238 (get_nat_flag(e
, IS_CHECKPOINTED
) ||
239 get_nat_flag(e
, HAS_FSYNCED_INODE
)))
241 up_read(&nm_i
->nat_tree_lock
);
245 static struct nat_entry
*grab_nat_entry(struct f2fs_nm_info
*nm_i
, nid_t nid
)
247 struct nat_entry
*new;
249 new = f2fs_kmem_cache_alloc(nat_entry_slab
, GFP_ATOMIC
);
250 f2fs_radix_tree_insert(&nm_i
->nat_root
, nid
, new);
251 memset(new, 0, sizeof(struct nat_entry
));
252 nat_set_nid(new, nid
);
254 list_add_tail(&new->list
, &nm_i
->nat_entries
);
259 static void cache_nat_entry(struct f2fs_nm_info
*nm_i
, nid_t nid
,
260 struct f2fs_nat_entry
*ne
)
264 down_write(&nm_i
->nat_tree_lock
);
265 e
= __lookup_nat_cache(nm_i
, nid
);
267 e
= grab_nat_entry(nm_i
, nid
);
268 node_info_from_raw_nat(&e
->ni
, ne
);
270 up_write(&nm_i
->nat_tree_lock
);
273 static void set_node_addr(struct f2fs_sb_info
*sbi
, struct node_info
*ni
,
274 block_t new_blkaddr
, bool fsync_done
)
276 struct f2fs_nm_info
*nm_i
= NM_I(sbi
);
279 down_write(&nm_i
->nat_tree_lock
);
280 e
= __lookup_nat_cache(nm_i
, ni
->nid
);
282 e
= grab_nat_entry(nm_i
, ni
->nid
);
283 copy_node_info(&e
->ni
, ni
);
284 f2fs_bug_on(sbi
, ni
->blk_addr
== NEW_ADDR
);
285 } else if (new_blkaddr
== NEW_ADDR
) {
287 * when nid is reallocated,
288 * previous nat entry can be remained in nat cache.
289 * So, reinitialize it with new information.
291 copy_node_info(&e
->ni
, ni
);
292 f2fs_bug_on(sbi
, ni
->blk_addr
!= NULL_ADDR
);
296 f2fs_bug_on(sbi
, nat_get_blkaddr(e
) != ni
->blk_addr
);
297 f2fs_bug_on(sbi
, nat_get_blkaddr(e
) == NULL_ADDR
&&
298 new_blkaddr
== NULL_ADDR
);
299 f2fs_bug_on(sbi
, nat_get_blkaddr(e
) == NEW_ADDR
&&
300 new_blkaddr
== NEW_ADDR
);
301 f2fs_bug_on(sbi
, nat_get_blkaddr(e
) != NEW_ADDR
&&
302 nat_get_blkaddr(e
) != NULL_ADDR
&&
303 new_blkaddr
== NEW_ADDR
);
305 /* increment version no as node is removed */
306 if (nat_get_blkaddr(e
) != NEW_ADDR
&& new_blkaddr
== NULL_ADDR
) {
307 unsigned char version
= nat_get_version(e
);
308 nat_set_version(e
, inc_node_version(version
));
312 nat_set_blkaddr(e
, new_blkaddr
);
313 if (new_blkaddr
== NEW_ADDR
|| new_blkaddr
== NULL_ADDR
)
314 set_nat_flag(e
, IS_CHECKPOINTED
, false);
315 __set_nat_cache_dirty(nm_i
, e
);
317 /* update fsync_mark if its inode nat entry is still alive */
318 if (ni
->nid
!= ni
->ino
)
319 e
= __lookup_nat_cache(nm_i
, ni
->ino
);
321 if (fsync_done
&& ni
->nid
== ni
->ino
)
322 set_nat_flag(e
, HAS_FSYNCED_INODE
, true);
323 set_nat_flag(e
, HAS_LAST_FSYNC
, fsync_done
);
325 up_write(&nm_i
->nat_tree_lock
);
328 int try_to_free_nats(struct f2fs_sb_info
*sbi
, int nr_shrink
)
330 struct f2fs_nm_info
*nm_i
= NM_I(sbi
);
332 if (available_free_memory(sbi
, NAT_ENTRIES
))
335 down_write(&nm_i
->nat_tree_lock
);
336 while (nr_shrink
&& !list_empty(&nm_i
->nat_entries
)) {
337 struct nat_entry
*ne
;
338 ne
= list_first_entry(&nm_i
->nat_entries
,
339 struct nat_entry
, list
);
340 __del_from_nat_cache(nm_i
, ne
);
343 up_write(&nm_i
->nat_tree_lock
);
348 * This function always returns success
350 void get_node_info(struct f2fs_sb_info
*sbi
, nid_t nid
, struct node_info
*ni
)
352 struct f2fs_nm_info
*nm_i
= NM_I(sbi
);
353 struct curseg_info
*curseg
= CURSEG_I(sbi
, CURSEG_HOT_DATA
);
354 struct f2fs_summary_block
*sum
= curseg
->sum_blk
;
355 nid_t start_nid
= START_NID(nid
);
356 struct f2fs_nat_block
*nat_blk
;
357 struct page
*page
= NULL
;
358 struct f2fs_nat_entry ne
;
364 /* Check nat cache */
365 down_read(&nm_i
->nat_tree_lock
);
366 e
= __lookup_nat_cache(nm_i
, nid
);
368 ni
->ino
= nat_get_ino(e
);
369 ni
->blk_addr
= nat_get_blkaddr(e
);
370 ni
->version
= nat_get_version(e
);
372 up_read(&nm_i
->nat_tree_lock
);
376 memset(&ne
, 0, sizeof(struct f2fs_nat_entry
));
378 /* Check current segment summary */
379 mutex_lock(&curseg
->curseg_mutex
);
380 i
= lookup_journal_in_cursum(sum
, NAT_JOURNAL
, nid
, 0);
382 ne
= nat_in_journal(sum
, i
);
383 node_info_from_raw_nat(ni
, &ne
);
385 mutex_unlock(&curseg
->curseg_mutex
);
389 /* Fill node_info from nat page */
390 page
= get_current_nat_page(sbi
, start_nid
);
391 nat_blk
= (struct f2fs_nat_block
*)page_address(page
);
392 ne
= nat_blk
->entries
[nid
- start_nid
];
393 node_info_from_raw_nat(ni
, &ne
);
394 f2fs_put_page(page
, 1);
396 /* cache nat entry */
397 cache_nat_entry(NM_I(sbi
), nid
, &ne
);
401 * The maximum depth is four.
402 * Offset[0] will have raw inode offset.
404 static int get_node_path(struct f2fs_inode_info
*fi
, long block
,
405 int offset
[4], unsigned int noffset
[4])
407 const long direct_index
= ADDRS_PER_INODE(fi
);
408 const long direct_blks
= ADDRS_PER_BLOCK
;
409 const long dptrs_per_blk
= NIDS_PER_BLOCK
;
410 const long indirect_blks
= ADDRS_PER_BLOCK
* NIDS_PER_BLOCK
;
411 const long dindirect_blks
= indirect_blks
* NIDS_PER_BLOCK
;
417 if (block
< direct_index
) {
421 block
-= direct_index
;
422 if (block
< direct_blks
) {
423 offset
[n
++] = NODE_DIR1_BLOCK
;
429 block
-= direct_blks
;
430 if (block
< direct_blks
) {
431 offset
[n
++] = NODE_DIR2_BLOCK
;
437 block
-= direct_blks
;
438 if (block
< indirect_blks
) {
439 offset
[n
++] = NODE_IND1_BLOCK
;
441 offset
[n
++] = block
/ direct_blks
;
442 noffset
[n
] = 4 + offset
[n
- 1];
443 offset
[n
] = block
% direct_blks
;
447 block
-= indirect_blks
;
448 if (block
< indirect_blks
) {
449 offset
[n
++] = NODE_IND2_BLOCK
;
450 noffset
[n
] = 4 + dptrs_per_blk
;
451 offset
[n
++] = block
/ direct_blks
;
452 noffset
[n
] = 5 + dptrs_per_blk
+ offset
[n
- 1];
453 offset
[n
] = block
% direct_blks
;
457 block
-= indirect_blks
;
458 if (block
< dindirect_blks
) {
459 offset
[n
++] = NODE_DIND_BLOCK
;
460 noffset
[n
] = 5 + (dptrs_per_blk
* 2);
461 offset
[n
++] = block
/ indirect_blks
;
462 noffset
[n
] = 6 + (dptrs_per_blk
* 2) +
463 offset
[n
- 1] * (dptrs_per_blk
+ 1);
464 offset
[n
++] = (block
/ direct_blks
) % dptrs_per_blk
;
465 noffset
[n
] = 7 + (dptrs_per_blk
* 2) +
466 offset
[n
- 2] * (dptrs_per_blk
+ 1) +
468 offset
[n
] = block
% direct_blks
;
479 * Caller should call f2fs_put_dnode(dn).
480 * Also, it should grab and release a rwsem by calling f2fs_lock_op() and
481 * f2fs_unlock_op() only if ro is not set RDONLY_NODE.
482 * In the case of RDONLY_NODE, we don't need to care about mutex.
484 int get_dnode_of_data(struct dnode_of_data
*dn
, pgoff_t index
, int mode
)
486 struct f2fs_sb_info
*sbi
= F2FS_I_SB(dn
->inode
);
487 struct page
*npage
[4];
488 struct page
*parent
= NULL
;
490 unsigned int noffset
[4];
495 level
= get_node_path(F2FS_I(dn
->inode
), index
, offset
, noffset
);
497 nids
[0] = dn
->inode
->i_ino
;
498 npage
[0] = dn
->inode_page
;
501 npage
[0] = get_node_page(sbi
, nids
[0]);
502 if (IS_ERR(npage
[0]))
503 return PTR_ERR(npage
[0]);
506 /* if inline_data is set, should not report any block indices */
507 if (f2fs_has_inline_data(dn
->inode
) && index
) {
509 f2fs_put_page(npage
[0], 1);
515 nids
[1] = get_nid(parent
, offset
[0], true);
516 dn
->inode_page
= npage
[0];
517 dn
->inode_page_locked
= true;
519 /* get indirect or direct nodes */
520 for (i
= 1; i
<= level
; i
++) {
523 if (!nids
[i
] && mode
== ALLOC_NODE
) {
525 if (!alloc_nid(sbi
, &(nids
[i
]))) {
531 npage
[i
] = new_node_page(dn
, noffset
[i
], NULL
);
532 if (IS_ERR(npage
[i
])) {
533 alloc_nid_failed(sbi
, nids
[i
]);
534 err
= PTR_ERR(npage
[i
]);
538 set_nid(parent
, offset
[i
- 1], nids
[i
], i
== 1);
539 alloc_nid_done(sbi
, nids
[i
]);
541 } else if (mode
== LOOKUP_NODE_RA
&& i
== level
&& level
> 1) {
542 npage
[i
] = get_node_page_ra(parent
, offset
[i
- 1]);
543 if (IS_ERR(npage
[i
])) {
544 err
= PTR_ERR(npage
[i
]);
550 dn
->inode_page_locked
= false;
553 f2fs_put_page(parent
, 1);
557 npage
[i
] = get_node_page(sbi
, nids
[i
]);
558 if (IS_ERR(npage
[i
])) {
559 err
= PTR_ERR(npage
[i
]);
560 f2fs_put_page(npage
[0], 0);
566 nids
[i
+ 1] = get_nid(parent
, offset
[i
], false);
569 dn
->nid
= nids
[level
];
570 dn
->ofs_in_node
= offset
[level
];
571 dn
->node_page
= npage
[level
];
572 dn
->data_blkaddr
= datablock_addr(dn
->node_page
, dn
->ofs_in_node
);
576 f2fs_put_page(parent
, 1);
578 f2fs_put_page(npage
[0], 0);
580 dn
->inode_page
= NULL
;
581 dn
->node_page
= NULL
;
585 static void truncate_node(struct dnode_of_data
*dn
)
587 struct f2fs_sb_info
*sbi
= F2FS_I_SB(dn
->inode
);
590 get_node_info(sbi
, dn
->nid
, &ni
);
591 if (dn
->inode
->i_blocks
== 0) {
592 f2fs_bug_on(sbi
, ni
.blk_addr
!= NULL_ADDR
);
595 f2fs_bug_on(sbi
, ni
.blk_addr
== NULL_ADDR
);
597 /* Deallocate node address */
598 invalidate_blocks(sbi
, ni
.blk_addr
);
599 dec_valid_node_count(sbi
, dn
->inode
);
600 set_node_addr(sbi
, &ni
, NULL_ADDR
, false);
602 if (dn
->nid
== dn
->inode
->i_ino
) {
603 remove_orphan_inode(sbi
, dn
->nid
);
604 dec_valid_inode_count(sbi
);
609 clear_node_page_dirty(dn
->node_page
);
610 set_sbi_flag(sbi
, SBI_IS_DIRTY
);
612 f2fs_put_page(dn
->node_page
, 1);
614 invalidate_mapping_pages(NODE_MAPPING(sbi
),
615 dn
->node_page
->index
, dn
->node_page
->index
);
617 dn
->node_page
= NULL
;
618 trace_f2fs_truncate_node(dn
->inode
, dn
->nid
, ni
.blk_addr
);
621 static int truncate_dnode(struct dnode_of_data
*dn
)
628 /* get direct node */
629 page
= get_node_page(F2FS_I_SB(dn
->inode
), dn
->nid
);
630 if (IS_ERR(page
) && PTR_ERR(page
) == -ENOENT
)
632 else if (IS_ERR(page
))
633 return PTR_ERR(page
);
635 /* Make dnode_of_data for parameter */
636 dn
->node_page
= page
;
638 truncate_data_blocks(dn
);
643 static int truncate_nodes(struct dnode_of_data
*dn
, unsigned int nofs
,
646 struct dnode_of_data rdn
= *dn
;
648 struct f2fs_node
*rn
;
650 unsigned int child_nofs
;
655 return NIDS_PER_BLOCK
+ 1;
657 trace_f2fs_truncate_nodes_enter(dn
->inode
, dn
->nid
, dn
->data_blkaddr
);
659 page
= get_node_page(F2FS_I_SB(dn
->inode
), dn
->nid
);
661 trace_f2fs_truncate_nodes_exit(dn
->inode
, PTR_ERR(page
));
662 return PTR_ERR(page
);
665 rn
= F2FS_NODE(page
);
667 for (i
= ofs
; i
< NIDS_PER_BLOCK
; i
++, freed
++) {
668 child_nid
= le32_to_cpu(rn
->in
.nid
[i
]);
672 ret
= truncate_dnode(&rdn
);
675 set_nid(page
, i
, 0, false);
678 child_nofs
= nofs
+ ofs
* (NIDS_PER_BLOCK
+ 1) + 1;
679 for (i
= ofs
; i
< NIDS_PER_BLOCK
; i
++) {
680 child_nid
= le32_to_cpu(rn
->in
.nid
[i
]);
681 if (child_nid
== 0) {
682 child_nofs
+= NIDS_PER_BLOCK
+ 1;
686 ret
= truncate_nodes(&rdn
, child_nofs
, 0, depth
- 1);
687 if (ret
== (NIDS_PER_BLOCK
+ 1)) {
688 set_nid(page
, i
, 0, false);
690 } else if (ret
< 0 && ret
!= -ENOENT
) {
698 /* remove current indirect node */
699 dn
->node_page
= page
;
703 f2fs_put_page(page
, 1);
705 trace_f2fs_truncate_nodes_exit(dn
->inode
, freed
);
709 f2fs_put_page(page
, 1);
710 trace_f2fs_truncate_nodes_exit(dn
->inode
, ret
);
714 static int truncate_partial_nodes(struct dnode_of_data
*dn
,
715 struct f2fs_inode
*ri
, int *offset
, int depth
)
717 struct page
*pages
[2];
724 nid
[0] = le32_to_cpu(ri
->i_nid
[offset
[0] - NODE_DIR1_BLOCK
]);
728 /* get indirect nodes in the path */
729 for (i
= 0; i
< idx
+ 1; i
++) {
730 /* reference count'll be increased */
731 pages
[i
] = get_node_page(F2FS_I_SB(dn
->inode
), nid
[i
]);
732 if (IS_ERR(pages
[i
])) {
733 err
= PTR_ERR(pages
[i
]);
737 nid
[i
+ 1] = get_nid(pages
[i
], offset
[i
+ 1], false);
740 /* free direct nodes linked to a partial indirect node */
741 for (i
= offset
[idx
+ 1]; i
< NIDS_PER_BLOCK
; i
++) {
742 child_nid
= get_nid(pages
[idx
], i
, false);
746 err
= truncate_dnode(dn
);
749 set_nid(pages
[idx
], i
, 0, false);
752 if (offset
[idx
+ 1] == 0) {
753 dn
->node_page
= pages
[idx
];
757 f2fs_put_page(pages
[idx
], 1);
763 for (i
= idx
; i
>= 0; i
--)
764 f2fs_put_page(pages
[i
], 1);
766 trace_f2fs_truncate_partial_nodes(dn
->inode
, nid
, depth
, err
);
772 * All the block addresses of data and nodes should be nullified.
774 int truncate_inode_blocks(struct inode
*inode
, pgoff_t from
)
776 struct f2fs_sb_info
*sbi
= F2FS_I_SB(inode
);
777 int err
= 0, cont
= 1;
778 int level
, offset
[4], noffset
[4];
779 unsigned int nofs
= 0;
780 struct f2fs_inode
*ri
;
781 struct dnode_of_data dn
;
784 trace_f2fs_truncate_inode_blocks_enter(inode
, from
);
786 level
= get_node_path(F2FS_I(inode
), from
, offset
, noffset
);
788 page
= get_node_page(sbi
, inode
->i_ino
);
790 trace_f2fs_truncate_inode_blocks_exit(inode
, PTR_ERR(page
));
791 return PTR_ERR(page
);
794 set_new_dnode(&dn
, inode
, page
, NULL
, 0);
797 ri
= F2FS_INODE(page
);
805 if (!offset
[level
- 1])
807 err
= truncate_partial_nodes(&dn
, ri
, offset
, level
);
808 if (err
< 0 && err
!= -ENOENT
)
810 nofs
+= 1 + NIDS_PER_BLOCK
;
813 nofs
= 5 + 2 * NIDS_PER_BLOCK
;
814 if (!offset
[level
- 1])
816 err
= truncate_partial_nodes(&dn
, ri
, offset
, level
);
817 if (err
< 0 && err
!= -ENOENT
)
826 dn
.nid
= le32_to_cpu(ri
->i_nid
[offset
[0] - NODE_DIR1_BLOCK
]);
828 case NODE_DIR1_BLOCK
:
829 case NODE_DIR2_BLOCK
:
830 err
= truncate_dnode(&dn
);
833 case NODE_IND1_BLOCK
:
834 case NODE_IND2_BLOCK
:
835 err
= truncate_nodes(&dn
, nofs
, offset
[1], 2);
838 case NODE_DIND_BLOCK
:
839 err
= truncate_nodes(&dn
, nofs
, offset
[1], 3);
846 if (err
< 0 && err
!= -ENOENT
)
848 if (offset
[1] == 0 &&
849 ri
->i_nid
[offset
[0] - NODE_DIR1_BLOCK
]) {
851 if (unlikely(page
->mapping
!= NODE_MAPPING(sbi
))) {
852 f2fs_put_page(page
, 1);
855 f2fs_wait_on_page_writeback(page
, NODE
);
856 ri
->i_nid
[offset
[0] - NODE_DIR1_BLOCK
] = 0;
857 set_page_dirty(page
);
865 f2fs_put_page(page
, 0);
866 trace_f2fs_truncate_inode_blocks_exit(inode
, err
);
867 return err
> 0 ? 0 : err
;
870 int truncate_xattr_node(struct inode
*inode
, struct page
*page
)
872 struct f2fs_sb_info
*sbi
= F2FS_I_SB(inode
);
873 nid_t nid
= F2FS_I(inode
)->i_xattr_nid
;
874 struct dnode_of_data dn
;
880 npage
= get_node_page(sbi
, nid
);
882 return PTR_ERR(npage
);
884 F2FS_I(inode
)->i_xattr_nid
= 0;
886 /* need to do checkpoint during fsync */
887 F2FS_I(inode
)->xattr_ver
= cur_cp_version(F2FS_CKPT(sbi
));
889 set_new_dnode(&dn
, inode
, page
, npage
, nid
);
892 dn
.inode_page_locked
= true;
898 * Caller should grab and release a rwsem by calling f2fs_lock_op() and
901 void remove_inode_page(struct inode
*inode
)
903 struct dnode_of_data dn
;
905 set_new_dnode(&dn
, inode
, NULL
, NULL
, inode
->i_ino
);
906 if (get_dnode_of_data(&dn
, 0, LOOKUP_NODE
))
909 if (truncate_xattr_node(inode
, dn
.inode_page
)) {
914 /* remove potential inline_data blocks */
915 if (S_ISREG(inode
->i_mode
) || S_ISDIR(inode
->i_mode
) ||
916 S_ISLNK(inode
->i_mode
))
917 truncate_data_blocks_range(&dn
, 1);
919 /* 0 is possible, after f2fs_new_inode() has failed */
920 f2fs_bug_on(F2FS_I_SB(inode
),
921 inode
->i_blocks
!= 0 && inode
->i_blocks
!= 1);
923 /* will put inode & node pages */
927 struct page
*new_inode_page(struct inode
*inode
)
929 struct dnode_of_data dn
;
931 /* allocate inode page for new inode */
932 set_new_dnode(&dn
, inode
, NULL
, NULL
, inode
->i_ino
);
934 /* caller should f2fs_put_page(page, 1); */
935 return new_node_page(&dn
, 0, NULL
);
938 struct page
*new_node_page(struct dnode_of_data
*dn
,
939 unsigned int ofs
, struct page
*ipage
)
941 struct f2fs_sb_info
*sbi
= F2FS_I_SB(dn
->inode
);
942 struct node_info old_ni
, new_ni
;
946 if (unlikely(is_inode_flag_set(F2FS_I(dn
->inode
), FI_NO_ALLOC
)))
947 return ERR_PTR(-EPERM
);
949 page
= grab_cache_page(NODE_MAPPING(sbi
), dn
->nid
);
951 return ERR_PTR(-ENOMEM
);
953 if (unlikely(!inc_valid_node_count(sbi
, dn
->inode
))) {
958 get_node_info(sbi
, dn
->nid
, &old_ni
);
960 /* Reinitialize old_ni with new node page */
961 f2fs_bug_on(sbi
, old_ni
.blk_addr
!= NULL_ADDR
);
963 new_ni
.ino
= dn
->inode
->i_ino
;
964 set_node_addr(sbi
, &new_ni
, NEW_ADDR
, false);
966 f2fs_wait_on_page_writeback(page
, NODE
);
967 fill_node_footer(page
, dn
->nid
, dn
->inode
->i_ino
, ofs
, true);
968 set_cold_node(dn
->inode
, page
);
969 SetPageUptodate(page
);
970 set_page_dirty(page
);
972 if (f2fs_has_xattr_block(ofs
))
973 F2FS_I(dn
->inode
)->i_xattr_nid
= dn
->nid
;
975 dn
->node_page
= page
;
977 update_inode(dn
->inode
, ipage
);
981 inc_valid_inode_count(sbi
);
986 clear_node_page_dirty(page
);
987 f2fs_put_page(page
, 1);
992 * Caller should do after getting the following values.
993 * 0: f2fs_put_page(page, 0)
994 * LOCKED_PAGE: f2fs_put_page(page, 1)
997 static int read_node_page(struct page
*page
, int rw
)
999 struct f2fs_sb_info
*sbi
= F2FS_P_SB(page
);
1000 struct node_info ni
;
1001 struct f2fs_io_info fio
= {
1006 .encrypted_page
= NULL
,
1009 get_node_info(sbi
, page
->index
, &ni
);
1011 if (unlikely(ni
.blk_addr
== NULL_ADDR
)) {
1012 ClearPageUptodate(page
);
1013 f2fs_put_page(page
, 1);
1017 if (PageUptodate(page
))
1020 fio
.blk_addr
= ni
.blk_addr
;
1021 return f2fs_submit_page_bio(&fio
);
1025 * Readahead a node page
1027 void ra_node_page(struct f2fs_sb_info
*sbi
, nid_t nid
)
1032 apage
= find_get_page(NODE_MAPPING(sbi
), nid
);
1033 if (apage
&& PageUptodate(apage
)) {
1034 f2fs_put_page(apage
, 0);
1037 f2fs_put_page(apage
, 0);
1039 apage
= grab_cache_page(NODE_MAPPING(sbi
), nid
);
1043 err
= read_node_page(apage
, READA
);
1045 f2fs_put_page(apage
, 0);
1046 else if (err
== LOCKED_PAGE
)
1047 f2fs_put_page(apage
, 1);
1050 struct page
*get_node_page(struct f2fs_sb_info
*sbi
, pgoff_t nid
)
1055 page
= grab_cache_page(NODE_MAPPING(sbi
), nid
);
1057 return ERR_PTR(-ENOMEM
);
1059 err
= read_node_page(page
, READ_SYNC
);
1061 return ERR_PTR(err
);
1062 else if (err
!= LOCKED_PAGE
)
1065 if (unlikely(!PageUptodate(page
) || nid
!= nid_of_node(page
))) {
1066 ClearPageUptodate(page
);
1067 f2fs_put_page(page
, 1);
1068 return ERR_PTR(-EIO
);
1070 if (unlikely(page
->mapping
!= NODE_MAPPING(sbi
))) {
1071 f2fs_put_page(page
, 1);
1078 * Return a locked page for the desired node page.
1079 * And, readahead MAX_RA_NODE number of node pages.
1081 struct page
*get_node_page_ra(struct page
*parent
, int start
)
1083 struct f2fs_sb_info
*sbi
= F2FS_P_SB(parent
);
1084 struct blk_plug plug
;
1089 /* First, try getting the desired direct node. */
1090 nid
= get_nid(parent
, start
, false);
1092 return ERR_PTR(-ENOENT
);
1094 page
= grab_cache_page(NODE_MAPPING(sbi
), nid
);
1096 return ERR_PTR(-ENOMEM
);
1098 err
= read_node_page(page
, READ_SYNC
);
1100 return ERR_PTR(err
);
1101 else if (err
== LOCKED_PAGE
)
1104 blk_start_plug(&plug
);
1106 /* Then, try readahead for siblings of the desired node */
1107 end
= start
+ MAX_RA_NODE
;
1108 end
= min(end
, NIDS_PER_BLOCK
);
1109 for (i
= start
+ 1; i
< end
; i
++) {
1110 nid
= get_nid(parent
, i
, false);
1113 ra_node_page(sbi
, nid
);
1116 blk_finish_plug(&plug
);
1119 if (unlikely(page
->mapping
!= NODE_MAPPING(sbi
))) {
1120 f2fs_put_page(page
, 1);
1124 if (unlikely(!PageUptodate(page
))) {
1125 f2fs_put_page(page
, 1);
1126 return ERR_PTR(-EIO
);
1131 void sync_inode_page(struct dnode_of_data
*dn
)
1133 if (IS_INODE(dn
->node_page
) || dn
->inode_page
== dn
->node_page
) {
1134 update_inode(dn
->inode
, dn
->node_page
);
1135 } else if (dn
->inode_page
) {
1136 if (!dn
->inode_page_locked
)
1137 lock_page(dn
->inode_page
);
1138 update_inode(dn
->inode
, dn
->inode_page
);
1139 if (!dn
->inode_page_locked
)
1140 unlock_page(dn
->inode_page
);
1142 update_inode_page(dn
->inode
);
1146 int sync_node_pages(struct f2fs_sb_info
*sbi
, nid_t ino
,
1147 struct writeback_control
*wbc
)
1150 struct pagevec pvec
;
1151 int step
= ino
? 2 : 0;
1152 int nwritten
= 0, wrote
= 0;
1154 pagevec_init(&pvec
, 0);
1160 while (index
<= end
) {
1162 nr_pages
= pagevec_lookup_tag(&pvec
, NODE_MAPPING(sbi
), &index
,
1163 PAGECACHE_TAG_DIRTY
,
1164 min(end
- index
, (pgoff_t
)PAGEVEC_SIZE
-1) + 1);
1168 for (i
= 0; i
< nr_pages
; i
++) {
1169 struct page
*page
= pvec
.pages
[i
];
1172 * flushing sequence with step:
1177 if (step
== 0 && IS_DNODE(page
))
1179 if (step
== 1 && (!IS_DNODE(page
) ||
1180 is_cold_node(page
)))
1182 if (step
== 2 && (!IS_DNODE(page
) ||
1183 !is_cold_node(page
)))
1188 * we should not skip writing node pages.
1190 if (ino
&& ino_of_node(page
) == ino
)
1192 else if (!trylock_page(page
))
1195 if (unlikely(page
->mapping
!= NODE_MAPPING(sbi
))) {
1200 if (ino
&& ino_of_node(page
) != ino
)
1201 goto continue_unlock
;
1203 if (!PageDirty(page
)) {
1204 /* someone wrote it for us */
1205 goto continue_unlock
;
1208 if (!clear_page_dirty_for_io(page
))
1209 goto continue_unlock
;
1211 /* called by fsync() */
1212 if (ino
&& IS_DNODE(page
)) {
1213 set_fsync_mark(page
, 1);
1215 set_dentry_mark(page
,
1216 need_dentry_mark(sbi
, ino
));
1219 set_fsync_mark(page
, 0);
1220 set_dentry_mark(page
, 0);
1223 if (NODE_MAPPING(sbi
)->a_ops
->writepage(page
, wbc
))
1228 if (--wbc
->nr_to_write
== 0)
1231 pagevec_release(&pvec
);
1234 if (wbc
->nr_to_write
== 0) {
1246 f2fs_submit_merged_bio(sbi
, NODE
, WRITE
);
1250 int wait_on_node_pages_writeback(struct f2fs_sb_info
*sbi
, nid_t ino
)
1252 pgoff_t index
= 0, end
= LONG_MAX
;
1253 struct pagevec pvec
;
1254 int ret2
= 0, ret
= 0;
1256 pagevec_init(&pvec
, 0);
1258 while (index
<= end
) {
1260 nr_pages
= pagevec_lookup_tag(&pvec
, NODE_MAPPING(sbi
), &index
,
1261 PAGECACHE_TAG_WRITEBACK
,
1262 min(end
- index
, (pgoff_t
)PAGEVEC_SIZE
-1) + 1);
1266 for (i
= 0; i
< nr_pages
; i
++) {
1267 struct page
*page
= pvec
.pages
[i
];
1269 /* until radix tree lookup accepts end_index */
1270 if (unlikely(page
->index
> end
))
1273 if (ino
&& ino_of_node(page
) == ino
) {
1274 f2fs_wait_on_page_writeback(page
, NODE
);
1275 if (TestClearPageError(page
))
1279 pagevec_release(&pvec
);
1283 if (unlikely(test_and_clear_bit(AS_ENOSPC
, &NODE_MAPPING(sbi
)->flags
)))
1285 if (unlikely(test_and_clear_bit(AS_EIO
, &NODE_MAPPING(sbi
)->flags
)))
1292 static int f2fs_write_node_page(struct page
*page
,
1293 struct writeback_control
*wbc
)
1295 struct f2fs_sb_info
*sbi
= F2FS_P_SB(page
);
1297 struct node_info ni
;
1298 struct f2fs_io_info fio
= {
1301 .rw
= (wbc
->sync_mode
== WB_SYNC_ALL
) ? WRITE_SYNC
: WRITE
,
1303 .encrypted_page
= NULL
,
1306 trace_f2fs_writepage(page
, NODE
);
1308 if (unlikely(is_sbi_flag_set(sbi
, SBI_POR_DOING
)))
1310 if (unlikely(f2fs_cp_error(sbi
)))
1313 f2fs_wait_on_page_writeback(page
, NODE
);
1315 /* get old block addr of this node page */
1316 nid
= nid_of_node(page
);
1317 f2fs_bug_on(sbi
, page
->index
!= nid
);
1319 get_node_info(sbi
, nid
, &ni
);
1321 /* This page is already truncated */
1322 if (unlikely(ni
.blk_addr
== NULL_ADDR
)) {
1323 ClearPageUptodate(page
);
1324 dec_page_count(sbi
, F2FS_DIRTY_NODES
);
1329 if (wbc
->for_reclaim
) {
1330 if (!down_read_trylock(&sbi
->node_write
))
1333 down_read(&sbi
->node_write
);
1336 set_page_writeback(page
);
1337 fio
.blk_addr
= ni
.blk_addr
;
1338 write_node_page(nid
, &fio
);
1339 set_node_addr(sbi
, &ni
, fio
.blk_addr
, is_fsync_dnode(page
));
1340 dec_page_count(sbi
, F2FS_DIRTY_NODES
);
1341 up_read(&sbi
->node_write
);
1344 if (wbc
->for_reclaim
)
1345 f2fs_submit_merged_bio(sbi
, NODE
, WRITE
);
1350 redirty_page_for_writepage(wbc
, page
);
1351 return AOP_WRITEPAGE_ACTIVATE
;
1354 static int f2fs_write_node_pages(struct address_space
*mapping
,
1355 struct writeback_control
*wbc
)
1357 struct f2fs_sb_info
*sbi
= F2FS_M_SB(mapping
);
1360 trace_f2fs_writepages(mapping
->host
, wbc
, NODE
);
1362 /* balancing f2fs's metadata in background */
1363 f2fs_balance_fs_bg(sbi
);
1365 /* collect a number of dirty node pages and write together */
1366 if (get_pages(sbi
, F2FS_DIRTY_NODES
) < nr_pages_to_skip(sbi
, NODE
))
1369 diff
= nr_pages_to_write(sbi
, NODE
, wbc
);
1370 wbc
->sync_mode
= WB_SYNC_NONE
;
1371 sync_node_pages(sbi
, 0, wbc
);
1372 wbc
->nr_to_write
= max((long)0, wbc
->nr_to_write
- diff
);
1376 wbc
->pages_skipped
+= get_pages(sbi
, F2FS_DIRTY_NODES
);
1380 static int f2fs_set_node_page_dirty(struct page
*page
)
1382 trace_f2fs_set_page_dirty(page
, NODE
);
1384 SetPageUptodate(page
);
1385 if (!PageDirty(page
)) {
1386 __set_page_dirty_nobuffers(page
);
1387 inc_page_count(F2FS_P_SB(page
), F2FS_DIRTY_NODES
);
1388 SetPagePrivate(page
);
1389 f2fs_trace_pid(page
);
1396 * Structure of the f2fs node operations
1398 const struct address_space_operations f2fs_node_aops
= {
1399 .writepage
= f2fs_write_node_page
,
1400 .writepages
= f2fs_write_node_pages
,
1401 .set_page_dirty
= f2fs_set_node_page_dirty
,
1402 .invalidatepage
= f2fs_invalidate_page
,
1403 .releasepage
= f2fs_release_page
,
1406 static struct free_nid
*__lookup_free_nid_list(struct f2fs_nm_info
*nm_i
,
1409 return radix_tree_lookup(&nm_i
->free_nid_root
, n
);
1412 static void __del_from_free_nid_list(struct f2fs_nm_info
*nm_i
,
1416 radix_tree_delete(&nm_i
->free_nid_root
, i
->nid
);
1419 static int add_free_nid(struct f2fs_sb_info
*sbi
, nid_t nid
, bool build
)
1421 struct f2fs_nm_info
*nm_i
= NM_I(sbi
);
1423 struct nat_entry
*ne
;
1424 bool allocated
= false;
1426 if (!available_free_memory(sbi
, FREE_NIDS
))
1429 /* 0 nid should not be used */
1430 if (unlikely(nid
== 0))
1434 /* do not add allocated nids */
1435 down_read(&nm_i
->nat_tree_lock
);
1436 ne
= __lookup_nat_cache(nm_i
, nid
);
1438 (!get_nat_flag(ne
, IS_CHECKPOINTED
) ||
1439 nat_get_blkaddr(ne
) != NULL_ADDR
))
1441 up_read(&nm_i
->nat_tree_lock
);
1446 i
= f2fs_kmem_cache_alloc(free_nid_slab
, GFP_NOFS
);
1450 if (radix_tree_preload(GFP_NOFS
)) {
1451 kmem_cache_free(free_nid_slab
, i
);
1455 spin_lock(&nm_i
->free_nid_list_lock
);
1456 if (radix_tree_insert(&nm_i
->free_nid_root
, i
->nid
, i
)) {
1457 spin_unlock(&nm_i
->free_nid_list_lock
);
1458 radix_tree_preload_end();
1459 kmem_cache_free(free_nid_slab
, i
);
1462 list_add_tail(&i
->list
, &nm_i
->free_nid_list
);
1464 spin_unlock(&nm_i
->free_nid_list_lock
);
1465 radix_tree_preload_end();
1469 static void remove_free_nid(struct f2fs_nm_info
*nm_i
, nid_t nid
)
1472 bool need_free
= false;
1474 spin_lock(&nm_i
->free_nid_list_lock
);
1475 i
= __lookup_free_nid_list(nm_i
, nid
);
1476 if (i
&& i
->state
== NID_NEW
) {
1477 __del_from_free_nid_list(nm_i
, i
);
1481 spin_unlock(&nm_i
->free_nid_list_lock
);
1484 kmem_cache_free(free_nid_slab
, i
);
1487 static void scan_nat_page(struct f2fs_sb_info
*sbi
,
1488 struct page
*nat_page
, nid_t start_nid
)
1490 struct f2fs_nm_info
*nm_i
= NM_I(sbi
);
1491 struct f2fs_nat_block
*nat_blk
= page_address(nat_page
);
1495 i
= start_nid
% NAT_ENTRY_PER_BLOCK
;
1497 for (; i
< NAT_ENTRY_PER_BLOCK
; i
++, start_nid
++) {
1499 if (unlikely(start_nid
>= nm_i
->max_nid
))
1502 blk_addr
= le32_to_cpu(nat_blk
->entries
[i
].block_addr
);
1503 f2fs_bug_on(sbi
, blk_addr
== NEW_ADDR
);
1504 if (blk_addr
== NULL_ADDR
) {
1505 if (add_free_nid(sbi
, start_nid
, true) < 0)
1511 static void build_free_nids(struct f2fs_sb_info
*sbi
)
1513 struct f2fs_nm_info
*nm_i
= NM_I(sbi
);
1514 struct curseg_info
*curseg
= CURSEG_I(sbi
, CURSEG_HOT_DATA
);
1515 struct f2fs_summary_block
*sum
= curseg
->sum_blk
;
1517 nid_t nid
= nm_i
->next_scan_nid
;
1519 /* Enough entries */
1520 if (nm_i
->fcnt
> NAT_ENTRY_PER_BLOCK
)
1523 /* readahead nat pages to be scanned */
1524 ra_meta_pages(sbi
, NAT_BLOCK_OFFSET(nid
), FREE_NID_PAGES
, META_NAT
);
1527 struct page
*page
= get_current_nat_page(sbi
, nid
);
1529 scan_nat_page(sbi
, page
, nid
);
1530 f2fs_put_page(page
, 1);
1532 nid
+= (NAT_ENTRY_PER_BLOCK
- (nid
% NAT_ENTRY_PER_BLOCK
));
1533 if (unlikely(nid
>= nm_i
->max_nid
))
1536 if (i
++ == FREE_NID_PAGES
)
1540 /* go to the next free nat pages to find free nids abundantly */
1541 nm_i
->next_scan_nid
= nid
;
1543 /* find free nids from current sum_pages */
1544 mutex_lock(&curseg
->curseg_mutex
);
1545 for (i
= 0; i
< nats_in_cursum(sum
); i
++) {
1546 block_t addr
= le32_to_cpu(nat_in_journal(sum
, i
).block_addr
);
1547 nid
= le32_to_cpu(nid_in_journal(sum
, i
));
1548 if (addr
== NULL_ADDR
)
1549 add_free_nid(sbi
, nid
, true);
1551 remove_free_nid(nm_i
, nid
);
1553 mutex_unlock(&curseg
->curseg_mutex
);
1557 * If this function returns success, caller can obtain a new nid
1558 * from second parameter of this function.
1559 * The returned nid could be used ino as well as nid when inode is created.
1561 bool alloc_nid(struct f2fs_sb_info
*sbi
, nid_t
*nid
)
1563 struct f2fs_nm_info
*nm_i
= NM_I(sbi
);
1564 struct free_nid
*i
= NULL
;
1566 if (unlikely(sbi
->total_valid_node_count
+ 1 > nm_i
->available_nids
))
1569 spin_lock(&nm_i
->free_nid_list_lock
);
1571 /* We should not use stale free nids created by build_free_nids */
1572 if (nm_i
->fcnt
&& !on_build_free_nids(nm_i
)) {
1573 f2fs_bug_on(sbi
, list_empty(&nm_i
->free_nid_list
));
1574 list_for_each_entry(i
, &nm_i
->free_nid_list
, list
)
1575 if (i
->state
== NID_NEW
)
1578 f2fs_bug_on(sbi
, i
->state
!= NID_NEW
);
1580 i
->state
= NID_ALLOC
;
1582 spin_unlock(&nm_i
->free_nid_list_lock
);
1585 spin_unlock(&nm_i
->free_nid_list_lock
);
1587 /* Let's scan nat pages and its caches to get free nids */
1588 mutex_lock(&nm_i
->build_lock
);
1589 build_free_nids(sbi
);
1590 mutex_unlock(&nm_i
->build_lock
);
1595 * alloc_nid() should be called prior to this function.
1597 void alloc_nid_done(struct f2fs_sb_info
*sbi
, nid_t nid
)
1599 struct f2fs_nm_info
*nm_i
= NM_I(sbi
);
1602 spin_lock(&nm_i
->free_nid_list_lock
);
1603 i
= __lookup_free_nid_list(nm_i
, nid
);
1604 f2fs_bug_on(sbi
, !i
|| i
->state
!= NID_ALLOC
);
1605 __del_from_free_nid_list(nm_i
, i
);
1606 spin_unlock(&nm_i
->free_nid_list_lock
);
1608 kmem_cache_free(free_nid_slab
, i
);
1612 * alloc_nid() should be called prior to this function.
1614 void alloc_nid_failed(struct f2fs_sb_info
*sbi
, nid_t nid
)
1616 struct f2fs_nm_info
*nm_i
= NM_I(sbi
);
1618 bool need_free
= false;
1623 spin_lock(&nm_i
->free_nid_list_lock
);
1624 i
= __lookup_free_nid_list(nm_i
, nid
);
1625 f2fs_bug_on(sbi
, !i
|| i
->state
!= NID_ALLOC
);
1626 if (!available_free_memory(sbi
, FREE_NIDS
)) {
1627 __del_from_free_nid_list(nm_i
, i
);
1633 spin_unlock(&nm_i
->free_nid_list_lock
);
1636 kmem_cache_free(free_nid_slab
, i
);
1639 void recover_inline_xattr(struct inode
*inode
, struct page
*page
)
1641 void *src_addr
, *dst_addr
;
1644 struct f2fs_inode
*ri
;
1646 ipage
= get_node_page(F2FS_I_SB(inode
), inode
->i_ino
);
1647 f2fs_bug_on(F2FS_I_SB(inode
), IS_ERR(ipage
));
1649 ri
= F2FS_INODE(page
);
1650 if (!(ri
->i_inline
& F2FS_INLINE_XATTR
)) {
1651 clear_inode_flag(F2FS_I(inode
), FI_INLINE_XATTR
);
1655 dst_addr
= inline_xattr_addr(ipage
);
1656 src_addr
= inline_xattr_addr(page
);
1657 inline_size
= inline_xattr_size(inode
);
1659 f2fs_wait_on_page_writeback(ipage
, NODE
);
1660 memcpy(dst_addr
, src_addr
, inline_size
);
1662 update_inode(inode
, ipage
);
1663 f2fs_put_page(ipage
, 1);
1666 void recover_xattr_data(struct inode
*inode
, struct page
*page
, block_t blkaddr
)
1668 struct f2fs_sb_info
*sbi
= F2FS_I_SB(inode
);
1669 nid_t prev_xnid
= F2FS_I(inode
)->i_xattr_nid
;
1670 nid_t new_xnid
= nid_of_node(page
);
1671 struct node_info ni
;
1673 /* 1: invalidate the previous xattr nid */
1677 /* Deallocate node address */
1678 get_node_info(sbi
, prev_xnid
, &ni
);
1679 f2fs_bug_on(sbi
, ni
.blk_addr
== NULL_ADDR
);
1680 invalidate_blocks(sbi
, ni
.blk_addr
);
1681 dec_valid_node_count(sbi
, inode
);
1682 set_node_addr(sbi
, &ni
, NULL_ADDR
, false);
1685 /* 2: allocate new xattr nid */
1686 if (unlikely(!inc_valid_node_count(sbi
, inode
)))
1687 f2fs_bug_on(sbi
, 1);
1689 remove_free_nid(NM_I(sbi
), new_xnid
);
1690 get_node_info(sbi
, new_xnid
, &ni
);
1691 ni
.ino
= inode
->i_ino
;
1692 set_node_addr(sbi
, &ni
, NEW_ADDR
, false);
1693 F2FS_I(inode
)->i_xattr_nid
= new_xnid
;
1695 /* 3: update xattr blkaddr */
1696 refresh_sit_entry(sbi
, NEW_ADDR
, blkaddr
);
1697 set_node_addr(sbi
, &ni
, blkaddr
, false);
1699 update_inode_page(inode
);
1702 int recover_inode_page(struct f2fs_sb_info
*sbi
, struct page
*page
)
1704 struct f2fs_inode
*src
, *dst
;
1705 nid_t ino
= ino_of_node(page
);
1706 struct node_info old_ni
, new_ni
;
1709 get_node_info(sbi
, ino
, &old_ni
);
1711 if (unlikely(old_ni
.blk_addr
!= NULL_ADDR
))
1714 ipage
= grab_cache_page(NODE_MAPPING(sbi
), ino
);
1718 /* Should not use this inode from free nid list */
1719 remove_free_nid(NM_I(sbi
), ino
);
1721 SetPageUptodate(ipage
);
1722 fill_node_footer(ipage
, ino
, ino
, 0, true);
1724 src
= F2FS_INODE(page
);
1725 dst
= F2FS_INODE(ipage
);
1727 memcpy(dst
, src
, (unsigned long)&src
->i_ext
- (unsigned long)src
);
1729 dst
->i_blocks
= cpu_to_le64(1);
1730 dst
->i_links
= cpu_to_le32(1);
1731 dst
->i_xattr_nid
= 0;
1732 dst
->i_inline
= src
->i_inline
& F2FS_INLINE_XATTR
;
1737 if (unlikely(!inc_valid_node_count(sbi
, NULL
)))
1739 set_node_addr(sbi
, &new_ni
, NEW_ADDR
, false);
1740 inc_valid_inode_count(sbi
);
1741 set_page_dirty(ipage
);
1742 f2fs_put_page(ipage
, 1);
1746 int restore_node_summary(struct f2fs_sb_info
*sbi
,
1747 unsigned int segno
, struct f2fs_summary_block
*sum
)
1749 struct f2fs_node
*rn
;
1750 struct f2fs_summary
*sum_entry
;
1752 int bio_blocks
= MAX_BIO_BLOCKS(sbi
);
1753 int i
, idx
, last_offset
, nrpages
;
1755 /* scan the node segment */
1756 last_offset
= sbi
->blocks_per_seg
;
1757 addr
= START_BLOCK(sbi
, segno
);
1758 sum_entry
= &sum
->entries
[0];
1760 for (i
= 0; i
< last_offset
; i
+= nrpages
, addr
+= nrpages
) {
1761 nrpages
= min(last_offset
- i
, bio_blocks
);
1763 /* readahead node pages */
1764 ra_meta_pages(sbi
, addr
, nrpages
, META_POR
);
1766 for (idx
= addr
; idx
< addr
+ nrpages
; idx
++) {
1767 struct page
*page
= get_meta_page(sbi
, idx
);
1769 rn
= F2FS_NODE(page
);
1770 sum_entry
->nid
= rn
->footer
.nid
;
1771 sum_entry
->version
= 0;
1772 sum_entry
->ofs_in_node
= 0;
1774 f2fs_put_page(page
, 1);
1777 invalidate_mapping_pages(META_MAPPING(sbi
), addr
,
1783 static void remove_nats_in_journal(struct f2fs_sb_info
*sbi
)
1785 struct f2fs_nm_info
*nm_i
= NM_I(sbi
);
1786 struct curseg_info
*curseg
= CURSEG_I(sbi
, CURSEG_HOT_DATA
);
1787 struct f2fs_summary_block
*sum
= curseg
->sum_blk
;
1790 mutex_lock(&curseg
->curseg_mutex
);
1791 for (i
= 0; i
< nats_in_cursum(sum
); i
++) {
1792 struct nat_entry
*ne
;
1793 struct f2fs_nat_entry raw_ne
;
1794 nid_t nid
= le32_to_cpu(nid_in_journal(sum
, i
));
1796 raw_ne
= nat_in_journal(sum
, i
);
1798 down_write(&nm_i
->nat_tree_lock
);
1799 ne
= __lookup_nat_cache(nm_i
, nid
);
1801 ne
= grab_nat_entry(nm_i
, nid
);
1802 node_info_from_raw_nat(&ne
->ni
, &raw_ne
);
1804 __set_nat_cache_dirty(nm_i
, ne
);
1805 up_write(&nm_i
->nat_tree_lock
);
1807 update_nats_in_cursum(sum
, -i
);
1808 mutex_unlock(&curseg
->curseg_mutex
);
1811 static void __adjust_nat_entry_set(struct nat_entry_set
*nes
,
1812 struct list_head
*head
, int max
)
1814 struct nat_entry_set
*cur
;
1816 if (nes
->entry_cnt
>= max
)
1819 list_for_each_entry(cur
, head
, set_list
) {
1820 if (cur
->entry_cnt
>= nes
->entry_cnt
) {
1821 list_add(&nes
->set_list
, cur
->set_list
.prev
);
1826 list_add_tail(&nes
->set_list
, head
);
1829 static void __flush_nat_entry_set(struct f2fs_sb_info
*sbi
,
1830 struct nat_entry_set
*set
)
1832 struct curseg_info
*curseg
= CURSEG_I(sbi
, CURSEG_HOT_DATA
);
1833 struct f2fs_summary_block
*sum
= curseg
->sum_blk
;
1834 nid_t start_nid
= set
->set
* NAT_ENTRY_PER_BLOCK
;
1835 bool to_journal
= true;
1836 struct f2fs_nat_block
*nat_blk
;
1837 struct nat_entry
*ne
, *cur
;
1838 struct page
*page
= NULL
;
1839 struct f2fs_nm_info
*nm_i
= NM_I(sbi
);
1842 * there are two steps to flush nat entries:
1843 * #1, flush nat entries to journal in current hot data summary block.
1844 * #2, flush nat entries to nat page.
1846 if (!__has_cursum_space(sum
, set
->entry_cnt
, NAT_JOURNAL
))
1850 mutex_lock(&curseg
->curseg_mutex
);
1852 page
= get_next_nat_page(sbi
, start_nid
);
1853 nat_blk
= page_address(page
);
1854 f2fs_bug_on(sbi
, !nat_blk
);
1857 /* flush dirty nats in nat entry set */
1858 list_for_each_entry_safe(ne
, cur
, &set
->entry_list
, list
) {
1859 struct f2fs_nat_entry
*raw_ne
;
1860 nid_t nid
= nat_get_nid(ne
);
1863 if (nat_get_blkaddr(ne
) == NEW_ADDR
)
1867 offset
= lookup_journal_in_cursum(sum
,
1868 NAT_JOURNAL
, nid
, 1);
1869 f2fs_bug_on(sbi
, offset
< 0);
1870 raw_ne
= &nat_in_journal(sum
, offset
);
1871 nid_in_journal(sum
, offset
) = cpu_to_le32(nid
);
1873 raw_ne
= &nat_blk
->entries
[nid
- start_nid
];
1875 raw_nat_from_node_info(raw_ne
, &ne
->ni
);
1877 down_write(&NM_I(sbi
)->nat_tree_lock
);
1879 __clear_nat_cache_dirty(NM_I(sbi
), ne
);
1880 up_write(&NM_I(sbi
)->nat_tree_lock
);
1882 if (nat_get_blkaddr(ne
) == NULL_ADDR
)
1883 add_free_nid(sbi
, nid
, false);
1887 mutex_unlock(&curseg
->curseg_mutex
);
1889 f2fs_put_page(page
, 1);
1891 f2fs_bug_on(sbi
, set
->entry_cnt
);
1893 down_write(&nm_i
->nat_tree_lock
);
1894 radix_tree_delete(&NM_I(sbi
)->nat_set_root
, set
->set
);
1895 up_write(&nm_i
->nat_tree_lock
);
1896 kmem_cache_free(nat_entry_set_slab
, set
);
1900 * This function is called during the checkpointing process.
1902 void flush_nat_entries(struct f2fs_sb_info
*sbi
)
1904 struct f2fs_nm_info
*nm_i
= NM_I(sbi
);
1905 struct curseg_info
*curseg
= CURSEG_I(sbi
, CURSEG_HOT_DATA
);
1906 struct f2fs_summary_block
*sum
= curseg
->sum_blk
;
1907 struct nat_entry_set
*setvec
[SETVEC_SIZE
];
1908 struct nat_entry_set
*set
, *tmp
;
1913 if (!nm_i
->dirty_nat_cnt
)
1916 * if there are no enough space in journal to store dirty nat
1917 * entries, remove all entries from journal and merge them
1918 * into nat entry set.
1920 if (!__has_cursum_space(sum
, nm_i
->dirty_nat_cnt
, NAT_JOURNAL
))
1921 remove_nats_in_journal(sbi
);
1923 down_write(&nm_i
->nat_tree_lock
);
1924 while ((found
= __gang_lookup_nat_set(nm_i
,
1925 set_idx
, SETVEC_SIZE
, setvec
))) {
1927 set_idx
= setvec
[found
- 1]->set
+ 1;
1928 for (idx
= 0; idx
< found
; idx
++)
1929 __adjust_nat_entry_set(setvec
[idx
], &sets
,
1930 MAX_NAT_JENTRIES(sum
));
1932 up_write(&nm_i
->nat_tree_lock
);
1934 /* flush dirty nats in nat entry set */
1935 list_for_each_entry_safe(set
, tmp
, &sets
, set_list
)
1936 __flush_nat_entry_set(sbi
, set
);
1938 f2fs_bug_on(sbi
, nm_i
->dirty_nat_cnt
);
1941 static int init_node_manager(struct f2fs_sb_info
*sbi
)
1943 struct f2fs_super_block
*sb_raw
= F2FS_RAW_SUPER(sbi
);
1944 struct f2fs_nm_info
*nm_i
= NM_I(sbi
);
1945 unsigned char *version_bitmap
;
1946 unsigned int nat_segs
, nat_blocks
;
1948 nm_i
->nat_blkaddr
= le32_to_cpu(sb_raw
->nat_blkaddr
);
1950 /* segment_count_nat includes pair segment so divide to 2. */
1951 nat_segs
= le32_to_cpu(sb_raw
->segment_count_nat
) >> 1;
1952 nat_blocks
= nat_segs
<< le32_to_cpu(sb_raw
->log_blocks_per_seg
);
1954 nm_i
->max_nid
= NAT_ENTRY_PER_BLOCK
* nat_blocks
;
1956 /* not used nids: 0, node, meta, (and root counted as valid node) */
1957 nm_i
->available_nids
= nm_i
->max_nid
- F2FS_RESERVED_NODE_NUM
;
1960 nm_i
->ram_thresh
= DEF_RAM_THRESHOLD
;
1962 INIT_RADIX_TREE(&nm_i
->free_nid_root
, GFP_ATOMIC
);
1963 INIT_LIST_HEAD(&nm_i
->free_nid_list
);
1964 INIT_RADIX_TREE(&nm_i
->nat_root
, GFP_NOIO
);
1965 INIT_RADIX_TREE(&nm_i
->nat_set_root
, GFP_NOIO
);
1966 INIT_LIST_HEAD(&nm_i
->nat_entries
);
1968 mutex_init(&nm_i
->build_lock
);
1969 spin_lock_init(&nm_i
->free_nid_list_lock
);
1970 init_rwsem(&nm_i
->nat_tree_lock
);
1972 nm_i
->next_scan_nid
= le32_to_cpu(sbi
->ckpt
->next_free_nid
);
1973 nm_i
->bitmap_size
= __bitmap_size(sbi
, NAT_BITMAP
);
1974 version_bitmap
= __bitmap_ptr(sbi
, NAT_BITMAP
);
1975 if (!version_bitmap
)
1978 nm_i
->nat_bitmap
= kmemdup(version_bitmap
, nm_i
->bitmap_size
,
1980 if (!nm_i
->nat_bitmap
)
1985 int build_node_manager(struct f2fs_sb_info
*sbi
)
1989 sbi
->nm_info
= kzalloc(sizeof(struct f2fs_nm_info
), GFP_KERNEL
);
1993 err
= init_node_manager(sbi
);
1997 build_free_nids(sbi
);
2001 void destroy_node_manager(struct f2fs_sb_info
*sbi
)
2003 struct f2fs_nm_info
*nm_i
= NM_I(sbi
);
2004 struct free_nid
*i
, *next_i
;
2005 struct nat_entry
*natvec
[NATVEC_SIZE
];
2006 struct nat_entry_set
*setvec
[SETVEC_SIZE
];
2013 /* destroy free nid list */
2014 spin_lock(&nm_i
->free_nid_list_lock
);
2015 list_for_each_entry_safe(i
, next_i
, &nm_i
->free_nid_list
, list
) {
2016 f2fs_bug_on(sbi
, i
->state
== NID_ALLOC
);
2017 __del_from_free_nid_list(nm_i
, i
);
2019 spin_unlock(&nm_i
->free_nid_list_lock
);
2020 kmem_cache_free(free_nid_slab
, i
);
2021 spin_lock(&nm_i
->free_nid_list_lock
);
2023 f2fs_bug_on(sbi
, nm_i
->fcnt
);
2024 spin_unlock(&nm_i
->free_nid_list_lock
);
2026 /* destroy nat cache */
2027 down_write(&nm_i
->nat_tree_lock
);
2028 while ((found
= __gang_lookup_nat_cache(nm_i
,
2029 nid
, NATVEC_SIZE
, natvec
))) {
2032 nid
= nat_get_nid(natvec
[found
- 1]) + 1;
2033 for (idx
= 0; idx
< found
; idx
++)
2034 __del_from_nat_cache(nm_i
, natvec
[idx
]);
2036 f2fs_bug_on(sbi
, nm_i
->nat_cnt
);
2038 /* destroy nat set cache */
2040 while ((found
= __gang_lookup_nat_set(nm_i
,
2041 nid
, SETVEC_SIZE
, setvec
))) {
2044 nid
= setvec
[found
- 1]->set
+ 1;
2045 for (idx
= 0; idx
< found
; idx
++) {
2046 /* entry_cnt is not zero, when cp_error was occurred */
2047 f2fs_bug_on(sbi
, !list_empty(&setvec
[idx
]->entry_list
));
2048 radix_tree_delete(&nm_i
->nat_set_root
, setvec
[idx
]->set
);
2049 kmem_cache_free(nat_entry_set_slab
, setvec
[idx
]);
2052 up_write(&nm_i
->nat_tree_lock
);
2054 kfree(nm_i
->nat_bitmap
);
2055 sbi
->nm_info
= NULL
;
2059 int __init
create_node_manager_caches(void)
2061 nat_entry_slab
= f2fs_kmem_cache_create("nat_entry",
2062 sizeof(struct nat_entry
));
2063 if (!nat_entry_slab
)
2066 free_nid_slab
= f2fs_kmem_cache_create("free_nid",
2067 sizeof(struct free_nid
));
2069 goto destroy_nat_entry
;
2071 nat_entry_set_slab
= f2fs_kmem_cache_create("nat_entry_set",
2072 sizeof(struct nat_entry_set
));
2073 if (!nat_entry_set_slab
)
2074 goto destroy_free_nid
;
2078 kmem_cache_destroy(free_nid_slab
);
2080 kmem_cache_destroy(nat_entry_slab
);
2085 void destroy_node_manager_caches(void)
2087 kmem_cache_destroy(nat_entry_set_slab
);
2088 kmem_cache_destroy(free_nid_slab
);
2089 kmem_cache_destroy(nat_entry_slab
);