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/swap.h>
18 #include <linux/timer.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 *discard_cmd_slab;
30 static struct kmem_cache *sit_entry_set_slab;
31 static struct kmem_cache *inmem_entry_slab;
33 static unsigned long __reverse_ulong(unsigned char *str)
35 unsigned long tmp = 0;
36 int shift = 24, idx = 0;
38 #if BITS_PER_LONG == 64
42 tmp |= (unsigned long)str[idx++] << shift;
43 shift -= BITS_PER_BYTE;
49 * __reverse_ffs is copied from include/asm-generic/bitops/__ffs.h since
50 * MSB and LSB are reversed in a byte by f2fs_set_bit.
52 static inline unsigned long __reverse_ffs(unsigned long word)
56 #if BITS_PER_LONG == 64
57 if ((word & 0xffffffff00000000UL) == 0)
62 if ((word & 0xffff0000) == 0)
67 if ((word & 0xff00) == 0)
72 if ((word & 0xf0) == 0)
77 if ((word & 0xc) == 0)
82 if ((word & 0x2) == 0)
88 * __find_rev_next(_zero)_bit is copied from lib/find_next_bit.c because
89 * f2fs_set_bit makes MSB and LSB reversed in a byte.
90 * @size must be integral times of unsigned long.
93 * f2fs_set_bit(0, bitmap) => 1000 0000
94 * f2fs_set_bit(7, bitmap) => 0000 0001
96 static unsigned long __find_rev_next_bit(const unsigned long *addr,
97 unsigned long size, unsigned long offset)
99 const unsigned long *p = addr + BIT_WORD(offset);
100 unsigned long result = size;
106 size -= (offset & ~(BITS_PER_LONG - 1));
107 offset %= BITS_PER_LONG;
113 tmp = __reverse_ulong((unsigned char *)p);
115 tmp &= ~0UL >> offset;
116 if (size < BITS_PER_LONG)
117 tmp &= (~0UL << (BITS_PER_LONG - size));
121 if (size <= BITS_PER_LONG)
123 size -= BITS_PER_LONG;
129 return result - size + __reverse_ffs(tmp);
132 static unsigned long __find_rev_next_zero_bit(const unsigned long *addr,
133 unsigned long size, unsigned long offset)
135 const unsigned long *p = addr + BIT_WORD(offset);
136 unsigned long result = size;
142 size -= (offset & ~(BITS_PER_LONG - 1));
143 offset %= BITS_PER_LONG;
149 tmp = __reverse_ulong((unsigned char *)p);
152 tmp |= ~0UL << (BITS_PER_LONG - offset);
153 if (size < BITS_PER_LONG)
158 if (size <= BITS_PER_LONG)
160 size -= BITS_PER_LONG;
166 return result - size + __reverse_ffz(tmp);
169 void register_inmem_page(struct inode *inode, struct page *page)
171 struct f2fs_inode_info *fi = F2FS_I(inode);
172 struct inmem_pages *new;
174 f2fs_trace_pid(page);
176 set_page_private(page, (unsigned long)ATOMIC_WRITTEN_PAGE);
177 SetPagePrivate(page);
179 new = f2fs_kmem_cache_alloc(inmem_entry_slab, GFP_NOFS);
181 /* add atomic page indices to the list */
183 INIT_LIST_HEAD(&new->list);
185 /* increase reference count with clean state */
186 mutex_lock(&fi->inmem_lock);
188 list_add_tail(&new->list, &fi->inmem_pages);
189 inc_page_count(F2FS_I_SB(inode), F2FS_INMEM_PAGES);
190 mutex_unlock(&fi->inmem_lock);
192 trace_f2fs_register_inmem_page(page, INMEM);
195 static int __revoke_inmem_pages(struct inode *inode,
196 struct list_head *head, bool drop, bool recover)
198 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
199 struct inmem_pages *cur, *tmp;
202 list_for_each_entry_safe(cur, tmp, head, list) {
203 struct page *page = cur->page;
206 trace_f2fs_commit_inmem_page(page, INMEM_DROP);
211 struct dnode_of_data dn;
214 trace_f2fs_commit_inmem_page(page, INMEM_REVOKE);
216 set_new_dnode(&dn, inode, NULL, NULL, 0);
217 if (get_dnode_of_data(&dn, page->index, LOOKUP_NODE)) {
221 get_node_info(sbi, dn.nid, &ni);
222 f2fs_replace_block(sbi, &dn, dn.data_blkaddr,
223 cur->old_addr, ni.version, true, true);
227 /* we don't need to invalidate this in the sccessful status */
229 ClearPageUptodate(page);
230 set_page_private(page, 0);
231 ClearPagePrivate(page);
232 f2fs_put_page(page, 1);
234 list_del(&cur->list);
235 kmem_cache_free(inmem_entry_slab, cur);
236 dec_page_count(F2FS_I_SB(inode), F2FS_INMEM_PAGES);
241 void drop_inmem_pages(struct inode *inode)
243 struct f2fs_inode_info *fi = F2FS_I(inode);
245 mutex_lock(&fi->inmem_lock);
246 __revoke_inmem_pages(inode, &fi->inmem_pages, true, false);
247 mutex_unlock(&fi->inmem_lock);
249 clear_inode_flag(inode, FI_ATOMIC_FILE);
250 stat_dec_atomic_write(inode);
253 void drop_inmem_page(struct inode *inode, struct page *page)
255 struct f2fs_inode_info *fi = F2FS_I(inode);
256 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
257 struct list_head *head = &fi->inmem_pages;
258 struct inmem_pages *cur = NULL;
260 f2fs_bug_on(sbi, !IS_ATOMIC_WRITTEN_PAGE(page));
262 mutex_lock(&fi->inmem_lock);
263 list_for_each_entry(cur, head, list) {
264 if (cur->page == page)
268 f2fs_bug_on(sbi, !cur || cur->page != page);
269 list_del(&cur->list);
270 mutex_unlock(&fi->inmem_lock);
272 dec_page_count(sbi, F2FS_INMEM_PAGES);
273 kmem_cache_free(inmem_entry_slab, cur);
275 ClearPageUptodate(page);
276 set_page_private(page, 0);
277 ClearPagePrivate(page);
278 f2fs_put_page(page, 0);
280 trace_f2fs_commit_inmem_page(page, INMEM_INVALIDATE);
283 static int __commit_inmem_pages(struct inode *inode,
284 struct list_head *revoke_list)
286 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
287 struct f2fs_inode_info *fi = F2FS_I(inode);
288 struct inmem_pages *cur, *tmp;
289 struct f2fs_io_info fio = {
293 .op_flags = REQ_SYNC | REQ_PRIO,
294 .encrypted_page = NULL,
296 pgoff_t last_idx = ULONG_MAX;
299 list_for_each_entry_safe(cur, tmp, &fi->inmem_pages, list) {
300 struct page *page = cur->page;
303 if (page->mapping == inode->i_mapping) {
304 trace_f2fs_commit_inmem_page(page, INMEM);
306 set_page_dirty(page);
307 f2fs_wait_on_page_writeback(page, DATA, true);
308 if (clear_page_dirty_for_io(page)) {
309 inode_dec_dirty_pages(inode);
310 remove_dirty_inode(inode);
314 err = do_write_data_page(&fio);
320 /* record old blkaddr for revoking */
321 cur->old_addr = fio.old_blkaddr;
322 last_idx = page->index;
325 list_move_tail(&cur->list, revoke_list);
328 if (last_idx != ULONG_MAX)
329 f2fs_submit_merged_bio_cond(sbi, inode, 0, last_idx,
333 __revoke_inmem_pages(inode, revoke_list, false, false);
338 int commit_inmem_pages(struct inode *inode)
340 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
341 struct f2fs_inode_info *fi = F2FS_I(inode);
342 struct list_head revoke_list;
345 INIT_LIST_HEAD(&revoke_list);
346 f2fs_balance_fs(sbi, true);
349 set_inode_flag(inode, FI_ATOMIC_COMMIT);
351 mutex_lock(&fi->inmem_lock);
352 err = __commit_inmem_pages(inode, &revoke_list);
356 * try to revoke all committed pages, but still we could fail
357 * due to no memory or other reason, if that happened, EAGAIN
358 * will be returned, which means in such case, transaction is
359 * already not integrity, caller should use journal to do the
360 * recovery or rewrite & commit last transaction. For other
361 * error number, revoking was done by filesystem itself.
363 ret = __revoke_inmem_pages(inode, &revoke_list, false, true);
367 /* drop all uncommitted pages */
368 __revoke_inmem_pages(inode, &fi->inmem_pages, true, false);
370 mutex_unlock(&fi->inmem_lock);
372 clear_inode_flag(inode, FI_ATOMIC_COMMIT);
379 * This function balances dirty node and dentry pages.
380 * In addition, it controls garbage collection.
382 void f2fs_balance_fs(struct f2fs_sb_info *sbi, bool need)
384 #ifdef CONFIG_F2FS_FAULT_INJECTION
385 if (time_to_inject(sbi, FAULT_CHECKPOINT)) {
386 f2fs_show_injection_info(FAULT_CHECKPOINT);
387 f2fs_stop_checkpoint(sbi, false);
394 /* balance_fs_bg is able to be pending */
395 if (excess_cached_nats(sbi))
396 f2fs_balance_fs_bg(sbi);
399 * We should do GC or end up with checkpoint, if there are so many dirty
400 * dir/node pages without enough free segments.
402 if (has_not_enough_free_secs(sbi, 0, 0)) {
403 mutex_lock(&sbi->gc_mutex);
404 f2fs_gc(sbi, false, false);
408 void f2fs_balance_fs_bg(struct f2fs_sb_info *sbi)
410 /* try to shrink extent cache when there is no enough memory */
411 if (!available_free_memory(sbi, EXTENT_CACHE))
412 f2fs_shrink_extent_tree(sbi, EXTENT_CACHE_SHRINK_NUMBER);
414 /* check the # of cached NAT entries */
415 if (!available_free_memory(sbi, NAT_ENTRIES))
416 try_to_free_nats(sbi, NAT_ENTRY_PER_BLOCK);
418 if (!available_free_memory(sbi, FREE_NIDS))
419 try_to_free_nids(sbi, MAX_FREE_NIDS);
421 build_free_nids(sbi, false, false);
426 /* checkpoint is the only way to shrink partial cached entries */
427 if (!available_free_memory(sbi, NAT_ENTRIES) ||
428 !available_free_memory(sbi, INO_ENTRIES) ||
429 excess_prefree_segs(sbi) ||
430 excess_dirty_nats(sbi) ||
431 f2fs_time_over(sbi, CP_TIME)) {
432 if (test_opt(sbi, DATA_FLUSH)) {
433 struct blk_plug plug;
435 blk_start_plug(&plug);
436 sync_dirty_inodes(sbi, FILE_INODE);
437 blk_finish_plug(&plug);
439 f2fs_sync_fs(sbi->sb, true);
440 stat_inc_bg_cp_count(sbi->stat_info);
444 static int __submit_flush_wait(struct f2fs_sb_info *sbi,
445 struct block_device *bdev)
447 struct bio *bio = f2fs_bio_alloc(0);
450 bio->bi_opf = REQ_OP_WRITE | REQ_PREFLUSH;
452 ret = submit_bio_wait(bio);
455 trace_f2fs_issue_flush(bdev, test_opt(sbi, NOBARRIER),
456 test_opt(sbi, FLUSH_MERGE), ret);
460 static int submit_flush_wait(struct f2fs_sb_info *sbi)
462 int ret = __submit_flush_wait(sbi, sbi->sb->s_bdev);
465 if (!sbi->s_ndevs || ret)
468 for (i = 1; i < sbi->s_ndevs; i++) {
469 ret = __submit_flush_wait(sbi, FDEV(i).bdev);
476 static int issue_flush_thread(void *data)
478 struct f2fs_sb_info *sbi = data;
479 struct flush_cmd_control *fcc = SM_I(sbi)->fcc_info;
480 wait_queue_head_t *q = &fcc->flush_wait_queue;
482 if (kthread_should_stop())
485 if (!llist_empty(&fcc->issue_list)) {
486 struct flush_cmd *cmd, *next;
489 fcc->dispatch_list = llist_del_all(&fcc->issue_list);
490 fcc->dispatch_list = llist_reverse_order(fcc->dispatch_list);
492 ret = submit_flush_wait(sbi);
493 llist_for_each_entry_safe(cmd, next,
494 fcc->dispatch_list, llnode) {
496 complete(&cmd->wait);
498 fcc->dispatch_list = NULL;
501 wait_event_interruptible(*q,
502 kthread_should_stop() || !llist_empty(&fcc->issue_list));
506 int f2fs_issue_flush(struct f2fs_sb_info *sbi)
508 struct flush_cmd_control *fcc = SM_I(sbi)->fcc_info;
509 struct flush_cmd cmd;
511 if (test_opt(sbi, NOBARRIER))
514 if (!test_opt(sbi, FLUSH_MERGE))
515 return submit_flush_wait(sbi);
517 if (!atomic_read(&fcc->submit_flush)) {
520 atomic_inc(&fcc->submit_flush);
521 ret = submit_flush_wait(sbi);
522 atomic_dec(&fcc->submit_flush);
526 init_completion(&cmd.wait);
528 atomic_inc(&fcc->submit_flush);
529 llist_add(&cmd.llnode, &fcc->issue_list);
531 if (!fcc->dispatch_list)
532 wake_up(&fcc->flush_wait_queue);
534 if (fcc->f2fs_issue_flush) {
535 wait_for_completion(&cmd.wait);
536 atomic_dec(&fcc->submit_flush);
538 llist_del_all(&fcc->issue_list);
539 atomic_set(&fcc->submit_flush, 0);
545 int create_flush_cmd_control(struct f2fs_sb_info *sbi)
547 dev_t dev = sbi->sb->s_bdev->bd_dev;
548 struct flush_cmd_control *fcc;
551 if (SM_I(sbi)->fcc_info) {
552 fcc = SM_I(sbi)->fcc_info;
556 fcc = kzalloc(sizeof(struct flush_cmd_control), GFP_KERNEL);
559 atomic_set(&fcc->submit_flush, 0);
560 init_waitqueue_head(&fcc->flush_wait_queue);
561 init_llist_head(&fcc->issue_list);
562 SM_I(sbi)->fcc_info = fcc;
564 fcc->f2fs_issue_flush = kthread_run(issue_flush_thread, sbi,
565 "f2fs_flush-%u:%u", MAJOR(dev), MINOR(dev));
566 if (IS_ERR(fcc->f2fs_issue_flush)) {
567 err = PTR_ERR(fcc->f2fs_issue_flush);
569 SM_I(sbi)->fcc_info = NULL;
576 void destroy_flush_cmd_control(struct f2fs_sb_info *sbi, bool free)
578 struct flush_cmd_control *fcc = SM_I(sbi)->fcc_info;
580 if (fcc && fcc->f2fs_issue_flush) {
581 struct task_struct *flush_thread = fcc->f2fs_issue_flush;
583 fcc->f2fs_issue_flush = NULL;
584 kthread_stop(flush_thread);
588 SM_I(sbi)->fcc_info = NULL;
592 static void __locate_dirty_segment(struct f2fs_sb_info *sbi, unsigned int segno,
593 enum dirty_type dirty_type)
595 struct dirty_seglist_info *dirty_i = DIRTY_I(sbi);
597 /* need not be added */
598 if (IS_CURSEG(sbi, segno))
601 if (!test_and_set_bit(segno, dirty_i->dirty_segmap[dirty_type]))
602 dirty_i->nr_dirty[dirty_type]++;
604 if (dirty_type == DIRTY) {
605 struct seg_entry *sentry = get_seg_entry(sbi, segno);
606 enum dirty_type t = sentry->type;
608 if (unlikely(t >= DIRTY)) {
612 if (!test_and_set_bit(segno, dirty_i->dirty_segmap[t]))
613 dirty_i->nr_dirty[t]++;
617 static void __remove_dirty_segment(struct f2fs_sb_info *sbi, unsigned int segno,
618 enum dirty_type dirty_type)
620 struct dirty_seglist_info *dirty_i = DIRTY_I(sbi);
622 if (test_and_clear_bit(segno, dirty_i->dirty_segmap[dirty_type]))
623 dirty_i->nr_dirty[dirty_type]--;
625 if (dirty_type == DIRTY) {
626 struct seg_entry *sentry = get_seg_entry(sbi, segno);
627 enum dirty_type t = sentry->type;
629 if (test_and_clear_bit(segno, dirty_i->dirty_segmap[t]))
630 dirty_i->nr_dirty[t]--;
632 if (get_valid_blocks(sbi, segno, sbi->segs_per_sec) == 0)
633 clear_bit(GET_SECNO(sbi, segno),
634 dirty_i->victim_secmap);
639 * Should not occur error such as -ENOMEM.
640 * Adding dirty entry into seglist is not critical operation.
641 * If a given segment is one of current working segments, it won't be added.
643 static void locate_dirty_segment(struct f2fs_sb_info *sbi, unsigned int segno)
645 struct dirty_seglist_info *dirty_i = DIRTY_I(sbi);
646 unsigned short valid_blocks;
648 if (segno == NULL_SEGNO || IS_CURSEG(sbi, segno))
651 mutex_lock(&dirty_i->seglist_lock);
653 valid_blocks = get_valid_blocks(sbi, segno, 0);
655 if (valid_blocks == 0) {
656 __locate_dirty_segment(sbi, segno, PRE);
657 __remove_dirty_segment(sbi, segno, DIRTY);
658 } else if (valid_blocks < sbi->blocks_per_seg) {
659 __locate_dirty_segment(sbi, segno, DIRTY);
661 /* Recovery routine with SSR needs this */
662 __remove_dirty_segment(sbi, segno, DIRTY);
665 mutex_unlock(&dirty_i->seglist_lock);
668 static void __add_discard_cmd(struct f2fs_sb_info *sbi,
669 struct block_device *bdev, block_t lstart,
670 block_t start, block_t len)
672 struct discard_cmd_control *dcc = SM_I(sbi)->dcc_info;
673 struct list_head *cmd_list = &(dcc->discard_cmd_list);
674 struct discard_cmd *dc;
676 dc = f2fs_kmem_cache_alloc(discard_cmd_slab, GFP_NOFS);
677 INIT_LIST_HEAD(&dc->list);
684 init_completion(&dc->wait);
686 mutex_lock(&dcc->cmd_lock);
687 list_add_tail(&dc->list, cmd_list);
688 mutex_unlock(&dcc->cmd_lock);
691 static void __remove_discard_cmd(struct f2fs_sb_info *sbi, struct discard_cmd *dc)
693 if (dc->state == D_DONE)
694 atomic_dec(&(SM_I(sbi)->dcc_info->submit_discard));
696 if (dc->error == -EOPNOTSUPP)
700 f2fs_msg(sbi->sb, KERN_INFO,
701 "Issue discard failed, ret: %d", dc->error);
703 kmem_cache_free(discard_cmd_slab, dc);
706 static void f2fs_submit_discard_endio(struct bio *bio)
708 struct discard_cmd *dc = (struct discard_cmd *)bio->bi_private;
711 dc->error = bio->bi_error;
716 /* this function is copied from blkdev_issue_discard from block/blk-lib.c */
717 static void __submit_discard_cmd(struct f2fs_sb_info *sbi,
718 struct discard_cmd *dc)
720 struct discard_cmd_control *dcc = SM_I(sbi)->dcc_info;
721 struct bio *bio = NULL;
723 if (dc->state != D_PREP)
726 dc->error = __blkdev_issue_discard(dc->bdev,
727 SECTOR_FROM_BLOCK(dc->start),
728 SECTOR_FROM_BLOCK(dc->len),
731 /* should keep before submission to avoid D_DONE right away */
732 dc->state = D_SUBMIT;
733 atomic_inc(&dcc->submit_discard);
735 bio->bi_private = dc;
736 bio->bi_end_io = f2fs_submit_discard_endio;
737 bio->bi_opf |= REQ_SYNC;
741 __remove_discard_cmd(sbi, dc);
745 static int __queue_discard_cmd(struct f2fs_sb_info *sbi,
746 struct block_device *bdev, block_t blkstart, block_t blklen)
748 block_t lblkstart = blkstart;
750 trace_f2fs_issue_discard(bdev, blkstart, blklen);
753 int devi = f2fs_target_device_index(sbi, blkstart);
755 blkstart -= FDEV(devi).start_blk;
757 __add_discard_cmd(sbi, bdev, lblkstart, blkstart, blklen);
758 wake_up(&SM_I(sbi)->dcc_info->discard_wait_queue);
762 static void __punch_discard_cmd(struct f2fs_sb_info *sbi,
763 struct discard_cmd *dc, block_t blkaddr)
765 block_t end_block = START_BLOCK(sbi, GET_SEGNO(sbi, blkaddr) + 1);
767 if (dc->state == D_DONE || dc->lstart + dc->len <= end_block) {
768 __remove_discard_cmd(sbi, dc);
772 if (blkaddr - dc->lstart < dc->lstart + dc->len - end_block) {
773 dc->start += (end_block - dc->lstart);
774 dc->len -= (end_block - dc->lstart);
775 dc->lstart = end_block;
777 dc->len = blkaddr - dc->lstart;
781 /* This should be covered by global mutex, &sit_i->sentry_lock */
782 void f2fs_wait_discard_bio(struct f2fs_sb_info *sbi, block_t blkaddr)
784 struct discard_cmd_control *dcc = SM_I(sbi)->dcc_info;
785 struct list_head *wait_list = &(dcc->discard_cmd_list);
786 struct discard_cmd *dc, *tmp;
787 struct blk_plug plug;
789 mutex_lock(&dcc->cmd_lock);
791 blk_start_plug(&plug);
793 list_for_each_entry_safe(dc, tmp, wait_list, list) {
795 if (blkaddr == NULL_ADDR) {
796 __submit_discard_cmd(sbi, dc);
800 if (dc->lstart <= blkaddr && blkaddr < dc->lstart + dc->len) {
801 if (dc->state == D_SUBMIT)
802 wait_for_completion_io(&dc->wait);
803 __punch_discard_cmd(sbi, dc, blkaddr);
806 blk_finish_plug(&plug);
808 /* this comes from f2fs_put_super */
809 if (blkaddr == NULL_ADDR) {
810 list_for_each_entry_safe(dc, tmp, wait_list, list) {
811 wait_for_completion_io(&dc->wait);
812 __remove_discard_cmd(sbi, dc);
815 mutex_unlock(&dcc->cmd_lock);
818 static int issue_discard_thread(void *data)
820 struct f2fs_sb_info *sbi = data;
821 struct discard_cmd_control *dcc = SM_I(sbi)->dcc_info;
822 wait_queue_head_t *q = &dcc->discard_wait_queue;
823 struct list_head *cmd_list = &dcc->discard_cmd_list;
824 struct discard_cmd *dc, *tmp;
825 struct blk_plug plug;
828 if (kthread_should_stop())
831 blk_start_plug(&plug);
833 mutex_lock(&dcc->cmd_lock);
834 list_for_each_entry_safe(dc, tmp, cmd_list, list) {
837 __submit_discard_cmd(sbi, dc);
839 if (dc->state == D_PREP && iter++ > DISCARD_ISSUE_RATE)
841 if (dc->state == D_DONE)
842 __remove_discard_cmd(sbi, dc);
844 mutex_unlock(&dcc->cmd_lock);
846 blk_finish_plug(&plug);
849 congestion_wait(BLK_RW_SYNC, HZ/50);
851 wait_event_interruptible(*q,
852 kthread_should_stop() || !list_empty(&dcc->discard_cmd_list));
856 #ifdef CONFIG_BLK_DEV_ZONED
857 static int __f2fs_issue_discard_zone(struct f2fs_sb_info *sbi,
858 struct block_device *bdev, block_t blkstart, block_t blklen)
860 sector_t sector, nr_sects;
861 block_t lblkstart = blkstart;
865 devi = f2fs_target_device_index(sbi, blkstart);
866 blkstart -= FDEV(devi).start_blk;
870 * We need to know the type of the zone: for conventional zones,
871 * use regular discard if the drive supports it. For sequential
872 * zones, reset the zone write pointer.
874 switch (get_blkz_type(sbi, bdev, blkstart)) {
876 case BLK_ZONE_TYPE_CONVENTIONAL:
877 if (!blk_queue_discard(bdev_get_queue(bdev)))
879 return __queue_discard_cmd(sbi, bdev, lblkstart, blklen);
880 case BLK_ZONE_TYPE_SEQWRITE_REQ:
881 case BLK_ZONE_TYPE_SEQWRITE_PREF:
882 sector = SECTOR_FROM_BLOCK(blkstart);
883 nr_sects = SECTOR_FROM_BLOCK(blklen);
885 if (sector & (bdev_zone_sectors(bdev) - 1) ||
886 nr_sects != bdev_zone_sectors(bdev)) {
887 f2fs_msg(sbi->sb, KERN_INFO,
888 "(%d) %s: Unaligned discard attempted (block %x + %x)",
889 devi, sbi->s_ndevs ? FDEV(devi).path: "",
893 trace_f2fs_issue_reset_zone(bdev, blkstart);
894 return blkdev_reset_zones(bdev, sector,
897 /* Unknown zone type: broken device ? */
903 static int __issue_discard_async(struct f2fs_sb_info *sbi,
904 struct block_device *bdev, block_t blkstart, block_t blklen)
906 #ifdef CONFIG_BLK_DEV_ZONED
907 if (f2fs_sb_mounted_blkzoned(sbi->sb) &&
908 bdev_zoned_model(bdev) != BLK_ZONED_NONE)
909 return __f2fs_issue_discard_zone(sbi, bdev, blkstart, blklen);
911 return __queue_discard_cmd(sbi, bdev, blkstart, blklen);
914 static int f2fs_issue_discard(struct f2fs_sb_info *sbi,
915 block_t blkstart, block_t blklen)
917 sector_t start = blkstart, len = 0;
918 struct block_device *bdev;
919 struct seg_entry *se;
924 bdev = f2fs_target_device(sbi, blkstart, NULL);
926 for (i = blkstart; i < blkstart + blklen; i++, len++) {
928 struct block_device *bdev2 =
929 f2fs_target_device(sbi, i, NULL);
932 err = __issue_discard_async(sbi, bdev,
942 se = get_seg_entry(sbi, GET_SEGNO(sbi, i));
943 offset = GET_BLKOFF_FROM_SEG0(sbi, i);
945 if (!f2fs_test_and_set_bit(offset, se->discard_map))
950 err = __issue_discard_async(sbi, bdev, start, len);
954 static void __add_discard_entry(struct f2fs_sb_info *sbi,
955 struct cp_control *cpc, struct seg_entry *se,
956 unsigned int start, unsigned int end)
958 struct list_head *head = &SM_I(sbi)->dcc_info->discard_entry_list;
959 struct discard_entry *new, *last;
961 if (!list_empty(head)) {
962 last = list_last_entry(head, struct discard_entry, list);
963 if (START_BLOCK(sbi, cpc->trim_start) + start ==
964 last->blkaddr + last->len &&
965 last->len < MAX_DISCARD_BLOCKS(sbi)) {
966 last->len += end - start;
971 new = f2fs_kmem_cache_alloc(discard_entry_slab, GFP_NOFS);
972 INIT_LIST_HEAD(&new->list);
973 new->blkaddr = START_BLOCK(sbi, cpc->trim_start) + start;
974 new->len = end - start;
975 list_add_tail(&new->list, head);
977 SM_I(sbi)->dcc_info->nr_discards += end - start;
980 static bool add_discard_addrs(struct f2fs_sb_info *sbi, struct cp_control *cpc,
983 int entries = SIT_VBLOCK_MAP_SIZE / sizeof(unsigned long);
984 int max_blocks = sbi->blocks_per_seg;
985 struct seg_entry *se = get_seg_entry(sbi, cpc->trim_start);
986 unsigned long *cur_map = (unsigned long *)se->cur_valid_map;
987 unsigned long *ckpt_map = (unsigned long *)se->ckpt_valid_map;
988 unsigned long *discard_map = (unsigned long *)se->discard_map;
989 unsigned long *dmap = SIT_I(sbi)->tmp_map;
990 unsigned int start = 0, end = -1;
991 bool force = (cpc->reason == CP_DISCARD);
994 if (se->valid_blocks == max_blocks || !f2fs_discard_en(sbi))
998 if (!test_opt(sbi, DISCARD) || !se->valid_blocks ||
999 SM_I(sbi)->dcc_info->nr_discards >=
1000 SM_I(sbi)->dcc_info->max_discards)
1004 /* SIT_VBLOCK_MAP_SIZE should be multiple of sizeof(unsigned long) */
1005 for (i = 0; i < entries; i++)
1006 dmap[i] = force ? ~ckpt_map[i] & ~discard_map[i] :
1007 (cur_map[i] ^ ckpt_map[i]) & ckpt_map[i];
1009 while (force || SM_I(sbi)->dcc_info->nr_discards <=
1010 SM_I(sbi)->dcc_info->max_discards) {
1011 start = __find_rev_next_bit(dmap, max_blocks, end + 1);
1012 if (start >= max_blocks)
1015 end = __find_rev_next_zero_bit(dmap, max_blocks, start + 1);
1016 if (force && start && end != max_blocks
1017 && (end - start) < cpc->trim_minlen)
1023 __add_discard_entry(sbi, cpc, se, start, end);
1028 void release_discard_addrs(struct f2fs_sb_info *sbi)
1030 struct list_head *head = &(SM_I(sbi)->dcc_info->discard_entry_list);
1031 struct discard_entry *entry, *this;
1034 list_for_each_entry_safe(entry, this, head, list) {
1035 list_del(&entry->list);
1036 kmem_cache_free(discard_entry_slab, entry);
1041 * Should call clear_prefree_segments after checkpoint is done.
1043 static void set_prefree_as_free_segments(struct f2fs_sb_info *sbi)
1045 struct dirty_seglist_info *dirty_i = DIRTY_I(sbi);
1048 mutex_lock(&dirty_i->seglist_lock);
1049 for_each_set_bit(segno, dirty_i->dirty_segmap[PRE], MAIN_SEGS(sbi))
1050 __set_test_and_free(sbi, segno);
1051 mutex_unlock(&dirty_i->seglist_lock);
1054 void clear_prefree_segments(struct f2fs_sb_info *sbi, struct cp_control *cpc)
1056 struct list_head *head = &(SM_I(sbi)->dcc_info->discard_entry_list);
1057 struct discard_entry *entry, *this;
1058 struct dirty_seglist_info *dirty_i = DIRTY_I(sbi);
1059 unsigned long *prefree_map = dirty_i->dirty_segmap[PRE];
1060 unsigned int start = 0, end = -1;
1061 unsigned int secno, start_segno;
1062 bool force = (cpc->reason == CP_DISCARD);
1064 mutex_lock(&dirty_i->seglist_lock);
1068 start = find_next_bit(prefree_map, MAIN_SEGS(sbi), end + 1);
1069 if (start >= MAIN_SEGS(sbi))
1071 end = find_next_zero_bit(prefree_map, MAIN_SEGS(sbi),
1074 for (i = start; i < end; i++)
1075 clear_bit(i, prefree_map);
1077 dirty_i->nr_dirty[PRE] -= end - start;
1079 if (!test_opt(sbi, DISCARD))
1082 if (force && start >= cpc->trim_start &&
1083 (end - 1) <= cpc->trim_end)
1086 if (!test_opt(sbi, LFS) || sbi->segs_per_sec == 1) {
1087 f2fs_issue_discard(sbi, START_BLOCK(sbi, start),
1088 (end - start) << sbi->log_blocks_per_seg);
1092 secno = GET_SECNO(sbi, start);
1093 start_segno = secno * sbi->segs_per_sec;
1094 if (!IS_CURSEC(sbi, secno) &&
1095 !get_valid_blocks(sbi, start, sbi->segs_per_sec))
1096 f2fs_issue_discard(sbi, START_BLOCK(sbi, start_segno),
1097 sbi->segs_per_sec << sbi->log_blocks_per_seg);
1099 start = start_segno + sbi->segs_per_sec;
1105 mutex_unlock(&dirty_i->seglist_lock);
1107 /* send small discards */
1108 list_for_each_entry_safe(entry, this, head, list) {
1109 if (force && entry->len < cpc->trim_minlen)
1111 f2fs_issue_discard(sbi, entry->blkaddr, entry->len);
1112 cpc->trimmed += entry->len;
1114 list_del(&entry->list);
1115 SM_I(sbi)->dcc_info->nr_discards -= entry->len;
1116 kmem_cache_free(discard_entry_slab, entry);
1120 static int create_discard_cmd_control(struct f2fs_sb_info *sbi)
1122 dev_t dev = sbi->sb->s_bdev->bd_dev;
1123 struct discard_cmd_control *dcc;
1126 if (SM_I(sbi)->dcc_info) {
1127 dcc = SM_I(sbi)->dcc_info;
1131 dcc = kzalloc(sizeof(struct discard_cmd_control), GFP_KERNEL);
1135 INIT_LIST_HEAD(&dcc->discard_entry_list);
1136 INIT_LIST_HEAD(&dcc->discard_cmd_list);
1137 mutex_init(&dcc->cmd_lock);
1138 atomic_set(&dcc->submit_discard, 0);
1139 dcc->nr_discards = 0;
1140 dcc->max_discards = 0;
1142 init_waitqueue_head(&dcc->discard_wait_queue);
1143 SM_I(sbi)->dcc_info = dcc;
1145 dcc->f2fs_issue_discard = kthread_run(issue_discard_thread, sbi,
1146 "f2fs_discard-%u:%u", MAJOR(dev), MINOR(dev));
1147 if (IS_ERR(dcc->f2fs_issue_discard)) {
1148 err = PTR_ERR(dcc->f2fs_issue_discard);
1150 SM_I(sbi)->dcc_info = NULL;
1157 static void destroy_discard_cmd_control(struct f2fs_sb_info *sbi, bool free)
1159 struct discard_cmd_control *dcc = SM_I(sbi)->dcc_info;
1161 if (dcc && dcc->f2fs_issue_discard) {
1162 struct task_struct *discard_thread = dcc->f2fs_issue_discard;
1164 dcc->f2fs_issue_discard = NULL;
1165 kthread_stop(discard_thread);
1169 SM_I(sbi)->dcc_info = NULL;
1173 static bool __mark_sit_entry_dirty(struct f2fs_sb_info *sbi, unsigned int segno)
1175 struct sit_info *sit_i = SIT_I(sbi);
1177 if (!__test_and_set_bit(segno, sit_i->dirty_sentries_bitmap)) {
1178 sit_i->dirty_sentries++;
1185 static void __set_sit_entry_type(struct f2fs_sb_info *sbi, int type,
1186 unsigned int segno, int modified)
1188 struct seg_entry *se = get_seg_entry(sbi, segno);
1191 __mark_sit_entry_dirty(sbi, segno);
1194 static void update_sit_entry(struct f2fs_sb_info *sbi, block_t blkaddr, int del)
1196 struct seg_entry *se;
1197 unsigned int segno, offset;
1198 long int new_vblocks;
1200 segno = GET_SEGNO(sbi, blkaddr);
1202 se = get_seg_entry(sbi, segno);
1203 new_vblocks = se->valid_blocks + del;
1204 offset = GET_BLKOFF_FROM_SEG0(sbi, blkaddr);
1206 f2fs_bug_on(sbi, (new_vblocks >> (sizeof(unsigned short) << 3) ||
1207 (new_vblocks > sbi->blocks_per_seg)));
1209 se->valid_blocks = new_vblocks;
1210 se->mtime = get_mtime(sbi);
1211 SIT_I(sbi)->max_mtime = se->mtime;
1213 /* Update valid block bitmap */
1215 if (f2fs_test_and_set_bit(offset, se->cur_valid_map)) {
1216 #ifdef CONFIG_F2FS_CHECK_FS
1217 if (f2fs_test_and_set_bit(offset,
1218 se->cur_valid_map_mir))
1219 f2fs_bug_on(sbi, 1);
1223 f2fs_bug_on(sbi, 1);
1226 if (f2fs_discard_en(sbi) &&
1227 !f2fs_test_and_set_bit(offset, se->discard_map))
1228 sbi->discard_blks--;
1230 /* don't overwrite by SSR to keep node chain */
1231 if (se->type == CURSEG_WARM_NODE) {
1232 if (!f2fs_test_and_set_bit(offset, se->ckpt_valid_map))
1233 se->ckpt_valid_blocks++;
1236 if (!f2fs_test_and_clear_bit(offset, se->cur_valid_map)) {
1237 #ifdef CONFIG_F2FS_CHECK_FS
1238 if (!f2fs_test_and_clear_bit(offset,
1239 se->cur_valid_map_mir))
1240 f2fs_bug_on(sbi, 1);
1244 f2fs_bug_on(sbi, 1);
1247 if (f2fs_discard_en(sbi) &&
1248 f2fs_test_and_clear_bit(offset, se->discard_map))
1249 sbi->discard_blks++;
1251 if (!f2fs_test_bit(offset, se->ckpt_valid_map))
1252 se->ckpt_valid_blocks += del;
1254 __mark_sit_entry_dirty(sbi, segno);
1256 /* update total number of valid blocks to be written in ckpt area */
1257 SIT_I(sbi)->written_valid_blocks += del;
1259 if (sbi->segs_per_sec > 1)
1260 get_sec_entry(sbi, segno)->valid_blocks += del;
1263 void refresh_sit_entry(struct f2fs_sb_info *sbi, block_t old, block_t new)
1265 update_sit_entry(sbi, new, 1);
1266 if (GET_SEGNO(sbi, old) != NULL_SEGNO)
1267 update_sit_entry(sbi, old, -1);
1269 locate_dirty_segment(sbi, GET_SEGNO(sbi, old));
1270 locate_dirty_segment(sbi, GET_SEGNO(sbi, new));
1273 void invalidate_blocks(struct f2fs_sb_info *sbi, block_t addr)
1275 unsigned int segno = GET_SEGNO(sbi, addr);
1276 struct sit_info *sit_i = SIT_I(sbi);
1278 f2fs_bug_on(sbi, addr == NULL_ADDR);
1279 if (addr == NEW_ADDR)
1282 /* add it into sit main buffer */
1283 mutex_lock(&sit_i->sentry_lock);
1285 update_sit_entry(sbi, addr, -1);
1287 /* add it into dirty seglist */
1288 locate_dirty_segment(sbi, segno);
1290 mutex_unlock(&sit_i->sentry_lock);
1293 bool is_checkpointed_data(struct f2fs_sb_info *sbi, block_t blkaddr)
1295 struct sit_info *sit_i = SIT_I(sbi);
1296 unsigned int segno, offset;
1297 struct seg_entry *se;
1300 if (blkaddr == NEW_ADDR || blkaddr == NULL_ADDR)
1303 mutex_lock(&sit_i->sentry_lock);
1305 segno = GET_SEGNO(sbi, blkaddr);
1306 se = get_seg_entry(sbi, segno);
1307 offset = GET_BLKOFF_FROM_SEG0(sbi, blkaddr);
1309 if (f2fs_test_bit(offset, se->ckpt_valid_map))
1312 mutex_unlock(&sit_i->sentry_lock);
1318 * This function should be resided under the curseg_mutex lock
1320 static void __add_sum_entry(struct f2fs_sb_info *sbi, int type,
1321 struct f2fs_summary *sum)
1323 struct curseg_info *curseg = CURSEG_I(sbi, type);
1324 void *addr = curseg->sum_blk;
1325 addr += curseg->next_blkoff * sizeof(struct f2fs_summary);
1326 memcpy(addr, sum, sizeof(struct f2fs_summary));
1330 * Calculate the number of current summary pages for writing
1332 int npages_for_summary_flush(struct f2fs_sb_info *sbi, bool for_ra)
1334 int valid_sum_count = 0;
1337 for (i = CURSEG_HOT_DATA; i <= CURSEG_COLD_DATA; i++) {
1338 if (sbi->ckpt->alloc_type[i] == SSR)
1339 valid_sum_count += sbi->blocks_per_seg;
1342 valid_sum_count += le16_to_cpu(
1343 F2FS_CKPT(sbi)->cur_data_blkoff[i]);
1345 valid_sum_count += curseg_blkoff(sbi, i);
1349 sum_in_page = (PAGE_SIZE - 2 * SUM_JOURNAL_SIZE -
1350 SUM_FOOTER_SIZE) / SUMMARY_SIZE;
1351 if (valid_sum_count <= sum_in_page)
1353 else if ((valid_sum_count - sum_in_page) <=
1354 (PAGE_SIZE - SUM_FOOTER_SIZE) / SUMMARY_SIZE)
1360 * Caller should put this summary page
1362 struct page *get_sum_page(struct f2fs_sb_info *sbi, unsigned int segno)
1364 return get_meta_page(sbi, GET_SUM_BLOCK(sbi, segno));
1367 void update_meta_page(struct f2fs_sb_info *sbi, void *src, block_t blk_addr)
1369 struct page *page = grab_meta_page(sbi, blk_addr);
1370 void *dst = page_address(page);
1373 memcpy(dst, src, PAGE_SIZE);
1375 memset(dst, 0, PAGE_SIZE);
1376 set_page_dirty(page);
1377 f2fs_put_page(page, 1);
1380 static void write_sum_page(struct f2fs_sb_info *sbi,
1381 struct f2fs_summary_block *sum_blk, block_t blk_addr)
1383 update_meta_page(sbi, (void *)sum_blk, blk_addr);
1386 static void write_current_sum_page(struct f2fs_sb_info *sbi,
1387 int type, block_t blk_addr)
1389 struct curseg_info *curseg = CURSEG_I(sbi, type);
1390 struct page *page = grab_meta_page(sbi, blk_addr);
1391 struct f2fs_summary_block *src = curseg->sum_blk;
1392 struct f2fs_summary_block *dst;
1394 dst = (struct f2fs_summary_block *)page_address(page);
1396 mutex_lock(&curseg->curseg_mutex);
1398 down_read(&curseg->journal_rwsem);
1399 memcpy(&dst->journal, curseg->journal, SUM_JOURNAL_SIZE);
1400 up_read(&curseg->journal_rwsem);
1402 memcpy(dst->entries, src->entries, SUM_ENTRY_SIZE);
1403 memcpy(&dst->footer, &src->footer, SUM_FOOTER_SIZE);
1405 mutex_unlock(&curseg->curseg_mutex);
1407 set_page_dirty(page);
1408 f2fs_put_page(page, 1);
1412 * Find a new segment from the free segments bitmap to right order
1413 * This function should be returned with success, otherwise BUG
1415 static void get_new_segment(struct f2fs_sb_info *sbi,
1416 unsigned int *newseg, bool new_sec, int dir)
1418 struct free_segmap_info *free_i = FREE_I(sbi);
1419 unsigned int segno, secno, zoneno;
1420 unsigned int total_zones = MAIN_SECS(sbi) / sbi->secs_per_zone;
1421 unsigned int hint = *newseg / sbi->segs_per_sec;
1422 unsigned int old_zoneno = GET_ZONENO_FROM_SEGNO(sbi, *newseg);
1423 unsigned int left_start = hint;
1428 spin_lock(&free_i->segmap_lock);
1430 if (!new_sec && ((*newseg + 1) % sbi->segs_per_sec)) {
1431 segno = find_next_zero_bit(free_i->free_segmap,
1432 (hint + 1) * sbi->segs_per_sec, *newseg + 1);
1433 if (segno < (hint + 1) * sbi->segs_per_sec)
1437 secno = find_next_zero_bit(free_i->free_secmap, MAIN_SECS(sbi), hint);
1438 if (secno >= MAIN_SECS(sbi)) {
1439 if (dir == ALLOC_RIGHT) {
1440 secno = find_next_zero_bit(free_i->free_secmap,
1442 f2fs_bug_on(sbi, secno >= MAIN_SECS(sbi));
1445 left_start = hint - 1;
1451 while (test_bit(left_start, free_i->free_secmap)) {
1452 if (left_start > 0) {
1456 left_start = find_next_zero_bit(free_i->free_secmap,
1458 f2fs_bug_on(sbi, left_start >= MAIN_SECS(sbi));
1464 segno = secno * sbi->segs_per_sec;
1465 zoneno = secno / sbi->secs_per_zone;
1467 /* give up on finding another zone */
1470 if (sbi->secs_per_zone == 1)
1472 if (zoneno == old_zoneno)
1474 if (dir == ALLOC_LEFT) {
1475 if (!go_left && zoneno + 1 >= total_zones)
1477 if (go_left && zoneno == 0)
1480 for (i = 0; i < NR_CURSEG_TYPE; i++)
1481 if (CURSEG_I(sbi, i)->zone == zoneno)
1484 if (i < NR_CURSEG_TYPE) {
1485 /* zone is in user, try another */
1487 hint = zoneno * sbi->secs_per_zone - 1;
1488 else if (zoneno + 1 >= total_zones)
1491 hint = (zoneno + 1) * sbi->secs_per_zone;
1493 goto find_other_zone;
1496 /* set it as dirty segment in free segmap */
1497 f2fs_bug_on(sbi, test_bit(segno, free_i->free_segmap));
1498 __set_inuse(sbi, segno);
1500 spin_unlock(&free_i->segmap_lock);
1503 static void reset_curseg(struct f2fs_sb_info *sbi, int type, int modified)
1505 struct curseg_info *curseg = CURSEG_I(sbi, type);
1506 struct summary_footer *sum_footer;
1508 curseg->segno = curseg->next_segno;
1509 curseg->zone = GET_ZONENO_FROM_SEGNO(sbi, curseg->segno);
1510 curseg->next_blkoff = 0;
1511 curseg->next_segno = NULL_SEGNO;
1513 sum_footer = &(curseg->sum_blk->footer);
1514 memset(sum_footer, 0, sizeof(struct summary_footer));
1515 if (IS_DATASEG(type))
1516 SET_SUM_TYPE(sum_footer, SUM_TYPE_DATA);
1517 if (IS_NODESEG(type))
1518 SET_SUM_TYPE(sum_footer, SUM_TYPE_NODE);
1519 __set_sit_entry_type(sbi, type, curseg->segno, modified);
1522 static unsigned int __get_next_segno(struct f2fs_sb_info *sbi, int type)
1524 if (type == CURSEG_HOT_DATA || IS_NODESEG(type))
1527 return CURSEG_I(sbi, type)->segno;
1531 * Allocate a current working segment.
1532 * This function always allocates a free segment in LFS manner.
1534 static void new_curseg(struct f2fs_sb_info *sbi, int type, bool new_sec)
1536 struct curseg_info *curseg = CURSEG_I(sbi, type);
1537 unsigned int segno = curseg->segno;
1538 int dir = ALLOC_LEFT;
1540 write_sum_page(sbi, curseg->sum_blk,
1541 GET_SUM_BLOCK(sbi, segno));
1542 if (type == CURSEG_WARM_DATA || type == CURSEG_COLD_DATA)
1545 if (test_opt(sbi, NOHEAP))
1548 segno = __get_next_segno(sbi, type);
1549 get_new_segment(sbi, &segno, new_sec, dir);
1550 curseg->next_segno = segno;
1551 reset_curseg(sbi, type, 1);
1552 curseg->alloc_type = LFS;
1555 static void __next_free_blkoff(struct f2fs_sb_info *sbi,
1556 struct curseg_info *seg, block_t start)
1558 struct seg_entry *se = get_seg_entry(sbi, seg->segno);
1559 int entries = SIT_VBLOCK_MAP_SIZE / sizeof(unsigned long);
1560 unsigned long *target_map = SIT_I(sbi)->tmp_map;
1561 unsigned long *ckpt_map = (unsigned long *)se->ckpt_valid_map;
1562 unsigned long *cur_map = (unsigned long *)se->cur_valid_map;
1565 for (i = 0; i < entries; i++)
1566 target_map[i] = ckpt_map[i] | cur_map[i];
1568 pos = __find_rev_next_zero_bit(target_map, sbi->blocks_per_seg, start);
1570 seg->next_blkoff = pos;
1574 * If a segment is written by LFS manner, next block offset is just obtained
1575 * by increasing the current block offset. However, if a segment is written by
1576 * SSR manner, next block offset obtained by calling __next_free_blkoff
1578 static void __refresh_next_blkoff(struct f2fs_sb_info *sbi,
1579 struct curseg_info *seg)
1581 if (seg->alloc_type == SSR)
1582 __next_free_blkoff(sbi, seg, seg->next_blkoff + 1);
1588 * This function always allocates a used segment(from dirty seglist) by SSR
1589 * manner, so it should recover the existing segment information of valid blocks
1591 static void change_curseg(struct f2fs_sb_info *sbi, int type, bool reuse)
1593 struct dirty_seglist_info *dirty_i = DIRTY_I(sbi);
1594 struct curseg_info *curseg = CURSEG_I(sbi, type);
1595 unsigned int new_segno = curseg->next_segno;
1596 struct f2fs_summary_block *sum_node;
1597 struct page *sum_page;
1599 write_sum_page(sbi, curseg->sum_blk,
1600 GET_SUM_BLOCK(sbi, curseg->segno));
1601 __set_test_and_inuse(sbi, new_segno);
1603 mutex_lock(&dirty_i->seglist_lock);
1604 __remove_dirty_segment(sbi, new_segno, PRE);
1605 __remove_dirty_segment(sbi, new_segno, DIRTY);
1606 mutex_unlock(&dirty_i->seglist_lock);
1608 reset_curseg(sbi, type, 1);
1609 curseg->alloc_type = SSR;
1610 __next_free_blkoff(sbi, curseg, 0);
1613 sum_page = get_sum_page(sbi, new_segno);
1614 sum_node = (struct f2fs_summary_block *)page_address(sum_page);
1615 memcpy(curseg->sum_blk, sum_node, SUM_ENTRY_SIZE);
1616 f2fs_put_page(sum_page, 1);
1620 static int get_ssr_segment(struct f2fs_sb_info *sbi, int type)
1622 struct curseg_info *curseg = CURSEG_I(sbi, type);
1623 const struct victim_selection *v_ops = DIRTY_I(sbi)->v_ops;
1625 bool reversed = false;
1627 /* need_SSR() already forces to do this */
1628 if (v_ops->get_victim(sbi, &(curseg)->next_segno, BG_GC, type, SSR))
1631 /* For node segments, let's do SSR more intensively */
1632 if (IS_NODESEG(type)) {
1633 if (type >= CURSEG_WARM_NODE) {
1635 i = CURSEG_COLD_NODE;
1637 i = CURSEG_HOT_NODE;
1639 cnt = NR_CURSEG_NODE_TYPE;
1641 if (type >= CURSEG_WARM_DATA) {
1643 i = CURSEG_COLD_DATA;
1645 i = CURSEG_HOT_DATA;
1647 cnt = NR_CURSEG_DATA_TYPE;
1650 for (; cnt-- > 0; reversed ? i-- : i++) {
1653 if (v_ops->get_victim(sbi, &(curseg)->next_segno,
1661 * flush out current segment and replace it with new segment
1662 * This function should be returned with success, otherwise BUG
1664 static void allocate_segment_by_default(struct f2fs_sb_info *sbi,
1665 int type, bool force)
1668 new_curseg(sbi, type, true);
1669 else if (!is_set_ckpt_flags(sbi, CP_CRC_RECOVERY_FLAG) &&
1670 type == CURSEG_WARM_NODE)
1671 new_curseg(sbi, type, false);
1672 else if (need_SSR(sbi) && get_ssr_segment(sbi, type))
1673 change_curseg(sbi, type, true);
1675 new_curseg(sbi, type, false);
1677 stat_inc_seg_type(sbi, CURSEG_I(sbi, type));
1680 void allocate_new_segments(struct f2fs_sb_info *sbi)
1682 struct curseg_info *curseg;
1683 unsigned int old_segno;
1686 for (i = CURSEG_HOT_DATA; i <= CURSEG_COLD_DATA; i++) {
1687 curseg = CURSEG_I(sbi, i);
1688 old_segno = curseg->segno;
1689 SIT_I(sbi)->s_ops->allocate_segment(sbi, i, true);
1690 locate_dirty_segment(sbi, old_segno);
1694 static const struct segment_allocation default_salloc_ops = {
1695 .allocate_segment = allocate_segment_by_default,
1698 bool exist_trim_candidates(struct f2fs_sb_info *sbi, struct cp_control *cpc)
1700 __u64 trim_start = cpc->trim_start;
1701 bool has_candidate = false;
1703 mutex_lock(&SIT_I(sbi)->sentry_lock);
1704 for (; cpc->trim_start <= cpc->trim_end; cpc->trim_start++) {
1705 if (add_discard_addrs(sbi, cpc, true)) {
1706 has_candidate = true;
1710 mutex_unlock(&SIT_I(sbi)->sentry_lock);
1712 cpc->trim_start = trim_start;
1713 return has_candidate;
1716 int f2fs_trim_fs(struct f2fs_sb_info *sbi, struct fstrim_range *range)
1718 __u64 start = F2FS_BYTES_TO_BLK(range->start);
1719 __u64 end = start + F2FS_BYTES_TO_BLK(range->len) - 1;
1720 unsigned int start_segno, end_segno;
1721 struct cp_control cpc;
1724 if (start >= MAX_BLKADDR(sbi) || range->len < sbi->blocksize)
1728 if (end <= MAIN_BLKADDR(sbi))
1731 if (is_sbi_flag_set(sbi, SBI_NEED_FSCK)) {
1732 f2fs_msg(sbi->sb, KERN_WARNING,
1733 "Found FS corruption, run fsck to fix.");
1737 /* start/end segment number in main_area */
1738 start_segno = (start <= MAIN_BLKADDR(sbi)) ? 0 : GET_SEGNO(sbi, start);
1739 end_segno = (end >= MAX_BLKADDR(sbi)) ? MAIN_SEGS(sbi) - 1 :
1740 GET_SEGNO(sbi, end);
1741 cpc.reason = CP_DISCARD;
1742 cpc.trim_minlen = max_t(__u64, 1, F2FS_BYTES_TO_BLK(range->minlen));
1744 /* do checkpoint to issue discard commands safely */
1745 for (; start_segno <= end_segno; start_segno = cpc.trim_end + 1) {
1746 cpc.trim_start = start_segno;
1748 if (sbi->discard_blks == 0)
1750 else if (sbi->discard_blks < BATCHED_TRIM_BLOCKS(sbi))
1751 cpc.trim_end = end_segno;
1753 cpc.trim_end = min_t(unsigned int,
1754 rounddown(start_segno +
1755 BATCHED_TRIM_SEGMENTS(sbi),
1756 sbi->segs_per_sec) - 1, end_segno);
1758 mutex_lock(&sbi->gc_mutex);
1759 err = write_checkpoint(sbi, &cpc);
1760 mutex_unlock(&sbi->gc_mutex);
1767 range->len = F2FS_BLK_TO_BYTES(cpc.trimmed);
1771 static bool __has_curseg_space(struct f2fs_sb_info *sbi, int type)
1773 struct curseg_info *curseg = CURSEG_I(sbi, type);
1774 if (curseg->next_blkoff < sbi->blocks_per_seg)
1779 static int __get_segment_type_2(struct page *page, enum page_type p_type)
1782 return CURSEG_HOT_DATA;
1784 return CURSEG_HOT_NODE;
1787 static int __get_segment_type_4(struct page *page, enum page_type p_type)
1789 if (p_type == DATA) {
1790 struct inode *inode = page->mapping->host;
1792 if (S_ISDIR(inode->i_mode))
1793 return CURSEG_HOT_DATA;
1795 return CURSEG_COLD_DATA;
1797 if (IS_DNODE(page) && is_cold_node(page))
1798 return CURSEG_WARM_NODE;
1800 return CURSEG_COLD_NODE;
1804 static int __get_segment_type_6(struct page *page, enum page_type p_type)
1806 if (p_type == DATA) {
1807 struct inode *inode = page->mapping->host;
1809 if (S_ISDIR(inode->i_mode))
1810 return CURSEG_HOT_DATA;
1811 else if (is_cold_data(page) || file_is_cold(inode))
1812 return CURSEG_COLD_DATA;
1814 return CURSEG_WARM_DATA;
1817 return is_cold_node(page) ? CURSEG_WARM_NODE :
1820 return CURSEG_COLD_NODE;
1824 static int __get_segment_type(struct page *page, enum page_type p_type)
1826 switch (F2FS_P_SB(page)->active_logs) {
1828 return __get_segment_type_2(page, p_type);
1830 return __get_segment_type_4(page, p_type);
1832 /* NR_CURSEG_TYPE(6) logs by default */
1833 f2fs_bug_on(F2FS_P_SB(page),
1834 F2FS_P_SB(page)->active_logs != NR_CURSEG_TYPE);
1835 return __get_segment_type_6(page, p_type);
1838 void allocate_data_block(struct f2fs_sb_info *sbi, struct page *page,
1839 block_t old_blkaddr, block_t *new_blkaddr,
1840 struct f2fs_summary *sum, int type)
1842 struct sit_info *sit_i = SIT_I(sbi);
1843 struct curseg_info *curseg = CURSEG_I(sbi, type);
1845 mutex_lock(&curseg->curseg_mutex);
1846 mutex_lock(&sit_i->sentry_lock);
1848 *new_blkaddr = NEXT_FREE_BLKADDR(sbi, curseg);
1850 f2fs_wait_discard_bio(sbi, *new_blkaddr);
1853 * __add_sum_entry should be resided under the curseg_mutex
1854 * because, this function updates a summary entry in the
1855 * current summary block.
1857 __add_sum_entry(sbi, type, sum);
1859 __refresh_next_blkoff(sbi, curseg);
1861 stat_inc_block_count(sbi, curseg);
1864 * SIT information should be updated before segment allocation,
1865 * since SSR needs latest valid block information.
1867 refresh_sit_entry(sbi, old_blkaddr, *new_blkaddr);
1869 if (!__has_curseg_space(sbi, type))
1870 sit_i->s_ops->allocate_segment(sbi, type, false);
1872 mutex_unlock(&sit_i->sentry_lock);
1874 if (page && IS_NODESEG(type))
1875 fill_node_footer_blkaddr(page, NEXT_FREE_BLKADDR(sbi, curseg));
1877 mutex_unlock(&curseg->curseg_mutex);
1880 static void do_write_page(struct f2fs_summary *sum, struct f2fs_io_info *fio)
1882 int type = __get_segment_type(fio->page, fio->type);
1885 if (fio->type == NODE || fio->type == DATA)
1886 mutex_lock(&fio->sbi->wio_mutex[fio->type]);
1888 allocate_data_block(fio->sbi, fio->page, fio->old_blkaddr,
1889 &fio->new_blkaddr, sum, type);
1891 /* writeout dirty page into bdev */
1892 err = f2fs_submit_page_mbio(fio);
1893 if (err == -EAGAIN) {
1894 fio->old_blkaddr = fio->new_blkaddr;
1898 if (fio->type == NODE || fio->type == DATA)
1899 mutex_unlock(&fio->sbi->wio_mutex[fio->type]);
1902 void write_meta_page(struct f2fs_sb_info *sbi, struct page *page)
1904 struct f2fs_io_info fio = {
1908 .op_flags = REQ_SYNC | REQ_META | REQ_PRIO,
1909 .old_blkaddr = page->index,
1910 .new_blkaddr = page->index,
1912 .encrypted_page = NULL,
1915 if (unlikely(page->index >= MAIN_BLKADDR(sbi)))
1916 fio.op_flags &= ~REQ_META;
1918 set_page_writeback(page);
1919 f2fs_submit_page_mbio(&fio);
1922 void write_node_page(unsigned int nid, struct f2fs_io_info *fio)
1924 struct f2fs_summary sum;
1926 set_summary(&sum, nid, 0, 0);
1927 do_write_page(&sum, fio);
1930 void write_data_page(struct dnode_of_data *dn, struct f2fs_io_info *fio)
1932 struct f2fs_sb_info *sbi = fio->sbi;
1933 struct f2fs_summary sum;
1934 struct node_info ni;
1936 f2fs_bug_on(sbi, dn->data_blkaddr == NULL_ADDR);
1937 get_node_info(sbi, dn->nid, &ni);
1938 set_summary(&sum, dn->nid, dn->ofs_in_node, ni.version);
1939 do_write_page(&sum, fio);
1940 f2fs_update_data_blkaddr(dn, fio->new_blkaddr);
1943 void rewrite_data_page(struct f2fs_io_info *fio)
1945 fio->new_blkaddr = fio->old_blkaddr;
1946 stat_inc_inplace_blocks(fio->sbi);
1947 f2fs_submit_page_mbio(fio);
1950 void __f2fs_replace_block(struct f2fs_sb_info *sbi, struct f2fs_summary *sum,
1951 block_t old_blkaddr, block_t new_blkaddr,
1952 bool recover_curseg, bool recover_newaddr)
1954 struct sit_info *sit_i = SIT_I(sbi);
1955 struct curseg_info *curseg;
1956 unsigned int segno, old_cursegno;
1957 struct seg_entry *se;
1959 unsigned short old_blkoff;
1961 segno = GET_SEGNO(sbi, new_blkaddr);
1962 se = get_seg_entry(sbi, segno);
1965 if (!recover_curseg) {
1966 /* for recovery flow */
1967 if (se->valid_blocks == 0 && !IS_CURSEG(sbi, segno)) {
1968 if (old_blkaddr == NULL_ADDR)
1969 type = CURSEG_COLD_DATA;
1971 type = CURSEG_WARM_DATA;
1974 if (!IS_CURSEG(sbi, segno))
1975 type = CURSEG_WARM_DATA;
1978 curseg = CURSEG_I(sbi, type);
1980 mutex_lock(&curseg->curseg_mutex);
1981 mutex_lock(&sit_i->sentry_lock);
1983 old_cursegno = curseg->segno;
1984 old_blkoff = curseg->next_blkoff;
1986 /* change the current segment */
1987 if (segno != curseg->segno) {
1988 curseg->next_segno = segno;
1989 change_curseg(sbi, type, true);
1992 curseg->next_blkoff = GET_BLKOFF_FROM_SEG0(sbi, new_blkaddr);
1993 __add_sum_entry(sbi, type, sum);
1995 if (!recover_curseg || recover_newaddr)
1996 update_sit_entry(sbi, new_blkaddr, 1);
1997 if (GET_SEGNO(sbi, old_blkaddr) != NULL_SEGNO)
1998 update_sit_entry(sbi, old_blkaddr, -1);
2000 locate_dirty_segment(sbi, GET_SEGNO(sbi, old_blkaddr));
2001 locate_dirty_segment(sbi, GET_SEGNO(sbi, new_blkaddr));
2003 locate_dirty_segment(sbi, old_cursegno);
2005 if (recover_curseg) {
2006 if (old_cursegno != curseg->segno) {
2007 curseg->next_segno = old_cursegno;
2008 change_curseg(sbi, type, true);
2010 curseg->next_blkoff = old_blkoff;
2013 mutex_unlock(&sit_i->sentry_lock);
2014 mutex_unlock(&curseg->curseg_mutex);
2017 void f2fs_replace_block(struct f2fs_sb_info *sbi, struct dnode_of_data *dn,
2018 block_t old_addr, block_t new_addr,
2019 unsigned char version, bool recover_curseg,
2020 bool recover_newaddr)
2022 struct f2fs_summary sum;
2024 set_summary(&sum, dn->nid, dn->ofs_in_node, version);
2026 __f2fs_replace_block(sbi, &sum, old_addr, new_addr,
2027 recover_curseg, recover_newaddr);
2029 f2fs_update_data_blkaddr(dn, new_addr);
2032 void f2fs_wait_on_page_writeback(struct page *page,
2033 enum page_type type, bool ordered)
2035 if (PageWriteback(page)) {
2036 struct f2fs_sb_info *sbi = F2FS_P_SB(page);
2038 f2fs_submit_merged_bio_cond(sbi, page->mapping->host,
2039 0, page->index, type, WRITE);
2041 wait_on_page_writeback(page);
2043 wait_for_stable_page(page);
2047 void f2fs_wait_on_encrypted_page_writeback(struct f2fs_sb_info *sbi,
2052 if (blkaddr == NEW_ADDR || blkaddr == NULL_ADDR)
2055 cpage = find_lock_page(META_MAPPING(sbi), blkaddr);
2057 f2fs_wait_on_page_writeback(cpage, DATA, true);
2058 f2fs_put_page(cpage, 1);
2062 static int read_compacted_summaries(struct f2fs_sb_info *sbi)
2064 struct f2fs_checkpoint *ckpt = F2FS_CKPT(sbi);
2065 struct curseg_info *seg_i;
2066 unsigned char *kaddr;
2071 start = start_sum_block(sbi);
2073 page = get_meta_page(sbi, start++);
2074 kaddr = (unsigned char *)page_address(page);
2076 /* Step 1: restore nat cache */
2077 seg_i = CURSEG_I(sbi, CURSEG_HOT_DATA);
2078 memcpy(seg_i->journal, kaddr, SUM_JOURNAL_SIZE);
2080 /* Step 2: restore sit cache */
2081 seg_i = CURSEG_I(sbi, CURSEG_COLD_DATA);
2082 memcpy(seg_i->journal, kaddr + SUM_JOURNAL_SIZE, SUM_JOURNAL_SIZE);
2083 offset = 2 * SUM_JOURNAL_SIZE;
2085 /* Step 3: restore summary entries */
2086 for (i = CURSEG_HOT_DATA; i <= CURSEG_COLD_DATA; i++) {
2087 unsigned short blk_off;
2090 seg_i = CURSEG_I(sbi, i);
2091 segno = le32_to_cpu(ckpt->cur_data_segno[i]);
2092 blk_off = le16_to_cpu(ckpt->cur_data_blkoff[i]);
2093 seg_i->next_segno = segno;
2094 reset_curseg(sbi, i, 0);
2095 seg_i->alloc_type = ckpt->alloc_type[i];
2096 seg_i->next_blkoff = blk_off;
2098 if (seg_i->alloc_type == SSR)
2099 blk_off = sbi->blocks_per_seg;
2101 for (j = 0; j < blk_off; j++) {
2102 struct f2fs_summary *s;
2103 s = (struct f2fs_summary *)(kaddr + offset);
2104 seg_i->sum_blk->entries[j] = *s;
2105 offset += SUMMARY_SIZE;
2106 if (offset + SUMMARY_SIZE <= PAGE_SIZE -
2110 f2fs_put_page(page, 1);
2113 page = get_meta_page(sbi, start++);
2114 kaddr = (unsigned char *)page_address(page);
2118 f2fs_put_page(page, 1);
2122 static int read_normal_summaries(struct f2fs_sb_info *sbi, int type)
2124 struct f2fs_checkpoint *ckpt = F2FS_CKPT(sbi);
2125 struct f2fs_summary_block *sum;
2126 struct curseg_info *curseg;
2128 unsigned short blk_off;
2129 unsigned int segno = 0;
2130 block_t blk_addr = 0;
2132 /* get segment number and block addr */
2133 if (IS_DATASEG(type)) {
2134 segno = le32_to_cpu(ckpt->cur_data_segno[type]);
2135 blk_off = le16_to_cpu(ckpt->cur_data_blkoff[type -
2137 if (__exist_node_summaries(sbi))
2138 blk_addr = sum_blk_addr(sbi, NR_CURSEG_TYPE, type);
2140 blk_addr = sum_blk_addr(sbi, NR_CURSEG_DATA_TYPE, type);
2142 segno = le32_to_cpu(ckpt->cur_node_segno[type -
2144 blk_off = le16_to_cpu(ckpt->cur_node_blkoff[type -
2146 if (__exist_node_summaries(sbi))
2147 blk_addr = sum_blk_addr(sbi, NR_CURSEG_NODE_TYPE,
2148 type - CURSEG_HOT_NODE);
2150 blk_addr = GET_SUM_BLOCK(sbi, segno);
2153 new = get_meta_page(sbi, blk_addr);
2154 sum = (struct f2fs_summary_block *)page_address(new);
2156 if (IS_NODESEG(type)) {
2157 if (__exist_node_summaries(sbi)) {
2158 struct f2fs_summary *ns = &sum->entries[0];
2160 for (i = 0; i < sbi->blocks_per_seg; i++, ns++) {
2162 ns->ofs_in_node = 0;
2167 err = restore_node_summary(sbi, segno, sum);
2169 f2fs_put_page(new, 1);
2175 /* set uncompleted segment to curseg */
2176 curseg = CURSEG_I(sbi, type);
2177 mutex_lock(&curseg->curseg_mutex);
2179 /* update journal info */
2180 down_write(&curseg->journal_rwsem);
2181 memcpy(curseg->journal, &sum->journal, SUM_JOURNAL_SIZE);
2182 up_write(&curseg->journal_rwsem);
2184 memcpy(curseg->sum_blk->entries, sum->entries, SUM_ENTRY_SIZE);
2185 memcpy(&curseg->sum_blk->footer, &sum->footer, SUM_FOOTER_SIZE);
2186 curseg->next_segno = segno;
2187 reset_curseg(sbi, type, 0);
2188 curseg->alloc_type = ckpt->alloc_type[type];
2189 curseg->next_blkoff = blk_off;
2190 mutex_unlock(&curseg->curseg_mutex);
2191 f2fs_put_page(new, 1);
2195 static int restore_curseg_summaries(struct f2fs_sb_info *sbi)
2197 int type = CURSEG_HOT_DATA;
2200 if (is_set_ckpt_flags(sbi, CP_COMPACT_SUM_FLAG)) {
2201 int npages = npages_for_summary_flush(sbi, true);
2204 ra_meta_pages(sbi, start_sum_block(sbi), npages,
2207 /* restore for compacted data summary */
2208 if (read_compacted_summaries(sbi))
2210 type = CURSEG_HOT_NODE;
2213 if (__exist_node_summaries(sbi))
2214 ra_meta_pages(sbi, sum_blk_addr(sbi, NR_CURSEG_TYPE, type),
2215 NR_CURSEG_TYPE - type, META_CP, true);
2217 for (; type <= CURSEG_COLD_NODE; type++) {
2218 err = read_normal_summaries(sbi, type);
2226 static void write_compacted_summaries(struct f2fs_sb_info *sbi, block_t blkaddr)
2229 unsigned char *kaddr;
2230 struct f2fs_summary *summary;
2231 struct curseg_info *seg_i;
2232 int written_size = 0;
2235 page = grab_meta_page(sbi, blkaddr++);
2236 kaddr = (unsigned char *)page_address(page);
2238 /* Step 1: write nat cache */
2239 seg_i = CURSEG_I(sbi, CURSEG_HOT_DATA);
2240 memcpy(kaddr, seg_i->journal, SUM_JOURNAL_SIZE);
2241 written_size += SUM_JOURNAL_SIZE;
2243 /* Step 2: write sit cache */
2244 seg_i = CURSEG_I(sbi, CURSEG_COLD_DATA);
2245 memcpy(kaddr + written_size, seg_i->journal, SUM_JOURNAL_SIZE);
2246 written_size += SUM_JOURNAL_SIZE;
2248 /* Step 3: write summary entries */
2249 for (i = CURSEG_HOT_DATA; i <= CURSEG_COLD_DATA; i++) {
2250 unsigned short blkoff;
2251 seg_i = CURSEG_I(sbi, i);
2252 if (sbi->ckpt->alloc_type[i] == SSR)
2253 blkoff = sbi->blocks_per_seg;
2255 blkoff = curseg_blkoff(sbi, i);
2257 for (j = 0; j < blkoff; j++) {
2259 page = grab_meta_page(sbi, blkaddr++);
2260 kaddr = (unsigned char *)page_address(page);
2263 summary = (struct f2fs_summary *)(kaddr + written_size);
2264 *summary = seg_i->sum_blk->entries[j];
2265 written_size += SUMMARY_SIZE;
2267 if (written_size + SUMMARY_SIZE <= PAGE_SIZE -
2271 set_page_dirty(page);
2272 f2fs_put_page(page, 1);
2277 set_page_dirty(page);
2278 f2fs_put_page(page, 1);
2282 static void write_normal_summaries(struct f2fs_sb_info *sbi,
2283 block_t blkaddr, int type)
2286 if (IS_DATASEG(type))
2287 end = type + NR_CURSEG_DATA_TYPE;
2289 end = type + NR_CURSEG_NODE_TYPE;
2291 for (i = type; i < end; i++)
2292 write_current_sum_page(sbi, i, blkaddr + (i - type));
2295 void write_data_summaries(struct f2fs_sb_info *sbi, block_t start_blk)
2297 if (is_set_ckpt_flags(sbi, CP_COMPACT_SUM_FLAG))
2298 write_compacted_summaries(sbi, start_blk);
2300 write_normal_summaries(sbi, start_blk, CURSEG_HOT_DATA);
2303 void write_node_summaries(struct f2fs_sb_info *sbi, block_t start_blk)
2305 write_normal_summaries(sbi, start_blk, CURSEG_HOT_NODE);
2308 int lookup_journal_in_cursum(struct f2fs_journal *journal, int type,
2309 unsigned int val, int alloc)
2313 if (type == NAT_JOURNAL) {
2314 for (i = 0; i < nats_in_cursum(journal); i++) {
2315 if (le32_to_cpu(nid_in_journal(journal, i)) == val)
2318 if (alloc && __has_cursum_space(journal, 1, NAT_JOURNAL))
2319 return update_nats_in_cursum(journal, 1);
2320 } else if (type == SIT_JOURNAL) {
2321 for (i = 0; i < sits_in_cursum(journal); i++)
2322 if (le32_to_cpu(segno_in_journal(journal, i)) == val)
2324 if (alloc && __has_cursum_space(journal, 1, SIT_JOURNAL))
2325 return update_sits_in_cursum(journal, 1);
2330 static struct page *get_current_sit_page(struct f2fs_sb_info *sbi,
2333 return get_meta_page(sbi, current_sit_addr(sbi, segno));
2336 static struct page *get_next_sit_page(struct f2fs_sb_info *sbi,
2339 struct sit_info *sit_i = SIT_I(sbi);
2340 struct page *src_page, *dst_page;
2341 pgoff_t src_off, dst_off;
2342 void *src_addr, *dst_addr;
2344 src_off = current_sit_addr(sbi, start);
2345 dst_off = next_sit_addr(sbi, src_off);
2347 /* get current sit block page without lock */
2348 src_page = get_meta_page(sbi, src_off);
2349 dst_page = grab_meta_page(sbi, dst_off);
2350 f2fs_bug_on(sbi, PageDirty(src_page));
2352 src_addr = page_address(src_page);
2353 dst_addr = page_address(dst_page);
2354 memcpy(dst_addr, src_addr, PAGE_SIZE);
2356 set_page_dirty(dst_page);
2357 f2fs_put_page(src_page, 1);
2359 set_to_next_sit(sit_i, start);
2364 static struct sit_entry_set *grab_sit_entry_set(void)
2366 struct sit_entry_set *ses =
2367 f2fs_kmem_cache_alloc(sit_entry_set_slab, GFP_NOFS);
2370 INIT_LIST_HEAD(&ses->set_list);
2374 static void release_sit_entry_set(struct sit_entry_set *ses)
2376 list_del(&ses->set_list);
2377 kmem_cache_free(sit_entry_set_slab, ses);
2380 static void adjust_sit_entry_set(struct sit_entry_set *ses,
2381 struct list_head *head)
2383 struct sit_entry_set *next = ses;
2385 if (list_is_last(&ses->set_list, head))
2388 list_for_each_entry_continue(next, head, set_list)
2389 if (ses->entry_cnt <= next->entry_cnt)
2392 list_move_tail(&ses->set_list, &next->set_list);
2395 static void add_sit_entry(unsigned int segno, struct list_head *head)
2397 struct sit_entry_set *ses;
2398 unsigned int start_segno = START_SEGNO(segno);
2400 list_for_each_entry(ses, head, set_list) {
2401 if (ses->start_segno == start_segno) {
2403 adjust_sit_entry_set(ses, head);
2408 ses = grab_sit_entry_set();
2410 ses->start_segno = start_segno;
2412 list_add(&ses->set_list, head);
2415 static void add_sits_in_set(struct f2fs_sb_info *sbi)
2417 struct f2fs_sm_info *sm_info = SM_I(sbi);
2418 struct list_head *set_list = &sm_info->sit_entry_set;
2419 unsigned long *bitmap = SIT_I(sbi)->dirty_sentries_bitmap;
2422 for_each_set_bit(segno, bitmap, MAIN_SEGS(sbi))
2423 add_sit_entry(segno, set_list);
2426 static void remove_sits_in_journal(struct f2fs_sb_info *sbi)
2428 struct curseg_info *curseg = CURSEG_I(sbi, CURSEG_COLD_DATA);
2429 struct f2fs_journal *journal = curseg->journal;
2432 down_write(&curseg->journal_rwsem);
2433 for (i = 0; i < sits_in_cursum(journal); i++) {
2437 segno = le32_to_cpu(segno_in_journal(journal, i));
2438 dirtied = __mark_sit_entry_dirty(sbi, segno);
2441 add_sit_entry(segno, &SM_I(sbi)->sit_entry_set);
2443 update_sits_in_cursum(journal, -i);
2444 up_write(&curseg->journal_rwsem);
2448 * CP calls this function, which flushes SIT entries including sit_journal,
2449 * and moves prefree segs to free segs.
2451 void flush_sit_entries(struct f2fs_sb_info *sbi, struct cp_control *cpc)
2453 struct sit_info *sit_i = SIT_I(sbi);
2454 unsigned long *bitmap = sit_i->dirty_sentries_bitmap;
2455 struct curseg_info *curseg = CURSEG_I(sbi, CURSEG_COLD_DATA);
2456 struct f2fs_journal *journal = curseg->journal;
2457 struct sit_entry_set *ses, *tmp;
2458 struct list_head *head = &SM_I(sbi)->sit_entry_set;
2459 bool to_journal = true;
2460 struct seg_entry *se;
2462 mutex_lock(&sit_i->sentry_lock);
2464 if (!sit_i->dirty_sentries)
2468 * add and account sit entries of dirty bitmap in sit entry
2471 add_sits_in_set(sbi);
2474 * if there are no enough space in journal to store dirty sit
2475 * entries, remove all entries from journal and add and account
2476 * them in sit entry set.
2478 if (!__has_cursum_space(journal, sit_i->dirty_sentries, SIT_JOURNAL))
2479 remove_sits_in_journal(sbi);
2482 * there are two steps to flush sit entries:
2483 * #1, flush sit entries to journal in current cold data summary block.
2484 * #2, flush sit entries to sit page.
2486 list_for_each_entry_safe(ses, tmp, head, set_list) {
2487 struct page *page = NULL;
2488 struct f2fs_sit_block *raw_sit = NULL;
2489 unsigned int start_segno = ses->start_segno;
2490 unsigned int end = min(start_segno + SIT_ENTRY_PER_BLOCK,
2491 (unsigned long)MAIN_SEGS(sbi));
2492 unsigned int segno = start_segno;
2495 !__has_cursum_space(journal, ses->entry_cnt, SIT_JOURNAL))
2499 down_write(&curseg->journal_rwsem);
2501 page = get_next_sit_page(sbi, start_segno);
2502 raw_sit = page_address(page);
2505 /* flush dirty sit entries in region of current sit set */
2506 for_each_set_bit_from(segno, bitmap, end) {
2507 int offset, sit_offset;
2509 se = get_seg_entry(sbi, segno);
2511 /* add discard candidates */
2512 if (cpc->reason != CP_DISCARD) {
2513 cpc->trim_start = segno;
2514 add_discard_addrs(sbi, cpc, false);
2518 offset = lookup_journal_in_cursum(journal,
2519 SIT_JOURNAL, segno, 1);
2520 f2fs_bug_on(sbi, offset < 0);
2521 segno_in_journal(journal, offset) =
2523 seg_info_to_raw_sit(se,
2524 &sit_in_journal(journal, offset));
2526 sit_offset = SIT_ENTRY_OFFSET(sit_i, segno);
2527 seg_info_to_raw_sit(se,
2528 &raw_sit->entries[sit_offset]);
2531 __clear_bit(segno, bitmap);
2532 sit_i->dirty_sentries--;
2537 up_write(&curseg->journal_rwsem);
2539 f2fs_put_page(page, 1);
2541 f2fs_bug_on(sbi, ses->entry_cnt);
2542 release_sit_entry_set(ses);
2545 f2fs_bug_on(sbi, !list_empty(head));
2546 f2fs_bug_on(sbi, sit_i->dirty_sentries);
2548 if (cpc->reason == CP_DISCARD) {
2549 __u64 trim_start = cpc->trim_start;
2551 for (; cpc->trim_start <= cpc->trim_end; cpc->trim_start++)
2552 add_discard_addrs(sbi, cpc, false);
2554 cpc->trim_start = trim_start;
2556 mutex_unlock(&sit_i->sentry_lock);
2558 set_prefree_as_free_segments(sbi);
2561 static int build_sit_info(struct f2fs_sb_info *sbi)
2563 struct f2fs_super_block *raw_super = F2FS_RAW_SUPER(sbi);
2564 struct sit_info *sit_i;
2565 unsigned int sit_segs, start;
2567 unsigned int bitmap_size;
2569 /* allocate memory for SIT information */
2570 sit_i = kzalloc(sizeof(struct sit_info), GFP_KERNEL);
2574 SM_I(sbi)->sit_info = sit_i;
2576 sit_i->sentries = f2fs_kvzalloc(MAIN_SEGS(sbi) *
2577 sizeof(struct seg_entry), GFP_KERNEL);
2578 if (!sit_i->sentries)
2581 bitmap_size = f2fs_bitmap_size(MAIN_SEGS(sbi));
2582 sit_i->dirty_sentries_bitmap = f2fs_kvzalloc(bitmap_size, GFP_KERNEL);
2583 if (!sit_i->dirty_sentries_bitmap)
2586 for (start = 0; start < MAIN_SEGS(sbi); start++) {
2587 sit_i->sentries[start].cur_valid_map
2588 = kzalloc(SIT_VBLOCK_MAP_SIZE, GFP_KERNEL);
2589 sit_i->sentries[start].ckpt_valid_map
2590 = kzalloc(SIT_VBLOCK_MAP_SIZE, GFP_KERNEL);
2591 if (!sit_i->sentries[start].cur_valid_map ||
2592 !sit_i->sentries[start].ckpt_valid_map)
2595 #ifdef CONFIG_F2FS_CHECK_FS
2596 sit_i->sentries[start].cur_valid_map_mir
2597 = kzalloc(SIT_VBLOCK_MAP_SIZE, GFP_KERNEL);
2598 if (!sit_i->sentries[start].cur_valid_map_mir)
2602 if (f2fs_discard_en(sbi)) {
2603 sit_i->sentries[start].discard_map
2604 = kzalloc(SIT_VBLOCK_MAP_SIZE, GFP_KERNEL);
2605 if (!sit_i->sentries[start].discard_map)
2610 sit_i->tmp_map = kzalloc(SIT_VBLOCK_MAP_SIZE, GFP_KERNEL);
2611 if (!sit_i->tmp_map)
2614 if (sbi->segs_per_sec > 1) {
2615 sit_i->sec_entries = f2fs_kvzalloc(MAIN_SECS(sbi) *
2616 sizeof(struct sec_entry), GFP_KERNEL);
2617 if (!sit_i->sec_entries)
2621 /* get information related with SIT */
2622 sit_segs = le32_to_cpu(raw_super->segment_count_sit) >> 1;
2624 /* setup SIT bitmap from ckeckpoint pack */
2625 bitmap_size = __bitmap_size(sbi, SIT_BITMAP);
2626 src_bitmap = __bitmap_ptr(sbi, SIT_BITMAP);
2628 sit_i->sit_bitmap = kmemdup(src_bitmap, bitmap_size, GFP_KERNEL);
2629 if (!sit_i->sit_bitmap)
2632 #ifdef CONFIG_F2FS_CHECK_FS
2633 sit_i->sit_bitmap_mir = kmemdup(src_bitmap, bitmap_size, GFP_KERNEL);
2634 if (!sit_i->sit_bitmap_mir)
2638 /* init SIT information */
2639 sit_i->s_ops = &default_salloc_ops;
2641 sit_i->sit_base_addr = le32_to_cpu(raw_super->sit_blkaddr);
2642 sit_i->sit_blocks = sit_segs << sbi->log_blocks_per_seg;
2643 sit_i->written_valid_blocks = 0;
2644 sit_i->bitmap_size = bitmap_size;
2645 sit_i->dirty_sentries = 0;
2646 sit_i->sents_per_block = SIT_ENTRY_PER_BLOCK;
2647 sit_i->elapsed_time = le64_to_cpu(sbi->ckpt->elapsed_time);
2648 sit_i->mounted_time = CURRENT_TIME_SEC.tv_sec;
2649 mutex_init(&sit_i->sentry_lock);
2653 static int build_free_segmap(struct f2fs_sb_info *sbi)
2655 struct free_segmap_info *free_i;
2656 unsigned int bitmap_size, sec_bitmap_size;
2658 /* allocate memory for free segmap information */
2659 free_i = kzalloc(sizeof(struct free_segmap_info), GFP_KERNEL);
2663 SM_I(sbi)->free_info = free_i;
2665 bitmap_size = f2fs_bitmap_size(MAIN_SEGS(sbi));
2666 free_i->free_segmap = f2fs_kvmalloc(bitmap_size, GFP_KERNEL);
2667 if (!free_i->free_segmap)
2670 sec_bitmap_size = f2fs_bitmap_size(MAIN_SECS(sbi));
2671 free_i->free_secmap = f2fs_kvmalloc(sec_bitmap_size, GFP_KERNEL);
2672 if (!free_i->free_secmap)
2675 /* set all segments as dirty temporarily */
2676 memset(free_i->free_segmap, 0xff, bitmap_size);
2677 memset(free_i->free_secmap, 0xff, sec_bitmap_size);
2679 /* init free segmap information */
2680 free_i->start_segno = GET_SEGNO_FROM_SEG0(sbi, MAIN_BLKADDR(sbi));
2681 free_i->free_segments = 0;
2682 free_i->free_sections = 0;
2683 spin_lock_init(&free_i->segmap_lock);
2687 static int build_curseg(struct f2fs_sb_info *sbi)
2689 struct curseg_info *array;
2692 array = kcalloc(NR_CURSEG_TYPE, sizeof(*array), GFP_KERNEL);
2696 SM_I(sbi)->curseg_array = array;
2698 for (i = 0; i < NR_CURSEG_TYPE; i++) {
2699 mutex_init(&array[i].curseg_mutex);
2700 array[i].sum_blk = kzalloc(PAGE_SIZE, GFP_KERNEL);
2701 if (!array[i].sum_blk)
2703 init_rwsem(&array[i].journal_rwsem);
2704 array[i].journal = kzalloc(sizeof(struct f2fs_journal),
2706 if (!array[i].journal)
2708 array[i].segno = NULL_SEGNO;
2709 array[i].next_blkoff = 0;
2711 return restore_curseg_summaries(sbi);
2714 static void build_sit_entries(struct f2fs_sb_info *sbi)
2716 struct sit_info *sit_i = SIT_I(sbi);
2717 struct curseg_info *curseg = CURSEG_I(sbi, CURSEG_COLD_DATA);
2718 struct f2fs_journal *journal = curseg->journal;
2719 struct seg_entry *se;
2720 struct f2fs_sit_entry sit;
2721 int sit_blk_cnt = SIT_BLK_CNT(sbi);
2722 unsigned int i, start, end;
2723 unsigned int readed, start_blk = 0;
2726 readed = ra_meta_pages(sbi, start_blk, BIO_MAX_PAGES,
2729 start = start_blk * sit_i->sents_per_block;
2730 end = (start_blk + readed) * sit_i->sents_per_block;
2732 for (; start < end && start < MAIN_SEGS(sbi); start++) {
2733 struct f2fs_sit_block *sit_blk;
2736 se = &sit_i->sentries[start];
2737 page = get_current_sit_page(sbi, start);
2738 sit_blk = (struct f2fs_sit_block *)page_address(page);
2739 sit = sit_blk->entries[SIT_ENTRY_OFFSET(sit_i, start)];
2740 f2fs_put_page(page, 1);
2742 check_block_count(sbi, start, &sit);
2743 seg_info_from_raw_sit(se, &sit);
2745 /* build discard map only one time */
2746 if (f2fs_discard_en(sbi)) {
2747 memcpy(se->discard_map, se->cur_valid_map,
2748 SIT_VBLOCK_MAP_SIZE);
2749 sbi->discard_blks += sbi->blocks_per_seg -
2753 if (sbi->segs_per_sec > 1)
2754 get_sec_entry(sbi, start)->valid_blocks +=
2757 start_blk += readed;
2758 } while (start_blk < sit_blk_cnt);
2760 down_read(&curseg->journal_rwsem);
2761 for (i = 0; i < sits_in_cursum(journal); i++) {
2762 unsigned int old_valid_blocks;
2764 start = le32_to_cpu(segno_in_journal(journal, i));
2765 se = &sit_i->sentries[start];
2766 sit = sit_in_journal(journal, i);
2768 old_valid_blocks = se->valid_blocks;
2770 check_block_count(sbi, start, &sit);
2771 seg_info_from_raw_sit(se, &sit);
2773 if (f2fs_discard_en(sbi)) {
2774 memcpy(se->discard_map, se->cur_valid_map,
2775 SIT_VBLOCK_MAP_SIZE);
2776 sbi->discard_blks += old_valid_blocks -
2780 if (sbi->segs_per_sec > 1)
2781 get_sec_entry(sbi, start)->valid_blocks +=
2782 se->valid_blocks - old_valid_blocks;
2784 up_read(&curseg->journal_rwsem);
2787 static void init_free_segmap(struct f2fs_sb_info *sbi)
2792 for (start = 0; start < MAIN_SEGS(sbi); start++) {
2793 struct seg_entry *sentry = get_seg_entry(sbi, start);
2794 if (!sentry->valid_blocks)
2795 __set_free(sbi, start);
2797 SIT_I(sbi)->written_valid_blocks +=
2798 sentry->valid_blocks;
2801 /* set use the current segments */
2802 for (type = CURSEG_HOT_DATA; type <= CURSEG_COLD_NODE; type++) {
2803 struct curseg_info *curseg_t = CURSEG_I(sbi, type);
2804 __set_test_and_inuse(sbi, curseg_t->segno);
2808 static void init_dirty_segmap(struct f2fs_sb_info *sbi)
2810 struct dirty_seglist_info *dirty_i = DIRTY_I(sbi);
2811 struct free_segmap_info *free_i = FREE_I(sbi);
2812 unsigned int segno = 0, offset = 0;
2813 unsigned short valid_blocks;
2816 /* find dirty segment based on free segmap */
2817 segno = find_next_inuse(free_i, MAIN_SEGS(sbi), offset);
2818 if (segno >= MAIN_SEGS(sbi))
2821 valid_blocks = get_valid_blocks(sbi, segno, 0);
2822 if (valid_blocks == sbi->blocks_per_seg || !valid_blocks)
2824 if (valid_blocks > sbi->blocks_per_seg) {
2825 f2fs_bug_on(sbi, 1);
2828 mutex_lock(&dirty_i->seglist_lock);
2829 __locate_dirty_segment(sbi, segno, DIRTY);
2830 mutex_unlock(&dirty_i->seglist_lock);
2834 static int init_victim_secmap(struct f2fs_sb_info *sbi)
2836 struct dirty_seglist_info *dirty_i = DIRTY_I(sbi);
2837 unsigned int bitmap_size = f2fs_bitmap_size(MAIN_SECS(sbi));
2839 dirty_i->victim_secmap = f2fs_kvzalloc(bitmap_size, GFP_KERNEL);
2840 if (!dirty_i->victim_secmap)
2845 static int build_dirty_segmap(struct f2fs_sb_info *sbi)
2847 struct dirty_seglist_info *dirty_i;
2848 unsigned int bitmap_size, i;
2850 /* allocate memory for dirty segments list information */
2851 dirty_i = kzalloc(sizeof(struct dirty_seglist_info), GFP_KERNEL);
2855 SM_I(sbi)->dirty_info = dirty_i;
2856 mutex_init(&dirty_i->seglist_lock);
2858 bitmap_size = f2fs_bitmap_size(MAIN_SEGS(sbi));
2860 for (i = 0; i < NR_DIRTY_TYPE; i++) {
2861 dirty_i->dirty_segmap[i] = f2fs_kvzalloc(bitmap_size, GFP_KERNEL);
2862 if (!dirty_i->dirty_segmap[i])
2866 init_dirty_segmap(sbi);
2867 return init_victim_secmap(sbi);
2871 * Update min, max modified time for cost-benefit GC algorithm
2873 static void init_min_max_mtime(struct f2fs_sb_info *sbi)
2875 struct sit_info *sit_i = SIT_I(sbi);
2878 mutex_lock(&sit_i->sentry_lock);
2880 sit_i->min_mtime = LLONG_MAX;
2882 for (segno = 0; segno < MAIN_SEGS(sbi); segno += sbi->segs_per_sec) {
2884 unsigned long long mtime = 0;
2886 for (i = 0; i < sbi->segs_per_sec; i++)
2887 mtime += get_seg_entry(sbi, segno + i)->mtime;
2889 mtime = div_u64(mtime, sbi->segs_per_sec);
2891 if (sit_i->min_mtime > mtime)
2892 sit_i->min_mtime = mtime;
2894 sit_i->max_mtime = get_mtime(sbi);
2895 mutex_unlock(&sit_i->sentry_lock);
2898 int build_segment_manager(struct f2fs_sb_info *sbi)
2900 struct f2fs_super_block *raw_super = F2FS_RAW_SUPER(sbi);
2901 struct f2fs_checkpoint *ckpt = F2FS_CKPT(sbi);
2902 struct f2fs_sm_info *sm_info;
2905 sm_info = kzalloc(sizeof(struct f2fs_sm_info), GFP_KERNEL);
2910 sbi->sm_info = sm_info;
2911 sm_info->seg0_blkaddr = le32_to_cpu(raw_super->segment0_blkaddr);
2912 sm_info->main_blkaddr = le32_to_cpu(raw_super->main_blkaddr);
2913 sm_info->segment_count = le32_to_cpu(raw_super->segment_count);
2914 sm_info->reserved_segments = le32_to_cpu(ckpt->rsvd_segment_count);
2915 sm_info->ovp_segments = le32_to_cpu(ckpt->overprov_segment_count);
2916 sm_info->main_segments = le32_to_cpu(raw_super->segment_count_main);
2917 sm_info->ssa_blkaddr = le32_to_cpu(raw_super->ssa_blkaddr);
2918 sm_info->rec_prefree_segments = sm_info->main_segments *
2919 DEF_RECLAIM_PREFREE_SEGMENTS / 100;
2920 if (sm_info->rec_prefree_segments > DEF_MAX_RECLAIM_PREFREE_SEGMENTS)
2921 sm_info->rec_prefree_segments = DEF_MAX_RECLAIM_PREFREE_SEGMENTS;
2923 if (!test_opt(sbi, LFS))
2924 sm_info->ipu_policy = 1 << F2FS_IPU_FSYNC;
2925 sm_info->min_ipu_util = DEF_MIN_IPU_UTIL;
2926 sm_info->min_fsync_blocks = DEF_MIN_FSYNC_BLOCKS;
2928 sm_info->trim_sections = DEF_BATCHED_TRIM_SECTIONS;
2930 INIT_LIST_HEAD(&sm_info->sit_entry_set);
2932 if (test_opt(sbi, FLUSH_MERGE) && !f2fs_readonly(sbi->sb)) {
2933 err = create_flush_cmd_control(sbi);
2938 err = create_discard_cmd_control(sbi);
2942 err = build_sit_info(sbi);
2945 err = build_free_segmap(sbi);
2948 err = build_curseg(sbi);
2952 /* reinit free segmap based on SIT */
2953 build_sit_entries(sbi);
2955 init_free_segmap(sbi);
2956 err = build_dirty_segmap(sbi);
2960 init_min_max_mtime(sbi);
2964 static void discard_dirty_segmap(struct f2fs_sb_info *sbi,
2965 enum dirty_type dirty_type)
2967 struct dirty_seglist_info *dirty_i = DIRTY_I(sbi);
2969 mutex_lock(&dirty_i->seglist_lock);
2970 kvfree(dirty_i->dirty_segmap[dirty_type]);
2971 dirty_i->nr_dirty[dirty_type] = 0;
2972 mutex_unlock(&dirty_i->seglist_lock);
2975 static void destroy_victim_secmap(struct f2fs_sb_info *sbi)
2977 struct dirty_seglist_info *dirty_i = DIRTY_I(sbi);
2978 kvfree(dirty_i->victim_secmap);
2981 static void destroy_dirty_segmap(struct f2fs_sb_info *sbi)
2983 struct dirty_seglist_info *dirty_i = DIRTY_I(sbi);
2989 /* discard pre-free/dirty segments list */
2990 for (i = 0; i < NR_DIRTY_TYPE; i++)
2991 discard_dirty_segmap(sbi, i);
2993 destroy_victim_secmap(sbi);
2994 SM_I(sbi)->dirty_info = NULL;
2998 static void destroy_curseg(struct f2fs_sb_info *sbi)
3000 struct curseg_info *array = SM_I(sbi)->curseg_array;
3005 SM_I(sbi)->curseg_array = NULL;
3006 for (i = 0; i < NR_CURSEG_TYPE; i++) {
3007 kfree(array[i].sum_blk);
3008 kfree(array[i].journal);
3013 static void destroy_free_segmap(struct f2fs_sb_info *sbi)
3015 struct free_segmap_info *free_i = SM_I(sbi)->free_info;
3018 SM_I(sbi)->free_info = NULL;
3019 kvfree(free_i->free_segmap);
3020 kvfree(free_i->free_secmap);
3024 static void destroy_sit_info(struct f2fs_sb_info *sbi)
3026 struct sit_info *sit_i = SIT_I(sbi);
3032 if (sit_i->sentries) {
3033 for (start = 0; start < MAIN_SEGS(sbi); start++) {
3034 kfree(sit_i->sentries[start].cur_valid_map);
3035 #ifdef CONFIG_F2FS_CHECK_FS
3036 kfree(sit_i->sentries[start].cur_valid_map_mir);
3038 kfree(sit_i->sentries[start].ckpt_valid_map);
3039 kfree(sit_i->sentries[start].discard_map);
3042 kfree(sit_i->tmp_map);
3044 kvfree(sit_i->sentries);
3045 kvfree(sit_i->sec_entries);
3046 kvfree(sit_i->dirty_sentries_bitmap);
3048 SM_I(sbi)->sit_info = NULL;
3049 kfree(sit_i->sit_bitmap);
3050 #ifdef CONFIG_F2FS_CHECK_FS
3051 kfree(sit_i->sit_bitmap_mir);
3056 void destroy_segment_manager(struct f2fs_sb_info *sbi)
3058 struct f2fs_sm_info *sm_info = SM_I(sbi);
3062 destroy_flush_cmd_control(sbi, true);
3063 destroy_discard_cmd_control(sbi, true);
3064 destroy_dirty_segmap(sbi);
3065 destroy_curseg(sbi);
3066 destroy_free_segmap(sbi);
3067 destroy_sit_info(sbi);
3068 sbi->sm_info = NULL;
3072 int __init create_segment_manager_caches(void)
3074 discard_entry_slab = f2fs_kmem_cache_create("discard_entry",
3075 sizeof(struct discard_entry));
3076 if (!discard_entry_slab)
3079 discard_cmd_slab = f2fs_kmem_cache_create("discard_cmd",
3080 sizeof(struct discard_cmd));
3081 if (!discard_cmd_slab)
3082 goto destroy_discard_entry;
3084 sit_entry_set_slab = f2fs_kmem_cache_create("sit_entry_set",
3085 sizeof(struct sit_entry_set));
3086 if (!sit_entry_set_slab)
3087 goto destroy_discard_cmd;
3089 inmem_entry_slab = f2fs_kmem_cache_create("inmem_page_entry",
3090 sizeof(struct inmem_pages));
3091 if (!inmem_entry_slab)
3092 goto destroy_sit_entry_set;
3095 destroy_sit_entry_set:
3096 kmem_cache_destroy(sit_entry_set_slab);
3097 destroy_discard_cmd:
3098 kmem_cache_destroy(discard_cmd_slab);
3099 destroy_discard_entry:
3100 kmem_cache_destroy(discard_entry_slab);
3105 void destroy_segment_manager_caches(void)
3107 kmem_cache_destroy(sit_entry_set_slab);
3108 kmem_cache_destroy(discard_cmd_slab);
3109 kmem_cache_destroy(discard_entry_slab);
3110 kmem_cache_destroy(inmem_entry_slab);