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/bio.h>
14 #include <linux/blkdev.h>
15 #include <linux/prefetch.h>
16 #include <linux/vmalloc.h>
21 #include <trace/events/f2fs.h>
24 * This function balances dirty node and dentry pages.
25 * In addition, it controls garbage collection.
27 void f2fs_balance_fs(struct f2fs_sb_info
*sbi
)
30 * We should do GC or end up with checkpoint, if there are so many dirty
31 * dir/node pages without enough free segments.
33 if (has_not_enough_free_secs(sbi
, 0)) {
34 mutex_lock(&sbi
->gc_mutex
);
39 void f2fs_balance_fs_bg(struct f2fs_sb_info
*sbi
)
41 /* check the # of cached NAT entries and prefree segments */
42 if (try_to_free_nats(sbi
, NAT_ENTRY_PER_BLOCK
) ||
43 excess_prefree_segs(sbi
))
44 f2fs_sync_fs(sbi
->sb
, true);
47 static void __locate_dirty_segment(struct f2fs_sb_info
*sbi
, unsigned int segno
,
48 enum dirty_type dirty_type
)
50 struct dirty_seglist_info
*dirty_i
= DIRTY_I(sbi
);
52 /* need not be added */
53 if (IS_CURSEG(sbi
, segno
))
56 if (!test_and_set_bit(segno
, dirty_i
->dirty_segmap
[dirty_type
]))
57 dirty_i
->nr_dirty
[dirty_type
]++;
59 if (dirty_type
== DIRTY
) {
60 struct seg_entry
*sentry
= get_seg_entry(sbi
, segno
);
61 enum dirty_type t
= sentry
->type
;
63 if (!test_and_set_bit(segno
, dirty_i
->dirty_segmap
[t
]))
64 dirty_i
->nr_dirty
[t
]++;
68 static void __remove_dirty_segment(struct f2fs_sb_info
*sbi
, unsigned int segno
,
69 enum dirty_type dirty_type
)
71 struct dirty_seglist_info
*dirty_i
= DIRTY_I(sbi
);
73 if (test_and_clear_bit(segno
, dirty_i
->dirty_segmap
[dirty_type
]))
74 dirty_i
->nr_dirty
[dirty_type
]--;
76 if (dirty_type
== DIRTY
) {
77 struct seg_entry
*sentry
= get_seg_entry(sbi
, segno
);
78 enum dirty_type t
= sentry
->type
;
80 if (test_and_clear_bit(segno
, dirty_i
->dirty_segmap
[t
]))
81 dirty_i
->nr_dirty
[t
]--;
83 if (get_valid_blocks(sbi
, segno
, sbi
->segs_per_sec
) == 0)
84 clear_bit(GET_SECNO(sbi
, segno
),
85 dirty_i
->victim_secmap
);
90 * Should not occur error such as -ENOMEM.
91 * Adding dirty entry into seglist is not critical operation.
92 * If a given segment is one of current working segments, it won't be added.
94 static void locate_dirty_segment(struct f2fs_sb_info
*sbi
, unsigned int segno
)
96 struct dirty_seglist_info
*dirty_i
= DIRTY_I(sbi
);
97 unsigned short valid_blocks
;
99 if (segno
== NULL_SEGNO
|| IS_CURSEG(sbi
, segno
))
102 mutex_lock(&dirty_i
->seglist_lock
);
104 valid_blocks
= get_valid_blocks(sbi
, segno
, 0);
106 if (valid_blocks
== 0) {
107 __locate_dirty_segment(sbi
, segno
, PRE
);
108 __remove_dirty_segment(sbi
, segno
, DIRTY
);
109 } else if (valid_blocks
< sbi
->blocks_per_seg
) {
110 __locate_dirty_segment(sbi
, segno
, DIRTY
);
112 /* Recovery routine with SSR needs this */
113 __remove_dirty_segment(sbi
, segno
, DIRTY
);
116 mutex_unlock(&dirty_i
->seglist_lock
);
120 * Should call clear_prefree_segments after checkpoint is done.
122 static void set_prefree_as_free_segments(struct f2fs_sb_info
*sbi
)
124 struct dirty_seglist_info
*dirty_i
= DIRTY_I(sbi
);
125 unsigned int segno
= -1;
126 unsigned int total_segs
= TOTAL_SEGS(sbi
);
128 mutex_lock(&dirty_i
->seglist_lock
);
130 segno
= find_next_bit(dirty_i
->dirty_segmap
[PRE
], total_segs
,
132 if (segno
>= total_segs
)
134 __set_test_and_free(sbi
, segno
);
136 mutex_unlock(&dirty_i
->seglist_lock
);
139 void clear_prefree_segments(struct f2fs_sb_info
*sbi
)
141 struct dirty_seglist_info
*dirty_i
= DIRTY_I(sbi
);
142 unsigned int segno
= -1;
143 unsigned int total_segs
= TOTAL_SEGS(sbi
);
145 mutex_lock(&dirty_i
->seglist_lock
);
147 segno
= find_next_bit(dirty_i
->dirty_segmap
[PRE
], total_segs
,
149 if (segno
>= total_segs
)
152 if (test_and_clear_bit(segno
, dirty_i
->dirty_segmap
[PRE
]))
153 dirty_i
->nr_dirty
[PRE
]--;
156 if (test_opt(sbi
, DISCARD
))
157 blkdev_issue_discard(sbi
->sb
->s_bdev
,
158 START_BLOCK(sbi
, segno
) <<
159 sbi
->log_sectors_per_block
,
160 1 << (sbi
->log_sectors_per_block
+
161 sbi
->log_blocks_per_seg
),
164 mutex_unlock(&dirty_i
->seglist_lock
);
167 static void __mark_sit_entry_dirty(struct f2fs_sb_info
*sbi
, unsigned int segno
)
169 struct sit_info
*sit_i
= SIT_I(sbi
);
170 if (!__test_and_set_bit(segno
, sit_i
->dirty_sentries_bitmap
))
171 sit_i
->dirty_sentries
++;
174 static void __set_sit_entry_type(struct f2fs_sb_info
*sbi
, int type
,
175 unsigned int segno
, int modified
)
177 struct seg_entry
*se
= get_seg_entry(sbi
, segno
);
180 __mark_sit_entry_dirty(sbi
, segno
);
183 static void update_sit_entry(struct f2fs_sb_info
*sbi
, block_t blkaddr
, int del
)
185 struct seg_entry
*se
;
186 unsigned int segno
, offset
;
187 long int new_vblocks
;
189 segno
= GET_SEGNO(sbi
, blkaddr
);
191 se
= get_seg_entry(sbi
, segno
);
192 new_vblocks
= se
->valid_blocks
+ del
;
193 offset
= GET_SEGOFF_FROM_SEG0(sbi
, blkaddr
) & (sbi
->blocks_per_seg
- 1);
195 BUG_ON((new_vblocks
>> (sizeof(unsigned short) << 3) ||
196 (new_vblocks
> sbi
->blocks_per_seg
)));
198 se
->valid_blocks
= new_vblocks
;
199 se
->mtime
= get_mtime(sbi
);
200 SIT_I(sbi
)->max_mtime
= se
->mtime
;
202 /* Update valid block bitmap */
204 if (f2fs_set_bit(offset
, se
->cur_valid_map
))
207 if (!f2fs_clear_bit(offset
, se
->cur_valid_map
))
210 if (!f2fs_test_bit(offset
, se
->ckpt_valid_map
))
211 se
->ckpt_valid_blocks
+= del
;
213 __mark_sit_entry_dirty(sbi
, segno
);
215 /* update total number of valid blocks to be written in ckpt area */
216 SIT_I(sbi
)->written_valid_blocks
+= del
;
218 if (sbi
->segs_per_sec
> 1)
219 get_sec_entry(sbi
, segno
)->valid_blocks
+= del
;
222 static void refresh_sit_entry(struct f2fs_sb_info
*sbi
,
223 block_t old_blkaddr
, block_t new_blkaddr
)
225 update_sit_entry(sbi
, new_blkaddr
, 1);
226 if (GET_SEGNO(sbi
, old_blkaddr
) != NULL_SEGNO
)
227 update_sit_entry(sbi
, old_blkaddr
, -1);
230 void invalidate_blocks(struct f2fs_sb_info
*sbi
, block_t addr
)
232 unsigned int segno
= GET_SEGNO(sbi
, addr
);
233 struct sit_info
*sit_i
= SIT_I(sbi
);
235 BUG_ON(addr
== NULL_ADDR
);
236 if (addr
== NEW_ADDR
)
239 /* add it into sit main buffer */
240 mutex_lock(&sit_i
->sentry_lock
);
242 update_sit_entry(sbi
, addr
, -1);
244 /* add it into dirty seglist */
245 locate_dirty_segment(sbi
, segno
);
247 mutex_unlock(&sit_i
->sentry_lock
);
251 * This function should be resided under the curseg_mutex lock
253 static void __add_sum_entry(struct f2fs_sb_info
*sbi
, int type
,
254 struct f2fs_summary
*sum
)
256 struct curseg_info
*curseg
= CURSEG_I(sbi
, type
);
257 void *addr
= curseg
->sum_blk
;
258 addr
+= curseg
->next_blkoff
* sizeof(struct f2fs_summary
);
259 memcpy(addr
, sum
, sizeof(struct f2fs_summary
));
263 * Calculate the number of current summary pages for writing
265 int npages_for_summary_flush(struct f2fs_sb_info
*sbi
)
267 int total_size_bytes
= 0;
268 int valid_sum_count
= 0;
271 for (i
= CURSEG_HOT_DATA
; i
<= CURSEG_COLD_DATA
; i
++) {
272 if (sbi
->ckpt
->alloc_type
[i
] == SSR
)
273 valid_sum_count
+= sbi
->blocks_per_seg
;
275 valid_sum_count
+= curseg_blkoff(sbi
, i
);
278 total_size_bytes
= valid_sum_count
* (SUMMARY_SIZE
+ 1)
279 + sizeof(struct nat_journal
) + 2
280 + sizeof(struct sit_journal
) + 2;
281 sum_space
= PAGE_CACHE_SIZE
- SUM_FOOTER_SIZE
;
282 if (total_size_bytes
< sum_space
)
284 else if (total_size_bytes
< 2 * sum_space
)
290 * Caller should put this summary page
292 struct page
*get_sum_page(struct f2fs_sb_info
*sbi
, unsigned int segno
)
294 return get_meta_page(sbi
, GET_SUM_BLOCK(sbi
, segno
));
297 static void write_sum_page(struct f2fs_sb_info
*sbi
,
298 struct f2fs_summary_block
*sum_blk
, block_t blk_addr
)
300 struct page
*page
= grab_meta_page(sbi
, blk_addr
);
301 void *kaddr
= page_address(page
);
302 memcpy(kaddr
, sum_blk
, PAGE_CACHE_SIZE
);
303 set_page_dirty(page
);
304 f2fs_put_page(page
, 1);
307 static int is_next_segment_free(struct f2fs_sb_info
*sbi
, int type
)
309 struct curseg_info
*curseg
= CURSEG_I(sbi
, type
);
310 unsigned int segno
= curseg
->segno
+ 1;
311 struct free_segmap_info
*free_i
= FREE_I(sbi
);
313 if (segno
< TOTAL_SEGS(sbi
) && segno
% sbi
->segs_per_sec
)
314 return !test_bit(segno
, free_i
->free_segmap
);
319 * Find a new segment from the free segments bitmap to right order
320 * This function should be returned with success, otherwise BUG
322 static void get_new_segment(struct f2fs_sb_info
*sbi
,
323 unsigned int *newseg
, bool new_sec
, int dir
)
325 struct free_segmap_info
*free_i
= FREE_I(sbi
);
326 unsigned int segno
, secno
, zoneno
;
327 unsigned int total_zones
= TOTAL_SECS(sbi
) / sbi
->secs_per_zone
;
328 unsigned int hint
= *newseg
/ sbi
->segs_per_sec
;
329 unsigned int old_zoneno
= GET_ZONENO_FROM_SEGNO(sbi
, *newseg
);
330 unsigned int left_start
= hint
;
335 write_lock(&free_i
->segmap_lock
);
337 if (!new_sec
&& ((*newseg
+ 1) % sbi
->segs_per_sec
)) {
338 segno
= find_next_zero_bit(free_i
->free_segmap
,
339 TOTAL_SEGS(sbi
), *newseg
+ 1);
340 if (segno
- *newseg
< sbi
->segs_per_sec
-
341 (*newseg
% sbi
->segs_per_sec
))
345 secno
= find_next_zero_bit(free_i
->free_secmap
, TOTAL_SECS(sbi
), hint
);
346 if (secno
>= TOTAL_SECS(sbi
)) {
347 if (dir
== ALLOC_RIGHT
) {
348 secno
= find_next_zero_bit(free_i
->free_secmap
,
350 BUG_ON(secno
>= TOTAL_SECS(sbi
));
353 left_start
= hint
- 1;
359 while (test_bit(left_start
, free_i
->free_secmap
)) {
360 if (left_start
> 0) {
364 left_start
= find_next_zero_bit(free_i
->free_secmap
,
366 BUG_ON(left_start
>= TOTAL_SECS(sbi
));
372 segno
= secno
* sbi
->segs_per_sec
;
373 zoneno
= secno
/ sbi
->secs_per_zone
;
375 /* give up on finding another zone */
378 if (sbi
->secs_per_zone
== 1)
380 if (zoneno
== old_zoneno
)
382 if (dir
== ALLOC_LEFT
) {
383 if (!go_left
&& zoneno
+ 1 >= total_zones
)
385 if (go_left
&& zoneno
== 0)
388 for (i
= 0; i
< NR_CURSEG_TYPE
; i
++)
389 if (CURSEG_I(sbi
, i
)->zone
== zoneno
)
392 if (i
< NR_CURSEG_TYPE
) {
393 /* zone is in user, try another */
395 hint
= zoneno
* sbi
->secs_per_zone
- 1;
396 else if (zoneno
+ 1 >= total_zones
)
399 hint
= (zoneno
+ 1) * sbi
->secs_per_zone
;
401 goto find_other_zone
;
404 /* set it as dirty segment in free segmap */
405 BUG_ON(test_bit(segno
, free_i
->free_segmap
));
406 __set_inuse(sbi
, segno
);
408 write_unlock(&free_i
->segmap_lock
);
411 static void reset_curseg(struct f2fs_sb_info
*sbi
, int type
, int modified
)
413 struct curseg_info
*curseg
= CURSEG_I(sbi
, type
);
414 struct summary_footer
*sum_footer
;
416 curseg
->segno
= curseg
->next_segno
;
417 curseg
->zone
= GET_ZONENO_FROM_SEGNO(sbi
, curseg
->segno
);
418 curseg
->next_blkoff
= 0;
419 curseg
->next_segno
= NULL_SEGNO
;
421 sum_footer
= &(curseg
->sum_blk
->footer
);
422 memset(sum_footer
, 0, sizeof(struct summary_footer
));
423 if (IS_DATASEG(type
))
424 SET_SUM_TYPE(sum_footer
, SUM_TYPE_DATA
);
425 if (IS_NODESEG(type
))
426 SET_SUM_TYPE(sum_footer
, SUM_TYPE_NODE
);
427 __set_sit_entry_type(sbi
, type
, curseg
->segno
, modified
);
431 * Allocate a current working segment.
432 * This function always allocates a free segment in LFS manner.
434 static void new_curseg(struct f2fs_sb_info
*sbi
, int type
, bool new_sec
)
436 struct curseg_info
*curseg
= CURSEG_I(sbi
, type
);
437 unsigned int segno
= curseg
->segno
;
438 int dir
= ALLOC_LEFT
;
440 write_sum_page(sbi
, curseg
->sum_blk
,
441 GET_SUM_BLOCK(sbi
, segno
));
442 if (type
== CURSEG_WARM_DATA
|| type
== CURSEG_COLD_DATA
)
445 if (test_opt(sbi
, NOHEAP
))
448 get_new_segment(sbi
, &segno
, new_sec
, dir
);
449 curseg
->next_segno
= segno
;
450 reset_curseg(sbi
, type
, 1);
451 curseg
->alloc_type
= LFS
;
454 static void __next_free_blkoff(struct f2fs_sb_info
*sbi
,
455 struct curseg_info
*seg
, block_t start
)
457 struct seg_entry
*se
= get_seg_entry(sbi
, seg
->segno
);
459 for (ofs
= start
; ofs
< sbi
->blocks_per_seg
; ofs
++) {
460 if (!f2fs_test_bit(ofs
, se
->ckpt_valid_map
)
461 && !f2fs_test_bit(ofs
, se
->cur_valid_map
))
464 seg
->next_blkoff
= ofs
;
468 * If a segment is written by LFS manner, next block offset is just obtained
469 * by increasing the current block offset. However, if a segment is written by
470 * SSR manner, next block offset obtained by calling __next_free_blkoff
472 static void __refresh_next_blkoff(struct f2fs_sb_info
*sbi
,
473 struct curseg_info
*seg
)
475 if (seg
->alloc_type
== SSR
)
476 __next_free_blkoff(sbi
, seg
, seg
->next_blkoff
+ 1);
482 * This function always allocates a used segment (from dirty seglist) by SSR
483 * manner, so it should recover the existing segment information of valid blocks
485 static void change_curseg(struct f2fs_sb_info
*sbi
, int type
, bool reuse
)
487 struct dirty_seglist_info
*dirty_i
= DIRTY_I(sbi
);
488 struct curseg_info
*curseg
= CURSEG_I(sbi
, type
);
489 unsigned int new_segno
= curseg
->next_segno
;
490 struct f2fs_summary_block
*sum_node
;
491 struct page
*sum_page
;
493 write_sum_page(sbi
, curseg
->sum_blk
,
494 GET_SUM_BLOCK(sbi
, curseg
->segno
));
495 __set_test_and_inuse(sbi
, new_segno
);
497 mutex_lock(&dirty_i
->seglist_lock
);
498 __remove_dirty_segment(sbi
, new_segno
, PRE
);
499 __remove_dirty_segment(sbi
, new_segno
, DIRTY
);
500 mutex_unlock(&dirty_i
->seglist_lock
);
502 reset_curseg(sbi
, type
, 1);
503 curseg
->alloc_type
= SSR
;
504 __next_free_blkoff(sbi
, curseg
, 0);
507 sum_page
= get_sum_page(sbi
, new_segno
);
508 sum_node
= (struct f2fs_summary_block
*)page_address(sum_page
);
509 memcpy(curseg
->sum_blk
, sum_node
, SUM_ENTRY_SIZE
);
510 f2fs_put_page(sum_page
, 1);
514 static int get_ssr_segment(struct f2fs_sb_info
*sbi
, int type
)
516 struct curseg_info
*curseg
= CURSEG_I(sbi
, type
);
517 const struct victim_selection
*v_ops
= DIRTY_I(sbi
)->v_ops
;
519 if (IS_NODESEG(type
) || !has_not_enough_free_secs(sbi
, 0))
520 return v_ops
->get_victim(sbi
,
521 &(curseg
)->next_segno
, BG_GC
, type
, SSR
);
523 /* For data segments, let's do SSR more intensively */
524 for (; type
>= CURSEG_HOT_DATA
; type
--)
525 if (v_ops
->get_victim(sbi
, &(curseg
)->next_segno
,
532 * flush out current segment and replace it with new segment
533 * This function should be returned with success, otherwise BUG
535 static void allocate_segment_by_default(struct f2fs_sb_info
*sbi
,
536 int type
, bool force
)
538 struct curseg_info
*curseg
= CURSEG_I(sbi
, type
);
541 new_curseg(sbi
, type
, true);
542 else if (type
== CURSEG_WARM_NODE
)
543 new_curseg(sbi
, type
, false);
544 else if (curseg
->alloc_type
== LFS
&& is_next_segment_free(sbi
, type
))
545 new_curseg(sbi
, type
, false);
546 else if (need_SSR(sbi
) && get_ssr_segment(sbi
, type
))
547 change_curseg(sbi
, type
, true);
549 new_curseg(sbi
, type
, false);
551 stat_inc_seg_type(sbi
, curseg
);
554 void allocate_new_segments(struct f2fs_sb_info
*sbi
)
556 struct curseg_info
*curseg
;
557 unsigned int old_curseg
;
560 for (i
= CURSEG_HOT_DATA
; i
<= CURSEG_COLD_DATA
; i
++) {
561 curseg
= CURSEG_I(sbi
, i
);
562 old_curseg
= curseg
->segno
;
563 SIT_I(sbi
)->s_ops
->allocate_segment(sbi
, i
, true);
564 locate_dirty_segment(sbi
, old_curseg
);
568 static const struct segment_allocation default_salloc_ops
= {
569 .allocate_segment
= allocate_segment_by_default
,
572 static void f2fs_end_io_write(struct bio
*bio
, int err
)
574 const int uptodate
= test_bit(BIO_UPTODATE
, &bio
->bi_flags
);
575 struct bio_vec
*bvec
= bio
->bi_io_vec
+ bio
->bi_vcnt
- 1;
576 struct bio_private
*p
= bio
->bi_private
;
579 struct page
*page
= bvec
->bv_page
;
581 if (--bvec
>= bio
->bi_io_vec
)
582 prefetchw(&bvec
->bv_page
->flags
);
586 set_bit(AS_EIO
, &page
->mapping
->flags
);
587 set_ckpt_flags(p
->sbi
->ckpt
, CP_ERROR_FLAG
);
588 p
->sbi
->sb
->s_flags
|= MS_RDONLY
;
590 end_page_writeback(page
);
591 dec_page_count(p
->sbi
, F2FS_WRITEBACK
);
592 } while (bvec
>= bio
->bi_io_vec
);
597 if (!get_pages(p
->sbi
, F2FS_WRITEBACK
) && p
->sbi
->cp_task
)
598 wake_up_process(p
->sbi
->cp_task
);
604 struct bio
*f2fs_bio_alloc(struct block_device
*bdev
, int npages
)
608 /* No failure on bio allocation */
609 bio
= bio_alloc(GFP_NOIO
, npages
);
611 bio
->bi_private
= NULL
;
616 static void do_submit_bio(struct f2fs_sb_info
*sbi
,
617 enum page_type type
, bool sync
)
619 int rw
= sync
? WRITE_SYNC
: WRITE
;
620 enum page_type btype
= type
> META
? META
: type
;
622 if (type
>= META_FLUSH
)
623 rw
= WRITE_FLUSH_FUA
;
628 if (sbi
->bio
[btype
]) {
629 struct bio_private
*p
= sbi
->bio
[btype
]->bi_private
;
631 sbi
->bio
[btype
]->bi_end_io
= f2fs_end_io_write
;
633 trace_f2fs_do_submit_bio(sbi
->sb
, btype
, sync
, sbi
->bio
[btype
]);
635 if (type
== META_FLUSH
) {
636 DECLARE_COMPLETION_ONSTACK(wait
);
639 submit_bio(rw
, sbi
->bio
[btype
]);
640 wait_for_completion(&wait
);
643 submit_bio(rw
, sbi
->bio
[btype
]);
645 sbi
->bio
[btype
] = NULL
;
649 void f2fs_submit_bio(struct f2fs_sb_info
*sbi
, enum page_type type
, bool sync
)
651 down_write(&sbi
->bio_sem
);
652 do_submit_bio(sbi
, type
, sync
);
653 up_write(&sbi
->bio_sem
);
656 static void submit_write_page(struct f2fs_sb_info
*sbi
, struct page
*page
,
657 block_t blk_addr
, enum page_type type
)
659 struct block_device
*bdev
= sbi
->sb
->s_bdev
;
662 verify_block_addr(sbi
, blk_addr
);
664 down_write(&sbi
->bio_sem
);
666 inc_page_count(sbi
, F2FS_WRITEBACK
);
668 if (sbi
->bio
[type
] && sbi
->last_block_in_bio
[type
] != blk_addr
- 1)
669 do_submit_bio(sbi
, type
, false);
671 if (sbi
->bio
[type
] == NULL
) {
672 struct bio_private
*priv
;
674 priv
= kmalloc(sizeof(struct bio_private
), GFP_NOFS
);
680 bio_blocks
= MAX_BIO_BLOCKS(max_hw_blocks(sbi
));
681 sbi
->bio
[type
] = f2fs_bio_alloc(bdev
, bio_blocks
);
682 sbi
->bio
[type
]->bi_sector
= SECTOR_FROM_BLOCK(sbi
, blk_addr
);
683 sbi
->bio
[type
]->bi_private
= priv
;
685 * The end_io will be assigned at the sumbission phase.
686 * Until then, let bio_add_page() merge consecutive IOs as much
691 if (bio_add_page(sbi
->bio
[type
], page
, PAGE_CACHE_SIZE
, 0) <
693 do_submit_bio(sbi
, type
, false);
697 sbi
->last_block_in_bio
[type
] = blk_addr
;
699 up_write(&sbi
->bio_sem
);
700 trace_f2fs_submit_write_page(page
, blk_addr
, type
);
703 void f2fs_wait_on_page_writeback(struct page
*page
,
704 enum page_type type
, bool sync
)
706 struct f2fs_sb_info
*sbi
= F2FS_SB(page
->mapping
->host
->i_sb
);
707 if (PageWriteback(page
)) {
708 f2fs_submit_bio(sbi
, type
, sync
);
709 wait_on_page_writeback(page
);
713 static bool __has_curseg_space(struct f2fs_sb_info
*sbi
, int type
)
715 struct curseg_info
*curseg
= CURSEG_I(sbi
, type
);
716 if (curseg
->next_blkoff
< sbi
->blocks_per_seg
)
721 static int __get_segment_type_2(struct page
*page
, enum page_type p_type
)
724 return CURSEG_HOT_DATA
;
726 return CURSEG_HOT_NODE
;
729 static int __get_segment_type_4(struct page
*page
, enum page_type p_type
)
731 if (p_type
== DATA
) {
732 struct inode
*inode
= page
->mapping
->host
;
734 if (S_ISDIR(inode
->i_mode
))
735 return CURSEG_HOT_DATA
;
737 return CURSEG_COLD_DATA
;
739 if (IS_DNODE(page
) && !is_cold_node(page
))
740 return CURSEG_HOT_NODE
;
742 return CURSEG_COLD_NODE
;
746 static int __get_segment_type_6(struct page
*page
, enum page_type p_type
)
748 if (p_type
== DATA
) {
749 struct inode
*inode
= page
->mapping
->host
;
751 if (S_ISDIR(inode
->i_mode
))
752 return CURSEG_HOT_DATA
;
753 else if (is_cold_data(page
) || file_is_cold(inode
))
754 return CURSEG_COLD_DATA
;
756 return CURSEG_WARM_DATA
;
759 return is_cold_node(page
) ? CURSEG_WARM_NODE
:
762 return CURSEG_COLD_NODE
;
766 static int __get_segment_type(struct page
*page
, enum page_type p_type
)
768 struct f2fs_sb_info
*sbi
= F2FS_SB(page
->mapping
->host
->i_sb
);
769 switch (sbi
->active_logs
) {
771 return __get_segment_type_2(page
, p_type
);
773 return __get_segment_type_4(page
, p_type
);
775 /* NR_CURSEG_TYPE(6) logs by default */
776 BUG_ON(sbi
->active_logs
!= NR_CURSEG_TYPE
);
777 return __get_segment_type_6(page
, p_type
);
780 static void do_write_page(struct f2fs_sb_info
*sbi
, struct page
*page
,
781 block_t old_blkaddr
, block_t
*new_blkaddr
,
782 struct f2fs_summary
*sum
, enum page_type p_type
)
784 struct sit_info
*sit_i
= SIT_I(sbi
);
785 struct curseg_info
*curseg
;
786 unsigned int old_cursegno
;
789 type
= __get_segment_type(page
, p_type
);
790 curseg
= CURSEG_I(sbi
, type
);
792 mutex_lock(&curseg
->curseg_mutex
);
794 *new_blkaddr
= NEXT_FREE_BLKADDR(sbi
, curseg
);
795 old_cursegno
= curseg
->segno
;
798 * __add_sum_entry should be resided under the curseg_mutex
799 * because, this function updates a summary entry in the
800 * current summary block.
802 __add_sum_entry(sbi
, type
, sum
);
804 mutex_lock(&sit_i
->sentry_lock
);
805 __refresh_next_blkoff(sbi
, curseg
);
807 stat_inc_block_count(sbi
, curseg
);
810 * SIT information should be updated before segment allocation,
811 * since SSR needs latest valid block information.
813 refresh_sit_entry(sbi
, old_blkaddr
, *new_blkaddr
);
815 if (!__has_curseg_space(sbi
, type
))
816 sit_i
->s_ops
->allocate_segment(sbi
, type
, false);
818 locate_dirty_segment(sbi
, old_cursegno
);
819 locate_dirty_segment(sbi
, GET_SEGNO(sbi
, old_blkaddr
));
820 mutex_unlock(&sit_i
->sentry_lock
);
823 fill_node_footer_blkaddr(page
, NEXT_FREE_BLKADDR(sbi
, curseg
));
825 /* writeout dirty page into bdev */
826 submit_write_page(sbi
, page
, *new_blkaddr
, p_type
);
828 mutex_unlock(&curseg
->curseg_mutex
);
831 void write_meta_page(struct f2fs_sb_info
*sbi
, struct page
*page
)
833 set_page_writeback(page
);
834 submit_write_page(sbi
, page
, page
->index
, META
);
837 void write_node_page(struct f2fs_sb_info
*sbi
, struct page
*page
,
838 unsigned int nid
, block_t old_blkaddr
, block_t
*new_blkaddr
)
840 struct f2fs_summary sum
;
841 set_summary(&sum
, nid
, 0, 0);
842 do_write_page(sbi
, page
, old_blkaddr
, new_blkaddr
, &sum
, NODE
);
845 void write_data_page(struct inode
*inode
, struct page
*page
,
846 struct dnode_of_data
*dn
, block_t old_blkaddr
,
847 block_t
*new_blkaddr
)
849 struct f2fs_sb_info
*sbi
= F2FS_SB(inode
->i_sb
);
850 struct f2fs_summary sum
;
853 BUG_ON(old_blkaddr
== NULL_ADDR
);
854 get_node_info(sbi
, dn
->nid
, &ni
);
855 set_summary(&sum
, dn
->nid
, dn
->ofs_in_node
, ni
.version
);
857 do_write_page(sbi
, page
, old_blkaddr
,
858 new_blkaddr
, &sum
, DATA
);
861 void rewrite_data_page(struct f2fs_sb_info
*sbi
, struct page
*page
,
862 block_t old_blk_addr
)
864 submit_write_page(sbi
, page
, old_blk_addr
, DATA
);
867 void recover_data_page(struct f2fs_sb_info
*sbi
,
868 struct page
*page
, struct f2fs_summary
*sum
,
869 block_t old_blkaddr
, block_t new_blkaddr
)
871 struct sit_info
*sit_i
= SIT_I(sbi
);
872 struct curseg_info
*curseg
;
873 unsigned int segno
, old_cursegno
;
874 struct seg_entry
*se
;
877 segno
= GET_SEGNO(sbi
, new_blkaddr
);
878 se
= get_seg_entry(sbi
, segno
);
881 if (se
->valid_blocks
== 0 && !IS_CURSEG(sbi
, segno
)) {
882 if (old_blkaddr
== NULL_ADDR
)
883 type
= CURSEG_COLD_DATA
;
885 type
= CURSEG_WARM_DATA
;
887 curseg
= CURSEG_I(sbi
, type
);
889 mutex_lock(&curseg
->curseg_mutex
);
890 mutex_lock(&sit_i
->sentry_lock
);
892 old_cursegno
= curseg
->segno
;
894 /* change the current segment */
895 if (segno
!= curseg
->segno
) {
896 curseg
->next_segno
= segno
;
897 change_curseg(sbi
, type
, true);
900 curseg
->next_blkoff
= GET_SEGOFF_FROM_SEG0(sbi
, new_blkaddr
) &
901 (sbi
->blocks_per_seg
- 1);
902 __add_sum_entry(sbi
, type
, sum
);
904 refresh_sit_entry(sbi
, old_blkaddr
, new_blkaddr
);
906 locate_dirty_segment(sbi
, old_cursegno
);
907 locate_dirty_segment(sbi
, GET_SEGNO(sbi
, old_blkaddr
));
909 mutex_unlock(&sit_i
->sentry_lock
);
910 mutex_unlock(&curseg
->curseg_mutex
);
913 void rewrite_node_page(struct f2fs_sb_info
*sbi
,
914 struct page
*page
, struct f2fs_summary
*sum
,
915 block_t old_blkaddr
, block_t new_blkaddr
)
917 struct sit_info
*sit_i
= SIT_I(sbi
);
918 int type
= CURSEG_WARM_NODE
;
919 struct curseg_info
*curseg
;
920 unsigned int segno
, old_cursegno
;
921 block_t next_blkaddr
= next_blkaddr_of_node(page
);
922 unsigned int next_segno
= GET_SEGNO(sbi
, next_blkaddr
);
924 curseg
= CURSEG_I(sbi
, type
);
926 mutex_lock(&curseg
->curseg_mutex
);
927 mutex_lock(&sit_i
->sentry_lock
);
929 segno
= GET_SEGNO(sbi
, new_blkaddr
);
930 old_cursegno
= curseg
->segno
;
932 /* change the current segment */
933 if (segno
!= curseg
->segno
) {
934 curseg
->next_segno
= segno
;
935 change_curseg(sbi
, type
, true);
937 curseg
->next_blkoff
= GET_SEGOFF_FROM_SEG0(sbi
, new_blkaddr
) &
938 (sbi
->blocks_per_seg
- 1);
939 __add_sum_entry(sbi
, type
, sum
);
941 /* change the current log to the next block addr in advance */
942 if (next_segno
!= segno
) {
943 curseg
->next_segno
= next_segno
;
944 change_curseg(sbi
, type
, true);
946 curseg
->next_blkoff
= GET_SEGOFF_FROM_SEG0(sbi
, next_blkaddr
) &
947 (sbi
->blocks_per_seg
- 1);
949 /* rewrite node page */
950 set_page_writeback(page
);
951 submit_write_page(sbi
, page
, new_blkaddr
, NODE
);
952 f2fs_submit_bio(sbi
, NODE
, true);
953 refresh_sit_entry(sbi
, old_blkaddr
, new_blkaddr
);
955 locate_dirty_segment(sbi
, old_cursegno
);
956 locate_dirty_segment(sbi
, GET_SEGNO(sbi
, old_blkaddr
));
958 mutex_unlock(&sit_i
->sentry_lock
);
959 mutex_unlock(&curseg
->curseg_mutex
);
962 static int read_compacted_summaries(struct f2fs_sb_info
*sbi
)
964 struct f2fs_checkpoint
*ckpt
= F2FS_CKPT(sbi
);
965 struct curseg_info
*seg_i
;
966 unsigned char *kaddr
;
971 start
= start_sum_block(sbi
);
973 page
= get_meta_page(sbi
, start
++);
974 kaddr
= (unsigned char *)page_address(page
);
976 /* Step 1: restore nat cache */
977 seg_i
= CURSEG_I(sbi
, CURSEG_HOT_DATA
);
978 memcpy(&seg_i
->sum_blk
->n_nats
, kaddr
, SUM_JOURNAL_SIZE
);
980 /* Step 2: restore sit cache */
981 seg_i
= CURSEG_I(sbi
, CURSEG_COLD_DATA
);
982 memcpy(&seg_i
->sum_blk
->n_sits
, kaddr
+ SUM_JOURNAL_SIZE
,
984 offset
= 2 * SUM_JOURNAL_SIZE
;
986 /* Step 3: restore summary entries */
987 for (i
= CURSEG_HOT_DATA
; i
<= CURSEG_COLD_DATA
; i
++) {
988 unsigned short blk_off
;
991 seg_i
= CURSEG_I(sbi
, i
);
992 segno
= le32_to_cpu(ckpt
->cur_data_segno
[i
]);
993 blk_off
= le16_to_cpu(ckpt
->cur_data_blkoff
[i
]);
994 seg_i
->next_segno
= segno
;
995 reset_curseg(sbi
, i
, 0);
996 seg_i
->alloc_type
= ckpt
->alloc_type
[i
];
997 seg_i
->next_blkoff
= blk_off
;
999 if (seg_i
->alloc_type
== SSR
)
1000 blk_off
= sbi
->blocks_per_seg
;
1002 for (j
= 0; j
< blk_off
; j
++) {
1003 struct f2fs_summary
*s
;
1004 s
= (struct f2fs_summary
*)(kaddr
+ offset
);
1005 seg_i
->sum_blk
->entries
[j
] = *s
;
1006 offset
+= SUMMARY_SIZE
;
1007 if (offset
+ SUMMARY_SIZE
<= PAGE_CACHE_SIZE
-
1011 f2fs_put_page(page
, 1);
1014 page
= get_meta_page(sbi
, start
++);
1015 kaddr
= (unsigned char *)page_address(page
);
1019 f2fs_put_page(page
, 1);
1023 static int read_normal_summaries(struct f2fs_sb_info
*sbi
, int type
)
1025 struct f2fs_checkpoint
*ckpt
= F2FS_CKPT(sbi
);
1026 struct f2fs_summary_block
*sum
;
1027 struct curseg_info
*curseg
;
1029 unsigned short blk_off
;
1030 unsigned int segno
= 0;
1031 block_t blk_addr
= 0;
1033 /* get segment number and block addr */
1034 if (IS_DATASEG(type
)) {
1035 segno
= le32_to_cpu(ckpt
->cur_data_segno
[type
]);
1036 blk_off
= le16_to_cpu(ckpt
->cur_data_blkoff
[type
-
1038 if (is_set_ckpt_flags(ckpt
, CP_UMOUNT_FLAG
))
1039 blk_addr
= sum_blk_addr(sbi
, NR_CURSEG_TYPE
, type
);
1041 blk_addr
= sum_blk_addr(sbi
, NR_CURSEG_DATA_TYPE
, type
);
1043 segno
= le32_to_cpu(ckpt
->cur_node_segno
[type
-
1045 blk_off
= le16_to_cpu(ckpt
->cur_node_blkoff
[type
-
1047 if (is_set_ckpt_flags(ckpt
, CP_UMOUNT_FLAG
))
1048 blk_addr
= sum_blk_addr(sbi
, NR_CURSEG_NODE_TYPE
,
1049 type
- CURSEG_HOT_NODE
);
1051 blk_addr
= GET_SUM_BLOCK(sbi
, segno
);
1054 new = get_meta_page(sbi
, blk_addr
);
1055 sum
= (struct f2fs_summary_block
*)page_address(new);
1057 if (IS_NODESEG(type
)) {
1058 if (is_set_ckpt_flags(ckpt
, CP_UMOUNT_FLAG
)) {
1059 struct f2fs_summary
*ns
= &sum
->entries
[0];
1061 for (i
= 0; i
< sbi
->blocks_per_seg
; i
++, ns
++) {
1063 ns
->ofs_in_node
= 0;
1066 if (restore_node_summary(sbi
, segno
, sum
)) {
1067 f2fs_put_page(new, 1);
1073 /* set uncompleted segment to curseg */
1074 curseg
= CURSEG_I(sbi
, type
);
1075 mutex_lock(&curseg
->curseg_mutex
);
1076 memcpy(curseg
->sum_blk
, sum
, PAGE_CACHE_SIZE
);
1077 curseg
->next_segno
= segno
;
1078 reset_curseg(sbi
, type
, 0);
1079 curseg
->alloc_type
= ckpt
->alloc_type
[type
];
1080 curseg
->next_blkoff
= blk_off
;
1081 mutex_unlock(&curseg
->curseg_mutex
);
1082 f2fs_put_page(new, 1);
1086 static int restore_curseg_summaries(struct f2fs_sb_info
*sbi
)
1088 int type
= CURSEG_HOT_DATA
;
1090 if (is_set_ckpt_flags(F2FS_CKPT(sbi
), CP_COMPACT_SUM_FLAG
)) {
1091 /* restore for compacted data summary */
1092 if (read_compacted_summaries(sbi
))
1094 type
= CURSEG_HOT_NODE
;
1097 for (; type
<= CURSEG_COLD_NODE
; type
++)
1098 if (read_normal_summaries(sbi
, type
))
1103 static void write_compacted_summaries(struct f2fs_sb_info
*sbi
, block_t blkaddr
)
1106 unsigned char *kaddr
;
1107 struct f2fs_summary
*summary
;
1108 struct curseg_info
*seg_i
;
1109 int written_size
= 0;
1112 page
= grab_meta_page(sbi
, blkaddr
++);
1113 kaddr
= (unsigned char *)page_address(page
);
1115 /* Step 1: write nat cache */
1116 seg_i
= CURSEG_I(sbi
, CURSEG_HOT_DATA
);
1117 memcpy(kaddr
, &seg_i
->sum_blk
->n_nats
, SUM_JOURNAL_SIZE
);
1118 written_size
+= SUM_JOURNAL_SIZE
;
1120 /* Step 2: write sit cache */
1121 seg_i
= CURSEG_I(sbi
, CURSEG_COLD_DATA
);
1122 memcpy(kaddr
+ written_size
, &seg_i
->sum_blk
->n_sits
,
1124 written_size
+= SUM_JOURNAL_SIZE
;
1126 /* Step 3: write summary entries */
1127 for (i
= CURSEG_HOT_DATA
; i
<= CURSEG_COLD_DATA
; i
++) {
1128 unsigned short blkoff
;
1129 seg_i
= CURSEG_I(sbi
, i
);
1130 if (sbi
->ckpt
->alloc_type
[i
] == SSR
)
1131 blkoff
= sbi
->blocks_per_seg
;
1133 blkoff
= curseg_blkoff(sbi
, i
);
1135 for (j
= 0; j
< blkoff
; j
++) {
1137 page
= grab_meta_page(sbi
, blkaddr
++);
1138 kaddr
= (unsigned char *)page_address(page
);
1141 summary
= (struct f2fs_summary
*)(kaddr
+ written_size
);
1142 *summary
= seg_i
->sum_blk
->entries
[j
];
1143 written_size
+= SUMMARY_SIZE
;
1145 if (written_size
+ SUMMARY_SIZE
<= PAGE_CACHE_SIZE
-
1149 set_page_dirty(page
);
1150 f2fs_put_page(page
, 1);
1155 set_page_dirty(page
);
1156 f2fs_put_page(page
, 1);
1160 static void write_normal_summaries(struct f2fs_sb_info
*sbi
,
1161 block_t blkaddr
, int type
)
1164 if (IS_DATASEG(type
))
1165 end
= type
+ NR_CURSEG_DATA_TYPE
;
1167 end
= type
+ NR_CURSEG_NODE_TYPE
;
1169 for (i
= type
; i
< end
; i
++) {
1170 struct curseg_info
*sum
= CURSEG_I(sbi
, i
);
1171 mutex_lock(&sum
->curseg_mutex
);
1172 write_sum_page(sbi
, sum
->sum_blk
, blkaddr
+ (i
- type
));
1173 mutex_unlock(&sum
->curseg_mutex
);
1177 void write_data_summaries(struct f2fs_sb_info
*sbi
, block_t start_blk
)
1179 if (is_set_ckpt_flags(F2FS_CKPT(sbi
), CP_COMPACT_SUM_FLAG
))
1180 write_compacted_summaries(sbi
, start_blk
);
1182 write_normal_summaries(sbi
, start_blk
, CURSEG_HOT_DATA
);
1185 void write_node_summaries(struct f2fs_sb_info
*sbi
, block_t start_blk
)
1187 if (is_set_ckpt_flags(F2FS_CKPT(sbi
), CP_UMOUNT_FLAG
))
1188 write_normal_summaries(sbi
, start_blk
, CURSEG_HOT_NODE
);
1191 int lookup_journal_in_cursum(struct f2fs_summary_block
*sum
, int type
,
1192 unsigned int val
, int alloc
)
1196 if (type
== NAT_JOURNAL
) {
1197 for (i
= 0; i
< nats_in_cursum(sum
); i
++) {
1198 if (le32_to_cpu(nid_in_journal(sum
, i
)) == val
)
1201 if (alloc
&& nats_in_cursum(sum
) < NAT_JOURNAL_ENTRIES
)
1202 return update_nats_in_cursum(sum
, 1);
1203 } else if (type
== SIT_JOURNAL
) {
1204 for (i
= 0; i
< sits_in_cursum(sum
); i
++)
1205 if (le32_to_cpu(segno_in_journal(sum
, i
)) == val
)
1207 if (alloc
&& sits_in_cursum(sum
) < SIT_JOURNAL_ENTRIES
)
1208 return update_sits_in_cursum(sum
, 1);
1213 static struct page
*get_current_sit_page(struct f2fs_sb_info
*sbi
,
1216 struct sit_info
*sit_i
= SIT_I(sbi
);
1217 unsigned int offset
= SIT_BLOCK_OFFSET(sit_i
, segno
);
1218 block_t blk_addr
= sit_i
->sit_base_addr
+ offset
;
1220 check_seg_range(sbi
, segno
);
1222 /* calculate sit block address */
1223 if (f2fs_test_bit(offset
, sit_i
->sit_bitmap
))
1224 blk_addr
+= sit_i
->sit_blocks
;
1226 return get_meta_page(sbi
, blk_addr
);
1229 static struct page
*get_next_sit_page(struct f2fs_sb_info
*sbi
,
1232 struct sit_info
*sit_i
= SIT_I(sbi
);
1233 struct page
*src_page
, *dst_page
;
1234 pgoff_t src_off
, dst_off
;
1235 void *src_addr
, *dst_addr
;
1237 src_off
= current_sit_addr(sbi
, start
);
1238 dst_off
= next_sit_addr(sbi
, src_off
);
1240 /* get current sit block page without lock */
1241 src_page
= get_meta_page(sbi
, src_off
);
1242 dst_page
= grab_meta_page(sbi
, dst_off
);
1243 BUG_ON(PageDirty(src_page
));
1245 src_addr
= page_address(src_page
);
1246 dst_addr
= page_address(dst_page
);
1247 memcpy(dst_addr
, src_addr
, PAGE_CACHE_SIZE
);
1249 set_page_dirty(dst_page
);
1250 f2fs_put_page(src_page
, 1);
1252 set_to_next_sit(sit_i
, start
);
1257 static bool flush_sits_in_journal(struct f2fs_sb_info
*sbi
)
1259 struct curseg_info
*curseg
= CURSEG_I(sbi
, CURSEG_COLD_DATA
);
1260 struct f2fs_summary_block
*sum
= curseg
->sum_blk
;
1264 * If the journal area in the current summary is full of sit entries,
1265 * all the sit entries will be flushed. Otherwise the sit entries
1266 * are not able to replace with newly hot sit entries.
1268 if (sits_in_cursum(sum
) >= SIT_JOURNAL_ENTRIES
) {
1269 for (i
= sits_in_cursum(sum
) - 1; i
>= 0; i
--) {
1271 segno
= le32_to_cpu(segno_in_journal(sum
, i
));
1272 __mark_sit_entry_dirty(sbi
, segno
);
1274 update_sits_in_cursum(sum
, -sits_in_cursum(sum
));
1281 * CP calls this function, which flushes SIT entries including sit_journal,
1282 * and moves prefree segs to free segs.
1284 void flush_sit_entries(struct f2fs_sb_info
*sbi
)
1286 struct sit_info
*sit_i
= SIT_I(sbi
);
1287 unsigned long *bitmap
= sit_i
->dirty_sentries_bitmap
;
1288 struct curseg_info
*curseg
= CURSEG_I(sbi
, CURSEG_COLD_DATA
);
1289 struct f2fs_summary_block
*sum
= curseg
->sum_blk
;
1290 unsigned long nsegs
= TOTAL_SEGS(sbi
);
1291 struct page
*page
= NULL
;
1292 struct f2fs_sit_block
*raw_sit
= NULL
;
1293 unsigned int start
= 0, end
= 0;
1294 unsigned int segno
= -1;
1297 mutex_lock(&curseg
->curseg_mutex
);
1298 mutex_lock(&sit_i
->sentry_lock
);
1301 * "flushed" indicates whether sit entries in journal are flushed
1302 * to the SIT area or not.
1304 flushed
= flush_sits_in_journal(sbi
);
1306 while ((segno
= find_next_bit(bitmap
, nsegs
, segno
+ 1)) < nsegs
) {
1307 struct seg_entry
*se
= get_seg_entry(sbi
, segno
);
1308 int sit_offset
, offset
;
1310 sit_offset
= SIT_ENTRY_OFFSET(sit_i
, segno
);
1315 offset
= lookup_journal_in_cursum(sum
, SIT_JOURNAL
, segno
, 1);
1317 segno_in_journal(sum
, offset
) = cpu_to_le32(segno
);
1318 seg_info_to_raw_sit(se
, &sit_in_journal(sum
, offset
));
1322 if (!page
|| (start
> segno
) || (segno
> end
)) {
1324 f2fs_put_page(page
, 1);
1328 start
= START_SEGNO(sit_i
, segno
);
1329 end
= start
+ SIT_ENTRY_PER_BLOCK
- 1;
1331 /* read sit block that will be updated */
1332 page
= get_next_sit_page(sbi
, start
);
1333 raw_sit
= page_address(page
);
1336 /* udpate entry in SIT block */
1337 seg_info_to_raw_sit(se
, &raw_sit
->entries
[sit_offset
]);
1339 __clear_bit(segno
, bitmap
);
1340 sit_i
->dirty_sentries
--;
1342 mutex_unlock(&sit_i
->sentry_lock
);
1343 mutex_unlock(&curseg
->curseg_mutex
);
1345 /* writeout last modified SIT block */
1346 f2fs_put_page(page
, 1);
1348 set_prefree_as_free_segments(sbi
);
1351 static int build_sit_info(struct f2fs_sb_info
*sbi
)
1353 struct f2fs_super_block
*raw_super
= F2FS_RAW_SUPER(sbi
);
1354 struct f2fs_checkpoint
*ckpt
= F2FS_CKPT(sbi
);
1355 struct sit_info
*sit_i
;
1356 unsigned int sit_segs
, start
;
1357 char *src_bitmap
, *dst_bitmap
;
1358 unsigned int bitmap_size
;
1360 /* allocate memory for SIT information */
1361 sit_i
= kzalloc(sizeof(struct sit_info
), GFP_KERNEL
);
1365 SM_I(sbi
)->sit_info
= sit_i
;
1367 sit_i
->sentries
= vzalloc(TOTAL_SEGS(sbi
) * sizeof(struct seg_entry
));
1368 if (!sit_i
->sentries
)
1371 bitmap_size
= f2fs_bitmap_size(TOTAL_SEGS(sbi
));
1372 sit_i
->dirty_sentries_bitmap
= kzalloc(bitmap_size
, GFP_KERNEL
);
1373 if (!sit_i
->dirty_sentries_bitmap
)
1376 for (start
= 0; start
< TOTAL_SEGS(sbi
); start
++) {
1377 sit_i
->sentries
[start
].cur_valid_map
1378 = kzalloc(SIT_VBLOCK_MAP_SIZE
, GFP_KERNEL
);
1379 sit_i
->sentries
[start
].ckpt_valid_map
1380 = kzalloc(SIT_VBLOCK_MAP_SIZE
, GFP_KERNEL
);
1381 if (!sit_i
->sentries
[start
].cur_valid_map
1382 || !sit_i
->sentries
[start
].ckpt_valid_map
)
1386 if (sbi
->segs_per_sec
> 1) {
1387 sit_i
->sec_entries
= vzalloc(TOTAL_SECS(sbi
) *
1388 sizeof(struct sec_entry
));
1389 if (!sit_i
->sec_entries
)
1393 /* get information related with SIT */
1394 sit_segs
= le32_to_cpu(raw_super
->segment_count_sit
) >> 1;
1396 /* setup SIT bitmap from ckeckpoint pack */
1397 bitmap_size
= __bitmap_size(sbi
, SIT_BITMAP
);
1398 src_bitmap
= __bitmap_ptr(sbi
, SIT_BITMAP
);
1400 dst_bitmap
= kmemdup(src_bitmap
, bitmap_size
, GFP_KERNEL
);
1404 /* init SIT information */
1405 sit_i
->s_ops
= &default_salloc_ops
;
1407 sit_i
->sit_base_addr
= le32_to_cpu(raw_super
->sit_blkaddr
);
1408 sit_i
->sit_blocks
= sit_segs
<< sbi
->log_blocks_per_seg
;
1409 sit_i
->written_valid_blocks
= le64_to_cpu(ckpt
->valid_block_count
);
1410 sit_i
->sit_bitmap
= dst_bitmap
;
1411 sit_i
->bitmap_size
= bitmap_size
;
1412 sit_i
->dirty_sentries
= 0;
1413 sit_i
->sents_per_block
= SIT_ENTRY_PER_BLOCK
;
1414 sit_i
->elapsed_time
= le64_to_cpu(sbi
->ckpt
->elapsed_time
);
1415 sit_i
->mounted_time
= CURRENT_TIME_SEC
.tv_sec
;
1416 mutex_init(&sit_i
->sentry_lock
);
1420 static int build_free_segmap(struct f2fs_sb_info
*sbi
)
1422 struct f2fs_sm_info
*sm_info
= SM_I(sbi
);
1423 struct free_segmap_info
*free_i
;
1424 unsigned int bitmap_size
, sec_bitmap_size
;
1426 /* allocate memory for free segmap information */
1427 free_i
= kzalloc(sizeof(struct free_segmap_info
), GFP_KERNEL
);
1431 SM_I(sbi
)->free_info
= free_i
;
1433 bitmap_size
= f2fs_bitmap_size(TOTAL_SEGS(sbi
));
1434 free_i
->free_segmap
= kmalloc(bitmap_size
, GFP_KERNEL
);
1435 if (!free_i
->free_segmap
)
1438 sec_bitmap_size
= f2fs_bitmap_size(TOTAL_SECS(sbi
));
1439 free_i
->free_secmap
= kmalloc(sec_bitmap_size
, GFP_KERNEL
);
1440 if (!free_i
->free_secmap
)
1443 /* set all segments as dirty temporarily */
1444 memset(free_i
->free_segmap
, 0xff, bitmap_size
);
1445 memset(free_i
->free_secmap
, 0xff, sec_bitmap_size
);
1447 /* init free segmap information */
1448 free_i
->start_segno
=
1449 (unsigned int) GET_SEGNO_FROM_SEG0(sbi
, sm_info
->main_blkaddr
);
1450 free_i
->free_segments
= 0;
1451 free_i
->free_sections
= 0;
1452 rwlock_init(&free_i
->segmap_lock
);
1456 static int build_curseg(struct f2fs_sb_info
*sbi
)
1458 struct curseg_info
*array
;
1461 array
= kzalloc(sizeof(*array
) * NR_CURSEG_TYPE
, GFP_KERNEL
);
1465 SM_I(sbi
)->curseg_array
= array
;
1467 for (i
= 0; i
< NR_CURSEG_TYPE
; i
++) {
1468 mutex_init(&array
[i
].curseg_mutex
);
1469 array
[i
].sum_blk
= kzalloc(PAGE_CACHE_SIZE
, GFP_KERNEL
);
1470 if (!array
[i
].sum_blk
)
1472 array
[i
].segno
= NULL_SEGNO
;
1473 array
[i
].next_blkoff
= 0;
1475 return restore_curseg_summaries(sbi
);
1478 static void build_sit_entries(struct f2fs_sb_info
*sbi
)
1480 struct sit_info
*sit_i
= SIT_I(sbi
);
1481 struct curseg_info
*curseg
= CURSEG_I(sbi
, CURSEG_COLD_DATA
);
1482 struct f2fs_summary_block
*sum
= curseg
->sum_blk
;
1485 for (start
= 0; start
< TOTAL_SEGS(sbi
); start
++) {
1486 struct seg_entry
*se
= &sit_i
->sentries
[start
];
1487 struct f2fs_sit_block
*sit_blk
;
1488 struct f2fs_sit_entry sit
;
1492 mutex_lock(&curseg
->curseg_mutex
);
1493 for (i
= 0; i
< sits_in_cursum(sum
); i
++) {
1494 if (le32_to_cpu(segno_in_journal(sum
, i
)) == start
) {
1495 sit
= sit_in_journal(sum
, i
);
1496 mutex_unlock(&curseg
->curseg_mutex
);
1500 mutex_unlock(&curseg
->curseg_mutex
);
1501 page
= get_current_sit_page(sbi
, start
);
1502 sit_blk
= (struct f2fs_sit_block
*)page_address(page
);
1503 sit
= sit_blk
->entries
[SIT_ENTRY_OFFSET(sit_i
, start
)];
1504 f2fs_put_page(page
, 1);
1506 check_block_count(sbi
, start
, &sit
);
1507 seg_info_from_raw_sit(se
, &sit
);
1508 if (sbi
->segs_per_sec
> 1) {
1509 struct sec_entry
*e
= get_sec_entry(sbi
, start
);
1510 e
->valid_blocks
+= se
->valid_blocks
;
1515 static void init_free_segmap(struct f2fs_sb_info
*sbi
)
1520 for (start
= 0; start
< TOTAL_SEGS(sbi
); start
++) {
1521 struct seg_entry
*sentry
= get_seg_entry(sbi
, start
);
1522 if (!sentry
->valid_blocks
)
1523 __set_free(sbi
, start
);
1526 /* set use the current segments */
1527 for (type
= CURSEG_HOT_DATA
; type
<= CURSEG_COLD_NODE
; type
++) {
1528 struct curseg_info
*curseg_t
= CURSEG_I(sbi
, type
);
1529 __set_test_and_inuse(sbi
, curseg_t
->segno
);
1533 static void init_dirty_segmap(struct f2fs_sb_info
*sbi
)
1535 struct dirty_seglist_info
*dirty_i
= DIRTY_I(sbi
);
1536 struct free_segmap_info
*free_i
= FREE_I(sbi
);
1537 unsigned int segno
= 0, offset
= 0, total_segs
= TOTAL_SEGS(sbi
);
1538 unsigned short valid_blocks
;
1541 /* find dirty segment based on free segmap */
1542 segno
= find_next_inuse(free_i
, total_segs
, offset
);
1543 if (segno
>= total_segs
)
1546 valid_blocks
= get_valid_blocks(sbi
, segno
, 0);
1547 if (valid_blocks
>= sbi
->blocks_per_seg
|| !valid_blocks
)
1549 mutex_lock(&dirty_i
->seglist_lock
);
1550 __locate_dirty_segment(sbi
, segno
, DIRTY
);
1551 mutex_unlock(&dirty_i
->seglist_lock
);
1555 static int init_victim_secmap(struct f2fs_sb_info
*sbi
)
1557 struct dirty_seglist_info
*dirty_i
= DIRTY_I(sbi
);
1558 unsigned int bitmap_size
= f2fs_bitmap_size(TOTAL_SECS(sbi
));
1560 dirty_i
->victim_secmap
= kzalloc(bitmap_size
, GFP_KERNEL
);
1561 if (!dirty_i
->victim_secmap
)
1566 static int build_dirty_segmap(struct f2fs_sb_info
*sbi
)
1568 struct dirty_seglist_info
*dirty_i
;
1569 unsigned int bitmap_size
, i
;
1571 /* allocate memory for dirty segments list information */
1572 dirty_i
= kzalloc(sizeof(struct dirty_seglist_info
), GFP_KERNEL
);
1576 SM_I(sbi
)->dirty_info
= dirty_i
;
1577 mutex_init(&dirty_i
->seglist_lock
);
1579 bitmap_size
= f2fs_bitmap_size(TOTAL_SEGS(sbi
));
1581 for (i
= 0; i
< NR_DIRTY_TYPE
; i
++) {
1582 dirty_i
->dirty_segmap
[i
] = kzalloc(bitmap_size
, GFP_KERNEL
);
1583 if (!dirty_i
->dirty_segmap
[i
])
1587 init_dirty_segmap(sbi
);
1588 return init_victim_secmap(sbi
);
1592 * Update min, max modified time for cost-benefit GC algorithm
1594 static void init_min_max_mtime(struct f2fs_sb_info
*sbi
)
1596 struct sit_info
*sit_i
= SIT_I(sbi
);
1599 mutex_lock(&sit_i
->sentry_lock
);
1601 sit_i
->min_mtime
= LLONG_MAX
;
1603 for (segno
= 0; segno
< TOTAL_SEGS(sbi
); segno
+= sbi
->segs_per_sec
) {
1605 unsigned long long mtime
= 0;
1607 for (i
= 0; i
< sbi
->segs_per_sec
; i
++)
1608 mtime
+= get_seg_entry(sbi
, segno
+ i
)->mtime
;
1610 mtime
= div_u64(mtime
, sbi
->segs_per_sec
);
1612 if (sit_i
->min_mtime
> mtime
)
1613 sit_i
->min_mtime
= mtime
;
1615 sit_i
->max_mtime
= get_mtime(sbi
);
1616 mutex_unlock(&sit_i
->sentry_lock
);
1619 int build_segment_manager(struct f2fs_sb_info
*sbi
)
1621 struct f2fs_super_block
*raw_super
= F2FS_RAW_SUPER(sbi
);
1622 struct f2fs_checkpoint
*ckpt
= F2FS_CKPT(sbi
);
1623 struct f2fs_sm_info
*sm_info
;
1626 sm_info
= kzalloc(sizeof(struct f2fs_sm_info
), GFP_KERNEL
);
1631 sbi
->sm_info
= sm_info
;
1632 INIT_LIST_HEAD(&sm_info
->wblist_head
);
1633 spin_lock_init(&sm_info
->wblist_lock
);
1634 sm_info
->seg0_blkaddr
= le32_to_cpu(raw_super
->segment0_blkaddr
);
1635 sm_info
->main_blkaddr
= le32_to_cpu(raw_super
->main_blkaddr
);
1636 sm_info
->segment_count
= le32_to_cpu(raw_super
->segment_count
);
1637 sm_info
->reserved_segments
= le32_to_cpu(ckpt
->rsvd_segment_count
);
1638 sm_info
->ovp_segments
= le32_to_cpu(ckpt
->overprov_segment_count
);
1639 sm_info
->main_segments
= le32_to_cpu(raw_super
->segment_count_main
);
1640 sm_info
->ssa_blkaddr
= le32_to_cpu(raw_super
->ssa_blkaddr
);
1641 sm_info
->rec_prefree_segments
= DEF_RECLAIM_PREFREE_SEGMENTS
;
1643 err
= build_sit_info(sbi
);
1646 err
= build_free_segmap(sbi
);
1649 err
= build_curseg(sbi
);
1653 /* reinit free segmap based on SIT */
1654 build_sit_entries(sbi
);
1656 init_free_segmap(sbi
);
1657 err
= build_dirty_segmap(sbi
);
1661 init_min_max_mtime(sbi
);
1665 static void discard_dirty_segmap(struct f2fs_sb_info
*sbi
,
1666 enum dirty_type dirty_type
)
1668 struct dirty_seglist_info
*dirty_i
= DIRTY_I(sbi
);
1670 mutex_lock(&dirty_i
->seglist_lock
);
1671 kfree(dirty_i
->dirty_segmap
[dirty_type
]);
1672 dirty_i
->nr_dirty
[dirty_type
] = 0;
1673 mutex_unlock(&dirty_i
->seglist_lock
);
1676 static void destroy_victim_secmap(struct f2fs_sb_info
*sbi
)
1678 struct dirty_seglist_info
*dirty_i
= DIRTY_I(sbi
);
1679 kfree(dirty_i
->victim_secmap
);
1682 static void destroy_dirty_segmap(struct f2fs_sb_info
*sbi
)
1684 struct dirty_seglist_info
*dirty_i
= DIRTY_I(sbi
);
1690 /* discard pre-free/dirty segments list */
1691 for (i
= 0; i
< NR_DIRTY_TYPE
; i
++)
1692 discard_dirty_segmap(sbi
, i
);
1694 destroy_victim_secmap(sbi
);
1695 SM_I(sbi
)->dirty_info
= NULL
;
1699 static void destroy_curseg(struct f2fs_sb_info
*sbi
)
1701 struct curseg_info
*array
= SM_I(sbi
)->curseg_array
;
1706 SM_I(sbi
)->curseg_array
= NULL
;
1707 for (i
= 0; i
< NR_CURSEG_TYPE
; i
++)
1708 kfree(array
[i
].sum_blk
);
1712 static void destroy_free_segmap(struct f2fs_sb_info
*sbi
)
1714 struct free_segmap_info
*free_i
= SM_I(sbi
)->free_info
;
1717 SM_I(sbi
)->free_info
= NULL
;
1718 kfree(free_i
->free_segmap
);
1719 kfree(free_i
->free_secmap
);
1723 static void destroy_sit_info(struct f2fs_sb_info
*sbi
)
1725 struct sit_info
*sit_i
= SIT_I(sbi
);
1731 if (sit_i
->sentries
) {
1732 for (start
= 0; start
< TOTAL_SEGS(sbi
); start
++) {
1733 kfree(sit_i
->sentries
[start
].cur_valid_map
);
1734 kfree(sit_i
->sentries
[start
].ckpt_valid_map
);
1737 vfree(sit_i
->sentries
);
1738 vfree(sit_i
->sec_entries
);
1739 kfree(sit_i
->dirty_sentries_bitmap
);
1741 SM_I(sbi
)->sit_info
= NULL
;
1742 kfree(sit_i
->sit_bitmap
);
1746 void destroy_segment_manager(struct f2fs_sb_info
*sbi
)
1748 struct f2fs_sm_info
*sm_info
= SM_I(sbi
);
1749 destroy_dirty_segmap(sbi
);
1750 destroy_curseg(sbi
);
1751 destroy_free_segmap(sbi
);
1752 destroy_sit_info(sbi
);
1753 sbi
->sm_info
= NULL
;