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 f2fs_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 f2fs_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 valid_sum_count
= 0;
270 for (i
= CURSEG_HOT_DATA
; i
<= CURSEG_COLD_DATA
; i
++) {
271 if (sbi
->ckpt
->alloc_type
[i
] == SSR
)
272 valid_sum_count
+= sbi
->blocks_per_seg
;
274 valid_sum_count
+= curseg_blkoff(sbi
, i
);
277 sum_in_page
= (PAGE_CACHE_SIZE
- 2 * SUM_JOURNAL_SIZE
-
278 SUM_FOOTER_SIZE
) / SUMMARY_SIZE
;
279 if (valid_sum_count
<= sum_in_page
)
281 else if ((valid_sum_count
- sum_in_page
) <=
282 (PAGE_CACHE_SIZE
- SUM_FOOTER_SIZE
) / SUMMARY_SIZE
)
288 * Caller should put this summary page
290 struct page
*get_sum_page(struct f2fs_sb_info
*sbi
, unsigned int segno
)
292 return get_meta_page(sbi
, GET_SUM_BLOCK(sbi
, segno
));
295 static void write_sum_page(struct f2fs_sb_info
*sbi
,
296 struct f2fs_summary_block
*sum_blk
, block_t blk_addr
)
298 struct page
*page
= grab_meta_page(sbi
, blk_addr
);
299 void *kaddr
= page_address(page
);
300 memcpy(kaddr
, sum_blk
, PAGE_CACHE_SIZE
);
301 set_page_dirty(page
);
302 f2fs_put_page(page
, 1);
305 static int is_next_segment_free(struct f2fs_sb_info
*sbi
, int type
)
307 struct curseg_info
*curseg
= CURSEG_I(sbi
, type
);
308 unsigned int segno
= curseg
->segno
+ 1;
309 struct free_segmap_info
*free_i
= FREE_I(sbi
);
311 if (segno
< TOTAL_SEGS(sbi
) && segno
% sbi
->segs_per_sec
)
312 return !test_bit(segno
, free_i
->free_segmap
);
317 * Find a new segment from the free segments bitmap to right order
318 * This function should be returned with success, otherwise BUG
320 static void get_new_segment(struct f2fs_sb_info
*sbi
,
321 unsigned int *newseg
, bool new_sec
, int dir
)
323 struct free_segmap_info
*free_i
= FREE_I(sbi
);
324 unsigned int segno
, secno
, zoneno
;
325 unsigned int total_zones
= TOTAL_SECS(sbi
) / sbi
->secs_per_zone
;
326 unsigned int hint
= *newseg
/ sbi
->segs_per_sec
;
327 unsigned int old_zoneno
= GET_ZONENO_FROM_SEGNO(sbi
, *newseg
);
328 unsigned int left_start
= hint
;
333 write_lock(&free_i
->segmap_lock
);
335 if (!new_sec
&& ((*newseg
+ 1) % sbi
->segs_per_sec
)) {
336 segno
= find_next_zero_bit(free_i
->free_segmap
,
337 TOTAL_SEGS(sbi
), *newseg
+ 1);
338 if (segno
- *newseg
< sbi
->segs_per_sec
-
339 (*newseg
% sbi
->segs_per_sec
))
343 secno
= find_next_zero_bit(free_i
->free_secmap
, TOTAL_SECS(sbi
), hint
);
344 if (secno
>= TOTAL_SECS(sbi
)) {
345 if (dir
== ALLOC_RIGHT
) {
346 secno
= find_next_zero_bit(free_i
->free_secmap
,
348 f2fs_bug_on(secno
>= TOTAL_SECS(sbi
));
351 left_start
= hint
- 1;
357 while (test_bit(left_start
, free_i
->free_secmap
)) {
358 if (left_start
> 0) {
362 left_start
= find_next_zero_bit(free_i
->free_secmap
,
364 f2fs_bug_on(left_start
>= TOTAL_SECS(sbi
));
370 segno
= secno
* sbi
->segs_per_sec
;
371 zoneno
= secno
/ sbi
->secs_per_zone
;
373 /* give up on finding another zone */
376 if (sbi
->secs_per_zone
== 1)
378 if (zoneno
== old_zoneno
)
380 if (dir
== ALLOC_LEFT
) {
381 if (!go_left
&& zoneno
+ 1 >= total_zones
)
383 if (go_left
&& zoneno
== 0)
386 for (i
= 0; i
< NR_CURSEG_TYPE
; i
++)
387 if (CURSEG_I(sbi
, i
)->zone
== zoneno
)
390 if (i
< NR_CURSEG_TYPE
) {
391 /* zone is in user, try another */
393 hint
= zoneno
* sbi
->secs_per_zone
- 1;
394 else if (zoneno
+ 1 >= total_zones
)
397 hint
= (zoneno
+ 1) * sbi
->secs_per_zone
;
399 goto find_other_zone
;
402 /* set it as dirty segment in free segmap */
403 f2fs_bug_on(test_bit(segno
, free_i
->free_segmap
));
404 __set_inuse(sbi
, segno
);
406 write_unlock(&free_i
->segmap_lock
);
409 static void reset_curseg(struct f2fs_sb_info
*sbi
, int type
, int modified
)
411 struct curseg_info
*curseg
= CURSEG_I(sbi
, type
);
412 struct summary_footer
*sum_footer
;
414 curseg
->segno
= curseg
->next_segno
;
415 curseg
->zone
= GET_ZONENO_FROM_SEGNO(sbi
, curseg
->segno
);
416 curseg
->next_blkoff
= 0;
417 curseg
->next_segno
= NULL_SEGNO
;
419 sum_footer
= &(curseg
->sum_blk
->footer
);
420 memset(sum_footer
, 0, sizeof(struct summary_footer
));
421 if (IS_DATASEG(type
))
422 SET_SUM_TYPE(sum_footer
, SUM_TYPE_DATA
);
423 if (IS_NODESEG(type
))
424 SET_SUM_TYPE(sum_footer
, SUM_TYPE_NODE
);
425 __set_sit_entry_type(sbi
, type
, curseg
->segno
, modified
);
429 * Allocate a current working segment.
430 * This function always allocates a free segment in LFS manner.
432 static void new_curseg(struct f2fs_sb_info
*sbi
, int type
, bool new_sec
)
434 struct curseg_info
*curseg
= CURSEG_I(sbi
, type
);
435 unsigned int segno
= curseg
->segno
;
436 int dir
= ALLOC_LEFT
;
438 write_sum_page(sbi
, curseg
->sum_blk
,
439 GET_SUM_BLOCK(sbi
, segno
));
440 if (type
== CURSEG_WARM_DATA
|| type
== CURSEG_COLD_DATA
)
443 if (test_opt(sbi
, NOHEAP
))
446 get_new_segment(sbi
, &segno
, new_sec
, dir
);
447 curseg
->next_segno
= segno
;
448 reset_curseg(sbi
, type
, 1);
449 curseg
->alloc_type
= LFS
;
452 static void __next_free_blkoff(struct f2fs_sb_info
*sbi
,
453 struct curseg_info
*seg
, block_t start
)
455 struct seg_entry
*se
= get_seg_entry(sbi
, seg
->segno
);
457 for (ofs
= start
; ofs
< sbi
->blocks_per_seg
; ofs
++) {
458 if (!f2fs_test_bit(ofs
, se
->ckpt_valid_map
)
459 && !f2fs_test_bit(ofs
, se
->cur_valid_map
))
462 seg
->next_blkoff
= ofs
;
466 * If a segment is written by LFS manner, next block offset is just obtained
467 * by increasing the current block offset. However, if a segment is written by
468 * SSR manner, next block offset obtained by calling __next_free_blkoff
470 static void __refresh_next_blkoff(struct f2fs_sb_info
*sbi
,
471 struct curseg_info
*seg
)
473 if (seg
->alloc_type
== SSR
)
474 __next_free_blkoff(sbi
, seg
, seg
->next_blkoff
+ 1);
480 * This function always allocates a used segment (from dirty seglist) by SSR
481 * manner, so it should recover the existing segment information of valid blocks
483 static void change_curseg(struct f2fs_sb_info
*sbi
, int type
, bool reuse
)
485 struct dirty_seglist_info
*dirty_i
= DIRTY_I(sbi
);
486 struct curseg_info
*curseg
= CURSEG_I(sbi
, type
);
487 unsigned int new_segno
= curseg
->next_segno
;
488 struct f2fs_summary_block
*sum_node
;
489 struct page
*sum_page
;
491 write_sum_page(sbi
, curseg
->sum_blk
,
492 GET_SUM_BLOCK(sbi
, curseg
->segno
));
493 __set_test_and_inuse(sbi
, new_segno
);
495 mutex_lock(&dirty_i
->seglist_lock
);
496 __remove_dirty_segment(sbi
, new_segno
, PRE
);
497 __remove_dirty_segment(sbi
, new_segno
, DIRTY
);
498 mutex_unlock(&dirty_i
->seglist_lock
);
500 reset_curseg(sbi
, type
, 1);
501 curseg
->alloc_type
= SSR
;
502 __next_free_blkoff(sbi
, curseg
, 0);
505 sum_page
= get_sum_page(sbi
, new_segno
);
506 sum_node
= (struct f2fs_summary_block
*)page_address(sum_page
);
507 memcpy(curseg
->sum_blk
, sum_node
, SUM_ENTRY_SIZE
);
508 f2fs_put_page(sum_page
, 1);
512 static int get_ssr_segment(struct f2fs_sb_info
*sbi
, int type
)
514 struct curseg_info
*curseg
= CURSEG_I(sbi
, type
);
515 const struct victim_selection
*v_ops
= DIRTY_I(sbi
)->v_ops
;
517 if (IS_NODESEG(type
) || !has_not_enough_free_secs(sbi
, 0))
518 return v_ops
->get_victim(sbi
,
519 &(curseg
)->next_segno
, BG_GC
, type
, SSR
);
521 /* For data segments, let's do SSR more intensively */
522 for (; type
>= CURSEG_HOT_DATA
; type
--)
523 if (v_ops
->get_victim(sbi
, &(curseg
)->next_segno
,
530 * flush out current segment and replace it with new segment
531 * This function should be returned with success, otherwise BUG
533 static void allocate_segment_by_default(struct f2fs_sb_info
*sbi
,
534 int type
, bool force
)
536 struct curseg_info
*curseg
= CURSEG_I(sbi
, type
);
539 new_curseg(sbi
, type
, true);
540 else if (type
== CURSEG_WARM_NODE
)
541 new_curseg(sbi
, type
, false);
542 else if (curseg
->alloc_type
== LFS
&& is_next_segment_free(sbi
, type
))
543 new_curseg(sbi
, type
, false);
544 else if (need_SSR(sbi
) && get_ssr_segment(sbi
, type
))
545 change_curseg(sbi
, type
, true);
547 new_curseg(sbi
, type
, false);
549 stat_inc_seg_type(sbi
, curseg
);
552 void allocate_new_segments(struct f2fs_sb_info
*sbi
)
554 struct curseg_info
*curseg
;
555 unsigned int old_curseg
;
558 for (i
= CURSEG_HOT_DATA
; i
<= CURSEG_COLD_DATA
; i
++) {
559 curseg
= CURSEG_I(sbi
, i
);
560 old_curseg
= curseg
->segno
;
561 SIT_I(sbi
)->s_ops
->allocate_segment(sbi
, i
, true);
562 locate_dirty_segment(sbi
, old_curseg
);
566 static const struct segment_allocation default_salloc_ops
= {
567 .allocate_segment
= allocate_segment_by_default
,
570 static void f2fs_end_io_write(struct bio
*bio
, int err
)
572 const int uptodate
= test_bit(BIO_UPTODATE
, &bio
->bi_flags
);
573 struct bio_vec
*bvec
= bio
->bi_io_vec
+ bio
->bi_vcnt
- 1;
574 struct bio_private
*p
= bio
->bi_private
;
577 struct page
*page
= bvec
->bv_page
;
579 if (--bvec
>= bio
->bi_io_vec
)
580 prefetchw(&bvec
->bv_page
->flags
);
584 set_bit(AS_EIO
, &page
->mapping
->flags
);
585 set_ckpt_flags(p
->sbi
->ckpt
, CP_ERROR_FLAG
);
586 p
->sbi
->sb
->s_flags
|= MS_RDONLY
;
588 end_page_writeback(page
);
589 dec_page_count(p
->sbi
, F2FS_WRITEBACK
);
590 } while (bvec
>= bio
->bi_io_vec
);
595 if (!get_pages(p
->sbi
, F2FS_WRITEBACK
) &&
596 !list_empty(&p
->sbi
->cp_wait
.task_list
))
597 wake_up(&p
->sbi
->cp_wait
);
603 struct bio
*f2fs_bio_alloc(struct block_device
*bdev
, int npages
)
607 /* No failure on bio allocation */
608 bio
= bio_alloc(GFP_NOIO
, npages
);
610 bio
->bi_private
= NULL
;
615 static void do_submit_bio(struct f2fs_sb_info
*sbi
,
616 enum page_type type
, bool sync
)
618 int rw
= sync
? WRITE_SYNC
: WRITE
;
619 enum page_type btype
= type
> META
? META
: type
;
621 if (type
>= META_FLUSH
)
622 rw
= WRITE_FLUSH_FUA
;
627 if (sbi
->bio
[btype
]) {
628 struct bio_private
*p
= sbi
->bio
[btype
]->bi_private
;
630 sbi
->bio
[btype
]->bi_end_io
= f2fs_end_io_write
;
632 trace_f2fs_do_submit_bio(sbi
->sb
, btype
, sync
, sbi
->bio
[btype
]);
634 if (type
== META_FLUSH
) {
635 DECLARE_COMPLETION_ONSTACK(wait
);
638 submit_bio(rw
, sbi
->bio
[btype
]);
639 wait_for_completion(&wait
);
642 submit_bio(rw
, sbi
->bio
[btype
]);
644 sbi
->bio
[btype
] = NULL
;
648 void f2fs_submit_bio(struct f2fs_sb_info
*sbi
, enum page_type type
, bool sync
)
650 down_write(&sbi
->bio_sem
);
651 do_submit_bio(sbi
, type
, sync
);
652 up_write(&sbi
->bio_sem
);
655 static void submit_write_page(struct f2fs_sb_info
*sbi
, struct page
*page
,
656 block_t blk_addr
, enum page_type type
)
658 struct block_device
*bdev
= sbi
->sb
->s_bdev
;
661 verify_block_addr(sbi
, blk_addr
);
663 down_write(&sbi
->bio_sem
);
665 inc_page_count(sbi
, F2FS_WRITEBACK
);
667 if (sbi
->bio
[type
] && sbi
->last_block_in_bio
[type
] != blk_addr
- 1)
668 do_submit_bio(sbi
, type
, false);
670 if (sbi
->bio
[type
] == NULL
) {
671 struct bio_private
*priv
;
673 priv
= kmalloc(sizeof(struct bio_private
), GFP_NOFS
);
679 bio_blocks
= MAX_BIO_BLOCKS(max_hw_blocks(sbi
));
680 sbi
->bio
[type
] = f2fs_bio_alloc(bdev
, bio_blocks
);
681 sbi
->bio
[type
]->bi_sector
= SECTOR_FROM_BLOCK(sbi
, blk_addr
);
682 sbi
->bio
[type
]->bi_private
= priv
;
684 * The end_io will be assigned at the sumbission phase.
685 * Until then, let bio_add_page() merge consecutive IOs as much
690 if (bio_add_page(sbi
->bio
[type
], page
, PAGE_CACHE_SIZE
, 0) <
692 do_submit_bio(sbi
, type
, false);
696 sbi
->last_block_in_bio
[type
] = blk_addr
;
698 up_write(&sbi
->bio_sem
);
699 trace_f2fs_submit_write_page(page
, blk_addr
, type
);
702 void f2fs_wait_on_page_writeback(struct page
*page
,
703 enum page_type type
, bool sync
)
705 struct f2fs_sb_info
*sbi
= F2FS_SB(page
->mapping
->host
->i_sb
);
706 if (PageWriteback(page
)) {
707 f2fs_submit_bio(sbi
, type
, sync
);
708 wait_on_page_writeback(page
);
712 static bool __has_curseg_space(struct f2fs_sb_info
*sbi
, int type
)
714 struct curseg_info
*curseg
= CURSEG_I(sbi
, type
);
715 if (curseg
->next_blkoff
< sbi
->blocks_per_seg
)
720 static int __get_segment_type_2(struct page
*page
, enum page_type p_type
)
723 return CURSEG_HOT_DATA
;
725 return CURSEG_HOT_NODE
;
728 static int __get_segment_type_4(struct page
*page
, enum page_type p_type
)
730 if (p_type
== DATA
) {
731 struct inode
*inode
= page
->mapping
->host
;
733 if (S_ISDIR(inode
->i_mode
))
734 return CURSEG_HOT_DATA
;
736 return CURSEG_COLD_DATA
;
738 if (IS_DNODE(page
) && !is_cold_node(page
))
739 return CURSEG_HOT_NODE
;
741 return CURSEG_COLD_NODE
;
745 static int __get_segment_type_6(struct page
*page
, enum page_type p_type
)
747 if (p_type
== DATA
) {
748 struct inode
*inode
= page
->mapping
->host
;
750 if (S_ISDIR(inode
->i_mode
))
751 return CURSEG_HOT_DATA
;
752 else if (is_cold_data(page
) || file_is_cold(inode
))
753 return CURSEG_COLD_DATA
;
755 return CURSEG_WARM_DATA
;
758 return is_cold_node(page
) ? CURSEG_WARM_NODE
:
761 return CURSEG_COLD_NODE
;
765 static int __get_segment_type(struct page
*page
, enum page_type p_type
)
767 struct f2fs_sb_info
*sbi
= F2FS_SB(page
->mapping
->host
->i_sb
);
768 switch (sbi
->active_logs
) {
770 return __get_segment_type_2(page
, p_type
);
772 return __get_segment_type_4(page
, p_type
);
774 /* NR_CURSEG_TYPE(6) logs by default */
775 f2fs_bug_on(sbi
->active_logs
!= NR_CURSEG_TYPE
);
776 return __get_segment_type_6(page
, p_type
);
779 static void do_write_page(struct f2fs_sb_info
*sbi
, struct page
*page
,
780 block_t old_blkaddr
, block_t
*new_blkaddr
,
781 struct f2fs_summary
*sum
, enum page_type p_type
)
783 struct sit_info
*sit_i
= SIT_I(sbi
);
784 struct curseg_info
*curseg
;
785 unsigned int old_cursegno
;
788 type
= __get_segment_type(page
, p_type
);
789 curseg
= CURSEG_I(sbi
, type
);
791 mutex_lock(&curseg
->curseg_mutex
);
793 *new_blkaddr
= NEXT_FREE_BLKADDR(sbi
, curseg
);
794 old_cursegno
= curseg
->segno
;
797 * __add_sum_entry should be resided under the curseg_mutex
798 * because, this function updates a summary entry in the
799 * current summary block.
801 __add_sum_entry(sbi
, type
, sum
);
803 mutex_lock(&sit_i
->sentry_lock
);
804 __refresh_next_blkoff(sbi
, curseg
);
806 stat_inc_block_count(sbi
, curseg
);
809 * SIT information should be updated before segment allocation,
810 * since SSR needs latest valid block information.
812 refresh_sit_entry(sbi
, old_blkaddr
, *new_blkaddr
);
814 if (!__has_curseg_space(sbi
, type
))
815 sit_i
->s_ops
->allocate_segment(sbi
, type
, false);
817 locate_dirty_segment(sbi
, old_cursegno
);
818 locate_dirty_segment(sbi
, GET_SEGNO(sbi
, old_blkaddr
));
819 mutex_unlock(&sit_i
->sentry_lock
);
822 fill_node_footer_blkaddr(page
, NEXT_FREE_BLKADDR(sbi
, curseg
));
824 /* writeout dirty page into bdev */
825 submit_write_page(sbi
, page
, *new_blkaddr
, p_type
);
827 mutex_unlock(&curseg
->curseg_mutex
);
830 void write_meta_page(struct f2fs_sb_info
*sbi
, struct page
*page
)
832 set_page_writeback(page
);
833 submit_write_page(sbi
, page
, page
->index
, META
);
836 void write_node_page(struct f2fs_sb_info
*sbi
, struct page
*page
,
837 unsigned int nid
, block_t old_blkaddr
, block_t
*new_blkaddr
)
839 struct f2fs_summary sum
;
840 set_summary(&sum
, nid
, 0, 0);
841 do_write_page(sbi
, page
, old_blkaddr
, new_blkaddr
, &sum
, NODE
);
844 void write_data_page(struct inode
*inode
, struct page
*page
,
845 struct dnode_of_data
*dn
, block_t old_blkaddr
,
846 block_t
*new_blkaddr
)
848 struct f2fs_sb_info
*sbi
= F2FS_SB(inode
->i_sb
);
849 struct f2fs_summary sum
;
852 f2fs_bug_on(old_blkaddr
== NULL_ADDR
);
853 get_node_info(sbi
, dn
->nid
, &ni
);
854 set_summary(&sum
, dn
->nid
, dn
->ofs_in_node
, ni
.version
);
856 do_write_page(sbi
, page
, old_blkaddr
,
857 new_blkaddr
, &sum
, DATA
);
860 void rewrite_data_page(struct f2fs_sb_info
*sbi
, struct page
*page
,
861 block_t old_blk_addr
)
863 submit_write_page(sbi
, page
, old_blk_addr
, DATA
);
866 void recover_data_page(struct f2fs_sb_info
*sbi
,
867 struct page
*page
, struct f2fs_summary
*sum
,
868 block_t old_blkaddr
, block_t new_blkaddr
)
870 struct sit_info
*sit_i
= SIT_I(sbi
);
871 struct curseg_info
*curseg
;
872 unsigned int segno
, old_cursegno
;
873 struct seg_entry
*se
;
876 segno
= GET_SEGNO(sbi
, new_blkaddr
);
877 se
= get_seg_entry(sbi
, segno
);
880 if (se
->valid_blocks
== 0 && !IS_CURSEG(sbi
, segno
)) {
881 if (old_blkaddr
== NULL_ADDR
)
882 type
= CURSEG_COLD_DATA
;
884 type
= CURSEG_WARM_DATA
;
886 curseg
= CURSEG_I(sbi
, type
);
888 mutex_lock(&curseg
->curseg_mutex
);
889 mutex_lock(&sit_i
->sentry_lock
);
891 old_cursegno
= curseg
->segno
;
893 /* change the current segment */
894 if (segno
!= curseg
->segno
) {
895 curseg
->next_segno
= segno
;
896 change_curseg(sbi
, type
, true);
899 curseg
->next_blkoff
= GET_SEGOFF_FROM_SEG0(sbi
, new_blkaddr
) &
900 (sbi
->blocks_per_seg
- 1);
901 __add_sum_entry(sbi
, type
, sum
);
903 refresh_sit_entry(sbi
, old_blkaddr
, new_blkaddr
);
905 locate_dirty_segment(sbi
, old_cursegno
);
906 locate_dirty_segment(sbi
, GET_SEGNO(sbi
, old_blkaddr
));
908 mutex_unlock(&sit_i
->sentry_lock
);
909 mutex_unlock(&curseg
->curseg_mutex
);
912 void rewrite_node_page(struct f2fs_sb_info
*sbi
,
913 struct page
*page
, struct f2fs_summary
*sum
,
914 block_t old_blkaddr
, block_t new_blkaddr
)
916 struct sit_info
*sit_i
= SIT_I(sbi
);
917 int type
= CURSEG_WARM_NODE
;
918 struct curseg_info
*curseg
;
919 unsigned int segno
, old_cursegno
;
920 block_t next_blkaddr
= next_blkaddr_of_node(page
);
921 unsigned int next_segno
= GET_SEGNO(sbi
, next_blkaddr
);
923 curseg
= CURSEG_I(sbi
, type
);
925 mutex_lock(&curseg
->curseg_mutex
);
926 mutex_lock(&sit_i
->sentry_lock
);
928 segno
= GET_SEGNO(sbi
, new_blkaddr
);
929 old_cursegno
= curseg
->segno
;
931 /* change the current segment */
932 if (segno
!= curseg
->segno
) {
933 curseg
->next_segno
= segno
;
934 change_curseg(sbi
, type
, true);
936 curseg
->next_blkoff
= GET_SEGOFF_FROM_SEG0(sbi
, new_blkaddr
) &
937 (sbi
->blocks_per_seg
- 1);
938 __add_sum_entry(sbi
, type
, sum
);
940 /* change the current log to the next block addr in advance */
941 if (next_segno
!= segno
) {
942 curseg
->next_segno
= next_segno
;
943 change_curseg(sbi
, type
, true);
945 curseg
->next_blkoff
= GET_SEGOFF_FROM_SEG0(sbi
, next_blkaddr
) &
946 (sbi
->blocks_per_seg
- 1);
948 /* rewrite node page */
949 set_page_writeback(page
);
950 submit_write_page(sbi
, page
, new_blkaddr
, NODE
);
951 f2fs_submit_bio(sbi
, NODE
, true);
952 refresh_sit_entry(sbi
, old_blkaddr
, new_blkaddr
);
954 locate_dirty_segment(sbi
, old_cursegno
);
955 locate_dirty_segment(sbi
, GET_SEGNO(sbi
, old_blkaddr
));
957 mutex_unlock(&sit_i
->sentry_lock
);
958 mutex_unlock(&curseg
->curseg_mutex
);
961 static int read_compacted_summaries(struct f2fs_sb_info
*sbi
)
963 struct f2fs_checkpoint
*ckpt
= F2FS_CKPT(sbi
);
964 struct curseg_info
*seg_i
;
965 unsigned char *kaddr
;
970 start
= start_sum_block(sbi
);
972 page
= get_meta_page(sbi
, start
++);
973 kaddr
= (unsigned char *)page_address(page
);
975 /* Step 1: restore nat cache */
976 seg_i
= CURSEG_I(sbi
, CURSEG_HOT_DATA
);
977 memcpy(&seg_i
->sum_blk
->n_nats
, kaddr
, SUM_JOURNAL_SIZE
);
979 /* Step 2: restore sit cache */
980 seg_i
= CURSEG_I(sbi
, CURSEG_COLD_DATA
);
981 memcpy(&seg_i
->sum_blk
->n_sits
, kaddr
+ SUM_JOURNAL_SIZE
,
983 offset
= 2 * SUM_JOURNAL_SIZE
;
985 /* Step 3: restore summary entries */
986 for (i
= CURSEG_HOT_DATA
; i
<= CURSEG_COLD_DATA
; i
++) {
987 unsigned short blk_off
;
990 seg_i
= CURSEG_I(sbi
, i
);
991 segno
= le32_to_cpu(ckpt
->cur_data_segno
[i
]);
992 blk_off
= le16_to_cpu(ckpt
->cur_data_blkoff
[i
]);
993 seg_i
->next_segno
= segno
;
994 reset_curseg(sbi
, i
, 0);
995 seg_i
->alloc_type
= ckpt
->alloc_type
[i
];
996 seg_i
->next_blkoff
= blk_off
;
998 if (seg_i
->alloc_type
== SSR
)
999 blk_off
= sbi
->blocks_per_seg
;
1001 for (j
= 0; j
< blk_off
; j
++) {
1002 struct f2fs_summary
*s
;
1003 s
= (struct f2fs_summary
*)(kaddr
+ offset
);
1004 seg_i
->sum_blk
->entries
[j
] = *s
;
1005 offset
+= SUMMARY_SIZE
;
1006 if (offset
+ SUMMARY_SIZE
<= PAGE_CACHE_SIZE
-
1010 f2fs_put_page(page
, 1);
1013 page
= get_meta_page(sbi
, start
++);
1014 kaddr
= (unsigned char *)page_address(page
);
1018 f2fs_put_page(page
, 1);
1022 static int read_normal_summaries(struct f2fs_sb_info
*sbi
, int type
)
1024 struct f2fs_checkpoint
*ckpt
= F2FS_CKPT(sbi
);
1025 struct f2fs_summary_block
*sum
;
1026 struct curseg_info
*curseg
;
1028 unsigned short blk_off
;
1029 unsigned int segno
= 0;
1030 block_t blk_addr
= 0;
1032 /* get segment number and block addr */
1033 if (IS_DATASEG(type
)) {
1034 segno
= le32_to_cpu(ckpt
->cur_data_segno
[type
]);
1035 blk_off
= le16_to_cpu(ckpt
->cur_data_blkoff
[type
-
1037 if (is_set_ckpt_flags(ckpt
, CP_UMOUNT_FLAG
))
1038 blk_addr
= sum_blk_addr(sbi
, NR_CURSEG_TYPE
, type
);
1040 blk_addr
= sum_blk_addr(sbi
, NR_CURSEG_DATA_TYPE
, type
);
1042 segno
= le32_to_cpu(ckpt
->cur_node_segno
[type
-
1044 blk_off
= le16_to_cpu(ckpt
->cur_node_blkoff
[type
-
1046 if (is_set_ckpt_flags(ckpt
, CP_UMOUNT_FLAG
))
1047 blk_addr
= sum_blk_addr(sbi
, NR_CURSEG_NODE_TYPE
,
1048 type
- CURSEG_HOT_NODE
);
1050 blk_addr
= GET_SUM_BLOCK(sbi
, segno
);
1053 new = get_meta_page(sbi
, blk_addr
);
1054 sum
= (struct f2fs_summary_block
*)page_address(new);
1056 if (IS_NODESEG(type
)) {
1057 if (is_set_ckpt_flags(ckpt
, CP_UMOUNT_FLAG
)) {
1058 struct f2fs_summary
*ns
= &sum
->entries
[0];
1060 for (i
= 0; i
< sbi
->blocks_per_seg
; i
++, ns
++) {
1062 ns
->ofs_in_node
= 0;
1065 if (restore_node_summary(sbi
, segno
, sum
)) {
1066 f2fs_put_page(new, 1);
1072 /* set uncompleted segment to curseg */
1073 curseg
= CURSEG_I(sbi
, type
);
1074 mutex_lock(&curseg
->curseg_mutex
);
1075 memcpy(curseg
->sum_blk
, sum
, PAGE_CACHE_SIZE
);
1076 curseg
->next_segno
= segno
;
1077 reset_curseg(sbi
, type
, 0);
1078 curseg
->alloc_type
= ckpt
->alloc_type
[type
];
1079 curseg
->next_blkoff
= blk_off
;
1080 mutex_unlock(&curseg
->curseg_mutex
);
1081 f2fs_put_page(new, 1);
1085 static int restore_curseg_summaries(struct f2fs_sb_info
*sbi
)
1087 int type
= CURSEG_HOT_DATA
;
1089 if (is_set_ckpt_flags(F2FS_CKPT(sbi
), CP_COMPACT_SUM_FLAG
)) {
1090 /* restore for compacted data summary */
1091 if (read_compacted_summaries(sbi
))
1093 type
= CURSEG_HOT_NODE
;
1096 for (; type
<= CURSEG_COLD_NODE
; type
++)
1097 if (read_normal_summaries(sbi
, type
))
1102 static void write_compacted_summaries(struct f2fs_sb_info
*sbi
, block_t blkaddr
)
1105 unsigned char *kaddr
;
1106 struct f2fs_summary
*summary
;
1107 struct curseg_info
*seg_i
;
1108 int written_size
= 0;
1111 page
= grab_meta_page(sbi
, blkaddr
++);
1112 kaddr
= (unsigned char *)page_address(page
);
1114 /* Step 1: write nat cache */
1115 seg_i
= CURSEG_I(sbi
, CURSEG_HOT_DATA
);
1116 memcpy(kaddr
, &seg_i
->sum_blk
->n_nats
, SUM_JOURNAL_SIZE
);
1117 written_size
+= SUM_JOURNAL_SIZE
;
1119 /* Step 2: write sit cache */
1120 seg_i
= CURSEG_I(sbi
, CURSEG_COLD_DATA
);
1121 memcpy(kaddr
+ written_size
, &seg_i
->sum_blk
->n_sits
,
1123 written_size
+= SUM_JOURNAL_SIZE
;
1125 /* Step 3: write summary entries */
1126 for (i
= CURSEG_HOT_DATA
; i
<= CURSEG_COLD_DATA
; i
++) {
1127 unsigned short blkoff
;
1128 seg_i
= CURSEG_I(sbi
, i
);
1129 if (sbi
->ckpt
->alloc_type
[i
] == SSR
)
1130 blkoff
= sbi
->blocks_per_seg
;
1132 blkoff
= curseg_blkoff(sbi
, i
);
1134 for (j
= 0; j
< blkoff
; j
++) {
1136 page
= grab_meta_page(sbi
, blkaddr
++);
1137 kaddr
= (unsigned char *)page_address(page
);
1140 summary
= (struct f2fs_summary
*)(kaddr
+ written_size
);
1141 *summary
= seg_i
->sum_blk
->entries
[j
];
1142 written_size
+= SUMMARY_SIZE
;
1144 if (written_size
+ SUMMARY_SIZE
<= PAGE_CACHE_SIZE
-
1148 set_page_dirty(page
);
1149 f2fs_put_page(page
, 1);
1154 set_page_dirty(page
);
1155 f2fs_put_page(page
, 1);
1159 static void write_normal_summaries(struct f2fs_sb_info
*sbi
,
1160 block_t blkaddr
, int type
)
1163 if (IS_DATASEG(type
))
1164 end
= type
+ NR_CURSEG_DATA_TYPE
;
1166 end
= type
+ NR_CURSEG_NODE_TYPE
;
1168 for (i
= type
; i
< end
; i
++) {
1169 struct curseg_info
*sum
= CURSEG_I(sbi
, i
);
1170 mutex_lock(&sum
->curseg_mutex
);
1171 write_sum_page(sbi
, sum
->sum_blk
, blkaddr
+ (i
- type
));
1172 mutex_unlock(&sum
->curseg_mutex
);
1176 void write_data_summaries(struct f2fs_sb_info
*sbi
, block_t start_blk
)
1178 if (is_set_ckpt_flags(F2FS_CKPT(sbi
), CP_COMPACT_SUM_FLAG
))
1179 write_compacted_summaries(sbi
, start_blk
);
1181 write_normal_summaries(sbi
, start_blk
, CURSEG_HOT_DATA
);
1184 void write_node_summaries(struct f2fs_sb_info
*sbi
, block_t start_blk
)
1186 if (is_set_ckpt_flags(F2FS_CKPT(sbi
), CP_UMOUNT_FLAG
))
1187 write_normal_summaries(sbi
, start_blk
, CURSEG_HOT_NODE
);
1190 int lookup_journal_in_cursum(struct f2fs_summary_block
*sum
, int type
,
1191 unsigned int val
, int alloc
)
1195 if (type
== NAT_JOURNAL
) {
1196 for (i
= 0; i
< nats_in_cursum(sum
); i
++) {
1197 if (le32_to_cpu(nid_in_journal(sum
, i
)) == val
)
1200 if (alloc
&& nats_in_cursum(sum
) < NAT_JOURNAL_ENTRIES
)
1201 return update_nats_in_cursum(sum
, 1);
1202 } else if (type
== SIT_JOURNAL
) {
1203 for (i
= 0; i
< sits_in_cursum(sum
); i
++)
1204 if (le32_to_cpu(segno_in_journal(sum
, i
)) == val
)
1206 if (alloc
&& sits_in_cursum(sum
) < SIT_JOURNAL_ENTRIES
)
1207 return update_sits_in_cursum(sum
, 1);
1212 static struct page
*get_current_sit_page(struct f2fs_sb_info
*sbi
,
1215 struct sit_info
*sit_i
= SIT_I(sbi
);
1216 unsigned int offset
= SIT_BLOCK_OFFSET(sit_i
, segno
);
1217 block_t blk_addr
= sit_i
->sit_base_addr
+ offset
;
1219 check_seg_range(sbi
, segno
);
1221 /* calculate sit block address */
1222 if (f2fs_test_bit(offset
, sit_i
->sit_bitmap
))
1223 blk_addr
+= sit_i
->sit_blocks
;
1225 return get_meta_page(sbi
, blk_addr
);
1228 static struct page
*get_next_sit_page(struct f2fs_sb_info
*sbi
,
1231 struct sit_info
*sit_i
= SIT_I(sbi
);
1232 struct page
*src_page
, *dst_page
;
1233 pgoff_t src_off
, dst_off
;
1234 void *src_addr
, *dst_addr
;
1236 src_off
= current_sit_addr(sbi
, start
);
1237 dst_off
= next_sit_addr(sbi
, src_off
);
1239 /* get current sit block page without lock */
1240 src_page
= get_meta_page(sbi
, src_off
);
1241 dst_page
= grab_meta_page(sbi
, dst_off
);
1242 f2fs_bug_on(PageDirty(src_page
));
1244 src_addr
= page_address(src_page
);
1245 dst_addr
= page_address(dst_page
);
1246 memcpy(dst_addr
, src_addr
, PAGE_CACHE_SIZE
);
1248 set_page_dirty(dst_page
);
1249 f2fs_put_page(src_page
, 1);
1251 set_to_next_sit(sit_i
, start
);
1256 static bool flush_sits_in_journal(struct f2fs_sb_info
*sbi
)
1258 struct curseg_info
*curseg
= CURSEG_I(sbi
, CURSEG_COLD_DATA
);
1259 struct f2fs_summary_block
*sum
= curseg
->sum_blk
;
1263 * If the journal area in the current summary is full of sit entries,
1264 * all the sit entries will be flushed. Otherwise the sit entries
1265 * are not able to replace with newly hot sit entries.
1267 if (sits_in_cursum(sum
) >= SIT_JOURNAL_ENTRIES
) {
1268 for (i
= sits_in_cursum(sum
) - 1; i
>= 0; i
--) {
1270 segno
= le32_to_cpu(segno_in_journal(sum
, i
));
1271 __mark_sit_entry_dirty(sbi
, segno
);
1273 update_sits_in_cursum(sum
, -sits_in_cursum(sum
));
1280 * CP calls this function, which flushes SIT entries including sit_journal,
1281 * and moves prefree segs to free segs.
1283 void flush_sit_entries(struct f2fs_sb_info
*sbi
)
1285 struct sit_info
*sit_i
= SIT_I(sbi
);
1286 unsigned long *bitmap
= sit_i
->dirty_sentries_bitmap
;
1287 struct curseg_info
*curseg
= CURSEG_I(sbi
, CURSEG_COLD_DATA
);
1288 struct f2fs_summary_block
*sum
= curseg
->sum_blk
;
1289 unsigned long nsegs
= TOTAL_SEGS(sbi
);
1290 struct page
*page
= NULL
;
1291 struct f2fs_sit_block
*raw_sit
= NULL
;
1292 unsigned int start
= 0, end
= 0;
1293 unsigned int segno
= -1;
1296 mutex_lock(&curseg
->curseg_mutex
);
1297 mutex_lock(&sit_i
->sentry_lock
);
1300 * "flushed" indicates whether sit entries in journal are flushed
1301 * to the SIT area or not.
1303 flushed
= flush_sits_in_journal(sbi
);
1305 while ((segno
= find_next_bit(bitmap
, nsegs
, segno
+ 1)) < nsegs
) {
1306 struct seg_entry
*se
= get_seg_entry(sbi
, segno
);
1307 int sit_offset
, offset
;
1309 sit_offset
= SIT_ENTRY_OFFSET(sit_i
, segno
);
1314 offset
= lookup_journal_in_cursum(sum
, SIT_JOURNAL
, segno
, 1);
1316 segno_in_journal(sum
, offset
) = cpu_to_le32(segno
);
1317 seg_info_to_raw_sit(se
, &sit_in_journal(sum
, offset
));
1321 if (!page
|| (start
> segno
) || (segno
> end
)) {
1323 f2fs_put_page(page
, 1);
1327 start
= START_SEGNO(sit_i
, segno
);
1328 end
= start
+ SIT_ENTRY_PER_BLOCK
- 1;
1330 /* read sit block that will be updated */
1331 page
= get_next_sit_page(sbi
, start
);
1332 raw_sit
= page_address(page
);
1335 /* udpate entry in SIT block */
1336 seg_info_to_raw_sit(se
, &raw_sit
->entries
[sit_offset
]);
1338 __clear_bit(segno
, bitmap
);
1339 sit_i
->dirty_sentries
--;
1341 mutex_unlock(&sit_i
->sentry_lock
);
1342 mutex_unlock(&curseg
->curseg_mutex
);
1344 /* writeout last modified SIT block */
1345 f2fs_put_page(page
, 1);
1347 set_prefree_as_free_segments(sbi
);
1350 static int build_sit_info(struct f2fs_sb_info
*sbi
)
1352 struct f2fs_super_block
*raw_super
= F2FS_RAW_SUPER(sbi
);
1353 struct f2fs_checkpoint
*ckpt
= F2FS_CKPT(sbi
);
1354 struct sit_info
*sit_i
;
1355 unsigned int sit_segs
, start
;
1356 char *src_bitmap
, *dst_bitmap
;
1357 unsigned int bitmap_size
;
1359 /* allocate memory for SIT information */
1360 sit_i
= kzalloc(sizeof(struct sit_info
), GFP_KERNEL
);
1364 SM_I(sbi
)->sit_info
= sit_i
;
1366 sit_i
->sentries
= vzalloc(TOTAL_SEGS(sbi
) * sizeof(struct seg_entry
));
1367 if (!sit_i
->sentries
)
1370 bitmap_size
= f2fs_bitmap_size(TOTAL_SEGS(sbi
));
1371 sit_i
->dirty_sentries_bitmap
= kzalloc(bitmap_size
, GFP_KERNEL
);
1372 if (!sit_i
->dirty_sentries_bitmap
)
1375 for (start
= 0; start
< TOTAL_SEGS(sbi
); start
++) {
1376 sit_i
->sentries
[start
].cur_valid_map
1377 = kzalloc(SIT_VBLOCK_MAP_SIZE
, GFP_KERNEL
);
1378 sit_i
->sentries
[start
].ckpt_valid_map
1379 = kzalloc(SIT_VBLOCK_MAP_SIZE
, GFP_KERNEL
);
1380 if (!sit_i
->sentries
[start
].cur_valid_map
1381 || !sit_i
->sentries
[start
].ckpt_valid_map
)
1385 if (sbi
->segs_per_sec
> 1) {
1386 sit_i
->sec_entries
= vzalloc(TOTAL_SECS(sbi
) *
1387 sizeof(struct sec_entry
));
1388 if (!sit_i
->sec_entries
)
1392 /* get information related with SIT */
1393 sit_segs
= le32_to_cpu(raw_super
->segment_count_sit
) >> 1;
1395 /* setup SIT bitmap from ckeckpoint pack */
1396 bitmap_size
= __bitmap_size(sbi
, SIT_BITMAP
);
1397 src_bitmap
= __bitmap_ptr(sbi
, SIT_BITMAP
);
1399 dst_bitmap
= kmemdup(src_bitmap
, bitmap_size
, GFP_KERNEL
);
1403 /* init SIT information */
1404 sit_i
->s_ops
= &default_salloc_ops
;
1406 sit_i
->sit_base_addr
= le32_to_cpu(raw_super
->sit_blkaddr
);
1407 sit_i
->sit_blocks
= sit_segs
<< sbi
->log_blocks_per_seg
;
1408 sit_i
->written_valid_blocks
= le64_to_cpu(ckpt
->valid_block_count
);
1409 sit_i
->sit_bitmap
= dst_bitmap
;
1410 sit_i
->bitmap_size
= bitmap_size
;
1411 sit_i
->dirty_sentries
= 0;
1412 sit_i
->sents_per_block
= SIT_ENTRY_PER_BLOCK
;
1413 sit_i
->elapsed_time
= le64_to_cpu(sbi
->ckpt
->elapsed_time
);
1414 sit_i
->mounted_time
= CURRENT_TIME_SEC
.tv_sec
;
1415 mutex_init(&sit_i
->sentry_lock
);
1419 static int build_free_segmap(struct f2fs_sb_info
*sbi
)
1421 struct f2fs_sm_info
*sm_info
= SM_I(sbi
);
1422 struct free_segmap_info
*free_i
;
1423 unsigned int bitmap_size
, sec_bitmap_size
;
1425 /* allocate memory for free segmap information */
1426 free_i
= kzalloc(sizeof(struct free_segmap_info
), GFP_KERNEL
);
1430 SM_I(sbi
)->free_info
= free_i
;
1432 bitmap_size
= f2fs_bitmap_size(TOTAL_SEGS(sbi
));
1433 free_i
->free_segmap
= kmalloc(bitmap_size
, GFP_KERNEL
);
1434 if (!free_i
->free_segmap
)
1437 sec_bitmap_size
= f2fs_bitmap_size(TOTAL_SECS(sbi
));
1438 free_i
->free_secmap
= kmalloc(sec_bitmap_size
, GFP_KERNEL
);
1439 if (!free_i
->free_secmap
)
1442 /* set all segments as dirty temporarily */
1443 memset(free_i
->free_segmap
, 0xff, bitmap_size
);
1444 memset(free_i
->free_secmap
, 0xff, sec_bitmap_size
);
1446 /* init free segmap information */
1447 free_i
->start_segno
=
1448 (unsigned int) GET_SEGNO_FROM_SEG0(sbi
, sm_info
->main_blkaddr
);
1449 free_i
->free_segments
= 0;
1450 free_i
->free_sections
= 0;
1451 rwlock_init(&free_i
->segmap_lock
);
1455 static int build_curseg(struct f2fs_sb_info
*sbi
)
1457 struct curseg_info
*array
;
1460 array
= kzalloc(sizeof(*array
) * NR_CURSEG_TYPE
, GFP_KERNEL
);
1464 SM_I(sbi
)->curseg_array
= array
;
1466 for (i
= 0; i
< NR_CURSEG_TYPE
; i
++) {
1467 mutex_init(&array
[i
].curseg_mutex
);
1468 array
[i
].sum_blk
= kzalloc(PAGE_CACHE_SIZE
, GFP_KERNEL
);
1469 if (!array
[i
].sum_blk
)
1471 array
[i
].segno
= NULL_SEGNO
;
1472 array
[i
].next_blkoff
= 0;
1474 return restore_curseg_summaries(sbi
);
1477 static void build_sit_entries(struct f2fs_sb_info
*sbi
)
1479 struct sit_info
*sit_i
= SIT_I(sbi
);
1480 struct curseg_info
*curseg
= CURSEG_I(sbi
, CURSEG_COLD_DATA
);
1481 struct f2fs_summary_block
*sum
= curseg
->sum_blk
;
1484 for (start
= 0; start
< TOTAL_SEGS(sbi
); start
++) {
1485 struct seg_entry
*se
= &sit_i
->sentries
[start
];
1486 struct f2fs_sit_block
*sit_blk
;
1487 struct f2fs_sit_entry sit
;
1491 mutex_lock(&curseg
->curseg_mutex
);
1492 for (i
= 0; i
< sits_in_cursum(sum
); i
++) {
1493 if (le32_to_cpu(segno_in_journal(sum
, i
)) == start
) {
1494 sit
= sit_in_journal(sum
, i
);
1495 mutex_unlock(&curseg
->curseg_mutex
);
1499 mutex_unlock(&curseg
->curseg_mutex
);
1500 page
= get_current_sit_page(sbi
, start
);
1501 sit_blk
= (struct f2fs_sit_block
*)page_address(page
);
1502 sit
= sit_blk
->entries
[SIT_ENTRY_OFFSET(sit_i
, start
)];
1503 f2fs_put_page(page
, 1);
1505 check_block_count(sbi
, start
, &sit
);
1506 seg_info_from_raw_sit(se
, &sit
);
1507 if (sbi
->segs_per_sec
> 1) {
1508 struct sec_entry
*e
= get_sec_entry(sbi
, start
);
1509 e
->valid_blocks
+= se
->valid_blocks
;
1514 static void init_free_segmap(struct f2fs_sb_info
*sbi
)
1519 for (start
= 0; start
< TOTAL_SEGS(sbi
); start
++) {
1520 struct seg_entry
*sentry
= get_seg_entry(sbi
, start
);
1521 if (!sentry
->valid_blocks
)
1522 __set_free(sbi
, start
);
1525 /* set use the current segments */
1526 for (type
= CURSEG_HOT_DATA
; type
<= CURSEG_COLD_NODE
; type
++) {
1527 struct curseg_info
*curseg_t
= CURSEG_I(sbi
, type
);
1528 __set_test_and_inuse(sbi
, curseg_t
->segno
);
1532 static void init_dirty_segmap(struct f2fs_sb_info
*sbi
)
1534 struct dirty_seglist_info
*dirty_i
= DIRTY_I(sbi
);
1535 struct free_segmap_info
*free_i
= FREE_I(sbi
);
1536 unsigned int segno
= 0, offset
= 0, total_segs
= TOTAL_SEGS(sbi
);
1537 unsigned short valid_blocks
;
1540 /* find dirty segment based on free segmap */
1541 segno
= find_next_inuse(free_i
, total_segs
, offset
);
1542 if (segno
>= total_segs
)
1545 valid_blocks
= get_valid_blocks(sbi
, segno
, 0);
1546 if (valid_blocks
>= sbi
->blocks_per_seg
|| !valid_blocks
)
1548 mutex_lock(&dirty_i
->seglist_lock
);
1549 __locate_dirty_segment(sbi
, segno
, DIRTY
);
1550 mutex_unlock(&dirty_i
->seglist_lock
);
1554 static int init_victim_secmap(struct f2fs_sb_info
*sbi
)
1556 struct dirty_seglist_info
*dirty_i
= DIRTY_I(sbi
);
1557 unsigned int bitmap_size
= f2fs_bitmap_size(TOTAL_SECS(sbi
));
1559 dirty_i
->victim_secmap
= kzalloc(bitmap_size
, GFP_KERNEL
);
1560 if (!dirty_i
->victim_secmap
)
1565 static int build_dirty_segmap(struct f2fs_sb_info
*sbi
)
1567 struct dirty_seglist_info
*dirty_i
;
1568 unsigned int bitmap_size
, i
;
1570 /* allocate memory for dirty segments list information */
1571 dirty_i
= kzalloc(sizeof(struct dirty_seglist_info
), GFP_KERNEL
);
1575 SM_I(sbi
)->dirty_info
= dirty_i
;
1576 mutex_init(&dirty_i
->seglist_lock
);
1578 bitmap_size
= f2fs_bitmap_size(TOTAL_SEGS(sbi
));
1580 for (i
= 0; i
< NR_DIRTY_TYPE
; i
++) {
1581 dirty_i
->dirty_segmap
[i
] = kzalloc(bitmap_size
, GFP_KERNEL
);
1582 if (!dirty_i
->dirty_segmap
[i
])
1586 init_dirty_segmap(sbi
);
1587 return init_victim_secmap(sbi
);
1591 * Update min, max modified time for cost-benefit GC algorithm
1593 static void init_min_max_mtime(struct f2fs_sb_info
*sbi
)
1595 struct sit_info
*sit_i
= SIT_I(sbi
);
1598 mutex_lock(&sit_i
->sentry_lock
);
1600 sit_i
->min_mtime
= LLONG_MAX
;
1602 for (segno
= 0; segno
< TOTAL_SEGS(sbi
); segno
+= sbi
->segs_per_sec
) {
1604 unsigned long long mtime
= 0;
1606 for (i
= 0; i
< sbi
->segs_per_sec
; i
++)
1607 mtime
+= get_seg_entry(sbi
, segno
+ i
)->mtime
;
1609 mtime
= div_u64(mtime
, sbi
->segs_per_sec
);
1611 if (sit_i
->min_mtime
> mtime
)
1612 sit_i
->min_mtime
= mtime
;
1614 sit_i
->max_mtime
= get_mtime(sbi
);
1615 mutex_unlock(&sit_i
->sentry_lock
);
1618 int build_segment_manager(struct f2fs_sb_info
*sbi
)
1620 struct f2fs_super_block
*raw_super
= F2FS_RAW_SUPER(sbi
);
1621 struct f2fs_checkpoint
*ckpt
= F2FS_CKPT(sbi
);
1622 struct f2fs_sm_info
*sm_info
;
1625 sm_info
= kzalloc(sizeof(struct f2fs_sm_info
), GFP_KERNEL
);
1630 sbi
->sm_info
= sm_info
;
1631 INIT_LIST_HEAD(&sm_info
->wblist_head
);
1632 spin_lock_init(&sm_info
->wblist_lock
);
1633 sm_info
->seg0_blkaddr
= le32_to_cpu(raw_super
->segment0_blkaddr
);
1634 sm_info
->main_blkaddr
= le32_to_cpu(raw_super
->main_blkaddr
);
1635 sm_info
->segment_count
= le32_to_cpu(raw_super
->segment_count
);
1636 sm_info
->reserved_segments
= le32_to_cpu(ckpt
->rsvd_segment_count
);
1637 sm_info
->ovp_segments
= le32_to_cpu(ckpt
->overprov_segment_count
);
1638 sm_info
->main_segments
= le32_to_cpu(raw_super
->segment_count_main
);
1639 sm_info
->ssa_blkaddr
= le32_to_cpu(raw_super
->ssa_blkaddr
);
1640 sm_info
->rec_prefree_segments
= DEF_RECLAIM_PREFREE_SEGMENTS
;
1642 err
= build_sit_info(sbi
);
1645 err
= build_free_segmap(sbi
);
1648 err
= build_curseg(sbi
);
1652 /* reinit free segmap based on SIT */
1653 build_sit_entries(sbi
);
1655 init_free_segmap(sbi
);
1656 err
= build_dirty_segmap(sbi
);
1660 init_min_max_mtime(sbi
);
1664 static void discard_dirty_segmap(struct f2fs_sb_info
*sbi
,
1665 enum dirty_type dirty_type
)
1667 struct dirty_seglist_info
*dirty_i
= DIRTY_I(sbi
);
1669 mutex_lock(&dirty_i
->seglist_lock
);
1670 kfree(dirty_i
->dirty_segmap
[dirty_type
]);
1671 dirty_i
->nr_dirty
[dirty_type
] = 0;
1672 mutex_unlock(&dirty_i
->seglist_lock
);
1675 static void destroy_victim_secmap(struct f2fs_sb_info
*sbi
)
1677 struct dirty_seglist_info
*dirty_i
= DIRTY_I(sbi
);
1678 kfree(dirty_i
->victim_secmap
);
1681 static void destroy_dirty_segmap(struct f2fs_sb_info
*sbi
)
1683 struct dirty_seglist_info
*dirty_i
= DIRTY_I(sbi
);
1689 /* discard pre-free/dirty segments list */
1690 for (i
= 0; i
< NR_DIRTY_TYPE
; i
++)
1691 discard_dirty_segmap(sbi
, i
);
1693 destroy_victim_secmap(sbi
);
1694 SM_I(sbi
)->dirty_info
= NULL
;
1698 static void destroy_curseg(struct f2fs_sb_info
*sbi
)
1700 struct curseg_info
*array
= SM_I(sbi
)->curseg_array
;
1705 SM_I(sbi
)->curseg_array
= NULL
;
1706 for (i
= 0; i
< NR_CURSEG_TYPE
; i
++)
1707 kfree(array
[i
].sum_blk
);
1711 static void destroy_free_segmap(struct f2fs_sb_info
*sbi
)
1713 struct free_segmap_info
*free_i
= SM_I(sbi
)->free_info
;
1716 SM_I(sbi
)->free_info
= NULL
;
1717 kfree(free_i
->free_segmap
);
1718 kfree(free_i
->free_secmap
);
1722 static void destroy_sit_info(struct f2fs_sb_info
*sbi
)
1724 struct sit_info
*sit_i
= SIT_I(sbi
);
1730 if (sit_i
->sentries
) {
1731 for (start
= 0; start
< TOTAL_SEGS(sbi
); start
++) {
1732 kfree(sit_i
->sentries
[start
].cur_valid_map
);
1733 kfree(sit_i
->sentries
[start
].ckpt_valid_map
);
1736 vfree(sit_i
->sentries
);
1737 vfree(sit_i
->sec_entries
);
1738 kfree(sit_i
->dirty_sentries_bitmap
);
1740 SM_I(sbi
)->sit_info
= NULL
;
1741 kfree(sit_i
->sit_bitmap
);
1745 void destroy_segment_manager(struct f2fs_sb_info
*sbi
)
1747 struct f2fs_sm_info
*sm_info
= SM_I(sbi
);
1750 destroy_dirty_segmap(sbi
);
1751 destroy_curseg(sbi
);
1752 destroy_free_segmap(sbi
);
1753 destroy_sit_info(sbi
);
1754 sbi
->sm_info
= NULL
;