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 static struct kmem_cache
*nat_entry_slab
;
25 static struct kmem_cache
*free_nid_slab
;
27 static void clear_node_page_dirty(struct page
*page
)
29 struct address_space
*mapping
= page
->mapping
;
30 struct f2fs_sb_info
*sbi
= F2FS_SB(mapping
->host
->i_sb
);
31 unsigned int long flags
;
33 if (PageDirty(page
)) {
34 spin_lock_irqsave(&mapping
->tree_lock
, flags
);
35 radix_tree_tag_clear(&mapping
->page_tree
,
38 spin_unlock_irqrestore(&mapping
->tree_lock
, flags
);
40 clear_page_dirty_for_io(page
);
41 dec_page_count(sbi
, F2FS_DIRTY_NODES
);
43 ClearPageUptodate(page
);
46 static struct page
*get_current_nat_page(struct f2fs_sb_info
*sbi
, nid_t nid
)
48 pgoff_t index
= current_nat_addr(sbi
, nid
);
49 return get_meta_page(sbi
, index
);
52 static struct page
*get_next_nat_page(struct f2fs_sb_info
*sbi
, nid_t nid
)
54 struct page
*src_page
;
55 struct page
*dst_page
;
60 struct f2fs_nm_info
*nm_i
= NM_I(sbi
);
62 src_off
= current_nat_addr(sbi
, nid
);
63 dst_off
= next_nat_addr(sbi
, src_off
);
65 /* get current nat block page with lock */
66 src_page
= get_meta_page(sbi
, src_off
);
68 /* Dirty src_page means that it is already the new target NAT page. */
69 if (PageDirty(src_page
))
72 dst_page
= grab_meta_page(sbi
, dst_off
);
74 src_addr
= page_address(src_page
);
75 dst_addr
= page_address(dst_page
);
76 memcpy(dst_addr
, src_addr
, PAGE_CACHE_SIZE
);
77 set_page_dirty(dst_page
);
78 f2fs_put_page(src_page
, 1);
80 set_to_next_nat(nm_i
, nid
);
88 static void ra_nat_pages(struct f2fs_sb_info
*sbi
, int nid
)
90 struct address_space
*mapping
= sbi
->meta_inode
->i_mapping
;
91 struct f2fs_nm_info
*nm_i
= NM_I(sbi
);
97 blk_start_plug(&plug
);
99 for (i
= 0; i
< FREE_NID_PAGES
; i
++, nid
+= NAT_ENTRY_PER_BLOCK
) {
100 if (nid
>= nm_i
->max_nid
)
102 index
= current_nat_addr(sbi
, nid
);
104 page
= grab_cache_page(mapping
, index
);
107 if (PageUptodate(page
)) {
108 f2fs_put_page(page
, 1);
111 if (f2fs_readpage(sbi
, page
, index
, READ
))
114 f2fs_put_page(page
, 0);
116 blk_finish_plug(&plug
);
119 static struct nat_entry
*__lookup_nat_cache(struct f2fs_nm_info
*nm_i
, nid_t n
)
121 return radix_tree_lookup(&nm_i
->nat_root
, n
);
124 static unsigned int __gang_lookup_nat_cache(struct f2fs_nm_info
*nm_i
,
125 nid_t start
, unsigned int nr
, struct nat_entry
**ep
)
127 return radix_tree_gang_lookup(&nm_i
->nat_root
, (void **)ep
, start
, nr
);
130 static void __del_from_nat_cache(struct f2fs_nm_info
*nm_i
, struct nat_entry
*e
)
133 radix_tree_delete(&nm_i
->nat_root
, nat_get_nid(e
));
135 kmem_cache_free(nat_entry_slab
, e
);
138 int is_checkpointed_node(struct f2fs_sb_info
*sbi
, nid_t nid
)
140 struct f2fs_nm_info
*nm_i
= NM_I(sbi
);
144 read_lock(&nm_i
->nat_tree_lock
);
145 e
= __lookup_nat_cache(nm_i
, nid
);
146 if (e
&& !e
->checkpointed
)
148 read_unlock(&nm_i
->nat_tree_lock
);
152 static struct nat_entry
*grab_nat_entry(struct f2fs_nm_info
*nm_i
, nid_t nid
)
154 struct nat_entry
*new;
156 new = kmem_cache_alloc(nat_entry_slab
, GFP_ATOMIC
);
159 if (radix_tree_insert(&nm_i
->nat_root
, nid
, new)) {
160 kmem_cache_free(nat_entry_slab
, new);
163 memset(new, 0, sizeof(struct nat_entry
));
164 nat_set_nid(new, nid
);
165 list_add_tail(&new->list
, &nm_i
->nat_entries
);
170 static void cache_nat_entry(struct f2fs_nm_info
*nm_i
, nid_t nid
,
171 struct f2fs_nat_entry
*ne
)
175 write_lock(&nm_i
->nat_tree_lock
);
176 e
= __lookup_nat_cache(nm_i
, nid
);
178 e
= grab_nat_entry(nm_i
, nid
);
180 write_unlock(&nm_i
->nat_tree_lock
);
183 nat_set_blkaddr(e
, le32_to_cpu(ne
->block_addr
));
184 nat_set_ino(e
, le32_to_cpu(ne
->ino
));
185 nat_set_version(e
, ne
->version
);
186 e
->checkpointed
= true;
188 write_unlock(&nm_i
->nat_tree_lock
);
191 static void set_node_addr(struct f2fs_sb_info
*sbi
, struct node_info
*ni
,
194 struct f2fs_nm_info
*nm_i
= NM_I(sbi
);
197 write_lock(&nm_i
->nat_tree_lock
);
198 e
= __lookup_nat_cache(nm_i
, ni
->nid
);
200 e
= grab_nat_entry(nm_i
, ni
->nid
);
202 write_unlock(&nm_i
->nat_tree_lock
);
206 e
->checkpointed
= true;
207 BUG_ON(ni
->blk_addr
== NEW_ADDR
);
208 } else if (new_blkaddr
== NEW_ADDR
) {
210 * when nid is reallocated,
211 * previous nat entry can be remained in nat cache.
212 * So, reinitialize it with new information.
215 BUG_ON(ni
->blk_addr
!= NULL_ADDR
);
218 if (new_blkaddr
== NEW_ADDR
)
219 e
->checkpointed
= false;
222 BUG_ON(nat_get_blkaddr(e
) != ni
->blk_addr
);
223 BUG_ON(nat_get_blkaddr(e
) == NULL_ADDR
&&
224 new_blkaddr
== NULL_ADDR
);
225 BUG_ON(nat_get_blkaddr(e
) == NEW_ADDR
&&
226 new_blkaddr
== NEW_ADDR
);
227 BUG_ON(nat_get_blkaddr(e
) != NEW_ADDR
&&
228 nat_get_blkaddr(e
) != NULL_ADDR
&&
229 new_blkaddr
== NEW_ADDR
);
231 /* increament version no as node is removed */
232 if (nat_get_blkaddr(e
) != NEW_ADDR
&& new_blkaddr
== NULL_ADDR
) {
233 unsigned char version
= nat_get_version(e
);
234 nat_set_version(e
, inc_node_version(version
));
238 nat_set_blkaddr(e
, new_blkaddr
);
239 __set_nat_cache_dirty(nm_i
, e
);
240 write_unlock(&nm_i
->nat_tree_lock
);
243 static int try_to_free_nats(struct f2fs_sb_info
*sbi
, int nr_shrink
)
245 struct f2fs_nm_info
*nm_i
= NM_I(sbi
);
247 if (nm_i
->nat_cnt
<= NM_WOUT_THRESHOLD
)
250 write_lock(&nm_i
->nat_tree_lock
);
251 while (nr_shrink
&& !list_empty(&nm_i
->nat_entries
)) {
252 struct nat_entry
*ne
;
253 ne
= list_first_entry(&nm_i
->nat_entries
,
254 struct nat_entry
, list
);
255 __del_from_nat_cache(nm_i
, ne
);
258 write_unlock(&nm_i
->nat_tree_lock
);
263 * This function returns always success
265 void get_node_info(struct f2fs_sb_info
*sbi
, nid_t nid
, struct node_info
*ni
)
267 struct f2fs_nm_info
*nm_i
= NM_I(sbi
);
268 struct curseg_info
*curseg
= CURSEG_I(sbi
, CURSEG_HOT_DATA
);
269 struct f2fs_summary_block
*sum
= curseg
->sum_blk
;
270 nid_t start_nid
= START_NID(nid
);
271 struct f2fs_nat_block
*nat_blk
;
272 struct page
*page
= NULL
;
273 struct f2fs_nat_entry ne
;
277 memset(&ne
, 0, sizeof(struct f2fs_nat_entry
));
280 /* Check nat cache */
281 read_lock(&nm_i
->nat_tree_lock
);
282 e
= __lookup_nat_cache(nm_i
, nid
);
284 ni
->ino
= nat_get_ino(e
);
285 ni
->blk_addr
= nat_get_blkaddr(e
);
286 ni
->version
= nat_get_version(e
);
288 read_unlock(&nm_i
->nat_tree_lock
);
292 /* Check current segment summary */
293 mutex_lock(&curseg
->curseg_mutex
);
294 i
= lookup_journal_in_cursum(sum
, NAT_JOURNAL
, nid
, 0);
296 ne
= nat_in_journal(sum
, i
);
297 node_info_from_raw_nat(ni
, &ne
);
299 mutex_unlock(&curseg
->curseg_mutex
);
303 /* Fill node_info from nat page */
304 page
= get_current_nat_page(sbi
, start_nid
);
305 nat_blk
= (struct f2fs_nat_block
*)page_address(page
);
306 ne
= nat_blk
->entries
[nid
- start_nid
];
307 node_info_from_raw_nat(ni
, &ne
);
308 f2fs_put_page(page
, 1);
310 /* cache nat entry */
311 cache_nat_entry(NM_I(sbi
), nid
, &ne
);
315 * The maximum depth is four.
316 * Offset[0] will have raw inode offset.
318 static int get_node_path(long block
, int offset
[4], unsigned int noffset
[4])
320 const long direct_index
= ADDRS_PER_INODE
;
321 const long direct_blks
= ADDRS_PER_BLOCK
;
322 const long dptrs_per_blk
= NIDS_PER_BLOCK
;
323 const long indirect_blks
= ADDRS_PER_BLOCK
* NIDS_PER_BLOCK
;
324 const long dindirect_blks
= indirect_blks
* NIDS_PER_BLOCK
;
330 if (block
< direct_index
) {
334 block
-= direct_index
;
335 if (block
< direct_blks
) {
336 offset
[n
++] = NODE_DIR1_BLOCK
;
342 block
-= direct_blks
;
343 if (block
< direct_blks
) {
344 offset
[n
++] = NODE_DIR2_BLOCK
;
350 block
-= direct_blks
;
351 if (block
< indirect_blks
) {
352 offset
[n
++] = NODE_IND1_BLOCK
;
354 offset
[n
++] = block
/ direct_blks
;
355 noffset
[n
] = 4 + offset
[n
- 1];
356 offset
[n
] = block
% direct_blks
;
360 block
-= indirect_blks
;
361 if (block
< indirect_blks
) {
362 offset
[n
++] = NODE_IND2_BLOCK
;
363 noffset
[n
] = 4 + dptrs_per_blk
;
364 offset
[n
++] = block
/ direct_blks
;
365 noffset
[n
] = 5 + dptrs_per_blk
+ offset
[n
- 1];
366 offset
[n
] = block
% direct_blks
;
370 block
-= indirect_blks
;
371 if (block
< dindirect_blks
) {
372 offset
[n
++] = NODE_DIND_BLOCK
;
373 noffset
[n
] = 5 + (dptrs_per_blk
* 2);
374 offset
[n
++] = block
/ indirect_blks
;
375 noffset
[n
] = 6 + (dptrs_per_blk
* 2) +
376 offset
[n
- 1] * (dptrs_per_blk
+ 1);
377 offset
[n
++] = (block
/ direct_blks
) % dptrs_per_blk
;
378 noffset
[n
] = 7 + (dptrs_per_blk
* 2) +
379 offset
[n
- 2] * (dptrs_per_blk
+ 1) +
381 offset
[n
] = block
% direct_blks
;
392 * Caller should call f2fs_put_dnode(dn).
393 * Also, it should grab and release a mutex by calling mutex_lock_op() and
394 * mutex_unlock_op() only if ro is not set RDONLY_NODE.
395 * In the case of RDONLY_NODE, we don't need to care about mutex.
397 int get_dnode_of_data(struct dnode_of_data
*dn
, pgoff_t index
, int mode
)
399 struct f2fs_sb_info
*sbi
= F2FS_SB(dn
->inode
->i_sb
);
400 struct page
*npage
[4];
403 unsigned int noffset
[4];
408 level
= get_node_path(index
, offset
, noffset
);
410 nids
[0] = dn
->inode
->i_ino
;
411 npage
[0] = dn
->inode_page
;
414 npage
[0] = get_node_page(sbi
, nids
[0]);
415 if (IS_ERR(npage
[0]))
416 return PTR_ERR(npage
[0]);
420 nids
[1] = get_nid(parent
, offset
[0], true);
421 dn
->inode_page
= npage
[0];
422 dn
->inode_page_locked
= true;
424 /* get indirect or direct nodes */
425 for (i
= 1; i
<= level
; i
++) {
428 if (!nids
[i
] && mode
== ALLOC_NODE
) {
430 if (!alloc_nid(sbi
, &(nids
[i
]))) {
436 npage
[i
] = new_node_page(dn
, noffset
[i
], NULL
);
437 if (IS_ERR(npage
[i
])) {
438 alloc_nid_failed(sbi
, nids
[i
]);
439 err
= PTR_ERR(npage
[i
]);
443 set_nid(parent
, offset
[i
- 1], nids
[i
], i
== 1);
444 alloc_nid_done(sbi
, nids
[i
]);
446 } else if (mode
== LOOKUP_NODE_RA
&& i
== level
&& level
> 1) {
447 npage
[i
] = get_node_page_ra(parent
, offset
[i
- 1]);
448 if (IS_ERR(npage
[i
])) {
449 err
= PTR_ERR(npage
[i
]);
455 dn
->inode_page_locked
= false;
458 f2fs_put_page(parent
, 1);
462 npage
[i
] = get_node_page(sbi
, nids
[i
]);
463 if (IS_ERR(npage
[i
])) {
464 err
= PTR_ERR(npage
[i
]);
465 f2fs_put_page(npage
[0], 0);
471 nids
[i
+ 1] = get_nid(parent
, offset
[i
], false);
474 dn
->nid
= nids
[level
];
475 dn
->ofs_in_node
= offset
[level
];
476 dn
->node_page
= npage
[level
];
477 dn
->data_blkaddr
= datablock_addr(dn
->node_page
, dn
->ofs_in_node
);
481 f2fs_put_page(parent
, 1);
483 f2fs_put_page(npage
[0], 0);
485 dn
->inode_page
= NULL
;
486 dn
->node_page
= NULL
;
490 static void truncate_node(struct dnode_of_data
*dn
)
492 struct f2fs_sb_info
*sbi
= F2FS_SB(dn
->inode
->i_sb
);
495 get_node_info(sbi
, dn
->nid
, &ni
);
496 if (dn
->inode
->i_blocks
== 0) {
497 BUG_ON(ni
.blk_addr
!= NULL_ADDR
);
500 BUG_ON(ni
.blk_addr
== NULL_ADDR
);
502 /* Deallocate node address */
503 invalidate_blocks(sbi
, ni
.blk_addr
);
504 dec_valid_node_count(sbi
, dn
->inode
, 1);
505 set_node_addr(sbi
, &ni
, NULL_ADDR
);
507 if (dn
->nid
== dn
->inode
->i_ino
) {
508 remove_orphan_inode(sbi
, dn
->nid
);
509 dec_valid_inode_count(sbi
);
514 clear_node_page_dirty(dn
->node_page
);
515 F2FS_SET_SB_DIRT(sbi
);
517 f2fs_put_page(dn
->node_page
, 1);
518 dn
->node_page
= NULL
;
519 trace_f2fs_truncate_node(dn
->inode
, dn
->nid
, ni
.blk_addr
);
522 static int truncate_dnode(struct dnode_of_data
*dn
)
524 struct f2fs_sb_info
*sbi
= F2FS_SB(dn
->inode
->i_sb
);
530 /* get direct node */
531 page
= get_node_page(sbi
, dn
->nid
);
532 if (IS_ERR(page
) && PTR_ERR(page
) == -ENOENT
)
534 else if (IS_ERR(page
))
535 return PTR_ERR(page
);
537 /* Make dnode_of_data for parameter */
538 dn
->node_page
= page
;
540 truncate_data_blocks(dn
);
545 static int truncate_nodes(struct dnode_of_data
*dn
, unsigned int nofs
,
548 struct f2fs_sb_info
*sbi
= F2FS_SB(dn
->inode
->i_sb
);
549 struct dnode_of_data rdn
= *dn
;
551 struct f2fs_node
*rn
;
553 unsigned int child_nofs
;
558 return NIDS_PER_BLOCK
+ 1;
560 trace_f2fs_truncate_nodes_enter(dn
->inode
, dn
->nid
, dn
->data_blkaddr
);
562 page
= get_node_page(sbi
, dn
->nid
);
564 trace_f2fs_truncate_nodes_exit(dn
->inode
, PTR_ERR(page
));
565 return PTR_ERR(page
);
568 rn
= (struct f2fs_node
*)page_address(page
);
570 for (i
= ofs
; i
< NIDS_PER_BLOCK
; i
++, freed
++) {
571 child_nid
= le32_to_cpu(rn
->in
.nid
[i
]);
575 ret
= truncate_dnode(&rdn
);
578 set_nid(page
, i
, 0, false);
581 child_nofs
= nofs
+ ofs
* (NIDS_PER_BLOCK
+ 1) + 1;
582 for (i
= ofs
; i
< NIDS_PER_BLOCK
; i
++) {
583 child_nid
= le32_to_cpu(rn
->in
.nid
[i
]);
584 if (child_nid
== 0) {
585 child_nofs
+= NIDS_PER_BLOCK
+ 1;
589 ret
= truncate_nodes(&rdn
, child_nofs
, 0, depth
- 1);
590 if (ret
== (NIDS_PER_BLOCK
+ 1)) {
591 set_nid(page
, i
, 0, false);
593 } else if (ret
< 0 && ret
!= -ENOENT
) {
601 /* remove current indirect node */
602 dn
->node_page
= page
;
606 f2fs_put_page(page
, 1);
608 trace_f2fs_truncate_nodes_exit(dn
->inode
, freed
);
612 f2fs_put_page(page
, 1);
613 trace_f2fs_truncate_nodes_exit(dn
->inode
, ret
);
617 static int truncate_partial_nodes(struct dnode_of_data
*dn
,
618 struct f2fs_inode
*ri
, int *offset
, int depth
)
620 struct f2fs_sb_info
*sbi
= F2FS_SB(dn
->inode
->i_sb
);
621 struct page
*pages
[2];
628 nid
[0] = le32_to_cpu(ri
->i_nid
[offset
[0] - NODE_DIR1_BLOCK
]);
632 /* get indirect nodes in the path */
633 for (i
= 0; i
< depth
- 1; i
++) {
634 /* refernece count'll be increased */
635 pages
[i
] = get_node_page(sbi
, nid
[i
]);
636 if (IS_ERR(pages
[i
])) {
638 err
= PTR_ERR(pages
[i
]);
641 nid
[i
+ 1] = get_nid(pages
[i
], offset
[i
+ 1], false);
644 /* free direct nodes linked to a partial indirect node */
645 for (i
= offset
[depth
- 1]; i
< NIDS_PER_BLOCK
; i
++) {
646 child_nid
= get_nid(pages
[idx
], i
, false);
650 err
= truncate_dnode(dn
);
653 set_nid(pages
[idx
], i
, 0, false);
656 if (offset
[depth
- 1] == 0) {
657 dn
->node_page
= pages
[idx
];
661 f2fs_put_page(pages
[idx
], 1);
664 offset
[depth
- 1] = 0;
666 for (i
= depth
- 3; i
>= 0; i
--)
667 f2fs_put_page(pages
[i
], 1);
669 trace_f2fs_truncate_partial_nodes(dn
->inode
, nid
, depth
, err
);
675 * All the block addresses of data and nodes should be nullified.
677 int truncate_inode_blocks(struct inode
*inode
, pgoff_t from
)
679 struct f2fs_sb_info
*sbi
= F2FS_SB(inode
->i_sb
);
680 struct address_space
*node_mapping
= sbi
->node_inode
->i_mapping
;
681 int err
= 0, cont
= 1;
682 int level
, offset
[4], noffset
[4];
683 unsigned int nofs
= 0;
684 struct f2fs_node
*rn
;
685 struct dnode_of_data dn
;
688 trace_f2fs_truncate_inode_blocks_enter(inode
, from
);
690 level
= get_node_path(from
, offset
, noffset
);
692 page
= get_node_page(sbi
, inode
->i_ino
);
694 trace_f2fs_truncate_inode_blocks_exit(inode
, PTR_ERR(page
));
695 return PTR_ERR(page
);
698 set_new_dnode(&dn
, inode
, page
, NULL
, 0);
701 rn
= page_address(page
);
709 if (!offset
[level
- 1])
711 err
= truncate_partial_nodes(&dn
, &rn
->i
, offset
, level
);
712 if (err
< 0 && err
!= -ENOENT
)
714 nofs
+= 1 + NIDS_PER_BLOCK
;
717 nofs
= 5 + 2 * NIDS_PER_BLOCK
;
718 if (!offset
[level
- 1])
720 err
= truncate_partial_nodes(&dn
, &rn
->i
, offset
, level
);
721 if (err
< 0 && err
!= -ENOENT
)
730 dn
.nid
= le32_to_cpu(rn
->i
.i_nid
[offset
[0] - NODE_DIR1_BLOCK
]);
732 case NODE_DIR1_BLOCK
:
733 case NODE_DIR2_BLOCK
:
734 err
= truncate_dnode(&dn
);
737 case NODE_IND1_BLOCK
:
738 case NODE_IND2_BLOCK
:
739 err
= truncate_nodes(&dn
, nofs
, offset
[1], 2);
742 case NODE_DIND_BLOCK
:
743 err
= truncate_nodes(&dn
, nofs
, offset
[1], 3);
750 if (err
< 0 && err
!= -ENOENT
)
752 if (offset
[1] == 0 &&
753 rn
->i
.i_nid
[offset
[0] - NODE_DIR1_BLOCK
]) {
755 if (page
->mapping
!= node_mapping
) {
756 f2fs_put_page(page
, 1);
759 wait_on_page_writeback(page
);
760 rn
->i
.i_nid
[offset
[0] - NODE_DIR1_BLOCK
] = 0;
761 set_page_dirty(page
);
769 f2fs_put_page(page
, 0);
770 trace_f2fs_truncate_inode_blocks_exit(inode
, err
);
771 return err
> 0 ? 0 : err
;
775 * Caller should grab and release a mutex by calling mutex_lock_op() and
778 int remove_inode_page(struct inode
*inode
)
780 struct f2fs_sb_info
*sbi
= F2FS_SB(inode
->i_sb
);
782 nid_t ino
= inode
->i_ino
;
783 struct dnode_of_data dn
;
785 page
= get_node_page(sbi
, ino
);
787 return PTR_ERR(page
);
789 if (F2FS_I(inode
)->i_xattr_nid
) {
790 nid_t nid
= F2FS_I(inode
)->i_xattr_nid
;
791 struct page
*npage
= get_node_page(sbi
, nid
);
794 return PTR_ERR(npage
);
796 F2FS_I(inode
)->i_xattr_nid
= 0;
797 set_new_dnode(&dn
, inode
, page
, npage
, nid
);
798 dn
.inode_page_locked
= 1;
802 /* 0 is possible, after f2fs_new_inode() is failed */
803 BUG_ON(inode
->i_blocks
!= 0 && inode
->i_blocks
!= 1);
804 set_new_dnode(&dn
, inode
, page
, page
, ino
);
809 struct page
*new_inode_page(struct inode
*inode
, const struct qstr
*name
)
811 struct dnode_of_data dn
;
813 /* allocate inode page for new inode */
814 set_new_dnode(&dn
, inode
, NULL
, NULL
, inode
->i_ino
);
816 /* caller should f2fs_put_page(page, 1); */
817 return new_node_page(&dn
, 0, NULL
);
820 struct page
*new_node_page(struct dnode_of_data
*dn
,
821 unsigned int ofs
, struct page
*ipage
)
823 struct f2fs_sb_info
*sbi
= F2FS_SB(dn
->inode
->i_sb
);
824 struct address_space
*mapping
= sbi
->node_inode
->i_mapping
;
825 struct node_info old_ni
, new_ni
;
829 if (is_inode_flag_set(F2FS_I(dn
->inode
), FI_NO_ALLOC
))
830 return ERR_PTR(-EPERM
);
832 page
= grab_cache_page(mapping
, dn
->nid
);
834 return ERR_PTR(-ENOMEM
);
836 get_node_info(sbi
, dn
->nid
, &old_ni
);
838 SetPageUptodate(page
);
839 fill_node_footer(page
, dn
->nid
, dn
->inode
->i_ino
, ofs
, true);
841 /* Reinitialize old_ni with new node page */
842 BUG_ON(old_ni
.blk_addr
!= NULL_ADDR
);
844 new_ni
.ino
= dn
->inode
->i_ino
;
846 if (!inc_valid_node_count(sbi
, dn
->inode
, 1)) {
850 set_node_addr(sbi
, &new_ni
, NEW_ADDR
);
851 set_cold_node(dn
->inode
, page
);
853 dn
->node_page
= page
;
855 update_inode(dn
->inode
, ipage
);
858 set_page_dirty(page
);
860 inc_valid_inode_count(sbi
);
865 clear_node_page_dirty(page
);
866 f2fs_put_page(page
, 1);
871 * Caller should do after getting the following values.
872 * 0: f2fs_put_page(page, 0)
873 * LOCKED_PAGE: f2fs_put_page(page, 1)
876 static int read_node_page(struct page
*page
, int type
)
878 struct f2fs_sb_info
*sbi
= F2FS_SB(page
->mapping
->host
->i_sb
);
881 get_node_info(sbi
, page
->index
, &ni
);
883 if (ni
.blk_addr
== NULL_ADDR
) {
884 f2fs_put_page(page
, 1);
888 if (PageUptodate(page
))
891 return f2fs_readpage(sbi
, page
, ni
.blk_addr
, type
);
895 * Readahead a node page
897 void ra_node_page(struct f2fs_sb_info
*sbi
, nid_t nid
)
899 struct address_space
*mapping
= sbi
->node_inode
->i_mapping
;
903 apage
= find_get_page(mapping
, nid
);
904 if (apage
&& PageUptodate(apage
)) {
905 f2fs_put_page(apage
, 0);
908 f2fs_put_page(apage
, 0);
910 apage
= grab_cache_page(mapping
, nid
);
914 err
= read_node_page(apage
, READA
);
916 f2fs_put_page(apage
, 0);
917 else if (err
== LOCKED_PAGE
)
918 f2fs_put_page(apage
, 1);
922 struct page
*get_node_page(struct f2fs_sb_info
*sbi
, pgoff_t nid
)
924 struct address_space
*mapping
= sbi
->node_inode
->i_mapping
;
928 page
= grab_cache_page(mapping
, nid
);
930 return ERR_PTR(-ENOMEM
);
932 err
= read_node_page(page
, READ_SYNC
);
935 else if (err
== LOCKED_PAGE
)
939 if (!PageUptodate(page
)) {
940 f2fs_put_page(page
, 1);
941 return ERR_PTR(-EIO
);
943 if (page
->mapping
!= mapping
) {
944 f2fs_put_page(page
, 1);
948 BUG_ON(nid
!= nid_of_node(page
));
949 mark_page_accessed(page
);
954 * Return a locked page for the desired node page.
955 * And, readahead MAX_RA_NODE number of node pages.
957 struct page
*get_node_page_ra(struct page
*parent
, int start
)
959 struct f2fs_sb_info
*sbi
= F2FS_SB(parent
->mapping
->host
->i_sb
);
960 struct address_space
*mapping
= sbi
->node_inode
->i_mapping
;
961 struct blk_plug plug
;
966 /* First, try getting the desired direct node. */
967 nid
= get_nid(parent
, start
, false);
969 return ERR_PTR(-ENOENT
);
971 page
= grab_cache_page(mapping
, nid
);
973 return ERR_PTR(-ENOMEM
);
975 err
= read_node_page(page
, READ_SYNC
);
978 else if (err
== LOCKED_PAGE
)
981 blk_start_plug(&plug
);
983 /* Then, try readahead for siblings of the desired node */
984 end
= start
+ MAX_RA_NODE
;
985 end
= min(end
, NIDS_PER_BLOCK
);
986 for (i
= start
+ 1; i
< end
; i
++) {
987 nid
= get_nid(parent
, i
, false);
990 ra_node_page(sbi
, nid
);
993 blk_finish_plug(&plug
);
996 if (page
->mapping
!= mapping
) {
997 f2fs_put_page(page
, 1);
1001 if (!PageUptodate(page
)) {
1002 f2fs_put_page(page
, 1);
1003 return ERR_PTR(-EIO
);
1005 mark_page_accessed(page
);
1009 void sync_inode_page(struct dnode_of_data
*dn
)
1011 if (IS_INODE(dn
->node_page
) || dn
->inode_page
== dn
->node_page
) {
1012 update_inode(dn
->inode
, dn
->node_page
);
1013 } else if (dn
->inode_page
) {
1014 if (!dn
->inode_page_locked
)
1015 lock_page(dn
->inode_page
);
1016 update_inode(dn
->inode
, dn
->inode_page
);
1017 if (!dn
->inode_page_locked
)
1018 unlock_page(dn
->inode_page
);
1020 update_inode_page(dn
->inode
);
1024 int sync_node_pages(struct f2fs_sb_info
*sbi
, nid_t ino
,
1025 struct writeback_control
*wbc
)
1027 struct address_space
*mapping
= sbi
->node_inode
->i_mapping
;
1029 struct pagevec pvec
;
1030 int step
= ino
? 2 : 0;
1031 int nwritten
= 0, wrote
= 0;
1033 pagevec_init(&pvec
, 0);
1039 while (index
<= end
) {
1041 nr_pages
= pagevec_lookup_tag(&pvec
, mapping
, &index
,
1042 PAGECACHE_TAG_DIRTY
,
1043 min(end
- index
, (pgoff_t
)PAGEVEC_SIZE
-1) + 1);
1047 for (i
= 0; i
< nr_pages
; i
++) {
1048 struct page
*page
= pvec
.pages
[i
];
1051 * flushing sequence with step:
1056 if (step
== 0 && IS_DNODE(page
))
1058 if (step
== 1 && (!IS_DNODE(page
) ||
1059 is_cold_node(page
)))
1061 if (step
== 2 && (!IS_DNODE(page
) ||
1062 !is_cold_node(page
)))
1067 * we should not skip writing node pages.
1069 if (ino
&& ino_of_node(page
) == ino
)
1071 else if (!trylock_page(page
))
1074 if (unlikely(page
->mapping
!= mapping
)) {
1079 if (ino
&& ino_of_node(page
) != ino
)
1080 goto continue_unlock
;
1082 if (!PageDirty(page
)) {
1083 /* someone wrote it for us */
1084 goto continue_unlock
;
1087 if (!clear_page_dirty_for_io(page
))
1088 goto continue_unlock
;
1090 /* called by fsync() */
1091 if (ino
&& IS_DNODE(page
)) {
1092 int mark
= !is_checkpointed_node(sbi
, ino
);
1093 set_fsync_mark(page
, 1);
1095 set_dentry_mark(page
, mark
);
1098 set_fsync_mark(page
, 0);
1099 set_dentry_mark(page
, 0);
1101 mapping
->a_ops
->writepage(page
, wbc
);
1104 if (--wbc
->nr_to_write
== 0)
1107 pagevec_release(&pvec
);
1110 if (wbc
->nr_to_write
== 0) {
1122 f2fs_submit_bio(sbi
, NODE
, wbc
->sync_mode
== WB_SYNC_ALL
);
1127 static int f2fs_write_node_page(struct page
*page
,
1128 struct writeback_control
*wbc
)
1130 struct f2fs_sb_info
*sbi
= F2FS_SB(page
->mapping
->host
->i_sb
);
1133 struct node_info ni
;
1135 wait_on_page_writeback(page
);
1137 /* get old block addr of this node page */
1138 nid
= nid_of_node(page
);
1139 BUG_ON(page
->index
!= nid
);
1141 get_node_info(sbi
, nid
, &ni
);
1143 /* This page is already truncated */
1144 if (ni
.blk_addr
== NULL_ADDR
) {
1145 dec_page_count(sbi
, F2FS_DIRTY_NODES
);
1150 if (wbc
->for_reclaim
) {
1151 dec_page_count(sbi
, F2FS_DIRTY_NODES
);
1152 wbc
->pages_skipped
++;
1153 set_page_dirty(page
);
1154 return AOP_WRITEPAGE_ACTIVATE
;
1157 mutex_lock(&sbi
->node_write
);
1158 set_page_writeback(page
);
1159 write_node_page(sbi
, page
, nid
, ni
.blk_addr
, &new_addr
);
1160 set_node_addr(sbi
, &ni
, new_addr
);
1161 dec_page_count(sbi
, F2FS_DIRTY_NODES
);
1162 mutex_unlock(&sbi
->node_write
);
1168 * It is very important to gather dirty pages and write at once, so that we can
1169 * submit a big bio without interfering other data writes.
1170 * Be default, 512 pages (2MB), a segment size, is quite reasonable.
1172 #define COLLECT_DIRTY_NODES 512
1173 static int f2fs_write_node_pages(struct address_space
*mapping
,
1174 struct writeback_control
*wbc
)
1176 struct f2fs_sb_info
*sbi
= F2FS_SB(mapping
->host
->i_sb
);
1177 long nr_to_write
= wbc
->nr_to_write
;
1179 /* First check balancing cached NAT entries */
1180 if (try_to_free_nats(sbi
, NAT_ENTRY_PER_BLOCK
)) {
1181 f2fs_sync_fs(sbi
->sb
, true);
1185 /* collect a number of dirty node pages and write together */
1186 if (get_pages(sbi
, F2FS_DIRTY_NODES
) < COLLECT_DIRTY_NODES
)
1189 /* if mounting is failed, skip writing node pages */
1190 wbc
->nr_to_write
= max_hw_blocks(sbi
);
1191 sync_node_pages(sbi
, 0, wbc
);
1192 wbc
->nr_to_write
= nr_to_write
- (max_hw_blocks(sbi
) - wbc
->nr_to_write
);
1196 static int f2fs_set_node_page_dirty(struct page
*page
)
1198 struct address_space
*mapping
= page
->mapping
;
1199 struct f2fs_sb_info
*sbi
= F2FS_SB(mapping
->host
->i_sb
);
1201 SetPageUptodate(page
);
1202 if (!PageDirty(page
)) {
1203 __set_page_dirty_nobuffers(page
);
1204 inc_page_count(sbi
, F2FS_DIRTY_NODES
);
1205 SetPagePrivate(page
);
1211 static void f2fs_invalidate_node_page(struct page
*page
, unsigned int offset
,
1212 unsigned int length
)
1214 struct inode
*inode
= page
->mapping
->host
;
1215 struct f2fs_sb_info
*sbi
= F2FS_SB(inode
->i_sb
);
1216 if (PageDirty(page
))
1217 dec_page_count(sbi
, F2FS_DIRTY_NODES
);
1218 ClearPagePrivate(page
);
1221 static int f2fs_release_node_page(struct page
*page
, gfp_t wait
)
1223 ClearPagePrivate(page
);
1228 * Structure of the f2fs node operations
1230 const struct address_space_operations f2fs_node_aops
= {
1231 .writepage
= f2fs_write_node_page
,
1232 .writepages
= f2fs_write_node_pages
,
1233 .set_page_dirty
= f2fs_set_node_page_dirty
,
1234 .invalidatepage
= f2fs_invalidate_node_page
,
1235 .releasepage
= f2fs_release_node_page
,
1238 static struct free_nid
*__lookup_free_nid_list(nid_t n
, struct list_head
*head
)
1240 struct list_head
*this;
1242 list_for_each(this, head
) {
1243 i
= list_entry(this, struct free_nid
, list
);
1250 static void __del_from_free_nid_list(struct free_nid
*i
)
1253 kmem_cache_free(free_nid_slab
, i
);
1256 static int add_free_nid(struct f2fs_nm_info
*nm_i
, nid_t nid
, bool build
)
1259 struct nat_entry
*ne
;
1260 bool allocated
= false;
1262 if (nm_i
->fcnt
> 2 * MAX_FREE_NIDS
)
1265 /* 0 nid should not be used */
1272 /* do not add allocated nids */
1273 read_lock(&nm_i
->nat_tree_lock
);
1274 ne
= __lookup_nat_cache(nm_i
, nid
);
1275 if (ne
&& nat_get_blkaddr(ne
) != NULL_ADDR
)
1277 read_unlock(&nm_i
->nat_tree_lock
);
1281 i
= kmem_cache_alloc(free_nid_slab
, GFP_NOFS
);
1289 spin_lock(&nm_i
->free_nid_list_lock
);
1290 if (__lookup_free_nid_list(nid
, &nm_i
->free_nid_list
)) {
1291 spin_unlock(&nm_i
->free_nid_list_lock
);
1292 kmem_cache_free(free_nid_slab
, i
);
1295 list_add_tail(&i
->list
, &nm_i
->free_nid_list
);
1297 spin_unlock(&nm_i
->free_nid_list_lock
);
1301 static void remove_free_nid(struct f2fs_nm_info
*nm_i
, nid_t nid
)
1304 spin_lock(&nm_i
->free_nid_list_lock
);
1305 i
= __lookup_free_nid_list(nid
, &nm_i
->free_nid_list
);
1306 if (i
&& i
->state
== NID_NEW
) {
1307 __del_from_free_nid_list(i
);
1310 spin_unlock(&nm_i
->free_nid_list_lock
);
1313 static void scan_nat_page(struct f2fs_nm_info
*nm_i
,
1314 struct page
*nat_page
, nid_t start_nid
)
1316 struct f2fs_nat_block
*nat_blk
= page_address(nat_page
);
1320 i
= start_nid
% NAT_ENTRY_PER_BLOCK
;
1322 for (; i
< NAT_ENTRY_PER_BLOCK
; i
++, start_nid
++) {
1324 if (start_nid
>= nm_i
->max_nid
)
1327 blk_addr
= le32_to_cpu(nat_blk
->entries
[i
].block_addr
);
1328 BUG_ON(blk_addr
== NEW_ADDR
);
1329 if (blk_addr
== NULL_ADDR
) {
1330 if (add_free_nid(nm_i
, start_nid
, true) < 0)
1336 static void build_free_nids(struct f2fs_sb_info
*sbi
)
1338 struct f2fs_nm_info
*nm_i
= NM_I(sbi
);
1339 struct curseg_info
*curseg
= CURSEG_I(sbi
, CURSEG_HOT_DATA
);
1340 struct f2fs_summary_block
*sum
= curseg
->sum_blk
;
1342 nid_t nid
= nm_i
->next_scan_nid
;
1344 /* Enough entries */
1345 if (nm_i
->fcnt
> NAT_ENTRY_PER_BLOCK
)
1348 /* readahead nat pages to be scanned */
1349 ra_nat_pages(sbi
, nid
);
1352 struct page
*page
= get_current_nat_page(sbi
, nid
);
1354 scan_nat_page(nm_i
, page
, nid
);
1355 f2fs_put_page(page
, 1);
1357 nid
+= (NAT_ENTRY_PER_BLOCK
- (nid
% NAT_ENTRY_PER_BLOCK
));
1358 if (nid
>= nm_i
->max_nid
)
1361 if (i
++ == FREE_NID_PAGES
)
1365 /* go to the next free nat pages to find free nids abundantly */
1366 nm_i
->next_scan_nid
= nid
;
1368 /* find free nids from current sum_pages */
1369 mutex_lock(&curseg
->curseg_mutex
);
1370 for (i
= 0; i
< nats_in_cursum(sum
); i
++) {
1371 block_t addr
= le32_to_cpu(nat_in_journal(sum
, i
).block_addr
);
1372 nid
= le32_to_cpu(nid_in_journal(sum
, i
));
1373 if (addr
== NULL_ADDR
)
1374 add_free_nid(nm_i
, nid
, true);
1376 remove_free_nid(nm_i
, nid
);
1378 mutex_unlock(&curseg
->curseg_mutex
);
1382 * If this function returns success, caller can obtain a new nid
1383 * from second parameter of this function.
1384 * The returned nid could be used ino as well as nid when inode is created.
1386 bool alloc_nid(struct f2fs_sb_info
*sbi
, nid_t
*nid
)
1388 struct f2fs_nm_info
*nm_i
= NM_I(sbi
);
1389 struct free_nid
*i
= NULL
;
1390 struct list_head
*this;
1392 if (sbi
->total_valid_node_count
+ 1 >= nm_i
->max_nid
)
1395 spin_lock(&nm_i
->free_nid_list_lock
);
1397 /* We should not use stale free nids created by build_free_nids */
1398 if (nm_i
->fcnt
&& !sbi
->on_build_free_nids
) {
1399 BUG_ON(list_empty(&nm_i
->free_nid_list
));
1400 list_for_each(this, &nm_i
->free_nid_list
) {
1401 i
= list_entry(this, struct free_nid
, list
);
1402 if (i
->state
== NID_NEW
)
1406 BUG_ON(i
->state
!= NID_NEW
);
1408 i
->state
= NID_ALLOC
;
1410 spin_unlock(&nm_i
->free_nid_list_lock
);
1413 spin_unlock(&nm_i
->free_nid_list_lock
);
1415 /* Let's scan nat pages and its caches to get free nids */
1416 mutex_lock(&nm_i
->build_lock
);
1417 sbi
->on_build_free_nids
= 1;
1418 build_free_nids(sbi
);
1419 sbi
->on_build_free_nids
= 0;
1420 mutex_unlock(&nm_i
->build_lock
);
1425 * alloc_nid() should be called prior to this function.
1427 void alloc_nid_done(struct f2fs_sb_info
*sbi
, nid_t nid
)
1429 struct f2fs_nm_info
*nm_i
= NM_I(sbi
);
1432 spin_lock(&nm_i
->free_nid_list_lock
);
1433 i
= __lookup_free_nid_list(nid
, &nm_i
->free_nid_list
);
1434 BUG_ON(!i
|| i
->state
!= NID_ALLOC
);
1435 __del_from_free_nid_list(i
);
1436 spin_unlock(&nm_i
->free_nid_list_lock
);
1440 * alloc_nid() should be called prior to this function.
1442 void alloc_nid_failed(struct f2fs_sb_info
*sbi
, nid_t nid
)
1444 struct f2fs_nm_info
*nm_i
= NM_I(sbi
);
1447 spin_lock(&nm_i
->free_nid_list_lock
);
1448 i
= __lookup_free_nid_list(nid
, &nm_i
->free_nid_list
);
1449 BUG_ON(!i
|| i
->state
!= NID_ALLOC
);
1450 if (nm_i
->fcnt
> 2 * MAX_FREE_NIDS
) {
1451 __del_from_free_nid_list(i
);
1456 spin_unlock(&nm_i
->free_nid_list_lock
);
1459 void recover_node_page(struct f2fs_sb_info
*sbi
, struct page
*page
,
1460 struct f2fs_summary
*sum
, struct node_info
*ni
,
1461 block_t new_blkaddr
)
1463 rewrite_node_page(sbi
, page
, sum
, ni
->blk_addr
, new_blkaddr
);
1464 set_node_addr(sbi
, ni
, new_blkaddr
);
1465 clear_node_page_dirty(page
);
1468 int recover_inode_page(struct f2fs_sb_info
*sbi
, struct page
*page
)
1470 struct address_space
*mapping
= sbi
->node_inode
->i_mapping
;
1471 struct f2fs_node
*src
, *dst
;
1472 nid_t ino
= ino_of_node(page
);
1473 struct node_info old_ni
, new_ni
;
1476 ipage
= grab_cache_page(mapping
, ino
);
1480 /* Should not use this inode from free nid list */
1481 remove_free_nid(NM_I(sbi
), ino
);
1483 get_node_info(sbi
, ino
, &old_ni
);
1484 SetPageUptodate(ipage
);
1485 fill_node_footer(ipage
, ino
, ino
, 0, true);
1487 src
= (struct f2fs_node
*)page_address(page
);
1488 dst
= (struct f2fs_node
*)page_address(ipage
);
1490 memcpy(dst
, src
, (unsigned long)&src
->i
.i_ext
- (unsigned long)&src
->i
);
1492 dst
->i
.i_blocks
= cpu_to_le64(1);
1493 dst
->i
.i_links
= cpu_to_le32(1);
1494 dst
->i
.i_xattr_nid
= 0;
1499 if (!inc_valid_node_count(sbi
, NULL
, 1))
1501 set_node_addr(sbi
, &new_ni
, NEW_ADDR
);
1502 inc_valid_inode_count(sbi
);
1503 f2fs_put_page(ipage
, 1);
1507 int restore_node_summary(struct f2fs_sb_info
*sbi
,
1508 unsigned int segno
, struct f2fs_summary_block
*sum
)
1510 struct f2fs_node
*rn
;
1511 struct f2fs_summary
*sum_entry
;
1516 /* alloc temporal page for read node */
1517 page
= alloc_page(GFP_NOFS
| __GFP_ZERO
);
1519 return PTR_ERR(page
);
1522 /* scan the node segment */
1523 last_offset
= sbi
->blocks_per_seg
;
1524 addr
= START_BLOCK(sbi
, segno
);
1525 sum_entry
= &sum
->entries
[0];
1527 for (i
= 0; i
< last_offset
; i
++, sum_entry
++) {
1529 * In order to read next node page,
1530 * we must clear PageUptodate flag.
1532 ClearPageUptodate(page
);
1534 if (f2fs_readpage(sbi
, page
, addr
, READ_SYNC
))
1538 rn
= (struct f2fs_node
*)page_address(page
);
1539 sum_entry
->nid
= rn
->footer
.nid
;
1540 sum_entry
->version
= 0;
1541 sum_entry
->ofs_in_node
= 0;
1546 __free_pages(page
, 0);
1550 static bool flush_nats_in_journal(struct f2fs_sb_info
*sbi
)
1552 struct f2fs_nm_info
*nm_i
= NM_I(sbi
);
1553 struct curseg_info
*curseg
= CURSEG_I(sbi
, CURSEG_HOT_DATA
);
1554 struct f2fs_summary_block
*sum
= curseg
->sum_blk
;
1557 mutex_lock(&curseg
->curseg_mutex
);
1559 if (nats_in_cursum(sum
) < NAT_JOURNAL_ENTRIES
) {
1560 mutex_unlock(&curseg
->curseg_mutex
);
1564 for (i
= 0; i
< nats_in_cursum(sum
); i
++) {
1565 struct nat_entry
*ne
;
1566 struct f2fs_nat_entry raw_ne
;
1567 nid_t nid
= le32_to_cpu(nid_in_journal(sum
, i
));
1569 raw_ne
= nat_in_journal(sum
, i
);
1571 write_lock(&nm_i
->nat_tree_lock
);
1572 ne
= __lookup_nat_cache(nm_i
, nid
);
1574 __set_nat_cache_dirty(nm_i
, ne
);
1575 write_unlock(&nm_i
->nat_tree_lock
);
1578 ne
= grab_nat_entry(nm_i
, nid
);
1580 write_unlock(&nm_i
->nat_tree_lock
);
1583 nat_set_blkaddr(ne
, le32_to_cpu(raw_ne
.block_addr
));
1584 nat_set_ino(ne
, le32_to_cpu(raw_ne
.ino
));
1585 nat_set_version(ne
, raw_ne
.version
);
1586 __set_nat_cache_dirty(nm_i
, ne
);
1587 write_unlock(&nm_i
->nat_tree_lock
);
1589 update_nats_in_cursum(sum
, -i
);
1590 mutex_unlock(&curseg
->curseg_mutex
);
1595 * This function is called during the checkpointing process.
1597 void flush_nat_entries(struct f2fs_sb_info
*sbi
)
1599 struct f2fs_nm_info
*nm_i
= NM_I(sbi
);
1600 struct curseg_info
*curseg
= CURSEG_I(sbi
, CURSEG_HOT_DATA
);
1601 struct f2fs_summary_block
*sum
= curseg
->sum_blk
;
1602 struct list_head
*cur
, *n
;
1603 struct page
*page
= NULL
;
1604 struct f2fs_nat_block
*nat_blk
= NULL
;
1605 nid_t start_nid
= 0, end_nid
= 0;
1608 flushed
= flush_nats_in_journal(sbi
);
1611 mutex_lock(&curseg
->curseg_mutex
);
1613 /* 1) flush dirty nat caches */
1614 list_for_each_safe(cur
, n
, &nm_i
->dirty_nat_entries
) {
1615 struct nat_entry
*ne
;
1617 struct f2fs_nat_entry raw_ne
;
1619 block_t new_blkaddr
;
1621 ne
= list_entry(cur
, struct nat_entry
, list
);
1622 nid
= nat_get_nid(ne
);
1624 if (nat_get_blkaddr(ne
) == NEW_ADDR
)
1629 /* if there is room for nat enries in curseg->sumpage */
1630 offset
= lookup_journal_in_cursum(sum
, NAT_JOURNAL
, nid
, 1);
1632 raw_ne
= nat_in_journal(sum
, offset
);
1636 if (!page
|| (start_nid
> nid
|| nid
> end_nid
)) {
1638 f2fs_put_page(page
, 1);
1641 start_nid
= START_NID(nid
);
1642 end_nid
= start_nid
+ NAT_ENTRY_PER_BLOCK
- 1;
1645 * get nat block with dirty flag, increased reference
1646 * count, mapped and lock
1648 page
= get_next_nat_page(sbi
, start_nid
);
1649 nat_blk
= page_address(page
);
1653 raw_ne
= nat_blk
->entries
[nid
- start_nid
];
1655 new_blkaddr
= nat_get_blkaddr(ne
);
1657 raw_ne
.ino
= cpu_to_le32(nat_get_ino(ne
));
1658 raw_ne
.block_addr
= cpu_to_le32(new_blkaddr
);
1659 raw_ne
.version
= nat_get_version(ne
);
1662 nat_blk
->entries
[nid
- start_nid
] = raw_ne
;
1664 nat_in_journal(sum
, offset
) = raw_ne
;
1665 nid_in_journal(sum
, offset
) = cpu_to_le32(nid
);
1668 if (nat_get_blkaddr(ne
) == NULL_ADDR
&&
1669 add_free_nid(NM_I(sbi
), nid
, false) <= 0) {
1670 write_lock(&nm_i
->nat_tree_lock
);
1671 __del_from_nat_cache(nm_i
, ne
);
1672 write_unlock(&nm_i
->nat_tree_lock
);
1674 write_lock(&nm_i
->nat_tree_lock
);
1675 __clear_nat_cache_dirty(nm_i
, ne
);
1676 ne
->checkpointed
= true;
1677 write_unlock(&nm_i
->nat_tree_lock
);
1681 mutex_unlock(&curseg
->curseg_mutex
);
1682 f2fs_put_page(page
, 1);
1684 /* 2) shrink nat caches if necessary */
1685 try_to_free_nats(sbi
, nm_i
->nat_cnt
- NM_WOUT_THRESHOLD
);
1688 static int init_node_manager(struct f2fs_sb_info
*sbi
)
1690 struct f2fs_super_block
*sb_raw
= F2FS_RAW_SUPER(sbi
);
1691 struct f2fs_nm_info
*nm_i
= NM_I(sbi
);
1692 unsigned char *version_bitmap
;
1693 unsigned int nat_segs
, nat_blocks
;
1695 nm_i
->nat_blkaddr
= le32_to_cpu(sb_raw
->nat_blkaddr
);
1697 /* segment_count_nat includes pair segment so divide to 2. */
1698 nat_segs
= le32_to_cpu(sb_raw
->segment_count_nat
) >> 1;
1699 nat_blocks
= nat_segs
<< le32_to_cpu(sb_raw
->log_blocks_per_seg
);
1700 nm_i
->max_nid
= NAT_ENTRY_PER_BLOCK
* nat_blocks
;
1704 INIT_LIST_HEAD(&nm_i
->free_nid_list
);
1705 INIT_RADIX_TREE(&nm_i
->nat_root
, GFP_ATOMIC
);
1706 INIT_LIST_HEAD(&nm_i
->nat_entries
);
1707 INIT_LIST_HEAD(&nm_i
->dirty_nat_entries
);
1709 mutex_init(&nm_i
->build_lock
);
1710 spin_lock_init(&nm_i
->free_nid_list_lock
);
1711 rwlock_init(&nm_i
->nat_tree_lock
);
1713 nm_i
->next_scan_nid
= le32_to_cpu(sbi
->ckpt
->next_free_nid
);
1714 nm_i
->bitmap_size
= __bitmap_size(sbi
, NAT_BITMAP
);
1715 version_bitmap
= __bitmap_ptr(sbi
, NAT_BITMAP
);
1716 if (!version_bitmap
)
1719 nm_i
->nat_bitmap
= kmemdup(version_bitmap
, nm_i
->bitmap_size
,
1721 if (!nm_i
->nat_bitmap
)
1726 int build_node_manager(struct f2fs_sb_info
*sbi
)
1730 sbi
->nm_info
= kzalloc(sizeof(struct f2fs_nm_info
), GFP_KERNEL
);
1734 err
= init_node_manager(sbi
);
1738 build_free_nids(sbi
);
1742 void destroy_node_manager(struct f2fs_sb_info
*sbi
)
1744 struct f2fs_nm_info
*nm_i
= NM_I(sbi
);
1745 struct free_nid
*i
, *next_i
;
1746 struct nat_entry
*natvec
[NATVEC_SIZE
];
1753 /* destroy free nid list */
1754 spin_lock(&nm_i
->free_nid_list_lock
);
1755 list_for_each_entry_safe(i
, next_i
, &nm_i
->free_nid_list
, list
) {
1756 BUG_ON(i
->state
== NID_ALLOC
);
1757 __del_from_free_nid_list(i
);
1761 spin_unlock(&nm_i
->free_nid_list_lock
);
1763 /* destroy nat cache */
1764 write_lock(&nm_i
->nat_tree_lock
);
1765 while ((found
= __gang_lookup_nat_cache(nm_i
,
1766 nid
, NATVEC_SIZE
, natvec
))) {
1768 for (idx
= 0; idx
< found
; idx
++) {
1769 struct nat_entry
*e
= natvec
[idx
];
1770 nid
= nat_get_nid(e
) + 1;
1771 __del_from_nat_cache(nm_i
, e
);
1774 BUG_ON(nm_i
->nat_cnt
);
1775 write_unlock(&nm_i
->nat_tree_lock
);
1777 kfree(nm_i
->nat_bitmap
);
1778 sbi
->nm_info
= NULL
;
1782 int __init
create_node_manager_caches(void)
1784 nat_entry_slab
= f2fs_kmem_cache_create("nat_entry",
1785 sizeof(struct nat_entry
), NULL
);
1786 if (!nat_entry_slab
)
1789 free_nid_slab
= f2fs_kmem_cache_create("free_nid",
1790 sizeof(struct free_nid
), NULL
);
1791 if (!free_nid_slab
) {
1792 kmem_cache_destroy(nat_entry_slab
);
1798 void destroy_node_manager_caches(void)
1800 kmem_cache_destroy(free_nid_slab
);
1801 kmem_cache_destroy(nat_entry_slab
);