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/kthread.h>
17 #include <linux/vmalloc.h>
18 #include <linux/swap.h>
23 #include <trace/events/f2fs.h>
25 #define __reverse_ffz(x) __reverse_ffs(~(x))
27 static struct kmem_cache *discard_entry_slab;
30 * __reverse_ffs is copied from include/asm-generic/bitops/__ffs.h since
31 * MSB and LSB are reversed in a byte by f2fs_set_bit.
33 static inline unsigned long __reverse_ffs(unsigned long word)
37 #if BITS_PER_LONG == 64
38 if ((word & 0xffffffff) == 0) {
43 if ((word & 0xffff) == 0) {
47 if ((word & 0xff) == 0) {
51 if ((word & 0xf0) == 0)
55 if ((word & 0xc) == 0)
59 if ((word & 0x2) == 0)
65 * __find_rev_next(_zero)_bit is copied from lib/find_next_bit.c becasue
66 * f2fs_set_bit makes MSB and LSB reversed in a byte.
69 * f2fs_set_bit(0, bitmap) => 0000 0001
70 * f2fs_set_bit(7, bitmap) => 1000 0000
72 static unsigned long __find_rev_next_bit(const unsigned long *addr,
73 unsigned long size, unsigned long offset)
75 const unsigned long *p = addr + BIT_WORD(offset);
76 unsigned long result = offset & ~(BITS_PER_LONG - 1);
78 unsigned long mask, submask;
79 unsigned long quot, rest;
85 offset %= BITS_PER_LONG;
90 quot = (offset >> 3) << 3;
93 submask = (unsigned char)(0xff << rest) >> rest;
97 if (size < BITS_PER_LONG)
102 size -= BITS_PER_LONG;
103 result += BITS_PER_LONG;
105 while (size & ~(BITS_PER_LONG-1)) {
109 result += BITS_PER_LONG;
110 size -= BITS_PER_LONG;
116 tmp &= (~0UL >> (BITS_PER_LONG - size));
117 if (tmp == 0UL) /* Are any bits set? */
118 return result + size; /* Nope. */
120 return result + __reverse_ffs(tmp);
123 static unsigned long __find_rev_next_zero_bit(const unsigned long *addr,
124 unsigned long size, unsigned long offset)
126 const unsigned long *p = addr + BIT_WORD(offset);
127 unsigned long result = offset & ~(BITS_PER_LONG - 1);
129 unsigned long mask, submask;
130 unsigned long quot, rest;
136 offset %= BITS_PER_LONG;
141 quot = (offset >> 3) << 3;
143 mask = ~(~0UL << quot);
144 submask = (unsigned char)~((unsigned char)(0xff << rest) >> rest);
148 if (size < BITS_PER_LONG)
153 size -= BITS_PER_LONG;
154 result += BITS_PER_LONG;
156 while (size & ~(BITS_PER_LONG - 1)) {
160 result += BITS_PER_LONG;
161 size -= BITS_PER_LONG;
169 if (tmp == ~0UL) /* Are any bits zero? */
170 return result + size; /* Nope. */
172 return result + __reverse_ffz(tmp);
176 * This function balances dirty node and dentry pages.
177 * In addition, it controls garbage collection.
179 void f2fs_balance_fs(struct f2fs_sb_info *sbi)
182 * We should do GC or end up with checkpoint, if there are so many dirty
183 * dir/node pages without enough free segments.
185 if (has_not_enough_free_secs(sbi, 0)) {
186 mutex_lock(&sbi->gc_mutex);
191 void f2fs_balance_fs_bg(struct f2fs_sb_info *sbi)
193 /* check the # of cached NAT entries and prefree segments */
194 if (try_to_free_nats(sbi, NAT_ENTRY_PER_BLOCK) ||
195 excess_prefree_segs(sbi))
196 f2fs_sync_fs(sbi->sb, true);
199 static int issue_flush_thread(void *data)
201 struct f2fs_sb_info *sbi = data;
202 struct flush_cmd_control *fcc = SM_I(sbi)->cmd_control_info;
203 wait_queue_head_t *q = &fcc->flush_wait_queue;
205 if (kthread_should_stop())
208 spin_lock(&fcc->issue_lock);
209 if (fcc->issue_list) {
210 fcc->dispatch_list = fcc->issue_list;
211 fcc->issue_list = fcc->issue_tail = NULL;
213 spin_unlock(&fcc->issue_lock);
215 if (fcc->dispatch_list) {
216 struct bio *bio = bio_alloc(GFP_NOIO, 0);
217 struct flush_cmd *cmd, *next;
220 bio->bi_bdev = sbi->sb->s_bdev;
221 ret = submit_bio_wait(WRITE_FLUSH, bio);
223 for (cmd = fcc->dispatch_list; cmd; cmd = next) {
226 complete(&cmd->wait);
229 fcc->dispatch_list = NULL;
232 wait_event_interruptible(*q,
233 kthread_should_stop() || fcc->issue_list);
237 int f2fs_issue_flush(struct f2fs_sb_info *sbi)
239 struct flush_cmd_control *fcc = SM_I(sbi)->cmd_control_info;
240 struct flush_cmd cmd;
242 trace_f2fs_issue_flush(sbi->sb, test_opt(sbi, NOBARRIER),
243 test_opt(sbi, FLUSH_MERGE));
245 if (test_opt(sbi, NOBARRIER))
248 if (!test_opt(sbi, FLUSH_MERGE))
249 return blkdev_issue_flush(sbi->sb->s_bdev, GFP_KERNEL, NULL);
251 init_completion(&cmd.wait);
254 spin_lock(&fcc->issue_lock);
256 fcc->issue_tail->next = &cmd;
258 fcc->issue_list = &cmd;
259 fcc->issue_tail = &cmd;
260 spin_unlock(&fcc->issue_lock);
262 if (!fcc->dispatch_list)
263 wake_up(&fcc->flush_wait_queue);
265 wait_for_completion(&cmd.wait);
270 int create_flush_cmd_control(struct f2fs_sb_info *sbi)
272 dev_t dev = sbi->sb->s_bdev->bd_dev;
273 struct flush_cmd_control *fcc;
276 fcc = kzalloc(sizeof(struct flush_cmd_control), GFP_KERNEL);
279 spin_lock_init(&fcc->issue_lock);
280 init_waitqueue_head(&fcc->flush_wait_queue);
281 SM_I(sbi)->cmd_control_info = fcc;
282 fcc->f2fs_issue_flush = kthread_run(issue_flush_thread, sbi,
283 "f2fs_flush-%u:%u", MAJOR(dev), MINOR(dev));
284 if (IS_ERR(fcc->f2fs_issue_flush)) {
285 err = PTR_ERR(fcc->f2fs_issue_flush);
287 SM_I(sbi)->cmd_control_info = NULL;
294 void destroy_flush_cmd_control(struct f2fs_sb_info *sbi)
296 struct flush_cmd_control *fcc = SM_I(sbi)->cmd_control_info;
298 if (fcc && fcc->f2fs_issue_flush)
299 kthread_stop(fcc->f2fs_issue_flush);
301 SM_I(sbi)->cmd_control_info = NULL;
304 static void __locate_dirty_segment(struct f2fs_sb_info *sbi, unsigned int segno,
305 enum dirty_type dirty_type)
307 struct dirty_seglist_info *dirty_i = DIRTY_I(sbi);
309 /* need not be added */
310 if (IS_CURSEG(sbi, segno))
313 if (!test_and_set_bit(segno, dirty_i->dirty_segmap[dirty_type]))
314 dirty_i->nr_dirty[dirty_type]++;
316 if (dirty_type == DIRTY) {
317 struct seg_entry *sentry = get_seg_entry(sbi, segno);
318 enum dirty_type t = sentry->type;
320 if (!test_and_set_bit(segno, dirty_i->dirty_segmap[t]))
321 dirty_i->nr_dirty[t]++;
325 static void __remove_dirty_segment(struct f2fs_sb_info *sbi, unsigned int segno,
326 enum dirty_type dirty_type)
328 struct dirty_seglist_info *dirty_i = DIRTY_I(sbi);
330 if (test_and_clear_bit(segno, dirty_i->dirty_segmap[dirty_type]))
331 dirty_i->nr_dirty[dirty_type]--;
333 if (dirty_type == DIRTY) {
334 struct seg_entry *sentry = get_seg_entry(sbi, segno);
335 enum dirty_type t = sentry->type;
337 if (test_and_clear_bit(segno, dirty_i->dirty_segmap[t]))
338 dirty_i->nr_dirty[t]--;
340 if (get_valid_blocks(sbi, segno, sbi->segs_per_sec) == 0)
341 clear_bit(GET_SECNO(sbi, segno),
342 dirty_i->victim_secmap);
347 * Should not occur error such as -ENOMEM.
348 * Adding dirty entry into seglist is not critical operation.
349 * If a given segment is one of current working segments, it won't be added.
351 static void locate_dirty_segment(struct f2fs_sb_info *sbi, unsigned int segno)
353 struct dirty_seglist_info *dirty_i = DIRTY_I(sbi);
354 unsigned short valid_blocks;
356 if (segno == NULL_SEGNO || IS_CURSEG(sbi, segno))
359 mutex_lock(&dirty_i->seglist_lock);
361 valid_blocks = get_valid_blocks(sbi, segno, 0);
363 if (valid_blocks == 0) {
364 __locate_dirty_segment(sbi, segno, PRE);
365 __remove_dirty_segment(sbi, segno, DIRTY);
366 } else if (valid_blocks < sbi->blocks_per_seg) {
367 __locate_dirty_segment(sbi, segno, DIRTY);
369 /* Recovery routine with SSR needs this */
370 __remove_dirty_segment(sbi, segno, DIRTY);
373 mutex_unlock(&dirty_i->seglist_lock);
376 static int f2fs_issue_discard(struct f2fs_sb_info *sbi,
377 block_t blkstart, block_t blklen)
379 sector_t start = SECTOR_FROM_BLOCK(sbi, blkstart);
380 sector_t len = SECTOR_FROM_BLOCK(sbi, blklen);
381 trace_f2fs_issue_discard(sbi->sb, blkstart, blklen);
382 return blkdev_issue_discard(sbi->sb->s_bdev, start, len, GFP_NOFS, 0);
385 void discard_next_dnode(struct f2fs_sb_info *sbi, block_t blkaddr)
387 if (f2fs_issue_discard(sbi, blkaddr, 1)) {
388 struct page *page = grab_meta_page(sbi, blkaddr);
389 /* zero-filled page */
390 set_page_dirty(page);
391 f2fs_put_page(page, 1);
395 static void add_discard_addrs(struct f2fs_sb_info *sbi,
396 unsigned int segno, struct seg_entry *se)
398 struct list_head *head = &SM_I(sbi)->discard_list;
399 struct discard_entry *new;
400 int entries = SIT_VBLOCK_MAP_SIZE / sizeof(unsigned long);
401 int max_blocks = sbi->blocks_per_seg;
402 unsigned long *cur_map = (unsigned long *)se->cur_valid_map;
403 unsigned long *ckpt_map = (unsigned long *)se->ckpt_valid_map;
404 unsigned long dmap[entries];
405 unsigned int start = 0, end = -1;
408 if (!test_opt(sbi, DISCARD))
411 /* zero block will be discarded through the prefree list */
412 if (!se->valid_blocks || se->valid_blocks == max_blocks)
415 /* SIT_VBLOCK_MAP_SIZE should be multiple of sizeof(unsigned long) */
416 for (i = 0; i < entries; i++)
417 dmap[i] = (cur_map[i] ^ ckpt_map[i]) & ckpt_map[i];
419 while (SM_I(sbi)->nr_discards <= SM_I(sbi)->max_discards) {
420 start = __find_rev_next_bit(dmap, max_blocks, end + 1);
421 if (start >= max_blocks)
424 end = __find_rev_next_zero_bit(dmap, max_blocks, start + 1);
426 new = f2fs_kmem_cache_alloc(discard_entry_slab, GFP_NOFS);
427 INIT_LIST_HEAD(&new->list);
428 new->blkaddr = START_BLOCK(sbi, segno) + start;
429 new->len = end - start;
431 list_add_tail(&new->list, head);
432 SM_I(sbi)->nr_discards += end - start;
437 * Should call clear_prefree_segments after checkpoint is done.
439 static void set_prefree_as_free_segments(struct f2fs_sb_info *sbi)
441 struct dirty_seglist_info *dirty_i = DIRTY_I(sbi);
443 unsigned int total_segs = TOTAL_SEGS(sbi);
445 mutex_lock(&dirty_i->seglist_lock);
446 for_each_set_bit(segno, dirty_i->dirty_segmap[PRE], total_segs)
447 __set_test_and_free(sbi, segno);
448 mutex_unlock(&dirty_i->seglist_lock);
451 void clear_prefree_segments(struct f2fs_sb_info *sbi)
453 struct list_head *head = &(SM_I(sbi)->discard_list);
454 struct discard_entry *entry, *this;
455 struct dirty_seglist_info *dirty_i = DIRTY_I(sbi);
456 unsigned long *prefree_map = dirty_i->dirty_segmap[PRE];
457 unsigned int total_segs = TOTAL_SEGS(sbi);
458 unsigned int start = 0, end = -1;
460 mutex_lock(&dirty_i->seglist_lock);
464 start = find_next_bit(prefree_map, total_segs, end + 1);
465 if (start >= total_segs)
467 end = find_next_zero_bit(prefree_map, total_segs, start + 1);
469 for (i = start; i < end; i++)
470 clear_bit(i, prefree_map);
472 dirty_i->nr_dirty[PRE] -= end - start;
474 if (!test_opt(sbi, DISCARD))
477 f2fs_issue_discard(sbi, START_BLOCK(sbi, start),
478 (end - start) << sbi->log_blocks_per_seg);
480 mutex_unlock(&dirty_i->seglist_lock);
482 /* send small discards */
483 list_for_each_entry_safe(entry, this, head, list) {
484 f2fs_issue_discard(sbi, entry->blkaddr, entry->len);
485 list_del(&entry->list);
486 SM_I(sbi)->nr_discards -= entry->len;
487 kmem_cache_free(discard_entry_slab, entry);
491 static void __mark_sit_entry_dirty(struct f2fs_sb_info *sbi, unsigned int segno)
493 struct sit_info *sit_i = SIT_I(sbi);
494 if (!__test_and_set_bit(segno, sit_i->dirty_sentries_bitmap))
495 sit_i->dirty_sentries++;
498 static void __set_sit_entry_type(struct f2fs_sb_info *sbi, int type,
499 unsigned int segno, int modified)
501 struct seg_entry *se = get_seg_entry(sbi, segno);
504 __mark_sit_entry_dirty(sbi, segno);
507 static void update_sit_entry(struct f2fs_sb_info *sbi, block_t blkaddr, int del)
509 struct seg_entry *se;
510 unsigned int segno, offset;
511 long int new_vblocks;
513 segno = GET_SEGNO(sbi, blkaddr);
515 se = get_seg_entry(sbi, segno);
516 new_vblocks = se->valid_blocks + del;
517 offset = GET_BLKOFF_FROM_SEG0(sbi, blkaddr);
519 f2fs_bug_on((new_vblocks >> (sizeof(unsigned short) << 3) ||
520 (new_vblocks > sbi->blocks_per_seg)));
522 se->valid_blocks = new_vblocks;
523 se->mtime = get_mtime(sbi);
524 SIT_I(sbi)->max_mtime = se->mtime;
526 /* Update valid block bitmap */
528 if (f2fs_set_bit(offset, se->cur_valid_map))
531 if (!f2fs_clear_bit(offset, se->cur_valid_map))
534 if (!f2fs_test_bit(offset, se->ckpt_valid_map))
535 se->ckpt_valid_blocks += del;
537 __mark_sit_entry_dirty(sbi, segno);
539 /* update total number of valid blocks to be written in ckpt area */
540 SIT_I(sbi)->written_valid_blocks += del;
542 if (sbi->segs_per_sec > 1)
543 get_sec_entry(sbi, segno)->valid_blocks += del;
546 void refresh_sit_entry(struct f2fs_sb_info *sbi, block_t old, block_t new)
548 update_sit_entry(sbi, new, 1);
549 if (GET_SEGNO(sbi, old) != NULL_SEGNO)
550 update_sit_entry(sbi, old, -1);
552 locate_dirty_segment(sbi, GET_SEGNO(sbi, old));
553 locate_dirty_segment(sbi, GET_SEGNO(sbi, new));
556 void invalidate_blocks(struct f2fs_sb_info *sbi, block_t addr)
558 unsigned int segno = GET_SEGNO(sbi, addr);
559 struct sit_info *sit_i = SIT_I(sbi);
561 f2fs_bug_on(addr == NULL_ADDR);
562 if (addr == NEW_ADDR)
565 /* add it into sit main buffer */
566 mutex_lock(&sit_i->sentry_lock);
568 update_sit_entry(sbi, addr, -1);
570 /* add it into dirty seglist */
571 locate_dirty_segment(sbi, segno);
573 mutex_unlock(&sit_i->sentry_lock);
577 * This function should be resided under the curseg_mutex lock
579 static void __add_sum_entry(struct f2fs_sb_info *sbi, int type,
580 struct f2fs_summary *sum)
582 struct curseg_info *curseg = CURSEG_I(sbi, type);
583 void *addr = curseg->sum_blk;
584 addr += curseg->next_blkoff * sizeof(struct f2fs_summary);
585 memcpy(addr, sum, sizeof(struct f2fs_summary));
589 * Calculate the number of current summary pages for writing
591 int npages_for_summary_flush(struct f2fs_sb_info *sbi)
593 int valid_sum_count = 0;
596 for (i = CURSEG_HOT_DATA; i <= CURSEG_COLD_DATA; i++) {
597 if (sbi->ckpt->alloc_type[i] == SSR)
598 valid_sum_count += sbi->blocks_per_seg;
600 valid_sum_count += curseg_blkoff(sbi, i);
603 sum_in_page = (PAGE_CACHE_SIZE - 2 * SUM_JOURNAL_SIZE -
604 SUM_FOOTER_SIZE) / SUMMARY_SIZE;
605 if (valid_sum_count <= sum_in_page)
607 else if ((valid_sum_count - sum_in_page) <=
608 (PAGE_CACHE_SIZE - SUM_FOOTER_SIZE) / SUMMARY_SIZE)
614 * Caller should put this summary page
616 struct page *get_sum_page(struct f2fs_sb_info *sbi, unsigned int segno)
618 return get_meta_page(sbi, GET_SUM_BLOCK(sbi, segno));
621 static void write_sum_page(struct f2fs_sb_info *sbi,
622 struct f2fs_summary_block *sum_blk, block_t blk_addr)
624 struct page *page = grab_meta_page(sbi, blk_addr);
625 void *kaddr = page_address(page);
626 memcpy(kaddr, sum_blk, PAGE_CACHE_SIZE);
627 set_page_dirty(page);
628 f2fs_put_page(page, 1);
631 static int is_next_segment_free(struct f2fs_sb_info *sbi, int type)
633 struct curseg_info *curseg = CURSEG_I(sbi, type);
634 unsigned int segno = curseg->segno + 1;
635 struct free_segmap_info *free_i = FREE_I(sbi);
637 if (segno < TOTAL_SEGS(sbi) && segno % sbi->segs_per_sec)
638 return !test_bit(segno, free_i->free_segmap);
643 * Find a new segment from the free segments bitmap to right order
644 * This function should be returned with success, otherwise BUG
646 static void get_new_segment(struct f2fs_sb_info *sbi,
647 unsigned int *newseg, bool new_sec, int dir)
649 struct free_segmap_info *free_i = FREE_I(sbi);
650 unsigned int segno, secno, zoneno;
651 unsigned int total_zones = TOTAL_SECS(sbi) / sbi->secs_per_zone;
652 unsigned int hint = *newseg / sbi->segs_per_sec;
653 unsigned int old_zoneno = GET_ZONENO_FROM_SEGNO(sbi, *newseg);
654 unsigned int left_start = hint;
659 write_lock(&free_i->segmap_lock);
661 if (!new_sec && ((*newseg + 1) % sbi->segs_per_sec)) {
662 segno = find_next_zero_bit(free_i->free_segmap,
663 TOTAL_SEGS(sbi), *newseg + 1);
664 if (segno - *newseg < sbi->segs_per_sec -
665 (*newseg % sbi->segs_per_sec))
669 secno = find_next_zero_bit(free_i->free_secmap, TOTAL_SECS(sbi), hint);
670 if (secno >= TOTAL_SECS(sbi)) {
671 if (dir == ALLOC_RIGHT) {
672 secno = find_next_zero_bit(free_i->free_secmap,
674 f2fs_bug_on(secno >= TOTAL_SECS(sbi));
677 left_start = hint - 1;
683 while (test_bit(left_start, free_i->free_secmap)) {
684 if (left_start > 0) {
688 left_start = find_next_zero_bit(free_i->free_secmap,
690 f2fs_bug_on(left_start >= TOTAL_SECS(sbi));
696 segno = secno * sbi->segs_per_sec;
697 zoneno = secno / sbi->secs_per_zone;
699 /* give up on finding another zone */
702 if (sbi->secs_per_zone == 1)
704 if (zoneno == old_zoneno)
706 if (dir == ALLOC_LEFT) {
707 if (!go_left && zoneno + 1 >= total_zones)
709 if (go_left && zoneno == 0)
712 for (i = 0; i < NR_CURSEG_TYPE; i++)
713 if (CURSEG_I(sbi, i)->zone == zoneno)
716 if (i < NR_CURSEG_TYPE) {
717 /* zone is in user, try another */
719 hint = zoneno * sbi->secs_per_zone - 1;
720 else if (zoneno + 1 >= total_zones)
723 hint = (zoneno + 1) * sbi->secs_per_zone;
725 goto find_other_zone;
728 /* set it as dirty segment in free segmap */
729 f2fs_bug_on(test_bit(segno, free_i->free_segmap));
730 __set_inuse(sbi, segno);
732 write_unlock(&free_i->segmap_lock);
735 static void reset_curseg(struct f2fs_sb_info *sbi, int type, int modified)
737 struct curseg_info *curseg = CURSEG_I(sbi, type);
738 struct summary_footer *sum_footer;
740 curseg->segno = curseg->next_segno;
741 curseg->zone = GET_ZONENO_FROM_SEGNO(sbi, curseg->segno);
742 curseg->next_blkoff = 0;
743 curseg->next_segno = NULL_SEGNO;
745 sum_footer = &(curseg->sum_blk->footer);
746 memset(sum_footer, 0, sizeof(struct summary_footer));
747 if (IS_DATASEG(type))
748 SET_SUM_TYPE(sum_footer, SUM_TYPE_DATA);
749 if (IS_NODESEG(type))
750 SET_SUM_TYPE(sum_footer, SUM_TYPE_NODE);
751 __set_sit_entry_type(sbi, type, curseg->segno, modified);
755 * Allocate a current working segment.
756 * This function always allocates a free segment in LFS manner.
758 static void new_curseg(struct f2fs_sb_info *sbi, int type, bool new_sec)
760 struct curseg_info *curseg = CURSEG_I(sbi, type);
761 unsigned int segno = curseg->segno;
762 int dir = ALLOC_LEFT;
764 write_sum_page(sbi, curseg->sum_blk,
765 GET_SUM_BLOCK(sbi, segno));
766 if (type == CURSEG_WARM_DATA || type == CURSEG_COLD_DATA)
769 if (test_opt(sbi, NOHEAP))
772 get_new_segment(sbi, &segno, new_sec, dir);
773 curseg->next_segno = segno;
774 reset_curseg(sbi, type, 1);
775 curseg->alloc_type = LFS;
778 static void __next_free_blkoff(struct f2fs_sb_info *sbi,
779 struct curseg_info *seg, block_t start)
781 struct seg_entry *se = get_seg_entry(sbi, seg->segno);
782 int entries = SIT_VBLOCK_MAP_SIZE / sizeof(unsigned long);
783 unsigned long target_map[entries];
784 unsigned long *ckpt_map = (unsigned long *)se->ckpt_valid_map;
785 unsigned long *cur_map = (unsigned long *)se->cur_valid_map;
788 for (i = 0; i < entries; i++)
789 target_map[i] = ckpt_map[i] | cur_map[i];
791 pos = __find_rev_next_zero_bit(target_map, sbi->blocks_per_seg, start);
793 seg->next_blkoff = pos;
797 * If a segment is written by LFS manner, next block offset is just obtained
798 * by increasing the current block offset. However, if a segment is written by
799 * SSR manner, next block offset obtained by calling __next_free_blkoff
801 static void __refresh_next_blkoff(struct f2fs_sb_info *sbi,
802 struct curseg_info *seg)
804 if (seg->alloc_type == SSR)
805 __next_free_blkoff(sbi, seg, seg->next_blkoff + 1);
811 * This function always allocates a used segment (from dirty seglist) by SSR
812 * manner, so it should recover the existing segment information of valid blocks
814 static void change_curseg(struct f2fs_sb_info *sbi, int type, bool reuse)
816 struct dirty_seglist_info *dirty_i = DIRTY_I(sbi);
817 struct curseg_info *curseg = CURSEG_I(sbi, type);
818 unsigned int new_segno = curseg->next_segno;
819 struct f2fs_summary_block *sum_node;
820 struct page *sum_page;
822 write_sum_page(sbi, curseg->sum_blk,
823 GET_SUM_BLOCK(sbi, curseg->segno));
824 __set_test_and_inuse(sbi, new_segno);
826 mutex_lock(&dirty_i->seglist_lock);
827 __remove_dirty_segment(sbi, new_segno, PRE);
828 __remove_dirty_segment(sbi, new_segno, DIRTY);
829 mutex_unlock(&dirty_i->seglist_lock);
831 reset_curseg(sbi, type, 1);
832 curseg->alloc_type = SSR;
833 __next_free_blkoff(sbi, curseg, 0);
836 sum_page = get_sum_page(sbi, new_segno);
837 sum_node = (struct f2fs_summary_block *)page_address(sum_page);
838 memcpy(curseg->sum_blk, sum_node, SUM_ENTRY_SIZE);
839 f2fs_put_page(sum_page, 1);
843 static int get_ssr_segment(struct f2fs_sb_info *sbi, int type)
845 struct curseg_info *curseg = CURSEG_I(sbi, type);
846 const struct victim_selection *v_ops = DIRTY_I(sbi)->v_ops;
848 if (IS_NODESEG(type) || !has_not_enough_free_secs(sbi, 0))
849 return v_ops->get_victim(sbi,
850 &(curseg)->next_segno, BG_GC, type, SSR);
852 /* For data segments, let's do SSR more intensively */
853 for (; type >= CURSEG_HOT_DATA; type--)
854 if (v_ops->get_victim(sbi, &(curseg)->next_segno,
861 * flush out current segment and replace it with new segment
862 * This function should be returned with success, otherwise BUG
864 static void allocate_segment_by_default(struct f2fs_sb_info *sbi,
865 int type, bool force)
867 struct curseg_info *curseg = CURSEG_I(sbi, type);
870 new_curseg(sbi, type, true);
871 else if (type == CURSEG_WARM_NODE)
872 new_curseg(sbi, type, false);
873 else if (curseg->alloc_type == LFS && is_next_segment_free(sbi, type))
874 new_curseg(sbi, type, false);
875 else if (need_SSR(sbi) && get_ssr_segment(sbi, type))
876 change_curseg(sbi, type, true);
878 new_curseg(sbi, type, false);
880 stat_inc_seg_type(sbi, curseg);
883 void allocate_new_segments(struct f2fs_sb_info *sbi)
885 struct curseg_info *curseg;
886 unsigned int old_curseg;
889 for (i = CURSEG_HOT_DATA; i <= CURSEG_COLD_DATA; i++) {
890 curseg = CURSEG_I(sbi, i);
891 old_curseg = curseg->segno;
892 SIT_I(sbi)->s_ops->allocate_segment(sbi, i, true);
893 locate_dirty_segment(sbi, old_curseg);
897 static const struct segment_allocation default_salloc_ops = {
898 .allocate_segment = allocate_segment_by_default,
901 static bool __has_curseg_space(struct f2fs_sb_info *sbi, int type)
903 struct curseg_info *curseg = CURSEG_I(sbi, type);
904 if (curseg->next_blkoff < sbi->blocks_per_seg)
909 static int __get_segment_type_2(struct page *page, enum page_type p_type)
912 return CURSEG_HOT_DATA;
914 return CURSEG_HOT_NODE;
917 static int __get_segment_type_4(struct page *page, enum page_type p_type)
919 if (p_type == DATA) {
920 struct inode *inode = page->mapping->host;
922 if (S_ISDIR(inode->i_mode))
923 return CURSEG_HOT_DATA;
925 return CURSEG_COLD_DATA;
927 if (IS_DNODE(page) && !is_cold_node(page))
928 return CURSEG_HOT_NODE;
930 return CURSEG_COLD_NODE;
934 static int __get_segment_type_6(struct page *page, enum page_type p_type)
936 if (p_type == DATA) {
937 struct inode *inode = page->mapping->host;
939 if (S_ISDIR(inode->i_mode))
940 return CURSEG_HOT_DATA;
941 else if (is_cold_data(page) || file_is_cold(inode))
942 return CURSEG_COLD_DATA;
944 return CURSEG_WARM_DATA;
947 return is_cold_node(page) ? CURSEG_WARM_NODE :
950 return CURSEG_COLD_NODE;
954 static int __get_segment_type(struct page *page, enum page_type p_type)
956 struct f2fs_sb_info *sbi = F2FS_SB(page->mapping->host->i_sb);
957 switch (sbi->active_logs) {
959 return __get_segment_type_2(page, p_type);
961 return __get_segment_type_4(page, p_type);
963 /* NR_CURSEG_TYPE(6) logs by default */
964 f2fs_bug_on(sbi->active_logs != NR_CURSEG_TYPE);
965 return __get_segment_type_6(page, p_type);
968 void allocate_data_block(struct f2fs_sb_info *sbi, struct page *page,
969 block_t old_blkaddr, block_t *new_blkaddr,
970 struct f2fs_summary *sum, int type)
972 struct sit_info *sit_i = SIT_I(sbi);
973 struct curseg_info *curseg;
975 curseg = CURSEG_I(sbi, type);
977 mutex_lock(&curseg->curseg_mutex);
979 *new_blkaddr = NEXT_FREE_BLKADDR(sbi, curseg);
982 * __add_sum_entry should be resided under the curseg_mutex
983 * because, this function updates a summary entry in the
984 * current summary block.
986 __add_sum_entry(sbi, type, sum);
988 mutex_lock(&sit_i->sentry_lock);
989 __refresh_next_blkoff(sbi, curseg);
991 stat_inc_block_count(sbi, curseg);
993 if (!__has_curseg_space(sbi, type))
994 sit_i->s_ops->allocate_segment(sbi, type, false);
996 * SIT information should be updated before segment allocation,
997 * since SSR needs latest valid block information.
999 refresh_sit_entry(sbi, old_blkaddr, *new_blkaddr);
1001 mutex_unlock(&sit_i->sentry_lock);
1003 if (page && IS_NODESEG(type))
1004 fill_node_footer_blkaddr(page, NEXT_FREE_BLKADDR(sbi, curseg));
1006 mutex_unlock(&curseg->curseg_mutex);
1009 static void do_write_page(struct f2fs_sb_info *sbi, struct page *page,
1010 block_t old_blkaddr, block_t *new_blkaddr,
1011 struct f2fs_summary *sum, struct f2fs_io_info *fio)
1013 int type = __get_segment_type(page, fio->type);
1015 allocate_data_block(sbi, page, old_blkaddr, new_blkaddr, sum, type);
1017 /* writeout dirty page into bdev */
1018 f2fs_submit_page_mbio(sbi, page, *new_blkaddr, fio);
1021 void write_meta_page(struct f2fs_sb_info *sbi, struct page *page)
1023 struct f2fs_io_info fio = {
1025 .rw = WRITE_SYNC | REQ_META | REQ_PRIO
1028 set_page_writeback(page);
1029 f2fs_submit_page_mbio(sbi, page, page->index, &fio);
1032 void write_node_page(struct f2fs_sb_info *sbi, struct page *page,
1033 struct f2fs_io_info *fio,
1034 unsigned int nid, block_t old_blkaddr, block_t *new_blkaddr)
1036 struct f2fs_summary sum;
1037 set_summary(&sum, nid, 0, 0);
1038 do_write_page(sbi, page, old_blkaddr, new_blkaddr, &sum, fio);
1041 void write_data_page(struct page *page, struct dnode_of_data *dn,
1042 block_t *new_blkaddr, struct f2fs_io_info *fio)
1044 struct f2fs_sb_info *sbi = F2FS_SB(dn->inode->i_sb);
1045 struct f2fs_summary sum;
1046 struct node_info ni;
1048 f2fs_bug_on(dn->data_blkaddr == NULL_ADDR);
1049 get_node_info(sbi, dn->nid, &ni);
1050 set_summary(&sum, dn->nid, dn->ofs_in_node, ni.version);
1052 do_write_page(sbi, page, dn->data_blkaddr, new_blkaddr, &sum, fio);
1055 void rewrite_data_page(struct page *page, block_t old_blkaddr,
1056 struct f2fs_io_info *fio)
1058 struct inode *inode = page->mapping->host;
1059 struct f2fs_sb_info *sbi = F2FS_SB(inode->i_sb);
1060 f2fs_submit_page_mbio(sbi, page, old_blkaddr, fio);
1063 void recover_data_page(struct f2fs_sb_info *sbi,
1064 struct page *page, struct f2fs_summary *sum,
1065 block_t old_blkaddr, block_t new_blkaddr)
1067 struct sit_info *sit_i = SIT_I(sbi);
1068 struct curseg_info *curseg;
1069 unsigned int segno, old_cursegno;
1070 struct seg_entry *se;
1073 segno = GET_SEGNO(sbi, new_blkaddr);
1074 se = get_seg_entry(sbi, segno);
1077 if (se->valid_blocks == 0 && !IS_CURSEG(sbi, segno)) {
1078 if (old_blkaddr == NULL_ADDR)
1079 type = CURSEG_COLD_DATA;
1081 type = CURSEG_WARM_DATA;
1083 curseg = CURSEG_I(sbi, type);
1085 mutex_lock(&curseg->curseg_mutex);
1086 mutex_lock(&sit_i->sentry_lock);
1088 old_cursegno = curseg->segno;
1090 /* change the current segment */
1091 if (segno != curseg->segno) {
1092 curseg->next_segno = segno;
1093 change_curseg(sbi, type, true);
1096 curseg->next_blkoff = GET_BLKOFF_FROM_SEG0(sbi, new_blkaddr);
1097 __add_sum_entry(sbi, type, sum);
1099 refresh_sit_entry(sbi, old_blkaddr, new_blkaddr);
1100 locate_dirty_segment(sbi, old_cursegno);
1102 mutex_unlock(&sit_i->sentry_lock);
1103 mutex_unlock(&curseg->curseg_mutex);
1106 void rewrite_node_page(struct f2fs_sb_info *sbi,
1107 struct page *page, struct f2fs_summary *sum,
1108 block_t old_blkaddr, block_t new_blkaddr)
1110 struct sit_info *sit_i = SIT_I(sbi);
1111 int type = CURSEG_WARM_NODE;
1112 struct curseg_info *curseg;
1113 unsigned int segno, old_cursegno;
1114 block_t next_blkaddr = next_blkaddr_of_node(page);
1115 unsigned int next_segno = GET_SEGNO(sbi, next_blkaddr);
1116 struct f2fs_io_info fio = {
1121 curseg = CURSEG_I(sbi, type);
1123 mutex_lock(&curseg->curseg_mutex);
1124 mutex_lock(&sit_i->sentry_lock);
1126 segno = GET_SEGNO(sbi, new_blkaddr);
1127 old_cursegno = curseg->segno;
1129 /* change the current segment */
1130 if (segno != curseg->segno) {
1131 curseg->next_segno = segno;
1132 change_curseg(sbi, type, true);
1134 curseg->next_blkoff = GET_BLKOFF_FROM_SEG0(sbi, new_blkaddr);
1135 __add_sum_entry(sbi, type, sum);
1137 /* change the current log to the next block addr in advance */
1138 if (next_segno != segno) {
1139 curseg->next_segno = next_segno;
1140 change_curseg(sbi, type, true);
1142 curseg->next_blkoff = GET_BLKOFF_FROM_SEG0(sbi, next_blkaddr);
1144 /* rewrite node page */
1145 set_page_writeback(page);
1146 f2fs_submit_page_mbio(sbi, page, new_blkaddr, &fio);
1147 f2fs_submit_merged_bio(sbi, NODE, WRITE);
1148 refresh_sit_entry(sbi, old_blkaddr, new_blkaddr);
1149 locate_dirty_segment(sbi, old_cursegno);
1151 mutex_unlock(&sit_i->sentry_lock);
1152 mutex_unlock(&curseg->curseg_mutex);
1155 static inline bool is_merged_page(struct f2fs_sb_info *sbi,
1156 struct page *page, enum page_type type)
1158 enum page_type btype = PAGE_TYPE_OF_BIO(type);
1159 struct f2fs_bio_info *io = &sbi->write_io[btype];
1160 struct bio_vec *bvec;
1163 down_read(&io->io_rwsem);
1167 bio_for_each_segment_all(bvec, io->bio, i) {
1168 if (page == bvec->bv_page) {
1169 up_read(&io->io_rwsem);
1175 up_read(&io->io_rwsem);
1179 void f2fs_wait_on_page_writeback(struct page *page,
1180 enum page_type type)
1182 struct f2fs_sb_info *sbi = F2FS_SB(page->mapping->host->i_sb);
1183 if (PageWriteback(page)) {
1184 if (is_merged_page(sbi, page, type))
1185 f2fs_submit_merged_bio(sbi, type, WRITE);
1186 wait_on_page_writeback(page);
1190 static int read_compacted_summaries(struct f2fs_sb_info *sbi)
1192 struct f2fs_checkpoint *ckpt = F2FS_CKPT(sbi);
1193 struct curseg_info *seg_i;
1194 unsigned char *kaddr;
1199 start = start_sum_block(sbi);
1201 page = get_meta_page(sbi, start++);
1202 kaddr = (unsigned char *)page_address(page);
1204 /* Step 1: restore nat cache */
1205 seg_i = CURSEG_I(sbi, CURSEG_HOT_DATA);
1206 memcpy(&seg_i->sum_blk->n_nats, kaddr, SUM_JOURNAL_SIZE);
1208 /* Step 2: restore sit cache */
1209 seg_i = CURSEG_I(sbi, CURSEG_COLD_DATA);
1210 memcpy(&seg_i->sum_blk->n_sits, kaddr + SUM_JOURNAL_SIZE,
1212 offset = 2 * SUM_JOURNAL_SIZE;
1214 /* Step 3: restore summary entries */
1215 for (i = CURSEG_HOT_DATA; i <= CURSEG_COLD_DATA; i++) {
1216 unsigned short blk_off;
1219 seg_i = CURSEG_I(sbi, i);
1220 segno = le32_to_cpu(ckpt->cur_data_segno[i]);
1221 blk_off = le16_to_cpu(ckpt->cur_data_blkoff[i]);
1222 seg_i->next_segno = segno;
1223 reset_curseg(sbi, i, 0);
1224 seg_i->alloc_type = ckpt->alloc_type[i];
1225 seg_i->next_blkoff = blk_off;
1227 if (seg_i->alloc_type == SSR)
1228 blk_off = sbi->blocks_per_seg;
1230 for (j = 0; j < blk_off; j++) {
1231 struct f2fs_summary *s;
1232 s = (struct f2fs_summary *)(kaddr + offset);
1233 seg_i->sum_blk->entries[j] = *s;
1234 offset += SUMMARY_SIZE;
1235 if (offset + SUMMARY_SIZE <= PAGE_CACHE_SIZE -
1239 f2fs_put_page(page, 1);
1242 page = get_meta_page(sbi, start++);
1243 kaddr = (unsigned char *)page_address(page);
1247 f2fs_put_page(page, 1);
1251 static int read_normal_summaries(struct f2fs_sb_info *sbi, int type)
1253 struct f2fs_checkpoint *ckpt = F2FS_CKPT(sbi);
1254 struct f2fs_summary_block *sum;
1255 struct curseg_info *curseg;
1257 unsigned short blk_off;
1258 unsigned int segno = 0;
1259 block_t blk_addr = 0;
1261 /* get segment number and block addr */
1262 if (IS_DATASEG(type)) {
1263 segno = le32_to_cpu(ckpt->cur_data_segno[type]);
1264 blk_off = le16_to_cpu(ckpt->cur_data_blkoff[type -
1266 if (is_set_ckpt_flags(ckpt, CP_UMOUNT_FLAG))
1267 blk_addr = sum_blk_addr(sbi, NR_CURSEG_TYPE, type);
1269 blk_addr = sum_blk_addr(sbi, NR_CURSEG_DATA_TYPE, type);
1271 segno = le32_to_cpu(ckpt->cur_node_segno[type -
1273 blk_off = le16_to_cpu(ckpt->cur_node_blkoff[type -
1275 if (is_set_ckpt_flags(ckpt, CP_UMOUNT_FLAG))
1276 blk_addr = sum_blk_addr(sbi, NR_CURSEG_NODE_TYPE,
1277 type - CURSEG_HOT_NODE);
1279 blk_addr = GET_SUM_BLOCK(sbi, segno);
1282 new = get_meta_page(sbi, blk_addr);
1283 sum = (struct f2fs_summary_block *)page_address(new);
1285 if (IS_NODESEG(type)) {
1286 if (is_set_ckpt_flags(ckpt, CP_UMOUNT_FLAG)) {
1287 struct f2fs_summary *ns = &sum->entries[0];
1289 for (i = 0; i < sbi->blocks_per_seg; i++, ns++) {
1291 ns->ofs_in_node = 0;
1296 err = restore_node_summary(sbi, segno, sum);
1298 f2fs_put_page(new, 1);
1304 /* set uncompleted segment to curseg */
1305 curseg = CURSEG_I(sbi, type);
1306 mutex_lock(&curseg->curseg_mutex);
1307 memcpy(curseg->sum_blk, sum, PAGE_CACHE_SIZE);
1308 curseg->next_segno = segno;
1309 reset_curseg(sbi, type, 0);
1310 curseg->alloc_type = ckpt->alloc_type[type];
1311 curseg->next_blkoff = blk_off;
1312 mutex_unlock(&curseg->curseg_mutex);
1313 f2fs_put_page(new, 1);
1317 static int restore_curseg_summaries(struct f2fs_sb_info *sbi)
1319 int type = CURSEG_HOT_DATA;
1322 if (is_set_ckpt_flags(F2FS_CKPT(sbi), CP_COMPACT_SUM_FLAG)) {
1323 /* restore for compacted data summary */
1324 if (read_compacted_summaries(sbi))
1326 type = CURSEG_HOT_NODE;
1329 for (; type <= CURSEG_COLD_NODE; type++) {
1330 err = read_normal_summaries(sbi, type);
1338 static void write_compacted_summaries(struct f2fs_sb_info *sbi, block_t blkaddr)
1341 unsigned char *kaddr;
1342 struct f2fs_summary *summary;
1343 struct curseg_info *seg_i;
1344 int written_size = 0;
1347 page = grab_meta_page(sbi, blkaddr++);
1348 kaddr = (unsigned char *)page_address(page);
1350 /* Step 1: write nat cache */
1351 seg_i = CURSEG_I(sbi, CURSEG_HOT_DATA);
1352 memcpy(kaddr, &seg_i->sum_blk->n_nats, SUM_JOURNAL_SIZE);
1353 written_size += SUM_JOURNAL_SIZE;
1355 /* Step 2: write sit cache */
1356 seg_i = CURSEG_I(sbi, CURSEG_COLD_DATA);
1357 memcpy(kaddr + written_size, &seg_i->sum_blk->n_sits,
1359 written_size += SUM_JOURNAL_SIZE;
1361 /* Step 3: write summary entries */
1362 for (i = CURSEG_HOT_DATA; i <= CURSEG_COLD_DATA; i++) {
1363 unsigned short blkoff;
1364 seg_i = CURSEG_I(sbi, i);
1365 if (sbi->ckpt->alloc_type[i] == SSR)
1366 blkoff = sbi->blocks_per_seg;
1368 blkoff = curseg_blkoff(sbi, i);
1370 for (j = 0; j < blkoff; j++) {
1372 page = grab_meta_page(sbi, blkaddr++);
1373 kaddr = (unsigned char *)page_address(page);
1376 summary = (struct f2fs_summary *)(kaddr + written_size);
1377 *summary = seg_i->sum_blk->entries[j];
1378 written_size += SUMMARY_SIZE;
1380 if (written_size + SUMMARY_SIZE <= PAGE_CACHE_SIZE -
1384 set_page_dirty(page);
1385 f2fs_put_page(page, 1);
1390 set_page_dirty(page);
1391 f2fs_put_page(page, 1);
1395 static void write_normal_summaries(struct f2fs_sb_info *sbi,
1396 block_t blkaddr, int type)
1399 if (IS_DATASEG(type))
1400 end = type + NR_CURSEG_DATA_TYPE;
1402 end = type + NR_CURSEG_NODE_TYPE;
1404 for (i = type; i < end; i++) {
1405 struct curseg_info *sum = CURSEG_I(sbi, i);
1406 mutex_lock(&sum->curseg_mutex);
1407 write_sum_page(sbi, sum->sum_blk, blkaddr + (i - type));
1408 mutex_unlock(&sum->curseg_mutex);
1412 void write_data_summaries(struct f2fs_sb_info *sbi, block_t start_blk)
1414 if (is_set_ckpt_flags(F2FS_CKPT(sbi), CP_COMPACT_SUM_FLAG))
1415 write_compacted_summaries(sbi, start_blk);
1417 write_normal_summaries(sbi, start_blk, CURSEG_HOT_DATA);
1420 void write_node_summaries(struct f2fs_sb_info *sbi, block_t start_blk)
1422 if (is_set_ckpt_flags(F2FS_CKPT(sbi), CP_UMOUNT_FLAG))
1423 write_normal_summaries(sbi, start_blk, CURSEG_HOT_NODE);
1426 int lookup_journal_in_cursum(struct f2fs_summary_block *sum, int type,
1427 unsigned int val, int alloc)
1431 if (type == NAT_JOURNAL) {
1432 for (i = 0; i < nats_in_cursum(sum); i++) {
1433 if (le32_to_cpu(nid_in_journal(sum, i)) == val)
1436 if (alloc && nats_in_cursum(sum) < NAT_JOURNAL_ENTRIES)
1437 return update_nats_in_cursum(sum, 1);
1438 } else if (type == SIT_JOURNAL) {
1439 for (i = 0; i < sits_in_cursum(sum); i++)
1440 if (le32_to_cpu(segno_in_journal(sum, i)) == val)
1442 if (alloc && sits_in_cursum(sum) < SIT_JOURNAL_ENTRIES)
1443 return update_sits_in_cursum(sum, 1);
1448 static struct page *get_current_sit_page(struct f2fs_sb_info *sbi,
1451 struct sit_info *sit_i = SIT_I(sbi);
1452 unsigned int offset = SIT_BLOCK_OFFSET(sit_i, segno);
1453 block_t blk_addr = sit_i->sit_base_addr + offset;
1455 check_seg_range(sbi, segno);
1457 /* calculate sit block address */
1458 if (f2fs_test_bit(offset, sit_i->sit_bitmap))
1459 blk_addr += sit_i->sit_blocks;
1461 return get_meta_page(sbi, blk_addr);
1464 static struct page *get_next_sit_page(struct f2fs_sb_info *sbi,
1467 struct sit_info *sit_i = SIT_I(sbi);
1468 struct page *src_page, *dst_page;
1469 pgoff_t src_off, dst_off;
1470 void *src_addr, *dst_addr;
1472 src_off = current_sit_addr(sbi, start);
1473 dst_off = next_sit_addr(sbi, src_off);
1475 /* get current sit block page without lock */
1476 src_page = get_meta_page(sbi, src_off);
1477 dst_page = grab_meta_page(sbi, dst_off);
1478 f2fs_bug_on(PageDirty(src_page));
1480 src_addr = page_address(src_page);
1481 dst_addr = page_address(dst_page);
1482 memcpy(dst_addr, src_addr, PAGE_CACHE_SIZE);
1484 set_page_dirty(dst_page);
1485 f2fs_put_page(src_page, 1);
1487 set_to_next_sit(sit_i, start);
1492 static bool flush_sits_in_journal(struct f2fs_sb_info *sbi)
1494 struct curseg_info *curseg = CURSEG_I(sbi, CURSEG_COLD_DATA);
1495 struct f2fs_summary_block *sum = curseg->sum_blk;
1499 * If the journal area in the current summary is full of sit entries,
1500 * all the sit entries will be flushed. Otherwise the sit entries
1501 * are not able to replace with newly hot sit entries.
1503 if (sits_in_cursum(sum) >= SIT_JOURNAL_ENTRIES) {
1504 for (i = sits_in_cursum(sum) - 1; i >= 0; i--) {
1506 segno = le32_to_cpu(segno_in_journal(sum, i));
1507 __mark_sit_entry_dirty(sbi, segno);
1509 update_sits_in_cursum(sum, -sits_in_cursum(sum));
1516 * CP calls this function, which flushes SIT entries including sit_journal,
1517 * and moves prefree segs to free segs.
1519 void flush_sit_entries(struct f2fs_sb_info *sbi)
1521 struct sit_info *sit_i = SIT_I(sbi);
1522 unsigned long *bitmap = sit_i->dirty_sentries_bitmap;
1523 struct curseg_info *curseg = CURSEG_I(sbi, CURSEG_COLD_DATA);
1524 struct f2fs_summary_block *sum = curseg->sum_blk;
1525 unsigned long nsegs = TOTAL_SEGS(sbi);
1526 struct page *page = NULL;
1527 struct f2fs_sit_block *raw_sit = NULL;
1528 unsigned int start = 0, end = 0;
1532 mutex_lock(&curseg->curseg_mutex);
1533 mutex_lock(&sit_i->sentry_lock);
1536 * "flushed" indicates whether sit entries in journal are flushed
1537 * to the SIT area or not.
1539 flushed = flush_sits_in_journal(sbi);
1541 for_each_set_bit(segno, bitmap, nsegs) {
1542 struct seg_entry *se = get_seg_entry(sbi, segno);
1543 int sit_offset, offset;
1545 sit_offset = SIT_ENTRY_OFFSET(sit_i, segno);
1547 /* add discard candidates */
1548 if (SM_I(sbi)->nr_discards < SM_I(sbi)->max_discards)
1549 add_discard_addrs(sbi, segno, se);
1554 offset = lookup_journal_in_cursum(sum, SIT_JOURNAL, segno, 1);
1556 segno_in_journal(sum, offset) = cpu_to_le32(segno);
1557 seg_info_to_raw_sit(se, &sit_in_journal(sum, offset));
1561 if (!page || (start > segno) || (segno > end)) {
1563 f2fs_put_page(page, 1);
1567 start = START_SEGNO(sit_i, segno);
1568 end = start + SIT_ENTRY_PER_BLOCK - 1;
1570 /* read sit block that will be updated */
1571 page = get_next_sit_page(sbi, start);
1572 raw_sit = page_address(page);
1575 /* udpate entry in SIT block */
1576 seg_info_to_raw_sit(se, &raw_sit->entries[sit_offset]);
1578 __clear_bit(segno, bitmap);
1579 sit_i->dirty_sentries--;
1581 mutex_unlock(&sit_i->sentry_lock);
1582 mutex_unlock(&curseg->curseg_mutex);
1584 /* writeout last modified SIT block */
1585 f2fs_put_page(page, 1);
1587 set_prefree_as_free_segments(sbi);
1590 static int build_sit_info(struct f2fs_sb_info *sbi)
1592 struct f2fs_super_block *raw_super = F2FS_RAW_SUPER(sbi);
1593 struct f2fs_checkpoint *ckpt = F2FS_CKPT(sbi);
1594 struct sit_info *sit_i;
1595 unsigned int sit_segs, start;
1596 char *src_bitmap, *dst_bitmap;
1597 unsigned int bitmap_size;
1599 /* allocate memory for SIT information */
1600 sit_i = kzalloc(sizeof(struct sit_info), GFP_KERNEL);
1604 SM_I(sbi)->sit_info = sit_i;
1606 sit_i->sentries = vzalloc(TOTAL_SEGS(sbi) * sizeof(struct seg_entry));
1607 if (!sit_i->sentries)
1610 bitmap_size = f2fs_bitmap_size(TOTAL_SEGS(sbi));
1611 sit_i->dirty_sentries_bitmap = kzalloc(bitmap_size, GFP_KERNEL);
1612 if (!sit_i->dirty_sentries_bitmap)
1615 for (start = 0; start < TOTAL_SEGS(sbi); start++) {
1616 sit_i->sentries[start].cur_valid_map
1617 = kzalloc(SIT_VBLOCK_MAP_SIZE, GFP_KERNEL);
1618 sit_i->sentries[start].ckpt_valid_map
1619 = kzalloc(SIT_VBLOCK_MAP_SIZE, GFP_KERNEL);
1620 if (!sit_i->sentries[start].cur_valid_map
1621 || !sit_i->sentries[start].ckpt_valid_map)
1625 if (sbi->segs_per_sec > 1) {
1626 sit_i->sec_entries = vzalloc(TOTAL_SECS(sbi) *
1627 sizeof(struct sec_entry));
1628 if (!sit_i->sec_entries)
1632 /* get information related with SIT */
1633 sit_segs = le32_to_cpu(raw_super->segment_count_sit) >> 1;
1635 /* setup SIT bitmap from ckeckpoint pack */
1636 bitmap_size = __bitmap_size(sbi, SIT_BITMAP);
1637 src_bitmap = __bitmap_ptr(sbi, SIT_BITMAP);
1639 dst_bitmap = kmemdup(src_bitmap, bitmap_size, GFP_KERNEL);
1643 /* init SIT information */
1644 sit_i->s_ops = &default_salloc_ops;
1646 sit_i->sit_base_addr = le32_to_cpu(raw_super->sit_blkaddr);
1647 sit_i->sit_blocks = sit_segs << sbi->log_blocks_per_seg;
1648 sit_i->written_valid_blocks = le64_to_cpu(ckpt->valid_block_count);
1649 sit_i->sit_bitmap = dst_bitmap;
1650 sit_i->bitmap_size = bitmap_size;
1651 sit_i->dirty_sentries = 0;
1652 sit_i->sents_per_block = SIT_ENTRY_PER_BLOCK;
1653 sit_i->elapsed_time = le64_to_cpu(sbi->ckpt->elapsed_time);
1654 sit_i->mounted_time = CURRENT_TIME_SEC.tv_sec;
1655 mutex_init(&sit_i->sentry_lock);
1659 static int build_free_segmap(struct f2fs_sb_info *sbi)
1661 struct f2fs_sm_info *sm_info = SM_I(sbi);
1662 struct free_segmap_info *free_i;
1663 unsigned int bitmap_size, sec_bitmap_size;
1665 /* allocate memory for free segmap information */
1666 free_i = kzalloc(sizeof(struct free_segmap_info), GFP_KERNEL);
1670 SM_I(sbi)->free_info = free_i;
1672 bitmap_size = f2fs_bitmap_size(TOTAL_SEGS(sbi));
1673 free_i->free_segmap = kmalloc(bitmap_size, GFP_KERNEL);
1674 if (!free_i->free_segmap)
1677 sec_bitmap_size = f2fs_bitmap_size(TOTAL_SECS(sbi));
1678 free_i->free_secmap = kmalloc(sec_bitmap_size, GFP_KERNEL);
1679 if (!free_i->free_secmap)
1682 /* set all segments as dirty temporarily */
1683 memset(free_i->free_segmap, 0xff, bitmap_size);
1684 memset(free_i->free_secmap, 0xff, sec_bitmap_size);
1686 /* init free segmap information */
1687 free_i->start_segno =
1688 (unsigned int) GET_SEGNO_FROM_SEG0(sbi, sm_info->main_blkaddr);
1689 free_i->free_segments = 0;
1690 free_i->free_sections = 0;
1691 rwlock_init(&free_i->segmap_lock);
1695 static int build_curseg(struct f2fs_sb_info *sbi)
1697 struct curseg_info *array;
1700 array = kcalloc(NR_CURSEG_TYPE, sizeof(*array), GFP_KERNEL);
1704 SM_I(sbi)->curseg_array = array;
1706 for (i = 0; i < NR_CURSEG_TYPE; i++) {
1707 mutex_init(&array[i].curseg_mutex);
1708 array[i].sum_blk = kzalloc(PAGE_CACHE_SIZE, GFP_KERNEL);
1709 if (!array[i].sum_blk)
1711 array[i].segno = NULL_SEGNO;
1712 array[i].next_blkoff = 0;
1714 return restore_curseg_summaries(sbi);
1717 static void build_sit_entries(struct f2fs_sb_info *sbi)
1719 struct sit_info *sit_i = SIT_I(sbi);
1720 struct curseg_info *curseg = CURSEG_I(sbi, CURSEG_COLD_DATA);
1721 struct f2fs_summary_block *sum = curseg->sum_blk;
1722 int sit_blk_cnt = SIT_BLK_CNT(sbi);
1723 unsigned int i, start, end;
1724 unsigned int readed, start_blk = 0;
1725 int nrpages = MAX_BIO_BLOCKS(max_hw_blocks(sbi));
1728 readed = ra_meta_pages(sbi, start_blk, nrpages, META_SIT);
1730 start = start_blk * sit_i->sents_per_block;
1731 end = (start_blk + readed) * sit_i->sents_per_block;
1733 for (; start < end && start < TOTAL_SEGS(sbi); start++) {
1734 struct seg_entry *se = &sit_i->sentries[start];
1735 struct f2fs_sit_block *sit_blk;
1736 struct f2fs_sit_entry sit;
1739 mutex_lock(&curseg->curseg_mutex);
1740 for (i = 0; i < sits_in_cursum(sum); i++) {
1741 if (le32_to_cpu(segno_in_journal(sum, i))
1743 sit = sit_in_journal(sum, i);
1744 mutex_unlock(&curseg->curseg_mutex);
1748 mutex_unlock(&curseg->curseg_mutex);
1750 page = get_current_sit_page(sbi, start);
1751 sit_blk = (struct f2fs_sit_block *)page_address(page);
1752 sit = sit_blk->entries[SIT_ENTRY_OFFSET(sit_i, start)];
1753 f2fs_put_page(page, 1);
1755 check_block_count(sbi, start, &sit);
1756 seg_info_from_raw_sit(se, &sit);
1757 if (sbi->segs_per_sec > 1) {
1758 struct sec_entry *e = get_sec_entry(sbi, start);
1759 e->valid_blocks += se->valid_blocks;
1762 start_blk += readed;
1763 } while (start_blk < sit_blk_cnt);
1766 static void init_free_segmap(struct f2fs_sb_info *sbi)
1771 for (start = 0; start < TOTAL_SEGS(sbi); start++) {
1772 struct seg_entry *sentry = get_seg_entry(sbi, start);
1773 if (!sentry->valid_blocks)
1774 __set_free(sbi, start);
1777 /* set use the current segments */
1778 for (type = CURSEG_HOT_DATA; type <= CURSEG_COLD_NODE; type++) {
1779 struct curseg_info *curseg_t = CURSEG_I(sbi, type);
1780 __set_test_and_inuse(sbi, curseg_t->segno);
1784 static void init_dirty_segmap(struct f2fs_sb_info *sbi)
1786 struct dirty_seglist_info *dirty_i = DIRTY_I(sbi);
1787 struct free_segmap_info *free_i = FREE_I(sbi);
1788 unsigned int segno = 0, offset = 0, total_segs = TOTAL_SEGS(sbi);
1789 unsigned short valid_blocks;
1792 /* find dirty segment based on free segmap */
1793 segno = find_next_inuse(free_i, total_segs, offset);
1794 if (segno >= total_segs)
1797 valid_blocks = get_valid_blocks(sbi, segno, 0);
1798 if (valid_blocks >= sbi->blocks_per_seg || !valid_blocks)
1800 mutex_lock(&dirty_i->seglist_lock);
1801 __locate_dirty_segment(sbi, segno, DIRTY);
1802 mutex_unlock(&dirty_i->seglist_lock);
1806 static int init_victim_secmap(struct f2fs_sb_info *sbi)
1808 struct dirty_seglist_info *dirty_i = DIRTY_I(sbi);
1809 unsigned int bitmap_size = f2fs_bitmap_size(TOTAL_SECS(sbi));
1811 dirty_i->victim_secmap = kzalloc(bitmap_size, GFP_KERNEL);
1812 if (!dirty_i->victim_secmap)
1817 static int build_dirty_segmap(struct f2fs_sb_info *sbi)
1819 struct dirty_seglist_info *dirty_i;
1820 unsigned int bitmap_size, i;
1822 /* allocate memory for dirty segments list information */
1823 dirty_i = kzalloc(sizeof(struct dirty_seglist_info), GFP_KERNEL);
1827 SM_I(sbi)->dirty_info = dirty_i;
1828 mutex_init(&dirty_i->seglist_lock);
1830 bitmap_size = f2fs_bitmap_size(TOTAL_SEGS(sbi));
1832 for (i = 0; i < NR_DIRTY_TYPE; i++) {
1833 dirty_i->dirty_segmap[i] = kzalloc(bitmap_size, GFP_KERNEL);
1834 if (!dirty_i->dirty_segmap[i])
1838 init_dirty_segmap(sbi);
1839 return init_victim_secmap(sbi);
1843 * Update min, max modified time for cost-benefit GC algorithm
1845 static void init_min_max_mtime(struct f2fs_sb_info *sbi)
1847 struct sit_info *sit_i = SIT_I(sbi);
1850 mutex_lock(&sit_i->sentry_lock);
1852 sit_i->min_mtime = LLONG_MAX;
1854 for (segno = 0; segno < TOTAL_SEGS(sbi); segno += sbi->segs_per_sec) {
1856 unsigned long long mtime = 0;
1858 for (i = 0; i < sbi->segs_per_sec; i++)
1859 mtime += get_seg_entry(sbi, segno + i)->mtime;
1861 mtime = div_u64(mtime, sbi->segs_per_sec);
1863 if (sit_i->min_mtime > mtime)
1864 sit_i->min_mtime = mtime;
1866 sit_i->max_mtime = get_mtime(sbi);
1867 mutex_unlock(&sit_i->sentry_lock);
1870 int build_segment_manager(struct f2fs_sb_info *sbi)
1872 struct f2fs_super_block *raw_super = F2FS_RAW_SUPER(sbi);
1873 struct f2fs_checkpoint *ckpt = F2FS_CKPT(sbi);
1874 struct f2fs_sm_info *sm_info;
1877 sm_info = kzalloc(sizeof(struct f2fs_sm_info), GFP_KERNEL);
1882 sbi->sm_info = sm_info;
1883 sm_info->seg0_blkaddr = le32_to_cpu(raw_super->segment0_blkaddr);
1884 sm_info->main_blkaddr = le32_to_cpu(raw_super->main_blkaddr);
1885 sm_info->segment_count = le32_to_cpu(raw_super->segment_count);
1886 sm_info->reserved_segments = le32_to_cpu(ckpt->rsvd_segment_count);
1887 sm_info->ovp_segments = le32_to_cpu(ckpt->overprov_segment_count);
1888 sm_info->main_segments = le32_to_cpu(raw_super->segment_count_main);
1889 sm_info->ssa_blkaddr = le32_to_cpu(raw_super->ssa_blkaddr);
1890 sm_info->rec_prefree_segments = sm_info->main_segments *
1891 DEF_RECLAIM_PREFREE_SEGMENTS / 100;
1892 sm_info->ipu_policy = F2FS_IPU_DISABLE;
1893 sm_info->min_ipu_util = DEF_MIN_IPU_UTIL;
1895 INIT_LIST_HEAD(&sm_info->discard_list);
1896 sm_info->nr_discards = 0;
1897 sm_info->max_discards = 0;
1899 if (test_opt(sbi, FLUSH_MERGE) && !f2fs_readonly(sbi->sb)) {
1900 err = create_flush_cmd_control(sbi);
1905 err = build_sit_info(sbi);
1908 err = build_free_segmap(sbi);
1911 err = build_curseg(sbi);
1915 /* reinit free segmap based on SIT */
1916 build_sit_entries(sbi);
1918 init_free_segmap(sbi);
1919 err = build_dirty_segmap(sbi);
1923 init_min_max_mtime(sbi);
1927 static void discard_dirty_segmap(struct f2fs_sb_info *sbi,
1928 enum dirty_type dirty_type)
1930 struct dirty_seglist_info *dirty_i = DIRTY_I(sbi);
1932 mutex_lock(&dirty_i->seglist_lock);
1933 kfree(dirty_i->dirty_segmap[dirty_type]);
1934 dirty_i->nr_dirty[dirty_type] = 0;
1935 mutex_unlock(&dirty_i->seglist_lock);
1938 static void destroy_victim_secmap(struct f2fs_sb_info *sbi)
1940 struct dirty_seglist_info *dirty_i = DIRTY_I(sbi);
1941 kfree(dirty_i->victim_secmap);
1944 static void destroy_dirty_segmap(struct f2fs_sb_info *sbi)
1946 struct dirty_seglist_info *dirty_i = DIRTY_I(sbi);
1952 /* discard pre-free/dirty segments list */
1953 for (i = 0; i < NR_DIRTY_TYPE; i++)
1954 discard_dirty_segmap(sbi, i);
1956 destroy_victim_secmap(sbi);
1957 SM_I(sbi)->dirty_info = NULL;
1961 static void destroy_curseg(struct f2fs_sb_info *sbi)
1963 struct curseg_info *array = SM_I(sbi)->curseg_array;
1968 SM_I(sbi)->curseg_array = NULL;
1969 for (i = 0; i < NR_CURSEG_TYPE; i++)
1970 kfree(array[i].sum_blk);
1974 static void destroy_free_segmap(struct f2fs_sb_info *sbi)
1976 struct free_segmap_info *free_i = SM_I(sbi)->free_info;
1979 SM_I(sbi)->free_info = NULL;
1980 kfree(free_i->free_segmap);
1981 kfree(free_i->free_secmap);
1985 static void destroy_sit_info(struct f2fs_sb_info *sbi)
1987 struct sit_info *sit_i = SIT_I(sbi);
1993 if (sit_i->sentries) {
1994 for (start = 0; start < TOTAL_SEGS(sbi); start++) {
1995 kfree(sit_i->sentries[start].cur_valid_map);
1996 kfree(sit_i->sentries[start].ckpt_valid_map);
1999 vfree(sit_i->sentries);
2000 vfree(sit_i->sec_entries);
2001 kfree(sit_i->dirty_sentries_bitmap);
2003 SM_I(sbi)->sit_info = NULL;
2004 kfree(sit_i->sit_bitmap);
2008 void destroy_segment_manager(struct f2fs_sb_info *sbi)
2010 struct f2fs_sm_info *sm_info = SM_I(sbi);
2014 destroy_flush_cmd_control(sbi);
2015 destroy_dirty_segmap(sbi);
2016 destroy_curseg(sbi);
2017 destroy_free_segmap(sbi);
2018 destroy_sit_info(sbi);
2019 sbi->sm_info = NULL;
2023 int __init create_segment_manager_caches(void)
2025 discard_entry_slab = f2fs_kmem_cache_create("discard_entry",
2026 sizeof(struct discard_entry));
2027 if (!discard_entry_slab)
2032 void destroy_segment_manager_caches(void)
2034 kmem_cache_destroy(discard_entry_slab);