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
= (atomic_read(&sbi
->total_ext_tree
) *
69 sizeof(struct extent_tree
) +
70 atomic_read(&sbi
->total_ext_node
) *
71 sizeof(struct extent_node
)) >> PAGE_CACHE_SHIFT
;
72 res
= mem_size
< ((avail_ram
* nm_i
->ram_thresh
/ 100) >> 1);
74 if (sbi
->sb
->s_bdi
->wb
.dirty_exceeded
)
80 static void clear_node_page_dirty(struct page
*page
)
82 struct address_space
*mapping
= page
->mapping
;
83 unsigned int long flags
;
85 if (PageDirty(page
)) {
86 spin_lock_irqsave(&mapping
->tree_lock
, flags
);
87 radix_tree_tag_clear(&mapping
->page_tree
,
90 spin_unlock_irqrestore(&mapping
->tree_lock
, flags
);
92 clear_page_dirty_for_io(page
);
93 dec_page_count(F2FS_M_SB(mapping
), F2FS_DIRTY_NODES
);
95 ClearPageUptodate(page
);
98 static struct page
*get_current_nat_page(struct f2fs_sb_info
*sbi
, nid_t nid
)
100 pgoff_t index
= current_nat_addr(sbi
, nid
);
101 return get_meta_page(sbi
, index
);
104 static struct page
*get_next_nat_page(struct f2fs_sb_info
*sbi
, nid_t nid
)
106 struct page
*src_page
;
107 struct page
*dst_page
;
112 struct f2fs_nm_info
*nm_i
= NM_I(sbi
);
114 src_off
= current_nat_addr(sbi
, nid
);
115 dst_off
= next_nat_addr(sbi
, src_off
);
117 /* get current nat block page with lock */
118 src_page
= get_meta_page(sbi
, src_off
);
119 dst_page
= grab_meta_page(sbi
, dst_off
);
120 f2fs_bug_on(sbi
, PageDirty(src_page
));
122 src_addr
= page_address(src_page
);
123 dst_addr
= page_address(dst_page
);
124 memcpy(dst_addr
, src_addr
, PAGE_CACHE_SIZE
);
125 set_page_dirty(dst_page
);
126 f2fs_put_page(src_page
, 1);
128 set_to_next_nat(nm_i
, nid
);
133 static struct nat_entry
*__lookup_nat_cache(struct f2fs_nm_info
*nm_i
, nid_t n
)
135 return radix_tree_lookup(&nm_i
->nat_root
, n
);
138 static unsigned int __gang_lookup_nat_cache(struct f2fs_nm_info
*nm_i
,
139 nid_t start
, unsigned int nr
, struct nat_entry
**ep
)
141 return radix_tree_gang_lookup(&nm_i
->nat_root
, (void **)ep
, start
, nr
);
144 static void __del_from_nat_cache(struct f2fs_nm_info
*nm_i
, struct nat_entry
*e
)
147 radix_tree_delete(&nm_i
->nat_root
, nat_get_nid(e
));
149 kmem_cache_free(nat_entry_slab
, e
);
152 static void __set_nat_cache_dirty(struct f2fs_nm_info
*nm_i
,
153 struct nat_entry
*ne
)
155 nid_t set
= NAT_BLOCK_OFFSET(ne
->ni
.nid
);
156 struct nat_entry_set
*head
;
158 if (get_nat_flag(ne
, IS_DIRTY
))
161 head
= radix_tree_lookup(&nm_i
->nat_set_root
, set
);
163 head
= f2fs_kmem_cache_alloc(nat_entry_set_slab
, GFP_NOFS
);
165 INIT_LIST_HEAD(&head
->entry_list
);
166 INIT_LIST_HEAD(&head
->set_list
);
169 f2fs_radix_tree_insert(&nm_i
->nat_set_root
, set
, head
);
171 list_move_tail(&ne
->list
, &head
->entry_list
);
172 nm_i
->dirty_nat_cnt
++;
174 set_nat_flag(ne
, IS_DIRTY
, true);
177 static void __clear_nat_cache_dirty(struct f2fs_nm_info
*nm_i
,
178 struct nat_entry
*ne
)
180 nid_t set
= NAT_BLOCK_OFFSET(ne
->ni
.nid
);
181 struct nat_entry_set
*head
;
183 head
= radix_tree_lookup(&nm_i
->nat_set_root
, set
);
185 list_move_tail(&ne
->list
, &nm_i
->nat_entries
);
186 set_nat_flag(ne
, IS_DIRTY
, false);
188 nm_i
->dirty_nat_cnt
--;
192 static unsigned int __gang_lookup_nat_set(struct f2fs_nm_info
*nm_i
,
193 nid_t start
, unsigned int nr
, struct nat_entry_set
**ep
)
195 return radix_tree_gang_lookup(&nm_i
->nat_set_root
, (void **)ep
,
199 int need_dentry_mark(struct f2fs_sb_info
*sbi
, nid_t nid
)
201 struct f2fs_nm_info
*nm_i
= NM_I(sbi
);
205 down_read(&nm_i
->nat_tree_lock
);
206 e
= __lookup_nat_cache(nm_i
, nid
);
208 if (!get_nat_flag(e
, IS_CHECKPOINTED
) &&
209 !get_nat_flag(e
, HAS_FSYNCED_INODE
))
212 up_read(&nm_i
->nat_tree_lock
);
216 bool is_checkpointed_node(struct f2fs_sb_info
*sbi
, nid_t nid
)
218 struct f2fs_nm_info
*nm_i
= NM_I(sbi
);
222 down_read(&nm_i
->nat_tree_lock
);
223 e
= __lookup_nat_cache(nm_i
, nid
);
224 if (e
&& !get_nat_flag(e
, IS_CHECKPOINTED
))
226 up_read(&nm_i
->nat_tree_lock
);
230 bool need_inode_block_update(struct f2fs_sb_info
*sbi
, nid_t ino
)
232 struct f2fs_nm_info
*nm_i
= NM_I(sbi
);
234 bool need_update
= true;
236 down_read(&nm_i
->nat_tree_lock
);
237 e
= __lookup_nat_cache(nm_i
, ino
);
238 if (e
&& get_nat_flag(e
, HAS_LAST_FSYNC
) &&
239 (get_nat_flag(e
, IS_CHECKPOINTED
) ||
240 get_nat_flag(e
, HAS_FSYNCED_INODE
)))
242 up_read(&nm_i
->nat_tree_lock
);
246 static struct nat_entry
*grab_nat_entry(struct f2fs_nm_info
*nm_i
, nid_t nid
)
248 struct nat_entry
*new;
250 new = f2fs_kmem_cache_alloc(nat_entry_slab
, GFP_NOFS
);
251 f2fs_radix_tree_insert(&nm_i
->nat_root
, nid
, new);
252 memset(new, 0, sizeof(struct nat_entry
));
253 nat_set_nid(new, nid
);
255 list_add_tail(&new->list
, &nm_i
->nat_entries
);
260 static void cache_nat_entry(struct f2fs_nm_info
*nm_i
, nid_t nid
,
261 struct f2fs_nat_entry
*ne
)
265 e
= __lookup_nat_cache(nm_i
, nid
);
267 e
= grab_nat_entry(nm_i
, nid
);
268 node_info_from_raw_nat(&e
->ni
, ne
);
272 static void set_node_addr(struct f2fs_sb_info
*sbi
, struct node_info
*ni
,
273 block_t new_blkaddr
, bool fsync_done
)
275 struct f2fs_nm_info
*nm_i
= NM_I(sbi
);
278 down_write(&nm_i
->nat_tree_lock
);
279 e
= __lookup_nat_cache(nm_i
, ni
->nid
);
281 e
= grab_nat_entry(nm_i
, ni
->nid
);
282 copy_node_info(&e
->ni
, ni
);
283 f2fs_bug_on(sbi
, ni
->blk_addr
== NEW_ADDR
);
284 } else if (new_blkaddr
== NEW_ADDR
) {
286 * when nid is reallocated,
287 * previous nat entry can be remained in nat cache.
288 * So, reinitialize it with new information.
290 copy_node_info(&e
->ni
, ni
);
291 f2fs_bug_on(sbi
, ni
->blk_addr
!= NULL_ADDR
);
295 f2fs_bug_on(sbi
, nat_get_blkaddr(e
) != ni
->blk_addr
);
296 f2fs_bug_on(sbi
, nat_get_blkaddr(e
) == NULL_ADDR
&&
297 new_blkaddr
== NULL_ADDR
);
298 f2fs_bug_on(sbi
, nat_get_blkaddr(e
) == NEW_ADDR
&&
299 new_blkaddr
== NEW_ADDR
);
300 f2fs_bug_on(sbi
, nat_get_blkaddr(e
) != NEW_ADDR
&&
301 nat_get_blkaddr(e
) != NULL_ADDR
&&
302 new_blkaddr
== NEW_ADDR
);
304 /* increment version no as node is removed */
305 if (nat_get_blkaddr(e
) != NEW_ADDR
&& new_blkaddr
== NULL_ADDR
) {
306 unsigned char version
= nat_get_version(e
);
307 nat_set_version(e
, inc_node_version(version
));
309 /* in order to reuse the nid */
310 if (nm_i
->next_scan_nid
> ni
->nid
)
311 nm_i
->next_scan_nid
= ni
->nid
;
315 nat_set_blkaddr(e
, new_blkaddr
);
316 if (new_blkaddr
== NEW_ADDR
|| new_blkaddr
== NULL_ADDR
)
317 set_nat_flag(e
, IS_CHECKPOINTED
, false);
318 __set_nat_cache_dirty(nm_i
, e
);
320 /* update fsync_mark if its inode nat entry is still alive */
321 if (ni
->nid
!= ni
->ino
)
322 e
= __lookup_nat_cache(nm_i
, ni
->ino
);
324 if (fsync_done
&& ni
->nid
== ni
->ino
)
325 set_nat_flag(e
, HAS_FSYNCED_INODE
, true);
326 set_nat_flag(e
, HAS_LAST_FSYNC
, fsync_done
);
328 up_write(&nm_i
->nat_tree_lock
);
331 int try_to_free_nats(struct f2fs_sb_info
*sbi
, int nr_shrink
)
333 struct f2fs_nm_info
*nm_i
= NM_I(sbi
);
336 if (!down_write_trylock(&nm_i
->nat_tree_lock
))
339 while (nr_shrink
&& !list_empty(&nm_i
->nat_entries
)) {
340 struct nat_entry
*ne
;
341 ne
= list_first_entry(&nm_i
->nat_entries
,
342 struct nat_entry
, list
);
343 __del_from_nat_cache(nm_i
, ne
);
346 up_write(&nm_i
->nat_tree_lock
);
347 return nr
- nr_shrink
;
351 * This function always returns success
353 void get_node_info(struct f2fs_sb_info
*sbi
, nid_t nid
, struct node_info
*ni
)
355 struct f2fs_nm_info
*nm_i
= NM_I(sbi
);
356 struct curseg_info
*curseg
= CURSEG_I(sbi
, CURSEG_HOT_DATA
);
357 struct f2fs_summary_block
*sum
= curseg
->sum_blk
;
358 nid_t start_nid
= START_NID(nid
);
359 struct f2fs_nat_block
*nat_blk
;
360 struct page
*page
= NULL
;
361 struct f2fs_nat_entry ne
;
367 /* Check nat cache */
368 down_read(&nm_i
->nat_tree_lock
);
369 e
= __lookup_nat_cache(nm_i
, nid
);
371 ni
->ino
= nat_get_ino(e
);
372 ni
->blk_addr
= nat_get_blkaddr(e
);
373 ni
->version
= nat_get_version(e
);
375 up_read(&nm_i
->nat_tree_lock
);
379 memset(&ne
, 0, sizeof(struct f2fs_nat_entry
));
381 down_write(&nm_i
->nat_tree_lock
);
383 /* Check current segment summary */
384 mutex_lock(&curseg
->curseg_mutex
);
385 i
= lookup_journal_in_cursum(sum
, NAT_JOURNAL
, nid
, 0);
387 ne
= nat_in_journal(sum
, i
);
388 node_info_from_raw_nat(ni
, &ne
);
390 mutex_unlock(&curseg
->curseg_mutex
);
394 /* Fill node_info from nat page */
395 page
= get_current_nat_page(sbi
, start_nid
);
396 nat_blk
= (struct f2fs_nat_block
*)page_address(page
);
397 ne
= nat_blk
->entries
[nid
- start_nid
];
398 node_info_from_raw_nat(ni
, &ne
);
399 f2fs_put_page(page
, 1);
401 /* cache nat entry */
402 cache_nat_entry(NM_I(sbi
), nid
, &ne
);
403 up_write(&nm_i
->nat_tree_lock
);
407 * The maximum depth is four.
408 * Offset[0] will have raw inode offset.
410 static int get_node_path(struct f2fs_inode_info
*fi
, long block
,
411 int offset
[4], unsigned int noffset
[4])
413 const long direct_index
= ADDRS_PER_INODE(fi
);
414 const long direct_blks
= ADDRS_PER_BLOCK
;
415 const long dptrs_per_blk
= NIDS_PER_BLOCK
;
416 const long indirect_blks
= ADDRS_PER_BLOCK
* NIDS_PER_BLOCK
;
417 const long dindirect_blks
= indirect_blks
* NIDS_PER_BLOCK
;
423 if (block
< direct_index
) {
427 block
-= direct_index
;
428 if (block
< direct_blks
) {
429 offset
[n
++] = NODE_DIR1_BLOCK
;
435 block
-= direct_blks
;
436 if (block
< direct_blks
) {
437 offset
[n
++] = NODE_DIR2_BLOCK
;
443 block
-= direct_blks
;
444 if (block
< indirect_blks
) {
445 offset
[n
++] = NODE_IND1_BLOCK
;
447 offset
[n
++] = block
/ direct_blks
;
448 noffset
[n
] = 4 + offset
[n
- 1];
449 offset
[n
] = block
% direct_blks
;
453 block
-= indirect_blks
;
454 if (block
< indirect_blks
) {
455 offset
[n
++] = NODE_IND2_BLOCK
;
456 noffset
[n
] = 4 + dptrs_per_blk
;
457 offset
[n
++] = block
/ direct_blks
;
458 noffset
[n
] = 5 + dptrs_per_blk
+ offset
[n
- 1];
459 offset
[n
] = block
% direct_blks
;
463 block
-= indirect_blks
;
464 if (block
< dindirect_blks
) {
465 offset
[n
++] = NODE_DIND_BLOCK
;
466 noffset
[n
] = 5 + (dptrs_per_blk
* 2);
467 offset
[n
++] = block
/ indirect_blks
;
468 noffset
[n
] = 6 + (dptrs_per_blk
* 2) +
469 offset
[n
- 1] * (dptrs_per_blk
+ 1);
470 offset
[n
++] = (block
/ direct_blks
) % dptrs_per_blk
;
471 noffset
[n
] = 7 + (dptrs_per_blk
* 2) +
472 offset
[n
- 2] * (dptrs_per_blk
+ 1) +
474 offset
[n
] = block
% direct_blks
;
485 * Caller should call f2fs_put_dnode(dn).
486 * Also, it should grab and release a rwsem by calling f2fs_lock_op() and
487 * f2fs_unlock_op() only if ro is not set RDONLY_NODE.
488 * In the case of RDONLY_NODE, we don't need to care about mutex.
490 int get_dnode_of_data(struct dnode_of_data
*dn
, pgoff_t index
, int mode
)
492 struct f2fs_sb_info
*sbi
= F2FS_I_SB(dn
->inode
);
493 struct page
*npage
[4];
494 struct page
*parent
= NULL
;
496 unsigned int noffset
[4];
501 level
= get_node_path(F2FS_I(dn
->inode
), index
, offset
, noffset
);
503 nids
[0] = dn
->inode
->i_ino
;
504 npage
[0] = dn
->inode_page
;
507 npage
[0] = get_node_page(sbi
, nids
[0]);
508 if (IS_ERR(npage
[0]))
509 return PTR_ERR(npage
[0]);
512 /* if inline_data is set, should not report any block indices */
513 if (f2fs_has_inline_data(dn
->inode
) && index
) {
515 f2fs_put_page(npage
[0], 1);
521 nids
[1] = get_nid(parent
, offset
[0], true);
522 dn
->inode_page
= npage
[0];
523 dn
->inode_page_locked
= true;
525 /* get indirect or direct nodes */
526 for (i
= 1; i
<= level
; i
++) {
529 if (!nids
[i
] && mode
== ALLOC_NODE
) {
531 if (!alloc_nid(sbi
, &(nids
[i
]))) {
537 npage
[i
] = new_node_page(dn
, noffset
[i
], NULL
);
538 if (IS_ERR(npage
[i
])) {
539 alloc_nid_failed(sbi
, nids
[i
]);
540 err
= PTR_ERR(npage
[i
]);
544 set_nid(parent
, offset
[i
- 1], nids
[i
], i
== 1);
545 alloc_nid_done(sbi
, nids
[i
]);
546 dn
->node_changed
= true;
548 } else if (mode
== LOOKUP_NODE_RA
&& i
== level
&& level
> 1) {
549 npage
[i
] = get_node_page_ra(parent
, offset
[i
- 1]);
550 if (IS_ERR(npage
[i
])) {
551 err
= PTR_ERR(npage
[i
]);
557 dn
->inode_page_locked
= false;
560 f2fs_put_page(parent
, 1);
564 npage
[i
] = get_node_page(sbi
, nids
[i
]);
565 if (IS_ERR(npage
[i
])) {
566 err
= PTR_ERR(npage
[i
]);
567 f2fs_put_page(npage
[0], 0);
573 nids
[i
+ 1] = get_nid(parent
, offset
[i
], false);
576 dn
->nid
= nids
[level
];
577 dn
->ofs_in_node
= offset
[level
];
578 dn
->node_page
= npage
[level
];
579 dn
->data_blkaddr
= datablock_addr(dn
->node_page
, dn
->ofs_in_node
);
583 f2fs_put_page(parent
, 1);
585 f2fs_put_page(npage
[0], 0);
587 dn
->inode_page
= NULL
;
588 dn
->node_page
= NULL
;
592 static void truncate_node(struct dnode_of_data
*dn
)
594 struct f2fs_sb_info
*sbi
= F2FS_I_SB(dn
->inode
);
597 get_node_info(sbi
, dn
->nid
, &ni
);
598 if (dn
->inode
->i_blocks
== 0) {
599 f2fs_bug_on(sbi
, ni
.blk_addr
!= NULL_ADDR
);
602 f2fs_bug_on(sbi
, ni
.blk_addr
== NULL_ADDR
);
604 /* Deallocate node address */
605 invalidate_blocks(sbi
, ni
.blk_addr
);
606 dec_valid_node_count(sbi
, dn
->inode
);
607 set_node_addr(sbi
, &ni
, NULL_ADDR
, false);
609 if (dn
->nid
== dn
->inode
->i_ino
) {
610 remove_orphan_inode(sbi
, dn
->nid
);
611 dec_valid_inode_count(sbi
);
616 clear_node_page_dirty(dn
->node_page
);
617 set_sbi_flag(sbi
, SBI_IS_DIRTY
);
619 f2fs_put_page(dn
->node_page
, 1);
621 invalidate_mapping_pages(NODE_MAPPING(sbi
),
622 dn
->node_page
->index
, dn
->node_page
->index
);
624 dn
->node_page
= NULL
;
625 trace_f2fs_truncate_node(dn
->inode
, dn
->nid
, ni
.blk_addr
);
628 static int truncate_dnode(struct dnode_of_data
*dn
)
635 /* get direct node */
636 page
= get_node_page(F2FS_I_SB(dn
->inode
), dn
->nid
);
637 if (IS_ERR(page
) && PTR_ERR(page
) == -ENOENT
)
639 else if (IS_ERR(page
))
640 return PTR_ERR(page
);
642 /* Make dnode_of_data for parameter */
643 dn
->node_page
= page
;
645 truncate_data_blocks(dn
);
650 static int truncate_nodes(struct dnode_of_data
*dn
, unsigned int nofs
,
653 struct dnode_of_data rdn
= *dn
;
655 struct f2fs_node
*rn
;
657 unsigned int child_nofs
;
662 return NIDS_PER_BLOCK
+ 1;
664 trace_f2fs_truncate_nodes_enter(dn
->inode
, dn
->nid
, dn
->data_blkaddr
);
666 page
= get_node_page(F2FS_I_SB(dn
->inode
), dn
->nid
);
668 trace_f2fs_truncate_nodes_exit(dn
->inode
, PTR_ERR(page
));
669 return PTR_ERR(page
);
672 rn
= F2FS_NODE(page
);
674 for (i
= ofs
; i
< NIDS_PER_BLOCK
; i
++, freed
++) {
675 child_nid
= le32_to_cpu(rn
->in
.nid
[i
]);
679 ret
= truncate_dnode(&rdn
);
682 set_nid(page
, i
, 0, false);
683 dn
->node_changed
= true;
686 child_nofs
= nofs
+ ofs
* (NIDS_PER_BLOCK
+ 1) + 1;
687 for (i
= ofs
; i
< NIDS_PER_BLOCK
; i
++) {
688 child_nid
= le32_to_cpu(rn
->in
.nid
[i
]);
689 if (child_nid
== 0) {
690 child_nofs
+= NIDS_PER_BLOCK
+ 1;
694 ret
= truncate_nodes(&rdn
, child_nofs
, 0, depth
- 1);
695 if (ret
== (NIDS_PER_BLOCK
+ 1)) {
696 set_nid(page
, i
, 0, false);
697 dn
->node_changed
= true;
699 } else if (ret
< 0 && ret
!= -ENOENT
) {
707 /* remove current indirect node */
708 dn
->node_page
= page
;
712 f2fs_put_page(page
, 1);
714 trace_f2fs_truncate_nodes_exit(dn
->inode
, freed
);
718 f2fs_put_page(page
, 1);
719 trace_f2fs_truncate_nodes_exit(dn
->inode
, ret
);
723 static int truncate_partial_nodes(struct dnode_of_data
*dn
,
724 struct f2fs_inode
*ri
, int *offset
, int depth
)
726 struct page
*pages
[2];
733 nid
[0] = le32_to_cpu(ri
->i_nid
[offset
[0] - NODE_DIR1_BLOCK
]);
737 /* get indirect nodes in the path */
738 for (i
= 0; i
< idx
+ 1; i
++) {
739 /* reference count'll be increased */
740 pages
[i
] = get_node_page(F2FS_I_SB(dn
->inode
), nid
[i
]);
741 if (IS_ERR(pages
[i
])) {
742 err
= PTR_ERR(pages
[i
]);
746 nid
[i
+ 1] = get_nid(pages
[i
], offset
[i
+ 1], false);
749 /* free direct nodes linked to a partial indirect node */
750 for (i
= offset
[idx
+ 1]; i
< NIDS_PER_BLOCK
; i
++) {
751 child_nid
= get_nid(pages
[idx
], i
, false);
755 err
= truncate_dnode(dn
);
758 set_nid(pages
[idx
], i
, 0, false);
759 dn
->node_changed
= true;
762 if (offset
[idx
+ 1] == 0) {
763 dn
->node_page
= pages
[idx
];
767 f2fs_put_page(pages
[idx
], 1);
773 for (i
= idx
; i
>= 0; i
--)
774 f2fs_put_page(pages
[i
], 1);
776 trace_f2fs_truncate_partial_nodes(dn
->inode
, nid
, depth
, err
);
782 * All the block addresses of data and nodes should be nullified.
784 int truncate_inode_blocks(struct inode
*inode
, pgoff_t from
)
786 struct f2fs_sb_info
*sbi
= F2FS_I_SB(inode
);
787 int err
= 0, cont
= 1;
788 int level
, offset
[4], noffset
[4];
789 unsigned int nofs
= 0;
790 struct f2fs_inode
*ri
;
791 struct dnode_of_data dn
;
794 trace_f2fs_truncate_inode_blocks_enter(inode
, from
);
796 level
= get_node_path(F2FS_I(inode
), from
, offset
, noffset
);
798 page
= get_node_page(sbi
, inode
->i_ino
);
800 trace_f2fs_truncate_inode_blocks_exit(inode
, PTR_ERR(page
));
801 return PTR_ERR(page
);
804 set_new_dnode(&dn
, inode
, page
, NULL
, 0);
807 ri
= F2FS_INODE(page
);
815 if (!offset
[level
- 1])
817 err
= truncate_partial_nodes(&dn
, ri
, offset
, level
);
818 if (err
< 0 && err
!= -ENOENT
)
820 nofs
+= 1 + NIDS_PER_BLOCK
;
823 nofs
= 5 + 2 * NIDS_PER_BLOCK
;
824 if (!offset
[level
- 1])
826 err
= truncate_partial_nodes(&dn
, ri
, offset
, level
);
827 if (err
< 0 && err
!= -ENOENT
)
836 dn
.nid
= le32_to_cpu(ri
->i_nid
[offset
[0] - NODE_DIR1_BLOCK
]);
838 case NODE_DIR1_BLOCK
:
839 case NODE_DIR2_BLOCK
:
840 err
= truncate_dnode(&dn
);
843 case NODE_IND1_BLOCK
:
844 case NODE_IND2_BLOCK
:
845 err
= truncate_nodes(&dn
, nofs
, offset
[1], 2);
848 case NODE_DIND_BLOCK
:
849 err
= truncate_nodes(&dn
, nofs
, offset
[1], 3);
856 if (err
< 0 && err
!= -ENOENT
)
858 if (offset
[1] == 0 &&
859 ri
->i_nid
[offset
[0] - NODE_DIR1_BLOCK
]) {
861 if (unlikely(page
->mapping
!= NODE_MAPPING(sbi
))) {
862 f2fs_put_page(page
, 1);
865 f2fs_wait_on_page_writeback(page
, NODE
);
866 ri
->i_nid
[offset
[0] - NODE_DIR1_BLOCK
] = 0;
867 set_page_dirty(page
);
875 f2fs_put_page(page
, 0);
876 trace_f2fs_truncate_inode_blocks_exit(inode
, err
);
877 return err
> 0 ? 0 : err
;
880 int truncate_xattr_node(struct inode
*inode
, struct page
*page
)
882 struct f2fs_sb_info
*sbi
= F2FS_I_SB(inode
);
883 nid_t nid
= F2FS_I(inode
)->i_xattr_nid
;
884 struct dnode_of_data dn
;
890 npage
= get_node_page(sbi
, nid
);
892 return PTR_ERR(npage
);
894 F2FS_I(inode
)->i_xattr_nid
= 0;
896 /* need to do checkpoint during fsync */
897 F2FS_I(inode
)->xattr_ver
= cur_cp_version(F2FS_CKPT(sbi
));
899 set_new_dnode(&dn
, inode
, page
, npage
, nid
);
902 dn
.inode_page_locked
= true;
908 * Caller should grab and release a rwsem by calling f2fs_lock_op() and
911 int remove_inode_page(struct inode
*inode
)
913 struct dnode_of_data dn
;
916 set_new_dnode(&dn
, inode
, NULL
, NULL
, inode
->i_ino
);
917 err
= get_dnode_of_data(&dn
, 0, LOOKUP_NODE
);
921 err
= truncate_xattr_node(inode
, dn
.inode_page
);
927 /* remove potential inline_data blocks */
928 if (S_ISREG(inode
->i_mode
) || S_ISDIR(inode
->i_mode
) ||
929 S_ISLNK(inode
->i_mode
))
930 truncate_data_blocks_range(&dn
, 1);
932 /* 0 is possible, after f2fs_new_inode() has failed */
933 f2fs_bug_on(F2FS_I_SB(inode
),
934 inode
->i_blocks
!= 0 && inode
->i_blocks
!= 1);
936 /* will put inode & node pages */
941 struct page
*new_inode_page(struct inode
*inode
)
943 struct dnode_of_data dn
;
945 /* allocate inode page for new inode */
946 set_new_dnode(&dn
, inode
, NULL
, NULL
, inode
->i_ino
);
948 /* caller should f2fs_put_page(page, 1); */
949 return new_node_page(&dn
, 0, NULL
);
952 struct page
*new_node_page(struct dnode_of_data
*dn
,
953 unsigned int ofs
, struct page
*ipage
)
955 struct f2fs_sb_info
*sbi
= F2FS_I_SB(dn
->inode
);
956 struct node_info old_ni
, new_ni
;
960 if (unlikely(is_inode_flag_set(F2FS_I(dn
->inode
), FI_NO_ALLOC
)))
961 return ERR_PTR(-EPERM
);
963 page
= grab_cache_page(NODE_MAPPING(sbi
), dn
->nid
);
965 return ERR_PTR(-ENOMEM
);
967 if (unlikely(!inc_valid_node_count(sbi
, dn
->inode
))) {
972 get_node_info(sbi
, dn
->nid
, &old_ni
);
974 /* Reinitialize old_ni with new node page */
975 f2fs_bug_on(sbi
, old_ni
.blk_addr
!= NULL_ADDR
);
977 new_ni
.ino
= dn
->inode
->i_ino
;
978 set_node_addr(sbi
, &new_ni
, NEW_ADDR
, false);
980 f2fs_wait_on_page_writeback(page
, NODE
);
981 fill_node_footer(page
, dn
->nid
, dn
->inode
->i_ino
, ofs
, true);
982 set_cold_node(dn
->inode
, page
);
983 SetPageUptodate(page
);
984 set_page_dirty(page
);
986 if (f2fs_has_xattr_block(ofs
))
987 F2FS_I(dn
->inode
)->i_xattr_nid
= dn
->nid
;
989 dn
->node_page
= page
;
991 update_inode(dn
->inode
, ipage
);
995 inc_valid_inode_count(sbi
);
1000 clear_node_page_dirty(page
);
1001 f2fs_put_page(page
, 1);
1002 return ERR_PTR(err
);
1006 * Caller should do after getting the following values.
1007 * 0: f2fs_put_page(page, 0)
1008 * LOCKED_PAGE or error: f2fs_put_page(page, 1)
1010 static int read_node_page(struct page
*page
, int rw
)
1012 struct f2fs_sb_info
*sbi
= F2FS_P_SB(page
);
1013 struct node_info ni
;
1014 struct f2fs_io_info fio
= {
1019 .encrypted_page
= NULL
,
1022 get_node_info(sbi
, page
->index
, &ni
);
1024 if (unlikely(ni
.blk_addr
== NULL_ADDR
)) {
1025 ClearPageUptodate(page
);
1029 if (PageUptodate(page
))
1032 fio
.blk_addr
= ni
.blk_addr
;
1033 return f2fs_submit_page_bio(&fio
);
1037 * Readahead a node page
1039 void ra_node_page(struct f2fs_sb_info
*sbi
, nid_t nid
)
1044 apage
= find_get_page(NODE_MAPPING(sbi
), nid
);
1045 if (apage
&& PageUptodate(apage
)) {
1046 f2fs_put_page(apage
, 0);
1049 f2fs_put_page(apage
, 0);
1051 apage
= grab_cache_page(NODE_MAPPING(sbi
), nid
);
1055 err
= read_node_page(apage
, READA
);
1056 f2fs_put_page(apage
, err
? 1 : 0);
1059 struct page
*get_node_page(struct f2fs_sb_info
*sbi
, pgoff_t nid
)
1065 return ERR_PTR(-ENOENT
);
1066 f2fs_bug_on(sbi
, check_nid_range(sbi
, nid
));
1068 page
= grab_cache_page(NODE_MAPPING(sbi
), nid
);
1070 return ERR_PTR(-ENOMEM
);
1072 err
= read_node_page(page
, READ_SYNC
);
1074 f2fs_put_page(page
, 1);
1075 return ERR_PTR(err
);
1076 } else if (err
== LOCKED_PAGE
) {
1082 if (unlikely(!PageUptodate(page
))) {
1083 f2fs_put_page(page
, 1);
1084 return ERR_PTR(-EIO
);
1086 if (unlikely(page
->mapping
!= NODE_MAPPING(sbi
))) {
1087 f2fs_put_page(page
, 1);
1091 f2fs_bug_on(sbi
, nid
!= nid_of_node(page
));
1096 * Return a locked page for the desired node page.
1097 * And, readahead MAX_RA_NODE number of node pages.
1099 struct page
*get_node_page_ra(struct page
*parent
, int start
)
1101 struct f2fs_sb_info
*sbi
= F2FS_P_SB(parent
);
1102 struct blk_plug plug
;
1107 /* First, try getting the desired direct node. */
1108 nid
= get_nid(parent
, start
, false);
1110 return ERR_PTR(-ENOENT
);
1112 page
= grab_cache_page(NODE_MAPPING(sbi
), nid
);
1114 return ERR_PTR(-ENOMEM
);
1116 err
= read_node_page(page
, READ_SYNC
);
1118 f2fs_put_page(page
, 1);
1119 return ERR_PTR(err
);
1120 } else if (err
== LOCKED_PAGE
) {
1124 blk_start_plug(&plug
);
1126 /* Then, try readahead for siblings of the desired node */
1127 end
= start
+ MAX_RA_NODE
;
1128 end
= min(end
, NIDS_PER_BLOCK
);
1129 for (i
= start
+ 1; i
< end
; i
++) {
1130 nid_t tnid
= get_nid(parent
, i
, false);
1133 ra_node_page(sbi
, tnid
);
1136 blk_finish_plug(&plug
);
1139 if (unlikely(!PageUptodate(page
))) {
1140 f2fs_put_page(page
, 1);
1141 return ERR_PTR(-EIO
);
1143 if (unlikely(page
->mapping
!= NODE_MAPPING(sbi
))) {
1144 f2fs_put_page(page
, 1);
1148 f2fs_bug_on(sbi
, nid
!= nid_of_node(page
));
1152 void sync_inode_page(struct dnode_of_data
*dn
)
1154 if (IS_INODE(dn
->node_page
) || dn
->inode_page
== dn
->node_page
) {
1155 update_inode(dn
->inode
, dn
->node_page
);
1156 } else if (dn
->inode_page
) {
1157 if (!dn
->inode_page_locked
)
1158 lock_page(dn
->inode_page
);
1159 update_inode(dn
->inode
, dn
->inode_page
);
1160 if (!dn
->inode_page_locked
)
1161 unlock_page(dn
->inode_page
);
1163 update_inode_page(dn
->inode
);
1165 dn
->node_changed
= true;
1168 int sync_node_pages(struct f2fs_sb_info
*sbi
, nid_t ino
,
1169 struct writeback_control
*wbc
)
1172 struct pagevec pvec
;
1173 int step
= ino
? 2 : 0;
1174 int nwritten
= 0, wrote
= 0;
1176 pagevec_init(&pvec
, 0);
1182 while (index
<= end
) {
1184 nr_pages
= pagevec_lookup_tag(&pvec
, NODE_MAPPING(sbi
), &index
,
1185 PAGECACHE_TAG_DIRTY
,
1186 min(end
- index
, (pgoff_t
)PAGEVEC_SIZE
-1) + 1);
1190 for (i
= 0; i
< nr_pages
; i
++) {
1191 struct page
*page
= pvec
.pages
[i
];
1193 if (unlikely(f2fs_cp_error(sbi
))) {
1194 pagevec_release(&pvec
);
1199 * flushing sequence with step:
1204 if (step
== 0 && IS_DNODE(page
))
1206 if (step
== 1 && (!IS_DNODE(page
) ||
1207 is_cold_node(page
)))
1209 if (step
== 2 && (!IS_DNODE(page
) ||
1210 !is_cold_node(page
)))
1215 * we should not skip writing node pages.
1217 if (ino
&& ino_of_node(page
) == ino
)
1219 else if (!trylock_page(page
))
1222 if (unlikely(page
->mapping
!= NODE_MAPPING(sbi
))) {
1227 if (ino
&& ino_of_node(page
) != ino
)
1228 goto continue_unlock
;
1230 if (!PageDirty(page
)) {
1231 /* someone wrote it for us */
1232 goto continue_unlock
;
1235 if (!clear_page_dirty_for_io(page
))
1236 goto continue_unlock
;
1238 /* called by fsync() */
1239 if (ino
&& IS_DNODE(page
)) {
1240 set_fsync_mark(page
, 1);
1242 set_dentry_mark(page
,
1243 need_dentry_mark(sbi
, ino
));
1246 set_fsync_mark(page
, 0);
1247 set_dentry_mark(page
, 0);
1250 if (NODE_MAPPING(sbi
)->a_ops
->writepage(page
, wbc
))
1255 if (--wbc
->nr_to_write
== 0)
1258 pagevec_release(&pvec
);
1261 if (wbc
->nr_to_write
== 0) {
1273 f2fs_submit_merged_bio(sbi
, NODE
, WRITE
);
1277 int wait_on_node_pages_writeback(struct f2fs_sb_info
*sbi
, nid_t ino
)
1279 pgoff_t index
= 0, end
= LONG_MAX
;
1280 struct pagevec pvec
;
1281 int ret2
= 0, ret
= 0;
1283 pagevec_init(&pvec
, 0);
1285 while (index
<= end
) {
1287 nr_pages
= pagevec_lookup_tag(&pvec
, NODE_MAPPING(sbi
), &index
,
1288 PAGECACHE_TAG_WRITEBACK
,
1289 min(end
- index
, (pgoff_t
)PAGEVEC_SIZE
-1) + 1);
1293 for (i
= 0; i
< nr_pages
; i
++) {
1294 struct page
*page
= pvec
.pages
[i
];
1296 /* until radix tree lookup accepts end_index */
1297 if (unlikely(page
->index
> end
))
1300 if (ino
&& ino_of_node(page
) == ino
) {
1301 f2fs_wait_on_page_writeback(page
, NODE
);
1302 if (TestClearPageError(page
))
1306 pagevec_release(&pvec
);
1310 if (unlikely(test_and_clear_bit(AS_ENOSPC
, &NODE_MAPPING(sbi
)->flags
)))
1312 if (unlikely(test_and_clear_bit(AS_EIO
, &NODE_MAPPING(sbi
)->flags
)))
1319 static int f2fs_write_node_page(struct page
*page
,
1320 struct writeback_control
*wbc
)
1322 struct f2fs_sb_info
*sbi
= F2FS_P_SB(page
);
1324 struct node_info ni
;
1325 struct f2fs_io_info fio
= {
1328 .rw
= (wbc
->sync_mode
== WB_SYNC_ALL
) ? WRITE_SYNC
: WRITE
,
1330 .encrypted_page
= NULL
,
1333 trace_f2fs_writepage(page
, NODE
);
1335 if (unlikely(is_sbi_flag_set(sbi
, SBI_POR_DOING
)))
1337 if (unlikely(f2fs_cp_error(sbi
)))
1340 f2fs_wait_on_page_writeback(page
, NODE
);
1342 /* get old block addr of this node page */
1343 nid
= nid_of_node(page
);
1344 f2fs_bug_on(sbi
, page
->index
!= nid
);
1346 if (wbc
->for_reclaim
) {
1347 if (!down_read_trylock(&sbi
->node_write
))
1350 down_read(&sbi
->node_write
);
1353 get_node_info(sbi
, nid
, &ni
);
1355 /* This page is already truncated */
1356 if (unlikely(ni
.blk_addr
== NULL_ADDR
)) {
1357 ClearPageUptodate(page
);
1358 dec_page_count(sbi
, F2FS_DIRTY_NODES
);
1359 up_read(&sbi
->node_write
);
1364 set_page_writeback(page
);
1365 fio
.blk_addr
= ni
.blk_addr
;
1366 write_node_page(nid
, &fio
);
1367 set_node_addr(sbi
, &ni
, fio
.blk_addr
, is_fsync_dnode(page
));
1368 dec_page_count(sbi
, F2FS_DIRTY_NODES
);
1369 up_read(&sbi
->node_write
);
1372 if (wbc
->for_reclaim
|| unlikely(f2fs_cp_error(sbi
)))
1373 f2fs_submit_merged_bio(sbi
, NODE
, WRITE
);
1378 redirty_page_for_writepage(wbc
, page
);
1379 return AOP_WRITEPAGE_ACTIVATE
;
1382 static int f2fs_write_node_pages(struct address_space
*mapping
,
1383 struct writeback_control
*wbc
)
1385 struct f2fs_sb_info
*sbi
= F2FS_M_SB(mapping
);
1388 trace_f2fs_writepages(mapping
->host
, wbc
, NODE
);
1390 /* balancing f2fs's metadata in background */
1391 f2fs_balance_fs_bg(sbi
);
1393 /* collect a number of dirty node pages and write together */
1394 if (get_pages(sbi
, F2FS_DIRTY_NODES
) < nr_pages_to_skip(sbi
, NODE
))
1397 diff
= nr_pages_to_write(sbi
, NODE
, wbc
);
1398 wbc
->sync_mode
= WB_SYNC_NONE
;
1399 sync_node_pages(sbi
, 0, wbc
);
1400 wbc
->nr_to_write
= max((long)0, wbc
->nr_to_write
- diff
);
1404 wbc
->pages_skipped
+= get_pages(sbi
, F2FS_DIRTY_NODES
);
1408 static int f2fs_set_node_page_dirty(struct page
*page
)
1410 trace_f2fs_set_page_dirty(page
, NODE
);
1412 SetPageUptodate(page
);
1413 if (!PageDirty(page
)) {
1414 __set_page_dirty_nobuffers(page
);
1415 inc_page_count(F2FS_P_SB(page
), F2FS_DIRTY_NODES
);
1416 SetPagePrivate(page
);
1417 f2fs_trace_pid(page
);
1424 * Structure of the f2fs node operations
1426 const struct address_space_operations f2fs_node_aops
= {
1427 .writepage
= f2fs_write_node_page
,
1428 .writepages
= f2fs_write_node_pages
,
1429 .set_page_dirty
= f2fs_set_node_page_dirty
,
1430 .invalidatepage
= f2fs_invalidate_page
,
1431 .releasepage
= f2fs_release_page
,
1434 static struct free_nid
*__lookup_free_nid_list(struct f2fs_nm_info
*nm_i
,
1437 return radix_tree_lookup(&nm_i
->free_nid_root
, n
);
1440 static void __del_from_free_nid_list(struct f2fs_nm_info
*nm_i
,
1444 radix_tree_delete(&nm_i
->free_nid_root
, i
->nid
);
1447 static int add_free_nid(struct f2fs_sb_info
*sbi
, nid_t nid
, bool build
)
1449 struct f2fs_nm_info
*nm_i
= NM_I(sbi
);
1451 struct nat_entry
*ne
;
1452 bool allocated
= false;
1454 if (!available_free_memory(sbi
, FREE_NIDS
))
1457 /* 0 nid should not be used */
1458 if (unlikely(nid
== 0))
1462 /* do not add allocated nids */
1463 ne
= __lookup_nat_cache(nm_i
, nid
);
1464 if (ne
&& (!get_nat_flag(ne
, IS_CHECKPOINTED
) ||
1465 nat_get_blkaddr(ne
) != NULL_ADDR
))
1471 i
= f2fs_kmem_cache_alloc(free_nid_slab
, GFP_NOFS
);
1475 if (radix_tree_preload(GFP_NOFS
)) {
1476 kmem_cache_free(free_nid_slab
, i
);
1480 spin_lock(&nm_i
->free_nid_list_lock
);
1481 if (radix_tree_insert(&nm_i
->free_nid_root
, i
->nid
, i
)) {
1482 spin_unlock(&nm_i
->free_nid_list_lock
);
1483 radix_tree_preload_end();
1484 kmem_cache_free(free_nid_slab
, i
);
1487 list_add_tail(&i
->list
, &nm_i
->free_nid_list
);
1489 spin_unlock(&nm_i
->free_nid_list_lock
);
1490 radix_tree_preload_end();
1494 static void remove_free_nid(struct f2fs_nm_info
*nm_i
, nid_t nid
)
1497 bool need_free
= false;
1499 spin_lock(&nm_i
->free_nid_list_lock
);
1500 i
= __lookup_free_nid_list(nm_i
, nid
);
1501 if (i
&& i
->state
== NID_NEW
) {
1502 __del_from_free_nid_list(nm_i
, i
);
1506 spin_unlock(&nm_i
->free_nid_list_lock
);
1509 kmem_cache_free(free_nid_slab
, i
);
1512 static void scan_nat_page(struct f2fs_sb_info
*sbi
,
1513 struct page
*nat_page
, nid_t start_nid
)
1515 struct f2fs_nm_info
*nm_i
= NM_I(sbi
);
1516 struct f2fs_nat_block
*nat_blk
= page_address(nat_page
);
1520 i
= start_nid
% NAT_ENTRY_PER_BLOCK
;
1522 for (; i
< NAT_ENTRY_PER_BLOCK
; i
++, start_nid
++) {
1524 if (unlikely(start_nid
>= nm_i
->max_nid
))
1527 blk_addr
= le32_to_cpu(nat_blk
->entries
[i
].block_addr
);
1528 f2fs_bug_on(sbi
, blk_addr
== NEW_ADDR
);
1529 if (blk_addr
== NULL_ADDR
) {
1530 if (add_free_nid(sbi
, start_nid
, true) < 0)
1536 static void build_free_nids(struct f2fs_sb_info
*sbi
)
1538 struct f2fs_nm_info
*nm_i
= NM_I(sbi
);
1539 struct curseg_info
*curseg
= CURSEG_I(sbi
, CURSEG_HOT_DATA
);
1540 struct f2fs_summary_block
*sum
= curseg
->sum_blk
;
1542 nid_t nid
= nm_i
->next_scan_nid
;
1544 /* Enough entries */
1545 if (nm_i
->fcnt
> NAT_ENTRY_PER_BLOCK
)
1548 /* readahead nat pages to be scanned */
1549 ra_meta_pages(sbi
, NAT_BLOCK_OFFSET(nid
), FREE_NID_PAGES
,
1552 down_read(&nm_i
->nat_tree_lock
);
1555 struct page
*page
= get_current_nat_page(sbi
, nid
);
1557 scan_nat_page(sbi
, page
, nid
);
1558 f2fs_put_page(page
, 1);
1560 nid
+= (NAT_ENTRY_PER_BLOCK
- (nid
% NAT_ENTRY_PER_BLOCK
));
1561 if (unlikely(nid
>= nm_i
->max_nid
))
1564 if (++i
>= FREE_NID_PAGES
)
1568 /* go to the next free nat pages to find free nids abundantly */
1569 nm_i
->next_scan_nid
= nid
;
1571 /* find free nids from current sum_pages */
1572 mutex_lock(&curseg
->curseg_mutex
);
1573 for (i
= 0; i
< nats_in_cursum(sum
); i
++) {
1574 block_t addr
= le32_to_cpu(nat_in_journal(sum
, i
).block_addr
);
1575 nid
= le32_to_cpu(nid_in_journal(sum
, i
));
1576 if (addr
== NULL_ADDR
)
1577 add_free_nid(sbi
, nid
, true);
1579 remove_free_nid(nm_i
, nid
);
1581 mutex_unlock(&curseg
->curseg_mutex
);
1582 up_read(&nm_i
->nat_tree_lock
);
1584 ra_meta_pages(sbi
, NAT_BLOCK_OFFSET(nm_i
->next_scan_nid
),
1585 nm_i
->ra_nid_pages
, META_NAT
, false);
1589 * If this function returns success, caller can obtain a new nid
1590 * from second parameter of this function.
1591 * The returned nid could be used ino as well as nid when inode is created.
1593 bool alloc_nid(struct f2fs_sb_info
*sbi
, nid_t
*nid
)
1595 struct f2fs_nm_info
*nm_i
= NM_I(sbi
);
1596 struct free_nid
*i
= NULL
;
1598 if (unlikely(sbi
->total_valid_node_count
+ 1 > nm_i
->available_nids
))
1601 spin_lock(&nm_i
->free_nid_list_lock
);
1603 /* We should not use stale free nids created by build_free_nids */
1604 if (nm_i
->fcnt
&& !on_build_free_nids(nm_i
)) {
1605 struct node_info ni
;
1607 f2fs_bug_on(sbi
, list_empty(&nm_i
->free_nid_list
));
1608 list_for_each_entry(i
, &nm_i
->free_nid_list
, list
)
1609 if (i
->state
== NID_NEW
)
1612 f2fs_bug_on(sbi
, i
->state
!= NID_NEW
);
1614 i
->state
= NID_ALLOC
;
1616 spin_unlock(&nm_i
->free_nid_list_lock
);
1618 /* check nid is allocated already */
1619 get_node_info(sbi
, *nid
, &ni
);
1620 if (ni
.blk_addr
!= NULL_ADDR
) {
1621 alloc_nid_done(sbi
, *nid
);
1626 spin_unlock(&nm_i
->free_nid_list_lock
);
1628 /* Let's scan nat pages and its caches to get free nids */
1629 mutex_lock(&nm_i
->build_lock
);
1630 build_free_nids(sbi
);
1631 mutex_unlock(&nm_i
->build_lock
);
1636 * alloc_nid() should be called prior to this function.
1638 void alloc_nid_done(struct f2fs_sb_info
*sbi
, nid_t nid
)
1640 struct f2fs_nm_info
*nm_i
= NM_I(sbi
);
1643 spin_lock(&nm_i
->free_nid_list_lock
);
1644 i
= __lookup_free_nid_list(nm_i
, nid
);
1645 f2fs_bug_on(sbi
, !i
|| i
->state
!= NID_ALLOC
);
1646 __del_from_free_nid_list(nm_i
, i
);
1647 spin_unlock(&nm_i
->free_nid_list_lock
);
1649 kmem_cache_free(free_nid_slab
, i
);
1653 * alloc_nid() should be called prior to this function.
1655 void alloc_nid_failed(struct f2fs_sb_info
*sbi
, nid_t nid
)
1657 struct f2fs_nm_info
*nm_i
= NM_I(sbi
);
1659 bool need_free
= false;
1664 spin_lock(&nm_i
->free_nid_list_lock
);
1665 i
= __lookup_free_nid_list(nm_i
, nid
);
1666 f2fs_bug_on(sbi
, !i
|| i
->state
!= NID_ALLOC
);
1667 if (!available_free_memory(sbi
, FREE_NIDS
)) {
1668 __del_from_free_nid_list(nm_i
, i
);
1674 spin_unlock(&nm_i
->free_nid_list_lock
);
1677 kmem_cache_free(free_nid_slab
, i
);
1680 int try_to_free_nids(struct f2fs_sb_info
*sbi
, int nr_shrink
)
1682 struct f2fs_nm_info
*nm_i
= NM_I(sbi
);
1683 struct free_nid
*i
, *next
;
1686 if (!mutex_trylock(&nm_i
->build_lock
))
1689 spin_lock(&nm_i
->free_nid_list_lock
);
1690 list_for_each_entry_safe(i
, next
, &nm_i
->free_nid_list
, list
) {
1691 if (nr_shrink
<= 0 || nm_i
->fcnt
<= NAT_ENTRY_PER_BLOCK
)
1693 if (i
->state
== NID_ALLOC
)
1695 __del_from_free_nid_list(nm_i
, i
);
1696 kmem_cache_free(free_nid_slab
, i
);
1700 spin_unlock(&nm_i
->free_nid_list_lock
);
1701 mutex_unlock(&nm_i
->build_lock
);
1703 return nr
- nr_shrink
;
1706 void recover_inline_xattr(struct inode
*inode
, struct page
*page
)
1708 void *src_addr
, *dst_addr
;
1711 struct f2fs_inode
*ri
;
1713 ipage
= get_node_page(F2FS_I_SB(inode
), inode
->i_ino
);
1714 f2fs_bug_on(F2FS_I_SB(inode
), IS_ERR(ipage
));
1716 ri
= F2FS_INODE(page
);
1717 if (!(ri
->i_inline
& F2FS_INLINE_XATTR
)) {
1718 clear_inode_flag(F2FS_I(inode
), FI_INLINE_XATTR
);
1722 dst_addr
= inline_xattr_addr(ipage
);
1723 src_addr
= inline_xattr_addr(page
);
1724 inline_size
= inline_xattr_size(inode
);
1726 f2fs_wait_on_page_writeback(ipage
, NODE
);
1727 memcpy(dst_addr
, src_addr
, inline_size
);
1729 update_inode(inode
, ipage
);
1730 f2fs_put_page(ipage
, 1);
1733 void recover_xattr_data(struct inode
*inode
, struct page
*page
, block_t blkaddr
)
1735 struct f2fs_sb_info
*sbi
= F2FS_I_SB(inode
);
1736 nid_t prev_xnid
= F2FS_I(inode
)->i_xattr_nid
;
1737 nid_t new_xnid
= nid_of_node(page
);
1738 struct node_info ni
;
1740 /* 1: invalidate the previous xattr nid */
1744 /* Deallocate node address */
1745 get_node_info(sbi
, prev_xnid
, &ni
);
1746 f2fs_bug_on(sbi
, ni
.blk_addr
== NULL_ADDR
);
1747 invalidate_blocks(sbi
, ni
.blk_addr
);
1748 dec_valid_node_count(sbi
, inode
);
1749 set_node_addr(sbi
, &ni
, NULL_ADDR
, false);
1752 /* 2: allocate new xattr nid */
1753 if (unlikely(!inc_valid_node_count(sbi
, inode
)))
1754 f2fs_bug_on(sbi
, 1);
1756 remove_free_nid(NM_I(sbi
), new_xnid
);
1757 get_node_info(sbi
, new_xnid
, &ni
);
1758 ni
.ino
= inode
->i_ino
;
1759 set_node_addr(sbi
, &ni
, NEW_ADDR
, false);
1760 F2FS_I(inode
)->i_xattr_nid
= new_xnid
;
1762 /* 3: update xattr blkaddr */
1763 refresh_sit_entry(sbi
, NEW_ADDR
, blkaddr
);
1764 set_node_addr(sbi
, &ni
, blkaddr
, false);
1766 update_inode_page(inode
);
1769 int recover_inode_page(struct f2fs_sb_info
*sbi
, struct page
*page
)
1771 struct f2fs_inode
*src
, *dst
;
1772 nid_t ino
= ino_of_node(page
);
1773 struct node_info old_ni
, new_ni
;
1776 get_node_info(sbi
, ino
, &old_ni
);
1778 if (unlikely(old_ni
.blk_addr
!= NULL_ADDR
))
1781 ipage
= grab_cache_page(NODE_MAPPING(sbi
), ino
);
1785 /* Should not use this inode from free nid list */
1786 remove_free_nid(NM_I(sbi
), ino
);
1788 SetPageUptodate(ipage
);
1789 fill_node_footer(ipage
, ino
, ino
, 0, true);
1791 src
= F2FS_INODE(page
);
1792 dst
= F2FS_INODE(ipage
);
1794 memcpy(dst
, src
, (unsigned long)&src
->i_ext
- (unsigned long)src
);
1796 dst
->i_blocks
= cpu_to_le64(1);
1797 dst
->i_links
= cpu_to_le32(1);
1798 dst
->i_xattr_nid
= 0;
1799 dst
->i_inline
= src
->i_inline
& F2FS_INLINE_XATTR
;
1804 if (unlikely(!inc_valid_node_count(sbi
, NULL
)))
1806 set_node_addr(sbi
, &new_ni
, NEW_ADDR
, false);
1807 inc_valid_inode_count(sbi
);
1808 set_page_dirty(ipage
);
1809 f2fs_put_page(ipage
, 1);
1813 int restore_node_summary(struct f2fs_sb_info
*sbi
,
1814 unsigned int segno
, struct f2fs_summary_block
*sum
)
1816 struct f2fs_node
*rn
;
1817 struct f2fs_summary
*sum_entry
;
1819 int bio_blocks
= MAX_BIO_BLOCKS(sbi
);
1820 int i
, idx
, last_offset
, nrpages
;
1822 /* scan the node segment */
1823 last_offset
= sbi
->blocks_per_seg
;
1824 addr
= START_BLOCK(sbi
, segno
);
1825 sum_entry
= &sum
->entries
[0];
1827 for (i
= 0; i
< last_offset
; i
+= nrpages
, addr
+= nrpages
) {
1828 nrpages
= min(last_offset
- i
, bio_blocks
);
1830 /* readahead node pages */
1831 ra_meta_pages(sbi
, addr
, nrpages
, META_POR
, true);
1833 for (idx
= addr
; idx
< addr
+ nrpages
; idx
++) {
1834 struct page
*page
= get_tmp_page(sbi
, idx
);
1836 rn
= F2FS_NODE(page
);
1837 sum_entry
->nid
= rn
->footer
.nid
;
1838 sum_entry
->version
= 0;
1839 sum_entry
->ofs_in_node
= 0;
1841 f2fs_put_page(page
, 1);
1844 invalidate_mapping_pages(META_MAPPING(sbi
), addr
,
1850 static void remove_nats_in_journal(struct f2fs_sb_info
*sbi
)
1852 struct f2fs_nm_info
*nm_i
= NM_I(sbi
);
1853 struct curseg_info
*curseg
= CURSEG_I(sbi
, CURSEG_HOT_DATA
);
1854 struct f2fs_summary_block
*sum
= curseg
->sum_blk
;
1857 mutex_lock(&curseg
->curseg_mutex
);
1858 for (i
= 0; i
< nats_in_cursum(sum
); i
++) {
1859 struct nat_entry
*ne
;
1860 struct f2fs_nat_entry raw_ne
;
1861 nid_t nid
= le32_to_cpu(nid_in_journal(sum
, i
));
1863 raw_ne
= nat_in_journal(sum
, i
);
1865 ne
= __lookup_nat_cache(nm_i
, nid
);
1867 ne
= grab_nat_entry(nm_i
, nid
);
1868 node_info_from_raw_nat(&ne
->ni
, &raw_ne
);
1870 __set_nat_cache_dirty(nm_i
, ne
);
1872 update_nats_in_cursum(sum
, -i
);
1873 mutex_unlock(&curseg
->curseg_mutex
);
1876 static void __adjust_nat_entry_set(struct nat_entry_set
*nes
,
1877 struct list_head
*head
, int max
)
1879 struct nat_entry_set
*cur
;
1881 if (nes
->entry_cnt
>= max
)
1884 list_for_each_entry(cur
, head
, set_list
) {
1885 if (cur
->entry_cnt
>= nes
->entry_cnt
) {
1886 list_add(&nes
->set_list
, cur
->set_list
.prev
);
1891 list_add_tail(&nes
->set_list
, head
);
1894 static void __flush_nat_entry_set(struct f2fs_sb_info
*sbi
,
1895 struct nat_entry_set
*set
)
1897 struct curseg_info
*curseg
= CURSEG_I(sbi
, CURSEG_HOT_DATA
);
1898 struct f2fs_summary_block
*sum
= curseg
->sum_blk
;
1899 nid_t start_nid
= set
->set
* NAT_ENTRY_PER_BLOCK
;
1900 bool to_journal
= true;
1901 struct f2fs_nat_block
*nat_blk
;
1902 struct nat_entry
*ne
, *cur
;
1903 struct page
*page
= NULL
;
1906 * there are two steps to flush nat entries:
1907 * #1, flush nat entries to journal in current hot data summary block.
1908 * #2, flush nat entries to nat page.
1910 if (!__has_cursum_space(sum
, set
->entry_cnt
, NAT_JOURNAL
))
1914 mutex_lock(&curseg
->curseg_mutex
);
1916 page
= get_next_nat_page(sbi
, start_nid
);
1917 nat_blk
= page_address(page
);
1918 f2fs_bug_on(sbi
, !nat_blk
);
1921 /* flush dirty nats in nat entry set */
1922 list_for_each_entry_safe(ne
, cur
, &set
->entry_list
, list
) {
1923 struct f2fs_nat_entry
*raw_ne
;
1924 nid_t nid
= nat_get_nid(ne
);
1927 if (nat_get_blkaddr(ne
) == NEW_ADDR
)
1931 offset
= lookup_journal_in_cursum(sum
,
1932 NAT_JOURNAL
, nid
, 1);
1933 f2fs_bug_on(sbi
, offset
< 0);
1934 raw_ne
= &nat_in_journal(sum
, offset
);
1935 nid_in_journal(sum
, offset
) = cpu_to_le32(nid
);
1937 raw_ne
= &nat_blk
->entries
[nid
- start_nid
];
1939 raw_nat_from_node_info(raw_ne
, &ne
->ni
);
1941 __clear_nat_cache_dirty(NM_I(sbi
), ne
);
1942 if (nat_get_blkaddr(ne
) == NULL_ADDR
)
1943 add_free_nid(sbi
, nid
, false);
1947 mutex_unlock(&curseg
->curseg_mutex
);
1949 f2fs_put_page(page
, 1);
1951 f2fs_bug_on(sbi
, set
->entry_cnt
);
1953 radix_tree_delete(&NM_I(sbi
)->nat_set_root
, set
->set
);
1954 kmem_cache_free(nat_entry_set_slab
, set
);
1958 * This function is called during the checkpointing process.
1960 void flush_nat_entries(struct f2fs_sb_info
*sbi
)
1962 struct f2fs_nm_info
*nm_i
= NM_I(sbi
);
1963 struct curseg_info
*curseg
= CURSEG_I(sbi
, CURSEG_HOT_DATA
);
1964 struct f2fs_summary_block
*sum
= curseg
->sum_blk
;
1965 struct nat_entry_set
*setvec
[SETVEC_SIZE
];
1966 struct nat_entry_set
*set
, *tmp
;
1971 if (!nm_i
->dirty_nat_cnt
)
1974 down_write(&nm_i
->nat_tree_lock
);
1977 * if there are no enough space in journal to store dirty nat
1978 * entries, remove all entries from journal and merge them
1979 * into nat entry set.
1981 if (!__has_cursum_space(sum
, nm_i
->dirty_nat_cnt
, NAT_JOURNAL
))
1982 remove_nats_in_journal(sbi
);
1984 while ((found
= __gang_lookup_nat_set(nm_i
,
1985 set_idx
, SETVEC_SIZE
, setvec
))) {
1987 set_idx
= setvec
[found
- 1]->set
+ 1;
1988 for (idx
= 0; idx
< found
; idx
++)
1989 __adjust_nat_entry_set(setvec
[idx
], &sets
,
1990 MAX_NAT_JENTRIES(sum
));
1993 /* flush dirty nats in nat entry set */
1994 list_for_each_entry_safe(set
, tmp
, &sets
, set_list
)
1995 __flush_nat_entry_set(sbi
, set
);
1997 up_write(&nm_i
->nat_tree_lock
);
1999 f2fs_bug_on(sbi
, nm_i
->dirty_nat_cnt
);
2002 static int init_node_manager(struct f2fs_sb_info
*sbi
)
2004 struct f2fs_super_block
*sb_raw
= F2FS_RAW_SUPER(sbi
);
2005 struct f2fs_nm_info
*nm_i
= NM_I(sbi
);
2006 unsigned char *version_bitmap
;
2007 unsigned int nat_segs
, nat_blocks
;
2009 nm_i
->nat_blkaddr
= le32_to_cpu(sb_raw
->nat_blkaddr
);
2011 /* segment_count_nat includes pair segment so divide to 2. */
2012 nat_segs
= le32_to_cpu(sb_raw
->segment_count_nat
) >> 1;
2013 nat_blocks
= nat_segs
<< le32_to_cpu(sb_raw
->log_blocks_per_seg
);
2015 nm_i
->max_nid
= NAT_ENTRY_PER_BLOCK
* nat_blocks
;
2017 /* not used nids: 0, node, meta, (and root counted as valid node) */
2018 nm_i
->available_nids
= nm_i
->max_nid
- F2FS_RESERVED_NODE_NUM
;
2021 nm_i
->ram_thresh
= DEF_RAM_THRESHOLD
;
2022 nm_i
->ra_nid_pages
= DEF_RA_NID_PAGES
;
2024 INIT_RADIX_TREE(&nm_i
->free_nid_root
, GFP_ATOMIC
);
2025 INIT_LIST_HEAD(&nm_i
->free_nid_list
);
2026 INIT_RADIX_TREE(&nm_i
->nat_root
, GFP_NOIO
);
2027 INIT_RADIX_TREE(&nm_i
->nat_set_root
, GFP_NOIO
);
2028 INIT_LIST_HEAD(&nm_i
->nat_entries
);
2030 mutex_init(&nm_i
->build_lock
);
2031 spin_lock_init(&nm_i
->free_nid_list_lock
);
2032 init_rwsem(&nm_i
->nat_tree_lock
);
2034 nm_i
->next_scan_nid
= le32_to_cpu(sbi
->ckpt
->next_free_nid
);
2035 nm_i
->bitmap_size
= __bitmap_size(sbi
, NAT_BITMAP
);
2036 version_bitmap
= __bitmap_ptr(sbi
, NAT_BITMAP
);
2037 if (!version_bitmap
)
2040 nm_i
->nat_bitmap
= kmemdup(version_bitmap
, nm_i
->bitmap_size
,
2042 if (!nm_i
->nat_bitmap
)
2047 int build_node_manager(struct f2fs_sb_info
*sbi
)
2051 sbi
->nm_info
= kzalloc(sizeof(struct f2fs_nm_info
), GFP_KERNEL
);
2055 err
= init_node_manager(sbi
);
2059 build_free_nids(sbi
);
2063 void destroy_node_manager(struct f2fs_sb_info
*sbi
)
2065 struct f2fs_nm_info
*nm_i
= NM_I(sbi
);
2066 struct free_nid
*i
, *next_i
;
2067 struct nat_entry
*natvec
[NATVEC_SIZE
];
2068 struct nat_entry_set
*setvec
[SETVEC_SIZE
];
2075 /* destroy free nid list */
2076 spin_lock(&nm_i
->free_nid_list_lock
);
2077 list_for_each_entry_safe(i
, next_i
, &nm_i
->free_nid_list
, list
) {
2078 f2fs_bug_on(sbi
, i
->state
== NID_ALLOC
);
2079 __del_from_free_nid_list(nm_i
, i
);
2081 spin_unlock(&nm_i
->free_nid_list_lock
);
2082 kmem_cache_free(free_nid_slab
, i
);
2083 spin_lock(&nm_i
->free_nid_list_lock
);
2085 f2fs_bug_on(sbi
, nm_i
->fcnt
);
2086 spin_unlock(&nm_i
->free_nid_list_lock
);
2088 /* destroy nat cache */
2089 down_write(&nm_i
->nat_tree_lock
);
2090 while ((found
= __gang_lookup_nat_cache(nm_i
,
2091 nid
, NATVEC_SIZE
, natvec
))) {
2094 nid
= nat_get_nid(natvec
[found
- 1]) + 1;
2095 for (idx
= 0; idx
< found
; idx
++)
2096 __del_from_nat_cache(nm_i
, natvec
[idx
]);
2098 f2fs_bug_on(sbi
, nm_i
->nat_cnt
);
2100 /* destroy nat set cache */
2102 while ((found
= __gang_lookup_nat_set(nm_i
,
2103 nid
, SETVEC_SIZE
, setvec
))) {
2106 nid
= setvec
[found
- 1]->set
+ 1;
2107 for (idx
= 0; idx
< found
; idx
++) {
2108 /* entry_cnt is not zero, when cp_error was occurred */
2109 f2fs_bug_on(sbi
, !list_empty(&setvec
[idx
]->entry_list
));
2110 radix_tree_delete(&nm_i
->nat_set_root
, setvec
[idx
]->set
);
2111 kmem_cache_free(nat_entry_set_slab
, setvec
[idx
]);
2114 up_write(&nm_i
->nat_tree_lock
);
2116 kfree(nm_i
->nat_bitmap
);
2117 sbi
->nm_info
= NULL
;
2121 int __init
create_node_manager_caches(void)
2123 nat_entry_slab
= f2fs_kmem_cache_create("nat_entry",
2124 sizeof(struct nat_entry
));
2125 if (!nat_entry_slab
)
2128 free_nid_slab
= f2fs_kmem_cache_create("free_nid",
2129 sizeof(struct free_nid
));
2131 goto destroy_nat_entry
;
2133 nat_entry_set_slab
= f2fs_kmem_cache_create("nat_entry_set",
2134 sizeof(struct nat_entry_set
));
2135 if (!nat_entry_set_slab
)
2136 goto destroy_free_nid
;
2140 kmem_cache_destroy(free_nid_slab
);
2142 kmem_cache_destroy(nat_entry_slab
);
2147 void destroy_node_manager_caches(void)
2149 kmem_cache_destroy(nat_entry_set_slab
);
2150 kmem_cache_destroy(free_nid_slab
);
2151 kmem_cache_destroy(nat_entry_slab
);