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 because
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 (unlikely(t >= DIRTY)) {
324 if (!test_and_set_bit(segno, dirty_i->dirty_segmap[t]))
325 dirty_i->nr_dirty[t]++;
329 static void __remove_dirty_segment(struct f2fs_sb_info *sbi, unsigned int segno,
330 enum dirty_type dirty_type)
332 struct dirty_seglist_info *dirty_i = DIRTY_I(sbi);
334 if (test_and_clear_bit(segno, dirty_i->dirty_segmap[dirty_type]))
335 dirty_i->nr_dirty[dirty_type]--;
337 if (dirty_type == DIRTY) {
338 struct seg_entry *sentry = get_seg_entry(sbi, segno);
339 enum dirty_type t = sentry->type;
341 if (test_and_clear_bit(segno, dirty_i->dirty_segmap[t]))
342 dirty_i->nr_dirty[t]--;
344 if (get_valid_blocks(sbi, segno, sbi->segs_per_sec) == 0)
345 clear_bit(GET_SECNO(sbi, segno),
346 dirty_i->victim_secmap);
351 * Should not occur error such as -ENOMEM.
352 * Adding dirty entry into seglist is not critical operation.
353 * If a given segment is one of current working segments, it won't be added.
355 static void locate_dirty_segment(struct f2fs_sb_info *sbi, unsigned int segno)
357 struct dirty_seglist_info *dirty_i = DIRTY_I(sbi);
358 unsigned short valid_blocks;
360 if (segno == NULL_SEGNO || IS_CURSEG(sbi, segno))
363 mutex_lock(&dirty_i->seglist_lock);
365 valid_blocks = get_valid_blocks(sbi, segno, 0);
367 if (valid_blocks == 0) {
368 __locate_dirty_segment(sbi, segno, PRE);
369 __remove_dirty_segment(sbi, segno, DIRTY);
370 } else if (valid_blocks < sbi->blocks_per_seg) {
371 __locate_dirty_segment(sbi, segno, DIRTY);
373 /* Recovery routine with SSR needs this */
374 __remove_dirty_segment(sbi, segno, DIRTY);
377 mutex_unlock(&dirty_i->seglist_lock);
380 static int f2fs_issue_discard(struct f2fs_sb_info *sbi,
381 block_t blkstart, block_t blklen)
383 sector_t start = SECTOR_FROM_BLOCK(sbi, blkstart);
384 sector_t len = SECTOR_FROM_BLOCK(sbi, blklen);
385 trace_f2fs_issue_discard(sbi->sb, blkstart, blklen);
386 return blkdev_issue_discard(sbi->sb->s_bdev, start, len, GFP_NOFS, 0);
389 void discard_next_dnode(struct f2fs_sb_info *sbi, block_t blkaddr)
391 if (f2fs_issue_discard(sbi, blkaddr, 1)) {
392 struct page *page = grab_meta_page(sbi, blkaddr);
393 /* zero-filled page */
394 set_page_dirty(page);
395 f2fs_put_page(page, 1);
399 static void add_discard_addrs(struct f2fs_sb_info *sbi,
400 unsigned int segno, struct seg_entry *se)
402 struct list_head *head = &SM_I(sbi)->discard_list;
403 struct discard_entry *new;
404 int entries = SIT_VBLOCK_MAP_SIZE / sizeof(unsigned long);
405 int max_blocks = sbi->blocks_per_seg;
406 unsigned long *cur_map = (unsigned long *)se->cur_valid_map;
407 unsigned long *ckpt_map = (unsigned long *)se->ckpt_valid_map;
408 unsigned long dmap[entries];
409 unsigned int start = 0, end = -1;
412 if (!test_opt(sbi, DISCARD))
415 /* zero block will be discarded through the prefree list */
416 if (!se->valid_blocks || se->valid_blocks == max_blocks)
419 /* SIT_VBLOCK_MAP_SIZE should be multiple of sizeof(unsigned long) */
420 for (i = 0; i < entries; i++)
421 dmap[i] = (cur_map[i] ^ ckpt_map[i]) & ckpt_map[i];
423 while (SM_I(sbi)->nr_discards <= SM_I(sbi)->max_discards) {
424 start = __find_rev_next_bit(dmap, max_blocks, end + 1);
425 if (start >= max_blocks)
428 end = __find_rev_next_zero_bit(dmap, max_blocks, start + 1);
430 new = f2fs_kmem_cache_alloc(discard_entry_slab, GFP_NOFS);
431 INIT_LIST_HEAD(&new->list);
432 new->blkaddr = START_BLOCK(sbi, segno) + start;
433 new->len = end - start;
435 list_add_tail(&new->list, head);
436 SM_I(sbi)->nr_discards += end - start;
441 * Should call clear_prefree_segments after checkpoint is done.
443 static void set_prefree_as_free_segments(struct f2fs_sb_info *sbi)
445 struct dirty_seglist_info *dirty_i = DIRTY_I(sbi);
447 unsigned int total_segs = TOTAL_SEGS(sbi);
449 mutex_lock(&dirty_i->seglist_lock);
450 for_each_set_bit(segno, dirty_i->dirty_segmap[PRE], total_segs)
451 __set_test_and_free(sbi, segno);
452 mutex_unlock(&dirty_i->seglist_lock);
455 void clear_prefree_segments(struct f2fs_sb_info *sbi)
457 struct list_head *head = &(SM_I(sbi)->discard_list);
458 struct discard_entry *entry, *this;
459 struct dirty_seglist_info *dirty_i = DIRTY_I(sbi);
460 unsigned long *prefree_map = dirty_i->dirty_segmap[PRE];
461 unsigned int total_segs = TOTAL_SEGS(sbi);
462 unsigned int start = 0, end = -1;
464 mutex_lock(&dirty_i->seglist_lock);
468 start = find_next_bit(prefree_map, total_segs, end + 1);
469 if (start >= total_segs)
471 end = find_next_zero_bit(prefree_map, total_segs, start + 1);
473 for (i = start; i < end; i++)
474 clear_bit(i, prefree_map);
476 dirty_i->nr_dirty[PRE] -= end - start;
478 if (!test_opt(sbi, DISCARD))
481 f2fs_issue_discard(sbi, START_BLOCK(sbi, start),
482 (end - start) << sbi->log_blocks_per_seg);
484 mutex_unlock(&dirty_i->seglist_lock);
486 /* send small discards */
487 list_for_each_entry_safe(entry, this, head, list) {
488 f2fs_issue_discard(sbi, entry->blkaddr, entry->len);
489 list_del(&entry->list);
490 SM_I(sbi)->nr_discards -= entry->len;
491 kmem_cache_free(discard_entry_slab, entry);
495 static void __mark_sit_entry_dirty(struct f2fs_sb_info *sbi, unsigned int segno)
497 struct sit_info *sit_i = SIT_I(sbi);
498 if (!__test_and_set_bit(segno, sit_i->dirty_sentries_bitmap))
499 sit_i->dirty_sentries++;
502 static void __set_sit_entry_type(struct f2fs_sb_info *sbi, int type,
503 unsigned int segno, int modified)
505 struct seg_entry *se = get_seg_entry(sbi, segno);
508 __mark_sit_entry_dirty(sbi, segno);
511 static void update_sit_entry(struct f2fs_sb_info *sbi, block_t blkaddr, int del)
513 struct seg_entry *se;
514 unsigned int segno, offset;
515 long int new_vblocks;
517 segno = GET_SEGNO(sbi, blkaddr);
519 se = get_seg_entry(sbi, segno);
520 new_vblocks = se->valid_blocks + del;
521 offset = GET_BLKOFF_FROM_SEG0(sbi, blkaddr);
523 f2fs_bug_on(sbi, (new_vblocks >> (sizeof(unsigned short) << 3) ||
524 (new_vblocks > sbi->blocks_per_seg)));
526 se->valid_blocks = new_vblocks;
527 se->mtime = get_mtime(sbi);
528 SIT_I(sbi)->max_mtime = se->mtime;
530 /* Update valid block bitmap */
532 if (f2fs_set_bit(offset, se->cur_valid_map))
535 if (!f2fs_clear_bit(offset, se->cur_valid_map))
538 if (!f2fs_test_bit(offset, se->ckpt_valid_map))
539 se->ckpt_valid_blocks += del;
541 __mark_sit_entry_dirty(sbi, segno);
543 /* update total number of valid blocks to be written in ckpt area */
544 SIT_I(sbi)->written_valid_blocks += del;
546 if (sbi->segs_per_sec > 1)
547 get_sec_entry(sbi, segno)->valid_blocks += del;
550 void refresh_sit_entry(struct f2fs_sb_info *sbi, block_t old, block_t new)
552 update_sit_entry(sbi, new, 1);
553 if (GET_SEGNO(sbi, old) != NULL_SEGNO)
554 update_sit_entry(sbi, old, -1);
556 locate_dirty_segment(sbi, GET_SEGNO(sbi, old));
557 locate_dirty_segment(sbi, GET_SEGNO(sbi, new));
560 void invalidate_blocks(struct f2fs_sb_info *sbi, block_t addr)
562 unsigned int segno = GET_SEGNO(sbi, addr);
563 struct sit_info *sit_i = SIT_I(sbi);
565 f2fs_bug_on(sbi, addr == NULL_ADDR);
566 if (addr == NEW_ADDR)
569 /* add it into sit main buffer */
570 mutex_lock(&sit_i->sentry_lock);
572 update_sit_entry(sbi, addr, -1);
574 /* add it into dirty seglist */
575 locate_dirty_segment(sbi, segno);
577 mutex_unlock(&sit_i->sentry_lock);
581 * This function should be resided under the curseg_mutex lock
583 static void __add_sum_entry(struct f2fs_sb_info *sbi, int type,
584 struct f2fs_summary *sum)
586 struct curseg_info *curseg = CURSEG_I(sbi, type);
587 void *addr = curseg->sum_blk;
588 addr += curseg->next_blkoff * sizeof(struct f2fs_summary);
589 memcpy(addr, sum, sizeof(struct f2fs_summary));
593 * Calculate the number of current summary pages for writing
595 int npages_for_summary_flush(struct f2fs_sb_info *sbi)
597 int valid_sum_count = 0;
600 for (i = CURSEG_HOT_DATA; i <= CURSEG_COLD_DATA; i++) {
601 if (sbi->ckpt->alloc_type[i] == SSR)
602 valid_sum_count += sbi->blocks_per_seg;
604 valid_sum_count += curseg_blkoff(sbi, i);
607 sum_in_page = (PAGE_CACHE_SIZE - 2 * SUM_JOURNAL_SIZE -
608 SUM_FOOTER_SIZE) / SUMMARY_SIZE;
609 if (valid_sum_count <= sum_in_page)
611 else if ((valid_sum_count - sum_in_page) <=
612 (PAGE_CACHE_SIZE - SUM_FOOTER_SIZE) / SUMMARY_SIZE)
618 * Caller should put this summary page
620 struct page *get_sum_page(struct f2fs_sb_info *sbi, unsigned int segno)
622 return get_meta_page(sbi, GET_SUM_BLOCK(sbi, segno));
625 static void write_sum_page(struct f2fs_sb_info *sbi,
626 struct f2fs_summary_block *sum_blk, block_t blk_addr)
628 struct page *page = grab_meta_page(sbi, blk_addr);
629 void *kaddr = page_address(page);
630 memcpy(kaddr, sum_blk, PAGE_CACHE_SIZE);
631 set_page_dirty(page);
632 f2fs_put_page(page, 1);
635 static int is_next_segment_free(struct f2fs_sb_info *sbi, int type)
637 struct curseg_info *curseg = CURSEG_I(sbi, type);
638 unsigned int segno = curseg->segno + 1;
639 struct free_segmap_info *free_i = FREE_I(sbi);
641 if (segno < TOTAL_SEGS(sbi) && segno % sbi->segs_per_sec)
642 return !test_bit(segno, free_i->free_segmap);
647 * Find a new segment from the free segments bitmap to right order
648 * This function should be returned with success, otherwise BUG
650 static void get_new_segment(struct f2fs_sb_info *sbi,
651 unsigned int *newseg, bool new_sec, int dir)
653 struct free_segmap_info *free_i = FREE_I(sbi);
654 unsigned int segno, secno, zoneno;
655 unsigned int total_zones = TOTAL_SECS(sbi) / sbi->secs_per_zone;
656 unsigned int hint = *newseg / sbi->segs_per_sec;
657 unsigned int old_zoneno = GET_ZONENO_FROM_SEGNO(sbi, *newseg);
658 unsigned int left_start = hint;
663 write_lock(&free_i->segmap_lock);
665 if (!new_sec && ((*newseg + 1) % sbi->segs_per_sec)) {
666 segno = find_next_zero_bit(free_i->free_segmap,
667 TOTAL_SEGS(sbi), *newseg + 1);
668 if (segno - *newseg < sbi->segs_per_sec -
669 (*newseg % sbi->segs_per_sec))
673 secno = find_next_zero_bit(free_i->free_secmap, TOTAL_SECS(sbi), hint);
674 if (secno >= TOTAL_SECS(sbi)) {
675 if (dir == ALLOC_RIGHT) {
676 secno = find_next_zero_bit(free_i->free_secmap,
678 f2fs_bug_on(sbi, secno >= TOTAL_SECS(sbi));
681 left_start = hint - 1;
687 while (test_bit(left_start, free_i->free_secmap)) {
688 if (left_start > 0) {
692 left_start = find_next_zero_bit(free_i->free_secmap,
694 f2fs_bug_on(sbi, left_start >= TOTAL_SECS(sbi));
700 segno = secno * sbi->segs_per_sec;
701 zoneno = secno / sbi->secs_per_zone;
703 /* give up on finding another zone */
706 if (sbi->secs_per_zone == 1)
708 if (zoneno == old_zoneno)
710 if (dir == ALLOC_LEFT) {
711 if (!go_left && zoneno + 1 >= total_zones)
713 if (go_left && zoneno == 0)
716 for (i = 0; i < NR_CURSEG_TYPE; i++)
717 if (CURSEG_I(sbi, i)->zone == zoneno)
720 if (i < NR_CURSEG_TYPE) {
721 /* zone is in user, try another */
723 hint = zoneno * sbi->secs_per_zone - 1;
724 else if (zoneno + 1 >= total_zones)
727 hint = (zoneno + 1) * sbi->secs_per_zone;
729 goto find_other_zone;
732 /* set it as dirty segment in free segmap */
733 f2fs_bug_on(sbi, test_bit(segno, free_i->free_segmap));
734 __set_inuse(sbi, segno);
736 write_unlock(&free_i->segmap_lock);
739 static void reset_curseg(struct f2fs_sb_info *sbi, int type, int modified)
741 struct curseg_info *curseg = CURSEG_I(sbi, type);
742 struct summary_footer *sum_footer;
744 curseg->segno = curseg->next_segno;
745 curseg->zone = GET_ZONENO_FROM_SEGNO(sbi, curseg->segno);
746 curseg->next_blkoff = 0;
747 curseg->next_segno = NULL_SEGNO;
749 sum_footer = &(curseg->sum_blk->footer);
750 memset(sum_footer, 0, sizeof(struct summary_footer));
751 if (IS_DATASEG(type))
752 SET_SUM_TYPE(sum_footer, SUM_TYPE_DATA);
753 if (IS_NODESEG(type))
754 SET_SUM_TYPE(sum_footer, SUM_TYPE_NODE);
755 __set_sit_entry_type(sbi, type, curseg->segno, modified);
759 * Allocate a current working segment.
760 * This function always allocates a free segment in LFS manner.
762 static void new_curseg(struct f2fs_sb_info *sbi, int type, bool new_sec)
764 struct curseg_info *curseg = CURSEG_I(sbi, type);
765 unsigned int segno = curseg->segno;
766 int dir = ALLOC_LEFT;
768 write_sum_page(sbi, curseg->sum_blk,
769 GET_SUM_BLOCK(sbi, segno));
770 if (type == CURSEG_WARM_DATA || type == CURSEG_COLD_DATA)
773 if (test_opt(sbi, NOHEAP))
776 get_new_segment(sbi, &segno, new_sec, dir);
777 curseg->next_segno = segno;
778 reset_curseg(sbi, type, 1);
779 curseg->alloc_type = LFS;
782 static void __next_free_blkoff(struct f2fs_sb_info *sbi,
783 struct curseg_info *seg, block_t start)
785 struct seg_entry *se = get_seg_entry(sbi, seg->segno);
786 int entries = SIT_VBLOCK_MAP_SIZE / sizeof(unsigned long);
787 unsigned long target_map[entries];
788 unsigned long *ckpt_map = (unsigned long *)se->ckpt_valid_map;
789 unsigned long *cur_map = (unsigned long *)se->cur_valid_map;
792 for (i = 0; i < entries; i++)
793 target_map[i] = ckpt_map[i] | cur_map[i];
795 pos = __find_rev_next_zero_bit(target_map, sbi->blocks_per_seg, start);
797 seg->next_blkoff = pos;
801 * If a segment is written by LFS manner, next block offset is just obtained
802 * by increasing the current block offset. However, if a segment is written by
803 * SSR manner, next block offset obtained by calling __next_free_blkoff
805 static void __refresh_next_blkoff(struct f2fs_sb_info *sbi,
806 struct curseg_info *seg)
808 if (seg->alloc_type == SSR)
809 __next_free_blkoff(sbi, seg, seg->next_blkoff + 1);
815 * This function always allocates a used segment(from dirty seglist) by SSR
816 * manner, so it should recover the existing segment information of valid blocks
818 static void change_curseg(struct f2fs_sb_info *sbi, int type, bool reuse)
820 struct dirty_seglist_info *dirty_i = DIRTY_I(sbi);
821 struct curseg_info *curseg = CURSEG_I(sbi, type);
822 unsigned int new_segno = curseg->next_segno;
823 struct f2fs_summary_block *sum_node;
824 struct page *sum_page;
826 write_sum_page(sbi, curseg->sum_blk,
827 GET_SUM_BLOCK(sbi, curseg->segno));
828 __set_test_and_inuse(sbi, new_segno);
830 mutex_lock(&dirty_i->seglist_lock);
831 __remove_dirty_segment(sbi, new_segno, PRE);
832 __remove_dirty_segment(sbi, new_segno, DIRTY);
833 mutex_unlock(&dirty_i->seglist_lock);
835 reset_curseg(sbi, type, 1);
836 curseg->alloc_type = SSR;
837 __next_free_blkoff(sbi, curseg, 0);
840 sum_page = get_sum_page(sbi, new_segno);
841 sum_node = (struct f2fs_summary_block *)page_address(sum_page);
842 memcpy(curseg->sum_blk, sum_node, SUM_ENTRY_SIZE);
843 f2fs_put_page(sum_page, 1);
847 static int get_ssr_segment(struct f2fs_sb_info *sbi, int type)
849 struct curseg_info *curseg = CURSEG_I(sbi, type);
850 const struct victim_selection *v_ops = DIRTY_I(sbi)->v_ops;
852 if (IS_NODESEG(type) || !has_not_enough_free_secs(sbi, 0))
853 return v_ops->get_victim(sbi,
854 &(curseg)->next_segno, BG_GC, type, SSR);
856 /* For data segments, let's do SSR more intensively */
857 for (; type >= CURSEG_HOT_DATA; type--)
858 if (v_ops->get_victim(sbi, &(curseg)->next_segno,
865 * flush out current segment and replace it with new segment
866 * This function should be returned with success, otherwise BUG
868 static void allocate_segment_by_default(struct f2fs_sb_info *sbi,
869 int type, bool force)
871 struct curseg_info *curseg = CURSEG_I(sbi, type);
874 new_curseg(sbi, type, true);
875 else if (type == CURSEG_WARM_NODE)
876 new_curseg(sbi, type, false);
877 else if (curseg->alloc_type == LFS && is_next_segment_free(sbi, type))
878 new_curseg(sbi, type, false);
879 else if (need_SSR(sbi) && get_ssr_segment(sbi, type))
880 change_curseg(sbi, type, true);
882 new_curseg(sbi, type, false);
884 stat_inc_seg_type(sbi, curseg);
887 void allocate_new_segments(struct f2fs_sb_info *sbi)
889 struct curseg_info *curseg;
890 unsigned int old_curseg;
893 for (i = CURSEG_HOT_DATA; i <= CURSEG_COLD_DATA; i++) {
894 curseg = CURSEG_I(sbi, i);
895 old_curseg = curseg->segno;
896 SIT_I(sbi)->s_ops->allocate_segment(sbi, i, true);
897 locate_dirty_segment(sbi, old_curseg);
901 static const struct segment_allocation default_salloc_ops = {
902 .allocate_segment = allocate_segment_by_default,
905 static bool __has_curseg_space(struct f2fs_sb_info *sbi, int type)
907 struct curseg_info *curseg = CURSEG_I(sbi, type);
908 if (curseg->next_blkoff < sbi->blocks_per_seg)
913 static int __get_segment_type_2(struct page *page, enum page_type p_type)
916 return CURSEG_HOT_DATA;
918 return CURSEG_HOT_NODE;
921 static int __get_segment_type_4(struct page *page, enum page_type p_type)
923 if (p_type == DATA) {
924 struct inode *inode = page->mapping->host;
926 if (S_ISDIR(inode->i_mode))
927 return CURSEG_HOT_DATA;
929 return CURSEG_COLD_DATA;
931 if (IS_DNODE(page) && !is_cold_node(page))
932 return CURSEG_HOT_NODE;
934 return CURSEG_COLD_NODE;
938 static int __get_segment_type_6(struct page *page, enum page_type p_type)
940 if (p_type == DATA) {
941 struct inode *inode = page->mapping->host;
943 if (S_ISDIR(inode->i_mode))
944 return CURSEG_HOT_DATA;
945 else if (is_cold_data(page) || file_is_cold(inode))
946 return CURSEG_COLD_DATA;
948 return CURSEG_WARM_DATA;
951 return is_cold_node(page) ? CURSEG_WARM_NODE :
954 return CURSEG_COLD_NODE;
958 static int __get_segment_type(struct page *page, enum page_type p_type)
960 switch (F2FS_P_SB(page)->active_logs) {
962 return __get_segment_type_2(page, p_type);
964 return __get_segment_type_4(page, p_type);
966 /* NR_CURSEG_TYPE(6) logs by default */
967 f2fs_bug_on(F2FS_P_SB(page),
968 F2FS_P_SB(page)->active_logs != NR_CURSEG_TYPE);
969 return __get_segment_type_6(page, p_type);
972 void allocate_data_block(struct f2fs_sb_info *sbi, struct page *page,
973 block_t old_blkaddr, block_t *new_blkaddr,
974 struct f2fs_summary *sum, int type)
976 struct sit_info *sit_i = SIT_I(sbi);
977 struct curseg_info *curseg;
979 curseg = CURSEG_I(sbi, type);
981 mutex_lock(&curseg->curseg_mutex);
983 *new_blkaddr = NEXT_FREE_BLKADDR(sbi, curseg);
986 * __add_sum_entry should be resided under the curseg_mutex
987 * because, this function updates a summary entry in the
988 * current summary block.
990 __add_sum_entry(sbi, type, sum);
992 mutex_lock(&sit_i->sentry_lock);
993 __refresh_next_blkoff(sbi, curseg);
995 stat_inc_block_count(sbi, curseg);
997 if (!__has_curseg_space(sbi, type))
998 sit_i->s_ops->allocate_segment(sbi, type, false);
1000 * SIT information should be updated before segment allocation,
1001 * since SSR needs latest valid block information.
1003 refresh_sit_entry(sbi, old_blkaddr, *new_blkaddr);
1005 mutex_unlock(&sit_i->sentry_lock);
1007 if (page && IS_NODESEG(type))
1008 fill_node_footer_blkaddr(page, NEXT_FREE_BLKADDR(sbi, curseg));
1010 mutex_unlock(&curseg->curseg_mutex);
1013 static void do_write_page(struct f2fs_sb_info *sbi, struct page *page,
1014 block_t old_blkaddr, block_t *new_blkaddr,
1015 struct f2fs_summary *sum, struct f2fs_io_info *fio)
1017 int type = __get_segment_type(page, fio->type);
1019 allocate_data_block(sbi, page, old_blkaddr, new_blkaddr, sum, type);
1021 /* writeout dirty page into bdev */
1022 f2fs_submit_page_mbio(sbi, page, *new_blkaddr, fio);
1025 void write_meta_page(struct f2fs_sb_info *sbi, struct page *page)
1027 struct f2fs_io_info fio = {
1029 .rw = WRITE_SYNC | REQ_META | REQ_PRIO
1032 set_page_writeback(page);
1033 f2fs_submit_page_mbio(sbi, page, page->index, &fio);
1036 void write_node_page(struct f2fs_sb_info *sbi, struct page *page,
1037 struct f2fs_io_info *fio,
1038 unsigned int nid, block_t old_blkaddr, block_t *new_blkaddr)
1040 struct f2fs_summary sum;
1041 set_summary(&sum, nid, 0, 0);
1042 do_write_page(sbi, page, old_blkaddr, new_blkaddr, &sum, fio);
1045 void write_data_page(struct page *page, struct dnode_of_data *dn,
1046 block_t *new_blkaddr, struct f2fs_io_info *fio)
1048 struct f2fs_sb_info *sbi = F2FS_I_SB(dn->inode);
1049 struct f2fs_summary sum;
1050 struct node_info ni;
1052 f2fs_bug_on(sbi, dn->data_blkaddr == NULL_ADDR);
1053 get_node_info(sbi, dn->nid, &ni);
1054 set_summary(&sum, dn->nid, dn->ofs_in_node, ni.version);
1056 do_write_page(sbi, page, dn->data_blkaddr, new_blkaddr, &sum, fio);
1059 void rewrite_data_page(struct page *page, block_t old_blkaddr,
1060 struct f2fs_io_info *fio)
1062 f2fs_submit_page_mbio(F2FS_P_SB(page), page, old_blkaddr, fio);
1065 void recover_data_page(struct f2fs_sb_info *sbi,
1066 struct page *page, struct f2fs_summary *sum,
1067 block_t old_blkaddr, block_t new_blkaddr)
1069 struct sit_info *sit_i = SIT_I(sbi);
1070 struct curseg_info *curseg;
1071 unsigned int segno, old_cursegno;
1072 struct seg_entry *se;
1075 segno = GET_SEGNO(sbi, new_blkaddr);
1076 se = get_seg_entry(sbi, segno);
1079 if (se->valid_blocks == 0 && !IS_CURSEG(sbi, segno)) {
1080 if (old_blkaddr == NULL_ADDR)
1081 type = CURSEG_COLD_DATA;
1083 type = CURSEG_WARM_DATA;
1085 curseg = CURSEG_I(sbi, type);
1087 mutex_lock(&curseg->curseg_mutex);
1088 mutex_lock(&sit_i->sentry_lock);
1090 old_cursegno = curseg->segno;
1092 /* change the current segment */
1093 if (segno != curseg->segno) {
1094 curseg->next_segno = segno;
1095 change_curseg(sbi, type, true);
1098 curseg->next_blkoff = GET_BLKOFF_FROM_SEG0(sbi, new_blkaddr);
1099 __add_sum_entry(sbi, type, sum);
1101 refresh_sit_entry(sbi, old_blkaddr, new_blkaddr);
1102 locate_dirty_segment(sbi, old_cursegno);
1104 mutex_unlock(&sit_i->sentry_lock);
1105 mutex_unlock(&curseg->curseg_mutex);
1108 static inline bool is_merged_page(struct f2fs_sb_info *sbi,
1109 struct page *page, enum page_type type)
1111 enum page_type btype = PAGE_TYPE_OF_BIO(type);
1112 struct f2fs_bio_info *io = &sbi->write_io[btype];
1113 struct bio_vec *bvec;
1116 down_read(&io->io_rwsem);
1120 bio_for_each_segment_all(bvec, io->bio, i) {
1121 if (page == bvec->bv_page) {
1122 up_read(&io->io_rwsem);
1128 up_read(&io->io_rwsem);
1132 void f2fs_wait_on_page_writeback(struct page *page,
1133 enum page_type type)
1135 if (PageWriteback(page)) {
1136 struct f2fs_sb_info *sbi = F2FS_P_SB(page);
1138 if (is_merged_page(sbi, page, type))
1139 f2fs_submit_merged_bio(sbi, type, WRITE);
1140 wait_on_page_writeback(page);
1144 static int read_compacted_summaries(struct f2fs_sb_info *sbi)
1146 struct f2fs_checkpoint *ckpt = F2FS_CKPT(sbi);
1147 struct curseg_info *seg_i;
1148 unsigned char *kaddr;
1153 start = start_sum_block(sbi);
1155 page = get_meta_page(sbi, start++);
1156 kaddr = (unsigned char *)page_address(page);
1158 /* Step 1: restore nat cache */
1159 seg_i = CURSEG_I(sbi, CURSEG_HOT_DATA);
1160 memcpy(&seg_i->sum_blk->n_nats, kaddr, SUM_JOURNAL_SIZE);
1162 /* Step 2: restore sit cache */
1163 seg_i = CURSEG_I(sbi, CURSEG_COLD_DATA);
1164 memcpy(&seg_i->sum_blk->n_sits, kaddr + SUM_JOURNAL_SIZE,
1166 offset = 2 * SUM_JOURNAL_SIZE;
1168 /* Step 3: restore summary entries */
1169 for (i = CURSEG_HOT_DATA; i <= CURSEG_COLD_DATA; i++) {
1170 unsigned short blk_off;
1173 seg_i = CURSEG_I(sbi, i);
1174 segno = le32_to_cpu(ckpt->cur_data_segno[i]);
1175 blk_off = le16_to_cpu(ckpt->cur_data_blkoff[i]);
1176 seg_i->next_segno = segno;
1177 reset_curseg(sbi, i, 0);
1178 seg_i->alloc_type = ckpt->alloc_type[i];
1179 seg_i->next_blkoff = blk_off;
1181 if (seg_i->alloc_type == SSR)
1182 blk_off = sbi->blocks_per_seg;
1184 for (j = 0; j < blk_off; j++) {
1185 struct f2fs_summary *s;
1186 s = (struct f2fs_summary *)(kaddr + offset);
1187 seg_i->sum_blk->entries[j] = *s;
1188 offset += SUMMARY_SIZE;
1189 if (offset + SUMMARY_SIZE <= PAGE_CACHE_SIZE -
1193 f2fs_put_page(page, 1);
1196 page = get_meta_page(sbi, start++);
1197 kaddr = (unsigned char *)page_address(page);
1201 f2fs_put_page(page, 1);
1205 static int read_normal_summaries(struct f2fs_sb_info *sbi, int type)
1207 struct f2fs_checkpoint *ckpt = F2FS_CKPT(sbi);
1208 struct f2fs_summary_block *sum;
1209 struct curseg_info *curseg;
1211 unsigned short blk_off;
1212 unsigned int segno = 0;
1213 block_t blk_addr = 0;
1215 /* get segment number and block addr */
1216 if (IS_DATASEG(type)) {
1217 segno = le32_to_cpu(ckpt->cur_data_segno[type]);
1218 blk_off = le16_to_cpu(ckpt->cur_data_blkoff[type -
1220 if (is_set_ckpt_flags(ckpt, CP_UMOUNT_FLAG))
1221 blk_addr = sum_blk_addr(sbi, NR_CURSEG_TYPE, type);
1223 blk_addr = sum_blk_addr(sbi, NR_CURSEG_DATA_TYPE, type);
1225 segno = le32_to_cpu(ckpt->cur_node_segno[type -
1227 blk_off = le16_to_cpu(ckpt->cur_node_blkoff[type -
1229 if (is_set_ckpt_flags(ckpt, CP_UMOUNT_FLAG))
1230 blk_addr = sum_blk_addr(sbi, NR_CURSEG_NODE_TYPE,
1231 type - CURSEG_HOT_NODE);
1233 blk_addr = GET_SUM_BLOCK(sbi, segno);
1236 new = get_meta_page(sbi, blk_addr);
1237 sum = (struct f2fs_summary_block *)page_address(new);
1239 if (IS_NODESEG(type)) {
1240 if (is_set_ckpt_flags(ckpt, CP_UMOUNT_FLAG)) {
1241 struct f2fs_summary *ns = &sum->entries[0];
1243 for (i = 0; i < sbi->blocks_per_seg; i++, ns++) {
1245 ns->ofs_in_node = 0;
1250 err = restore_node_summary(sbi, segno, sum);
1252 f2fs_put_page(new, 1);
1258 /* set uncompleted segment to curseg */
1259 curseg = CURSEG_I(sbi, type);
1260 mutex_lock(&curseg->curseg_mutex);
1261 memcpy(curseg->sum_blk, sum, PAGE_CACHE_SIZE);
1262 curseg->next_segno = segno;
1263 reset_curseg(sbi, type, 0);
1264 curseg->alloc_type = ckpt->alloc_type[type];
1265 curseg->next_blkoff = blk_off;
1266 mutex_unlock(&curseg->curseg_mutex);
1267 f2fs_put_page(new, 1);
1271 static int restore_curseg_summaries(struct f2fs_sb_info *sbi)
1273 int type = CURSEG_HOT_DATA;
1276 if (is_set_ckpt_flags(F2FS_CKPT(sbi), CP_COMPACT_SUM_FLAG)) {
1277 /* restore for compacted data summary */
1278 if (read_compacted_summaries(sbi))
1280 type = CURSEG_HOT_NODE;
1283 for (; type <= CURSEG_COLD_NODE; type++) {
1284 err = read_normal_summaries(sbi, type);
1292 static void write_compacted_summaries(struct f2fs_sb_info *sbi, block_t blkaddr)
1295 unsigned char *kaddr;
1296 struct f2fs_summary *summary;
1297 struct curseg_info *seg_i;
1298 int written_size = 0;
1301 page = grab_meta_page(sbi, blkaddr++);
1302 kaddr = (unsigned char *)page_address(page);
1304 /* Step 1: write nat cache */
1305 seg_i = CURSEG_I(sbi, CURSEG_HOT_DATA);
1306 memcpy(kaddr, &seg_i->sum_blk->n_nats, SUM_JOURNAL_SIZE);
1307 written_size += SUM_JOURNAL_SIZE;
1309 /* Step 2: write sit cache */
1310 seg_i = CURSEG_I(sbi, CURSEG_COLD_DATA);
1311 memcpy(kaddr + written_size, &seg_i->sum_blk->n_sits,
1313 written_size += SUM_JOURNAL_SIZE;
1315 /* Step 3: write summary entries */
1316 for (i = CURSEG_HOT_DATA; i <= CURSEG_COLD_DATA; i++) {
1317 unsigned short blkoff;
1318 seg_i = CURSEG_I(sbi, i);
1319 if (sbi->ckpt->alloc_type[i] == SSR)
1320 blkoff = sbi->blocks_per_seg;
1322 blkoff = curseg_blkoff(sbi, i);
1324 for (j = 0; j < blkoff; j++) {
1326 page = grab_meta_page(sbi, blkaddr++);
1327 kaddr = (unsigned char *)page_address(page);
1330 summary = (struct f2fs_summary *)(kaddr + written_size);
1331 *summary = seg_i->sum_blk->entries[j];
1332 written_size += SUMMARY_SIZE;
1334 if (written_size + SUMMARY_SIZE <= PAGE_CACHE_SIZE -
1338 set_page_dirty(page);
1339 f2fs_put_page(page, 1);
1344 set_page_dirty(page);
1345 f2fs_put_page(page, 1);
1349 static void write_normal_summaries(struct f2fs_sb_info *sbi,
1350 block_t blkaddr, int type)
1353 if (IS_DATASEG(type))
1354 end = type + NR_CURSEG_DATA_TYPE;
1356 end = type + NR_CURSEG_NODE_TYPE;
1358 for (i = type; i < end; i++) {
1359 struct curseg_info *sum = CURSEG_I(sbi, i);
1360 mutex_lock(&sum->curseg_mutex);
1361 write_sum_page(sbi, sum->sum_blk, blkaddr + (i - type));
1362 mutex_unlock(&sum->curseg_mutex);
1366 void write_data_summaries(struct f2fs_sb_info *sbi, block_t start_blk)
1368 if (is_set_ckpt_flags(F2FS_CKPT(sbi), CP_COMPACT_SUM_FLAG))
1369 write_compacted_summaries(sbi, start_blk);
1371 write_normal_summaries(sbi, start_blk, CURSEG_HOT_DATA);
1374 void write_node_summaries(struct f2fs_sb_info *sbi, block_t start_blk)
1376 if (is_set_ckpt_flags(F2FS_CKPT(sbi), CP_UMOUNT_FLAG))
1377 write_normal_summaries(sbi, start_blk, CURSEG_HOT_NODE);
1380 int lookup_journal_in_cursum(struct f2fs_summary_block *sum, int type,
1381 unsigned int val, int alloc)
1385 if (type == NAT_JOURNAL) {
1386 for (i = 0; i < nats_in_cursum(sum); i++) {
1387 if (le32_to_cpu(nid_in_journal(sum, i)) == val)
1390 if (alloc && nats_in_cursum(sum) < NAT_JOURNAL_ENTRIES)
1391 return update_nats_in_cursum(sum, 1);
1392 } else if (type == SIT_JOURNAL) {
1393 for (i = 0; i < sits_in_cursum(sum); i++)
1394 if (le32_to_cpu(segno_in_journal(sum, i)) == val)
1396 if (alloc && sits_in_cursum(sum) < SIT_JOURNAL_ENTRIES)
1397 return update_sits_in_cursum(sum, 1);
1402 static struct page *get_current_sit_page(struct f2fs_sb_info *sbi,
1405 struct sit_info *sit_i = SIT_I(sbi);
1406 unsigned int offset = SIT_BLOCK_OFFSET(sit_i, segno);
1407 block_t blk_addr = sit_i->sit_base_addr + offset;
1409 check_seg_range(sbi, segno);
1411 /* calculate sit block address */
1412 if (f2fs_test_bit(offset, sit_i->sit_bitmap))
1413 blk_addr += sit_i->sit_blocks;
1415 return get_meta_page(sbi, blk_addr);
1418 static struct page *get_next_sit_page(struct f2fs_sb_info *sbi,
1421 struct sit_info *sit_i = SIT_I(sbi);
1422 struct page *src_page, *dst_page;
1423 pgoff_t src_off, dst_off;
1424 void *src_addr, *dst_addr;
1426 src_off = current_sit_addr(sbi, start);
1427 dst_off = next_sit_addr(sbi, src_off);
1429 /* get current sit block page without lock */
1430 src_page = get_meta_page(sbi, src_off);
1431 dst_page = grab_meta_page(sbi, dst_off);
1432 f2fs_bug_on(sbi, PageDirty(src_page));
1434 src_addr = page_address(src_page);
1435 dst_addr = page_address(dst_page);
1436 memcpy(dst_addr, src_addr, PAGE_CACHE_SIZE);
1438 set_page_dirty(dst_page);
1439 f2fs_put_page(src_page, 1);
1441 set_to_next_sit(sit_i, start);
1446 static bool flush_sits_in_journal(struct f2fs_sb_info *sbi)
1448 struct curseg_info *curseg = CURSEG_I(sbi, CURSEG_COLD_DATA);
1449 struct f2fs_summary_block *sum = curseg->sum_blk;
1453 * If the journal area in the current summary is full of sit entries,
1454 * all the sit entries will be flushed. Otherwise the sit entries
1455 * are not able to replace with newly hot sit entries.
1457 if (sits_in_cursum(sum) >= SIT_JOURNAL_ENTRIES) {
1458 for (i = sits_in_cursum(sum) - 1; i >= 0; i--) {
1460 segno = le32_to_cpu(segno_in_journal(sum, i));
1461 __mark_sit_entry_dirty(sbi, segno);
1463 update_sits_in_cursum(sum, -sits_in_cursum(sum));
1470 * CP calls this function, which flushes SIT entries including sit_journal,
1471 * and moves prefree segs to free segs.
1473 void flush_sit_entries(struct f2fs_sb_info *sbi)
1475 struct sit_info *sit_i = SIT_I(sbi);
1476 unsigned long *bitmap = sit_i->dirty_sentries_bitmap;
1477 struct curseg_info *curseg = CURSEG_I(sbi, CURSEG_COLD_DATA);
1478 struct f2fs_summary_block *sum = curseg->sum_blk;
1479 unsigned long nsegs = TOTAL_SEGS(sbi);
1480 struct page *page = NULL;
1481 struct f2fs_sit_block *raw_sit = NULL;
1482 unsigned int start = 0, end = 0;
1486 mutex_lock(&curseg->curseg_mutex);
1487 mutex_lock(&sit_i->sentry_lock);
1490 * "flushed" indicates whether sit entries in journal are flushed
1491 * to the SIT area or not.
1493 flushed = flush_sits_in_journal(sbi);
1495 for_each_set_bit(segno, bitmap, nsegs) {
1496 struct seg_entry *se = get_seg_entry(sbi, segno);
1497 int sit_offset, offset;
1499 sit_offset = SIT_ENTRY_OFFSET(sit_i, segno);
1501 /* add discard candidates */
1502 if (SM_I(sbi)->nr_discards < SM_I(sbi)->max_discards)
1503 add_discard_addrs(sbi, segno, se);
1508 offset = lookup_journal_in_cursum(sum, SIT_JOURNAL, segno, 1);
1510 segno_in_journal(sum, offset) = cpu_to_le32(segno);
1511 seg_info_to_raw_sit(se, &sit_in_journal(sum, offset));
1515 if (!page || (start > segno) || (segno > end)) {
1517 f2fs_put_page(page, 1);
1521 start = START_SEGNO(sit_i, segno);
1522 end = start + SIT_ENTRY_PER_BLOCK - 1;
1524 /* read sit block that will be updated */
1525 page = get_next_sit_page(sbi, start);
1526 raw_sit = page_address(page);
1529 /* udpate entry in SIT block */
1530 seg_info_to_raw_sit(se, &raw_sit->entries[sit_offset]);
1532 __clear_bit(segno, bitmap);
1533 sit_i->dirty_sentries--;
1535 mutex_unlock(&sit_i->sentry_lock);
1536 mutex_unlock(&curseg->curseg_mutex);
1538 /* writeout last modified SIT block */
1539 f2fs_put_page(page, 1);
1541 set_prefree_as_free_segments(sbi);
1544 static int build_sit_info(struct f2fs_sb_info *sbi)
1546 struct f2fs_super_block *raw_super = F2FS_RAW_SUPER(sbi);
1547 struct f2fs_checkpoint *ckpt = F2FS_CKPT(sbi);
1548 struct sit_info *sit_i;
1549 unsigned int sit_segs, start;
1550 char *src_bitmap, *dst_bitmap;
1551 unsigned int bitmap_size;
1553 /* allocate memory for SIT information */
1554 sit_i = kzalloc(sizeof(struct sit_info), GFP_KERNEL);
1558 SM_I(sbi)->sit_info = sit_i;
1560 sit_i->sentries = vzalloc(TOTAL_SEGS(sbi) * sizeof(struct seg_entry));
1561 if (!sit_i->sentries)
1564 bitmap_size = f2fs_bitmap_size(TOTAL_SEGS(sbi));
1565 sit_i->dirty_sentries_bitmap = kzalloc(bitmap_size, GFP_KERNEL);
1566 if (!sit_i->dirty_sentries_bitmap)
1569 for (start = 0; start < TOTAL_SEGS(sbi); start++) {
1570 sit_i->sentries[start].cur_valid_map
1571 = kzalloc(SIT_VBLOCK_MAP_SIZE, GFP_KERNEL);
1572 sit_i->sentries[start].ckpt_valid_map
1573 = kzalloc(SIT_VBLOCK_MAP_SIZE, GFP_KERNEL);
1574 if (!sit_i->sentries[start].cur_valid_map
1575 || !sit_i->sentries[start].ckpt_valid_map)
1579 if (sbi->segs_per_sec > 1) {
1580 sit_i->sec_entries = vzalloc(TOTAL_SECS(sbi) *
1581 sizeof(struct sec_entry));
1582 if (!sit_i->sec_entries)
1586 /* get information related with SIT */
1587 sit_segs = le32_to_cpu(raw_super->segment_count_sit) >> 1;
1589 /* setup SIT bitmap from ckeckpoint pack */
1590 bitmap_size = __bitmap_size(sbi, SIT_BITMAP);
1591 src_bitmap = __bitmap_ptr(sbi, SIT_BITMAP);
1593 dst_bitmap = kmemdup(src_bitmap, bitmap_size, GFP_KERNEL);
1597 /* init SIT information */
1598 sit_i->s_ops = &default_salloc_ops;
1600 sit_i->sit_base_addr = le32_to_cpu(raw_super->sit_blkaddr);
1601 sit_i->sit_blocks = sit_segs << sbi->log_blocks_per_seg;
1602 sit_i->written_valid_blocks = le64_to_cpu(ckpt->valid_block_count);
1603 sit_i->sit_bitmap = dst_bitmap;
1604 sit_i->bitmap_size = bitmap_size;
1605 sit_i->dirty_sentries = 0;
1606 sit_i->sents_per_block = SIT_ENTRY_PER_BLOCK;
1607 sit_i->elapsed_time = le64_to_cpu(sbi->ckpt->elapsed_time);
1608 sit_i->mounted_time = CURRENT_TIME_SEC.tv_sec;
1609 mutex_init(&sit_i->sentry_lock);
1613 static int build_free_segmap(struct f2fs_sb_info *sbi)
1615 struct f2fs_sm_info *sm_info = SM_I(sbi);
1616 struct free_segmap_info *free_i;
1617 unsigned int bitmap_size, sec_bitmap_size;
1619 /* allocate memory for free segmap information */
1620 free_i = kzalloc(sizeof(struct free_segmap_info), GFP_KERNEL);
1624 SM_I(sbi)->free_info = free_i;
1626 bitmap_size = f2fs_bitmap_size(TOTAL_SEGS(sbi));
1627 free_i->free_segmap = kmalloc(bitmap_size, GFP_KERNEL);
1628 if (!free_i->free_segmap)
1631 sec_bitmap_size = f2fs_bitmap_size(TOTAL_SECS(sbi));
1632 free_i->free_secmap = kmalloc(sec_bitmap_size, GFP_KERNEL);
1633 if (!free_i->free_secmap)
1636 /* set all segments as dirty temporarily */
1637 memset(free_i->free_segmap, 0xff, bitmap_size);
1638 memset(free_i->free_secmap, 0xff, sec_bitmap_size);
1640 /* init free segmap information */
1641 free_i->start_segno =
1642 (unsigned int) GET_SEGNO_FROM_SEG0(sbi, sm_info->main_blkaddr);
1643 free_i->free_segments = 0;
1644 free_i->free_sections = 0;
1645 rwlock_init(&free_i->segmap_lock);
1649 static int build_curseg(struct f2fs_sb_info *sbi)
1651 struct curseg_info *array;
1654 array = kcalloc(NR_CURSEG_TYPE, sizeof(*array), GFP_KERNEL);
1658 SM_I(sbi)->curseg_array = array;
1660 for (i = 0; i < NR_CURSEG_TYPE; i++) {
1661 mutex_init(&array[i].curseg_mutex);
1662 array[i].sum_blk = kzalloc(PAGE_CACHE_SIZE, GFP_KERNEL);
1663 if (!array[i].sum_blk)
1665 array[i].segno = NULL_SEGNO;
1666 array[i].next_blkoff = 0;
1668 return restore_curseg_summaries(sbi);
1671 static void build_sit_entries(struct f2fs_sb_info *sbi)
1673 struct sit_info *sit_i = SIT_I(sbi);
1674 struct curseg_info *curseg = CURSEG_I(sbi, CURSEG_COLD_DATA);
1675 struct f2fs_summary_block *sum = curseg->sum_blk;
1676 int sit_blk_cnt = SIT_BLK_CNT(sbi);
1677 unsigned int i, start, end;
1678 unsigned int readed, start_blk = 0;
1679 int nrpages = MAX_BIO_BLOCKS(max_hw_blocks(sbi));
1682 readed = ra_meta_pages(sbi, start_blk, nrpages, META_SIT);
1684 start = start_blk * sit_i->sents_per_block;
1685 end = (start_blk + readed) * sit_i->sents_per_block;
1687 for (; start < end && start < TOTAL_SEGS(sbi); start++) {
1688 struct seg_entry *se = &sit_i->sentries[start];
1689 struct f2fs_sit_block *sit_blk;
1690 struct f2fs_sit_entry sit;
1693 mutex_lock(&curseg->curseg_mutex);
1694 for (i = 0; i < sits_in_cursum(sum); i++) {
1695 if (le32_to_cpu(segno_in_journal(sum, i))
1697 sit = sit_in_journal(sum, i);
1698 mutex_unlock(&curseg->curseg_mutex);
1702 mutex_unlock(&curseg->curseg_mutex);
1704 page = get_current_sit_page(sbi, start);
1705 sit_blk = (struct f2fs_sit_block *)page_address(page);
1706 sit = sit_blk->entries[SIT_ENTRY_OFFSET(sit_i, start)];
1707 f2fs_put_page(page, 1);
1709 check_block_count(sbi, start, &sit);
1710 seg_info_from_raw_sit(se, &sit);
1711 if (sbi->segs_per_sec > 1) {
1712 struct sec_entry *e = get_sec_entry(sbi, start);
1713 e->valid_blocks += se->valid_blocks;
1716 start_blk += readed;
1717 } while (start_blk < sit_blk_cnt);
1720 static void init_free_segmap(struct f2fs_sb_info *sbi)
1725 for (start = 0; start < TOTAL_SEGS(sbi); start++) {
1726 struct seg_entry *sentry = get_seg_entry(sbi, start);
1727 if (!sentry->valid_blocks)
1728 __set_free(sbi, start);
1731 /* set use the current segments */
1732 for (type = CURSEG_HOT_DATA; type <= CURSEG_COLD_NODE; type++) {
1733 struct curseg_info *curseg_t = CURSEG_I(sbi, type);
1734 __set_test_and_inuse(sbi, curseg_t->segno);
1738 static void init_dirty_segmap(struct f2fs_sb_info *sbi)
1740 struct dirty_seglist_info *dirty_i = DIRTY_I(sbi);
1741 struct free_segmap_info *free_i = FREE_I(sbi);
1742 unsigned int segno = 0, offset = 0, total_segs = TOTAL_SEGS(sbi);
1743 unsigned short valid_blocks;
1746 /* find dirty segment based on free segmap */
1747 segno = find_next_inuse(free_i, total_segs, offset);
1748 if (segno >= total_segs)
1751 valid_blocks = get_valid_blocks(sbi, segno, 0);
1752 if (valid_blocks == sbi->blocks_per_seg || !valid_blocks)
1754 if (valid_blocks > sbi->blocks_per_seg) {
1755 f2fs_bug_on(sbi, 1);
1758 mutex_lock(&dirty_i->seglist_lock);
1759 __locate_dirty_segment(sbi, segno, DIRTY);
1760 mutex_unlock(&dirty_i->seglist_lock);
1764 static int init_victim_secmap(struct f2fs_sb_info *sbi)
1766 struct dirty_seglist_info *dirty_i = DIRTY_I(sbi);
1767 unsigned int bitmap_size = f2fs_bitmap_size(TOTAL_SECS(sbi));
1769 dirty_i->victim_secmap = kzalloc(bitmap_size, GFP_KERNEL);
1770 if (!dirty_i->victim_secmap)
1775 static int build_dirty_segmap(struct f2fs_sb_info *sbi)
1777 struct dirty_seglist_info *dirty_i;
1778 unsigned int bitmap_size, i;
1780 /* allocate memory for dirty segments list information */
1781 dirty_i = kzalloc(sizeof(struct dirty_seglist_info), GFP_KERNEL);
1785 SM_I(sbi)->dirty_info = dirty_i;
1786 mutex_init(&dirty_i->seglist_lock);
1788 bitmap_size = f2fs_bitmap_size(TOTAL_SEGS(sbi));
1790 for (i = 0; i < NR_DIRTY_TYPE; i++) {
1791 dirty_i->dirty_segmap[i] = kzalloc(bitmap_size, GFP_KERNEL);
1792 if (!dirty_i->dirty_segmap[i])
1796 init_dirty_segmap(sbi);
1797 return init_victim_secmap(sbi);
1801 * Update min, max modified time for cost-benefit GC algorithm
1803 static void init_min_max_mtime(struct f2fs_sb_info *sbi)
1805 struct sit_info *sit_i = SIT_I(sbi);
1808 mutex_lock(&sit_i->sentry_lock);
1810 sit_i->min_mtime = LLONG_MAX;
1812 for (segno = 0; segno < TOTAL_SEGS(sbi); segno += sbi->segs_per_sec) {
1814 unsigned long long mtime = 0;
1816 for (i = 0; i < sbi->segs_per_sec; i++)
1817 mtime += get_seg_entry(sbi, segno + i)->mtime;
1819 mtime = div_u64(mtime, sbi->segs_per_sec);
1821 if (sit_i->min_mtime > mtime)
1822 sit_i->min_mtime = mtime;
1824 sit_i->max_mtime = get_mtime(sbi);
1825 mutex_unlock(&sit_i->sentry_lock);
1828 int build_segment_manager(struct f2fs_sb_info *sbi)
1830 struct f2fs_super_block *raw_super = F2FS_RAW_SUPER(sbi);
1831 struct f2fs_checkpoint *ckpt = F2FS_CKPT(sbi);
1832 struct f2fs_sm_info *sm_info;
1835 sm_info = kzalloc(sizeof(struct f2fs_sm_info), GFP_KERNEL);
1840 sbi->sm_info = sm_info;
1841 sm_info->seg0_blkaddr = le32_to_cpu(raw_super->segment0_blkaddr);
1842 sm_info->main_blkaddr = le32_to_cpu(raw_super->main_blkaddr);
1843 sm_info->segment_count = le32_to_cpu(raw_super->segment_count);
1844 sm_info->reserved_segments = le32_to_cpu(ckpt->rsvd_segment_count);
1845 sm_info->ovp_segments = le32_to_cpu(ckpt->overprov_segment_count);
1846 sm_info->main_segments = le32_to_cpu(raw_super->segment_count_main);
1847 sm_info->ssa_blkaddr = le32_to_cpu(raw_super->ssa_blkaddr);
1848 sm_info->rec_prefree_segments = sm_info->main_segments *
1849 DEF_RECLAIM_PREFREE_SEGMENTS / 100;
1850 sm_info->ipu_policy = F2FS_IPU_DISABLE;
1851 sm_info->min_ipu_util = DEF_MIN_IPU_UTIL;
1853 INIT_LIST_HEAD(&sm_info->discard_list);
1854 sm_info->nr_discards = 0;
1855 sm_info->max_discards = 0;
1857 if (test_opt(sbi, FLUSH_MERGE) && !f2fs_readonly(sbi->sb)) {
1858 err = create_flush_cmd_control(sbi);
1863 err = build_sit_info(sbi);
1866 err = build_free_segmap(sbi);
1869 err = build_curseg(sbi);
1873 /* reinit free segmap based on SIT */
1874 build_sit_entries(sbi);
1876 init_free_segmap(sbi);
1877 err = build_dirty_segmap(sbi);
1881 init_min_max_mtime(sbi);
1885 static void discard_dirty_segmap(struct f2fs_sb_info *sbi,
1886 enum dirty_type dirty_type)
1888 struct dirty_seglist_info *dirty_i = DIRTY_I(sbi);
1890 mutex_lock(&dirty_i->seglist_lock);
1891 kfree(dirty_i->dirty_segmap[dirty_type]);
1892 dirty_i->nr_dirty[dirty_type] = 0;
1893 mutex_unlock(&dirty_i->seglist_lock);
1896 static void destroy_victim_secmap(struct f2fs_sb_info *sbi)
1898 struct dirty_seglist_info *dirty_i = DIRTY_I(sbi);
1899 kfree(dirty_i->victim_secmap);
1902 static void destroy_dirty_segmap(struct f2fs_sb_info *sbi)
1904 struct dirty_seglist_info *dirty_i = DIRTY_I(sbi);
1910 /* discard pre-free/dirty segments list */
1911 for (i = 0; i < NR_DIRTY_TYPE; i++)
1912 discard_dirty_segmap(sbi, i);
1914 destroy_victim_secmap(sbi);
1915 SM_I(sbi)->dirty_info = NULL;
1919 static void destroy_curseg(struct f2fs_sb_info *sbi)
1921 struct curseg_info *array = SM_I(sbi)->curseg_array;
1926 SM_I(sbi)->curseg_array = NULL;
1927 for (i = 0; i < NR_CURSEG_TYPE; i++)
1928 kfree(array[i].sum_blk);
1932 static void destroy_free_segmap(struct f2fs_sb_info *sbi)
1934 struct free_segmap_info *free_i = SM_I(sbi)->free_info;
1937 SM_I(sbi)->free_info = NULL;
1938 kfree(free_i->free_segmap);
1939 kfree(free_i->free_secmap);
1943 static void destroy_sit_info(struct f2fs_sb_info *sbi)
1945 struct sit_info *sit_i = SIT_I(sbi);
1951 if (sit_i->sentries) {
1952 for (start = 0; start < TOTAL_SEGS(sbi); start++) {
1953 kfree(sit_i->sentries[start].cur_valid_map);
1954 kfree(sit_i->sentries[start].ckpt_valid_map);
1957 vfree(sit_i->sentries);
1958 vfree(sit_i->sec_entries);
1959 kfree(sit_i->dirty_sentries_bitmap);
1961 SM_I(sbi)->sit_info = NULL;
1962 kfree(sit_i->sit_bitmap);
1966 void destroy_segment_manager(struct f2fs_sb_info *sbi)
1968 struct f2fs_sm_info *sm_info = SM_I(sbi);
1972 destroy_flush_cmd_control(sbi);
1973 destroy_dirty_segmap(sbi);
1974 destroy_curseg(sbi);
1975 destroy_free_segmap(sbi);
1976 destroy_sit_info(sbi);
1977 sbi->sm_info = NULL;
1981 int __init create_segment_manager_caches(void)
1983 discard_entry_slab = f2fs_kmem_cache_create("discard_entry",
1984 sizeof(struct discard_entry));
1985 if (!discard_entry_slab)
1990 void destroy_segment_manager_caches(void)
1992 kmem_cache_destroy(discard_entry_slab);