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>
23 * This function balances dirty node and dentry pages.
24 * In addition, it controls garbage collection.
26 void f2fs_balance_fs(struct f2fs_sb_info
*sbi
)
29 * We should do GC or end up with checkpoint, if there are so many dirty
30 * dir/node pages without enough free segments.
32 if (has_not_enough_free_secs(sbi
, 0)) {
33 mutex_lock(&sbi
->gc_mutex
);
38 static void __locate_dirty_segment(struct f2fs_sb_info
*sbi
, unsigned int segno
,
39 enum dirty_type dirty_type
)
41 struct dirty_seglist_info
*dirty_i
= DIRTY_I(sbi
);
43 /* need not be added */
44 if (IS_CURSEG(sbi
, segno
))
47 if (!test_and_set_bit(segno
, dirty_i
->dirty_segmap
[dirty_type
]))
48 dirty_i
->nr_dirty
[dirty_type
]++;
50 if (dirty_type
== DIRTY
) {
51 struct seg_entry
*sentry
= get_seg_entry(sbi
, segno
);
52 dirty_type
= sentry
->type
;
53 if (!test_and_set_bit(segno
, dirty_i
->dirty_segmap
[dirty_type
]))
54 dirty_i
->nr_dirty
[dirty_type
]++;
58 static void __remove_dirty_segment(struct f2fs_sb_info
*sbi
, unsigned int segno
,
59 enum dirty_type dirty_type
)
61 struct dirty_seglist_info
*dirty_i
= DIRTY_I(sbi
);
63 if (test_and_clear_bit(segno
, dirty_i
->dirty_segmap
[dirty_type
]))
64 dirty_i
->nr_dirty
[dirty_type
]--;
66 if (dirty_type
== DIRTY
) {
67 struct seg_entry
*sentry
= get_seg_entry(sbi
, segno
);
68 dirty_type
= sentry
->type
;
69 if (test_and_clear_bit(segno
,
70 dirty_i
->dirty_segmap
[dirty_type
]))
71 dirty_i
->nr_dirty
[dirty_type
]--;
72 clear_bit(segno
, dirty_i
->victim_segmap
[FG_GC
]);
73 clear_bit(segno
, dirty_i
->victim_segmap
[BG_GC
]);
78 * Should not occur error such as -ENOMEM.
79 * Adding dirty entry into seglist is not critical operation.
80 * If a given segment is one of current working segments, it won't be added.
82 void locate_dirty_segment(struct f2fs_sb_info
*sbi
, unsigned int segno
)
84 struct dirty_seglist_info
*dirty_i
= DIRTY_I(sbi
);
85 unsigned short valid_blocks
;
87 if (segno
== NULL_SEGNO
|| IS_CURSEG(sbi
, segno
))
90 mutex_lock(&dirty_i
->seglist_lock
);
92 valid_blocks
= get_valid_blocks(sbi
, segno
, 0);
94 if (valid_blocks
== 0) {
95 __locate_dirty_segment(sbi
, segno
, PRE
);
96 __remove_dirty_segment(sbi
, segno
, DIRTY
);
97 } else if (valid_blocks
< sbi
->blocks_per_seg
) {
98 __locate_dirty_segment(sbi
, segno
, DIRTY
);
100 /* Recovery routine with SSR needs this */
101 __remove_dirty_segment(sbi
, segno
, DIRTY
);
104 mutex_unlock(&dirty_i
->seglist_lock
);
109 * Should call clear_prefree_segments after checkpoint is done.
111 static void set_prefree_as_free_segments(struct f2fs_sb_info
*sbi
)
113 struct dirty_seglist_info
*dirty_i
= DIRTY_I(sbi
);
114 unsigned int segno
, offset
= 0;
115 unsigned int total_segs
= TOTAL_SEGS(sbi
);
117 mutex_lock(&dirty_i
->seglist_lock
);
119 segno
= find_next_bit(dirty_i
->dirty_segmap
[PRE
], total_segs
,
121 if (segno
>= total_segs
)
123 __set_test_and_free(sbi
, segno
);
126 mutex_unlock(&dirty_i
->seglist_lock
);
129 void clear_prefree_segments(struct f2fs_sb_info
*sbi
)
131 struct dirty_seglist_info
*dirty_i
= DIRTY_I(sbi
);
132 unsigned int segno
, offset
= 0;
133 unsigned int total_segs
= TOTAL_SEGS(sbi
);
135 mutex_lock(&dirty_i
->seglist_lock
);
137 segno
= find_next_bit(dirty_i
->dirty_segmap
[PRE
], total_segs
,
139 if (segno
>= total_segs
)
143 if (test_and_clear_bit(segno
, dirty_i
->dirty_segmap
[PRE
]))
144 dirty_i
->nr_dirty
[PRE
]--;
147 if (test_opt(sbi
, DISCARD
))
148 blkdev_issue_discard(sbi
->sb
->s_bdev
,
149 START_BLOCK(sbi
, segno
) <<
150 sbi
->log_sectors_per_block
,
151 1 << (sbi
->log_sectors_per_block
+
152 sbi
->log_blocks_per_seg
),
155 mutex_unlock(&dirty_i
->seglist_lock
);
158 static void __mark_sit_entry_dirty(struct f2fs_sb_info
*sbi
, unsigned int segno
)
160 struct sit_info
*sit_i
= SIT_I(sbi
);
161 if (!__test_and_set_bit(segno
, sit_i
->dirty_sentries_bitmap
))
162 sit_i
->dirty_sentries
++;
165 static void __set_sit_entry_type(struct f2fs_sb_info
*sbi
, int type
,
166 unsigned int segno
, int modified
)
168 struct seg_entry
*se
= get_seg_entry(sbi
, segno
);
171 __mark_sit_entry_dirty(sbi
, segno
);
174 static void update_sit_entry(struct f2fs_sb_info
*sbi
, block_t blkaddr
, int del
)
176 struct seg_entry
*se
;
177 unsigned int segno
, offset
;
178 long int new_vblocks
;
180 segno
= GET_SEGNO(sbi
, blkaddr
);
182 se
= get_seg_entry(sbi
, segno
);
183 new_vblocks
= se
->valid_blocks
+ del
;
184 offset
= GET_SEGOFF_FROM_SEG0(sbi
, blkaddr
) & (sbi
->blocks_per_seg
- 1);
186 BUG_ON((new_vblocks
>> (sizeof(unsigned short) << 3) ||
187 (new_vblocks
> sbi
->blocks_per_seg
)));
189 se
->valid_blocks
= new_vblocks
;
190 se
->mtime
= get_mtime(sbi
);
191 SIT_I(sbi
)->max_mtime
= se
->mtime
;
193 /* Update valid block bitmap */
195 if (f2fs_set_bit(offset
, se
->cur_valid_map
))
198 if (!f2fs_clear_bit(offset
, se
->cur_valid_map
))
201 if (!f2fs_test_bit(offset
, se
->ckpt_valid_map
))
202 se
->ckpt_valid_blocks
+= del
;
204 __mark_sit_entry_dirty(sbi
, segno
);
206 /* update total number of valid blocks to be written in ckpt area */
207 SIT_I(sbi
)->written_valid_blocks
+= del
;
209 if (sbi
->segs_per_sec
> 1)
210 get_sec_entry(sbi
, segno
)->valid_blocks
+= del
;
213 static void refresh_sit_entry(struct f2fs_sb_info
*sbi
,
214 block_t old_blkaddr
, block_t new_blkaddr
)
216 update_sit_entry(sbi
, new_blkaddr
, 1);
217 if (GET_SEGNO(sbi
, old_blkaddr
) != NULL_SEGNO
)
218 update_sit_entry(sbi
, old_blkaddr
, -1);
221 void invalidate_blocks(struct f2fs_sb_info
*sbi
, block_t addr
)
223 unsigned int segno
= GET_SEGNO(sbi
, addr
);
224 struct sit_info
*sit_i
= SIT_I(sbi
);
226 BUG_ON(addr
== NULL_ADDR
);
227 if (addr
== NEW_ADDR
)
230 /* add it into sit main buffer */
231 mutex_lock(&sit_i
->sentry_lock
);
233 update_sit_entry(sbi
, addr
, -1);
235 /* add it into dirty seglist */
236 locate_dirty_segment(sbi
, segno
);
238 mutex_unlock(&sit_i
->sentry_lock
);
242 * This function should be resided under the curseg_mutex lock
244 static void __add_sum_entry(struct f2fs_sb_info
*sbi
, int type
,
245 struct f2fs_summary
*sum
, unsigned short offset
)
247 struct curseg_info
*curseg
= CURSEG_I(sbi
, type
);
248 void *addr
= curseg
->sum_blk
;
249 addr
+= offset
* sizeof(struct f2fs_summary
);
250 memcpy(addr
, sum
, sizeof(struct f2fs_summary
));
255 * Calculate the number of current summary pages for writing
257 int npages_for_summary_flush(struct f2fs_sb_info
*sbi
)
259 int total_size_bytes
= 0;
260 int valid_sum_count
= 0;
263 for (i
= CURSEG_HOT_DATA
; i
<= CURSEG_COLD_DATA
; i
++) {
264 if (sbi
->ckpt
->alloc_type
[i
] == SSR
)
265 valid_sum_count
+= sbi
->blocks_per_seg
;
267 valid_sum_count
+= curseg_blkoff(sbi
, i
);
270 total_size_bytes
= valid_sum_count
* (SUMMARY_SIZE
+ 1)
271 + sizeof(struct nat_journal
) + 2
272 + sizeof(struct sit_journal
) + 2;
273 sum_space
= PAGE_CACHE_SIZE
- SUM_FOOTER_SIZE
;
274 if (total_size_bytes
< sum_space
)
276 else if (total_size_bytes
< 2 * sum_space
)
282 * Caller should put this summary page
284 struct page
*get_sum_page(struct f2fs_sb_info
*sbi
, unsigned int segno
)
286 return get_meta_page(sbi
, GET_SUM_BLOCK(sbi
, segno
));
289 static void write_sum_page(struct f2fs_sb_info
*sbi
,
290 struct f2fs_summary_block
*sum_blk
, block_t blk_addr
)
292 struct page
*page
= grab_meta_page(sbi
, blk_addr
);
293 void *kaddr
= page_address(page
);
294 memcpy(kaddr
, sum_blk
, PAGE_CACHE_SIZE
);
295 set_page_dirty(page
);
296 f2fs_put_page(page
, 1);
299 static unsigned int check_prefree_segments(struct f2fs_sb_info
*sbi
,
300 int ofs_unit
, int type
)
302 struct dirty_seglist_info
*dirty_i
= DIRTY_I(sbi
);
303 unsigned long *prefree_segmap
= dirty_i
->dirty_segmap
[PRE
];
304 unsigned int segno
, next_segno
, i
;
308 * If there is not enough reserved sections,
309 * we should not reuse prefree segments.
311 if (has_not_enough_free_secs(sbi
, 0))
315 * NODE page should not reuse prefree segment,
316 * since those information is used for SPOR.
318 if (IS_NODESEG(type
))
321 segno
= find_next_bit(prefree_segmap
, TOTAL_SEGS(sbi
), ofs
++);
322 ofs
= ((segno
/ ofs_unit
) * ofs_unit
) + ofs_unit
;
323 if (segno
< TOTAL_SEGS(sbi
)) {
324 /* skip intermediate segments in a section */
325 if (segno
% ofs_unit
)
328 /* skip if whole section is not prefree */
329 next_segno
= find_next_zero_bit(prefree_segmap
,
330 TOTAL_SEGS(sbi
), segno
+ 1);
331 if (next_segno
- segno
< ofs_unit
)
334 /* skip if whole section was not free at the last checkpoint */
335 for (i
= 0; i
< ofs_unit
; i
++)
336 if (get_seg_entry(sbi
, segno
)->ckpt_valid_blocks
)
344 * Find a new segment from the free segments bitmap to right order
345 * This function should be returned with success, otherwise BUG
347 static void get_new_segment(struct f2fs_sb_info
*sbi
,
348 unsigned int *newseg
, bool new_sec
, int dir
)
350 struct free_segmap_info
*free_i
= FREE_I(sbi
);
351 unsigned int total_secs
= sbi
->total_sections
;
352 unsigned int segno
, secno
, zoneno
;
353 unsigned int total_zones
= sbi
->total_sections
/ sbi
->secs_per_zone
;
354 unsigned int hint
= *newseg
/ sbi
->segs_per_sec
;
355 unsigned int old_zoneno
= GET_ZONENO_FROM_SEGNO(sbi
, *newseg
);
356 unsigned int left_start
= hint
;
361 write_lock(&free_i
->segmap_lock
);
363 if (!new_sec
&& ((*newseg
+ 1) % sbi
->segs_per_sec
)) {
364 segno
= find_next_zero_bit(free_i
->free_segmap
,
365 TOTAL_SEGS(sbi
), *newseg
+ 1);
366 if (segno
< TOTAL_SEGS(sbi
))
370 secno
= find_next_zero_bit(free_i
->free_secmap
, total_secs
, hint
);
371 if (secno
>= total_secs
) {
372 if (dir
== ALLOC_RIGHT
) {
373 secno
= find_next_zero_bit(free_i
->free_secmap
,
375 BUG_ON(secno
>= total_secs
);
378 left_start
= hint
- 1;
384 while (test_bit(left_start
, free_i
->free_secmap
)) {
385 if (left_start
> 0) {
389 left_start
= find_next_zero_bit(free_i
->free_secmap
,
391 BUG_ON(left_start
>= total_secs
);
397 segno
= secno
* sbi
->segs_per_sec
;
398 zoneno
= secno
/ sbi
->secs_per_zone
;
400 /* give up on finding another zone */
403 if (sbi
->secs_per_zone
== 1)
405 if (zoneno
== old_zoneno
)
407 if (dir
== ALLOC_LEFT
) {
408 if (!go_left
&& zoneno
+ 1 >= total_zones
)
410 if (go_left
&& zoneno
== 0)
413 for (i
= 0; i
< NR_CURSEG_TYPE
; i
++)
414 if (CURSEG_I(sbi
, i
)->zone
== zoneno
)
417 if (i
< NR_CURSEG_TYPE
) {
418 /* zone is in user, try another */
420 hint
= zoneno
* sbi
->secs_per_zone
- 1;
421 else if (zoneno
+ 1 >= total_zones
)
424 hint
= (zoneno
+ 1) * sbi
->secs_per_zone
;
426 goto find_other_zone
;
429 /* set it as dirty segment in free segmap */
430 BUG_ON(test_bit(segno
, free_i
->free_segmap
));
431 __set_inuse(sbi
, segno
);
433 write_unlock(&free_i
->segmap_lock
);
436 static void reset_curseg(struct f2fs_sb_info
*sbi
, int type
, int modified
)
438 struct curseg_info
*curseg
= CURSEG_I(sbi
, type
);
439 struct summary_footer
*sum_footer
;
441 curseg
->segno
= curseg
->next_segno
;
442 curseg
->zone
= GET_ZONENO_FROM_SEGNO(sbi
, curseg
->segno
);
443 curseg
->next_blkoff
= 0;
444 curseg
->next_segno
= NULL_SEGNO
;
446 sum_footer
= &(curseg
->sum_blk
->footer
);
447 memset(sum_footer
, 0, sizeof(struct summary_footer
));
448 if (IS_DATASEG(type
))
449 SET_SUM_TYPE(sum_footer
, SUM_TYPE_DATA
);
450 if (IS_NODESEG(type
))
451 SET_SUM_TYPE(sum_footer
, SUM_TYPE_NODE
);
452 __set_sit_entry_type(sbi
, type
, curseg
->segno
, modified
);
456 * Allocate a current working segment.
457 * This function always allocates a free segment in LFS manner.
459 static void new_curseg(struct f2fs_sb_info
*sbi
, int type
, bool new_sec
)
461 struct curseg_info
*curseg
= CURSEG_I(sbi
, type
);
462 unsigned int segno
= curseg
->segno
;
463 int dir
= ALLOC_LEFT
;
465 write_sum_page(sbi
, curseg
->sum_blk
,
466 GET_SUM_BLOCK(sbi
, curseg
->segno
));
467 if (type
== CURSEG_WARM_DATA
|| type
== CURSEG_COLD_DATA
)
470 if (test_opt(sbi
, NOHEAP
))
473 get_new_segment(sbi
, &segno
, new_sec
, dir
);
474 curseg
->next_segno
= segno
;
475 reset_curseg(sbi
, type
, 1);
476 curseg
->alloc_type
= LFS
;
479 static void __next_free_blkoff(struct f2fs_sb_info
*sbi
,
480 struct curseg_info
*seg
, block_t start
)
482 struct seg_entry
*se
= get_seg_entry(sbi
, seg
->segno
);
484 for (ofs
= start
; ofs
< sbi
->blocks_per_seg
; ofs
++) {
485 if (!f2fs_test_bit(ofs
, se
->ckpt_valid_map
)
486 && !f2fs_test_bit(ofs
, se
->cur_valid_map
))
489 seg
->next_blkoff
= ofs
;
493 * If a segment is written by LFS manner, next block offset is just obtained
494 * by increasing the current block offset. However, if a segment is written by
495 * SSR manner, next block offset obtained by calling __next_free_blkoff
497 static void __refresh_next_blkoff(struct f2fs_sb_info
*sbi
,
498 struct curseg_info
*seg
)
500 if (seg
->alloc_type
== SSR
)
501 __next_free_blkoff(sbi
, seg
, seg
->next_blkoff
+ 1);
507 * This function always allocates a used segment (from dirty seglist) by SSR
508 * manner, so it should recover the existing segment information of valid blocks
510 static void change_curseg(struct f2fs_sb_info
*sbi
, int type
, bool reuse
)
512 struct dirty_seglist_info
*dirty_i
= DIRTY_I(sbi
);
513 struct curseg_info
*curseg
= CURSEG_I(sbi
, type
);
514 unsigned int new_segno
= curseg
->next_segno
;
515 struct f2fs_summary_block
*sum_node
;
516 struct page
*sum_page
;
518 write_sum_page(sbi
, curseg
->sum_blk
,
519 GET_SUM_BLOCK(sbi
, curseg
->segno
));
520 __set_test_and_inuse(sbi
, new_segno
);
522 mutex_lock(&dirty_i
->seglist_lock
);
523 __remove_dirty_segment(sbi
, new_segno
, PRE
);
524 __remove_dirty_segment(sbi
, new_segno
, DIRTY
);
525 mutex_unlock(&dirty_i
->seglist_lock
);
527 reset_curseg(sbi
, type
, 1);
528 curseg
->alloc_type
= SSR
;
529 __next_free_blkoff(sbi
, curseg
, 0);
532 sum_page
= get_sum_page(sbi
, new_segno
);
533 sum_node
= (struct f2fs_summary_block
*)page_address(sum_page
);
534 memcpy(curseg
->sum_blk
, sum_node
, SUM_ENTRY_SIZE
);
535 f2fs_put_page(sum_page
, 1);
539 static int get_ssr_segment(struct f2fs_sb_info
*sbi
, int type
)
541 struct curseg_info
*curseg
= CURSEG_I(sbi
, type
);
542 const struct victim_selection
*v_ops
= DIRTY_I(sbi
)->v_ops
;
544 if (IS_NODESEG(type
) || !has_not_enough_free_secs(sbi
, 0))
545 return v_ops
->get_victim(sbi
,
546 &(curseg
)->next_segno
, BG_GC
, type
, SSR
);
548 /* For data segments, let's do SSR more intensively */
549 for (; type
>= CURSEG_HOT_DATA
; type
--)
550 if (v_ops
->get_victim(sbi
, &(curseg
)->next_segno
,
557 * flush out current segment and replace it with new segment
558 * This function should be returned with success, otherwise BUG
560 static void allocate_segment_by_default(struct f2fs_sb_info
*sbi
,
561 int type
, bool force
)
563 struct curseg_info
*curseg
= CURSEG_I(sbi
, type
);
564 unsigned int ofs_unit
;
567 new_curseg(sbi
, type
, true);
571 ofs_unit
= need_SSR(sbi
) ? 1 : sbi
->segs_per_sec
;
572 curseg
->next_segno
= check_prefree_segments(sbi
, ofs_unit
, type
);
574 if (curseg
->next_segno
!= NULL_SEGNO
)
575 change_curseg(sbi
, type
, false);
576 else if (type
== CURSEG_WARM_NODE
)
577 new_curseg(sbi
, type
, false);
578 else if (need_SSR(sbi
) && get_ssr_segment(sbi
, type
))
579 change_curseg(sbi
, type
, true);
581 new_curseg(sbi
, type
, false);
583 sbi
->segment_count
[curseg
->alloc_type
]++;
586 void allocate_new_segments(struct f2fs_sb_info
*sbi
)
588 struct curseg_info
*curseg
;
589 unsigned int old_curseg
;
592 for (i
= CURSEG_HOT_DATA
; i
<= CURSEG_COLD_DATA
; i
++) {
593 curseg
= CURSEG_I(sbi
, i
);
594 old_curseg
= curseg
->segno
;
595 SIT_I(sbi
)->s_ops
->allocate_segment(sbi
, i
, true);
596 locate_dirty_segment(sbi
, old_curseg
);
600 static const struct segment_allocation default_salloc_ops
= {
601 .allocate_segment
= allocate_segment_by_default
,
604 static void f2fs_end_io_write(struct bio
*bio
, int err
)
606 const int uptodate
= test_bit(BIO_UPTODATE
, &bio
->bi_flags
);
607 struct bio_vec
*bvec
= bio
->bi_io_vec
+ bio
->bi_vcnt
- 1;
608 struct bio_private
*p
= bio
->bi_private
;
611 struct page
*page
= bvec
->bv_page
;
613 if (--bvec
>= bio
->bi_io_vec
)
614 prefetchw(&bvec
->bv_page
->flags
);
618 set_bit(AS_EIO
, &page
->mapping
->flags
);
619 set_ckpt_flags(p
->sbi
->ckpt
, CP_ERROR_FLAG
);
620 p
->sbi
->sb
->s_flags
|= MS_RDONLY
;
622 end_page_writeback(page
);
623 dec_page_count(p
->sbi
, F2FS_WRITEBACK
);
624 } while (bvec
>= bio
->bi_io_vec
);
632 struct bio
*f2fs_bio_alloc(struct block_device
*bdev
, int npages
)
635 struct bio_private
*priv
;
637 priv
= kmalloc(sizeof(struct bio_private
), GFP_NOFS
);
643 /* No failure on bio allocation */
644 bio
= bio_alloc(GFP_NOIO
, npages
);
646 bio
->bi_private
= priv
;
650 static void do_submit_bio(struct f2fs_sb_info
*sbi
,
651 enum page_type type
, bool sync
)
653 int rw
= sync
? WRITE_SYNC
: WRITE
;
654 enum page_type btype
= type
> META
? META
: type
;
656 if (type
>= META_FLUSH
)
657 rw
= WRITE_FLUSH_FUA
;
659 if (sbi
->bio
[btype
]) {
660 struct bio_private
*p
= sbi
->bio
[btype
]->bi_private
;
662 sbi
->bio
[btype
]->bi_end_io
= f2fs_end_io_write
;
663 if (type
== META_FLUSH
) {
664 DECLARE_COMPLETION_ONSTACK(wait
);
667 submit_bio(rw
, sbi
->bio
[btype
]);
668 wait_for_completion(&wait
);
671 submit_bio(rw
, sbi
->bio
[btype
]);
673 sbi
->bio
[btype
] = NULL
;
677 void f2fs_submit_bio(struct f2fs_sb_info
*sbi
, enum page_type type
, bool sync
)
679 down_write(&sbi
->bio_sem
);
680 do_submit_bio(sbi
, type
, sync
);
681 up_write(&sbi
->bio_sem
);
684 static void submit_write_page(struct f2fs_sb_info
*sbi
, struct page
*page
,
685 block_t blk_addr
, enum page_type type
)
687 struct block_device
*bdev
= sbi
->sb
->s_bdev
;
689 verify_block_addr(sbi
, blk_addr
);
691 down_write(&sbi
->bio_sem
);
693 inc_page_count(sbi
, F2FS_WRITEBACK
);
695 if (sbi
->bio
[type
] && sbi
->last_block_in_bio
[type
] != blk_addr
- 1)
696 do_submit_bio(sbi
, type
, false);
698 if (sbi
->bio
[type
] == NULL
) {
699 sbi
->bio
[type
] = f2fs_bio_alloc(bdev
, bio_get_nr_vecs(bdev
));
700 sbi
->bio
[type
]->bi_sector
= SECTOR_FROM_BLOCK(sbi
, blk_addr
);
702 * The end_io will be assigned at the sumbission phase.
703 * Until then, let bio_add_page() merge consecutive IOs as much
708 if (bio_add_page(sbi
->bio
[type
], page
, PAGE_CACHE_SIZE
, 0) <
710 do_submit_bio(sbi
, type
, false);
714 sbi
->last_block_in_bio
[type
] = blk_addr
;
716 up_write(&sbi
->bio_sem
);
719 static bool __has_curseg_space(struct f2fs_sb_info
*sbi
, int type
)
721 struct curseg_info
*curseg
= CURSEG_I(sbi
, type
);
722 if (curseg
->next_blkoff
< sbi
->blocks_per_seg
)
727 static int __get_segment_type_2(struct page
*page
, enum page_type p_type
)
730 return CURSEG_HOT_DATA
;
732 return CURSEG_HOT_NODE
;
735 static int __get_segment_type_4(struct page
*page
, enum page_type p_type
)
737 if (p_type
== DATA
) {
738 struct inode
*inode
= page
->mapping
->host
;
740 if (S_ISDIR(inode
->i_mode
))
741 return CURSEG_HOT_DATA
;
743 return CURSEG_COLD_DATA
;
745 if (IS_DNODE(page
) && !is_cold_node(page
))
746 return CURSEG_HOT_NODE
;
748 return CURSEG_COLD_NODE
;
752 static int __get_segment_type_6(struct page
*page
, enum page_type p_type
)
754 if (p_type
== DATA
) {
755 struct inode
*inode
= page
->mapping
->host
;
757 if (S_ISDIR(inode
->i_mode
))
758 return CURSEG_HOT_DATA
;
759 else if (is_cold_data(page
) || is_cold_file(inode
))
760 return CURSEG_COLD_DATA
;
762 return CURSEG_WARM_DATA
;
765 return is_cold_node(page
) ? CURSEG_WARM_NODE
:
768 return CURSEG_COLD_NODE
;
772 static int __get_segment_type(struct page
*page
, enum page_type p_type
)
774 struct f2fs_sb_info
*sbi
= F2FS_SB(page
->mapping
->host
->i_sb
);
775 switch (sbi
->active_logs
) {
777 return __get_segment_type_2(page
, p_type
);
779 return __get_segment_type_4(page
, p_type
);
781 /* NR_CURSEG_TYPE(6) logs by default */
782 BUG_ON(sbi
->active_logs
!= NR_CURSEG_TYPE
);
783 return __get_segment_type_6(page
, p_type
);
786 static void do_write_page(struct f2fs_sb_info
*sbi
, struct page
*page
,
787 block_t old_blkaddr
, block_t
*new_blkaddr
,
788 struct f2fs_summary
*sum
, enum page_type p_type
)
790 struct sit_info
*sit_i
= SIT_I(sbi
);
791 struct curseg_info
*curseg
;
792 unsigned int old_cursegno
;
795 type
= __get_segment_type(page
, p_type
);
796 curseg
= CURSEG_I(sbi
, type
);
798 mutex_lock(&curseg
->curseg_mutex
);
800 *new_blkaddr
= NEXT_FREE_BLKADDR(sbi
, curseg
);
801 old_cursegno
= curseg
->segno
;
804 * __add_sum_entry should be resided under the curseg_mutex
805 * because, this function updates a summary entry in the
806 * current summary block.
808 __add_sum_entry(sbi
, type
, sum
, curseg
->next_blkoff
);
810 mutex_lock(&sit_i
->sentry_lock
);
811 __refresh_next_blkoff(sbi
, curseg
);
812 sbi
->block_count
[curseg
->alloc_type
]++;
815 * SIT information should be updated before segment allocation,
816 * since SSR needs latest valid block information.
818 refresh_sit_entry(sbi
, old_blkaddr
, *new_blkaddr
);
820 if (!__has_curseg_space(sbi
, type
))
821 sit_i
->s_ops
->allocate_segment(sbi
, type
, false);
823 locate_dirty_segment(sbi
, old_cursegno
);
824 locate_dirty_segment(sbi
, GET_SEGNO(sbi
, old_blkaddr
));
825 mutex_unlock(&sit_i
->sentry_lock
);
828 fill_node_footer_blkaddr(page
, NEXT_FREE_BLKADDR(sbi
, curseg
));
830 /* writeout dirty page into bdev */
831 submit_write_page(sbi
, page
, *new_blkaddr
, p_type
);
833 mutex_unlock(&curseg
->curseg_mutex
);
836 void write_meta_page(struct f2fs_sb_info
*sbi
, struct page
*page
)
838 set_page_writeback(page
);
839 submit_write_page(sbi
, page
, page
->index
, META
);
842 void write_node_page(struct f2fs_sb_info
*sbi
, struct page
*page
,
843 unsigned int nid
, block_t old_blkaddr
, block_t
*new_blkaddr
)
845 struct f2fs_summary sum
;
846 set_summary(&sum
, nid
, 0, 0);
847 do_write_page(sbi
, page
, old_blkaddr
, new_blkaddr
, &sum
, NODE
);
850 void write_data_page(struct inode
*inode
, struct page
*page
,
851 struct dnode_of_data
*dn
, block_t old_blkaddr
,
852 block_t
*new_blkaddr
)
854 struct f2fs_sb_info
*sbi
= F2FS_SB(inode
->i_sb
);
855 struct f2fs_summary sum
;
858 BUG_ON(old_blkaddr
== NULL_ADDR
);
859 get_node_info(sbi
, dn
->nid
, &ni
);
860 set_summary(&sum
, dn
->nid
, dn
->ofs_in_node
, ni
.version
);
862 do_write_page(sbi
, page
, old_blkaddr
,
863 new_blkaddr
, &sum
, DATA
);
866 void rewrite_data_page(struct f2fs_sb_info
*sbi
, struct page
*page
,
867 block_t old_blk_addr
)
869 submit_write_page(sbi
, page
, old_blk_addr
, DATA
);
872 void recover_data_page(struct f2fs_sb_info
*sbi
,
873 struct page
*page
, struct f2fs_summary
*sum
,
874 block_t old_blkaddr
, block_t new_blkaddr
)
876 struct sit_info
*sit_i
= SIT_I(sbi
);
877 struct curseg_info
*curseg
;
878 unsigned int segno
, old_cursegno
;
879 struct seg_entry
*se
;
882 segno
= GET_SEGNO(sbi
, new_blkaddr
);
883 se
= get_seg_entry(sbi
, segno
);
886 if (se
->valid_blocks
== 0 && !IS_CURSEG(sbi
, segno
)) {
887 if (old_blkaddr
== NULL_ADDR
)
888 type
= CURSEG_COLD_DATA
;
890 type
= CURSEG_WARM_DATA
;
892 curseg
= CURSEG_I(sbi
, type
);
894 mutex_lock(&curseg
->curseg_mutex
);
895 mutex_lock(&sit_i
->sentry_lock
);
897 old_cursegno
= curseg
->segno
;
899 /* change the current segment */
900 if (segno
!= curseg
->segno
) {
901 curseg
->next_segno
= segno
;
902 change_curseg(sbi
, type
, true);
905 curseg
->next_blkoff
= GET_SEGOFF_FROM_SEG0(sbi
, new_blkaddr
) &
906 (sbi
->blocks_per_seg
- 1);
907 __add_sum_entry(sbi
, type
, sum
, curseg
->next_blkoff
);
909 refresh_sit_entry(sbi
, old_blkaddr
, new_blkaddr
);
911 locate_dirty_segment(sbi
, old_cursegno
);
912 locate_dirty_segment(sbi
, GET_SEGNO(sbi
, old_blkaddr
));
914 mutex_unlock(&sit_i
->sentry_lock
);
915 mutex_unlock(&curseg
->curseg_mutex
);
918 void rewrite_node_page(struct f2fs_sb_info
*sbi
,
919 struct page
*page
, struct f2fs_summary
*sum
,
920 block_t old_blkaddr
, block_t new_blkaddr
)
922 struct sit_info
*sit_i
= SIT_I(sbi
);
923 int type
= CURSEG_WARM_NODE
;
924 struct curseg_info
*curseg
;
925 unsigned int segno
, old_cursegno
;
926 block_t next_blkaddr
= next_blkaddr_of_node(page
);
927 unsigned int next_segno
= GET_SEGNO(sbi
, next_blkaddr
);
929 curseg
= CURSEG_I(sbi
, type
);
931 mutex_lock(&curseg
->curseg_mutex
);
932 mutex_lock(&sit_i
->sentry_lock
);
934 segno
= GET_SEGNO(sbi
, new_blkaddr
);
935 old_cursegno
= curseg
->segno
;
937 /* change the current segment */
938 if (segno
!= curseg
->segno
) {
939 curseg
->next_segno
= segno
;
940 change_curseg(sbi
, type
, true);
942 curseg
->next_blkoff
= GET_SEGOFF_FROM_SEG0(sbi
, new_blkaddr
) &
943 (sbi
->blocks_per_seg
- 1);
944 __add_sum_entry(sbi
, type
, sum
, curseg
->next_blkoff
);
946 /* change the current log to the next block addr in advance */
947 if (next_segno
!= segno
) {
948 curseg
->next_segno
= next_segno
;
949 change_curseg(sbi
, type
, true);
951 curseg
->next_blkoff
= GET_SEGOFF_FROM_SEG0(sbi
, next_blkaddr
) &
952 (sbi
->blocks_per_seg
- 1);
954 /* rewrite node page */
955 set_page_writeback(page
);
956 submit_write_page(sbi
, page
, new_blkaddr
, NODE
);
957 f2fs_submit_bio(sbi
, NODE
, true);
958 refresh_sit_entry(sbi
, old_blkaddr
, new_blkaddr
);
960 locate_dirty_segment(sbi
, old_cursegno
);
961 locate_dirty_segment(sbi
, GET_SEGNO(sbi
, old_blkaddr
));
963 mutex_unlock(&sit_i
->sentry_lock
);
964 mutex_unlock(&curseg
->curseg_mutex
);
967 static int read_compacted_summaries(struct f2fs_sb_info
*sbi
)
969 struct f2fs_checkpoint
*ckpt
= F2FS_CKPT(sbi
);
970 struct curseg_info
*seg_i
;
971 unsigned char *kaddr
;
976 start
= start_sum_block(sbi
);
978 page
= get_meta_page(sbi
, start
++);
979 kaddr
= (unsigned char *)page_address(page
);
981 /* Step 1: restore nat cache */
982 seg_i
= CURSEG_I(sbi
, CURSEG_HOT_DATA
);
983 memcpy(&seg_i
->sum_blk
->n_nats
, kaddr
, SUM_JOURNAL_SIZE
);
985 /* Step 2: restore sit cache */
986 seg_i
= CURSEG_I(sbi
, CURSEG_COLD_DATA
);
987 memcpy(&seg_i
->sum_blk
->n_sits
, kaddr
+ SUM_JOURNAL_SIZE
,
989 offset
= 2 * SUM_JOURNAL_SIZE
;
991 /* Step 3: restore summary entries */
992 for (i
= CURSEG_HOT_DATA
; i
<= CURSEG_COLD_DATA
; i
++) {
993 unsigned short blk_off
;
996 seg_i
= CURSEG_I(sbi
, i
);
997 segno
= le32_to_cpu(ckpt
->cur_data_segno
[i
]);
998 blk_off
= le16_to_cpu(ckpt
->cur_data_blkoff
[i
]);
999 seg_i
->next_segno
= segno
;
1000 reset_curseg(sbi
, i
, 0);
1001 seg_i
->alloc_type
= ckpt
->alloc_type
[i
];
1002 seg_i
->next_blkoff
= blk_off
;
1004 if (seg_i
->alloc_type
== SSR
)
1005 blk_off
= sbi
->blocks_per_seg
;
1007 for (j
= 0; j
< blk_off
; j
++) {
1008 struct f2fs_summary
*s
;
1009 s
= (struct f2fs_summary
*)(kaddr
+ offset
);
1010 seg_i
->sum_blk
->entries
[j
] = *s
;
1011 offset
+= SUMMARY_SIZE
;
1012 if (offset
+ SUMMARY_SIZE
<= PAGE_CACHE_SIZE
-
1016 f2fs_put_page(page
, 1);
1019 page
= get_meta_page(sbi
, start
++);
1020 kaddr
= (unsigned char *)page_address(page
);
1024 f2fs_put_page(page
, 1);
1028 static int read_normal_summaries(struct f2fs_sb_info
*sbi
, int type
)
1030 struct f2fs_checkpoint
*ckpt
= F2FS_CKPT(sbi
);
1031 struct f2fs_summary_block
*sum
;
1032 struct curseg_info
*curseg
;
1034 unsigned short blk_off
;
1035 unsigned int segno
= 0;
1036 block_t blk_addr
= 0;
1038 /* get segment number and block addr */
1039 if (IS_DATASEG(type
)) {
1040 segno
= le32_to_cpu(ckpt
->cur_data_segno
[type
]);
1041 blk_off
= le16_to_cpu(ckpt
->cur_data_blkoff
[type
-
1043 if (is_set_ckpt_flags(ckpt
, CP_UMOUNT_FLAG
))
1044 blk_addr
= sum_blk_addr(sbi
, NR_CURSEG_TYPE
, type
);
1046 blk_addr
= sum_blk_addr(sbi
, NR_CURSEG_DATA_TYPE
, type
);
1048 segno
= le32_to_cpu(ckpt
->cur_node_segno
[type
-
1050 blk_off
= le16_to_cpu(ckpt
->cur_node_blkoff
[type
-
1052 if (is_set_ckpt_flags(ckpt
, CP_UMOUNT_FLAG
))
1053 blk_addr
= sum_blk_addr(sbi
, NR_CURSEG_NODE_TYPE
,
1054 type
- CURSEG_HOT_NODE
);
1056 blk_addr
= GET_SUM_BLOCK(sbi
, segno
);
1059 new = get_meta_page(sbi
, blk_addr
);
1060 sum
= (struct f2fs_summary_block
*)page_address(new);
1062 if (IS_NODESEG(type
)) {
1063 if (is_set_ckpt_flags(ckpt
, CP_UMOUNT_FLAG
)) {
1064 struct f2fs_summary
*ns
= &sum
->entries
[0];
1066 for (i
= 0; i
< sbi
->blocks_per_seg
; i
++, ns
++) {
1068 ns
->ofs_in_node
= 0;
1071 if (restore_node_summary(sbi
, segno
, sum
)) {
1072 f2fs_put_page(new, 1);
1078 /* set uncompleted segment to curseg */
1079 curseg
= CURSEG_I(sbi
, type
);
1080 mutex_lock(&curseg
->curseg_mutex
);
1081 memcpy(curseg
->sum_blk
, sum
, PAGE_CACHE_SIZE
);
1082 curseg
->next_segno
= segno
;
1083 reset_curseg(sbi
, type
, 0);
1084 curseg
->alloc_type
= ckpt
->alloc_type
[type
];
1085 curseg
->next_blkoff
= blk_off
;
1086 mutex_unlock(&curseg
->curseg_mutex
);
1087 f2fs_put_page(new, 1);
1091 static int restore_curseg_summaries(struct f2fs_sb_info
*sbi
)
1093 int type
= CURSEG_HOT_DATA
;
1095 if (is_set_ckpt_flags(F2FS_CKPT(sbi
), CP_COMPACT_SUM_FLAG
)) {
1096 /* restore for compacted data summary */
1097 if (read_compacted_summaries(sbi
))
1099 type
= CURSEG_HOT_NODE
;
1102 for (; type
<= CURSEG_COLD_NODE
; type
++)
1103 if (read_normal_summaries(sbi
, type
))
1108 static void write_compacted_summaries(struct f2fs_sb_info
*sbi
, block_t blkaddr
)
1111 unsigned char *kaddr
;
1112 struct f2fs_summary
*summary
;
1113 struct curseg_info
*seg_i
;
1114 int written_size
= 0;
1117 page
= grab_meta_page(sbi
, blkaddr
++);
1118 kaddr
= (unsigned char *)page_address(page
);
1120 /* Step 1: write nat cache */
1121 seg_i
= CURSEG_I(sbi
, CURSEG_HOT_DATA
);
1122 memcpy(kaddr
, &seg_i
->sum_blk
->n_nats
, SUM_JOURNAL_SIZE
);
1123 written_size
+= SUM_JOURNAL_SIZE
;
1125 /* Step 2: write sit cache */
1126 seg_i
= CURSEG_I(sbi
, CURSEG_COLD_DATA
);
1127 memcpy(kaddr
+ written_size
, &seg_i
->sum_blk
->n_sits
,
1129 written_size
+= SUM_JOURNAL_SIZE
;
1131 set_page_dirty(page
);
1133 /* Step 3: write summary entries */
1134 for (i
= CURSEG_HOT_DATA
; i
<= CURSEG_COLD_DATA
; i
++) {
1135 unsigned short blkoff
;
1136 seg_i
= CURSEG_I(sbi
, i
);
1137 if (sbi
->ckpt
->alloc_type
[i
] == SSR
)
1138 blkoff
= sbi
->blocks_per_seg
;
1140 blkoff
= curseg_blkoff(sbi
, i
);
1142 for (j
= 0; j
< blkoff
; j
++) {
1144 page
= grab_meta_page(sbi
, blkaddr
++);
1145 kaddr
= (unsigned char *)page_address(page
);
1148 summary
= (struct f2fs_summary
*)(kaddr
+ written_size
);
1149 *summary
= seg_i
->sum_blk
->entries
[j
];
1150 written_size
+= SUMMARY_SIZE
;
1151 set_page_dirty(page
);
1153 if (written_size
+ SUMMARY_SIZE
<= PAGE_CACHE_SIZE
-
1157 f2fs_put_page(page
, 1);
1162 f2fs_put_page(page
, 1);
1165 static void write_normal_summaries(struct f2fs_sb_info
*sbi
,
1166 block_t blkaddr
, int type
)
1169 if (IS_DATASEG(type
))
1170 end
= type
+ NR_CURSEG_DATA_TYPE
;
1172 end
= type
+ NR_CURSEG_NODE_TYPE
;
1174 for (i
= type
; i
< end
; i
++) {
1175 struct curseg_info
*sum
= CURSEG_I(sbi
, i
);
1176 mutex_lock(&sum
->curseg_mutex
);
1177 write_sum_page(sbi
, sum
->sum_blk
, blkaddr
+ (i
- type
));
1178 mutex_unlock(&sum
->curseg_mutex
);
1182 void write_data_summaries(struct f2fs_sb_info
*sbi
, block_t start_blk
)
1184 if (is_set_ckpt_flags(F2FS_CKPT(sbi
), CP_COMPACT_SUM_FLAG
))
1185 write_compacted_summaries(sbi
, start_blk
);
1187 write_normal_summaries(sbi
, start_blk
, CURSEG_HOT_DATA
);
1190 void write_node_summaries(struct f2fs_sb_info
*sbi
, block_t start_blk
)
1192 if (is_set_ckpt_flags(F2FS_CKPT(sbi
), CP_UMOUNT_FLAG
))
1193 write_normal_summaries(sbi
, start_blk
, CURSEG_HOT_NODE
);
1197 int lookup_journal_in_cursum(struct f2fs_summary_block
*sum
, int type
,
1198 unsigned int val
, int alloc
)
1202 if (type
== NAT_JOURNAL
) {
1203 for (i
= 0; i
< nats_in_cursum(sum
); i
++) {
1204 if (le32_to_cpu(nid_in_journal(sum
, i
)) == val
)
1207 if (alloc
&& nats_in_cursum(sum
) < NAT_JOURNAL_ENTRIES
)
1208 return update_nats_in_cursum(sum
, 1);
1209 } else if (type
== SIT_JOURNAL
) {
1210 for (i
= 0; i
< sits_in_cursum(sum
); i
++)
1211 if (le32_to_cpu(segno_in_journal(sum
, i
)) == val
)
1213 if (alloc
&& sits_in_cursum(sum
) < SIT_JOURNAL_ENTRIES
)
1214 return update_sits_in_cursum(sum
, 1);
1219 static struct page
*get_current_sit_page(struct f2fs_sb_info
*sbi
,
1222 struct sit_info
*sit_i
= SIT_I(sbi
);
1223 unsigned int offset
= SIT_BLOCK_OFFSET(sit_i
, segno
);
1224 block_t blk_addr
= sit_i
->sit_base_addr
+ offset
;
1226 check_seg_range(sbi
, segno
);
1228 /* calculate sit block address */
1229 if (f2fs_test_bit(offset
, sit_i
->sit_bitmap
))
1230 blk_addr
+= sit_i
->sit_blocks
;
1232 return get_meta_page(sbi
, blk_addr
);
1235 static struct page
*get_next_sit_page(struct f2fs_sb_info
*sbi
,
1238 struct sit_info
*sit_i
= SIT_I(sbi
);
1239 struct page
*src_page
, *dst_page
;
1240 pgoff_t src_off
, dst_off
;
1241 void *src_addr
, *dst_addr
;
1243 src_off
= current_sit_addr(sbi
, start
);
1244 dst_off
= next_sit_addr(sbi
, src_off
);
1246 /* get current sit block page without lock */
1247 src_page
= get_meta_page(sbi
, src_off
);
1248 dst_page
= grab_meta_page(sbi
, dst_off
);
1249 BUG_ON(PageDirty(src_page
));
1251 src_addr
= page_address(src_page
);
1252 dst_addr
= page_address(dst_page
);
1253 memcpy(dst_addr
, src_addr
, PAGE_CACHE_SIZE
);
1255 set_page_dirty(dst_page
);
1256 f2fs_put_page(src_page
, 1);
1258 set_to_next_sit(sit_i
, start
);
1263 static bool flush_sits_in_journal(struct f2fs_sb_info
*sbi
)
1265 struct curseg_info
*curseg
= CURSEG_I(sbi
, CURSEG_COLD_DATA
);
1266 struct f2fs_summary_block
*sum
= curseg
->sum_blk
;
1270 * If the journal area in the current summary is full of sit entries,
1271 * all the sit entries will be flushed. Otherwise the sit entries
1272 * are not able to replace with newly hot sit entries.
1274 if (sits_in_cursum(sum
) >= SIT_JOURNAL_ENTRIES
) {
1275 for (i
= sits_in_cursum(sum
) - 1; i
>= 0; i
--) {
1277 segno
= le32_to_cpu(segno_in_journal(sum
, i
));
1278 __mark_sit_entry_dirty(sbi
, segno
);
1280 update_sits_in_cursum(sum
, -sits_in_cursum(sum
));
1287 * CP calls this function, which flushes SIT entries including sit_journal,
1288 * and moves prefree segs to free segs.
1290 void flush_sit_entries(struct f2fs_sb_info
*sbi
)
1292 struct sit_info
*sit_i
= SIT_I(sbi
);
1293 unsigned long *bitmap
= sit_i
->dirty_sentries_bitmap
;
1294 struct curseg_info
*curseg
= CURSEG_I(sbi
, CURSEG_COLD_DATA
);
1295 struct f2fs_summary_block
*sum
= curseg
->sum_blk
;
1296 unsigned long nsegs
= TOTAL_SEGS(sbi
);
1297 struct page
*page
= NULL
;
1298 struct f2fs_sit_block
*raw_sit
= NULL
;
1299 unsigned int start
= 0, end
= 0;
1300 unsigned int segno
= -1;
1303 mutex_lock(&curseg
->curseg_mutex
);
1304 mutex_lock(&sit_i
->sentry_lock
);
1307 * "flushed" indicates whether sit entries in journal are flushed
1308 * to the SIT area or not.
1310 flushed
= flush_sits_in_journal(sbi
);
1312 while ((segno
= find_next_bit(bitmap
, nsegs
, segno
+ 1)) < nsegs
) {
1313 struct seg_entry
*se
= get_seg_entry(sbi
, segno
);
1314 int sit_offset
, offset
;
1316 sit_offset
= SIT_ENTRY_OFFSET(sit_i
, segno
);
1321 offset
= lookup_journal_in_cursum(sum
, SIT_JOURNAL
, segno
, 1);
1323 segno_in_journal(sum
, offset
) = cpu_to_le32(segno
);
1324 seg_info_to_raw_sit(se
, &sit_in_journal(sum
, offset
));
1328 if (!page
|| (start
> segno
) || (segno
> end
)) {
1330 f2fs_put_page(page
, 1);
1334 start
= START_SEGNO(sit_i
, segno
);
1335 end
= start
+ SIT_ENTRY_PER_BLOCK
- 1;
1337 /* read sit block that will be updated */
1338 page
= get_next_sit_page(sbi
, start
);
1339 raw_sit
= page_address(page
);
1342 /* udpate entry in SIT block */
1343 seg_info_to_raw_sit(se
, &raw_sit
->entries
[sit_offset
]);
1345 __clear_bit(segno
, bitmap
);
1346 sit_i
->dirty_sentries
--;
1348 mutex_unlock(&sit_i
->sentry_lock
);
1349 mutex_unlock(&curseg
->curseg_mutex
);
1351 /* writeout last modified SIT block */
1352 f2fs_put_page(page
, 1);
1354 set_prefree_as_free_segments(sbi
);
1357 static int build_sit_info(struct f2fs_sb_info
*sbi
)
1359 struct f2fs_super_block
*raw_super
= F2FS_RAW_SUPER(sbi
);
1360 struct f2fs_checkpoint
*ckpt
= F2FS_CKPT(sbi
);
1361 struct sit_info
*sit_i
;
1362 unsigned int sit_segs
, start
;
1363 char *src_bitmap
, *dst_bitmap
;
1364 unsigned int bitmap_size
;
1366 /* allocate memory for SIT information */
1367 sit_i
= kzalloc(sizeof(struct sit_info
), GFP_KERNEL
);
1371 SM_I(sbi
)->sit_info
= sit_i
;
1373 sit_i
->sentries
= vzalloc(TOTAL_SEGS(sbi
) * sizeof(struct seg_entry
));
1374 if (!sit_i
->sentries
)
1377 bitmap_size
= f2fs_bitmap_size(TOTAL_SEGS(sbi
));
1378 sit_i
->dirty_sentries_bitmap
= kzalloc(bitmap_size
, GFP_KERNEL
);
1379 if (!sit_i
->dirty_sentries_bitmap
)
1382 for (start
= 0; start
< TOTAL_SEGS(sbi
); start
++) {
1383 sit_i
->sentries
[start
].cur_valid_map
1384 = kzalloc(SIT_VBLOCK_MAP_SIZE
, GFP_KERNEL
);
1385 sit_i
->sentries
[start
].ckpt_valid_map
1386 = kzalloc(SIT_VBLOCK_MAP_SIZE
, GFP_KERNEL
);
1387 if (!sit_i
->sentries
[start
].cur_valid_map
1388 || !sit_i
->sentries
[start
].ckpt_valid_map
)
1392 if (sbi
->segs_per_sec
> 1) {
1393 sit_i
->sec_entries
= vzalloc(sbi
->total_sections
*
1394 sizeof(struct sec_entry
));
1395 if (!sit_i
->sec_entries
)
1399 /* get information related with SIT */
1400 sit_segs
= le32_to_cpu(raw_super
->segment_count_sit
) >> 1;
1402 /* setup SIT bitmap from ckeckpoint pack */
1403 bitmap_size
= __bitmap_size(sbi
, SIT_BITMAP
);
1404 src_bitmap
= __bitmap_ptr(sbi
, SIT_BITMAP
);
1406 dst_bitmap
= kzalloc(bitmap_size
, GFP_KERNEL
);
1409 memcpy(dst_bitmap
, src_bitmap
, bitmap_size
);
1411 /* init SIT information */
1412 sit_i
->s_ops
= &default_salloc_ops
;
1414 sit_i
->sit_base_addr
= le32_to_cpu(raw_super
->sit_blkaddr
);
1415 sit_i
->sit_blocks
= sit_segs
<< sbi
->log_blocks_per_seg
;
1416 sit_i
->written_valid_blocks
= le64_to_cpu(ckpt
->valid_block_count
);
1417 sit_i
->sit_bitmap
= dst_bitmap
;
1418 sit_i
->bitmap_size
= bitmap_size
;
1419 sit_i
->dirty_sentries
= 0;
1420 sit_i
->sents_per_block
= SIT_ENTRY_PER_BLOCK
;
1421 sit_i
->elapsed_time
= le64_to_cpu(sbi
->ckpt
->elapsed_time
);
1422 sit_i
->mounted_time
= CURRENT_TIME_SEC
.tv_sec
;
1423 mutex_init(&sit_i
->sentry_lock
);
1427 static int build_free_segmap(struct f2fs_sb_info
*sbi
)
1429 struct f2fs_sm_info
*sm_info
= SM_I(sbi
);
1430 struct free_segmap_info
*free_i
;
1431 unsigned int bitmap_size
, sec_bitmap_size
;
1433 /* allocate memory for free segmap information */
1434 free_i
= kzalloc(sizeof(struct free_segmap_info
), GFP_KERNEL
);
1438 SM_I(sbi
)->free_info
= free_i
;
1440 bitmap_size
= f2fs_bitmap_size(TOTAL_SEGS(sbi
));
1441 free_i
->free_segmap
= kmalloc(bitmap_size
, GFP_KERNEL
);
1442 if (!free_i
->free_segmap
)
1445 sec_bitmap_size
= f2fs_bitmap_size(sbi
->total_sections
);
1446 free_i
->free_secmap
= kmalloc(sec_bitmap_size
, GFP_KERNEL
);
1447 if (!free_i
->free_secmap
)
1450 /* set all segments as dirty temporarily */
1451 memset(free_i
->free_segmap
, 0xff, bitmap_size
);
1452 memset(free_i
->free_secmap
, 0xff, sec_bitmap_size
);
1454 /* init free segmap information */
1455 free_i
->start_segno
=
1456 (unsigned int) GET_SEGNO_FROM_SEG0(sbi
, sm_info
->main_blkaddr
);
1457 free_i
->free_segments
= 0;
1458 free_i
->free_sections
= 0;
1459 rwlock_init(&free_i
->segmap_lock
);
1463 static int build_curseg(struct f2fs_sb_info
*sbi
)
1465 struct curseg_info
*array
;
1468 array
= kzalloc(sizeof(*array
) * NR_CURSEG_TYPE
, GFP_KERNEL
);
1472 SM_I(sbi
)->curseg_array
= array
;
1474 for (i
= 0; i
< NR_CURSEG_TYPE
; i
++) {
1475 mutex_init(&array
[i
].curseg_mutex
);
1476 array
[i
].sum_blk
= kzalloc(PAGE_CACHE_SIZE
, GFP_KERNEL
);
1477 if (!array
[i
].sum_blk
)
1479 array
[i
].segno
= NULL_SEGNO
;
1480 array
[i
].next_blkoff
= 0;
1482 return restore_curseg_summaries(sbi
);
1485 static void build_sit_entries(struct f2fs_sb_info
*sbi
)
1487 struct sit_info
*sit_i
= SIT_I(sbi
);
1488 struct curseg_info
*curseg
= CURSEG_I(sbi
, CURSEG_COLD_DATA
);
1489 struct f2fs_summary_block
*sum
= curseg
->sum_blk
;
1492 for (start
= 0; start
< TOTAL_SEGS(sbi
); start
++) {
1493 struct seg_entry
*se
= &sit_i
->sentries
[start
];
1494 struct f2fs_sit_block
*sit_blk
;
1495 struct f2fs_sit_entry sit
;
1499 mutex_lock(&curseg
->curseg_mutex
);
1500 for (i
= 0; i
< sits_in_cursum(sum
); i
++) {
1501 if (le32_to_cpu(segno_in_journal(sum
, i
)) == start
) {
1502 sit
= sit_in_journal(sum
, i
);
1503 mutex_unlock(&curseg
->curseg_mutex
);
1507 mutex_unlock(&curseg
->curseg_mutex
);
1508 page
= get_current_sit_page(sbi
, start
);
1509 sit_blk
= (struct f2fs_sit_block
*)page_address(page
);
1510 sit
= sit_blk
->entries
[SIT_ENTRY_OFFSET(sit_i
, start
)];
1511 f2fs_put_page(page
, 1);
1513 check_block_count(sbi
, start
, &sit
);
1514 seg_info_from_raw_sit(se
, &sit
);
1515 if (sbi
->segs_per_sec
> 1) {
1516 struct sec_entry
*e
= get_sec_entry(sbi
, start
);
1517 e
->valid_blocks
+= se
->valid_blocks
;
1522 static void init_free_segmap(struct f2fs_sb_info
*sbi
)
1527 for (start
= 0; start
< TOTAL_SEGS(sbi
); start
++) {
1528 struct seg_entry
*sentry
= get_seg_entry(sbi
, start
);
1529 if (!sentry
->valid_blocks
)
1530 __set_free(sbi
, start
);
1533 /* set use the current segments */
1534 for (type
= CURSEG_HOT_DATA
; type
<= CURSEG_COLD_NODE
; type
++) {
1535 struct curseg_info
*curseg_t
= CURSEG_I(sbi
, type
);
1536 __set_test_and_inuse(sbi
, curseg_t
->segno
);
1540 static void init_dirty_segmap(struct f2fs_sb_info
*sbi
)
1542 struct dirty_seglist_info
*dirty_i
= DIRTY_I(sbi
);
1543 struct free_segmap_info
*free_i
= FREE_I(sbi
);
1544 unsigned int segno
= 0, offset
= 0;
1545 unsigned short valid_blocks
;
1547 while (segno
< TOTAL_SEGS(sbi
)) {
1548 /* find dirty segment based on free segmap */
1549 segno
= find_next_inuse(free_i
, TOTAL_SEGS(sbi
), offset
);
1550 if (segno
>= TOTAL_SEGS(sbi
))
1553 valid_blocks
= get_valid_blocks(sbi
, segno
, 0);
1554 if (valid_blocks
>= sbi
->blocks_per_seg
|| !valid_blocks
)
1556 mutex_lock(&dirty_i
->seglist_lock
);
1557 __locate_dirty_segment(sbi
, segno
, DIRTY
);
1558 mutex_unlock(&dirty_i
->seglist_lock
);
1562 static int init_victim_segmap(struct f2fs_sb_info
*sbi
)
1564 struct dirty_seglist_info
*dirty_i
= DIRTY_I(sbi
);
1565 unsigned int bitmap_size
= f2fs_bitmap_size(TOTAL_SEGS(sbi
));
1567 dirty_i
->victim_segmap
[FG_GC
] = kzalloc(bitmap_size
, GFP_KERNEL
);
1568 dirty_i
->victim_segmap
[BG_GC
] = kzalloc(bitmap_size
, GFP_KERNEL
);
1569 if (!dirty_i
->victim_segmap
[FG_GC
] || !dirty_i
->victim_segmap
[BG_GC
])
1574 static int build_dirty_segmap(struct f2fs_sb_info
*sbi
)
1576 struct dirty_seglist_info
*dirty_i
;
1577 unsigned int bitmap_size
, i
;
1579 /* allocate memory for dirty segments list information */
1580 dirty_i
= kzalloc(sizeof(struct dirty_seglist_info
), GFP_KERNEL
);
1584 SM_I(sbi
)->dirty_info
= dirty_i
;
1585 mutex_init(&dirty_i
->seglist_lock
);
1587 bitmap_size
= f2fs_bitmap_size(TOTAL_SEGS(sbi
));
1589 for (i
= 0; i
< NR_DIRTY_TYPE
; i
++) {
1590 dirty_i
->dirty_segmap
[i
] = kzalloc(bitmap_size
, GFP_KERNEL
);
1591 if (!dirty_i
->dirty_segmap
[i
])
1595 init_dirty_segmap(sbi
);
1596 return init_victim_segmap(sbi
);
1600 * Update min, max modified time for cost-benefit GC algorithm
1602 static void init_min_max_mtime(struct f2fs_sb_info
*sbi
)
1604 struct sit_info
*sit_i
= SIT_I(sbi
);
1607 mutex_lock(&sit_i
->sentry_lock
);
1609 sit_i
->min_mtime
= LLONG_MAX
;
1611 for (segno
= 0; segno
< TOTAL_SEGS(sbi
); segno
+= sbi
->segs_per_sec
) {
1613 unsigned long long mtime
= 0;
1615 for (i
= 0; i
< sbi
->segs_per_sec
; i
++)
1616 mtime
+= get_seg_entry(sbi
, segno
+ i
)->mtime
;
1618 mtime
= div_u64(mtime
, sbi
->segs_per_sec
);
1620 if (sit_i
->min_mtime
> mtime
)
1621 sit_i
->min_mtime
= mtime
;
1623 sit_i
->max_mtime
= get_mtime(sbi
);
1624 mutex_unlock(&sit_i
->sentry_lock
);
1627 int build_segment_manager(struct f2fs_sb_info
*sbi
)
1629 struct f2fs_super_block
*raw_super
= F2FS_RAW_SUPER(sbi
);
1630 struct f2fs_checkpoint
*ckpt
= F2FS_CKPT(sbi
);
1631 struct f2fs_sm_info
*sm_info
;
1634 sm_info
= kzalloc(sizeof(struct f2fs_sm_info
), GFP_KERNEL
);
1639 sbi
->sm_info
= sm_info
;
1640 INIT_LIST_HEAD(&sm_info
->wblist_head
);
1641 spin_lock_init(&sm_info
->wblist_lock
);
1642 sm_info
->seg0_blkaddr
= le32_to_cpu(raw_super
->segment0_blkaddr
);
1643 sm_info
->main_blkaddr
= le32_to_cpu(raw_super
->main_blkaddr
);
1644 sm_info
->segment_count
= le32_to_cpu(raw_super
->segment_count
);
1645 sm_info
->reserved_segments
= le32_to_cpu(ckpt
->rsvd_segment_count
);
1646 sm_info
->ovp_segments
= le32_to_cpu(ckpt
->overprov_segment_count
);
1647 sm_info
->main_segments
= le32_to_cpu(raw_super
->segment_count_main
);
1648 sm_info
->ssa_blkaddr
= le32_to_cpu(raw_super
->ssa_blkaddr
);
1650 err
= build_sit_info(sbi
);
1653 err
= build_free_segmap(sbi
);
1656 err
= build_curseg(sbi
);
1660 /* reinit free segmap based on SIT */
1661 build_sit_entries(sbi
);
1663 init_free_segmap(sbi
);
1664 err
= build_dirty_segmap(sbi
);
1668 init_min_max_mtime(sbi
);
1672 static void discard_dirty_segmap(struct f2fs_sb_info
*sbi
,
1673 enum dirty_type dirty_type
)
1675 struct dirty_seglist_info
*dirty_i
= DIRTY_I(sbi
);
1677 mutex_lock(&dirty_i
->seglist_lock
);
1678 kfree(dirty_i
->dirty_segmap
[dirty_type
]);
1679 dirty_i
->nr_dirty
[dirty_type
] = 0;
1680 mutex_unlock(&dirty_i
->seglist_lock
);
1683 void reset_victim_segmap(struct f2fs_sb_info
*sbi
)
1685 unsigned int bitmap_size
= f2fs_bitmap_size(TOTAL_SEGS(sbi
));
1686 memset(DIRTY_I(sbi
)->victim_segmap
[FG_GC
], 0, bitmap_size
);
1689 static void destroy_victim_segmap(struct f2fs_sb_info
*sbi
)
1691 struct dirty_seglist_info
*dirty_i
= DIRTY_I(sbi
);
1693 kfree(dirty_i
->victim_segmap
[FG_GC
]);
1694 kfree(dirty_i
->victim_segmap
[BG_GC
]);
1697 static void destroy_dirty_segmap(struct f2fs_sb_info
*sbi
)
1699 struct dirty_seglist_info
*dirty_i
= DIRTY_I(sbi
);
1705 /* discard pre-free/dirty segments list */
1706 for (i
= 0; i
< NR_DIRTY_TYPE
; i
++)
1707 discard_dirty_segmap(sbi
, i
);
1709 destroy_victim_segmap(sbi
);
1710 SM_I(sbi
)->dirty_info
= NULL
;
1714 static void destroy_curseg(struct f2fs_sb_info
*sbi
)
1716 struct curseg_info
*array
= SM_I(sbi
)->curseg_array
;
1721 SM_I(sbi
)->curseg_array
= NULL
;
1722 for (i
= 0; i
< NR_CURSEG_TYPE
; i
++)
1723 kfree(array
[i
].sum_blk
);
1727 static void destroy_free_segmap(struct f2fs_sb_info
*sbi
)
1729 struct free_segmap_info
*free_i
= SM_I(sbi
)->free_info
;
1732 SM_I(sbi
)->free_info
= NULL
;
1733 kfree(free_i
->free_segmap
);
1734 kfree(free_i
->free_secmap
);
1738 static void destroy_sit_info(struct f2fs_sb_info
*sbi
)
1740 struct sit_info
*sit_i
= SIT_I(sbi
);
1746 if (sit_i
->sentries
) {
1747 for (start
= 0; start
< TOTAL_SEGS(sbi
); start
++) {
1748 kfree(sit_i
->sentries
[start
].cur_valid_map
);
1749 kfree(sit_i
->sentries
[start
].ckpt_valid_map
);
1752 vfree(sit_i
->sentries
);
1753 vfree(sit_i
->sec_entries
);
1754 kfree(sit_i
->dirty_sentries_bitmap
);
1756 SM_I(sbi
)->sit_info
= NULL
;
1757 kfree(sit_i
->sit_bitmap
);
1761 void destroy_segment_manager(struct f2fs_sb_info
*sbi
)
1763 struct f2fs_sm_info
*sm_info
= SM_I(sbi
);
1764 destroy_dirty_segmap(sbi
);
1765 destroy_curseg(sbi
);
1766 destroy_free_segmap(sbi
);
1767 destroy_sit_info(sbi
);
1768 sbi
->sm_info
= NULL
;