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 static void __locate_dirty_segment(struct f2fs_sb_info
*sbi
, unsigned int segno
,
40 enum dirty_type dirty_type
)
42 struct dirty_seglist_info
*dirty_i
= DIRTY_I(sbi
);
44 /* need not be added */
45 if (IS_CURSEG(sbi
, segno
))
48 if (!test_and_set_bit(segno
, dirty_i
->dirty_segmap
[dirty_type
]))
49 dirty_i
->nr_dirty
[dirty_type
]++;
51 if (dirty_type
== DIRTY
) {
52 struct seg_entry
*sentry
= get_seg_entry(sbi
, segno
);
53 enum dirty_type t
= DIRTY_HOT_DATA
;
55 dirty_type
= sentry
->type
;
57 if (!test_and_set_bit(segno
, dirty_i
->dirty_segmap
[dirty_type
]))
58 dirty_i
->nr_dirty
[dirty_type
]++;
60 /* Only one bitmap should be set */
61 for (; t
<= DIRTY_COLD_NODE
; t
++) {
64 if (test_and_clear_bit(segno
, dirty_i
->dirty_segmap
[t
]))
65 dirty_i
->nr_dirty
[t
]--;
70 static void __remove_dirty_segment(struct f2fs_sb_info
*sbi
, unsigned int segno
,
71 enum dirty_type dirty_type
)
73 struct dirty_seglist_info
*dirty_i
= DIRTY_I(sbi
);
75 if (test_and_clear_bit(segno
, dirty_i
->dirty_segmap
[dirty_type
]))
76 dirty_i
->nr_dirty
[dirty_type
]--;
78 if (dirty_type
== DIRTY
) {
79 enum dirty_type t
= DIRTY_HOT_DATA
;
81 /* clear all the bitmaps */
82 for (; t
<= DIRTY_COLD_NODE
; t
++)
83 if (test_and_clear_bit(segno
, dirty_i
->dirty_segmap
[t
]))
84 dirty_i
->nr_dirty
[t
]--;
86 if (get_valid_blocks(sbi
, segno
, sbi
->segs_per_sec
) == 0)
87 clear_bit(GET_SECNO(sbi
, segno
),
88 dirty_i
->victim_secmap
);
93 * Should not occur error such as -ENOMEM.
94 * Adding dirty entry into seglist is not critical operation.
95 * If a given segment is one of current working segments, it won't be added.
97 void locate_dirty_segment(struct f2fs_sb_info
*sbi
, unsigned int segno
)
99 struct dirty_seglist_info
*dirty_i
= DIRTY_I(sbi
);
100 unsigned short valid_blocks
;
102 if (segno
== NULL_SEGNO
|| IS_CURSEG(sbi
, segno
))
105 mutex_lock(&dirty_i
->seglist_lock
);
107 valid_blocks
= get_valid_blocks(sbi
, segno
, 0);
109 if (valid_blocks
== 0) {
110 __locate_dirty_segment(sbi
, segno
, PRE
);
111 __remove_dirty_segment(sbi
, segno
, DIRTY
);
112 } else if (valid_blocks
< sbi
->blocks_per_seg
) {
113 __locate_dirty_segment(sbi
, segno
, DIRTY
);
115 /* Recovery routine with SSR needs this */
116 __remove_dirty_segment(sbi
, segno
, DIRTY
);
119 mutex_unlock(&dirty_i
->seglist_lock
);
124 * Should call clear_prefree_segments after checkpoint is done.
126 static void set_prefree_as_free_segments(struct f2fs_sb_info
*sbi
)
128 struct dirty_seglist_info
*dirty_i
= DIRTY_I(sbi
);
129 unsigned int segno
, offset
= 0;
130 unsigned int total_segs
= TOTAL_SEGS(sbi
);
132 mutex_lock(&dirty_i
->seglist_lock
);
134 segno
= find_next_bit(dirty_i
->dirty_segmap
[PRE
], total_segs
,
136 if (segno
>= total_segs
)
138 __set_test_and_free(sbi
, segno
);
141 mutex_unlock(&dirty_i
->seglist_lock
);
144 void clear_prefree_segments(struct f2fs_sb_info
*sbi
)
146 struct dirty_seglist_info
*dirty_i
= DIRTY_I(sbi
);
147 unsigned int segno
, offset
= 0;
148 unsigned int total_segs
= TOTAL_SEGS(sbi
);
150 mutex_lock(&dirty_i
->seglist_lock
);
152 segno
= find_next_bit(dirty_i
->dirty_segmap
[PRE
], total_segs
,
154 if (segno
>= total_segs
)
158 if (test_and_clear_bit(segno
, dirty_i
->dirty_segmap
[PRE
]))
159 dirty_i
->nr_dirty
[PRE
]--;
162 if (test_opt(sbi
, DISCARD
))
163 blkdev_issue_discard(sbi
->sb
->s_bdev
,
164 START_BLOCK(sbi
, segno
) <<
165 sbi
->log_sectors_per_block
,
166 1 << (sbi
->log_sectors_per_block
+
167 sbi
->log_blocks_per_seg
),
170 mutex_unlock(&dirty_i
->seglist_lock
);
173 static void __mark_sit_entry_dirty(struct f2fs_sb_info
*sbi
, unsigned int segno
)
175 struct sit_info
*sit_i
= SIT_I(sbi
);
176 if (!__test_and_set_bit(segno
, sit_i
->dirty_sentries_bitmap
))
177 sit_i
->dirty_sentries
++;
180 static void __set_sit_entry_type(struct f2fs_sb_info
*sbi
, int type
,
181 unsigned int segno
, int modified
)
183 struct seg_entry
*se
= get_seg_entry(sbi
, segno
);
186 __mark_sit_entry_dirty(sbi
, segno
);
189 static void update_sit_entry(struct f2fs_sb_info
*sbi
, block_t blkaddr
, int del
)
191 struct seg_entry
*se
;
192 unsigned int segno
, offset
;
193 long int new_vblocks
;
195 segno
= GET_SEGNO(sbi
, blkaddr
);
197 se
= get_seg_entry(sbi
, segno
);
198 new_vblocks
= se
->valid_blocks
+ del
;
199 offset
= GET_SEGOFF_FROM_SEG0(sbi
, blkaddr
) & (sbi
->blocks_per_seg
- 1);
201 BUG_ON((new_vblocks
>> (sizeof(unsigned short) << 3) ||
202 (new_vblocks
> sbi
->blocks_per_seg
)));
204 se
->valid_blocks
= new_vblocks
;
205 se
->mtime
= get_mtime(sbi
);
206 SIT_I(sbi
)->max_mtime
= se
->mtime
;
208 /* Update valid block bitmap */
210 if (f2fs_set_bit(offset
, se
->cur_valid_map
))
213 if (!f2fs_clear_bit(offset
, se
->cur_valid_map
))
216 if (!f2fs_test_bit(offset
, se
->ckpt_valid_map
))
217 se
->ckpt_valid_blocks
+= del
;
219 __mark_sit_entry_dirty(sbi
, segno
);
221 /* update total number of valid blocks to be written in ckpt area */
222 SIT_I(sbi
)->written_valid_blocks
+= del
;
224 if (sbi
->segs_per_sec
> 1)
225 get_sec_entry(sbi
, segno
)->valid_blocks
+= del
;
228 static void refresh_sit_entry(struct f2fs_sb_info
*sbi
,
229 block_t old_blkaddr
, block_t new_blkaddr
)
231 update_sit_entry(sbi
, new_blkaddr
, 1);
232 if (GET_SEGNO(sbi
, old_blkaddr
) != NULL_SEGNO
)
233 update_sit_entry(sbi
, old_blkaddr
, -1);
236 void invalidate_blocks(struct f2fs_sb_info
*sbi
, block_t addr
)
238 unsigned int segno
= GET_SEGNO(sbi
, addr
);
239 struct sit_info
*sit_i
= SIT_I(sbi
);
241 BUG_ON(addr
== NULL_ADDR
);
242 if (addr
== NEW_ADDR
)
245 /* add it into sit main buffer */
246 mutex_lock(&sit_i
->sentry_lock
);
248 update_sit_entry(sbi
, addr
, -1);
250 /* add it into dirty seglist */
251 locate_dirty_segment(sbi
, segno
);
253 mutex_unlock(&sit_i
->sentry_lock
);
257 * This function should be resided under the curseg_mutex lock
259 static void __add_sum_entry(struct f2fs_sb_info
*sbi
, int type
,
260 struct f2fs_summary
*sum
, unsigned short offset
)
262 struct curseg_info
*curseg
= CURSEG_I(sbi
, type
);
263 void *addr
= curseg
->sum_blk
;
264 addr
+= offset
* sizeof(struct f2fs_summary
);
265 memcpy(addr
, sum
, sizeof(struct f2fs_summary
));
270 * Calculate the number of current summary pages for writing
272 int npages_for_summary_flush(struct f2fs_sb_info
*sbi
)
274 int total_size_bytes
= 0;
275 int valid_sum_count
= 0;
278 for (i
= CURSEG_HOT_DATA
; i
<= CURSEG_COLD_DATA
; i
++) {
279 if (sbi
->ckpt
->alloc_type
[i
] == SSR
)
280 valid_sum_count
+= sbi
->blocks_per_seg
;
282 valid_sum_count
+= curseg_blkoff(sbi
, i
);
285 total_size_bytes
= valid_sum_count
* (SUMMARY_SIZE
+ 1)
286 + sizeof(struct nat_journal
) + 2
287 + sizeof(struct sit_journal
) + 2;
288 sum_space
= PAGE_CACHE_SIZE
- SUM_FOOTER_SIZE
;
289 if (total_size_bytes
< sum_space
)
291 else if (total_size_bytes
< 2 * sum_space
)
297 * Caller should put this summary page
299 struct page
*get_sum_page(struct f2fs_sb_info
*sbi
, unsigned int segno
)
301 return get_meta_page(sbi
, GET_SUM_BLOCK(sbi
, segno
));
304 static void write_sum_page(struct f2fs_sb_info
*sbi
,
305 struct f2fs_summary_block
*sum_blk
, block_t blk_addr
)
307 struct page
*page
= grab_meta_page(sbi
, blk_addr
);
308 void *kaddr
= page_address(page
);
309 memcpy(kaddr
, sum_blk
, PAGE_CACHE_SIZE
);
310 set_page_dirty(page
);
311 f2fs_put_page(page
, 1);
314 static unsigned int check_prefree_segments(struct f2fs_sb_info
*sbi
, int type
)
316 struct dirty_seglist_info
*dirty_i
= DIRTY_I(sbi
);
317 unsigned long *prefree_segmap
= dirty_i
->dirty_segmap
[PRE
];
319 unsigned int ofs
= 0;
322 * If there is not enough reserved sections,
323 * we should not reuse prefree segments.
325 if (has_not_enough_free_secs(sbi
, 0))
329 * NODE page should not reuse prefree segment,
330 * since those information is used for SPOR.
332 if (IS_NODESEG(type
))
335 segno
= find_next_bit(prefree_segmap
, TOTAL_SEGS(sbi
), ofs
);
336 ofs
+= sbi
->segs_per_sec
;
338 if (segno
< TOTAL_SEGS(sbi
)) {
341 /* skip intermediate segments in a section */
342 if (segno
% sbi
->segs_per_sec
)
345 /* skip if the section is currently used */
346 if (sec_usage_check(sbi
, GET_SECNO(sbi
, segno
)))
349 /* skip if whole section is not prefree */
350 for (i
= 1; i
< sbi
->segs_per_sec
; i
++)
351 if (!test_bit(segno
+ i
, prefree_segmap
))
354 /* skip if whole section was not free at the last checkpoint */
355 for (i
= 0; i
< sbi
->segs_per_sec
; i
++)
356 if (get_seg_entry(sbi
, segno
+ i
)->ckpt_valid_blocks
)
364 static int is_next_segment_free(struct f2fs_sb_info
*sbi
, int type
)
366 struct curseg_info
*curseg
= CURSEG_I(sbi
, type
);
367 unsigned int segno
= curseg
->segno
;
368 struct free_segmap_info
*free_i
= FREE_I(sbi
);
370 if (segno
+ 1 < TOTAL_SEGS(sbi
) && (segno
+ 1) % sbi
->segs_per_sec
)
371 return !test_bit(segno
+ 1, free_i
->free_segmap
);
376 * Find a new segment from the free segments bitmap to right order
377 * This function should be returned with success, otherwise BUG
379 static void get_new_segment(struct f2fs_sb_info
*sbi
,
380 unsigned int *newseg
, bool new_sec
, int dir
)
382 struct free_segmap_info
*free_i
= FREE_I(sbi
);
383 unsigned int segno
, secno
, zoneno
;
384 unsigned int total_zones
= TOTAL_SECS(sbi
) / sbi
->secs_per_zone
;
385 unsigned int hint
= *newseg
/ sbi
->segs_per_sec
;
386 unsigned int old_zoneno
= GET_ZONENO_FROM_SEGNO(sbi
, *newseg
);
387 unsigned int left_start
= hint
;
392 write_lock(&free_i
->segmap_lock
);
394 if (!new_sec
&& ((*newseg
+ 1) % sbi
->segs_per_sec
)) {
395 segno
= find_next_zero_bit(free_i
->free_segmap
,
396 TOTAL_SEGS(sbi
), *newseg
+ 1);
397 if (segno
- *newseg
< sbi
->segs_per_sec
-
398 (*newseg
% sbi
->segs_per_sec
))
402 secno
= find_next_zero_bit(free_i
->free_secmap
, TOTAL_SECS(sbi
), hint
);
403 if (secno
>= TOTAL_SECS(sbi
)) {
404 if (dir
== ALLOC_RIGHT
) {
405 secno
= find_next_zero_bit(free_i
->free_secmap
,
407 BUG_ON(secno
>= TOTAL_SECS(sbi
));
410 left_start
= hint
- 1;
416 while (test_bit(left_start
, free_i
->free_secmap
)) {
417 if (left_start
> 0) {
421 left_start
= find_next_zero_bit(free_i
->free_secmap
,
423 BUG_ON(left_start
>= TOTAL_SECS(sbi
));
429 segno
= secno
* sbi
->segs_per_sec
;
430 zoneno
= secno
/ sbi
->secs_per_zone
;
432 /* give up on finding another zone */
435 if (sbi
->secs_per_zone
== 1)
437 if (zoneno
== old_zoneno
)
439 if (dir
== ALLOC_LEFT
) {
440 if (!go_left
&& zoneno
+ 1 >= total_zones
)
442 if (go_left
&& zoneno
== 0)
445 for (i
= 0; i
< NR_CURSEG_TYPE
; i
++)
446 if (CURSEG_I(sbi
, i
)->zone
== zoneno
)
449 if (i
< NR_CURSEG_TYPE
) {
450 /* zone is in user, try another */
452 hint
= zoneno
* sbi
->secs_per_zone
- 1;
453 else if (zoneno
+ 1 >= total_zones
)
456 hint
= (zoneno
+ 1) * sbi
->secs_per_zone
;
458 goto find_other_zone
;
461 /* set it as dirty segment in free segmap */
462 BUG_ON(test_bit(segno
, free_i
->free_segmap
));
463 __set_inuse(sbi
, segno
);
465 write_unlock(&free_i
->segmap_lock
);
468 static void reset_curseg(struct f2fs_sb_info
*sbi
, int type
, int modified
)
470 struct curseg_info
*curseg
= CURSEG_I(sbi
, type
);
471 struct summary_footer
*sum_footer
;
473 curseg
->segno
= curseg
->next_segno
;
474 curseg
->zone
= GET_ZONENO_FROM_SEGNO(sbi
, curseg
->segno
);
475 curseg
->next_blkoff
= 0;
476 curseg
->next_segno
= NULL_SEGNO
;
478 sum_footer
= &(curseg
->sum_blk
->footer
);
479 memset(sum_footer
, 0, sizeof(struct summary_footer
));
480 if (IS_DATASEG(type
))
481 SET_SUM_TYPE(sum_footer
, SUM_TYPE_DATA
);
482 if (IS_NODESEG(type
))
483 SET_SUM_TYPE(sum_footer
, SUM_TYPE_NODE
);
484 __set_sit_entry_type(sbi
, type
, curseg
->segno
, modified
);
488 * Allocate a current working segment.
489 * This function always allocates a free segment in LFS manner.
491 static void new_curseg(struct f2fs_sb_info
*sbi
, int type
, bool new_sec
)
493 struct curseg_info
*curseg
= CURSEG_I(sbi
, type
);
494 unsigned int segno
= curseg
->segno
;
495 int dir
= ALLOC_LEFT
;
497 write_sum_page(sbi
, curseg
->sum_blk
,
498 GET_SUM_BLOCK(sbi
, curseg
->segno
));
499 if (type
== CURSEG_WARM_DATA
|| type
== CURSEG_COLD_DATA
)
502 if (test_opt(sbi
, NOHEAP
))
505 get_new_segment(sbi
, &segno
, new_sec
, dir
);
506 curseg
->next_segno
= segno
;
507 reset_curseg(sbi
, type
, 1);
508 curseg
->alloc_type
= LFS
;
511 static void __next_free_blkoff(struct f2fs_sb_info
*sbi
,
512 struct curseg_info
*seg
, block_t start
)
514 struct seg_entry
*se
= get_seg_entry(sbi
, seg
->segno
);
516 for (ofs
= start
; ofs
< sbi
->blocks_per_seg
; ofs
++) {
517 if (!f2fs_test_bit(ofs
, se
->ckpt_valid_map
)
518 && !f2fs_test_bit(ofs
, se
->cur_valid_map
))
521 seg
->next_blkoff
= ofs
;
525 * If a segment is written by LFS manner, next block offset is just obtained
526 * by increasing the current block offset. However, if a segment is written by
527 * SSR manner, next block offset obtained by calling __next_free_blkoff
529 static void __refresh_next_blkoff(struct f2fs_sb_info
*sbi
,
530 struct curseg_info
*seg
)
532 if (seg
->alloc_type
== SSR
)
533 __next_free_blkoff(sbi
, seg
, seg
->next_blkoff
+ 1);
539 * This function always allocates a used segment (from dirty seglist) by SSR
540 * manner, so it should recover the existing segment information of valid blocks
542 static void change_curseg(struct f2fs_sb_info
*sbi
, int type
, bool reuse
)
544 struct dirty_seglist_info
*dirty_i
= DIRTY_I(sbi
);
545 struct curseg_info
*curseg
= CURSEG_I(sbi
, type
);
546 unsigned int new_segno
= curseg
->next_segno
;
547 struct f2fs_summary_block
*sum_node
;
548 struct page
*sum_page
;
550 write_sum_page(sbi
, curseg
->sum_blk
,
551 GET_SUM_BLOCK(sbi
, curseg
->segno
));
552 __set_test_and_inuse(sbi
, new_segno
);
554 mutex_lock(&dirty_i
->seglist_lock
);
555 __remove_dirty_segment(sbi
, new_segno
, PRE
);
556 __remove_dirty_segment(sbi
, new_segno
, DIRTY
);
557 mutex_unlock(&dirty_i
->seglist_lock
);
559 reset_curseg(sbi
, type
, 1);
560 curseg
->alloc_type
= SSR
;
561 __next_free_blkoff(sbi
, curseg
, 0);
564 sum_page
= get_sum_page(sbi
, new_segno
);
565 sum_node
= (struct f2fs_summary_block
*)page_address(sum_page
);
566 memcpy(curseg
->sum_blk
, sum_node
, SUM_ENTRY_SIZE
);
567 f2fs_put_page(sum_page
, 1);
571 static int get_ssr_segment(struct f2fs_sb_info
*sbi
, int type
)
573 struct curseg_info
*curseg
= CURSEG_I(sbi
, type
);
574 const struct victim_selection
*v_ops
= DIRTY_I(sbi
)->v_ops
;
576 if (IS_NODESEG(type
) || !has_not_enough_free_secs(sbi
, 0))
577 return v_ops
->get_victim(sbi
,
578 &(curseg
)->next_segno
, BG_GC
, type
, SSR
);
580 /* For data segments, let's do SSR more intensively */
581 for (; type
>= CURSEG_HOT_DATA
; type
--)
582 if (v_ops
->get_victim(sbi
, &(curseg
)->next_segno
,
589 * flush out current segment and replace it with new segment
590 * This function should be returned with success, otherwise BUG
592 static void allocate_segment_by_default(struct f2fs_sb_info
*sbi
,
593 int type
, bool force
)
595 struct curseg_info
*curseg
= CURSEG_I(sbi
, type
);
598 new_curseg(sbi
, type
, true);
602 curseg
->next_segno
= check_prefree_segments(sbi
, type
);
604 if (curseg
->next_segno
!= NULL_SEGNO
)
605 change_curseg(sbi
, type
, false);
606 else if (type
== CURSEG_WARM_NODE
)
607 new_curseg(sbi
, type
, false);
608 else if (curseg
->alloc_type
== LFS
&& is_next_segment_free(sbi
, type
))
609 new_curseg(sbi
, type
, false);
610 else if (need_SSR(sbi
) && get_ssr_segment(sbi
, type
))
611 change_curseg(sbi
, type
, true);
613 new_curseg(sbi
, type
, false);
615 sbi
->segment_count
[curseg
->alloc_type
]++;
618 void allocate_new_segments(struct f2fs_sb_info
*sbi
)
620 struct curseg_info
*curseg
;
621 unsigned int old_curseg
;
624 for (i
= CURSEG_HOT_DATA
; i
<= CURSEG_COLD_DATA
; i
++) {
625 curseg
= CURSEG_I(sbi
, i
);
626 old_curseg
= curseg
->segno
;
627 SIT_I(sbi
)->s_ops
->allocate_segment(sbi
, i
, true);
628 locate_dirty_segment(sbi
, old_curseg
);
632 static const struct segment_allocation default_salloc_ops
= {
633 .allocate_segment
= allocate_segment_by_default
,
636 static void f2fs_end_io_write(struct bio
*bio
, int err
)
638 const int uptodate
= test_bit(BIO_UPTODATE
, &bio
->bi_flags
);
639 struct bio_vec
*bvec
= bio
->bi_io_vec
+ bio
->bi_vcnt
- 1;
640 struct bio_private
*p
= bio
->bi_private
;
643 struct page
*page
= bvec
->bv_page
;
645 if (--bvec
>= bio
->bi_io_vec
)
646 prefetchw(&bvec
->bv_page
->flags
);
650 set_bit(AS_EIO
, &page
->mapping
->flags
);
651 set_ckpt_flags(p
->sbi
->ckpt
, CP_ERROR_FLAG
);
652 p
->sbi
->sb
->s_flags
|= MS_RDONLY
;
654 end_page_writeback(page
);
655 dec_page_count(p
->sbi
, F2FS_WRITEBACK
);
656 } while (bvec
>= bio
->bi_io_vec
);
664 struct bio
*f2fs_bio_alloc(struct block_device
*bdev
, int npages
)
667 struct bio_private
*priv
;
669 priv
= kmalloc(sizeof(struct bio_private
), GFP_NOFS
);
675 /* No failure on bio allocation */
676 bio
= bio_alloc(GFP_NOIO
, npages
);
678 bio
->bi_private
= priv
;
682 static void do_submit_bio(struct f2fs_sb_info
*sbi
,
683 enum page_type type
, bool sync
)
685 int rw
= sync
? WRITE_SYNC
: WRITE
;
686 enum page_type btype
= type
> META
? META
: type
;
688 if (type
>= META_FLUSH
)
689 rw
= WRITE_FLUSH_FUA
;
694 if (sbi
->bio
[btype
]) {
695 struct bio_private
*p
= sbi
->bio
[btype
]->bi_private
;
697 sbi
->bio
[btype
]->bi_end_io
= f2fs_end_io_write
;
699 trace_f2fs_do_submit_bio(sbi
->sb
, btype
, sync
, sbi
->bio
[btype
]);
701 if (type
== META_FLUSH
) {
702 DECLARE_COMPLETION_ONSTACK(wait
);
705 submit_bio(rw
, sbi
->bio
[btype
]);
706 wait_for_completion(&wait
);
709 submit_bio(rw
, sbi
->bio
[btype
]);
711 sbi
->bio
[btype
] = NULL
;
715 void f2fs_submit_bio(struct f2fs_sb_info
*sbi
, enum page_type type
, bool sync
)
717 down_write(&sbi
->bio_sem
);
718 do_submit_bio(sbi
, type
, sync
);
719 up_write(&sbi
->bio_sem
);
722 static void submit_write_page(struct f2fs_sb_info
*sbi
, struct page
*page
,
723 block_t blk_addr
, enum page_type type
)
725 struct block_device
*bdev
= sbi
->sb
->s_bdev
;
727 verify_block_addr(sbi
, blk_addr
);
729 down_write(&sbi
->bio_sem
);
731 inc_page_count(sbi
, F2FS_WRITEBACK
);
733 if (sbi
->bio
[type
] && sbi
->last_block_in_bio
[type
] != blk_addr
- 1)
734 do_submit_bio(sbi
, type
, false);
736 if (sbi
->bio
[type
] == NULL
) {
737 sbi
->bio
[type
] = f2fs_bio_alloc(bdev
, bio_get_nr_vecs(bdev
));
738 sbi
->bio
[type
]->bi_sector
= SECTOR_FROM_BLOCK(sbi
, blk_addr
);
740 * The end_io will be assigned at the sumbission phase.
741 * Until then, let bio_add_page() merge consecutive IOs as much
746 if (bio_add_page(sbi
->bio
[type
], page
, PAGE_CACHE_SIZE
, 0) <
748 do_submit_bio(sbi
, type
, false);
752 sbi
->last_block_in_bio
[type
] = blk_addr
;
754 up_write(&sbi
->bio_sem
);
755 trace_f2fs_submit_write_page(page
, blk_addr
, type
);
758 static bool __has_curseg_space(struct f2fs_sb_info
*sbi
, int type
)
760 struct curseg_info
*curseg
= CURSEG_I(sbi
, type
);
761 if (curseg
->next_blkoff
< sbi
->blocks_per_seg
)
766 static int __get_segment_type_2(struct page
*page
, enum page_type p_type
)
769 return CURSEG_HOT_DATA
;
771 return CURSEG_HOT_NODE
;
774 static int __get_segment_type_4(struct page
*page
, enum page_type p_type
)
776 if (p_type
== DATA
) {
777 struct inode
*inode
= page
->mapping
->host
;
779 if (S_ISDIR(inode
->i_mode
))
780 return CURSEG_HOT_DATA
;
782 return CURSEG_COLD_DATA
;
784 if (IS_DNODE(page
) && !is_cold_node(page
))
785 return CURSEG_HOT_NODE
;
787 return CURSEG_COLD_NODE
;
791 static int __get_segment_type_6(struct page
*page
, enum page_type p_type
)
793 if (p_type
== DATA
) {
794 struct inode
*inode
= page
->mapping
->host
;
796 if (S_ISDIR(inode
->i_mode
))
797 return CURSEG_HOT_DATA
;
798 else if (is_cold_data(page
) || is_cold_file(inode
))
799 return CURSEG_COLD_DATA
;
801 return CURSEG_WARM_DATA
;
804 return is_cold_node(page
) ? CURSEG_WARM_NODE
:
807 return CURSEG_COLD_NODE
;
811 static int __get_segment_type(struct page
*page
, enum page_type p_type
)
813 struct f2fs_sb_info
*sbi
= F2FS_SB(page
->mapping
->host
->i_sb
);
814 switch (sbi
->active_logs
) {
816 return __get_segment_type_2(page
, p_type
);
818 return __get_segment_type_4(page
, p_type
);
820 /* NR_CURSEG_TYPE(6) logs by default */
821 BUG_ON(sbi
->active_logs
!= NR_CURSEG_TYPE
);
822 return __get_segment_type_6(page
, p_type
);
825 static void do_write_page(struct f2fs_sb_info
*sbi
, struct page
*page
,
826 block_t old_blkaddr
, block_t
*new_blkaddr
,
827 struct f2fs_summary
*sum
, enum page_type p_type
)
829 struct sit_info
*sit_i
= SIT_I(sbi
);
830 struct curseg_info
*curseg
;
831 unsigned int old_cursegno
;
834 type
= __get_segment_type(page
, p_type
);
835 curseg
= CURSEG_I(sbi
, type
);
837 mutex_lock(&curseg
->curseg_mutex
);
839 *new_blkaddr
= NEXT_FREE_BLKADDR(sbi
, curseg
);
840 old_cursegno
= curseg
->segno
;
843 * __add_sum_entry should be resided under the curseg_mutex
844 * because, this function updates a summary entry in the
845 * current summary block.
847 __add_sum_entry(sbi
, type
, sum
, curseg
->next_blkoff
);
849 mutex_lock(&sit_i
->sentry_lock
);
850 __refresh_next_blkoff(sbi
, curseg
);
851 sbi
->block_count
[curseg
->alloc_type
]++;
854 * SIT information should be updated before segment allocation,
855 * since SSR needs latest valid block information.
857 refresh_sit_entry(sbi
, old_blkaddr
, *new_blkaddr
);
859 if (!__has_curseg_space(sbi
, type
))
860 sit_i
->s_ops
->allocate_segment(sbi
, type
, false);
862 locate_dirty_segment(sbi
, old_cursegno
);
863 locate_dirty_segment(sbi
, GET_SEGNO(sbi
, old_blkaddr
));
864 mutex_unlock(&sit_i
->sentry_lock
);
867 fill_node_footer_blkaddr(page
, NEXT_FREE_BLKADDR(sbi
, curseg
));
869 /* writeout dirty page into bdev */
870 submit_write_page(sbi
, page
, *new_blkaddr
, p_type
);
872 mutex_unlock(&curseg
->curseg_mutex
);
875 void write_meta_page(struct f2fs_sb_info
*sbi
, struct page
*page
)
877 set_page_writeback(page
);
878 submit_write_page(sbi
, page
, page
->index
, META
);
881 void write_node_page(struct f2fs_sb_info
*sbi
, struct page
*page
,
882 unsigned int nid
, block_t old_blkaddr
, block_t
*new_blkaddr
)
884 struct f2fs_summary sum
;
885 set_summary(&sum
, nid
, 0, 0);
886 do_write_page(sbi
, page
, old_blkaddr
, new_blkaddr
, &sum
, NODE
);
889 void write_data_page(struct inode
*inode
, struct page
*page
,
890 struct dnode_of_data
*dn
, block_t old_blkaddr
,
891 block_t
*new_blkaddr
)
893 struct f2fs_sb_info
*sbi
= F2FS_SB(inode
->i_sb
);
894 struct f2fs_summary sum
;
897 BUG_ON(old_blkaddr
== NULL_ADDR
);
898 get_node_info(sbi
, dn
->nid
, &ni
);
899 set_summary(&sum
, dn
->nid
, dn
->ofs_in_node
, ni
.version
);
901 do_write_page(sbi
, page
, old_blkaddr
,
902 new_blkaddr
, &sum
, DATA
);
905 void rewrite_data_page(struct f2fs_sb_info
*sbi
, struct page
*page
,
906 block_t old_blk_addr
)
908 submit_write_page(sbi
, page
, old_blk_addr
, DATA
);
911 void recover_data_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 struct curseg_info
*curseg
;
917 unsigned int segno
, old_cursegno
;
918 struct seg_entry
*se
;
921 segno
= GET_SEGNO(sbi
, new_blkaddr
);
922 se
= get_seg_entry(sbi
, segno
);
925 if (se
->valid_blocks
== 0 && !IS_CURSEG(sbi
, segno
)) {
926 if (old_blkaddr
== NULL_ADDR
)
927 type
= CURSEG_COLD_DATA
;
929 type
= CURSEG_WARM_DATA
;
931 curseg
= CURSEG_I(sbi
, type
);
933 mutex_lock(&curseg
->curseg_mutex
);
934 mutex_lock(&sit_i
->sentry_lock
);
936 old_cursegno
= curseg
->segno
;
938 /* change the current segment */
939 if (segno
!= curseg
->segno
) {
940 curseg
->next_segno
= segno
;
941 change_curseg(sbi
, type
, true);
944 curseg
->next_blkoff
= GET_SEGOFF_FROM_SEG0(sbi
, new_blkaddr
) &
945 (sbi
->blocks_per_seg
- 1);
946 __add_sum_entry(sbi
, type
, sum
, curseg
->next_blkoff
);
948 refresh_sit_entry(sbi
, old_blkaddr
, new_blkaddr
);
950 locate_dirty_segment(sbi
, old_cursegno
);
951 locate_dirty_segment(sbi
, GET_SEGNO(sbi
, old_blkaddr
));
953 mutex_unlock(&sit_i
->sentry_lock
);
954 mutex_unlock(&curseg
->curseg_mutex
);
957 void rewrite_node_page(struct f2fs_sb_info
*sbi
,
958 struct page
*page
, struct f2fs_summary
*sum
,
959 block_t old_blkaddr
, block_t new_blkaddr
)
961 struct sit_info
*sit_i
= SIT_I(sbi
);
962 int type
= CURSEG_WARM_NODE
;
963 struct curseg_info
*curseg
;
964 unsigned int segno
, old_cursegno
;
965 block_t next_blkaddr
= next_blkaddr_of_node(page
);
966 unsigned int next_segno
= GET_SEGNO(sbi
, next_blkaddr
);
968 curseg
= CURSEG_I(sbi
, type
);
970 mutex_lock(&curseg
->curseg_mutex
);
971 mutex_lock(&sit_i
->sentry_lock
);
973 segno
= GET_SEGNO(sbi
, new_blkaddr
);
974 old_cursegno
= curseg
->segno
;
976 /* change the current segment */
977 if (segno
!= curseg
->segno
) {
978 curseg
->next_segno
= segno
;
979 change_curseg(sbi
, type
, true);
981 curseg
->next_blkoff
= GET_SEGOFF_FROM_SEG0(sbi
, new_blkaddr
) &
982 (sbi
->blocks_per_seg
- 1);
983 __add_sum_entry(sbi
, type
, sum
, curseg
->next_blkoff
);
985 /* change the current log to the next block addr in advance */
986 if (next_segno
!= segno
) {
987 curseg
->next_segno
= next_segno
;
988 change_curseg(sbi
, type
, true);
990 curseg
->next_blkoff
= GET_SEGOFF_FROM_SEG0(sbi
, next_blkaddr
) &
991 (sbi
->blocks_per_seg
- 1);
993 /* rewrite node page */
994 set_page_writeback(page
);
995 submit_write_page(sbi
, page
, new_blkaddr
, NODE
);
996 f2fs_submit_bio(sbi
, NODE
, true);
997 refresh_sit_entry(sbi
, old_blkaddr
, new_blkaddr
);
999 locate_dirty_segment(sbi
, old_cursegno
);
1000 locate_dirty_segment(sbi
, GET_SEGNO(sbi
, old_blkaddr
));
1002 mutex_unlock(&sit_i
->sentry_lock
);
1003 mutex_unlock(&curseg
->curseg_mutex
);
1006 static int read_compacted_summaries(struct f2fs_sb_info
*sbi
)
1008 struct f2fs_checkpoint
*ckpt
= F2FS_CKPT(sbi
);
1009 struct curseg_info
*seg_i
;
1010 unsigned char *kaddr
;
1015 start
= start_sum_block(sbi
);
1017 page
= get_meta_page(sbi
, start
++);
1018 kaddr
= (unsigned char *)page_address(page
);
1020 /* Step 1: restore nat cache */
1021 seg_i
= CURSEG_I(sbi
, CURSEG_HOT_DATA
);
1022 memcpy(&seg_i
->sum_blk
->n_nats
, kaddr
, SUM_JOURNAL_SIZE
);
1024 /* Step 2: restore sit cache */
1025 seg_i
= CURSEG_I(sbi
, CURSEG_COLD_DATA
);
1026 memcpy(&seg_i
->sum_blk
->n_sits
, kaddr
+ SUM_JOURNAL_SIZE
,
1028 offset
= 2 * SUM_JOURNAL_SIZE
;
1030 /* Step 3: restore summary entries */
1031 for (i
= CURSEG_HOT_DATA
; i
<= CURSEG_COLD_DATA
; i
++) {
1032 unsigned short blk_off
;
1035 seg_i
= CURSEG_I(sbi
, i
);
1036 segno
= le32_to_cpu(ckpt
->cur_data_segno
[i
]);
1037 blk_off
= le16_to_cpu(ckpt
->cur_data_blkoff
[i
]);
1038 seg_i
->next_segno
= segno
;
1039 reset_curseg(sbi
, i
, 0);
1040 seg_i
->alloc_type
= ckpt
->alloc_type
[i
];
1041 seg_i
->next_blkoff
= blk_off
;
1043 if (seg_i
->alloc_type
== SSR
)
1044 blk_off
= sbi
->blocks_per_seg
;
1046 for (j
= 0; j
< blk_off
; j
++) {
1047 struct f2fs_summary
*s
;
1048 s
= (struct f2fs_summary
*)(kaddr
+ offset
);
1049 seg_i
->sum_blk
->entries
[j
] = *s
;
1050 offset
+= SUMMARY_SIZE
;
1051 if (offset
+ SUMMARY_SIZE
<= PAGE_CACHE_SIZE
-
1055 f2fs_put_page(page
, 1);
1058 page
= get_meta_page(sbi
, start
++);
1059 kaddr
= (unsigned char *)page_address(page
);
1063 f2fs_put_page(page
, 1);
1067 static int read_normal_summaries(struct f2fs_sb_info
*sbi
, int type
)
1069 struct f2fs_checkpoint
*ckpt
= F2FS_CKPT(sbi
);
1070 struct f2fs_summary_block
*sum
;
1071 struct curseg_info
*curseg
;
1073 unsigned short blk_off
;
1074 unsigned int segno
= 0;
1075 block_t blk_addr
= 0;
1077 /* get segment number and block addr */
1078 if (IS_DATASEG(type
)) {
1079 segno
= le32_to_cpu(ckpt
->cur_data_segno
[type
]);
1080 blk_off
= le16_to_cpu(ckpt
->cur_data_blkoff
[type
-
1082 if (is_set_ckpt_flags(ckpt
, CP_UMOUNT_FLAG
))
1083 blk_addr
= sum_blk_addr(sbi
, NR_CURSEG_TYPE
, type
);
1085 blk_addr
= sum_blk_addr(sbi
, NR_CURSEG_DATA_TYPE
, type
);
1087 segno
= le32_to_cpu(ckpt
->cur_node_segno
[type
-
1089 blk_off
= le16_to_cpu(ckpt
->cur_node_blkoff
[type
-
1091 if (is_set_ckpt_flags(ckpt
, CP_UMOUNT_FLAG
))
1092 blk_addr
= sum_blk_addr(sbi
, NR_CURSEG_NODE_TYPE
,
1093 type
- CURSEG_HOT_NODE
);
1095 blk_addr
= GET_SUM_BLOCK(sbi
, segno
);
1098 new = get_meta_page(sbi
, blk_addr
);
1099 sum
= (struct f2fs_summary_block
*)page_address(new);
1101 if (IS_NODESEG(type
)) {
1102 if (is_set_ckpt_flags(ckpt
, CP_UMOUNT_FLAG
)) {
1103 struct f2fs_summary
*ns
= &sum
->entries
[0];
1105 for (i
= 0; i
< sbi
->blocks_per_seg
; i
++, ns
++) {
1107 ns
->ofs_in_node
= 0;
1110 if (restore_node_summary(sbi
, segno
, sum
)) {
1111 f2fs_put_page(new, 1);
1117 /* set uncompleted segment to curseg */
1118 curseg
= CURSEG_I(sbi
, type
);
1119 mutex_lock(&curseg
->curseg_mutex
);
1120 memcpy(curseg
->sum_blk
, sum
, PAGE_CACHE_SIZE
);
1121 curseg
->next_segno
= segno
;
1122 reset_curseg(sbi
, type
, 0);
1123 curseg
->alloc_type
= ckpt
->alloc_type
[type
];
1124 curseg
->next_blkoff
= blk_off
;
1125 mutex_unlock(&curseg
->curseg_mutex
);
1126 f2fs_put_page(new, 1);
1130 static int restore_curseg_summaries(struct f2fs_sb_info
*sbi
)
1132 int type
= CURSEG_HOT_DATA
;
1134 if (is_set_ckpt_flags(F2FS_CKPT(sbi
), CP_COMPACT_SUM_FLAG
)) {
1135 /* restore for compacted data summary */
1136 if (read_compacted_summaries(sbi
))
1138 type
= CURSEG_HOT_NODE
;
1141 for (; type
<= CURSEG_COLD_NODE
; type
++)
1142 if (read_normal_summaries(sbi
, type
))
1147 static void write_compacted_summaries(struct f2fs_sb_info
*sbi
, block_t blkaddr
)
1150 unsigned char *kaddr
;
1151 struct f2fs_summary
*summary
;
1152 struct curseg_info
*seg_i
;
1153 int written_size
= 0;
1156 page
= grab_meta_page(sbi
, blkaddr
++);
1157 kaddr
= (unsigned char *)page_address(page
);
1159 /* Step 1: write nat cache */
1160 seg_i
= CURSEG_I(sbi
, CURSEG_HOT_DATA
);
1161 memcpy(kaddr
, &seg_i
->sum_blk
->n_nats
, SUM_JOURNAL_SIZE
);
1162 written_size
+= SUM_JOURNAL_SIZE
;
1164 /* Step 2: write sit cache */
1165 seg_i
= CURSEG_I(sbi
, CURSEG_COLD_DATA
);
1166 memcpy(kaddr
+ written_size
, &seg_i
->sum_blk
->n_sits
,
1168 written_size
+= SUM_JOURNAL_SIZE
;
1170 set_page_dirty(page
);
1172 /* Step 3: write summary entries */
1173 for (i
= CURSEG_HOT_DATA
; i
<= CURSEG_COLD_DATA
; i
++) {
1174 unsigned short blkoff
;
1175 seg_i
= CURSEG_I(sbi
, i
);
1176 if (sbi
->ckpt
->alloc_type
[i
] == SSR
)
1177 blkoff
= sbi
->blocks_per_seg
;
1179 blkoff
= curseg_blkoff(sbi
, i
);
1181 for (j
= 0; j
< blkoff
; j
++) {
1183 page
= grab_meta_page(sbi
, blkaddr
++);
1184 kaddr
= (unsigned char *)page_address(page
);
1187 summary
= (struct f2fs_summary
*)(kaddr
+ written_size
);
1188 *summary
= seg_i
->sum_blk
->entries
[j
];
1189 written_size
+= SUMMARY_SIZE
;
1190 set_page_dirty(page
);
1192 if (written_size
+ SUMMARY_SIZE
<= PAGE_CACHE_SIZE
-
1196 f2fs_put_page(page
, 1);
1201 f2fs_put_page(page
, 1);
1204 static void write_normal_summaries(struct f2fs_sb_info
*sbi
,
1205 block_t blkaddr
, int type
)
1208 if (IS_DATASEG(type
))
1209 end
= type
+ NR_CURSEG_DATA_TYPE
;
1211 end
= type
+ NR_CURSEG_NODE_TYPE
;
1213 for (i
= type
; i
< end
; i
++) {
1214 struct curseg_info
*sum
= CURSEG_I(sbi
, i
);
1215 mutex_lock(&sum
->curseg_mutex
);
1216 write_sum_page(sbi
, sum
->sum_blk
, blkaddr
+ (i
- type
));
1217 mutex_unlock(&sum
->curseg_mutex
);
1221 void write_data_summaries(struct f2fs_sb_info
*sbi
, block_t start_blk
)
1223 if (is_set_ckpt_flags(F2FS_CKPT(sbi
), CP_COMPACT_SUM_FLAG
))
1224 write_compacted_summaries(sbi
, start_blk
);
1226 write_normal_summaries(sbi
, start_blk
, CURSEG_HOT_DATA
);
1229 void write_node_summaries(struct f2fs_sb_info
*sbi
, block_t start_blk
)
1231 if (is_set_ckpt_flags(F2FS_CKPT(sbi
), CP_UMOUNT_FLAG
))
1232 write_normal_summaries(sbi
, start_blk
, CURSEG_HOT_NODE
);
1236 int lookup_journal_in_cursum(struct f2fs_summary_block
*sum
, int type
,
1237 unsigned int val
, int alloc
)
1241 if (type
== NAT_JOURNAL
) {
1242 for (i
= 0; i
< nats_in_cursum(sum
); i
++) {
1243 if (le32_to_cpu(nid_in_journal(sum
, i
)) == val
)
1246 if (alloc
&& nats_in_cursum(sum
) < NAT_JOURNAL_ENTRIES
)
1247 return update_nats_in_cursum(sum
, 1);
1248 } else if (type
== SIT_JOURNAL
) {
1249 for (i
= 0; i
< sits_in_cursum(sum
); i
++)
1250 if (le32_to_cpu(segno_in_journal(sum
, i
)) == val
)
1252 if (alloc
&& sits_in_cursum(sum
) < SIT_JOURNAL_ENTRIES
)
1253 return update_sits_in_cursum(sum
, 1);
1258 static struct page
*get_current_sit_page(struct f2fs_sb_info
*sbi
,
1261 struct sit_info
*sit_i
= SIT_I(sbi
);
1262 unsigned int offset
= SIT_BLOCK_OFFSET(sit_i
, segno
);
1263 block_t blk_addr
= sit_i
->sit_base_addr
+ offset
;
1265 check_seg_range(sbi
, segno
);
1267 /* calculate sit block address */
1268 if (f2fs_test_bit(offset
, sit_i
->sit_bitmap
))
1269 blk_addr
+= sit_i
->sit_blocks
;
1271 return get_meta_page(sbi
, blk_addr
);
1274 static struct page
*get_next_sit_page(struct f2fs_sb_info
*sbi
,
1277 struct sit_info
*sit_i
= SIT_I(sbi
);
1278 struct page
*src_page
, *dst_page
;
1279 pgoff_t src_off
, dst_off
;
1280 void *src_addr
, *dst_addr
;
1282 src_off
= current_sit_addr(sbi
, start
);
1283 dst_off
= next_sit_addr(sbi
, src_off
);
1285 /* get current sit block page without lock */
1286 src_page
= get_meta_page(sbi
, src_off
);
1287 dst_page
= grab_meta_page(sbi
, dst_off
);
1288 BUG_ON(PageDirty(src_page
));
1290 src_addr
= page_address(src_page
);
1291 dst_addr
= page_address(dst_page
);
1292 memcpy(dst_addr
, src_addr
, PAGE_CACHE_SIZE
);
1294 set_page_dirty(dst_page
);
1295 f2fs_put_page(src_page
, 1);
1297 set_to_next_sit(sit_i
, start
);
1302 static bool flush_sits_in_journal(struct f2fs_sb_info
*sbi
)
1304 struct curseg_info
*curseg
= CURSEG_I(sbi
, CURSEG_COLD_DATA
);
1305 struct f2fs_summary_block
*sum
= curseg
->sum_blk
;
1309 * If the journal area in the current summary is full of sit entries,
1310 * all the sit entries will be flushed. Otherwise the sit entries
1311 * are not able to replace with newly hot sit entries.
1313 if (sits_in_cursum(sum
) >= SIT_JOURNAL_ENTRIES
) {
1314 for (i
= sits_in_cursum(sum
) - 1; i
>= 0; i
--) {
1316 segno
= le32_to_cpu(segno_in_journal(sum
, i
));
1317 __mark_sit_entry_dirty(sbi
, segno
);
1319 update_sits_in_cursum(sum
, -sits_in_cursum(sum
));
1326 * CP calls this function, which flushes SIT entries including sit_journal,
1327 * and moves prefree segs to free segs.
1329 void flush_sit_entries(struct f2fs_sb_info
*sbi
)
1331 struct sit_info
*sit_i
= SIT_I(sbi
);
1332 unsigned long *bitmap
= sit_i
->dirty_sentries_bitmap
;
1333 struct curseg_info
*curseg
= CURSEG_I(sbi
, CURSEG_COLD_DATA
);
1334 struct f2fs_summary_block
*sum
= curseg
->sum_blk
;
1335 unsigned long nsegs
= TOTAL_SEGS(sbi
);
1336 struct page
*page
= NULL
;
1337 struct f2fs_sit_block
*raw_sit
= NULL
;
1338 unsigned int start
= 0, end
= 0;
1339 unsigned int segno
= -1;
1342 mutex_lock(&curseg
->curseg_mutex
);
1343 mutex_lock(&sit_i
->sentry_lock
);
1346 * "flushed" indicates whether sit entries in journal are flushed
1347 * to the SIT area or not.
1349 flushed
= flush_sits_in_journal(sbi
);
1351 while ((segno
= find_next_bit(bitmap
, nsegs
, segno
+ 1)) < nsegs
) {
1352 struct seg_entry
*se
= get_seg_entry(sbi
, segno
);
1353 int sit_offset
, offset
;
1355 sit_offset
= SIT_ENTRY_OFFSET(sit_i
, segno
);
1360 offset
= lookup_journal_in_cursum(sum
, SIT_JOURNAL
, segno
, 1);
1362 segno_in_journal(sum
, offset
) = cpu_to_le32(segno
);
1363 seg_info_to_raw_sit(se
, &sit_in_journal(sum
, offset
));
1367 if (!page
|| (start
> segno
) || (segno
> end
)) {
1369 f2fs_put_page(page
, 1);
1373 start
= START_SEGNO(sit_i
, segno
);
1374 end
= start
+ SIT_ENTRY_PER_BLOCK
- 1;
1376 /* read sit block that will be updated */
1377 page
= get_next_sit_page(sbi
, start
);
1378 raw_sit
= page_address(page
);
1381 /* udpate entry in SIT block */
1382 seg_info_to_raw_sit(se
, &raw_sit
->entries
[sit_offset
]);
1384 __clear_bit(segno
, bitmap
);
1385 sit_i
->dirty_sentries
--;
1387 mutex_unlock(&sit_i
->sentry_lock
);
1388 mutex_unlock(&curseg
->curseg_mutex
);
1390 /* writeout last modified SIT block */
1391 f2fs_put_page(page
, 1);
1393 set_prefree_as_free_segments(sbi
);
1396 static int build_sit_info(struct f2fs_sb_info
*sbi
)
1398 struct f2fs_super_block
*raw_super
= F2FS_RAW_SUPER(sbi
);
1399 struct f2fs_checkpoint
*ckpt
= F2FS_CKPT(sbi
);
1400 struct sit_info
*sit_i
;
1401 unsigned int sit_segs
, start
;
1402 char *src_bitmap
, *dst_bitmap
;
1403 unsigned int bitmap_size
;
1405 /* allocate memory for SIT information */
1406 sit_i
= kzalloc(sizeof(struct sit_info
), GFP_KERNEL
);
1410 SM_I(sbi
)->sit_info
= sit_i
;
1412 sit_i
->sentries
= vzalloc(TOTAL_SEGS(sbi
) * sizeof(struct seg_entry
));
1413 if (!sit_i
->sentries
)
1416 bitmap_size
= f2fs_bitmap_size(TOTAL_SEGS(sbi
));
1417 sit_i
->dirty_sentries_bitmap
= kzalloc(bitmap_size
, GFP_KERNEL
);
1418 if (!sit_i
->dirty_sentries_bitmap
)
1421 for (start
= 0; start
< TOTAL_SEGS(sbi
); start
++) {
1422 sit_i
->sentries
[start
].cur_valid_map
1423 = kzalloc(SIT_VBLOCK_MAP_SIZE
, GFP_KERNEL
);
1424 sit_i
->sentries
[start
].ckpt_valid_map
1425 = kzalloc(SIT_VBLOCK_MAP_SIZE
, GFP_KERNEL
);
1426 if (!sit_i
->sentries
[start
].cur_valid_map
1427 || !sit_i
->sentries
[start
].ckpt_valid_map
)
1431 if (sbi
->segs_per_sec
> 1) {
1432 sit_i
->sec_entries
= vzalloc(TOTAL_SECS(sbi
) *
1433 sizeof(struct sec_entry
));
1434 if (!sit_i
->sec_entries
)
1438 /* get information related with SIT */
1439 sit_segs
= le32_to_cpu(raw_super
->segment_count_sit
) >> 1;
1441 /* setup SIT bitmap from ckeckpoint pack */
1442 bitmap_size
= __bitmap_size(sbi
, SIT_BITMAP
);
1443 src_bitmap
= __bitmap_ptr(sbi
, SIT_BITMAP
);
1445 dst_bitmap
= kmemdup(src_bitmap
, bitmap_size
, GFP_KERNEL
);
1449 /* init SIT information */
1450 sit_i
->s_ops
= &default_salloc_ops
;
1452 sit_i
->sit_base_addr
= le32_to_cpu(raw_super
->sit_blkaddr
);
1453 sit_i
->sit_blocks
= sit_segs
<< sbi
->log_blocks_per_seg
;
1454 sit_i
->written_valid_blocks
= le64_to_cpu(ckpt
->valid_block_count
);
1455 sit_i
->sit_bitmap
= dst_bitmap
;
1456 sit_i
->bitmap_size
= bitmap_size
;
1457 sit_i
->dirty_sentries
= 0;
1458 sit_i
->sents_per_block
= SIT_ENTRY_PER_BLOCK
;
1459 sit_i
->elapsed_time
= le64_to_cpu(sbi
->ckpt
->elapsed_time
);
1460 sit_i
->mounted_time
= CURRENT_TIME_SEC
.tv_sec
;
1461 mutex_init(&sit_i
->sentry_lock
);
1465 static int build_free_segmap(struct f2fs_sb_info
*sbi
)
1467 struct f2fs_sm_info
*sm_info
= SM_I(sbi
);
1468 struct free_segmap_info
*free_i
;
1469 unsigned int bitmap_size
, sec_bitmap_size
;
1471 /* allocate memory for free segmap information */
1472 free_i
= kzalloc(sizeof(struct free_segmap_info
), GFP_KERNEL
);
1476 SM_I(sbi
)->free_info
= free_i
;
1478 bitmap_size
= f2fs_bitmap_size(TOTAL_SEGS(sbi
));
1479 free_i
->free_segmap
= kmalloc(bitmap_size
, GFP_KERNEL
);
1480 if (!free_i
->free_segmap
)
1483 sec_bitmap_size
= f2fs_bitmap_size(TOTAL_SECS(sbi
));
1484 free_i
->free_secmap
= kmalloc(sec_bitmap_size
, GFP_KERNEL
);
1485 if (!free_i
->free_secmap
)
1488 /* set all segments as dirty temporarily */
1489 memset(free_i
->free_segmap
, 0xff, bitmap_size
);
1490 memset(free_i
->free_secmap
, 0xff, sec_bitmap_size
);
1492 /* init free segmap information */
1493 free_i
->start_segno
=
1494 (unsigned int) GET_SEGNO_FROM_SEG0(sbi
, sm_info
->main_blkaddr
);
1495 free_i
->free_segments
= 0;
1496 free_i
->free_sections
= 0;
1497 rwlock_init(&free_i
->segmap_lock
);
1501 static int build_curseg(struct f2fs_sb_info
*sbi
)
1503 struct curseg_info
*array
;
1506 array
= kzalloc(sizeof(*array
) * NR_CURSEG_TYPE
, GFP_KERNEL
);
1510 SM_I(sbi
)->curseg_array
= array
;
1512 for (i
= 0; i
< NR_CURSEG_TYPE
; i
++) {
1513 mutex_init(&array
[i
].curseg_mutex
);
1514 array
[i
].sum_blk
= kzalloc(PAGE_CACHE_SIZE
, GFP_KERNEL
);
1515 if (!array
[i
].sum_blk
)
1517 array
[i
].segno
= NULL_SEGNO
;
1518 array
[i
].next_blkoff
= 0;
1520 return restore_curseg_summaries(sbi
);
1523 static void build_sit_entries(struct f2fs_sb_info
*sbi
)
1525 struct sit_info
*sit_i
= SIT_I(sbi
);
1526 struct curseg_info
*curseg
= CURSEG_I(sbi
, CURSEG_COLD_DATA
);
1527 struct f2fs_summary_block
*sum
= curseg
->sum_blk
;
1530 for (start
= 0; start
< TOTAL_SEGS(sbi
); start
++) {
1531 struct seg_entry
*se
= &sit_i
->sentries
[start
];
1532 struct f2fs_sit_block
*sit_blk
;
1533 struct f2fs_sit_entry sit
;
1537 mutex_lock(&curseg
->curseg_mutex
);
1538 for (i
= 0; i
< sits_in_cursum(sum
); i
++) {
1539 if (le32_to_cpu(segno_in_journal(sum
, i
)) == start
) {
1540 sit
= sit_in_journal(sum
, i
);
1541 mutex_unlock(&curseg
->curseg_mutex
);
1545 mutex_unlock(&curseg
->curseg_mutex
);
1546 page
= get_current_sit_page(sbi
, start
);
1547 sit_blk
= (struct f2fs_sit_block
*)page_address(page
);
1548 sit
= sit_blk
->entries
[SIT_ENTRY_OFFSET(sit_i
, start
)];
1549 f2fs_put_page(page
, 1);
1551 check_block_count(sbi
, start
, &sit
);
1552 seg_info_from_raw_sit(se
, &sit
);
1553 if (sbi
->segs_per_sec
> 1) {
1554 struct sec_entry
*e
= get_sec_entry(sbi
, start
);
1555 e
->valid_blocks
+= se
->valid_blocks
;
1560 static void init_free_segmap(struct f2fs_sb_info
*sbi
)
1565 for (start
= 0; start
< TOTAL_SEGS(sbi
); start
++) {
1566 struct seg_entry
*sentry
= get_seg_entry(sbi
, start
);
1567 if (!sentry
->valid_blocks
)
1568 __set_free(sbi
, start
);
1571 /* set use the current segments */
1572 for (type
= CURSEG_HOT_DATA
; type
<= CURSEG_COLD_NODE
; type
++) {
1573 struct curseg_info
*curseg_t
= CURSEG_I(sbi
, type
);
1574 __set_test_and_inuse(sbi
, curseg_t
->segno
);
1578 static void init_dirty_segmap(struct f2fs_sb_info
*sbi
)
1580 struct dirty_seglist_info
*dirty_i
= DIRTY_I(sbi
);
1581 struct free_segmap_info
*free_i
= FREE_I(sbi
);
1582 unsigned int segno
= 0, offset
= 0;
1583 unsigned short valid_blocks
;
1585 while (segno
< TOTAL_SEGS(sbi
)) {
1586 /* find dirty segment based on free segmap */
1587 segno
= find_next_inuse(free_i
, TOTAL_SEGS(sbi
), offset
);
1588 if (segno
>= TOTAL_SEGS(sbi
))
1591 valid_blocks
= get_valid_blocks(sbi
, segno
, 0);
1592 if (valid_blocks
>= sbi
->blocks_per_seg
|| !valid_blocks
)
1594 mutex_lock(&dirty_i
->seglist_lock
);
1595 __locate_dirty_segment(sbi
, segno
, DIRTY
);
1596 mutex_unlock(&dirty_i
->seglist_lock
);
1600 static int init_victim_secmap(struct f2fs_sb_info
*sbi
)
1602 struct dirty_seglist_info
*dirty_i
= DIRTY_I(sbi
);
1603 unsigned int bitmap_size
= f2fs_bitmap_size(TOTAL_SECS(sbi
));
1605 dirty_i
->victim_secmap
= kzalloc(bitmap_size
, GFP_KERNEL
);
1606 if (!dirty_i
->victim_secmap
)
1611 static int build_dirty_segmap(struct f2fs_sb_info
*sbi
)
1613 struct dirty_seglist_info
*dirty_i
;
1614 unsigned int bitmap_size
, i
;
1616 /* allocate memory for dirty segments list information */
1617 dirty_i
= kzalloc(sizeof(struct dirty_seglist_info
), GFP_KERNEL
);
1621 SM_I(sbi
)->dirty_info
= dirty_i
;
1622 mutex_init(&dirty_i
->seglist_lock
);
1624 bitmap_size
= f2fs_bitmap_size(TOTAL_SEGS(sbi
));
1626 for (i
= 0; i
< NR_DIRTY_TYPE
; i
++) {
1627 dirty_i
->dirty_segmap
[i
] = kzalloc(bitmap_size
, GFP_KERNEL
);
1628 if (!dirty_i
->dirty_segmap
[i
])
1632 init_dirty_segmap(sbi
);
1633 return init_victim_secmap(sbi
);
1637 * Update min, max modified time for cost-benefit GC algorithm
1639 static void init_min_max_mtime(struct f2fs_sb_info
*sbi
)
1641 struct sit_info
*sit_i
= SIT_I(sbi
);
1644 mutex_lock(&sit_i
->sentry_lock
);
1646 sit_i
->min_mtime
= LLONG_MAX
;
1648 for (segno
= 0; segno
< TOTAL_SEGS(sbi
); segno
+= sbi
->segs_per_sec
) {
1650 unsigned long long mtime
= 0;
1652 for (i
= 0; i
< sbi
->segs_per_sec
; i
++)
1653 mtime
+= get_seg_entry(sbi
, segno
+ i
)->mtime
;
1655 mtime
= div_u64(mtime
, sbi
->segs_per_sec
);
1657 if (sit_i
->min_mtime
> mtime
)
1658 sit_i
->min_mtime
= mtime
;
1660 sit_i
->max_mtime
= get_mtime(sbi
);
1661 mutex_unlock(&sit_i
->sentry_lock
);
1664 int build_segment_manager(struct f2fs_sb_info
*sbi
)
1666 struct f2fs_super_block
*raw_super
= F2FS_RAW_SUPER(sbi
);
1667 struct f2fs_checkpoint
*ckpt
= F2FS_CKPT(sbi
);
1668 struct f2fs_sm_info
*sm_info
;
1671 sm_info
= kzalloc(sizeof(struct f2fs_sm_info
), GFP_KERNEL
);
1676 sbi
->sm_info
= sm_info
;
1677 INIT_LIST_HEAD(&sm_info
->wblist_head
);
1678 spin_lock_init(&sm_info
->wblist_lock
);
1679 sm_info
->seg0_blkaddr
= le32_to_cpu(raw_super
->segment0_blkaddr
);
1680 sm_info
->main_blkaddr
= le32_to_cpu(raw_super
->main_blkaddr
);
1681 sm_info
->segment_count
= le32_to_cpu(raw_super
->segment_count
);
1682 sm_info
->reserved_segments
= le32_to_cpu(ckpt
->rsvd_segment_count
);
1683 sm_info
->ovp_segments
= le32_to_cpu(ckpt
->overprov_segment_count
);
1684 sm_info
->main_segments
= le32_to_cpu(raw_super
->segment_count_main
);
1685 sm_info
->ssa_blkaddr
= le32_to_cpu(raw_super
->ssa_blkaddr
);
1687 err
= build_sit_info(sbi
);
1690 err
= build_free_segmap(sbi
);
1693 err
= build_curseg(sbi
);
1697 /* reinit free segmap based on SIT */
1698 build_sit_entries(sbi
);
1700 init_free_segmap(sbi
);
1701 err
= build_dirty_segmap(sbi
);
1705 init_min_max_mtime(sbi
);
1709 static void discard_dirty_segmap(struct f2fs_sb_info
*sbi
,
1710 enum dirty_type dirty_type
)
1712 struct dirty_seglist_info
*dirty_i
= DIRTY_I(sbi
);
1714 mutex_lock(&dirty_i
->seglist_lock
);
1715 kfree(dirty_i
->dirty_segmap
[dirty_type
]);
1716 dirty_i
->nr_dirty
[dirty_type
] = 0;
1717 mutex_unlock(&dirty_i
->seglist_lock
);
1720 static void destroy_victim_secmap(struct f2fs_sb_info
*sbi
)
1722 struct dirty_seglist_info
*dirty_i
= DIRTY_I(sbi
);
1723 kfree(dirty_i
->victim_secmap
);
1726 static void destroy_dirty_segmap(struct f2fs_sb_info
*sbi
)
1728 struct dirty_seglist_info
*dirty_i
= DIRTY_I(sbi
);
1734 /* discard pre-free/dirty segments list */
1735 for (i
= 0; i
< NR_DIRTY_TYPE
; i
++)
1736 discard_dirty_segmap(sbi
, i
);
1738 destroy_victim_secmap(sbi
);
1739 SM_I(sbi
)->dirty_info
= NULL
;
1743 static void destroy_curseg(struct f2fs_sb_info
*sbi
)
1745 struct curseg_info
*array
= SM_I(sbi
)->curseg_array
;
1750 SM_I(sbi
)->curseg_array
= NULL
;
1751 for (i
= 0; i
< NR_CURSEG_TYPE
; i
++)
1752 kfree(array
[i
].sum_blk
);
1756 static void destroy_free_segmap(struct f2fs_sb_info
*sbi
)
1758 struct free_segmap_info
*free_i
= SM_I(sbi
)->free_info
;
1761 SM_I(sbi
)->free_info
= NULL
;
1762 kfree(free_i
->free_segmap
);
1763 kfree(free_i
->free_secmap
);
1767 static void destroy_sit_info(struct f2fs_sb_info
*sbi
)
1769 struct sit_info
*sit_i
= SIT_I(sbi
);
1775 if (sit_i
->sentries
) {
1776 for (start
= 0; start
< TOTAL_SEGS(sbi
); start
++) {
1777 kfree(sit_i
->sentries
[start
].cur_valid_map
);
1778 kfree(sit_i
->sentries
[start
].ckpt_valid_map
);
1781 vfree(sit_i
->sentries
);
1782 vfree(sit_i
->sec_entries
);
1783 kfree(sit_i
->dirty_sentries_bitmap
);
1785 SM_I(sbi
)->sit_info
= NULL
;
1786 kfree(sit_i
->sit_bitmap
);
1790 void destroy_segment_manager(struct f2fs_sb_info
*sbi
)
1792 struct f2fs_sm_info
*sm_info
= SM_I(sbi
);
1793 destroy_dirty_segmap(sbi
);
1794 destroy_curseg(sbi
);
1795 destroy_free_segmap(sbi
);
1796 destroy_sit_info(sbi
);
1797 sbi
->sm_info
= NULL
;