diff options
Diffstat (limited to 'fs/f2fs/segment.c')
-rw-r--r-- | fs/f2fs/segment.c | 584 |
1 files changed, 380 insertions, 204 deletions
diff --git a/fs/f2fs/segment.c b/fs/f2fs/segment.c index fa284d397199..7caac5f2ca9e 100644 --- a/fs/f2fs/segment.c +++ b/fs/f2fs/segment.c | |||
@@ -14,12 +14,163 @@ | |||
14 | #include <linux/blkdev.h> | 14 | #include <linux/blkdev.h> |
15 | #include <linux/prefetch.h> | 15 | #include <linux/prefetch.h> |
16 | #include <linux/vmalloc.h> | 16 | #include <linux/vmalloc.h> |
17 | #include <linux/swap.h> | ||
17 | 18 | ||
18 | #include "f2fs.h" | 19 | #include "f2fs.h" |
19 | #include "segment.h" | 20 | #include "segment.h" |
20 | #include "node.h" | 21 | #include "node.h" |
21 | #include <trace/events/f2fs.h> | 22 | #include <trace/events/f2fs.h> |
22 | 23 | ||
24 | #define __reverse_ffz(x) __reverse_ffs(~(x)) | ||
25 | |||
26 | static struct kmem_cache *discard_entry_slab; | ||
27 | |||
28 | /* | ||
29 | * __reverse_ffs is copied from include/asm-generic/bitops/__ffs.h since | ||
30 | * MSB and LSB are reversed in a byte by f2fs_set_bit. | ||
31 | */ | ||
32 | static inline unsigned long __reverse_ffs(unsigned long word) | ||
33 | { | ||
34 | int num = 0; | ||
35 | |||
36 | #if BITS_PER_LONG == 64 | ||
37 | if ((word & 0xffffffff) == 0) { | ||
38 | num += 32; | ||
39 | word >>= 32; | ||
40 | } | ||
41 | #endif | ||
42 | if ((word & 0xffff) == 0) { | ||
43 | num += 16; | ||
44 | word >>= 16; | ||
45 | } | ||
46 | if ((word & 0xff) == 0) { | ||
47 | num += 8; | ||
48 | word >>= 8; | ||
49 | } | ||
50 | if ((word & 0xf0) == 0) | ||
51 | num += 4; | ||
52 | else | ||
53 | word >>= 4; | ||
54 | if ((word & 0xc) == 0) | ||
55 | num += 2; | ||
56 | else | ||
57 | word >>= 2; | ||
58 | if ((word & 0x2) == 0) | ||
59 | num += 1; | ||
60 | return num; | ||
61 | } | ||
62 | |||
63 | /* | ||
64 | * __find_rev_next(_zero)_bit is copied from lib/find_next_bit.c becasue | ||
65 | * f2fs_set_bit makes MSB and LSB reversed in a byte. | ||
66 | * Example: | ||
67 | * LSB <--> MSB | ||
68 | * f2fs_set_bit(0, bitmap) => 0000 0001 | ||
69 | * f2fs_set_bit(7, bitmap) => 1000 0000 | ||
70 | */ | ||
71 | static unsigned long __find_rev_next_bit(const unsigned long *addr, | ||
72 | unsigned long size, unsigned long offset) | ||
73 | { | ||
74 | const unsigned long *p = addr + BIT_WORD(offset); | ||
75 | unsigned long result = offset & ~(BITS_PER_LONG - 1); | ||
76 | unsigned long tmp; | ||
77 | unsigned long mask, submask; | ||
78 | unsigned long quot, rest; | ||
79 | |||
80 | if (offset >= size) | ||
81 | return size; | ||
82 | |||
83 | size -= result; | ||
84 | offset %= BITS_PER_LONG; | ||
85 | if (!offset) | ||
86 | goto aligned; | ||
87 | |||
88 | tmp = *(p++); | ||
89 | quot = (offset >> 3) << 3; | ||
90 | rest = offset & 0x7; | ||
91 | mask = ~0UL << quot; | ||
92 | submask = (unsigned char)(0xff << rest) >> rest; | ||
93 | submask <<= quot; | ||
94 | mask &= submask; | ||
95 | tmp &= mask; | ||
96 | if (size < BITS_PER_LONG) | ||
97 | goto found_first; | ||
98 | if (tmp) | ||
99 | goto found_middle; | ||
100 | |||
101 | size -= BITS_PER_LONG; | ||
102 | result += BITS_PER_LONG; | ||
103 | aligned: | ||
104 | while (size & ~(BITS_PER_LONG-1)) { | ||
105 | tmp = *(p++); | ||
106 | if (tmp) | ||
107 | goto found_middle; | ||
108 | result += BITS_PER_LONG; | ||
109 | size -= BITS_PER_LONG; | ||
110 | } | ||
111 | if (!size) | ||
112 | return result; | ||
113 | tmp = *p; | ||
114 | found_first: | ||
115 | tmp &= (~0UL >> (BITS_PER_LONG - size)); | ||
116 | if (tmp == 0UL) /* Are any bits set? */ | ||
117 | return result + size; /* Nope. */ | ||
118 | found_middle: | ||
119 | return result + __reverse_ffs(tmp); | ||
120 | } | ||
121 | |||
122 | static unsigned long __find_rev_next_zero_bit(const unsigned long *addr, | ||
123 | unsigned long size, unsigned long offset) | ||
124 | { | ||
125 | const unsigned long *p = addr + BIT_WORD(offset); | ||
126 | unsigned long result = offset & ~(BITS_PER_LONG - 1); | ||
127 | unsigned long tmp; | ||
128 | unsigned long mask, submask; | ||
129 | unsigned long quot, rest; | ||
130 | |||
131 | if (offset >= size) | ||
132 | return size; | ||
133 | |||
134 | size -= result; | ||
135 | offset %= BITS_PER_LONG; | ||
136 | if (!offset) | ||
137 | goto aligned; | ||
138 | |||
139 | tmp = *(p++); | ||
140 | quot = (offset >> 3) << 3; | ||
141 | rest = offset & 0x7; | ||
142 | mask = ~(~0UL << quot); | ||
143 | submask = (unsigned char)~((unsigned char)(0xff << rest) >> rest); | ||
144 | submask <<= quot; | ||
145 | mask += submask; | ||
146 | tmp |= mask; | ||
147 | if (size < BITS_PER_LONG) | ||
148 | goto found_first; | ||
149 | if (~tmp) | ||
150 | goto found_middle; | ||
151 | |||
152 | size -= BITS_PER_LONG; | ||
153 | result += BITS_PER_LONG; | ||
154 | aligned: | ||
155 | while (size & ~(BITS_PER_LONG - 1)) { | ||
156 | tmp = *(p++); | ||
157 | if (~tmp) | ||
158 | goto found_middle; | ||
159 | result += BITS_PER_LONG; | ||
160 | size -= BITS_PER_LONG; | ||
161 | } | ||
162 | if (!size) | ||
163 | return result; | ||
164 | tmp = *p; | ||
165 | |||
166 | found_first: | ||
167 | tmp |= ~0UL << size; | ||
168 | if (tmp == ~0UL) /* Are any bits zero? */ | ||
169 | return result + size; /* Nope. */ | ||
170 | found_middle: | ||
171 | return result + __reverse_ffz(tmp); | ||
172 | } | ||
173 | |||
23 | /* | 174 | /* |
24 | * This function balances dirty node and dentry pages. | 175 | * This function balances dirty node and dentry pages. |
25 | * In addition, it controls garbage collection. | 176 | * In addition, it controls garbage collection. |
@@ -116,6 +267,56 @@ static void locate_dirty_segment(struct f2fs_sb_info *sbi, unsigned int segno) | |||
116 | mutex_unlock(&dirty_i->seglist_lock); | 267 | mutex_unlock(&dirty_i->seglist_lock); |
117 | } | 268 | } |
118 | 269 | ||
270 | static void f2fs_issue_discard(struct f2fs_sb_info *sbi, | ||
271 | block_t blkstart, block_t blklen) | ||
272 | { | ||
273 | sector_t start = SECTOR_FROM_BLOCK(sbi, blkstart); | ||
274 | sector_t len = SECTOR_FROM_BLOCK(sbi, blklen); | ||
275 | blkdev_issue_discard(sbi->sb->s_bdev, start, len, GFP_NOFS, 0); | ||
276 | trace_f2fs_issue_discard(sbi->sb, blkstart, blklen); | ||
277 | } | ||
278 | |||
279 | static void add_discard_addrs(struct f2fs_sb_info *sbi, | ||
280 | unsigned int segno, struct seg_entry *se) | ||
281 | { | ||
282 | struct list_head *head = &SM_I(sbi)->discard_list; | ||
283 | struct discard_entry *new; | ||
284 | int entries = SIT_VBLOCK_MAP_SIZE / sizeof(unsigned long); | ||
285 | int max_blocks = sbi->blocks_per_seg; | ||
286 | unsigned long *cur_map = (unsigned long *)se->cur_valid_map; | ||
287 | unsigned long *ckpt_map = (unsigned long *)se->ckpt_valid_map; | ||
288 | unsigned long dmap[entries]; | ||
289 | unsigned int start = 0, end = -1; | ||
290 | int i; | ||
291 | |||
292 | if (!test_opt(sbi, DISCARD)) | ||
293 | return; | ||
294 | |||
295 | /* zero block will be discarded through the prefree list */ | ||
296 | if (!se->valid_blocks || se->valid_blocks == max_blocks) | ||
297 | return; | ||
298 | |||
299 | /* SIT_VBLOCK_MAP_SIZE should be multiple of sizeof(unsigned long) */ | ||
300 | for (i = 0; i < entries; i++) | ||
301 | dmap[i] = (cur_map[i] ^ ckpt_map[i]) & ckpt_map[i]; | ||
302 | |||
303 | while (SM_I(sbi)->nr_discards <= SM_I(sbi)->max_discards) { | ||
304 | start = __find_rev_next_bit(dmap, max_blocks, end + 1); | ||
305 | if (start >= max_blocks) | ||
306 | break; | ||
307 | |||
308 | end = __find_rev_next_zero_bit(dmap, max_blocks, start + 1); | ||
309 | |||
310 | new = f2fs_kmem_cache_alloc(discard_entry_slab, GFP_NOFS); | ||
311 | INIT_LIST_HEAD(&new->list); | ||
312 | new->blkaddr = START_BLOCK(sbi, segno) + start; | ||
313 | new->len = end - start; | ||
314 | |||
315 | list_add_tail(&new->list, head); | ||
316 | SM_I(sbi)->nr_discards += end - start; | ||
317 | } | ||
318 | } | ||
319 | |||
119 | /* | 320 | /* |
120 | * Should call clear_prefree_segments after checkpoint is done. | 321 | * Should call clear_prefree_segments after checkpoint is done. |
121 | */ | 322 | */ |
@@ -138,6 +339,9 @@ static void set_prefree_as_free_segments(struct f2fs_sb_info *sbi) | |||
138 | 339 | ||
139 | void clear_prefree_segments(struct f2fs_sb_info *sbi) | 340 | void clear_prefree_segments(struct f2fs_sb_info *sbi) |
140 | { | 341 | { |
342 | struct list_head *head = &(SM_I(sbi)->discard_list); | ||
343 | struct list_head *this, *next; | ||
344 | struct discard_entry *entry; | ||
141 | struct dirty_seglist_info *dirty_i = DIRTY_I(sbi); | 345 | struct dirty_seglist_info *dirty_i = DIRTY_I(sbi); |
142 | unsigned long *prefree_map = dirty_i->dirty_segmap[PRE]; | 346 | unsigned long *prefree_map = dirty_i->dirty_segmap[PRE]; |
143 | unsigned int total_segs = TOTAL_SEGS(sbi); | 347 | unsigned int total_segs = TOTAL_SEGS(sbi); |
@@ -160,14 +364,19 @@ void clear_prefree_segments(struct f2fs_sb_info *sbi) | |||
160 | if (!test_opt(sbi, DISCARD)) | 364 | if (!test_opt(sbi, DISCARD)) |
161 | continue; | 365 | continue; |
162 | 366 | ||
163 | blkdev_issue_discard(sbi->sb->s_bdev, | 367 | f2fs_issue_discard(sbi, START_BLOCK(sbi, start), |
164 | START_BLOCK(sbi, start) << | 368 | (end - start) << sbi->log_blocks_per_seg); |
165 | sbi->log_sectors_per_block, | ||
166 | (1 << (sbi->log_sectors_per_block + | ||
167 | sbi->log_blocks_per_seg)) * (end - start), | ||
168 | GFP_NOFS, 0); | ||
169 | } | 369 | } |
170 | mutex_unlock(&dirty_i->seglist_lock); | 370 | mutex_unlock(&dirty_i->seglist_lock); |
371 | |||
372 | /* send small discards */ | ||
373 | list_for_each_safe(this, next, head) { | ||
374 | entry = list_entry(this, struct discard_entry, list); | ||
375 | f2fs_issue_discard(sbi, entry->blkaddr, entry->len); | ||
376 | list_del(&entry->list); | ||
377 | SM_I(sbi)->nr_discards -= entry->len; | ||
378 | kmem_cache_free(discard_entry_slab, entry); | ||
379 | } | ||
171 | } | 380 | } |
172 | 381 | ||
173 | static void __mark_sit_entry_dirty(struct f2fs_sb_info *sbi, unsigned int segno) | 382 | static void __mark_sit_entry_dirty(struct f2fs_sb_info *sbi, unsigned int segno) |
@@ -459,13 +668,18 @@ static void __next_free_blkoff(struct f2fs_sb_info *sbi, | |||
459 | struct curseg_info *seg, block_t start) | 668 | struct curseg_info *seg, block_t start) |
460 | { | 669 | { |
461 | struct seg_entry *se = get_seg_entry(sbi, seg->segno); | 670 | struct seg_entry *se = get_seg_entry(sbi, seg->segno); |
462 | block_t ofs; | 671 | int entries = SIT_VBLOCK_MAP_SIZE / sizeof(unsigned long); |
463 | for (ofs = start; ofs < sbi->blocks_per_seg; ofs++) { | 672 | unsigned long target_map[entries]; |
464 | if (!f2fs_test_bit(ofs, se->ckpt_valid_map) | 673 | unsigned long *ckpt_map = (unsigned long *)se->ckpt_valid_map; |
465 | && !f2fs_test_bit(ofs, se->cur_valid_map)) | 674 | unsigned long *cur_map = (unsigned long *)se->cur_valid_map; |
466 | break; | 675 | int i, pos; |
467 | } | 676 | |
468 | seg->next_blkoff = ofs; | 677 | for (i = 0; i < entries; i++) |
678 | target_map[i] = ckpt_map[i] | cur_map[i]; | ||
679 | |||
680 | pos = __find_rev_next_zero_bit(target_map, sbi->blocks_per_seg, start); | ||
681 | |||
682 | seg->next_blkoff = pos; | ||
469 | } | 683 | } |
470 | 684 | ||
471 | /* | 685 | /* |
@@ -573,148 +787,6 @@ static const struct segment_allocation default_salloc_ops = { | |||
573 | .allocate_segment = allocate_segment_by_default, | 787 | .allocate_segment = allocate_segment_by_default, |
574 | }; | 788 | }; |
575 | 789 | ||
576 | static void f2fs_end_io_write(struct bio *bio, int err) | ||
577 | { | ||
578 | const int uptodate = test_bit(BIO_UPTODATE, &bio->bi_flags); | ||
579 | struct bio_vec *bvec = bio->bi_io_vec + bio->bi_vcnt - 1; | ||
580 | struct bio_private *p = bio->bi_private; | ||
581 | |||
582 | do { | ||
583 | struct page *page = bvec->bv_page; | ||
584 | |||
585 | if (--bvec >= bio->bi_io_vec) | ||
586 | prefetchw(&bvec->bv_page->flags); | ||
587 | if (!uptodate) { | ||
588 | SetPageError(page); | ||
589 | if (page->mapping) | ||
590 | set_bit(AS_EIO, &page->mapping->flags); | ||
591 | set_ckpt_flags(p->sbi->ckpt, CP_ERROR_FLAG); | ||
592 | p->sbi->sb->s_flags |= MS_RDONLY; | ||
593 | } | ||
594 | end_page_writeback(page); | ||
595 | dec_page_count(p->sbi, F2FS_WRITEBACK); | ||
596 | } while (bvec >= bio->bi_io_vec); | ||
597 | |||
598 | if (p->is_sync) | ||
599 | complete(p->wait); | ||
600 | |||
601 | if (!get_pages(p->sbi, F2FS_WRITEBACK) && | ||
602 | !list_empty(&p->sbi->cp_wait.task_list)) | ||
603 | wake_up(&p->sbi->cp_wait); | ||
604 | |||
605 | kfree(p); | ||
606 | bio_put(bio); | ||
607 | } | ||
608 | |||
609 | struct bio *f2fs_bio_alloc(struct block_device *bdev, int npages) | ||
610 | { | ||
611 | struct bio *bio; | ||
612 | |||
613 | /* No failure on bio allocation */ | ||
614 | bio = bio_alloc(GFP_NOIO, npages); | ||
615 | bio->bi_bdev = bdev; | ||
616 | bio->bi_private = NULL; | ||
617 | |||
618 | return bio; | ||
619 | } | ||
620 | |||
621 | static void do_submit_bio(struct f2fs_sb_info *sbi, | ||
622 | enum page_type type, bool sync) | ||
623 | { | ||
624 | int rw = sync ? WRITE_SYNC : WRITE; | ||
625 | enum page_type btype = type > META ? META : type; | ||
626 | |||
627 | if (type >= META_FLUSH) | ||
628 | rw = WRITE_FLUSH_FUA; | ||
629 | |||
630 | if (btype == META) | ||
631 | rw |= REQ_META; | ||
632 | |||
633 | if (sbi->bio[btype]) { | ||
634 | struct bio_private *p = sbi->bio[btype]->bi_private; | ||
635 | p->sbi = sbi; | ||
636 | sbi->bio[btype]->bi_end_io = f2fs_end_io_write; | ||
637 | |||
638 | trace_f2fs_do_submit_bio(sbi->sb, btype, sync, sbi->bio[btype]); | ||
639 | |||
640 | if (type == META_FLUSH) { | ||
641 | DECLARE_COMPLETION_ONSTACK(wait); | ||
642 | p->is_sync = true; | ||
643 | p->wait = &wait; | ||
644 | submit_bio(rw, sbi->bio[btype]); | ||
645 | wait_for_completion(&wait); | ||
646 | } else { | ||
647 | p->is_sync = false; | ||
648 | submit_bio(rw, sbi->bio[btype]); | ||
649 | } | ||
650 | sbi->bio[btype] = NULL; | ||
651 | } | ||
652 | } | ||
653 | |||
654 | void f2fs_submit_bio(struct f2fs_sb_info *sbi, enum page_type type, bool sync) | ||
655 | { | ||
656 | down_write(&sbi->bio_sem); | ||
657 | do_submit_bio(sbi, type, sync); | ||
658 | up_write(&sbi->bio_sem); | ||
659 | } | ||
660 | |||
661 | static void submit_write_page(struct f2fs_sb_info *sbi, struct page *page, | ||
662 | block_t blk_addr, enum page_type type) | ||
663 | { | ||
664 | struct block_device *bdev = sbi->sb->s_bdev; | ||
665 | int bio_blocks; | ||
666 | |||
667 | verify_block_addr(sbi, blk_addr); | ||
668 | |||
669 | down_write(&sbi->bio_sem); | ||
670 | |||
671 | inc_page_count(sbi, F2FS_WRITEBACK); | ||
672 | |||
673 | if (sbi->bio[type] && sbi->last_block_in_bio[type] != blk_addr - 1) | ||
674 | do_submit_bio(sbi, type, false); | ||
675 | alloc_new: | ||
676 | if (sbi->bio[type] == NULL) { | ||
677 | struct bio_private *priv; | ||
678 | retry: | ||
679 | priv = kmalloc(sizeof(struct bio_private), GFP_NOFS); | ||
680 | if (!priv) { | ||
681 | cond_resched(); | ||
682 | goto retry; | ||
683 | } | ||
684 | |||
685 | bio_blocks = MAX_BIO_BLOCKS(max_hw_blocks(sbi)); | ||
686 | sbi->bio[type] = f2fs_bio_alloc(bdev, bio_blocks); | ||
687 | sbi->bio[type]->bi_sector = SECTOR_FROM_BLOCK(sbi, blk_addr); | ||
688 | sbi->bio[type]->bi_private = priv; | ||
689 | /* | ||
690 | * The end_io will be assigned at the sumbission phase. | ||
691 | * Until then, let bio_add_page() merge consecutive IOs as much | ||
692 | * as possible. | ||
693 | */ | ||
694 | } | ||
695 | |||
696 | if (bio_add_page(sbi->bio[type], page, PAGE_CACHE_SIZE, 0) < | ||
697 | PAGE_CACHE_SIZE) { | ||
698 | do_submit_bio(sbi, type, false); | ||
699 | goto alloc_new; | ||
700 | } | ||
701 | |||
702 | sbi->last_block_in_bio[type] = blk_addr; | ||
703 | |||
704 | up_write(&sbi->bio_sem); | ||
705 | trace_f2fs_submit_write_page(page, blk_addr, type); | ||
706 | } | ||
707 | |||
708 | void f2fs_wait_on_page_writeback(struct page *page, | ||
709 | enum page_type type, bool sync) | ||
710 | { | ||
711 | struct f2fs_sb_info *sbi = F2FS_SB(page->mapping->host->i_sb); | ||
712 | if (PageWriteback(page)) { | ||
713 | f2fs_submit_bio(sbi, type, sync); | ||
714 | wait_on_page_writeback(page); | ||
715 | } | ||
716 | } | ||
717 | |||
718 | static bool __has_curseg_space(struct f2fs_sb_info *sbi, int type) | 790 | static bool __has_curseg_space(struct f2fs_sb_info *sbi, int type) |
719 | { | 791 | { |
720 | struct curseg_info *curseg = CURSEG_I(sbi, type); | 792 | struct curseg_info *curseg = CURSEG_I(sbi, type); |
@@ -782,16 +854,14 @@ static int __get_segment_type(struct page *page, enum page_type p_type) | |||
782 | return __get_segment_type_6(page, p_type); | 854 | return __get_segment_type_6(page, p_type); |
783 | } | 855 | } |
784 | 856 | ||
785 | static void do_write_page(struct f2fs_sb_info *sbi, struct page *page, | 857 | void allocate_data_block(struct f2fs_sb_info *sbi, struct page *page, |
786 | block_t old_blkaddr, block_t *new_blkaddr, | 858 | block_t old_blkaddr, block_t *new_blkaddr, |
787 | struct f2fs_summary *sum, enum page_type p_type) | 859 | struct f2fs_summary *sum, int type) |
788 | { | 860 | { |
789 | struct sit_info *sit_i = SIT_I(sbi); | 861 | struct sit_info *sit_i = SIT_I(sbi); |
790 | struct curseg_info *curseg; | 862 | struct curseg_info *curseg; |
791 | unsigned int old_cursegno; | 863 | unsigned int old_cursegno; |
792 | int type; | ||
793 | 864 | ||
794 | type = __get_segment_type(page, p_type); | ||
795 | curseg = CURSEG_I(sbi, type); | 865 | curseg = CURSEG_I(sbi, type); |
796 | 866 | ||
797 | mutex_lock(&curseg->curseg_mutex); | 867 | mutex_lock(&curseg->curseg_mutex); |
@@ -824,49 +894,64 @@ static void do_write_page(struct f2fs_sb_info *sbi, struct page *page, | |||
824 | locate_dirty_segment(sbi, GET_SEGNO(sbi, old_blkaddr)); | 894 | locate_dirty_segment(sbi, GET_SEGNO(sbi, old_blkaddr)); |
825 | mutex_unlock(&sit_i->sentry_lock); | 895 | mutex_unlock(&sit_i->sentry_lock); |
826 | 896 | ||
827 | if (p_type == NODE) | 897 | if (page && IS_NODESEG(type)) |
828 | fill_node_footer_blkaddr(page, NEXT_FREE_BLKADDR(sbi, curseg)); | 898 | fill_node_footer_blkaddr(page, NEXT_FREE_BLKADDR(sbi, curseg)); |
829 | 899 | ||
830 | /* writeout dirty page into bdev */ | ||
831 | submit_write_page(sbi, page, *new_blkaddr, p_type); | ||
832 | |||
833 | mutex_unlock(&curseg->curseg_mutex); | 900 | mutex_unlock(&curseg->curseg_mutex); |
834 | } | 901 | } |
835 | 902 | ||
903 | static void do_write_page(struct f2fs_sb_info *sbi, struct page *page, | ||
904 | block_t old_blkaddr, block_t *new_blkaddr, | ||
905 | struct f2fs_summary *sum, struct f2fs_io_info *fio) | ||
906 | { | ||
907 | int type = __get_segment_type(page, fio->type); | ||
908 | |||
909 | allocate_data_block(sbi, page, old_blkaddr, new_blkaddr, sum, type); | ||
910 | |||
911 | /* writeout dirty page into bdev */ | ||
912 | f2fs_submit_page_mbio(sbi, page, *new_blkaddr, fio); | ||
913 | } | ||
914 | |||
836 | void write_meta_page(struct f2fs_sb_info *sbi, struct page *page) | 915 | void write_meta_page(struct f2fs_sb_info *sbi, struct page *page) |
837 | { | 916 | { |
917 | struct f2fs_io_info fio = { | ||
918 | .type = META, | ||
919 | .rw = WRITE_SYNC | REQ_META | REQ_PRIO | ||
920 | }; | ||
921 | |||
838 | set_page_writeback(page); | 922 | set_page_writeback(page); |
839 | submit_write_page(sbi, page, page->index, META); | 923 | f2fs_submit_page_mbio(sbi, page, page->index, &fio); |
840 | } | 924 | } |
841 | 925 | ||
842 | void write_node_page(struct f2fs_sb_info *sbi, struct page *page, | 926 | void write_node_page(struct f2fs_sb_info *sbi, struct page *page, |
927 | struct f2fs_io_info *fio, | ||
843 | unsigned int nid, block_t old_blkaddr, block_t *new_blkaddr) | 928 | unsigned int nid, block_t old_blkaddr, block_t *new_blkaddr) |
844 | { | 929 | { |
845 | struct f2fs_summary sum; | 930 | struct f2fs_summary sum; |
846 | set_summary(&sum, nid, 0, 0); | 931 | set_summary(&sum, nid, 0, 0); |
847 | do_write_page(sbi, page, old_blkaddr, new_blkaddr, &sum, NODE); | 932 | do_write_page(sbi, page, old_blkaddr, new_blkaddr, &sum, fio); |
848 | } | 933 | } |
849 | 934 | ||
850 | void write_data_page(struct inode *inode, struct page *page, | 935 | void write_data_page(struct page *page, struct dnode_of_data *dn, |
851 | struct dnode_of_data *dn, block_t old_blkaddr, | 936 | block_t *new_blkaddr, struct f2fs_io_info *fio) |
852 | block_t *new_blkaddr) | ||
853 | { | 937 | { |
854 | struct f2fs_sb_info *sbi = F2FS_SB(inode->i_sb); | 938 | struct f2fs_sb_info *sbi = F2FS_SB(dn->inode->i_sb); |
855 | struct f2fs_summary sum; | 939 | struct f2fs_summary sum; |
856 | struct node_info ni; | 940 | struct node_info ni; |
857 | 941 | ||
858 | f2fs_bug_on(old_blkaddr == NULL_ADDR); | 942 | f2fs_bug_on(dn->data_blkaddr == NULL_ADDR); |
859 | get_node_info(sbi, dn->nid, &ni); | 943 | get_node_info(sbi, dn->nid, &ni); |
860 | set_summary(&sum, dn->nid, dn->ofs_in_node, ni.version); | 944 | set_summary(&sum, dn->nid, dn->ofs_in_node, ni.version); |
861 | 945 | ||
862 | do_write_page(sbi, page, old_blkaddr, | 946 | do_write_page(sbi, page, dn->data_blkaddr, new_blkaddr, &sum, fio); |
863 | new_blkaddr, &sum, DATA); | ||
864 | } | 947 | } |
865 | 948 | ||
866 | void rewrite_data_page(struct f2fs_sb_info *sbi, struct page *page, | 949 | void rewrite_data_page(struct page *page, block_t old_blkaddr, |
867 | block_t old_blk_addr) | 950 | struct f2fs_io_info *fio) |
868 | { | 951 | { |
869 | submit_write_page(sbi, page, old_blk_addr, DATA); | 952 | struct inode *inode = page->mapping->host; |
953 | struct f2fs_sb_info *sbi = F2FS_SB(inode->i_sb); | ||
954 | f2fs_submit_page_mbio(sbi, page, old_blkaddr, fio); | ||
870 | } | 955 | } |
871 | 956 | ||
872 | void recover_data_page(struct f2fs_sb_info *sbi, | 957 | void recover_data_page(struct f2fs_sb_info *sbi, |
@@ -925,6 +1010,10 @@ void rewrite_node_page(struct f2fs_sb_info *sbi, | |||
925 | unsigned int segno, old_cursegno; | 1010 | unsigned int segno, old_cursegno; |
926 | block_t next_blkaddr = next_blkaddr_of_node(page); | 1011 | block_t next_blkaddr = next_blkaddr_of_node(page); |
927 | unsigned int next_segno = GET_SEGNO(sbi, next_blkaddr); | 1012 | unsigned int next_segno = GET_SEGNO(sbi, next_blkaddr); |
1013 | struct f2fs_io_info fio = { | ||
1014 | .type = NODE, | ||
1015 | .rw = WRITE_SYNC, | ||
1016 | }; | ||
928 | 1017 | ||
929 | curseg = CURSEG_I(sbi, type); | 1018 | curseg = CURSEG_I(sbi, type); |
930 | 1019 | ||
@@ -953,8 +1042,8 @@ void rewrite_node_page(struct f2fs_sb_info *sbi, | |||
953 | 1042 | ||
954 | /* rewrite node page */ | 1043 | /* rewrite node page */ |
955 | set_page_writeback(page); | 1044 | set_page_writeback(page); |
956 | submit_write_page(sbi, page, new_blkaddr, NODE); | 1045 | f2fs_submit_page_mbio(sbi, page, new_blkaddr, &fio); |
957 | f2fs_submit_bio(sbi, NODE, true); | 1046 | f2fs_submit_merged_bio(sbi, NODE, WRITE); |
958 | refresh_sit_entry(sbi, old_blkaddr, new_blkaddr); | 1047 | refresh_sit_entry(sbi, old_blkaddr, new_blkaddr); |
959 | 1048 | ||
960 | locate_dirty_segment(sbi, old_cursegno); | 1049 | locate_dirty_segment(sbi, old_cursegno); |
@@ -964,6 +1053,16 @@ void rewrite_node_page(struct f2fs_sb_info *sbi, | |||
964 | mutex_unlock(&curseg->curseg_mutex); | 1053 | mutex_unlock(&curseg->curseg_mutex); |
965 | } | 1054 | } |
966 | 1055 | ||
1056 | void f2fs_wait_on_page_writeback(struct page *page, | ||
1057 | enum page_type type) | ||
1058 | { | ||
1059 | struct f2fs_sb_info *sbi = F2FS_SB(page->mapping->host->i_sb); | ||
1060 | if (PageWriteback(page)) { | ||
1061 | f2fs_submit_merged_bio(sbi, type, WRITE); | ||
1062 | wait_on_page_writeback(page); | ||
1063 | } | ||
1064 | } | ||
1065 | |||
967 | static int read_compacted_summaries(struct f2fs_sb_info *sbi) | 1066 | static int read_compacted_summaries(struct f2fs_sb_info *sbi) |
968 | { | 1067 | { |
969 | struct f2fs_checkpoint *ckpt = F2FS_CKPT(sbi); | 1068 | struct f2fs_checkpoint *ckpt = F2FS_CKPT(sbi); |
@@ -1314,6 +1413,10 @@ void flush_sit_entries(struct f2fs_sb_info *sbi) | |||
1314 | 1413 | ||
1315 | sit_offset = SIT_ENTRY_OFFSET(sit_i, segno); | 1414 | sit_offset = SIT_ENTRY_OFFSET(sit_i, segno); |
1316 | 1415 | ||
1416 | /* add discard candidates */ | ||
1417 | if (SM_I(sbi)->nr_discards < SM_I(sbi)->max_discards) | ||
1418 | add_discard_addrs(sbi, segno, se); | ||
1419 | |||
1317 | if (flushed) | 1420 | if (flushed) |
1318 | goto to_sit_page; | 1421 | goto to_sit_page; |
1319 | 1422 | ||
@@ -1480,41 +1583,94 @@ static int build_curseg(struct f2fs_sb_info *sbi) | |||
1480 | return restore_curseg_summaries(sbi); | 1583 | return restore_curseg_summaries(sbi); |
1481 | } | 1584 | } |
1482 | 1585 | ||
1586 | static int ra_sit_pages(struct f2fs_sb_info *sbi, int start, int nrpages) | ||
1587 | { | ||
1588 | struct address_space *mapping = META_MAPPING(sbi); | ||
1589 | struct page *page; | ||
1590 | block_t blk_addr, prev_blk_addr = 0; | ||
1591 | int sit_blk_cnt = SIT_BLK_CNT(sbi); | ||
1592 | int blkno = start; | ||
1593 | struct f2fs_io_info fio = { | ||
1594 | .type = META, | ||
1595 | .rw = READ_SYNC | REQ_META | REQ_PRIO | ||
1596 | }; | ||
1597 | |||
1598 | for (; blkno < start + nrpages && blkno < sit_blk_cnt; blkno++) { | ||
1599 | |||
1600 | blk_addr = current_sit_addr(sbi, blkno * SIT_ENTRY_PER_BLOCK); | ||
1601 | |||
1602 | if (blkno != start && prev_blk_addr + 1 != blk_addr) | ||
1603 | break; | ||
1604 | prev_blk_addr = blk_addr; | ||
1605 | repeat: | ||
1606 | page = grab_cache_page(mapping, blk_addr); | ||
1607 | if (!page) { | ||
1608 | cond_resched(); | ||
1609 | goto repeat; | ||
1610 | } | ||
1611 | if (PageUptodate(page)) { | ||
1612 | mark_page_accessed(page); | ||
1613 | f2fs_put_page(page, 1); | ||
1614 | continue; | ||
1615 | } | ||
1616 | |||
1617 | f2fs_submit_page_mbio(sbi, page, blk_addr, &fio); | ||
1618 | |||
1619 | mark_page_accessed(page); | ||
1620 | f2fs_put_page(page, 0); | ||
1621 | } | ||
1622 | |||
1623 | f2fs_submit_merged_bio(sbi, META, READ); | ||
1624 | return blkno - start; | ||
1625 | } | ||
1626 | |||
1483 | static void build_sit_entries(struct f2fs_sb_info *sbi) | 1627 | static void build_sit_entries(struct f2fs_sb_info *sbi) |
1484 | { | 1628 | { |
1485 | struct sit_info *sit_i = SIT_I(sbi); | 1629 | struct sit_info *sit_i = SIT_I(sbi); |
1486 | struct curseg_info *curseg = CURSEG_I(sbi, CURSEG_COLD_DATA); | 1630 | struct curseg_info *curseg = CURSEG_I(sbi, CURSEG_COLD_DATA); |
1487 | struct f2fs_summary_block *sum = curseg->sum_blk; | 1631 | struct f2fs_summary_block *sum = curseg->sum_blk; |
1488 | unsigned int start; | 1632 | int sit_blk_cnt = SIT_BLK_CNT(sbi); |
1633 | unsigned int i, start, end; | ||
1634 | unsigned int readed, start_blk = 0; | ||
1635 | int nrpages = MAX_BIO_BLOCKS(max_hw_blocks(sbi)); | ||
1489 | 1636 | ||
1490 | for (start = 0; start < TOTAL_SEGS(sbi); start++) { | 1637 | do { |
1491 | struct seg_entry *se = &sit_i->sentries[start]; | 1638 | readed = ra_sit_pages(sbi, start_blk, nrpages); |
1492 | struct f2fs_sit_block *sit_blk; | 1639 | |
1493 | struct f2fs_sit_entry sit; | 1640 | start = start_blk * sit_i->sents_per_block; |
1494 | struct page *page; | 1641 | end = (start_blk + readed) * sit_i->sents_per_block; |
1495 | int i; | 1642 | |
1496 | 1643 | for (; start < end && start < TOTAL_SEGS(sbi); start++) { | |
1497 | mutex_lock(&curseg->curseg_mutex); | 1644 | struct seg_entry *se = &sit_i->sentries[start]; |
1498 | for (i = 0; i < sits_in_cursum(sum); i++) { | 1645 | struct f2fs_sit_block *sit_blk; |
1499 | if (le32_to_cpu(segno_in_journal(sum, i)) == start) { | 1646 | struct f2fs_sit_entry sit; |
1500 | sit = sit_in_journal(sum, i); | 1647 | struct page *page; |
1501 | mutex_unlock(&curseg->curseg_mutex); | 1648 | |
1502 | goto got_it; | 1649 | mutex_lock(&curseg->curseg_mutex); |
1650 | for (i = 0; i < sits_in_cursum(sum); i++) { | ||
1651 | if (le32_to_cpu(segno_in_journal(sum, i)) | ||
1652 | == start) { | ||
1653 | sit = sit_in_journal(sum, i); | ||
1654 | mutex_unlock(&curseg->curseg_mutex); | ||
1655 | goto got_it; | ||
1656 | } | ||
1503 | } | 1657 | } |
1504 | } | 1658 | mutex_unlock(&curseg->curseg_mutex); |
1505 | mutex_unlock(&curseg->curseg_mutex); | 1659 | |
1506 | page = get_current_sit_page(sbi, start); | 1660 | page = get_current_sit_page(sbi, start); |
1507 | sit_blk = (struct f2fs_sit_block *)page_address(page); | 1661 | sit_blk = (struct f2fs_sit_block *)page_address(page); |
1508 | sit = sit_blk->entries[SIT_ENTRY_OFFSET(sit_i, start)]; | 1662 | sit = sit_blk->entries[SIT_ENTRY_OFFSET(sit_i, start)]; |
1509 | f2fs_put_page(page, 1); | 1663 | f2fs_put_page(page, 1); |
1510 | got_it: | 1664 | got_it: |
1511 | check_block_count(sbi, start, &sit); | 1665 | check_block_count(sbi, start, &sit); |
1512 | seg_info_from_raw_sit(se, &sit); | 1666 | seg_info_from_raw_sit(se, &sit); |
1513 | if (sbi->segs_per_sec > 1) { | 1667 | if (sbi->segs_per_sec > 1) { |
1514 | struct sec_entry *e = get_sec_entry(sbi, start); | 1668 | struct sec_entry *e = get_sec_entry(sbi, start); |
1515 | e->valid_blocks += se->valid_blocks; | 1669 | e->valid_blocks += se->valid_blocks; |
1670 | } | ||
1516 | } | 1671 | } |
1517 | } | 1672 | start_blk += readed; |
1673 | } while (start_blk < sit_blk_cnt); | ||
1518 | } | 1674 | } |
1519 | 1675 | ||
1520 | static void init_free_segmap(struct f2fs_sb_info *sbi) | 1676 | static void init_free_segmap(struct f2fs_sb_info *sbi) |
@@ -1644,6 +1800,12 @@ int build_segment_manager(struct f2fs_sb_info *sbi) | |||
1644 | sm_info->main_segments = le32_to_cpu(raw_super->segment_count_main); | 1800 | sm_info->main_segments = le32_to_cpu(raw_super->segment_count_main); |
1645 | sm_info->ssa_blkaddr = le32_to_cpu(raw_super->ssa_blkaddr); | 1801 | sm_info->ssa_blkaddr = le32_to_cpu(raw_super->ssa_blkaddr); |
1646 | sm_info->rec_prefree_segments = DEF_RECLAIM_PREFREE_SEGMENTS; | 1802 | sm_info->rec_prefree_segments = DEF_RECLAIM_PREFREE_SEGMENTS; |
1803 | sm_info->ipu_policy = F2FS_IPU_DISABLE; | ||
1804 | sm_info->min_ipu_util = DEF_MIN_IPU_UTIL; | ||
1805 | |||
1806 | INIT_LIST_HEAD(&sm_info->discard_list); | ||
1807 | sm_info->nr_discards = 0; | ||
1808 | sm_info->max_discards = 0; | ||
1647 | 1809 | ||
1648 | err = build_sit_info(sbi); | 1810 | err = build_sit_info(sbi); |
1649 | if (err) | 1811 | if (err) |
@@ -1760,3 +1922,17 @@ void destroy_segment_manager(struct f2fs_sb_info *sbi) | |||
1760 | sbi->sm_info = NULL; | 1922 | sbi->sm_info = NULL; |
1761 | kfree(sm_info); | 1923 | kfree(sm_info); |
1762 | } | 1924 | } |
1925 | |||
1926 | int __init create_segment_manager_caches(void) | ||
1927 | { | ||
1928 | discard_entry_slab = f2fs_kmem_cache_create("discard_entry", | ||
1929 | sizeof(struct discard_entry), NULL); | ||
1930 | if (!discard_entry_slab) | ||
1931 | return -ENOMEM; | ||
1932 | return 0; | ||
1933 | } | ||
1934 | |||
1935 | void destroy_segment_manager_caches(void) | ||
1936 | { | ||
1937 | kmem_cache_destroy(discard_entry_slab); | ||
1938 | } | ||