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>
24 #include <trace/events/f2fs.h>
26 #define __reverse_ffz(x) __reverse_ffs(~(x))
28 static struct kmem_cache *discard_entry_slab;
29 static struct kmem_cache *sit_entry_set_slab;
30 static struct kmem_cache *inmem_entry_slab;
33 * __reverse_ffs is copied from include/asm-generic/bitops/__ffs.h since
34 * MSB and LSB are reversed in a byte by f2fs_set_bit.
36 static inline unsigned long __reverse_ffs(unsigned long word)
40 #if BITS_PER_LONG == 64
41 if ((word & 0xffffffff) == 0) {
46 if ((word & 0xffff) == 0) {
50 if ((word & 0xff) == 0) {
54 if ((word & 0xf0) == 0)
58 if ((word & 0xc) == 0)
62 if ((word & 0x2) == 0)
68 * __find_rev_next(_zero)_bit is copied from lib/find_next_bit.c because
69 * f2fs_set_bit makes MSB and LSB reversed in a byte.
72 * f2fs_set_bit(0, bitmap) => 0000 0001
73 * f2fs_set_bit(7, bitmap) => 1000 0000
75 static unsigned long __find_rev_next_bit(const unsigned long *addr,
76 unsigned long size, unsigned long offset)
78 const unsigned long *p = addr + BIT_WORD(offset);
79 unsigned long result = offset & ~(BITS_PER_LONG - 1);
81 unsigned long mask, submask;
82 unsigned long quot, rest;
88 offset %= BITS_PER_LONG;
93 quot = (offset >> 3) << 3;
96 submask = (unsigned char)(0xff << rest) >> rest;
100 if (size < BITS_PER_LONG)
105 size -= BITS_PER_LONG;
106 result += BITS_PER_LONG;
108 while (size & ~(BITS_PER_LONG-1)) {
112 result += BITS_PER_LONG;
113 size -= BITS_PER_LONG;
119 tmp &= (~0UL >> (BITS_PER_LONG - size));
120 if (tmp == 0UL) /* Are any bits set? */
121 return result + size; /* Nope. */
123 return result + __reverse_ffs(tmp);
126 static unsigned long __find_rev_next_zero_bit(const unsigned long *addr,
127 unsigned long size, unsigned long offset)
129 const unsigned long *p = addr + BIT_WORD(offset);
130 unsigned long result = offset & ~(BITS_PER_LONG - 1);
132 unsigned long mask, submask;
133 unsigned long quot, rest;
139 offset %= BITS_PER_LONG;
144 quot = (offset >> 3) << 3;
146 mask = ~(~0UL << quot);
147 submask = (unsigned char)~((unsigned char)(0xff << rest) >> rest);
151 if (size < BITS_PER_LONG)
156 size -= BITS_PER_LONG;
157 result += BITS_PER_LONG;
159 while (size & ~(BITS_PER_LONG - 1)) {
163 result += BITS_PER_LONG;
164 size -= BITS_PER_LONG;
172 if (tmp == ~0UL) /* Are any bits zero? */
173 return result + size; /* Nope. */
175 return result + __reverse_ffz(tmp);
178 void register_inmem_page(struct inode *inode, struct page *page)
180 struct f2fs_inode_info *fi = F2FS_I(inode);
181 struct inmem_pages *new;
184 SetPagePrivate(page);
185 f2fs_trace_pid(page);
187 new = f2fs_kmem_cache_alloc(inmem_entry_slab, GFP_NOFS);
189 /* add atomic page indices to the list */
191 INIT_LIST_HEAD(&new->list);
193 /* increase reference count with clean state */
194 mutex_lock(&fi->inmem_lock);
195 err = radix_tree_insert(&fi->inmem_root, page->index, new);
196 if (err == -EEXIST) {
197 mutex_unlock(&fi->inmem_lock);
198 kmem_cache_free(inmem_entry_slab, new);
201 mutex_unlock(&fi->inmem_lock);
205 list_add_tail(&new->list, &fi->inmem_pages);
206 inc_page_count(F2FS_I_SB(inode), F2FS_INMEM_PAGES);
207 mutex_unlock(&fi->inmem_lock);
210 void commit_inmem_pages(struct inode *inode, bool abort)
212 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
213 struct f2fs_inode_info *fi = F2FS_I(inode);
214 struct inmem_pages *cur, *tmp;
215 bool submit_bio = false;
216 struct f2fs_io_info fio = {
218 .rw = WRITE_SYNC | REQ_PRIO,
222 * The abort is true only when f2fs_evict_inode is called.
223 * Basically, the f2fs_evict_inode doesn't produce any data writes, so
224 * that we don't need to call f2fs_balance_fs.
225 * Otherwise, f2fs_gc in f2fs_balance_fs can wait forever until this
226 * inode becomes free by iget_locked in f2fs_iget.
229 f2fs_balance_fs(sbi);
233 mutex_lock(&fi->inmem_lock);
234 list_for_each_entry_safe(cur, tmp, &fi->inmem_pages, list) {
236 lock_page(cur->page);
237 if (cur->page->mapping == inode->i_mapping) {
238 f2fs_wait_on_page_writeback(cur->page, DATA);
239 if (clear_page_dirty_for_io(cur->page))
240 inode_dec_dirty_pages(inode);
241 do_write_data_page(cur->page, &fio);
244 f2fs_put_page(cur->page, 1);
248 radix_tree_delete(&fi->inmem_root, cur->page->index);
249 list_del(&cur->list);
250 kmem_cache_free(inmem_entry_slab, cur);
251 dec_page_count(F2FS_I_SB(inode), F2FS_INMEM_PAGES);
253 mutex_unlock(&fi->inmem_lock);
258 f2fs_submit_merged_bio(sbi, DATA, WRITE);
263 * This function balances dirty node and dentry pages.
264 * In addition, it controls garbage collection.
266 void f2fs_balance_fs(struct f2fs_sb_info *sbi)
269 * We should do GC or end up with checkpoint, if there are so many dirty
270 * dir/node pages without enough free segments.
272 if (has_not_enough_free_secs(sbi, 0)) {
273 mutex_lock(&sbi->gc_mutex);
278 void f2fs_balance_fs_bg(struct f2fs_sb_info *sbi)
280 /* check the # of cached NAT entries and prefree segments */
281 if (try_to_free_nats(sbi, NAT_ENTRY_PER_BLOCK) ||
282 excess_prefree_segs(sbi) ||
283 !available_free_memory(sbi, INO_ENTRIES))
284 f2fs_sync_fs(sbi->sb, true);
287 static int issue_flush_thread(void *data)
289 struct f2fs_sb_info *sbi = data;
290 struct flush_cmd_control *fcc = SM_I(sbi)->cmd_control_info;
291 wait_queue_head_t *q = &fcc->flush_wait_queue;
293 if (kthread_should_stop())
296 if (!llist_empty(&fcc->issue_list)) {
297 struct bio *bio = bio_alloc(GFP_NOIO, 0);
298 struct flush_cmd *cmd, *next;
301 fcc->dispatch_list = llist_del_all(&fcc->issue_list);
302 fcc->dispatch_list = llist_reverse_order(fcc->dispatch_list);
304 bio->bi_bdev = sbi->sb->s_bdev;
305 ret = submit_bio_wait(WRITE_FLUSH, bio);
307 llist_for_each_entry_safe(cmd, next,
308 fcc->dispatch_list, llnode) {
310 complete(&cmd->wait);
313 fcc->dispatch_list = NULL;
316 wait_event_interruptible(*q,
317 kthread_should_stop() || !llist_empty(&fcc->issue_list));
321 int f2fs_issue_flush(struct f2fs_sb_info *sbi)
323 struct flush_cmd_control *fcc = SM_I(sbi)->cmd_control_info;
324 struct flush_cmd cmd;
326 trace_f2fs_issue_flush(sbi->sb, test_opt(sbi, NOBARRIER),
327 test_opt(sbi, FLUSH_MERGE));
329 if (test_opt(sbi, NOBARRIER))
332 if (!test_opt(sbi, FLUSH_MERGE))
333 return blkdev_issue_flush(sbi->sb->s_bdev, GFP_KERNEL, NULL);
335 init_completion(&cmd.wait);
337 llist_add(&cmd.llnode, &fcc->issue_list);
339 if (!fcc->dispatch_list)
340 wake_up(&fcc->flush_wait_queue);
342 wait_for_completion(&cmd.wait);
347 int create_flush_cmd_control(struct f2fs_sb_info *sbi)
349 dev_t dev = sbi->sb->s_bdev->bd_dev;
350 struct flush_cmd_control *fcc;
353 fcc = kzalloc(sizeof(struct flush_cmd_control), GFP_KERNEL);
356 init_waitqueue_head(&fcc->flush_wait_queue);
357 init_llist_head(&fcc->issue_list);
358 SM_I(sbi)->cmd_control_info = fcc;
359 fcc->f2fs_issue_flush = kthread_run(issue_flush_thread, sbi,
360 "f2fs_flush-%u:%u", MAJOR(dev), MINOR(dev));
361 if (IS_ERR(fcc->f2fs_issue_flush)) {
362 err = PTR_ERR(fcc->f2fs_issue_flush);
364 SM_I(sbi)->cmd_control_info = NULL;
371 void destroy_flush_cmd_control(struct f2fs_sb_info *sbi)
373 struct flush_cmd_control *fcc = SM_I(sbi)->cmd_control_info;
375 if (fcc && fcc->f2fs_issue_flush)
376 kthread_stop(fcc->f2fs_issue_flush);
378 SM_I(sbi)->cmd_control_info = NULL;
381 static void __locate_dirty_segment(struct f2fs_sb_info *sbi, unsigned int segno,
382 enum dirty_type dirty_type)
384 struct dirty_seglist_info *dirty_i = DIRTY_I(sbi);
386 /* need not be added */
387 if (IS_CURSEG(sbi, segno))
390 if (!test_and_set_bit(segno, dirty_i->dirty_segmap[dirty_type]))
391 dirty_i->nr_dirty[dirty_type]++;
393 if (dirty_type == DIRTY) {
394 struct seg_entry *sentry = get_seg_entry(sbi, segno);
395 enum dirty_type t = sentry->type;
397 if (unlikely(t >= DIRTY)) {
401 if (!test_and_set_bit(segno, dirty_i->dirty_segmap[t]))
402 dirty_i->nr_dirty[t]++;
406 static void __remove_dirty_segment(struct f2fs_sb_info *sbi, unsigned int segno,
407 enum dirty_type dirty_type)
409 struct dirty_seglist_info *dirty_i = DIRTY_I(sbi);
411 if (test_and_clear_bit(segno, dirty_i->dirty_segmap[dirty_type]))
412 dirty_i->nr_dirty[dirty_type]--;
414 if (dirty_type == DIRTY) {
415 struct seg_entry *sentry = get_seg_entry(sbi, segno);
416 enum dirty_type t = sentry->type;
418 if (test_and_clear_bit(segno, dirty_i->dirty_segmap[t]))
419 dirty_i->nr_dirty[t]--;
421 if (get_valid_blocks(sbi, segno, sbi->segs_per_sec) == 0)
422 clear_bit(GET_SECNO(sbi, segno),
423 dirty_i->victim_secmap);
428 * Should not occur error such as -ENOMEM.
429 * Adding dirty entry into seglist is not critical operation.
430 * If a given segment is one of current working segments, it won't be added.
432 static void locate_dirty_segment(struct f2fs_sb_info *sbi, unsigned int segno)
434 struct dirty_seglist_info *dirty_i = DIRTY_I(sbi);
435 unsigned short valid_blocks;
437 if (segno == NULL_SEGNO || IS_CURSEG(sbi, segno))
440 mutex_lock(&dirty_i->seglist_lock);
442 valid_blocks = get_valid_blocks(sbi, segno, 0);
444 if (valid_blocks == 0) {
445 __locate_dirty_segment(sbi, segno, PRE);
446 __remove_dirty_segment(sbi, segno, DIRTY);
447 } else if (valid_blocks < sbi->blocks_per_seg) {
448 __locate_dirty_segment(sbi, segno, DIRTY);
450 /* Recovery routine with SSR needs this */
451 __remove_dirty_segment(sbi, segno, DIRTY);
454 mutex_unlock(&dirty_i->seglist_lock);
457 static int f2fs_issue_discard(struct f2fs_sb_info *sbi,
458 block_t blkstart, block_t blklen)
460 sector_t start = SECTOR_FROM_BLOCK(blkstart);
461 sector_t len = SECTOR_FROM_BLOCK(blklen);
462 trace_f2fs_issue_discard(sbi->sb, blkstart, blklen);
463 return blkdev_issue_discard(sbi->sb->s_bdev, start, len, GFP_NOFS, 0);
466 void discard_next_dnode(struct f2fs_sb_info *sbi, block_t blkaddr)
468 if (f2fs_issue_discard(sbi, blkaddr, 1)) {
469 struct page *page = grab_meta_page(sbi, blkaddr);
470 /* zero-filled page */
471 set_page_dirty(page);
472 f2fs_put_page(page, 1);
476 static void __add_discard_entry(struct f2fs_sb_info *sbi,
477 struct cp_control *cpc, unsigned int start, unsigned int end)
479 struct list_head *head = &SM_I(sbi)->discard_list;
480 struct discard_entry *new, *last;
482 if (!list_empty(head)) {
483 last = list_last_entry(head, struct discard_entry, list);
484 if (START_BLOCK(sbi, cpc->trim_start) + start ==
485 last->blkaddr + last->len) {
486 last->len += end - start;
491 new = f2fs_kmem_cache_alloc(discard_entry_slab, GFP_NOFS);
492 INIT_LIST_HEAD(&new->list);
493 new->blkaddr = START_BLOCK(sbi, cpc->trim_start) + start;
494 new->len = end - start;
495 list_add_tail(&new->list, head);
497 SM_I(sbi)->nr_discards += end - start;
498 cpc->trimmed += end - start;
501 static void add_discard_addrs(struct f2fs_sb_info *sbi, struct cp_control *cpc)
503 int entries = SIT_VBLOCK_MAP_SIZE / sizeof(unsigned long);
504 int max_blocks = sbi->blocks_per_seg;
505 struct seg_entry *se = get_seg_entry(sbi, cpc->trim_start);
506 unsigned long *cur_map = (unsigned long *)se->cur_valid_map;
507 unsigned long *ckpt_map = (unsigned long *)se->ckpt_valid_map;
508 unsigned long dmap[entries];
509 unsigned int start = 0, end = -1;
510 bool force = (cpc->reason == CP_DISCARD);
513 if (!force && (!test_opt(sbi, DISCARD) ||
514 SM_I(sbi)->nr_discards >= SM_I(sbi)->max_discards))
517 if (force && !se->valid_blocks) {
518 struct dirty_seglist_info *dirty_i = DIRTY_I(sbi);
520 * if this segment is registered in the prefree list, then
521 * we should skip adding a discard candidate, and let the
522 * checkpoint do that later.
524 mutex_lock(&dirty_i->seglist_lock);
525 if (test_bit(cpc->trim_start, dirty_i->dirty_segmap[PRE])) {
526 mutex_unlock(&dirty_i->seglist_lock);
527 cpc->trimmed += sbi->blocks_per_seg;
530 mutex_unlock(&dirty_i->seglist_lock);
532 __add_discard_entry(sbi, cpc, 0, sbi->blocks_per_seg);
536 /* zero block will be discarded through the prefree list */
537 if (!se->valid_blocks || se->valid_blocks == max_blocks)
540 /* SIT_VBLOCK_MAP_SIZE should be multiple of sizeof(unsigned long) */
541 for (i = 0; i < entries; i++)
542 dmap[i] = force ? ~ckpt_map[i] :
543 (cur_map[i] ^ ckpt_map[i]) & ckpt_map[i];
545 while (force || SM_I(sbi)->nr_discards <= SM_I(sbi)->max_discards) {
546 start = __find_rev_next_bit(dmap, max_blocks, end + 1);
547 if (start >= max_blocks)
550 end = __find_rev_next_zero_bit(dmap, max_blocks, start + 1);
552 if (end - start < cpc->trim_minlen)
555 __add_discard_entry(sbi, cpc, start, end);
559 void release_discard_addrs(struct f2fs_sb_info *sbi)
561 struct list_head *head = &(SM_I(sbi)->discard_list);
562 struct discard_entry *entry, *this;
565 list_for_each_entry_safe(entry, this, head, list) {
566 list_del(&entry->list);
567 kmem_cache_free(discard_entry_slab, entry);
572 * Should call clear_prefree_segments after checkpoint is done.
574 static void set_prefree_as_free_segments(struct f2fs_sb_info *sbi)
576 struct dirty_seglist_info *dirty_i = DIRTY_I(sbi);
579 mutex_lock(&dirty_i->seglist_lock);
580 for_each_set_bit(segno, dirty_i->dirty_segmap[PRE], MAIN_SEGS(sbi))
581 __set_test_and_free(sbi, segno);
582 mutex_unlock(&dirty_i->seglist_lock);
585 void clear_prefree_segments(struct f2fs_sb_info *sbi)
587 struct list_head *head = &(SM_I(sbi)->discard_list);
588 struct discard_entry *entry, *this;
589 struct dirty_seglist_info *dirty_i = DIRTY_I(sbi);
590 unsigned long *prefree_map = dirty_i->dirty_segmap[PRE];
591 unsigned int start = 0, end = -1;
593 mutex_lock(&dirty_i->seglist_lock);
597 start = find_next_bit(prefree_map, MAIN_SEGS(sbi), end + 1);
598 if (start >= MAIN_SEGS(sbi))
600 end = find_next_zero_bit(prefree_map, MAIN_SEGS(sbi),
603 for (i = start; i < end; i++)
604 clear_bit(i, prefree_map);
606 dirty_i->nr_dirty[PRE] -= end - start;
608 if (!test_opt(sbi, DISCARD))
611 f2fs_issue_discard(sbi, START_BLOCK(sbi, start),
612 (end - start) << sbi->log_blocks_per_seg);
614 mutex_unlock(&dirty_i->seglist_lock);
616 /* send small discards */
617 list_for_each_entry_safe(entry, this, head, list) {
618 f2fs_issue_discard(sbi, entry->blkaddr, entry->len);
619 list_del(&entry->list);
620 SM_I(sbi)->nr_discards -= entry->len;
621 kmem_cache_free(discard_entry_slab, entry);
625 static bool __mark_sit_entry_dirty(struct f2fs_sb_info *sbi, unsigned int segno)
627 struct sit_info *sit_i = SIT_I(sbi);
629 if (!__test_and_set_bit(segno, sit_i->dirty_sentries_bitmap)) {
630 sit_i->dirty_sentries++;
637 static void __set_sit_entry_type(struct f2fs_sb_info *sbi, int type,
638 unsigned int segno, int modified)
640 struct seg_entry *se = get_seg_entry(sbi, segno);
643 __mark_sit_entry_dirty(sbi, segno);
646 static void update_sit_entry(struct f2fs_sb_info *sbi, block_t blkaddr, int del)
648 struct seg_entry *se;
649 unsigned int segno, offset;
650 long int new_vblocks;
652 segno = GET_SEGNO(sbi, blkaddr);
654 se = get_seg_entry(sbi, segno);
655 new_vblocks = se->valid_blocks + del;
656 offset = GET_BLKOFF_FROM_SEG0(sbi, blkaddr);
658 f2fs_bug_on(sbi, (new_vblocks >> (sizeof(unsigned short) << 3) ||
659 (new_vblocks > sbi->blocks_per_seg)));
661 se->valid_blocks = new_vblocks;
662 se->mtime = get_mtime(sbi);
663 SIT_I(sbi)->max_mtime = se->mtime;
665 /* Update valid block bitmap */
667 if (f2fs_test_and_set_bit(offset, se->cur_valid_map))
670 if (!f2fs_test_and_clear_bit(offset, se->cur_valid_map))
673 if (!f2fs_test_bit(offset, se->ckpt_valid_map))
674 se->ckpt_valid_blocks += del;
676 __mark_sit_entry_dirty(sbi, segno);
678 /* update total number of valid blocks to be written in ckpt area */
679 SIT_I(sbi)->written_valid_blocks += del;
681 if (sbi->segs_per_sec > 1)
682 get_sec_entry(sbi, segno)->valid_blocks += del;
685 void refresh_sit_entry(struct f2fs_sb_info *sbi, block_t old, block_t new)
687 update_sit_entry(sbi, new, 1);
688 if (GET_SEGNO(sbi, old) != NULL_SEGNO)
689 update_sit_entry(sbi, old, -1);
691 locate_dirty_segment(sbi, GET_SEGNO(sbi, old));
692 locate_dirty_segment(sbi, GET_SEGNO(sbi, new));
695 void invalidate_blocks(struct f2fs_sb_info *sbi, block_t addr)
697 unsigned int segno = GET_SEGNO(sbi, addr);
698 struct sit_info *sit_i = SIT_I(sbi);
700 f2fs_bug_on(sbi, addr == NULL_ADDR);
701 if (addr == NEW_ADDR)
704 /* add it into sit main buffer */
705 mutex_lock(&sit_i->sentry_lock);
707 update_sit_entry(sbi, addr, -1);
709 /* add it into dirty seglist */
710 locate_dirty_segment(sbi, segno);
712 mutex_unlock(&sit_i->sentry_lock);
716 * This function should be resided under the curseg_mutex lock
718 static void __add_sum_entry(struct f2fs_sb_info *sbi, int type,
719 struct f2fs_summary *sum)
721 struct curseg_info *curseg = CURSEG_I(sbi, type);
722 void *addr = curseg->sum_blk;
723 addr += curseg->next_blkoff * sizeof(struct f2fs_summary);
724 memcpy(addr, sum, sizeof(struct f2fs_summary));
728 * Calculate the number of current summary pages for writing
730 int npages_for_summary_flush(struct f2fs_sb_info *sbi, bool for_ra)
732 int valid_sum_count = 0;
735 for (i = CURSEG_HOT_DATA; i <= CURSEG_COLD_DATA; i++) {
736 if (sbi->ckpt->alloc_type[i] == SSR)
737 valid_sum_count += sbi->blocks_per_seg;
740 valid_sum_count += le16_to_cpu(
741 F2FS_CKPT(sbi)->cur_data_blkoff[i]);
743 valid_sum_count += curseg_blkoff(sbi, i);
747 sum_in_page = (PAGE_CACHE_SIZE - 2 * SUM_JOURNAL_SIZE -
748 SUM_FOOTER_SIZE) / SUMMARY_SIZE;
749 if (valid_sum_count <= sum_in_page)
751 else if ((valid_sum_count - sum_in_page) <=
752 (PAGE_CACHE_SIZE - SUM_FOOTER_SIZE) / SUMMARY_SIZE)
758 * Caller should put this summary page
760 struct page *get_sum_page(struct f2fs_sb_info *sbi, unsigned int segno)
762 return get_meta_page(sbi, GET_SUM_BLOCK(sbi, segno));
765 static void write_sum_page(struct f2fs_sb_info *sbi,
766 struct f2fs_summary_block *sum_blk, block_t blk_addr)
768 struct page *page = grab_meta_page(sbi, blk_addr);
769 void *kaddr = page_address(page);
770 memcpy(kaddr, sum_blk, PAGE_CACHE_SIZE);
771 set_page_dirty(page);
772 f2fs_put_page(page, 1);
775 static int is_next_segment_free(struct f2fs_sb_info *sbi, int type)
777 struct curseg_info *curseg = CURSEG_I(sbi, type);
778 unsigned int segno = curseg->segno + 1;
779 struct free_segmap_info *free_i = FREE_I(sbi);
781 if (segno < MAIN_SEGS(sbi) && segno % sbi->segs_per_sec)
782 return !test_bit(segno, free_i->free_segmap);
787 * Find a new segment from the free segments bitmap to right order
788 * This function should be returned with success, otherwise BUG
790 static void get_new_segment(struct f2fs_sb_info *sbi,
791 unsigned int *newseg, bool new_sec, int dir)
793 struct free_segmap_info *free_i = FREE_I(sbi);
794 unsigned int segno, secno, zoneno;
795 unsigned int total_zones = MAIN_SECS(sbi) / sbi->secs_per_zone;
796 unsigned int hint = *newseg / sbi->segs_per_sec;
797 unsigned int old_zoneno = GET_ZONENO_FROM_SEGNO(sbi, *newseg);
798 unsigned int left_start = hint;
803 write_lock(&free_i->segmap_lock);
805 if (!new_sec && ((*newseg + 1) % sbi->segs_per_sec)) {
806 segno = find_next_zero_bit(free_i->free_segmap,
807 MAIN_SEGS(sbi), *newseg + 1);
808 if (segno - *newseg < sbi->segs_per_sec -
809 (*newseg % sbi->segs_per_sec))
813 secno = find_next_zero_bit(free_i->free_secmap, MAIN_SECS(sbi), hint);
814 if (secno >= MAIN_SECS(sbi)) {
815 if (dir == ALLOC_RIGHT) {
816 secno = find_next_zero_bit(free_i->free_secmap,
818 f2fs_bug_on(sbi, secno >= MAIN_SECS(sbi));
821 left_start = hint - 1;
827 while (test_bit(left_start, free_i->free_secmap)) {
828 if (left_start > 0) {
832 left_start = find_next_zero_bit(free_i->free_secmap,
834 f2fs_bug_on(sbi, left_start >= MAIN_SECS(sbi));
840 segno = secno * sbi->segs_per_sec;
841 zoneno = secno / sbi->secs_per_zone;
843 /* give up on finding another zone */
846 if (sbi->secs_per_zone == 1)
848 if (zoneno == old_zoneno)
850 if (dir == ALLOC_LEFT) {
851 if (!go_left && zoneno + 1 >= total_zones)
853 if (go_left && zoneno == 0)
856 for (i = 0; i < NR_CURSEG_TYPE; i++)
857 if (CURSEG_I(sbi, i)->zone == zoneno)
860 if (i < NR_CURSEG_TYPE) {
861 /* zone is in user, try another */
863 hint = zoneno * sbi->secs_per_zone - 1;
864 else if (zoneno + 1 >= total_zones)
867 hint = (zoneno + 1) * sbi->secs_per_zone;
869 goto find_other_zone;
872 /* set it as dirty segment in free segmap */
873 f2fs_bug_on(sbi, test_bit(segno, free_i->free_segmap));
874 __set_inuse(sbi, segno);
876 write_unlock(&free_i->segmap_lock);
879 static void reset_curseg(struct f2fs_sb_info *sbi, int type, int modified)
881 struct curseg_info *curseg = CURSEG_I(sbi, type);
882 struct summary_footer *sum_footer;
884 curseg->segno = curseg->next_segno;
885 curseg->zone = GET_ZONENO_FROM_SEGNO(sbi, curseg->segno);
886 curseg->next_blkoff = 0;
887 curseg->next_segno = NULL_SEGNO;
889 sum_footer = &(curseg->sum_blk->footer);
890 memset(sum_footer, 0, sizeof(struct summary_footer));
891 if (IS_DATASEG(type))
892 SET_SUM_TYPE(sum_footer, SUM_TYPE_DATA);
893 if (IS_NODESEG(type))
894 SET_SUM_TYPE(sum_footer, SUM_TYPE_NODE);
895 __set_sit_entry_type(sbi, type, curseg->segno, modified);
899 * Allocate a current working segment.
900 * This function always allocates a free segment in LFS manner.
902 static void new_curseg(struct f2fs_sb_info *sbi, int type, bool new_sec)
904 struct curseg_info *curseg = CURSEG_I(sbi, type);
905 unsigned int segno = curseg->segno;
906 int dir = ALLOC_LEFT;
908 write_sum_page(sbi, curseg->sum_blk,
909 GET_SUM_BLOCK(sbi, segno));
910 if (type == CURSEG_WARM_DATA || type == CURSEG_COLD_DATA)
913 if (test_opt(sbi, NOHEAP))
916 get_new_segment(sbi, &segno, new_sec, dir);
917 curseg->next_segno = segno;
918 reset_curseg(sbi, type, 1);
919 curseg->alloc_type = LFS;
922 static void __next_free_blkoff(struct f2fs_sb_info *sbi,
923 struct curseg_info *seg, block_t start)
925 struct seg_entry *se = get_seg_entry(sbi, seg->segno);
926 int entries = SIT_VBLOCK_MAP_SIZE / sizeof(unsigned long);
927 unsigned long target_map[entries];
928 unsigned long *ckpt_map = (unsigned long *)se->ckpt_valid_map;
929 unsigned long *cur_map = (unsigned long *)se->cur_valid_map;
932 for (i = 0; i < entries; i++)
933 target_map[i] = ckpt_map[i] | cur_map[i];
935 pos = __find_rev_next_zero_bit(target_map, sbi->blocks_per_seg, start);
937 seg->next_blkoff = pos;
941 * If a segment is written by LFS manner, next block offset is just obtained
942 * by increasing the current block offset. However, if a segment is written by
943 * SSR manner, next block offset obtained by calling __next_free_blkoff
945 static void __refresh_next_blkoff(struct f2fs_sb_info *sbi,
946 struct curseg_info *seg)
948 if (seg->alloc_type == SSR)
949 __next_free_blkoff(sbi, seg, seg->next_blkoff + 1);
955 * This function always allocates a used segment(from dirty seglist) by SSR
956 * manner, so it should recover the existing segment information of valid blocks
958 static void change_curseg(struct f2fs_sb_info *sbi, int type, bool reuse)
960 struct dirty_seglist_info *dirty_i = DIRTY_I(sbi);
961 struct curseg_info *curseg = CURSEG_I(sbi, type);
962 unsigned int new_segno = curseg->next_segno;
963 struct f2fs_summary_block *sum_node;
964 struct page *sum_page;
966 write_sum_page(sbi, curseg->sum_blk,
967 GET_SUM_BLOCK(sbi, curseg->segno));
968 __set_test_and_inuse(sbi, new_segno);
970 mutex_lock(&dirty_i->seglist_lock);
971 __remove_dirty_segment(sbi, new_segno, PRE);
972 __remove_dirty_segment(sbi, new_segno, DIRTY);
973 mutex_unlock(&dirty_i->seglist_lock);
975 reset_curseg(sbi, type, 1);
976 curseg->alloc_type = SSR;
977 __next_free_blkoff(sbi, curseg, 0);
980 sum_page = get_sum_page(sbi, new_segno);
981 sum_node = (struct f2fs_summary_block *)page_address(sum_page);
982 memcpy(curseg->sum_blk, sum_node, SUM_ENTRY_SIZE);
983 f2fs_put_page(sum_page, 1);
987 static int get_ssr_segment(struct f2fs_sb_info *sbi, int type)
989 struct curseg_info *curseg = CURSEG_I(sbi, type);
990 const struct victim_selection *v_ops = DIRTY_I(sbi)->v_ops;
992 if (IS_NODESEG(type) || !has_not_enough_free_secs(sbi, 0))
993 return v_ops->get_victim(sbi,
994 &(curseg)->next_segno, BG_GC, type, SSR);
996 /* For data segments, let's do SSR more intensively */
997 for (; type >= CURSEG_HOT_DATA; type--)
998 if (v_ops->get_victim(sbi, &(curseg)->next_segno,
1005 * flush out current segment and replace it with new segment
1006 * This function should be returned with success, otherwise BUG
1008 static void allocate_segment_by_default(struct f2fs_sb_info *sbi,
1009 int type, bool force)
1011 struct curseg_info *curseg = CURSEG_I(sbi, type);
1014 new_curseg(sbi, type, true);
1015 else if (type == CURSEG_WARM_NODE)
1016 new_curseg(sbi, type, false);
1017 else if (curseg->alloc_type == LFS && is_next_segment_free(sbi, type))
1018 new_curseg(sbi, type, false);
1019 else if (need_SSR(sbi) && get_ssr_segment(sbi, type))
1020 change_curseg(sbi, type, true);
1022 new_curseg(sbi, type, false);
1024 stat_inc_seg_type(sbi, curseg);
1027 static void __allocate_new_segments(struct f2fs_sb_info *sbi, int type)
1029 struct curseg_info *curseg = CURSEG_I(sbi, type);
1030 unsigned int old_segno;
1032 old_segno = curseg->segno;
1033 SIT_I(sbi)->s_ops->allocate_segment(sbi, type, true);
1034 locate_dirty_segment(sbi, old_segno);
1037 void allocate_new_segments(struct f2fs_sb_info *sbi)
1041 for (i = CURSEG_HOT_DATA; i <= CURSEG_COLD_DATA; i++)
1042 __allocate_new_segments(sbi, i);
1045 static const struct segment_allocation default_salloc_ops = {
1046 .allocate_segment = allocate_segment_by_default,
1049 int f2fs_trim_fs(struct f2fs_sb_info *sbi, struct fstrim_range *range)
1051 __u64 start = range->start >> sbi->log_blocksize;
1052 __u64 end = start + (range->len >> sbi->log_blocksize) - 1;
1053 unsigned int start_segno, end_segno;
1054 struct cp_control cpc;
1056 if (range->minlen > SEGMENT_SIZE(sbi) || start >= MAX_BLKADDR(sbi) ||
1057 range->len < sbi->blocksize)
1061 if (end <= MAIN_BLKADDR(sbi))
1064 /* start/end segment number in main_area */
1065 start_segno = (start <= MAIN_BLKADDR(sbi)) ? 0 : GET_SEGNO(sbi, start);
1066 end_segno = (end >= MAX_BLKADDR(sbi)) ? MAIN_SEGS(sbi) - 1 :
1067 GET_SEGNO(sbi, end);
1068 cpc.reason = CP_DISCARD;
1069 cpc.trim_start = start_segno;
1070 cpc.trim_end = end_segno;
1071 cpc.trim_minlen = range->minlen >> sbi->log_blocksize;
1073 /* do checkpoint to issue discard commands safely */
1074 mutex_lock(&sbi->gc_mutex);
1075 write_checkpoint(sbi, &cpc);
1076 mutex_unlock(&sbi->gc_mutex);
1078 range->len = cpc.trimmed << sbi->log_blocksize;
1082 static bool __has_curseg_space(struct f2fs_sb_info *sbi, int type)
1084 struct curseg_info *curseg = CURSEG_I(sbi, type);
1085 if (curseg->next_blkoff < sbi->blocks_per_seg)
1090 static int __get_segment_type_2(struct page *page, enum page_type p_type)
1093 return CURSEG_HOT_DATA;
1095 return CURSEG_HOT_NODE;
1098 static int __get_segment_type_4(struct page *page, enum page_type p_type)
1100 if (p_type == DATA) {
1101 struct inode *inode = page->mapping->host;
1103 if (S_ISDIR(inode->i_mode))
1104 return CURSEG_HOT_DATA;
1106 return CURSEG_COLD_DATA;
1108 if (IS_DNODE(page) && is_cold_node(page))
1109 return CURSEG_WARM_NODE;
1111 return CURSEG_COLD_NODE;
1115 static int __get_segment_type_6(struct page *page, enum page_type p_type)
1117 if (p_type == DATA) {
1118 struct inode *inode = page->mapping->host;
1120 if (S_ISDIR(inode->i_mode))
1121 return CURSEG_HOT_DATA;
1122 else if (is_cold_data(page) || file_is_cold(inode))
1123 return CURSEG_COLD_DATA;
1125 return CURSEG_WARM_DATA;
1128 return is_cold_node(page) ? CURSEG_WARM_NODE :
1131 return CURSEG_COLD_NODE;
1135 static int __get_segment_type(struct page *page, enum page_type p_type)
1137 switch (F2FS_P_SB(page)->active_logs) {
1139 return __get_segment_type_2(page, p_type);
1141 return __get_segment_type_4(page, p_type);
1143 /* NR_CURSEG_TYPE(6) logs by default */
1144 f2fs_bug_on(F2FS_P_SB(page),
1145 F2FS_P_SB(page)->active_logs != NR_CURSEG_TYPE);
1146 return __get_segment_type_6(page, p_type);
1149 void allocate_data_block(struct f2fs_sb_info *sbi, struct page *page,
1150 block_t old_blkaddr, block_t *new_blkaddr,
1151 struct f2fs_summary *sum, int type)
1153 struct sit_info *sit_i = SIT_I(sbi);
1154 struct curseg_info *curseg;
1155 bool direct_io = (type == CURSEG_DIRECT_IO);
1157 type = direct_io ? CURSEG_WARM_DATA : type;
1159 curseg = CURSEG_I(sbi, type);
1161 mutex_lock(&curseg->curseg_mutex);
1163 /* direct_io'ed data is aligned to the segment for better performance */
1164 if (direct_io && curseg->next_blkoff)
1165 __allocate_new_segments(sbi, type);
1167 *new_blkaddr = NEXT_FREE_BLKADDR(sbi, curseg);
1170 * __add_sum_entry should be resided under the curseg_mutex
1171 * because, this function updates a summary entry in the
1172 * current summary block.
1174 __add_sum_entry(sbi, type, sum);
1176 mutex_lock(&sit_i->sentry_lock);
1177 __refresh_next_blkoff(sbi, curseg);
1179 stat_inc_block_count(sbi, curseg);
1181 if (!__has_curseg_space(sbi, type))
1182 sit_i->s_ops->allocate_segment(sbi, type, false);
1184 * SIT information should be updated before segment allocation,
1185 * since SSR needs latest valid block information.
1187 refresh_sit_entry(sbi, old_blkaddr, *new_blkaddr);
1189 mutex_unlock(&sit_i->sentry_lock);
1191 if (page && IS_NODESEG(type))
1192 fill_node_footer_blkaddr(page, NEXT_FREE_BLKADDR(sbi, curseg));
1194 mutex_unlock(&curseg->curseg_mutex);
1197 static void do_write_page(struct f2fs_sb_info *sbi, struct page *page,
1198 struct f2fs_summary *sum,
1199 struct f2fs_io_info *fio)
1201 int type = __get_segment_type(page, fio->type);
1203 allocate_data_block(sbi, page, fio->blk_addr, &fio->blk_addr, sum, type);
1205 /* writeout dirty page into bdev */
1206 f2fs_submit_page_mbio(sbi, page, fio);
1209 void write_meta_page(struct f2fs_sb_info *sbi, struct page *page)
1211 struct f2fs_io_info fio = {
1213 .rw = WRITE_SYNC | REQ_META | REQ_PRIO,
1214 .blk_addr = page->index,
1217 set_page_writeback(page);
1218 f2fs_submit_page_mbio(sbi, page, &fio);
1221 void write_node_page(struct f2fs_sb_info *sbi, struct page *page,
1222 unsigned int nid, struct f2fs_io_info *fio)
1224 struct f2fs_summary sum;
1225 set_summary(&sum, nid, 0, 0);
1226 do_write_page(sbi, page, &sum, fio);
1229 void write_data_page(struct page *page, struct dnode_of_data *dn,
1230 struct f2fs_io_info *fio)
1232 struct f2fs_sb_info *sbi = F2FS_I_SB(dn->inode);
1233 struct f2fs_summary sum;
1234 struct node_info ni;
1236 f2fs_bug_on(sbi, dn->data_blkaddr == NULL_ADDR);
1237 get_node_info(sbi, dn->nid, &ni);
1238 set_summary(&sum, dn->nid, dn->ofs_in_node, ni.version);
1239 do_write_page(sbi, page, &sum, fio);
1240 dn->data_blkaddr = fio->blk_addr;
1243 void rewrite_data_page(struct page *page, struct f2fs_io_info *fio)
1245 stat_inc_inplace_blocks(F2FS_P_SB(page));
1246 f2fs_submit_page_mbio(F2FS_P_SB(page), page, fio);
1249 void recover_data_page(struct f2fs_sb_info *sbi,
1250 struct page *page, struct f2fs_summary *sum,
1251 block_t old_blkaddr, block_t new_blkaddr)
1253 struct sit_info *sit_i = SIT_I(sbi);
1254 struct curseg_info *curseg;
1255 unsigned int segno, old_cursegno;
1256 struct seg_entry *se;
1259 segno = GET_SEGNO(sbi, new_blkaddr);
1260 se = get_seg_entry(sbi, segno);
1263 if (se->valid_blocks == 0 && !IS_CURSEG(sbi, segno)) {
1264 if (old_blkaddr == NULL_ADDR)
1265 type = CURSEG_COLD_DATA;
1267 type = CURSEG_WARM_DATA;
1269 curseg = CURSEG_I(sbi, type);
1271 mutex_lock(&curseg->curseg_mutex);
1272 mutex_lock(&sit_i->sentry_lock);
1274 old_cursegno = curseg->segno;
1276 /* change the current segment */
1277 if (segno != curseg->segno) {
1278 curseg->next_segno = segno;
1279 change_curseg(sbi, type, true);
1282 curseg->next_blkoff = GET_BLKOFF_FROM_SEG0(sbi, new_blkaddr);
1283 __add_sum_entry(sbi, type, sum);
1285 refresh_sit_entry(sbi, old_blkaddr, new_blkaddr);
1286 locate_dirty_segment(sbi, old_cursegno);
1288 mutex_unlock(&sit_i->sentry_lock);
1289 mutex_unlock(&curseg->curseg_mutex);
1292 static inline bool is_merged_page(struct f2fs_sb_info *sbi,
1293 struct page *page, enum page_type type)
1295 enum page_type btype = PAGE_TYPE_OF_BIO(type);
1296 struct f2fs_bio_info *io = &sbi->write_io[btype];
1297 struct bio_vec *bvec;
1300 down_read(&io->io_rwsem);
1304 bio_for_each_segment_all(bvec, io->bio, i) {
1305 if (page == bvec->bv_page) {
1306 up_read(&io->io_rwsem);
1312 up_read(&io->io_rwsem);
1316 void f2fs_wait_on_page_writeback(struct page *page,
1317 enum page_type type)
1319 if (PageWriteback(page)) {
1320 struct f2fs_sb_info *sbi = F2FS_P_SB(page);
1322 if (is_merged_page(sbi, page, type))
1323 f2fs_submit_merged_bio(sbi, type, WRITE);
1324 wait_on_page_writeback(page);
1328 static int read_compacted_summaries(struct f2fs_sb_info *sbi)
1330 struct f2fs_checkpoint *ckpt = F2FS_CKPT(sbi);
1331 struct curseg_info *seg_i;
1332 unsigned char *kaddr;
1337 start = start_sum_block(sbi);
1339 page = get_meta_page(sbi, start++);
1340 kaddr = (unsigned char *)page_address(page);
1342 /* Step 1: restore nat cache */
1343 seg_i = CURSEG_I(sbi, CURSEG_HOT_DATA);
1344 memcpy(&seg_i->sum_blk->n_nats, kaddr, SUM_JOURNAL_SIZE);
1346 /* Step 2: restore sit cache */
1347 seg_i = CURSEG_I(sbi, CURSEG_COLD_DATA);
1348 memcpy(&seg_i->sum_blk->n_sits, kaddr + SUM_JOURNAL_SIZE,
1350 offset = 2 * SUM_JOURNAL_SIZE;
1352 /* Step 3: restore summary entries */
1353 for (i = CURSEG_HOT_DATA; i <= CURSEG_COLD_DATA; i++) {
1354 unsigned short blk_off;
1357 seg_i = CURSEG_I(sbi, i);
1358 segno = le32_to_cpu(ckpt->cur_data_segno[i]);
1359 blk_off = le16_to_cpu(ckpt->cur_data_blkoff[i]);
1360 seg_i->next_segno = segno;
1361 reset_curseg(sbi, i, 0);
1362 seg_i->alloc_type = ckpt->alloc_type[i];
1363 seg_i->next_blkoff = blk_off;
1365 if (seg_i->alloc_type == SSR)
1366 blk_off = sbi->blocks_per_seg;
1368 for (j = 0; j < blk_off; j++) {
1369 struct f2fs_summary *s;
1370 s = (struct f2fs_summary *)(kaddr + offset);
1371 seg_i->sum_blk->entries[j] = *s;
1372 offset += SUMMARY_SIZE;
1373 if (offset + SUMMARY_SIZE <= PAGE_CACHE_SIZE -
1377 f2fs_put_page(page, 1);
1380 page = get_meta_page(sbi, start++);
1381 kaddr = (unsigned char *)page_address(page);
1385 f2fs_put_page(page, 1);
1389 static int read_normal_summaries(struct f2fs_sb_info *sbi, int type)
1391 struct f2fs_checkpoint *ckpt = F2FS_CKPT(sbi);
1392 struct f2fs_summary_block *sum;
1393 struct curseg_info *curseg;
1395 unsigned short blk_off;
1396 unsigned int segno = 0;
1397 block_t blk_addr = 0;
1399 /* get segment number and block addr */
1400 if (IS_DATASEG(type)) {
1401 segno = le32_to_cpu(ckpt->cur_data_segno[type]);
1402 blk_off = le16_to_cpu(ckpt->cur_data_blkoff[type -
1404 if (__exist_node_summaries(sbi))
1405 blk_addr = sum_blk_addr(sbi, NR_CURSEG_TYPE, type);
1407 blk_addr = sum_blk_addr(sbi, NR_CURSEG_DATA_TYPE, type);
1409 segno = le32_to_cpu(ckpt->cur_node_segno[type -
1411 blk_off = le16_to_cpu(ckpt->cur_node_blkoff[type -
1413 if (__exist_node_summaries(sbi))
1414 blk_addr = sum_blk_addr(sbi, NR_CURSEG_NODE_TYPE,
1415 type - CURSEG_HOT_NODE);
1417 blk_addr = GET_SUM_BLOCK(sbi, segno);
1420 new = get_meta_page(sbi, blk_addr);
1421 sum = (struct f2fs_summary_block *)page_address(new);
1423 if (IS_NODESEG(type)) {
1424 if (__exist_node_summaries(sbi)) {
1425 struct f2fs_summary *ns = &sum->entries[0];
1427 for (i = 0; i < sbi->blocks_per_seg; i++, ns++) {
1429 ns->ofs_in_node = 0;
1434 err = restore_node_summary(sbi, segno, sum);
1436 f2fs_put_page(new, 1);
1442 /* set uncompleted segment to curseg */
1443 curseg = CURSEG_I(sbi, type);
1444 mutex_lock(&curseg->curseg_mutex);
1445 memcpy(curseg->sum_blk, sum, PAGE_CACHE_SIZE);
1446 curseg->next_segno = segno;
1447 reset_curseg(sbi, type, 0);
1448 curseg->alloc_type = ckpt->alloc_type[type];
1449 curseg->next_blkoff = blk_off;
1450 mutex_unlock(&curseg->curseg_mutex);
1451 f2fs_put_page(new, 1);
1455 static int restore_curseg_summaries(struct f2fs_sb_info *sbi)
1457 int type = CURSEG_HOT_DATA;
1460 if (is_set_ckpt_flags(F2FS_CKPT(sbi), CP_COMPACT_SUM_FLAG)) {
1461 int npages = npages_for_summary_flush(sbi, true);
1464 ra_meta_pages(sbi, start_sum_block(sbi), npages,
1467 /* restore for compacted data summary */
1468 if (read_compacted_summaries(sbi))
1470 type = CURSEG_HOT_NODE;
1473 if (__exist_node_summaries(sbi))
1474 ra_meta_pages(sbi, sum_blk_addr(sbi, NR_CURSEG_TYPE, type),
1475 NR_CURSEG_TYPE - type, META_CP);
1477 for (; type <= CURSEG_COLD_NODE; type++) {
1478 err = read_normal_summaries(sbi, type);
1486 static void write_compacted_summaries(struct f2fs_sb_info *sbi, block_t blkaddr)
1489 unsigned char *kaddr;
1490 struct f2fs_summary *summary;
1491 struct curseg_info *seg_i;
1492 int written_size = 0;
1495 page = grab_meta_page(sbi, blkaddr++);
1496 kaddr = (unsigned char *)page_address(page);
1498 /* Step 1: write nat cache */
1499 seg_i = CURSEG_I(sbi, CURSEG_HOT_DATA);
1500 memcpy(kaddr, &seg_i->sum_blk->n_nats, SUM_JOURNAL_SIZE);
1501 written_size += SUM_JOURNAL_SIZE;
1503 /* Step 2: write sit cache */
1504 seg_i = CURSEG_I(sbi, CURSEG_COLD_DATA);
1505 memcpy(kaddr + written_size, &seg_i->sum_blk->n_sits,
1507 written_size += SUM_JOURNAL_SIZE;
1509 /* Step 3: write summary entries */
1510 for (i = CURSEG_HOT_DATA; i <= CURSEG_COLD_DATA; i++) {
1511 unsigned short blkoff;
1512 seg_i = CURSEG_I(sbi, i);
1513 if (sbi->ckpt->alloc_type[i] == SSR)
1514 blkoff = sbi->blocks_per_seg;
1516 blkoff = curseg_blkoff(sbi, i);
1518 for (j = 0; j < blkoff; j++) {
1520 page = grab_meta_page(sbi, blkaddr++);
1521 kaddr = (unsigned char *)page_address(page);
1524 summary = (struct f2fs_summary *)(kaddr + written_size);
1525 *summary = seg_i->sum_blk->entries[j];
1526 written_size += SUMMARY_SIZE;
1528 if (written_size + SUMMARY_SIZE <= PAGE_CACHE_SIZE -
1532 set_page_dirty(page);
1533 f2fs_put_page(page, 1);
1538 set_page_dirty(page);
1539 f2fs_put_page(page, 1);
1543 static void write_normal_summaries(struct f2fs_sb_info *sbi,
1544 block_t blkaddr, int type)
1547 if (IS_DATASEG(type))
1548 end = type + NR_CURSEG_DATA_TYPE;
1550 end = type + NR_CURSEG_NODE_TYPE;
1552 for (i = type; i < end; i++) {
1553 struct curseg_info *sum = CURSEG_I(sbi, i);
1554 mutex_lock(&sum->curseg_mutex);
1555 write_sum_page(sbi, sum->sum_blk, blkaddr + (i - type));
1556 mutex_unlock(&sum->curseg_mutex);
1560 void write_data_summaries(struct f2fs_sb_info *sbi, block_t start_blk)
1562 if (is_set_ckpt_flags(F2FS_CKPT(sbi), CP_COMPACT_SUM_FLAG))
1563 write_compacted_summaries(sbi, start_blk);
1565 write_normal_summaries(sbi, start_blk, CURSEG_HOT_DATA);
1568 void write_node_summaries(struct f2fs_sb_info *sbi, block_t start_blk)
1570 write_normal_summaries(sbi, start_blk, CURSEG_HOT_NODE);
1573 int lookup_journal_in_cursum(struct f2fs_summary_block *sum, int type,
1574 unsigned int val, int alloc)
1578 if (type == NAT_JOURNAL) {
1579 for (i = 0; i < nats_in_cursum(sum); i++) {
1580 if (le32_to_cpu(nid_in_journal(sum, i)) == val)
1583 if (alloc && nats_in_cursum(sum) < NAT_JOURNAL_ENTRIES)
1584 return update_nats_in_cursum(sum, 1);
1585 } else if (type == SIT_JOURNAL) {
1586 for (i = 0; i < sits_in_cursum(sum); i++)
1587 if (le32_to_cpu(segno_in_journal(sum, i)) == val)
1589 if (alloc && sits_in_cursum(sum) < SIT_JOURNAL_ENTRIES)
1590 return update_sits_in_cursum(sum, 1);
1595 static struct page *get_current_sit_page(struct f2fs_sb_info *sbi,
1598 return get_meta_page(sbi, current_sit_addr(sbi, segno));
1601 static struct page *get_next_sit_page(struct f2fs_sb_info *sbi,
1604 struct sit_info *sit_i = SIT_I(sbi);
1605 struct page *src_page, *dst_page;
1606 pgoff_t src_off, dst_off;
1607 void *src_addr, *dst_addr;
1609 src_off = current_sit_addr(sbi, start);
1610 dst_off = next_sit_addr(sbi, src_off);
1612 /* get current sit block page without lock */
1613 src_page = get_meta_page(sbi, src_off);
1614 dst_page = grab_meta_page(sbi, dst_off);
1615 f2fs_bug_on(sbi, PageDirty(src_page));
1617 src_addr = page_address(src_page);
1618 dst_addr = page_address(dst_page);
1619 memcpy(dst_addr, src_addr, PAGE_CACHE_SIZE);
1621 set_page_dirty(dst_page);
1622 f2fs_put_page(src_page, 1);
1624 set_to_next_sit(sit_i, start);
1629 static struct sit_entry_set *grab_sit_entry_set(void)
1631 struct sit_entry_set *ses =
1632 f2fs_kmem_cache_alloc(sit_entry_set_slab, GFP_ATOMIC);
1635 INIT_LIST_HEAD(&ses->set_list);
1639 static void release_sit_entry_set(struct sit_entry_set *ses)
1641 list_del(&ses->set_list);
1642 kmem_cache_free(sit_entry_set_slab, ses);
1645 static void adjust_sit_entry_set(struct sit_entry_set *ses,
1646 struct list_head *head)
1648 struct sit_entry_set *next = ses;
1650 if (list_is_last(&ses->set_list, head))
1653 list_for_each_entry_continue(next, head, set_list)
1654 if (ses->entry_cnt <= next->entry_cnt)
1657 list_move_tail(&ses->set_list, &next->set_list);
1660 static void add_sit_entry(unsigned int segno, struct list_head *head)
1662 struct sit_entry_set *ses;
1663 unsigned int start_segno = START_SEGNO(segno);
1665 list_for_each_entry(ses, head, set_list) {
1666 if (ses->start_segno == start_segno) {
1668 adjust_sit_entry_set(ses, head);
1673 ses = grab_sit_entry_set();
1675 ses->start_segno = start_segno;
1677 list_add(&ses->set_list, head);
1680 static void add_sits_in_set(struct f2fs_sb_info *sbi)
1682 struct f2fs_sm_info *sm_info = SM_I(sbi);
1683 struct list_head *set_list = &sm_info->sit_entry_set;
1684 unsigned long *bitmap = SIT_I(sbi)->dirty_sentries_bitmap;
1687 for_each_set_bit(segno, bitmap, MAIN_SEGS(sbi))
1688 add_sit_entry(segno, set_list);
1691 static void remove_sits_in_journal(struct f2fs_sb_info *sbi)
1693 struct curseg_info *curseg = CURSEG_I(sbi, CURSEG_COLD_DATA);
1694 struct f2fs_summary_block *sum = curseg->sum_blk;
1697 for (i = sits_in_cursum(sum) - 1; i >= 0; i--) {
1701 segno = le32_to_cpu(segno_in_journal(sum, i));
1702 dirtied = __mark_sit_entry_dirty(sbi, segno);
1705 add_sit_entry(segno, &SM_I(sbi)->sit_entry_set);
1707 update_sits_in_cursum(sum, -sits_in_cursum(sum));
1711 * CP calls this function, which flushes SIT entries including sit_journal,
1712 * and moves prefree segs to free segs.
1714 void flush_sit_entries(struct f2fs_sb_info *sbi, struct cp_control *cpc)
1716 struct sit_info *sit_i = SIT_I(sbi);
1717 unsigned long *bitmap = sit_i->dirty_sentries_bitmap;
1718 struct curseg_info *curseg = CURSEG_I(sbi, CURSEG_COLD_DATA);
1719 struct f2fs_summary_block *sum = curseg->sum_blk;
1720 struct sit_entry_set *ses, *tmp;
1721 struct list_head *head = &SM_I(sbi)->sit_entry_set;
1722 bool to_journal = true;
1723 struct seg_entry *se;
1725 mutex_lock(&curseg->curseg_mutex);
1726 mutex_lock(&sit_i->sentry_lock);
1729 * add and account sit entries of dirty bitmap in sit entry
1732 add_sits_in_set(sbi);
1735 * if there are no enough space in journal to store dirty sit
1736 * entries, remove all entries from journal and add and account
1737 * them in sit entry set.
1739 if (!__has_cursum_space(sum, sit_i->dirty_sentries, SIT_JOURNAL))
1740 remove_sits_in_journal(sbi);
1742 if (!sit_i->dirty_sentries)
1746 * there are two steps to flush sit entries:
1747 * #1, flush sit entries to journal in current cold data summary block.
1748 * #2, flush sit entries to sit page.
1750 list_for_each_entry_safe(ses, tmp, head, set_list) {
1751 struct page *page = NULL;
1752 struct f2fs_sit_block *raw_sit = NULL;
1753 unsigned int start_segno = ses->start_segno;
1754 unsigned int end = min(start_segno + SIT_ENTRY_PER_BLOCK,
1755 (unsigned long)MAIN_SEGS(sbi));
1756 unsigned int segno = start_segno;
1759 !__has_cursum_space(sum, ses->entry_cnt, SIT_JOURNAL))
1763 page = get_next_sit_page(sbi, start_segno);
1764 raw_sit = page_address(page);
1767 /* flush dirty sit entries in region of current sit set */
1768 for_each_set_bit_from(segno, bitmap, end) {
1769 int offset, sit_offset;
1771 se = get_seg_entry(sbi, segno);
1773 /* add discard candidates */
1774 if (cpc->reason != CP_DISCARD) {
1775 cpc->trim_start = segno;
1776 add_discard_addrs(sbi, cpc);
1780 offset = lookup_journal_in_cursum(sum,
1781 SIT_JOURNAL, segno, 1);
1782 f2fs_bug_on(sbi, offset < 0);
1783 segno_in_journal(sum, offset) =
1785 seg_info_to_raw_sit(se,
1786 &sit_in_journal(sum, offset));
1788 sit_offset = SIT_ENTRY_OFFSET(sit_i, segno);
1789 seg_info_to_raw_sit(se,
1790 &raw_sit->entries[sit_offset]);
1793 __clear_bit(segno, bitmap);
1794 sit_i->dirty_sentries--;
1799 f2fs_put_page(page, 1);
1801 f2fs_bug_on(sbi, ses->entry_cnt);
1802 release_sit_entry_set(ses);
1805 f2fs_bug_on(sbi, !list_empty(head));
1806 f2fs_bug_on(sbi, sit_i->dirty_sentries);
1808 if (cpc->reason == CP_DISCARD) {
1809 for (; cpc->trim_start <= cpc->trim_end; cpc->trim_start++)
1810 add_discard_addrs(sbi, cpc);
1812 mutex_unlock(&sit_i->sentry_lock);
1813 mutex_unlock(&curseg->curseg_mutex);
1815 set_prefree_as_free_segments(sbi);
1818 static int build_sit_info(struct f2fs_sb_info *sbi)
1820 struct f2fs_super_block *raw_super = F2FS_RAW_SUPER(sbi);
1821 struct f2fs_checkpoint *ckpt = F2FS_CKPT(sbi);
1822 struct sit_info *sit_i;
1823 unsigned int sit_segs, start;
1824 char *src_bitmap, *dst_bitmap;
1825 unsigned int bitmap_size;
1827 /* allocate memory for SIT information */
1828 sit_i = kzalloc(sizeof(struct sit_info), GFP_KERNEL);
1832 SM_I(sbi)->sit_info = sit_i;
1834 sit_i->sentries = vzalloc(MAIN_SEGS(sbi) * sizeof(struct seg_entry));
1835 if (!sit_i->sentries)
1838 bitmap_size = f2fs_bitmap_size(MAIN_SEGS(sbi));
1839 sit_i->dirty_sentries_bitmap = kzalloc(bitmap_size, GFP_KERNEL);
1840 if (!sit_i->dirty_sentries_bitmap)
1843 for (start = 0; start < MAIN_SEGS(sbi); start++) {
1844 sit_i->sentries[start].cur_valid_map
1845 = kzalloc(SIT_VBLOCK_MAP_SIZE, GFP_KERNEL);
1846 sit_i->sentries[start].ckpt_valid_map
1847 = kzalloc(SIT_VBLOCK_MAP_SIZE, GFP_KERNEL);
1848 if (!sit_i->sentries[start].cur_valid_map
1849 || !sit_i->sentries[start].ckpt_valid_map)
1853 if (sbi->segs_per_sec > 1) {
1854 sit_i->sec_entries = vzalloc(MAIN_SECS(sbi) *
1855 sizeof(struct sec_entry));
1856 if (!sit_i->sec_entries)
1860 /* get information related with SIT */
1861 sit_segs = le32_to_cpu(raw_super->segment_count_sit) >> 1;
1863 /* setup SIT bitmap from ckeckpoint pack */
1864 bitmap_size = __bitmap_size(sbi, SIT_BITMAP);
1865 src_bitmap = __bitmap_ptr(sbi, SIT_BITMAP);
1867 dst_bitmap = kmemdup(src_bitmap, bitmap_size, GFP_KERNEL);
1871 /* init SIT information */
1872 sit_i->s_ops = &default_salloc_ops;
1874 sit_i->sit_base_addr = le32_to_cpu(raw_super->sit_blkaddr);
1875 sit_i->sit_blocks = sit_segs << sbi->log_blocks_per_seg;
1876 sit_i->written_valid_blocks = le64_to_cpu(ckpt->valid_block_count);
1877 sit_i->sit_bitmap = dst_bitmap;
1878 sit_i->bitmap_size = bitmap_size;
1879 sit_i->dirty_sentries = 0;
1880 sit_i->sents_per_block = SIT_ENTRY_PER_BLOCK;
1881 sit_i->elapsed_time = le64_to_cpu(sbi->ckpt->elapsed_time);
1882 sit_i->mounted_time = CURRENT_TIME_SEC.tv_sec;
1883 mutex_init(&sit_i->sentry_lock);
1887 static int build_free_segmap(struct f2fs_sb_info *sbi)
1889 struct free_segmap_info *free_i;
1890 unsigned int bitmap_size, sec_bitmap_size;
1892 /* allocate memory for free segmap information */
1893 free_i = kzalloc(sizeof(struct free_segmap_info), GFP_KERNEL);
1897 SM_I(sbi)->free_info = free_i;
1899 bitmap_size = f2fs_bitmap_size(MAIN_SEGS(sbi));
1900 free_i->free_segmap = kmalloc(bitmap_size, GFP_KERNEL);
1901 if (!free_i->free_segmap)
1904 sec_bitmap_size = f2fs_bitmap_size(MAIN_SECS(sbi));
1905 free_i->free_secmap = kmalloc(sec_bitmap_size, GFP_KERNEL);
1906 if (!free_i->free_secmap)
1909 /* set all segments as dirty temporarily */
1910 memset(free_i->free_segmap, 0xff, bitmap_size);
1911 memset(free_i->free_secmap, 0xff, sec_bitmap_size);
1913 /* init free segmap information */
1914 free_i->start_segno = GET_SEGNO_FROM_SEG0(sbi, MAIN_BLKADDR(sbi));
1915 free_i->free_segments = 0;
1916 free_i->free_sections = 0;
1917 rwlock_init(&free_i->segmap_lock);
1921 static int build_curseg(struct f2fs_sb_info *sbi)
1923 struct curseg_info *array;
1926 array = kcalloc(NR_CURSEG_TYPE, sizeof(*array), GFP_KERNEL);
1930 SM_I(sbi)->curseg_array = array;
1932 for (i = 0; i < NR_CURSEG_TYPE; i++) {
1933 mutex_init(&array[i].curseg_mutex);
1934 array[i].sum_blk = kzalloc(PAGE_CACHE_SIZE, GFP_KERNEL);
1935 if (!array[i].sum_blk)
1937 array[i].segno = NULL_SEGNO;
1938 array[i].next_blkoff = 0;
1940 return restore_curseg_summaries(sbi);
1943 static void build_sit_entries(struct f2fs_sb_info *sbi)
1945 struct sit_info *sit_i = SIT_I(sbi);
1946 struct curseg_info *curseg = CURSEG_I(sbi, CURSEG_COLD_DATA);
1947 struct f2fs_summary_block *sum = curseg->sum_blk;
1948 int sit_blk_cnt = SIT_BLK_CNT(sbi);
1949 unsigned int i, start, end;
1950 unsigned int readed, start_blk = 0;
1951 int nrpages = MAX_BIO_BLOCKS(sbi);
1954 readed = ra_meta_pages(sbi, start_blk, nrpages, META_SIT);
1956 start = start_blk * sit_i->sents_per_block;
1957 end = (start_blk + readed) * sit_i->sents_per_block;
1959 for (; start < end && start < MAIN_SEGS(sbi); start++) {
1960 struct seg_entry *se = &sit_i->sentries[start];
1961 struct f2fs_sit_block *sit_blk;
1962 struct f2fs_sit_entry sit;
1965 mutex_lock(&curseg->curseg_mutex);
1966 for (i = 0; i < sits_in_cursum(sum); i++) {
1967 if (le32_to_cpu(segno_in_journal(sum, i))
1969 sit = sit_in_journal(sum, i);
1970 mutex_unlock(&curseg->curseg_mutex);
1974 mutex_unlock(&curseg->curseg_mutex);
1976 page = get_current_sit_page(sbi, start);
1977 sit_blk = (struct f2fs_sit_block *)page_address(page);
1978 sit = sit_blk->entries[SIT_ENTRY_OFFSET(sit_i, start)];
1979 f2fs_put_page(page, 1);
1981 check_block_count(sbi, start, &sit);
1982 seg_info_from_raw_sit(se, &sit);
1983 if (sbi->segs_per_sec > 1) {
1984 struct sec_entry *e = get_sec_entry(sbi, start);
1985 e->valid_blocks += se->valid_blocks;
1988 start_blk += readed;
1989 } while (start_blk < sit_blk_cnt);
1992 static void init_free_segmap(struct f2fs_sb_info *sbi)
1997 for (start = 0; start < MAIN_SEGS(sbi); start++) {
1998 struct seg_entry *sentry = get_seg_entry(sbi, start);
1999 if (!sentry->valid_blocks)
2000 __set_free(sbi, start);
2003 /* set use the current segments */
2004 for (type = CURSEG_HOT_DATA; type <= CURSEG_COLD_NODE; type++) {
2005 struct curseg_info *curseg_t = CURSEG_I(sbi, type);
2006 __set_test_and_inuse(sbi, curseg_t->segno);
2010 static void init_dirty_segmap(struct f2fs_sb_info *sbi)
2012 struct dirty_seglist_info *dirty_i = DIRTY_I(sbi);
2013 struct free_segmap_info *free_i = FREE_I(sbi);
2014 unsigned int segno = 0, offset = 0;
2015 unsigned short valid_blocks;
2018 /* find dirty segment based on free segmap */
2019 segno = find_next_inuse(free_i, MAIN_SEGS(sbi), offset);
2020 if (segno >= MAIN_SEGS(sbi))
2023 valid_blocks = get_valid_blocks(sbi, segno, 0);
2024 if (valid_blocks == sbi->blocks_per_seg || !valid_blocks)
2026 if (valid_blocks > sbi->blocks_per_seg) {
2027 f2fs_bug_on(sbi, 1);
2030 mutex_lock(&dirty_i->seglist_lock);
2031 __locate_dirty_segment(sbi, segno, DIRTY);
2032 mutex_unlock(&dirty_i->seglist_lock);
2036 static int init_victim_secmap(struct f2fs_sb_info *sbi)
2038 struct dirty_seglist_info *dirty_i = DIRTY_I(sbi);
2039 unsigned int bitmap_size = f2fs_bitmap_size(MAIN_SECS(sbi));
2041 dirty_i->victim_secmap = kzalloc(bitmap_size, GFP_KERNEL);
2042 if (!dirty_i->victim_secmap)
2047 static int build_dirty_segmap(struct f2fs_sb_info *sbi)
2049 struct dirty_seglist_info *dirty_i;
2050 unsigned int bitmap_size, i;
2052 /* allocate memory for dirty segments list information */
2053 dirty_i = kzalloc(sizeof(struct dirty_seglist_info), GFP_KERNEL);
2057 SM_I(sbi)->dirty_info = dirty_i;
2058 mutex_init(&dirty_i->seglist_lock);
2060 bitmap_size = f2fs_bitmap_size(MAIN_SEGS(sbi));
2062 for (i = 0; i < NR_DIRTY_TYPE; i++) {
2063 dirty_i->dirty_segmap[i] = kzalloc(bitmap_size, GFP_KERNEL);
2064 if (!dirty_i->dirty_segmap[i])
2068 init_dirty_segmap(sbi);
2069 return init_victim_secmap(sbi);
2073 * Update min, max modified time for cost-benefit GC algorithm
2075 static void init_min_max_mtime(struct f2fs_sb_info *sbi)
2077 struct sit_info *sit_i = SIT_I(sbi);
2080 mutex_lock(&sit_i->sentry_lock);
2082 sit_i->min_mtime = LLONG_MAX;
2084 for (segno = 0; segno < MAIN_SEGS(sbi); segno += sbi->segs_per_sec) {
2086 unsigned long long mtime = 0;
2088 for (i = 0; i < sbi->segs_per_sec; i++)
2089 mtime += get_seg_entry(sbi, segno + i)->mtime;
2091 mtime = div_u64(mtime, sbi->segs_per_sec);
2093 if (sit_i->min_mtime > mtime)
2094 sit_i->min_mtime = mtime;
2096 sit_i->max_mtime = get_mtime(sbi);
2097 mutex_unlock(&sit_i->sentry_lock);
2100 int build_segment_manager(struct f2fs_sb_info *sbi)
2102 struct f2fs_super_block *raw_super = F2FS_RAW_SUPER(sbi);
2103 struct f2fs_checkpoint *ckpt = F2FS_CKPT(sbi);
2104 struct f2fs_sm_info *sm_info;
2107 sm_info = kzalloc(sizeof(struct f2fs_sm_info), GFP_KERNEL);
2112 sbi->sm_info = sm_info;
2113 sm_info->seg0_blkaddr = le32_to_cpu(raw_super->segment0_blkaddr);
2114 sm_info->main_blkaddr = le32_to_cpu(raw_super->main_blkaddr);
2115 sm_info->segment_count = le32_to_cpu(raw_super->segment_count);
2116 sm_info->reserved_segments = le32_to_cpu(ckpt->rsvd_segment_count);
2117 sm_info->ovp_segments = le32_to_cpu(ckpt->overprov_segment_count);
2118 sm_info->main_segments = le32_to_cpu(raw_super->segment_count_main);
2119 sm_info->ssa_blkaddr = le32_to_cpu(raw_super->ssa_blkaddr);
2120 sm_info->rec_prefree_segments = sm_info->main_segments *
2121 DEF_RECLAIM_PREFREE_SEGMENTS / 100;
2122 sm_info->ipu_policy = 1 << F2FS_IPU_FSYNC;
2123 sm_info->min_ipu_util = DEF_MIN_IPU_UTIL;
2124 sm_info->min_fsync_blocks = DEF_MIN_FSYNC_BLOCKS;
2126 INIT_LIST_HEAD(&sm_info->discard_list);
2127 sm_info->nr_discards = 0;
2128 sm_info->max_discards = 0;
2130 INIT_LIST_HEAD(&sm_info->sit_entry_set);
2132 if (test_opt(sbi, FLUSH_MERGE) && !f2fs_readonly(sbi->sb)) {
2133 err = create_flush_cmd_control(sbi);
2138 err = build_sit_info(sbi);
2141 err = build_free_segmap(sbi);
2144 err = build_curseg(sbi);
2148 /* reinit free segmap based on SIT */
2149 build_sit_entries(sbi);
2151 init_free_segmap(sbi);
2152 err = build_dirty_segmap(sbi);
2156 init_min_max_mtime(sbi);
2160 static void discard_dirty_segmap(struct f2fs_sb_info *sbi,
2161 enum dirty_type dirty_type)
2163 struct dirty_seglist_info *dirty_i = DIRTY_I(sbi);
2165 mutex_lock(&dirty_i->seglist_lock);
2166 kfree(dirty_i->dirty_segmap[dirty_type]);
2167 dirty_i->nr_dirty[dirty_type] = 0;
2168 mutex_unlock(&dirty_i->seglist_lock);
2171 static void destroy_victim_secmap(struct f2fs_sb_info *sbi)
2173 struct dirty_seglist_info *dirty_i = DIRTY_I(sbi);
2174 kfree(dirty_i->victim_secmap);
2177 static void destroy_dirty_segmap(struct f2fs_sb_info *sbi)
2179 struct dirty_seglist_info *dirty_i = DIRTY_I(sbi);
2185 /* discard pre-free/dirty segments list */
2186 for (i = 0; i < NR_DIRTY_TYPE; i++)
2187 discard_dirty_segmap(sbi, i);
2189 destroy_victim_secmap(sbi);
2190 SM_I(sbi)->dirty_info = NULL;
2194 static void destroy_curseg(struct f2fs_sb_info *sbi)
2196 struct curseg_info *array = SM_I(sbi)->curseg_array;
2201 SM_I(sbi)->curseg_array = NULL;
2202 for (i = 0; i < NR_CURSEG_TYPE; i++)
2203 kfree(array[i].sum_blk);
2207 static void destroy_free_segmap(struct f2fs_sb_info *sbi)
2209 struct free_segmap_info *free_i = SM_I(sbi)->free_info;
2212 SM_I(sbi)->free_info = NULL;
2213 kfree(free_i->free_segmap);
2214 kfree(free_i->free_secmap);
2218 static void destroy_sit_info(struct f2fs_sb_info *sbi)
2220 struct sit_info *sit_i = SIT_I(sbi);
2226 if (sit_i->sentries) {
2227 for (start = 0; start < MAIN_SEGS(sbi); start++) {
2228 kfree(sit_i->sentries[start].cur_valid_map);
2229 kfree(sit_i->sentries[start].ckpt_valid_map);
2232 vfree(sit_i->sentries);
2233 vfree(sit_i->sec_entries);
2234 kfree(sit_i->dirty_sentries_bitmap);
2236 SM_I(sbi)->sit_info = NULL;
2237 kfree(sit_i->sit_bitmap);
2241 void destroy_segment_manager(struct f2fs_sb_info *sbi)
2243 struct f2fs_sm_info *sm_info = SM_I(sbi);
2247 destroy_flush_cmd_control(sbi);
2248 destroy_dirty_segmap(sbi);
2249 destroy_curseg(sbi);
2250 destroy_free_segmap(sbi);
2251 destroy_sit_info(sbi);
2252 sbi->sm_info = NULL;
2256 int __init create_segment_manager_caches(void)
2258 discard_entry_slab = f2fs_kmem_cache_create("discard_entry",
2259 sizeof(struct discard_entry));
2260 if (!discard_entry_slab)
2263 sit_entry_set_slab = f2fs_kmem_cache_create("sit_entry_set",
2264 sizeof(struct sit_entry_set));
2265 if (!sit_entry_set_slab)
2266 goto destory_discard_entry;
2268 inmem_entry_slab = f2fs_kmem_cache_create("inmem_page_entry",
2269 sizeof(struct inmem_pages));
2270 if (!inmem_entry_slab)
2271 goto destroy_sit_entry_set;
2274 destroy_sit_entry_set:
2275 kmem_cache_destroy(sit_entry_set_slab);
2276 destory_discard_entry:
2277 kmem_cache_destroy(discard_entry_slab);
2282 void destroy_segment_manager_caches(void)
2284 kmem_cache_destroy(sit_entry_set_slab);
2285 kmem_cache_destroy(discard_entry_slab);
2286 kmem_cache_destroy(inmem_entry_slab);