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
->nid_cnt
[FREE_NID_LIST
] *
49 sizeof(struct free_nid
)) >> PAGE_SHIFT
;
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 if (excess_cached_nats(sbi
))
57 } else if (type
== DIRTY_DENTS
) {
58 if (sbi
->sb
->s_bdi
->wb
.dirty_exceeded
)
60 mem_size
= get_pages(sbi
, F2FS_DIRTY_DENTS
);
61 res
= mem_size
< ((avail_ram
* nm_i
->ram_thresh
/ 100) >> 1);
62 } else if (type
== INO_ENTRIES
) {
65 for (i
= 0; i
<= UPDATE_INO
; i
++)
66 mem_size
+= sbi
->im
[i
].ino_num
*
67 sizeof(struct ino_entry
);
68 mem_size
>>= PAGE_SHIFT
;
69 res
= mem_size
< ((avail_ram
* nm_i
->ram_thresh
/ 100) >> 1);
70 } else if (type
== EXTENT_CACHE
) {
71 mem_size
= (atomic_read(&sbi
->total_ext_tree
) *
72 sizeof(struct extent_tree
) +
73 atomic_read(&sbi
->total_ext_node
) *
74 sizeof(struct extent_node
)) >> PAGE_SHIFT
;
75 res
= mem_size
< ((avail_ram
* nm_i
->ram_thresh
/ 100) >> 1);
77 if (!sbi
->sb
->s_bdi
->wb
.dirty_exceeded
)
83 static void clear_node_page_dirty(struct page
*page
)
85 struct address_space
*mapping
= page
->mapping
;
86 unsigned int long flags
;
88 if (PageDirty(page
)) {
89 spin_lock_irqsave(&mapping
->tree_lock
, flags
);
90 radix_tree_tag_clear(&mapping
->page_tree
,
93 spin_unlock_irqrestore(&mapping
->tree_lock
, flags
);
95 clear_page_dirty_for_io(page
);
96 dec_page_count(F2FS_M_SB(mapping
), F2FS_DIRTY_NODES
);
98 ClearPageUptodate(page
);
101 static struct page
*get_current_nat_page(struct f2fs_sb_info
*sbi
, nid_t nid
)
103 pgoff_t index
= current_nat_addr(sbi
, nid
);
104 return get_meta_page(sbi
, index
);
107 static struct page
*get_next_nat_page(struct f2fs_sb_info
*sbi
, nid_t nid
)
109 struct page
*src_page
;
110 struct page
*dst_page
;
115 struct f2fs_nm_info
*nm_i
= NM_I(sbi
);
117 src_off
= current_nat_addr(sbi
, nid
);
118 dst_off
= next_nat_addr(sbi
, src_off
);
120 /* get current nat block page with lock */
121 src_page
= get_meta_page(sbi
, src_off
);
122 dst_page
= grab_meta_page(sbi
, dst_off
);
123 f2fs_bug_on(sbi
, PageDirty(src_page
));
125 src_addr
= page_address(src_page
);
126 dst_addr
= page_address(dst_page
);
127 memcpy(dst_addr
, src_addr
, PAGE_SIZE
);
128 set_page_dirty(dst_page
);
129 f2fs_put_page(src_page
, 1);
131 set_to_next_nat(nm_i
, nid
);
136 static struct nat_entry
*__lookup_nat_cache(struct f2fs_nm_info
*nm_i
, nid_t n
)
138 return radix_tree_lookup(&nm_i
->nat_root
, n
);
141 static unsigned int __gang_lookup_nat_cache(struct f2fs_nm_info
*nm_i
,
142 nid_t start
, unsigned int nr
, struct nat_entry
**ep
)
144 return radix_tree_gang_lookup(&nm_i
->nat_root
, (void **)ep
, start
, nr
);
147 static void __del_from_nat_cache(struct f2fs_nm_info
*nm_i
, struct nat_entry
*e
)
150 radix_tree_delete(&nm_i
->nat_root
, nat_get_nid(e
));
152 kmem_cache_free(nat_entry_slab
, e
);
155 static void __set_nat_cache_dirty(struct f2fs_nm_info
*nm_i
,
156 struct nat_entry
*ne
)
158 nid_t set
= NAT_BLOCK_OFFSET(ne
->ni
.nid
);
159 struct nat_entry_set
*head
;
161 if (get_nat_flag(ne
, IS_DIRTY
))
164 head
= radix_tree_lookup(&nm_i
->nat_set_root
, set
);
166 head
= f2fs_kmem_cache_alloc(nat_entry_set_slab
, GFP_NOFS
);
168 INIT_LIST_HEAD(&head
->entry_list
);
169 INIT_LIST_HEAD(&head
->set_list
);
172 f2fs_radix_tree_insert(&nm_i
->nat_set_root
, set
, head
);
174 list_move_tail(&ne
->list
, &head
->entry_list
);
175 nm_i
->dirty_nat_cnt
++;
177 set_nat_flag(ne
, IS_DIRTY
, true);
180 static void __clear_nat_cache_dirty(struct f2fs_nm_info
*nm_i
,
181 struct nat_entry_set
*set
, struct nat_entry
*ne
)
183 list_move_tail(&ne
->list
, &nm_i
->nat_entries
);
184 set_nat_flag(ne
, IS_DIRTY
, false);
186 nm_i
->dirty_nat_cnt
--;
189 static unsigned int __gang_lookup_nat_set(struct f2fs_nm_info
*nm_i
,
190 nid_t start
, unsigned int nr
, struct nat_entry_set
**ep
)
192 return radix_tree_gang_lookup(&nm_i
->nat_set_root
, (void **)ep
,
196 int need_dentry_mark(struct f2fs_sb_info
*sbi
, nid_t nid
)
198 struct f2fs_nm_info
*nm_i
= NM_I(sbi
);
202 down_read(&nm_i
->nat_tree_lock
);
203 e
= __lookup_nat_cache(nm_i
, nid
);
205 if (!get_nat_flag(e
, IS_CHECKPOINTED
) &&
206 !get_nat_flag(e
, HAS_FSYNCED_INODE
))
209 up_read(&nm_i
->nat_tree_lock
);
213 bool is_checkpointed_node(struct f2fs_sb_info
*sbi
, nid_t nid
)
215 struct f2fs_nm_info
*nm_i
= NM_I(sbi
);
219 down_read(&nm_i
->nat_tree_lock
);
220 e
= __lookup_nat_cache(nm_i
, nid
);
221 if (e
&& !get_nat_flag(e
, IS_CHECKPOINTED
))
223 up_read(&nm_i
->nat_tree_lock
);
227 bool need_inode_block_update(struct f2fs_sb_info
*sbi
, nid_t ino
)
229 struct f2fs_nm_info
*nm_i
= NM_I(sbi
);
231 bool need_update
= true;
233 down_read(&nm_i
->nat_tree_lock
);
234 e
= __lookup_nat_cache(nm_i
, ino
);
235 if (e
&& get_nat_flag(e
, HAS_LAST_FSYNC
) &&
236 (get_nat_flag(e
, IS_CHECKPOINTED
) ||
237 get_nat_flag(e
, HAS_FSYNCED_INODE
)))
239 up_read(&nm_i
->nat_tree_lock
);
243 static struct nat_entry
*grab_nat_entry(struct f2fs_nm_info
*nm_i
, nid_t nid
,
246 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);
252 new = kmem_cache_alloc(nat_entry_slab
, GFP_NOFS
);
255 if (radix_tree_insert(&nm_i
->nat_root
, nid
, new)) {
256 kmem_cache_free(nat_entry_slab
, new);
261 memset(new, 0, sizeof(struct nat_entry
));
262 nat_set_nid(new, nid
);
264 list_add_tail(&new->list
, &nm_i
->nat_entries
);
269 static void cache_nat_entry(struct f2fs_sb_info
*sbi
, nid_t nid
,
270 struct f2fs_nat_entry
*ne
)
272 struct f2fs_nm_info
*nm_i
= NM_I(sbi
);
275 e
= __lookup_nat_cache(nm_i
, nid
);
277 e
= grab_nat_entry(nm_i
, nid
, false);
279 node_info_from_raw_nat(&e
->ni
, ne
);
281 f2fs_bug_on(sbi
, nat_get_ino(e
) != le32_to_cpu(ne
->ino
) ||
282 nat_get_blkaddr(e
) !=
283 le32_to_cpu(ne
->block_addr
) ||
284 nat_get_version(e
) != ne
->version
);
288 static void set_node_addr(struct f2fs_sb_info
*sbi
, struct node_info
*ni
,
289 block_t new_blkaddr
, bool fsync_done
)
291 struct f2fs_nm_info
*nm_i
= NM_I(sbi
);
294 down_write(&nm_i
->nat_tree_lock
);
295 e
= __lookup_nat_cache(nm_i
, ni
->nid
);
297 e
= grab_nat_entry(nm_i
, ni
->nid
, true);
298 copy_node_info(&e
->ni
, ni
);
299 f2fs_bug_on(sbi
, ni
->blk_addr
== NEW_ADDR
);
300 } else if (new_blkaddr
== NEW_ADDR
) {
302 * when nid is reallocated,
303 * previous nat entry can be remained in nat cache.
304 * So, reinitialize it with new information.
306 copy_node_info(&e
->ni
, ni
);
307 f2fs_bug_on(sbi
, ni
->blk_addr
!= NULL_ADDR
);
311 f2fs_bug_on(sbi
, nat_get_blkaddr(e
) != ni
->blk_addr
);
312 f2fs_bug_on(sbi
, nat_get_blkaddr(e
) == NULL_ADDR
&&
313 new_blkaddr
== NULL_ADDR
);
314 f2fs_bug_on(sbi
, nat_get_blkaddr(e
) == NEW_ADDR
&&
315 new_blkaddr
== NEW_ADDR
);
316 f2fs_bug_on(sbi
, nat_get_blkaddr(e
) != NEW_ADDR
&&
317 nat_get_blkaddr(e
) != NULL_ADDR
&&
318 new_blkaddr
== NEW_ADDR
);
320 /* increment version no as node is removed */
321 if (nat_get_blkaddr(e
) != NEW_ADDR
&& new_blkaddr
== NULL_ADDR
) {
322 unsigned char version
= nat_get_version(e
);
323 nat_set_version(e
, inc_node_version(version
));
325 /* in order to reuse the nid */
326 if (nm_i
->next_scan_nid
> ni
->nid
)
327 nm_i
->next_scan_nid
= ni
->nid
;
331 nat_set_blkaddr(e
, new_blkaddr
);
332 if (new_blkaddr
== NEW_ADDR
|| new_blkaddr
== NULL_ADDR
)
333 set_nat_flag(e
, IS_CHECKPOINTED
, false);
334 __set_nat_cache_dirty(nm_i
, e
);
336 /* update fsync_mark if its inode nat entry is still alive */
337 if (ni
->nid
!= ni
->ino
)
338 e
= __lookup_nat_cache(nm_i
, ni
->ino
);
340 if (fsync_done
&& ni
->nid
== ni
->ino
)
341 set_nat_flag(e
, HAS_FSYNCED_INODE
, true);
342 set_nat_flag(e
, HAS_LAST_FSYNC
, fsync_done
);
344 up_write(&nm_i
->nat_tree_lock
);
347 int try_to_free_nats(struct f2fs_sb_info
*sbi
, int nr_shrink
)
349 struct f2fs_nm_info
*nm_i
= NM_I(sbi
);
352 if (!down_write_trylock(&nm_i
->nat_tree_lock
))
355 while (nr_shrink
&& !list_empty(&nm_i
->nat_entries
)) {
356 struct nat_entry
*ne
;
357 ne
= list_first_entry(&nm_i
->nat_entries
,
358 struct nat_entry
, list
);
359 __del_from_nat_cache(nm_i
, ne
);
362 up_write(&nm_i
->nat_tree_lock
);
363 return nr
- nr_shrink
;
367 * This function always returns success
369 void get_node_info(struct f2fs_sb_info
*sbi
, nid_t nid
, struct node_info
*ni
)
371 struct f2fs_nm_info
*nm_i
= NM_I(sbi
);
372 struct curseg_info
*curseg
= CURSEG_I(sbi
, CURSEG_HOT_DATA
);
373 struct f2fs_journal
*journal
= curseg
->journal
;
374 nid_t start_nid
= START_NID(nid
);
375 struct f2fs_nat_block
*nat_blk
;
376 struct page
*page
= NULL
;
377 struct f2fs_nat_entry ne
;
383 /* Check nat cache */
384 down_read(&nm_i
->nat_tree_lock
);
385 e
= __lookup_nat_cache(nm_i
, nid
);
387 ni
->ino
= nat_get_ino(e
);
388 ni
->blk_addr
= nat_get_blkaddr(e
);
389 ni
->version
= nat_get_version(e
);
390 up_read(&nm_i
->nat_tree_lock
);
394 memset(&ne
, 0, sizeof(struct f2fs_nat_entry
));
396 /* Check current segment summary */
397 down_read(&curseg
->journal_rwsem
);
398 i
= lookup_journal_in_cursum(journal
, NAT_JOURNAL
, nid
, 0);
400 ne
= nat_in_journal(journal
, i
);
401 node_info_from_raw_nat(ni
, &ne
);
403 up_read(&curseg
->journal_rwsem
);
407 /* Fill node_info from nat page */
408 page
= get_current_nat_page(sbi
, start_nid
);
409 nat_blk
= (struct f2fs_nat_block
*)page_address(page
);
410 ne
= nat_blk
->entries
[nid
- start_nid
];
411 node_info_from_raw_nat(ni
, &ne
);
412 f2fs_put_page(page
, 1);
414 up_read(&nm_i
->nat_tree_lock
);
415 /* cache nat entry */
416 down_write(&nm_i
->nat_tree_lock
);
417 cache_nat_entry(sbi
, nid
, &ne
);
418 up_write(&nm_i
->nat_tree_lock
);
422 * readahead MAX_RA_NODE number of node pages.
424 static void ra_node_pages(struct page
*parent
, int start
, int n
)
426 struct f2fs_sb_info
*sbi
= F2FS_P_SB(parent
);
427 struct blk_plug plug
;
431 blk_start_plug(&plug
);
433 /* Then, try readahead for siblings of the desired node */
435 end
= min(end
, NIDS_PER_BLOCK
);
436 for (i
= start
; i
< end
; i
++) {
437 nid
= get_nid(parent
, i
, false);
438 ra_node_page(sbi
, nid
);
441 blk_finish_plug(&plug
);
444 pgoff_t
get_next_page_offset(struct dnode_of_data
*dn
, pgoff_t pgofs
)
446 const long direct_index
= ADDRS_PER_INODE(dn
->inode
);
447 const long direct_blks
= ADDRS_PER_BLOCK
;
448 const long indirect_blks
= ADDRS_PER_BLOCK
* NIDS_PER_BLOCK
;
449 unsigned int skipped_unit
= ADDRS_PER_BLOCK
;
450 int cur_level
= dn
->cur_level
;
451 int max_level
= dn
->max_level
;
457 while (max_level
-- > cur_level
)
458 skipped_unit
*= NIDS_PER_BLOCK
;
460 switch (dn
->max_level
) {
462 base
+= 2 * indirect_blks
;
464 base
+= 2 * direct_blks
;
466 base
+= direct_index
;
469 f2fs_bug_on(F2FS_I_SB(dn
->inode
), 1);
472 return ((pgofs
- base
) / skipped_unit
+ 1) * skipped_unit
+ base
;
476 * The maximum depth is four.
477 * Offset[0] will have raw inode offset.
479 static int get_node_path(struct inode
*inode
, long block
,
480 int offset
[4], unsigned int noffset
[4])
482 const long direct_index
= ADDRS_PER_INODE(inode
);
483 const long direct_blks
= ADDRS_PER_BLOCK
;
484 const long dptrs_per_blk
= NIDS_PER_BLOCK
;
485 const long indirect_blks
= ADDRS_PER_BLOCK
* NIDS_PER_BLOCK
;
486 const long dindirect_blks
= indirect_blks
* NIDS_PER_BLOCK
;
492 if (block
< direct_index
) {
496 block
-= direct_index
;
497 if (block
< direct_blks
) {
498 offset
[n
++] = NODE_DIR1_BLOCK
;
504 block
-= direct_blks
;
505 if (block
< direct_blks
) {
506 offset
[n
++] = NODE_DIR2_BLOCK
;
512 block
-= direct_blks
;
513 if (block
< indirect_blks
) {
514 offset
[n
++] = NODE_IND1_BLOCK
;
516 offset
[n
++] = block
/ direct_blks
;
517 noffset
[n
] = 4 + offset
[n
- 1];
518 offset
[n
] = block
% direct_blks
;
522 block
-= indirect_blks
;
523 if (block
< indirect_blks
) {
524 offset
[n
++] = NODE_IND2_BLOCK
;
525 noffset
[n
] = 4 + dptrs_per_blk
;
526 offset
[n
++] = block
/ direct_blks
;
527 noffset
[n
] = 5 + dptrs_per_blk
+ offset
[n
- 1];
528 offset
[n
] = block
% direct_blks
;
532 block
-= indirect_blks
;
533 if (block
< dindirect_blks
) {
534 offset
[n
++] = NODE_DIND_BLOCK
;
535 noffset
[n
] = 5 + (dptrs_per_blk
* 2);
536 offset
[n
++] = block
/ indirect_blks
;
537 noffset
[n
] = 6 + (dptrs_per_blk
* 2) +
538 offset
[n
- 1] * (dptrs_per_blk
+ 1);
539 offset
[n
++] = (block
/ direct_blks
) % dptrs_per_blk
;
540 noffset
[n
] = 7 + (dptrs_per_blk
* 2) +
541 offset
[n
- 2] * (dptrs_per_blk
+ 1) +
543 offset
[n
] = block
% direct_blks
;
554 * Caller should call f2fs_put_dnode(dn).
555 * Also, it should grab and release a rwsem by calling f2fs_lock_op() and
556 * f2fs_unlock_op() only if ro is not set RDONLY_NODE.
557 * In the case of RDONLY_NODE, we don't need to care about mutex.
559 int get_dnode_of_data(struct dnode_of_data
*dn
, pgoff_t index
, int mode
)
561 struct f2fs_sb_info
*sbi
= F2FS_I_SB(dn
->inode
);
562 struct page
*npage
[4];
563 struct page
*parent
= NULL
;
565 unsigned int noffset
[4];
570 level
= get_node_path(dn
->inode
, index
, offset
, noffset
);
572 nids
[0] = dn
->inode
->i_ino
;
573 npage
[0] = dn
->inode_page
;
576 npage
[0] = get_node_page(sbi
, nids
[0]);
577 if (IS_ERR(npage
[0]))
578 return PTR_ERR(npage
[0]);
581 /* if inline_data is set, should not report any block indices */
582 if (f2fs_has_inline_data(dn
->inode
) && index
) {
584 f2fs_put_page(npage
[0], 1);
590 nids
[1] = get_nid(parent
, offset
[0], true);
591 dn
->inode_page
= npage
[0];
592 dn
->inode_page_locked
= true;
594 /* get indirect or direct nodes */
595 for (i
= 1; i
<= level
; i
++) {
598 if (!nids
[i
] && mode
== ALLOC_NODE
) {
600 if (!alloc_nid(sbi
, &(nids
[i
]))) {
606 npage
[i
] = new_node_page(dn
, noffset
[i
], NULL
);
607 if (IS_ERR(npage
[i
])) {
608 alloc_nid_failed(sbi
, nids
[i
]);
609 err
= PTR_ERR(npage
[i
]);
613 set_nid(parent
, offset
[i
- 1], nids
[i
], i
== 1);
614 alloc_nid_done(sbi
, nids
[i
]);
616 } else if (mode
== LOOKUP_NODE_RA
&& i
== level
&& level
> 1) {
617 npage
[i
] = get_node_page_ra(parent
, offset
[i
- 1]);
618 if (IS_ERR(npage
[i
])) {
619 err
= PTR_ERR(npage
[i
]);
625 dn
->inode_page_locked
= false;
628 f2fs_put_page(parent
, 1);
632 npage
[i
] = get_node_page(sbi
, nids
[i
]);
633 if (IS_ERR(npage
[i
])) {
634 err
= PTR_ERR(npage
[i
]);
635 f2fs_put_page(npage
[0], 0);
641 nids
[i
+ 1] = get_nid(parent
, offset
[i
], false);
644 dn
->nid
= nids
[level
];
645 dn
->ofs_in_node
= offset
[level
];
646 dn
->node_page
= npage
[level
];
647 dn
->data_blkaddr
= datablock_addr(dn
->node_page
, dn
->ofs_in_node
);
651 f2fs_put_page(parent
, 1);
653 f2fs_put_page(npage
[0], 0);
655 dn
->inode_page
= NULL
;
656 dn
->node_page
= NULL
;
657 if (err
== -ENOENT
) {
659 dn
->max_level
= level
;
660 dn
->ofs_in_node
= offset
[level
];
665 static void truncate_node(struct dnode_of_data
*dn
)
667 struct f2fs_sb_info
*sbi
= F2FS_I_SB(dn
->inode
);
670 get_node_info(sbi
, dn
->nid
, &ni
);
671 if (dn
->inode
->i_blocks
== 0) {
672 f2fs_bug_on(sbi
, ni
.blk_addr
!= NULL_ADDR
);
675 f2fs_bug_on(sbi
, ni
.blk_addr
== NULL_ADDR
);
677 /* Deallocate node address */
678 invalidate_blocks(sbi
, ni
.blk_addr
);
679 dec_valid_node_count(sbi
, dn
->inode
);
680 set_node_addr(sbi
, &ni
, NULL_ADDR
, false);
682 if (dn
->nid
== dn
->inode
->i_ino
) {
683 remove_orphan_inode(sbi
, dn
->nid
);
684 dec_valid_inode_count(sbi
);
685 f2fs_inode_synced(dn
->inode
);
688 clear_node_page_dirty(dn
->node_page
);
689 set_sbi_flag(sbi
, SBI_IS_DIRTY
);
691 f2fs_put_page(dn
->node_page
, 1);
693 invalidate_mapping_pages(NODE_MAPPING(sbi
),
694 dn
->node_page
->index
, dn
->node_page
->index
);
696 dn
->node_page
= NULL
;
697 trace_f2fs_truncate_node(dn
->inode
, dn
->nid
, ni
.blk_addr
);
700 static int truncate_dnode(struct dnode_of_data
*dn
)
707 /* get direct node */
708 page
= get_node_page(F2FS_I_SB(dn
->inode
), dn
->nid
);
709 if (IS_ERR(page
) && PTR_ERR(page
) == -ENOENT
)
711 else if (IS_ERR(page
))
712 return PTR_ERR(page
);
714 /* Make dnode_of_data for parameter */
715 dn
->node_page
= page
;
717 truncate_data_blocks(dn
);
722 static int truncate_nodes(struct dnode_of_data
*dn
, unsigned int nofs
,
725 struct dnode_of_data rdn
= *dn
;
727 struct f2fs_node
*rn
;
729 unsigned int child_nofs
;
734 return NIDS_PER_BLOCK
+ 1;
736 trace_f2fs_truncate_nodes_enter(dn
->inode
, dn
->nid
, dn
->data_blkaddr
);
738 page
= get_node_page(F2FS_I_SB(dn
->inode
), dn
->nid
);
740 trace_f2fs_truncate_nodes_exit(dn
->inode
, PTR_ERR(page
));
741 return PTR_ERR(page
);
744 ra_node_pages(page
, ofs
, NIDS_PER_BLOCK
);
746 rn
= F2FS_NODE(page
);
748 for (i
= ofs
; i
< NIDS_PER_BLOCK
; i
++, freed
++) {
749 child_nid
= le32_to_cpu(rn
->in
.nid
[i
]);
753 ret
= truncate_dnode(&rdn
);
756 if (set_nid(page
, i
, 0, false))
757 dn
->node_changed
= true;
760 child_nofs
= nofs
+ ofs
* (NIDS_PER_BLOCK
+ 1) + 1;
761 for (i
= ofs
; i
< NIDS_PER_BLOCK
; i
++) {
762 child_nid
= le32_to_cpu(rn
->in
.nid
[i
]);
763 if (child_nid
== 0) {
764 child_nofs
+= NIDS_PER_BLOCK
+ 1;
768 ret
= truncate_nodes(&rdn
, child_nofs
, 0, depth
- 1);
769 if (ret
== (NIDS_PER_BLOCK
+ 1)) {
770 if (set_nid(page
, i
, 0, false))
771 dn
->node_changed
= true;
773 } else if (ret
< 0 && ret
!= -ENOENT
) {
781 /* remove current indirect node */
782 dn
->node_page
= page
;
786 f2fs_put_page(page
, 1);
788 trace_f2fs_truncate_nodes_exit(dn
->inode
, freed
);
792 f2fs_put_page(page
, 1);
793 trace_f2fs_truncate_nodes_exit(dn
->inode
, ret
);
797 static int truncate_partial_nodes(struct dnode_of_data
*dn
,
798 struct f2fs_inode
*ri
, int *offset
, int depth
)
800 struct page
*pages
[2];
807 nid
[0] = le32_to_cpu(ri
->i_nid
[offset
[0] - NODE_DIR1_BLOCK
]);
811 /* get indirect nodes in the path */
812 for (i
= 0; i
< idx
+ 1; i
++) {
813 /* reference count'll be increased */
814 pages
[i
] = get_node_page(F2FS_I_SB(dn
->inode
), nid
[i
]);
815 if (IS_ERR(pages
[i
])) {
816 err
= PTR_ERR(pages
[i
]);
820 nid
[i
+ 1] = get_nid(pages
[i
], offset
[i
+ 1], false);
823 ra_node_pages(pages
[idx
], offset
[idx
+ 1], NIDS_PER_BLOCK
);
825 /* free direct nodes linked to a partial indirect node */
826 for (i
= offset
[idx
+ 1]; i
< NIDS_PER_BLOCK
; i
++) {
827 child_nid
= get_nid(pages
[idx
], i
, false);
831 err
= truncate_dnode(dn
);
834 if (set_nid(pages
[idx
], i
, 0, false))
835 dn
->node_changed
= true;
838 if (offset
[idx
+ 1] == 0) {
839 dn
->node_page
= pages
[idx
];
843 f2fs_put_page(pages
[idx
], 1);
849 for (i
= idx
; i
>= 0; i
--)
850 f2fs_put_page(pages
[i
], 1);
852 trace_f2fs_truncate_partial_nodes(dn
->inode
, nid
, depth
, err
);
858 * All the block addresses of data and nodes should be nullified.
860 int truncate_inode_blocks(struct inode
*inode
, pgoff_t from
)
862 struct f2fs_sb_info
*sbi
= F2FS_I_SB(inode
);
863 int err
= 0, cont
= 1;
864 int level
, offset
[4], noffset
[4];
865 unsigned int nofs
= 0;
866 struct f2fs_inode
*ri
;
867 struct dnode_of_data dn
;
870 trace_f2fs_truncate_inode_blocks_enter(inode
, from
);
872 level
= get_node_path(inode
, from
, offset
, noffset
);
874 page
= get_node_page(sbi
, inode
->i_ino
);
876 trace_f2fs_truncate_inode_blocks_exit(inode
, PTR_ERR(page
));
877 return PTR_ERR(page
);
880 set_new_dnode(&dn
, inode
, page
, NULL
, 0);
883 ri
= F2FS_INODE(page
);
891 if (!offset
[level
- 1])
893 err
= truncate_partial_nodes(&dn
, ri
, offset
, level
);
894 if (err
< 0 && err
!= -ENOENT
)
896 nofs
+= 1 + NIDS_PER_BLOCK
;
899 nofs
= 5 + 2 * NIDS_PER_BLOCK
;
900 if (!offset
[level
- 1])
902 err
= truncate_partial_nodes(&dn
, ri
, offset
, level
);
903 if (err
< 0 && err
!= -ENOENT
)
912 dn
.nid
= le32_to_cpu(ri
->i_nid
[offset
[0] - NODE_DIR1_BLOCK
]);
914 case NODE_DIR1_BLOCK
:
915 case NODE_DIR2_BLOCK
:
916 err
= truncate_dnode(&dn
);
919 case NODE_IND1_BLOCK
:
920 case NODE_IND2_BLOCK
:
921 err
= truncate_nodes(&dn
, nofs
, offset
[1], 2);
924 case NODE_DIND_BLOCK
:
925 err
= truncate_nodes(&dn
, nofs
, offset
[1], 3);
932 if (err
< 0 && err
!= -ENOENT
)
934 if (offset
[1] == 0 &&
935 ri
->i_nid
[offset
[0] - NODE_DIR1_BLOCK
]) {
937 BUG_ON(page
->mapping
!= NODE_MAPPING(sbi
));
938 f2fs_wait_on_page_writeback(page
, NODE
, true);
939 ri
->i_nid
[offset
[0] - NODE_DIR1_BLOCK
] = 0;
940 set_page_dirty(page
);
948 f2fs_put_page(page
, 0);
949 trace_f2fs_truncate_inode_blocks_exit(inode
, err
);
950 return err
> 0 ? 0 : err
;
953 int truncate_xattr_node(struct inode
*inode
, struct page
*page
)
955 struct f2fs_sb_info
*sbi
= F2FS_I_SB(inode
);
956 nid_t nid
= F2FS_I(inode
)->i_xattr_nid
;
957 struct dnode_of_data dn
;
963 npage
= get_node_page(sbi
, nid
);
965 return PTR_ERR(npage
);
967 f2fs_i_xnid_write(inode
, 0);
969 set_new_dnode(&dn
, inode
, page
, npage
, nid
);
972 dn
.inode_page_locked
= true;
978 * Caller should grab and release a rwsem by calling f2fs_lock_op() and
981 int remove_inode_page(struct inode
*inode
)
983 struct dnode_of_data dn
;
986 set_new_dnode(&dn
, inode
, NULL
, NULL
, inode
->i_ino
);
987 err
= get_dnode_of_data(&dn
, 0, LOOKUP_NODE
);
991 err
= truncate_xattr_node(inode
, dn
.inode_page
);
997 /* remove potential inline_data blocks */
998 if (S_ISREG(inode
->i_mode
) || S_ISDIR(inode
->i_mode
) ||
999 S_ISLNK(inode
->i_mode
))
1000 truncate_data_blocks_range(&dn
, 1);
1002 /* 0 is possible, after f2fs_new_inode() has failed */
1003 f2fs_bug_on(F2FS_I_SB(inode
),
1004 inode
->i_blocks
!= 0 && inode
->i_blocks
!= 1);
1006 /* will put inode & node pages */
1011 struct page
*new_inode_page(struct inode
*inode
)
1013 struct dnode_of_data dn
;
1015 /* allocate inode page for new inode */
1016 set_new_dnode(&dn
, inode
, NULL
, NULL
, inode
->i_ino
);
1018 /* caller should f2fs_put_page(page, 1); */
1019 return new_node_page(&dn
, 0, NULL
);
1022 struct page
*new_node_page(struct dnode_of_data
*dn
,
1023 unsigned int ofs
, struct page
*ipage
)
1025 struct f2fs_sb_info
*sbi
= F2FS_I_SB(dn
->inode
);
1026 struct node_info new_ni
;
1030 if (unlikely(is_inode_flag_set(dn
->inode
, FI_NO_ALLOC
)))
1031 return ERR_PTR(-EPERM
);
1033 page
= f2fs_grab_cache_page(NODE_MAPPING(sbi
), dn
->nid
, false);
1035 return ERR_PTR(-ENOMEM
);
1037 if (unlikely(!inc_valid_node_count(sbi
, dn
->inode
))) {
1041 #ifdef CONFIG_F2FS_CHECK_FS
1042 get_node_info(sbi
, dn
->nid
, &new_ni
);
1043 f2fs_bug_on(sbi
, new_ni
.blk_addr
!= NULL_ADDR
);
1045 new_ni
.nid
= dn
->nid
;
1046 new_ni
.ino
= dn
->inode
->i_ino
;
1047 new_ni
.blk_addr
= NULL_ADDR
;
1050 set_node_addr(sbi
, &new_ni
, NEW_ADDR
, false);
1052 f2fs_wait_on_page_writeback(page
, NODE
, true);
1053 fill_node_footer(page
, dn
->nid
, dn
->inode
->i_ino
, ofs
, true);
1054 set_cold_node(dn
->inode
, page
);
1055 if (!PageUptodate(page
))
1056 SetPageUptodate(page
);
1057 if (set_page_dirty(page
))
1058 dn
->node_changed
= true;
1060 if (f2fs_has_xattr_block(ofs
))
1061 f2fs_i_xnid_write(dn
->inode
, dn
->nid
);
1064 inc_valid_inode_count(sbi
);
1068 clear_node_page_dirty(page
);
1069 f2fs_put_page(page
, 1);
1070 return ERR_PTR(err
);
1074 * Caller should do after getting the following values.
1075 * 0: f2fs_put_page(page, 0)
1076 * LOCKED_PAGE or error: f2fs_put_page(page, 1)
1078 static int read_node_page(struct page
*page
, int op_flags
)
1080 struct f2fs_sb_info
*sbi
= F2FS_P_SB(page
);
1081 struct node_info ni
;
1082 struct f2fs_io_info fio
= {
1086 .op_flags
= op_flags
,
1088 .encrypted_page
= NULL
,
1091 if (PageUptodate(page
))
1094 get_node_info(sbi
, page
->index
, &ni
);
1096 if (unlikely(ni
.blk_addr
== NULL_ADDR
)) {
1097 ClearPageUptodate(page
);
1101 fio
.new_blkaddr
= fio
.old_blkaddr
= ni
.blk_addr
;
1102 return f2fs_submit_page_bio(&fio
);
1106 * Readahead a node page
1108 void ra_node_page(struct f2fs_sb_info
*sbi
, nid_t nid
)
1115 f2fs_bug_on(sbi
, check_nid_range(sbi
, nid
));
1118 apage
= radix_tree_lookup(&NODE_MAPPING(sbi
)->page_tree
, nid
);
1123 apage
= f2fs_grab_cache_page(NODE_MAPPING(sbi
), nid
, false);
1127 err
= read_node_page(apage
, REQ_RAHEAD
);
1128 f2fs_put_page(apage
, err
? 1 : 0);
1131 static struct page
*__get_node_page(struct f2fs_sb_info
*sbi
, pgoff_t nid
,
1132 struct page
*parent
, int start
)
1138 return ERR_PTR(-ENOENT
);
1139 f2fs_bug_on(sbi
, check_nid_range(sbi
, nid
));
1141 page
= f2fs_grab_cache_page(NODE_MAPPING(sbi
), nid
, false);
1143 return ERR_PTR(-ENOMEM
);
1145 err
= read_node_page(page
, 0);
1147 f2fs_put_page(page
, 1);
1148 return ERR_PTR(err
);
1149 } else if (err
== LOCKED_PAGE
) {
1154 ra_node_pages(parent
, start
+ 1, MAX_RA_NODE
);
1158 if (unlikely(page
->mapping
!= NODE_MAPPING(sbi
))) {
1159 f2fs_put_page(page
, 1);
1163 if (unlikely(!PageUptodate(page
)))
1166 if(unlikely(nid
!= nid_of_node(page
))) {
1167 f2fs_bug_on(sbi
, 1);
1168 ClearPageUptodate(page
);
1170 f2fs_put_page(page
, 1);
1171 return ERR_PTR(-EIO
);
1176 struct page
*get_node_page(struct f2fs_sb_info
*sbi
, pgoff_t nid
)
1178 return __get_node_page(sbi
, nid
, NULL
, 0);
1181 struct page
*get_node_page_ra(struct page
*parent
, int start
)
1183 struct f2fs_sb_info
*sbi
= F2FS_P_SB(parent
);
1184 nid_t nid
= get_nid(parent
, start
, false);
1186 return __get_node_page(sbi
, nid
, parent
, start
);
1189 static void flush_inline_data(struct f2fs_sb_info
*sbi
, nid_t ino
)
1191 struct inode
*inode
;
1195 /* should flush inline_data before evict_inode */
1196 inode
= ilookup(sbi
->sb
, ino
);
1200 page
= pagecache_get_page(inode
->i_mapping
, 0, FGP_LOCK
|FGP_NOWAIT
, 0);
1204 if (!PageUptodate(page
))
1207 if (!PageDirty(page
))
1210 if (!clear_page_dirty_for_io(page
))
1213 ret
= f2fs_write_inline_data(inode
, page
);
1214 inode_dec_dirty_pages(inode
);
1215 remove_dirty_inode(inode
);
1217 set_page_dirty(page
);
1219 f2fs_put_page(page
, 1);
1224 void move_node_page(struct page
*node_page
, int gc_type
)
1226 if (gc_type
== FG_GC
) {
1227 struct f2fs_sb_info
*sbi
= F2FS_P_SB(node_page
);
1228 struct writeback_control wbc
= {
1229 .sync_mode
= WB_SYNC_ALL
,
1234 set_page_dirty(node_page
);
1235 f2fs_wait_on_page_writeback(node_page
, NODE
, true);
1237 f2fs_bug_on(sbi
, PageWriteback(node_page
));
1238 if (!clear_page_dirty_for_io(node_page
))
1241 if (NODE_MAPPING(sbi
)->a_ops
->writepage(node_page
, &wbc
))
1242 unlock_page(node_page
);
1245 /* set page dirty and write it */
1246 if (!PageWriteback(node_page
))
1247 set_page_dirty(node_page
);
1250 unlock_page(node_page
);
1252 f2fs_put_page(node_page
, 0);
1255 static struct page
*last_fsync_dnode(struct f2fs_sb_info
*sbi
, nid_t ino
)
1258 struct pagevec pvec
;
1259 struct page
*last_page
= NULL
;
1261 pagevec_init(&pvec
, 0);
1265 while (index
<= end
) {
1267 nr_pages
= pagevec_lookup_tag(&pvec
, NODE_MAPPING(sbi
), &index
,
1268 PAGECACHE_TAG_DIRTY
,
1269 min(end
- index
, (pgoff_t
)PAGEVEC_SIZE
-1) + 1);
1273 for (i
= 0; i
< nr_pages
; i
++) {
1274 struct page
*page
= pvec
.pages
[i
];
1276 if (unlikely(f2fs_cp_error(sbi
))) {
1277 f2fs_put_page(last_page
, 0);
1278 pagevec_release(&pvec
);
1279 return ERR_PTR(-EIO
);
1282 if (!IS_DNODE(page
) || !is_cold_node(page
))
1284 if (ino_of_node(page
) != ino
)
1289 if (unlikely(page
->mapping
!= NODE_MAPPING(sbi
))) {
1294 if (ino_of_node(page
) != ino
)
1295 goto continue_unlock
;
1297 if (!PageDirty(page
)) {
1298 /* someone wrote it for us */
1299 goto continue_unlock
;
1303 f2fs_put_page(last_page
, 0);
1309 pagevec_release(&pvec
);
1315 static int __write_node_page(struct page
*page
, bool atomic
, bool *submitted
,
1316 struct writeback_control
*wbc
)
1318 struct f2fs_sb_info
*sbi
= F2FS_P_SB(page
);
1320 struct node_info ni
;
1321 struct f2fs_io_info fio
= {
1325 .op_flags
= wbc_to_write_flags(wbc
),
1327 .encrypted_page
= NULL
,
1331 trace_f2fs_writepage(page
, NODE
);
1333 if (unlikely(is_sbi_flag_set(sbi
, SBI_POR_DOING
)))
1335 if (unlikely(f2fs_cp_error(sbi
)))
1338 /* get old block addr of this node page */
1339 nid
= nid_of_node(page
);
1340 f2fs_bug_on(sbi
, page
->index
!= nid
);
1342 if (wbc
->for_reclaim
) {
1343 if (!down_read_trylock(&sbi
->node_write
))
1346 down_read(&sbi
->node_write
);
1349 get_node_info(sbi
, nid
, &ni
);
1351 /* This page is already truncated */
1352 if (unlikely(ni
.blk_addr
== NULL_ADDR
)) {
1353 ClearPageUptodate(page
);
1354 dec_page_count(sbi
, F2FS_DIRTY_NODES
);
1355 up_read(&sbi
->node_write
);
1360 if (atomic
&& !test_opt(sbi
, NOBARRIER
))
1361 fio
.op_flags
|= REQ_PREFLUSH
| REQ_FUA
;
1363 set_page_writeback(page
);
1364 fio
.old_blkaddr
= ni
.blk_addr
;
1365 write_node_page(nid
, &fio
);
1366 set_node_addr(sbi
, &ni
, fio
.new_blkaddr
, is_fsync_dnode(page
));
1367 dec_page_count(sbi
, F2FS_DIRTY_NODES
);
1368 up_read(&sbi
->node_write
);
1370 if (wbc
->for_reclaim
) {
1371 f2fs_submit_merged_bio_cond(sbi
, page
->mapping
->host
, 0,
1372 page
->index
, NODE
, WRITE
);
1378 if (unlikely(f2fs_cp_error(sbi
))) {
1379 f2fs_submit_merged_bio(sbi
, NODE
, WRITE
);
1383 *submitted
= fio
.submitted
;
1388 redirty_page_for_writepage(wbc
, page
);
1389 return AOP_WRITEPAGE_ACTIVATE
;
1392 static int f2fs_write_node_page(struct page
*page
,
1393 struct writeback_control
*wbc
)
1395 return __write_node_page(page
, false, NULL
, wbc
);
1398 int fsync_node_pages(struct f2fs_sb_info
*sbi
, struct inode
*inode
,
1399 struct writeback_control
*wbc
, bool atomic
)
1402 pgoff_t last_idx
= ULONG_MAX
;
1403 struct pagevec pvec
;
1405 struct page
*last_page
= NULL
;
1406 bool marked
= false;
1407 nid_t ino
= inode
->i_ino
;
1410 last_page
= last_fsync_dnode(sbi
, ino
);
1411 if (IS_ERR_OR_NULL(last_page
))
1412 return PTR_ERR_OR_ZERO(last_page
);
1415 pagevec_init(&pvec
, 0);
1419 while (index
<= end
) {
1421 nr_pages
= pagevec_lookup_tag(&pvec
, NODE_MAPPING(sbi
), &index
,
1422 PAGECACHE_TAG_DIRTY
,
1423 min(end
- index
, (pgoff_t
)PAGEVEC_SIZE
-1) + 1);
1427 for (i
= 0; i
< nr_pages
; i
++) {
1428 struct page
*page
= pvec
.pages
[i
];
1429 bool submitted
= false;
1431 if (unlikely(f2fs_cp_error(sbi
))) {
1432 f2fs_put_page(last_page
, 0);
1433 pagevec_release(&pvec
);
1438 if (!IS_DNODE(page
) || !is_cold_node(page
))
1440 if (ino_of_node(page
) != ino
)
1445 if (unlikely(page
->mapping
!= NODE_MAPPING(sbi
))) {
1450 if (ino_of_node(page
) != ino
)
1451 goto continue_unlock
;
1453 if (!PageDirty(page
) && page
!= last_page
) {
1454 /* someone wrote it for us */
1455 goto continue_unlock
;
1458 f2fs_wait_on_page_writeback(page
, NODE
, true);
1459 BUG_ON(PageWriteback(page
));
1461 set_fsync_mark(page
, 0);
1462 set_dentry_mark(page
, 0);
1464 if (!atomic
|| page
== last_page
) {
1465 set_fsync_mark(page
, 1);
1466 if (IS_INODE(page
)) {
1467 if (is_inode_flag_set(inode
,
1469 update_inode(inode
, page
);
1470 set_dentry_mark(page
,
1471 need_dentry_mark(sbi
, ino
));
1473 /* may be written by other thread */
1474 if (!PageDirty(page
))
1475 set_page_dirty(page
);
1478 if (!clear_page_dirty_for_io(page
))
1479 goto continue_unlock
;
1481 ret
= __write_node_page(page
, atomic
&&
1486 f2fs_put_page(last_page
, 0);
1488 } else if (submitted
) {
1489 last_idx
= page
->index
;
1492 if (page
== last_page
) {
1493 f2fs_put_page(page
, 0);
1498 pagevec_release(&pvec
);
1504 if (!ret
&& atomic
&& !marked
) {
1505 f2fs_msg(sbi
->sb
, KERN_DEBUG
,
1506 "Retry to write fsync mark: ino=%u, idx=%lx",
1507 ino
, last_page
->index
);
1508 lock_page(last_page
);
1509 f2fs_wait_on_page_writeback(last_page
, NODE
, true);
1510 set_page_dirty(last_page
);
1511 unlock_page(last_page
);
1515 if (last_idx
!= ULONG_MAX
)
1516 f2fs_submit_merged_bio_cond(sbi
, NULL
, ino
, last_idx
,
1518 return ret
? -EIO
: 0;
1521 int sync_node_pages(struct f2fs_sb_info
*sbi
, struct writeback_control
*wbc
)
1524 struct pagevec pvec
;
1529 pagevec_init(&pvec
, 0);
1535 while (index
<= end
) {
1537 nr_pages
= pagevec_lookup_tag(&pvec
, NODE_MAPPING(sbi
), &index
,
1538 PAGECACHE_TAG_DIRTY
,
1539 min(end
- index
, (pgoff_t
)PAGEVEC_SIZE
-1) + 1);
1543 for (i
= 0; i
< nr_pages
; i
++) {
1544 struct page
*page
= pvec
.pages
[i
];
1545 bool submitted
= false;
1547 if (unlikely(f2fs_cp_error(sbi
))) {
1548 pagevec_release(&pvec
);
1554 * flushing sequence with step:
1559 if (step
== 0 && IS_DNODE(page
))
1561 if (step
== 1 && (!IS_DNODE(page
) ||
1562 is_cold_node(page
)))
1564 if (step
== 2 && (!IS_DNODE(page
) ||
1565 !is_cold_node(page
)))
1568 if (!trylock_page(page
))
1571 if (unlikely(page
->mapping
!= NODE_MAPPING(sbi
))) {
1577 if (!PageDirty(page
)) {
1578 /* someone wrote it for us */
1579 goto continue_unlock
;
1582 /* flush inline_data */
1583 if (is_inline_node(page
)) {
1584 clear_inline_node(page
);
1586 flush_inline_data(sbi
, ino_of_node(page
));
1590 f2fs_wait_on_page_writeback(page
, NODE
, true);
1592 BUG_ON(PageWriteback(page
));
1593 if (!clear_page_dirty_for_io(page
))
1594 goto continue_unlock
;
1596 set_fsync_mark(page
, 0);
1597 set_dentry_mark(page
, 0);
1599 ret
= __write_node_page(page
, false, &submitted
, wbc
);
1605 if (--wbc
->nr_to_write
== 0)
1608 pagevec_release(&pvec
);
1611 if (wbc
->nr_to_write
== 0) {
1623 f2fs_submit_merged_bio(sbi
, NODE
, WRITE
);
1627 int wait_on_node_pages_writeback(struct f2fs_sb_info
*sbi
, nid_t ino
)
1629 pgoff_t index
= 0, end
= ULONG_MAX
;
1630 struct pagevec pvec
;
1633 pagevec_init(&pvec
, 0);
1635 while (index
<= end
) {
1637 nr_pages
= pagevec_lookup_tag(&pvec
, NODE_MAPPING(sbi
), &index
,
1638 PAGECACHE_TAG_WRITEBACK
,
1639 min(end
- index
, (pgoff_t
)PAGEVEC_SIZE
-1) + 1);
1643 for (i
= 0; i
< nr_pages
; i
++) {
1644 struct page
*page
= pvec
.pages
[i
];
1646 /* until radix tree lookup accepts end_index */
1647 if (unlikely(page
->index
> end
))
1650 if (ino
&& ino_of_node(page
) == ino
) {
1651 f2fs_wait_on_page_writeback(page
, NODE
, true);
1652 if (TestClearPageError(page
))
1656 pagevec_release(&pvec
);
1660 ret2
= filemap_check_errors(NODE_MAPPING(sbi
));
1666 static int f2fs_write_node_pages(struct address_space
*mapping
,
1667 struct writeback_control
*wbc
)
1669 struct f2fs_sb_info
*sbi
= F2FS_M_SB(mapping
);
1670 struct blk_plug plug
;
1673 /* balancing f2fs's metadata in background */
1674 f2fs_balance_fs_bg(sbi
);
1676 /* collect a number of dirty node pages and write together */
1677 if (get_pages(sbi
, F2FS_DIRTY_NODES
) < nr_pages_to_skip(sbi
, NODE
))
1680 trace_f2fs_writepages(mapping
->host
, wbc
, NODE
);
1682 diff
= nr_pages_to_write(sbi
, NODE
, wbc
);
1683 wbc
->sync_mode
= WB_SYNC_NONE
;
1684 blk_start_plug(&plug
);
1685 sync_node_pages(sbi
, wbc
);
1686 blk_finish_plug(&plug
);
1687 wbc
->nr_to_write
= max((long)0, wbc
->nr_to_write
- diff
);
1691 wbc
->pages_skipped
+= get_pages(sbi
, F2FS_DIRTY_NODES
);
1692 trace_f2fs_writepages(mapping
->host
, wbc
, NODE
);
1696 static int f2fs_set_node_page_dirty(struct page
*page
)
1698 trace_f2fs_set_page_dirty(page
, NODE
);
1700 if (!PageUptodate(page
))
1701 SetPageUptodate(page
);
1702 if (!PageDirty(page
)) {
1703 f2fs_set_page_dirty_nobuffers(page
);
1704 inc_page_count(F2FS_P_SB(page
), F2FS_DIRTY_NODES
);
1705 SetPagePrivate(page
);
1706 f2fs_trace_pid(page
);
1713 * Structure of the f2fs node operations
1715 const struct address_space_operations f2fs_node_aops
= {
1716 .writepage
= f2fs_write_node_page
,
1717 .writepages
= f2fs_write_node_pages
,
1718 .set_page_dirty
= f2fs_set_node_page_dirty
,
1719 .invalidatepage
= f2fs_invalidate_page
,
1720 .releasepage
= f2fs_release_page
,
1721 #ifdef CONFIG_MIGRATION
1722 .migratepage
= f2fs_migrate_page
,
1726 static struct free_nid
*__lookup_free_nid_list(struct f2fs_nm_info
*nm_i
,
1729 return radix_tree_lookup(&nm_i
->free_nid_root
, n
);
1732 static int __insert_nid_to_list(struct f2fs_sb_info
*sbi
,
1733 struct free_nid
*i
, enum nid_list list
, bool new)
1735 struct f2fs_nm_info
*nm_i
= NM_I(sbi
);
1738 int err
= radix_tree_insert(&nm_i
->free_nid_root
, i
->nid
, i
);
1743 f2fs_bug_on(sbi
, list
== FREE_NID_LIST
? i
->state
!= NID_NEW
:
1744 i
->state
!= NID_ALLOC
);
1745 nm_i
->nid_cnt
[list
]++;
1746 list_add_tail(&i
->list
, &nm_i
->nid_list
[list
]);
1750 static void __remove_nid_from_list(struct f2fs_sb_info
*sbi
,
1751 struct free_nid
*i
, enum nid_list list
, bool reuse
)
1753 struct f2fs_nm_info
*nm_i
= NM_I(sbi
);
1755 f2fs_bug_on(sbi
, list
== FREE_NID_LIST
? i
->state
!= NID_NEW
:
1756 i
->state
!= NID_ALLOC
);
1757 nm_i
->nid_cnt
[list
]--;
1760 radix_tree_delete(&nm_i
->free_nid_root
, i
->nid
);
1763 /* return if the nid is recognized as free */
1764 static bool add_free_nid(struct f2fs_sb_info
*sbi
, nid_t nid
, bool build
)
1766 struct f2fs_nm_info
*nm_i
= NM_I(sbi
);
1767 struct free_nid
*i
, *e
;
1768 struct nat_entry
*ne
;
1772 /* 0 nid should not be used */
1773 if (unlikely(nid
== 0))
1776 i
= f2fs_kmem_cache_alloc(free_nid_slab
, GFP_NOFS
);
1780 if (radix_tree_preload(GFP_NOFS
))
1783 spin_lock(&nm_i
->nid_list_lock
);
1791 * - __insert_nid_to_list(ALLOC_NID_LIST)
1792 * - f2fs_balance_fs_bg
1794 * - __build_free_nids
1797 * - __lookup_nat_cache
1799 * - init_inode_metadata
1804 * - __remove_nid_from_list(ALLOC_NID_LIST)
1805 * - __insert_nid_to_list(FREE_NID_LIST)
1807 ne
= __lookup_nat_cache(nm_i
, nid
);
1808 if (ne
&& (!get_nat_flag(ne
, IS_CHECKPOINTED
) ||
1809 nat_get_blkaddr(ne
) != NULL_ADDR
))
1812 e
= __lookup_free_nid_list(nm_i
, nid
);
1814 if (e
->state
== NID_NEW
)
1820 err
= __insert_nid_to_list(sbi
, i
, FREE_NID_LIST
, true);
1822 spin_unlock(&nm_i
->nid_list_lock
);
1823 radix_tree_preload_end();
1826 kmem_cache_free(free_nid_slab
, i
);
1830 static void remove_free_nid(struct f2fs_sb_info
*sbi
, nid_t nid
)
1832 struct f2fs_nm_info
*nm_i
= NM_I(sbi
);
1834 bool need_free
= false;
1836 spin_lock(&nm_i
->nid_list_lock
);
1837 i
= __lookup_free_nid_list(nm_i
, nid
);
1838 if (i
&& i
->state
== NID_NEW
) {
1839 __remove_nid_from_list(sbi
, i
, FREE_NID_LIST
, false);
1842 spin_unlock(&nm_i
->nid_list_lock
);
1845 kmem_cache_free(free_nid_slab
, i
);
1848 static void update_free_nid_bitmap(struct f2fs_sb_info
*sbi
, nid_t nid
,
1849 bool set
, bool build
)
1851 struct f2fs_nm_info
*nm_i
= NM_I(sbi
);
1852 unsigned int nat_ofs
= NAT_BLOCK_OFFSET(nid
);
1853 unsigned int nid_ofs
= nid
- START_NID(nid
);
1855 if (!test_bit_le(nat_ofs
, nm_i
->nat_block_bitmap
))
1859 __set_bit_le(nid_ofs
, nm_i
->free_nid_bitmap
[nat_ofs
]);
1861 __clear_bit_le(nid_ofs
, nm_i
->free_nid_bitmap
[nat_ofs
]);
1864 nm_i
->free_nid_count
[nat_ofs
]++;
1866 nm_i
->free_nid_count
[nat_ofs
]--;
1869 static void scan_nat_page(struct f2fs_sb_info
*sbi
,
1870 struct page
*nat_page
, nid_t start_nid
)
1872 struct f2fs_nm_info
*nm_i
= NM_I(sbi
);
1873 struct f2fs_nat_block
*nat_blk
= page_address(nat_page
);
1875 unsigned int nat_ofs
= NAT_BLOCK_OFFSET(start_nid
);
1878 if (test_bit_le(nat_ofs
, nm_i
->nat_block_bitmap
))
1881 __set_bit_le(nat_ofs
, nm_i
->nat_block_bitmap
);
1883 i
= start_nid
% NAT_ENTRY_PER_BLOCK
;
1885 for (; i
< NAT_ENTRY_PER_BLOCK
; i
++, start_nid
++) {
1888 if (unlikely(start_nid
>= nm_i
->max_nid
))
1891 blk_addr
= le32_to_cpu(nat_blk
->entries
[i
].block_addr
);
1892 f2fs_bug_on(sbi
, blk_addr
== NEW_ADDR
);
1893 if (blk_addr
== NULL_ADDR
)
1894 freed
= add_free_nid(sbi
, start_nid
, true);
1895 spin_lock(&NM_I(sbi
)->nid_list_lock
);
1896 update_free_nid_bitmap(sbi
, start_nid
, freed
, true);
1897 spin_unlock(&NM_I(sbi
)->nid_list_lock
);
1901 static void scan_free_nid_bits(struct f2fs_sb_info
*sbi
)
1903 struct f2fs_nm_info
*nm_i
= NM_I(sbi
);
1904 struct curseg_info
*curseg
= CURSEG_I(sbi
, CURSEG_HOT_DATA
);
1905 struct f2fs_journal
*journal
= curseg
->journal
;
1906 unsigned int i
, idx
;
1908 down_read(&nm_i
->nat_tree_lock
);
1910 for (i
= 0; i
< nm_i
->nat_blocks
; i
++) {
1911 if (!test_bit_le(i
, nm_i
->nat_block_bitmap
))
1913 if (!nm_i
->free_nid_count
[i
])
1915 for (idx
= 0; idx
< NAT_ENTRY_PER_BLOCK
; idx
++) {
1918 if (!test_bit_le(idx
, nm_i
->free_nid_bitmap
[i
]))
1921 nid
= i
* NAT_ENTRY_PER_BLOCK
+ idx
;
1922 add_free_nid(sbi
, nid
, true);
1924 if (nm_i
->nid_cnt
[FREE_NID_LIST
] >= MAX_FREE_NIDS
)
1929 down_read(&curseg
->journal_rwsem
);
1930 for (i
= 0; i
< nats_in_cursum(journal
); i
++) {
1934 addr
= le32_to_cpu(nat_in_journal(journal
, i
).block_addr
);
1935 nid
= le32_to_cpu(nid_in_journal(journal
, i
));
1936 if (addr
== NULL_ADDR
)
1937 add_free_nid(sbi
, nid
, true);
1939 remove_free_nid(sbi
, nid
);
1941 up_read(&curseg
->journal_rwsem
);
1942 up_read(&nm_i
->nat_tree_lock
);
1945 static void __build_free_nids(struct f2fs_sb_info
*sbi
, bool sync
, bool mount
)
1947 struct f2fs_nm_info
*nm_i
= NM_I(sbi
);
1948 struct curseg_info
*curseg
= CURSEG_I(sbi
, CURSEG_HOT_DATA
);
1949 struct f2fs_journal
*journal
= curseg
->journal
;
1951 nid_t nid
= nm_i
->next_scan_nid
;
1953 /* Enough entries */
1954 if (nm_i
->nid_cnt
[FREE_NID_LIST
] >= NAT_ENTRY_PER_BLOCK
)
1957 if (!sync
&& !available_free_memory(sbi
, FREE_NIDS
))
1961 /* try to find free nids in free_nid_bitmap */
1962 scan_free_nid_bits(sbi
);
1964 if (nm_i
->nid_cnt
[FREE_NID_LIST
])
1968 /* readahead nat pages to be scanned */
1969 ra_meta_pages(sbi
, NAT_BLOCK_OFFSET(nid
), FREE_NID_PAGES
,
1972 down_read(&nm_i
->nat_tree_lock
);
1975 struct page
*page
= get_current_nat_page(sbi
, nid
);
1977 scan_nat_page(sbi
, page
, nid
);
1978 f2fs_put_page(page
, 1);
1980 nid
+= (NAT_ENTRY_PER_BLOCK
- (nid
% NAT_ENTRY_PER_BLOCK
));
1981 if (unlikely(nid
>= nm_i
->max_nid
))
1984 if (++i
>= FREE_NID_PAGES
)
1988 /* go to the next free nat pages to find free nids abundantly */
1989 nm_i
->next_scan_nid
= nid
;
1991 /* find free nids from current sum_pages */
1992 down_read(&curseg
->journal_rwsem
);
1993 for (i
= 0; i
< nats_in_cursum(journal
); i
++) {
1996 addr
= le32_to_cpu(nat_in_journal(journal
, i
).block_addr
);
1997 nid
= le32_to_cpu(nid_in_journal(journal
, i
));
1998 if (addr
== NULL_ADDR
)
1999 add_free_nid(sbi
, nid
, true);
2001 remove_free_nid(sbi
, nid
);
2003 up_read(&curseg
->journal_rwsem
);
2004 up_read(&nm_i
->nat_tree_lock
);
2006 ra_meta_pages(sbi
, NAT_BLOCK_OFFSET(nm_i
->next_scan_nid
),
2007 nm_i
->ra_nid_pages
, META_NAT
, false);
2010 void build_free_nids(struct f2fs_sb_info
*sbi
, bool sync
, bool mount
)
2012 mutex_lock(&NM_I(sbi
)->build_lock
);
2013 __build_free_nids(sbi
, sync
, mount
);
2014 mutex_unlock(&NM_I(sbi
)->build_lock
);
2018 * If this function returns success, caller can obtain a new nid
2019 * from second parameter of this function.
2020 * The returned nid could be used ino as well as nid when inode is created.
2022 bool alloc_nid(struct f2fs_sb_info
*sbi
, nid_t
*nid
)
2024 struct f2fs_nm_info
*nm_i
= NM_I(sbi
);
2025 struct free_nid
*i
= NULL
;
2027 #ifdef CONFIG_F2FS_FAULT_INJECTION
2028 if (time_to_inject(sbi
, FAULT_ALLOC_NID
)) {
2029 f2fs_show_injection_info(FAULT_ALLOC_NID
);
2033 spin_lock(&nm_i
->nid_list_lock
);
2035 if (unlikely(nm_i
->available_nids
== 0)) {
2036 spin_unlock(&nm_i
->nid_list_lock
);
2040 /* We should not use stale free nids created by build_free_nids */
2041 if (nm_i
->nid_cnt
[FREE_NID_LIST
] && !on_build_free_nids(nm_i
)) {
2042 f2fs_bug_on(sbi
, list_empty(&nm_i
->nid_list
[FREE_NID_LIST
]));
2043 i
= list_first_entry(&nm_i
->nid_list
[FREE_NID_LIST
],
2044 struct free_nid
, list
);
2047 __remove_nid_from_list(sbi
, i
, FREE_NID_LIST
, true);
2048 i
->state
= NID_ALLOC
;
2049 __insert_nid_to_list(sbi
, i
, ALLOC_NID_LIST
, false);
2050 nm_i
->available_nids
--;
2052 update_free_nid_bitmap(sbi
, *nid
, false, false);
2054 spin_unlock(&nm_i
->nid_list_lock
);
2057 spin_unlock(&nm_i
->nid_list_lock
);
2059 /* Let's scan nat pages and its caches to get free nids */
2060 build_free_nids(sbi
, true, false);
2065 * alloc_nid() should be called prior to this function.
2067 void alloc_nid_done(struct f2fs_sb_info
*sbi
, nid_t nid
)
2069 struct f2fs_nm_info
*nm_i
= NM_I(sbi
);
2072 spin_lock(&nm_i
->nid_list_lock
);
2073 i
= __lookup_free_nid_list(nm_i
, nid
);
2074 f2fs_bug_on(sbi
, !i
);
2075 __remove_nid_from_list(sbi
, i
, ALLOC_NID_LIST
, false);
2076 spin_unlock(&nm_i
->nid_list_lock
);
2078 kmem_cache_free(free_nid_slab
, i
);
2082 * alloc_nid() should be called prior to this function.
2084 void alloc_nid_failed(struct f2fs_sb_info
*sbi
, nid_t nid
)
2086 struct f2fs_nm_info
*nm_i
= NM_I(sbi
);
2088 bool need_free
= false;
2093 spin_lock(&nm_i
->nid_list_lock
);
2094 i
= __lookup_free_nid_list(nm_i
, nid
);
2095 f2fs_bug_on(sbi
, !i
);
2097 if (!available_free_memory(sbi
, FREE_NIDS
)) {
2098 __remove_nid_from_list(sbi
, i
, ALLOC_NID_LIST
, false);
2101 __remove_nid_from_list(sbi
, i
, ALLOC_NID_LIST
, true);
2103 __insert_nid_to_list(sbi
, i
, FREE_NID_LIST
, false);
2106 nm_i
->available_nids
++;
2108 update_free_nid_bitmap(sbi
, nid
, true, false);
2110 spin_unlock(&nm_i
->nid_list_lock
);
2113 kmem_cache_free(free_nid_slab
, i
);
2116 int try_to_free_nids(struct f2fs_sb_info
*sbi
, int nr_shrink
)
2118 struct f2fs_nm_info
*nm_i
= NM_I(sbi
);
2119 struct free_nid
*i
, *next
;
2122 if (nm_i
->nid_cnt
[FREE_NID_LIST
] <= MAX_FREE_NIDS
)
2125 if (!mutex_trylock(&nm_i
->build_lock
))
2128 spin_lock(&nm_i
->nid_list_lock
);
2129 list_for_each_entry_safe(i
, next
, &nm_i
->nid_list
[FREE_NID_LIST
],
2131 if (nr_shrink
<= 0 ||
2132 nm_i
->nid_cnt
[FREE_NID_LIST
] <= MAX_FREE_NIDS
)
2135 __remove_nid_from_list(sbi
, i
, FREE_NID_LIST
, false);
2136 kmem_cache_free(free_nid_slab
, i
);
2139 spin_unlock(&nm_i
->nid_list_lock
);
2140 mutex_unlock(&nm_i
->build_lock
);
2142 return nr
- nr_shrink
;
2145 void recover_inline_xattr(struct inode
*inode
, struct page
*page
)
2147 void *src_addr
, *dst_addr
;
2150 struct f2fs_inode
*ri
;
2152 ipage
= get_node_page(F2FS_I_SB(inode
), inode
->i_ino
);
2153 f2fs_bug_on(F2FS_I_SB(inode
), IS_ERR(ipage
));
2155 ri
= F2FS_INODE(page
);
2156 if (!(ri
->i_inline
& F2FS_INLINE_XATTR
)) {
2157 clear_inode_flag(inode
, FI_INLINE_XATTR
);
2161 dst_addr
= inline_xattr_addr(ipage
);
2162 src_addr
= inline_xattr_addr(page
);
2163 inline_size
= inline_xattr_size(inode
);
2165 f2fs_wait_on_page_writeback(ipage
, NODE
, true);
2166 memcpy(dst_addr
, src_addr
, inline_size
);
2168 update_inode(inode
, ipage
);
2169 f2fs_put_page(ipage
, 1);
2172 int recover_xattr_data(struct inode
*inode
, struct page
*page
, block_t blkaddr
)
2174 struct f2fs_sb_info
*sbi
= F2FS_I_SB(inode
);
2175 nid_t prev_xnid
= F2FS_I(inode
)->i_xattr_nid
;
2176 nid_t new_xnid
= nid_of_node(page
);
2177 struct node_info ni
;
2183 /* 1: invalidate the previous xattr nid */
2184 get_node_info(sbi
, prev_xnid
, &ni
);
2185 f2fs_bug_on(sbi
, ni
.blk_addr
== NULL_ADDR
);
2186 invalidate_blocks(sbi
, ni
.blk_addr
);
2187 dec_valid_node_count(sbi
, inode
);
2188 set_node_addr(sbi
, &ni
, NULL_ADDR
, false);
2191 /* 2: update xattr nid in inode */
2192 remove_free_nid(sbi
, new_xnid
);
2193 f2fs_i_xnid_write(inode
, new_xnid
);
2194 if (unlikely(!inc_valid_node_count(sbi
, inode
)))
2195 f2fs_bug_on(sbi
, 1);
2196 update_inode_page(inode
);
2198 /* 3: update and set xattr node page dirty */
2199 xpage
= grab_cache_page(NODE_MAPPING(sbi
), new_xnid
);
2203 memcpy(F2FS_NODE(xpage
), F2FS_NODE(page
), PAGE_SIZE
);
2205 get_node_info(sbi
, new_xnid
, &ni
);
2206 ni
.ino
= inode
->i_ino
;
2207 set_node_addr(sbi
, &ni
, NEW_ADDR
, false);
2208 set_page_dirty(xpage
);
2209 f2fs_put_page(xpage
, 1);
2214 int recover_inode_page(struct f2fs_sb_info
*sbi
, struct page
*page
)
2216 struct f2fs_inode
*src
, *dst
;
2217 nid_t ino
= ino_of_node(page
);
2218 struct node_info old_ni
, new_ni
;
2221 get_node_info(sbi
, ino
, &old_ni
);
2223 if (unlikely(old_ni
.blk_addr
!= NULL_ADDR
))
2226 ipage
= f2fs_grab_cache_page(NODE_MAPPING(sbi
), ino
, false);
2228 congestion_wait(BLK_RW_ASYNC
, HZ
/50);
2232 /* Should not use this inode from free nid list */
2233 remove_free_nid(sbi
, ino
);
2235 if (!PageUptodate(ipage
))
2236 SetPageUptodate(ipage
);
2237 fill_node_footer(ipage
, ino
, ino
, 0, true);
2239 src
= F2FS_INODE(page
);
2240 dst
= F2FS_INODE(ipage
);
2242 memcpy(dst
, src
, (unsigned long)&src
->i_ext
- (unsigned long)src
);
2244 dst
->i_blocks
= cpu_to_le64(1);
2245 dst
->i_links
= cpu_to_le32(1);
2246 dst
->i_xattr_nid
= 0;
2247 dst
->i_inline
= src
->i_inline
& F2FS_INLINE_XATTR
;
2252 if (unlikely(!inc_valid_node_count(sbi
, NULL
)))
2254 set_node_addr(sbi
, &new_ni
, NEW_ADDR
, false);
2255 inc_valid_inode_count(sbi
);
2256 set_page_dirty(ipage
);
2257 f2fs_put_page(ipage
, 1);
2261 int restore_node_summary(struct f2fs_sb_info
*sbi
,
2262 unsigned int segno
, struct f2fs_summary_block
*sum
)
2264 struct f2fs_node
*rn
;
2265 struct f2fs_summary
*sum_entry
;
2267 int i
, idx
, last_offset
, nrpages
;
2269 /* scan the node segment */
2270 last_offset
= sbi
->blocks_per_seg
;
2271 addr
= START_BLOCK(sbi
, segno
);
2272 sum_entry
= &sum
->entries
[0];
2274 for (i
= 0; i
< last_offset
; i
+= nrpages
, addr
+= nrpages
) {
2275 nrpages
= min(last_offset
- i
, BIO_MAX_PAGES
);
2277 /* readahead node pages */
2278 ra_meta_pages(sbi
, addr
, nrpages
, META_POR
, true);
2280 for (idx
= addr
; idx
< addr
+ nrpages
; idx
++) {
2281 struct page
*page
= get_tmp_page(sbi
, idx
);
2283 rn
= F2FS_NODE(page
);
2284 sum_entry
->nid
= rn
->footer
.nid
;
2285 sum_entry
->version
= 0;
2286 sum_entry
->ofs_in_node
= 0;
2288 f2fs_put_page(page
, 1);
2291 invalidate_mapping_pages(META_MAPPING(sbi
), addr
,
2297 static void remove_nats_in_journal(struct f2fs_sb_info
*sbi
)
2299 struct f2fs_nm_info
*nm_i
= NM_I(sbi
);
2300 struct curseg_info
*curseg
= CURSEG_I(sbi
, CURSEG_HOT_DATA
);
2301 struct f2fs_journal
*journal
= curseg
->journal
;
2304 down_write(&curseg
->journal_rwsem
);
2305 for (i
= 0; i
< nats_in_cursum(journal
); i
++) {
2306 struct nat_entry
*ne
;
2307 struct f2fs_nat_entry raw_ne
;
2308 nid_t nid
= le32_to_cpu(nid_in_journal(journal
, i
));
2310 raw_ne
= nat_in_journal(journal
, i
);
2312 ne
= __lookup_nat_cache(nm_i
, nid
);
2314 ne
= grab_nat_entry(nm_i
, nid
, true);
2315 node_info_from_raw_nat(&ne
->ni
, &raw_ne
);
2319 * if a free nat in journal has not been used after last
2320 * checkpoint, we should remove it from available nids,
2321 * since later we will add it again.
2323 if (!get_nat_flag(ne
, IS_DIRTY
) &&
2324 le32_to_cpu(raw_ne
.block_addr
) == NULL_ADDR
) {
2325 spin_lock(&nm_i
->nid_list_lock
);
2326 nm_i
->available_nids
--;
2327 spin_unlock(&nm_i
->nid_list_lock
);
2330 __set_nat_cache_dirty(nm_i
, ne
);
2332 update_nats_in_cursum(journal
, -i
);
2333 up_write(&curseg
->journal_rwsem
);
2336 static void __adjust_nat_entry_set(struct nat_entry_set
*nes
,
2337 struct list_head
*head
, int max
)
2339 struct nat_entry_set
*cur
;
2341 if (nes
->entry_cnt
>= max
)
2344 list_for_each_entry(cur
, head
, set_list
) {
2345 if (cur
->entry_cnt
>= nes
->entry_cnt
) {
2346 list_add(&nes
->set_list
, cur
->set_list
.prev
);
2351 list_add_tail(&nes
->set_list
, head
);
2354 static void __update_nat_bits(struct f2fs_sb_info
*sbi
, nid_t start_nid
,
2357 struct f2fs_nm_info
*nm_i
= NM_I(sbi
);
2358 unsigned int nat_index
= start_nid
/ NAT_ENTRY_PER_BLOCK
;
2359 struct f2fs_nat_block
*nat_blk
= page_address(page
);
2363 if (!enabled_nat_bits(sbi
, NULL
))
2366 for (i
= 0; i
< NAT_ENTRY_PER_BLOCK
; i
++) {
2367 if (start_nid
== 0 && i
== 0)
2369 if (nat_blk
->entries
[i
].block_addr
)
2373 __set_bit_le(nat_index
, nm_i
->empty_nat_bits
);
2374 __clear_bit_le(nat_index
, nm_i
->full_nat_bits
);
2378 __clear_bit_le(nat_index
, nm_i
->empty_nat_bits
);
2379 if (valid
== NAT_ENTRY_PER_BLOCK
)
2380 __set_bit_le(nat_index
, nm_i
->full_nat_bits
);
2382 __clear_bit_le(nat_index
, nm_i
->full_nat_bits
);
2385 static void __flush_nat_entry_set(struct f2fs_sb_info
*sbi
,
2386 struct nat_entry_set
*set
, struct cp_control
*cpc
)
2388 struct curseg_info
*curseg
= CURSEG_I(sbi
, CURSEG_HOT_DATA
);
2389 struct f2fs_journal
*journal
= curseg
->journal
;
2390 nid_t start_nid
= set
->set
* NAT_ENTRY_PER_BLOCK
;
2391 bool to_journal
= true;
2392 struct f2fs_nat_block
*nat_blk
;
2393 struct nat_entry
*ne
, *cur
;
2394 struct page
*page
= NULL
;
2397 * there are two steps to flush nat entries:
2398 * #1, flush nat entries to journal in current hot data summary block.
2399 * #2, flush nat entries to nat page.
2401 if (enabled_nat_bits(sbi
, cpc
) ||
2402 !__has_cursum_space(journal
, set
->entry_cnt
, NAT_JOURNAL
))
2406 down_write(&curseg
->journal_rwsem
);
2408 page
= get_next_nat_page(sbi
, start_nid
);
2409 nat_blk
= page_address(page
);
2410 f2fs_bug_on(sbi
, !nat_blk
);
2413 /* flush dirty nats in nat entry set */
2414 list_for_each_entry_safe(ne
, cur
, &set
->entry_list
, list
) {
2415 struct f2fs_nat_entry
*raw_ne
;
2416 nid_t nid
= nat_get_nid(ne
);
2419 if (nat_get_blkaddr(ne
) == NEW_ADDR
)
2423 offset
= lookup_journal_in_cursum(journal
,
2424 NAT_JOURNAL
, nid
, 1);
2425 f2fs_bug_on(sbi
, offset
< 0);
2426 raw_ne
= &nat_in_journal(journal
, offset
);
2427 nid_in_journal(journal
, offset
) = cpu_to_le32(nid
);
2429 raw_ne
= &nat_blk
->entries
[nid
- start_nid
];
2431 raw_nat_from_node_info(raw_ne
, &ne
->ni
);
2433 __clear_nat_cache_dirty(NM_I(sbi
), set
, ne
);
2434 if (nat_get_blkaddr(ne
) == NULL_ADDR
) {
2435 add_free_nid(sbi
, nid
, false);
2436 spin_lock(&NM_I(sbi
)->nid_list_lock
);
2437 NM_I(sbi
)->available_nids
++;
2438 update_free_nid_bitmap(sbi
, nid
, true, false);
2439 spin_unlock(&NM_I(sbi
)->nid_list_lock
);
2441 spin_lock(&NM_I(sbi
)->nid_list_lock
);
2442 update_free_nid_bitmap(sbi
, nid
, false, false);
2443 spin_unlock(&NM_I(sbi
)->nid_list_lock
);
2448 up_write(&curseg
->journal_rwsem
);
2450 __update_nat_bits(sbi
, start_nid
, page
);
2451 f2fs_put_page(page
, 1);
2454 /* Allow dirty nats by node block allocation in write_begin */
2455 if (!set
->entry_cnt
) {
2456 radix_tree_delete(&NM_I(sbi
)->nat_set_root
, set
->set
);
2457 kmem_cache_free(nat_entry_set_slab
, set
);
2462 * This function is called during the checkpointing process.
2464 void flush_nat_entries(struct f2fs_sb_info
*sbi
, struct cp_control
*cpc
)
2466 struct f2fs_nm_info
*nm_i
= NM_I(sbi
);
2467 struct curseg_info
*curseg
= CURSEG_I(sbi
, CURSEG_HOT_DATA
);
2468 struct f2fs_journal
*journal
= curseg
->journal
;
2469 struct nat_entry_set
*setvec
[SETVEC_SIZE
];
2470 struct nat_entry_set
*set
, *tmp
;
2475 if (!nm_i
->dirty_nat_cnt
)
2478 down_write(&nm_i
->nat_tree_lock
);
2481 * if there are no enough space in journal to store dirty nat
2482 * entries, remove all entries from journal and merge them
2483 * into nat entry set.
2485 if (enabled_nat_bits(sbi
, cpc
) ||
2486 !__has_cursum_space(journal
, nm_i
->dirty_nat_cnt
, NAT_JOURNAL
))
2487 remove_nats_in_journal(sbi
);
2489 while ((found
= __gang_lookup_nat_set(nm_i
,
2490 set_idx
, SETVEC_SIZE
, setvec
))) {
2492 set_idx
= setvec
[found
- 1]->set
+ 1;
2493 for (idx
= 0; idx
< found
; idx
++)
2494 __adjust_nat_entry_set(setvec
[idx
], &sets
,
2495 MAX_NAT_JENTRIES(journal
));
2498 /* flush dirty nats in nat entry set */
2499 list_for_each_entry_safe(set
, tmp
, &sets
, set_list
)
2500 __flush_nat_entry_set(sbi
, set
, cpc
);
2502 up_write(&nm_i
->nat_tree_lock
);
2503 /* Allow dirty nats by node block allocation in write_begin */
2506 static int __get_nat_bitmaps(struct f2fs_sb_info
*sbi
)
2508 struct f2fs_checkpoint
*ckpt
= F2FS_CKPT(sbi
);
2509 struct f2fs_nm_info
*nm_i
= NM_I(sbi
);
2510 unsigned int nat_bits_bytes
= nm_i
->nat_blocks
/ BITS_PER_BYTE
;
2512 __u64 cp_ver
= cur_cp_version(ckpt
);
2513 block_t nat_bits_addr
;
2515 if (!enabled_nat_bits(sbi
, NULL
))
2518 nm_i
->nat_bits_blocks
= F2FS_BYTES_TO_BLK((nat_bits_bytes
<< 1) + 8 +
2520 nm_i
->nat_bits
= kzalloc(nm_i
->nat_bits_blocks
<< F2FS_BLKSIZE_BITS
,
2522 if (!nm_i
->nat_bits
)
2525 nat_bits_addr
= __start_cp_addr(sbi
) + sbi
->blocks_per_seg
-
2526 nm_i
->nat_bits_blocks
;
2527 for (i
= 0; i
< nm_i
->nat_bits_blocks
; i
++) {
2528 struct page
*page
= get_meta_page(sbi
, nat_bits_addr
++);
2530 memcpy(nm_i
->nat_bits
+ (i
<< F2FS_BLKSIZE_BITS
),
2531 page_address(page
), F2FS_BLKSIZE
);
2532 f2fs_put_page(page
, 1);
2535 cp_ver
|= (cur_cp_crc(ckpt
) << 32);
2536 if (cpu_to_le64(cp_ver
) != *(__le64
*)nm_i
->nat_bits
) {
2537 disable_nat_bits(sbi
, true);
2541 nm_i
->full_nat_bits
= nm_i
->nat_bits
+ 8;
2542 nm_i
->empty_nat_bits
= nm_i
->full_nat_bits
+ nat_bits_bytes
;
2544 f2fs_msg(sbi
->sb
, KERN_NOTICE
, "Found nat_bits in checkpoint");
2548 inline void load_free_nid_bitmap(struct f2fs_sb_info
*sbi
)
2550 struct f2fs_nm_info
*nm_i
= NM_I(sbi
);
2552 nid_t nid
, last_nid
;
2554 if (!enabled_nat_bits(sbi
, NULL
))
2557 for (i
= 0; i
< nm_i
->nat_blocks
; i
++) {
2558 i
= find_next_bit_le(nm_i
->empty_nat_bits
, nm_i
->nat_blocks
, i
);
2559 if (i
>= nm_i
->nat_blocks
)
2562 __set_bit_le(i
, nm_i
->nat_block_bitmap
);
2564 nid
= i
* NAT_ENTRY_PER_BLOCK
;
2565 last_nid
= (i
+ 1) * NAT_ENTRY_PER_BLOCK
;
2567 spin_lock(&NM_I(sbi
)->nid_list_lock
);
2568 for (; nid
< last_nid
; nid
++)
2569 update_free_nid_bitmap(sbi
, nid
, true, true);
2570 spin_unlock(&NM_I(sbi
)->nid_list_lock
);
2573 for (i
= 0; i
< nm_i
->nat_blocks
; i
++) {
2574 i
= find_next_bit_le(nm_i
->full_nat_bits
, nm_i
->nat_blocks
, i
);
2575 if (i
>= nm_i
->nat_blocks
)
2578 __set_bit_le(i
, nm_i
->nat_block_bitmap
);
2582 static int init_node_manager(struct f2fs_sb_info
*sbi
)
2584 struct f2fs_super_block
*sb_raw
= F2FS_RAW_SUPER(sbi
);
2585 struct f2fs_nm_info
*nm_i
= NM_I(sbi
);
2586 unsigned char *version_bitmap
;
2587 unsigned int nat_segs
;
2590 nm_i
->nat_blkaddr
= le32_to_cpu(sb_raw
->nat_blkaddr
);
2592 /* segment_count_nat includes pair segment so divide to 2. */
2593 nat_segs
= le32_to_cpu(sb_raw
->segment_count_nat
) >> 1;
2594 nm_i
->nat_blocks
= nat_segs
<< le32_to_cpu(sb_raw
->log_blocks_per_seg
);
2595 nm_i
->max_nid
= NAT_ENTRY_PER_BLOCK
* nm_i
->nat_blocks
;
2597 /* not used nids: 0, node, meta, (and root counted as valid node) */
2598 nm_i
->available_nids
= nm_i
->max_nid
- sbi
->total_valid_node_count
-
2599 F2FS_RESERVED_NODE_NUM
;
2600 nm_i
->nid_cnt
[FREE_NID_LIST
] = 0;
2601 nm_i
->nid_cnt
[ALLOC_NID_LIST
] = 0;
2603 nm_i
->ram_thresh
= DEF_RAM_THRESHOLD
;
2604 nm_i
->ra_nid_pages
= DEF_RA_NID_PAGES
;
2605 nm_i
->dirty_nats_ratio
= DEF_DIRTY_NAT_RATIO_THRESHOLD
;
2607 INIT_RADIX_TREE(&nm_i
->free_nid_root
, GFP_ATOMIC
);
2608 INIT_LIST_HEAD(&nm_i
->nid_list
[FREE_NID_LIST
]);
2609 INIT_LIST_HEAD(&nm_i
->nid_list
[ALLOC_NID_LIST
]);
2610 INIT_RADIX_TREE(&nm_i
->nat_root
, GFP_NOIO
);
2611 INIT_RADIX_TREE(&nm_i
->nat_set_root
, GFP_NOIO
);
2612 INIT_LIST_HEAD(&nm_i
->nat_entries
);
2614 mutex_init(&nm_i
->build_lock
);
2615 spin_lock_init(&nm_i
->nid_list_lock
);
2616 init_rwsem(&nm_i
->nat_tree_lock
);
2618 nm_i
->next_scan_nid
= le32_to_cpu(sbi
->ckpt
->next_free_nid
);
2619 nm_i
->bitmap_size
= __bitmap_size(sbi
, NAT_BITMAP
);
2620 version_bitmap
= __bitmap_ptr(sbi
, NAT_BITMAP
);
2621 if (!version_bitmap
)
2624 nm_i
->nat_bitmap
= kmemdup(version_bitmap
, nm_i
->bitmap_size
,
2626 if (!nm_i
->nat_bitmap
)
2629 err
= __get_nat_bitmaps(sbi
);
2633 #ifdef CONFIG_F2FS_CHECK_FS
2634 nm_i
->nat_bitmap_mir
= kmemdup(version_bitmap
, nm_i
->bitmap_size
,
2636 if (!nm_i
->nat_bitmap_mir
)
2643 static int init_free_nid_cache(struct f2fs_sb_info
*sbi
)
2645 struct f2fs_nm_info
*nm_i
= NM_I(sbi
);
2647 nm_i
->free_nid_bitmap
= f2fs_kvzalloc(nm_i
->nat_blocks
*
2648 NAT_ENTRY_BITMAP_SIZE
, GFP_KERNEL
);
2649 if (!nm_i
->free_nid_bitmap
)
2652 nm_i
->nat_block_bitmap
= f2fs_kvzalloc(nm_i
->nat_blocks
/ 8,
2654 if (!nm_i
->nat_block_bitmap
)
2657 nm_i
->free_nid_count
= f2fs_kvzalloc(nm_i
->nat_blocks
*
2658 sizeof(unsigned short), GFP_KERNEL
);
2659 if (!nm_i
->free_nid_count
)
2664 int build_node_manager(struct f2fs_sb_info
*sbi
)
2668 sbi
->nm_info
= kzalloc(sizeof(struct f2fs_nm_info
), GFP_KERNEL
);
2672 err
= init_node_manager(sbi
);
2676 err
= init_free_nid_cache(sbi
);
2680 /* load free nid status from nat_bits table */
2681 load_free_nid_bitmap(sbi
);
2683 build_free_nids(sbi
, true, true);
2687 void destroy_node_manager(struct f2fs_sb_info
*sbi
)
2689 struct f2fs_nm_info
*nm_i
= NM_I(sbi
);
2690 struct free_nid
*i
, *next_i
;
2691 struct nat_entry
*natvec
[NATVEC_SIZE
];
2692 struct nat_entry_set
*setvec
[SETVEC_SIZE
];
2699 /* destroy free nid list */
2700 spin_lock(&nm_i
->nid_list_lock
);
2701 list_for_each_entry_safe(i
, next_i
, &nm_i
->nid_list
[FREE_NID_LIST
],
2703 __remove_nid_from_list(sbi
, i
, FREE_NID_LIST
, false);
2704 spin_unlock(&nm_i
->nid_list_lock
);
2705 kmem_cache_free(free_nid_slab
, i
);
2706 spin_lock(&nm_i
->nid_list_lock
);
2708 f2fs_bug_on(sbi
, nm_i
->nid_cnt
[FREE_NID_LIST
]);
2709 f2fs_bug_on(sbi
, nm_i
->nid_cnt
[ALLOC_NID_LIST
]);
2710 f2fs_bug_on(sbi
, !list_empty(&nm_i
->nid_list
[ALLOC_NID_LIST
]));
2711 spin_unlock(&nm_i
->nid_list_lock
);
2713 /* destroy nat cache */
2714 down_write(&nm_i
->nat_tree_lock
);
2715 while ((found
= __gang_lookup_nat_cache(nm_i
,
2716 nid
, NATVEC_SIZE
, natvec
))) {
2719 nid
= nat_get_nid(natvec
[found
- 1]) + 1;
2720 for (idx
= 0; idx
< found
; idx
++)
2721 __del_from_nat_cache(nm_i
, natvec
[idx
]);
2723 f2fs_bug_on(sbi
, nm_i
->nat_cnt
);
2725 /* destroy nat set cache */
2727 while ((found
= __gang_lookup_nat_set(nm_i
,
2728 nid
, SETVEC_SIZE
, setvec
))) {
2731 nid
= setvec
[found
- 1]->set
+ 1;
2732 for (idx
= 0; idx
< found
; idx
++) {
2733 /* entry_cnt is not zero, when cp_error was occurred */
2734 f2fs_bug_on(sbi
, !list_empty(&setvec
[idx
]->entry_list
));
2735 radix_tree_delete(&nm_i
->nat_set_root
, setvec
[idx
]->set
);
2736 kmem_cache_free(nat_entry_set_slab
, setvec
[idx
]);
2739 up_write(&nm_i
->nat_tree_lock
);
2741 kvfree(nm_i
->nat_block_bitmap
);
2742 kvfree(nm_i
->free_nid_bitmap
);
2743 kvfree(nm_i
->free_nid_count
);
2745 kfree(nm_i
->nat_bitmap
);
2746 kfree(nm_i
->nat_bits
);
2747 #ifdef CONFIG_F2FS_CHECK_FS
2748 kfree(nm_i
->nat_bitmap_mir
);
2750 sbi
->nm_info
= NULL
;
2754 int __init
create_node_manager_caches(void)
2756 nat_entry_slab
= f2fs_kmem_cache_create("nat_entry",
2757 sizeof(struct nat_entry
));
2758 if (!nat_entry_slab
)
2761 free_nid_slab
= f2fs_kmem_cache_create("free_nid",
2762 sizeof(struct free_nid
));
2764 goto destroy_nat_entry
;
2766 nat_entry_set_slab
= f2fs_kmem_cache_create("nat_entry_set",
2767 sizeof(struct nat_entry_set
));
2768 if (!nat_entry_set_slab
)
2769 goto destroy_free_nid
;
2773 kmem_cache_destroy(free_nid_slab
);
2775 kmem_cache_destroy(nat_entry_slab
);
2780 void destroy_node_manager_caches(void)
2782 kmem_cache_destroy(nat_entry_set_slab
);
2783 kmem_cache_destroy(free_nid_slab
);
2784 kmem_cache_destroy(nat_entry_slab
);