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))
26 * __reverse_ffs is copied from include/asm-generic/bitops/__ffs.h since
27 * MSB and LSB are reversed in a byte by f2fs_set_bit.
29 static inline unsigned long __reverse_ffs(unsigned long word)
33 #if BITS_PER_LONG == 64
34 if ((word & 0xffffffff) == 0) {
39 if ((word & 0xffff) == 0) {
43 if ((word & 0xff) == 0) {
47 if ((word & 0xf0) == 0)
51 if ((word & 0xc) == 0)
55 if ((word & 0x2) == 0)
61 * __find_rev_next(_zero)_bit is copied from lib/find_next_bit.c becasue
62 * f2fs_set_bit makes MSB and LSB reversed in a byte.
65 * f2fs_set_bit(0, bitmap) => 0000 0001
66 * f2fs_set_bit(7, bitmap) => 1000 0000
68 static unsigned long __find_rev_next_bit(const unsigned long *addr,
69 unsigned long size, unsigned long offset)
71 const unsigned long *p = addr + BIT_WORD(offset);
72 unsigned long result = offset & ~(BITS_PER_LONG - 1);
74 unsigned long mask, submask;
75 unsigned long quot, rest;
81 offset %= BITS_PER_LONG;
86 quot = (offset >> 3) << 3;
89 submask = (unsigned char)(0xff << rest) >> rest;
93 if (size < BITS_PER_LONG)
98 size -= BITS_PER_LONG;
99 result += BITS_PER_LONG;
101 while (size & ~(BITS_PER_LONG-1)) {
105 result += BITS_PER_LONG;
106 size -= BITS_PER_LONG;
112 tmp &= (~0UL >> (BITS_PER_LONG - size));
113 if (tmp == 0UL) /* Are any bits set? */
114 return result + size; /* Nope. */
116 return result + __reverse_ffs(tmp);
119 static unsigned long __find_rev_next_zero_bit(const unsigned long *addr,
120 unsigned long size, unsigned long offset)
122 const unsigned long *p = addr + BIT_WORD(offset);
123 unsigned long result = offset & ~(BITS_PER_LONG - 1);
125 unsigned long mask, submask;
126 unsigned long quot, rest;
132 offset %= BITS_PER_LONG;
137 quot = (offset >> 3) << 3;
139 mask = ~(~0UL << quot);
140 submask = (unsigned char)~((unsigned char)(0xff << rest) >> rest);
144 if (size < BITS_PER_LONG)
149 size -= BITS_PER_LONG;
150 result += BITS_PER_LONG;
152 while (size & ~(BITS_PER_LONG - 1)) {
156 result += BITS_PER_LONG;
157 size -= BITS_PER_LONG;
165 if (tmp == ~0UL) /* Are any bits zero? */
166 return result + size; /* Nope. */
168 return result + __reverse_ffz(tmp);
172 * This function balances dirty node and dentry pages.
173 * In addition, it controls garbage collection.
175 void f2fs_balance_fs(struct f2fs_sb_info *sbi)
178 * We should do GC or end up with checkpoint, if there are so many dirty
179 * dir/node pages without enough free segments.
181 if (has_not_enough_free_secs(sbi, 0)) {
182 mutex_lock(&sbi->gc_mutex);
187 void f2fs_balance_fs_bg(struct f2fs_sb_info *sbi)
189 /* check the # of cached NAT entries and prefree segments */
190 if (try_to_free_nats(sbi, NAT_ENTRY_PER_BLOCK) ||
191 excess_prefree_segs(sbi))
192 f2fs_sync_fs(sbi->sb, true);
195 static void __locate_dirty_segment(struct f2fs_sb_info *sbi, unsigned int segno,
196 enum dirty_type dirty_type)
198 struct dirty_seglist_info *dirty_i = DIRTY_I(sbi);
200 /* need not be added */
201 if (IS_CURSEG(sbi, segno))
204 if (!test_and_set_bit(segno, dirty_i->dirty_segmap[dirty_type]))
205 dirty_i->nr_dirty[dirty_type]++;
207 if (dirty_type == DIRTY) {
208 struct seg_entry *sentry = get_seg_entry(sbi, segno);
209 enum dirty_type t = sentry->type;
211 if (!test_and_set_bit(segno, dirty_i->dirty_segmap[t]))
212 dirty_i->nr_dirty[t]++;
216 static void __remove_dirty_segment(struct f2fs_sb_info *sbi, unsigned int segno,
217 enum dirty_type dirty_type)
219 struct dirty_seglist_info *dirty_i = DIRTY_I(sbi);
221 if (test_and_clear_bit(segno, dirty_i->dirty_segmap[dirty_type]))
222 dirty_i->nr_dirty[dirty_type]--;
224 if (dirty_type == DIRTY) {
225 struct seg_entry *sentry = get_seg_entry(sbi, segno);
226 enum dirty_type t = sentry->type;
228 if (test_and_clear_bit(segno, dirty_i->dirty_segmap[t]))
229 dirty_i->nr_dirty[t]--;
231 if (get_valid_blocks(sbi, segno, sbi->segs_per_sec) == 0)
232 clear_bit(GET_SECNO(sbi, segno),
233 dirty_i->victim_secmap);
238 * Should not occur error such as -ENOMEM.
239 * Adding dirty entry into seglist is not critical operation.
240 * If a given segment is one of current working segments, it won't be added.
242 static void locate_dirty_segment(struct f2fs_sb_info *sbi, unsigned int segno)
244 struct dirty_seglist_info *dirty_i = DIRTY_I(sbi);
245 unsigned short valid_blocks;
247 if (segno == NULL_SEGNO || IS_CURSEG(sbi, segno))
250 mutex_lock(&dirty_i->seglist_lock);
252 valid_blocks = get_valid_blocks(sbi, segno, 0);
254 if (valid_blocks == 0) {
255 __locate_dirty_segment(sbi, segno, PRE);
256 __remove_dirty_segment(sbi, segno, DIRTY);
257 } else if (valid_blocks < sbi->blocks_per_seg) {
258 __locate_dirty_segment(sbi, segno, DIRTY);
260 /* Recovery routine with SSR needs this */
261 __remove_dirty_segment(sbi, segno, DIRTY);
264 mutex_unlock(&dirty_i->seglist_lock);
268 * Should call clear_prefree_segments after checkpoint is done.
270 static void set_prefree_as_free_segments(struct f2fs_sb_info *sbi)
272 struct dirty_seglist_info *dirty_i = DIRTY_I(sbi);
273 unsigned int segno = -1;
274 unsigned int total_segs = TOTAL_SEGS(sbi);
276 mutex_lock(&dirty_i->seglist_lock);
278 segno = find_next_bit(dirty_i->dirty_segmap[PRE], total_segs,
280 if (segno >= total_segs)
282 __set_test_and_free(sbi, segno);
284 mutex_unlock(&dirty_i->seglist_lock);
287 void clear_prefree_segments(struct f2fs_sb_info *sbi)
289 struct dirty_seglist_info *dirty_i = DIRTY_I(sbi);
290 unsigned long *prefree_map = dirty_i->dirty_segmap[PRE];
291 unsigned int total_segs = TOTAL_SEGS(sbi);
292 unsigned int start = 0, end = -1;
294 mutex_lock(&dirty_i->seglist_lock);
298 start = find_next_bit(prefree_map, total_segs, end + 1);
299 if (start >= total_segs)
301 end = find_next_zero_bit(prefree_map, total_segs, start + 1);
303 for (i = start; i < end; i++)
304 clear_bit(i, prefree_map);
306 dirty_i->nr_dirty[PRE] -= end - start;
308 if (!test_opt(sbi, DISCARD))
311 blkdev_issue_discard(sbi->sb->s_bdev,
312 START_BLOCK(sbi, start) <<
313 sbi->log_sectors_per_block,
314 (1 << (sbi->log_sectors_per_block +
315 sbi->log_blocks_per_seg)) * (end - start),
318 mutex_unlock(&dirty_i->seglist_lock);
321 static void __mark_sit_entry_dirty(struct f2fs_sb_info *sbi, unsigned int segno)
323 struct sit_info *sit_i = SIT_I(sbi);
324 if (!__test_and_set_bit(segno, sit_i->dirty_sentries_bitmap))
325 sit_i->dirty_sentries++;
328 static void __set_sit_entry_type(struct f2fs_sb_info *sbi, int type,
329 unsigned int segno, int modified)
331 struct seg_entry *se = get_seg_entry(sbi, segno);
334 __mark_sit_entry_dirty(sbi, segno);
337 static void update_sit_entry(struct f2fs_sb_info *sbi, block_t blkaddr, int del)
339 struct seg_entry *se;
340 unsigned int segno, offset;
341 long int new_vblocks;
343 segno = GET_SEGNO(sbi, blkaddr);
345 se = get_seg_entry(sbi, segno);
346 new_vblocks = se->valid_blocks + del;
347 offset = GET_SEGOFF_FROM_SEG0(sbi, blkaddr) & (sbi->blocks_per_seg - 1);
349 f2fs_bug_on((new_vblocks >> (sizeof(unsigned short) << 3) ||
350 (new_vblocks > sbi->blocks_per_seg)));
352 se->valid_blocks = new_vblocks;
353 se->mtime = get_mtime(sbi);
354 SIT_I(sbi)->max_mtime = se->mtime;
356 /* Update valid block bitmap */
358 if (f2fs_set_bit(offset, se->cur_valid_map))
361 if (!f2fs_clear_bit(offset, se->cur_valid_map))
364 if (!f2fs_test_bit(offset, se->ckpt_valid_map))
365 se->ckpt_valid_blocks += del;
367 __mark_sit_entry_dirty(sbi, segno);
369 /* update total number of valid blocks to be written in ckpt area */
370 SIT_I(sbi)->written_valid_blocks += del;
372 if (sbi->segs_per_sec > 1)
373 get_sec_entry(sbi, segno)->valid_blocks += del;
376 static void refresh_sit_entry(struct f2fs_sb_info *sbi,
377 block_t old_blkaddr, block_t new_blkaddr)
379 update_sit_entry(sbi, new_blkaddr, 1);
380 if (GET_SEGNO(sbi, old_blkaddr) != NULL_SEGNO)
381 update_sit_entry(sbi, old_blkaddr, -1);
384 void invalidate_blocks(struct f2fs_sb_info *sbi, block_t addr)
386 unsigned int segno = GET_SEGNO(sbi, addr);
387 struct sit_info *sit_i = SIT_I(sbi);
389 f2fs_bug_on(addr == NULL_ADDR);
390 if (addr == NEW_ADDR)
393 /* add it into sit main buffer */
394 mutex_lock(&sit_i->sentry_lock);
396 update_sit_entry(sbi, addr, -1);
398 /* add it into dirty seglist */
399 locate_dirty_segment(sbi, segno);
401 mutex_unlock(&sit_i->sentry_lock);
405 * This function should be resided under the curseg_mutex lock
407 static void __add_sum_entry(struct f2fs_sb_info *sbi, int type,
408 struct f2fs_summary *sum)
410 struct curseg_info *curseg = CURSEG_I(sbi, type);
411 void *addr = curseg->sum_blk;
412 addr += curseg->next_blkoff * sizeof(struct f2fs_summary);
413 memcpy(addr, sum, sizeof(struct f2fs_summary));
417 * Calculate the number of current summary pages for writing
419 int npages_for_summary_flush(struct f2fs_sb_info *sbi)
421 int valid_sum_count = 0;
424 for (i = CURSEG_HOT_DATA; i <= CURSEG_COLD_DATA; i++) {
425 if (sbi->ckpt->alloc_type[i] == SSR)
426 valid_sum_count += sbi->blocks_per_seg;
428 valid_sum_count += curseg_blkoff(sbi, i);
431 sum_in_page = (PAGE_CACHE_SIZE - 2 * SUM_JOURNAL_SIZE -
432 SUM_FOOTER_SIZE) / SUMMARY_SIZE;
433 if (valid_sum_count <= sum_in_page)
435 else if ((valid_sum_count - sum_in_page) <=
436 (PAGE_CACHE_SIZE - SUM_FOOTER_SIZE) / SUMMARY_SIZE)
442 * Caller should put this summary page
444 struct page *get_sum_page(struct f2fs_sb_info *sbi, unsigned int segno)
446 return get_meta_page(sbi, GET_SUM_BLOCK(sbi, segno));
449 static void write_sum_page(struct f2fs_sb_info *sbi,
450 struct f2fs_summary_block *sum_blk, block_t blk_addr)
452 struct page *page = grab_meta_page(sbi, blk_addr);
453 void *kaddr = page_address(page);
454 memcpy(kaddr, sum_blk, PAGE_CACHE_SIZE);
455 set_page_dirty(page);
456 f2fs_put_page(page, 1);
459 static int is_next_segment_free(struct f2fs_sb_info *sbi, int type)
461 struct curseg_info *curseg = CURSEG_I(sbi, type);
462 unsigned int segno = curseg->segno + 1;
463 struct free_segmap_info *free_i = FREE_I(sbi);
465 if (segno < TOTAL_SEGS(sbi) && segno % sbi->segs_per_sec)
466 return !test_bit(segno, free_i->free_segmap);
471 * Find a new segment from the free segments bitmap to right order
472 * This function should be returned with success, otherwise BUG
474 static void get_new_segment(struct f2fs_sb_info *sbi,
475 unsigned int *newseg, bool new_sec, int dir)
477 struct free_segmap_info *free_i = FREE_I(sbi);
478 unsigned int segno, secno, zoneno;
479 unsigned int total_zones = TOTAL_SECS(sbi) / sbi->secs_per_zone;
480 unsigned int hint = *newseg / sbi->segs_per_sec;
481 unsigned int old_zoneno = GET_ZONENO_FROM_SEGNO(sbi, *newseg);
482 unsigned int left_start = hint;
487 write_lock(&free_i->segmap_lock);
489 if (!new_sec && ((*newseg + 1) % sbi->segs_per_sec)) {
490 segno = find_next_zero_bit(free_i->free_segmap,
491 TOTAL_SEGS(sbi), *newseg + 1);
492 if (segno - *newseg < sbi->segs_per_sec -
493 (*newseg % sbi->segs_per_sec))
497 secno = find_next_zero_bit(free_i->free_secmap, TOTAL_SECS(sbi), hint);
498 if (secno >= TOTAL_SECS(sbi)) {
499 if (dir == ALLOC_RIGHT) {
500 secno = find_next_zero_bit(free_i->free_secmap,
502 f2fs_bug_on(secno >= TOTAL_SECS(sbi));
505 left_start = hint - 1;
511 while (test_bit(left_start, free_i->free_secmap)) {
512 if (left_start > 0) {
516 left_start = find_next_zero_bit(free_i->free_secmap,
518 f2fs_bug_on(left_start >= TOTAL_SECS(sbi));
524 segno = secno * sbi->segs_per_sec;
525 zoneno = secno / sbi->secs_per_zone;
527 /* give up on finding another zone */
530 if (sbi->secs_per_zone == 1)
532 if (zoneno == old_zoneno)
534 if (dir == ALLOC_LEFT) {
535 if (!go_left && zoneno + 1 >= total_zones)
537 if (go_left && zoneno == 0)
540 for (i = 0; i < NR_CURSEG_TYPE; i++)
541 if (CURSEG_I(sbi, i)->zone == zoneno)
544 if (i < NR_CURSEG_TYPE) {
545 /* zone is in user, try another */
547 hint = zoneno * sbi->secs_per_zone - 1;
548 else if (zoneno + 1 >= total_zones)
551 hint = (zoneno + 1) * sbi->secs_per_zone;
553 goto find_other_zone;
556 /* set it as dirty segment in free segmap */
557 f2fs_bug_on(test_bit(segno, free_i->free_segmap));
558 __set_inuse(sbi, segno);
560 write_unlock(&free_i->segmap_lock);
563 static void reset_curseg(struct f2fs_sb_info *sbi, int type, int modified)
565 struct curseg_info *curseg = CURSEG_I(sbi, type);
566 struct summary_footer *sum_footer;
568 curseg->segno = curseg->next_segno;
569 curseg->zone = GET_ZONENO_FROM_SEGNO(sbi, curseg->segno);
570 curseg->next_blkoff = 0;
571 curseg->next_segno = NULL_SEGNO;
573 sum_footer = &(curseg->sum_blk->footer);
574 memset(sum_footer, 0, sizeof(struct summary_footer));
575 if (IS_DATASEG(type))
576 SET_SUM_TYPE(sum_footer, SUM_TYPE_DATA);
577 if (IS_NODESEG(type))
578 SET_SUM_TYPE(sum_footer, SUM_TYPE_NODE);
579 __set_sit_entry_type(sbi, type, curseg->segno, modified);
583 * Allocate a current working segment.
584 * This function always allocates a free segment in LFS manner.
586 static void new_curseg(struct f2fs_sb_info *sbi, int type, bool new_sec)
588 struct curseg_info *curseg = CURSEG_I(sbi, type);
589 unsigned int segno = curseg->segno;
590 int dir = ALLOC_LEFT;
592 write_sum_page(sbi, curseg->sum_blk,
593 GET_SUM_BLOCK(sbi, segno));
594 if (type == CURSEG_WARM_DATA || type == CURSEG_COLD_DATA)
597 if (test_opt(sbi, NOHEAP))
600 get_new_segment(sbi, &segno, new_sec, dir);
601 curseg->next_segno = segno;
602 reset_curseg(sbi, type, 1);
603 curseg->alloc_type = LFS;
606 static void __next_free_blkoff(struct f2fs_sb_info *sbi,
607 struct curseg_info *seg, block_t start)
609 struct seg_entry *se = get_seg_entry(sbi, seg->segno);
610 int entries = SIT_VBLOCK_MAP_SIZE / sizeof(unsigned long);
611 unsigned long target_map[entries];
612 unsigned long *ckpt_map = (unsigned long *)se->ckpt_valid_map;
613 unsigned long *cur_map = (unsigned long *)se->cur_valid_map;
616 for (i = 0; i < entries; i++)
617 target_map[i] = ckpt_map[i] | cur_map[i];
619 pos = __find_rev_next_zero_bit(target_map, sbi->blocks_per_seg, start);
621 seg->next_blkoff = pos;
625 * If a segment is written by LFS manner, next block offset is just obtained
626 * by increasing the current block offset. However, if a segment is written by
627 * SSR manner, next block offset obtained by calling __next_free_blkoff
629 static void __refresh_next_blkoff(struct f2fs_sb_info *sbi,
630 struct curseg_info *seg)
632 if (seg->alloc_type == SSR)
633 __next_free_blkoff(sbi, seg, seg->next_blkoff + 1);
639 * This function always allocates a used segment (from dirty seglist) by SSR
640 * manner, so it should recover the existing segment information of valid blocks
642 static void change_curseg(struct f2fs_sb_info *sbi, int type, bool reuse)
644 struct dirty_seglist_info *dirty_i = DIRTY_I(sbi);
645 struct curseg_info *curseg = CURSEG_I(sbi, type);
646 unsigned int new_segno = curseg->next_segno;
647 struct f2fs_summary_block *sum_node;
648 struct page *sum_page;
650 write_sum_page(sbi, curseg->sum_blk,
651 GET_SUM_BLOCK(sbi, curseg->segno));
652 __set_test_and_inuse(sbi, new_segno);
654 mutex_lock(&dirty_i->seglist_lock);
655 __remove_dirty_segment(sbi, new_segno, PRE);
656 __remove_dirty_segment(sbi, new_segno, DIRTY);
657 mutex_unlock(&dirty_i->seglist_lock);
659 reset_curseg(sbi, type, 1);
660 curseg->alloc_type = SSR;
661 __next_free_blkoff(sbi, curseg, 0);
664 sum_page = get_sum_page(sbi, new_segno);
665 sum_node = (struct f2fs_summary_block *)page_address(sum_page);
666 memcpy(curseg->sum_blk, sum_node, SUM_ENTRY_SIZE);
667 f2fs_put_page(sum_page, 1);
671 static int get_ssr_segment(struct f2fs_sb_info *sbi, int type)
673 struct curseg_info *curseg = CURSEG_I(sbi, type);
674 const struct victim_selection *v_ops = DIRTY_I(sbi)->v_ops;
676 if (IS_NODESEG(type) || !has_not_enough_free_secs(sbi, 0))
677 return v_ops->get_victim(sbi,
678 &(curseg)->next_segno, BG_GC, type, SSR);
680 /* For data segments, let's do SSR more intensively */
681 for (; type >= CURSEG_HOT_DATA; type--)
682 if (v_ops->get_victim(sbi, &(curseg)->next_segno,
689 * flush out current segment and replace it with new segment
690 * This function should be returned with success, otherwise BUG
692 static void allocate_segment_by_default(struct f2fs_sb_info *sbi,
693 int type, bool force)
695 struct curseg_info *curseg = CURSEG_I(sbi, type);
698 new_curseg(sbi, type, true);
699 else if (type == CURSEG_WARM_NODE)
700 new_curseg(sbi, type, false);
701 else if (curseg->alloc_type == LFS && is_next_segment_free(sbi, type))
702 new_curseg(sbi, type, false);
703 else if (need_SSR(sbi) && get_ssr_segment(sbi, type))
704 change_curseg(sbi, type, true);
706 new_curseg(sbi, type, false);
708 stat_inc_seg_type(sbi, curseg);
711 void allocate_new_segments(struct f2fs_sb_info *sbi)
713 struct curseg_info *curseg;
714 unsigned int old_curseg;
717 for (i = CURSEG_HOT_DATA; i <= CURSEG_COLD_DATA; i++) {
718 curseg = CURSEG_I(sbi, i);
719 old_curseg = curseg->segno;
720 SIT_I(sbi)->s_ops->allocate_segment(sbi, i, true);
721 locate_dirty_segment(sbi, old_curseg);
725 static const struct segment_allocation default_salloc_ops = {
726 .allocate_segment = allocate_segment_by_default,
729 static void f2fs_end_io_write(struct bio *bio, int err)
731 const int uptodate = test_bit(BIO_UPTODATE, &bio->bi_flags);
732 struct bio_vec *bvec = bio->bi_io_vec + bio->bi_vcnt - 1;
733 struct bio_private *p = bio->bi_private;
736 struct page *page = bvec->bv_page;
738 if (--bvec >= bio->bi_io_vec)
739 prefetchw(&bvec->bv_page->flags);
743 set_bit(AS_EIO, &page->mapping->flags);
744 set_ckpt_flags(p->sbi->ckpt, CP_ERROR_FLAG);
745 p->sbi->sb->s_flags |= MS_RDONLY;
747 end_page_writeback(page);
748 dec_page_count(p->sbi, F2FS_WRITEBACK);
749 } while (bvec >= bio->bi_io_vec);
754 if (!get_pages(p->sbi, F2FS_WRITEBACK) &&
755 !list_empty(&p->sbi->cp_wait.task_list))
756 wake_up(&p->sbi->cp_wait);
762 struct bio *f2fs_bio_alloc(struct block_device *bdev, int npages)
766 /* No failure on bio allocation */
767 bio = bio_alloc(GFP_NOIO, npages);
769 bio->bi_private = NULL;
774 static void do_submit_bio(struct f2fs_sb_info *sbi,
775 enum page_type type, bool sync)
777 int rw = sync ? WRITE_SYNC : WRITE;
778 enum page_type btype = type > META ? META : type;
780 if (type >= META_FLUSH)
781 rw = WRITE_FLUSH_FUA;
786 if (sbi->bio[btype]) {
787 struct bio_private *p = sbi->bio[btype]->bi_private;
789 sbi->bio[btype]->bi_end_io = f2fs_end_io_write;
791 trace_f2fs_do_submit_bio(sbi->sb, btype, sync, sbi->bio[btype]);
793 if (type == META_FLUSH) {
794 DECLARE_COMPLETION_ONSTACK(wait);
797 submit_bio(rw, sbi->bio[btype]);
798 wait_for_completion(&wait);
801 submit_bio(rw, sbi->bio[btype]);
803 sbi->bio[btype] = NULL;
807 void f2fs_submit_bio(struct f2fs_sb_info *sbi, enum page_type type, bool sync)
809 down_write(&sbi->bio_sem);
810 do_submit_bio(sbi, type, sync);
811 up_write(&sbi->bio_sem);
814 static void submit_write_page(struct f2fs_sb_info *sbi, struct page *page,
815 block_t blk_addr, enum page_type type)
817 struct block_device *bdev = sbi->sb->s_bdev;
820 verify_block_addr(sbi, blk_addr);
822 down_write(&sbi->bio_sem);
824 inc_page_count(sbi, F2FS_WRITEBACK);
826 if (sbi->bio[type] && sbi->last_block_in_bio[type] != blk_addr - 1)
827 do_submit_bio(sbi, type, false);
829 if (sbi->bio[type] == NULL) {
830 struct bio_private *priv;
832 priv = kmalloc(sizeof(struct bio_private), GFP_NOFS);
838 bio_blocks = MAX_BIO_BLOCKS(max_hw_blocks(sbi));
839 sbi->bio[type] = f2fs_bio_alloc(bdev, bio_blocks);
840 sbi->bio[type]->bi_sector = SECTOR_FROM_BLOCK(sbi, blk_addr);
841 sbi->bio[type]->bi_private = priv;
843 * The end_io will be assigned at the sumbission phase.
844 * Until then, let bio_add_page() merge consecutive IOs as much
849 if (bio_add_page(sbi->bio[type], page, PAGE_CACHE_SIZE, 0) <
851 do_submit_bio(sbi, type, false);
855 sbi->last_block_in_bio[type] = blk_addr;
857 up_write(&sbi->bio_sem);
858 trace_f2fs_submit_write_page(page, blk_addr, type);
861 void f2fs_wait_on_page_writeback(struct page *page,
862 enum page_type type, bool sync)
864 struct f2fs_sb_info *sbi = F2FS_SB(page->mapping->host->i_sb);
865 if (PageWriteback(page)) {
866 f2fs_submit_bio(sbi, type, sync);
867 wait_on_page_writeback(page);
871 static bool __has_curseg_space(struct f2fs_sb_info *sbi, int type)
873 struct curseg_info *curseg = CURSEG_I(sbi, type);
874 if (curseg->next_blkoff < sbi->blocks_per_seg)
879 static int __get_segment_type_2(struct page *page, enum page_type p_type)
882 return CURSEG_HOT_DATA;
884 return CURSEG_HOT_NODE;
887 static int __get_segment_type_4(struct page *page, enum page_type p_type)
889 if (p_type == DATA) {
890 struct inode *inode = page->mapping->host;
892 if (S_ISDIR(inode->i_mode))
893 return CURSEG_HOT_DATA;
895 return CURSEG_COLD_DATA;
897 if (IS_DNODE(page) && !is_cold_node(page))
898 return CURSEG_HOT_NODE;
900 return CURSEG_COLD_NODE;
904 static int __get_segment_type_6(struct page *page, enum page_type p_type)
906 if (p_type == DATA) {
907 struct inode *inode = page->mapping->host;
909 if (S_ISDIR(inode->i_mode))
910 return CURSEG_HOT_DATA;
911 else if (is_cold_data(page) || file_is_cold(inode))
912 return CURSEG_COLD_DATA;
914 return CURSEG_WARM_DATA;
917 return is_cold_node(page) ? CURSEG_WARM_NODE :
920 return CURSEG_COLD_NODE;
924 static int __get_segment_type(struct page *page, enum page_type p_type)
926 struct f2fs_sb_info *sbi = F2FS_SB(page->mapping->host->i_sb);
927 switch (sbi->active_logs) {
929 return __get_segment_type_2(page, p_type);
931 return __get_segment_type_4(page, p_type);
933 /* NR_CURSEG_TYPE(6) logs by default */
934 f2fs_bug_on(sbi->active_logs != NR_CURSEG_TYPE);
935 return __get_segment_type_6(page, p_type);
938 static void do_write_page(struct f2fs_sb_info *sbi, struct page *page,
939 block_t old_blkaddr, block_t *new_blkaddr,
940 struct f2fs_summary *sum, enum page_type p_type)
942 struct sit_info *sit_i = SIT_I(sbi);
943 struct curseg_info *curseg;
944 unsigned int old_cursegno;
947 type = __get_segment_type(page, p_type);
948 curseg = CURSEG_I(sbi, type);
950 mutex_lock(&curseg->curseg_mutex);
952 *new_blkaddr = NEXT_FREE_BLKADDR(sbi, curseg);
953 old_cursegno = curseg->segno;
956 * __add_sum_entry should be resided under the curseg_mutex
957 * because, this function updates a summary entry in the
958 * current summary block.
960 __add_sum_entry(sbi, type, sum);
962 mutex_lock(&sit_i->sentry_lock);
963 __refresh_next_blkoff(sbi, curseg);
965 stat_inc_block_count(sbi, curseg);
968 * SIT information should be updated before segment allocation,
969 * since SSR needs latest valid block information.
971 refresh_sit_entry(sbi, old_blkaddr, *new_blkaddr);
973 if (!__has_curseg_space(sbi, type))
974 sit_i->s_ops->allocate_segment(sbi, type, false);
976 locate_dirty_segment(sbi, old_cursegno);
977 locate_dirty_segment(sbi, GET_SEGNO(sbi, old_blkaddr));
978 mutex_unlock(&sit_i->sentry_lock);
981 fill_node_footer_blkaddr(page, NEXT_FREE_BLKADDR(sbi, curseg));
983 /* writeout dirty page into bdev */
984 submit_write_page(sbi, page, *new_blkaddr, p_type);
986 mutex_unlock(&curseg->curseg_mutex);
989 void write_meta_page(struct f2fs_sb_info *sbi, struct page *page)
991 set_page_writeback(page);
992 submit_write_page(sbi, page, page->index, META);
995 void write_node_page(struct f2fs_sb_info *sbi, struct page *page,
996 unsigned int nid, block_t old_blkaddr, block_t *new_blkaddr)
998 struct f2fs_summary sum;
999 set_summary(&sum, nid, 0, 0);
1000 do_write_page(sbi, page, old_blkaddr, new_blkaddr, &sum, NODE);
1003 void write_data_page(struct inode *inode, struct page *page,
1004 struct dnode_of_data *dn, block_t old_blkaddr,
1005 block_t *new_blkaddr)
1007 struct f2fs_sb_info *sbi = F2FS_SB(inode->i_sb);
1008 struct f2fs_summary sum;
1009 struct node_info ni;
1011 f2fs_bug_on(old_blkaddr == NULL_ADDR);
1012 get_node_info(sbi, dn->nid, &ni);
1013 set_summary(&sum, dn->nid, dn->ofs_in_node, ni.version);
1015 do_write_page(sbi, page, old_blkaddr,
1016 new_blkaddr, &sum, DATA);
1019 void rewrite_data_page(struct f2fs_sb_info *sbi, struct page *page,
1020 block_t old_blk_addr)
1022 submit_write_page(sbi, page, old_blk_addr, DATA);
1025 void recover_data_page(struct f2fs_sb_info *sbi,
1026 struct page *page, struct f2fs_summary *sum,
1027 block_t old_blkaddr, block_t new_blkaddr)
1029 struct sit_info *sit_i = SIT_I(sbi);
1030 struct curseg_info *curseg;
1031 unsigned int segno, old_cursegno;
1032 struct seg_entry *se;
1035 segno = GET_SEGNO(sbi, new_blkaddr);
1036 se = get_seg_entry(sbi, segno);
1039 if (se->valid_blocks == 0 && !IS_CURSEG(sbi, segno)) {
1040 if (old_blkaddr == NULL_ADDR)
1041 type = CURSEG_COLD_DATA;
1043 type = CURSEG_WARM_DATA;
1045 curseg = CURSEG_I(sbi, type);
1047 mutex_lock(&curseg->curseg_mutex);
1048 mutex_lock(&sit_i->sentry_lock);
1050 old_cursegno = curseg->segno;
1052 /* change the current segment */
1053 if (segno != curseg->segno) {
1054 curseg->next_segno = segno;
1055 change_curseg(sbi, type, true);
1058 curseg->next_blkoff = GET_SEGOFF_FROM_SEG0(sbi, new_blkaddr) &
1059 (sbi->blocks_per_seg - 1);
1060 __add_sum_entry(sbi, type, sum);
1062 refresh_sit_entry(sbi, old_blkaddr, new_blkaddr);
1064 locate_dirty_segment(sbi, old_cursegno);
1065 locate_dirty_segment(sbi, GET_SEGNO(sbi, old_blkaddr));
1067 mutex_unlock(&sit_i->sentry_lock);
1068 mutex_unlock(&curseg->curseg_mutex);
1071 void rewrite_node_page(struct f2fs_sb_info *sbi,
1072 struct page *page, struct f2fs_summary *sum,
1073 block_t old_blkaddr, block_t new_blkaddr)
1075 struct sit_info *sit_i = SIT_I(sbi);
1076 int type = CURSEG_WARM_NODE;
1077 struct curseg_info *curseg;
1078 unsigned int segno, old_cursegno;
1079 block_t next_blkaddr = next_blkaddr_of_node(page);
1080 unsigned int next_segno = GET_SEGNO(sbi, next_blkaddr);
1082 curseg = CURSEG_I(sbi, type);
1084 mutex_lock(&curseg->curseg_mutex);
1085 mutex_lock(&sit_i->sentry_lock);
1087 segno = GET_SEGNO(sbi, new_blkaddr);
1088 old_cursegno = curseg->segno;
1090 /* change the current segment */
1091 if (segno != curseg->segno) {
1092 curseg->next_segno = segno;
1093 change_curseg(sbi, type, true);
1095 curseg->next_blkoff = GET_SEGOFF_FROM_SEG0(sbi, new_blkaddr) &
1096 (sbi->blocks_per_seg - 1);
1097 __add_sum_entry(sbi, type, sum);
1099 /* change the current log to the next block addr in advance */
1100 if (next_segno != segno) {
1101 curseg->next_segno = next_segno;
1102 change_curseg(sbi, type, true);
1104 curseg->next_blkoff = GET_SEGOFF_FROM_SEG0(sbi, next_blkaddr) &
1105 (sbi->blocks_per_seg - 1);
1107 /* rewrite node page */
1108 set_page_writeback(page);
1109 submit_write_page(sbi, page, new_blkaddr, NODE);
1110 f2fs_submit_bio(sbi, NODE, true);
1111 refresh_sit_entry(sbi, old_blkaddr, new_blkaddr);
1113 locate_dirty_segment(sbi, old_cursegno);
1114 locate_dirty_segment(sbi, GET_SEGNO(sbi, old_blkaddr));
1116 mutex_unlock(&sit_i->sentry_lock);
1117 mutex_unlock(&curseg->curseg_mutex);
1120 static int read_compacted_summaries(struct f2fs_sb_info *sbi)
1122 struct f2fs_checkpoint *ckpt = F2FS_CKPT(sbi);
1123 struct curseg_info *seg_i;
1124 unsigned char *kaddr;
1129 start = start_sum_block(sbi);
1131 page = get_meta_page(sbi, start++);
1132 kaddr = (unsigned char *)page_address(page);
1134 /* Step 1: restore nat cache */
1135 seg_i = CURSEG_I(sbi, CURSEG_HOT_DATA);
1136 memcpy(&seg_i->sum_blk->n_nats, kaddr, SUM_JOURNAL_SIZE);
1138 /* Step 2: restore sit cache */
1139 seg_i = CURSEG_I(sbi, CURSEG_COLD_DATA);
1140 memcpy(&seg_i->sum_blk->n_sits, kaddr + SUM_JOURNAL_SIZE,
1142 offset = 2 * SUM_JOURNAL_SIZE;
1144 /* Step 3: restore summary entries */
1145 for (i = CURSEG_HOT_DATA; i <= CURSEG_COLD_DATA; i++) {
1146 unsigned short blk_off;
1149 seg_i = CURSEG_I(sbi, i);
1150 segno = le32_to_cpu(ckpt->cur_data_segno[i]);
1151 blk_off = le16_to_cpu(ckpt->cur_data_blkoff[i]);
1152 seg_i->next_segno = segno;
1153 reset_curseg(sbi, i, 0);
1154 seg_i->alloc_type = ckpt->alloc_type[i];
1155 seg_i->next_blkoff = blk_off;
1157 if (seg_i->alloc_type == SSR)
1158 blk_off = sbi->blocks_per_seg;
1160 for (j = 0; j < blk_off; j++) {
1161 struct f2fs_summary *s;
1162 s = (struct f2fs_summary *)(kaddr + offset);
1163 seg_i->sum_blk->entries[j] = *s;
1164 offset += SUMMARY_SIZE;
1165 if (offset + SUMMARY_SIZE <= PAGE_CACHE_SIZE -
1169 f2fs_put_page(page, 1);
1172 page = get_meta_page(sbi, start++);
1173 kaddr = (unsigned char *)page_address(page);
1177 f2fs_put_page(page, 1);
1181 static int read_normal_summaries(struct f2fs_sb_info *sbi, int type)
1183 struct f2fs_checkpoint *ckpt = F2FS_CKPT(sbi);
1184 struct f2fs_summary_block *sum;
1185 struct curseg_info *curseg;
1187 unsigned short blk_off;
1188 unsigned int segno = 0;
1189 block_t blk_addr = 0;
1191 /* get segment number and block addr */
1192 if (IS_DATASEG(type)) {
1193 segno = le32_to_cpu(ckpt->cur_data_segno[type]);
1194 blk_off = le16_to_cpu(ckpt->cur_data_blkoff[type -
1196 if (is_set_ckpt_flags(ckpt, CP_UMOUNT_FLAG))
1197 blk_addr = sum_blk_addr(sbi, NR_CURSEG_TYPE, type);
1199 blk_addr = sum_blk_addr(sbi, NR_CURSEG_DATA_TYPE, type);
1201 segno = le32_to_cpu(ckpt->cur_node_segno[type -
1203 blk_off = le16_to_cpu(ckpt->cur_node_blkoff[type -
1205 if (is_set_ckpt_flags(ckpt, CP_UMOUNT_FLAG))
1206 blk_addr = sum_blk_addr(sbi, NR_CURSEG_NODE_TYPE,
1207 type - CURSEG_HOT_NODE);
1209 blk_addr = GET_SUM_BLOCK(sbi, segno);
1212 new = get_meta_page(sbi, blk_addr);
1213 sum = (struct f2fs_summary_block *)page_address(new);
1215 if (IS_NODESEG(type)) {
1216 if (is_set_ckpt_flags(ckpt, CP_UMOUNT_FLAG)) {
1217 struct f2fs_summary *ns = &sum->entries[0];
1219 for (i = 0; i < sbi->blocks_per_seg; i++, ns++) {
1221 ns->ofs_in_node = 0;
1224 if (restore_node_summary(sbi, segno, sum)) {
1225 f2fs_put_page(new, 1);
1231 /* set uncompleted segment to curseg */
1232 curseg = CURSEG_I(sbi, type);
1233 mutex_lock(&curseg->curseg_mutex);
1234 memcpy(curseg->sum_blk, sum, PAGE_CACHE_SIZE);
1235 curseg->next_segno = segno;
1236 reset_curseg(sbi, type, 0);
1237 curseg->alloc_type = ckpt->alloc_type[type];
1238 curseg->next_blkoff = blk_off;
1239 mutex_unlock(&curseg->curseg_mutex);
1240 f2fs_put_page(new, 1);
1244 static int restore_curseg_summaries(struct f2fs_sb_info *sbi)
1246 int type = CURSEG_HOT_DATA;
1248 if (is_set_ckpt_flags(F2FS_CKPT(sbi), CP_COMPACT_SUM_FLAG)) {
1249 /* restore for compacted data summary */
1250 if (read_compacted_summaries(sbi))
1252 type = CURSEG_HOT_NODE;
1255 for (; type <= CURSEG_COLD_NODE; type++)
1256 if (read_normal_summaries(sbi, type))
1261 static void write_compacted_summaries(struct f2fs_sb_info *sbi, block_t blkaddr)
1264 unsigned char *kaddr;
1265 struct f2fs_summary *summary;
1266 struct curseg_info *seg_i;
1267 int written_size = 0;
1270 page = grab_meta_page(sbi, blkaddr++);
1271 kaddr = (unsigned char *)page_address(page);
1273 /* Step 1: write nat cache */
1274 seg_i = CURSEG_I(sbi, CURSEG_HOT_DATA);
1275 memcpy(kaddr, &seg_i->sum_blk->n_nats, SUM_JOURNAL_SIZE);
1276 written_size += SUM_JOURNAL_SIZE;
1278 /* Step 2: write sit cache */
1279 seg_i = CURSEG_I(sbi, CURSEG_COLD_DATA);
1280 memcpy(kaddr + written_size, &seg_i->sum_blk->n_sits,
1282 written_size += SUM_JOURNAL_SIZE;
1284 /* Step 3: write summary entries */
1285 for (i = CURSEG_HOT_DATA; i <= CURSEG_COLD_DATA; i++) {
1286 unsigned short blkoff;
1287 seg_i = CURSEG_I(sbi, i);
1288 if (sbi->ckpt->alloc_type[i] == SSR)
1289 blkoff = sbi->blocks_per_seg;
1291 blkoff = curseg_blkoff(sbi, i);
1293 for (j = 0; j < blkoff; j++) {
1295 page = grab_meta_page(sbi, blkaddr++);
1296 kaddr = (unsigned char *)page_address(page);
1299 summary = (struct f2fs_summary *)(kaddr + written_size);
1300 *summary = seg_i->sum_blk->entries[j];
1301 written_size += SUMMARY_SIZE;
1303 if (written_size + SUMMARY_SIZE <= PAGE_CACHE_SIZE -
1307 set_page_dirty(page);
1308 f2fs_put_page(page, 1);
1313 set_page_dirty(page);
1314 f2fs_put_page(page, 1);
1318 static void write_normal_summaries(struct f2fs_sb_info *sbi,
1319 block_t blkaddr, int type)
1322 if (IS_DATASEG(type))
1323 end = type + NR_CURSEG_DATA_TYPE;
1325 end = type + NR_CURSEG_NODE_TYPE;
1327 for (i = type; i < end; i++) {
1328 struct curseg_info *sum = CURSEG_I(sbi, i);
1329 mutex_lock(&sum->curseg_mutex);
1330 write_sum_page(sbi, sum->sum_blk, blkaddr + (i - type));
1331 mutex_unlock(&sum->curseg_mutex);
1335 void write_data_summaries(struct f2fs_sb_info *sbi, block_t start_blk)
1337 if (is_set_ckpt_flags(F2FS_CKPT(sbi), CP_COMPACT_SUM_FLAG))
1338 write_compacted_summaries(sbi, start_blk);
1340 write_normal_summaries(sbi, start_blk, CURSEG_HOT_DATA);
1343 void write_node_summaries(struct f2fs_sb_info *sbi, block_t start_blk)
1345 if (is_set_ckpt_flags(F2FS_CKPT(sbi), CP_UMOUNT_FLAG))
1346 write_normal_summaries(sbi, start_blk, CURSEG_HOT_NODE);
1349 int lookup_journal_in_cursum(struct f2fs_summary_block *sum, int type,
1350 unsigned int val, int alloc)
1354 if (type == NAT_JOURNAL) {
1355 for (i = 0; i < nats_in_cursum(sum); i++) {
1356 if (le32_to_cpu(nid_in_journal(sum, i)) == val)
1359 if (alloc && nats_in_cursum(sum) < NAT_JOURNAL_ENTRIES)
1360 return update_nats_in_cursum(sum, 1);
1361 } else if (type == SIT_JOURNAL) {
1362 for (i = 0; i < sits_in_cursum(sum); i++)
1363 if (le32_to_cpu(segno_in_journal(sum, i)) == val)
1365 if (alloc && sits_in_cursum(sum) < SIT_JOURNAL_ENTRIES)
1366 return update_sits_in_cursum(sum, 1);
1371 static struct page *get_current_sit_page(struct f2fs_sb_info *sbi,
1374 struct sit_info *sit_i = SIT_I(sbi);
1375 unsigned int offset = SIT_BLOCK_OFFSET(sit_i, segno);
1376 block_t blk_addr = sit_i->sit_base_addr + offset;
1378 check_seg_range(sbi, segno);
1380 /* calculate sit block address */
1381 if (f2fs_test_bit(offset, sit_i->sit_bitmap))
1382 blk_addr += sit_i->sit_blocks;
1384 return get_meta_page(sbi, blk_addr);
1387 static struct page *get_next_sit_page(struct f2fs_sb_info *sbi,
1390 struct sit_info *sit_i = SIT_I(sbi);
1391 struct page *src_page, *dst_page;
1392 pgoff_t src_off, dst_off;
1393 void *src_addr, *dst_addr;
1395 src_off = current_sit_addr(sbi, start);
1396 dst_off = next_sit_addr(sbi, src_off);
1398 /* get current sit block page without lock */
1399 src_page = get_meta_page(sbi, src_off);
1400 dst_page = grab_meta_page(sbi, dst_off);
1401 f2fs_bug_on(PageDirty(src_page));
1403 src_addr = page_address(src_page);
1404 dst_addr = page_address(dst_page);
1405 memcpy(dst_addr, src_addr, PAGE_CACHE_SIZE);
1407 set_page_dirty(dst_page);
1408 f2fs_put_page(src_page, 1);
1410 set_to_next_sit(sit_i, start);
1415 static bool flush_sits_in_journal(struct f2fs_sb_info *sbi)
1417 struct curseg_info *curseg = CURSEG_I(sbi, CURSEG_COLD_DATA);
1418 struct f2fs_summary_block *sum = curseg->sum_blk;
1422 * If the journal area in the current summary is full of sit entries,
1423 * all the sit entries will be flushed. Otherwise the sit entries
1424 * are not able to replace with newly hot sit entries.
1426 if (sits_in_cursum(sum) >= SIT_JOURNAL_ENTRIES) {
1427 for (i = sits_in_cursum(sum) - 1; i >= 0; i--) {
1429 segno = le32_to_cpu(segno_in_journal(sum, i));
1430 __mark_sit_entry_dirty(sbi, segno);
1432 update_sits_in_cursum(sum, -sits_in_cursum(sum));
1439 * CP calls this function, which flushes SIT entries including sit_journal,
1440 * and moves prefree segs to free segs.
1442 void flush_sit_entries(struct f2fs_sb_info *sbi)
1444 struct sit_info *sit_i = SIT_I(sbi);
1445 unsigned long *bitmap = sit_i->dirty_sentries_bitmap;
1446 struct curseg_info *curseg = CURSEG_I(sbi, CURSEG_COLD_DATA);
1447 struct f2fs_summary_block *sum = curseg->sum_blk;
1448 unsigned long nsegs = TOTAL_SEGS(sbi);
1449 struct page *page = NULL;
1450 struct f2fs_sit_block *raw_sit = NULL;
1451 unsigned int start = 0, end = 0;
1452 unsigned int segno = -1;
1455 mutex_lock(&curseg->curseg_mutex);
1456 mutex_lock(&sit_i->sentry_lock);
1459 * "flushed" indicates whether sit entries in journal are flushed
1460 * to the SIT area or not.
1462 flushed = flush_sits_in_journal(sbi);
1464 while ((segno = find_next_bit(bitmap, nsegs, segno + 1)) < nsegs) {
1465 struct seg_entry *se = get_seg_entry(sbi, segno);
1466 int sit_offset, offset;
1468 sit_offset = SIT_ENTRY_OFFSET(sit_i, segno);
1473 offset = lookup_journal_in_cursum(sum, SIT_JOURNAL, segno, 1);
1475 segno_in_journal(sum, offset) = cpu_to_le32(segno);
1476 seg_info_to_raw_sit(se, &sit_in_journal(sum, offset));
1480 if (!page || (start > segno) || (segno > end)) {
1482 f2fs_put_page(page, 1);
1486 start = START_SEGNO(sit_i, segno);
1487 end = start + SIT_ENTRY_PER_BLOCK - 1;
1489 /* read sit block that will be updated */
1490 page = get_next_sit_page(sbi, start);
1491 raw_sit = page_address(page);
1494 /* udpate entry in SIT block */
1495 seg_info_to_raw_sit(se, &raw_sit->entries[sit_offset]);
1497 __clear_bit(segno, bitmap);
1498 sit_i->dirty_sentries--;
1500 mutex_unlock(&sit_i->sentry_lock);
1501 mutex_unlock(&curseg->curseg_mutex);
1503 /* writeout last modified SIT block */
1504 f2fs_put_page(page, 1);
1506 set_prefree_as_free_segments(sbi);
1509 static int build_sit_info(struct f2fs_sb_info *sbi)
1511 struct f2fs_super_block *raw_super = F2FS_RAW_SUPER(sbi);
1512 struct f2fs_checkpoint *ckpt = F2FS_CKPT(sbi);
1513 struct sit_info *sit_i;
1514 unsigned int sit_segs, start;
1515 char *src_bitmap, *dst_bitmap;
1516 unsigned int bitmap_size;
1518 /* allocate memory for SIT information */
1519 sit_i = kzalloc(sizeof(struct sit_info), GFP_KERNEL);
1523 SM_I(sbi)->sit_info = sit_i;
1525 sit_i->sentries = vzalloc(TOTAL_SEGS(sbi) * sizeof(struct seg_entry));
1526 if (!sit_i->sentries)
1529 bitmap_size = f2fs_bitmap_size(TOTAL_SEGS(sbi));
1530 sit_i->dirty_sentries_bitmap = kzalloc(bitmap_size, GFP_KERNEL);
1531 if (!sit_i->dirty_sentries_bitmap)
1534 for (start = 0; start < TOTAL_SEGS(sbi); start++) {
1535 sit_i->sentries[start].cur_valid_map
1536 = kzalloc(SIT_VBLOCK_MAP_SIZE, GFP_KERNEL);
1537 sit_i->sentries[start].ckpt_valid_map
1538 = kzalloc(SIT_VBLOCK_MAP_SIZE, GFP_KERNEL);
1539 if (!sit_i->sentries[start].cur_valid_map
1540 || !sit_i->sentries[start].ckpt_valid_map)
1544 if (sbi->segs_per_sec > 1) {
1545 sit_i->sec_entries = vzalloc(TOTAL_SECS(sbi) *
1546 sizeof(struct sec_entry));
1547 if (!sit_i->sec_entries)
1551 /* get information related with SIT */
1552 sit_segs = le32_to_cpu(raw_super->segment_count_sit) >> 1;
1554 /* setup SIT bitmap from ckeckpoint pack */
1555 bitmap_size = __bitmap_size(sbi, SIT_BITMAP);
1556 src_bitmap = __bitmap_ptr(sbi, SIT_BITMAP);
1558 dst_bitmap = kmemdup(src_bitmap, bitmap_size, GFP_KERNEL);
1562 /* init SIT information */
1563 sit_i->s_ops = &default_salloc_ops;
1565 sit_i->sit_base_addr = le32_to_cpu(raw_super->sit_blkaddr);
1566 sit_i->sit_blocks = sit_segs << sbi->log_blocks_per_seg;
1567 sit_i->written_valid_blocks = le64_to_cpu(ckpt->valid_block_count);
1568 sit_i->sit_bitmap = dst_bitmap;
1569 sit_i->bitmap_size = bitmap_size;
1570 sit_i->dirty_sentries = 0;
1571 sit_i->sents_per_block = SIT_ENTRY_PER_BLOCK;
1572 sit_i->elapsed_time = le64_to_cpu(sbi->ckpt->elapsed_time);
1573 sit_i->mounted_time = CURRENT_TIME_SEC.tv_sec;
1574 mutex_init(&sit_i->sentry_lock);
1578 static int build_free_segmap(struct f2fs_sb_info *sbi)
1580 struct f2fs_sm_info *sm_info = SM_I(sbi);
1581 struct free_segmap_info *free_i;
1582 unsigned int bitmap_size, sec_bitmap_size;
1584 /* allocate memory for free segmap information */
1585 free_i = kzalloc(sizeof(struct free_segmap_info), GFP_KERNEL);
1589 SM_I(sbi)->free_info = free_i;
1591 bitmap_size = f2fs_bitmap_size(TOTAL_SEGS(sbi));
1592 free_i->free_segmap = kmalloc(bitmap_size, GFP_KERNEL);
1593 if (!free_i->free_segmap)
1596 sec_bitmap_size = f2fs_bitmap_size(TOTAL_SECS(sbi));
1597 free_i->free_secmap = kmalloc(sec_bitmap_size, GFP_KERNEL);
1598 if (!free_i->free_secmap)
1601 /* set all segments as dirty temporarily */
1602 memset(free_i->free_segmap, 0xff, bitmap_size);
1603 memset(free_i->free_secmap, 0xff, sec_bitmap_size);
1605 /* init free segmap information */
1606 free_i->start_segno =
1607 (unsigned int) GET_SEGNO_FROM_SEG0(sbi, sm_info->main_blkaddr);
1608 free_i->free_segments = 0;
1609 free_i->free_sections = 0;
1610 rwlock_init(&free_i->segmap_lock);
1614 static int build_curseg(struct f2fs_sb_info *sbi)
1616 struct curseg_info *array;
1619 array = kzalloc(sizeof(*array) * NR_CURSEG_TYPE, GFP_KERNEL);
1623 SM_I(sbi)->curseg_array = array;
1625 for (i = 0; i < NR_CURSEG_TYPE; i++) {
1626 mutex_init(&array[i].curseg_mutex);
1627 array[i].sum_blk = kzalloc(PAGE_CACHE_SIZE, GFP_KERNEL);
1628 if (!array[i].sum_blk)
1630 array[i].segno = NULL_SEGNO;
1631 array[i].next_blkoff = 0;
1633 return restore_curseg_summaries(sbi);
1636 static void build_sit_entries(struct f2fs_sb_info *sbi)
1638 struct sit_info *sit_i = SIT_I(sbi);
1639 struct curseg_info *curseg = CURSEG_I(sbi, CURSEG_COLD_DATA);
1640 struct f2fs_summary_block *sum = curseg->sum_blk;
1643 for (start = 0; start < TOTAL_SEGS(sbi); start++) {
1644 struct seg_entry *se = &sit_i->sentries[start];
1645 struct f2fs_sit_block *sit_blk;
1646 struct f2fs_sit_entry sit;
1650 mutex_lock(&curseg->curseg_mutex);
1651 for (i = 0; i < sits_in_cursum(sum); i++) {
1652 if (le32_to_cpu(segno_in_journal(sum, i)) == start) {
1653 sit = sit_in_journal(sum, i);
1654 mutex_unlock(&curseg->curseg_mutex);
1658 mutex_unlock(&curseg->curseg_mutex);
1659 page = get_current_sit_page(sbi, start);
1660 sit_blk = (struct f2fs_sit_block *)page_address(page);
1661 sit = sit_blk->entries[SIT_ENTRY_OFFSET(sit_i, start)];
1662 f2fs_put_page(page, 1);
1664 check_block_count(sbi, start, &sit);
1665 seg_info_from_raw_sit(se, &sit);
1666 if (sbi->segs_per_sec > 1) {
1667 struct sec_entry *e = get_sec_entry(sbi, start);
1668 e->valid_blocks += se->valid_blocks;
1673 static void init_free_segmap(struct f2fs_sb_info *sbi)
1678 for (start = 0; start < TOTAL_SEGS(sbi); start++) {
1679 struct seg_entry *sentry = get_seg_entry(sbi, start);
1680 if (!sentry->valid_blocks)
1681 __set_free(sbi, start);
1684 /* set use the current segments */
1685 for (type = CURSEG_HOT_DATA; type <= CURSEG_COLD_NODE; type++) {
1686 struct curseg_info *curseg_t = CURSEG_I(sbi, type);
1687 __set_test_and_inuse(sbi, curseg_t->segno);
1691 static void init_dirty_segmap(struct f2fs_sb_info *sbi)
1693 struct dirty_seglist_info *dirty_i = DIRTY_I(sbi);
1694 struct free_segmap_info *free_i = FREE_I(sbi);
1695 unsigned int segno = 0, offset = 0, total_segs = TOTAL_SEGS(sbi);
1696 unsigned short valid_blocks;
1699 /* find dirty segment based on free segmap */
1700 segno = find_next_inuse(free_i, total_segs, offset);
1701 if (segno >= total_segs)
1704 valid_blocks = get_valid_blocks(sbi, segno, 0);
1705 if (valid_blocks >= sbi->blocks_per_seg || !valid_blocks)
1707 mutex_lock(&dirty_i->seglist_lock);
1708 __locate_dirty_segment(sbi, segno, DIRTY);
1709 mutex_unlock(&dirty_i->seglist_lock);
1713 static int init_victim_secmap(struct f2fs_sb_info *sbi)
1715 struct dirty_seglist_info *dirty_i = DIRTY_I(sbi);
1716 unsigned int bitmap_size = f2fs_bitmap_size(TOTAL_SECS(sbi));
1718 dirty_i->victim_secmap = kzalloc(bitmap_size, GFP_KERNEL);
1719 if (!dirty_i->victim_secmap)
1724 static int build_dirty_segmap(struct f2fs_sb_info *sbi)
1726 struct dirty_seglist_info *dirty_i;
1727 unsigned int bitmap_size, i;
1729 /* allocate memory for dirty segments list information */
1730 dirty_i = kzalloc(sizeof(struct dirty_seglist_info), GFP_KERNEL);
1734 SM_I(sbi)->dirty_info = dirty_i;
1735 mutex_init(&dirty_i->seglist_lock);
1737 bitmap_size = f2fs_bitmap_size(TOTAL_SEGS(sbi));
1739 for (i = 0; i < NR_DIRTY_TYPE; i++) {
1740 dirty_i->dirty_segmap[i] = kzalloc(bitmap_size, GFP_KERNEL);
1741 if (!dirty_i->dirty_segmap[i])
1745 init_dirty_segmap(sbi);
1746 return init_victim_secmap(sbi);
1750 * Update min, max modified time for cost-benefit GC algorithm
1752 static void init_min_max_mtime(struct f2fs_sb_info *sbi)
1754 struct sit_info *sit_i = SIT_I(sbi);
1757 mutex_lock(&sit_i->sentry_lock);
1759 sit_i->min_mtime = LLONG_MAX;
1761 for (segno = 0; segno < TOTAL_SEGS(sbi); segno += sbi->segs_per_sec) {
1763 unsigned long long mtime = 0;
1765 for (i = 0; i < sbi->segs_per_sec; i++)
1766 mtime += get_seg_entry(sbi, segno + i)->mtime;
1768 mtime = div_u64(mtime, sbi->segs_per_sec);
1770 if (sit_i->min_mtime > mtime)
1771 sit_i->min_mtime = mtime;
1773 sit_i->max_mtime = get_mtime(sbi);
1774 mutex_unlock(&sit_i->sentry_lock);
1777 int build_segment_manager(struct f2fs_sb_info *sbi)
1779 struct f2fs_super_block *raw_super = F2FS_RAW_SUPER(sbi);
1780 struct f2fs_checkpoint *ckpt = F2FS_CKPT(sbi);
1781 struct f2fs_sm_info *sm_info;
1784 sm_info = kzalloc(sizeof(struct f2fs_sm_info), GFP_KERNEL);
1789 sbi->sm_info = sm_info;
1790 INIT_LIST_HEAD(&sm_info->wblist_head);
1791 spin_lock_init(&sm_info->wblist_lock);
1792 sm_info->seg0_blkaddr = le32_to_cpu(raw_super->segment0_blkaddr);
1793 sm_info->main_blkaddr = le32_to_cpu(raw_super->main_blkaddr);
1794 sm_info->segment_count = le32_to_cpu(raw_super->segment_count);
1795 sm_info->reserved_segments = le32_to_cpu(ckpt->rsvd_segment_count);
1796 sm_info->ovp_segments = le32_to_cpu(ckpt->overprov_segment_count);
1797 sm_info->main_segments = le32_to_cpu(raw_super->segment_count_main);
1798 sm_info->ssa_blkaddr = le32_to_cpu(raw_super->ssa_blkaddr);
1799 sm_info->rec_prefree_segments = DEF_RECLAIM_PREFREE_SEGMENTS;
1801 err = build_sit_info(sbi);
1804 err = build_free_segmap(sbi);
1807 err = build_curseg(sbi);
1811 /* reinit free segmap based on SIT */
1812 build_sit_entries(sbi);
1814 init_free_segmap(sbi);
1815 err = build_dirty_segmap(sbi);
1819 init_min_max_mtime(sbi);
1823 static void discard_dirty_segmap(struct f2fs_sb_info *sbi,
1824 enum dirty_type dirty_type)
1826 struct dirty_seglist_info *dirty_i = DIRTY_I(sbi);
1828 mutex_lock(&dirty_i->seglist_lock);
1829 kfree(dirty_i->dirty_segmap[dirty_type]);
1830 dirty_i->nr_dirty[dirty_type] = 0;
1831 mutex_unlock(&dirty_i->seglist_lock);
1834 static void destroy_victim_secmap(struct f2fs_sb_info *sbi)
1836 struct dirty_seglist_info *dirty_i = DIRTY_I(sbi);
1837 kfree(dirty_i->victim_secmap);
1840 static void destroy_dirty_segmap(struct f2fs_sb_info *sbi)
1842 struct dirty_seglist_info *dirty_i = DIRTY_I(sbi);
1848 /* discard pre-free/dirty segments list */
1849 for (i = 0; i < NR_DIRTY_TYPE; i++)
1850 discard_dirty_segmap(sbi, i);
1852 destroy_victim_secmap(sbi);
1853 SM_I(sbi)->dirty_info = NULL;
1857 static void destroy_curseg(struct f2fs_sb_info *sbi)
1859 struct curseg_info *array = SM_I(sbi)->curseg_array;
1864 SM_I(sbi)->curseg_array = NULL;
1865 for (i = 0; i < NR_CURSEG_TYPE; i++)
1866 kfree(array[i].sum_blk);
1870 static void destroy_free_segmap(struct f2fs_sb_info *sbi)
1872 struct free_segmap_info *free_i = SM_I(sbi)->free_info;
1875 SM_I(sbi)->free_info = NULL;
1876 kfree(free_i->free_segmap);
1877 kfree(free_i->free_secmap);
1881 static void destroy_sit_info(struct f2fs_sb_info *sbi)
1883 struct sit_info *sit_i = SIT_I(sbi);
1889 if (sit_i->sentries) {
1890 for (start = 0; start < TOTAL_SEGS(sbi); start++) {
1891 kfree(sit_i->sentries[start].cur_valid_map);
1892 kfree(sit_i->sentries[start].ckpt_valid_map);
1895 vfree(sit_i->sentries);
1896 vfree(sit_i->sec_entries);
1897 kfree(sit_i->dirty_sentries_bitmap);
1899 SM_I(sbi)->sit_info = NULL;
1900 kfree(sit_i->sit_bitmap);
1904 void destroy_segment_manager(struct f2fs_sb_info *sbi)
1906 struct f2fs_sm_info *sm_info = SM_I(sbi);
1909 destroy_dirty_segmap(sbi);
1910 destroy_curseg(sbi);
1911 destroy_free_segmap(sbi);
1912 destroy_sit_info(sbi);
1913 sbi->sm_info = NULL;