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>
23 #define __reverse_ffz(x) __reverse_ffs(~(x))
25 static struct kmem_cache
*discard_entry_slab
;
28 * __reverse_ffs is copied from include/asm-generic/bitops/__ffs.h since
29 * MSB and LSB are reversed in a byte by f2fs_set_bit.
31 static inline unsigned long __reverse_ffs(unsigned long word
)
35 #if BITS_PER_LONG == 64
36 if ((word
& 0xffffffff) == 0) {
41 if ((word
& 0xffff) == 0) {
45 if ((word
& 0xff) == 0) {
49 if ((word
& 0xf0) == 0)
53 if ((word
& 0xc) == 0)
57 if ((word
& 0x2) == 0)
63 * __find_rev_next(_zero)_bit is copied from lib/find_next_bit.c becasue
64 * f2fs_set_bit makes MSB and LSB reversed in a byte.
67 * f2fs_set_bit(0, bitmap) => 0000 0001
68 * f2fs_set_bit(7, bitmap) => 1000 0000
70 static unsigned long __find_rev_next_bit(const unsigned long *addr
,
71 unsigned long size
, unsigned long offset
)
73 const unsigned long *p
= addr
+ BIT_WORD(offset
);
74 unsigned long result
= offset
& ~(BITS_PER_LONG
- 1);
76 unsigned long mask
, submask
;
77 unsigned long quot
, rest
;
83 offset
%= BITS_PER_LONG
;
88 quot
= (offset
>> 3) << 3;
91 submask
= (unsigned char)(0xff << rest
) >> rest
;
95 if (size
< BITS_PER_LONG
)
100 size
-= BITS_PER_LONG
;
101 result
+= BITS_PER_LONG
;
103 while (size
& ~(BITS_PER_LONG
-1)) {
107 result
+= BITS_PER_LONG
;
108 size
-= BITS_PER_LONG
;
114 tmp
&= (~0UL >> (BITS_PER_LONG
- size
));
115 if (tmp
== 0UL) /* Are any bits set? */
116 return result
+ size
; /* Nope. */
118 return result
+ __reverse_ffs(tmp
);
121 static unsigned long __find_rev_next_zero_bit(const unsigned long *addr
,
122 unsigned long size
, unsigned long offset
)
124 const unsigned long *p
= addr
+ BIT_WORD(offset
);
125 unsigned long result
= offset
& ~(BITS_PER_LONG
- 1);
127 unsigned long mask
, submask
;
128 unsigned long quot
, rest
;
134 offset
%= BITS_PER_LONG
;
139 quot
= (offset
>> 3) << 3;
141 mask
= ~(~0UL << quot
);
142 submask
= (unsigned char)~((unsigned char)(0xff << rest
) >> rest
);
146 if (size
< BITS_PER_LONG
)
151 size
-= BITS_PER_LONG
;
152 result
+= BITS_PER_LONG
;
154 while (size
& ~(BITS_PER_LONG
- 1)) {
158 result
+= BITS_PER_LONG
;
159 size
-= BITS_PER_LONG
;
167 if (tmp
== ~0UL) /* Are any bits zero? */
168 return result
+ size
; /* Nope. */
170 return result
+ __reverse_ffz(tmp
);
174 * This function balances dirty node and dentry pages.
175 * In addition, it controls garbage collection.
177 void f2fs_balance_fs(struct f2fs_sb_info
*sbi
)
180 * We should do GC or end up with checkpoint, if there are so many dirty
181 * dir/node pages without enough free segments.
183 if (has_not_enough_free_secs(sbi
, 0)) {
184 mutex_lock(&sbi
->gc_mutex
);
189 void f2fs_balance_fs_bg(struct f2fs_sb_info
*sbi
)
191 /* check the # of cached NAT entries and prefree segments */
192 if (try_to_free_nats(sbi
, NAT_ENTRY_PER_BLOCK
) ||
193 excess_prefree_segs(sbi
))
194 f2fs_sync_fs(sbi
->sb
, true);
197 static void __locate_dirty_segment(struct f2fs_sb_info
*sbi
, unsigned int segno
,
198 enum dirty_type dirty_type
)
200 struct dirty_seglist_info
*dirty_i
= DIRTY_I(sbi
);
202 /* need not be added */
203 if (IS_CURSEG(sbi
, segno
))
206 if (!test_and_set_bit(segno
, dirty_i
->dirty_segmap
[dirty_type
]))
207 dirty_i
->nr_dirty
[dirty_type
]++;
209 if (dirty_type
== DIRTY
) {
210 struct seg_entry
*sentry
= get_seg_entry(sbi
, segno
);
211 enum dirty_type t
= sentry
->type
;
213 if (!test_and_set_bit(segno
, dirty_i
->dirty_segmap
[t
]))
214 dirty_i
->nr_dirty
[t
]++;
218 static void __remove_dirty_segment(struct f2fs_sb_info
*sbi
, unsigned int segno
,
219 enum dirty_type dirty_type
)
221 struct dirty_seglist_info
*dirty_i
= DIRTY_I(sbi
);
223 if (test_and_clear_bit(segno
, dirty_i
->dirty_segmap
[dirty_type
]))
224 dirty_i
->nr_dirty
[dirty_type
]--;
226 if (dirty_type
== DIRTY
) {
227 struct seg_entry
*sentry
= get_seg_entry(sbi
, segno
);
228 enum dirty_type t
= sentry
->type
;
230 if (test_and_clear_bit(segno
, dirty_i
->dirty_segmap
[t
]))
231 dirty_i
->nr_dirty
[t
]--;
233 if (get_valid_blocks(sbi
, segno
, sbi
->segs_per_sec
) == 0)
234 clear_bit(GET_SECNO(sbi
, segno
),
235 dirty_i
->victim_secmap
);
240 * Should not occur error such as -ENOMEM.
241 * Adding dirty entry into seglist is not critical operation.
242 * If a given segment is one of current working segments, it won't be added.
244 static void locate_dirty_segment(struct f2fs_sb_info
*sbi
, unsigned int segno
)
246 struct dirty_seglist_info
*dirty_i
= DIRTY_I(sbi
);
247 unsigned short valid_blocks
;
249 if (segno
== NULL_SEGNO
|| IS_CURSEG(sbi
, segno
))
252 mutex_lock(&dirty_i
->seglist_lock
);
254 valid_blocks
= get_valid_blocks(sbi
, segno
, 0);
256 if (valid_blocks
== 0) {
257 __locate_dirty_segment(sbi
, segno
, PRE
);
258 __remove_dirty_segment(sbi
, segno
, DIRTY
);
259 } else if (valid_blocks
< sbi
->blocks_per_seg
) {
260 __locate_dirty_segment(sbi
, segno
, DIRTY
);
262 /* Recovery routine with SSR needs this */
263 __remove_dirty_segment(sbi
, segno
, DIRTY
);
266 mutex_unlock(&dirty_i
->seglist_lock
);
270 * Should call clear_prefree_segments after checkpoint is done.
272 static void set_prefree_as_free_segments(struct f2fs_sb_info
*sbi
)
274 struct dirty_seglist_info
*dirty_i
= DIRTY_I(sbi
);
275 unsigned int segno
= -1;
276 unsigned int total_segs
= TOTAL_SEGS(sbi
);
278 mutex_lock(&dirty_i
->seglist_lock
);
280 segno
= find_next_bit(dirty_i
->dirty_segmap
[PRE
], total_segs
,
282 if (segno
>= total_segs
)
284 __set_test_and_free(sbi
, segno
);
286 mutex_unlock(&dirty_i
->seglist_lock
);
289 void clear_prefree_segments(struct f2fs_sb_info
*sbi
)
291 struct dirty_seglist_info
*dirty_i
= DIRTY_I(sbi
);
292 unsigned long *prefree_map
= dirty_i
->dirty_segmap
[PRE
];
293 unsigned int total_segs
= TOTAL_SEGS(sbi
);
294 unsigned int start
= 0, end
= -1;
296 mutex_lock(&dirty_i
->seglist_lock
);
300 start
= find_next_bit(prefree_map
, total_segs
, end
+ 1);
301 if (start
>= total_segs
)
303 end
= find_next_zero_bit(prefree_map
, total_segs
, start
+ 1);
305 for (i
= start
; i
< end
; i
++)
306 clear_bit(i
, prefree_map
);
308 dirty_i
->nr_dirty
[PRE
] -= end
- start
;
310 if (!test_opt(sbi
, DISCARD
))
313 blkdev_issue_discard(sbi
->sb
->s_bdev
,
314 START_BLOCK(sbi
, start
) <<
315 sbi
->log_sectors_per_block
,
316 (1 << (sbi
->log_sectors_per_block
+
317 sbi
->log_blocks_per_seg
)) * (end
- start
),
320 mutex_unlock(&dirty_i
->seglist_lock
);
323 static void __mark_sit_entry_dirty(struct f2fs_sb_info
*sbi
, unsigned int segno
)
325 struct sit_info
*sit_i
= SIT_I(sbi
);
326 if (!__test_and_set_bit(segno
, sit_i
->dirty_sentries_bitmap
))
327 sit_i
->dirty_sentries
++;
330 static void __set_sit_entry_type(struct f2fs_sb_info
*sbi
, int type
,
331 unsigned int segno
, int modified
)
333 struct seg_entry
*se
= get_seg_entry(sbi
, segno
);
336 __mark_sit_entry_dirty(sbi
, segno
);
339 static void update_sit_entry(struct f2fs_sb_info
*sbi
, block_t blkaddr
, int del
)
341 struct seg_entry
*se
;
342 unsigned int segno
, offset
;
343 long int new_vblocks
;
345 segno
= GET_SEGNO(sbi
, blkaddr
);
347 se
= get_seg_entry(sbi
, segno
);
348 new_vblocks
= se
->valid_blocks
+ del
;
349 offset
= GET_SEGOFF_FROM_SEG0(sbi
, blkaddr
) & (sbi
->blocks_per_seg
- 1);
351 f2fs_bug_on((new_vblocks
>> (sizeof(unsigned short) << 3) ||
352 (new_vblocks
> sbi
->blocks_per_seg
)));
354 se
->valid_blocks
= new_vblocks
;
355 se
->mtime
= get_mtime(sbi
);
356 SIT_I(sbi
)->max_mtime
= se
->mtime
;
358 /* Update valid block bitmap */
360 if (f2fs_set_bit(offset
, se
->cur_valid_map
))
363 if (!f2fs_clear_bit(offset
, se
->cur_valid_map
))
366 if (!f2fs_test_bit(offset
, se
->ckpt_valid_map
))
367 se
->ckpt_valid_blocks
+= del
;
369 __mark_sit_entry_dirty(sbi
, segno
);
371 /* update total number of valid blocks to be written in ckpt area */
372 SIT_I(sbi
)->written_valid_blocks
+= del
;
374 if (sbi
->segs_per_sec
> 1)
375 get_sec_entry(sbi
, segno
)->valid_blocks
+= del
;
378 static void refresh_sit_entry(struct f2fs_sb_info
*sbi
,
379 block_t old_blkaddr
, block_t new_blkaddr
)
381 update_sit_entry(sbi
, new_blkaddr
, 1);
382 if (GET_SEGNO(sbi
, old_blkaddr
) != NULL_SEGNO
)
383 update_sit_entry(sbi
, old_blkaddr
, -1);
386 void invalidate_blocks(struct f2fs_sb_info
*sbi
, block_t addr
)
388 unsigned int segno
= GET_SEGNO(sbi
, addr
);
389 struct sit_info
*sit_i
= SIT_I(sbi
);
391 f2fs_bug_on(addr
== NULL_ADDR
);
392 if (addr
== NEW_ADDR
)
395 /* add it into sit main buffer */
396 mutex_lock(&sit_i
->sentry_lock
);
398 update_sit_entry(sbi
, addr
, -1);
400 /* add it into dirty seglist */
401 locate_dirty_segment(sbi
, segno
);
403 mutex_unlock(&sit_i
->sentry_lock
);
407 * This function should be resided under the curseg_mutex lock
409 static void __add_sum_entry(struct f2fs_sb_info
*sbi
, int type
,
410 struct f2fs_summary
*sum
)
412 struct curseg_info
*curseg
= CURSEG_I(sbi
, type
);
413 void *addr
= curseg
->sum_blk
;
414 addr
+= curseg
->next_blkoff
* sizeof(struct f2fs_summary
);
415 memcpy(addr
, sum
, sizeof(struct f2fs_summary
));
419 * Calculate the number of current summary pages for writing
421 int npages_for_summary_flush(struct f2fs_sb_info
*sbi
)
423 int valid_sum_count
= 0;
426 for (i
= CURSEG_HOT_DATA
; i
<= CURSEG_COLD_DATA
; i
++) {
427 if (sbi
->ckpt
->alloc_type
[i
] == SSR
)
428 valid_sum_count
+= sbi
->blocks_per_seg
;
430 valid_sum_count
+= curseg_blkoff(sbi
, i
);
433 sum_in_page
= (PAGE_CACHE_SIZE
- 2 * SUM_JOURNAL_SIZE
-
434 SUM_FOOTER_SIZE
) / SUMMARY_SIZE
;
435 if (valid_sum_count
<= sum_in_page
)
437 else if ((valid_sum_count
- sum_in_page
) <=
438 (PAGE_CACHE_SIZE
- SUM_FOOTER_SIZE
) / SUMMARY_SIZE
)
444 * Caller should put this summary page
446 struct page
*get_sum_page(struct f2fs_sb_info
*sbi
, unsigned int segno
)
448 return get_meta_page(sbi
, GET_SUM_BLOCK(sbi
, segno
));
451 static void write_sum_page(struct f2fs_sb_info
*sbi
,
452 struct f2fs_summary_block
*sum_blk
, block_t blk_addr
)
454 struct page
*page
= grab_meta_page(sbi
, blk_addr
);
455 void *kaddr
= page_address(page
);
456 memcpy(kaddr
, sum_blk
, PAGE_CACHE_SIZE
);
457 set_page_dirty(page
);
458 f2fs_put_page(page
, 1);
461 static int is_next_segment_free(struct f2fs_sb_info
*sbi
, int type
)
463 struct curseg_info
*curseg
= CURSEG_I(sbi
, type
);
464 unsigned int segno
= curseg
->segno
+ 1;
465 struct free_segmap_info
*free_i
= FREE_I(sbi
);
467 if (segno
< TOTAL_SEGS(sbi
) && segno
% sbi
->segs_per_sec
)
468 return !test_bit(segno
, free_i
->free_segmap
);
473 * Find a new segment from the free segments bitmap to right order
474 * This function should be returned with success, otherwise BUG
476 static void get_new_segment(struct f2fs_sb_info
*sbi
,
477 unsigned int *newseg
, bool new_sec
, int dir
)
479 struct free_segmap_info
*free_i
= FREE_I(sbi
);
480 unsigned int segno
, secno
, zoneno
;
481 unsigned int total_zones
= TOTAL_SECS(sbi
) / sbi
->secs_per_zone
;
482 unsigned int hint
= *newseg
/ sbi
->segs_per_sec
;
483 unsigned int old_zoneno
= GET_ZONENO_FROM_SEGNO(sbi
, *newseg
);
484 unsigned int left_start
= hint
;
489 write_lock(&free_i
->segmap_lock
);
491 if (!new_sec
&& ((*newseg
+ 1) % sbi
->segs_per_sec
)) {
492 segno
= find_next_zero_bit(free_i
->free_segmap
,
493 TOTAL_SEGS(sbi
), *newseg
+ 1);
494 if (segno
- *newseg
< sbi
->segs_per_sec
-
495 (*newseg
% sbi
->segs_per_sec
))
499 secno
= find_next_zero_bit(free_i
->free_secmap
, TOTAL_SECS(sbi
), hint
);
500 if (secno
>= TOTAL_SECS(sbi
)) {
501 if (dir
== ALLOC_RIGHT
) {
502 secno
= find_next_zero_bit(free_i
->free_secmap
,
504 f2fs_bug_on(secno
>= TOTAL_SECS(sbi
));
507 left_start
= hint
- 1;
513 while (test_bit(left_start
, free_i
->free_secmap
)) {
514 if (left_start
> 0) {
518 left_start
= find_next_zero_bit(free_i
->free_secmap
,
520 f2fs_bug_on(left_start
>= TOTAL_SECS(sbi
));
526 segno
= secno
* sbi
->segs_per_sec
;
527 zoneno
= secno
/ sbi
->secs_per_zone
;
529 /* give up on finding another zone */
532 if (sbi
->secs_per_zone
== 1)
534 if (zoneno
== old_zoneno
)
536 if (dir
== ALLOC_LEFT
) {
537 if (!go_left
&& zoneno
+ 1 >= total_zones
)
539 if (go_left
&& zoneno
== 0)
542 for (i
= 0; i
< NR_CURSEG_TYPE
; i
++)
543 if (CURSEG_I(sbi
, i
)->zone
== zoneno
)
546 if (i
< NR_CURSEG_TYPE
) {
547 /* zone is in user, try another */
549 hint
= zoneno
* sbi
->secs_per_zone
- 1;
550 else if (zoneno
+ 1 >= total_zones
)
553 hint
= (zoneno
+ 1) * sbi
->secs_per_zone
;
555 goto find_other_zone
;
558 /* set it as dirty segment in free segmap */
559 f2fs_bug_on(test_bit(segno
, free_i
->free_segmap
));
560 __set_inuse(sbi
, segno
);
562 write_unlock(&free_i
->segmap_lock
);
565 static void reset_curseg(struct f2fs_sb_info
*sbi
, int type
, int modified
)
567 struct curseg_info
*curseg
= CURSEG_I(sbi
, type
);
568 struct summary_footer
*sum_footer
;
570 curseg
->segno
= curseg
->next_segno
;
571 curseg
->zone
= GET_ZONENO_FROM_SEGNO(sbi
, curseg
->segno
);
572 curseg
->next_blkoff
= 0;
573 curseg
->next_segno
= NULL_SEGNO
;
575 sum_footer
= &(curseg
->sum_blk
->footer
);
576 memset(sum_footer
, 0, sizeof(struct summary_footer
));
577 if (IS_DATASEG(type
))
578 SET_SUM_TYPE(sum_footer
, SUM_TYPE_DATA
);
579 if (IS_NODESEG(type
))
580 SET_SUM_TYPE(sum_footer
, SUM_TYPE_NODE
);
581 __set_sit_entry_type(sbi
, type
, curseg
->segno
, modified
);
585 * Allocate a current working segment.
586 * This function always allocates a free segment in LFS manner.
588 static void new_curseg(struct f2fs_sb_info
*sbi
, int type
, bool new_sec
)
590 struct curseg_info
*curseg
= CURSEG_I(sbi
, type
);
591 unsigned int segno
= curseg
->segno
;
592 int dir
= ALLOC_LEFT
;
594 write_sum_page(sbi
, curseg
->sum_blk
,
595 GET_SUM_BLOCK(sbi
, segno
));
596 if (type
== CURSEG_WARM_DATA
|| type
== CURSEG_COLD_DATA
)
599 if (test_opt(sbi
, NOHEAP
))
602 get_new_segment(sbi
, &segno
, new_sec
, dir
);
603 curseg
->next_segno
= segno
;
604 reset_curseg(sbi
, type
, 1);
605 curseg
->alloc_type
= LFS
;
608 static void __next_free_blkoff(struct f2fs_sb_info
*sbi
,
609 struct curseg_info
*seg
, block_t start
)
611 struct seg_entry
*se
= get_seg_entry(sbi
, seg
->segno
);
612 int entries
= SIT_VBLOCK_MAP_SIZE
/ sizeof(unsigned long);
613 unsigned long target_map
[entries
];
614 unsigned long *ckpt_map
= (unsigned long *)se
->ckpt_valid_map
;
615 unsigned long *cur_map
= (unsigned long *)se
->cur_valid_map
;
618 for (i
= 0; i
< entries
; i
++)
619 target_map
[i
] = ckpt_map
[i
] | cur_map
[i
];
621 pos
= __find_rev_next_zero_bit(target_map
, sbi
->blocks_per_seg
, start
);
623 seg
->next_blkoff
= pos
;
627 * If a segment is written by LFS manner, next block offset is just obtained
628 * by increasing the current block offset. However, if a segment is written by
629 * SSR manner, next block offset obtained by calling __next_free_blkoff
631 static void __refresh_next_blkoff(struct f2fs_sb_info
*sbi
,
632 struct curseg_info
*seg
)
634 if (seg
->alloc_type
== SSR
)
635 __next_free_blkoff(sbi
, seg
, seg
->next_blkoff
+ 1);
641 * This function always allocates a used segment (from dirty seglist) by SSR
642 * manner, so it should recover the existing segment information of valid blocks
644 static void change_curseg(struct f2fs_sb_info
*sbi
, int type
, bool reuse
)
646 struct dirty_seglist_info
*dirty_i
= DIRTY_I(sbi
);
647 struct curseg_info
*curseg
= CURSEG_I(sbi
, type
);
648 unsigned int new_segno
= curseg
->next_segno
;
649 struct f2fs_summary_block
*sum_node
;
650 struct page
*sum_page
;
652 write_sum_page(sbi
, curseg
->sum_blk
,
653 GET_SUM_BLOCK(sbi
, curseg
->segno
));
654 __set_test_and_inuse(sbi
, new_segno
);
656 mutex_lock(&dirty_i
->seglist_lock
);
657 __remove_dirty_segment(sbi
, new_segno
, PRE
);
658 __remove_dirty_segment(sbi
, new_segno
, DIRTY
);
659 mutex_unlock(&dirty_i
->seglist_lock
);
661 reset_curseg(sbi
, type
, 1);
662 curseg
->alloc_type
= SSR
;
663 __next_free_blkoff(sbi
, curseg
, 0);
666 sum_page
= get_sum_page(sbi
, new_segno
);
667 sum_node
= (struct f2fs_summary_block
*)page_address(sum_page
);
668 memcpy(curseg
->sum_blk
, sum_node
, SUM_ENTRY_SIZE
);
669 f2fs_put_page(sum_page
, 1);
673 static int get_ssr_segment(struct f2fs_sb_info
*sbi
, int type
)
675 struct curseg_info
*curseg
= CURSEG_I(sbi
, type
);
676 const struct victim_selection
*v_ops
= DIRTY_I(sbi
)->v_ops
;
678 if (IS_NODESEG(type
) || !has_not_enough_free_secs(sbi
, 0))
679 return v_ops
->get_victim(sbi
,
680 &(curseg
)->next_segno
, BG_GC
, type
, SSR
);
682 /* For data segments, let's do SSR more intensively */
683 for (; type
>= CURSEG_HOT_DATA
; type
--)
684 if (v_ops
->get_victim(sbi
, &(curseg
)->next_segno
,
691 * flush out current segment and replace it with new segment
692 * This function should be returned with success, otherwise BUG
694 static void allocate_segment_by_default(struct f2fs_sb_info
*sbi
,
695 int type
, bool force
)
697 struct curseg_info
*curseg
= CURSEG_I(sbi
, type
);
700 new_curseg(sbi
, type
, true);
701 else if (type
== CURSEG_WARM_NODE
)
702 new_curseg(sbi
, type
, false);
703 else if (curseg
->alloc_type
== LFS
&& is_next_segment_free(sbi
, type
))
704 new_curseg(sbi
, type
, false);
705 else if (need_SSR(sbi
) && get_ssr_segment(sbi
, type
))
706 change_curseg(sbi
, type
, true);
708 new_curseg(sbi
, type
, false);
710 stat_inc_seg_type(sbi
, curseg
);
713 void allocate_new_segments(struct f2fs_sb_info
*sbi
)
715 struct curseg_info
*curseg
;
716 unsigned int old_curseg
;
719 for (i
= CURSEG_HOT_DATA
; i
<= CURSEG_COLD_DATA
; i
++) {
720 curseg
= CURSEG_I(sbi
, i
);
721 old_curseg
= curseg
->segno
;
722 SIT_I(sbi
)->s_ops
->allocate_segment(sbi
, i
, true);
723 locate_dirty_segment(sbi
, old_curseg
);
727 static const struct segment_allocation default_salloc_ops
= {
728 .allocate_segment
= allocate_segment_by_default
,
731 static void f2fs_end_io_write(struct bio
*bio
, int err
)
733 const int uptodate
= test_bit(BIO_UPTODATE
, &bio
->bi_flags
);
734 struct bio_vec
*bvec
= bio
->bi_io_vec
+ bio
->bi_vcnt
- 1;
735 struct bio_private
*p
= bio
->bi_private
;
738 struct page
*page
= bvec
->bv_page
;
740 if (--bvec
>= bio
->bi_io_vec
)
741 prefetchw(&bvec
->bv_page
->flags
);
745 set_bit(AS_EIO
, &page
->mapping
->flags
);
746 set_ckpt_flags(p
->sbi
->ckpt
, CP_ERROR_FLAG
);
747 p
->sbi
->sb
->s_flags
|= MS_RDONLY
;
749 end_page_writeback(page
);
750 dec_page_count(p
->sbi
, F2FS_WRITEBACK
);
751 } while (bvec
>= bio
->bi_io_vec
);
756 if (!get_pages(p
->sbi
, F2FS_WRITEBACK
) &&
757 !list_empty(&p
->sbi
->cp_wait
.task_list
))
758 wake_up(&p
->sbi
->cp_wait
);
764 struct bio
*f2fs_bio_alloc(struct block_device
*bdev
, int npages
)
768 /* No failure on bio allocation */
769 bio
= bio_alloc(GFP_NOIO
, npages
);
771 bio
->bi_private
= NULL
;
776 static void do_submit_bio(struct f2fs_sb_info
*sbi
,
777 enum page_type type
, bool sync
)
779 int rw
= sync
? WRITE_SYNC
: WRITE
;
780 enum page_type btype
= type
> META
? META
: type
;
782 if (type
>= META_FLUSH
)
783 rw
= WRITE_FLUSH_FUA
;
788 if (sbi
->bio
[btype
]) {
789 struct bio_private
*p
= sbi
->bio
[btype
]->bi_private
;
791 sbi
->bio
[btype
]->bi_end_io
= f2fs_end_io_write
;
793 trace_f2fs_do_submit_bio(sbi
->sb
, btype
, sync
, sbi
->bio
[btype
]);
795 if (type
== META_FLUSH
) {
796 DECLARE_COMPLETION_ONSTACK(wait
);
799 submit_bio(rw
, sbi
->bio
[btype
]);
800 wait_for_completion(&wait
);
803 submit_bio(rw
, sbi
->bio
[btype
]);
805 sbi
->bio
[btype
] = NULL
;
809 void f2fs_submit_bio(struct f2fs_sb_info
*sbi
, enum page_type type
, bool sync
)
811 down_write(&sbi
->bio_sem
);
812 do_submit_bio(sbi
, type
, sync
);
813 up_write(&sbi
->bio_sem
);
816 static void submit_write_page(struct f2fs_sb_info
*sbi
, struct page
*page
,
817 block_t blk_addr
, enum page_type type
)
819 struct block_device
*bdev
= sbi
->sb
->s_bdev
;
822 verify_block_addr(sbi
, blk_addr
);
824 down_write(&sbi
->bio_sem
);
826 inc_page_count(sbi
, F2FS_WRITEBACK
);
828 if (sbi
->bio
[type
] && sbi
->last_block_in_bio
[type
] != blk_addr
- 1)
829 do_submit_bio(sbi
, type
, false);
831 if (sbi
->bio
[type
] == NULL
) {
832 struct bio_private
*priv
;
834 priv
= kmalloc(sizeof(struct bio_private
), GFP_NOFS
);
840 bio_blocks
= MAX_BIO_BLOCKS(max_hw_blocks(sbi
));
841 sbi
->bio
[type
] = f2fs_bio_alloc(bdev
, bio_blocks
);
842 sbi
->bio
[type
]->bi_sector
= SECTOR_FROM_BLOCK(sbi
, blk_addr
);
843 sbi
->bio
[type
]->bi_private
= priv
;
845 * The end_io will be assigned at the sumbission phase.
846 * Until then, let bio_add_page() merge consecutive IOs as much
851 if (bio_add_page(sbi
->bio
[type
], page
, PAGE_CACHE_SIZE
, 0) <
853 do_submit_bio(sbi
, type
, false);
857 sbi
->last_block_in_bio
[type
] = blk_addr
;
859 up_write(&sbi
->bio_sem
);
860 trace_f2fs_submit_write_page(page
, blk_addr
, type
);
863 void f2fs_wait_on_page_writeback(struct page
*page
,
864 enum page_type type
, bool sync
)
866 struct f2fs_sb_info
*sbi
= F2FS_SB(page
->mapping
->host
->i_sb
);
867 if (PageWriteback(page
)) {
868 f2fs_submit_bio(sbi
, type
, sync
);
869 wait_on_page_writeback(page
);
873 static bool __has_curseg_space(struct f2fs_sb_info
*sbi
, int type
)
875 struct curseg_info
*curseg
= CURSEG_I(sbi
, type
);
876 if (curseg
->next_blkoff
< sbi
->blocks_per_seg
)
881 static int __get_segment_type_2(struct page
*page
, enum page_type p_type
)
884 return CURSEG_HOT_DATA
;
886 return CURSEG_HOT_NODE
;
889 static int __get_segment_type_4(struct page
*page
, enum page_type p_type
)
891 if (p_type
== DATA
) {
892 struct inode
*inode
= page
->mapping
->host
;
894 if (S_ISDIR(inode
->i_mode
))
895 return CURSEG_HOT_DATA
;
897 return CURSEG_COLD_DATA
;
899 if (IS_DNODE(page
) && !is_cold_node(page
))
900 return CURSEG_HOT_NODE
;
902 return CURSEG_COLD_NODE
;
906 static int __get_segment_type_6(struct page
*page
, enum page_type p_type
)
908 if (p_type
== DATA
) {
909 struct inode
*inode
= page
->mapping
->host
;
911 if (S_ISDIR(inode
->i_mode
))
912 return CURSEG_HOT_DATA
;
913 else if (is_cold_data(page
) || file_is_cold(inode
))
914 return CURSEG_COLD_DATA
;
916 return CURSEG_WARM_DATA
;
919 return is_cold_node(page
) ? CURSEG_WARM_NODE
:
922 return CURSEG_COLD_NODE
;
926 static int __get_segment_type(struct page
*page
, enum page_type p_type
)
928 struct f2fs_sb_info
*sbi
= F2FS_SB(page
->mapping
->host
->i_sb
);
929 switch (sbi
->active_logs
) {
931 return __get_segment_type_2(page
, p_type
);
933 return __get_segment_type_4(page
, p_type
);
935 /* NR_CURSEG_TYPE(6) logs by default */
936 f2fs_bug_on(sbi
->active_logs
!= NR_CURSEG_TYPE
);
937 return __get_segment_type_6(page
, p_type
);
940 static void do_write_page(struct f2fs_sb_info
*sbi
, struct page
*page
,
941 block_t old_blkaddr
, block_t
*new_blkaddr
,
942 struct f2fs_summary
*sum
, enum page_type p_type
)
944 struct sit_info
*sit_i
= SIT_I(sbi
);
945 struct curseg_info
*curseg
;
946 unsigned int old_cursegno
;
949 type
= __get_segment_type(page
, p_type
);
950 curseg
= CURSEG_I(sbi
, type
);
952 mutex_lock(&curseg
->curseg_mutex
);
954 *new_blkaddr
= NEXT_FREE_BLKADDR(sbi
, curseg
);
955 old_cursegno
= curseg
->segno
;
958 * __add_sum_entry should be resided under the curseg_mutex
959 * because, this function updates a summary entry in the
960 * current summary block.
962 __add_sum_entry(sbi
, type
, sum
);
964 mutex_lock(&sit_i
->sentry_lock
);
965 __refresh_next_blkoff(sbi
, curseg
);
967 stat_inc_block_count(sbi
, curseg
);
970 * SIT information should be updated before segment allocation,
971 * since SSR needs latest valid block information.
973 refresh_sit_entry(sbi
, old_blkaddr
, *new_blkaddr
);
975 if (!__has_curseg_space(sbi
, type
))
976 sit_i
->s_ops
->allocate_segment(sbi
, type
, false);
978 locate_dirty_segment(sbi
, old_cursegno
);
979 locate_dirty_segment(sbi
, GET_SEGNO(sbi
, old_blkaddr
));
980 mutex_unlock(&sit_i
->sentry_lock
);
983 fill_node_footer_blkaddr(page
, NEXT_FREE_BLKADDR(sbi
, curseg
));
985 /* writeout dirty page into bdev */
986 submit_write_page(sbi
, page
, *new_blkaddr
, p_type
);
988 mutex_unlock(&curseg
->curseg_mutex
);
991 void write_meta_page(struct f2fs_sb_info
*sbi
, struct page
*page
)
993 set_page_writeback(page
);
994 submit_write_page(sbi
, page
, page
->index
, META
);
997 void write_node_page(struct f2fs_sb_info
*sbi
, struct page
*page
,
998 unsigned int nid
, block_t old_blkaddr
, block_t
*new_blkaddr
)
1000 struct f2fs_summary sum
;
1001 set_summary(&sum
, nid
, 0, 0);
1002 do_write_page(sbi
, page
, old_blkaddr
, new_blkaddr
, &sum
, NODE
);
1005 void write_data_page(struct inode
*inode
, struct page
*page
,
1006 struct dnode_of_data
*dn
, block_t old_blkaddr
,
1007 block_t
*new_blkaddr
)
1009 struct f2fs_sb_info
*sbi
= F2FS_SB(inode
->i_sb
);
1010 struct f2fs_summary sum
;
1011 struct node_info ni
;
1013 f2fs_bug_on(old_blkaddr
== NULL_ADDR
);
1014 get_node_info(sbi
, dn
->nid
, &ni
);
1015 set_summary(&sum
, dn
->nid
, dn
->ofs_in_node
, ni
.version
);
1017 do_write_page(sbi
, page
, old_blkaddr
,
1018 new_blkaddr
, &sum
, DATA
);
1021 void rewrite_data_page(struct f2fs_sb_info
*sbi
, struct page
*page
,
1022 block_t old_blk_addr
)
1024 submit_write_page(sbi
, page
, old_blk_addr
, DATA
);
1027 void recover_data_page(struct f2fs_sb_info
*sbi
,
1028 struct page
*page
, struct f2fs_summary
*sum
,
1029 block_t old_blkaddr
, block_t new_blkaddr
)
1031 struct sit_info
*sit_i
= SIT_I(sbi
);
1032 struct curseg_info
*curseg
;
1033 unsigned int segno
, old_cursegno
;
1034 struct seg_entry
*se
;
1037 segno
= GET_SEGNO(sbi
, new_blkaddr
);
1038 se
= get_seg_entry(sbi
, segno
);
1041 if (se
->valid_blocks
== 0 && !IS_CURSEG(sbi
, segno
)) {
1042 if (old_blkaddr
== NULL_ADDR
)
1043 type
= CURSEG_COLD_DATA
;
1045 type
= CURSEG_WARM_DATA
;
1047 curseg
= CURSEG_I(sbi
, type
);
1049 mutex_lock(&curseg
->curseg_mutex
);
1050 mutex_lock(&sit_i
->sentry_lock
);
1052 old_cursegno
= curseg
->segno
;
1054 /* change the current segment */
1055 if (segno
!= curseg
->segno
) {
1056 curseg
->next_segno
= segno
;
1057 change_curseg(sbi
, type
, true);
1060 curseg
->next_blkoff
= GET_SEGOFF_FROM_SEG0(sbi
, new_blkaddr
) &
1061 (sbi
->blocks_per_seg
- 1);
1062 __add_sum_entry(sbi
, type
, sum
);
1064 refresh_sit_entry(sbi
, old_blkaddr
, new_blkaddr
);
1066 locate_dirty_segment(sbi
, old_cursegno
);
1067 locate_dirty_segment(sbi
, GET_SEGNO(sbi
, old_blkaddr
));
1069 mutex_unlock(&sit_i
->sentry_lock
);
1070 mutex_unlock(&curseg
->curseg_mutex
);
1073 void rewrite_node_page(struct f2fs_sb_info
*sbi
,
1074 struct page
*page
, struct f2fs_summary
*sum
,
1075 block_t old_blkaddr
, block_t new_blkaddr
)
1077 struct sit_info
*sit_i
= SIT_I(sbi
);
1078 int type
= CURSEG_WARM_NODE
;
1079 struct curseg_info
*curseg
;
1080 unsigned int segno
, old_cursegno
;
1081 block_t next_blkaddr
= next_blkaddr_of_node(page
);
1082 unsigned int next_segno
= GET_SEGNO(sbi
, next_blkaddr
);
1084 curseg
= CURSEG_I(sbi
, type
);
1086 mutex_lock(&curseg
->curseg_mutex
);
1087 mutex_lock(&sit_i
->sentry_lock
);
1089 segno
= GET_SEGNO(sbi
, new_blkaddr
);
1090 old_cursegno
= curseg
->segno
;
1092 /* change the current segment */
1093 if (segno
!= curseg
->segno
) {
1094 curseg
->next_segno
= segno
;
1095 change_curseg(sbi
, type
, true);
1097 curseg
->next_blkoff
= GET_SEGOFF_FROM_SEG0(sbi
, new_blkaddr
) &
1098 (sbi
->blocks_per_seg
- 1);
1099 __add_sum_entry(sbi
, type
, sum
);
1101 /* change the current log to the next block addr in advance */
1102 if (next_segno
!= segno
) {
1103 curseg
->next_segno
= next_segno
;
1104 change_curseg(sbi
, type
, true);
1106 curseg
->next_blkoff
= GET_SEGOFF_FROM_SEG0(sbi
, next_blkaddr
) &
1107 (sbi
->blocks_per_seg
- 1);
1109 /* rewrite node page */
1110 set_page_writeback(page
);
1111 submit_write_page(sbi
, page
, new_blkaddr
, NODE
);
1112 f2fs_submit_bio(sbi
, NODE
, true);
1113 refresh_sit_entry(sbi
, old_blkaddr
, new_blkaddr
);
1115 locate_dirty_segment(sbi
, old_cursegno
);
1116 locate_dirty_segment(sbi
, GET_SEGNO(sbi
, old_blkaddr
));
1118 mutex_unlock(&sit_i
->sentry_lock
);
1119 mutex_unlock(&curseg
->curseg_mutex
);
1122 static int read_compacted_summaries(struct f2fs_sb_info
*sbi
)
1124 struct f2fs_checkpoint
*ckpt
= F2FS_CKPT(sbi
);
1125 struct curseg_info
*seg_i
;
1126 unsigned char *kaddr
;
1131 start
= start_sum_block(sbi
);
1133 page
= get_meta_page(sbi
, start
++);
1134 kaddr
= (unsigned char *)page_address(page
);
1136 /* Step 1: restore nat cache */
1137 seg_i
= CURSEG_I(sbi
, CURSEG_HOT_DATA
);
1138 memcpy(&seg_i
->sum_blk
->n_nats
, kaddr
, SUM_JOURNAL_SIZE
);
1140 /* Step 2: restore sit cache */
1141 seg_i
= CURSEG_I(sbi
, CURSEG_COLD_DATA
);
1142 memcpy(&seg_i
->sum_blk
->n_sits
, kaddr
+ SUM_JOURNAL_SIZE
,
1144 offset
= 2 * SUM_JOURNAL_SIZE
;
1146 /* Step 3: restore summary entries */
1147 for (i
= CURSEG_HOT_DATA
; i
<= CURSEG_COLD_DATA
; i
++) {
1148 unsigned short blk_off
;
1151 seg_i
= CURSEG_I(sbi
, i
);
1152 segno
= le32_to_cpu(ckpt
->cur_data_segno
[i
]);
1153 blk_off
= le16_to_cpu(ckpt
->cur_data_blkoff
[i
]);
1154 seg_i
->next_segno
= segno
;
1155 reset_curseg(sbi
, i
, 0);
1156 seg_i
->alloc_type
= ckpt
->alloc_type
[i
];
1157 seg_i
->next_blkoff
= blk_off
;
1159 if (seg_i
->alloc_type
== SSR
)
1160 blk_off
= sbi
->blocks_per_seg
;
1162 for (j
= 0; j
< blk_off
; j
++) {
1163 struct f2fs_summary
*s
;
1164 s
= (struct f2fs_summary
*)(kaddr
+ offset
);
1165 seg_i
->sum_blk
->entries
[j
] = *s
;
1166 offset
+= SUMMARY_SIZE
;
1167 if (offset
+ SUMMARY_SIZE
<= PAGE_CACHE_SIZE
-
1171 f2fs_put_page(page
, 1);
1174 page
= get_meta_page(sbi
, start
++);
1175 kaddr
= (unsigned char *)page_address(page
);
1179 f2fs_put_page(page
, 1);
1183 static int read_normal_summaries(struct f2fs_sb_info
*sbi
, int type
)
1185 struct f2fs_checkpoint
*ckpt
= F2FS_CKPT(sbi
);
1186 struct f2fs_summary_block
*sum
;
1187 struct curseg_info
*curseg
;
1189 unsigned short blk_off
;
1190 unsigned int segno
= 0;
1191 block_t blk_addr
= 0;
1193 /* get segment number and block addr */
1194 if (IS_DATASEG(type
)) {
1195 segno
= le32_to_cpu(ckpt
->cur_data_segno
[type
]);
1196 blk_off
= le16_to_cpu(ckpt
->cur_data_blkoff
[type
-
1198 if (is_set_ckpt_flags(ckpt
, CP_UMOUNT_FLAG
))
1199 blk_addr
= sum_blk_addr(sbi
, NR_CURSEG_TYPE
, type
);
1201 blk_addr
= sum_blk_addr(sbi
, NR_CURSEG_DATA_TYPE
, type
);
1203 segno
= le32_to_cpu(ckpt
->cur_node_segno
[type
-
1205 blk_off
= le16_to_cpu(ckpt
->cur_node_blkoff
[type
-
1207 if (is_set_ckpt_flags(ckpt
, CP_UMOUNT_FLAG
))
1208 blk_addr
= sum_blk_addr(sbi
, NR_CURSEG_NODE_TYPE
,
1209 type
- CURSEG_HOT_NODE
);
1211 blk_addr
= GET_SUM_BLOCK(sbi
, segno
);
1214 new = get_meta_page(sbi
, blk_addr
);
1215 sum
= (struct f2fs_summary_block
*)page_address(new);
1217 if (IS_NODESEG(type
)) {
1218 if (is_set_ckpt_flags(ckpt
, CP_UMOUNT_FLAG
)) {
1219 struct f2fs_summary
*ns
= &sum
->entries
[0];
1221 for (i
= 0; i
< sbi
->blocks_per_seg
; i
++, ns
++) {
1223 ns
->ofs_in_node
= 0;
1226 if (restore_node_summary(sbi
, segno
, sum
)) {
1227 f2fs_put_page(new, 1);
1233 /* set uncompleted segment to curseg */
1234 curseg
= CURSEG_I(sbi
, type
);
1235 mutex_lock(&curseg
->curseg_mutex
);
1236 memcpy(curseg
->sum_blk
, sum
, PAGE_CACHE_SIZE
);
1237 curseg
->next_segno
= segno
;
1238 reset_curseg(sbi
, type
, 0);
1239 curseg
->alloc_type
= ckpt
->alloc_type
[type
];
1240 curseg
->next_blkoff
= blk_off
;
1241 mutex_unlock(&curseg
->curseg_mutex
);
1242 f2fs_put_page(new, 1);
1246 static int restore_curseg_summaries(struct f2fs_sb_info
*sbi
)
1248 int type
= CURSEG_HOT_DATA
;
1250 if (is_set_ckpt_flags(F2FS_CKPT(sbi
), CP_COMPACT_SUM_FLAG
)) {
1251 /* restore for compacted data summary */
1252 if (read_compacted_summaries(sbi
))
1254 type
= CURSEG_HOT_NODE
;
1257 for (; type
<= CURSEG_COLD_NODE
; type
++)
1258 if (read_normal_summaries(sbi
, type
))
1263 static void write_compacted_summaries(struct f2fs_sb_info
*sbi
, block_t blkaddr
)
1266 unsigned char *kaddr
;
1267 struct f2fs_summary
*summary
;
1268 struct curseg_info
*seg_i
;
1269 int written_size
= 0;
1272 page
= grab_meta_page(sbi
, blkaddr
++);
1273 kaddr
= (unsigned char *)page_address(page
);
1275 /* Step 1: write nat cache */
1276 seg_i
= CURSEG_I(sbi
, CURSEG_HOT_DATA
);
1277 memcpy(kaddr
, &seg_i
->sum_blk
->n_nats
, SUM_JOURNAL_SIZE
);
1278 written_size
+= SUM_JOURNAL_SIZE
;
1280 /* Step 2: write sit cache */
1281 seg_i
= CURSEG_I(sbi
, CURSEG_COLD_DATA
);
1282 memcpy(kaddr
+ written_size
, &seg_i
->sum_blk
->n_sits
,
1284 written_size
+= SUM_JOURNAL_SIZE
;
1286 /* Step 3: write summary entries */
1287 for (i
= CURSEG_HOT_DATA
; i
<= CURSEG_COLD_DATA
; i
++) {
1288 unsigned short blkoff
;
1289 seg_i
= CURSEG_I(sbi
, i
);
1290 if (sbi
->ckpt
->alloc_type
[i
] == SSR
)
1291 blkoff
= sbi
->blocks_per_seg
;
1293 blkoff
= curseg_blkoff(sbi
, i
);
1295 for (j
= 0; j
< blkoff
; j
++) {
1297 page
= grab_meta_page(sbi
, blkaddr
++);
1298 kaddr
= (unsigned char *)page_address(page
);
1301 summary
= (struct f2fs_summary
*)(kaddr
+ written_size
);
1302 *summary
= seg_i
->sum_blk
->entries
[j
];
1303 written_size
+= SUMMARY_SIZE
;
1305 if (written_size
+ SUMMARY_SIZE
<= PAGE_CACHE_SIZE
-
1309 set_page_dirty(page
);
1310 f2fs_put_page(page
, 1);
1315 set_page_dirty(page
);
1316 f2fs_put_page(page
, 1);
1320 static void write_normal_summaries(struct f2fs_sb_info
*sbi
,
1321 block_t blkaddr
, int type
)
1324 if (IS_DATASEG(type
))
1325 end
= type
+ NR_CURSEG_DATA_TYPE
;
1327 end
= type
+ NR_CURSEG_NODE_TYPE
;
1329 for (i
= type
; i
< end
; i
++) {
1330 struct curseg_info
*sum
= CURSEG_I(sbi
, i
);
1331 mutex_lock(&sum
->curseg_mutex
);
1332 write_sum_page(sbi
, sum
->sum_blk
, blkaddr
+ (i
- type
));
1333 mutex_unlock(&sum
->curseg_mutex
);
1337 void write_data_summaries(struct f2fs_sb_info
*sbi
, block_t start_blk
)
1339 if (is_set_ckpt_flags(F2FS_CKPT(sbi
), CP_COMPACT_SUM_FLAG
))
1340 write_compacted_summaries(sbi
, start_blk
);
1342 write_normal_summaries(sbi
, start_blk
, CURSEG_HOT_DATA
);
1345 void write_node_summaries(struct f2fs_sb_info
*sbi
, block_t start_blk
)
1347 if (is_set_ckpt_flags(F2FS_CKPT(sbi
), CP_UMOUNT_FLAG
))
1348 write_normal_summaries(sbi
, start_blk
, CURSEG_HOT_NODE
);
1351 int lookup_journal_in_cursum(struct f2fs_summary_block
*sum
, int type
,
1352 unsigned int val
, int alloc
)
1356 if (type
== NAT_JOURNAL
) {
1357 for (i
= 0; i
< nats_in_cursum(sum
); i
++) {
1358 if (le32_to_cpu(nid_in_journal(sum
, i
)) == val
)
1361 if (alloc
&& nats_in_cursum(sum
) < NAT_JOURNAL_ENTRIES
)
1362 return update_nats_in_cursum(sum
, 1);
1363 } else if (type
== SIT_JOURNAL
) {
1364 for (i
= 0; i
< sits_in_cursum(sum
); i
++)
1365 if (le32_to_cpu(segno_in_journal(sum
, i
)) == val
)
1367 if (alloc
&& sits_in_cursum(sum
) < SIT_JOURNAL_ENTRIES
)
1368 return update_sits_in_cursum(sum
, 1);
1373 static struct page
*get_current_sit_page(struct f2fs_sb_info
*sbi
,
1376 struct sit_info
*sit_i
= SIT_I(sbi
);
1377 unsigned int offset
= SIT_BLOCK_OFFSET(sit_i
, segno
);
1378 block_t blk_addr
= sit_i
->sit_base_addr
+ offset
;
1380 check_seg_range(sbi
, segno
);
1382 /* calculate sit block address */
1383 if (f2fs_test_bit(offset
, sit_i
->sit_bitmap
))
1384 blk_addr
+= sit_i
->sit_blocks
;
1386 return get_meta_page(sbi
, blk_addr
);
1389 static struct page
*get_next_sit_page(struct f2fs_sb_info
*sbi
,
1392 struct sit_info
*sit_i
= SIT_I(sbi
);
1393 struct page
*src_page
, *dst_page
;
1394 pgoff_t src_off
, dst_off
;
1395 void *src_addr
, *dst_addr
;
1397 src_off
= current_sit_addr(sbi
, start
);
1398 dst_off
= next_sit_addr(sbi
, src_off
);
1400 /* get current sit block page without lock */
1401 src_page
= get_meta_page(sbi
, src_off
);
1402 dst_page
= grab_meta_page(sbi
, dst_off
);
1403 f2fs_bug_on(PageDirty(src_page
));
1405 src_addr
= page_address(src_page
);
1406 dst_addr
= page_address(dst_page
);
1407 memcpy(dst_addr
, src_addr
, PAGE_CACHE_SIZE
);
1409 set_page_dirty(dst_page
);
1410 f2fs_put_page(src_page
, 1);
1412 set_to_next_sit(sit_i
, start
);
1417 static bool flush_sits_in_journal(struct f2fs_sb_info
*sbi
)
1419 struct curseg_info
*curseg
= CURSEG_I(sbi
, CURSEG_COLD_DATA
);
1420 struct f2fs_summary_block
*sum
= curseg
->sum_blk
;
1424 * If the journal area in the current summary is full of sit entries,
1425 * all the sit entries will be flushed. Otherwise the sit entries
1426 * are not able to replace with newly hot sit entries.
1428 if (sits_in_cursum(sum
) >= SIT_JOURNAL_ENTRIES
) {
1429 for (i
= sits_in_cursum(sum
) - 1; i
>= 0; i
--) {
1431 segno
= le32_to_cpu(segno_in_journal(sum
, i
));
1432 __mark_sit_entry_dirty(sbi
, segno
);
1434 update_sits_in_cursum(sum
, -sits_in_cursum(sum
));
1441 * CP calls this function, which flushes SIT entries including sit_journal,
1442 * and moves prefree segs to free segs.
1444 void flush_sit_entries(struct f2fs_sb_info
*sbi
)
1446 struct sit_info
*sit_i
= SIT_I(sbi
);
1447 unsigned long *bitmap
= sit_i
->dirty_sentries_bitmap
;
1448 struct curseg_info
*curseg
= CURSEG_I(sbi
, CURSEG_COLD_DATA
);
1449 struct f2fs_summary_block
*sum
= curseg
->sum_blk
;
1450 unsigned long nsegs
= TOTAL_SEGS(sbi
);
1451 struct page
*page
= NULL
;
1452 struct f2fs_sit_block
*raw_sit
= NULL
;
1453 unsigned int start
= 0, end
= 0;
1454 unsigned int segno
= -1;
1457 mutex_lock(&curseg
->curseg_mutex
);
1458 mutex_lock(&sit_i
->sentry_lock
);
1461 * "flushed" indicates whether sit entries in journal are flushed
1462 * to the SIT area or not.
1464 flushed
= flush_sits_in_journal(sbi
);
1466 while ((segno
= find_next_bit(bitmap
, nsegs
, segno
+ 1)) < nsegs
) {
1467 struct seg_entry
*se
= get_seg_entry(sbi
, segno
);
1468 int sit_offset
, offset
;
1470 sit_offset
= SIT_ENTRY_OFFSET(sit_i
, segno
);
1475 offset
= lookup_journal_in_cursum(sum
, SIT_JOURNAL
, segno
, 1);
1477 segno_in_journal(sum
, offset
) = cpu_to_le32(segno
);
1478 seg_info_to_raw_sit(se
, &sit_in_journal(sum
, offset
));
1482 if (!page
|| (start
> segno
) || (segno
> end
)) {
1484 f2fs_put_page(page
, 1);
1488 start
= START_SEGNO(sit_i
, segno
);
1489 end
= start
+ SIT_ENTRY_PER_BLOCK
- 1;
1491 /* read sit block that will be updated */
1492 page
= get_next_sit_page(sbi
, start
);
1493 raw_sit
= page_address(page
);
1496 /* udpate entry in SIT block */
1497 seg_info_to_raw_sit(se
, &raw_sit
->entries
[sit_offset
]);
1499 __clear_bit(segno
, bitmap
);
1500 sit_i
->dirty_sentries
--;
1502 mutex_unlock(&sit_i
->sentry_lock
);
1503 mutex_unlock(&curseg
->curseg_mutex
);
1505 /* writeout last modified SIT block */
1506 f2fs_put_page(page
, 1);
1508 set_prefree_as_free_segments(sbi
);
1511 static int build_sit_info(struct f2fs_sb_info
*sbi
)
1513 struct f2fs_super_block
*raw_super
= F2FS_RAW_SUPER(sbi
);
1514 struct f2fs_checkpoint
*ckpt
= F2FS_CKPT(sbi
);
1515 struct sit_info
*sit_i
;
1516 unsigned int sit_segs
, start
;
1517 char *src_bitmap
, *dst_bitmap
;
1518 unsigned int bitmap_size
;
1520 /* allocate memory for SIT information */
1521 sit_i
= kzalloc(sizeof(struct sit_info
), GFP_KERNEL
);
1525 SM_I(sbi
)->sit_info
= sit_i
;
1527 sit_i
->sentries
= vzalloc(TOTAL_SEGS(sbi
) * sizeof(struct seg_entry
));
1528 if (!sit_i
->sentries
)
1531 bitmap_size
= f2fs_bitmap_size(TOTAL_SEGS(sbi
));
1532 sit_i
->dirty_sentries_bitmap
= kzalloc(bitmap_size
, GFP_KERNEL
);
1533 if (!sit_i
->dirty_sentries_bitmap
)
1536 for (start
= 0; start
< TOTAL_SEGS(sbi
); start
++) {
1537 sit_i
->sentries
[start
].cur_valid_map
1538 = kzalloc(SIT_VBLOCK_MAP_SIZE
, GFP_KERNEL
);
1539 sit_i
->sentries
[start
].ckpt_valid_map
1540 = kzalloc(SIT_VBLOCK_MAP_SIZE
, GFP_KERNEL
);
1541 if (!sit_i
->sentries
[start
].cur_valid_map
1542 || !sit_i
->sentries
[start
].ckpt_valid_map
)
1546 if (sbi
->segs_per_sec
> 1) {
1547 sit_i
->sec_entries
= vzalloc(TOTAL_SECS(sbi
) *
1548 sizeof(struct sec_entry
));
1549 if (!sit_i
->sec_entries
)
1553 /* get information related with SIT */
1554 sit_segs
= le32_to_cpu(raw_super
->segment_count_sit
) >> 1;
1556 /* setup SIT bitmap from ckeckpoint pack */
1557 bitmap_size
= __bitmap_size(sbi
, SIT_BITMAP
);
1558 src_bitmap
= __bitmap_ptr(sbi
, SIT_BITMAP
);
1560 dst_bitmap
= kmemdup(src_bitmap
, bitmap_size
, GFP_KERNEL
);
1564 /* init SIT information */
1565 sit_i
->s_ops
= &default_salloc_ops
;
1567 sit_i
->sit_base_addr
= le32_to_cpu(raw_super
->sit_blkaddr
);
1568 sit_i
->sit_blocks
= sit_segs
<< sbi
->log_blocks_per_seg
;
1569 sit_i
->written_valid_blocks
= le64_to_cpu(ckpt
->valid_block_count
);
1570 sit_i
->sit_bitmap
= dst_bitmap
;
1571 sit_i
->bitmap_size
= bitmap_size
;
1572 sit_i
->dirty_sentries
= 0;
1573 sit_i
->sents_per_block
= SIT_ENTRY_PER_BLOCK
;
1574 sit_i
->elapsed_time
= le64_to_cpu(sbi
->ckpt
->elapsed_time
);
1575 sit_i
->mounted_time
= CURRENT_TIME_SEC
.tv_sec
;
1576 mutex_init(&sit_i
->sentry_lock
);
1580 static int build_free_segmap(struct f2fs_sb_info
*sbi
)
1582 struct f2fs_sm_info
*sm_info
= SM_I(sbi
);
1583 struct free_segmap_info
*free_i
;
1584 unsigned int bitmap_size
, sec_bitmap_size
;
1586 /* allocate memory for free segmap information */
1587 free_i
= kzalloc(sizeof(struct free_segmap_info
), GFP_KERNEL
);
1591 SM_I(sbi
)->free_info
= free_i
;
1593 bitmap_size
= f2fs_bitmap_size(TOTAL_SEGS(sbi
));
1594 free_i
->free_segmap
= kmalloc(bitmap_size
, GFP_KERNEL
);
1595 if (!free_i
->free_segmap
)
1598 sec_bitmap_size
= f2fs_bitmap_size(TOTAL_SECS(sbi
));
1599 free_i
->free_secmap
= kmalloc(sec_bitmap_size
, GFP_KERNEL
);
1600 if (!free_i
->free_secmap
)
1603 /* set all segments as dirty temporarily */
1604 memset(free_i
->free_segmap
, 0xff, bitmap_size
);
1605 memset(free_i
->free_secmap
, 0xff, sec_bitmap_size
);
1607 /* init free segmap information */
1608 free_i
->start_segno
=
1609 (unsigned int) GET_SEGNO_FROM_SEG0(sbi
, sm_info
->main_blkaddr
);
1610 free_i
->free_segments
= 0;
1611 free_i
->free_sections
= 0;
1612 rwlock_init(&free_i
->segmap_lock
);
1616 static int build_curseg(struct f2fs_sb_info
*sbi
)
1618 struct curseg_info
*array
;
1621 array
= kzalloc(sizeof(*array
) * NR_CURSEG_TYPE
, GFP_KERNEL
);
1625 SM_I(sbi
)->curseg_array
= array
;
1627 for (i
= 0; i
< NR_CURSEG_TYPE
; i
++) {
1628 mutex_init(&array
[i
].curseg_mutex
);
1629 array
[i
].sum_blk
= kzalloc(PAGE_CACHE_SIZE
, GFP_KERNEL
);
1630 if (!array
[i
].sum_blk
)
1632 array
[i
].segno
= NULL_SEGNO
;
1633 array
[i
].next_blkoff
= 0;
1635 return restore_curseg_summaries(sbi
);
1638 static void build_sit_entries(struct f2fs_sb_info
*sbi
)
1640 struct sit_info
*sit_i
= SIT_I(sbi
);
1641 struct curseg_info
*curseg
= CURSEG_I(sbi
, CURSEG_COLD_DATA
);
1642 struct f2fs_summary_block
*sum
= curseg
->sum_blk
;
1645 for (start
= 0; start
< TOTAL_SEGS(sbi
); start
++) {
1646 struct seg_entry
*se
= &sit_i
->sentries
[start
];
1647 struct f2fs_sit_block
*sit_blk
;
1648 struct f2fs_sit_entry sit
;
1652 mutex_lock(&curseg
->curseg_mutex
);
1653 for (i
= 0; i
< sits_in_cursum(sum
); i
++) {
1654 if (le32_to_cpu(segno_in_journal(sum
, i
)) == start
) {
1655 sit
= sit_in_journal(sum
, i
);
1656 mutex_unlock(&curseg
->curseg_mutex
);
1660 mutex_unlock(&curseg
->curseg_mutex
);
1661 page
= get_current_sit_page(sbi
, start
);
1662 sit_blk
= (struct f2fs_sit_block
*)page_address(page
);
1663 sit
= sit_blk
->entries
[SIT_ENTRY_OFFSET(sit_i
, start
)];
1664 f2fs_put_page(page
, 1);
1666 check_block_count(sbi
, start
, &sit
);
1667 seg_info_from_raw_sit(se
, &sit
);
1668 if (sbi
->segs_per_sec
> 1) {
1669 struct sec_entry
*e
= get_sec_entry(sbi
, start
);
1670 e
->valid_blocks
+= se
->valid_blocks
;
1675 static void init_free_segmap(struct f2fs_sb_info
*sbi
)
1680 for (start
= 0; start
< TOTAL_SEGS(sbi
); start
++) {
1681 struct seg_entry
*sentry
= get_seg_entry(sbi
, start
);
1682 if (!sentry
->valid_blocks
)
1683 __set_free(sbi
, start
);
1686 /* set use the current segments */
1687 for (type
= CURSEG_HOT_DATA
; type
<= CURSEG_COLD_NODE
; type
++) {
1688 struct curseg_info
*curseg_t
= CURSEG_I(sbi
, type
);
1689 __set_test_and_inuse(sbi
, curseg_t
->segno
);
1693 static void init_dirty_segmap(struct f2fs_sb_info
*sbi
)
1695 struct dirty_seglist_info
*dirty_i
= DIRTY_I(sbi
);
1696 struct free_segmap_info
*free_i
= FREE_I(sbi
);
1697 unsigned int segno
= 0, offset
= 0, total_segs
= TOTAL_SEGS(sbi
);
1698 unsigned short valid_blocks
;
1701 /* find dirty segment based on free segmap */
1702 segno
= find_next_inuse(free_i
, total_segs
, offset
);
1703 if (segno
>= total_segs
)
1706 valid_blocks
= get_valid_blocks(sbi
, segno
, 0);
1707 if (valid_blocks
>= sbi
->blocks_per_seg
|| !valid_blocks
)
1709 mutex_lock(&dirty_i
->seglist_lock
);
1710 __locate_dirty_segment(sbi
, segno
, DIRTY
);
1711 mutex_unlock(&dirty_i
->seglist_lock
);
1715 static int init_victim_secmap(struct f2fs_sb_info
*sbi
)
1717 struct dirty_seglist_info
*dirty_i
= DIRTY_I(sbi
);
1718 unsigned int bitmap_size
= f2fs_bitmap_size(TOTAL_SECS(sbi
));
1720 dirty_i
->victim_secmap
= kzalloc(bitmap_size
, GFP_KERNEL
);
1721 if (!dirty_i
->victim_secmap
)
1726 static int build_dirty_segmap(struct f2fs_sb_info
*sbi
)
1728 struct dirty_seglist_info
*dirty_i
;
1729 unsigned int bitmap_size
, i
;
1731 /* allocate memory for dirty segments list information */
1732 dirty_i
= kzalloc(sizeof(struct dirty_seglist_info
), GFP_KERNEL
);
1736 SM_I(sbi
)->dirty_info
= dirty_i
;
1737 mutex_init(&dirty_i
->seglist_lock
);
1739 bitmap_size
= f2fs_bitmap_size(TOTAL_SEGS(sbi
));
1741 for (i
= 0; i
< NR_DIRTY_TYPE
; i
++) {
1742 dirty_i
->dirty_segmap
[i
] = kzalloc(bitmap_size
, GFP_KERNEL
);
1743 if (!dirty_i
->dirty_segmap
[i
])
1747 init_dirty_segmap(sbi
);
1748 return init_victim_secmap(sbi
);
1752 * Update min, max modified time for cost-benefit GC algorithm
1754 static void init_min_max_mtime(struct f2fs_sb_info
*sbi
)
1756 struct sit_info
*sit_i
= SIT_I(sbi
);
1759 mutex_lock(&sit_i
->sentry_lock
);
1761 sit_i
->min_mtime
= LLONG_MAX
;
1763 for (segno
= 0; segno
< TOTAL_SEGS(sbi
); segno
+= sbi
->segs_per_sec
) {
1765 unsigned long long mtime
= 0;
1767 for (i
= 0; i
< sbi
->segs_per_sec
; i
++)
1768 mtime
+= get_seg_entry(sbi
, segno
+ i
)->mtime
;
1770 mtime
= div_u64(mtime
, sbi
->segs_per_sec
);
1772 if (sit_i
->min_mtime
> mtime
)
1773 sit_i
->min_mtime
= mtime
;
1775 sit_i
->max_mtime
= get_mtime(sbi
);
1776 mutex_unlock(&sit_i
->sentry_lock
);
1779 int build_segment_manager(struct f2fs_sb_info
*sbi
)
1781 struct f2fs_super_block
*raw_super
= F2FS_RAW_SUPER(sbi
);
1782 struct f2fs_checkpoint
*ckpt
= F2FS_CKPT(sbi
);
1783 struct f2fs_sm_info
*sm_info
;
1786 sm_info
= kzalloc(sizeof(struct f2fs_sm_info
), GFP_KERNEL
);
1791 sbi
->sm_info
= sm_info
;
1792 INIT_LIST_HEAD(&sm_info
->wblist_head
);
1793 spin_lock_init(&sm_info
->wblist_lock
);
1794 sm_info
->seg0_blkaddr
= le32_to_cpu(raw_super
->segment0_blkaddr
);
1795 sm_info
->main_blkaddr
= le32_to_cpu(raw_super
->main_blkaddr
);
1796 sm_info
->segment_count
= le32_to_cpu(raw_super
->segment_count
);
1797 sm_info
->reserved_segments
= le32_to_cpu(ckpt
->rsvd_segment_count
);
1798 sm_info
->ovp_segments
= le32_to_cpu(ckpt
->overprov_segment_count
);
1799 sm_info
->main_segments
= le32_to_cpu(raw_super
->segment_count_main
);
1800 sm_info
->ssa_blkaddr
= le32_to_cpu(raw_super
->ssa_blkaddr
);
1801 sm_info
->rec_prefree_segments
= DEF_RECLAIM_PREFREE_SEGMENTS
;
1803 INIT_LIST_HEAD(&sm_info
->discard_list
);
1804 sm_info
->nr_discards
= 0;
1805 sm_info
->max_discards
= 0;
1807 err
= build_sit_info(sbi
);
1810 err
= build_free_segmap(sbi
);
1813 err
= build_curseg(sbi
);
1817 /* reinit free segmap based on SIT */
1818 build_sit_entries(sbi
);
1820 init_free_segmap(sbi
);
1821 err
= build_dirty_segmap(sbi
);
1825 init_min_max_mtime(sbi
);
1829 static void discard_dirty_segmap(struct f2fs_sb_info
*sbi
,
1830 enum dirty_type dirty_type
)
1832 struct dirty_seglist_info
*dirty_i
= DIRTY_I(sbi
);
1834 mutex_lock(&dirty_i
->seglist_lock
);
1835 kfree(dirty_i
->dirty_segmap
[dirty_type
]);
1836 dirty_i
->nr_dirty
[dirty_type
] = 0;
1837 mutex_unlock(&dirty_i
->seglist_lock
);
1840 static void destroy_victim_secmap(struct f2fs_sb_info
*sbi
)
1842 struct dirty_seglist_info
*dirty_i
= DIRTY_I(sbi
);
1843 kfree(dirty_i
->victim_secmap
);
1846 static void destroy_dirty_segmap(struct f2fs_sb_info
*sbi
)
1848 struct dirty_seglist_info
*dirty_i
= DIRTY_I(sbi
);
1854 /* discard pre-free/dirty segments list */
1855 for (i
= 0; i
< NR_DIRTY_TYPE
; i
++)
1856 discard_dirty_segmap(sbi
, i
);
1858 destroy_victim_secmap(sbi
);
1859 SM_I(sbi
)->dirty_info
= NULL
;
1863 static void destroy_curseg(struct f2fs_sb_info
*sbi
)
1865 struct curseg_info
*array
= SM_I(sbi
)->curseg_array
;
1870 SM_I(sbi
)->curseg_array
= NULL
;
1871 for (i
= 0; i
< NR_CURSEG_TYPE
; i
++)
1872 kfree(array
[i
].sum_blk
);
1876 static void destroy_free_segmap(struct f2fs_sb_info
*sbi
)
1878 struct free_segmap_info
*free_i
= SM_I(sbi
)->free_info
;
1881 SM_I(sbi
)->free_info
= NULL
;
1882 kfree(free_i
->free_segmap
);
1883 kfree(free_i
->free_secmap
);
1887 static void destroy_sit_info(struct f2fs_sb_info
*sbi
)
1889 struct sit_info
*sit_i
= SIT_I(sbi
);
1895 if (sit_i
->sentries
) {
1896 for (start
= 0; start
< TOTAL_SEGS(sbi
); start
++) {
1897 kfree(sit_i
->sentries
[start
].cur_valid_map
);
1898 kfree(sit_i
->sentries
[start
].ckpt_valid_map
);
1901 vfree(sit_i
->sentries
);
1902 vfree(sit_i
->sec_entries
);
1903 kfree(sit_i
->dirty_sentries_bitmap
);
1905 SM_I(sbi
)->sit_info
= NULL
;
1906 kfree(sit_i
->sit_bitmap
);
1910 void destroy_segment_manager(struct f2fs_sb_info
*sbi
)
1912 struct f2fs_sm_info
*sm_info
= SM_I(sbi
);
1915 destroy_dirty_segmap(sbi
);
1916 destroy_curseg(sbi
);
1917 destroy_free_segmap(sbi
);
1918 destroy_sit_info(sbi
);
1919 sbi
->sm_info
= NULL
;
1923 int __init
create_segment_manager_caches(void)
1925 discard_entry_slab
= f2fs_kmem_cache_create("discard_entry",
1926 sizeof(struct discard_entry
), NULL
);
1927 if (!discard_entry_slab
)
1932 void destroy_segment_manager_caches(void)
1934 kmem_cache_destroy(discard_entry_slab
);