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>
17 #include <linux/swap.h>
22 #include <trace/events/f2fs.h>
24 #define __reverse_ffz(x) __reverse_ffs(~(x))
26 static struct kmem_cache *discard_entry_slab;
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.
32 static inline unsigned long __reverse_ffs(unsigned long word)
36 #if BITS_PER_LONG == 64
37 if ((word & 0xffffffff) == 0) {
42 if ((word & 0xffff) == 0) {
46 if ((word & 0xff) == 0) {
50 if ((word & 0xf0) == 0)
54 if ((word & 0xc) == 0)
58 if ((word & 0x2) == 0)
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.
68 * f2fs_set_bit(0, bitmap) => 0000 0001
69 * f2fs_set_bit(7, bitmap) => 1000 0000
71 static unsigned long __find_rev_next_bit(const unsigned long *addr,
72 unsigned long size, unsigned long offset)
74 const unsigned long *p = addr + BIT_WORD(offset);
75 unsigned long result = offset & ~(BITS_PER_LONG - 1);
77 unsigned long mask, submask;
78 unsigned long quot, rest;
84 offset %= BITS_PER_LONG;
89 quot = (offset >> 3) << 3;
92 submask = (unsigned char)(0xff << rest) >> rest;
96 if (size < BITS_PER_LONG)
101 size -= BITS_PER_LONG;
102 result += BITS_PER_LONG;
104 while (size & ~(BITS_PER_LONG-1)) {
108 result += BITS_PER_LONG;
109 size -= BITS_PER_LONG;
115 tmp &= (~0UL >> (BITS_PER_LONG - size));
116 if (tmp == 0UL) /* Are any bits set? */
117 return result + size; /* Nope. */
119 return result + __reverse_ffs(tmp);
122 static unsigned long __find_rev_next_zero_bit(const unsigned long *addr,
123 unsigned long size, unsigned long offset)
125 const unsigned long *p = addr + BIT_WORD(offset);
126 unsigned long result = offset & ~(BITS_PER_LONG - 1);
128 unsigned long mask, submask;
129 unsigned long quot, rest;
135 offset %= BITS_PER_LONG;
140 quot = (offset >> 3) << 3;
142 mask = ~(~0UL << quot);
143 submask = (unsigned char)~((unsigned char)(0xff << rest) >> rest);
147 if (size < BITS_PER_LONG)
152 size -= BITS_PER_LONG;
153 result += BITS_PER_LONG;
155 while (size & ~(BITS_PER_LONG - 1)) {
159 result += BITS_PER_LONG;
160 size -= BITS_PER_LONG;
168 if (tmp == ~0UL) /* Are any bits zero? */
169 return result + size; /* Nope. */
171 return result + __reverse_ffz(tmp);
175 * This function balances dirty node and dentry pages.
176 * In addition, it controls garbage collection.
178 void f2fs_balance_fs(struct f2fs_sb_info *sbi)
181 * We should do GC or end up with checkpoint, if there are so many dirty
182 * dir/node pages without enough free segments.
184 if (has_not_enough_free_secs(sbi, 0)) {
185 mutex_lock(&sbi->gc_mutex);
190 void f2fs_balance_fs_bg(struct f2fs_sb_info *sbi)
192 /* check the # of cached NAT entries and prefree segments */
193 if (try_to_free_nats(sbi, NAT_ENTRY_PER_BLOCK) ||
194 excess_prefree_segs(sbi))
195 f2fs_sync_fs(sbi->sb, true);
198 static void __locate_dirty_segment(struct f2fs_sb_info *sbi, unsigned int segno,
199 enum dirty_type dirty_type)
201 struct dirty_seglist_info *dirty_i = DIRTY_I(sbi);
203 /* need not be added */
204 if (IS_CURSEG(sbi, segno))
207 if (!test_and_set_bit(segno, dirty_i->dirty_segmap[dirty_type]))
208 dirty_i->nr_dirty[dirty_type]++;
210 if (dirty_type == DIRTY) {
211 struct seg_entry *sentry = get_seg_entry(sbi, segno);
212 enum dirty_type t = sentry->type;
214 if (!test_and_set_bit(segno, dirty_i->dirty_segmap[t]))
215 dirty_i->nr_dirty[t]++;
219 static void __remove_dirty_segment(struct f2fs_sb_info *sbi, unsigned int segno,
220 enum dirty_type dirty_type)
222 struct dirty_seglist_info *dirty_i = DIRTY_I(sbi);
224 if (test_and_clear_bit(segno, dirty_i->dirty_segmap[dirty_type]))
225 dirty_i->nr_dirty[dirty_type]--;
227 if (dirty_type == DIRTY) {
228 struct seg_entry *sentry = get_seg_entry(sbi, segno);
229 enum dirty_type t = sentry->type;
231 if (test_and_clear_bit(segno, dirty_i->dirty_segmap[t]))
232 dirty_i->nr_dirty[t]--;
234 if (get_valid_blocks(sbi, segno, sbi->segs_per_sec) == 0)
235 clear_bit(GET_SECNO(sbi, segno),
236 dirty_i->victim_secmap);
241 * Should not occur error such as -ENOMEM.
242 * Adding dirty entry into seglist is not critical operation.
243 * If a given segment is one of current working segments, it won't be added.
245 static void locate_dirty_segment(struct f2fs_sb_info *sbi, unsigned int segno)
247 struct dirty_seglist_info *dirty_i = DIRTY_I(sbi);
248 unsigned short valid_blocks;
250 if (segno == NULL_SEGNO || IS_CURSEG(sbi, segno))
253 mutex_lock(&dirty_i->seglist_lock);
255 valid_blocks = get_valid_blocks(sbi, segno, 0);
257 if (valid_blocks == 0) {
258 __locate_dirty_segment(sbi, segno, PRE);
259 __remove_dirty_segment(sbi, segno, DIRTY);
260 } else if (valid_blocks < sbi->blocks_per_seg) {
261 __locate_dirty_segment(sbi, segno, DIRTY);
263 /* Recovery routine with SSR needs this */
264 __remove_dirty_segment(sbi, segno, DIRTY);
267 mutex_unlock(&dirty_i->seglist_lock);
270 static void f2fs_issue_discard(struct f2fs_sb_info *sbi,
271 block_t blkstart, block_t blklen)
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);
279 static void add_discard_addrs(struct f2fs_sb_info *sbi,
280 unsigned int segno, struct seg_entry *se)
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;
292 if (!test_opt(sbi, DISCARD))
295 /* zero block will be discarded through the prefree list */
296 if (!se->valid_blocks || se->valid_blocks == max_blocks)
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];
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)
308 end = __find_rev_next_zero_bit(dmap, max_blocks, start + 1);
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;
315 list_add_tail(&new->list, head);
316 SM_I(sbi)->nr_discards += end - start;
321 * Should call clear_prefree_segments after checkpoint is done.
323 static void set_prefree_as_free_segments(struct f2fs_sb_info *sbi)
325 struct dirty_seglist_info *dirty_i = DIRTY_I(sbi);
326 unsigned int segno = -1;
327 unsigned int total_segs = TOTAL_SEGS(sbi);
329 mutex_lock(&dirty_i->seglist_lock);
331 segno = find_next_bit(dirty_i->dirty_segmap[PRE], total_segs,
333 if (segno >= total_segs)
335 __set_test_and_free(sbi, segno);
337 mutex_unlock(&dirty_i->seglist_lock);
340 void clear_prefree_segments(struct f2fs_sb_info *sbi)
342 struct list_head *head = &(SM_I(sbi)->discard_list);
343 struct discard_entry *entry, *this;
344 struct dirty_seglist_info *dirty_i = DIRTY_I(sbi);
345 unsigned long *prefree_map = dirty_i->dirty_segmap[PRE];
346 unsigned int total_segs = TOTAL_SEGS(sbi);
347 unsigned int start = 0, end = -1;
349 mutex_lock(&dirty_i->seglist_lock);
353 start = find_next_bit(prefree_map, total_segs, end + 1);
354 if (start >= total_segs)
356 end = find_next_zero_bit(prefree_map, total_segs, start + 1);
358 for (i = start; i < end; i++)
359 clear_bit(i, prefree_map);
361 dirty_i->nr_dirty[PRE] -= end - start;
363 if (!test_opt(sbi, DISCARD))
366 f2fs_issue_discard(sbi, START_BLOCK(sbi, start),
367 (end - start) << sbi->log_blocks_per_seg);
369 mutex_unlock(&dirty_i->seglist_lock);
371 /* send small discards */
372 list_for_each_entry_safe(entry, this, head, list) {
373 f2fs_issue_discard(sbi, entry->blkaddr, entry->len);
374 list_del(&entry->list);
375 SM_I(sbi)->nr_discards -= entry->len;
376 kmem_cache_free(discard_entry_slab, entry);
380 static void __mark_sit_entry_dirty(struct f2fs_sb_info *sbi, unsigned int segno)
382 struct sit_info *sit_i = SIT_I(sbi);
383 if (!__test_and_set_bit(segno, sit_i->dirty_sentries_bitmap))
384 sit_i->dirty_sentries++;
387 static void __set_sit_entry_type(struct f2fs_sb_info *sbi, int type,
388 unsigned int segno, int modified)
390 struct seg_entry *se = get_seg_entry(sbi, segno);
393 __mark_sit_entry_dirty(sbi, segno);
396 static void update_sit_entry(struct f2fs_sb_info *sbi, block_t blkaddr, int del)
398 struct seg_entry *se;
399 unsigned int segno, offset;
400 long int new_vblocks;
402 segno = GET_SEGNO(sbi, blkaddr);
404 se = get_seg_entry(sbi, segno);
405 new_vblocks = se->valid_blocks + del;
406 offset = GET_BLKOFF_FROM_SEG0(sbi, blkaddr);
408 f2fs_bug_on((new_vblocks >> (sizeof(unsigned short) << 3) ||
409 (new_vblocks > sbi->blocks_per_seg)));
411 se->valid_blocks = new_vblocks;
412 se->mtime = get_mtime(sbi);
413 SIT_I(sbi)->max_mtime = se->mtime;
415 /* Update valid block bitmap */
417 if (f2fs_set_bit(offset, se->cur_valid_map))
420 if (!f2fs_clear_bit(offset, se->cur_valid_map))
423 if (!f2fs_test_bit(offset, se->ckpt_valid_map))
424 se->ckpt_valid_blocks += del;
426 __mark_sit_entry_dirty(sbi, segno);
428 /* update total number of valid blocks to be written in ckpt area */
429 SIT_I(sbi)->written_valid_blocks += del;
431 if (sbi->segs_per_sec > 1)
432 get_sec_entry(sbi, segno)->valid_blocks += del;
435 void refresh_sit_entry(struct f2fs_sb_info *sbi, block_t old, block_t new)
437 update_sit_entry(sbi, new, 1);
438 if (GET_SEGNO(sbi, old) != NULL_SEGNO)
439 update_sit_entry(sbi, old, -1);
441 locate_dirty_segment(sbi, GET_SEGNO(sbi, old));
442 locate_dirty_segment(sbi, GET_SEGNO(sbi, new));
445 void invalidate_blocks(struct f2fs_sb_info *sbi, block_t addr)
447 unsigned int segno = GET_SEGNO(sbi, addr);
448 struct sit_info *sit_i = SIT_I(sbi);
450 f2fs_bug_on(addr == NULL_ADDR);
451 if (addr == NEW_ADDR)
454 /* add it into sit main buffer */
455 mutex_lock(&sit_i->sentry_lock);
457 update_sit_entry(sbi, addr, -1);
459 /* add it into dirty seglist */
460 locate_dirty_segment(sbi, segno);
462 mutex_unlock(&sit_i->sentry_lock);
466 * This function should be resided under the curseg_mutex lock
468 static void __add_sum_entry(struct f2fs_sb_info *sbi, int type,
469 struct f2fs_summary *sum)
471 struct curseg_info *curseg = CURSEG_I(sbi, type);
472 void *addr = curseg->sum_blk;
473 addr += curseg->next_blkoff * sizeof(struct f2fs_summary);
474 memcpy(addr, sum, sizeof(struct f2fs_summary));
478 * Calculate the number of current summary pages for writing
480 int npages_for_summary_flush(struct f2fs_sb_info *sbi)
482 int valid_sum_count = 0;
485 for (i = CURSEG_HOT_DATA; i <= CURSEG_COLD_DATA; i++) {
486 if (sbi->ckpt->alloc_type[i] == SSR)
487 valid_sum_count += sbi->blocks_per_seg;
489 valid_sum_count += curseg_blkoff(sbi, i);
492 sum_in_page = (PAGE_CACHE_SIZE - 2 * SUM_JOURNAL_SIZE -
493 SUM_FOOTER_SIZE) / SUMMARY_SIZE;
494 if (valid_sum_count <= sum_in_page)
496 else if ((valid_sum_count - sum_in_page) <=
497 (PAGE_CACHE_SIZE - SUM_FOOTER_SIZE) / SUMMARY_SIZE)
503 * Caller should put this summary page
505 struct page *get_sum_page(struct f2fs_sb_info *sbi, unsigned int segno)
507 return get_meta_page(sbi, GET_SUM_BLOCK(sbi, segno));
510 static void write_sum_page(struct f2fs_sb_info *sbi,
511 struct f2fs_summary_block *sum_blk, block_t blk_addr)
513 struct page *page = grab_meta_page(sbi, blk_addr);
514 void *kaddr = page_address(page);
515 memcpy(kaddr, sum_blk, PAGE_CACHE_SIZE);
516 set_page_dirty(page);
517 f2fs_put_page(page, 1);
520 static int is_next_segment_free(struct f2fs_sb_info *sbi, int type)
522 struct curseg_info *curseg = CURSEG_I(sbi, type);
523 unsigned int segno = curseg->segno + 1;
524 struct free_segmap_info *free_i = FREE_I(sbi);
526 if (segno < TOTAL_SEGS(sbi) && segno % sbi->segs_per_sec)
527 return !test_bit(segno, free_i->free_segmap);
532 * Find a new segment from the free segments bitmap to right order
533 * This function should be returned with success, otherwise BUG
535 static void get_new_segment(struct f2fs_sb_info *sbi,
536 unsigned int *newseg, bool new_sec, int dir)
538 struct free_segmap_info *free_i = FREE_I(sbi);
539 unsigned int segno, secno, zoneno;
540 unsigned int total_zones = TOTAL_SECS(sbi) / sbi->secs_per_zone;
541 unsigned int hint = *newseg / sbi->segs_per_sec;
542 unsigned int old_zoneno = GET_ZONENO_FROM_SEGNO(sbi, *newseg);
543 unsigned int left_start = hint;
548 write_lock(&free_i->segmap_lock);
550 if (!new_sec && ((*newseg + 1) % sbi->segs_per_sec)) {
551 segno = find_next_zero_bit(free_i->free_segmap,
552 TOTAL_SEGS(sbi), *newseg + 1);
553 if (segno - *newseg < sbi->segs_per_sec -
554 (*newseg % sbi->segs_per_sec))
558 secno = find_next_zero_bit(free_i->free_secmap, TOTAL_SECS(sbi), hint);
559 if (secno >= TOTAL_SECS(sbi)) {
560 if (dir == ALLOC_RIGHT) {
561 secno = find_next_zero_bit(free_i->free_secmap,
563 f2fs_bug_on(secno >= TOTAL_SECS(sbi));
566 left_start = hint - 1;
572 while (test_bit(left_start, free_i->free_secmap)) {
573 if (left_start > 0) {
577 left_start = find_next_zero_bit(free_i->free_secmap,
579 f2fs_bug_on(left_start >= TOTAL_SECS(sbi));
585 segno = secno * sbi->segs_per_sec;
586 zoneno = secno / sbi->secs_per_zone;
588 /* give up on finding another zone */
591 if (sbi->secs_per_zone == 1)
593 if (zoneno == old_zoneno)
595 if (dir == ALLOC_LEFT) {
596 if (!go_left && zoneno + 1 >= total_zones)
598 if (go_left && zoneno == 0)
601 for (i = 0; i < NR_CURSEG_TYPE; i++)
602 if (CURSEG_I(sbi, i)->zone == zoneno)
605 if (i < NR_CURSEG_TYPE) {
606 /* zone is in user, try another */
608 hint = zoneno * sbi->secs_per_zone - 1;
609 else if (zoneno + 1 >= total_zones)
612 hint = (zoneno + 1) * sbi->secs_per_zone;
614 goto find_other_zone;
617 /* set it as dirty segment in free segmap */
618 f2fs_bug_on(test_bit(segno, free_i->free_segmap));
619 __set_inuse(sbi, segno);
621 write_unlock(&free_i->segmap_lock);
624 static void reset_curseg(struct f2fs_sb_info *sbi, int type, int modified)
626 struct curseg_info *curseg = CURSEG_I(sbi, type);
627 struct summary_footer *sum_footer;
629 curseg->segno = curseg->next_segno;
630 curseg->zone = GET_ZONENO_FROM_SEGNO(sbi, curseg->segno);
631 curseg->next_blkoff = 0;
632 curseg->next_segno = NULL_SEGNO;
634 sum_footer = &(curseg->sum_blk->footer);
635 memset(sum_footer, 0, sizeof(struct summary_footer));
636 if (IS_DATASEG(type))
637 SET_SUM_TYPE(sum_footer, SUM_TYPE_DATA);
638 if (IS_NODESEG(type))
639 SET_SUM_TYPE(sum_footer, SUM_TYPE_NODE);
640 __set_sit_entry_type(sbi, type, curseg->segno, modified);
644 * Allocate a current working segment.
645 * This function always allocates a free segment in LFS manner.
647 static void new_curseg(struct f2fs_sb_info *sbi, int type, bool new_sec)
649 struct curseg_info *curseg = CURSEG_I(sbi, type);
650 unsigned int segno = curseg->segno;
651 int dir = ALLOC_LEFT;
653 write_sum_page(sbi, curseg->sum_blk,
654 GET_SUM_BLOCK(sbi, segno));
655 if (type == CURSEG_WARM_DATA || type == CURSEG_COLD_DATA)
658 if (test_opt(sbi, NOHEAP))
661 get_new_segment(sbi, &segno, new_sec, dir);
662 curseg->next_segno = segno;
663 reset_curseg(sbi, type, 1);
664 curseg->alloc_type = LFS;
667 static void __next_free_blkoff(struct f2fs_sb_info *sbi,
668 struct curseg_info *seg, block_t start)
670 struct seg_entry *se = get_seg_entry(sbi, seg->segno);
671 int entries = SIT_VBLOCK_MAP_SIZE / sizeof(unsigned long);
672 unsigned long target_map[entries];
673 unsigned long *ckpt_map = (unsigned long *)se->ckpt_valid_map;
674 unsigned long *cur_map = (unsigned long *)se->cur_valid_map;
677 for (i = 0; i < entries; i++)
678 target_map[i] = ckpt_map[i] | cur_map[i];
680 pos = __find_rev_next_zero_bit(target_map, sbi->blocks_per_seg, start);
682 seg->next_blkoff = pos;
686 * If a segment is written by LFS manner, next block offset is just obtained
687 * by increasing the current block offset. However, if a segment is written by
688 * SSR manner, next block offset obtained by calling __next_free_blkoff
690 static void __refresh_next_blkoff(struct f2fs_sb_info *sbi,
691 struct curseg_info *seg)
693 if (seg->alloc_type == SSR)
694 __next_free_blkoff(sbi, seg, seg->next_blkoff + 1);
700 * This function always allocates a used segment (from dirty seglist) by SSR
701 * manner, so it should recover the existing segment information of valid blocks
703 static void change_curseg(struct f2fs_sb_info *sbi, int type, bool reuse)
705 struct dirty_seglist_info *dirty_i = DIRTY_I(sbi);
706 struct curseg_info *curseg = CURSEG_I(sbi, type);
707 unsigned int new_segno = curseg->next_segno;
708 struct f2fs_summary_block *sum_node;
709 struct page *sum_page;
711 write_sum_page(sbi, curseg->sum_blk,
712 GET_SUM_BLOCK(sbi, curseg->segno));
713 __set_test_and_inuse(sbi, new_segno);
715 mutex_lock(&dirty_i->seglist_lock);
716 __remove_dirty_segment(sbi, new_segno, PRE);
717 __remove_dirty_segment(sbi, new_segno, DIRTY);
718 mutex_unlock(&dirty_i->seglist_lock);
720 reset_curseg(sbi, type, 1);
721 curseg->alloc_type = SSR;
722 __next_free_blkoff(sbi, curseg, 0);
725 sum_page = get_sum_page(sbi, new_segno);
726 sum_node = (struct f2fs_summary_block *)page_address(sum_page);
727 memcpy(curseg->sum_blk, sum_node, SUM_ENTRY_SIZE);
728 f2fs_put_page(sum_page, 1);
732 static int get_ssr_segment(struct f2fs_sb_info *sbi, int type)
734 struct curseg_info *curseg = CURSEG_I(sbi, type);
735 const struct victim_selection *v_ops = DIRTY_I(sbi)->v_ops;
737 if (IS_NODESEG(type) || !has_not_enough_free_secs(sbi, 0))
738 return v_ops->get_victim(sbi,
739 &(curseg)->next_segno, BG_GC, type, SSR);
741 /* For data segments, let's do SSR more intensively */
742 for (; type >= CURSEG_HOT_DATA; type--)
743 if (v_ops->get_victim(sbi, &(curseg)->next_segno,
750 * flush out current segment and replace it with new segment
751 * This function should be returned with success, otherwise BUG
753 static void allocate_segment_by_default(struct f2fs_sb_info *sbi,
754 int type, bool force)
756 struct curseg_info *curseg = CURSEG_I(sbi, type);
759 new_curseg(sbi, type, true);
760 else if (type == CURSEG_WARM_NODE)
761 new_curseg(sbi, type, false);
762 else if (curseg->alloc_type == LFS && is_next_segment_free(sbi, type))
763 new_curseg(sbi, type, false);
764 else if (need_SSR(sbi) && get_ssr_segment(sbi, type))
765 change_curseg(sbi, type, true);
767 new_curseg(sbi, type, false);
769 stat_inc_seg_type(sbi, curseg);
772 void allocate_new_segments(struct f2fs_sb_info *sbi)
774 struct curseg_info *curseg;
775 unsigned int old_curseg;
778 for (i = CURSEG_HOT_DATA; i <= CURSEG_COLD_DATA; i++) {
779 curseg = CURSEG_I(sbi, i);
780 old_curseg = curseg->segno;
781 SIT_I(sbi)->s_ops->allocate_segment(sbi, i, true);
782 locate_dirty_segment(sbi, old_curseg);
786 static const struct segment_allocation default_salloc_ops = {
787 .allocate_segment = allocate_segment_by_default,
790 static bool __has_curseg_space(struct f2fs_sb_info *sbi, int type)
792 struct curseg_info *curseg = CURSEG_I(sbi, type);
793 if (curseg->next_blkoff < sbi->blocks_per_seg)
798 static int __get_segment_type_2(struct page *page, enum page_type p_type)
801 return CURSEG_HOT_DATA;
803 return CURSEG_HOT_NODE;
806 static int __get_segment_type_4(struct page *page, enum page_type p_type)
808 if (p_type == DATA) {
809 struct inode *inode = page->mapping->host;
811 if (S_ISDIR(inode->i_mode))
812 return CURSEG_HOT_DATA;
814 return CURSEG_COLD_DATA;
816 if (IS_DNODE(page) && !is_cold_node(page))
817 return CURSEG_HOT_NODE;
819 return CURSEG_COLD_NODE;
823 static int __get_segment_type_6(struct page *page, enum page_type p_type)
825 if (p_type == DATA) {
826 struct inode *inode = page->mapping->host;
828 if (S_ISDIR(inode->i_mode))
829 return CURSEG_HOT_DATA;
830 else if (is_cold_data(page) || file_is_cold(inode))
831 return CURSEG_COLD_DATA;
833 return CURSEG_WARM_DATA;
836 return is_cold_node(page) ? CURSEG_WARM_NODE :
839 return CURSEG_COLD_NODE;
843 static int __get_segment_type(struct page *page, enum page_type p_type)
845 struct f2fs_sb_info *sbi = F2FS_SB(page->mapping->host->i_sb);
846 switch (sbi->active_logs) {
848 return __get_segment_type_2(page, p_type);
850 return __get_segment_type_4(page, p_type);
852 /* NR_CURSEG_TYPE(6) logs by default */
853 f2fs_bug_on(sbi->active_logs != NR_CURSEG_TYPE);
854 return __get_segment_type_6(page, p_type);
857 void allocate_data_block(struct f2fs_sb_info *sbi, struct page *page,
858 block_t old_blkaddr, block_t *new_blkaddr,
859 struct f2fs_summary *sum, int type)
861 struct sit_info *sit_i = SIT_I(sbi);
862 struct curseg_info *curseg;
863 unsigned int old_cursegno;
865 curseg = CURSEG_I(sbi, type);
867 mutex_lock(&curseg->curseg_mutex);
869 *new_blkaddr = NEXT_FREE_BLKADDR(sbi, curseg);
870 old_cursegno = curseg->segno;
873 * __add_sum_entry should be resided under the curseg_mutex
874 * because, this function updates a summary entry in the
875 * current summary block.
877 __add_sum_entry(sbi, type, sum);
879 mutex_lock(&sit_i->sentry_lock);
880 __refresh_next_blkoff(sbi, curseg);
882 stat_inc_block_count(sbi, curseg);
884 if (!__has_curseg_space(sbi, type))
885 sit_i->s_ops->allocate_segment(sbi, type, false);
887 * SIT information should be updated before segment allocation,
888 * since SSR needs latest valid block information.
890 refresh_sit_entry(sbi, old_blkaddr, *new_blkaddr);
891 locate_dirty_segment(sbi, old_cursegno);
893 mutex_unlock(&sit_i->sentry_lock);
895 if (page && IS_NODESEG(type))
896 fill_node_footer_blkaddr(page, NEXT_FREE_BLKADDR(sbi, curseg));
898 mutex_unlock(&curseg->curseg_mutex);
901 static void do_write_page(struct f2fs_sb_info *sbi, struct page *page,
902 block_t old_blkaddr, block_t *new_blkaddr,
903 struct f2fs_summary *sum, struct f2fs_io_info *fio)
905 int type = __get_segment_type(page, fio->type);
907 allocate_data_block(sbi, page, old_blkaddr, new_blkaddr, sum, type);
909 /* writeout dirty page into bdev */
910 f2fs_submit_page_mbio(sbi, page, *new_blkaddr, fio);
913 void write_meta_page(struct f2fs_sb_info *sbi, struct page *page)
915 struct f2fs_io_info fio = {
917 .rw = WRITE_SYNC | REQ_META | REQ_PRIO
920 set_page_writeback(page);
921 f2fs_submit_page_mbio(sbi, page, page->index, &fio);
924 void write_node_page(struct f2fs_sb_info *sbi, struct page *page,
925 struct f2fs_io_info *fio,
926 unsigned int nid, block_t old_blkaddr, block_t *new_blkaddr)
928 struct f2fs_summary sum;
929 set_summary(&sum, nid, 0, 0);
930 do_write_page(sbi, page, old_blkaddr, new_blkaddr, &sum, fio);
933 void write_data_page(struct page *page, struct dnode_of_data *dn,
934 block_t *new_blkaddr, struct f2fs_io_info *fio)
936 struct f2fs_sb_info *sbi = F2FS_SB(dn->inode->i_sb);
937 struct f2fs_summary sum;
940 f2fs_bug_on(dn->data_blkaddr == NULL_ADDR);
941 get_node_info(sbi, dn->nid, &ni);
942 set_summary(&sum, dn->nid, dn->ofs_in_node, ni.version);
944 do_write_page(sbi, page, dn->data_blkaddr, new_blkaddr, &sum, fio);
947 void rewrite_data_page(struct page *page, block_t old_blkaddr,
948 struct f2fs_io_info *fio)
950 struct inode *inode = page->mapping->host;
951 struct f2fs_sb_info *sbi = F2FS_SB(inode->i_sb);
952 f2fs_submit_page_mbio(sbi, page, old_blkaddr, fio);
955 void recover_data_page(struct f2fs_sb_info *sbi,
956 struct page *page, struct f2fs_summary *sum,
957 block_t old_blkaddr, block_t new_blkaddr)
959 struct sit_info *sit_i = SIT_I(sbi);
960 struct curseg_info *curseg;
961 unsigned int segno, old_cursegno;
962 struct seg_entry *se;
965 segno = GET_SEGNO(sbi, new_blkaddr);
966 se = get_seg_entry(sbi, segno);
969 if (se->valid_blocks == 0 && !IS_CURSEG(sbi, segno)) {
970 if (old_blkaddr == NULL_ADDR)
971 type = CURSEG_COLD_DATA;
973 type = CURSEG_WARM_DATA;
975 curseg = CURSEG_I(sbi, type);
977 mutex_lock(&curseg->curseg_mutex);
978 mutex_lock(&sit_i->sentry_lock);
980 old_cursegno = curseg->segno;
982 /* change the current segment */
983 if (segno != curseg->segno) {
984 curseg->next_segno = segno;
985 change_curseg(sbi, type, true);
988 curseg->next_blkoff = GET_BLKOFF_FROM_SEG0(sbi, new_blkaddr);
989 __add_sum_entry(sbi, type, sum);
991 refresh_sit_entry(sbi, old_blkaddr, new_blkaddr);
992 locate_dirty_segment(sbi, old_cursegno);
994 mutex_unlock(&sit_i->sentry_lock);
995 mutex_unlock(&curseg->curseg_mutex);
998 void rewrite_node_page(struct f2fs_sb_info *sbi,
999 struct page *page, struct f2fs_summary *sum,
1000 block_t old_blkaddr, block_t new_blkaddr)
1002 struct sit_info *sit_i = SIT_I(sbi);
1003 int type = CURSEG_WARM_NODE;
1004 struct curseg_info *curseg;
1005 unsigned int segno, old_cursegno;
1006 block_t next_blkaddr = next_blkaddr_of_node(page);
1007 unsigned int next_segno = GET_SEGNO(sbi, next_blkaddr);
1008 struct f2fs_io_info fio = {
1013 curseg = CURSEG_I(sbi, type);
1015 mutex_lock(&curseg->curseg_mutex);
1016 mutex_lock(&sit_i->sentry_lock);
1018 segno = GET_SEGNO(sbi, new_blkaddr);
1019 old_cursegno = curseg->segno;
1021 /* change the current segment */
1022 if (segno != curseg->segno) {
1023 curseg->next_segno = segno;
1024 change_curseg(sbi, type, true);
1026 curseg->next_blkoff = GET_BLKOFF_FROM_SEG0(sbi, new_blkaddr);
1027 __add_sum_entry(sbi, type, sum);
1029 /* change the current log to the next block addr in advance */
1030 if (next_segno != segno) {
1031 curseg->next_segno = next_segno;
1032 change_curseg(sbi, type, true);
1034 curseg->next_blkoff = GET_BLKOFF_FROM_SEG0(sbi, next_blkaddr);
1036 /* rewrite node page */
1037 set_page_writeback(page);
1038 f2fs_submit_page_mbio(sbi, page, new_blkaddr, &fio);
1039 f2fs_submit_merged_bio(sbi, NODE, WRITE);
1040 refresh_sit_entry(sbi, old_blkaddr, new_blkaddr);
1041 locate_dirty_segment(sbi, old_cursegno);
1043 mutex_unlock(&sit_i->sentry_lock);
1044 mutex_unlock(&curseg->curseg_mutex);
1047 static inline bool is_merged_page(struct f2fs_sb_info *sbi,
1048 struct page *page, enum page_type type)
1050 enum page_type btype = PAGE_TYPE_OF_BIO(type);
1051 struct f2fs_bio_info *io = &sbi->write_io[btype];
1052 struct bio *bio = io->bio;
1053 struct bio_vec *bvec;
1056 down_read(&io->io_rwsem);
1060 bio_for_each_segment_all(bvec, bio, i) {
1061 if (page == bvec->bv_page) {
1062 up_read(&io->io_rwsem);
1068 up_read(&io->io_rwsem);
1072 void f2fs_wait_on_page_writeback(struct page *page,
1073 enum page_type type)
1075 struct f2fs_sb_info *sbi = F2FS_SB(page->mapping->host->i_sb);
1076 if (PageWriteback(page)) {
1077 if (is_merged_page(sbi, page, type))
1078 f2fs_submit_merged_bio(sbi, type, WRITE);
1079 wait_on_page_writeback(page);
1083 static int read_compacted_summaries(struct f2fs_sb_info *sbi)
1085 struct f2fs_checkpoint *ckpt = F2FS_CKPT(sbi);
1086 struct curseg_info *seg_i;
1087 unsigned char *kaddr;
1092 start = start_sum_block(sbi);
1094 page = get_meta_page(sbi, start++);
1095 kaddr = (unsigned char *)page_address(page);
1097 /* Step 1: restore nat cache */
1098 seg_i = CURSEG_I(sbi, CURSEG_HOT_DATA);
1099 memcpy(&seg_i->sum_blk->n_nats, kaddr, SUM_JOURNAL_SIZE);
1101 /* Step 2: restore sit cache */
1102 seg_i = CURSEG_I(sbi, CURSEG_COLD_DATA);
1103 memcpy(&seg_i->sum_blk->n_sits, kaddr + SUM_JOURNAL_SIZE,
1105 offset = 2 * SUM_JOURNAL_SIZE;
1107 /* Step 3: restore summary entries */
1108 for (i = CURSEG_HOT_DATA; i <= CURSEG_COLD_DATA; i++) {
1109 unsigned short blk_off;
1112 seg_i = CURSEG_I(sbi, i);
1113 segno = le32_to_cpu(ckpt->cur_data_segno[i]);
1114 blk_off = le16_to_cpu(ckpt->cur_data_blkoff[i]);
1115 seg_i->next_segno = segno;
1116 reset_curseg(sbi, i, 0);
1117 seg_i->alloc_type = ckpt->alloc_type[i];
1118 seg_i->next_blkoff = blk_off;
1120 if (seg_i->alloc_type == SSR)
1121 blk_off = sbi->blocks_per_seg;
1123 for (j = 0; j < blk_off; j++) {
1124 struct f2fs_summary *s;
1125 s = (struct f2fs_summary *)(kaddr + offset);
1126 seg_i->sum_blk->entries[j] = *s;
1127 offset += SUMMARY_SIZE;
1128 if (offset + SUMMARY_SIZE <= PAGE_CACHE_SIZE -
1132 f2fs_put_page(page, 1);
1135 page = get_meta_page(sbi, start++);
1136 kaddr = (unsigned char *)page_address(page);
1140 f2fs_put_page(page, 1);
1144 static int read_normal_summaries(struct f2fs_sb_info *sbi, int type)
1146 struct f2fs_checkpoint *ckpt = F2FS_CKPT(sbi);
1147 struct f2fs_summary_block *sum;
1148 struct curseg_info *curseg;
1150 unsigned short blk_off;
1151 unsigned int segno = 0;
1152 block_t blk_addr = 0;
1154 /* get segment number and block addr */
1155 if (IS_DATASEG(type)) {
1156 segno = le32_to_cpu(ckpt->cur_data_segno[type]);
1157 blk_off = le16_to_cpu(ckpt->cur_data_blkoff[type -
1159 if (is_set_ckpt_flags(ckpt, CP_UMOUNT_FLAG))
1160 blk_addr = sum_blk_addr(sbi, NR_CURSEG_TYPE, type);
1162 blk_addr = sum_blk_addr(sbi, NR_CURSEG_DATA_TYPE, type);
1164 segno = le32_to_cpu(ckpt->cur_node_segno[type -
1166 blk_off = le16_to_cpu(ckpt->cur_node_blkoff[type -
1168 if (is_set_ckpt_flags(ckpt, CP_UMOUNT_FLAG))
1169 blk_addr = sum_blk_addr(sbi, NR_CURSEG_NODE_TYPE,
1170 type - CURSEG_HOT_NODE);
1172 blk_addr = GET_SUM_BLOCK(sbi, segno);
1175 new = get_meta_page(sbi, blk_addr);
1176 sum = (struct f2fs_summary_block *)page_address(new);
1178 if (IS_NODESEG(type)) {
1179 if (is_set_ckpt_flags(ckpt, CP_UMOUNT_FLAG)) {
1180 struct f2fs_summary *ns = &sum->entries[0];
1182 for (i = 0; i < sbi->blocks_per_seg; i++, ns++) {
1184 ns->ofs_in_node = 0;
1189 err = restore_node_summary(sbi, segno, sum);
1191 f2fs_put_page(new, 1);
1197 /* set uncompleted segment to curseg */
1198 curseg = CURSEG_I(sbi, type);
1199 mutex_lock(&curseg->curseg_mutex);
1200 memcpy(curseg->sum_blk, sum, PAGE_CACHE_SIZE);
1201 curseg->next_segno = segno;
1202 reset_curseg(sbi, type, 0);
1203 curseg->alloc_type = ckpt->alloc_type[type];
1204 curseg->next_blkoff = blk_off;
1205 mutex_unlock(&curseg->curseg_mutex);
1206 f2fs_put_page(new, 1);
1210 static int restore_curseg_summaries(struct f2fs_sb_info *sbi)
1212 int type = CURSEG_HOT_DATA;
1215 if (is_set_ckpt_flags(F2FS_CKPT(sbi), CP_COMPACT_SUM_FLAG)) {
1216 /* restore for compacted data summary */
1217 if (read_compacted_summaries(sbi))
1219 type = CURSEG_HOT_NODE;
1222 for (; type <= CURSEG_COLD_NODE; type++) {
1223 err = read_normal_summaries(sbi, type);
1231 static void write_compacted_summaries(struct f2fs_sb_info *sbi, block_t blkaddr)
1234 unsigned char *kaddr;
1235 struct f2fs_summary *summary;
1236 struct curseg_info *seg_i;
1237 int written_size = 0;
1240 page = grab_meta_page(sbi, blkaddr++);
1241 kaddr = (unsigned char *)page_address(page);
1243 /* Step 1: write nat cache */
1244 seg_i = CURSEG_I(sbi, CURSEG_HOT_DATA);
1245 memcpy(kaddr, &seg_i->sum_blk->n_nats, SUM_JOURNAL_SIZE);
1246 written_size += SUM_JOURNAL_SIZE;
1248 /* Step 2: write sit cache */
1249 seg_i = CURSEG_I(sbi, CURSEG_COLD_DATA);
1250 memcpy(kaddr + written_size, &seg_i->sum_blk->n_sits,
1252 written_size += SUM_JOURNAL_SIZE;
1254 /* Step 3: write summary entries */
1255 for (i = CURSEG_HOT_DATA; i <= CURSEG_COLD_DATA; i++) {
1256 unsigned short blkoff;
1257 seg_i = CURSEG_I(sbi, i);
1258 if (sbi->ckpt->alloc_type[i] == SSR)
1259 blkoff = sbi->blocks_per_seg;
1261 blkoff = curseg_blkoff(sbi, i);
1263 for (j = 0; j < blkoff; j++) {
1265 page = grab_meta_page(sbi, blkaddr++);
1266 kaddr = (unsigned char *)page_address(page);
1269 summary = (struct f2fs_summary *)(kaddr + written_size);
1270 *summary = seg_i->sum_blk->entries[j];
1271 written_size += SUMMARY_SIZE;
1273 if (written_size + SUMMARY_SIZE <= PAGE_CACHE_SIZE -
1277 set_page_dirty(page);
1278 f2fs_put_page(page, 1);
1283 set_page_dirty(page);
1284 f2fs_put_page(page, 1);
1288 static void write_normal_summaries(struct f2fs_sb_info *sbi,
1289 block_t blkaddr, int type)
1292 if (IS_DATASEG(type))
1293 end = type + NR_CURSEG_DATA_TYPE;
1295 end = type + NR_CURSEG_NODE_TYPE;
1297 for (i = type; i < end; i++) {
1298 struct curseg_info *sum = CURSEG_I(sbi, i);
1299 mutex_lock(&sum->curseg_mutex);
1300 write_sum_page(sbi, sum->sum_blk, blkaddr + (i - type));
1301 mutex_unlock(&sum->curseg_mutex);
1305 void write_data_summaries(struct f2fs_sb_info *sbi, block_t start_blk)
1307 if (is_set_ckpt_flags(F2FS_CKPT(sbi), CP_COMPACT_SUM_FLAG))
1308 write_compacted_summaries(sbi, start_blk);
1310 write_normal_summaries(sbi, start_blk, CURSEG_HOT_DATA);
1313 void write_node_summaries(struct f2fs_sb_info *sbi, block_t start_blk)
1315 if (is_set_ckpt_flags(F2FS_CKPT(sbi), CP_UMOUNT_FLAG))
1316 write_normal_summaries(sbi, start_blk, CURSEG_HOT_NODE);
1319 int lookup_journal_in_cursum(struct f2fs_summary_block *sum, int type,
1320 unsigned int val, int alloc)
1324 if (type == NAT_JOURNAL) {
1325 for (i = 0; i < nats_in_cursum(sum); i++) {
1326 if (le32_to_cpu(nid_in_journal(sum, i)) == val)
1329 if (alloc && nats_in_cursum(sum) < NAT_JOURNAL_ENTRIES)
1330 return update_nats_in_cursum(sum, 1);
1331 } else if (type == SIT_JOURNAL) {
1332 for (i = 0; i < sits_in_cursum(sum); i++)
1333 if (le32_to_cpu(segno_in_journal(sum, i)) == val)
1335 if (alloc && sits_in_cursum(sum) < SIT_JOURNAL_ENTRIES)
1336 return update_sits_in_cursum(sum, 1);
1341 static struct page *get_current_sit_page(struct f2fs_sb_info *sbi,
1344 struct sit_info *sit_i = SIT_I(sbi);
1345 unsigned int offset = SIT_BLOCK_OFFSET(sit_i, segno);
1346 block_t blk_addr = sit_i->sit_base_addr + offset;
1348 check_seg_range(sbi, segno);
1350 /* calculate sit block address */
1351 if (f2fs_test_bit(offset, sit_i->sit_bitmap))
1352 blk_addr += sit_i->sit_blocks;
1354 return get_meta_page(sbi, blk_addr);
1357 static struct page *get_next_sit_page(struct f2fs_sb_info *sbi,
1360 struct sit_info *sit_i = SIT_I(sbi);
1361 struct page *src_page, *dst_page;
1362 pgoff_t src_off, dst_off;
1363 void *src_addr, *dst_addr;
1365 src_off = current_sit_addr(sbi, start);
1366 dst_off = next_sit_addr(sbi, src_off);
1368 /* get current sit block page without lock */
1369 src_page = get_meta_page(sbi, src_off);
1370 dst_page = grab_meta_page(sbi, dst_off);
1371 f2fs_bug_on(PageDirty(src_page));
1373 src_addr = page_address(src_page);
1374 dst_addr = page_address(dst_page);
1375 memcpy(dst_addr, src_addr, PAGE_CACHE_SIZE);
1377 set_page_dirty(dst_page);
1378 f2fs_put_page(src_page, 1);
1380 set_to_next_sit(sit_i, start);
1385 static bool flush_sits_in_journal(struct f2fs_sb_info *sbi)
1387 struct curseg_info *curseg = CURSEG_I(sbi, CURSEG_COLD_DATA);
1388 struct f2fs_summary_block *sum = curseg->sum_blk;
1392 * If the journal area in the current summary is full of sit entries,
1393 * all the sit entries will be flushed. Otherwise the sit entries
1394 * are not able to replace with newly hot sit entries.
1396 if (sits_in_cursum(sum) >= SIT_JOURNAL_ENTRIES) {
1397 for (i = sits_in_cursum(sum) - 1; i >= 0; i--) {
1399 segno = le32_to_cpu(segno_in_journal(sum, i));
1400 __mark_sit_entry_dirty(sbi, segno);
1402 update_sits_in_cursum(sum, -sits_in_cursum(sum));
1409 * CP calls this function, which flushes SIT entries including sit_journal,
1410 * and moves prefree segs to free segs.
1412 void flush_sit_entries(struct f2fs_sb_info *sbi)
1414 struct sit_info *sit_i = SIT_I(sbi);
1415 unsigned long *bitmap = sit_i->dirty_sentries_bitmap;
1416 struct curseg_info *curseg = CURSEG_I(sbi, CURSEG_COLD_DATA);
1417 struct f2fs_summary_block *sum = curseg->sum_blk;
1418 unsigned long nsegs = TOTAL_SEGS(sbi);
1419 struct page *page = NULL;
1420 struct f2fs_sit_block *raw_sit = NULL;
1421 unsigned int start = 0, end = 0;
1422 unsigned int segno = -1;
1425 mutex_lock(&curseg->curseg_mutex);
1426 mutex_lock(&sit_i->sentry_lock);
1429 * "flushed" indicates whether sit entries in journal are flushed
1430 * to the SIT area or not.
1432 flushed = flush_sits_in_journal(sbi);
1434 while ((segno = find_next_bit(bitmap, nsegs, segno + 1)) < nsegs) {
1435 struct seg_entry *se = get_seg_entry(sbi, segno);
1436 int sit_offset, offset;
1438 sit_offset = SIT_ENTRY_OFFSET(sit_i, segno);
1440 /* add discard candidates */
1441 if (SM_I(sbi)->nr_discards < SM_I(sbi)->max_discards)
1442 add_discard_addrs(sbi, segno, se);
1447 offset = lookup_journal_in_cursum(sum, SIT_JOURNAL, segno, 1);
1449 segno_in_journal(sum, offset) = cpu_to_le32(segno);
1450 seg_info_to_raw_sit(se, &sit_in_journal(sum, offset));
1454 if (!page || (start > segno) || (segno > end)) {
1456 f2fs_put_page(page, 1);
1460 start = START_SEGNO(sit_i, segno);
1461 end = start + SIT_ENTRY_PER_BLOCK - 1;
1463 /* read sit block that will be updated */
1464 page = get_next_sit_page(sbi, start);
1465 raw_sit = page_address(page);
1468 /* udpate entry in SIT block */
1469 seg_info_to_raw_sit(se, &raw_sit->entries[sit_offset]);
1471 __clear_bit(segno, bitmap);
1472 sit_i->dirty_sentries--;
1474 mutex_unlock(&sit_i->sentry_lock);
1475 mutex_unlock(&curseg->curseg_mutex);
1477 /* writeout last modified SIT block */
1478 f2fs_put_page(page, 1);
1480 set_prefree_as_free_segments(sbi);
1483 static int build_sit_info(struct f2fs_sb_info *sbi)
1485 struct f2fs_super_block *raw_super = F2FS_RAW_SUPER(sbi);
1486 struct f2fs_checkpoint *ckpt = F2FS_CKPT(sbi);
1487 struct sit_info *sit_i;
1488 unsigned int sit_segs, start;
1489 char *src_bitmap, *dst_bitmap;
1490 unsigned int bitmap_size;
1492 /* allocate memory for SIT information */
1493 sit_i = kzalloc(sizeof(struct sit_info), GFP_KERNEL);
1497 SM_I(sbi)->sit_info = sit_i;
1499 sit_i->sentries = vzalloc(TOTAL_SEGS(sbi) * sizeof(struct seg_entry));
1500 if (!sit_i->sentries)
1503 bitmap_size = f2fs_bitmap_size(TOTAL_SEGS(sbi));
1504 sit_i->dirty_sentries_bitmap = kzalloc(bitmap_size, GFP_KERNEL);
1505 if (!sit_i->dirty_sentries_bitmap)
1508 for (start = 0; start < TOTAL_SEGS(sbi); start++) {
1509 sit_i->sentries[start].cur_valid_map
1510 = kzalloc(SIT_VBLOCK_MAP_SIZE, GFP_KERNEL);
1511 sit_i->sentries[start].ckpt_valid_map
1512 = kzalloc(SIT_VBLOCK_MAP_SIZE, GFP_KERNEL);
1513 if (!sit_i->sentries[start].cur_valid_map
1514 || !sit_i->sentries[start].ckpt_valid_map)
1518 if (sbi->segs_per_sec > 1) {
1519 sit_i->sec_entries = vzalloc(TOTAL_SECS(sbi) *
1520 sizeof(struct sec_entry));
1521 if (!sit_i->sec_entries)
1525 /* get information related with SIT */
1526 sit_segs = le32_to_cpu(raw_super->segment_count_sit) >> 1;
1528 /* setup SIT bitmap from ckeckpoint pack */
1529 bitmap_size = __bitmap_size(sbi, SIT_BITMAP);
1530 src_bitmap = __bitmap_ptr(sbi, SIT_BITMAP);
1532 dst_bitmap = kmemdup(src_bitmap, bitmap_size, GFP_KERNEL);
1536 /* init SIT information */
1537 sit_i->s_ops = &default_salloc_ops;
1539 sit_i->sit_base_addr = le32_to_cpu(raw_super->sit_blkaddr);
1540 sit_i->sit_blocks = sit_segs << sbi->log_blocks_per_seg;
1541 sit_i->written_valid_blocks = le64_to_cpu(ckpt->valid_block_count);
1542 sit_i->sit_bitmap = dst_bitmap;
1543 sit_i->bitmap_size = bitmap_size;
1544 sit_i->dirty_sentries = 0;
1545 sit_i->sents_per_block = SIT_ENTRY_PER_BLOCK;
1546 sit_i->elapsed_time = le64_to_cpu(sbi->ckpt->elapsed_time);
1547 sit_i->mounted_time = CURRENT_TIME_SEC.tv_sec;
1548 mutex_init(&sit_i->sentry_lock);
1552 static int build_free_segmap(struct f2fs_sb_info *sbi)
1554 struct f2fs_sm_info *sm_info = SM_I(sbi);
1555 struct free_segmap_info *free_i;
1556 unsigned int bitmap_size, sec_bitmap_size;
1558 /* allocate memory for free segmap information */
1559 free_i = kzalloc(sizeof(struct free_segmap_info), GFP_KERNEL);
1563 SM_I(sbi)->free_info = free_i;
1565 bitmap_size = f2fs_bitmap_size(TOTAL_SEGS(sbi));
1566 free_i->free_segmap = kmalloc(bitmap_size, GFP_KERNEL);
1567 if (!free_i->free_segmap)
1570 sec_bitmap_size = f2fs_bitmap_size(TOTAL_SECS(sbi));
1571 free_i->free_secmap = kmalloc(sec_bitmap_size, GFP_KERNEL);
1572 if (!free_i->free_secmap)
1575 /* set all segments as dirty temporarily */
1576 memset(free_i->free_segmap, 0xff, bitmap_size);
1577 memset(free_i->free_secmap, 0xff, sec_bitmap_size);
1579 /* init free segmap information */
1580 free_i->start_segno =
1581 (unsigned int) GET_SEGNO_FROM_SEG0(sbi, sm_info->main_blkaddr);
1582 free_i->free_segments = 0;
1583 free_i->free_sections = 0;
1584 rwlock_init(&free_i->segmap_lock);
1588 static int build_curseg(struct f2fs_sb_info *sbi)
1590 struct curseg_info *array;
1593 array = kzalloc(sizeof(*array) * NR_CURSEG_TYPE, GFP_KERNEL);
1597 SM_I(sbi)->curseg_array = array;
1599 for (i = 0; i < NR_CURSEG_TYPE; i++) {
1600 mutex_init(&array[i].curseg_mutex);
1601 array[i].sum_blk = kzalloc(PAGE_CACHE_SIZE, GFP_KERNEL);
1602 if (!array[i].sum_blk)
1604 array[i].segno = NULL_SEGNO;
1605 array[i].next_blkoff = 0;
1607 return restore_curseg_summaries(sbi);
1610 static void build_sit_entries(struct f2fs_sb_info *sbi)
1612 struct sit_info *sit_i = SIT_I(sbi);
1613 struct curseg_info *curseg = CURSEG_I(sbi, CURSEG_COLD_DATA);
1614 struct f2fs_summary_block *sum = curseg->sum_blk;
1615 int sit_blk_cnt = SIT_BLK_CNT(sbi);
1616 unsigned int i, start, end;
1617 unsigned int readed, start_blk = 0;
1618 int nrpages = MAX_BIO_BLOCKS(max_hw_blocks(sbi));
1621 readed = ra_meta_pages(sbi, start_blk, nrpages, META_SIT);
1623 start = start_blk * sit_i->sents_per_block;
1624 end = (start_blk + readed) * sit_i->sents_per_block;
1626 for (; start < end && start < TOTAL_SEGS(sbi); start++) {
1627 struct seg_entry *se = &sit_i->sentries[start];
1628 struct f2fs_sit_block *sit_blk;
1629 struct f2fs_sit_entry sit;
1632 mutex_lock(&curseg->curseg_mutex);
1633 for (i = 0; i < sits_in_cursum(sum); i++) {
1634 if (le32_to_cpu(segno_in_journal(sum, i))
1636 sit = sit_in_journal(sum, i);
1637 mutex_unlock(&curseg->curseg_mutex);
1641 mutex_unlock(&curseg->curseg_mutex);
1643 page = get_current_sit_page(sbi, start);
1644 sit_blk = (struct f2fs_sit_block *)page_address(page);
1645 sit = sit_blk->entries[SIT_ENTRY_OFFSET(sit_i, start)];
1646 f2fs_put_page(page, 1);
1648 check_block_count(sbi, start, &sit);
1649 seg_info_from_raw_sit(se, &sit);
1650 if (sbi->segs_per_sec > 1) {
1651 struct sec_entry *e = get_sec_entry(sbi, start);
1652 e->valid_blocks += se->valid_blocks;
1655 start_blk += readed;
1656 } while (start_blk < sit_blk_cnt);
1659 static void init_free_segmap(struct f2fs_sb_info *sbi)
1664 for (start = 0; start < TOTAL_SEGS(sbi); start++) {
1665 struct seg_entry *sentry = get_seg_entry(sbi, start);
1666 if (!sentry->valid_blocks)
1667 __set_free(sbi, start);
1670 /* set use the current segments */
1671 for (type = CURSEG_HOT_DATA; type <= CURSEG_COLD_NODE; type++) {
1672 struct curseg_info *curseg_t = CURSEG_I(sbi, type);
1673 __set_test_and_inuse(sbi, curseg_t->segno);
1677 static void init_dirty_segmap(struct f2fs_sb_info *sbi)
1679 struct dirty_seglist_info *dirty_i = DIRTY_I(sbi);
1680 struct free_segmap_info *free_i = FREE_I(sbi);
1681 unsigned int segno = 0, offset = 0, total_segs = TOTAL_SEGS(sbi);
1682 unsigned short valid_blocks;
1685 /* find dirty segment based on free segmap */
1686 segno = find_next_inuse(free_i, total_segs, offset);
1687 if (segno >= total_segs)
1690 valid_blocks = get_valid_blocks(sbi, segno, 0);
1691 if (valid_blocks >= sbi->blocks_per_seg || !valid_blocks)
1693 mutex_lock(&dirty_i->seglist_lock);
1694 __locate_dirty_segment(sbi, segno, DIRTY);
1695 mutex_unlock(&dirty_i->seglist_lock);
1699 static int init_victim_secmap(struct f2fs_sb_info *sbi)
1701 struct dirty_seglist_info *dirty_i = DIRTY_I(sbi);
1702 unsigned int bitmap_size = f2fs_bitmap_size(TOTAL_SECS(sbi));
1704 dirty_i->victim_secmap = kzalloc(bitmap_size, GFP_KERNEL);
1705 if (!dirty_i->victim_secmap)
1710 static int build_dirty_segmap(struct f2fs_sb_info *sbi)
1712 struct dirty_seglist_info *dirty_i;
1713 unsigned int bitmap_size, i;
1715 /* allocate memory for dirty segments list information */
1716 dirty_i = kzalloc(sizeof(struct dirty_seglist_info), GFP_KERNEL);
1720 SM_I(sbi)->dirty_info = dirty_i;
1721 mutex_init(&dirty_i->seglist_lock);
1723 bitmap_size = f2fs_bitmap_size(TOTAL_SEGS(sbi));
1725 for (i = 0; i < NR_DIRTY_TYPE; i++) {
1726 dirty_i->dirty_segmap[i] = kzalloc(bitmap_size, GFP_KERNEL);
1727 if (!dirty_i->dirty_segmap[i])
1731 init_dirty_segmap(sbi);
1732 return init_victim_secmap(sbi);
1736 * Update min, max modified time for cost-benefit GC algorithm
1738 static void init_min_max_mtime(struct f2fs_sb_info *sbi)
1740 struct sit_info *sit_i = SIT_I(sbi);
1743 mutex_lock(&sit_i->sentry_lock);
1745 sit_i->min_mtime = LLONG_MAX;
1747 for (segno = 0; segno < TOTAL_SEGS(sbi); segno += sbi->segs_per_sec) {
1749 unsigned long long mtime = 0;
1751 for (i = 0; i < sbi->segs_per_sec; i++)
1752 mtime += get_seg_entry(sbi, segno + i)->mtime;
1754 mtime = div_u64(mtime, sbi->segs_per_sec);
1756 if (sit_i->min_mtime > mtime)
1757 sit_i->min_mtime = mtime;
1759 sit_i->max_mtime = get_mtime(sbi);
1760 mutex_unlock(&sit_i->sentry_lock);
1763 int build_segment_manager(struct f2fs_sb_info *sbi)
1765 struct f2fs_super_block *raw_super = F2FS_RAW_SUPER(sbi);
1766 struct f2fs_checkpoint *ckpt = F2FS_CKPT(sbi);
1767 struct f2fs_sm_info *sm_info;
1770 sm_info = kzalloc(sizeof(struct f2fs_sm_info), GFP_KERNEL);
1775 sbi->sm_info = sm_info;
1776 INIT_LIST_HEAD(&sm_info->wblist_head);
1777 spin_lock_init(&sm_info->wblist_lock);
1778 sm_info->seg0_blkaddr = le32_to_cpu(raw_super->segment0_blkaddr);
1779 sm_info->main_blkaddr = le32_to_cpu(raw_super->main_blkaddr);
1780 sm_info->segment_count = le32_to_cpu(raw_super->segment_count);
1781 sm_info->reserved_segments = le32_to_cpu(ckpt->rsvd_segment_count);
1782 sm_info->ovp_segments = le32_to_cpu(ckpt->overprov_segment_count);
1783 sm_info->main_segments = le32_to_cpu(raw_super->segment_count_main);
1784 sm_info->ssa_blkaddr = le32_to_cpu(raw_super->ssa_blkaddr);
1785 sm_info->rec_prefree_segments = sm_info->main_segments *
1786 DEF_RECLAIM_PREFREE_SEGMENTS / 100;
1787 sm_info->ipu_policy = F2FS_IPU_DISABLE;
1788 sm_info->min_ipu_util = DEF_MIN_IPU_UTIL;
1790 INIT_LIST_HEAD(&sm_info->discard_list);
1791 sm_info->nr_discards = 0;
1792 sm_info->max_discards = 0;
1794 err = build_sit_info(sbi);
1797 err = build_free_segmap(sbi);
1800 err = build_curseg(sbi);
1804 /* reinit free segmap based on SIT */
1805 build_sit_entries(sbi);
1807 init_free_segmap(sbi);
1808 err = build_dirty_segmap(sbi);
1812 init_min_max_mtime(sbi);
1816 static void discard_dirty_segmap(struct f2fs_sb_info *sbi,
1817 enum dirty_type dirty_type)
1819 struct dirty_seglist_info *dirty_i = DIRTY_I(sbi);
1821 mutex_lock(&dirty_i->seglist_lock);
1822 kfree(dirty_i->dirty_segmap[dirty_type]);
1823 dirty_i->nr_dirty[dirty_type] = 0;
1824 mutex_unlock(&dirty_i->seglist_lock);
1827 static void destroy_victim_secmap(struct f2fs_sb_info *sbi)
1829 struct dirty_seglist_info *dirty_i = DIRTY_I(sbi);
1830 kfree(dirty_i->victim_secmap);
1833 static void destroy_dirty_segmap(struct f2fs_sb_info *sbi)
1835 struct dirty_seglist_info *dirty_i = DIRTY_I(sbi);
1841 /* discard pre-free/dirty segments list */
1842 for (i = 0; i < NR_DIRTY_TYPE; i++)
1843 discard_dirty_segmap(sbi, i);
1845 destroy_victim_secmap(sbi);
1846 SM_I(sbi)->dirty_info = NULL;
1850 static void destroy_curseg(struct f2fs_sb_info *sbi)
1852 struct curseg_info *array = SM_I(sbi)->curseg_array;
1857 SM_I(sbi)->curseg_array = NULL;
1858 for (i = 0; i < NR_CURSEG_TYPE; i++)
1859 kfree(array[i].sum_blk);
1863 static void destroy_free_segmap(struct f2fs_sb_info *sbi)
1865 struct free_segmap_info *free_i = SM_I(sbi)->free_info;
1868 SM_I(sbi)->free_info = NULL;
1869 kfree(free_i->free_segmap);
1870 kfree(free_i->free_secmap);
1874 static void destroy_sit_info(struct f2fs_sb_info *sbi)
1876 struct sit_info *sit_i = SIT_I(sbi);
1882 if (sit_i->sentries) {
1883 for (start = 0; start < TOTAL_SEGS(sbi); start++) {
1884 kfree(sit_i->sentries[start].cur_valid_map);
1885 kfree(sit_i->sentries[start].ckpt_valid_map);
1888 vfree(sit_i->sentries);
1889 vfree(sit_i->sec_entries);
1890 kfree(sit_i->dirty_sentries_bitmap);
1892 SM_I(sbi)->sit_info = NULL;
1893 kfree(sit_i->sit_bitmap);
1897 void destroy_segment_manager(struct f2fs_sb_info *sbi)
1899 struct f2fs_sm_info *sm_info = SM_I(sbi);
1902 destroy_dirty_segmap(sbi);
1903 destroy_curseg(sbi);
1904 destroy_free_segmap(sbi);
1905 destroy_sit_info(sbi);
1906 sbi->sm_info = NULL;
1910 int __init create_segment_manager_caches(void)
1912 discard_entry_slab = f2fs_kmem_cache_create("discard_entry",
1913 sizeof(struct discard_entry));
1914 if (!discard_entry_slab)
1919 void destroy_segment_manager_caches(void)
1921 kmem_cache_destroy(discard_entry_slab);