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 list_head *this, *next;
344 struct discard_entry *entry;
345 struct dirty_seglist_info *dirty_i = DIRTY_I(sbi);
346 unsigned long *prefree_map = dirty_i->dirty_segmap[PRE];
347 unsigned int total_segs = TOTAL_SEGS(sbi);
348 unsigned int start = 0, end = -1;
350 mutex_lock(&dirty_i->seglist_lock);
354 start = find_next_bit(prefree_map, total_segs, end + 1);
355 if (start >= total_segs)
357 end = find_next_zero_bit(prefree_map, total_segs, start + 1);
359 for (i = start; i < end; i++)
360 clear_bit(i, prefree_map);
362 dirty_i->nr_dirty[PRE] -= end - start;
364 if (!test_opt(sbi, DISCARD))
367 f2fs_issue_discard(sbi, START_BLOCK(sbi, start),
368 (end - start) << sbi->log_blocks_per_seg);
370 mutex_unlock(&dirty_i->seglist_lock);
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);
382 static void __mark_sit_entry_dirty(struct f2fs_sb_info *sbi, unsigned int segno)
384 struct sit_info *sit_i = SIT_I(sbi);
385 if (!__test_and_set_bit(segno, sit_i->dirty_sentries_bitmap))
386 sit_i->dirty_sentries++;
389 static void __set_sit_entry_type(struct f2fs_sb_info *sbi, int type,
390 unsigned int segno, int modified)
392 struct seg_entry *se = get_seg_entry(sbi, segno);
395 __mark_sit_entry_dirty(sbi, segno);
398 static void update_sit_entry(struct f2fs_sb_info *sbi, block_t blkaddr, int del)
400 struct seg_entry *se;
401 unsigned int segno, offset;
402 long int new_vblocks;
404 segno = GET_SEGNO(sbi, blkaddr);
406 se = get_seg_entry(sbi, segno);
407 new_vblocks = se->valid_blocks + del;
408 offset = GET_BLKOFF_FROM_SEG0(sbi, blkaddr);
410 f2fs_bug_on((new_vblocks >> (sizeof(unsigned short) << 3) ||
411 (new_vblocks > sbi->blocks_per_seg)));
413 se->valid_blocks = new_vblocks;
414 se->mtime = get_mtime(sbi);
415 SIT_I(sbi)->max_mtime = se->mtime;
417 /* Update valid block bitmap */
419 if (f2fs_set_bit(offset, se->cur_valid_map))
422 if (!f2fs_clear_bit(offset, se->cur_valid_map))
425 if (!f2fs_test_bit(offset, se->ckpt_valid_map))
426 se->ckpt_valid_blocks += del;
428 __mark_sit_entry_dirty(sbi, segno);
430 /* update total number of valid blocks to be written in ckpt area */
431 SIT_I(sbi)->written_valid_blocks += del;
433 if (sbi->segs_per_sec > 1)
434 get_sec_entry(sbi, segno)->valid_blocks += del;
437 void refresh_sit_entry(struct f2fs_sb_info *sbi, block_t old, block_t new)
439 update_sit_entry(sbi, new, 1);
440 if (GET_SEGNO(sbi, old) != NULL_SEGNO)
441 update_sit_entry(sbi, old, -1);
443 locate_dirty_segment(sbi, GET_SEGNO(sbi, old));
444 locate_dirty_segment(sbi, GET_SEGNO(sbi, new));
447 void invalidate_blocks(struct f2fs_sb_info *sbi, block_t addr)
449 unsigned int segno = GET_SEGNO(sbi, addr);
450 struct sit_info *sit_i = SIT_I(sbi);
452 f2fs_bug_on(addr == NULL_ADDR);
453 if (addr == NEW_ADDR)
456 /* add it into sit main buffer */
457 mutex_lock(&sit_i->sentry_lock);
459 update_sit_entry(sbi, addr, -1);
461 /* add it into dirty seglist */
462 locate_dirty_segment(sbi, segno);
464 mutex_unlock(&sit_i->sentry_lock);
468 * This function should be resided under the curseg_mutex lock
470 static void __add_sum_entry(struct f2fs_sb_info *sbi, int type,
471 struct f2fs_summary *sum)
473 struct curseg_info *curseg = CURSEG_I(sbi, type);
474 void *addr = curseg->sum_blk;
475 addr += curseg->next_blkoff * sizeof(struct f2fs_summary);
476 memcpy(addr, sum, sizeof(struct f2fs_summary));
480 * Calculate the number of current summary pages for writing
482 int npages_for_summary_flush(struct f2fs_sb_info *sbi)
484 int valid_sum_count = 0;
487 for (i = CURSEG_HOT_DATA; i <= CURSEG_COLD_DATA; i++) {
488 if (sbi->ckpt->alloc_type[i] == SSR)
489 valid_sum_count += sbi->blocks_per_seg;
491 valid_sum_count += curseg_blkoff(sbi, i);
494 sum_in_page = (PAGE_CACHE_SIZE - 2 * SUM_JOURNAL_SIZE -
495 SUM_FOOTER_SIZE) / SUMMARY_SIZE;
496 if (valid_sum_count <= sum_in_page)
498 else if ((valid_sum_count - sum_in_page) <=
499 (PAGE_CACHE_SIZE - SUM_FOOTER_SIZE) / SUMMARY_SIZE)
505 * Caller should put this summary page
507 struct page *get_sum_page(struct f2fs_sb_info *sbi, unsigned int segno)
509 return get_meta_page(sbi, GET_SUM_BLOCK(sbi, segno));
512 static void write_sum_page(struct f2fs_sb_info *sbi,
513 struct f2fs_summary_block *sum_blk, block_t blk_addr)
515 struct page *page = grab_meta_page(sbi, blk_addr);
516 void *kaddr = page_address(page);
517 memcpy(kaddr, sum_blk, PAGE_CACHE_SIZE);
518 set_page_dirty(page);
519 f2fs_put_page(page, 1);
522 static int is_next_segment_free(struct f2fs_sb_info *sbi, int type)
524 struct curseg_info *curseg = CURSEG_I(sbi, type);
525 unsigned int segno = curseg->segno + 1;
526 struct free_segmap_info *free_i = FREE_I(sbi);
528 if (segno < TOTAL_SEGS(sbi) && segno % sbi->segs_per_sec)
529 return !test_bit(segno, free_i->free_segmap);
534 * Find a new segment from the free segments bitmap to right order
535 * This function should be returned with success, otherwise BUG
537 static void get_new_segment(struct f2fs_sb_info *sbi,
538 unsigned int *newseg, bool new_sec, int dir)
540 struct free_segmap_info *free_i = FREE_I(sbi);
541 unsigned int segno, secno, zoneno;
542 unsigned int total_zones = TOTAL_SECS(sbi) / sbi->secs_per_zone;
543 unsigned int hint = *newseg / sbi->segs_per_sec;
544 unsigned int old_zoneno = GET_ZONENO_FROM_SEGNO(sbi, *newseg);
545 unsigned int left_start = hint;
550 write_lock(&free_i->segmap_lock);
552 if (!new_sec && ((*newseg + 1) % sbi->segs_per_sec)) {
553 segno = find_next_zero_bit(free_i->free_segmap,
554 TOTAL_SEGS(sbi), *newseg + 1);
555 if (segno - *newseg < sbi->segs_per_sec -
556 (*newseg % sbi->segs_per_sec))
560 secno = find_next_zero_bit(free_i->free_secmap, TOTAL_SECS(sbi), hint);
561 if (secno >= TOTAL_SECS(sbi)) {
562 if (dir == ALLOC_RIGHT) {
563 secno = find_next_zero_bit(free_i->free_secmap,
565 f2fs_bug_on(secno >= TOTAL_SECS(sbi));
568 left_start = hint - 1;
574 while (test_bit(left_start, free_i->free_secmap)) {
575 if (left_start > 0) {
579 left_start = find_next_zero_bit(free_i->free_secmap,
581 f2fs_bug_on(left_start >= TOTAL_SECS(sbi));
587 segno = secno * sbi->segs_per_sec;
588 zoneno = secno / sbi->secs_per_zone;
590 /* give up on finding another zone */
593 if (sbi->secs_per_zone == 1)
595 if (zoneno == old_zoneno)
597 if (dir == ALLOC_LEFT) {
598 if (!go_left && zoneno + 1 >= total_zones)
600 if (go_left && zoneno == 0)
603 for (i = 0; i < NR_CURSEG_TYPE; i++)
604 if (CURSEG_I(sbi, i)->zone == zoneno)
607 if (i < NR_CURSEG_TYPE) {
608 /* zone is in user, try another */
610 hint = zoneno * sbi->secs_per_zone - 1;
611 else if (zoneno + 1 >= total_zones)
614 hint = (zoneno + 1) * sbi->secs_per_zone;
616 goto find_other_zone;
619 /* set it as dirty segment in free segmap */
620 f2fs_bug_on(test_bit(segno, free_i->free_segmap));
621 __set_inuse(sbi, segno);
623 write_unlock(&free_i->segmap_lock);
626 static void reset_curseg(struct f2fs_sb_info *sbi, int type, int modified)
628 struct curseg_info *curseg = CURSEG_I(sbi, type);
629 struct summary_footer *sum_footer;
631 curseg->segno = curseg->next_segno;
632 curseg->zone = GET_ZONENO_FROM_SEGNO(sbi, curseg->segno);
633 curseg->next_blkoff = 0;
634 curseg->next_segno = NULL_SEGNO;
636 sum_footer = &(curseg->sum_blk->footer);
637 memset(sum_footer, 0, sizeof(struct summary_footer));
638 if (IS_DATASEG(type))
639 SET_SUM_TYPE(sum_footer, SUM_TYPE_DATA);
640 if (IS_NODESEG(type))
641 SET_SUM_TYPE(sum_footer, SUM_TYPE_NODE);
642 __set_sit_entry_type(sbi, type, curseg->segno, modified);
646 * Allocate a current working segment.
647 * This function always allocates a free segment in LFS manner.
649 static void new_curseg(struct f2fs_sb_info *sbi, int type, bool new_sec)
651 struct curseg_info *curseg = CURSEG_I(sbi, type);
652 unsigned int segno = curseg->segno;
653 int dir = ALLOC_LEFT;
655 write_sum_page(sbi, curseg->sum_blk,
656 GET_SUM_BLOCK(sbi, segno));
657 if (type == CURSEG_WARM_DATA || type == CURSEG_COLD_DATA)
660 if (test_opt(sbi, NOHEAP))
663 get_new_segment(sbi, &segno, new_sec, dir);
664 curseg->next_segno = segno;
665 reset_curseg(sbi, type, 1);
666 curseg->alloc_type = LFS;
669 static void __next_free_blkoff(struct f2fs_sb_info *sbi,
670 struct curseg_info *seg, block_t start)
672 struct seg_entry *se = get_seg_entry(sbi, seg->segno);
673 int entries = SIT_VBLOCK_MAP_SIZE / sizeof(unsigned long);
674 unsigned long target_map[entries];
675 unsigned long *ckpt_map = (unsigned long *)se->ckpt_valid_map;
676 unsigned long *cur_map = (unsigned long *)se->cur_valid_map;
679 for (i = 0; i < entries; i++)
680 target_map[i] = ckpt_map[i] | cur_map[i];
682 pos = __find_rev_next_zero_bit(target_map, sbi->blocks_per_seg, start);
684 seg->next_blkoff = pos;
688 * If a segment is written by LFS manner, next block offset is just obtained
689 * by increasing the current block offset. However, if a segment is written by
690 * SSR manner, next block offset obtained by calling __next_free_blkoff
692 static void __refresh_next_blkoff(struct f2fs_sb_info *sbi,
693 struct curseg_info *seg)
695 if (seg->alloc_type == SSR)
696 __next_free_blkoff(sbi, seg, seg->next_blkoff + 1);
702 * This function always allocates a used segment (from dirty seglist) by SSR
703 * manner, so it should recover the existing segment information of valid blocks
705 static void change_curseg(struct f2fs_sb_info *sbi, int type, bool reuse)
707 struct dirty_seglist_info *dirty_i = DIRTY_I(sbi);
708 struct curseg_info *curseg = CURSEG_I(sbi, type);
709 unsigned int new_segno = curseg->next_segno;
710 struct f2fs_summary_block *sum_node;
711 struct page *sum_page;
713 write_sum_page(sbi, curseg->sum_blk,
714 GET_SUM_BLOCK(sbi, curseg->segno));
715 __set_test_and_inuse(sbi, new_segno);
717 mutex_lock(&dirty_i->seglist_lock);
718 __remove_dirty_segment(sbi, new_segno, PRE);
719 __remove_dirty_segment(sbi, new_segno, DIRTY);
720 mutex_unlock(&dirty_i->seglist_lock);
722 reset_curseg(sbi, type, 1);
723 curseg->alloc_type = SSR;
724 __next_free_blkoff(sbi, curseg, 0);
727 sum_page = get_sum_page(sbi, new_segno);
728 sum_node = (struct f2fs_summary_block *)page_address(sum_page);
729 memcpy(curseg->sum_blk, sum_node, SUM_ENTRY_SIZE);
730 f2fs_put_page(sum_page, 1);
734 static int get_ssr_segment(struct f2fs_sb_info *sbi, int type)
736 struct curseg_info *curseg = CURSEG_I(sbi, type);
737 const struct victim_selection *v_ops = DIRTY_I(sbi)->v_ops;
739 if (IS_NODESEG(type) || !has_not_enough_free_secs(sbi, 0))
740 return v_ops->get_victim(sbi,
741 &(curseg)->next_segno, BG_GC, type, SSR);
743 /* For data segments, let's do SSR more intensively */
744 for (; type >= CURSEG_HOT_DATA; type--)
745 if (v_ops->get_victim(sbi, &(curseg)->next_segno,
752 * flush out current segment and replace it with new segment
753 * This function should be returned with success, otherwise BUG
755 static void allocate_segment_by_default(struct f2fs_sb_info *sbi,
756 int type, bool force)
758 struct curseg_info *curseg = CURSEG_I(sbi, type);
761 new_curseg(sbi, type, true);
762 else if (type == CURSEG_WARM_NODE)
763 new_curseg(sbi, type, false);
764 else if (curseg->alloc_type == LFS && is_next_segment_free(sbi, type))
765 new_curseg(sbi, type, false);
766 else if (need_SSR(sbi) && get_ssr_segment(sbi, type))
767 change_curseg(sbi, type, true);
769 new_curseg(sbi, type, false);
771 stat_inc_seg_type(sbi, curseg);
774 void allocate_new_segments(struct f2fs_sb_info *sbi)
776 struct curseg_info *curseg;
777 unsigned int old_curseg;
780 for (i = CURSEG_HOT_DATA; i <= CURSEG_COLD_DATA; i++) {
781 curseg = CURSEG_I(sbi, i);
782 old_curseg = curseg->segno;
783 SIT_I(sbi)->s_ops->allocate_segment(sbi, i, true);
784 locate_dirty_segment(sbi, old_curseg);
788 static const struct segment_allocation default_salloc_ops = {
789 .allocate_segment = allocate_segment_by_default,
792 static bool __has_curseg_space(struct f2fs_sb_info *sbi, int type)
794 struct curseg_info *curseg = CURSEG_I(sbi, type);
795 if (curseg->next_blkoff < sbi->blocks_per_seg)
800 static int __get_segment_type_2(struct page *page, enum page_type p_type)
803 return CURSEG_HOT_DATA;
805 return CURSEG_HOT_NODE;
808 static int __get_segment_type_4(struct page *page, enum page_type p_type)
810 if (p_type == DATA) {
811 struct inode *inode = page->mapping->host;
813 if (S_ISDIR(inode->i_mode))
814 return CURSEG_HOT_DATA;
816 return CURSEG_COLD_DATA;
818 if (IS_DNODE(page) && !is_cold_node(page))
819 return CURSEG_HOT_NODE;
821 return CURSEG_COLD_NODE;
825 static int __get_segment_type_6(struct page *page, enum page_type p_type)
827 if (p_type == DATA) {
828 struct inode *inode = page->mapping->host;
830 if (S_ISDIR(inode->i_mode))
831 return CURSEG_HOT_DATA;
832 else if (is_cold_data(page) || file_is_cold(inode))
833 return CURSEG_COLD_DATA;
835 return CURSEG_WARM_DATA;
838 return is_cold_node(page) ? CURSEG_WARM_NODE :
841 return CURSEG_COLD_NODE;
845 static int __get_segment_type(struct page *page, enum page_type p_type)
847 struct f2fs_sb_info *sbi = F2FS_SB(page->mapping->host->i_sb);
848 switch (sbi->active_logs) {
850 return __get_segment_type_2(page, p_type);
852 return __get_segment_type_4(page, p_type);
854 /* NR_CURSEG_TYPE(6) logs by default */
855 f2fs_bug_on(sbi->active_logs != NR_CURSEG_TYPE);
856 return __get_segment_type_6(page, p_type);
859 void allocate_data_block(struct f2fs_sb_info *sbi, struct page *page,
860 block_t old_blkaddr, block_t *new_blkaddr,
861 struct f2fs_summary *sum, int type)
863 struct sit_info *sit_i = SIT_I(sbi);
864 struct curseg_info *curseg;
865 unsigned int old_cursegno;
867 curseg = CURSEG_I(sbi, type);
869 mutex_lock(&curseg->curseg_mutex);
871 *new_blkaddr = NEXT_FREE_BLKADDR(sbi, curseg);
872 old_cursegno = curseg->segno;
875 * __add_sum_entry should be resided under the curseg_mutex
876 * because, this function updates a summary entry in the
877 * current summary block.
879 __add_sum_entry(sbi, type, sum);
881 mutex_lock(&sit_i->sentry_lock);
882 __refresh_next_blkoff(sbi, curseg);
884 stat_inc_block_count(sbi, curseg);
886 if (!__has_curseg_space(sbi, type))
887 sit_i->s_ops->allocate_segment(sbi, type, false);
889 * SIT information should be updated before segment allocation,
890 * since SSR needs latest valid block information.
892 refresh_sit_entry(sbi, old_blkaddr, *new_blkaddr);
893 locate_dirty_segment(sbi, old_cursegno);
895 mutex_unlock(&sit_i->sentry_lock);
897 if (page && IS_NODESEG(type))
898 fill_node_footer_blkaddr(page, NEXT_FREE_BLKADDR(sbi, curseg));
900 mutex_unlock(&curseg->curseg_mutex);
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)
907 int type = __get_segment_type(page, fio->type);
909 allocate_data_block(sbi, page, old_blkaddr, new_blkaddr, sum, type);
911 /* writeout dirty page into bdev */
912 f2fs_submit_page_mbio(sbi, page, *new_blkaddr, fio);
915 void write_meta_page(struct f2fs_sb_info *sbi, struct page *page)
917 struct f2fs_io_info fio = {
919 .rw = WRITE_SYNC | REQ_META | REQ_PRIO
922 set_page_writeback(page);
923 f2fs_submit_page_mbio(sbi, page, page->index, &fio);
926 void write_node_page(struct f2fs_sb_info *sbi, struct page *page,
927 struct f2fs_io_info *fio,
928 unsigned int nid, block_t old_blkaddr, block_t *new_blkaddr)
930 struct f2fs_summary sum;
931 set_summary(&sum, nid, 0, 0);
932 do_write_page(sbi, page, old_blkaddr, new_blkaddr, &sum, fio);
935 void write_data_page(struct page *page, struct dnode_of_data *dn,
936 block_t *new_blkaddr, struct f2fs_io_info *fio)
938 struct f2fs_sb_info *sbi = F2FS_SB(dn->inode->i_sb);
939 struct f2fs_summary sum;
942 f2fs_bug_on(dn->data_blkaddr == NULL_ADDR);
943 get_node_info(sbi, dn->nid, &ni);
944 set_summary(&sum, dn->nid, dn->ofs_in_node, ni.version);
946 do_write_page(sbi, page, dn->data_blkaddr, new_blkaddr, &sum, fio);
949 void rewrite_data_page(struct page *page, block_t old_blkaddr,
950 struct f2fs_io_info *fio)
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);
957 void recover_data_page(struct f2fs_sb_info *sbi,
958 struct page *page, struct f2fs_summary *sum,
959 block_t old_blkaddr, block_t new_blkaddr)
961 struct sit_info *sit_i = SIT_I(sbi);
962 struct curseg_info *curseg;
963 unsigned int segno, old_cursegno;
964 struct seg_entry *se;
967 segno = GET_SEGNO(sbi, new_blkaddr);
968 se = get_seg_entry(sbi, segno);
971 if (se->valid_blocks == 0 && !IS_CURSEG(sbi, segno)) {
972 if (old_blkaddr == NULL_ADDR)
973 type = CURSEG_COLD_DATA;
975 type = CURSEG_WARM_DATA;
977 curseg = CURSEG_I(sbi, type);
979 mutex_lock(&curseg->curseg_mutex);
980 mutex_lock(&sit_i->sentry_lock);
982 old_cursegno = curseg->segno;
984 /* change the current segment */
985 if (segno != curseg->segno) {
986 curseg->next_segno = segno;
987 change_curseg(sbi, type, true);
990 curseg->next_blkoff = GET_BLKOFF_FROM_SEG0(sbi, new_blkaddr);
991 __add_sum_entry(sbi, type, sum);
993 refresh_sit_entry(sbi, old_blkaddr, new_blkaddr);
994 locate_dirty_segment(sbi, old_cursegno);
996 mutex_unlock(&sit_i->sentry_lock);
997 mutex_unlock(&curseg->curseg_mutex);
1000 void rewrite_node_page(struct f2fs_sb_info *sbi,
1001 struct page *page, struct f2fs_summary *sum,
1002 block_t old_blkaddr, block_t new_blkaddr)
1004 struct sit_info *sit_i = SIT_I(sbi);
1005 int type = CURSEG_WARM_NODE;
1006 struct curseg_info *curseg;
1007 unsigned int segno, old_cursegno;
1008 block_t next_blkaddr = next_blkaddr_of_node(page);
1009 unsigned int next_segno = GET_SEGNO(sbi, next_blkaddr);
1010 struct f2fs_io_info fio = {
1015 curseg = CURSEG_I(sbi, type);
1017 mutex_lock(&curseg->curseg_mutex);
1018 mutex_lock(&sit_i->sentry_lock);
1020 segno = GET_SEGNO(sbi, new_blkaddr);
1021 old_cursegno = curseg->segno;
1023 /* change the current segment */
1024 if (segno != curseg->segno) {
1025 curseg->next_segno = segno;
1026 change_curseg(sbi, type, true);
1028 curseg->next_blkoff = GET_BLKOFF_FROM_SEG0(sbi, new_blkaddr);
1029 __add_sum_entry(sbi, type, sum);
1031 /* change the current log to the next block addr in advance */
1032 if (next_segno != segno) {
1033 curseg->next_segno = next_segno;
1034 change_curseg(sbi, type, true);
1036 curseg->next_blkoff = GET_BLKOFF_FROM_SEG0(sbi, next_blkaddr);
1038 /* rewrite node page */
1039 set_page_writeback(page);
1040 f2fs_submit_page_mbio(sbi, page, new_blkaddr, &fio);
1041 f2fs_submit_merged_bio(sbi, NODE, WRITE);
1042 refresh_sit_entry(sbi, old_blkaddr, new_blkaddr);
1043 locate_dirty_segment(sbi, old_cursegno);
1045 mutex_unlock(&sit_i->sentry_lock);
1046 mutex_unlock(&curseg->curseg_mutex);
1049 static inline bool is_merged_page(struct f2fs_sb_info *sbi,
1050 struct page *page, enum page_type type)
1052 enum page_type btype = PAGE_TYPE_OF_BIO(type);
1053 struct f2fs_bio_info *io = &sbi->write_io[btype];
1054 struct bio *bio = io->bio;
1055 struct bio_vec *bvec;
1058 down_read(&io->io_rwsem);
1062 bio_for_each_segment_all(bvec, bio, i) {
1063 if (page == bvec->bv_page) {
1064 up_read(&io->io_rwsem);
1070 up_read(&io->io_rwsem);
1074 void f2fs_wait_on_page_writeback(struct page *page,
1075 enum page_type type)
1077 struct f2fs_sb_info *sbi = F2FS_SB(page->mapping->host->i_sb);
1078 if (PageWriteback(page)) {
1079 if (is_merged_page(sbi, page, type))
1080 f2fs_submit_merged_bio(sbi, type, WRITE);
1081 wait_on_page_writeback(page);
1085 static int read_compacted_summaries(struct f2fs_sb_info *sbi)
1087 struct f2fs_checkpoint *ckpt = F2FS_CKPT(sbi);
1088 struct curseg_info *seg_i;
1089 unsigned char *kaddr;
1094 start = start_sum_block(sbi);
1096 page = get_meta_page(sbi, start++);
1097 kaddr = (unsigned char *)page_address(page);
1099 /* Step 1: restore nat cache */
1100 seg_i = CURSEG_I(sbi, CURSEG_HOT_DATA);
1101 memcpy(&seg_i->sum_blk->n_nats, kaddr, SUM_JOURNAL_SIZE);
1103 /* Step 2: restore sit cache */
1104 seg_i = CURSEG_I(sbi, CURSEG_COLD_DATA);
1105 memcpy(&seg_i->sum_blk->n_sits, kaddr + SUM_JOURNAL_SIZE,
1107 offset = 2 * SUM_JOURNAL_SIZE;
1109 /* Step 3: restore summary entries */
1110 for (i = CURSEG_HOT_DATA; i <= CURSEG_COLD_DATA; i++) {
1111 unsigned short blk_off;
1114 seg_i = CURSEG_I(sbi, i);
1115 segno = le32_to_cpu(ckpt->cur_data_segno[i]);
1116 blk_off = le16_to_cpu(ckpt->cur_data_blkoff[i]);
1117 seg_i->next_segno = segno;
1118 reset_curseg(sbi, i, 0);
1119 seg_i->alloc_type = ckpt->alloc_type[i];
1120 seg_i->next_blkoff = blk_off;
1122 if (seg_i->alloc_type == SSR)
1123 blk_off = sbi->blocks_per_seg;
1125 for (j = 0; j < blk_off; j++) {
1126 struct f2fs_summary *s;
1127 s = (struct f2fs_summary *)(kaddr + offset);
1128 seg_i->sum_blk->entries[j] = *s;
1129 offset += SUMMARY_SIZE;
1130 if (offset + SUMMARY_SIZE <= PAGE_CACHE_SIZE -
1134 f2fs_put_page(page, 1);
1137 page = get_meta_page(sbi, start++);
1138 kaddr = (unsigned char *)page_address(page);
1142 f2fs_put_page(page, 1);
1146 static int read_normal_summaries(struct f2fs_sb_info *sbi, int type)
1148 struct f2fs_checkpoint *ckpt = F2FS_CKPT(sbi);
1149 struct f2fs_summary_block *sum;
1150 struct curseg_info *curseg;
1152 unsigned short blk_off;
1153 unsigned int segno = 0;
1154 block_t blk_addr = 0;
1156 /* get segment number and block addr */
1157 if (IS_DATASEG(type)) {
1158 segno = le32_to_cpu(ckpt->cur_data_segno[type]);
1159 blk_off = le16_to_cpu(ckpt->cur_data_blkoff[type -
1161 if (is_set_ckpt_flags(ckpt, CP_UMOUNT_FLAG))
1162 blk_addr = sum_blk_addr(sbi, NR_CURSEG_TYPE, type);
1164 blk_addr = sum_blk_addr(sbi, NR_CURSEG_DATA_TYPE, type);
1166 segno = le32_to_cpu(ckpt->cur_node_segno[type -
1168 blk_off = le16_to_cpu(ckpt->cur_node_blkoff[type -
1170 if (is_set_ckpt_flags(ckpt, CP_UMOUNT_FLAG))
1171 blk_addr = sum_blk_addr(sbi, NR_CURSEG_NODE_TYPE,
1172 type - CURSEG_HOT_NODE);
1174 blk_addr = GET_SUM_BLOCK(sbi, segno);
1177 new = get_meta_page(sbi, blk_addr);
1178 sum = (struct f2fs_summary_block *)page_address(new);
1180 if (IS_NODESEG(type)) {
1181 if (is_set_ckpt_flags(ckpt, CP_UMOUNT_FLAG)) {
1182 struct f2fs_summary *ns = &sum->entries[0];
1184 for (i = 0; i < sbi->blocks_per_seg; i++, ns++) {
1186 ns->ofs_in_node = 0;
1191 err = restore_node_summary(sbi, segno, sum);
1193 f2fs_put_page(new, 1);
1199 /* set uncompleted segment to curseg */
1200 curseg = CURSEG_I(sbi, type);
1201 mutex_lock(&curseg->curseg_mutex);
1202 memcpy(curseg->sum_blk, sum, PAGE_CACHE_SIZE);
1203 curseg->next_segno = segno;
1204 reset_curseg(sbi, type, 0);
1205 curseg->alloc_type = ckpt->alloc_type[type];
1206 curseg->next_blkoff = blk_off;
1207 mutex_unlock(&curseg->curseg_mutex);
1208 f2fs_put_page(new, 1);
1212 static int restore_curseg_summaries(struct f2fs_sb_info *sbi)
1214 int type = CURSEG_HOT_DATA;
1217 if (is_set_ckpt_flags(F2FS_CKPT(sbi), CP_COMPACT_SUM_FLAG)) {
1218 /* restore for compacted data summary */
1219 if (read_compacted_summaries(sbi))
1221 type = CURSEG_HOT_NODE;
1224 for (; type <= CURSEG_COLD_NODE; type++) {
1225 err = read_normal_summaries(sbi, type);
1233 static void write_compacted_summaries(struct f2fs_sb_info *sbi, block_t blkaddr)
1236 unsigned char *kaddr;
1237 struct f2fs_summary *summary;
1238 struct curseg_info *seg_i;
1239 int written_size = 0;
1242 page = grab_meta_page(sbi, blkaddr++);
1243 kaddr = (unsigned char *)page_address(page);
1245 /* Step 1: write nat cache */
1246 seg_i = CURSEG_I(sbi, CURSEG_HOT_DATA);
1247 memcpy(kaddr, &seg_i->sum_blk->n_nats, SUM_JOURNAL_SIZE);
1248 written_size += SUM_JOURNAL_SIZE;
1250 /* Step 2: write sit cache */
1251 seg_i = CURSEG_I(sbi, CURSEG_COLD_DATA);
1252 memcpy(kaddr + written_size, &seg_i->sum_blk->n_sits,
1254 written_size += SUM_JOURNAL_SIZE;
1256 /* Step 3: write summary entries */
1257 for (i = CURSEG_HOT_DATA; i <= CURSEG_COLD_DATA; i++) {
1258 unsigned short blkoff;
1259 seg_i = CURSEG_I(sbi, i);
1260 if (sbi->ckpt->alloc_type[i] == SSR)
1261 blkoff = sbi->blocks_per_seg;
1263 blkoff = curseg_blkoff(sbi, i);
1265 for (j = 0; j < blkoff; j++) {
1267 page = grab_meta_page(sbi, blkaddr++);
1268 kaddr = (unsigned char *)page_address(page);
1271 summary = (struct f2fs_summary *)(kaddr + written_size);
1272 *summary = seg_i->sum_blk->entries[j];
1273 written_size += SUMMARY_SIZE;
1275 if (written_size + SUMMARY_SIZE <= PAGE_CACHE_SIZE -
1279 set_page_dirty(page);
1280 f2fs_put_page(page, 1);
1285 set_page_dirty(page);
1286 f2fs_put_page(page, 1);
1290 static void write_normal_summaries(struct f2fs_sb_info *sbi,
1291 block_t blkaddr, int type)
1294 if (IS_DATASEG(type))
1295 end = type + NR_CURSEG_DATA_TYPE;
1297 end = type + NR_CURSEG_NODE_TYPE;
1299 for (i = type; i < end; i++) {
1300 struct curseg_info *sum = CURSEG_I(sbi, i);
1301 mutex_lock(&sum->curseg_mutex);
1302 write_sum_page(sbi, sum->sum_blk, blkaddr + (i - type));
1303 mutex_unlock(&sum->curseg_mutex);
1307 void write_data_summaries(struct f2fs_sb_info *sbi, block_t start_blk)
1309 if (is_set_ckpt_flags(F2FS_CKPT(sbi), CP_COMPACT_SUM_FLAG))
1310 write_compacted_summaries(sbi, start_blk);
1312 write_normal_summaries(sbi, start_blk, CURSEG_HOT_DATA);
1315 void write_node_summaries(struct f2fs_sb_info *sbi, block_t start_blk)
1317 if (is_set_ckpt_flags(F2FS_CKPT(sbi), CP_UMOUNT_FLAG))
1318 write_normal_summaries(sbi, start_blk, CURSEG_HOT_NODE);
1321 int lookup_journal_in_cursum(struct f2fs_summary_block *sum, int type,
1322 unsigned int val, int alloc)
1326 if (type == NAT_JOURNAL) {
1327 for (i = 0; i < nats_in_cursum(sum); i++) {
1328 if (le32_to_cpu(nid_in_journal(sum, i)) == val)
1331 if (alloc && nats_in_cursum(sum) < NAT_JOURNAL_ENTRIES)
1332 return update_nats_in_cursum(sum, 1);
1333 } else if (type == SIT_JOURNAL) {
1334 for (i = 0; i < sits_in_cursum(sum); i++)
1335 if (le32_to_cpu(segno_in_journal(sum, i)) == val)
1337 if (alloc && sits_in_cursum(sum) < SIT_JOURNAL_ENTRIES)
1338 return update_sits_in_cursum(sum, 1);
1343 static struct page *get_current_sit_page(struct f2fs_sb_info *sbi,
1346 struct sit_info *sit_i = SIT_I(sbi);
1347 unsigned int offset = SIT_BLOCK_OFFSET(sit_i, segno);
1348 block_t blk_addr = sit_i->sit_base_addr + offset;
1350 check_seg_range(sbi, segno);
1352 /* calculate sit block address */
1353 if (f2fs_test_bit(offset, sit_i->sit_bitmap))
1354 blk_addr += sit_i->sit_blocks;
1356 return get_meta_page(sbi, blk_addr);
1359 static struct page *get_next_sit_page(struct f2fs_sb_info *sbi,
1362 struct sit_info *sit_i = SIT_I(sbi);
1363 struct page *src_page, *dst_page;
1364 pgoff_t src_off, dst_off;
1365 void *src_addr, *dst_addr;
1367 src_off = current_sit_addr(sbi, start);
1368 dst_off = next_sit_addr(sbi, src_off);
1370 /* get current sit block page without lock */
1371 src_page = get_meta_page(sbi, src_off);
1372 dst_page = grab_meta_page(sbi, dst_off);
1373 f2fs_bug_on(PageDirty(src_page));
1375 src_addr = page_address(src_page);
1376 dst_addr = page_address(dst_page);
1377 memcpy(dst_addr, src_addr, PAGE_CACHE_SIZE);
1379 set_page_dirty(dst_page);
1380 f2fs_put_page(src_page, 1);
1382 set_to_next_sit(sit_i, start);
1387 static bool flush_sits_in_journal(struct f2fs_sb_info *sbi)
1389 struct curseg_info *curseg = CURSEG_I(sbi, CURSEG_COLD_DATA);
1390 struct f2fs_summary_block *sum = curseg->sum_blk;
1394 * If the journal area in the current summary is full of sit entries,
1395 * all the sit entries will be flushed. Otherwise the sit entries
1396 * are not able to replace with newly hot sit entries.
1398 if (sits_in_cursum(sum) >= SIT_JOURNAL_ENTRIES) {
1399 for (i = sits_in_cursum(sum) - 1; i >= 0; i--) {
1401 segno = le32_to_cpu(segno_in_journal(sum, i));
1402 __mark_sit_entry_dirty(sbi, segno);
1404 update_sits_in_cursum(sum, -sits_in_cursum(sum));
1411 * CP calls this function, which flushes SIT entries including sit_journal,
1412 * and moves prefree segs to free segs.
1414 void flush_sit_entries(struct f2fs_sb_info *sbi)
1416 struct sit_info *sit_i = SIT_I(sbi);
1417 unsigned long *bitmap = sit_i->dirty_sentries_bitmap;
1418 struct curseg_info *curseg = CURSEG_I(sbi, CURSEG_COLD_DATA);
1419 struct f2fs_summary_block *sum = curseg->sum_blk;
1420 unsigned long nsegs = TOTAL_SEGS(sbi);
1421 struct page *page = NULL;
1422 struct f2fs_sit_block *raw_sit = NULL;
1423 unsigned int start = 0, end = 0;
1424 unsigned int segno = -1;
1427 mutex_lock(&curseg->curseg_mutex);
1428 mutex_lock(&sit_i->sentry_lock);
1431 * "flushed" indicates whether sit entries in journal are flushed
1432 * to the SIT area or not.
1434 flushed = flush_sits_in_journal(sbi);
1436 while ((segno = find_next_bit(bitmap, nsegs, segno + 1)) < nsegs) {
1437 struct seg_entry *se = get_seg_entry(sbi, segno);
1438 int sit_offset, offset;
1440 sit_offset = SIT_ENTRY_OFFSET(sit_i, segno);
1442 /* add discard candidates */
1443 if (SM_I(sbi)->nr_discards < SM_I(sbi)->max_discards)
1444 add_discard_addrs(sbi, segno, se);
1449 offset = lookup_journal_in_cursum(sum, SIT_JOURNAL, segno, 1);
1451 segno_in_journal(sum, offset) = cpu_to_le32(segno);
1452 seg_info_to_raw_sit(se, &sit_in_journal(sum, offset));
1456 if (!page || (start > segno) || (segno > end)) {
1458 f2fs_put_page(page, 1);
1462 start = START_SEGNO(sit_i, segno);
1463 end = start + SIT_ENTRY_PER_BLOCK - 1;
1465 /* read sit block that will be updated */
1466 page = get_next_sit_page(sbi, start);
1467 raw_sit = page_address(page);
1470 /* udpate entry in SIT block */
1471 seg_info_to_raw_sit(se, &raw_sit->entries[sit_offset]);
1473 __clear_bit(segno, bitmap);
1474 sit_i->dirty_sentries--;
1476 mutex_unlock(&sit_i->sentry_lock);
1477 mutex_unlock(&curseg->curseg_mutex);
1479 /* writeout last modified SIT block */
1480 f2fs_put_page(page, 1);
1482 set_prefree_as_free_segments(sbi);
1485 static int build_sit_info(struct f2fs_sb_info *sbi)
1487 struct f2fs_super_block *raw_super = F2FS_RAW_SUPER(sbi);
1488 struct f2fs_checkpoint *ckpt = F2FS_CKPT(sbi);
1489 struct sit_info *sit_i;
1490 unsigned int sit_segs, start;
1491 char *src_bitmap, *dst_bitmap;
1492 unsigned int bitmap_size;
1494 /* allocate memory for SIT information */
1495 sit_i = kzalloc(sizeof(struct sit_info), GFP_KERNEL);
1499 SM_I(sbi)->sit_info = sit_i;
1501 sit_i->sentries = vzalloc(TOTAL_SEGS(sbi) * sizeof(struct seg_entry));
1502 if (!sit_i->sentries)
1505 bitmap_size = f2fs_bitmap_size(TOTAL_SEGS(sbi));
1506 sit_i->dirty_sentries_bitmap = kzalloc(bitmap_size, GFP_KERNEL);
1507 if (!sit_i->dirty_sentries_bitmap)
1510 for (start = 0; start < TOTAL_SEGS(sbi); start++) {
1511 sit_i->sentries[start].cur_valid_map
1512 = kzalloc(SIT_VBLOCK_MAP_SIZE, GFP_KERNEL);
1513 sit_i->sentries[start].ckpt_valid_map
1514 = kzalloc(SIT_VBLOCK_MAP_SIZE, GFP_KERNEL);
1515 if (!sit_i->sentries[start].cur_valid_map
1516 || !sit_i->sentries[start].ckpt_valid_map)
1520 if (sbi->segs_per_sec > 1) {
1521 sit_i->sec_entries = vzalloc(TOTAL_SECS(sbi) *
1522 sizeof(struct sec_entry));
1523 if (!sit_i->sec_entries)
1527 /* get information related with SIT */
1528 sit_segs = le32_to_cpu(raw_super->segment_count_sit) >> 1;
1530 /* setup SIT bitmap from ckeckpoint pack */
1531 bitmap_size = __bitmap_size(sbi, SIT_BITMAP);
1532 src_bitmap = __bitmap_ptr(sbi, SIT_BITMAP);
1534 dst_bitmap = kmemdup(src_bitmap, bitmap_size, GFP_KERNEL);
1538 /* init SIT information */
1539 sit_i->s_ops = &default_salloc_ops;
1541 sit_i->sit_base_addr = le32_to_cpu(raw_super->sit_blkaddr);
1542 sit_i->sit_blocks = sit_segs << sbi->log_blocks_per_seg;
1543 sit_i->written_valid_blocks = le64_to_cpu(ckpt->valid_block_count);
1544 sit_i->sit_bitmap = dst_bitmap;
1545 sit_i->bitmap_size = bitmap_size;
1546 sit_i->dirty_sentries = 0;
1547 sit_i->sents_per_block = SIT_ENTRY_PER_BLOCK;
1548 sit_i->elapsed_time = le64_to_cpu(sbi->ckpt->elapsed_time);
1549 sit_i->mounted_time = CURRENT_TIME_SEC.tv_sec;
1550 mutex_init(&sit_i->sentry_lock);
1554 static int build_free_segmap(struct f2fs_sb_info *sbi)
1556 struct f2fs_sm_info *sm_info = SM_I(sbi);
1557 struct free_segmap_info *free_i;
1558 unsigned int bitmap_size, sec_bitmap_size;
1560 /* allocate memory for free segmap information */
1561 free_i = kzalloc(sizeof(struct free_segmap_info), GFP_KERNEL);
1565 SM_I(sbi)->free_info = free_i;
1567 bitmap_size = f2fs_bitmap_size(TOTAL_SEGS(sbi));
1568 free_i->free_segmap = kmalloc(bitmap_size, GFP_KERNEL);
1569 if (!free_i->free_segmap)
1572 sec_bitmap_size = f2fs_bitmap_size(TOTAL_SECS(sbi));
1573 free_i->free_secmap = kmalloc(sec_bitmap_size, GFP_KERNEL);
1574 if (!free_i->free_secmap)
1577 /* set all segments as dirty temporarily */
1578 memset(free_i->free_segmap, 0xff, bitmap_size);
1579 memset(free_i->free_secmap, 0xff, sec_bitmap_size);
1581 /* init free segmap information */
1582 free_i->start_segno =
1583 (unsigned int) GET_SEGNO_FROM_SEG0(sbi, sm_info->main_blkaddr);
1584 free_i->free_segments = 0;
1585 free_i->free_sections = 0;
1586 rwlock_init(&free_i->segmap_lock);
1590 static int build_curseg(struct f2fs_sb_info *sbi)
1592 struct curseg_info *array;
1595 array = kzalloc(sizeof(*array) * NR_CURSEG_TYPE, GFP_KERNEL);
1599 SM_I(sbi)->curseg_array = array;
1601 for (i = 0; i < NR_CURSEG_TYPE; i++) {
1602 mutex_init(&array[i].curseg_mutex);
1603 array[i].sum_blk = kzalloc(PAGE_CACHE_SIZE, GFP_KERNEL);
1604 if (!array[i].sum_blk)
1606 array[i].segno = NULL_SEGNO;
1607 array[i].next_blkoff = 0;
1609 return restore_curseg_summaries(sbi);
1612 static void build_sit_entries(struct f2fs_sb_info *sbi)
1614 struct sit_info *sit_i = SIT_I(sbi);
1615 struct curseg_info *curseg = CURSEG_I(sbi, CURSEG_COLD_DATA);
1616 struct f2fs_summary_block *sum = curseg->sum_blk;
1617 int sit_blk_cnt = SIT_BLK_CNT(sbi);
1618 unsigned int i, start, end;
1619 unsigned int readed, start_blk = 0;
1620 int nrpages = MAX_BIO_BLOCKS(max_hw_blocks(sbi));
1623 readed = ra_meta_pages(sbi, start_blk, nrpages, META_SIT);
1625 start = start_blk * sit_i->sents_per_block;
1626 end = (start_blk + readed) * sit_i->sents_per_block;
1628 for (; start < end && start < TOTAL_SEGS(sbi); start++) {
1629 struct seg_entry *se = &sit_i->sentries[start];
1630 struct f2fs_sit_block *sit_blk;
1631 struct f2fs_sit_entry sit;
1634 mutex_lock(&curseg->curseg_mutex);
1635 for (i = 0; i < sits_in_cursum(sum); i++) {
1636 if (le32_to_cpu(segno_in_journal(sum, i))
1638 sit = sit_in_journal(sum, i);
1639 mutex_unlock(&curseg->curseg_mutex);
1643 mutex_unlock(&curseg->curseg_mutex);
1645 page = get_current_sit_page(sbi, start);
1646 sit_blk = (struct f2fs_sit_block *)page_address(page);
1647 sit = sit_blk->entries[SIT_ENTRY_OFFSET(sit_i, start)];
1648 f2fs_put_page(page, 1);
1650 check_block_count(sbi, start, &sit);
1651 seg_info_from_raw_sit(se, &sit);
1652 if (sbi->segs_per_sec > 1) {
1653 struct sec_entry *e = get_sec_entry(sbi, start);
1654 e->valid_blocks += se->valid_blocks;
1657 start_blk += readed;
1658 } while (start_blk < sit_blk_cnt);
1661 static void init_free_segmap(struct f2fs_sb_info *sbi)
1666 for (start = 0; start < TOTAL_SEGS(sbi); start++) {
1667 struct seg_entry *sentry = get_seg_entry(sbi, start);
1668 if (!sentry->valid_blocks)
1669 __set_free(sbi, start);
1672 /* set use the current segments */
1673 for (type = CURSEG_HOT_DATA; type <= CURSEG_COLD_NODE; type++) {
1674 struct curseg_info *curseg_t = CURSEG_I(sbi, type);
1675 __set_test_and_inuse(sbi, curseg_t->segno);
1679 static void init_dirty_segmap(struct f2fs_sb_info *sbi)
1681 struct dirty_seglist_info *dirty_i = DIRTY_I(sbi);
1682 struct free_segmap_info *free_i = FREE_I(sbi);
1683 unsigned int segno = 0, offset = 0, total_segs = TOTAL_SEGS(sbi);
1684 unsigned short valid_blocks;
1687 /* find dirty segment based on free segmap */
1688 segno = find_next_inuse(free_i, total_segs, offset);
1689 if (segno >= total_segs)
1692 valid_blocks = get_valid_blocks(sbi, segno, 0);
1693 if (valid_blocks >= sbi->blocks_per_seg || !valid_blocks)
1695 mutex_lock(&dirty_i->seglist_lock);
1696 __locate_dirty_segment(sbi, segno, DIRTY);
1697 mutex_unlock(&dirty_i->seglist_lock);
1701 static int init_victim_secmap(struct f2fs_sb_info *sbi)
1703 struct dirty_seglist_info *dirty_i = DIRTY_I(sbi);
1704 unsigned int bitmap_size = f2fs_bitmap_size(TOTAL_SECS(sbi));
1706 dirty_i->victim_secmap = kzalloc(bitmap_size, GFP_KERNEL);
1707 if (!dirty_i->victim_secmap)
1712 static int build_dirty_segmap(struct f2fs_sb_info *sbi)
1714 struct dirty_seglist_info *dirty_i;
1715 unsigned int bitmap_size, i;
1717 /* allocate memory for dirty segments list information */
1718 dirty_i = kzalloc(sizeof(struct dirty_seglist_info), GFP_KERNEL);
1722 SM_I(sbi)->dirty_info = dirty_i;
1723 mutex_init(&dirty_i->seglist_lock);
1725 bitmap_size = f2fs_bitmap_size(TOTAL_SEGS(sbi));
1727 for (i = 0; i < NR_DIRTY_TYPE; i++) {
1728 dirty_i->dirty_segmap[i] = kzalloc(bitmap_size, GFP_KERNEL);
1729 if (!dirty_i->dirty_segmap[i])
1733 init_dirty_segmap(sbi);
1734 return init_victim_secmap(sbi);
1738 * Update min, max modified time for cost-benefit GC algorithm
1740 static void init_min_max_mtime(struct f2fs_sb_info *sbi)
1742 struct sit_info *sit_i = SIT_I(sbi);
1745 mutex_lock(&sit_i->sentry_lock);
1747 sit_i->min_mtime = LLONG_MAX;
1749 for (segno = 0; segno < TOTAL_SEGS(sbi); segno += sbi->segs_per_sec) {
1751 unsigned long long mtime = 0;
1753 for (i = 0; i < sbi->segs_per_sec; i++)
1754 mtime += get_seg_entry(sbi, segno + i)->mtime;
1756 mtime = div_u64(mtime, sbi->segs_per_sec);
1758 if (sit_i->min_mtime > mtime)
1759 sit_i->min_mtime = mtime;
1761 sit_i->max_mtime = get_mtime(sbi);
1762 mutex_unlock(&sit_i->sentry_lock);
1765 int build_segment_manager(struct f2fs_sb_info *sbi)
1767 struct f2fs_super_block *raw_super = F2FS_RAW_SUPER(sbi);
1768 struct f2fs_checkpoint *ckpt = F2FS_CKPT(sbi);
1769 struct f2fs_sm_info *sm_info;
1772 sm_info = kzalloc(sizeof(struct f2fs_sm_info), GFP_KERNEL);
1777 sbi->sm_info = sm_info;
1778 INIT_LIST_HEAD(&sm_info->wblist_head);
1779 spin_lock_init(&sm_info->wblist_lock);
1780 sm_info->seg0_blkaddr = le32_to_cpu(raw_super->segment0_blkaddr);
1781 sm_info->main_blkaddr = le32_to_cpu(raw_super->main_blkaddr);
1782 sm_info->segment_count = le32_to_cpu(raw_super->segment_count);
1783 sm_info->reserved_segments = le32_to_cpu(ckpt->rsvd_segment_count);
1784 sm_info->ovp_segments = le32_to_cpu(ckpt->overprov_segment_count);
1785 sm_info->main_segments = le32_to_cpu(raw_super->segment_count_main);
1786 sm_info->ssa_blkaddr = le32_to_cpu(raw_super->ssa_blkaddr);
1787 sm_info->rec_prefree_segments = sm_info->main_segments *
1788 DEF_RECLAIM_PREFREE_SEGMENTS / 100;
1789 sm_info->ipu_policy = F2FS_IPU_DISABLE;
1790 sm_info->min_ipu_util = DEF_MIN_IPU_UTIL;
1792 INIT_LIST_HEAD(&sm_info->discard_list);
1793 sm_info->nr_discards = 0;
1794 sm_info->max_discards = 0;
1796 err = build_sit_info(sbi);
1799 err = build_free_segmap(sbi);
1802 err = build_curseg(sbi);
1806 /* reinit free segmap based on SIT */
1807 build_sit_entries(sbi);
1809 init_free_segmap(sbi);
1810 err = build_dirty_segmap(sbi);
1814 init_min_max_mtime(sbi);
1818 static void discard_dirty_segmap(struct f2fs_sb_info *sbi,
1819 enum dirty_type dirty_type)
1821 struct dirty_seglist_info *dirty_i = DIRTY_I(sbi);
1823 mutex_lock(&dirty_i->seglist_lock);
1824 kfree(dirty_i->dirty_segmap[dirty_type]);
1825 dirty_i->nr_dirty[dirty_type] = 0;
1826 mutex_unlock(&dirty_i->seglist_lock);
1829 static void destroy_victim_secmap(struct f2fs_sb_info *sbi)
1831 struct dirty_seglist_info *dirty_i = DIRTY_I(sbi);
1832 kfree(dirty_i->victim_secmap);
1835 static void destroy_dirty_segmap(struct f2fs_sb_info *sbi)
1837 struct dirty_seglist_info *dirty_i = DIRTY_I(sbi);
1843 /* discard pre-free/dirty segments list */
1844 for (i = 0; i < NR_DIRTY_TYPE; i++)
1845 discard_dirty_segmap(sbi, i);
1847 destroy_victim_secmap(sbi);
1848 SM_I(sbi)->dirty_info = NULL;
1852 static void destroy_curseg(struct f2fs_sb_info *sbi)
1854 struct curseg_info *array = SM_I(sbi)->curseg_array;
1859 SM_I(sbi)->curseg_array = NULL;
1860 for (i = 0; i < NR_CURSEG_TYPE; i++)
1861 kfree(array[i].sum_blk);
1865 static void destroy_free_segmap(struct f2fs_sb_info *sbi)
1867 struct free_segmap_info *free_i = SM_I(sbi)->free_info;
1870 SM_I(sbi)->free_info = NULL;
1871 kfree(free_i->free_segmap);
1872 kfree(free_i->free_secmap);
1876 static void destroy_sit_info(struct f2fs_sb_info *sbi)
1878 struct sit_info *sit_i = SIT_I(sbi);
1884 if (sit_i->sentries) {
1885 for (start = 0; start < TOTAL_SEGS(sbi); start++) {
1886 kfree(sit_i->sentries[start].cur_valid_map);
1887 kfree(sit_i->sentries[start].ckpt_valid_map);
1890 vfree(sit_i->sentries);
1891 vfree(sit_i->sec_entries);
1892 kfree(sit_i->dirty_sentries_bitmap);
1894 SM_I(sbi)->sit_info = NULL;
1895 kfree(sit_i->sit_bitmap);
1899 void destroy_segment_manager(struct f2fs_sb_info *sbi)
1901 struct f2fs_sm_info *sm_info = SM_I(sbi);
1904 destroy_dirty_segmap(sbi);
1905 destroy_curseg(sbi);
1906 destroy_free_segmap(sbi);
1907 destroy_sit_info(sbi);
1908 sbi->sm_info = NULL;
1912 int __init create_segment_manager_caches(void)
1914 discard_entry_slab = f2fs_kmem_cache_create("discard_entry",
1915 sizeof(struct discard_entry));
1916 if (!discard_entry_slab)
1921 void destroy_segment_manager_caches(void)
1923 kmem_cache_destroy(discard_entry_slab);