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 enum dirty_type t
= DIRTY_HOT_DATA
;
54 dirty_type
= sentry
->type
;
56 if (!test_and_set_bit(segno
, dirty_i
->dirty_segmap
[dirty_type
]))
57 dirty_i
->nr_dirty
[dirty_type
]++;
59 /* Only one bitmap should be set */
60 for (; t
<= DIRTY_COLD_NODE
; t
++) {
63 if (test_and_clear_bit(segno
, dirty_i
->dirty_segmap
[t
]))
64 dirty_i
->nr_dirty
[t
]--;
69 static void __remove_dirty_segment(struct f2fs_sb_info
*sbi
, unsigned int segno
,
70 enum dirty_type dirty_type
)
72 struct dirty_seglist_info
*dirty_i
= DIRTY_I(sbi
);
74 if (test_and_clear_bit(segno
, dirty_i
->dirty_segmap
[dirty_type
]))
75 dirty_i
->nr_dirty
[dirty_type
]--;
77 if (dirty_type
== DIRTY
) {
78 enum dirty_type t
= DIRTY_HOT_DATA
;
80 /* clear all the bitmaps */
81 for (; t
<= DIRTY_COLD_NODE
; t
++)
82 if (test_and_clear_bit(segno
, dirty_i
->dirty_segmap
[t
]))
83 dirty_i
->nr_dirty
[t
]--;
85 if (get_valid_blocks(sbi
, segno
, sbi
->segs_per_sec
) == 0)
86 clear_bit(GET_SECNO(sbi
, segno
),
87 dirty_i
->victim_secmap
);
92 * Should not occur error such as -ENOMEM.
93 * Adding dirty entry into seglist is not critical operation.
94 * If a given segment is one of current working segments, it won't be added.
96 void locate_dirty_segment(struct f2fs_sb_info
*sbi
, unsigned int segno
)
98 struct dirty_seglist_info
*dirty_i
= DIRTY_I(sbi
);
99 unsigned short valid_blocks
;
101 if (segno
== NULL_SEGNO
|| IS_CURSEG(sbi
, segno
))
104 mutex_lock(&dirty_i
->seglist_lock
);
106 valid_blocks
= get_valid_blocks(sbi
, segno
, 0);
108 if (valid_blocks
== 0) {
109 __locate_dirty_segment(sbi
, segno
, PRE
);
110 __remove_dirty_segment(sbi
, segno
, DIRTY
);
111 } else if (valid_blocks
< sbi
->blocks_per_seg
) {
112 __locate_dirty_segment(sbi
, segno
, DIRTY
);
114 /* Recovery routine with SSR needs this */
115 __remove_dirty_segment(sbi
, segno
, DIRTY
);
118 mutex_unlock(&dirty_i
->seglist_lock
);
123 * Should call clear_prefree_segments after checkpoint is done.
125 static void set_prefree_as_free_segments(struct f2fs_sb_info
*sbi
)
127 struct dirty_seglist_info
*dirty_i
= DIRTY_I(sbi
);
128 unsigned int segno
, offset
= 0;
129 unsigned int total_segs
= TOTAL_SEGS(sbi
);
131 mutex_lock(&dirty_i
->seglist_lock
);
133 segno
= find_next_bit(dirty_i
->dirty_segmap
[PRE
], total_segs
,
135 if (segno
>= total_segs
)
137 __set_test_and_free(sbi
, segno
);
140 mutex_unlock(&dirty_i
->seglist_lock
);
143 void clear_prefree_segments(struct f2fs_sb_info
*sbi
)
145 struct dirty_seglist_info
*dirty_i
= DIRTY_I(sbi
);
146 unsigned int segno
, offset
= 0;
147 unsigned int total_segs
= TOTAL_SEGS(sbi
);
149 mutex_lock(&dirty_i
->seglist_lock
);
151 segno
= find_next_bit(dirty_i
->dirty_segmap
[PRE
], total_segs
,
153 if (segno
>= total_segs
)
157 if (test_and_clear_bit(segno
, dirty_i
->dirty_segmap
[PRE
]))
158 dirty_i
->nr_dirty
[PRE
]--;
161 if (test_opt(sbi
, DISCARD
))
162 blkdev_issue_discard(sbi
->sb
->s_bdev
,
163 START_BLOCK(sbi
, segno
) <<
164 sbi
->log_sectors_per_block
,
165 1 << (sbi
->log_sectors_per_block
+
166 sbi
->log_blocks_per_seg
),
169 mutex_unlock(&dirty_i
->seglist_lock
);
172 static void __mark_sit_entry_dirty(struct f2fs_sb_info
*sbi
, unsigned int segno
)
174 struct sit_info
*sit_i
= SIT_I(sbi
);
175 if (!__test_and_set_bit(segno
, sit_i
->dirty_sentries_bitmap
))
176 sit_i
->dirty_sentries
++;
179 static void __set_sit_entry_type(struct f2fs_sb_info
*sbi
, int type
,
180 unsigned int segno
, int modified
)
182 struct seg_entry
*se
= get_seg_entry(sbi
, segno
);
185 __mark_sit_entry_dirty(sbi
, segno
);
188 static void update_sit_entry(struct f2fs_sb_info
*sbi
, block_t blkaddr
, int del
)
190 struct seg_entry
*se
;
191 unsigned int segno
, offset
;
192 long int new_vblocks
;
194 segno
= GET_SEGNO(sbi
, blkaddr
);
196 se
= get_seg_entry(sbi
, segno
);
197 new_vblocks
= se
->valid_blocks
+ del
;
198 offset
= GET_SEGOFF_FROM_SEG0(sbi
, blkaddr
) & (sbi
->blocks_per_seg
- 1);
200 BUG_ON((new_vblocks
>> (sizeof(unsigned short) << 3) ||
201 (new_vblocks
> sbi
->blocks_per_seg
)));
203 se
->valid_blocks
= new_vblocks
;
204 se
->mtime
= get_mtime(sbi
);
205 SIT_I(sbi
)->max_mtime
= se
->mtime
;
207 /* Update valid block bitmap */
209 if (f2fs_set_bit(offset
, se
->cur_valid_map
))
212 if (!f2fs_clear_bit(offset
, se
->cur_valid_map
))
215 if (!f2fs_test_bit(offset
, se
->ckpt_valid_map
))
216 se
->ckpt_valid_blocks
+= del
;
218 __mark_sit_entry_dirty(sbi
, segno
);
220 /* update total number of valid blocks to be written in ckpt area */
221 SIT_I(sbi
)->written_valid_blocks
+= del
;
223 if (sbi
->segs_per_sec
> 1)
224 get_sec_entry(sbi
, segno
)->valid_blocks
+= del
;
227 static void refresh_sit_entry(struct f2fs_sb_info
*sbi
,
228 block_t old_blkaddr
, block_t new_blkaddr
)
230 update_sit_entry(sbi
, new_blkaddr
, 1);
231 if (GET_SEGNO(sbi
, old_blkaddr
) != NULL_SEGNO
)
232 update_sit_entry(sbi
, old_blkaddr
, -1);
235 void invalidate_blocks(struct f2fs_sb_info
*sbi
, block_t addr
)
237 unsigned int segno
= GET_SEGNO(sbi
, addr
);
238 struct sit_info
*sit_i
= SIT_I(sbi
);
240 BUG_ON(addr
== NULL_ADDR
);
241 if (addr
== NEW_ADDR
)
244 /* add it into sit main buffer */
245 mutex_lock(&sit_i
->sentry_lock
);
247 update_sit_entry(sbi
, addr
, -1);
249 /* add it into dirty seglist */
250 locate_dirty_segment(sbi
, segno
);
252 mutex_unlock(&sit_i
->sentry_lock
);
256 * This function should be resided under the curseg_mutex lock
258 static void __add_sum_entry(struct f2fs_sb_info
*sbi
, int type
,
259 struct f2fs_summary
*sum
, unsigned short offset
)
261 struct curseg_info
*curseg
= CURSEG_I(sbi
, type
);
262 void *addr
= curseg
->sum_blk
;
263 addr
+= offset
* sizeof(struct f2fs_summary
);
264 memcpy(addr
, sum
, sizeof(struct f2fs_summary
));
269 * Calculate the number of current summary pages for writing
271 int npages_for_summary_flush(struct f2fs_sb_info
*sbi
)
273 int total_size_bytes
= 0;
274 int valid_sum_count
= 0;
277 for (i
= CURSEG_HOT_DATA
; i
<= CURSEG_COLD_DATA
; i
++) {
278 if (sbi
->ckpt
->alloc_type
[i
] == SSR
)
279 valid_sum_count
+= sbi
->blocks_per_seg
;
281 valid_sum_count
+= curseg_blkoff(sbi
, i
);
284 total_size_bytes
= valid_sum_count
* (SUMMARY_SIZE
+ 1)
285 + sizeof(struct nat_journal
) + 2
286 + sizeof(struct sit_journal
) + 2;
287 sum_space
= PAGE_CACHE_SIZE
- SUM_FOOTER_SIZE
;
288 if (total_size_bytes
< sum_space
)
290 else if (total_size_bytes
< 2 * sum_space
)
296 * Caller should put this summary page
298 struct page
*get_sum_page(struct f2fs_sb_info
*sbi
, unsigned int segno
)
300 return get_meta_page(sbi
, GET_SUM_BLOCK(sbi
, segno
));
303 static void write_sum_page(struct f2fs_sb_info
*sbi
,
304 struct f2fs_summary_block
*sum_blk
, block_t blk_addr
)
306 struct page
*page
= grab_meta_page(sbi
, blk_addr
);
307 void *kaddr
= page_address(page
);
308 memcpy(kaddr
, sum_blk
, PAGE_CACHE_SIZE
);
309 set_page_dirty(page
);
310 f2fs_put_page(page
, 1);
313 static unsigned int check_prefree_segments(struct f2fs_sb_info
*sbi
, int type
)
315 struct dirty_seglist_info
*dirty_i
= DIRTY_I(sbi
);
316 unsigned long *prefree_segmap
= dirty_i
->dirty_segmap
[PRE
];
318 unsigned int ofs
= 0;
321 * If there is not enough reserved sections,
322 * we should not reuse prefree segments.
324 if (has_not_enough_free_secs(sbi
, 0))
328 * NODE page should not reuse prefree segment,
329 * since those information is used for SPOR.
331 if (IS_NODESEG(type
))
334 segno
= find_next_bit(prefree_segmap
, TOTAL_SEGS(sbi
), ofs
);
335 ofs
+= sbi
->segs_per_sec
;
337 if (segno
< TOTAL_SEGS(sbi
)) {
340 /* skip intermediate segments in a section */
341 if (segno
% sbi
->segs_per_sec
)
344 /* skip if the section is currently used */
345 if (sec_usage_check(sbi
, GET_SECNO(sbi
, segno
)))
348 /* skip if whole section is not prefree */
349 for (i
= 1; i
< sbi
->segs_per_sec
; i
++)
350 if (!test_bit(segno
+ i
, prefree_segmap
))
353 /* skip if whole section was not free at the last checkpoint */
354 for (i
= 0; i
< sbi
->segs_per_sec
; i
++)
355 if (get_seg_entry(sbi
, segno
+ i
)->ckpt_valid_blocks
)
363 static int is_next_segment_free(struct f2fs_sb_info
*sbi
, int type
)
365 struct curseg_info
*curseg
= CURSEG_I(sbi
, type
);
366 unsigned int segno
= curseg
->segno
;
367 struct free_segmap_info
*free_i
= FREE_I(sbi
);
369 if (segno
+ 1 < TOTAL_SEGS(sbi
) && (segno
+ 1) % sbi
->segs_per_sec
)
370 return !test_bit(segno
+ 1, free_i
->free_segmap
);
375 * Find a new segment from the free segments bitmap to right order
376 * This function should be returned with success, otherwise BUG
378 static void get_new_segment(struct f2fs_sb_info
*sbi
,
379 unsigned int *newseg
, bool new_sec
, int dir
)
381 struct free_segmap_info
*free_i
= FREE_I(sbi
);
382 unsigned int segno
, secno
, zoneno
;
383 unsigned int total_zones
= TOTAL_SECS(sbi
) / sbi
->secs_per_zone
;
384 unsigned int hint
= *newseg
/ sbi
->segs_per_sec
;
385 unsigned int old_zoneno
= GET_ZONENO_FROM_SEGNO(sbi
, *newseg
);
386 unsigned int left_start
= hint
;
391 write_lock(&free_i
->segmap_lock
);
393 if (!new_sec
&& ((*newseg
+ 1) % sbi
->segs_per_sec
)) {
394 segno
= find_next_zero_bit(free_i
->free_segmap
,
395 TOTAL_SEGS(sbi
), *newseg
+ 1);
396 if (segno
- *newseg
< sbi
->segs_per_sec
-
397 (*newseg
% sbi
->segs_per_sec
))
401 secno
= find_next_zero_bit(free_i
->free_secmap
, TOTAL_SECS(sbi
), hint
);
402 if (secno
>= TOTAL_SECS(sbi
)) {
403 if (dir
== ALLOC_RIGHT
) {
404 secno
= find_next_zero_bit(free_i
->free_secmap
,
406 BUG_ON(secno
>= TOTAL_SECS(sbi
));
409 left_start
= hint
- 1;
415 while (test_bit(left_start
, free_i
->free_secmap
)) {
416 if (left_start
> 0) {
420 left_start
= find_next_zero_bit(free_i
->free_secmap
,
422 BUG_ON(left_start
>= TOTAL_SECS(sbi
));
428 segno
= secno
* sbi
->segs_per_sec
;
429 zoneno
= secno
/ sbi
->secs_per_zone
;
431 /* give up on finding another zone */
434 if (sbi
->secs_per_zone
== 1)
436 if (zoneno
== old_zoneno
)
438 if (dir
== ALLOC_LEFT
) {
439 if (!go_left
&& zoneno
+ 1 >= total_zones
)
441 if (go_left
&& zoneno
== 0)
444 for (i
= 0; i
< NR_CURSEG_TYPE
; i
++)
445 if (CURSEG_I(sbi
, i
)->zone
== zoneno
)
448 if (i
< NR_CURSEG_TYPE
) {
449 /* zone is in user, try another */
451 hint
= zoneno
* sbi
->secs_per_zone
- 1;
452 else if (zoneno
+ 1 >= total_zones
)
455 hint
= (zoneno
+ 1) * sbi
->secs_per_zone
;
457 goto find_other_zone
;
460 /* set it as dirty segment in free segmap */
461 BUG_ON(test_bit(segno
, free_i
->free_segmap
));
462 __set_inuse(sbi
, segno
);
464 write_unlock(&free_i
->segmap_lock
);
467 static void reset_curseg(struct f2fs_sb_info
*sbi
, int type
, int modified
)
469 struct curseg_info
*curseg
= CURSEG_I(sbi
, type
);
470 struct summary_footer
*sum_footer
;
472 curseg
->segno
= curseg
->next_segno
;
473 curseg
->zone
= GET_ZONENO_FROM_SEGNO(sbi
, curseg
->segno
);
474 curseg
->next_blkoff
= 0;
475 curseg
->next_segno
= NULL_SEGNO
;
477 sum_footer
= &(curseg
->sum_blk
->footer
);
478 memset(sum_footer
, 0, sizeof(struct summary_footer
));
479 if (IS_DATASEG(type
))
480 SET_SUM_TYPE(sum_footer
, SUM_TYPE_DATA
);
481 if (IS_NODESEG(type
))
482 SET_SUM_TYPE(sum_footer
, SUM_TYPE_NODE
);
483 __set_sit_entry_type(sbi
, type
, curseg
->segno
, modified
);
487 * Allocate a current working segment.
488 * This function always allocates a free segment in LFS manner.
490 static void new_curseg(struct f2fs_sb_info
*sbi
, int type
, bool new_sec
)
492 struct curseg_info
*curseg
= CURSEG_I(sbi
, type
);
493 unsigned int segno
= curseg
->segno
;
494 int dir
= ALLOC_LEFT
;
496 write_sum_page(sbi
, curseg
->sum_blk
,
497 GET_SUM_BLOCK(sbi
, curseg
->segno
));
498 if (type
== CURSEG_WARM_DATA
|| type
== CURSEG_COLD_DATA
)
501 if (test_opt(sbi
, NOHEAP
))
504 get_new_segment(sbi
, &segno
, new_sec
, dir
);
505 curseg
->next_segno
= segno
;
506 reset_curseg(sbi
, type
, 1);
507 curseg
->alloc_type
= LFS
;
510 static void __next_free_blkoff(struct f2fs_sb_info
*sbi
,
511 struct curseg_info
*seg
, block_t start
)
513 struct seg_entry
*se
= get_seg_entry(sbi
, seg
->segno
);
515 for (ofs
= start
; ofs
< sbi
->blocks_per_seg
; ofs
++) {
516 if (!f2fs_test_bit(ofs
, se
->ckpt_valid_map
)
517 && !f2fs_test_bit(ofs
, se
->cur_valid_map
))
520 seg
->next_blkoff
= ofs
;
524 * If a segment is written by LFS manner, next block offset is just obtained
525 * by increasing the current block offset. However, if a segment is written by
526 * SSR manner, next block offset obtained by calling __next_free_blkoff
528 static void __refresh_next_blkoff(struct f2fs_sb_info
*sbi
,
529 struct curseg_info
*seg
)
531 if (seg
->alloc_type
== SSR
)
532 __next_free_blkoff(sbi
, seg
, seg
->next_blkoff
+ 1);
538 * This function always allocates a used segment (from dirty seglist) by SSR
539 * manner, so it should recover the existing segment information of valid blocks
541 static void change_curseg(struct f2fs_sb_info
*sbi
, int type
, bool reuse
)
543 struct dirty_seglist_info
*dirty_i
= DIRTY_I(sbi
);
544 struct curseg_info
*curseg
= CURSEG_I(sbi
, type
);
545 unsigned int new_segno
= curseg
->next_segno
;
546 struct f2fs_summary_block
*sum_node
;
547 struct page
*sum_page
;
549 write_sum_page(sbi
, curseg
->sum_blk
,
550 GET_SUM_BLOCK(sbi
, curseg
->segno
));
551 __set_test_and_inuse(sbi
, new_segno
);
553 mutex_lock(&dirty_i
->seglist_lock
);
554 __remove_dirty_segment(sbi
, new_segno
, PRE
);
555 __remove_dirty_segment(sbi
, new_segno
, DIRTY
);
556 mutex_unlock(&dirty_i
->seglist_lock
);
558 reset_curseg(sbi
, type
, 1);
559 curseg
->alloc_type
= SSR
;
560 __next_free_blkoff(sbi
, curseg
, 0);
563 sum_page
= get_sum_page(sbi
, new_segno
);
564 sum_node
= (struct f2fs_summary_block
*)page_address(sum_page
);
565 memcpy(curseg
->sum_blk
, sum_node
, SUM_ENTRY_SIZE
);
566 f2fs_put_page(sum_page
, 1);
570 static int get_ssr_segment(struct f2fs_sb_info
*sbi
, int type
)
572 struct curseg_info
*curseg
= CURSEG_I(sbi
, type
);
573 const struct victim_selection
*v_ops
= DIRTY_I(sbi
)->v_ops
;
575 if (IS_NODESEG(type
) || !has_not_enough_free_secs(sbi
, 0))
576 return v_ops
->get_victim(sbi
,
577 &(curseg
)->next_segno
, BG_GC
, type
, SSR
);
579 /* For data segments, let's do SSR more intensively */
580 for (; type
>= CURSEG_HOT_DATA
; type
--)
581 if (v_ops
->get_victim(sbi
, &(curseg
)->next_segno
,
588 * flush out current segment and replace it with new segment
589 * This function should be returned with success, otherwise BUG
591 static void allocate_segment_by_default(struct f2fs_sb_info
*sbi
,
592 int type
, bool force
)
594 struct curseg_info
*curseg
= CURSEG_I(sbi
, type
);
597 new_curseg(sbi
, type
, true);
601 curseg
->next_segno
= check_prefree_segments(sbi
, type
);
603 if (curseg
->next_segno
!= NULL_SEGNO
)
604 change_curseg(sbi
, type
, false);
605 else if (type
== CURSEG_WARM_NODE
)
606 new_curseg(sbi
, type
, false);
607 else if (curseg
->alloc_type
== LFS
&& is_next_segment_free(sbi
, type
))
608 new_curseg(sbi
, type
, false);
609 else if (need_SSR(sbi
) && get_ssr_segment(sbi
, type
))
610 change_curseg(sbi
, type
, true);
612 new_curseg(sbi
, type
, false);
614 sbi
->segment_count
[curseg
->alloc_type
]++;
617 void allocate_new_segments(struct f2fs_sb_info
*sbi
)
619 struct curseg_info
*curseg
;
620 unsigned int old_curseg
;
623 for (i
= CURSEG_HOT_DATA
; i
<= CURSEG_COLD_DATA
; i
++) {
624 curseg
= CURSEG_I(sbi
, i
);
625 old_curseg
= curseg
->segno
;
626 SIT_I(sbi
)->s_ops
->allocate_segment(sbi
, i
, true);
627 locate_dirty_segment(sbi
, old_curseg
);
631 static const struct segment_allocation default_salloc_ops
= {
632 .allocate_segment
= allocate_segment_by_default
,
635 static void f2fs_end_io_write(struct bio
*bio
, int err
)
637 const int uptodate
= test_bit(BIO_UPTODATE
, &bio
->bi_flags
);
638 struct bio_vec
*bvec
= bio
->bi_io_vec
+ bio
->bi_vcnt
- 1;
639 struct bio_private
*p
= bio
->bi_private
;
642 struct page
*page
= bvec
->bv_page
;
644 if (--bvec
>= bio
->bi_io_vec
)
645 prefetchw(&bvec
->bv_page
->flags
);
649 set_bit(AS_EIO
, &page
->mapping
->flags
);
650 set_ckpt_flags(p
->sbi
->ckpt
, CP_ERROR_FLAG
);
651 p
->sbi
->sb
->s_flags
|= MS_RDONLY
;
653 end_page_writeback(page
);
654 dec_page_count(p
->sbi
, F2FS_WRITEBACK
);
655 } while (bvec
>= bio
->bi_io_vec
);
663 struct bio
*f2fs_bio_alloc(struct block_device
*bdev
, int npages
)
666 struct bio_private
*priv
;
668 priv
= kmalloc(sizeof(struct bio_private
), GFP_NOFS
);
674 /* No failure on bio allocation */
675 bio
= bio_alloc(GFP_NOIO
, npages
);
677 bio
->bi_private
= priv
;
681 static void do_submit_bio(struct f2fs_sb_info
*sbi
,
682 enum page_type type
, bool sync
)
684 int rw
= sync
? WRITE_SYNC
: WRITE
;
685 enum page_type btype
= type
> META
? META
: type
;
687 if (type
>= META_FLUSH
)
688 rw
= WRITE_FLUSH_FUA
;
690 if (sbi
->bio
[btype
]) {
691 struct bio_private
*p
= sbi
->bio
[btype
]->bi_private
;
693 sbi
->bio
[btype
]->bi_end_io
= f2fs_end_io_write
;
694 if (type
== META_FLUSH
) {
695 DECLARE_COMPLETION_ONSTACK(wait
);
698 submit_bio(rw
, sbi
->bio
[btype
]);
699 wait_for_completion(&wait
);
702 submit_bio(rw
, sbi
->bio
[btype
]);
704 sbi
->bio
[btype
] = NULL
;
708 void f2fs_submit_bio(struct f2fs_sb_info
*sbi
, enum page_type type
, bool sync
)
710 down_write(&sbi
->bio_sem
);
711 do_submit_bio(sbi
, type
, sync
);
712 up_write(&sbi
->bio_sem
);
715 static void submit_write_page(struct f2fs_sb_info
*sbi
, struct page
*page
,
716 block_t blk_addr
, enum page_type type
)
718 struct block_device
*bdev
= sbi
->sb
->s_bdev
;
720 verify_block_addr(sbi
, blk_addr
);
722 down_write(&sbi
->bio_sem
);
724 inc_page_count(sbi
, F2FS_WRITEBACK
);
726 if (sbi
->bio
[type
] && sbi
->last_block_in_bio
[type
] != blk_addr
- 1)
727 do_submit_bio(sbi
, type
, false);
729 if (sbi
->bio
[type
] == NULL
) {
730 sbi
->bio
[type
] = f2fs_bio_alloc(bdev
, bio_get_nr_vecs(bdev
));
731 sbi
->bio
[type
]->bi_sector
= SECTOR_FROM_BLOCK(sbi
, blk_addr
);
733 * The end_io will be assigned at the sumbission phase.
734 * Until then, let bio_add_page() merge consecutive IOs as much
739 if (bio_add_page(sbi
->bio
[type
], page
, PAGE_CACHE_SIZE
, 0) <
741 do_submit_bio(sbi
, type
, false);
745 sbi
->last_block_in_bio
[type
] = blk_addr
;
747 up_write(&sbi
->bio_sem
);
750 static bool __has_curseg_space(struct f2fs_sb_info
*sbi
, int type
)
752 struct curseg_info
*curseg
= CURSEG_I(sbi
, type
);
753 if (curseg
->next_blkoff
< sbi
->blocks_per_seg
)
758 static int __get_segment_type_2(struct page
*page
, enum page_type p_type
)
761 return CURSEG_HOT_DATA
;
763 return CURSEG_HOT_NODE
;
766 static int __get_segment_type_4(struct page
*page
, enum page_type p_type
)
768 if (p_type
== DATA
) {
769 struct inode
*inode
= page
->mapping
->host
;
771 if (S_ISDIR(inode
->i_mode
))
772 return CURSEG_HOT_DATA
;
774 return CURSEG_COLD_DATA
;
776 if (IS_DNODE(page
) && !is_cold_node(page
))
777 return CURSEG_HOT_NODE
;
779 return CURSEG_COLD_NODE
;
783 static int __get_segment_type_6(struct page
*page
, enum page_type p_type
)
785 if (p_type
== DATA
) {
786 struct inode
*inode
= page
->mapping
->host
;
788 if (S_ISDIR(inode
->i_mode
))
789 return CURSEG_HOT_DATA
;
790 else if (is_cold_data(page
) || is_cold_file(inode
))
791 return CURSEG_COLD_DATA
;
793 return CURSEG_WARM_DATA
;
796 return is_cold_node(page
) ? CURSEG_WARM_NODE
:
799 return CURSEG_COLD_NODE
;
803 static int __get_segment_type(struct page
*page
, enum page_type p_type
)
805 struct f2fs_sb_info
*sbi
= F2FS_SB(page
->mapping
->host
->i_sb
);
806 switch (sbi
->active_logs
) {
808 return __get_segment_type_2(page
, p_type
);
810 return __get_segment_type_4(page
, p_type
);
812 /* NR_CURSEG_TYPE(6) logs by default */
813 BUG_ON(sbi
->active_logs
!= NR_CURSEG_TYPE
);
814 return __get_segment_type_6(page
, p_type
);
817 static void do_write_page(struct f2fs_sb_info
*sbi
, struct page
*page
,
818 block_t old_blkaddr
, block_t
*new_blkaddr
,
819 struct f2fs_summary
*sum
, enum page_type p_type
)
821 struct sit_info
*sit_i
= SIT_I(sbi
);
822 struct curseg_info
*curseg
;
823 unsigned int old_cursegno
;
826 type
= __get_segment_type(page
, p_type
);
827 curseg
= CURSEG_I(sbi
, type
);
829 mutex_lock(&curseg
->curseg_mutex
);
831 *new_blkaddr
= NEXT_FREE_BLKADDR(sbi
, curseg
);
832 old_cursegno
= curseg
->segno
;
835 * __add_sum_entry should be resided under the curseg_mutex
836 * because, this function updates a summary entry in the
837 * current summary block.
839 __add_sum_entry(sbi
, type
, sum
, curseg
->next_blkoff
);
841 mutex_lock(&sit_i
->sentry_lock
);
842 __refresh_next_blkoff(sbi
, curseg
);
843 sbi
->block_count
[curseg
->alloc_type
]++;
846 * SIT information should be updated before segment allocation,
847 * since SSR needs latest valid block information.
849 refresh_sit_entry(sbi
, old_blkaddr
, *new_blkaddr
);
851 if (!__has_curseg_space(sbi
, type
))
852 sit_i
->s_ops
->allocate_segment(sbi
, type
, false);
854 locate_dirty_segment(sbi
, old_cursegno
);
855 locate_dirty_segment(sbi
, GET_SEGNO(sbi
, old_blkaddr
));
856 mutex_unlock(&sit_i
->sentry_lock
);
859 fill_node_footer_blkaddr(page
, NEXT_FREE_BLKADDR(sbi
, curseg
));
861 /* writeout dirty page into bdev */
862 submit_write_page(sbi
, page
, *new_blkaddr
, p_type
);
864 mutex_unlock(&curseg
->curseg_mutex
);
867 void write_meta_page(struct f2fs_sb_info
*sbi
, struct page
*page
)
869 set_page_writeback(page
);
870 submit_write_page(sbi
, page
, page
->index
, META
);
873 void write_node_page(struct f2fs_sb_info
*sbi
, struct page
*page
,
874 unsigned int nid
, block_t old_blkaddr
, block_t
*new_blkaddr
)
876 struct f2fs_summary sum
;
877 set_summary(&sum
, nid
, 0, 0);
878 do_write_page(sbi
, page
, old_blkaddr
, new_blkaddr
, &sum
, NODE
);
881 void write_data_page(struct inode
*inode
, struct page
*page
,
882 struct dnode_of_data
*dn
, block_t old_blkaddr
,
883 block_t
*new_blkaddr
)
885 struct f2fs_sb_info
*sbi
= F2FS_SB(inode
->i_sb
);
886 struct f2fs_summary sum
;
889 BUG_ON(old_blkaddr
== NULL_ADDR
);
890 get_node_info(sbi
, dn
->nid
, &ni
);
891 set_summary(&sum
, dn
->nid
, dn
->ofs_in_node
, ni
.version
);
893 do_write_page(sbi
, page
, old_blkaddr
,
894 new_blkaddr
, &sum
, DATA
);
897 void rewrite_data_page(struct f2fs_sb_info
*sbi
, struct page
*page
,
898 block_t old_blk_addr
)
900 submit_write_page(sbi
, page
, old_blk_addr
, DATA
);
903 void recover_data_page(struct f2fs_sb_info
*sbi
,
904 struct page
*page
, struct f2fs_summary
*sum
,
905 block_t old_blkaddr
, block_t new_blkaddr
)
907 struct sit_info
*sit_i
= SIT_I(sbi
);
908 struct curseg_info
*curseg
;
909 unsigned int segno
, old_cursegno
;
910 struct seg_entry
*se
;
913 segno
= GET_SEGNO(sbi
, new_blkaddr
);
914 se
= get_seg_entry(sbi
, segno
);
917 if (se
->valid_blocks
== 0 && !IS_CURSEG(sbi
, segno
)) {
918 if (old_blkaddr
== NULL_ADDR
)
919 type
= CURSEG_COLD_DATA
;
921 type
= CURSEG_WARM_DATA
;
923 curseg
= CURSEG_I(sbi
, type
);
925 mutex_lock(&curseg
->curseg_mutex
);
926 mutex_lock(&sit_i
->sentry_lock
);
928 old_cursegno
= curseg
->segno
;
930 /* change the current segment */
931 if (segno
!= curseg
->segno
) {
932 curseg
->next_segno
= segno
;
933 change_curseg(sbi
, type
, true);
936 curseg
->next_blkoff
= GET_SEGOFF_FROM_SEG0(sbi
, new_blkaddr
) &
937 (sbi
->blocks_per_seg
- 1);
938 __add_sum_entry(sbi
, type
, sum
, curseg
->next_blkoff
);
940 refresh_sit_entry(sbi
, old_blkaddr
, new_blkaddr
);
942 locate_dirty_segment(sbi
, old_cursegno
);
943 locate_dirty_segment(sbi
, GET_SEGNO(sbi
, old_blkaddr
));
945 mutex_unlock(&sit_i
->sentry_lock
);
946 mutex_unlock(&curseg
->curseg_mutex
);
949 void rewrite_node_page(struct f2fs_sb_info
*sbi
,
950 struct page
*page
, struct f2fs_summary
*sum
,
951 block_t old_blkaddr
, block_t new_blkaddr
)
953 struct sit_info
*sit_i
= SIT_I(sbi
);
954 int type
= CURSEG_WARM_NODE
;
955 struct curseg_info
*curseg
;
956 unsigned int segno
, old_cursegno
;
957 block_t next_blkaddr
= next_blkaddr_of_node(page
);
958 unsigned int next_segno
= GET_SEGNO(sbi
, next_blkaddr
);
960 curseg
= CURSEG_I(sbi
, type
);
962 mutex_lock(&curseg
->curseg_mutex
);
963 mutex_lock(&sit_i
->sentry_lock
);
965 segno
= GET_SEGNO(sbi
, new_blkaddr
);
966 old_cursegno
= curseg
->segno
;
968 /* change the current segment */
969 if (segno
!= curseg
->segno
) {
970 curseg
->next_segno
= segno
;
971 change_curseg(sbi
, type
, true);
973 curseg
->next_blkoff
= GET_SEGOFF_FROM_SEG0(sbi
, new_blkaddr
) &
974 (sbi
->blocks_per_seg
- 1);
975 __add_sum_entry(sbi
, type
, sum
, curseg
->next_blkoff
);
977 /* change the current log to the next block addr in advance */
978 if (next_segno
!= segno
) {
979 curseg
->next_segno
= next_segno
;
980 change_curseg(sbi
, type
, true);
982 curseg
->next_blkoff
= GET_SEGOFF_FROM_SEG0(sbi
, next_blkaddr
) &
983 (sbi
->blocks_per_seg
- 1);
985 /* rewrite node page */
986 set_page_writeback(page
);
987 submit_write_page(sbi
, page
, new_blkaddr
, NODE
);
988 f2fs_submit_bio(sbi
, NODE
, true);
989 refresh_sit_entry(sbi
, old_blkaddr
, new_blkaddr
);
991 locate_dirty_segment(sbi
, old_cursegno
);
992 locate_dirty_segment(sbi
, GET_SEGNO(sbi
, old_blkaddr
));
994 mutex_unlock(&sit_i
->sentry_lock
);
995 mutex_unlock(&curseg
->curseg_mutex
);
998 static int read_compacted_summaries(struct f2fs_sb_info
*sbi
)
1000 struct f2fs_checkpoint
*ckpt
= F2FS_CKPT(sbi
);
1001 struct curseg_info
*seg_i
;
1002 unsigned char *kaddr
;
1007 start
= start_sum_block(sbi
);
1009 page
= get_meta_page(sbi
, start
++);
1010 kaddr
= (unsigned char *)page_address(page
);
1012 /* Step 1: restore nat cache */
1013 seg_i
= CURSEG_I(sbi
, CURSEG_HOT_DATA
);
1014 memcpy(&seg_i
->sum_blk
->n_nats
, kaddr
, SUM_JOURNAL_SIZE
);
1016 /* Step 2: restore sit cache */
1017 seg_i
= CURSEG_I(sbi
, CURSEG_COLD_DATA
);
1018 memcpy(&seg_i
->sum_blk
->n_sits
, kaddr
+ SUM_JOURNAL_SIZE
,
1020 offset
= 2 * SUM_JOURNAL_SIZE
;
1022 /* Step 3: restore summary entries */
1023 for (i
= CURSEG_HOT_DATA
; i
<= CURSEG_COLD_DATA
; i
++) {
1024 unsigned short blk_off
;
1027 seg_i
= CURSEG_I(sbi
, i
);
1028 segno
= le32_to_cpu(ckpt
->cur_data_segno
[i
]);
1029 blk_off
= le16_to_cpu(ckpt
->cur_data_blkoff
[i
]);
1030 seg_i
->next_segno
= segno
;
1031 reset_curseg(sbi
, i
, 0);
1032 seg_i
->alloc_type
= ckpt
->alloc_type
[i
];
1033 seg_i
->next_blkoff
= blk_off
;
1035 if (seg_i
->alloc_type
== SSR
)
1036 blk_off
= sbi
->blocks_per_seg
;
1038 for (j
= 0; j
< blk_off
; j
++) {
1039 struct f2fs_summary
*s
;
1040 s
= (struct f2fs_summary
*)(kaddr
+ offset
);
1041 seg_i
->sum_blk
->entries
[j
] = *s
;
1042 offset
+= SUMMARY_SIZE
;
1043 if (offset
+ SUMMARY_SIZE
<= PAGE_CACHE_SIZE
-
1047 f2fs_put_page(page
, 1);
1050 page
= get_meta_page(sbi
, start
++);
1051 kaddr
= (unsigned char *)page_address(page
);
1055 f2fs_put_page(page
, 1);
1059 static int read_normal_summaries(struct f2fs_sb_info
*sbi
, int type
)
1061 struct f2fs_checkpoint
*ckpt
= F2FS_CKPT(sbi
);
1062 struct f2fs_summary_block
*sum
;
1063 struct curseg_info
*curseg
;
1065 unsigned short blk_off
;
1066 unsigned int segno
= 0;
1067 block_t blk_addr
= 0;
1069 /* get segment number and block addr */
1070 if (IS_DATASEG(type
)) {
1071 segno
= le32_to_cpu(ckpt
->cur_data_segno
[type
]);
1072 blk_off
= le16_to_cpu(ckpt
->cur_data_blkoff
[type
-
1074 if (is_set_ckpt_flags(ckpt
, CP_UMOUNT_FLAG
))
1075 blk_addr
= sum_blk_addr(sbi
, NR_CURSEG_TYPE
, type
);
1077 blk_addr
= sum_blk_addr(sbi
, NR_CURSEG_DATA_TYPE
, type
);
1079 segno
= le32_to_cpu(ckpt
->cur_node_segno
[type
-
1081 blk_off
= le16_to_cpu(ckpt
->cur_node_blkoff
[type
-
1083 if (is_set_ckpt_flags(ckpt
, CP_UMOUNT_FLAG
))
1084 blk_addr
= sum_blk_addr(sbi
, NR_CURSEG_NODE_TYPE
,
1085 type
- CURSEG_HOT_NODE
);
1087 blk_addr
= GET_SUM_BLOCK(sbi
, segno
);
1090 new = get_meta_page(sbi
, blk_addr
);
1091 sum
= (struct f2fs_summary_block
*)page_address(new);
1093 if (IS_NODESEG(type
)) {
1094 if (is_set_ckpt_flags(ckpt
, CP_UMOUNT_FLAG
)) {
1095 struct f2fs_summary
*ns
= &sum
->entries
[0];
1097 for (i
= 0; i
< sbi
->blocks_per_seg
; i
++, ns
++) {
1099 ns
->ofs_in_node
= 0;
1102 if (restore_node_summary(sbi
, segno
, sum
)) {
1103 f2fs_put_page(new, 1);
1109 /* set uncompleted segment to curseg */
1110 curseg
= CURSEG_I(sbi
, type
);
1111 mutex_lock(&curseg
->curseg_mutex
);
1112 memcpy(curseg
->sum_blk
, sum
, PAGE_CACHE_SIZE
);
1113 curseg
->next_segno
= segno
;
1114 reset_curseg(sbi
, type
, 0);
1115 curseg
->alloc_type
= ckpt
->alloc_type
[type
];
1116 curseg
->next_blkoff
= blk_off
;
1117 mutex_unlock(&curseg
->curseg_mutex
);
1118 f2fs_put_page(new, 1);
1122 static int restore_curseg_summaries(struct f2fs_sb_info
*sbi
)
1124 int type
= CURSEG_HOT_DATA
;
1126 if (is_set_ckpt_flags(F2FS_CKPT(sbi
), CP_COMPACT_SUM_FLAG
)) {
1127 /* restore for compacted data summary */
1128 if (read_compacted_summaries(sbi
))
1130 type
= CURSEG_HOT_NODE
;
1133 for (; type
<= CURSEG_COLD_NODE
; type
++)
1134 if (read_normal_summaries(sbi
, type
))
1139 static void write_compacted_summaries(struct f2fs_sb_info
*sbi
, block_t blkaddr
)
1142 unsigned char *kaddr
;
1143 struct f2fs_summary
*summary
;
1144 struct curseg_info
*seg_i
;
1145 int written_size
= 0;
1148 page
= grab_meta_page(sbi
, blkaddr
++);
1149 kaddr
= (unsigned char *)page_address(page
);
1151 /* Step 1: write nat cache */
1152 seg_i
= CURSEG_I(sbi
, CURSEG_HOT_DATA
);
1153 memcpy(kaddr
, &seg_i
->sum_blk
->n_nats
, SUM_JOURNAL_SIZE
);
1154 written_size
+= SUM_JOURNAL_SIZE
;
1156 /* Step 2: write sit cache */
1157 seg_i
= CURSEG_I(sbi
, CURSEG_COLD_DATA
);
1158 memcpy(kaddr
+ written_size
, &seg_i
->sum_blk
->n_sits
,
1160 written_size
+= SUM_JOURNAL_SIZE
;
1162 set_page_dirty(page
);
1164 /* Step 3: write summary entries */
1165 for (i
= CURSEG_HOT_DATA
; i
<= CURSEG_COLD_DATA
; i
++) {
1166 unsigned short blkoff
;
1167 seg_i
= CURSEG_I(sbi
, i
);
1168 if (sbi
->ckpt
->alloc_type
[i
] == SSR
)
1169 blkoff
= sbi
->blocks_per_seg
;
1171 blkoff
= curseg_blkoff(sbi
, i
);
1173 for (j
= 0; j
< blkoff
; j
++) {
1175 page
= grab_meta_page(sbi
, blkaddr
++);
1176 kaddr
= (unsigned char *)page_address(page
);
1179 summary
= (struct f2fs_summary
*)(kaddr
+ written_size
);
1180 *summary
= seg_i
->sum_blk
->entries
[j
];
1181 written_size
+= SUMMARY_SIZE
;
1182 set_page_dirty(page
);
1184 if (written_size
+ SUMMARY_SIZE
<= PAGE_CACHE_SIZE
-
1188 f2fs_put_page(page
, 1);
1193 f2fs_put_page(page
, 1);
1196 static void write_normal_summaries(struct f2fs_sb_info
*sbi
,
1197 block_t blkaddr
, int type
)
1200 if (IS_DATASEG(type
))
1201 end
= type
+ NR_CURSEG_DATA_TYPE
;
1203 end
= type
+ NR_CURSEG_NODE_TYPE
;
1205 for (i
= type
; i
< end
; i
++) {
1206 struct curseg_info
*sum
= CURSEG_I(sbi
, i
);
1207 mutex_lock(&sum
->curseg_mutex
);
1208 write_sum_page(sbi
, sum
->sum_blk
, blkaddr
+ (i
- type
));
1209 mutex_unlock(&sum
->curseg_mutex
);
1213 void write_data_summaries(struct f2fs_sb_info
*sbi
, block_t start_blk
)
1215 if (is_set_ckpt_flags(F2FS_CKPT(sbi
), CP_COMPACT_SUM_FLAG
))
1216 write_compacted_summaries(sbi
, start_blk
);
1218 write_normal_summaries(sbi
, start_blk
, CURSEG_HOT_DATA
);
1221 void write_node_summaries(struct f2fs_sb_info
*sbi
, block_t start_blk
)
1223 if (is_set_ckpt_flags(F2FS_CKPT(sbi
), CP_UMOUNT_FLAG
))
1224 write_normal_summaries(sbi
, start_blk
, CURSEG_HOT_NODE
);
1228 int lookup_journal_in_cursum(struct f2fs_summary_block
*sum
, int type
,
1229 unsigned int val
, int alloc
)
1233 if (type
== NAT_JOURNAL
) {
1234 for (i
= 0; i
< nats_in_cursum(sum
); i
++) {
1235 if (le32_to_cpu(nid_in_journal(sum
, i
)) == val
)
1238 if (alloc
&& nats_in_cursum(sum
) < NAT_JOURNAL_ENTRIES
)
1239 return update_nats_in_cursum(sum
, 1);
1240 } else if (type
== SIT_JOURNAL
) {
1241 for (i
= 0; i
< sits_in_cursum(sum
); i
++)
1242 if (le32_to_cpu(segno_in_journal(sum
, i
)) == val
)
1244 if (alloc
&& sits_in_cursum(sum
) < SIT_JOURNAL_ENTRIES
)
1245 return update_sits_in_cursum(sum
, 1);
1250 static struct page
*get_current_sit_page(struct f2fs_sb_info
*sbi
,
1253 struct sit_info
*sit_i
= SIT_I(sbi
);
1254 unsigned int offset
= SIT_BLOCK_OFFSET(sit_i
, segno
);
1255 block_t blk_addr
= sit_i
->sit_base_addr
+ offset
;
1257 check_seg_range(sbi
, segno
);
1259 /* calculate sit block address */
1260 if (f2fs_test_bit(offset
, sit_i
->sit_bitmap
))
1261 blk_addr
+= sit_i
->sit_blocks
;
1263 return get_meta_page(sbi
, blk_addr
);
1266 static struct page
*get_next_sit_page(struct f2fs_sb_info
*sbi
,
1269 struct sit_info
*sit_i
= SIT_I(sbi
);
1270 struct page
*src_page
, *dst_page
;
1271 pgoff_t src_off
, dst_off
;
1272 void *src_addr
, *dst_addr
;
1274 src_off
= current_sit_addr(sbi
, start
);
1275 dst_off
= next_sit_addr(sbi
, src_off
);
1277 /* get current sit block page without lock */
1278 src_page
= get_meta_page(sbi
, src_off
);
1279 dst_page
= grab_meta_page(sbi
, dst_off
);
1280 BUG_ON(PageDirty(src_page
));
1282 src_addr
= page_address(src_page
);
1283 dst_addr
= page_address(dst_page
);
1284 memcpy(dst_addr
, src_addr
, PAGE_CACHE_SIZE
);
1286 set_page_dirty(dst_page
);
1287 f2fs_put_page(src_page
, 1);
1289 set_to_next_sit(sit_i
, start
);
1294 static bool flush_sits_in_journal(struct f2fs_sb_info
*sbi
)
1296 struct curseg_info
*curseg
= CURSEG_I(sbi
, CURSEG_COLD_DATA
);
1297 struct f2fs_summary_block
*sum
= curseg
->sum_blk
;
1301 * If the journal area in the current summary is full of sit entries,
1302 * all the sit entries will be flushed. Otherwise the sit entries
1303 * are not able to replace with newly hot sit entries.
1305 if (sits_in_cursum(sum
) >= SIT_JOURNAL_ENTRIES
) {
1306 for (i
= sits_in_cursum(sum
) - 1; i
>= 0; i
--) {
1308 segno
= le32_to_cpu(segno_in_journal(sum
, i
));
1309 __mark_sit_entry_dirty(sbi
, segno
);
1311 update_sits_in_cursum(sum
, -sits_in_cursum(sum
));
1318 * CP calls this function, which flushes SIT entries including sit_journal,
1319 * and moves prefree segs to free segs.
1321 void flush_sit_entries(struct f2fs_sb_info
*sbi
)
1323 struct sit_info
*sit_i
= SIT_I(sbi
);
1324 unsigned long *bitmap
= sit_i
->dirty_sentries_bitmap
;
1325 struct curseg_info
*curseg
= CURSEG_I(sbi
, CURSEG_COLD_DATA
);
1326 struct f2fs_summary_block
*sum
= curseg
->sum_blk
;
1327 unsigned long nsegs
= TOTAL_SEGS(sbi
);
1328 struct page
*page
= NULL
;
1329 struct f2fs_sit_block
*raw_sit
= NULL
;
1330 unsigned int start
= 0, end
= 0;
1331 unsigned int segno
= -1;
1334 mutex_lock(&curseg
->curseg_mutex
);
1335 mutex_lock(&sit_i
->sentry_lock
);
1338 * "flushed" indicates whether sit entries in journal are flushed
1339 * to the SIT area or not.
1341 flushed
= flush_sits_in_journal(sbi
);
1343 while ((segno
= find_next_bit(bitmap
, nsegs
, segno
+ 1)) < nsegs
) {
1344 struct seg_entry
*se
= get_seg_entry(sbi
, segno
);
1345 int sit_offset
, offset
;
1347 sit_offset
= SIT_ENTRY_OFFSET(sit_i
, segno
);
1352 offset
= lookup_journal_in_cursum(sum
, SIT_JOURNAL
, segno
, 1);
1354 segno_in_journal(sum
, offset
) = cpu_to_le32(segno
);
1355 seg_info_to_raw_sit(se
, &sit_in_journal(sum
, offset
));
1359 if (!page
|| (start
> segno
) || (segno
> end
)) {
1361 f2fs_put_page(page
, 1);
1365 start
= START_SEGNO(sit_i
, segno
);
1366 end
= start
+ SIT_ENTRY_PER_BLOCK
- 1;
1368 /* read sit block that will be updated */
1369 page
= get_next_sit_page(sbi
, start
);
1370 raw_sit
= page_address(page
);
1373 /* udpate entry in SIT block */
1374 seg_info_to_raw_sit(se
, &raw_sit
->entries
[sit_offset
]);
1376 __clear_bit(segno
, bitmap
);
1377 sit_i
->dirty_sentries
--;
1379 mutex_unlock(&sit_i
->sentry_lock
);
1380 mutex_unlock(&curseg
->curseg_mutex
);
1382 /* writeout last modified SIT block */
1383 f2fs_put_page(page
, 1);
1385 set_prefree_as_free_segments(sbi
);
1388 static int build_sit_info(struct f2fs_sb_info
*sbi
)
1390 struct f2fs_super_block
*raw_super
= F2FS_RAW_SUPER(sbi
);
1391 struct f2fs_checkpoint
*ckpt
= F2FS_CKPT(sbi
);
1392 struct sit_info
*sit_i
;
1393 unsigned int sit_segs
, start
;
1394 char *src_bitmap
, *dst_bitmap
;
1395 unsigned int bitmap_size
;
1397 /* allocate memory for SIT information */
1398 sit_i
= kzalloc(sizeof(struct sit_info
), GFP_KERNEL
);
1402 SM_I(sbi
)->sit_info
= sit_i
;
1404 sit_i
->sentries
= vzalloc(TOTAL_SEGS(sbi
) * sizeof(struct seg_entry
));
1405 if (!sit_i
->sentries
)
1408 bitmap_size
= f2fs_bitmap_size(TOTAL_SEGS(sbi
));
1409 sit_i
->dirty_sentries_bitmap
= kzalloc(bitmap_size
, GFP_KERNEL
);
1410 if (!sit_i
->dirty_sentries_bitmap
)
1413 for (start
= 0; start
< TOTAL_SEGS(sbi
); start
++) {
1414 sit_i
->sentries
[start
].cur_valid_map
1415 = kzalloc(SIT_VBLOCK_MAP_SIZE
, GFP_KERNEL
);
1416 sit_i
->sentries
[start
].ckpt_valid_map
1417 = kzalloc(SIT_VBLOCK_MAP_SIZE
, GFP_KERNEL
);
1418 if (!sit_i
->sentries
[start
].cur_valid_map
1419 || !sit_i
->sentries
[start
].ckpt_valid_map
)
1423 if (sbi
->segs_per_sec
> 1) {
1424 sit_i
->sec_entries
= vzalloc(TOTAL_SECS(sbi
) *
1425 sizeof(struct sec_entry
));
1426 if (!sit_i
->sec_entries
)
1430 /* get information related with SIT */
1431 sit_segs
= le32_to_cpu(raw_super
->segment_count_sit
) >> 1;
1433 /* setup SIT bitmap from ckeckpoint pack */
1434 bitmap_size
= __bitmap_size(sbi
, SIT_BITMAP
);
1435 src_bitmap
= __bitmap_ptr(sbi
, SIT_BITMAP
);
1437 dst_bitmap
= kmemdup(src_bitmap
, bitmap_size
, GFP_KERNEL
);
1441 /* init SIT information */
1442 sit_i
->s_ops
= &default_salloc_ops
;
1444 sit_i
->sit_base_addr
= le32_to_cpu(raw_super
->sit_blkaddr
);
1445 sit_i
->sit_blocks
= sit_segs
<< sbi
->log_blocks_per_seg
;
1446 sit_i
->written_valid_blocks
= le64_to_cpu(ckpt
->valid_block_count
);
1447 sit_i
->sit_bitmap
= dst_bitmap
;
1448 sit_i
->bitmap_size
= bitmap_size
;
1449 sit_i
->dirty_sentries
= 0;
1450 sit_i
->sents_per_block
= SIT_ENTRY_PER_BLOCK
;
1451 sit_i
->elapsed_time
= le64_to_cpu(sbi
->ckpt
->elapsed_time
);
1452 sit_i
->mounted_time
= CURRENT_TIME_SEC
.tv_sec
;
1453 mutex_init(&sit_i
->sentry_lock
);
1457 static int build_free_segmap(struct f2fs_sb_info
*sbi
)
1459 struct f2fs_sm_info
*sm_info
= SM_I(sbi
);
1460 struct free_segmap_info
*free_i
;
1461 unsigned int bitmap_size
, sec_bitmap_size
;
1463 /* allocate memory for free segmap information */
1464 free_i
= kzalloc(sizeof(struct free_segmap_info
), GFP_KERNEL
);
1468 SM_I(sbi
)->free_info
= free_i
;
1470 bitmap_size
= f2fs_bitmap_size(TOTAL_SEGS(sbi
));
1471 free_i
->free_segmap
= kmalloc(bitmap_size
, GFP_KERNEL
);
1472 if (!free_i
->free_segmap
)
1475 sec_bitmap_size
= f2fs_bitmap_size(TOTAL_SECS(sbi
));
1476 free_i
->free_secmap
= kmalloc(sec_bitmap_size
, GFP_KERNEL
);
1477 if (!free_i
->free_secmap
)
1480 /* set all segments as dirty temporarily */
1481 memset(free_i
->free_segmap
, 0xff, bitmap_size
);
1482 memset(free_i
->free_secmap
, 0xff, sec_bitmap_size
);
1484 /* init free segmap information */
1485 free_i
->start_segno
=
1486 (unsigned int) GET_SEGNO_FROM_SEG0(sbi
, sm_info
->main_blkaddr
);
1487 free_i
->free_segments
= 0;
1488 free_i
->free_sections
= 0;
1489 rwlock_init(&free_i
->segmap_lock
);
1493 static int build_curseg(struct f2fs_sb_info
*sbi
)
1495 struct curseg_info
*array
;
1498 array
= kzalloc(sizeof(*array
) * NR_CURSEG_TYPE
, GFP_KERNEL
);
1502 SM_I(sbi
)->curseg_array
= array
;
1504 for (i
= 0; i
< NR_CURSEG_TYPE
; i
++) {
1505 mutex_init(&array
[i
].curseg_mutex
);
1506 array
[i
].sum_blk
= kzalloc(PAGE_CACHE_SIZE
, GFP_KERNEL
);
1507 if (!array
[i
].sum_blk
)
1509 array
[i
].segno
= NULL_SEGNO
;
1510 array
[i
].next_blkoff
= 0;
1512 return restore_curseg_summaries(sbi
);
1515 static void build_sit_entries(struct f2fs_sb_info
*sbi
)
1517 struct sit_info
*sit_i
= SIT_I(sbi
);
1518 struct curseg_info
*curseg
= CURSEG_I(sbi
, CURSEG_COLD_DATA
);
1519 struct f2fs_summary_block
*sum
= curseg
->sum_blk
;
1522 for (start
= 0; start
< TOTAL_SEGS(sbi
); start
++) {
1523 struct seg_entry
*se
= &sit_i
->sentries
[start
];
1524 struct f2fs_sit_block
*sit_blk
;
1525 struct f2fs_sit_entry sit
;
1529 mutex_lock(&curseg
->curseg_mutex
);
1530 for (i
= 0; i
< sits_in_cursum(sum
); i
++) {
1531 if (le32_to_cpu(segno_in_journal(sum
, i
)) == start
) {
1532 sit
= sit_in_journal(sum
, i
);
1533 mutex_unlock(&curseg
->curseg_mutex
);
1537 mutex_unlock(&curseg
->curseg_mutex
);
1538 page
= get_current_sit_page(sbi
, start
);
1539 sit_blk
= (struct f2fs_sit_block
*)page_address(page
);
1540 sit
= sit_blk
->entries
[SIT_ENTRY_OFFSET(sit_i
, start
)];
1541 f2fs_put_page(page
, 1);
1543 check_block_count(sbi
, start
, &sit
);
1544 seg_info_from_raw_sit(se
, &sit
);
1545 if (sbi
->segs_per_sec
> 1) {
1546 struct sec_entry
*e
= get_sec_entry(sbi
, start
);
1547 e
->valid_blocks
+= se
->valid_blocks
;
1552 static void init_free_segmap(struct f2fs_sb_info
*sbi
)
1557 for (start
= 0; start
< TOTAL_SEGS(sbi
); start
++) {
1558 struct seg_entry
*sentry
= get_seg_entry(sbi
, start
);
1559 if (!sentry
->valid_blocks
)
1560 __set_free(sbi
, start
);
1563 /* set use the current segments */
1564 for (type
= CURSEG_HOT_DATA
; type
<= CURSEG_COLD_NODE
; type
++) {
1565 struct curseg_info
*curseg_t
= CURSEG_I(sbi
, type
);
1566 __set_test_and_inuse(sbi
, curseg_t
->segno
);
1570 static void init_dirty_segmap(struct f2fs_sb_info
*sbi
)
1572 struct dirty_seglist_info
*dirty_i
= DIRTY_I(sbi
);
1573 struct free_segmap_info
*free_i
= FREE_I(sbi
);
1574 unsigned int segno
= 0, offset
= 0;
1575 unsigned short valid_blocks
;
1577 while (segno
< TOTAL_SEGS(sbi
)) {
1578 /* find dirty segment based on free segmap */
1579 segno
= find_next_inuse(free_i
, TOTAL_SEGS(sbi
), offset
);
1580 if (segno
>= TOTAL_SEGS(sbi
))
1583 valid_blocks
= get_valid_blocks(sbi
, segno
, 0);
1584 if (valid_blocks
>= sbi
->blocks_per_seg
|| !valid_blocks
)
1586 mutex_lock(&dirty_i
->seglist_lock
);
1587 __locate_dirty_segment(sbi
, segno
, DIRTY
);
1588 mutex_unlock(&dirty_i
->seglist_lock
);
1592 static int init_victim_secmap(struct f2fs_sb_info
*sbi
)
1594 struct dirty_seglist_info
*dirty_i
= DIRTY_I(sbi
);
1595 unsigned int bitmap_size
= f2fs_bitmap_size(TOTAL_SECS(sbi
));
1597 dirty_i
->victim_secmap
= kzalloc(bitmap_size
, GFP_KERNEL
);
1598 if (!dirty_i
->victim_secmap
)
1603 static int build_dirty_segmap(struct f2fs_sb_info
*sbi
)
1605 struct dirty_seglist_info
*dirty_i
;
1606 unsigned int bitmap_size
, i
;
1608 /* allocate memory for dirty segments list information */
1609 dirty_i
= kzalloc(sizeof(struct dirty_seglist_info
), GFP_KERNEL
);
1613 SM_I(sbi
)->dirty_info
= dirty_i
;
1614 mutex_init(&dirty_i
->seglist_lock
);
1616 bitmap_size
= f2fs_bitmap_size(TOTAL_SEGS(sbi
));
1618 for (i
= 0; i
< NR_DIRTY_TYPE
; i
++) {
1619 dirty_i
->dirty_segmap
[i
] = kzalloc(bitmap_size
, GFP_KERNEL
);
1620 if (!dirty_i
->dirty_segmap
[i
])
1624 init_dirty_segmap(sbi
);
1625 return init_victim_secmap(sbi
);
1629 * Update min, max modified time for cost-benefit GC algorithm
1631 static void init_min_max_mtime(struct f2fs_sb_info
*sbi
)
1633 struct sit_info
*sit_i
= SIT_I(sbi
);
1636 mutex_lock(&sit_i
->sentry_lock
);
1638 sit_i
->min_mtime
= LLONG_MAX
;
1640 for (segno
= 0; segno
< TOTAL_SEGS(sbi
); segno
+= sbi
->segs_per_sec
) {
1642 unsigned long long mtime
= 0;
1644 for (i
= 0; i
< sbi
->segs_per_sec
; i
++)
1645 mtime
+= get_seg_entry(sbi
, segno
+ i
)->mtime
;
1647 mtime
= div_u64(mtime
, sbi
->segs_per_sec
);
1649 if (sit_i
->min_mtime
> mtime
)
1650 sit_i
->min_mtime
= mtime
;
1652 sit_i
->max_mtime
= get_mtime(sbi
);
1653 mutex_unlock(&sit_i
->sentry_lock
);
1656 int build_segment_manager(struct f2fs_sb_info
*sbi
)
1658 struct f2fs_super_block
*raw_super
= F2FS_RAW_SUPER(sbi
);
1659 struct f2fs_checkpoint
*ckpt
= F2FS_CKPT(sbi
);
1660 struct f2fs_sm_info
*sm_info
;
1663 sm_info
= kzalloc(sizeof(struct f2fs_sm_info
), GFP_KERNEL
);
1668 sbi
->sm_info
= sm_info
;
1669 INIT_LIST_HEAD(&sm_info
->wblist_head
);
1670 spin_lock_init(&sm_info
->wblist_lock
);
1671 sm_info
->seg0_blkaddr
= le32_to_cpu(raw_super
->segment0_blkaddr
);
1672 sm_info
->main_blkaddr
= le32_to_cpu(raw_super
->main_blkaddr
);
1673 sm_info
->segment_count
= le32_to_cpu(raw_super
->segment_count
);
1674 sm_info
->reserved_segments
= le32_to_cpu(ckpt
->rsvd_segment_count
);
1675 sm_info
->ovp_segments
= le32_to_cpu(ckpt
->overprov_segment_count
);
1676 sm_info
->main_segments
= le32_to_cpu(raw_super
->segment_count_main
);
1677 sm_info
->ssa_blkaddr
= le32_to_cpu(raw_super
->ssa_blkaddr
);
1679 err
= build_sit_info(sbi
);
1682 err
= build_free_segmap(sbi
);
1685 err
= build_curseg(sbi
);
1689 /* reinit free segmap based on SIT */
1690 build_sit_entries(sbi
);
1692 init_free_segmap(sbi
);
1693 err
= build_dirty_segmap(sbi
);
1697 init_min_max_mtime(sbi
);
1701 static void discard_dirty_segmap(struct f2fs_sb_info
*sbi
,
1702 enum dirty_type dirty_type
)
1704 struct dirty_seglist_info
*dirty_i
= DIRTY_I(sbi
);
1706 mutex_lock(&dirty_i
->seglist_lock
);
1707 kfree(dirty_i
->dirty_segmap
[dirty_type
]);
1708 dirty_i
->nr_dirty
[dirty_type
] = 0;
1709 mutex_unlock(&dirty_i
->seglist_lock
);
1712 static void destroy_victim_secmap(struct f2fs_sb_info
*sbi
)
1714 struct dirty_seglist_info
*dirty_i
= DIRTY_I(sbi
);
1715 kfree(dirty_i
->victim_secmap
);
1718 static void destroy_dirty_segmap(struct f2fs_sb_info
*sbi
)
1720 struct dirty_seglist_info
*dirty_i
= DIRTY_I(sbi
);
1726 /* discard pre-free/dirty segments list */
1727 for (i
= 0; i
< NR_DIRTY_TYPE
; i
++)
1728 discard_dirty_segmap(sbi
, i
);
1730 destroy_victim_secmap(sbi
);
1731 SM_I(sbi
)->dirty_info
= NULL
;
1735 static void destroy_curseg(struct f2fs_sb_info
*sbi
)
1737 struct curseg_info
*array
= SM_I(sbi
)->curseg_array
;
1742 SM_I(sbi
)->curseg_array
= NULL
;
1743 for (i
= 0; i
< NR_CURSEG_TYPE
; i
++)
1744 kfree(array
[i
].sum_blk
);
1748 static void destroy_free_segmap(struct f2fs_sb_info
*sbi
)
1750 struct free_segmap_info
*free_i
= SM_I(sbi
)->free_info
;
1753 SM_I(sbi
)->free_info
= NULL
;
1754 kfree(free_i
->free_segmap
);
1755 kfree(free_i
->free_secmap
);
1759 static void destroy_sit_info(struct f2fs_sb_info
*sbi
)
1761 struct sit_info
*sit_i
= SIT_I(sbi
);
1767 if (sit_i
->sentries
) {
1768 for (start
= 0; start
< TOTAL_SEGS(sbi
); start
++) {
1769 kfree(sit_i
->sentries
[start
].cur_valid_map
);
1770 kfree(sit_i
->sentries
[start
].ckpt_valid_map
);
1773 vfree(sit_i
->sentries
);
1774 vfree(sit_i
->sec_entries
);
1775 kfree(sit_i
->dirty_sentries_bitmap
);
1777 SM_I(sbi
)->sit_info
= NULL
;
1778 kfree(sit_i
->sit_bitmap
);
1782 void destroy_segment_manager(struct f2fs_sb_info
*sbi
)
1784 struct f2fs_sm_info
*sm_info
= SM_I(sbi
);
1785 destroy_dirty_segmap(sbi
);
1786 destroy_curseg(sbi
);
1787 destroy_free_segmap(sbi
);
1788 destroy_sit_info(sbi
);
1789 sbi
->sm_info
= NULL
;