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
) && p
->sbi
->cp_task
)
596 wake_up_process(p
->sbi
->cp_task
);
602 struct bio
*f2fs_bio_alloc(struct block_device
*bdev
, int npages
)
606 /* No failure on bio allocation */
607 bio
= bio_alloc(GFP_NOIO
, npages
);
609 bio
->bi_private
= NULL
;
614 static void do_submit_bio(struct f2fs_sb_info
*sbi
,
615 enum page_type type
, bool sync
)
617 int rw
= sync
? WRITE_SYNC
: WRITE
;
618 enum page_type btype
= type
> META
? META
: type
;
620 if (type
>= META_FLUSH
)
621 rw
= WRITE_FLUSH_FUA
;
626 if (sbi
->bio
[btype
]) {
627 struct bio_private
*p
= sbi
->bio
[btype
]->bi_private
;
629 sbi
->bio
[btype
]->bi_end_io
= f2fs_end_io_write
;
631 trace_f2fs_do_submit_bio(sbi
->sb
, btype
, sync
, sbi
->bio
[btype
]);
633 if (type
== META_FLUSH
) {
634 DECLARE_COMPLETION_ONSTACK(wait
);
637 submit_bio(rw
, sbi
->bio
[btype
]);
638 wait_for_completion(&wait
);
641 submit_bio(rw
, sbi
->bio
[btype
]);
643 sbi
->bio
[btype
] = NULL
;
647 void f2fs_submit_bio(struct f2fs_sb_info
*sbi
, enum page_type type
, bool sync
)
649 down_write(&sbi
->bio_sem
);
650 do_submit_bio(sbi
, type
, sync
);
651 up_write(&sbi
->bio_sem
);
654 static void submit_write_page(struct f2fs_sb_info
*sbi
, struct page
*page
,
655 block_t blk_addr
, enum page_type type
)
657 struct block_device
*bdev
= sbi
->sb
->s_bdev
;
660 verify_block_addr(sbi
, blk_addr
);
662 down_write(&sbi
->bio_sem
);
664 inc_page_count(sbi
, F2FS_WRITEBACK
);
666 if (sbi
->bio
[type
] && sbi
->last_block_in_bio
[type
] != blk_addr
- 1)
667 do_submit_bio(sbi
, type
, false);
669 if (sbi
->bio
[type
] == NULL
) {
670 struct bio_private
*priv
;
672 priv
= kmalloc(sizeof(struct bio_private
), GFP_NOFS
);
678 bio_blocks
= MAX_BIO_BLOCKS(max_hw_blocks(sbi
));
679 sbi
->bio
[type
] = f2fs_bio_alloc(bdev
, bio_blocks
);
680 sbi
->bio
[type
]->bi_sector
= SECTOR_FROM_BLOCK(sbi
, blk_addr
);
681 sbi
->bio
[type
]->bi_private
= priv
;
683 * The end_io will be assigned at the sumbission phase.
684 * Until then, let bio_add_page() merge consecutive IOs as much
689 if (bio_add_page(sbi
->bio
[type
], page
, PAGE_CACHE_SIZE
, 0) <
691 do_submit_bio(sbi
, type
, false);
695 sbi
->last_block_in_bio
[type
] = blk_addr
;
697 up_write(&sbi
->bio_sem
);
698 trace_f2fs_submit_write_page(page
, blk_addr
, type
);
701 void f2fs_wait_on_page_writeback(struct page
*page
,
702 enum page_type type
, bool sync
)
704 struct f2fs_sb_info
*sbi
= F2FS_SB(page
->mapping
->host
->i_sb
);
705 if (PageWriteback(page
)) {
706 f2fs_submit_bio(sbi
, type
, sync
);
707 wait_on_page_writeback(page
);
711 static bool __has_curseg_space(struct f2fs_sb_info
*sbi
, int type
)
713 struct curseg_info
*curseg
= CURSEG_I(sbi
, type
);
714 if (curseg
->next_blkoff
< sbi
->blocks_per_seg
)
719 static int __get_segment_type_2(struct page
*page
, enum page_type p_type
)
722 return CURSEG_HOT_DATA
;
724 return CURSEG_HOT_NODE
;
727 static int __get_segment_type_4(struct page
*page
, enum page_type p_type
)
729 if (p_type
== DATA
) {
730 struct inode
*inode
= page
->mapping
->host
;
732 if (S_ISDIR(inode
->i_mode
))
733 return CURSEG_HOT_DATA
;
735 return CURSEG_COLD_DATA
;
737 if (IS_DNODE(page
) && !is_cold_node(page
))
738 return CURSEG_HOT_NODE
;
740 return CURSEG_COLD_NODE
;
744 static int __get_segment_type_6(struct page
*page
, enum page_type p_type
)
746 if (p_type
== DATA
) {
747 struct inode
*inode
= page
->mapping
->host
;
749 if (S_ISDIR(inode
->i_mode
))
750 return CURSEG_HOT_DATA
;
751 else if (is_cold_data(page
) || file_is_cold(inode
))
752 return CURSEG_COLD_DATA
;
754 return CURSEG_WARM_DATA
;
757 return is_cold_node(page
) ? CURSEG_WARM_NODE
:
760 return CURSEG_COLD_NODE
;
764 static int __get_segment_type(struct page
*page
, enum page_type p_type
)
766 struct f2fs_sb_info
*sbi
= F2FS_SB(page
->mapping
->host
->i_sb
);
767 switch (sbi
->active_logs
) {
769 return __get_segment_type_2(page
, p_type
);
771 return __get_segment_type_4(page
, p_type
);
773 /* NR_CURSEG_TYPE(6) logs by default */
774 f2fs_bug_on(sbi
->active_logs
!= NR_CURSEG_TYPE
);
775 return __get_segment_type_6(page
, p_type
);
778 static void do_write_page(struct f2fs_sb_info
*sbi
, struct page
*page
,
779 block_t old_blkaddr
, block_t
*new_blkaddr
,
780 struct f2fs_summary
*sum
, enum page_type p_type
)
782 struct sit_info
*sit_i
= SIT_I(sbi
);
783 struct curseg_info
*curseg
;
784 unsigned int old_cursegno
;
787 type
= __get_segment_type(page
, p_type
);
788 curseg
= CURSEG_I(sbi
, type
);
790 mutex_lock(&curseg
->curseg_mutex
);
792 *new_blkaddr
= NEXT_FREE_BLKADDR(sbi
, curseg
);
793 old_cursegno
= curseg
->segno
;
796 * __add_sum_entry should be resided under the curseg_mutex
797 * because, this function updates a summary entry in the
798 * current summary block.
800 __add_sum_entry(sbi
, type
, sum
);
802 mutex_lock(&sit_i
->sentry_lock
);
803 __refresh_next_blkoff(sbi
, curseg
);
805 stat_inc_block_count(sbi
, curseg
);
808 * SIT information should be updated before segment allocation,
809 * since SSR needs latest valid block information.
811 refresh_sit_entry(sbi
, old_blkaddr
, *new_blkaddr
);
813 if (!__has_curseg_space(sbi
, type
))
814 sit_i
->s_ops
->allocate_segment(sbi
, type
, false);
816 locate_dirty_segment(sbi
, old_cursegno
);
817 locate_dirty_segment(sbi
, GET_SEGNO(sbi
, old_blkaddr
));
818 mutex_unlock(&sit_i
->sentry_lock
);
821 fill_node_footer_blkaddr(page
, NEXT_FREE_BLKADDR(sbi
, curseg
));
823 /* writeout dirty page into bdev */
824 submit_write_page(sbi
, page
, *new_blkaddr
, p_type
);
826 mutex_unlock(&curseg
->curseg_mutex
);
829 void write_meta_page(struct f2fs_sb_info
*sbi
, struct page
*page
)
831 set_page_writeback(page
);
832 submit_write_page(sbi
, page
, page
->index
, META
);
835 void write_node_page(struct f2fs_sb_info
*sbi
, struct page
*page
,
836 unsigned int nid
, block_t old_blkaddr
, block_t
*new_blkaddr
)
838 struct f2fs_summary sum
;
839 set_summary(&sum
, nid
, 0, 0);
840 do_write_page(sbi
, page
, old_blkaddr
, new_blkaddr
, &sum
, NODE
);
843 void write_data_page(struct inode
*inode
, struct page
*page
,
844 struct dnode_of_data
*dn
, block_t old_blkaddr
,
845 block_t
*new_blkaddr
)
847 struct f2fs_sb_info
*sbi
= F2FS_SB(inode
->i_sb
);
848 struct f2fs_summary sum
;
851 f2fs_bug_on(old_blkaddr
== NULL_ADDR
);
852 get_node_info(sbi
, dn
->nid
, &ni
);
853 set_summary(&sum
, dn
->nid
, dn
->ofs_in_node
, ni
.version
);
855 do_write_page(sbi
, page
, old_blkaddr
,
856 new_blkaddr
, &sum
, DATA
);
859 void rewrite_data_page(struct f2fs_sb_info
*sbi
, struct page
*page
,
860 block_t old_blk_addr
)
862 submit_write_page(sbi
, page
, old_blk_addr
, DATA
);
865 void recover_data_page(struct f2fs_sb_info
*sbi
,
866 struct page
*page
, struct f2fs_summary
*sum
,
867 block_t old_blkaddr
, block_t new_blkaddr
)
869 struct sit_info
*sit_i
= SIT_I(sbi
);
870 struct curseg_info
*curseg
;
871 unsigned int segno
, old_cursegno
;
872 struct seg_entry
*se
;
875 segno
= GET_SEGNO(sbi
, new_blkaddr
);
876 se
= get_seg_entry(sbi
, segno
);
879 if (se
->valid_blocks
== 0 && !IS_CURSEG(sbi
, segno
)) {
880 if (old_blkaddr
== NULL_ADDR
)
881 type
= CURSEG_COLD_DATA
;
883 type
= CURSEG_WARM_DATA
;
885 curseg
= CURSEG_I(sbi
, type
);
887 mutex_lock(&curseg
->curseg_mutex
);
888 mutex_lock(&sit_i
->sentry_lock
);
890 old_cursegno
= curseg
->segno
;
892 /* change the current segment */
893 if (segno
!= curseg
->segno
) {
894 curseg
->next_segno
= segno
;
895 change_curseg(sbi
, type
, true);
898 curseg
->next_blkoff
= GET_SEGOFF_FROM_SEG0(sbi
, new_blkaddr
) &
899 (sbi
->blocks_per_seg
- 1);
900 __add_sum_entry(sbi
, type
, sum
);
902 refresh_sit_entry(sbi
, old_blkaddr
, new_blkaddr
);
904 locate_dirty_segment(sbi
, old_cursegno
);
905 locate_dirty_segment(sbi
, GET_SEGNO(sbi
, old_blkaddr
));
907 mutex_unlock(&sit_i
->sentry_lock
);
908 mutex_unlock(&curseg
->curseg_mutex
);
911 void rewrite_node_page(struct f2fs_sb_info
*sbi
,
912 struct page
*page
, struct f2fs_summary
*sum
,
913 block_t old_blkaddr
, block_t new_blkaddr
)
915 struct sit_info
*sit_i
= SIT_I(sbi
);
916 int type
= CURSEG_WARM_NODE
;
917 struct curseg_info
*curseg
;
918 unsigned int segno
, old_cursegno
;
919 block_t next_blkaddr
= next_blkaddr_of_node(page
);
920 unsigned int next_segno
= GET_SEGNO(sbi
, next_blkaddr
);
922 curseg
= CURSEG_I(sbi
, type
);
924 mutex_lock(&curseg
->curseg_mutex
);
925 mutex_lock(&sit_i
->sentry_lock
);
927 segno
= GET_SEGNO(sbi
, new_blkaddr
);
928 old_cursegno
= curseg
->segno
;
930 /* change the current segment */
931 if (segno
!= curseg
->segno
) {
932 curseg
->next_segno
= segno
;
933 change_curseg(sbi
, type
, true);
935 curseg
->next_blkoff
= GET_SEGOFF_FROM_SEG0(sbi
, new_blkaddr
) &
936 (sbi
->blocks_per_seg
- 1);
937 __add_sum_entry(sbi
, type
, sum
);
939 /* change the current log to the next block addr in advance */
940 if (next_segno
!= segno
) {
941 curseg
->next_segno
= next_segno
;
942 change_curseg(sbi
, type
, true);
944 curseg
->next_blkoff
= GET_SEGOFF_FROM_SEG0(sbi
, next_blkaddr
) &
945 (sbi
->blocks_per_seg
- 1);
947 /* rewrite node page */
948 set_page_writeback(page
);
949 submit_write_page(sbi
, page
, new_blkaddr
, NODE
);
950 f2fs_submit_bio(sbi
, NODE
, true);
951 refresh_sit_entry(sbi
, old_blkaddr
, new_blkaddr
);
953 locate_dirty_segment(sbi
, old_cursegno
);
954 locate_dirty_segment(sbi
, GET_SEGNO(sbi
, old_blkaddr
));
956 mutex_unlock(&sit_i
->sentry_lock
);
957 mutex_unlock(&curseg
->curseg_mutex
);
960 static int read_compacted_summaries(struct f2fs_sb_info
*sbi
)
962 struct f2fs_checkpoint
*ckpt
= F2FS_CKPT(sbi
);
963 struct curseg_info
*seg_i
;
964 unsigned char *kaddr
;
969 start
= start_sum_block(sbi
);
971 page
= get_meta_page(sbi
, start
++);
972 kaddr
= (unsigned char *)page_address(page
);
974 /* Step 1: restore nat cache */
975 seg_i
= CURSEG_I(sbi
, CURSEG_HOT_DATA
);
976 memcpy(&seg_i
->sum_blk
->n_nats
, kaddr
, SUM_JOURNAL_SIZE
);
978 /* Step 2: restore sit cache */
979 seg_i
= CURSEG_I(sbi
, CURSEG_COLD_DATA
);
980 memcpy(&seg_i
->sum_blk
->n_sits
, kaddr
+ SUM_JOURNAL_SIZE
,
982 offset
= 2 * SUM_JOURNAL_SIZE
;
984 /* Step 3: restore summary entries */
985 for (i
= CURSEG_HOT_DATA
; i
<= CURSEG_COLD_DATA
; i
++) {
986 unsigned short blk_off
;
989 seg_i
= CURSEG_I(sbi
, i
);
990 segno
= le32_to_cpu(ckpt
->cur_data_segno
[i
]);
991 blk_off
= le16_to_cpu(ckpt
->cur_data_blkoff
[i
]);
992 seg_i
->next_segno
= segno
;
993 reset_curseg(sbi
, i
, 0);
994 seg_i
->alloc_type
= ckpt
->alloc_type
[i
];
995 seg_i
->next_blkoff
= blk_off
;
997 if (seg_i
->alloc_type
== SSR
)
998 blk_off
= sbi
->blocks_per_seg
;
1000 for (j
= 0; j
< blk_off
; j
++) {
1001 struct f2fs_summary
*s
;
1002 s
= (struct f2fs_summary
*)(kaddr
+ offset
);
1003 seg_i
->sum_blk
->entries
[j
] = *s
;
1004 offset
+= SUMMARY_SIZE
;
1005 if (offset
+ SUMMARY_SIZE
<= PAGE_CACHE_SIZE
-
1009 f2fs_put_page(page
, 1);
1012 page
= get_meta_page(sbi
, start
++);
1013 kaddr
= (unsigned char *)page_address(page
);
1017 f2fs_put_page(page
, 1);
1021 static int read_normal_summaries(struct f2fs_sb_info
*sbi
, int type
)
1023 struct f2fs_checkpoint
*ckpt
= F2FS_CKPT(sbi
);
1024 struct f2fs_summary_block
*sum
;
1025 struct curseg_info
*curseg
;
1027 unsigned short blk_off
;
1028 unsigned int segno
= 0;
1029 block_t blk_addr
= 0;
1031 /* get segment number and block addr */
1032 if (IS_DATASEG(type
)) {
1033 segno
= le32_to_cpu(ckpt
->cur_data_segno
[type
]);
1034 blk_off
= le16_to_cpu(ckpt
->cur_data_blkoff
[type
-
1036 if (is_set_ckpt_flags(ckpt
, CP_UMOUNT_FLAG
))
1037 blk_addr
= sum_blk_addr(sbi
, NR_CURSEG_TYPE
, type
);
1039 blk_addr
= sum_blk_addr(sbi
, NR_CURSEG_DATA_TYPE
, type
);
1041 segno
= le32_to_cpu(ckpt
->cur_node_segno
[type
-
1043 blk_off
= le16_to_cpu(ckpt
->cur_node_blkoff
[type
-
1045 if (is_set_ckpt_flags(ckpt
, CP_UMOUNT_FLAG
))
1046 blk_addr
= sum_blk_addr(sbi
, NR_CURSEG_NODE_TYPE
,
1047 type
- CURSEG_HOT_NODE
);
1049 blk_addr
= GET_SUM_BLOCK(sbi
, segno
);
1052 new = get_meta_page(sbi
, blk_addr
);
1053 sum
= (struct f2fs_summary_block
*)page_address(new);
1055 if (IS_NODESEG(type
)) {
1056 if (is_set_ckpt_flags(ckpt
, CP_UMOUNT_FLAG
)) {
1057 struct f2fs_summary
*ns
= &sum
->entries
[0];
1059 for (i
= 0; i
< sbi
->blocks_per_seg
; i
++, ns
++) {
1061 ns
->ofs_in_node
= 0;
1064 if (restore_node_summary(sbi
, segno
, sum
)) {
1065 f2fs_put_page(new, 1);
1071 /* set uncompleted segment to curseg */
1072 curseg
= CURSEG_I(sbi
, type
);
1073 mutex_lock(&curseg
->curseg_mutex
);
1074 memcpy(curseg
->sum_blk
, sum
, PAGE_CACHE_SIZE
);
1075 curseg
->next_segno
= segno
;
1076 reset_curseg(sbi
, type
, 0);
1077 curseg
->alloc_type
= ckpt
->alloc_type
[type
];
1078 curseg
->next_blkoff
= blk_off
;
1079 mutex_unlock(&curseg
->curseg_mutex
);
1080 f2fs_put_page(new, 1);
1084 static int restore_curseg_summaries(struct f2fs_sb_info
*sbi
)
1086 int type
= CURSEG_HOT_DATA
;
1088 if (is_set_ckpt_flags(F2FS_CKPT(sbi
), CP_COMPACT_SUM_FLAG
)) {
1089 /* restore for compacted data summary */
1090 if (read_compacted_summaries(sbi
))
1092 type
= CURSEG_HOT_NODE
;
1095 for (; type
<= CURSEG_COLD_NODE
; type
++)
1096 if (read_normal_summaries(sbi
, type
))
1101 static void write_compacted_summaries(struct f2fs_sb_info
*sbi
, block_t blkaddr
)
1104 unsigned char *kaddr
;
1105 struct f2fs_summary
*summary
;
1106 struct curseg_info
*seg_i
;
1107 int written_size
= 0;
1110 page
= grab_meta_page(sbi
, blkaddr
++);
1111 kaddr
= (unsigned char *)page_address(page
);
1113 /* Step 1: write nat cache */
1114 seg_i
= CURSEG_I(sbi
, CURSEG_HOT_DATA
);
1115 memcpy(kaddr
, &seg_i
->sum_blk
->n_nats
, SUM_JOURNAL_SIZE
);
1116 written_size
+= SUM_JOURNAL_SIZE
;
1118 /* Step 2: write sit cache */
1119 seg_i
= CURSEG_I(sbi
, CURSEG_COLD_DATA
);
1120 memcpy(kaddr
+ written_size
, &seg_i
->sum_blk
->n_sits
,
1122 written_size
+= SUM_JOURNAL_SIZE
;
1124 /* Step 3: write summary entries */
1125 for (i
= CURSEG_HOT_DATA
; i
<= CURSEG_COLD_DATA
; i
++) {
1126 unsigned short blkoff
;
1127 seg_i
= CURSEG_I(sbi
, i
);
1128 if (sbi
->ckpt
->alloc_type
[i
] == SSR
)
1129 blkoff
= sbi
->blocks_per_seg
;
1131 blkoff
= curseg_blkoff(sbi
, i
);
1133 for (j
= 0; j
< blkoff
; j
++) {
1135 page
= grab_meta_page(sbi
, blkaddr
++);
1136 kaddr
= (unsigned char *)page_address(page
);
1139 summary
= (struct f2fs_summary
*)(kaddr
+ written_size
);
1140 *summary
= seg_i
->sum_blk
->entries
[j
];
1141 written_size
+= SUMMARY_SIZE
;
1143 if (written_size
+ SUMMARY_SIZE
<= PAGE_CACHE_SIZE
-
1147 set_page_dirty(page
);
1148 f2fs_put_page(page
, 1);
1153 set_page_dirty(page
);
1154 f2fs_put_page(page
, 1);
1158 static void write_normal_summaries(struct f2fs_sb_info
*sbi
,
1159 block_t blkaddr
, int type
)
1162 if (IS_DATASEG(type
))
1163 end
= type
+ NR_CURSEG_DATA_TYPE
;
1165 end
= type
+ NR_CURSEG_NODE_TYPE
;
1167 for (i
= type
; i
< end
; i
++) {
1168 struct curseg_info
*sum
= CURSEG_I(sbi
, i
);
1169 mutex_lock(&sum
->curseg_mutex
);
1170 write_sum_page(sbi
, sum
->sum_blk
, blkaddr
+ (i
- type
));
1171 mutex_unlock(&sum
->curseg_mutex
);
1175 void write_data_summaries(struct f2fs_sb_info
*sbi
, block_t start_blk
)
1177 if (is_set_ckpt_flags(F2FS_CKPT(sbi
), CP_COMPACT_SUM_FLAG
))
1178 write_compacted_summaries(sbi
, start_blk
);
1180 write_normal_summaries(sbi
, start_blk
, CURSEG_HOT_DATA
);
1183 void write_node_summaries(struct f2fs_sb_info
*sbi
, block_t start_blk
)
1185 if (is_set_ckpt_flags(F2FS_CKPT(sbi
), CP_UMOUNT_FLAG
))
1186 write_normal_summaries(sbi
, start_blk
, CURSEG_HOT_NODE
);
1189 int lookup_journal_in_cursum(struct f2fs_summary_block
*sum
, int type
,
1190 unsigned int val
, int alloc
)
1194 if (type
== NAT_JOURNAL
) {
1195 for (i
= 0; i
< nats_in_cursum(sum
); i
++) {
1196 if (le32_to_cpu(nid_in_journal(sum
, i
)) == val
)
1199 if (alloc
&& nats_in_cursum(sum
) < NAT_JOURNAL_ENTRIES
)
1200 return update_nats_in_cursum(sum
, 1);
1201 } else if (type
== SIT_JOURNAL
) {
1202 for (i
= 0; i
< sits_in_cursum(sum
); i
++)
1203 if (le32_to_cpu(segno_in_journal(sum
, i
)) == val
)
1205 if (alloc
&& sits_in_cursum(sum
) < SIT_JOURNAL_ENTRIES
)
1206 return update_sits_in_cursum(sum
, 1);
1211 static struct page
*get_current_sit_page(struct f2fs_sb_info
*sbi
,
1214 struct sit_info
*sit_i
= SIT_I(sbi
);
1215 unsigned int offset
= SIT_BLOCK_OFFSET(sit_i
, segno
);
1216 block_t blk_addr
= sit_i
->sit_base_addr
+ offset
;
1218 check_seg_range(sbi
, segno
);
1220 /* calculate sit block address */
1221 if (f2fs_test_bit(offset
, sit_i
->sit_bitmap
))
1222 blk_addr
+= sit_i
->sit_blocks
;
1224 return get_meta_page(sbi
, blk_addr
);
1227 static struct page
*get_next_sit_page(struct f2fs_sb_info
*sbi
,
1230 struct sit_info
*sit_i
= SIT_I(sbi
);
1231 struct page
*src_page
, *dst_page
;
1232 pgoff_t src_off
, dst_off
;
1233 void *src_addr
, *dst_addr
;
1235 src_off
= current_sit_addr(sbi
, start
);
1236 dst_off
= next_sit_addr(sbi
, src_off
);
1238 /* get current sit block page without lock */
1239 src_page
= get_meta_page(sbi
, src_off
);
1240 dst_page
= grab_meta_page(sbi
, dst_off
);
1241 f2fs_bug_on(PageDirty(src_page
));
1243 src_addr
= page_address(src_page
);
1244 dst_addr
= page_address(dst_page
);
1245 memcpy(dst_addr
, src_addr
, PAGE_CACHE_SIZE
);
1247 set_page_dirty(dst_page
);
1248 f2fs_put_page(src_page
, 1);
1250 set_to_next_sit(sit_i
, start
);
1255 static bool flush_sits_in_journal(struct f2fs_sb_info
*sbi
)
1257 struct curseg_info
*curseg
= CURSEG_I(sbi
, CURSEG_COLD_DATA
);
1258 struct f2fs_summary_block
*sum
= curseg
->sum_blk
;
1262 * If the journal area in the current summary is full of sit entries,
1263 * all the sit entries will be flushed. Otherwise the sit entries
1264 * are not able to replace with newly hot sit entries.
1266 if (sits_in_cursum(sum
) >= SIT_JOURNAL_ENTRIES
) {
1267 for (i
= sits_in_cursum(sum
) - 1; i
>= 0; i
--) {
1269 segno
= le32_to_cpu(segno_in_journal(sum
, i
));
1270 __mark_sit_entry_dirty(sbi
, segno
);
1272 update_sits_in_cursum(sum
, -sits_in_cursum(sum
));
1279 * CP calls this function, which flushes SIT entries including sit_journal,
1280 * and moves prefree segs to free segs.
1282 void flush_sit_entries(struct f2fs_sb_info
*sbi
)
1284 struct sit_info
*sit_i
= SIT_I(sbi
);
1285 unsigned long *bitmap
= sit_i
->dirty_sentries_bitmap
;
1286 struct curseg_info
*curseg
= CURSEG_I(sbi
, CURSEG_COLD_DATA
);
1287 struct f2fs_summary_block
*sum
= curseg
->sum_blk
;
1288 unsigned long nsegs
= TOTAL_SEGS(sbi
);
1289 struct page
*page
= NULL
;
1290 struct f2fs_sit_block
*raw_sit
= NULL
;
1291 unsigned int start
= 0, end
= 0;
1292 unsigned int segno
= -1;
1295 mutex_lock(&curseg
->curseg_mutex
);
1296 mutex_lock(&sit_i
->sentry_lock
);
1299 * "flushed" indicates whether sit entries in journal are flushed
1300 * to the SIT area or not.
1302 flushed
= flush_sits_in_journal(sbi
);
1304 while ((segno
= find_next_bit(bitmap
, nsegs
, segno
+ 1)) < nsegs
) {
1305 struct seg_entry
*se
= get_seg_entry(sbi
, segno
);
1306 int sit_offset
, offset
;
1308 sit_offset
= SIT_ENTRY_OFFSET(sit_i
, segno
);
1313 offset
= lookup_journal_in_cursum(sum
, SIT_JOURNAL
, segno
, 1);
1315 segno_in_journal(sum
, offset
) = cpu_to_le32(segno
);
1316 seg_info_to_raw_sit(se
, &sit_in_journal(sum
, offset
));
1320 if (!page
|| (start
> segno
) || (segno
> end
)) {
1322 f2fs_put_page(page
, 1);
1326 start
= START_SEGNO(sit_i
, segno
);
1327 end
= start
+ SIT_ENTRY_PER_BLOCK
- 1;
1329 /* read sit block that will be updated */
1330 page
= get_next_sit_page(sbi
, start
);
1331 raw_sit
= page_address(page
);
1334 /* udpate entry in SIT block */
1335 seg_info_to_raw_sit(se
, &raw_sit
->entries
[sit_offset
]);
1337 __clear_bit(segno
, bitmap
);
1338 sit_i
->dirty_sentries
--;
1340 mutex_unlock(&sit_i
->sentry_lock
);
1341 mutex_unlock(&curseg
->curseg_mutex
);
1343 /* writeout last modified SIT block */
1344 f2fs_put_page(page
, 1);
1346 set_prefree_as_free_segments(sbi
);
1349 static int build_sit_info(struct f2fs_sb_info
*sbi
)
1351 struct f2fs_super_block
*raw_super
= F2FS_RAW_SUPER(sbi
);
1352 struct f2fs_checkpoint
*ckpt
= F2FS_CKPT(sbi
);
1353 struct sit_info
*sit_i
;
1354 unsigned int sit_segs
, start
;
1355 char *src_bitmap
, *dst_bitmap
;
1356 unsigned int bitmap_size
;
1358 /* allocate memory for SIT information */
1359 sit_i
= kzalloc(sizeof(struct sit_info
), GFP_KERNEL
);
1363 SM_I(sbi
)->sit_info
= sit_i
;
1365 sit_i
->sentries
= vzalloc(TOTAL_SEGS(sbi
) * sizeof(struct seg_entry
));
1366 if (!sit_i
->sentries
)
1369 bitmap_size
= f2fs_bitmap_size(TOTAL_SEGS(sbi
));
1370 sit_i
->dirty_sentries_bitmap
= kzalloc(bitmap_size
, GFP_KERNEL
);
1371 if (!sit_i
->dirty_sentries_bitmap
)
1374 for (start
= 0; start
< TOTAL_SEGS(sbi
); start
++) {
1375 sit_i
->sentries
[start
].cur_valid_map
1376 = kzalloc(SIT_VBLOCK_MAP_SIZE
, GFP_KERNEL
);
1377 sit_i
->sentries
[start
].ckpt_valid_map
1378 = kzalloc(SIT_VBLOCK_MAP_SIZE
, GFP_KERNEL
);
1379 if (!sit_i
->sentries
[start
].cur_valid_map
1380 || !sit_i
->sentries
[start
].ckpt_valid_map
)
1384 if (sbi
->segs_per_sec
> 1) {
1385 sit_i
->sec_entries
= vzalloc(TOTAL_SECS(sbi
) *
1386 sizeof(struct sec_entry
));
1387 if (!sit_i
->sec_entries
)
1391 /* get information related with SIT */
1392 sit_segs
= le32_to_cpu(raw_super
->segment_count_sit
) >> 1;
1394 /* setup SIT bitmap from ckeckpoint pack */
1395 bitmap_size
= __bitmap_size(sbi
, SIT_BITMAP
);
1396 src_bitmap
= __bitmap_ptr(sbi
, SIT_BITMAP
);
1398 dst_bitmap
= kmemdup(src_bitmap
, bitmap_size
, GFP_KERNEL
);
1402 /* init SIT information */
1403 sit_i
->s_ops
= &default_salloc_ops
;
1405 sit_i
->sit_base_addr
= le32_to_cpu(raw_super
->sit_blkaddr
);
1406 sit_i
->sit_blocks
= sit_segs
<< sbi
->log_blocks_per_seg
;
1407 sit_i
->written_valid_blocks
= le64_to_cpu(ckpt
->valid_block_count
);
1408 sit_i
->sit_bitmap
= dst_bitmap
;
1409 sit_i
->bitmap_size
= bitmap_size
;
1410 sit_i
->dirty_sentries
= 0;
1411 sit_i
->sents_per_block
= SIT_ENTRY_PER_BLOCK
;
1412 sit_i
->elapsed_time
= le64_to_cpu(sbi
->ckpt
->elapsed_time
);
1413 sit_i
->mounted_time
= CURRENT_TIME_SEC
.tv_sec
;
1414 mutex_init(&sit_i
->sentry_lock
);
1418 static int build_free_segmap(struct f2fs_sb_info
*sbi
)
1420 struct f2fs_sm_info
*sm_info
= SM_I(sbi
);
1421 struct free_segmap_info
*free_i
;
1422 unsigned int bitmap_size
, sec_bitmap_size
;
1424 /* allocate memory for free segmap information */
1425 free_i
= kzalloc(sizeof(struct free_segmap_info
), GFP_KERNEL
);
1429 SM_I(sbi
)->free_info
= free_i
;
1431 bitmap_size
= f2fs_bitmap_size(TOTAL_SEGS(sbi
));
1432 free_i
->free_segmap
= kmalloc(bitmap_size
, GFP_KERNEL
);
1433 if (!free_i
->free_segmap
)
1436 sec_bitmap_size
= f2fs_bitmap_size(TOTAL_SECS(sbi
));
1437 free_i
->free_secmap
= kmalloc(sec_bitmap_size
, GFP_KERNEL
);
1438 if (!free_i
->free_secmap
)
1441 /* set all segments as dirty temporarily */
1442 memset(free_i
->free_segmap
, 0xff, bitmap_size
);
1443 memset(free_i
->free_secmap
, 0xff, sec_bitmap_size
);
1445 /* init free segmap information */
1446 free_i
->start_segno
=
1447 (unsigned int) GET_SEGNO_FROM_SEG0(sbi
, sm_info
->main_blkaddr
);
1448 free_i
->free_segments
= 0;
1449 free_i
->free_sections
= 0;
1450 rwlock_init(&free_i
->segmap_lock
);
1454 static int build_curseg(struct f2fs_sb_info
*sbi
)
1456 struct curseg_info
*array
;
1459 array
= kzalloc(sizeof(*array
) * NR_CURSEG_TYPE
, GFP_KERNEL
);
1463 SM_I(sbi
)->curseg_array
= array
;
1465 for (i
= 0; i
< NR_CURSEG_TYPE
; i
++) {
1466 mutex_init(&array
[i
].curseg_mutex
);
1467 array
[i
].sum_blk
= kzalloc(PAGE_CACHE_SIZE
, GFP_KERNEL
);
1468 if (!array
[i
].sum_blk
)
1470 array
[i
].segno
= NULL_SEGNO
;
1471 array
[i
].next_blkoff
= 0;
1473 return restore_curseg_summaries(sbi
);
1476 static void build_sit_entries(struct f2fs_sb_info
*sbi
)
1478 struct sit_info
*sit_i
= SIT_I(sbi
);
1479 struct curseg_info
*curseg
= CURSEG_I(sbi
, CURSEG_COLD_DATA
);
1480 struct f2fs_summary_block
*sum
= curseg
->sum_blk
;
1483 for (start
= 0; start
< TOTAL_SEGS(sbi
); start
++) {
1484 struct seg_entry
*se
= &sit_i
->sentries
[start
];
1485 struct f2fs_sit_block
*sit_blk
;
1486 struct f2fs_sit_entry sit
;
1490 mutex_lock(&curseg
->curseg_mutex
);
1491 for (i
= 0; i
< sits_in_cursum(sum
); i
++) {
1492 if (le32_to_cpu(segno_in_journal(sum
, i
)) == start
) {
1493 sit
= sit_in_journal(sum
, i
);
1494 mutex_unlock(&curseg
->curseg_mutex
);
1498 mutex_unlock(&curseg
->curseg_mutex
);
1499 page
= get_current_sit_page(sbi
, start
);
1500 sit_blk
= (struct f2fs_sit_block
*)page_address(page
);
1501 sit
= sit_blk
->entries
[SIT_ENTRY_OFFSET(sit_i
, start
)];
1502 f2fs_put_page(page
, 1);
1504 check_block_count(sbi
, start
, &sit
);
1505 seg_info_from_raw_sit(se
, &sit
);
1506 if (sbi
->segs_per_sec
> 1) {
1507 struct sec_entry
*e
= get_sec_entry(sbi
, start
);
1508 e
->valid_blocks
+= se
->valid_blocks
;
1513 static void init_free_segmap(struct f2fs_sb_info
*sbi
)
1518 for (start
= 0; start
< TOTAL_SEGS(sbi
); start
++) {
1519 struct seg_entry
*sentry
= get_seg_entry(sbi
, start
);
1520 if (!sentry
->valid_blocks
)
1521 __set_free(sbi
, start
);
1524 /* set use the current segments */
1525 for (type
= CURSEG_HOT_DATA
; type
<= CURSEG_COLD_NODE
; type
++) {
1526 struct curseg_info
*curseg_t
= CURSEG_I(sbi
, type
);
1527 __set_test_and_inuse(sbi
, curseg_t
->segno
);
1531 static void init_dirty_segmap(struct f2fs_sb_info
*sbi
)
1533 struct dirty_seglist_info
*dirty_i
= DIRTY_I(sbi
);
1534 struct free_segmap_info
*free_i
= FREE_I(sbi
);
1535 unsigned int segno
= 0, offset
= 0, total_segs
= TOTAL_SEGS(sbi
);
1536 unsigned short valid_blocks
;
1539 /* find dirty segment based on free segmap */
1540 segno
= find_next_inuse(free_i
, total_segs
, offset
);
1541 if (segno
>= total_segs
)
1544 valid_blocks
= get_valid_blocks(sbi
, segno
, 0);
1545 if (valid_blocks
>= sbi
->blocks_per_seg
|| !valid_blocks
)
1547 mutex_lock(&dirty_i
->seglist_lock
);
1548 __locate_dirty_segment(sbi
, segno
, DIRTY
);
1549 mutex_unlock(&dirty_i
->seglist_lock
);
1553 static int init_victim_secmap(struct f2fs_sb_info
*sbi
)
1555 struct dirty_seglist_info
*dirty_i
= DIRTY_I(sbi
);
1556 unsigned int bitmap_size
= f2fs_bitmap_size(TOTAL_SECS(sbi
));
1558 dirty_i
->victim_secmap
= kzalloc(bitmap_size
, GFP_KERNEL
);
1559 if (!dirty_i
->victim_secmap
)
1564 static int build_dirty_segmap(struct f2fs_sb_info
*sbi
)
1566 struct dirty_seglist_info
*dirty_i
;
1567 unsigned int bitmap_size
, i
;
1569 /* allocate memory for dirty segments list information */
1570 dirty_i
= kzalloc(sizeof(struct dirty_seglist_info
), GFP_KERNEL
);
1574 SM_I(sbi
)->dirty_info
= dirty_i
;
1575 mutex_init(&dirty_i
->seglist_lock
);
1577 bitmap_size
= f2fs_bitmap_size(TOTAL_SEGS(sbi
));
1579 for (i
= 0; i
< NR_DIRTY_TYPE
; i
++) {
1580 dirty_i
->dirty_segmap
[i
] = kzalloc(bitmap_size
, GFP_KERNEL
);
1581 if (!dirty_i
->dirty_segmap
[i
])
1585 init_dirty_segmap(sbi
);
1586 return init_victim_secmap(sbi
);
1590 * Update min, max modified time for cost-benefit GC algorithm
1592 static void init_min_max_mtime(struct f2fs_sb_info
*sbi
)
1594 struct sit_info
*sit_i
= SIT_I(sbi
);
1597 mutex_lock(&sit_i
->sentry_lock
);
1599 sit_i
->min_mtime
= LLONG_MAX
;
1601 for (segno
= 0; segno
< TOTAL_SEGS(sbi
); segno
+= sbi
->segs_per_sec
) {
1603 unsigned long long mtime
= 0;
1605 for (i
= 0; i
< sbi
->segs_per_sec
; i
++)
1606 mtime
+= get_seg_entry(sbi
, segno
+ i
)->mtime
;
1608 mtime
= div_u64(mtime
, sbi
->segs_per_sec
);
1610 if (sit_i
->min_mtime
> mtime
)
1611 sit_i
->min_mtime
= mtime
;
1613 sit_i
->max_mtime
= get_mtime(sbi
);
1614 mutex_unlock(&sit_i
->sentry_lock
);
1617 int build_segment_manager(struct f2fs_sb_info
*sbi
)
1619 struct f2fs_super_block
*raw_super
= F2FS_RAW_SUPER(sbi
);
1620 struct f2fs_checkpoint
*ckpt
= F2FS_CKPT(sbi
);
1621 struct f2fs_sm_info
*sm_info
;
1624 sm_info
= kzalloc(sizeof(struct f2fs_sm_info
), GFP_KERNEL
);
1629 sbi
->sm_info
= sm_info
;
1630 INIT_LIST_HEAD(&sm_info
->wblist_head
);
1631 spin_lock_init(&sm_info
->wblist_lock
);
1632 sm_info
->seg0_blkaddr
= le32_to_cpu(raw_super
->segment0_blkaddr
);
1633 sm_info
->main_blkaddr
= le32_to_cpu(raw_super
->main_blkaddr
);
1634 sm_info
->segment_count
= le32_to_cpu(raw_super
->segment_count
);
1635 sm_info
->reserved_segments
= le32_to_cpu(ckpt
->rsvd_segment_count
);
1636 sm_info
->ovp_segments
= le32_to_cpu(ckpt
->overprov_segment_count
);
1637 sm_info
->main_segments
= le32_to_cpu(raw_super
->segment_count_main
);
1638 sm_info
->ssa_blkaddr
= le32_to_cpu(raw_super
->ssa_blkaddr
);
1639 sm_info
->rec_prefree_segments
= DEF_RECLAIM_PREFREE_SEGMENTS
;
1641 err
= build_sit_info(sbi
);
1644 err
= build_free_segmap(sbi
);
1647 err
= build_curseg(sbi
);
1651 /* reinit free segmap based on SIT */
1652 build_sit_entries(sbi
);
1654 init_free_segmap(sbi
);
1655 err
= build_dirty_segmap(sbi
);
1659 init_min_max_mtime(sbi
);
1663 static void discard_dirty_segmap(struct f2fs_sb_info
*sbi
,
1664 enum dirty_type dirty_type
)
1666 struct dirty_seglist_info
*dirty_i
= DIRTY_I(sbi
);
1668 mutex_lock(&dirty_i
->seglist_lock
);
1669 kfree(dirty_i
->dirty_segmap
[dirty_type
]);
1670 dirty_i
->nr_dirty
[dirty_type
] = 0;
1671 mutex_unlock(&dirty_i
->seglist_lock
);
1674 static void destroy_victim_secmap(struct f2fs_sb_info
*sbi
)
1676 struct dirty_seglist_info
*dirty_i
= DIRTY_I(sbi
);
1677 kfree(dirty_i
->victim_secmap
);
1680 static void destroy_dirty_segmap(struct f2fs_sb_info
*sbi
)
1682 struct dirty_seglist_info
*dirty_i
= DIRTY_I(sbi
);
1688 /* discard pre-free/dirty segments list */
1689 for (i
= 0; i
< NR_DIRTY_TYPE
; i
++)
1690 discard_dirty_segmap(sbi
, i
);
1692 destroy_victim_secmap(sbi
);
1693 SM_I(sbi
)->dirty_info
= NULL
;
1697 static void destroy_curseg(struct f2fs_sb_info
*sbi
)
1699 struct curseg_info
*array
= SM_I(sbi
)->curseg_array
;
1704 SM_I(sbi
)->curseg_array
= NULL
;
1705 for (i
= 0; i
< NR_CURSEG_TYPE
; i
++)
1706 kfree(array
[i
].sum_blk
);
1710 static void destroy_free_segmap(struct f2fs_sb_info
*sbi
)
1712 struct free_segmap_info
*free_i
= SM_I(sbi
)->free_info
;
1715 SM_I(sbi
)->free_info
= NULL
;
1716 kfree(free_i
->free_segmap
);
1717 kfree(free_i
->free_secmap
);
1721 static void destroy_sit_info(struct f2fs_sb_info
*sbi
)
1723 struct sit_info
*sit_i
= SIT_I(sbi
);
1729 if (sit_i
->sentries
) {
1730 for (start
= 0; start
< TOTAL_SEGS(sbi
); start
++) {
1731 kfree(sit_i
->sentries
[start
].cur_valid_map
);
1732 kfree(sit_i
->sentries
[start
].ckpt_valid_map
);
1735 vfree(sit_i
->sentries
);
1736 vfree(sit_i
->sec_entries
);
1737 kfree(sit_i
->dirty_sentries_bitmap
);
1739 SM_I(sbi
)->sit_info
= NULL
;
1740 kfree(sit_i
->sit_bitmap
);
1744 void destroy_segment_manager(struct f2fs_sb_info
*sbi
)
1746 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
;