4 * Copyright (c) 2012 Samsung Electronics Co., Ltd.
5 * http://www.samsung.com/
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License version 2 as
9 * published by the Free Software Foundation.
12 #include <linux/f2fs_fs.h>
13 #include <linux/mpage.h>
14 #include <linux/backing-dev.h>
15 #include <linux/blkdev.h>
16 #include <linux/pagevec.h>
17 #include <linux/swap.h>
22 #include <trace/events/f2fs.h>
24 #define on_build_free_nids(nmi) mutex_is_locked(&nm_i->build_lock)
26 static struct kmem_cache
*nat_entry_slab
;
27 static struct kmem_cache
*free_nid_slab
;
28 static struct kmem_cache
*nat_entry_set_slab
;
30 bool available_free_memory(struct f2fs_sb_info
*sbi
, int type
)
32 struct f2fs_nm_info
*nm_i
= NM_I(sbi
);
34 unsigned long avail_ram
;
35 unsigned long mem_size
= 0;
40 /* only uses low memory */
41 avail_ram
= val
.totalram
- val
.totalhigh
;
43 /* give 25%, 25%, 50%, 50% memory for each components respectively */
44 if (type
== FREE_NIDS
) {
45 mem_size
= (nm_i
->fcnt
* sizeof(struct free_nid
)) >>
47 res
= mem_size
< ((avail_ram
* nm_i
->ram_thresh
/ 100) >> 2);
48 } else if (type
== NAT_ENTRIES
) {
49 mem_size
= (nm_i
->nat_cnt
* sizeof(struct nat_entry
)) >>
51 res
= mem_size
< ((avail_ram
* nm_i
->ram_thresh
/ 100) >> 2);
52 } else if (type
== DIRTY_DENTS
) {
53 if (sbi
->sb
->s_bdi
->dirty_exceeded
)
55 mem_size
= get_pages(sbi
, F2FS_DIRTY_DENTS
);
56 res
= mem_size
< ((avail_ram
* nm_i
->ram_thresh
/ 100) >> 1);
57 } else if (type
== INO_ENTRIES
) {
60 if (sbi
->sb
->s_bdi
->dirty_exceeded
)
62 for (i
= 0; i
<= UPDATE_INO
; i
++)
63 mem_size
+= (sbi
->im
[i
].ino_num
*
64 sizeof(struct ino_entry
)) >> PAGE_CACHE_SHIFT
;
65 res
= mem_size
< ((avail_ram
* nm_i
->ram_thresh
/ 100) >> 1);
70 static void clear_node_page_dirty(struct page
*page
)
72 struct address_space
*mapping
= page
->mapping
;
73 unsigned int long flags
;
75 if (PageDirty(page
)) {
76 spin_lock_irqsave(&mapping
->tree_lock
, flags
);
77 radix_tree_tag_clear(&mapping
->page_tree
,
80 spin_unlock_irqrestore(&mapping
->tree_lock
, flags
);
82 clear_page_dirty_for_io(page
);
83 dec_page_count(F2FS_M_SB(mapping
), F2FS_DIRTY_NODES
);
85 ClearPageUptodate(page
);
88 static struct page
*get_current_nat_page(struct f2fs_sb_info
*sbi
, nid_t nid
)
90 pgoff_t index
= current_nat_addr(sbi
, nid
);
91 return get_meta_page(sbi
, index
);
94 static struct page
*get_next_nat_page(struct f2fs_sb_info
*sbi
, nid_t nid
)
96 struct page
*src_page
;
97 struct page
*dst_page
;
102 struct f2fs_nm_info
*nm_i
= NM_I(sbi
);
104 src_off
= current_nat_addr(sbi
, nid
);
105 dst_off
= next_nat_addr(sbi
, src_off
);
107 /* get current nat block page with lock */
108 src_page
= get_meta_page(sbi
, src_off
);
109 dst_page
= grab_meta_page(sbi
, dst_off
);
110 f2fs_bug_on(sbi
, PageDirty(src_page
));
112 src_addr
= page_address(src_page
);
113 dst_addr
= page_address(dst_page
);
114 memcpy(dst_addr
, src_addr
, PAGE_CACHE_SIZE
);
115 set_page_dirty(dst_page
);
116 f2fs_put_page(src_page
, 1);
118 set_to_next_nat(nm_i
, nid
);
123 static struct nat_entry
*__lookup_nat_cache(struct f2fs_nm_info
*nm_i
, nid_t n
)
125 return radix_tree_lookup(&nm_i
->nat_root
, n
);
128 static unsigned int __gang_lookup_nat_cache(struct f2fs_nm_info
*nm_i
,
129 nid_t start
, unsigned int nr
, struct nat_entry
**ep
)
131 return radix_tree_gang_lookup(&nm_i
->nat_root
, (void **)ep
, start
, nr
);
134 static void __del_from_nat_cache(struct f2fs_nm_info
*nm_i
, struct nat_entry
*e
)
137 radix_tree_delete(&nm_i
->nat_root
, nat_get_nid(e
));
139 kmem_cache_free(nat_entry_slab
, e
);
142 static void __set_nat_cache_dirty(struct f2fs_nm_info
*nm_i
,
143 struct nat_entry
*ne
)
145 nid_t set
= NAT_BLOCK_OFFSET(ne
->ni
.nid
);
146 struct nat_entry_set
*head
;
148 if (get_nat_flag(ne
, IS_DIRTY
))
151 head
= radix_tree_lookup(&nm_i
->nat_set_root
, set
);
153 head
= f2fs_kmem_cache_alloc(nat_entry_set_slab
, GFP_ATOMIC
);
155 INIT_LIST_HEAD(&head
->entry_list
);
156 INIT_LIST_HEAD(&head
->set_list
);
160 if (radix_tree_insert(&nm_i
->nat_set_root
, set
, head
)) {
161 kmem_cache_free(nat_entry_set_slab
, head
);
165 list_move_tail(&ne
->list
, &head
->entry_list
);
166 nm_i
->dirty_nat_cnt
++;
168 set_nat_flag(ne
, IS_DIRTY
, true);
171 static void __clear_nat_cache_dirty(struct f2fs_nm_info
*nm_i
,
172 struct nat_entry
*ne
)
174 nid_t set
= NAT_BLOCK_OFFSET(ne
->ni
.nid
);
175 struct nat_entry_set
*head
;
177 head
= radix_tree_lookup(&nm_i
->nat_set_root
, set
);
179 list_move_tail(&ne
->list
, &nm_i
->nat_entries
);
180 set_nat_flag(ne
, IS_DIRTY
, false);
182 nm_i
->dirty_nat_cnt
--;
186 static unsigned int __gang_lookup_nat_set(struct f2fs_nm_info
*nm_i
,
187 nid_t start
, unsigned int nr
, struct nat_entry_set
**ep
)
189 return radix_tree_gang_lookup(&nm_i
->nat_set_root
, (void **)ep
,
193 bool is_checkpointed_node(struct f2fs_sb_info
*sbi
, nid_t nid
)
195 struct f2fs_nm_info
*nm_i
= NM_I(sbi
);
199 read_lock(&nm_i
->nat_tree_lock
);
200 e
= __lookup_nat_cache(nm_i
, nid
);
201 if (e
&& !get_nat_flag(e
, IS_CHECKPOINTED
))
203 read_unlock(&nm_i
->nat_tree_lock
);
207 bool has_fsynced_inode(struct f2fs_sb_info
*sbi
, nid_t ino
)
209 struct f2fs_nm_info
*nm_i
= NM_I(sbi
);
211 bool fsynced
= false;
213 read_lock(&nm_i
->nat_tree_lock
);
214 e
= __lookup_nat_cache(nm_i
, ino
);
215 if (e
&& get_nat_flag(e
, HAS_FSYNCED_INODE
))
217 read_unlock(&nm_i
->nat_tree_lock
);
221 bool need_inode_block_update(struct f2fs_sb_info
*sbi
, nid_t ino
)
223 struct f2fs_nm_info
*nm_i
= NM_I(sbi
);
225 bool need_update
= true;
227 read_lock(&nm_i
->nat_tree_lock
);
228 e
= __lookup_nat_cache(nm_i
, ino
);
229 if (e
&& get_nat_flag(e
, HAS_LAST_FSYNC
) &&
230 (get_nat_flag(e
, IS_CHECKPOINTED
) ||
231 get_nat_flag(e
, HAS_FSYNCED_INODE
)))
233 read_unlock(&nm_i
->nat_tree_lock
);
237 static struct nat_entry
*grab_nat_entry(struct f2fs_nm_info
*nm_i
, nid_t nid
)
239 struct nat_entry
*new;
241 new = kmem_cache_alloc(nat_entry_slab
, GFP_ATOMIC
);
244 if (radix_tree_insert(&nm_i
->nat_root
, nid
, new)) {
245 kmem_cache_free(nat_entry_slab
, new);
248 memset(new, 0, sizeof(struct nat_entry
));
249 nat_set_nid(new, nid
);
251 list_add_tail(&new->list
, &nm_i
->nat_entries
);
256 static void cache_nat_entry(struct f2fs_nm_info
*nm_i
, nid_t nid
,
257 struct f2fs_nat_entry
*ne
)
261 write_lock(&nm_i
->nat_tree_lock
);
262 e
= __lookup_nat_cache(nm_i
, nid
);
264 e
= grab_nat_entry(nm_i
, nid
);
266 write_unlock(&nm_i
->nat_tree_lock
);
269 node_info_from_raw_nat(&e
->ni
, ne
);
271 write_unlock(&nm_i
->nat_tree_lock
);
274 static void set_node_addr(struct f2fs_sb_info
*sbi
, struct node_info
*ni
,
275 block_t new_blkaddr
, bool fsync_done
)
277 struct f2fs_nm_info
*nm_i
= NM_I(sbi
);
280 write_lock(&nm_i
->nat_tree_lock
);
281 e
= __lookup_nat_cache(nm_i
, ni
->nid
);
283 e
= grab_nat_entry(nm_i
, ni
->nid
);
285 write_unlock(&nm_i
->nat_tree_lock
);
289 f2fs_bug_on(sbi
, ni
->blk_addr
== NEW_ADDR
);
290 } else if (new_blkaddr
== NEW_ADDR
) {
292 * when nid is reallocated,
293 * previous nat entry can be remained in nat cache.
294 * So, reinitialize it with new information.
297 f2fs_bug_on(sbi
, ni
->blk_addr
!= NULL_ADDR
);
301 f2fs_bug_on(sbi
, nat_get_blkaddr(e
) != ni
->blk_addr
);
302 f2fs_bug_on(sbi
, nat_get_blkaddr(e
) == NULL_ADDR
&&
303 new_blkaddr
== NULL_ADDR
);
304 f2fs_bug_on(sbi
, nat_get_blkaddr(e
) == NEW_ADDR
&&
305 new_blkaddr
== NEW_ADDR
);
306 f2fs_bug_on(sbi
, nat_get_blkaddr(e
) != NEW_ADDR
&&
307 nat_get_blkaddr(e
) != NULL_ADDR
&&
308 new_blkaddr
== NEW_ADDR
);
310 /* increment version no as node is removed */
311 if (nat_get_blkaddr(e
) != NEW_ADDR
&& new_blkaddr
== NULL_ADDR
) {
312 unsigned char version
= nat_get_version(e
);
313 nat_set_version(e
, inc_node_version(version
));
317 nat_set_blkaddr(e
, new_blkaddr
);
318 if (new_blkaddr
== NEW_ADDR
|| new_blkaddr
== NULL_ADDR
)
319 set_nat_flag(e
, IS_CHECKPOINTED
, false);
320 __set_nat_cache_dirty(nm_i
, e
);
322 /* update fsync_mark if its inode nat entry is still alive */
323 e
= __lookup_nat_cache(nm_i
, ni
->ino
);
325 if (fsync_done
&& ni
->nid
== ni
->ino
)
326 set_nat_flag(e
, HAS_FSYNCED_INODE
, true);
327 set_nat_flag(e
, HAS_LAST_FSYNC
, fsync_done
);
329 write_unlock(&nm_i
->nat_tree_lock
);
332 int try_to_free_nats(struct f2fs_sb_info
*sbi
, int nr_shrink
)
334 struct f2fs_nm_info
*nm_i
= NM_I(sbi
);
336 if (available_free_memory(sbi
, NAT_ENTRIES
))
339 write_lock(&nm_i
->nat_tree_lock
);
340 while (nr_shrink
&& !list_empty(&nm_i
->nat_entries
)) {
341 struct nat_entry
*ne
;
342 ne
= list_first_entry(&nm_i
->nat_entries
,
343 struct nat_entry
, list
);
344 __del_from_nat_cache(nm_i
, ne
);
347 write_unlock(&nm_i
->nat_tree_lock
);
352 * This function always returns success
354 void get_node_info(struct f2fs_sb_info
*sbi
, nid_t nid
, struct node_info
*ni
)
356 struct f2fs_nm_info
*nm_i
= NM_I(sbi
);
357 struct curseg_info
*curseg
= CURSEG_I(sbi
, CURSEG_HOT_DATA
);
358 struct f2fs_summary_block
*sum
= curseg
->sum_blk
;
359 nid_t start_nid
= START_NID(nid
);
360 struct f2fs_nat_block
*nat_blk
;
361 struct page
*page
= NULL
;
362 struct f2fs_nat_entry ne
;
366 memset(&ne
, 0, sizeof(struct f2fs_nat_entry
));
369 /* Check nat cache */
370 read_lock(&nm_i
->nat_tree_lock
);
371 e
= __lookup_nat_cache(nm_i
, nid
);
373 ni
->ino
= nat_get_ino(e
);
374 ni
->blk_addr
= nat_get_blkaddr(e
);
375 ni
->version
= nat_get_version(e
);
377 read_unlock(&nm_i
->nat_tree_lock
);
381 /* Check current segment summary */
382 mutex_lock(&curseg
->curseg_mutex
);
383 i
= lookup_journal_in_cursum(sum
, NAT_JOURNAL
, nid
, 0);
385 ne
= nat_in_journal(sum
, i
);
386 node_info_from_raw_nat(ni
, &ne
);
388 mutex_unlock(&curseg
->curseg_mutex
);
392 /* Fill node_info from nat page */
393 page
= get_current_nat_page(sbi
, start_nid
);
394 nat_blk
= (struct f2fs_nat_block
*)page_address(page
);
395 ne
= nat_blk
->entries
[nid
- start_nid
];
396 node_info_from_raw_nat(ni
, &ne
);
397 f2fs_put_page(page
, 1);
399 /* cache nat entry */
400 cache_nat_entry(NM_I(sbi
), nid
, &ne
);
404 * The maximum depth is four.
405 * Offset[0] will have raw inode offset.
407 static int get_node_path(struct f2fs_inode_info
*fi
, long block
,
408 int offset
[4], unsigned int noffset
[4])
410 const long direct_index
= ADDRS_PER_INODE(fi
);
411 const long direct_blks
= ADDRS_PER_BLOCK
;
412 const long dptrs_per_blk
= NIDS_PER_BLOCK
;
413 const long indirect_blks
= ADDRS_PER_BLOCK
* NIDS_PER_BLOCK
;
414 const long dindirect_blks
= indirect_blks
* NIDS_PER_BLOCK
;
420 if (block
< direct_index
) {
424 block
-= direct_index
;
425 if (block
< direct_blks
) {
426 offset
[n
++] = NODE_DIR1_BLOCK
;
432 block
-= direct_blks
;
433 if (block
< direct_blks
) {
434 offset
[n
++] = NODE_DIR2_BLOCK
;
440 block
-= direct_blks
;
441 if (block
< indirect_blks
) {
442 offset
[n
++] = NODE_IND1_BLOCK
;
444 offset
[n
++] = block
/ direct_blks
;
445 noffset
[n
] = 4 + offset
[n
- 1];
446 offset
[n
] = block
% direct_blks
;
450 block
-= indirect_blks
;
451 if (block
< indirect_blks
) {
452 offset
[n
++] = NODE_IND2_BLOCK
;
453 noffset
[n
] = 4 + dptrs_per_blk
;
454 offset
[n
++] = block
/ direct_blks
;
455 noffset
[n
] = 5 + dptrs_per_blk
+ offset
[n
- 1];
456 offset
[n
] = block
% direct_blks
;
460 block
-= indirect_blks
;
461 if (block
< dindirect_blks
) {
462 offset
[n
++] = NODE_DIND_BLOCK
;
463 noffset
[n
] = 5 + (dptrs_per_blk
* 2);
464 offset
[n
++] = block
/ indirect_blks
;
465 noffset
[n
] = 6 + (dptrs_per_blk
* 2) +
466 offset
[n
- 1] * (dptrs_per_blk
+ 1);
467 offset
[n
++] = (block
/ direct_blks
) % dptrs_per_blk
;
468 noffset
[n
] = 7 + (dptrs_per_blk
* 2) +
469 offset
[n
- 2] * (dptrs_per_blk
+ 1) +
471 offset
[n
] = block
% direct_blks
;
482 * Caller should call f2fs_put_dnode(dn).
483 * Also, it should grab and release a rwsem by calling f2fs_lock_op() and
484 * f2fs_unlock_op() only if ro is not set RDONLY_NODE.
485 * In the case of RDONLY_NODE, we don't need to care about mutex.
487 int get_dnode_of_data(struct dnode_of_data
*dn
, pgoff_t index
, int mode
)
489 struct f2fs_sb_info
*sbi
= F2FS_I_SB(dn
->inode
);
490 struct page
*npage
[4];
493 unsigned int noffset
[4];
498 level
= get_node_path(F2FS_I(dn
->inode
), index
, offset
, noffset
);
500 nids
[0] = dn
->inode
->i_ino
;
501 npage
[0] = dn
->inode_page
;
504 npage
[0] = get_node_page(sbi
, nids
[0]);
505 if (IS_ERR(npage
[0]))
506 return PTR_ERR(npage
[0]);
510 nids
[1] = get_nid(parent
, offset
[0], true);
511 dn
->inode_page
= npage
[0];
512 dn
->inode_page_locked
= true;
514 /* get indirect or direct nodes */
515 for (i
= 1; i
<= level
; i
++) {
518 if (!nids
[i
] && mode
== ALLOC_NODE
) {
520 if (!alloc_nid(sbi
, &(nids
[i
]))) {
526 npage
[i
] = new_node_page(dn
, noffset
[i
], NULL
);
527 if (IS_ERR(npage
[i
])) {
528 alloc_nid_failed(sbi
, nids
[i
]);
529 err
= PTR_ERR(npage
[i
]);
533 set_nid(parent
, offset
[i
- 1], nids
[i
], i
== 1);
534 alloc_nid_done(sbi
, nids
[i
]);
536 } else if (mode
== LOOKUP_NODE_RA
&& i
== level
&& level
> 1) {
537 npage
[i
] = get_node_page_ra(parent
, offset
[i
- 1]);
538 if (IS_ERR(npage
[i
])) {
539 err
= PTR_ERR(npage
[i
]);
545 dn
->inode_page_locked
= false;
548 f2fs_put_page(parent
, 1);
552 npage
[i
] = get_node_page(sbi
, nids
[i
]);
553 if (IS_ERR(npage
[i
])) {
554 err
= PTR_ERR(npage
[i
]);
555 f2fs_put_page(npage
[0], 0);
561 nids
[i
+ 1] = get_nid(parent
, offset
[i
], false);
564 dn
->nid
= nids
[level
];
565 dn
->ofs_in_node
= offset
[level
];
566 dn
->node_page
= npage
[level
];
567 dn
->data_blkaddr
= datablock_addr(dn
->node_page
, dn
->ofs_in_node
);
571 f2fs_put_page(parent
, 1);
573 f2fs_put_page(npage
[0], 0);
575 dn
->inode_page
= NULL
;
576 dn
->node_page
= NULL
;
580 static void truncate_node(struct dnode_of_data
*dn
)
582 struct f2fs_sb_info
*sbi
= F2FS_I_SB(dn
->inode
);
585 get_node_info(sbi
, dn
->nid
, &ni
);
586 if (dn
->inode
->i_blocks
== 0) {
587 f2fs_bug_on(sbi
, ni
.blk_addr
!= NULL_ADDR
);
590 f2fs_bug_on(sbi
, ni
.blk_addr
== NULL_ADDR
);
592 /* Deallocate node address */
593 invalidate_blocks(sbi
, ni
.blk_addr
);
594 dec_valid_node_count(sbi
, dn
->inode
);
595 set_node_addr(sbi
, &ni
, NULL_ADDR
, false);
597 if (dn
->nid
== dn
->inode
->i_ino
) {
598 remove_orphan_inode(sbi
, dn
->nid
);
599 dec_valid_inode_count(sbi
);
604 clear_node_page_dirty(dn
->node_page
);
605 F2FS_SET_SB_DIRT(sbi
);
607 f2fs_put_page(dn
->node_page
, 1);
609 invalidate_mapping_pages(NODE_MAPPING(sbi
),
610 dn
->node_page
->index
, dn
->node_page
->index
);
612 dn
->node_page
= NULL
;
613 trace_f2fs_truncate_node(dn
->inode
, dn
->nid
, ni
.blk_addr
);
616 static int truncate_dnode(struct dnode_of_data
*dn
)
623 /* get direct node */
624 page
= get_node_page(F2FS_I_SB(dn
->inode
), dn
->nid
);
625 if (IS_ERR(page
) && PTR_ERR(page
) == -ENOENT
)
627 else if (IS_ERR(page
))
628 return PTR_ERR(page
);
630 /* Make dnode_of_data for parameter */
631 dn
->node_page
= page
;
633 truncate_data_blocks(dn
);
638 static int truncate_nodes(struct dnode_of_data
*dn
, unsigned int nofs
,
641 struct dnode_of_data rdn
= *dn
;
643 struct f2fs_node
*rn
;
645 unsigned int child_nofs
;
650 return NIDS_PER_BLOCK
+ 1;
652 trace_f2fs_truncate_nodes_enter(dn
->inode
, dn
->nid
, dn
->data_blkaddr
);
654 page
= get_node_page(F2FS_I_SB(dn
->inode
), dn
->nid
);
656 trace_f2fs_truncate_nodes_exit(dn
->inode
, PTR_ERR(page
));
657 return PTR_ERR(page
);
660 rn
= F2FS_NODE(page
);
662 for (i
= ofs
; i
< NIDS_PER_BLOCK
; i
++, freed
++) {
663 child_nid
= le32_to_cpu(rn
->in
.nid
[i
]);
667 ret
= truncate_dnode(&rdn
);
670 set_nid(page
, i
, 0, false);
673 child_nofs
= nofs
+ ofs
* (NIDS_PER_BLOCK
+ 1) + 1;
674 for (i
= ofs
; i
< NIDS_PER_BLOCK
; i
++) {
675 child_nid
= le32_to_cpu(rn
->in
.nid
[i
]);
676 if (child_nid
== 0) {
677 child_nofs
+= NIDS_PER_BLOCK
+ 1;
681 ret
= truncate_nodes(&rdn
, child_nofs
, 0, depth
- 1);
682 if (ret
== (NIDS_PER_BLOCK
+ 1)) {
683 set_nid(page
, i
, 0, false);
685 } else if (ret
< 0 && ret
!= -ENOENT
) {
693 /* remove current indirect node */
694 dn
->node_page
= page
;
698 f2fs_put_page(page
, 1);
700 trace_f2fs_truncate_nodes_exit(dn
->inode
, freed
);
704 f2fs_put_page(page
, 1);
705 trace_f2fs_truncate_nodes_exit(dn
->inode
, ret
);
709 static int truncate_partial_nodes(struct dnode_of_data
*dn
,
710 struct f2fs_inode
*ri
, int *offset
, int depth
)
712 struct page
*pages
[2];
719 nid
[0] = le32_to_cpu(ri
->i_nid
[offset
[0] - NODE_DIR1_BLOCK
]);
723 /* get indirect nodes in the path */
724 for (i
= 0; i
< idx
+ 1; i
++) {
725 /* reference count'll be increased */
726 pages
[i
] = get_node_page(F2FS_I_SB(dn
->inode
), nid
[i
]);
727 if (IS_ERR(pages
[i
])) {
728 err
= PTR_ERR(pages
[i
]);
732 nid
[i
+ 1] = get_nid(pages
[i
], offset
[i
+ 1], false);
735 /* free direct nodes linked to a partial indirect node */
736 for (i
= offset
[idx
+ 1]; i
< NIDS_PER_BLOCK
; i
++) {
737 child_nid
= get_nid(pages
[idx
], i
, false);
741 err
= truncate_dnode(dn
);
744 set_nid(pages
[idx
], i
, 0, false);
747 if (offset
[idx
+ 1] == 0) {
748 dn
->node_page
= pages
[idx
];
752 f2fs_put_page(pages
[idx
], 1);
758 for (i
= idx
; i
>= 0; i
--)
759 f2fs_put_page(pages
[i
], 1);
761 trace_f2fs_truncate_partial_nodes(dn
->inode
, nid
, depth
, err
);
767 * All the block addresses of data and nodes should be nullified.
769 int truncate_inode_blocks(struct inode
*inode
, pgoff_t from
)
771 struct f2fs_sb_info
*sbi
= F2FS_I_SB(inode
);
772 int err
= 0, cont
= 1;
773 int level
, offset
[4], noffset
[4];
774 unsigned int nofs
= 0;
775 struct f2fs_inode
*ri
;
776 struct dnode_of_data dn
;
779 trace_f2fs_truncate_inode_blocks_enter(inode
, from
);
781 level
= get_node_path(F2FS_I(inode
), from
, offset
, noffset
);
783 page
= get_node_page(sbi
, inode
->i_ino
);
785 trace_f2fs_truncate_inode_blocks_exit(inode
, PTR_ERR(page
));
786 return PTR_ERR(page
);
789 set_new_dnode(&dn
, inode
, page
, NULL
, 0);
792 ri
= F2FS_INODE(page
);
800 if (!offset
[level
- 1])
802 err
= truncate_partial_nodes(&dn
, ri
, offset
, level
);
803 if (err
< 0 && err
!= -ENOENT
)
805 nofs
+= 1 + NIDS_PER_BLOCK
;
808 nofs
= 5 + 2 * NIDS_PER_BLOCK
;
809 if (!offset
[level
- 1])
811 err
= truncate_partial_nodes(&dn
, ri
, offset
, level
);
812 if (err
< 0 && err
!= -ENOENT
)
821 dn
.nid
= le32_to_cpu(ri
->i_nid
[offset
[0] - NODE_DIR1_BLOCK
]);
823 case NODE_DIR1_BLOCK
:
824 case NODE_DIR2_BLOCK
:
825 err
= truncate_dnode(&dn
);
828 case NODE_IND1_BLOCK
:
829 case NODE_IND2_BLOCK
:
830 err
= truncate_nodes(&dn
, nofs
, offset
[1], 2);
833 case NODE_DIND_BLOCK
:
834 err
= truncate_nodes(&dn
, nofs
, offset
[1], 3);
841 if (err
< 0 && err
!= -ENOENT
)
843 if (offset
[1] == 0 &&
844 ri
->i_nid
[offset
[0] - NODE_DIR1_BLOCK
]) {
846 if (unlikely(page
->mapping
!= NODE_MAPPING(sbi
))) {
847 f2fs_put_page(page
, 1);
850 f2fs_wait_on_page_writeback(page
, NODE
);
851 ri
->i_nid
[offset
[0] - NODE_DIR1_BLOCK
] = 0;
852 set_page_dirty(page
);
860 f2fs_put_page(page
, 0);
861 trace_f2fs_truncate_inode_blocks_exit(inode
, err
);
862 return err
> 0 ? 0 : err
;
865 int truncate_xattr_node(struct inode
*inode
, struct page
*page
)
867 struct f2fs_sb_info
*sbi
= F2FS_I_SB(inode
);
868 nid_t nid
= F2FS_I(inode
)->i_xattr_nid
;
869 struct dnode_of_data dn
;
875 npage
= get_node_page(sbi
, nid
);
877 return PTR_ERR(npage
);
879 F2FS_I(inode
)->i_xattr_nid
= 0;
881 /* need to do checkpoint during fsync */
882 F2FS_I(inode
)->xattr_ver
= cur_cp_version(F2FS_CKPT(sbi
));
884 set_new_dnode(&dn
, inode
, page
, npage
, nid
);
887 dn
.inode_page_locked
= true;
893 * Caller should grab and release a rwsem by calling f2fs_lock_op() and
896 void remove_inode_page(struct inode
*inode
)
898 struct dnode_of_data dn
;
900 set_new_dnode(&dn
, inode
, NULL
, NULL
, inode
->i_ino
);
901 if (get_dnode_of_data(&dn
, 0, LOOKUP_NODE
))
904 if (truncate_xattr_node(inode
, dn
.inode_page
)) {
909 /* remove potential inline_data blocks */
910 if (S_ISREG(inode
->i_mode
) || S_ISDIR(inode
->i_mode
) ||
911 S_ISLNK(inode
->i_mode
))
912 truncate_data_blocks_range(&dn
, 1);
914 /* 0 is possible, after f2fs_new_inode() has failed */
915 f2fs_bug_on(F2FS_I_SB(inode
),
916 inode
->i_blocks
!= 0 && inode
->i_blocks
!= 1);
918 /* will put inode & node pages */
922 struct page
*new_inode_page(struct inode
*inode
)
924 struct dnode_of_data dn
;
926 /* allocate inode page for new inode */
927 set_new_dnode(&dn
, inode
, NULL
, NULL
, inode
->i_ino
);
929 /* caller should f2fs_put_page(page, 1); */
930 return new_node_page(&dn
, 0, NULL
);
933 struct page
*new_node_page(struct dnode_of_data
*dn
,
934 unsigned int ofs
, struct page
*ipage
)
936 struct f2fs_sb_info
*sbi
= F2FS_I_SB(dn
->inode
);
937 struct node_info old_ni
, new_ni
;
941 if (unlikely(is_inode_flag_set(F2FS_I(dn
->inode
), FI_NO_ALLOC
)))
942 return ERR_PTR(-EPERM
);
944 page
= grab_cache_page(NODE_MAPPING(sbi
), dn
->nid
);
946 return ERR_PTR(-ENOMEM
);
948 if (unlikely(!inc_valid_node_count(sbi
, dn
->inode
))) {
953 get_node_info(sbi
, dn
->nid
, &old_ni
);
955 /* Reinitialize old_ni with new node page */
956 f2fs_bug_on(sbi
, old_ni
.blk_addr
!= NULL_ADDR
);
958 new_ni
.ino
= dn
->inode
->i_ino
;
959 set_node_addr(sbi
, &new_ni
, NEW_ADDR
, false);
961 f2fs_wait_on_page_writeback(page
, NODE
);
962 fill_node_footer(page
, dn
->nid
, dn
->inode
->i_ino
, ofs
, true);
963 set_cold_node(dn
->inode
, page
);
964 SetPageUptodate(page
);
965 set_page_dirty(page
);
967 if (f2fs_has_xattr_block(ofs
))
968 F2FS_I(dn
->inode
)->i_xattr_nid
= dn
->nid
;
970 dn
->node_page
= page
;
972 update_inode(dn
->inode
, ipage
);
976 inc_valid_inode_count(sbi
);
981 clear_node_page_dirty(page
);
982 f2fs_put_page(page
, 1);
987 * Caller should do after getting the following values.
988 * 0: f2fs_put_page(page, 0)
989 * LOCKED_PAGE: f2fs_put_page(page, 1)
992 static int read_node_page(struct page
*page
, int rw
)
994 struct f2fs_sb_info
*sbi
= F2FS_P_SB(page
);
997 get_node_info(sbi
, page
->index
, &ni
);
999 if (unlikely(ni
.blk_addr
== NULL_ADDR
)) {
1000 f2fs_put_page(page
, 1);
1004 if (PageUptodate(page
))
1007 return f2fs_submit_page_bio(sbi
, page
, ni
.blk_addr
, rw
);
1011 * Readahead a node page
1013 void ra_node_page(struct f2fs_sb_info
*sbi
, nid_t nid
)
1018 apage
= find_get_page(NODE_MAPPING(sbi
), nid
);
1019 if (apage
&& PageUptodate(apage
)) {
1020 f2fs_put_page(apage
, 0);
1023 f2fs_put_page(apage
, 0);
1025 apage
= grab_cache_page(NODE_MAPPING(sbi
), nid
);
1029 err
= read_node_page(apage
, READA
);
1031 f2fs_put_page(apage
, 0);
1032 else if (err
== LOCKED_PAGE
)
1033 f2fs_put_page(apage
, 1);
1036 struct page
*get_node_page(struct f2fs_sb_info
*sbi
, pgoff_t nid
)
1041 page
= grab_cache_page(NODE_MAPPING(sbi
), nid
);
1043 return ERR_PTR(-ENOMEM
);
1045 err
= read_node_page(page
, READ_SYNC
);
1047 return ERR_PTR(err
);
1048 else if (err
== LOCKED_PAGE
)
1052 if (unlikely(!PageUptodate(page
) || nid
!= nid_of_node(page
))) {
1053 f2fs_put_page(page
, 1);
1054 return ERR_PTR(-EIO
);
1056 if (unlikely(page
->mapping
!= NODE_MAPPING(sbi
))) {
1057 f2fs_put_page(page
, 1);
1065 * Return a locked page for the desired node page.
1066 * And, readahead MAX_RA_NODE number of node pages.
1068 struct page
*get_node_page_ra(struct page
*parent
, int start
)
1070 struct f2fs_sb_info
*sbi
= F2FS_P_SB(parent
);
1071 struct blk_plug plug
;
1076 /* First, try getting the desired direct node. */
1077 nid
= get_nid(parent
, start
, false);
1079 return ERR_PTR(-ENOENT
);
1081 page
= grab_cache_page(NODE_MAPPING(sbi
), nid
);
1083 return ERR_PTR(-ENOMEM
);
1085 err
= read_node_page(page
, READ_SYNC
);
1087 return ERR_PTR(err
);
1088 else if (err
== LOCKED_PAGE
)
1091 blk_start_plug(&plug
);
1093 /* Then, try readahead for siblings of the desired node */
1094 end
= start
+ MAX_RA_NODE
;
1095 end
= min(end
, NIDS_PER_BLOCK
);
1096 for (i
= start
+ 1; i
< end
; i
++) {
1097 nid
= get_nid(parent
, i
, false);
1100 ra_node_page(sbi
, nid
);
1103 blk_finish_plug(&plug
);
1106 if (unlikely(page
->mapping
!= NODE_MAPPING(sbi
))) {
1107 f2fs_put_page(page
, 1);
1111 if (unlikely(!PageUptodate(page
))) {
1112 f2fs_put_page(page
, 1);
1113 return ERR_PTR(-EIO
);
1118 void sync_inode_page(struct dnode_of_data
*dn
)
1120 if (IS_INODE(dn
->node_page
) || dn
->inode_page
== dn
->node_page
) {
1121 update_inode(dn
->inode
, dn
->node_page
);
1122 } else if (dn
->inode_page
) {
1123 if (!dn
->inode_page_locked
)
1124 lock_page(dn
->inode_page
);
1125 update_inode(dn
->inode
, dn
->inode_page
);
1126 if (!dn
->inode_page_locked
)
1127 unlock_page(dn
->inode_page
);
1129 update_inode_page(dn
->inode
);
1133 int sync_node_pages(struct f2fs_sb_info
*sbi
, nid_t ino
,
1134 struct writeback_control
*wbc
)
1137 struct pagevec pvec
;
1138 int step
= ino
? 2 : 0;
1139 int nwritten
= 0, wrote
= 0;
1141 pagevec_init(&pvec
, 0);
1147 while (index
<= end
) {
1149 nr_pages
= pagevec_lookup_tag(&pvec
, NODE_MAPPING(sbi
), &index
,
1150 PAGECACHE_TAG_DIRTY
,
1151 min(end
- index
, (pgoff_t
)PAGEVEC_SIZE
-1) + 1);
1155 for (i
= 0; i
< nr_pages
; i
++) {
1156 struct page
*page
= pvec
.pages
[i
];
1159 * flushing sequence with step:
1164 if (step
== 0 && IS_DNODE(page
))
1166 if (step
== 1 && (!IS_DNODE(page
) ||
1167 is_cold_node(page
)))
1169 if (step
== 2 && (!IS_DNODE(page
) ||
1170 !is_cold_node(page
)))
1175 * we should not skip writing node pages.
1177 if (ino
&& ino_of_node(page
) == ino
)
1179 else if (!trylock_page(page
))
1182 if (unlikely(page
->mapping
!= NODE_MAPPING(sbi
))) {
1187 if (ino
&& ino_of_node(page
) != ino
)
1188 goto continue_unlock
;
1190 if (!PageDirty(page
)) {
1191 /* someone wrote it for us */
1192 goto continue_unlock
;
1195 if (!clear_page_dirty_for_io(page
))
1196 goto continue_unlock
;
1198 /* called by fsync() */
1199 if (ino
&& IS_DNODE(page
)) {
1200 set_fsync_mark(page
, 1);
1201 if (IS_INODE(page
)) {
1202 if (!is_checkpointed_node(sbi
, ino
) &&
1203 !has_fsynced_inode(sbi
, ino
))
1204 set_dentry_mark(page
, 1);
1206 set_dentry_mark(page
, 0);
1210 set_fsync_mark(page
, 0);
1211 set_dentry_mark(page
, 0);
1214 if (NODE_MAPPING(sbi
)->a_ops
->writepage(page
, wbc
))
1219 if (--wbc
->nr_to_write
== 0)
1222 pagevec_release(&pvec
);
1225 if (wbc
->nr_to_write
== 0) {
1237 f2fs_submit_merged_bio(sbi
, NODE
, WRITE
);
1241 int wait_on_node_pages_writeback(struct f2fs_sb_info
*sbi
, nid_t ino
)
1243 pgoff_t index
= 0, end
= LONG_MAX
;
1244 struct pagevec pvec
;
1245 int ret2
= 0, ret
= 0;
1247 pagevec_init(&pvec
, 0);
1249 while (index
<= end
) {
1251 nr_pages
= pagevec_lookup_tag(&pvec
, NODE_MAPPING(sbi
), &index
,
1252 PAGECACHE_TAG_WRITEBACK
,
1253 min(end
- index
, (pgoff_t
)PAGEVEC_SIZE
-1) + 1);
1257 for (i
= 0; i
< nr_pages
; i
++) {
1258 struct page
*page
= pvec
.pages
[i
];
1260 /* until radix tree lookup accepts end_index */
1261 if (unlikely(page
->index
> end
))
1264 if (ino
&& ino_of_node(page
) == ino
) {
1265 f2fs_wait_on_page_writeback(page
, NODE
);
1266 if (TestClearPageError(page
))
1270 pagevec_release(&pvec
);
1274 if (unlikely(test_and_clear_bit(AS_ENOSPC
, &NODE_MAPPING(sbi
)->flags
)))
1276 if (unlikely(test_and_clear_bit(AS_EIO
, &NODE_MAPPING(sbi
)->flags
)))
1283 static int f2fs_write_node_page(struct page
*page
,
1284 struct writeback_control
*wbc
)
1286 struct f2fs_sb_info
*sbi
= F2FS_P_SB(page
);
1289 struct node_info ni
;
1290 struct f2fs_io_info fio
= {
1292 .rw
= (wbc
->sync_mode
== WB_SYNC_ALL
) ? WRITE_SYNC
: WRITE
,
1295 trace_f2fs_writepage(page
, NODE
);
1297 if (unlikely(sbi
->por_doing
))
1299 if (unlikely(f2fs_cp_error(sbi
)))
1302 f2fs_wait_on_page_writeback(page
, NODE
);
1304 /* get old block addr of this node page */
1305 nid
= nid_of_node(page
);
1306 f2fs_bug_on(sbi
, page
->index
!= nid
);
1308 get_node_info(sbi
, nid
, &ni
);
1310 /* This page is already truncated */
1311 if (unlikely(ni
.blk_addr
== NULL_ADDR
)) {
1312 dec_page_count(sbi
, F2FS_DIRTY_NODES
);
1317 if (wbc
->for_reclaim
) {
1318 if (!down_read_trylock(&sbi
->node_write
))
1321 down_read(&sbi
->node_write
);
1323 set_page_writeback(page
);
1324 write_node_page(sbi
, page
, &fio
, nid
, ni
.blk_addr
, &new_addr
);
1325 set_node_addr(sbi
, &ni
, new_addr
, is_fsync_dnode(page
));
1326 dec_page_count(sbi
, F2FS_DIRTY_NODES
);
1327 up_read(&sbi
->node_write
);
1330 if (wbc
->for_reclaim
)
1331 f2fs_submit_merged_bio(sbi
, NODE
, WRITE
);
1336 redirty_page_for_writepage(wbc
, page
);
1337 return AOP_WRITEPAGE_ACTIVATE
;
1340 static int f2fs_write_node_pages(struct address_space
*mapping
,
1341 struct writeback_control
*wbc
)
1343 struct f2fs_sb_info
*sbi
= F2FS_M_SB(mapping
);
1346 trace_f2fs_writepages(mapping
->host
, wbc
, NODE
);
1348 /* balancing f2fs's metadata in background */
1349 f2fs_balance_fs_bg(sbi
);
1351 /* collect a number of dirty node pages and write together */
1352 if (get_pages(sbi
, F2FS_DIRTY_NODES
) < nr_pages_to_skip(sbi
, NODE
))
1355 diff
= nr_pages_to_write(sbi
, NODE
, wbc
);
1356 wbc
->sync_mode
= WB_SYNC_NONE
;
1357 sync_node_pages(sbi
, 0, wbc
);
1358 wbc
->nr_to_write
= max((long)0, wbc
->nr_to_write
- diff
);
1362 wbc
->pages_skipped
+= get_pages(sbi
, F2FS_DIRTY_NODES
);
1366 static int f2fs_set_node_page_dirty(struct page
*page
)
1368 trace_f2fs_set_page_dirty(page
, NODE
);
1370 SetPageUptodate(page
);
1371 if (!PageDirty(page
)) {
1372 __set_page_dirty_nobuffers(page
);
1373 inc_page_count(F2FS_P_SB(page
), F2FS_DIRTY_NODES
);
1374 SetPagePrivate(page
);
1380 static void f2fs_invalidate_node_page(struct page
*page
, unsigned int offset
,
1381 unsigned int length
)
1383 struct inode
*inode
= page
->mapping
->host
;
1384 if (PageDirty(page
))
1385 dec_page_count(F2FS_I_SB(inode
), F2FS_DIRTY_NODES
);
1386 ClearPagePrivate(page
);
1389 static int f2fs_release_node_page(struct page
*page
, gfp_t wait
)
1391 ClearPagePrivate(page
);
1396 * Structure of the f2fs node operations
1398 const struct address_space_operations f2fs_node_aops
= {
1399 .writepage
= f2fs_write_node_page
,
1400 .writepages
= f2fs_write_node_pages
,
1401 .set_page_dirty
= f2fs_set_node_page_dirty
,
1402 .invalidatepage
= f2fs_invalidate_node_page
,
1403 .releasepage
= f2fs_release_node_page
,
1406 static struct free_nid
*__lookup_free_nid_list(struct f2fs_nm_info
*nm_i
,
1409 return radix_tree_lookup(&nm_i
->free_nid_root
, n
);
1412 static void __del_from_free_nid_list(struct f2fs_nm_info
*nm_i
,
1416 radix_tree_delete(&nm_i
->free_nid_root
, i
->nid
);
1419 static int add_free_nid(struct f2fs_sb_info
*sbi
, nid_t nid
, bool build
)
1421 struct f2fs_nm_info
*nm_i
= NM_I(sbi
);
1423 struct nat_entry
*ne
;
1424 bool allocated
= false;
1426 if (!available_free_memory(sbi
, FREE_NIDS
))
1429 /* 0 nid should not be used */
1430 if (unlikely(nid
== 0))
1434 /* do not add allocated nids */
1435 read_lock(&nm_i
->nat_tree_lock
);
1436 ne
= __lookup_nat_cache(nm_i
, nid
);
1438 (!get_nat_flag(ne
, IS_CHECKPOINTED
) ||
1439 nat_get_blkaddr(ne
) != NULL_ADDR
))
1441 read_unlock(&nm_i
->nat_tree_lock
);
1446 i
= f2fs_kmem_cache_alloc(free_nid_slab
, GFP_NOFS
);
1450 spin_lock(&nm_i
->free_nid_list_lock
);
1451 if (radix_tree_insert(&nm_i
->free_nid_root
, i
->nid
, i
)) {
1452 spin_unlock(&nm_i
->free_nid_list_lock
);
1453 kmem_cache_free(free_nid_slab
, i
);
1456 list_add_tail(&i
->list
, &nm_i
->free_nid_list
);
1458 spin_unlock(&nm_i
->free_nid_list_lock
);
1462 static void remove_free_nid(struct f2fs_nm_info
*nm_i
, nid_t nid
)
1465 bool need_free
= false;
1467 spin_lock(&nm_i
->free_nid_list_lock
);
1468 i
= __lookup_free_nid_list(nm_i
, nid
);
1469 if (i
&& i
->state
== NID_NEW
) {
1470 __del_from_free_nid_list(nm_i
, i
);
1474 spin_unlock(&nm_i
->free_nid_list_lock
);
1477 kmem_cache_free(free_nid_slab
, i
);
1480 static void scan_nat_page(struct f2fs_sb_info
*sbi
,
1481 struct page
*nat_page
, nid_t start_nid
)
1483 struct f2fs_nm_info
*nm_i
= NM_I(sbi
);
1484 struct f2fs_nat_block
*nat_blk
= page_address(nat_page
);
1488 i
= start_nid
% NAT_ENTRY_PER_BLOCK
;
1490 for (; i
< NAT_ENTRY_PER_BLOCK
; i
++, start_nid
++) {
1492 if (unlikely(start_nid
>= nm_i
->max_nid
))
1495 blk_addr
= le32_to_cpu(nat_blk
->entries
[i
].block_addr
);
1496 f2fs_bug_on(sbi
, blk_addr
== NEW_ADDR
);
1497 if (blk_addr
== NULL_ADDR
) {
1498 if (add_free_nid(sbi
, start_nid
, true) < 0)
1504 static void build_free_nids(struct f2fs_sb_info
*sbi
)
1506 struct f2fs_nm_info
*nm_i
= NM_I(sbi
);
1507 struct curseg_info
*curseg
= CURSEG_I(sbi
, CURSEG_HOT_DATA
);
1508 struct f2fs_summary_block
*sum
= curseg
->sum_blk
;
1510 nid_t nid
= nm_i
->next_scan_nid
;
1512 /* Enough entries */
1513 if (nm_i
->fcnt
> NAT_ENTRY_PER_BLOCK
)
1516 /* readahead nat pages to be scanned */
1517 ra_meta_pages(sbi
, NAT_BLOCK_OFFSET(nid
), FREE_NID_PAGES
, META_NAT
);
1520 struct page
*page
= get_current_nat_page(sbi
, nid
);
1522 scan_nat_page(sbi
, page
, nid
);
1523 f2fs_put_page(page
, 1);
1525 nid
+= (NAT_ENTRY_PER_BLOCK
- (nid
% NAT_ENTRY_PER_BLOCK
));
1526 if (unlikely(nid
>= nm_i
->max_nid
))
1529 if (i
++ == FREE_NID_PAGES
)
1533 /* go to the next free nat pages to find free nids abundantly */
1534 nm_i
->next_scan_nid
= nid
;
1536 /* find free nids from current sum_pages */
1537 mutex_lock(&curseg
->curseg_mutex
);
1538 for (i
= 0; i
< nats_in_cursum(sum
); i
++) {
1539 block_t addr
= le32_to_cpu(nat_in_journal(sum
, i
).block_addr
);
1540 nid
= le32_to_cpu(nid_in_journal(sum
, i
));
1541 if (addr
== NULL_ADDR
)
1542 add_free_nid(sbi
, nid
, true);
1544 remove_free_nid(nm_i
, nid
);
1546 mutex_unlock(&curseg
->curseg_mutex
);
1550 * If this function returns success, caller can obtain a new nid
1551 * from second parameter of this function.
1552 * The returned nid could be used ino as well as nid when inode is created.
1554 bool alloc_nid(struct f2fs_sb_info
*sbi
, nid_t
*nid
)
1556 struct f2fs_nm_info
*nm_i
= NM_I(sbi
);
1557 struct free_nid
*i
= NULL
;
1559 if (unlikely(sbi
->total_valid_node_count
+ 1 > nm_i
->available_nids
))
1562 spin_lock(&nm_i
->free_nid_list_lock
);
1564 /* We should not use stale free nids created by build_free_nids */
1565 if (nm_i
->fcnt
&& !on_build_free_nids(nm_i
)) {
1566 f2fs_bug_on(sbi
, list_empty(&nm_i
->free_nid_list
));
1567 list_for_each_entry(i
, &nm_i
->free_nid_list
, list
)
1568 if (i
->state
== NID_NEW
)
1571 f2fs_bug_on(sbi
, i
->state
!= NID_NEW
);
1573 i
->state
= NID_ALLOC
;
1575 spin_unlock(&nm_i
->free_nid_list_lock
);
1578 spin_unlock(&nm_i
->free_nid_list_lock
);
1580 /* Let's scan nat pages and its caches to get free nids */
1581 mutex_lock(&nm_i
->build_lock
);
1582 build_free_nids(sbi
);
1583 mutex_unlock(&nm_i
->build_lock
);
1588 * alloc_nid() should be called prior to this function.
1590 void alloc_nid_done(struct f2fs_sb_info
*sbi
, nid_t nid
)
1592 struct f2fs_nm_info
*nm_i
= NM_I(sbi
);
1595 spin_lock(&nm_i
->free_nid_list_lock
);
1596 i
= __lookup_free_nid_list(nm_i
, nid
);
1597 f2fs_bug_on(sbi
, !i
|| i
->state
!= NID_ALLOC
);
1598 __del_from_free_nid_list(nm_i
, i
);
1599 spin_unlock(&nm_i
->free_nid_list_lock
);
1601 kmem_cache_free(free_nid_slab
, i
);
1605 * alloc_nid() should be called prior to this function.
1607 void alloc_nid_failed(struct f2fs_sb_info
*sbi
, nid_t nid
)
1609 struct f2fs_nm_info
*nm_i
= NM_I(sbi
);
1611 bool need_free
= false;
1616 spin_lock(&nm_i
->free_nid_list_lock
);
1617 i
= __lookup_free_nid_list(nm_i
, nid
);
1618 f2fs_bug_on(sbi
, !i
|| i
->state
!= NID_ALLOC
);
1619 if (!available_free_memory(sbi
, FREE_NIDS
)) {
1620 __del_from_free_nid_list(nm_i
, i
);
1626 spin_unlock(&nm_i
->free_nid_list_lock
);
1629 kmem_cache_free(free_nid_slab
, i
);
1632 void recover_inline_xattr(struct inode
*inode
, struct page
*page
)
1634 void *src_addr
, *dst_addr
;
1637 struct f2fs_inode
*ri
;
1639 ipage
= get_node_page(F2FS_I_SB(inode
), inode
->i_ino
);
1640 f2fs_bug_on(F2FS_I_SB(inode
), IS_ERR(ipage
));
1642 ri
= F2FS_INODE(page
);
1643 if (!(ri
->i_inline
& F2FS_INLINE_XATTR
)) {
1644 clear_inode_flag(F2FS_I(inode
), FI_INLINE_XATTR
);
1648 dst_addr
= inline_xattr_addr(ipage
);
1649 src_addr
= inline_xattr_addr(page
);
1650 inline_size
= inline_xattr_size(inode
);
1652 f2fs_wait_on_page_writeback(ipage
, NODE
);
1653 memcpy(dst_addr
, src_addr
, inline_size
);
1655 update_inode(inode
, ipage
);
1656 f2fs_put_page(ipage
, 1);
1659 void recover_xattr_data(struct inode
*inode
, struct page
*page
, block_t blkaddr
)
1661 struct f2fs_sb_info
*sbi
= F2FS_I_SB(inode
);
1662 nid_t prev_xnid
= F2FS_I(inode
)->i_xattr_nid
;
1663 nid_t new_xnid
= nid_of_node(page
);
1664 struct node_info ni
;
1666 /* 1: invalidate the previous xattr nid */
1670 /* Deallocate node address */
1671 get_node_info(sbi
, prev_xnid
, &ni
);
1672 f2fs_bug_on(sbi
, ni
.blk_addr
== NULL_ADDR
);
1673 invalidate_blocks(sbi
, ni
.blk_addr
);
1674 dec_valid_node_count(sbi
, inode
);
1675 set_node_addr(sbi
, &ni
, NULL_ADDR
, false);
1678 /* 2: allocate new xattr nid */
1679 if (unlikely(!inc_valid_node_count(sbi
, inode
)))
1680 f2fs_bug_on(sbi
, 1);
1682 remove_free_nid(NM_I(sbi
), new_xnid
);
1683 get_node_info(sbi
, new_xnid
, &ni
);
1684 ni
.ino
= inode
->i_ino
;
1685 set_node_addr(sbi
, &ni
, NEW_ADDR
, false);
1686 F2FS_I(inode
)->i_xattr_nid
= new_xnid
;
1688 /* 3: update xattr blkaddr */
1689 refresh_sit_entry(sbi
, NEW_ADDR
, blkaddr
);
1690 set_node_addr(sbi
, &ni
, blkaddr
, false);
1692 update_inode_page(inode
);
1695 int recover_inode_page(struct f2fs_sb_info
*sbi
, struct page
*page
)
1697 struct f2fs_inode
*src
, *dst
;
1698 nid_t ino
= ino_of_node(page
);
1699 struct node_info old_ni
, new_ni
;
1702 get_node_info(sbi
, ino
, &old_ni
);
1704 if (unlikely(old_ni
.blk_addr
!= NULL_ADDR
))
1707 ipage
= grab_cache_page(NODE_MAPPING(sbi
), ino
);
1711 /* Should not use this inode from free nid list */
1712 remove_free_nid(NM_I(sbi
), ino
);
1714 SetPageUptodate(ipage
);
1715 fill_node_footer(ipage
, ino
, ino
, 0, true);
1717 src
= F2FS_INODE(page
);
1718 dst
= F2FS_INODE(ipage
);
1720 memcpy(dst
, src
, (unsigned long)&src
->i_ext
- (unsigned long)src
);
1722 dst
->i_blocks
= cpu_to_le64(1);
1723 dst
->i_links
= cpu_to_le32(1);
1724 dst
->i_xattr_nid
= 0;
1725 dst
->i_inline
= src
->i_inline
& F2FS_INLINE_XATTR
;
1730 if (unlikely(!inc_valid_node_count(sbi
, NULL
)))
1732 set_node_addr(sbi
, &new_ni
, NEW_ADDR
, false);
1733 inc_valid_inode_count(sbi
);
1734 set_page_dirty(ipage
);
1735 f2fs_put_page(ipage
, 1);
1740 * ra_sum_pages() merge contiguous pages into one bio and submit.
1741 * these pre-read pages are allocated in bd_inode's mapping tree.
1743 static int ra_sum_pages(struct f2fs_sb_info
*sbi
, struct page
**pages
,
1744 int start
, int nrpages
)
1746 struct inode
*inode
= sbi
->sb
->s_bdev
->bd_inode
;
1747 struct address_space
*mapping
= inode
->i_mapping
;
1748 int i
, page_idx
= start
;
1749 struct f2fs_io_info fio
= {
1751 .rw
= READ_SYNC
| REQ_META
| REQ_PRIO
1754 for (i
= 0; page_idx
< start
+ nrpages
; page_idx
++, i
++) {
1755 /* alloc page in bd_inode for reading node summary info */
1756 pages
[i
] = grab_cache_page(mapping
, page_idx
);
1759 f2fs_submit_page_mbio(sbi
, pages
[i
], page_idx
, &fio
);
1762 f2fs_submit_merged_bio(sbi
, META
, READ
);
1766 int restore_node_summary(struct f2fs_sb_info
*sbi
,
1767 unsigned int segno
, struct f2fs_summary_block
*sum
)
1769 struct f2fs_node
*rn
;
1770 struct f2fs_summary
*sum_entry
;
1771 struct inode
*inode
= sbi
->sb
->s_bdev
->bd_inode
;
1773 int bio_blocks
= MAX_BIO_BLOCKS(sbi
);
1774 struct page
*pages
[bio_blocks
];
1775 int i
, idx
, last_offset
, nrpages
, err
= 0;
1777 /* scan the node segment */
1778 last_offset
= sbi
->blocks_per_seg
;
1779 addr
= START_BLOCK(sbi
, segno
);
1780 sum_entry
= &sum
->entries
[0];
1782 for (i
= 0; !err
&& i
< last_offset
; i
+= nrpages
, addr
+= nrpages
) {
1783 nrpages
= min(last_offset
- i
, bio_blocks
);
1785 /* readahead node pages */
1786 nrpages
= ra_sum_pages(sbi
, pages
, addr
, nrpages
);
1790 for (idx
= 0; idx
< nrpages
; idx
++) {
1794 lock_page(pages
[idx
]);
1795 if (unlikely(!PageUptodate(pages
[idx
]))) {
1798 rn
= F2FS_NODE(pages
[idx
]);
1799 sum_entry
->nid
= rn
->footer
.nid
;
1800 sum_entry
->version
= 0;
1801 sum_entry
->ofs_in_node
= 0;
1804 unlock_page(pages
[idx
]);
1806 page_cache_release(pages
[idx
]);
1809 invalidate_mapping_pages(inode
->i_mapping
, addr
,
1815 static void remove_nats_in_journal(struct f2fs_sb_info
*sbi
)
1817 struct f2fs_nm_info
*nm_i
= NM_I(sbi
);
1818 struct curseg_info
*curseg
= CURSEG_I(sbi
, CURSEG_HOT_DATA
);
1819 struct f2fs_summary_block
*sum
= curseg
->sum_blk
;
1822 mutex_lock(&curseg
->curseg_mutex
);
1823 for (i
= 0; i
< nats_in_cursum(sum
); i
++) {
1824 struct nat_entry
*ne
;
1825 struct f2fs_nat_entry raw_ne
;
1826 nid_t nid
= le32_to_cpu(nid_in_journal(sum
, i
));
1828 raw_ne
= nat_in_journal(sum
, i
);
1830 write_lock(&nm_i
->nat_tree_lock
);
1831 ne
= __lookup_nat_cache(nm_i
, nid
);
1835 ne
= grab_nat_entry(nm_i
, nid
);
1837 write_unlock(&nm_i
->nat_tree_lock
);
1840 node_info_from_raw_nat(&ne
->ni
, &raw_ne
);
1842 __set_nat_cache_dirty(nm_i
, ne
);
1843 write_unlock(&nm_i
->nat_tree_lock
);
1845 update_nats_in_cursum(sum
, -i
);
1846 mutex_unlock(&curseg
->curseg_mutex
);
1849 static void __adjust_nat_entry_set(struct nat_entry_set
*nes
,
1850 struct list_head
*head
, int max
)
1852 struct nat_entry_set
*cur
;
1854 if (nes
->entry_cnt
>= max
)
1857 list_for_each_entry(cur
, head
, set_list
) {
1858 if (cur
->entry_cnt
>= nes
->entry_cnt
) {
1859 list_add(&nes
->set_list
, cur
->set_list
.prev
);
1864 list_add_tail(&nes
->set_list
, head
);
1867 static void __flush_nat_entry_set(struct f2fs_sb_info
*sbi
,
1868 struct nat_entry_set
*set
)
1870 struct curseg_info
*curseg
= CURSEG_I(sbi
, CURSEG_HOT_DATA
);
1871 struct f2fs_summary_block
*sum
= curseg
->sum_blk
;
1872 nid_t start_nid
= set
->set
* NAT_ENTRY_PER_BLOCK
;
1873 bool to_journal
= true;
1874 struct f2fs_nat_block
*nat_blk
;
1875 struct nat_entry
*ne
, *cur
;
1876 struct page
*page
= NULL
;
1879 * there are two steps to flush nat entries:
1880 * #1, flush nat entries to journal in current hot data summary block.
1881 * #2, flush nat entries to nat page.
1883 if (!__has_cursum_space(sum
, set
->entry_cnt
, NAT_JOURNAL
))
1887 mutex_lock(&curseg
->curseg_mutex
);
1889 page
= get_next_nat_page(sbi
, start_nid
);
1890 nat_blk
= page_address(page
);
1891 f2fs_bug_on(sbi
, !nat_blk
);
1894 /* flush dirty nats in nat entry set */
1895 list_for_each_entry_safe(ne
, cur
, &set
->entry_list
, list
) {
1896 struct f2fs_nat_entry
*raw_ne
;
1897 nid_t nid
= nat_get_nid(ne
);
1900 if (nat_get_blkaddr(ne
) == NEW_ADDR
)
1904 offset
= lookup_journal_in_cursum(sum
,
1905 NAT_JOURNAL
, nid
, 1);
1906 f2fs_bug_on(sbi
, offset
< 0);
1907 raw_ne
= &nat_in_journal(sum
, offset
);
1908 nid_in_journal(sum
, offset
) = cpu_to_le32(nid
);
1910 raw_ne
= &nat_blk
->entries
[nid
- start_nid
];
1912 raw_nat_from_node_info(raw_ne
, &ne
->ni
);
1914 write_lock(&NM_I(sbi
)->nat_tree_lock
);
1916 __clear_nat_cache_dirty(NM_I(sbi
), ne
);
1917 write_unlock(&NM_I(sbi
)->nat_tree_lock
);
1919 if (nat_get_blkaddr(ne
) == NULL_ADDR
)
1920 add_free_nid(sbi
, nid
, false);
1924 mutex_unlock(&curseg
->curseg_mutex
);
1926 f2fs_put_page(page
, 1);
1928 f2fs_bug_on(sbi
, set
->entry_cnt
);
1930 radix_tree_delete(&NM_I(sbi
)->nat_set_root
, set
->set
);
1931 kmem_cache_free(nat_entry_set_slab
, set
);
1935 * This function is called during the checkpointing process.
1937 void flush_nat_entries(struct f2fs_sb_info
*sbi
)
1939 struct f2fs_nm_info
*nm_i
= NM_I(sbi
);
1940 struct curseg_info
*curseg
= CURSEG_I(sbi
, CURSEG_HOT_DATA
);
1941 struct f2fs_summary_block
*sum
= curseg
->sum_blk
;
1942 struct nat_entry_set
*setvec
[NATVEC_SIZE
];
1943 struct nat_entry_set
*set
, *tmp
;
1948 if (!nm_i
->dirty_nat_cnt
)
1951 * if there are no enough space in journal to store dirty nat
1952 * entries, remove all entries from journal and merge them
1953 * into nat entry set.
1955 if (!__has_cursum_space(sum
, nm_i
->dirty_nat_cnt
, NAT_JOURNAL
))
1956 remove_nats_in_journal(sbi
);
1958 while ((found
= __gang_lookup_nat_set(nm_i
,
1959 set_idx
, NATVEC_SIZE
, setvec
))) {
1961 set_idx
= setvec
[found
- 1]->set
+ 1;
1962 for (idx
= 0; idx
< found
; idx
++)
1963 __adjust_nat_entry_set(setvec
[idx
], &sets
,
1964 MAX_NAT_JENTRIES(sum
));
1967 /* flush dirty nats in nat entry set */
1968 list_for_each_entry_safe(set
, tmp
, &sets
, set_list
)
1969 __flush_nat_entry_set(sbi
, set
);
1971 f2fs_bug_on(sbi
, nm_i
->dirty_nat_cnt
);
1974 static int init_node_manager(struct f2fs_sb_info
*sbi
)
1976 struct f2fs_super_block
*sb_raw
= F2FS_RAW_SUPER(sbi
);
1977 struct f2fs_nm_info
*nm_i
= NM_I(sbi
);
1978 unsigned char *version_bitmap
;
1979 unsigned int nat_segs
, nat_blocks
;
1981 nm_i
->nat_blkaddr
= le32_to_cpu(sb_raw
->nat_blkaddr
);
1983 /* segment_count_nat includes pair segment so divide to 2. */
1984 nat_segs
= le32_to_cpu(sb_raw
->segment_count_nat
) >> 1;
1985 nat_blocks
= nat_segs
<< le32_to_cpu(sb_raw
->log_blocks_per_seg
);
1987 nm_i
->max_nid
= NAT_ENTRY_PER_BLOCK
* nat_blocks
;
1989 /* not used nids: 0, node, meta, (and root counted as valid node) */
1990 nm_i
->available_nids
= nm_i
->max_nid
- F2FS_RESERVED_NODE_NUM
;
1993 nm_i
->ram_thresh
= DEF_RAM_THRESHOLD
;
1995 INIT_RADIX_TREE(&nm_i
->free_nid_root
, GFP_ATOMIC
);
1996 INIT_LIST_HEAD(&nm_i
->free_nid_list
);
1997 INIT_RADIX_TREE(&nm_i
->nat_root
, GFP_ATOMIC
);
1998 INIT_RADIX_TREE(&nm_i
->nat_set_root
, GFP_ATOMIC
);
1999 INIT_LIST_HEAD(&nm_i
->nat_entries
);
2001 mutex_init(&nm_i
->build_lock
);
2002 spin_lock_init(&nm_i
->free_nid_list_lock
);
2003 rwlock_init(&nm_i
->nat_tree_lock
);
2005 nm_i
->next_scan_nid
= le32_to_cpu(sbi
->ckpt
->next_free_nid
);
2006 nm_i
->bitmap_size
= __bitmap_size(sbi
, NAT_BITMAP
);
2007 version_bitmap
= __bitmap_ptr(sbi
, NAT_BITMAP
);
2008 if (!version_bitmap
)
2011 nm_i
->nat_bitmap
= kmemdup(version_bitmap
, nm_i
->bitmap_size
,
2013 if (!nm_i
->nat_bitmap
)
2018 int build_node_manager(struct f2fs_sb_info
*sbi
)
2022 sbi
->nm_info
= kzalloc(sizeof(struct f2fs_nm_info
), GFP_KERNEL
);
2026 err
= init_node_manager(sbi
);
2030 build_free_nids(sbi
);
2034 void destroy_node_manager(struct f2fs_sb_info
*sbi
)
2036 struct f2fs_nm_info
*nm_i
= NM_I(sbi
);
2037 struct free_nid
*i
, *next_i
;
2038 struct nat_entry
*natvec
[NATVEC_SIZE
];
2045 /* destroy free nid list */
2046 spin_lock(&nm_i
->free_nid_list_lock
);
2047 list_for_each_entry_safe(i
, next_i
, &nm_i
->free_nid_list
, list
) {
2048 f2fs_bug_on(sbi
, i
->state
== NID_ALLOC
);
2049 __del_from_free_nid_list(nm_i
, i
);
2051 spin_unlock(&nm_i
->free_nid_list_lock
);
2052 kmem_cache_free(free_nid_slab
, i
);
2053 spin_lock(&nm_i
->free_nid_list_lock
);
2055 f2fs_bug_on(sbi
, nm_i
->fcnt
);
2056 spin_unlock(&nm_i
->free_nid_list_lock
);
2058 /* destroy nat cache */
2059 write_lock(&nm_i
->nat_tree_lock
);
2060 while ((found
= __gang_lookup_nat_cache(nm_i
,
2061 nid
, NATVEC_SIZE
, natvec
))) {
2063 nid
= nat_get_nid(natvec
[found
- 1]) + 1;
2064 for (idx
= 0; idx
< found
; idx
++)
2065 __del_from_nat_cache(nm_i
, natvec
[idx
]);
2067 f2fs_bug_on(sbi
, nm_i
->nat_cnt
);
2068 write_unlock(&nm_i
->nat_tree_lock
);
2070 kfree(nm_i
->nat_bitmap
);
2071 sbi
->nm_info
= NULL
;
2075 int __init
create_node_manager_caches(void)
2077 nat_entry_slab
= f2fs_kmem_cache_create("nat_entry",
2078 sizeof(struct nat_entry
));
2079 if (!nat_entry_slab
)
2082 free_nid_slab
= f2fs_kmem_cache_create("free_nid",
2083 sizeof(struct free_nid
));
2085 goto destroy_nat_entry
;
2087 nat_entry_set_slab
= f2fs_kmem_cache_create("nat_entry_set",
2088 sizeof(struct nat_entry_set
));
2089 if (!nat_entry_set_slab
)
2090 goto destroy_free_nid
;
2094 kmem_cache_destroy(free_nid_slab
);
2096 kmem_cache_destroy(nat_entry_slab
);
2101 void destroy_node_manager_caches(void)
2103 kmem_cache_destroy(nat_entry_set_slab
);
2104 kmem_cache_destroy(free_nid_slab
);
2105 kmem_cache_destroy(nat_entry_slab
);