4 * Copyright (c) 2012 Samsung Electronics Co., Ltd.
5 * http://www.samsung.com/
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License version 2 as
9 * published by the Free Software Foundation.
12 #include <linux/f2fs_fs.h>
13 #include <linux/bio.h>
14 #include <linux/blkdev.h>
15 #include <linux/prefetch.h>
16 #include <linux/vmalloc.h>
21 #include <trace/events/f2fs.h>
24 * This function balances dirty node and dentry pages.
25 * In addition, it controls garbage collection.
27 void f2fs_balance_fs(struct f2fs_sb_info
*sbi
)
30 * We should do GC or end up with checkpoint, if there are so many dirty
31 * dir/node pages without enough free segments.
33 if (has_not_enough_free_secs(sbi
, 0)) {
34 mutex_lock(&sbi
->gc_mutex
);
39 void f2fs_balance_fs_bg(struct f2fs_sb_info
*sbi
)
41 /* check the # of cached NAT entries and prefree segments */
42 if (try_to_free_nats(sbi
, NAT_ENTRY_PER_BLOCK
) ||
43 excess_prefree_segs(sbi
))
44 f2fs_sync_fs(sbi
->sb
, true);
47 static void __locate_dirty_segment(struct f2fs_sb_info
*sbi
, unsigned int segno
,
48 enum dirty_type dirty_type
)
50 struct dirty_seglist_info
*dirty_i
= DIRTY_I(sbi
);
52 /* need not be added */
53 if (IS_CURSEG(sbi
, segno
))
56 if (!test_and_set_bit(segno
, dirty_i
->dirty_segmap
[dirty_type
]))
57 dirty_i
->nr_dirty
[dirty_type
]++;
59 if (dirty_type
== DIRTY
) {
60 struct seg_entry
*sentry
= get_seg_entry(sbi
, segno
);
61 enum dirty_type t
= DIRTY_HOT_DATA
;
63 dirty_type
= sentry
->type
;
65 if (!test_and_set_bit(segno
, dirty_i
->dirty_segmap
[dirty_type
]))
66 dirty_i
->nr_dirty
[dirty_type
]++;
68 /* Only one bitmap should be set */
69 for (; t
<= DIRTY_COLD_NODE
; t
++) {
72 if (test_and_clear_bit(segno
, dirty_i
->dirty_segmap
[t
]))
73 dirty_i
->nr_dirty
[t
]--;
78 static void __remove_dirty_segment(struct f2fs_sb_info
*sbi
, unsigned int segno
,
79 enum dirty_type dirty_type
)
81 struct dirty_seglist_info
*dirty_i
= DIRTY_I(sbi
);
83 if (test_and_clear_bit(segno
, dirty_i
->dirty_segmap
[dirty_type
]))
84 dirty_i
->nr_dirty
[dirty_type
]--;
86 if (dirty_type
== DIRTY
) {
87 enum dirty_type t
= DIRTY_HOT_DATA
;
89 /* clear its dirty bitmap */
90 for (; t
<= DIRTY_COLD_NODE
; t
++) {
91 if (test_and_clear_bit(segno
,
92 dirty_i
->dirty_segmap
[t
])) {
93 dirty_i
->nr_dirty
[t
]--;
98 if (get_valid_blocks(sbi
, segno
, sbi
->segs_per_sec
) == 0)
99 clear_bit(GET_SECNO(sbi
, segno
),
100 dirty_i
->victim_secmap
);
105 * Should not occur error such as -ENOMEM.
106 * Adding dirty entry into seglist is not critical operation.
107 * If a given segment is one of current working segments, it won't be added.
109 static void locate_dirty_segment(struct f2fs_sb_info
*sbi
, unsigned int segno
)
111 struct dirty_seglist_info
*dirty_i
= DIRTY_I(sbi
);
112 unsigned short valid_blocks
;
114 if (segno
== NULL_SEGNO
|| IS_CURSEG(sbi
, segno
))
117 mutex_lock(&dirty_i
->seglist_lock
);
119 valid_blocks
= get_valid_blocks(sbi
, segno
, 0);
121 if (valid_blocks
== 0) {
122 __locate_dirty_segment(sbi
, segno
, PRE
);
123 __remove_dirty_segment(sbi
, segno
, DIRTY
);
124 } else if (valid_blocks
< sbi
->blocks_per_seg
) {
125 __locate_dirty_segment(sbi
, segno
, DIRTY
);
127 /* Recovery routine with SSR needs this */
128 __remove_dirty_segment(sbi
, segno
, DIRTY
);
131 mutex_unlock(&dirty_i
->seglist_lock
);
135 * Should call clear_prefree_segments after checkpoint is done.
137 static void set_prefree_as_free_segments(struct f2fs_sb_info
*sbi
)
139 struct dirty_seglist_info
*dirty_i
= DIRTY_I(sbi
);
140 unsigned int segno
= -1;
141 unsigned int total_segs
= TOTAL_SEGS(sbi
);
143 mutex_lock(&dirty_i
->seglist_lock
);
145 segno
= find_next_bit(dirty_i
->dirty_segmap
[PRE
], total_segs
,
147 if (segno
>= total_segs
)
149 __set_test_and_free(sbi
, segno
);
151 mutex_unlock(&dirty_i
->seglist_lock
);
154 void clear_prefree_segments(struct f2fs_sb_info
*sbi
)
156 struct dirty_seglist_info
*dirty_i
= DIRTY_I(sbi
);
157 unsigned int segno
= -1;
158 unsigned int total_segs
= TOTAL_SEGS(sbi
);
160 mutex_lock(&dirty_i
->seglist_lock
);
162 segno
= find_next_bit(dirty_i
->dirty_segmap
[PRE
], total_segs
,
164 if (segno
>= total_segs
)
167 if (test_and_clear_bit(segno
, dirty_i
->dirty_segmap
[PRE
]))
168 dirty_i
->nr_dirty
[PRE
]--;
171 if (test_opt(sbi
, DISCARD
))
172 blkdev_issue_discard(sbi
->sb
->s_bdev
,
173 START_BLOCK(sbi
, segno
) <<
174 sbi
->log_sectors_per_block
,
175 1 << (sbi
->log_sectors_per_block
+
176 sbi
->log_blocks_per_seg
),
179 mutex_unlock(&dirty_i
->seglist_lock
);
182 static void __mark_sit_entry_dirty(struct f2fs_sb_info
*sbi
, unsigned int segno
)
184 struct sit_info
*sit_i
= SIT_I(sbi
);
185 if (!__test_and_set_bit(segno
, sit_i
->dirty_sentries_bitmap
))
186 sit_i
->dirty_sentries
++;
189 static void __set_sit_entry_type(struct f2fs_sb_info
*sbi
, int type
,
190 unsigned int segno
, int modified
)
192 struct seg_entry
*se
= get_seg_entry(sbi
, segno
);
195 __mark_sit_entry_dirty(sbi
, segno
);
198 static void update_sit_entry(struct f2fs_sb_info
*sbi
, block_t blkaddr
, int del
)
200 struct seg_entry
*se
;
201 unsigned int segno
, offset
;
202 long int new_vblocks
;
204 segno
= GET_SEGNO(sbi
, blkaddr
);
206 se
= get_seg_entry(sbi
, segno
);
207 new_vblocks
= se
->valid_blocks
+ del
;
208 offset
= GET_SEGOFF_FROM_SEG0(sbi
, blkaddr
) & (sbi
->blocks_per_seg
- 1);
210 BUG_ON((new_vblocks
>> (sizeof(unsigned short) << 3) ||
211 (new_vblocks
> sbi
->blocks_per_seg
)));
213 se
->valid_blocks
= new_vblocks
;
214 se
->mtime
= get_mtime(sbi
);
215 SIT_I(sbi
)->max_mtime
= se
->mtime
;
217 /* Update valid block bitmap */
219 if (f2fs_set_bit(offset
, se
->cur_valid_map
))
222 if (!f2fs_clear_bit(offset
, se
->cur_valid_map
))
225 if (!f2fs_test_bit(offset
, se
->ckpt_valid_map
))
226 se
->ckpt_valid_blocks
+= del
;
228 __mark_sit_entry_dirty(sbi
, segno
);
230 /* update total number of valid blocks to be written in ckpt area */
231 SIT_I(sbi
)->written_valid_blocks
+= del
;
233 if (sbi
->segs_per_sec
> 1)
234 get_sec_entry(sbi
, segno
)->valid_blocks
+= del
;
237 static void refresh_sit_entry(struct f2fs_sb_info
*sbi
,
238 block_t old_blkaddr
, block_t new_blkaddr
)
240 update_sit_entry(sbi
, new_blkaddr
, 1);
241 if (GET_SEGNO(sbi
, old_blkaddr
) != NULL_SEGNO
)
242 update_sit_entry(sbi
, old_blkaddr
, -1);
245 void invalidate_blocks(struct f2fs_sb_info
*sbi
, block_t addr
)
247 unsigned int segno
= GET_SEGNO(sbi
, addr
);
248 struct sit_info
*sit_i
= SIT_I(sbi
);
250 BUG_ON(addr
== NULL_ADDR
);
251 if (addr
== NEW_ADDR
)
254 /* add it into sit main buffer */
255 mutex_lock(&sit_i
->sentry_lock
);
257 update_sit_entry(sbi
, addr
, -1);
259 /* add it into dirty seglist */
260 locate_dirty_segment(sbi
, segno
);
262 mutex_unlock(&sit_i
->sentry_lock
);
266 * This function should be resided under the curseg_mutex lock
268 static void __add_sum_entry(struct f2fs_sb_info
*sbi
, int type
,
269 struct f2fs_summary
*sum
)
271 struct curseg_info
*curseg
= CURSEG_I(sbi
, type
);
272 void *addr
= curseg
->sum_blk
;
273 addr
+= curseg
->next_blkoff
* sizeof(struct f2fs_summary
);
274 memcpy(addr
, sum
, sizeof(struct f2fs_summary
));
278 * Calculate the number of current summary pages for writing
280 int npages_for_summary_flush(struct f2fs_sb_info
*sbi
)
282 int total_size_bytes
= 0;
283 int valid_sum_count
= 0;
286 for (i
= CURSEG_HOT_DATA
; i
<= CURSEG_COLD_DATA
; i
++) {
287 if (sbi
->ckpt
->alloc_type
[i
] == SSR
)
288 valid_sum_count
+= sbi
->blocks_per_seg
;
290 valid_sum_count
+= curseg_blkoff(sbi
, i
);
293 total_size_bytes
= valid_sum_count
* (SUMMARY_SIZE
+ 1)
294 + sizeof(struct nat_journal
) + 2
295 + sizeof(struct sit_journal
) + 2;
296 sum_space
= PAGE_CACHE_SIZE
- SUM_FOOTER_SIZE
;
297 if (total_size_bytes
< sum_space
)
299 else if (total_size_bytes
< 2 * sum_space
)
305 * Caller should put this summary page
307 struct page
*get_sum_page(struct f2fs_sb_info
*sbi
, unsigned int segno
)
309 return get_meta_page(sbi
, GET_SUM_BLOCK(sbi
, segno
));
312 static void write_sum_page(struct f2fs_sb_info
*sbi
,
313 struct f2fs_summary_block
*sum_blk
, block_t blk_addr
)
315 struct page
*page
= grab_meta_page(sbi
, blk_addr
);
316 void *kaddr
= page_address(page
);
317 memcpy(kaddr
, sum_blk
, PAGE_CACHE_SIZE
);
318 set_page_dirty(page
);
319 f2fs_put_page(page
, 1);
322 static int is_next_segment_free(struct f2fs_sb_info
*sbi
, int type
)
324 struct curseg_info
*curseg
= CURSEG_I(sbi
, type
);
325 unsigned int segno
= curseg
->segno
+ 1;
326 struct free_segmap_info
*free_i
= FREE_I(sbi
);
328 if (segno
< TOTAL_SEGS(sbi
) && segno
% sbi
->segs_per_sec
)
329 return !test_bit(segno
, free_i
->free_segmap
);
334 * Find a new segment from the free segments bitmap to right order
335 * This function should be returned with success, otherwise BUG
337 static void get_new_segment(struct f2fs_sb_info
*sbi
,
338 unsigned int *newseg
, bool new_sec
, int dir
)
340 struct free_segmap_info
*free_i
= FREE_I(sbi
);
341 unsigned int segno
, secno
, zoneno
;
342 unsigned int total_zones
= TOTAL_SECS(sbi
) / sbi
->secs_per_zone
;
343 unsigned int hint
= *newseg
/ sbi
->segs_per_sec
;
344 unsigned int old_zoneno
= GET_ZONENO_FROM_SEGNO(sbi
, *newseg
);
345 unsigned int left_start
= hint
;
350 write_lock(&free_i
->segmap_lock
);
352 if (!new_sec
&& ((*newseg
+ 1) % sbi
->segs_per_sec
)) {
353 segno
= find_next_zero_bit(free_i
->free_segmap
,
354 TOTAL_SEGS(sbi
), *newseg
+ 1);
355 if (segno
- *newseg
< sbi
->segs_per_sec
-
356 (*newseg
% sbi
->segs_per_sec
))
360 secno
= find_next_zero_bit(free_i
->free_secmap
, TOTAL_SECS(sbi
), hint
);
361 if (secno
>= TOTAL_SECS(sbi
)) {
362 if (dir
== ALLOC_RIGHT
) {
363 secno
= find_next_zero_bit(free_i
->free_secmap
,
365 BUG_ON(secno
>= TOTAL_SECS(sbi
));
368 left_start
= hint
- 1;
374 while (test_bit(left_start
, free_i
->free_secmap
)) {
375 if (left_start
> 0) {
379 left_start
= find_next_zero_bit(free_i
->free_secmap
,
381 BUG_ON(left_start
>= TOTAL_SECS(sbi
));
387 segno
= secno
* sbi
->segs_per_sec
;
388 zoneno
= secno
/ sbi
->secs_per_zone
;
390 /* give up on finding another zone */
393 if (sbi
->secs_per_zone
== 1)
395 if (zoneno
== old_zoneno
)
397 if (dir
== ALLOC_LEFT
) {
398 if (!go_left
&& zoneno
+ 1 >= total_zones
)
400 if (go_left
&& zoneno
== 0)
403 for (i
= 0; i
< NR_CURSEG_TYPE
; i
++)
404 if (CURSEG_I(sbi
, i
)->zone
== zoneno
)
407 if (i
< NR_CURSEG_TYPE
) {
408 /* zone is in user, try another */
410 hint
= zoneno
* sbi
->secs_per_zone
- 1;
411 else if (zoneno
+ 1 >= total_zones
)
414 hint
= (zoneno
+ 1) * sbi
->secs_per_zone
;
416 goto find_other_zone
;
419 /* set it as dirty segment in free segmap */
420 BUG_ON(test_bit(segno
, free_i
->free_segmap
));
421 __set_inuse(sbi
, segno
);
423 write_unlock(&free_i
->segmap_lock
);
426 static void reset_curseg(struct f2fs_sb_info
*sbi
, int type
, int modified
)
428 struct curseg_info
*curseg
= CURSEG_I(sbi
, type
);
429 struct summary_footer
*sum_footer
;
431 curseg
->segno
= curseg
->next_segno
;
432 curseg
->zone
= GET_ZONENO_FROM_SEGNO(sbi
, curseg
->segno
);
433 curseg
->next_blkoff
= 0;
434 curseg
->next_segno
= NULL_SEGNO
;
436 sum_footer
= &(curseg
->sum_blk
->footer
);
437 memset(sum_footer
, 0, sizeof(struct summary_footer
));
438 if (IS_DATASEG(type
))
439 SET_SUM_TYPE(sum_footer
, SUM_TYPE_DATA
);
440 if (IS_NODESEG(type
))
441 SET_SUM_TYPE(sum_footer
, SUM_TYPE_NODE
);
442 __set_sit_entry_type(sbi
, type
, curseg
->segno
, modified
);
446 * Allocate a current working segment.
447 * This function always allocates a free segment in LFS manner.
449 static void new_curseg(struct f2fs_sb_info
*sbi
, int type
, bool new_sec
)
451 struct curseg_info
*curseg
= CURSEG_I(sbi
, type
);
452 unsigned int segno
= curseg
->segno
;
453 int dir
= ALLOC_LEFT
;
455 write_sum_page(sbi
, curseg
->sum_blk
,
456 GET_SUM_BLOCK(sbi
, segno
));
457 if (type
== CURSEG_WARM_DATA
|| type
== CURSEG_COLD_DATA
)
460 if (test_opt(sbi
, NOHEAP
))
463 get_new_segment(sbi
, &segno
, new_sec
, dir
);
464 curseg
->next_segno
= segno
;
465 reset_curseg(sbi
, type
, 1);
466 curseg
->alloc_type
= LFS
;
469 static void __next_free_blkoff(struct f2fs_sb_info
*sbi
,
470 struct curseg_info
*seg
, block_t start
)
472 struct seg_entry
*se
= get_seg_entry(sbi
, seg
->segno
);
474 for (ofs
= start
; ofs
< sbi
->blocks_per_seg
; ofs
++) {
475 if (!f2fs_test_bit(ofs
, se
->ckpt_valid_map
)
476 && !f2fs_test_bit(ofs
, se
->cur_valid_map
))
479 seg
->next_blkoff
= ofs
;
483 * If a segment is written by LFS manner, next block offset is just obtained
484 * by increasing the current block offset. However, if a segment is written by
485 * SSR manner, next block offset obtained by calling __next_free_blkoff
487 static void __refresh_next_blkoff(struct f2fs_sb_info
*sbi
,
488 struct curseg_info
*seg
)
490 if (seg
->alloc_type
== SSR
)
491 __next_free_blkoff(sbi
, seg
, seg
->next_blkoff
+ 1);
497 * This function always allocates a used segment (from dirty seglist) by SSR
498 * manner, so it should recover the existing segment information of valid blocks
500 static void change_curseg(struct f2fs_sb_info
*sbi
, int type
, bool reuse
)
502 struct dirty_seglist_info
*dirty_i
= DIRTY_I(sbi
);
503 struct curseg_info
*curseg
= CURSEG_I(sbi
, type
);
504 unsigned int new_segno
= curseg
->next_segno
;
505 struct f2fs_summary_block
*sum_node
;
506 struct page
*sum_page
;
508 write_sum_page(sbi
, curseg
->sum_blk
,
509 GET_SUM_BLOCK(sbi
, curseg
->segno
));
510 __set_test_and_inuse(sbi
, new_segno
);
512 mutex_lock(&dirty_i
->seglist_lock
);
513 __remove_dirty_segment(sbi
, new_segno
, PRE
);
514 __remove_dirty_segment(sbi
, new_segno
, DIRTY
);
515 mutex_unlock(&dirty_i
->seglist_lock
);
517 reset_curseg(sbi
, type
, 1);
518 curseg
->alloc_type
= SSR
;
519 __next_free_blkoff(sbi
, curseg
, 0);
522 sum_page
= get_sum_page(sbi
, new_segno
);
523 sum_node
= (struct f2fs_summary_block
*)page_address(sum_page
);
524 memcpy(curseg
->sum_blk
, sum_node
, SUM_ENTRY_SIZE
);
525 f2fs_put_page(sum_page
, 1);
529 static int get_ssr_segment(struct f2fs_sb_info
*sbi
, int type
)
531 struct curseg_info
*curseg
= CURSEG_I(sbi
, type
);
532 const struct victim_selection
*v_ops
= DIRTY_I(sbi
)->v_ops
;
534 if (IS_NODESEG(type
) || !has_not_enough_free_secs(sbi
, 0))
535 return v_ops
->get_victim(sbi
,
536 &(curseg
)->next_segno
, BG_GC
, type
, SSR
);
538 /* For data segments, let's do SSR more intensively */
539 for (; type
>= CURSEG_HOT_DATA
; type
--)
540 if (v_ops
->get_victim(sbi
, &(curseg
)->next_segno
,
547 * flush out current segment and replace it with new segment
548 * This function should be returned with success, otherwise BUG
550 static void allocate_segment_by_default(struct f2fs_sb_info
*sbi
,
551 int type
, bool force
)
553 struct curseg_info
*curseg
= CURSEG_I(sbi
, type
);
556 new_curseg(sbi
, type
, true);
557 else if (type
== CURSEG_WARM_NODE
)
558 new_curseg(sbi
, type
, false);
559 else if (curseg
->alloc_type
== LFS
&& is_next_segment_free(sbi
, type
))
560 new_curseg(sbi
, type
, false);
561 else if (need_SSR(sbi
) && get_ssr_segment(sbi
, type
))
562 change_curseg(sbi
, type
, true);
564 new_curseg(sbi
, type
, false);
566 stat_inc_seg_type(sbi
, curseg
);
569 void allocate_new_segments(struct f2fs_sb_info
*sbi
)
571 struct curseg_info
*curseg
;
572 unsigned int old_curseg
;
575 for (i
= CURSEG_HOT_DATA
; i
<= CURSEG_COLD_DATA
; i
++) {
576 curseg
= CURSEG_I(sbi
, i
);
577 old_curseg
= curseg
->segno
;
578 SIT_I(sbi
)->s_ops
->allocate_segment(sbi
, i
, true);
579 locate_dirty_segment(sbi
, old_curseg
);
583 static const struct segment_allocation default_salloc_ops
= {
584 .allocate_segment
= allocate_segment_by_default
,
587 static void f2fs_end_io_write(struct bio
*bio
, int err
)
589 const int uptodate
= test_bit(BIO_UPTODATE
, &bio
->bi_flags
);
590 struct bio_vec
*bvec
= bio
->bi_io_vec
+ bio
->bi_vcnt
- 1;
591 struct bio_private
*p
= bio
->bi_private
;
594 struct page
*page
= bvec
->bv_page
;
596 if (--bvec
>= bio
->bi_io_vec
)
597 prefetchw(&bvec
->bv_page
->flags
);
601 set_bit(AS_EIO
, &page
->mapping
->flags
);
602 set_ckpt_flags(p
->sbi
->ckpt
, CP_ERROR_FLAG
);
603 p
->sbi
->sb
->s_flags
|= MS_RDONLY
;
605 end_page_writeback(page
);
606 dec_page_count(p
->sbi
, F2FS_WRITEBACK
);
607 } while (bvec
>= bio
->bi_io_vec
);
612 if (!get_pages(p
->sbi
, F2FS_WRITEBACK
) && p
->sbi
->cp_task
)
613 wake_up_process(p
->sbi
->cp_task
);
619 struct bio
*f2fs_bio_alloc(struct block_device
*bdev
, int npages
)
623 /* No failure on bio allocation */
624 bio
= bio_alloc(GFP_NOIO
, npages
);
626 bio
->bi_private
= NULL
;
631 static void do_submit_bio(struct f2fs_sb_info
*sbi
,
632 enum page_type type
, bool sync
)
634 int rw
= sync
? WRITE_SYNC
: WRITE
;
635 enum page_type btype
= type
> META
? META
: type
;
637 if (type
>= META_FLUSH
)
638 rw
= WRITE_FLUSH_FUA
;
643 if (sbi
->bio
[btype
]) {
644 struct bio_private
*p
= sbi
->bio
[btype
]->bi_private
;
646 sbi
->bio
[btype
]->bi_end_io
= f2fs_end_io_write
;
648 trace_f2fs_do_submit_bio(sbi
->sb
, btype
, sync
, sbi
->bio
[btype
]);
650 if (type
== META_FLUSH
) {
651 DECLARE_COMPLETION_ONSTACK(wait
);
654 submit_bio(rw
, sbi
->bio
[btype
]);
655 wait_for_completion(&wait
);
658 submit_bio(rw
, sbi
->bio
[btype
]);
660 sbi
->bio
[btype
] = NULL
;
664 void f2fs_submit_bio(struct f2fs_sb_info
*sbi
, enum page_type type
, bool sync
)
666 down_write(&sbi
->bio_sem
);
667 do_submit_bio(sbi
, type
, sync
);
668 up_write(&sbi
->bio_sem
);
671 static void submit_write_page(struct f2fs_sb_info
*sbi
, struct page
*page
,
672 block_t blk_addr
, enum page_type type
)
674 struct block_device
*bdev
= sbi
->sb
->s_bdev
;
677 verify_block_addr(sbi
, blk_addr
);
679 down_write(&sbi
->bio_sem
);
681 inc_page_count(sbi
, F2FS_WRITEBACK
);
683 if (sbi
->bio
[type
] && sbi
->last_block_in_bio
[type
] != blk_addr
- 1)
684 do_submit_bio(sbi
, type
, false);
686 if (sbi
->bio
[type
] == NULL
) {
687 struct bio_private
*priv
;
689 priv
= kmalloc(sizeof(struct bio_private
), GFP_NOFS
);
695 bio_blocks
= MAX_BIO_BLOCKS(max_hw_blocks(sbi
));
696 sbi
->bio
[type
] = f2fs_bio_alloc(bdev
, bio_blocks
);
697 sbi
->bio
[type
]->bi_sector
= SECTOR_FROM_BLOCK(sbi
, blk_addr
);
698 sbi
->bio
[type
]->bi_private
= priv
;
700 * The end_io will be assigned at the sumbission phase.
701 * Until then, let bio_add_page() merge consecutive IOs as much
706 if (bio_add_page(sbi
->bio
[type
], page
, PAGE_CACHE_SIZE
, 0) <
708 do_submit_bio(sbi
, type
, false);
712 sbi
->last_block_in_bio
[type
] = blk_addr
;
714 up_write(&sbi
->bio_sem
);
715 trace_f2fs_submit_write_page(page
, blk_addr
, type
);
718 void f2fs_wait_on_page_writeback(struct page
*page
,
719 enum page_type type
, bool sync
)
721 struct f2fs_sb_info
*sbi
= F2FS_SB(page
->mapping
->host
->i_sb
);
722 if (PageWriteback(page
)) {
723 f2fs_submit_bio(sbi
, type
, sync
);
724 wait_on_page_writeback(page
);
728 static bool __has_curseg_space(struct f2fs_sb_info
*sbi
, int type
)
730 struct curseg_info
*curseg
= CURSEG_I(sbi
, type
);
731 if (curseg
->next_blkoff
< sbi
->blocks_per_seg
)
736 static int __get_segment_type_2(struct page
*page
, enum page_type p_type
)
739 return CURSEG_HOT_DATA
;
741 return CURSEG_HOT_NODE
;
744 static int __get_segment_type_4(struct page
*page
, enum page_type p_type
)
746 if (p_type
== DATA
) {
747 struct inode
*inode
= page
->mapping
->host
;
749 if (S_ISDIR(inode
->i_mode
))
750 return CURSEG_HOT_DATA
;
752 return CURSEG_COLD_DATA
;
754 if (IS_DNODE(page
) && !is_cold_node(page
))
755 return CURSEG_HOT_NODE
;
757 return CURSEG_COLD_NODE
;
761 static int __get_segment_type_6(struct page
*page
, enum page_type p_type
)
763 if (p_type
== DATA
) {
764 struct inode
*inode
= page
->mapping
->host
;
766 if (S_ISDIR(inode
->i_mode
))
767 return CURSEG_HOT_DATA
;
768 else if (is_cold_data(page
) || file_is_cold(inode
))
769 return CURSEG_COLD_DATA
;
771 return CURSEG_WARM_DATA
;
774 return is_cold_node(page
) ? CURSEG_WARM_NODE
:
777 return CURSEG_COLD_NODE
;
781 static int __get_segment_type(struct page
*page
, enum page_type p_type
)
783 struct f2fs_sb_info
*sbi
= F2FS_SB(page
->mapping
->host
->i_sb
);
784 switch (sbi
->active_logs
) {
786 return __get_segment_type_2(page
, p_type
);
788 return __get_segment_type_4(page
, p_type
);
790 /* NR_CURSEG_TYPE(6) logs by default */
791 BUG_ON(sbi
->active_logs
!= NR_CURSEG_TYPE
);
792 return __get_segment_type_6(page
, p_type
);
795 static void do_write_page(struct f2fs_sb_info
*sbi
, struct page
*page
,
796 block_t old_blkaddr
, block_t
*new_blkaddr
,
797 struct f2fs_summary
*sum
, enum page_type p_type
)
799 struct sit_info
*sit_i
= SIT_I(sbi
);
800 struct curseg_info
*curseg
;
801 unsigned int old_cursegno
;
804 type
= __get_segment_type(page
, p_type
);
805 curseg
= CURSEG_I(sbi
, type
);
807 mutex_lock(&curseg
->curseg_mutex
);
809 *new_blkaddr
= NEXT_FREE_BLKADDR(sbi
, curseg
);
810 old_cursegno
= curseg
->segno
;
813 * __add_sum_entry should be resided under the curseg_mutex
814 * because, this function updates a summary entry in the
815 * current summary block.
817 __add_sum_entry(sbi
, type
, sum
);
819 mutex_lock(&sit_i
->sentry_lock
);
820 __refresh_next_blkoff(sbi
, curseg
);
822 stat_inc_block_count(sbi
, curseg
);
825 * SIT information should be updated before segment allocation,
826 * since SSR needs latest valid block information.
828 refresh_sit_entry(sbi
, old_blkaddr
, *new_blkaddr
);
830 if (!__has_curseg_space(sbi
, type
))
831 sit_i
->s_ops
->allocate_segment(sbi
, type
, false);
833 locate_dirty_segment(sbi
, old_cursegno
);
834 locate_dirty_segment(sbi
, GET_SEGNO(sbi
, old_blkaddr
));
835 mutex_unlock(&sit_i
->sentry_lock
);
838 fill_node_footer_blkaddr(page
, NEXT_FREE_BLKADDR(sbi
, curseg
));
840 /* writeout dirty page into bdev */
841 submit_write_page(sbi
, page
, *new_blkaddr
, p_type
);
843 mutex_unlock(&curseg
->curseg_mutex
);
846 void write_meta_page(struct f2fs_sb_info
*sbi
, struct page
*page
)
848 set_page_writeback(page
);
849 submit_write_page(sbi
, page
, page
->index
, META
);
852 void write_node_page(struct f2fs_sb_info
*sbi
, struct page
*page
,
853 unsigned int nid
, block_t old_blkaddr
, block_t
*new_blkaddr
)
855 struct f2fs_summary sum
;
856 set_summary(&sum
, nid
, 0, 0);
857 do_write_page(sbi
, page
, old_blkaddr
, new_blkaddr
, &sum
, NODE
);
860 void write_data_page(struct inode
*inode
, struct page
*page
,
861 struct dnode_of_data
*dn
, block_t old_blkaddr
,
862 block_t
*new_blkaddr
)
864 struct f2fs_sb_info
*sbi
= F2FS_SB(inode
->i_sb
);
865 struct f2fs_summary sum
;
868 BUG_ON(old_blkaddr
== NULL_ADDR
);
869 get_node_info(sbi
, dn
->nid
, &ni
);
870 set_summary(&sum
, dn
->nid
, dn
->ofs_in_node
, ni
.version
);
872 do_write_page(sbi
, page
, old_blkaddr
,
873 new_blkaddr
, &sum
, DATA
);
876 void rewrite_data_page(struct f2fs_sb_info
*sbi
, struct page
*page
,
877 block_t old_blk_addr
)
879 submit_write_page(sbi
, page
, old_blk_addr
, DATA
);
882 void recover_data_page(struct f2fs_sb_info
*sbi
,
883 struct page
*page
, struct f2fs_summary
*sum
,
884 block_t old_blkaddr
, block_t new_blkaddr
)
886 struct sit_info
*sit_i
= SIT_I(sbi
);
887 struct curseg_info
*curseg
;
888 unsigned int segno
, old_cursegno
;
889 struct seg_entry
*se
;
892 segno
= GET_SEGNO(sbi
, new_blkaddr
);
893 se
= get_seg_entry(sbi
, segno
);
896 if (se
->valid_blocks
== 0 && !IS_CURSEG(sbi
, segno
)) {
897 if (old_blkaddr
== NULL_ADDR
)
898 type
= CURSEG_COLD_DATA
;
900 type
= CURSEG_WARM_DATA
;
902 curseg
= CURSEG_I(sbi
, type
);
904 mutex_lock(&curseg
->curseg_mutex
);
905 mutex_lock(&sit_i
->sentry_lock
);
907 old_cursegno
= curseg
->segno
;
909 /* change the current segment */
910 if (segno
!= curseg
->segno
) {
911 curseg
->next_segno
= segno
;
912 change_curseg(sbi
, type
, true);
915 curseg
->next_blkoff
= GET_SEGOFF_FROM_SEG0(sbi
, new_blkaddr
) &
916 (sbi
->blocks_per_seg
- 1);
917 __add_sum_entry(sbi
, type
, sum
);
919 refresh_sit_entry(sbi
, old_blkaddr
, new_blkaddr
);
921 locate_dirty_segment(sbi
, old_cursegno
);
922 locate_dirty_segment(sbi
, GET_SEGNO(sbi
, old_blkaddr
));
924 mutex_unlock(&sit_i
->sentry_lock
);
925 mutex_unlock(&curseg
->curseg_mutex
);
928 void rewrite_node_page(struct f2fs_sb_info
*sbi
,
929 struct page
*page
, struct f2fs_summary
*sum
,
930 block_t old_blkaddr
, block_t new_blkaddr
)
932 struct sit_info
*sit_i
= SIT_I(sbi
);
933 int type
= CURSEG_WARM_NODE
;
934 struct curseg_info
*curseg
;
935 unsigned int segno
, old_cursegno
;
936 block_t next_blkaddr
= next_blkaddr_of_node(page
);
937 unsigned int next_segno
= GET_SEGNO(sbi
, next_blkaddr
);
939 curseg
= CURSEG_I(sbi
, type
);
941 mutex_lock(&curseg
->curseg_mutex
);
942 mutex_lock(&sit_i
->sentry_lock
);
944 segno
= GET_SEGNO(sbi
, new_blkaddr
);
945 old_cursegno
= curseg
->segno
;
947 /* change the current segment */
948 if (segno
!= curseg
->segno
) {
949 curseg
->next_segno
= segno
;
950 change_curseg(sbi
, type
, true);
952 curseg
->next_blkoff
= GET_SEGOFF_FROM_SEG0(sbi
, new_blkaddr
) &
953 (sbi
->blocks_per_seg
- 1);
954 __add_sum_entry(sbi
, type
, sum
);
956 /* change the current log to the next block addr in advance */
957 if (next_segno
!= segno
) {
958 curseg
->next_segno
= next_segno
;
959 change_curseg(sbi
, type
, true);
961 curseg
->next_blkoff
= GET_SEGOFF_FROM_SEG0(sbi
, next_blkaddr
) &
962 (sbi
->blocks_per_seg
- 1);
964 /* rewrite node page */
965 set_page_writeback(page
);
966 submit_write_page(sbi
, page
, new_blkaddr
, NODE
);
967 f2fs_submit_bio(sbi
, NODE
, true);
968 refresh_sit_entry(sbi
, old_blkaddr
, new_blkaddr
);
970 locate_dirty_segment(sbi
, old_cursegno
);
971 locate_dirty_segment(sbi
, GET_SEGNO(sbi
, old_blkaddr
));
973 mutex_unlock(&sit_i
->sentry_lock
);
974 mutex_unlock(&curseg
->curseg_mutex
);
977 static int read_compacted_summaries(struct f2fs_sb_info
*sbi
)
979 struct f2fs_checkpoint
*ckpt
= F2FS_CKPT(sbi
);
980 struct curseg_info
*seg_i
;
981 unsigned char *kaddr
;
986 start
= start_sum_block(sbi
);
988 page
= get_meta_page(sbi
, start
++);
989 kaddr
= (unsigned char *)page_address(page
);
991 /* Step 1: restore nat cache */
992 seg_i
= CURSEG_I(sbi
, CURSEG_HOT_DATA
);
993 memcpy(&seg_i
->sum_blk
->n_nats
, kaddr
, SUM_JOURNAL_SIZE
);
995 /* Step 2: restore sit cache */
996 seg_i
= CURSEG_I(sbi
, CURSEG_COLD_DATA
);
997 memcpy(&seg_i
->sum_blk
->n_sits
, kaddr
+ SUM_JOURNAL_SIZE
,
999 offset
= 2 * SUM_JOURNAL_SIZE
;
1001 /* Step 3: restore summary entries */
1002 for (i
= CURSEG_HOT_DATA
; i
<= CURSEG_COLD_DATA
; i
++) {
1003 unsigned short blk_off
;
1006 seg_i
= CURSEG_I(sbi
, i
);
1007 segno
= le32_to_cpu(ckpt
->cur_data_segno
[i
]);
1008 blk_off
= le16_to_cpu(ckpt
->cur_data_blkoff
[i
]);
1009 seg_i
->next_segno
= segno
;
1010 reset_curseg(sbi
, i
, 0);
1011 seg_i
->alloc_type
= ckpt
->alloc_type
[i
];
1012 seg_i
->next_blkoff
= blk_off
;
1014 if (seg_i
->alloc_type
== SSR
)
1015 blk_off
= sbi
->blocks_per_seg
;
1017 for (j
= 0; j
< blk_off
; j
++) {
1018 struct f2fs_summary
*s
;
1019 s
= (struct f2fs_summary
*)(kaddr
+ offset
);
1020 seg_i
->sum_blk
->entries
[j
] = *s
;
1021 offset
+= SUMMARY_SIZE
;
1022 if (offset
+ SUMMARY_SIZE
<= PAGE_CACHE_SIZE
-
1026 f2fs_put_page(page
, 1);
1029 page
= get_meta_page(sbi
, start
++);
1030 kaddr
= (unsigned char *)page_address(page
);
1034 f2fs_put_page(page
, 1);
1038 static int read_normal_summaries(struct f2fs_sb_info
*sbi
, int type
)
1040 struct f2fs_checkpoint
*ckpt
= F2FS_CKPT(sbi
);
1041 struct f2fs_summary_block
*sum
;
1042 struct curseg_info
*curseg
;
1044 unsigned short blk_off
;
1045 unsigned int segno
= 0;
1046 block_t blk_addr
= 0;
1048 /* get segment number and block addr */
1049 if (IS_DATASEG(type
)) {
1050 segno
= le32_to_cpu(ckpt
->cur_data_segno
[type
]);
1051 blk_off
= le16_to_cpu(ckpt
->cur_data_blkoff
[type
-
1053 if (is_set_ckpt_flags(ckpt
, CP_UMOUNT_FLAG
))
1054 blk_addr
= sum_blk_addr(sbi
, NR_CURSEG_TYPE
, type
);
1056 blk_addr
= sum_blk_addr(sbi
, NR_CURSEG_DATA_TYPE
, type
);
1058 segno
= le32_to_cpu(ckpt
->cur_node_segno
[type
-
1060 blk_off
= le16_to_cpu(ckpt
->cur_node_blkoff
[type
-
1062 if (is_set_ckpt_flags(ckpt
, CP_UMOUNT_FLAG
))
1063 blk_addr
= sum_blk_addr(sbi
, NR_CURSEG_NODE_TYPE
,
1064 type
- CURSEG_HOT_NODE
);
1066 blk_addr
= GET_SUM_BLOCK(sbi
, segno
);
1069 new = get_meta_page(sbi
, blk_addr
);
1070 sum
= (struct f2fs_summary_block
*)page_address(new);
1072 if (IS_NODESEG(type
)) {
1073 if (is_set_ckpt_flags(ckpt
, CP_UMOUNT_FLAG
)) {
1074 struct f2fs_summary
*ns
= &sum
->entries
[0];
1076 for (i
= 0; i
< sbi
->blocks_per_seg
; i
++, ns
++) {
1078 ns
->ofs_in_node
= 0;
1081 if (restore_node_summary(sbi
, segno
, sum
)) {
1082 f2fs_put_page(new, 1);
1088 /* set uncompleted segment to curseg */
1089 curseg
= CURSEG_I(sbi
, type
);
1090 mutex_lock(&curseg
->curseg_mutex
);
1091 memcpy(curseg
->sum_blk
, sum
, PAGE_CACHE_SIZE
);
1092 curseg
->next_segno
= segno
;
1093 reset_curseg(sbi
, type
, 0);
1094 curseg
->alloc_type
= ckpt
->alloc_type
[type
];
1095 curseg
->next_blkoff
= blk_off
;
1096 mutex_unlock(&curseg
->curseg_mutex
);
1097 f2fs_put_page(new, 1);
1101 static int restore_curseg_summaries(struct f2fs_sb_info
*sbi
)
1103 int type
= CURSEG_HOT_DATA
;
1105 if (is_set_ckpt_flags(F2FS_CKPT(sbi
), CP_COMPACT_SUM_FLAG
)) {
1106 /* restore for compacted data summary */
1107 if (read_compacted_summaries(sbi
))
1109 type
= CURSEG_HOT_NODE
;
1112 for (; type
<= CURSEG_COLD_NODE
; type
++)
1113 if (read_normal_summaries(sbi
, type
))
1118 static void write_compacted_summaries(struct f2fs_sb_info
*sbi
, block_t blkaddr
)
1121 unsigned char *kaddr
;
1122 struct f2fs_summary
*summary
;
1123 struct curseg_info
*seg_i
;
1124 int written_size
= 0;
1127 page
= grab_meta_page(sbi
, blkaddr
++);
1128 kaddr
= (unsigned char *)page_address(page
);
1130 /* Step 1: write nat cache */
1131 seg_i
= CURSEG_I(sbi
, CURSEG_HOT_DATA
);
1132 memcpy(kaddr
, &seg_i
->sum_blk
->n_nats
, SUM_JOURNAL_SIZE
);
1133 written_size
+= SUM_JOURNAL_SIZE
;
1135 /* Step 2: write sit cache */
1136 seg_i
= CURSEG_I(sbi
, CURSEG_COLD_DATA
);
1137 memcpy(kaddr
+ written_size
, &seg_i
->sum_blk
->n_sits
,
1139 written_size
+= SUM_JOURNAL_SIZE
;
1141 /* Step 3: write summary entries */
1142 for (i
= CURSEG_HOT_DATA
; i
<= CURSEG_COLD_DATA
; i
++) {
1143 unsigned short blkoff
;
1144 seg_i
= CURSEG_I(sbi
, i
);
1145 if (sbi
->ckpt
->alloc_type
[i
] == SSR
)
1146 blkoff
= sbi
->blocks_per_seg
;
1148 blkoff
= curseg_blkoff(sbi
, i
);
1150 for (j
= 0; j
< blkoff
; j
++) {
1152 page
= grab_meta_page(sbi
, blkaddr
++);
1153 kaddr
= (unsigned char *)page_address(page
);
1156 summary
= (struct f2fs_summary
*)(kaddr
+ written_size
);
1157 *summary
= seg_i
->sum_blk
->entries
[j
];
1158 written_size
+= SUMMARY_SIZE
;
1160 if (written_size
+ SUMMARY_SIZE
<= PAGE_CACHE_SIZE
-
1164 set_page_dirty(page
);
1165 f2fs_put_page(page
, 1);
1170 set_page_dirty(page
);
1171 f2fs_put_page(page
, 1);
1175 static void write_normal_summaries(struct f2fs_sb_info
*sbi
,
1176 block_t blkaddr
, int type
)
1179 if (IS_DATASEG(type
))
1180 end
= type
+ NR_CURSEG_DATA_TYPE
;
1182 end
= type
+ NR_CURSEG_NODE_TYPE
;
1184 for (i
= type
; i
< end
; i
++) {
1185 struct curseg_info
*sum
= CURSEG_I(sbi
, i
);
1186 mutex_lock(&sum
->curseg_mutex
);
1187 write_sum_page(sbi
, sum
->sum_blk
, blkaddr
+ (i
- type
));
1188 mutex_unlock(&sum
->curseg_mutex
);
1192 void write_data_summaries(struct f2fs_sb_info
*sbi
, block_t start_blk
)
1194 if (is_set_ckpt_flags(F2FS_CKPT(sbi
), CP_COMPACT_SUM_FLAG
))
1195 write_compacted_summaries(sbi
, start_blk
);
1197 write_normal_summaries(sbi
, start_blk
, CURSEG_HOT_DATA
);
1200 void write_node_summaries(struct f2fs_sb_info
*sbi
, block_t start_blk
)
1202 if (is_set_ckpt_flags(F2FS_CKPT(sbi
), CP_UMOUNT_FLAG
))
1203 write_normal_summaries(sbi
, start_blk
, CURSEG_HOT_NODE
);
1206 int lookup_journal_in_cursum(struct f2fs_summary_block
*sum
, int type
,
1207 unsigned int val
, int alloc
)
1211 if (type
== NAT_JOURNAL
) {
1212 for (i
= 0; i
< nats_in_cursum(sum
); i
++) {
1213 if (le32_to_cpu(nid_in_journal(sum
, i
)) == val
)
1216 if (alloc
&& nats_in_cursum(sum
) < NAT_JOURNAL_ENTRIES
)
1217 return update_nats_in_cursum(sum
, 1);
1218 } else if (type
== SIT_JOURNAL
) {
1219 for (i
= 0; i
< sits_in_cursum(sum
); i
++)
1220 if (le32_to_cpu(segno_in_journal(sum
, i
)) == val
)
1222 if (alloc
&& sits_in_cursum(sum
) < SIT_JOURNAL_ENTRIES
)
1223 return update_sits_in_cursum(sum
, 1);
1228 static struct page
*get_current_sit_page(struct f2fs_sb_info
*sbi
,
1231 struct sit_info
*sit_i
= SIT_I(sbi
);
1232 unsigned int offset
= SIT_BLOCK_OFFSET(sit_i
, segno
);
1233 block_t blk_addr
= sit_i
->sit_base_addr
+ offset
;
1235 check_seg_range(sbi
, segno
);
1237 /* calculate sit block address */
1238 if (f2fs_test_bit(offset
, sit_i
->sit_bitmap
))
1239 blk_addr
+= sit_i
->sit_blocks
;
1241 return get_meta_page(sbi
, blk_addr
);
1244 static struct page
*get_next_sit_page(struct f2fs_sb_info
*sbi
,
1247 struct sit_info
*sit_i
= SIT_I(sbi
);
1248 struct page
*src_page
, *dst_page
;
1249 pgoff_t src_off
, dst_off
;
1250 void *src_addr
, *dst_addr
;
1252 src_off
= current_sit_addr(sbi
, start
);
1253 dst_off
= next_sit_addr(sbi
, src_off
);
1255 /* get current sit block page without lock */
1256 src_page
= get_meta_page(sbi
, src_off
);
1257 dst_page
= grab_meta_page(sbi
, dst_off
);
1258 BUG_ON(PageDirty(src_page
));
1260 src_addr
= page_address(src_page
);
1261 dst_addr
= page_address(dst_page
);
1262 memcpy(dst_addr
, src_addr
, PAGE_CACHE_SIZE
);
1264 set_page_dirty(dst_page
);
1265 f2fs_put_page(src_page
, 1);
1267 set_to_next_sit(sit_i
, start
);
1272 static bool flush_sits_in_journal(struct f2fs_sb_info
*sbi
)
1274 struct curseg_info
*curseg
= CURSEG_I(sbi
, CURSEG_COLD_DATA
);
1275 struct f2fs_summary_block
*sum
= curseg
->sum_blk
;
1279 * If the journal area in the current summary is full of sit entries,
1280 * all the sit entries will be flushed. Otherwise the sit entries
1281 * are not able to replace with newly hot sit entries.
1283 if (sits_in_cursum(sum
) >= SIT_JOURNAL_ENTRIES
) {
1284 for (i
= sits_in_cursum(sum
) - 1; i
>= 0; i
--) {
1286 segno
= le32_to_cpu(segno_in_journal(sum
, i
));
1287 __mark_sit_entry_dirty(sbi
, segno
);
1289 update_sits_in_cursum(sum
, -sits_in_cursum(sum
));
1296 * CP calls this function, which flushes SIT entries including sit_journal,
1297 * and moves prefree segs to free segs.
1299 void flush_sit_entries(struct f2fs_sb_info
*sbi
)
1301 struct sit_info
*sit_i
= SIT_I(sbi
);
1302 unsigned long *bitmap
= sit_i
->dirty_sentries_bitmap
;
1303 struct curseg_info
*curseg
= CURSEG_I(sbi
, CURSEG_COLD_DATA
);
1304 struct f2fs_summary_block
*sum
= curseg
->sum_blk
;
1305 unsigned long nsegs
= TOTAL_SEGS(sbi
);
1306 struct page
*page
= NULL
;
1307 struct f2fs_sit_block
*raw_sit
= NULL
;
1308 unsigned int start
= 0, end
= 0;
1309 unsigned int segno
= -1;
1312 mutex_lock(&curseg
->curseg_mutex
);
1313 mutex_lock(&sit_i
->sentry_lock
);
1316 * "flushed" indicates whether sit entries in journal are flushed
1317 * to the SIT area or not.
1319 flushed
= flush_sits_in_journal(sbi
);
1321 while ((segno
= find_next_bit(bitmap
, nsegs
, segno
+ 1)) < nsegs
) {
1322 struct seg_entry
*se
= get_seg_entry(sbi
, segno
);
1323 int sit_offset
, offset
;
1325 sit_offset
= SIT_ENTRY_OFFSET(sit_i
, segno
);
1330 offset
= lookup_journal_in_cursum(sum
, SIT_JOURNAL
, segno
, 1);
1332 segno_in_journal(sum
, offset
) = cpu_to_le32(segno
);
1333 seg_info_to_raw_sit(se
, &sit_in_journal(sum
, offset
));
1337 if (!page
|| (start
> segno
) || (segno
> end
)) {
1339 f2fs_put_page(page
, 1);
1343 start
= START_SEGNO(sit_i
, segno
);
1344 end
= start
+ SIT_ENTRY_PER_BLOCK
- 1;
1346 /* read sit block that will be updated */
1347 page
= get_next_sit_page(sbi
, start
);
1348 raw_sit
= page_address(page
);
1351 /* udpate entry in SIT block */
1352 seg_info_to_raw_sit(se
, &raw_sit
->entries
[sit_offset
]);
1354 __clear_bit(segno
, bitmap
);
1355 sit_i
->dirty_sentries
--;
1357 mutex_unlock(&sit_i
->sentry_lock
);
1358 mutex_unlock(&curseg
->curseg_mutex
);
1360 /* writeout last modified SIT block */
1361 f2fs_put_page(page
, 1);
1363 set_prefree_as_free_segments(sbi
);
1366 static int build_sit_info(struct f2fs_sb_info
*sbi
)
1368 struct f2fs_super_block
*raw_super
= F2FS_RAW_SUPER(sbi
);
1369 struct f2fs_checkpoint
*ckpt
= F2FS_CKPT(sbi
);
1370 struct sit_info
*sit_i
;
1371 unsigned int sit_segs
, start
;
1372 char *src_bitmap
, *dst_bitmap
;
1373 unsigned int bitmap_size
;
1375 /* allocate memory for SIT information */
1376 sit_i
= kzalloc(sizeof(struct sit_info
), GFP_KERNEL
);
1380 SM_I(sbi
)->sit_info
= sit_i
;
1382 sit_i
->sentries
= vzalloc(TOTAL_SEGS(sbi
) * sizeof(struct seg_entry
));
1383 if (!sit_i
->sentries
)
1386 bitmap_size
= f2fs_bitmap_size(TOTAL_SEGS(sbi
));
1387 sit_i
->dirty_sentries_bitmap
= kzalloc(bitmap_size
, GFP_KERNEL
);
1388 if (!sit_i
->dirty_sentries_bitmap
)
1391 for (start
= 0; start
< TOTAL_SEGS(sbi
); start
++) {
1392 sit_i
->sentries
[start
].cur_valid_map
1393 = kzalloc(SIT_VBLOCK_MAP_SIZE
, GFP_KERNEL
);
1394 sit_i
->sentries
[start
].ckpt_valid_map
1395 = kzalloc(SIT_VBLOCK_MAP_SIZE
, GFP_KERNEL
);
1396 if (!sit_i
->sentries
[start
].cur_valid_map
1397 || !sit_i
->sentries
[start
].ckpt_valid_map
)
1401 if (sbi
->segs_per_sec
> 1) {
1402 sit_i
->sec_entries
= vzalloc(TOTAL_SECS(sbi
) *
1403 sizeof(struct sec_entry
));
1404 if (!sit_i
->sec_entries
)
1408 /* get information related with SIT */
1409 sit_segs
= le32_to_cpu(raw_super
->segment_count_sit
) >> 1;
1411 /* setup SIT bitmap from ckeckpoint pack */
1412 bitmap_size
= __bitmap_size(sbi
, SIT_BITMAP
);
1413 src_bitmap
= __bitmap_ptr(sbi
, SIT_BITMAP
);
1415 dst_bitmap
= kmemdup(src_bitmap
, bitmap_size
, GFP_KERNEL
);
1419 /* init SIT information */
1420 sit_i
->s_ops
= &default_salloc_ops
;
1422 sit_i
->sit_base_addr
= le32_to_cpu(raw_super
->sit_blkaddr
);
1423 sit_i
->sit_blocks
= sit_segs
<< sbi
->log_blocks_per_seg
;
1424 sit_i
->written_valid_blocks
= le64_to_cpu(ckpt
->valid_block_count
);
1425 sit_i
->sit_bitmap
= dst_bitmap
;
1426 sit_i
->bitmap_size
= bitmap_size
;
1427 sit_i
->dirty_sentries
= 0;
1428 sit_i
->sents_per_block
= SIT_ENTRY_PER_BLOCK
;
1429 sit_i
->elapsed_time
= le64_to_cpu(sbi
->ckpt
->elapsed_time
);
1430 sit_i
->mounted_time
= CURRENT_TIME_SEC
.tv_sec
;
1431 mutex_init(&sit_i
->sentry_lock
);
1435 static int build_free_segmap(struct f2fs_sb_info
*sbi
)
1437 struct f2fs_sm_info
*sm_info
= SM_I(sbi
);
1438 struct free_segmap_info
*free_i
;
1439 unsigned int bitmap_size
, sec_bitmap_size
;
1441 /* allocate memory for free segmap information */
1442 free_i
= kzalloc(sizeof(struct free_segmap_info
), GFP_KERNEL
);
1446 SM_I(sbi
)->free_info
= free_i
;
1448 bitmap_size
= f2fs_bitmap_size(TOTAL_SEGS(sbi
));
1449 free_i
->free_segmap
= kmalloc(bitmap_size
, GFP_KERNEL
);
1450 if (!free_i
->free_segmap
)
1453 sec_bitmap_size
= f2fs_bitmap_size(TOTAL_SECS(sbi
));
1454 free_i
->free_secmap
= kmalloc(sec_bitmap_size
, GFP_KERNEL
);
1455 if (!free_i
->free_secmap
)
1458 /* set all segments as dirty temporarily */
1459 memset(free_i
->free_segmap
, 0xff, bitmap_size
);
1460 memset(free_i
->free_secmap
, 0xff, sec_bitmap_size
);
1462 /* init free segmap information */
1463 free_i
->start_segno
=
1464 (unsigned int) GET_SEGNO_FROM_SEG0(sbi
, sm_info
->main_blkaddr
);
1465 free_i
->free_segments
= 0;
1466 free_i
->free_sections
= 0;
1467 rwlock_init(&free_i
->segmap_lock
);
1471 static int build_curseg(struct f2fs_sb_info
*sbi
)
1473 struct curseg_info
*array
;
1476 array
= kzalloc(sizeof(*array
) * NR_CURSEG_TYPE
, GFP_KERNEL
);
1480 SM_I(sbi
)->curseg_array
= array
;
1482 for (i
= 0; i
< NR_CURSEG_TYPE
; i
++) {
1483 mutex_init(&array
[i
].curseg_mutex
);
1484 array
[i
].sum_blk
= kzalloc(PAGE_CACHE_SIZE
, GFP_KERNEL
);
1485 if (!array
[i
].sum_blk
)
1487 array
[i
].segno
= NULL_SEGNO
;
1488 array
[i
].next_blkoff
= 0;
1490 return restore_curseg_summaries(sbi
);
1493 static void build_sit_entries(struct f2fs_sb_info
*sbi
)
1495 struct sit_info
*sit_i
= SIT_I(sbi
);
1496 struct curseg_info
*curseg
= CURSEG_I(sbi
, CURSEG_COLD_DATA
);
1497 struct f2fs_summary_block
*sum
= curseg
->sum_blk
;
1500 for (start
= 0; start
< TOTAL_SEGS(sbi
); start
++) {
1501 struct seg_entry
*se
= &sit_i
->sentries
[start
];
1502 struct f2fs_sit_block
*sit_blk
;
1503 struct f2fs_sit_entry sit
;
1507 mutex_lock(&curseg
->curseg_mutex
);
1508 for (i
= 0; i
< sits_in_cursum(sum
); i
++) {
1509 if (le32_to_cpu(segno_in_journal(sum
, i
)) == start
) {
1510 sit
= sit_in_journal(sum
, i
);
1511 mutex_unlock(&curseg
->curseg_mutex
);
1515 mutex_unlock(&curseg
->curseg_mutex
);
1516 page
= get_current_sit_page(sbi
, start
);
1517 sit_blk
= (struct f2fs_sit_block
*)page_address(page
);
1518 sit
= sit_blk
->entries
[SIT_ENTRY_OFFSET(sit_i
, start
)];
1519 f2fs_put_page(page
, 1);
1521 check_block_count(sbi
, start
, &sit
);
1522 seg_info_from_raw_sit(se
, &sit
);
1523 if (sbi
->segs_per_sec
> 1) {
1524 struct sec_entry
*e
= get_sec_entry(sbi
, start
);
1525 e
->valid_blocks
+= se
->valid_blocks
;
1530 static void init_free_segmap(struct f2fs_sb_info
*sbi
)
1535 for (start
= 0; start
< TOTAL_SEGS(sbi
); start
++) {
1536 struct seg_entry
*sentry
= get_seg_entry(sbi
, start
);
1537 if (!sentry
->valid_blocks
)
1538 __set_free(sbi
, start
);
1541 /* set use the current segments */
1542 for (type
= CURSEG_HOT_DATA
; type
<= CURSEG_COLD_NODE
; type
++) {
1543 struct curseg_info
*curseg_t
= CURSEG_I(sbi
, type
);
1544 __set_test_and_inuse(sbi
, curseg_t
->segno
);
1548 static void init_dirty_segmap(struct f2fs_sb_info
*sbi
)
1550 struct dirty_seglist_info
*dirty_i
= DIRTY_I(sbi
);
1551 struct free_segmap_info
*free_i
= FREE_I(sbi
);
1552 unsigned int segno
= 0, offset
= 0, total_segs
= TOTAL_SEGS(sbi
);
1553 unsigned short valid_blocks
;
1556 /* find dirty segment based on free segmap */
1557 segno
= find_next_inuse(free_i
, total_segs
, offset
);
1558 if (segno
>= total_segs
)
1561 valid_blocks
= get_valid_blocks(sbi
, segno
, 0);
1562 if (valid_blocks
>= sbi
->blocks_per_seg
|| !valid_blocks
)
1564 mutex_lock(&dirty_i
->seglist_lock
);
1565 __locate_dirty_segment(sbi
, segno
, DIRTY
);
1566 mutex_unlock(&dirty_i
->seglist_lock
);
1570 static int init_victim_secmap(struct f2fs_sb_info
*sbi
)
1572 struct dirty_seglist_info
*dirty_i
= DIRTY_I(sbi
);
1573 unsigned int bitmap_size
= f2fs_bitmap_size(TOTAL_SECS(sbi
));
1575 dirty_i
->victim_secmap
= kzalloc(bitmap_size
, GFP_KERNEL
);
1576 if (!dirty_i
->victim_secmap
)
1581 static int build_dirty_segmap(struct f2fs_sb_info
*sbi
)
1583 struct dirty_seglist_info
*dirty_i
;
1584 unsigned int bitmap_size
, i
;
1586 /* allocate memory for dirty segments list information */
1587 dirty_i
= kzalloc(sizeof(struct dirty_seglist_info
), GFP_KERNEL
);
1591 SM_I(sbi
)->dirty_info
= dirty_i
;
1592 mutex_init(&dirty_i
->seglist_lock
);
1594 bitmap_size
= f2fs_bitmap_size(TOTAL_SEGS(sbi
));
1596 for (i
= 0; i
< NR_DIRTY_TYPE
; i
++) {
1597 dirty_i
->dirty_segmap
[i
] = kzalloc(bitmap_size
, GFP_KERNEL
);
1598 if (!dirty_i
->dirty_segmap
[i
])
1602 init_dirty_segmap(sbi
);
1603 return init_victim_secmap(sbi
);
1607 * Update min, max modified time for cost-benefit GC algorithm
1609 static void init_min_max_mtime(struct f2fs_sb_info
*sbi
)
1611 struct sit_info
*sit_i
= SIT_I(sbi
);
1614 mutex_lock(&sit_i
->sentry_lock
);
1616 sit_i
->min_mtime
= LLONG_MAX
;
1618 for (segno
= 0; segno
< TOTAL_SEGS(sbi
); segno
+= sbi
->segs_per_sec
) {
1620 unsigned long long mtime
= 0;
1622 for (i
= 0; i
< sbi
->segs_per_sec
; i
++)
1623 mtime
+= get_seg_entry(sbi
, segno
+ i
)->mtime
;
1625 mtime
= div_u64(mtime
, sbi
->segs_per_sec
);
1627 if (sit_i
->min_mtime
> mtime
)
1628 sit_i
->min_mtime
= mtime
;
1630 sit_i
->max_mtime
= get_mtime(sbi
);
1631 mutex_unlock(&sit_i
->sentry_lock
);
1634 int build_segment_manager(struct f2fs_sb_info
*sbi
)
1636 struct f2fs_super_block
*raw_super
= F2FS_RAW_SUPER(sbi
);
1637 struct f2fs_checkpoint
*ckpt
= F2FS_CKPT(sbi
);
1638 struct f2fs_sm_info
*sm_info
;
1641 sm_info
= kzalloc(sizeof(struct f2fs_sm_info
), GFP_KERNEL
);
1646 sbi
->sm_info
= sm_info
;
1647 INIT_LIST_HEAD(&sm_info
->wblist_head
);
1648 spin_lock_init(&sm_info
->wblist_lock
);
1649 sm_info
->seg0_blkaddr
= le32_to_cpu(raw_super
->segment0_blkaddr
);
1650 sm_info
->main_blkaddr
= le32_to_cpu(raw_super
->main_blkaddr
);
1651 sm_info
->segment_count
= le32_to_cpu(raw_super
->segment_count
);
1652 sm_info
->reserved_segments
= le32_to_cpu(ckpt
->rsvd_segment_count
);
1653 sm_info
->ovp_segments
= le32_to_cpu(ckpt
->overprov_segment_count
);
1654 sm_info
->main_segments
= le32_to_cpu(raw_super
->segment_count_main
);
1655 sm_info
->ssa_blkaddr
= le32_to_cpu(raw_super
->ssa_blkaddr
);
1656 sm_info
->rec_prefree_segments
= DEF_RECLAIM_PREFREE_SEGMENTS
;
1658 err
= build_sit_info(sbi
);
1661 err
= build_free_segmap(sbi
);
1664 err
= build_curseg(sbi
);
1668 /* reinit free segmap based on SIT */
1669 build_sit_entries(sbi
);
1671 init_free_segmap(sbi
);
1672 err
= build_dirty_segmap(sbi
);
1676 init_min_max_mtime(sbi
);
1680 static void discard_dirty_segmap(struct f2fs_sb_info
*sbi
,
1681 enum dirty_type dirty_type
)
1683 struct dirty_seglist_info
*dirty_i
= DIRTY_I(sbi
);
1685 mutex_lock(&dirty_i
->seglist_lock
);
1686 kfree(dirty_i
->dirty_segmap
[dirty_type
]);
1687 dirty_i
->nr_dirty
[dirty_type
] = 0;
1688 mutex_unlock(&dirty_i
->seglist_lock
);
1691 static void destroy_victim_secmap(struct f2fs_sb_info
*sbi
)
1693 struct dirty_seglist_info
*dirty_i
= DIRTY_I(sbi
);
1694 kfree(dirty_i
->victim_secmap
);
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_secmap(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
;