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_NOFS
);
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_NOFS
);
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 e
= __lookup_nat_cache(nm_i
, nid
);
266 e
= grab_nat_entry(nm_i
, nid
);
267 node_info_from_raw_nat(&e
->ni
, ne
);
271 static void set_node_addr(struct f2fs_sb_info
*sbi
, struct node_info
*ni
,
272 block_t new_blkaddr
, bool fsync_done
)
274 struct f2fs_nm_info
*nm_i
= NM_I(sbi
);
277 down_write(&nm_i
->nat_tree_lock
);
278 e
= __lookup_nat_cache(nm_i
, ni
->nid
);
280 e
= grab_nat_entry(nm_i
, ni
->nid
);
281 copy_node_info(&e
->ni
, ni
);
282 f2fs_bug_on(sbi
, ni
->blk_addr
== NEW_ADDR
);
283 } else if (new_blkaddr
== NEW_ADDR
) {
285 * when nid is reallocated,
286 * previous nat entry can be remained in nat cache.
287 * So, reinitialize it with new information.
289 copy_node_info(&e
->ni
, ni
);
290 f2fs_bug_on(sbi
, ni
->blk_addr
!= NULL_ADDR
);
294 f2fs_bug_on(sbi
, nat_get_blkaddr(e
) != ni
->blk_addr
);
295 f2fs_bug_on(sbi
, nat_get_blkaddr(e
) == NULL_ADDR
&&
296 new_blkaddr
== NULL_ADDR
);
297 f2fs_bug_on(sbi
, nat_get_blkaddr(e
) == NEW_ADDR
&&
298 new_blkaddr
== NEW_ADDR
);
299 f2fs_bug_on(sbi
, is_valid_data_blkaddr(sbi
, nat_get_blkaddr(e
)) &&
300 new_blkaddr
== NEW_ADDR
);
302 /* increment version no as node is removed */
303 if (nat_get_blkaddr(e
) != NEW_ADDR
&& new_blkaddr
== NULL_ADDR
) {
304 unsigned char version
= nat_get_version(e
);
305 nat_set_version(e
, inc_node_version(version
));
307 /* in order to reuse the nid */
308 if (nm_i
->next_scan_nid
> ni
->nid
)
309 nm_i
->next_scan_nid
= ni
->nid
;
313 nat_set_blkaddr(e
, new_blkaddr
);
314 if (!is_valid_data_blkaddr(sbi
, new_blkaddr
))
315 set_nat_flag(e
, IS_CHECKPOINTED
, false);
316 __set_nat_cache_dirty(nm_i
, e
);
318 /* update fsync_mark if its inode nat entry is still alive */
319 if (ni
->nid
!= ni
->ino
)
320 e
= __lookup_nat_cache(nm_i
, ni
->ino
);
322 if (fsync_done
&& ni
->nid
== ni
->ino
)
323 set_nat_flag(e
, HAS_FSYNCED_INODE
, true);
324 set_nat_flag(e
, HAS_LAST_FSYNC
, fsync_done
);
326 up_write(&nm_i
->nat_tree_lock
);
329 int try_to_free_nats(struct f2fs_sb_info
*sbi
, int nr_shrink
)
331 struct f2fs_nm_info
*nm_i
= NM_I(sbi
);
334 if (!down_write_trylock(&nm_i
->nat_tree_lock
))
337 while (nr_shrink
&& !list_empty(&nm_i
->nat_entries
)) {
338 struct nat_entry
*ne
;
339 ne
= list_first_entry(&nm_i
->nat_entries
,
340 struct nat_entry
, list
);
341 __del_from_nat_cache(nm_i
, ne
);
344 up_write(&nm_i
->nat_tree_lock
);
345 return nr
- nr_shrink
;
349 * This function always returns success
351 void get_node_info(struct f2fs_sb_info
*sbi
, nid_t nid
, struct node_info
*ni
)
353 struct f2fs_nm_info
*nm_i
= NM_I(sbi
);
354 struct curseg_info
*curseg
= CURSEG_I(sbi
, CURSEG_HOT_DATA
);
355 struct f2fs_summary_block
*sum
= curseg
->sum_blk
;
356 nid_t start_nid
= START_NID(nid
);
357 struct f2fs_nat_block
*nat_blk
;
358 struct page
*page
= NULL
;
359 struct f2fs_nat_entry ne
;
365 /* Check nat cache */
366 down_read(&nm_i
->nat_tree_lock
);
367 e
= __lookup_nat_cache(nm_i
, nid
);
369 ni
->ino
= nat_get_ino(e
);
370 ni
->blk_addr
= nat_get_blkaddr(e
);
371 ni
->version
= nat_get_version(e
);
373 up_read(&nm_i
->nat_tree_lock
);
377 memset(&ne
, 0, sizeof(struct f2fs_nat_entry
));
379 down_write(&nm_i
->nat_tree_lock
);
381 /* Check current segment summary */
382 mutex_lock(&curseg
->curseg_mutex
);
383 i
= lookup_journal_in_cursum(sum
, NAT_JOURNAL
, nid
, 0);
385 ne
= nat_in_journal(sum
, i
);
386 node_info_from_raw_nat(ni
, &ne
);
388 mutex_unlock(&curseg
->curseg_mutex
);
392 /* Fill node_info from nat page */
393 page
= get_current_nat_page(sbi
, start_nid
);
394 nat_blk
= (struct f2fs_nat_block
*)page_address(page
);
395 ne
= nat_blk
->entries
[nid
- start_nid
];
396 node_info_from_raw_nat(ni
, &ne
);
397 f2fs_put_page(page
, 1);
399 /* cache nat entry */
400 cache_nat_entry(NM_I(sbi
), nid
, &ne
);
401 up_write(&nm_i
->nat_tree_lock
);
405 * The maximum depth is four.
406 * Offset[0] will have raw inode offset.
408 static int get_node_path(struct f2fs_inode_info
*fi
, long block
,
409 int offset
[4], unsigned int noffset
[4])
411 const long direct_index
= ADDRS_PER_INODE(fi
);
412 const long direct_blks
= ADDRS_PER_BLOCK
;
413 const long dptrs_per_blk
= NIDS_PER_BLOCK
;
414 const long indirect_blks
= ADDRS_PER_BLOCK
* NIDS_PER_BLOCK
;
415 const long dindirect_blks
= indirect_blks
* NIDS_PER_BLOCK
;
421 if (block
< direct_index
) {
425 block
-= direct_index
;
426 if (block
< direct_blks
) {
427 offset
[n
++] = NODE_DIR1_BLOCK
;
433 block
-= direct_blks
;
434 if (block
< direct_blks
) {
435 offset
[n
++] = NODE_DIR2_BLOCK
;
441 block
-= direct_blks
;
442 if (block
< indirect_blks
) {
443 offset
[n
++] = NODE_IND1_BLOCK
;
445 offset
[n
++] = block
/ direct_blks
;
446 noffset
[n
] = 4 + offset
[n
- 1];
447 offset
[n
] = block
% direct_blks
;
451 block
-= indirect_blks
;
452 if (block
< indirect_blks
) {
453 offset
[n
++] = NODE_IND2_BLOCK
;
454 noffset
[n
] = 4 + dptrs_per_blk
;
455 offset
[n
++] = block
/ direct_blks
;
456 noffset
[n
] = 5 + dptrs_per_blk
+ offset
[n
- 1];
457 offset
[n
] = block
% direct_blks
;
461 block
-= indirect_blks
;
462 if (block
< dindirect_blks
) {
463 offset
[n
++] = NODE_DIND_BLOCK
;
464 noffset
[n
] = 5 + (dptrs_per_blk
* 2);
465 offset
[n
++] = block
/ indirect_blks
;
466 noffset
[n
] = 6 + (dptrs_per_blk
* 2) +
467 offset
[n
- 1] * (dptrs_per_blk
+ 1);
468 offset
[n
++] = (block
/ direct_blks
) % dptrs_per_blk
;
469 noffset
[n
] = 7 + (dptrs_per_blk
* 2) +
470 offset
[n
- 2] * (dptrs_per_blk
+ 1) +
472 offset
[n
] = block
% direct_blks
;
483 * Caller should call f2fs_put_dnode(dn).
484 * Also, it should grab and release a rwsem by calling f2fs_lock_op() and
485 * f2fs_unlock_op() only if ro is not set RDONLY_NODE.
486 * In the case of RDONLY_NODE, we don't need to care about mutex.
488 int get_dnode_of_data(struct dnode_of_data
*dn
, pgoff_t index
, int mode
)
490 struct f2fs_sb_info
*sbi
= F2FS_I_SB(dn
->inode
);
491 struct page
*npage
[4];
492 struct page
*parent
= NULL
;
494 unsigned int noffset
[4];
499 level
= get_node_path(F2FS_I(dn
->inode
), index
, offset
, noffset
);
501 nids
[0] = dn
->inode
->i_ino
;
502 npage
[0] = dn
->inode_page
;
505 npage
[0] = get_node_page(sbi
, nids
[0]);
506 if (IS_ERR(npage
[0]))
507 return PTR_ERR(npage
[0]);
510 /* if inline_data is set, should not report any block indices */
511 if (f2fs_has_inline_data(dn
->inode
) && index
) {
513 f2fs_put_page(npage
[0], 1);
519 nids
[1] = get_nid(parent
, offset
[0], true);
520 dn
->inode_page
= npage
[0];
521 dn
->inode_page_locked
= true;
523 /* get indirect or direct nodes */
524 for (i
= 1; i
<= level
; i
++) {
527 if (!nids
[i
] && mode
== ALLOC_NODE
) {
529 if (!alloc_nid(sbi
, &(nids
[i
]))) {
535 npage
[i
] = new_node_page(dn
, noffset
[i
], NULL
);
536 if (IS_ERR(npage
[i
])) {
537 alloc_nid_failed(sbi
, nids
[i
]);
538 err
= PTR_ERR(npage
[i
]);
542 set_nid(parent
, offset
[i
- 1], nids
[i
], i
== 1);
543 alloc_nid_done(sbi
, nids
[i
]);
545 } else if (mode
== LOOKUP_NODE_RA
&& i
== level
&& level
> 1) {
546 npage
[i
] = get_node_page_ra(parent
, offset
[i
- 1]);
547 if (IS_ERR(npage
[i
])) {
548 err
= PTR_ERR(npage
[i
]);
554 dn
->inode_page_locked
= false;
557 f2fs_put_page(parent
, 1);
561 npage
[i
] = get_node_page(sbi
, nids
[i
]);
562 if (IS_ERR(npage
[i
])) {
563 err
= PTR_ERR(npage
[i
]);
564 f2fs_put_page(npage
[0], 0);
570 nids
[i
+ 1] = get_nid(parent
, offset
[i
], false);
573 dn
->nid
= nids
[level
];
574 dn
->ofs_in_node
= offset
[level
];
575 dn
->node_page
= npage
[level
];
576 dn
->data_blkaddr
= datablock_addr(dn
->node_page
, dn
->ofs_in_node
);
580 f2fs_put_page(parent
, 1);
582 f2fs_put_page(npage
[0], 0);
584 dn
->inode_page
= NULL
;
585 dn
->node_page
= NULL
;
589 static void truncate_node(struct dnode_of_data
*dn
)
591 struct f2fs_sb_info
*sbi
= F2FS_I_SB(dn
->inode
);
595 get_node_info(sbi
, dn
->nid
, &ni
);
596 if (dn
->inode
->i_blocks
== 0) {
597 f2fs_bug_on(sbi
, ni
.blk_addr
!= NULL_ADDR
);
600 f2fs_bug_on(sbi
, ni
.blk_addr
== NULL_ADDR
);
602 /* Deallocate node address */
603 invalidate_blocks(sbi
, ni
.blk_addr
);
604 dec_valid_node_count(sbi
, dn
->inode
);
605 set_node_addr(sbi
, &ni
, NULL_ADDR
, false);
607 if (dn
->nid
== dn
->inode
->i_ino
) {
608 remove_orphan_inode(sbi
, dn
->nid
);
609 dec_valid_inode_count(sbi
);
614 clear_node_page_dirty(dn
->node_page
);
615 set_sbi_flag(sbi
, SBI_IS_DIRTY
);
617 index
= dn
->node_page
->index
;
618 f2fs_put_page(dn
->node_page
, 1);
620 invalidate_mapping_pages(NODE_MAPPING(sbi
),
623 dn
->node_page
= NULL
;
624 trace_f2fs_truncate_node(dn
->inode
, dn
->nid
, ni
.blk_addr
);
627 static int truncate_dnode(struct dnode_of_data
*dn
)
634 /* get direct node */
635 page
= get_node_page(F2FS_I_SB(dn
->inode
), dn
->nid
);
636 if (IS_ERR(page
) && PTR_ERR(page
) == -ENOENT
)
638 else if (IS_ERR(page
))
639 return PTR_ERR(page
);
641 /* Make dnode_of_data for parameter */
642 dn
->node_page
= page
;
644 truncate_data_blocks(dn
);
649 static int truncate_nodes(struct dnode_of_data
*dn
, unsigned int nofs
,
652 struct dnode_of_data rdn
= *dn
;
654 struct f2fs_node
*rn
;
656 unsigned int child_nofs
;
661 return NIDS_PER_BLOCK
+ 1;
663 trace_f2fs_truncate_nodes_enter(dn
->inode
, dn
->nid
, dn
->data_blkaddr
);
665 page
= get_node_page(F2FS_I_SB(dn
->inode
), dn
->nid
);
667 trace_f2fs_truncate_nodes_exit(dn
->inode
, PTR_ERR(page
));
668 return PTR_ERR(page
);
671 rn
= F2FS_NODE(page
);
673 for (i
= ofs
; i
< NIDS_PER_BLOCK
; i
++, freed
++) {
674 child_nid
= le32_to_cpu(rn
->in
.nid
[i
]);
678 ret
= truncate_dnode(&rdn
);
681 set_nid(page
, i
, 0, false);
684 child_nofs
= nofs
+ ofs
* (NIDS_PER_BLOCK
+ 1) + 1;
685 for (i
= ofs
; i
< NIDS_PER_BLOCK
; i
++) {
686 child_nid
= le32_to_cpu(rn
->in
.nid
[i
]);
687 if (child_nid
== 0) {
688 child_nofs
+= NIDS_PER_BLOCK
+ 1;
692 ret
= truncate_nodes(&rdn
, child_nofs
, 0, depth
- 1);
693 if (ret
== (NIDS_PER_BLOCK
+ 1)) {
694 set_nid(page
, i
, 0, false);
696 } else if (ret
< 0 && ret
!= -ENOENT
) {
704 /* remove current indirect node */
705 dn
->node_page
= page
;
709 f2fs_put_page(page
, 1);
711 trace_f2fs_truncate_nodes_exit(dn
->inode
, freed
);
715 f2fs_put_page(page
, 1);
716 trace_f2fs_truncate_nodes_exit(dn
->inode
, ret
);
720 static int truncate_partial_nodes(struct dnode_of_data
*dn
,
721 struct f2fs_inode
*ri
, int *offset
, int depth
)
723 struct page
*pages
[2];
730 nid
[0] = le32_to_cpu(ri
->i_nid
[offset
[0] - NODE_DIR1_BLOCK
]);
734 /* get indirect nodes in the path */
735 for (i
= 0; i
< idx
+ 1; i
++) {
736 /* reference count'll be increased */
737 pages
[i
] = get_node_page(F2FS_I_SB(dn
->inode
), nid
[i
]);
738 if (IS_ERR(pages
[i
])) {
739 err
= PTR_ERR(pages
[i
]);
743 nid
[i
+ 1] = get_nid(pages
[i
], offset
[i
+ 1], false);
746 /* free direct nodes linked to a partial indirect node */
747 for (i
= offset
[idx
+ 1]; i
< NIDS_PER_BLOCK
; i
++) {
748 child_nid
= get_nid(pages
[idx
], i
, false);
752 err
= truncate_dnode(dn
);
755 set_nid(pages
[idx
], i
, 0, false);
758 if (offset
[idx
+ 1] == 0) {
759 dn
->node_page
= pages
[idx
];
763 f2fs_put_page(pages
[idx
], 1);
769 for (i
= idx
; i
>= 0; i
--)
770 f2fs_put_page(pages
[i
], 1);
772 trace_f2fs_truncate_partial_nodes(dn
->inode
, nid
, depth
, err
);
778 * All the block addresses of data and nodes should be nullified.
780 int truncate_inode_blocks(struct inode
*inode
, pgoff_t from
)
782 struct f2fs_sb_info
*sbi
= F2FS_I_SB(inode
);
783 int err
= 0, cont
= 1;
784 int level
, offset
[4], noffset
[4];
785 unsigned int nofs
= 0;
786 struct f2fs_inode
*ri
;
787 struct dnode_of_data dn
;
790 trace_f2fs_truncate_inode_blocks_enter(inode
, from
);
792 level
= get_node_path(F2FS_I(inode
), from
, offset
, noffset
);
794 page
= get_node_page(sbi
, inode
->i_ino
);
796 trace_f2fs_truncate_inode_blocks_exit(inode
, PTR_ERR(page
));
797 return PTR_ERR(page
);
800 set_new_dnode(&dn
, inode
, page
, NULL
, 0);
803 ri
= F2FS_INODE(page
);
811 if (!offset
[level
- 1])
813 err
= truncate_partial_nodes(&dn
, ri
, offset
, level
);
814 if (err
< 0 && err
!= -ENOENT
)
816 nofs
+= 1 + NIDS_PER_BLOCK
;
819 nofs
= 5 + 2 * NIDS_PER_BLOCK
;
820 if (!offset
[level
- 1])
822 err
= truncate_partial_nodes(&dn
, ri
, offset
, level
);
823 if (err
< 0 && err
!= -ENOENT
)
832 dn
.nid
= le32_to_cpu(ri
->i_nid
[offset
[0] - NODE_DIR1_BLOCK
]);
834 case NODE_DIR1_BLOCK
:
835 case NODE_DIR2_BLOCK
:
836 err
= truncate_dnode(&dn
);
839 case NODE_IND1_BLOCK
:
840 case NODE_IND2_BLOCK
:
841 err
= truncate_nodes(&dn
, nofs
, offset
[1], 2);
844 case NODE_DIND_BLOCK
:
845 err
= truncate_nodes(&dn
, nofs
, offset
[1], 3);
852 if (err
< 0 && err
!= -ENOENT
)
854 if (offset
[1] == 0 &&
855 ri
->i_nid
[offset
[0] - NODE_DIR1_BLOCK
]) {
857 if (unlikely(page
->mapping
!= NODE_MAPPING(sbi
))) {
858 f2fs_put_page(page
, 1);
861 f2fs_wait_on_page_writeback(page
, NODE
);
862 ri
->i_nid
[offset
[0] - NODE_DIR1_BLOCK
] = 0;
863 set_page_dirty(page
);
871 f2fs_put_page(page
, 0);
872 trace_f2fs_truncate_inode_blocks_exit(inode
, err
);
873 return err
> 0 ? 0 : err
;
876 int truncate_xattr_node(struct inode
*inode
, struct page
*page
)
878 struct f2fs_sb_info
*sbi
= F2FS_I_SB(inode
);
879 nid_t nid
= F2FS_I(inode
)->i_xattr_nid
;
880 struct dnode_of_data dn
;
886 npage
= get_node_page(sbi
, nid
);
888 return PTR_ERR(npage
);
890 F2FS_I(inode
)->i_xattr_nid
= 0;
892 /* need to do checkpoint during fsync */
893 F2FS_I(inode
)->xattr_ver
= cur_cp_version(F2FS_CKPT(sbi
));
895 set_new_dnode(&dn
, inode
, page
, npage
, nid
);
898 dn
.inode_page_locked
= true;
904 * Caller should grab and release a rwsem by calling f2fs_lock_op() and
907 int remove_inode_page(struct inode
*inode
)
909 struct dnode_of_data dn
;
912 set_new_dnode(&dn
, inode
, NULL
, NULL
, inode
->i_ino
);
913 err
= get_dnode_of_data(&dn
, 0, LOOKUP_NODE
);
917 err
= truncate_xattr_node(inode
, dn
.inode_page
);
923 /* remove potential inline_data blocks */
924 if (S_ISREG(inode
->i_mode
) || S_ISDIR(inode
->i_mode
) ||
925 S_ISLNK(inode
->i_mode
))
926 truncate_data_blocks_range(&dn
, 1);
928 /* 0 is possible, after f2fs_new_inode() has failed */
929 f2fs_bug_on(F2FS_I_SB(inode
),
930 inode
->i_blocks
!= 0 && inode
->i_blocks
!= 1);
932 /* will put inode & node pages */
937 struct page
*new_inode_page(struct inode
*inode
)
939 struct dnode_of_data dn
;
941 /* allocate inode page for new inode */
942 set_new_dnode(&dn
, inode
, NULL
, NULL
, inode
->i_ino
);
944 /* caller should f2fs_put_page(page, 1); */
945 return new_node_page(&dn
, 0, NULL
);
948 struct page
*new_node_page(struct dnode_of_data
*dn
,
949 unsigned int ofs
, struct page
*ipage
)
951 struct f2fs_sb_info
*sbi
= F2FS_I_SB(dn
->inode
);
952 struct node_info old_ni
, new_ni
;
956 if (unlikely(is_inode_flag_set(F2FS_I(dn
->inode
), FI_NO_ALLOC
)))
957 return ERR_PTR(-EPERM
);
959 page
= grab_cache_page(NODE_MAPPING(sbi
), dn
->nid
);
961 return ERR_PTR(-ENOMEM
);
963 if (unlikely(!inc_valid_node_count(sbi
, dn
->inode
))) {
968 get_node_info(sbi
, dn
->nid
, &old_ni
);
970 /* Reinitialize old_ni with new node page */
971 f2fs_bug_on(sbi
, old_ni
.blk_addr
!= NULL_ADDR
);
973 new_ni
.ino
= dn
->inode
->i_ino
;
974 set_node_addr(sbi
, &new_ni
, NEW_ADDR
, false);
976 f2fs_wait_on_page_writeback(page
, NODE
);
977 fill_node_footer(page
, dn
->nid
, dn
->inode
->i_ino
, ofs
, true);
978 set_cold_node(dn
->inode
, page
);
979 SetPageUptodate(page
);
980 set_page_dirty(page
);
982 if (f2fs_has_xattr_block(ofs
))
983 F2FS_I(dn
->inode
)->i_xattr_nid
= dn
->nid
;
985 dn
->node_page
= page
;
987 update_inode(dn
->inode
, ipage
);
991 inc_valid_inode_count(sbi
);
996 clear_node_page_dirty(page
);
997 f2fs_put_page(page
, 1);
1002 * Caller should do after getting the following values.
1003 * 0: f2fs_put_page(page, 0)
1004 * LOCKED_PAGE or error: f2fs_put_page(page, 1)
1006 static int read_node_page(struct page
*page
, int rw
)
1008 struct f2fs_sb_info
*sbi
= F2FS_P_SB(page
);
1009 struct node_info ni
;
1010 struct f2fs_io_info fio
= {
1015 .encrypted_page
= NULL
,
1018 get_node_info(sbi
, page
->index
, &ni
);
1020 if (unlikely(ni
.blk_addr
== NULL_ADDR
)) {
1021 ClearPageUptodate(page
);
1025 if (PageUptodate(page
))
1028 fio
.blk_addr
= ni
.blk_addr
;
1029 return f2fs_submit_page_bio(&fio
);
1033 * Readahead a node page
1035 void ra_node_page(struct f2fs_sb_info
*sbi
, nid_t nid
)
1040 apage
= find_get_page(NODE_MAPPING(sbi
), nid
);
1041 if (apage
&& PageUptodate(apage
)) {
1042 f2fs_put_page(apage
, 0);
1045 f2fs_put_page(apage
, 0);
1047 apage
= grab_cache_page(NODE_MAPPING(sbi
), nid
);
1051 err
= read_node_page(apage
, READA
);
1052 f2fs_put_page(apage
, err
? 1 : 0);
1055 struct page
*get_node_page(struct f2fs_sb_info
*sbi
, pgoff_t nid
)
1060 page
= grab_cache_page(NODE_MAPPING(sbi
), nid
);
1062 return ERR_PTR(-ENOMEM
);
1064 err
= read_node_page(page
, READ_SYNC
);
1066 f2fs_put_page(page
, 1);
1067 return ERR_PTR(err
);
1068 } else if (err
!= LOCKED_PAGE
) {
1072 if (unlikely(!PageUptodate(page
) || nid
!= nid_of_node(page
))) {
1073 ClearPageUptodate(page
);
1074 f2fs_put_page(page
, 1);
1075 return ERR_PTR(-EIO
);
1077 if (unlikely(page
->mapping
!= NODE_MAPPING(sbi
))) {
1078 f2fs_put_page(page
, 1);
1085 * Return a locked page for the desired node page.
1086 * And, readahead MAX_RA_NODE number of node pages.
1088 struct page
*get_node_page_ra(struct page
*parent
, int start
)
1090 struct f2fs_sb_info
*sbi
= F2FS_P_SB(parent
);
1091 struct blk_plug plug
;
1096 /* First, try getting the desired direct node. */
1097 nid
= get_nid(parent
, start
, false);
1099 return ERR_PTR(-ENOENT
);
1101 page
= grab_cache_page(NODE_MAPPING(sbi
), nid
);
1103 return ERR_PTR(-ENOMEM
);
1105 err
= read_node_page(page
, READ_SYNC
);
1107 f2fs_put_page(page
, 1);
1108 return ERR_PTR(err
);
1109 } else if (err
== LOCKED_PAGE
) {
1113 blk_start_plug(&plug
);
1115 /* Then, try readahead for siblings of the desired node */
1116 end
= start
+ MAX_RA_NODE
;
1117 end
= min(end
, NIDS_PER_BLOCK
);
1118 for (i
= start
+ 1; i
< end
; i
++) {
1119 nid
= get_nid(parent
, i
, false);
1122 ra_node_page(sbi
, nid
);
1125 blk_finish_plug(&plug
);
1128 if (unlikely(page
->mapping
!= NODE_MAPPING(sbi
))) {
1129 f2fs_put_page(page
, 1);
1133 if (unlikely(!PageUptodate(page
))) {
1134 f2fs_put_page(page
, 1);
1135 return ERR_PTR(-EIO
);
1140 void sync_inode_page(struct dnode_of_data
*dn
)
1142 if (IS_INODE(dn
->node_page
) || dn
->inode_page
== dn
->node_page
) {
1143 update_inode(dn
->inode
, dn
->node_page
);
1144 } else if (dn
->inode_page
) {
1145 if (!dn
->inode_page_locked
)
1146 lock_page(dn
->inode_page
);
1147 update_inode(dn
->inode
, dn
->inode_page
);
1148 if (!dn
->inode_page_locked
)
1149 unlock_page(dn
->inode_page
);
1151 update_inode_page(dn
->inode
);
1155 int sync_node_pages(struct f2fs_sb_info
*sbi
, nid_t ino
,
1156 struct writeback_control
*wbc
)
1159 struct pagevec pvec
;
1160 int step
= ino
? 2 : 0;
1161 int nwritten
= 0, wrote
= 0;
1163 pagevec_init(&pvec
, 0);
1169 while (index
<= end
) {
1171 nr_pages
= pagevec_lookup_tag(&pvec
, NODE_MAPPING(sbi
), &index
,
1172 PAGECACHE_TAG_DIRTY
,
1173 min(end
- index
, (pgoff_t
)PAGEVEC_SIZE
-1) + 1);
1177 for (i
= 0; i
< nr_pages
; i
++) {
1178 struct page
*page
= pvec
.pages
[i
];
1181 * flushing sequence with step:
1186 if (step
== 0 && IS_DNODE(page
))
1188 if (step
== 1 && (!IS_DNODE(page
) ||
1189 is_cold_node(page
)))
1191 if (step
== 2 && (!IS_DNODE(page
) ||
1192 !is_cold_node(page
)))
1197 * we should not skip writing node pages.
1199 if (ino
&& ino_of_node(page
) == ino
)
1201 else if (!trylock_page(page
))
1204 if (unlikely(page
->mapping
!= NODE_MAPPING(sbi
))) {
1209 if (ino
&& ino_of_node(page
) != ino
)
1210 goto continue_unlock
;
1212 if (!PageDirty(page
)) {
1213 /* someone wrote it for us */
1214 goto continue_unlock
;
1217 if (!clear_page_dirty_for_io(page
))
1218 goto continue_unlock
;
1220 /* called by fsync() */
1221 if (ino
&& IS_DNODE(page
)) {
1222 set_fsync_mark(page
, 1);
1224 set_dentry_mark(page
,
1225 need_dentry_mark(sbi
, ino
));
1228 set_fsync_mark(page
, 0);
1229 set_dentry_mark(page
, 0);
1232 if (NODE_MAPPING(sbi
)->a_ops
->writepage(page
, wbc
))
1237 if (--wbc
->nr_to_write
== 0)
1240 pagevec_release(&pvec
);
1243 if (wbc
->nr_to_write
== 0) {
1255 f2fs_submit_merged_bio(sbi
, NODE
, WRITE
);
1259 int wait_on_node_pages_writeback(struct f2fs_sb_info
*sbi
, nid_t ino
)
1261 pgoff_t index
= 0, end
= LONG_MAX
;
1262 struct pagevec pvec
;
1263 int ret2
= 0, ret
= 0;
1265 pagevec_init(&pvec
, 0);
1267 while (index
<= end
) {
1269 nr_pages
= pagevec_lookup_tag(&pvec
, NODE_MAPPING(sbi
), &index
,
1270 PAGECACHE_TAG_WRITEBACK
,
1271 min(end
- index
, (pgoff_t
)PAGEVEC_SIZE
-1) + 1);
1275 for (i
= 0; i
< nr_pages
; i
++) {
1276 struct page
*page
= pvec
.pages
[i
];
1278 /* until radix tree lookup accepts end_index */
1279 if (unlikely(page
->index
> end
))
1282 if (ino
&& ino_of_node(page
) == ino
) {
1283 f2fs_wait_on_page_writeback(page
, NODE
);
1284 if (TestClearPageError(page
))
1288 pagevec_release(&pvec
);
1292 if (unlikely(test_and_clear_bit(AS_ENOSPC
, &NODE_MAPPING(sbi
)->flags
)))
1294 if (unlikely(test_and_clear_bit(AS_EIO
, &NODE_MAPPING(sbi
)->flags
)))
1301 static int f2fs_write_node_page(struct page
*page
,
1302 struct writeback_control
*wbc
)
1304 struct f2fs_sb_info
*sbi
= F2FS_P_SB(page
);
1306 struct node_info ni
;
1307 struct f2fs_io_info fio
= {
1310 .rw
= (wbc
->sync_mode
== WB_SYNC_ALL
) ? WRITE_SYNC
: WRITE
,
1312 .encrypted_page
= NULL
,
1315 trace_f2fs_writepage(page
, NODE
);
1317 if (unlikely(is_sbi_flag_set(sbi
, SBI_POR_DOING
)))
1319 if (unlikely(f2fs_cp_error(sbi
)))
1322 f2fs_wait_on_page_writeback(page
, NODE
);
1324 /* get old block addr of this node page */
1325 nid
= nid_of_node(page
);
1326 f2fs_bug_on(sbi
, page
->index
!= nid
);
1328 if (wbc
->for_reclaim
) {
1329 if (!down_read_trylock(&sbi
->node_write
))
1332 down_read(&sbi
->node_write
);
1335 get_node_info(sbi
, nid
, &ni
);
1337 /* This page is already truncated */
1338 if (unlikely(ni
.blk_addr
== NULL_ADDR
)) {
1339 ClearPageUptodate(page
);
1340 dec_page_count(sbi
, F2FS_DIRTY_NODES
);
1341 up_read(&sbi
->node_write
);
1346 if (__is_valid_data_blkaddr(ni
.blk_addr
) &&
1347 !f2fs_is_valid_blkaddr(sbi
, ni
.blk_addr
, DATA_GENERIC
)) {
1348 up_read(&sbi
->node_write
);
1352 set_page_writeback(page
);
1353 fio
.blk_addr
= ni
.blk_addr
;
1354 write_node_page(nid
, &fio
);
1355 set_node_addr(sbi
, &ni
, fio
.blk_addr
, is_fsync_dnode(page
));
1356 dec_page_count(sbi
, F2FS_DIRTY_NODES
);
1357 up_read(&sbi
->node_write
);
1360 if (wbc
->for_reclaim
)
1361 f2fs_submit_merged_bio(sbi
, NODE
, WRITE
);
1366 redirty_page_for_writepage(wbc
, page
);
1367 return AOP_WRITEPAGE_ACTIVATE
;
1370 static int f2fs_write_node_pages(struct address_space
*mapping
,
1371 struct writeback_control
*wbc
)
1373 struct f2fs_sb_info
*sbi
= F2FS_M_SB(mapping
);
1376 trace_f2fs_writepages(mapping
->host
, wbc
, NODE
);
1378 /* balancing f2fs's metadata in background */
1379 f2fs_balance_fs_bg(sbi
);
1381 /* collect a number of dirty node pages and write together */
1382 if (get_pages(sbi
, F2FS_DIRTY_NODES
) < nr_pages_to_skip(sbi
, NODE
))
1385 diff
= nr_pages_to_write(sbi
, NODE
, wbc
);
1386 wbc
->sync_mode
= WB_SYNC_NONE
;
1387 sync_node_pages(sbi
, 0, wbc
);
1388 wbc
->nr_to_write
= max((long)0, wbc
->nr_to_write
- diff
);
1392 wbc
->pages_skipped
+= get_pages(sbi
, F2FS_DIRTY_NODES
);
1396 static int f2fs_set_node_page_dirty(struct page
*page
)
1398 trace_f2fs_set_page_dirty(page
, NODE
);
1400 SetPageUptodate(page
);
1401 if (!PageDirty(page
)) {
1402 __set_page_dirty_nobuffers(page
);
1403 inc_page_count(F2FS_P_SB(page
), F2FS_DIRTY_NODES
);
1404 SetPagePrivate(page
);
1405 f2fs_trace_pid(page
);
1412 * Structure of the f2fs node operations
1414 const struct address_space_operations f2fs_node_aops
= {
1415 .writepage
= f2fs_write_node_page
,
1416 .writepages
= f2fs_write_node_pages
,
1417 .set_page_dirty
= f2fs_set_node_page_dirty
,
1418 .invalidatepage
= f2fs_invalidate_page
,
1419 .releasepage
= f2fs_release_page
,
1422 static struct free_nid
*__lookup_free_nid_list(struct f2fs_nm_info
*nm_i
,
1425 return radix_tree_lookup(&nm_i
->free_nid_root
, n
);
1428 static void __del_from_free_nid_list(struct f2fs_nm_info
*nm_i
,
1432 radix_tree_delete(&nm_i
->free_nid_root
, i
->nid
);
1435 static int add_free_nid(struct f2fs_sb_info
*sbi
, nid_t nid
, bool build
)
1437 struct f2fs_nm_info
*nm_i
= NM_I(sbi
);
1438 struct free_nid
*i
, *e
;
1439 struct nat_entry
*ne
;
1442 if (!available_free_memory(sbi
, FREE_NIDS
))
1445 /* 0 nid should not be used */
1446 if (unlikely(nid
== 0))
1449 i
= f2fs_kmem_cache_alloc(free_nid_slab
, GFP_NOFS
);
1453 if (radix_tree_preload(GFP_NOFS
))
1456 spin_lock(&nm_i
->free_nid_list_lock
);
1464 * - __insert_nid_to_list(ALLOC_NID_LIST)
1465 * - f2fs_balance_fs_bg
1467 * - __build_free_nids
1470 * - __lookup_nat_cache
1472 * - init_inode_metadata
1477 * - __remove_nid_from_list(ALLOC_NID_LIST)
1478 * - __insert_nid_to_list(FREE_NID_LIST)
1480 ne
= __lookup_nat_cache(nm_i
, nid
);
1481 if (ne
&& (!get_nat_flag(ne
, IS_CHECKPOINTED
) ||
1482 nat_get_blkaddr(ne
) != NULL_ADDR
))
1485 e
= __lookup_free_nid_list(nm_i
, nid
);
1489 if (radix_tree_insert(&nm_i
->free_nid_root
, i
->nid
, i
))
1492 list_add_tail(&i
->list
, &nm_i
->free_nid_list
);
1495 spin_unlock(&nm_i
->free_nid_list_lock
);
1496 radix_tree_preload_end();
1499 kmem_cache_free(free_nid_slab
, i
);
1503 static void remove_free_nid(struct f2fs_nm_info
*nm_i
, nid_t nid
)
1506 bool need_free
= false;
1508 spin_lock(&nm_i
->free_nid_list_lock
);
1509 i
= __lookup_free_nid_list(nm_i
, nid
);
1510 if (i
&& i
->state
== NID_NEW
) {
1511 __del_from_free_nid_list(nm_i
, i
);
1515 spin_unlock(&nm_i
->free_nid_list_lock
);
1518 kmem_cache_free(free_nid_slab
, i
);
1521 static void scan_nat_page(struct f2fs_sb_info
*sbi
,
1522 struct page
*nat_page
, nid_t start_nid
)
1524 struct f2fs_nm_info
*nm_i
= NM_I(sbi
);
1525 struct f2fs_nat_block
*nat_blk
= page_address(nat_page
);
1529 i
= start_nid
% NAT_ENTRY_PER_BLOCK
;
1531 for (; i
< NAT_ENTRY_PER_BLOCK
; i
++, start_nid
++) {
1533 if (unlikely(start_nid
>= nm_i
->max_nid
))
1536 blk_addr
= le32_to_cpu(nat_blk
->entries
[i
].block_addr
);
1537 f2fs_bug_on(sbi
, blk_addr
== NEW_ADDR
);
1538 if (blk_addr
== NULL_ADDR
) {
1539 if (add_free_nid(sbi
, start_nid
, true) < 0)
1545 static void build_free_nids(struct f2fs_sb_info
*sbi
)
1547 struct f2fs_nm_info
*nm_i
= NM_I(sbi
);
1548 struct curseg_info
*curseg
= CURSEG_I(sbi
, CURSEG_HOT_DATA
);
1549 struct f2fs_summary_block
*sum
= curseg
->sum_blk
;
1551 nid_t nid
= nm_i
->next_scan_nid
;
1553 /* Enough entries */
1554 if (nm_i
->fcnt
> NAT_ENTRY_PER_BLOCK
)
1557 /* readahead nat pages to be scanned */
1558 ra_meta_pages(sbi
, NAT_BLOCK_OFFSET(nid
), FREE_NID_PAGES
,
1561 down_read(&nm_i
->nat_tree_lock
);
1564 struct page
*page
= get_current_nat_page(sbi
, nid
);
1566 scan_nat_page(sbi
, page
, nid
);
1567 f2fs_put_page(page
, 1);
1569 nid
+= (NAT_ENTRY_PER_BLOCK
- (nid
% NAT_ENTRY_PER_BLOCK
));
1570 if (unlikely(nid
>= nm_i
->max_nid
))
1573 if (++i
>= FREE_NID_PAGES
)
1577 /* go to the next free nat pages to find free nids abundantly */
1578 nm_i
->next_scan_nid
= nid
;
1580 /* find free nids from current sum_pages */
1581 mutex_lock(&curseg
->curseg_mutex
);
1582 for (i
= 0; i
< nats_in_cursum(sum
); i
++) {
1583 block_t addr
= le32_to_cpu(nat_in_journal(sum
, i
).block_addr
);
1584 nid
= le32_to_cpu(nid_in_journal(sum
, i
));
1585 if (addr
== NULL_ADDR
)
1586 add_free_nid(sbi
, nid
, true);
1588 remove_free_nid(nm_i
, nid
);
1590 mutex_unlock(&curseg
->curseg_mutex
);
1591 up_read(&nm_i
->nat_tree_lock
);
1593 ra_meta_pages(sbi
, NAT_BLOCK_OFFSET(nm_i
->next_scan_nid
),
1594 nm_i
->ra_nid_pages
, META_NAT
, false);
1598 * If this function returns success, caller can obtain a new nid
1599 * from second parameter of this function.
1600 * The returned nid could be used ino as well as nid when inode is created.
1602 bool alloc_nid(struct f2fs_sb_info
*sbi
, nid_t
*nid
)
1604 struct f2fs_nm_info
*nm_i
= NM_I(sbi
);
1605 struct free_nid
*i
= NULL
;
1607 if (unlikely(sbi
->total_valid_node_count
+ 1 > nm_i
->available_nids
))
1610 spin_lock(&nm_i
->free_nid_list_lock
);
1612 /* We should not use stale free nids created by build_free_nids */
1613 if (nm_i
->fcnt
&& !on_build_free_nids(nm_i
)) {
1614 struct node_info ni
;
1616 f2fs_bug_on(sbi
, list_empty(&nm_i
->free_nid_list
));
1617 list_for_each_entry(i
, &nm_i
->free_nid_list
, list
)
1618 if (i
->state
== NID_NEW
)
1621 f2fs_bug_on(sbi
, i
->state
!= NID_NEW
);
1623 i
->state
= NID_ALLOC
;
1625 spin_unlock(&nm_i
->free_nid_list_lock
);
1627 /* check nid is allocated already */
1628 get_node_info(sbi
, *nid
, &ni
);
1629 if (ni
.blk_addr
!= NULL_ADDR
) {
1630 alloc_nid_done(sbi
, *nid
);
1635 spin_unlock(&nm_i
->free_nid_list_lock
);
1637 /* Let's scan nat pages and its caches to get free nids */
1638 mutex_lock(&nm_i
->build_lock
);
1639 build_free_nids(sbi
);
1640 mutex_unlock(&nm_i
->build_lock
);
1645 * alloc_nid() should be called prior to this function.
1647 void alloc_nid_done(struct f2fs_sb_info
*sbi
, nid_t nid
)
1649 struct f2fs_nm_info
*nm_i
= NM_I(sbi
);
1652 spin_lock(&nm_i
->free_nid_list_lock
);
1653 i
= __lookup_free_nid_list(nm_i
, nid
);
1654 f2fs_bug_on(sbi
, !i
|| i
->state
!= NID_ALLOC
);
1655 __del_from_free_nid_list(nm_i
, i
);
1656 spin_unlock(&nm_i
->free_nid_list_lock
);
1658 kmem_cache_free(free_nid_slab
, i
);
1662 * alloc_nid() should be called prior to this function.
1664 void alloc_nid_failed(struct f2fs_sb_info
*sbi
, nid_t nid
)
1666 struct f2fs_nm_info
*nm_i
= NM_I(sbi
);
1668 bool need_free
= false;
1673 spin_lock(&nm_i
->free_nid_list_lock
);
1674 i
= __lookup_free_nid_list(nm_i
, nid
);
1675 f2fs_bug_on(sbi
, !i
|| i
->state
!= NID_ALLOC
);
1676 if (!available_free_memory(sbi
, FREE_NIDS
)) {
1677 __del_from_free_nid_list(nm_i
, i
);
1683 spin_unlock(&nm_i
->free_nid_list_lock
);
1686 kmem_cache_free(free_nid_slab
, i
);
1689 int try_to_free_nids(struct f2fs_sb_info
*sbi
, int nr_shrink
)
1691 struct f2fs_nm_info
*nm_i
= NM_I(sbi
);
1692 struct free_nid
*i
, *next
;
1695 if (!mutex_trylock(&nm_i
->build_lock
))
1698 spin_lock(&nm_i
->free_nid_list_lock
);
1699 list_for_each_entry_safe(i
, next
, &nm_i
->free_nid_list
, list
) {
1700 if (nr_shrink
<= 0 || nm_i
->fcnt
<= NAT_ENTRY_PER_BLOCK
)
1702 if (i
->state
== NID_ALLOC
)
1704 __del_from_free_nid_list(nm_i
, i
);
1705 kmem_cache_free(free_nid_slab
, i
);
1709 spin_unlock(&nm_i
->free_nid_list_lock
);
1710 mutex_unlock(&nm_i
->build_lock
);
1712 return nr
- nr_shrink
;
1715 void recover_inline_xattr(struct inode
*inode
, struct page
*page
)
1717 void *src_addr
, *dst_addr
;
1720 struct f2fs_inode
*ri
;
1722 ipage
= get_node_page(F2FS_I_SB(inode
), inode
->i_ino
);
1723 f2fs_bug_on(F2FS_I_SB(inode
), IS_ERR(ipage
));
1725 ri
= F2FS_INODE(page
);
1726 if (!(ri
->i_inline
& F2FS_INLINE_XATTR
)) {
1727 clear_inode_flag(F2FS_I(inode
), FI_INLINE_XATTR
);
1731 dst_addr
= inline_xattr_addr(ipage
);
1732 src_addr
= inline_xattr_addr(page
);
1733 inline_size
= inline_xattr_size(inode
);
1735 f2fs_wait_on_page_writeback(ipage
, NODE
);
1736 memcpy(dst_addr
, src_addr
, inline_size
);
1738 update_inode(inode
, ipage
);
1739 f2fs_put_page(ipage
, 1);
1742 void recover_xattr_data(struct inode
*inode
, struct page
*page
, block_t blkaddr
)
1744 struct f2fs_sb_info
*sbi
= F2FS_I_SB(inode
);
1745 nid_t prev_xnid
= F2FS_I(inode
)->i_xattr_nid
;
1746 nid_t new_xnid
= nid_of_node(page
);
1747 struct node_info ni
;
1749 /* 1: invalidate the previous xattr nid */
1753 /* Deallocate node address */
1754 get_node_info(sbi
, prev_xnid
, &ni
);
1755 f2fs_bug_on(sbi
, ni
.blk_addr
== NULL_ADDR
);
1756 invalidate_blocks(sbi
, ni
.blk_addr
);
1757 dec_valid_node_count(sbi
, inode
);
1758 set_node_addr(sbi
, &ni
, NULL_ADDR
, false);
1761 /* 2: allocate new xattr nid */
1762 if (unlikely(!inc_valid_node_count(sbi
, inode
)))
1763 f2fs_bug_on(sbi
, 1);
1765 remove_free_nid(NM_I(sbi
), new_xnid
);
1766 get_node_info(sbi
, new_xnid
, &ni
);
1767 ni
.ino
= inode
->i_ino
;
1768 set_node_addr(sbi
, &ni
, NEW_ADDR
, false);
1769 F2FS_I(inode
)->i_xattr_nid
= new_xnid
;
1771 /* 3: update xattr blkaddr */
1772 refresh_sit_entry(sbi
, NEW_ADDR
, blkaddr
);
1773 set_node_addr(sbi
, &ni
, blkaddr
, false);
1775 update_inode_page(inode
);
1778 int recover_inode_page(struct f2fs_sb_info
*sbi
, struct page
*page
)
1780 struct f2fs_inode
*src
, *dst
;
1781 nid_t ino
= ino_of_node(page
);
1782 struct node_info old_ni
, new_ni
;
1785 get_node_info(sbi
, ino
, &old_ni
);
1787 if (unlikely(old_ni
.blk_addr
!= NULL_ADDR
))
1790 ipage
= grab_cache_page(NODE_MAPPING(sbi
), ino
);
1794 /* Should not use this inode from free nid list */
1795 remove_free_nid(NM_I(sbi
), ino
);
1797 SetPageUptodate(ipage
);
1798 fill_node_footer(ipage
, ino
, ino
, 0, true);
1800 src
= F2FS_INODE(page
);
1801 dst
= F2FS_INODE(ipage
);
1803 memcpy(dst
, src
, (unsigned long)&src
->i_ext
- (unsigned long)src
);
1805 dst
->i_blocks
= cpu_to_le64(1);
1806 dst
->i_links
= cpu_to_le32(1);
1807 dst
->i_xattr_nid
= 0;
1808 dst
->i_inline
= src
->i_inline
& F2FS_INLINE_XATTR
;
1813 if (unlikely(!inc_valid_node_count(sbi
, NULL
)))
1815 set_node_addr(sbi
, &new_ni
, NEW_ADDR
, false);
1816 inc_valid_inode_count(sbi
);
1817 set_page_dirty(ipage
);
1818 f2fs_put_page(ipage
, 1);
1822 int restore_node_summary(struct f2fs_sb_info
*sbi
,
1823 unsigned int segno
, struct f2fs_summary_block
*sum
)
1825 struct f2fs_node
*rn
;
1826 struct f2fs_summary
*sum_entry
;
1828 int bio_blocks
= MAX_BIO_BLOCKS(sbi
);
1829 int i
, idx
, last_offset
, nrpages
;
1831 /* scan the node segment */
1832 last_offset
= sbi
->blocks_per_seg
;
1833 addr
= START_BLOCK(sbi
, segno
);
1834 sum_entry
= &sum
->entries
[0];
1836 for (i
= 0; i
< last_offset
; i
+= nrpages
, addr
+= nrpages
) {
1837 nrpages
= min(last_offset
- i
, bio_blocks
);
1839 /* readahead node pages */
1840 ra_meta_pages(sbi
, addr
, nrpages
, META_POR
, true);
1842 for (idx
= addr
; idx
< addr
+ nrpages
; idx
++) {
1843 struct page
*page
= get_tmp_page(sbi
, idx
);
1845 rn
= F2FS_NODE(page
);
1846 sum_entry
->nid
= rn
->footer
.nid
;
1847 sum_entry
->version
= 0;
1848 sum_entry
->ofs_in_node
= 0;
1850 f2fs_put_page(page
, 1);
1853 invalidate_mapping_pages(META_MAPPING(sbi
), addr
,
1859 static void remove_nats_in_journal(struct f2fs_sb_info
*sbi
)
1861 struct f2fs_nm_info
*nm_i
= NM_I(sbi
);
1862 struct curseg_info
*curseg
= CURSEG_I(sbi
, CURSEG_HOT_DATA
);
1863 struct f2fs_summary_block
*sum
= curseg
->sum_blk
;
1866 mutex_lock(&curseg
->curseg_mutex
);
1867 for (i
= 0; i
< nats_in_cursum(sum
); i
++) {
1868 struct nat_entry
*ne
;
1869 struct f2fs_nat_entry raw_ne
;
1870 nid_t nid
= le32_to_cpu(nid_in_journal(sum
, i
));
1872 raw_ne
= nat_in_journal(sum
, i
);
1874 ne
= __lookup_nat_cache(nm_i
, nid
);
1876 ne
= grab_nat_entry(nm_i
, nid
);
1877 node_info_from_raw_nat(&ne
->ni
, &raw_ne
);
1879 __set_nat_cache_dirty(nm_i
, ne
);
1881 update_nats_in_cursum(sum
, -i
);
1882 mutex_unlock(&curseg
->curseg_mutex
);
1885 static void __adjust_nat_entry_set(struct nat_entry_set
*nes
,
1886 struct list_head
*head
, int max
)
1888 struct nat_entry_set
*cur
;
1890 if (nes
->entry_cnt
>= max
)
1893 list_for_each_entry(cur
, head
, set_list
) {
1894 if (cur
->entry_cnt
>= nes
->entry_cnt
) {
1895 list_add(&nes
->set_list
, cur
->set_list
.prev
);
1900 list_add_tail(&nes
->set_list
, head
);
1903 static void __flush_nat_entry_set(struct f2fs_sb_info
*sbi
,
1904 struct nat_entry_set
*set
)
1906 struct curseg_info
*curseg
= CURSEG_I(sbi
, CURSEG_HOT_DATA
);
1907 struct f2fs_summary_block
*sum
= curseg
->sum_blk
;
1908 nid_t start_nid
= set
->set
* NAT_ENTRY_PER_BLOCK
;
1909 bool to_journal
= true;
1910 struct f2fs_nat_block
*nat_blk
;
1911 struct nat_entry
*ne
, *cur
;
1912 struct page
*page
= NULL
;
1915 * there are two steps to flush nat entries:
1916 * #1, flush nat entries to journal in current hot data summary block.
1917 * #2, flush nat entries to nat page.
1919 if (!__has_cursum_space(sum
, set
->entry_cnt
, NAT_JOURNAL
))
1923 mutex_lock(&curseg
->curseg_mutex
);
1925 page
= get_next_nat_page(sbi
, start_nid
);
1926 nat_blk
= page_address(page
);
1927 f2fs_bug_on(sbi
, !nat_blk
);
1930 /* flush dirty nats in nat entry set */
1931 list_for_each_entry_safe(ne
, cur
, &set
->entry_list
, list
) {
1932 struct f2fs_nat_entry
*raw_ne
;
1933 nid_t nid
= nat_get_nid(ne
);
1936 if (nat_get_blkaddr(ne
) == NEW_ADDR
)
1940 offset
= lookup_journal_in_cursum(sum
,
1941 NAT_JOURNAL
, nid
, 1);
1942 f2fs_bug_on(sbi
, offset
< 0);
1943 raw_ne
= &nat_in_journal(sum
, offset
);
1944 nid_in_journal(sum
, offset
) = cpu_to_le32(nid
);
1946 raw_ne
= &nat_blk
->entries
[nid
- start_nid
];
1948 raw_nat_from_node_info(raw_ne
, &ne
->ni
);
1950 __clear_nat_cache_dirty(NM_I(sbi
), ne
);
1951 if (nat_get_blkaddr(ne
) == NULL_ADDR
)
1952 add_free_nid(sbi
, nid
, false);
1956 mutex_unlock(&curseg
->curseg_mutex
);
1958 f2fs_put_page(page
, 1);
1960 f2fs_bug_on(sbi
, set
->entry_cnt
);
1962 radix_tree_delete(&NM_I(sbi
)->nat_set_root
, set
->set
);
1963 kmem_cache_free(nat_entry_set_slab
, set
);
1967 * This function is called during the checkpointing process.
1969 void flush_nat_entries(struct f2fs_sb_info
*sbi
)
1971 struct f2fs_nm_info
*nm_i
= NM_I(sbi
);
1972 struct curseg_info
*curseg
= CURSEG_I(sbi
, CURSEG_HOT_DATA
);
1973 struct f2fs_summary_block
*sum
= curseg
->sum_blk
;
1974 struct nat_entry_set
*setvec
[SETVEC_SIZE
];
1975 struct nat_entry_set
*set
, *tmp
;
1980 if (!nm_i
->dirty_nat_cnt
)
1983 down_write(&nm_i
->nat_tree_lock
);
1986 * if there are no enough space in journal to store dirty nat
1987 * entries, remove all entries from journal and merge them
1988 * into nat entry set.
1990 if (!__has_cursum_space(sum
, nm_i
->dirty_nat_cnt
, NAT_JOURNAL
))
1991 remove_nats_in_journal(sbi
);
1993 while ((found
= __gang_lookup_nat_set(nm_i
,
1994 set_idx
, SETVEC_SIZE
, setvec
))) {
1996 set_idx
= setvec
[found
- 1]->set
+ 1;
1997 for (idx
= 0; idx
< found
; idx
++)
1998 __adjust_nat_entry_set(setvec
[idx
], &sets
,
1999 MAX_NAT_JENTRIES(sum
));
2002 /* flush dirty nats in nat entry set */
2003 list_for_each_entry_safe(set
, tmp
, &sets
, set_list
)
2004 __flush_nat_entry_set(sbi
, set
);
2006 up_write(&nm_i
->nat_tree_lock
);
2008 f2fs_bug_on(sbi
, nm_i
->dirty_nat_cnt
);
2011 static int init_node_manager(struct f2fs_sb_info
*sbi
)
2013 struct f2fs_super_block
*sb_raw
= F2FS_RAW_SUPER(sbi
);
2014 struct f2fs_nm_info
*nm_i
= NM_I(sbi
);
2015 unsigned char *version_bitmap
;
2016 unsigned int nat_segs
, nat_blocks
;
2018 nm_i
->nat_blkaddr
= le32_to_cpu(sb_raw
->nat_blkaddr
);
2020 /* segment_count_nat includes pair segment so divide to 2. */
2021 nat_segs
= le32_to_cpu(sb_raw
->segment_count_nat
) >> 1;
2022 nat_blocks
= nat_segs
<< le32_to_cpu(sb_raw
->log_blocks_per_seg
);
2024 nm_i
->max_nid
= NAT_ENTRY_PER_BLOCK
* nat_blocks
;
2026 /* not used nids: 0, node, meta, (and root counted as valid node) */
2027 nm_i
->available_nids
= nm_i
->max_nid
- F2FS_RESERVED_NODE_NUM
;
2030 nm_i
->ram_thresh
= DEF_RAM_THRESHOLD
;
2031 nm_i
->ra_nid_pages
= DEF_RA_NID_PAGES
;
2033 INIT_RADIX_TREE(&nm_i
->free_nid_root
, GFP_ATOMIC
);
2034 INIT_LIST_HEAD(&nm_i
->free_nid_list
);
2035 INIT_RADIX_TREE(&nm_i
->nat_root
, GFP_NOIO
);
2036 INIT_RADIX_TREE(&nm_i
->nat_set_root
, GFP_NOIO
);
2037 INIT_LIST_HEAD(&nm_i
->nat_entries
);
2039 mutex_init(&nm_i
->build_lock
);
2040 spin_lock_init(&nm_i
->free_nid_list_lock
);
2041 init_rwsem(&nm_i
->nat_tree_lock
);
2043 nm_i
->next_scan_nid
= le32_to_cpu(sbi
->ckpt
->next_free_nid
);
2044 nm_i
->bitmap_size
= __bitmap_size(sbi
, NAT_BITMAP
);
2045 version_bitmap
= __bitmap_ptr(sbi
, NAT_BITMAP
);
2046 if (!version_bitmap
)
2049 nm_i
->nat_bitmap
= kmemdup(version_bitmap
, nm_i
->bitmap_size
,
2051 if (!nm_i
->nat_bitmap
)
2056 int build_node_manager(struct f2fs_sb_info
*sbi
)
2060 sbi
->nm_info
= kzalloc(sizeof(struct f2fs_nm_info
), GFP_KERNEL
);
2064 err
= init_node_manager(sbi
);
2068 build_free_nids(sbi
);
2072 void destroy_node_manager(struct f2fs_sb_info
*sbi
)
2074 struct f2fs_nm_info
*nm_i
= NM_I(sbi
);
2075 struct free_nid
*i
, *next_i
;
2076 struct nat_entry
*natvec
[NATVEC_SIZE
];
2077 struct nat_entry_set
*setvec
[SETVEC_SIZE
];
2084 /* destroy free nid list */
2085 spin_lock(&nm_i
->free_nid_list_lock
);
2086 list_for_each_entry_safe(i
, next_i
, &nm_i
->free_nid_list
, list
) {
2087 f2fs_bug_on(sbi
, i
->state
== NID_ALLOC
);
2088 __del_from_free_nid_list(nm_i
, i
);
2090 spin_unlock(&nm_i
->free_nid_list_lock
);
2091 kmem_cache_free(free_nid_slab
, i
);
2092 spin_lock(&nm_i
->free_nid_list_lock
);
2094 f2fs_bug_on(sbi
, nm_i
->fcnt
);
2095 spin_unlock(&nm_i
->free_nid_list_lock
);
2097 /* destroy nat cache */
2098 down_write(&nm_i
->nat_tree_lock
);
2099 while ((found
= __gang_lookup_nat_cache(nm_i
,
2100 nid
, NATVEC_SIZE
, natvec
))) {
2103 nid
= nat_get_nid(natvec
[found
- 1]) + 1;
2104 for (idx
= 0; idx
< found
; idx
++)
2105 __del_from_nat_cache(nm_i
, natvec
[idx
]);
2107 f2fs_bug_on(sbi
, nm_i
->nat_cnt
);
2109 /* destroy nat set cache */
2111 while ((found
= __gang_lookup_nat_set(nm_i
,
2112 nid
, SETVEC_SIZE
, setvec
))) {
2115 nid
= setvec
[found
- 1]->set
+ 1;
2116 for (idx
= 0; idx
< found
; idx
++) {
2117 /* entry_cnt is not zero, when cp_error was occurred */
2118 f2fs_bug_on(sbi
, !list_empty(&setvec
[idx
]->entry_list
));
2119 radix_tree_delete(&nm_i
->nat_set_root
, setvec
[idx
]->set
);
2120 kmem_cache_free(nat_entry_set_slab
, setvec
[idx
]);
2123 up_write(&nm_i
->nat_tree_lock
);
2125 kfree(nm_i
->nat_bitmap
);
2126 sbi
->nm_info
= NULL
;
2130 int __init
create_node_manager_caches(void)
2132 nat_entry_slab
= f2fs_kmem_cache_create("nat_entry",
2133 sizeof(struct nat_entry
));
2134 if (!nat_entry_slab
)
2137 free_nid_slab
= f2fs_kmem_cache_create("free_nid",
2138 sizeof(struct free_nid
));
2140 goto destroy_nat_entry
;
2142 nat_entry_set_slab
= f2fs_kmem_cache_create("nat_entry_set",
2143 sizeof(struct nat_entry_set
));
2144 if (!nat_entry_set_slab
)
2145 goto destroy_free_nid
;
2149 kmem_cache_destroy(free_nid_slab
);
2151 kmem_cache_destroy(nat_entry_slab
);
2156 void destroy_node_manager_caches(void)
2158 kmem_cache_destroy(nat_entry_set_slab
);
2159 kmem_cache_destroy(free_nid_slab
);
2160 kmem_cache_destroy(nat_entry_slab
);