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;
28 static struct kmem_cache *flush_cmd_slab;
31 * __reverse_ffs is copied from include/asm-generic/bitops/__ffs.h since
32 * MSB and LSB are reversed in a byte by f2fs_set_bit.
34 static inline unsigned long __reverse_ffs(unsigned long word)
38 #if BITS_PER_LONG == 64
39 if ((word & 0xffffffff) == 0) {
44 if ((word & 0xffff) == 0) {
48 if ((word & 0xff) == 0) {
52 if ((word & 0xf0) == 0)
56 if ((word & 0xc) == 0)
60 if ((word & 0x2) == 0)
66 * __find_rev_next(_zero)_bit is copied from lib/find_next_bit.c becasue
67 * f2fs_set_bit makes MSB and LSB reversed in a byte.
70 * f2fs_set_bit(0, bitmap) => 0000 0001
71 * f2fs_set_bit(7, bitmap) => 1000 0000
73 static unsigned long __find_rev_next_bit(const unsigned long *addr,
74 unsigned long size, unsigned long offset)
76 const unsigned long *p = addr + BIT_WORD(offset);
77 unsigned long result = offset & ~(BITS_PER_LONG - 1);
79 unsigned long mask, submask;
80 unsigned long quot, rest;
86 offset %= BITS_PER_LONG;
91 quot = (offset >> 3) << 3;
94 submask = (unsigned char)(0xff << rest) >> rest;
98 if (size < BITS_PER_LONG)
103 size -= BITS_PER_LONG;
104 result += BITS_PER_LONG;
106 while (size & ~(BITS_PER_LONG-1)) {
110 result += BITS_PER_LONG;
111 size -= BITS_PER_LONG;
117 tmp &= (~0UL >> (BITS_PER_LONG - size));
118 if (tmp == 0UL) /* Are any bits set? */
119 return result + size; /* Nope. */
121 return result + __reverse_ffs(tmp);
124 static unsigned long __find_rev_next_zero_bit(const unsigned long *addr,
125 unsigned long size, unsigned long offset)
127 const unsigned long *p = addr + BIT_WORD(offset);
128 unsigned long result = offset & ~(BITS_PER_LONG - 1);
130 unsigned long mask, submask;
131 unsigned long quot, rest;
137 offset %= BITS_PER_LONG;
142 quot = (offset >> 3) << 3;
144 mask = ~(~0UL << quot);
145 submask = (unsigned char)~((unsigned char)(0xff << rest) >> rest);
149 if (size < BITS_PER_LONG)
154 size -= BITS_PER_LONG;
155 result += BITS_PER_LONG;
157 while (size & ~(BITS_PER_LONG - 1)) {
161 result += BITS_PER_LONG;
162 size -= BITS_PER_LONG;
170 if (tmp == ~0UL) /* Are any bits zero? */
171 return result + size; /* Nope. */
173 return result + __reverse_ffz(tmp);
177 * This function balances dirty node and dentry pages.
178 * In addition, it controls garbage collection.
180 void f2fs_balance_fs(struct f2fs_sb_info *sbi)
183 * We should do GC or end up with checkpoint, if there are so many dirty
184 * dir/node pages without enough free segments.
186 if (has_not_enough_free_secs(sbi, 0)) {
187 mutex_lock(&sbi->gc_mutex);
192 void f2fs_balance_fs_bg(struct f2fs_sb_info *sbi)
194 /* check the # of cached NAT entries and prefree segments */
195 if (try_to_free_nats(sbi, NAT_ENTRY_PER_BLOCK) ||
196 excess_prefree_segs(sbi))
197 f2fs_sync_fs(sbi->sb, true);
200 static int issue_flush_thread(void *data)
202 struct f2fs_sb_info *sbi = data;
203 struct f2fs_sm_info *sm_i = SM_I(sbi);
204 wait_queue_head_t *q = &sm_i->flush_wait_queue;
206 if (kthread_should_stop())
209 spin_lock(&sm_i->issue_lock);
210 if (sm_i->issue_list) {
211 sm_i->dispatch_list = sm_i->issue_list;
212 sm_i->issue_list = sm_i->issue_tail = NULL;
214 spin_unlock(&sm_i->issue_lock);
216 if (sm_i->dispatch_list) {
217 struct bio *bio = bio_alloc(GFP_NOIO, 0);
218 struct flush_cmd *cmd, *next;
221 bio->bi_bdev = sbi->sb->s_bdev;
222 ret = submit_bio_wait(WRITE_FLUSH, bio);
224 for (cmd = sm_i->dispatch_list; cmd; cmd = next) {
227 complete(&cmd->wait);
229 sm_i->dispatch_list = NULL;
232 wait_event_interruptible(*q, kthread_should_stop() || sm_i->issue_list);
236 int f2fs_issue_flush(struct f2fs_sb_info *sbi)
238 struct f2fs_sm_info *sm_i = SM_I(sbi);
239 struct flush_cmd *cmd;
242 if (!test_opt(sbi, FLUSH_MERGE))
243 return blkdev_issue_flush(sbi->sb->s_bdev, GFP_KERNEL, NULL);
245 cmd = f2fs_kmem_cache_alloc(flush_cmd_slab, GFP_ATOMIC);
248 init_completion(&cmd->wait);
250 spin_lock(&sm_i->issue_lock);
251 if (sm_i->issue_list)
252 sm_i->issue_tail->next = cmd;
254 sm_i->issue_list = cmd;
255 sm_i->issue_tail = cmd;
256 spin_unlock(&sm_i->issue_lock);
258 if (!sm_i->dispatch_list)
259 wake_up(&sm_i->flush_wait_queue);
261 wait_for_completion(&cmd->wait);
263 kmem_cache_free(flush_cmd_slab, cmd);
267 static void __locate_dirty_segment(struct f2fs_sb_info *sbi, unsigned int segno,
268 enum dirty_type dirty_type)
270 struct dirty_seglist_info *dirty_i = DIRTY_I(sbi);
272 /* need not be added */
273 if (IS_CURSEG(sbi, segno))
276 if (!test_and_set_bit(segno, dirty_i->dirty_segmap[dirty_type]))
277 dirty_i->nr_dirty[dirty_type]++;
279 if (dirty_type == DIRTY) {
280 struct seg_entry *sentry = get_seg_entry(sbi, segno);
281 enum dirty_type t = sentry->type;
283 if (!test_and_set_bit(segno, dirty_i->dirty_segmap[t]))
284 dirty_i->nr_dirty[t]++;
288 static void __remove_dirty_segment(struct f2fs_sb_info *sbi, unsigned int segno,
289 enum dirty_type dirty_type)
291 struct dirty_seglist_info *dirty_i = DIRTY_I(sbi);
293 if (test_and_clear_bit(segno, dirty_i->dirty_segmap[dirty_type]))
294 dirty_i->nr_dirty[dirty_type]--;
296 if (dirty_type == DIRTY) {
297 struct seg_entry *sentry = get_seg_entry(sbi, segno);
298 enum dirty_type t = sentry->type;
300 if (test_and_clear_bit(segno, dirty_i->dirty_segmap[t]))
301 dirty_i->nr_dirty[t]--;
303 if (get_valid_blocks(sbi, segno, sbi->segs_per_sec) == 0)
304 clear_bit(GET_SECNO(sbi, segno),
305 dirty_i->victim_secmap);
310 * Should not occur error such as -ENOMEM.
311 * Adding dirty entry into seglist is not critical operation.
312 * If a given segment is one of current working segments, it won't be added.
314 static void locate_dirty_segment(struct f2fs_sb_info *sbi, unsigned int segno)
316 struct dirty_seglist_info *dirty_i = DIRTY_I(sbi);
317 unsigned short valid_blocks;
319 if (segno == NULL_SEGNO || IS_CURSEG(sbi, segno))
322 mutex_lock(&dirty_i->seglist_lock);
324 valid_blocks = get_valid_blocks(sbi, segno, 0);
326 if (valid_blocks == 0) {
327 __locate_dirty_segment(sbi, segno, PRE);
328 __remove_dirty_segment(sbi, segno, DIRTY);
329 } else if (valid_blocks < sbi->blocks_per_seg) {
330 __locate_dirty_segment(sbi, segno, DIRTY);
332 /* Recovery routine with SSR needs this */
333 __remove_dirty_segment(sbi, segno, DIRTY);
336 mutex_unlock(&dirty_i->seglist_lock);
339 static void f2fs_issue_discard(struct f2fs_sb_info *sbi,
340 block_t blkstart, block_t blklen)
342 sector_t start = SECTOR_FROM_BLOCK(sbi, blkstart);
343 sector_t len = SECTOR_FROM_BLOCK(sbi, blklen);
344 blkdev_issue_discard(sbi->sb->s_bdev, start, len, GFP_NOFS, 0);
345 trace_f2fs_issue_discard(sbi->sb, blkstart, blklen);
348 static void add_discard_addrs(struct f2fs_sb_info *sbi,
349 unsigned int segno, struct seg_entry *se)
351 struct list_head *head = &SM_I(sbi)->discard_list;
352 struct discard_entry *new;
353 int entries = SIT_VBLOCK_MAP_SIZE / sizeof(unsigned long);
354 int max_blocks = sbi->blocks_per_seg;
355 unsigned long *cur_map = (unsigned long *)se->cur_valid_map;
356 unsigned long *ckpt_map = (unsigned long *)se->ckpt_valid_map;
357 unsigned long dmap[entries];
358 unsigned int start = 0, end = -1;
361 if (!test_opt(sbi, DISCARD))
364 /* zero block will be discarded through the prefree list */
365 if (!se->valid_blocks || se->valid_blocks == max_blocks)
368 /* SIT_VBLOCK_MAP_SIZE should be multiple of sizeof(unsigned long) */
369 for (i = 0; i < entries; i++)
370 dmap[i] = (cur_map[i] ^ ckpt_map[i]) & ckpt_map[i];
372 while (SM_I(sbi)->nr_discards <= SM_I(sbi)->max_discards) {
373 start = __find_rev_next_bit(dmap, max_blocks, end + 1);
374 if (start >= max_blocks)
377 end = __find_rev_next_zero_bit(dmap, max_blocks, start + 1);
379 new = f2fs_kmem_cache_alloc(discard_entry_slab, GFP_NOFS);
380 INIT_LIST_HEAD(&new->list);
381 new->blkaddr = START_BLOCK(sbi, segno) + start;
382 new->len = end - start;
384 list_add_tail(&new->list, head);
385 SM_I(sbi)->nr_discards += end - start;
390 * Should call clear_prefree_segments after checkpoint is done.
392 static void set_prefree_as_free_segments(struct f2fs_sb_info *sbi)
394 struct dirty_seglist_info *dirty_i = DIRTY_I(sbi);
395 unsigned int segno = -1;
396 unsigned int total_segs = TOTAL_SEGS(sbi);
398 mutex_lock(&dirty_i->seglist_lock);
400 segno = find_next_bit(dirty_i->dirty_segmap[PRE], total_segs,
402 if (segno >= total_segs)
404 __set_test_and_free(sbi, segno);
406 mutex_unlock(&dirty_i->seglist_lock);
409 void clear_prefree_segments(struct f2fs_sb_info *sbi)
411 struct list_head *head = &(SM_I(sbi)->discard_list);
412 struct discard_entry *entry, *this;
413 struct dirty_seglist_info *dirty_i = DIRTY_I(sbi);
414 unsigned long *prefree_map = dirty_i->dirty_segmap[PRE];
415 unsigned int total_segs = TOTAL_SEGS(sbi);
416 unsigned int start = 0, end = -1;
418 mutex_lock(&dirty_i->seglist_lock);
422 start = find_next_bit(prefree_map, total_segs, end + 1);
423 if (start >= total_segs)
425 end = find_next_zero_bit(prefree_map, total_segs, start + 1);
427 for (i = start; i < end; i++)
428 clear_bit(i, prefree_map);
430 dirty_i->nr_dirty[PRE] -= end - start;
432 if (!test_opt(sbi, DISCARD))
435 f2fs_issue_discard(sbi, START_BLOCK(sbi, start),
436 (end - start) << sbi->log_blocks_per_seg);
438 mutex_unlock(&dirty_i->seglist_lock);
440 /* send small discards */
441 list_for_each_entry_safe(entry, this, head, list) {
442 f2fs_issue_discard(sbi, entry->blkaddr, entry->len);
443 list_del(&entry->list);
444 SM_I(sbi)->nr_discards -= entry->len;
445 kmem_cache_free(discard_entry_slab, entry);
449 static void __mark_sit_entry_dirty(struct f2fs_sb_info *sbi, unsigned int segno)
451 struct sit_info *sit_i = SIT_I(sbi);
452 if (!__test_and_set_bit(segno, sit_i->dirty_sentries_bitmap))
453 sit_i->dirty_sentries++;
456 static void __set_sit_entry_type(struct f2fs_sb_info *sbi, int type,
457 unsigned int segno, int modified)
459 struct seg_entry *se = get_seg_entry(sbi, segno);
462 __mark_sit_entry_dirty(sbi, segno);
465 static void update_sit_entry(struct f2fs_sb_info *sbi, block_t blkaddr, int del)
467 struct seg_entry *se;
468 unsigned int segno, offset;
469 long int new_vblocks;
471 segno = GET_SEGNO(sbi, blkaddr);
473 se = get_seg_entry(sbi, segno);
474 new_vblocks = se->valid_blocks + del;
475 offset = GET_BLKOFF_FROM_SEG0(sbi, blkaddr);
477 f2fs_bug_on((new_vblocks >> (sizeof(unsigned short) << 3) ||
478 (new_vblocks > sbi->blocks_per_seg)));
480 se->valid_blocks = new_vblocks;
481 se->mtime = get_mtime(sbi);
482 SIT_I(sbi)->max_mtime = se->mtime;
484 /* Update valid block bitmap */
486 if (f2fs_set_bit(offset, se->cur_valid_map))
489 if (!f2fs_clear_bit(offset, se->cur_valid_map))
492 if (!f2fs_test_bit(offset, se->ckpt_valid_map))
493 se->ckpt_valid_blocks += del;
495 __mark_sit_entry_dirty(sbi, segno);
497 /* update total number of valid blocks to be written in ckpt area */
498 SIT_I(sbi)->written_valid_blocks += del;
500 if (sbi->segs_per_sec > 1)
501 get_sec_entry(sbi, segno)->valid_blocks += del;
504 void refresh_sit_entry(struct f2fs_sb_info *sbi, block_t old, block_t new)
506 update_sit_entry(sbi, new, 1);
507 if (GET_SEGNO(sbi, old) != NULL_SEGNO)
508 update_sit_entry(sbi, old, -1);
510 locate_dirty_segment(sbi, GET_SEGNO(sbi, old));
511 locate_dirty_segment(sbi, GET_SEGNO(sbi, new));
514 void invalidate_blocks(struct f2fs_sb_info *sbi, block_t addr)
516 unsigned int segno = GET_SEGNO(sbi, addr);
517 struct sit_info *sit_i = SIT_I(sbi);
519 f2fs_bug_on(addr == NULL_ADDR);
520 if (addr == NEW_ADDR)
523 /* add it into sit main buffer */
524 mutex_lock(&sit_i->sentry_lock);
526 update_sit_entry(sbi, addr, -1);
528 /* add it into dirty seglist */
529 locate_dirty_segment(sbi, segno);
531 mutex_unlock(&sit_i->sentry_lock);
535 * This function should be resided under the curseg_mutex lock
537 static void __add_sum_entry(struct f2fs_sb_info *sbi, int type,
538 struct f2fs_summary *sum)
540 struct curseg_info *curseg = CURSEG_I(sbi, type);
541 void *addr = curseg->sum_blk;
542 addr += curseg->next_blkoff * sizeof(struct f2fs_summary);
543 memcpy(addr, sum, sizeof(struct f2fs_summary));
547 * Calculate the number of current summary pages for writing
549 int npages_for_summary_flush(struct f2fs_sb_info *sbi)
551 int valid_sum_count = 0;
554 for (i = CURSEG_HOT_DATA; i <= CURSEG_COLD_DATA; i++) {
555 if (sbi->ckpt->alloc_type[i] == SSR)
556 valid_sum_count += sbi->blocks_per_seg;
558 valid_sum_count += curseg_blkoff(sbi, i);
561 sum_in_page = (PAGE_CACHE_SIZE - 2 * SUM_JOURNAL_SIZE -
562 SUM_FOOTER_SIZE) / SUMMARY_SIZE;
563 if (valid_sum_count <= sum_in_page)
565 else if ((valid_sum_count - sum_in_page) <=
566 (PAGE_CACHE_SIZE - SUM_FOOTER_SIZE) / SUMMARY_SIZE)
572 * Caller should put this summary page
574 struct page *get_sum_page(struct f2fs_sb_info *sbi, unsigned int segno)
576 return get_meta_page(sbi, GET_SUM_BLOCK(sbi, segno));
579 static void write_sum_page(struct f2fs_sb_info *sbi,
580 struct f2fs_summary_block *sum_blk, block_t blk_addr)
582 struct page *page = grab_meta_page(sbi, blk_addr);
583 void *kaddr = page_address(page);
584 memcpy(kaddr, sum_blk, PAGE_CACHE_SIZE);
585 set_page_dirty(page);
586 f2fs_put_page(page, 1);
589 static int is_next_segment_free(struct f2fs_sb_info *sbi, int type)
591 struct curseg_info *curseg = CURSEG_I(sbi, type);
592 unsigned int segno = curseg->segno + 1;
593 struct free_segmap_info *free_i = FREE_I(sbi);
595 if (segno < TOTAL_SEGS(sbi) && segno % sbi->segs_per_sec)
596 return !test_bit(segno, free_i->free_segmap);
601 * Find a new segment from the free segments bitmap to right order
602 * This function should be returned with success, otherwise BUG
604 static void get_new_segment(struct f2fs_sb_info *sbi,
605 unsigned int *newseg, bool new_sec, int dir)
607 struct free_segmap_info *free_i = FREE_I(sbi);
608 unsigned int segno, secno, zoneno;
609 unsigned int total_zones = TOTAL_SECS(sbi) / sbi->secs_per_zone;
610 unsigned int hint = *newseg / sbi->segs_per_sec;
611 unsigned int old_zoneno = GET_ZONENO_FROM_SEGNO(sbi, *newseg);
612 unsigned int left_start = hint;
617 write_lock(&free_i->segmap_lock);
619 if (!new_sec && ((*newseg + 1) % sbi->segs_per_sec)) {
620 segno = find_next_zero_bit(free_i->free_segmap,
621 TOTAL_SEGS(sbi), *newseg + 1);
622 if (segno - *newseg < sbi->segs_per_sec -
623 (*newseg % sbi->segs_per_sec))
627 secno = find_next_zero_bit(free_i->free_secmap, TOTAL_SECS(sbi), hint);
628 if (secno >= TOTAL_SECS(sbi)) {
629 if (dir == ALLOC_RIGHT) {
630 secno = find_next_zero_bit(free_i->free_secmap,
632 f2fs_bug_on(secno >= TOTAL_SECS(sbi));
635 left_start = hint - 1;
641 while (test_bit(left_start, free_i->free_secmap)) {
642 if (left_start > 0) {
646 left_start = find_next_zero_bit(free_i->free_secmap,
648 f2fs_bug_on(left_start >= TOTAL_SECS(sbi));
654 segno = secno * sbi->segs_per_sec;
655 zoneno = secno / sbi->secs_per_zone;
657 /* give up on finding another zone */
660 if (sbi->secs_per_zone == 1)
662 if (zoneno == old_zoneno)
664 if (dir == ALLOC_LEFT) {
665 if (!go_left && zoneno + 1 >= total_zones)
667 if (go_left && zoneno == 0)
670 for (i = 0; i < NR_CURSEG_TYPE; i++)
671 if (CURSEG_I(sbi, i)->zone == zoneno)
674 if (i < NR_CURSEG_TYPE) {
675 /* zone is in user, try another */
677 hint = zoneno * sbi->secs_per_zone - 1;
678 else if (zoneno + 1 >= total_zones)
681 hint = (zoneno + 1) * sbi->secs_per_zone;
683 goto find_other_zone;
686 /* set it as dirty segment in free segmap */
687 f2fs_bug_on(test_bit(segno, free_i->free_segmap));
688 __set_inuse(sbi, segno);
690 write_unlock(&free_i->segmap_lock);
693 static void reset_curseg(struct f2fs_sb_info *sbi, int type, int modified)
695 struct curseg_info *curseg = CURSEG_I(sbi, type);
696 struct summary_footer *sum_footer;
698 curseg->segno = curseg->next_segno;
699 curseg->zone = GET_ZONENO_FROM_SEGNO(sbi, curseg->segno);
700 curseg->next_blkoff = 0;
701 curseg->next_segno = NULL_SEGNO;
703 sum_footer = &(curseg->sum_blk->footer);
704 memset(sum_footer, 0, sizeof(struct summary_footer));
705 if (IS_DATASEG(type))
706 SET_SUM_TYPE(sum_footer, SUM_TYPE_DATA);
707 if (IS_NODESEG(type))
708 SET_SUM_TYPE(sum_footer, SUM_TYPE_NODE);
709 __set_sit_entry_type(sbi, type, curseg->segno, modified);
713 * Allocate a current working segment.
714 * This function always allocates a free segment in LFS manner.
716 static void new_curseg(struct f2fs_sb_info *sbi, int type, bool new_sec)
718 struct curseg_info *curseg = CURSEG_I(sbi, type);
719 unsigned int segno = curseg->segno;
720 int dir = ALLOC_LEFT;
722 write_sum_page(sbi, curseg->sum_blk,
723 GET_SUM_BLOCK(sbi, segno));
724 if (type == CURSEG_WARM_DATA || type == CURSEG_COLD_DATA)
727 if (test_opt(sbi, NOHEAP))
730 get_new_segment(sbi, &segno, new_sec, dir);
731 curseg->next_segno = segno;
732 reset_curseg(sbi, type, 1);
733 curseg->alloc_type = LFS;
736 static void __next_free_blkoff(struct f2fs_sb_info *sbi,
737 struct curseg_info *seg, block_t start)
739 struct seg_entry *se = get_seg_entry(sbi, seg->segno);
740 int entries = SIT_VBLOCK_MAP_SIZE / sizeof(unsigned long);
741 unsigned long target_map[entries];
742 unsigned long *ckpt_map = (unsigned long *)se->ckpt_valid_map;
743 unsigned long *cur_map = (unsigned long *)se->cur_valid_map;
746 for (i = 0; i < entries; i++)
747 target_map[i] = ckpt_map[i] | cur_map[i];
749 pos = __find_rev_next_zero_bit(target_map, sbi->blocks_per_seg, start);
751 seg->next_blkoff = pos;
755 * If a segment is written by LFS manner, next block offset is just obtained
756 * by increasing the current block offset. However, if a segment is written by
757 * SSR manner, next block offset obtained by calling __next_free_blkoff
759 static void __refresh_next_blkoff(struct f2fs_sb_info *sbi,
760 struct curseg_info *seg)
762 if (seg->alloc_type == SSR)
763 __next_free_blkoff(sbi, seg, seg->next_blkoff + 1);
769 * This function always allocates a used segment (from dirty seglist) by SSR
770 * manner, so it should recover the existing segment information of valid blocks
772 static void change_curseg(struct f2fs_sb_info *sbi, int type, bool reuse)
774 struct dirty_seglist_info *dirty_i = DIRTY_I(sbi);
775 struct curseg_info *curseg = CURSEG_I(sbi, type);
776 unsigned int new_segno = curseg->next_segno;
777 struct f2fs_summary_block *sum_node;
778 struct page *sum_page;
780 write_sum_page(sbi, curseg->sum_blk,
781 GET_SUM_BLOCK(sbi, curseg->segno));
782 __set_test_and_inuse(sbi, new_segno);
784 mutex_lock(&dirty_i->seglist_lock);
785 __remove_dirty_segment(sbi, new_segno, PRE);
786 __remove_dirty_segment(sbi, new_segno, DIRTY);
787 mutex_unlock(&dirty_i->seglist_lock);
789 reset_curseg(sbi, type, 1);
790 curseg->alloc_type = SSR;
791 __next_free_blkoff(sbi, curseg, 0);
794 sum_page = get_sum_page(sbi, new_segno);
795 sum_node = (struct f2fs_summary_block *)page_address(sum_page);
796 memcpy(curseg->sum_blk, sum_node, SUM_ENTRY_SIZE);
797 f2fs_put_page(sum_page, 1);
801 static int get_ssr_segment(struct f2fs_sb_info *sbi, int type)
803 struct curseg_info *curseg = CURSEG_I(sbi, type);
804 const struct victim_selection *v_ops = DIRTY_I(sbi)->v_ops;
806 if (IS_NODESEG(type) || !has_not_enough_free_secs(sbi, 0))
807 return v_ops->get_victim(sbi,
808 &(curseg)->next_segno, BG_GC, type, SSR);
810 /* For data segments, let's do SSR more intensively */
811 for (; type >= CURSEG_HOT_DATA; type--)
812 if (v_ops->get_victim(sbi, &(curseg)->next_segno,
819 * flush out current segment and replace it with new segment
820 * This function should be returned with success, otherwise BUG
822 static void allocate_segment_by_default(struct f2fs_sb_info *sbi,
823 int type, bool force)
825 struct curseg_info *curseg = CURSEG_I(sbi, type);
828 new_curseg(sbi, type, true);
829 else if (type == CURSEG_WARM_NODE)
830 new_curseg(sbi, type, false);
831 else if (curseg->alloc_type == LFS && is_next_segment_free(sbi, type))
832 new_curseg(sbi, type, false);
833 else if (need_SSR(sbi) && get_ssr_segment(sbi, type))
834 change_curseg(sbi, type, true);
836 new_curseg(sbi, type, false);
838 stat_inc_seg_type(sbi, curseg);
841 void allocate_new_segments(struct f2fs_sb_info *sbi)
843 struct curseg_info *curseg;
844 unsigned int old_curseg;
847 for (i = CURSEG_HOT_DATA; i <= CURSEG_COLD_DATA; i++) {
848 curseg = CURSEG_I(sbi, i);
849 old_curseg = curseg->segno;
850 SIT_I(sbi)->s_ops->allocate_segment(sbi, i, true);
851 locate_dirty_segment(sbi, old_curseg);
855 static const struct segment_allocation default_salloc_ops = {
856 .allocate_segment = allocate_segment_by_default,
859 static bool __has_curseg_space(struct f2fs_sb_info *sbi, int type)
861 struct curseg_info *curseg = CURSEG_I(sbi, type);
862 if (curseg->next_blkoff < sbi->blocks_per_seg)
867 static int __get_segment_type_2(struct page *page, enum page_type p_type)
870 return CURSEG_HOT_DATA;
872 return CURSEG_HOT_NODE;
875 static int __get_segment_type_4(struct page *page, enum page_type p_type)
877 if (p_type == DATA) {
878 struct inode *inode = page->mapping->host;
880 if (S_ISDIR(inode->i_mode))
881 return CURSEG_HOT_DATA;
883 return CURSEG_COLD_DATA;
885 if (IS_DNODE(page) && !is_cold_node(page))
886 return CURSEG_HOT_NODE;
888 return CURSEG_COLD_NODE;
892 static int __get_segment_type_6(struct page *page, enum page_type p_type)
894 if (p_type == DATA) {
895 struct inode *inode = page->mapping->host;
897 if (S_ISDIR(inode->i_mode))
898 return CURSEG_HOT_DATA;
899 else if (is_cold_data(page) || file_is_cold(inode))
900 return CURSEG_COLD_DATA;
902 return CURSEG_WARM_DATA;
905 return is_cold_node(page) ? CURSEG_WARM_NODE :
908 return CURSEG_COLD_NODE;
912 static int __get_segment_type(struct page *page, enum page_type p_type)
914 struct f2fs_sb_info *sbi = F2FS_SB(page->mapping->host->i_sb);
915 switch (sbi->active_logs) {
917 return __get_segment_type_2(page, p_type);
919 return __get_segment_type_4(page, p_type);
921 /* NR_CURSEG_TYPE(6) logs by default */
922 f2fs_bug_on(sbi->active_logs != NR_CURSEG_TYPE);
923 return __get_segment_type_6(page, p_type);
926 void allocate_data_block(struct f2fs_sb_info *sbi, struct page *page,
927 block_t old_blkaddr, block_t *new_blkaddr,
928 struct f2fs_summary *sum, int type)
930 struct sit_info *sit_i = SIT_I(sbi);
931 struct curseg_info *curseg;
932 unsigned int old_cursegno;
934 curseg = CURSEG_I(sbi, type);
936 mutex_lock(&curseg->curseg_mutex);
938 *new_blkaddr = NEXT_FREE_BLKADDR(sbi, curseg);
939 old_cursegno = curseg->segno;
942 * __add_sum_entry should be resided under the curseg_mutex
943 * because, this function updates a summary entry in the
944 * current summary block.
946 __add_sum_entry(sbi, type, sum);
948 mutex_lock(&sit_i->sentry_lock);
949 __refresh_next_blkoff(sbi, curseg);
951 stat_inc_block_count(sbi, curseg);
953 if (!__has_curseg_space(sbi, type))
954 sit_i->s_ops->allocate_segment(sbi, type, false);
956 * SIT information should be updated before segment allocation,
957 * since SSR needs latest valid block information.
959 refresh_sit_entry(sbi, old_blkaddr, *new_blkaddr);
960 locate_dirty_segment(sbi, old_cursegno);
962 mutex_unlock(&sit_i->sentry_lock);
964 if (page && IS_NODESEG(type))
965 fill_node_footer_blkaddr(page, NEXT_FREE_BLKADDR(sbi, curseg));
967 mutex_unlock(&curseg->curseg_mutex);
970 static void do_write_page(struct f2fs_sb_info *sbi, struct page *page,
971 block_t old_blkaddr, block_t *new_blkaddr,
972 struct f2fs_summary *sum, struct f2fs_io_info *fio)
974 int type = __get_segment_type(page, fio->type);
976 allocate_data_block(sbi, page, old_blkaddr, new_blkaddr, sum, type);
978 /* writeout dirty page into bdev */
979 f2fs_submit_page_mbio(sbi, page, *new_blkaddr, fio);
982 void write_meta_page(struct f2fs_sb_info *sbi, struct page *page)
984 struct f2fs_io_info fio = {
986 .rw = WRITE_SYNC | REQ_META | REQ_PRIO
989 set_page_writeback(page);
990 f2fs_submit_page_mbio(sbi, page, page->index, &fio);
993 void write_node_page(struct f2fs_sb_info *sbi, struct page *page,
994 struct f2fs_io_info *fio,
995 unsigned int nid, block_t old_blkaddr, block_t *new_blkaddr)
997 struct f2fs_summary sum;
998 set_summary(&sum, nid, 0, 0);
999 do_write_page(sbi, page, old_blkaddr, new_blkaddr, &sum, fio);
1002 void write_data_page(struct page *page, struct dnode_of_data *dn,
1003 block_t *new_blkaddr, struct f2fs_io_info *fio)
1005 struct f2fs_sb_info *sbi = F2FS_SB(dn->inode->i_sb);
1006 struct f2fs_summary sum;
1007 struct node_info ni;
1009 f2fs_bug_on(dn->data_blkaddr == NULL_ADDR);
1010 get_node_info(sbi, dn->nid, &ni);
1011 set_summary(&sum, dn->nid, dn->ofs_in_node, ni.version);
1013 do_write_page(sbi, page, dn->data_blkaddr, new_blkaddr, &sum, fio);
1016 void rewrite_data_page(struct page *page, block_t old_blkaddr,
1017 struct f2fs_io_info *fio)
1019 struct inode *inode = page->mapping->host;
1020 struct f2fs_sb_info *sbi = F2FS_SB(inode->i_sb);
1021 f2fs_submit_page_mbio(sbi, page, old_blkaddr, fio);
1024 void recover_data_page(struct f2fs_sb_info *sbi,
1025 struct page *page, struct f2fs_summary *sum,
1026 block_t old_blkaddr, block_t new_blkaddr)
1028 struct sit_info *sit_i = SIT_I(sbi);
1029 struct curseg_info *curseg;
1030 unsigned int segno, old_cursegno;
1031 struct seg_entry *se;
1034 segno = GET_SEGNO(sbi, new_blkaddr);
1035 se = get_seg_entry(sbi, segno);
1038 if (se->valid_blocks == 0 && !IS_CURSEG(sbi, segno)) {
1039 if (old_blkaddr == NULL_ADDR)
1040 type = CURSEG_COLD_DATA;
1042 type = CURSEG_WARM_DATA;
1044 curseg = CURSEG_I(sbi, type);
1046 mutex_lock(&curseg->curseg_mutex);
1047 mutex_lock(&sit_i->sentry_lock);
1049 old_cursegno = curseg->segno;
1051 /* change the current segment */
1052 if (segno != curseg->segno) {
1053 curseg->next_segno = segno;
1054 change_curseg(sbi, type, true);
1057 curseg->next_blkoff = GET_BLKOFF_FROM_SEG0(sbi, new_blkaddr);
1058 __add_sum_entry(sbi, type, sum);
1060 refresh_sit_entry(sbi, old_blkaddr, new_blkaddr);
1061 locate_dirty_segment(sbi, old_cursegno);
1063 mutex_unlock(&sit_i->sentry_lock);
1064 mutex_unlock(&curseg->curseg_mutex);
1067 void rewrite_node_page(struct f2fs_sb_info *sbi,
1068 struct page *page, struct f2fs_summary *sum,
1069 block_t old_blkaddr, block_t new_blkaddr)
1071 struct sit_info *sit_i = SIT_I(sbi);
1072 int type = CURSEG_WARM_NODE;
1073 struct curseg_info *curseg;
1074 unsigned int segno, old_cursegno;
1075 block_t next_blkaddr = next_blkaddr_of_node(page);
1076 unsigned int next_segno = GET_SEGNO(sbi, next_blkaddr);
1077 struct f2fs_io_info fio = {
1082 curseg = CURSEG_I(sbi, type);
1084 mutex_lock(&curseg->curseg_mutex);
1085 mutex_lock(&sit_i->sentry_lock);
1087 segno = GET_SEGNO(sbi, new_blkaddr);
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);
1095 curseg->next_blkoff = GET_BLKOFF_FROM_SEG0(sbi, new_blkaddr);
1096 __add_sum_entry(sbi, type, sum);
1098 /* change the current log to the next block addr in advance */
1099 if (next_segno != segno) {
1100 curseg->next_segno = next_segno;
1101 change_curseg(sbi, type, true);
1103 curseg->next_blkoff = GET_BLKOFF_FROM_SEG0(sbi, next_blkaddr);
1105 /* rewrite node page */
1106 set_page_writeback(page);
1107 f2fs_submit_page_mbio(sbi, page, new_blkaddr, &fio);
1108 f2fs_submit_merged_bio(sbi, NODE, WRITE);
1109 refresh_sit_entry(sbi, old_blkaddr, new_blkaddr);
1110 locate_dirty_segment(sbi, old_cursegno);
1112 mutex_unlock(&sit_i->sentry_lock);
1113 mutex_unlock(&curseg->curseg_mutex);
1116 static inline bool is_merged_page(struct f2fs_sb_info *sbi,
1117 struct page *page, enum page_type type)
1119 enum page_type btype = PAGE_TYPE_OF_BIO(type);
1120 struct f2fs_bio_info *io = &sbi->write_io[btype];
1121 struct bio_vec *bvec;
1124 down_read(&io->io_rwsem);
1128 bio_for_each_segment_all(bvec, io->bio, i) {
1129 if (page == bvec->bv_page) {
1130 up_read(&io->io_rwsem);
1136 up_read(&io->io_rwsem);
1140 void f2fs_wait_on_page_writeback(struct page *page,
1141 enum page_type type)
1143 struct f2fs_sb_info *sbi = F2FS_SB(page->mapping->host->i_sb);
1144 if (PageWriteback(page)) {
1145 if (is_merged_page(sbi, page, type))
1146 f2fs_submit_merged_bio(sbi, type, WRITE);
1147 wait_on_page_writeback(page);
1151 static int read_compacted_summaries(struct f2fs_sb_info *sbi)
1153 struct f2fs_checkpoint *ckpt = F2FS_CKPT(sbi);
1154 struct curseg_info *seg_i;
1155 unsigned char *kaddr;
1160 start = start_sum_block(sbi);
1162 page = get_meta_page(sbi, start++);
1163 kaddr = (unsigned char *)page_address(page);
1165 /* Step 1: restore nat cache */
1166 seg_i = CURSEG_I(sbi, CURSEG_HOT_DATA);
1167 memcpy(&seg_i->sum_blk->n_nats, kaddr, SUM_JOURNAL_SIZE);
1169 /* Step 2: restore sit cache */
1170 seg_i = CURSEG_I(sbi, CURSEG_COLD_DATA);
1171 memcpy(&seg_i->sum_blk->n_sits, kaddr + SUM_JOURNAL_SIZE,
1173 offset = 2 * SUM_JOURNAL_SIZE;
1175 /* Step 3: restore summary entries */
1176 for (i = CURSEG_HOT_DATA; i <= CURSEG_COLD_DATA; i++) {
1177 unsigned short blk_off;
1180 seg_i = CURSEG_I(sbi, i);
1181 segno = le32_to_cpu(ckpt->cur_data_segno[i]);
1182 blk_off = le16_to_cpu(ckpt->cur_data_blkoff[i]);
1183 seg_i->next_segno = segno;
1184 reset_curseg(sbi, i, 0);
1185 seg_i->alloc_type = ckpt->alloc_type[i];
1186 seg_i->next_blkoff = blk_off;
1188 if (seg_i->alloc_type == SSR)
1189 blk_off = sbi->blocks_per_seg;
1191 for (j = 0; j < blk_off; j++) {
1192 struct f2fs_summary *s;
1193 s = (struct f2fs_summary *)(kaddr + offset);
1194 seg_i->sum_blk->entries[j] = *s;
1195 offset += SUMMARY_SIZE;
1196 if (offset + SUMMARY_SIZE <= PAGE_CACHE_SIZE -
1200 f2fs_put_page(page, 1);
1203 page = get_meta_page(sbi, start++);
1204 kaddr = (unsigned char *)page_address(page);
1208 f2fs_put_page(page, 1);
1212 static int read_normal_summaries(struct f2fs_sb_info *sbi, int type)
1214 struct f2fs_checkpoint *ckpt = F2FS_CKPT(sbi);
1215 struct f2fs_summary_block *sum;
1216 struct curseg_info *curseg;
1218 unsigned short blk_off;
1219 unsigned int segno = 0;
1220 block_t blk_addr = 0;
1222 /* get segment number and block addr */
1223 if (IS_DATASEG(type)) {
1224 segno = le32_to_cpu(ckpt->cur_data_segno[type]);
1225 blk_off = le16_to_cpu(ckpt->cur_data_blkoff[type -
1227 if (is_set_ckpt_flags(ckpt, CP_UMOUNT_FLAG))
1228 blk_addr = sum_blk_addr(sbi, NR_CURSEG_TYPE, type);
1230 blk_addr = sum_blk_addr(sbi, NR_CURSEG_DATA_TYPE, type);
1232 segno = le32_to_cpu(ckpt->cur_node_segno[type -
1234 blk_off = le16_to_cpu(ckpt->cur_node_blkoff[type -
1236 if (is_set_ckpt_flags(ckpt, CP_UMOUNT_FLAG))
1237 blk_addr = sum_blk_addr(sbi, NR_CURSEG_NODE_TYPE,
1238 type - CURSEG_HOT_NODE);
1240 blk_addr = GET_SUM_BLOCK(sbi, segno);
1243 new = get_meta_page(sbi, blk_addr);
1244 sum = (struct f2fs_summary_block *)page_address(new);
1246 if (IS_NODESEG(type)) {
1247 if (is_set_ckpt_flags(ckpt, CP_UMOUNT_FLAG)) {
1248 struct f2fs_summary *ns = &sum->entries[0];
1250 for (i = 0; i < sbi->blocks_per_seg; i++, ns++) {
1252 ns->ofs_in_node = 0;
1257 err = restore_node_summary(sbi, segno, sum);
1259 f2fs_put_page(new, 1);
1265 /* set uncompleted segment to curseg */
1266 curseg = CURSEG_I(sbi, type);
1267 mutex_lock(&curseg->curseg_mutex);
1268 memcpy(curseg->sum_blk, sum, PAGE_CACHE_SIZE);
1269 curseg->next_segno = segno;
1270 reset_curseg(sbi, type, 0);
1271 curseg->alloc_type = ckpt->alloc_type[type];
1272 curseg->next_blkoff = blk_off;
1273 mutex_unlock(&curseg->curseg_mutex);
1274 f2fs_put_page(new, 1);
1278 static int restore_curseg_summaries(struct f2fs_sb_info *sbi)
1280 int type = CURSEG_HOT_DATA;
1283 if (is_set_ckpt_flags(F2FS_CKPT(sbi), CP_COMPACT_SUM_FLAG)) {
1284 /* restore for compacted data summary */
1285 if (read_compacted_summaries(sbi))
1287 type = CURSEG_HOT_NODE;
1290 for (; type <= CURSEG_COLD_NODE; type++) {
1291 err = read_normal_summaries(sbi, type);
1299 static void write_compacted_summaries(struct f2fs_sb_info *sbi, block_t blkaddr)
1302 unsigned char *kaddr;
1303 struct f2fs_summary *summary;
1304 struct curseg_info *seg_i;
1305 int written_size = 0;
1308 page = grab_meta_page(sbi, blkaddr++);
1309 kaddr = (unsigned char *)page_address(page);
1311 /* Step 1: write nat cache */
1312 seg_i = CURSEG_I(sbi, CURSEG_HOT_DATA);
1313 memcpy(kaddr, &seg_i->sum_blk->n_nats, SUM_JOURNAL_SIZE);
1314 written_size += SUM_JOURNAL_SIZE;
1316 /* Step 2: write sit cache */
1317 seg_i = CURSEG_I(sbi, CURSEG_COLD_DATA);
1318 memcpy(kaddr + written_size, &seg_i->sum_blk->n_sits,
1320 written_size += SUM_JOURNAL_SIZE;
1322 /* Step 3: write summary entries */
1323 for (i = CURSEG_HOT_DATA; i <= CURSEG_COLD_DATA; i++) {
1324 unsigned short blkoff;
1325 seg_i = CURSEG_I(sbi, i);
1326 if (sbi->ckpt->alloc_type[i] == SSR)
1327 blkoff = sbi->blocks_per_seg;
1329 blkoff = curseg_blkoff(sbi, i);
1331 for (j = 0; j < blkoff; j++) {
1333 page = grab_meta_page(sbi, blkaddr++);
1334 kaddr = (unsigned char *)page_address(page);
1337 summary = (struct f2fs_summary *)(kaddr + written_size);
1338 *summary = seg_i->sum_blk->entries[j];
1339 written_size += SUMMARY_SIZE;
1341 if (written_size + SUMMARY_SIZE <= PAGE_CACHE_SIZE -
1345 set_page_dirty(page);
1346 f2fs_put_page(page, 1);
1351 set_page_dirty(page);
1352 f2fs_put_page(page, 1);
1356 static void write_normal_summaries(struct f2fs_sb_info *sbi,
1357 block_t blkaddr, int type)
1360 if (IS_DATASEG(type))
1361 end = type + NR_CURSEG_DATA_TYPE;
1363 end = type + NR_CURSEG_NODE_TYPE;
1365 for (i = type; i < end; i++) {
1366 struct curseg_info *sum = CURSEG_I(sbi, i);
1367 mutex_lock(&sum->curseg_mutex);
1368 write_sum_page(sbi, sum->sum_blk, blkaddr + (i - type));
1369 mutex_unlock(&sum->curseg_mutex);
1373 void write_data_summaries(struct f2fs_sb_info *sbi, block_t start_blk)
1375 if (is_set_ckpt_flags(F2FS_CKPT(sbi), CP_COMPACT_SUM_FLAG))
1376 write_compacted_summaries(sbi, start_blk);
1378 write_normal_summaries(sbi, start_blk, CURSEG_HOT_DATA);
1381 void write_node_summaries(struct f2fs_sb_info *sbi, block_t start_blk)
1383 if (is_set_ckpt_flags(F2FS_CKPT(sbi), CP_UMOUNT_FLAG))
1384 write_normal_summaries(sbi, start_blk, CURSEG_HOT_NODE);
1387 int lookup_journal_in_cursum(struct f2fs_summary_block *sum, int type,
1388 unsigned int val, int alloc)
1392 if (type == NAT_JOURNAL) {
1393 for (i = 0; i < nats_in_cursum(sum); i++) {
1394 if (le32_to_cpu(nid_in_journal(sum, i)) == val)
1397 if (alloc && nats_in_cursum(sum) < NAT_JOURNAL_ENTRIES)
1398 return update_nats_in_cursum(sum, 1);
1399 } else if (type == SIT_JOURNAL) {
1400 for (i = 0; i < sits_in_cursum(sum); i++)
1401 if (le32_to_cpu(segno_in_journal(sum, i)) == val)
1403 if (alloc && sits_in_cursum(sum) < SIT_JOURNAL_ENTRIES)
1404 return update_sits_in_cursum(sum, 1);
1409 static struct page *get_current_sit_page(struct f2fs_sb_info *sbi,
1412 struct sit_info *sit_i = SIT_I(sbi);
1413 unsigned int offset = SIT_BLOCK_OFFSET(sit_i, segno);
1414 block_t blk_addr = sit_i->sit_base_addr + offset;
1416 check_seg_range(sbi, segno);
1418 /* calculate sit block address */
1419 if (f2fs_test_bit(offset, sit_i->sit_bitmap))
1420 blk_addr += sit_i->sit_blocks;
1422 return get_meta_page(sbi, blk_addr);
1425 static struct page *get_next_sit_page(struct f2fs_sb_info *sbi,
1428 struct sit_info *sit_i = SIT_I(sbi);
1429 struct page *src_page, *dst_page;
1430 pgoff_t src_off, dst_off;
1431 void *src_addr, *dst_addr;
1433 src_off = current_sit_addr(sbi, start);
1434 dst_off = next_sit_addr(sbi, src_off);
1436 /* get current sit block page without lock */
1437 src_page = get_meta_page(sbi, src_off);
1438 dst_page = grab_meta_page(sbi, dst_off);
1439 f2fs_bug_on(PageDirty(src_page));
1441 src_addr = page_address(src_page);
1442 dst_addr = page_address(dst_page);
1443 memcpy(dst_addr, src_addr, PAGE_CACHE_SIZE);
1445 set_page_dirty(dst_page);
1446 f2fs_put_page(src_page, 1);
1448 set_to_next_sit(sit_i, start);
1453 static bool flush_sits_in_journal(struct f2fs_sb_info *sbi)
1455 struct curseg_info *curseg = CURSEG_I(sbi, CURSEG_COLD_DATA);
1456 struct f2fs_summary_block *sum = curseg->sum_blk;
1460 * If the journal area in the current summary is full of sit entries,
1461 * all the sit entries will be flushed. Otherwise the sit entries
1462 * are not able to replace with newly hot sit entries.
1464 if (sits_in_cursum(sum) >= SIT_JOURNAL_ENTRIES) {
1465 for (i = sits_in_cursum(sum) - 1; i >= 0; i--) {
1467 segno = le32_to_cpu(segno_in_journal(sum, i));
1468 __mark_sit_entry_dirty(sbi, segno);
1470 update_sits_in_cursum(sum, -sits_in_cursum(sum));
1477 * CP calls this function, which flushes SIT entries including sit_journal,
1478 * and moves prefree segs to free segs.
1480 void flush_sit_entries(struct f2fs_sb_info *sbi)
1482 struct sit_info *sit_i = SIT_I(sbi);
1483 unsigned long *bitmap = sit_i->dirty_sentries_bitmap;
1484 struct curseg_info *curseg = CURSEG_I(sbi, CURSEG_COLD_DATA);
1485 struct f2fs_summary_block *sum = curseg->sum_blk;
1486 unsigned long nsegs = TOTAL_SEGS(sbi);
1487 struct page *page = NULL;
1488 struct f2fs_sit_block *raw_sit = NULL;
1489 unsigned int start = 0, end = 0;
1490 unsigned int segno = -1;
1493 mutex_lock(&curseg->curseg_mutex);
1494 mutex_lock(&sit_i->sentry_lock);
1497 * "flushed" indicates whether sit entries in journal are flushed
1498 * to the SIT area or not.
1500 flushed = flush_sits_in_journal(sbi);
1502 while ((segno = find_next_bit(bitmap, nsegs, segno + 1)) < nsegs) {
1503 struct seg_entry *se = get_seg_entry(sbi, segno);
1504 int sit_offset, offset;
1506 sit_offset = SIT_ENTRY_OFFSET(sit_i, segno);
1508 /* add discard candidates */
1509 if (SM_I(sbi)->nr_discards < SM_I(sbi)->max_discards)
1510 add_discard_addrs(sbi, segno, se);
1515 offset = lookup_journal_in_cursum(sum, SIT_JOURNAL, segno, 1);
1517 segno_in_journal(sum, offset) = cpu_to_le32(segno);
1518 seg_info_to_raw_sit(se, &sit_in_journal(sum, offset));
1522 if (!page || (start > segno) || (segno > end)) {
1524 f2fs_put_page(page, 1);
1528 start = START_SEGNO(sit_i, segno);
1529 end = start + SIT_ENTRY_PER_BLOCK - 1;
1531 /* read sit block that will be updated */
1532 page = get_next_sit_page(sbi, start);
1533 raw_sit = page_address(page);
1536 /* udpate entry in SIT block */
1537 seg_info_to_raw_sit(se, &raw_sit->entries[sit_offset]);
1539 __clear_bit(segno, bitmap);
1540 sit_i->dirty_sentries--;
1542 mutex_unlock(&sit_i->sentry_lock);
1543 mutex_unlock(&curseg->curseg_mutex);
1545 /* writeout last modified SIT block */
1546 f2fs_put_page(page, 1);
1548 set_prefree_as_free_segments(sbi);
1551 static int build_sit_info(struct f2fs_sb_info *sbi)
1553 struct f2fs_super_block *raw_super = F2FS_RAW_SUPER(sbi);
1554 struct f2fs_checkpoint *ckpt = F2FS_CKPT(sbi);
1555 struct sit_info *sit_i;
1556 unsigned int sit_segs, start;
1557 char *src_bitmap, *dst_bitmap;
1558 unsigned int bitmap_size;
1560 /* allocate memory for SIT information */
1561 sit_i = kzalloc(sizeof(struct sit_info), GFP_KERNEL);
1565 SM_I(sbi)->sit_info = sit_i;
1567 sit_i->sentries = vzalloc(TOTAL_SEGS(sbi) * sizeof(struct seg_entry));
1568 if (!sit_i->sentries)
1571 bitmap_size = f2fs_bitmap_size(TOTAL_SEGS(sbi));
1572 sit_i->dirty_sentries_bitmap = kzalloc(bitmap_size, GFP_KERNEL);
1573 if (!sit_i->dirty_sentries_bitmap)
1576 for (start = 0; start < TOTAL_SEGS(sbi); start++) {
1577 sit_i->sentries[start].cur_valid_map
1578 = kzalloc(SIT_VBLOCK_MAP_SIZE, GFP_KERNEL);
1579 sit_i->sentries[start].ckpt_valid_map
1580 = kzalloc(SIT_VBLOCK_MAP_SIZE, GFP_KERNEL);
1581 if (!sit_i->sentries[start].cur_valid_map
1582 || !sit_i->sentries[start].ckpt_valid_map)
1586 if (sbi->segs_per_sec > 1) {
1587 sit_i->sec_entries = vzalloc(TOTAL_SECS(sbi) *
1588 sizeof(struct sec_entry));
1589 if (!sit_i->sec_entries)
1593 /* get information related with SIT */
1594 sit_segs = le32_to_cpu(raw_super->segment_count_sit) >> 1;
1596 /* setup SIT bitmap from ckeckpoint pack */
1597 bitmap_size = __bitmap_size(sbi, SIT_BITMAP);
1598 src_bitmap = __bitmap_ptr(sbi, SIT_BITMAP);
1600 dst_bitmap = kmemdup(src_bitmap, bitmap_size, GFP_KERNEL);
1604 /* init SIT information */
1605 sit_i->s_ops = &default_salloc_ops;
1607 sit_i->sit_base_addr = le32_to_cpu(raw_super->sit_blkaddr);
1608 sit_i->sit_blocks = sit_segs << sbi->log_blocks_per_seg;
1609 sit_i->written_valid_blocks = le64_to_cpu(ckpt->valid_block_count);
1610 sit_i->sit_bitmap = dst_bitmap;
1611 sit_i->bitmap_size = bitmap_size;
1612 sit_i->dirty_sentries = 0;
1613 sit_i->sents_per_block = SIT_ENTRY_PER_BLOCK;
1614 sit_i->elapsed_time = le64_to_cpu(sbi->ckpt->elapsed_time);
1615 sit_i->mounted_time = CURRENT_TIME_SEC.tv_sec;
1616 mutex_init(&sit_i->sentry_lock);
1620 static int build_free_segmap(struct f2fs_sb_info *sbi)
1622 struct f2fs_sm_info *sm_info = SM_I(sbi);
1623 struct free_segmap_info *free_i;
1624 unsigned int bitmap_size, sec_bitmap_size;
1626 /* allocate memory for free segmap information */
1627 free_i = kzalloc(sizeof(struct free_segmap_info), GFP_KERNEL);
1631 SM_I(sbi)->free_info = free_i;
1633 bitmap_size = f2fs_bitmap_size(TOTAL_SEGS(sbi));
1634 free_i->free_segmap = kmalloc(bitmap_size, GFP_KERNEL);
1635 if (!free_i->free_segmap)
1638 sec_bitmap_size = f2fs_bitmap_size(TOTAL_SECS(sbi));
1639 free_i->free_secmap = kmalloc(sec_bitmap_size, GFP_KERNEL);
1640 if (!free_i->free_secmap)
1643 /* set all segments as dirty temporarily */
1644 memset(free_i->free_segmap, 0xff, bitmap_size);
1645 memset(free_i->free_secmap, 0xff, sec_bitmap_size);
1647 /* init free segmap information */
1648 free_i->start_segno =
1649 (unsigned int) GET_SEGNO_FROM_SEG0(sbi, sm_info->main_blkaddr);
1650 free_i->free_segments = 0;
1651 free_i->free_sections = 0;
1652 rwlock_init(&free_i->segmap_lock);
1656 static int build_curseg(struct f2fs_sb_info *sbi)
1658 struct curseg_info *array;
1661 array = kzalloc(sizeof(*array) * NR_CURSEG_TYPE, GFP_KERNEL);
1665 SM_I(sbi)->curseg_array = array;
1667 for (i = 0; i < NR_CURSEG_TYPE; i++) {
1668 mutex_init(&array[i].curseg_mutex);
1669 array[i].sum_blk = kzalloc(PAGE_CACHE_SIZE, GFP_KERNEL);
1670 if (!array[i].sum_blk)
1672 array[i].segno = NULL_SEGNO;
1673 array[i].next_blkoff = 0;
1675 return restore_curseg_summaries(sbi);
1678 static void build_sit_entries(struct f2fs_sb_info *sbi)
1680 struct sit_info *sit_i = SIT_I(sbi);
1681 struct curseg_info *curseg = CURSEG_I(sbi, CURSEG_COLD_DATA);
1682 struct f2fs_summary_block *sum = curseg->sum_blk;
1683 int sit_blk_cnt = SIT_BLK_CNT(sbi);
1684 unsigned int i, start, end;
1685 unsigned int readed, start_blk = 0;
1686 int nrpages = MAX_BIO_BLOCKS(max_hw_blocks(sbi));
1689 readed = ra_meta_pages(sbi, start_blk, nrpages, META_SIT);
1691 start = start_blk * sit_i->sents_per_block;
1692 end = (start_blk + readed) * sit_i->sents_per_block;
1694 for (; start < end && start < TOTAL_SEGS(sbi); start++) {
1695 struct seg_entry *se = &sit_i->sentries[start];
1696 struct f2fs_sit_block *sit_blk;
1697 struct f2fs_sit_entry sit;
1700 mutex_lock(&curseg->curseg_mutex);
1701 for (i = 0; i < sits_in_cursum(sum); i++) {
1702 if (le32_to_cpu(segno_in_journal(sum, i))
1704 sit = sit_in_journal(sum, i);
1705 mutex_unlock(&curseg->curseg_mutex);
1709 mutex_unlock(&curseg->curseg_mutex);
1711 page = get_current_sit_page(sbi, start);
1712 sit_blk = (struct f2fs_sit_block *)page_address(page);
1713 sit = sit_blk->entries[SIT_ENTRY_OFFSET(sit_i, start)];
1714 f2fs_put_page(page, 1);
1716 check_block_count(sbi, start, &sit);
1717 seg_info_from_raw_sit(se, &sit);
1718 if (sbi->segs_per_sec > 1) {
1719 struct sec_entry *e = get_sec_entry(sbi, start);
1720 e->valid_blocks += se->valid_blocks;
1723 start_blk += readed;
1724 } while (start_blk < sit_blk_cnt);
1727 static void init_free_segmap(struct f2fs_sb_info *sbi)
1732 for (start = 0; start < TOTAL_SEGS(sbi); start++) {
1733 struct seg_entry *sentry = get_seg_entry(sbi, start);
1734 if (!sentry->valid_blocks)
1735 __set_free(sbi, start);
1738 /* set use the current segments */
1739 for (type = CURSEG_HOT_DATA; type <= CURSEG_COLD_NODE; type++) {
1740 struct curseg_info *curseg_t = CURSEG_I(sbi, type);
1741 __set_test_and_inuse(sbi, curseg_t->segno);
1745 static void init_dirty_segmap(struct f2fs_sb_info *sbi)
1747 struct dirty_seglist_info *dirty_i = DIRTY_I(sbi);
1748 struct free_segmap_info *free_i = FREE_I(sbi);
1749 unsigned int segno = 0, offset = 0, total_segs = TOTAL_SEGS(sbi);
1750 unsigned short valid_blocks;
1753 /* find dirty segment based on free segmap */
1754 segno = find_next_inuse(free_i, total_segs, offset);
1755 if (segno >= total_segs)
1758 valid_blocks = get_valid_blocks(sbi, segno, 0);
1759 if (valid_blocks >= sbi->blocks_per_seg || !valid_blocks)
1761 mutex_lock(&dirty_i->seglist_lock);
1762 __locate_dirty_segment(sbi, segno, DIRTY);
1763 mutex_unlock(&dirty_i->seglist_lock);
1767 static int init_victim_secmap(struct f2fs_sb_info *sbi)
1769 struct dirty_seglist_info *dirty_i = DIRTY_I(sbi);
1770 unsigned int bitmap_size = f2fs_bitmap_size(TOTAL_SECS(sbi));
1772 dirty_i->victim_secmap = kzalloc(bitmap_size, GFP_KERNEL);
1773 if (!dirty_i->victim_secmap)
1778 static int build_dirty_segmap(struct f2fs_sb_info *sbi)
1780 struct dirty_seglist_info *dirty_i;
1781 unsigned int bitmap_size, i;
1783 /* allocate memory for dirty segments list information */
1784 dirty_i = kzalloc(sizeof(struct dirty_seglist_info), GFP_KERNEL);
1788 SM_I(sbi)->dirty_info = dirty_i;
1789 mutex_init(&dirty_i->seglist_lock);
1791 bitmap_size = f2fs_bitmap_size(TOTAL_SEGS(sbi));
1793 for (i = 0; i < NR_DIRTY_TYPE; i++) {
1794 dirty_i->dirty_segmap[i] = kzalloc(bitmap_size, GFP_KERNEL);
1795 if (!dirty_i->dirty_segmap[i])
1799 init_dirty_segmap(sbi);
1800 return init_victim_secmap(sbi);
1804 * Update min, max modified time for cost-benefit GC algorithm
1806 static void init_min_max_mtime(struct f2fs_sb_info *sbi)
1808 struct sit_info *sit_i = SIT_I(sbi);
1811 mutex_lock(&sit_i->sentry_lock);
1813 sit_i->min_mtime = LLONG_MAX;
1815 for (segno = 0; segno < TOTAL_SEGS(sbi); segno += sbi->segs_per_sec) {
1817 unsigned long long mtime = 0;
1819 for (i = 0; i < sbi->segs_per_sec; i++)
1820 mtime += get_seg_entry(sbi, segno + i)->mtime;
1822 mtime = div_u64(mtime, sbi->segs_per_sec);
1824 if (sit_i->min_mtime > mtime)
1825 sit_i->min_mtime = mtime;
1827 sit_i->max_mtime = get_mtime(sbi);
1828 mutex_unlock(&sit_i->sentry_lock);
1831 int build_segment_manager(struct f2fs_sb_info *sbi)
1833 struct f2fs_super_block *raw_super = F2FS_RAW_SUPER(sbi);
1834 struct f2fs_checkpoint *ckpt = F2FS_CKPT(sbi);
1835 dev_t dev = sbi->sb->s_bdev->bd_dev;
1836 struct f2fs_sm_info *sm_info;
1839 sm_info = kzalloc(sizeof(struct f2fs_sm_info), GFP_KERNEL);
1844 sbi->sm_info = sm_info;
1845 INIT_LIST_HEAD(&sm_info->wblist_head);
1846 spin_lock_init(&sm_info->wblist_lock);
1847 sm_info->seg0_blkaddr = le32_to_cpu(raw_super->segment0_blkaddr);
1848 sm_info->main_blkaddr = le32_to_cpu(raw_super->main_blkaddr);
1849 sm_info->segment_count = le32_to_cpu(raw_super->segment_count);
1850 sm_info->reserved_segments = le32_to_cpu(ckpt->rsvd_segment_count);
1851 sm_info->ovp_segments = le32_to_cpu(ckpt->overprov_segment_count);
1852 sm_info->main_segments = le32_to_cpu(raw_super->segment_count_main);
1853 sm_info->ssa_blkaddr = le32_to_cpu(raw_super->ssa_blkaddr);
1854 sm_info->rec_prefree_segments = sm_info->main_segments *
1855 DEF_RECLAIM_PREFREE_SEGMENTS / 100;
1856 sm_info->ipu_policy = F2FS_IPU_DISABLE;
1857 sm_info->min_ipu_util = DEF_MIN_IPU_UTIL;
1859 INIT_LIST_HEAD(&sm_info->discard_list);
1860 sm_info->nr_discards = 0;
1861 sm_info->max_discards = 0;
1863 if (test_opt(sbi, FLUSH_MERGE)) {
1864 spin_lock_init(&sm_info->issue_lock);
1865 init_waitqueue_head(&sm_info->flush_wait_queue);
1867 sm_info->f2fs_issue_flush = kthread_run(issue_flush_thread, sbi,
1868 "f2fs_flush-%u:%u", MAJOR(dev), MINOR(dev));
1869 if (IS_ERR(sm_info->f2fs_issue_flush))
1870 return PTR_ERR(sm_info->f2fs_issue_flush);
1873 err = build_sit_info(sbi);
1876 err = build_free_segmap(sbi);
1879 err = build_curseg(sbi);
1883 /* reinit free segmap based on SIT */
1884 build_sit_entries(sbi);
1886 init_free_segmap(sbi);
1887 err = build_dirty_segmap(sbi);
1891 init_min_max_mtime(sbi);
1895 static void discard_dirty_segmap(struct f2fs_sb_info *sbi,
1896 enum dirty_type dirty_type)
1898 struct dirty_seglist_info *dirty_i = DIRTY_I(sbi);
1900 mutex_lock(&dirty_i->seglist_lock);
1901 kfree(dirty_i->dirty_segmap[dirty_type]);
1902 dirty_i->nr_dirty[dirty_type] = 0;
1903 mutex_unlock(&dirty_i->seglist_lock);
1906 static void destroy_victim_secmap(struct f2fs_sb_info *sbi)
1908 struct dirty_seglist_info *dirty_i = DIRTY_I(sbi);
1909 kfree(dirty_i->victim_secmap);
1912 static void destroy_dirty_segmap(struct f2fs_sb_info *sbi)
1914 struct dirty_seglist_info *dirty_i = DIRTY_I(sbi);
1920 /* discard pre-free/dirty segments list */
1921 for (i = 0; i < NR_DIRTY_TYPE; i++)
1922 discard_dirty_segmap(sbi, i);
1924 destroy_victim_secmap(sbi);
1925 SM_I(sbi)->dirty_info = NULL;
1929 static void destroy_curseg(struct f2fs_sb_info *sbi)
1931 struct curseg_info *array = SM_I(sbi)->curseg_array;
1936 SM_I(sbi)->curseg_array = NULL;
1937 for (i = 0; i < NR_CURSEG_TYPE; i++)
1938 kfree(array[i].sum_blk);
1942 static void destroy_free_segmap(struct f2fs_sb_info *sbi)
1944 struct free_segmap_info *free_i = SM_I(sbi)->free_info;
1947 SM_I(sbi)->free_info = NULL;
1948 kfree(free_i->free_segmap);
1949 kfree(free_i->free_secmap);
1953 static void destroy_sit_info(struct f2fs_sb_info *sbi)
1955 struct sit_info *sit_i = SIT_I(sbi);
1961 if (sit_i->sentries) {
1962 for (start = 0; start < TOTAL_SEGS(sbi); start++) {
1963 kfree(sit_i->sentries[start].cur_valid_map);
1964 kfree(sit_i->sentries[start].ckpt_valid_map);
1967 vfree(sit_i->sentries);
1968 vfree(sit_i->sec_entries);
1969 kfree(sit_i->dirty_sentries_bitmap);
1971 SM_I(sbi)->sit_info = NULL;
1972 kfree(sit_i->sit_bitmap);
1976 void destroy_segment_manager(struct f2fs_sb_info *sbi)
1978 struct f2fs_sm_info *sm_info = SM_I(sbi);
1981 if (sm_info->f2fs_issue_flush)
1982 kthread_stop(sm_info->f2fs_issue_flush);
1983 destroy_dirty_segmap(sbi);
1984 destroy_curseg(sbi);
1985 destroy_free_segmap(sbi);
1986 destroy_sit_info(sbi);
1987 sbi->sm_info = NULL;
1991 int __init create_segment_manager_caches(void)
1993 discard_entry_slab = f2fs_kmem_cache_create("discard_entry",
1994 sizeof(struct discard_entry));
1995 if (!discard_entry_slab)
1997 flush_cmd_slab = f2fs_kmem_cache_create("flush_command",
1998 sizeof(struct flush_cmd));
1999 if (!flush_cmd_slab) {
2000 kmem_cache_destroy(discard_entry_slab);
2006 void destroy_segment_manager_caches(void)
2008 kmem_cache_destroy(discard_entry_slab);
2009 kmem_cache_destroy(flush_cmd_slab);