1 // SPDX-License-Identifier: GPL-2.0
5 * Copyright (c) 2012 Samsung Electronics Co., Ltd.
6 * http://www.samsung.com/
9 #include <linux/f2fs_fs.h>
10 #include <linux/bio.h>
11 #include <linux/blkdev.h>
12 #include <linux/sched/mm.h>
13 #include <linux/prefetch.h>
14 #include <linux/kthread.h>
15 #include <linux/swap.h>
16 #include <linux/timer.h>
17 #include <linux/freezer.h>
18 #include <linux/sched/signal.h>
19 #include <linux/random.h>
26 #include <trace/events/f2fs.h>
28 #define __reverse_ffz(x) __reverse_ffs(~(x))
30 static struct kmem_cache *discard_entry_slab;
31 static struct kmem_cache *discard_cmd_slab;
32 static struct kmem_cache *sit_entry_set_slab;
33 static struct kmem_cache *inmem_entry_slab;
35 static unsigned long __reverse_ulong(unsigned char *str)
37 unsigned long tmp = 0;
38 int shift = 24, idx = 0;
40 #if BITS_PER_LONG == 64
44 tmp |= (unsigned long)str[idx++] << shift;
45 shift -= BITS_PER_BYTE;
51 * __reverse_ffs is copied from include/asm-generic/bitops/__ffs.h since
52 * MSB and LSB are reversed in a byte by f2fs_set_bit.
54 static inline unsigned long __reverse_ffs(unsigned long word)
58 #if BITS_PER_LONG == 64
59 if ((word & 0xffffffff00000000UL) == 0)
64 if ((word & 0xffff0000) == 0)
69 if ((word & 0xff00) == 0)
74 if ((word & 0xf0) == 0)
79 if ((word & 0xc) == 0)
84 if ((word & 0x2) == 0)
90 * __find_rev_next(_zero)_bit is copied from lib/find_next_bit.c because
91 * f2fs_set_bit makes MSB and LSB reversed in a byte.
92 * @size must be integral times of unsigned long.
95 * f2fs_set_bit(0, bitmap) => 1000 0000
96 * f2fs_set_bit(7, bitmap) => 0000 0001
98 static unsigned long __find_rev_next_bit(const unsigned long *addr,
99 unsigned long size, unsigned long offset)
101 const unsigned long *p = addr + BIT_WORD(offset);
102 unsigned long result = size;
108 size -= (offset & ~(BITS_PER_LONG - 1));
109 offset %= BITS_PER_LONG;
115 tmp = __reverse_ulong((unsigned char *)p);
117 tmp &= ~0UL >> offset;
118 if (size < BITS_PER_LONG)
119 tmp &= (~0UL << (BITS_PER_LONG - size));
123 if (size <= BITS_PER_LONG)
125 size -= BITS_PER_LONG;
131 return result - size + __reverse_ffs(tmp);
134 static unsigned long __find_rev_next_zero_bit(const unsigned long *addr,
135 unsigned long size, unsigned long offset)
137 const unsigned long *p = addr + BIT_WORD(offset);
138 unsigned long result = size;
144 size -= (offset & ~(BITS_PER_LONG - 1));
145 offset %= BITS_PER_LONG;
151 tmp = __reverse_ulong((unsigned char *)p);
154 tmp |= ~0UL << (BITS_PER_LONG - offset);
155 if (size < BITS_PER_LONG)
160 if (size <= BITS_PER_LONG)
162 size -= BITS_PER_LONG;
168 return result - size + __reverse_ffz(tmp);
171 bool f2fs_need_SSR(struct f2fs_sb_info *sbi)
173 int node_secs = get_blocktype_secs(sbi, F2FS_DIRTY_NODES);
174 int dent_secs = get_blocktype_secs(sbi, F2FS_DIRTY_DENTS);
175 int imeta_secs = get_blocktype_secs(sbi, F2FS_DIRTY_IMETA);
177 if (f2fs_lfs_mode(sbi))
179 if (sbi->gc_mode == GC_URGENT_HIGH)
181 if (unlikely(is_sbi_flag_set(sbi, SBI_CP_DISABLED)))
184 return free_sections(sbi) <= (node_secs + 2 * dent_secs + imeta_secs +
185 SM_I(sbi)->min_ssr_sections + reserved_sections(sbi));
188 void f2fs_register_inmem_page(struct inode *inode, struct page *page)
190 struct inmem_pages *new;
192 set_page_private_atomic(page);
194 new = f2fs_kmem_cache_alloc(inmem_entry_slab,
195 GFP_NOFS, true, NULL);
197 /* add atomic page indices to the list */
199 INIT_LIST_HEAD(&new->list);
201 /* increase reference count with clean state */
203 mutex_lock(&F2FS_I(inode)->inmem_lock);
204 list_add_tail(&new->list, &F2FS_I(inode)->inmem_pages);
205 inc_page_count(F2FS_I_SB(inode), F2FS_INMEM_PAGES);
206 mutex_unlock(&F2FS_I(inode)->inmem_lock);
208 trace_f2fs_register_inmem_page(page, INMEM);
211 static int __revoke_inmem_pages(struct inode *inode,
212 struct list_head *head, bool drop, bool recover,
215 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
216 struct inmem_pages *cur, *tmp;
219 list_for_each_entry_safe(cur, tmp, head, list) {
220 struct page *page = cur->page;
223 trace_f2fs_commit_inmem_page(page, INMEM_DROP);
227 * to avoid deadlock in between page lock and
230 if (!trylock_page(page))
236 f2fs_wait_on_page_writeback(page, DATA, true, true);
239 struct dnode_of_data dn;
242 trace_f2fs_commit_inmem_page(page, INMEM_REVOKE);
244 set_new_dnode(&dn, inode, NULL, NULL, 0);
245 err = f2fs_get_dnode_of_data(&dn, page->index,
248 if (err == -ENOMEM) {
249 memalloc_retry_wait(GFP_NOFS);
256 err = f2fs_get_node_info(sbi, dn.nid, &ni, false);
262 if (cur->old_addr == NEW_ADDR) {
263 f2fs_invalidate_blocks(sbi, dn.data_blkaddr);
264 f2fs_update_data_blkaddr(&dn, NEW_ADDR);
266 f2fs_replace_block(sbi, &dn, dn.data_blkaddr,
267 cur->old_addr, ni.version, true, true);
271 /* we don't need to invalidate this in the sccessful status */
272 if (drop || recover) {
273 ClearPageUptodate(page);
274 clear_page_private_gcing(page);
276 detach_page_private(page);
277 set_page_private(page, 0);
278 f2fs_put_page(page, 1);
280 list_del(&cur->list);
281 kmem_cache_free(inmem_entry_slab, cur);
282 dec_page_count(F2FS_I_SB(inode), F2FS_INMEM_PAGES);
287 void f2fs_drop_inmem_pages_all(struct f2fs_sb_info *sbi, bool gc_failure)
289 struct list_head *head = &sbi->inode_list[ATOMIC_FILE];
291 struct f2fs_inode_info *fi;
292 unsigned int count = sbi->atomic_files;
293 unsigned int looped = 0;
295 spin_lock(&sbi->inode_lock[ATOMIC_FILE]);
296 if (list_empty(head)) {
297 spin_unlock(&sbi->inode_lock[ATOMIC_FILE]);
300 fi = list_first_entry(head, struct f2fs_inode_info, inmem_ilist);
301 inode = igrab(&fi->vfs_inode);
303 list_move_tail(&fi->inmem_ilist, head);
304 spin_unlock(&sbi->inode_lock[ATOMIC_FILE]);
308 if (!fi->i_gc_failures[GC_FAILURE_ATOMIC])
311 set_inode_flag(inode, FI_ATOMIC_REVOKE_REQUEST);
312 f2fs_drop_inmem_pages(inode);
316 f2fs_io_schedule_timeout(DEFAULT_IO_TIMEOUT);
318 if (++looped >= count)
324 void f2fs_drop_inmem_pages(struct inode *inode)
326 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
327 struct f2fs_inode_info *fi = F2FS_I(inode);
330 mutex_lock(&fi->inmem_lock);
331 if (list_empty(&fi->inmem_pages)) {
332 fi->i_gc_failures[GC_FAILURE_ATOMIC] = 0;
334 spin_lock(&sbi->inode_lock[ATOMIC_FILE]);
335 if (!list_empty(&fi->inmem_ilist))
336 list_del_init(&fi->inmem_ilist);
337 if (f2fs_is_atomic_file(inode)) {
338 clear_inode_flag(inode, FI_ATOMIC_FILE);
341 spin_unlock(&sbi->inode_lock[ATOMIC_FILE]);
343 mutex_unlock(&fi->inmem_lock);
346 __revoke_inmem_pages(inode, &fi->inmem_pages,
348 mutex_unlock(&fi->inmem_lock);
352 void f2fs_drop_inmem_page(struct inode *inode, struct page *page)
354 struct f2fs_inode_info *fi = F2FS_I(inode);
355 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
356 struct list_head *head = &fi->inmem_pages;
357 struct inmem_pages *cur = NULL;
359 f2fs_bug_on(sbi, !page_private_atomic(page));
361 mutex_lock(&fi->inmem_lock);
362 list_for_each_entry(cur, head, list) {
363 if (cur->page == page)
367 f2fs_bug_on(sbi, list_empty(head) || cur->page != page);
368 list_del(&cur->list);
369 mutex_unlock(&fi->inmem_lock);
371 dec_page_count(sbi, F2FS_INMEM_PAGES);
372 kmem_cache_free(inmem_entry_slab, cur);
374 ClearPageUptodate(page);
375 clear_page_private_atomic(page);
376 f2fs_put_page(page, 0);
378 detach_page_private(page);
379 set_page_private(page, 0);
381 trace_f2fs_commit_inmem_page(page, INMEM_INVALIDATE);
384 static int __f2fs_commit_inmem_pages(struct inode *inode)
386 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
387 struct f2fs_inode_info *fi = F2FS_I(inode);
388 struct inmem_pages *cur, *tmp;
389 struct f2fs_io_info fio = {
394 .op_flags = REQ_SYNC | REQ_PRIO,
395 .io_type = FS_DATA_IO,
397 struct list_head revoke_list;
398 bool submit_bio = false;
401 INIT_LIST_HEAD(&revoke_list);
403 list_for_each_entry_safe(cur, tmp, &fi->inmem_pages, list) {
404 struct page *page = cur->page;
407 if (page->mapping == inode->i_mapping) {
408 trace_f2fs_commit_inmem_page(page, INMEM);
410 f2fs_wait_on_page_writeback(page, DATA, true, true);
412 set_page_dirty(page);
413 if (clear_page_dirty_for_io(page)) {
414 inode_dec_dirty_pages(inode);
415 f2fs_remove_dirty_inode(inode);
419 fio.old_blkaddr = NULL_ADDR;
420 fio.encrypted_page = NULL;
421 fio.need_lock = LOCK_DONE;
422 err = f2fs_do_write_data_page(&fio);
424 if (err == -ENOMEM) {
425 memalloc_retry_wait(GFP_NOFS);
431 /* record old blkaddr for revoking */
432 cur->old_addr = fio.old_blkaddr;
436 list_move_tail(&cur->list, &revoke_list);
440 f2fs_submit_merged_write_cond(sbi, inode, NULL, 0, DATA);
444 * try to revoke all committed pages, but still we could fail
445 * due to no memory or other reason, if that happened, EAGAIN
446 * will be returned, which means in such case, transaction is
447 * already not integrity, caller should use journal to do the
448 * recovery or rewrite & commit last transaction. For other
449 * error number, revoking was done by filesystem itself.
451 err = __revoke_inmem_pages(inode, &revoke_list,
454 /* drop all uncommitted pages */
455 __revoke_inmem_pages(inode, &fi->inmem_pages,
458 __revoke_inmem_pages(inode, &revoke_list,
459 false, false, false);
465 int f2fs_commit_inmem_pages(struct inode *inode)
467 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
468 struct f2fs_inode_info *fi = F2FS_I(inode);
471 f2fs_balance_fs(sbi, true);
473 f2fs_down_write(&fi->i_gc_rwsem[WRITE]);
476 set_inode_flag(inode, FI_ATOMIC_COMMIT);
478 mutex_lock(&fi->inmem_lock);
479 err = __f2fs_commit_inmem_pages(inode);
480 mutex_unlock(&fi->inmem_lock);
482 clear_inode_flag(inode, FI_ATOMIC_COMMIT);
485 f2fs_up_write(&fi->i_gc_rwsem[WRITE]);
491 * This function balances dirty node and dentry pages.
492 * In addition, it controls garbage collection.
494 void f2fs_balance_fs(struct f2fs_sb_info *sbi, bool need)
496 if (time_to_inject(sbi, FAULT_CHECKPOINT)) {
497 f2fs_show_injection_info(sbi, FAULT_CHECKPOINT);
498 f2fs_stop_checkpoint(sbi, false);
501 /* balance_fs_bg is able to be pending */
502 if (need && excess_cached_nats(sbi))
503 f2fs_balance_fs_bg(sbi, false);
505 if (!f2fs_is_checkpoint_ready(sbi))
509 * We should do GC or end up with checkpoint, if there are so many dirty
510 * dir/node pages without enough free segments.
512 if (has_not_enough_free_secs(sbi, 0, 0)) {
513 if (test_opt(sbi, GC_MERGE) && sbi->gc_thread &&
514 sbi->gc_thread->f2fs_gc_task) {
517 prepare_to_wait(&sbi->gc_thread->fggc_wq, &wait,
518 TASK_UNINTERRUPTIBLE);
519 wake_up(&sbi->gc_thread->gc_wait_queue_head);
521 finish_wait(&sbi->gc_thread->fggc_wq, &wait);
523 f2fs_down_write(&sbi->gc_lock);
524 f2fs_gc(sbi, false, false, false, NULL_SEGNO);
529 static inline bool excess_dirty_threshold(struct f2fs_sb_info *sbi)
531 int factor = f2fs_rwsem_is_locked(&sbi->cp_rwsem) ? 3 : 2;
532 unsigned int dents = get_pages(sbi, F2FS_DIRTY_DENTS);
533 unsigned int qdata = get_pages(sbi, F2FS_DIRTY_QDATA);
534 unsigned int nodes = get_pages(sbi, F2FS_DIRTY_NODES);
535 unsigned int meta = get_pages(sbi, F2FS_DIRTY_META);
536 unsigned int imeta = get_pages(sbi, F2FS_DIRTY_IMETA);
537 unsigned int threshold = sbi->blocks_per_seg * factor *
538 DEFAULT_DIRTY_THRESHOLD;
539 unsigned int global_threshold = threshold * 3 / 2;
541 if (dents >= threshold || qdata >= threshold ||
542 nodes >= threshold || meta >= threshold ||
545 return dents + qdata + nodes + meta + imeta > global_threshold;
548 void f2fs_balance_fs_bg(struct f2fs_sb_info *sbi, bool from_bg)
550 if (unlikely(is_sbi_flag_set(sbi, SBI_POR_DOING)))
553 /* try to shrink extent cache when there is no enough memory */
554 if (!f2fs_available_free_memory(sbi, EXTENT_CACHE))
555 f2fs_shrink_extent_tree(sbi, EXTENT_CACHE_SHRINK_NUMBER);
557 /* check the # of cached NAT entries */
558 if (!f2fs_available_free_memory(sbi, NAT_ENTRIES))
559 f2fs_try_to_free_nats(sbi, NAT_ENTRY_PER_BLOCK);
561 if (!f2fs_available_free_memory(sbi, FREE_NIDS))
562 f2fs_try_to_free_nids(sbi, MAX_FREE_NIDS);
564 f2fs_build_free_nids(sbi, false, false);
566 if (excess_dirty_nats(sbi) || excess_dirty_threshold(sbi) ||
567 excess_prefree_segs(sbi) || !f2fs_space_for_roll_forward(sbi))
570 /* there is background inflight IO or foreground operation recently */
571 if (is_inflight_io(sbi, REQ_TIME) ||
572 (!f2fs_time_over(sbi, REQ_TIME) && f2fs_rwsem_is_locked(&sbi->cp_rwsem)))
575 /* exceed periodical checkpoint timeout threshold */
576 if (f2fs_time_over(sbi, CP_TIME))
579 /* checkpoint is the only way to shrink partial cached entries */
580 if (f2fs_available_free_memory(sbi, NAT_ENTRIES) &&
581 f2fs_available_free_memory(sbi, INO_ENTRIES))
585 if (test_opt(sbi, DATA_FLUSH) && from_bg) {
586 struct blk_plug plug;
588 mutex_lock(&sbi->flush_lock);
590 blk_start_plug(&plug);
591 f2fs_sync_dirty_inodes(sbi, FILE_INODE);
592 blk_finish_plug(&plug);
594 mutex_unlock(&sbi->flush_lock);
596 f2fs_sync_fs(sbi->sb, true);
597 stat_inc_bg_cp_count(sbi->stat_info);
600 static int __submit_flush_wait(struct f2fs_sb_info *sbi,
601 struct block_device *bdev)
603 int ret = blkdev_issue_flush(bdev);
605 trace_f2fs_issue_flush(bdev, test_opt(sbi, NOBARRIER),
606 test_opt(sbi, FLUSH_MERGE), ret);
610 static int submit_flush_wait(struct f2fs_sb_info *sbi, nid_t ino)
615 if (!f2fs_is_multi_device(sbi))
616 return __submit_flush_wait(sbi, sbi->sb->s_bdev);
618 for (i = 0; i < sbi->s_ndevs; i++) {
619 if (!f2fs_is_dirty_device(sbi, ino, i, FLUSH_INO))
621 ret = __submit_flush_wait(sbi, FDEV(i).bdev);
628 static int issue_flush_thread(void *data)
630 struct f2fs_sb_info *sbi = data;
631 struct flush_cmd_control *fcc = SM_I(sbi)->fcc_info;
632 wait_queue_head_t *q = &fcc->flush_wait_queue;
634 if (kthread_should_stop())
637 if (!llist_empty(&fcc->issue_list)) {
638 struct flush_cmd *cmd, *next;
641 fcc->dispatch_list = llist_del_all(&fcc->issue_list);
642 fcc->dispatch_list = llist_reverse_order(fcc->dispatch_list);
644 cmd = llist_entry(fcc->dispatch_list, struct flush_cmd, llnode);
646 ret = submit_flush_wait(sbi, cmd->ino);
647 atomic_inc(&fcc->issued_flush);
649 llist_for_each_entry_safe(cmd, next,
650 fcc->dispatch_list, llnode) {
652 complete(&cmd->wait);
654 fcc->dispatch_list = NULL;
657 wait_event_interruptible(*q,
658 kthread_should_stop() || !llist_empty(&fcc->issue_list));
662 int f2fs_issue_flush(struct f2fs_sb_info *sbi, nid_t ino)
664 struct flush_cmd_control *fcc = SM_I(sbi)->fcc_info;
665 struct flush_cmd cmd;
668 if (test_opt(sbi, NOBARRIER))
671 if (!test_opt(sbi, FLUSH_MERGE)) {
672 atomic_inc(&fcc->queued_flush);
673 ret = submit_flush_wait(sbi, ino);
674 atomic_dec(&fcc->queued_flush);
675 atomic_inc(&fcc->issued_flush);
679 if (atomic_inc_return(&fcc->queued_flush) == 1 ||
680 f2fs_is_multi_device(sbi)) {
681 ret = submit_flush_wait(sbi, ino);
682 atomic_dec(&fcc->queued_flush);
684 atomic_inc(&fcc->issued_flush);
689 init_completion(&cmd.wait);
691 llist_add(&cmd.llnode, &fcc->issue_list);
694 * update issue_list before we wake up issue_flush thread, this
695 * smp_mb() pairs with another barrier in ___wait_event(), see
696 * more details in comments of waitqueue_active().
700 if (waitqueue_active(&fcc->flush_wait_queue))
701 wake_up(&fcc->flush_wait_queue);
703 if (fcc->f2fs_issue_flush) {
704 wait_for_completion(&cmd.wait);
705 atomic_dec(&fcc->queued_flush);
707 struct llist_node *list;
709 list = llist_del_all(&fcc->issue_list);
711 wait_for_completion(&cmd.wait);
712 atomic_dec(&fcc->queued_flush);
714 struct flush_cmd *tmp, *next;
716 ret = submit_flush_wait(sbi, ino);
718 llist_for_each_entry_safe(tmp, next, list, llnode) {
721 atomic_dec(&fcc->queued_flush);
725 complete(&tmp->wait);
733 int f2fs_create_flush_cmd_control(struct f2fs_sb_info *sbi)
735 dev_t dev = sbi->sb->s_bdev->bd_dev;
736 struct flush_cmd_control *fcc;
739 if (SM_I(sbi)->fcc_info) {
740 fcc = SM_I(sbi)->fcc_info;
741 if (fcc->f2fs_issue_flush)
746 fcc = f2fs_kzalloc(sbi, sizeof(struct flush_cmd_control), GFP_KERNEL);
749 atomic_set(&fcc->issued_flush, 0);
750 atomic_set(&fcc->queued_flush, 0);
751 init_waitqueue_head(&fcc->flush_wait_queue);
752 init_llist_head(&fcc->issue_list);
753 SM_I(sbi)->fcc_info = fcc;
754 if (!test_opt(sbi, FLUSH_MERGE))
758 fcc->f2fs_issue_flush = kthread_run(issue_flush_thread, sbi,
759 "f2fs_flush-%u:%u", MAJOR(dev), MINOR(dev));
760 if (IS_ERR(fcc->f2fs_issue_flush)) {
761 err = PTR_ERR(fcc->f2fs_issue_flush);
763 SM_I(sbi)->fcc_info = NULL;
770 void f2fs_destroy_flush_cmd_control(struct f2fs_sb_info *sbi, bool free)
772 struct flush_cmd_control *fcc = SM_I(sbi)->fcc_info;
774 if (fcc && fcc->f2fs_issue_flush) {
775 struct task_struct *flush_thread = fcc->f2fs_issue_flush;
777 fcc->f2fs_issue_flush = NULL;
778 kthread_stop(flush_thread);
782 SM_I(sbi)->fcc_info = NULL;
786 int f2fs_flush_device_cache(struct f2fs_sb_info *sbi)
790 if (!f2fs_is_multi_device(sbi))
793 if (test_opt(sbi, NOBARRIER))
796 for (i = 1; i < sbi->s_ndevs; i++) {
797 int count = DEFAULT_RETRY_IO_COUNT;
799 if (!f2fs_test_bit(i, (char *)&sbi->dirty_device))
803 ret = __submit_flush_wait(sbi, FDEV(i).bdev);
805 f2fs_io_schedule_timeout(DEFAULT_IO_TIMEOUT);
806 } while (ret && --count);
809 f2fs_stop_checkpoint(sbi, false);
813 spin_lock(&sbi->dev_lock);
814 f2fs_clear_bit(i, (char *)&sbi->dirty_device);
815 spin_unlock(&sbi->dev_lock);
821 static void __locate_dirty_segment(struct f2fs_sb_info *sbi, unsigned int segno,
822 enum dirty_type dirty_type)
824 struct dirty_seglist_info *dirty_i = DIRTY_I(sbi);
826 /* need not be added */
827 if (IS_CURSEG(sbi, segno))
830 if (!test_and_set_bit(segno, dirty_i->dirty_segmap[dirty_type]))
831 dirty_i->nr_dirty[dirty_type]++;
833 if (dirty_type == DIRTY) {
834 struct seg_entry *sentry = get_seg_entry(sbi, segno);
835 enum dirty_type t = sentry->type;
837 if (unlikely(t >= DIRTY)) {
841 if (!test_and_set_bit(segno, dirty_i->dirty_segmap[t]))
842 dirty_i->nr_dirty[t]++;
844 if (__is_large_section(sbi)) {
845 unsigned int secno = GET_SEC_FROM_SEG(sbi, segno);
846 block_t valid_blocks =
847 get_valid_blocks(sbi, segno, true);
849 f2fs_bug_on(sbi, unlikely(!valid_blocks ||
850 valid_blocks == BLKS_PER_SEC(sbi)));
852 if (!IS_CURSEC(sbi, secno))
853 set_bit(secno, dirty_i->dirty_secmap);
858 static void __remove_dirty_segment(struct f2fs_sb_info *sbi, unsigned int segno,
859 enum dirty_type dirty_type)
861 struct dirty_seglist_info *dirty_i = DIRTY_I(sbi);
862 block_t valid_blocks;
864 if (test_and_clear_bit(segno, dirty_i->dirty_segmap[dirty_type]))
865 dirty_i->nr_dirty[dirty_type]--;
867 if (dirty_type == DIRTY) {
868 struct seg_entry *sentry = get_seg_entry(sbi, segno);
869 enum dirty_type t = sentry->type;
871 if (test_and_clear_bit(segno, dirty_i->dirty_segmap[t]))
872 dirty_i->nr_dirty[t]--;
874 valid_blocks = get_valid_blocks(sbi, segno, true);
875 if (valid_blocks == 0) {
876 clear_bit(GET_SEC_FROM_SEG(sbi, segno),
877 dirty_i->victim_secmap);
878 #ifdef CONFIG_F2FS_CHECK_FS
879 clear_bit(segno, SIT_I(sbi)->invalid_segmap);
882 if (__is_large_section(sbi)) {
883 unsigned int secno = GET_SEC_FROM_SEG(sbi, segno);
886 valid_blocks == BLKS_PER_SEC(sbi)) {
887 clear_bit(secno, dirty_i->dirty_secmap);
891 if (!IS_CURSEC(sbi, secno))
892 set_bit(secno, dirty_i->dirty_secmap);
898 * Should not occur error such as -ENOMEM.
899 * Adding dirty entry into seglist is not critical operation.
900 * If a given segment is one of current working segments, it won't be added.
902 static void locate_dirty_segment(struct f2fs_sb_info *sbi, unsigned int segno)
904 struct dirty_seglist_info *dirty_i = DIRTY_I(sbi);
905 unsigned short valid_blocks, ckpt_valid_blocks;
906 unsigned int usable_blocks;
908 if (segno == NULL_SEGNO || IS_CURSEG(sbi, segno))
911 usable_blocks = f2fs_usable_blks_in_seg(sbi, segno);
912 mutex_lock(&dirty_i->seglist_lock);
914 valid_blocks = get_valid_blocks(sbi, segno, false);
915 ckpt_valid_blocks = get_ckpt_valid_blocks(sbi, segno, false);
917 if (valid_blocks == 0 && (!is_sbi_flag_set(sbi, SBI_CP_DISABLED) ||
918 ckpt_valid_blocks == usable_blocks)) {
919 __locate_dirty_segment(sbi, segno, PRE);
920 __remove_dirty_segment(sbi, segno, DIRTY);
921 } else if (valid_blocks < usable_blocks) {
922 __locate_dirty_segment(sbi, segno, DIRTY);
924 /* Recovery routine with SSR needs this */
925 __remove_dirty_segment(sbi, segno, DIRTY);
928 mutex_unlock(&dirty_i->seglist_lock);
931 /* This moves currently empty dirty blocks to prefree. Must hold seglist_lock */
932 void f2fs_dirty_to_prefree(struct f2fs_sb_info *sbi)
934 struct dirty_seglist_info *dirty_i = DIRTY_I(sbi);
937 mutex_lock(&dirty_i->seglist_lock);
938 for_each_set_bit(segno, dirty_i->dirty_segmap[DIRTY], MAIN_SEGS(sbi)) {
939 if (get_valid_blocks(sbi, segno, false))
941 if (IS_CURSEG(sbi, segno))
943 __locate_dirty_segment(sbi, segno, PRE);
944 __remove_dirty_segment(sbi, segno, DIRTY);
946 mutex_unlock(&dirty_i->seglist_lock);
949 block_t f2fs_get_unusable_blocks(struct f2fs_sb_info *sbi)
952 (overprovision_segments(sbi) - reserved_segments(sbi));
953 block_t ovp_holes = ovp_hole_segs << sbi->log_blocks_per_seg;
954 struct dirty_seglist_info *dirty_i = DIRTY_I(sbi);
955 block_t holes[2] = {0, 0}; /* DATA and NODE */
957 struct seg_entry *se;
960 mutex_lock(&dirty_i->seglist_lock);
961 for_each_set_bit(segno, dirty_i->dirty_segmap[DIRTY], MAIN_SEGS(sbi)) {
962 se = get_seg_entry(sbi, segno);
963 if (IS_NODESEG(se->type))
964 holes[NODE] += f2fs_usable_blks_in_seg(sbi, segno) -
967 holes[DATA] += f2fs_usable_blks_in_seg(sbi, segno) -
970 mutex_unlock(&dirty_i->seglist_lock);
972 unusable = holes[DATA] > holes[NODE] ? holes[DATA] : holes[NODE];
973 if (unusable > ovp_holes)
974 return unusable - ovp_holes;
978 int f2fs_disable_cp_again(struct f2fs_sb_info *sbi, block_t unusable)
981 (overprovision_segments(sbi) - reserved_segments(sbi));
982 if (unusable > F2FS_OPTION(sbi).unusable_cap)
984 if (is_sbi_flag_set(sbi, SBI_CP_DISABLED_QUICK) &&
985 dirty_segments(sbi) > ovp_hole_segs)
990 /* This is only used by SBI_CP_DISABLED */
991 static unsigned int get_free_segment(struct f2fs_sb_info *sbi)
993 struct dirty_seglist_info *dirty_i = DIRTY_I(sbi);
994 unsigned int segno = 0;
996 mutex_lock(&dirty_i->seglist_lock);
997 for_each_set_bit(segno, dirty_i->dirty_segmap[DIRTY], MAIN_SEGS(sbi)) {
998 if (get_valid_blocks(sbi, segno, false))
1000 if (get_ckpt_valid_blocks(sbi, segno, false))
1002 mutex_unlock(&dirty_i->seglist_lock);
1005 mutex_unlock(&dirty_i->seglist_lock);
1009 static struct discard_cmd *__create_discard_cmd(struct f2fs_sb_info *sbi,
1010 struct block_device *bdev, block_t lstart,
1011 block_t start, block_t len)
1013 struct discard_cmd_control *dcc = SM_I(sbi)->dcc_info;
1014 struct list_head *pend_list;
1015 struct discard_cmd *dc;
1017 f2fs_bug_on(sbi, !len);
1019 pend_list = &dcc->pend_list[plist_idx(len)];
1021 dc = f2fs_kmem_cache_alloc(discard_cmd_slab, GFP_NOFS, true, NULL);
1022 INIT_LIST_HEAD(&dc->list);
1024 dc->lstart = lstart;
1031 init_completion(&dc->wait);
1032 list_add_tail(&dc->list, pend_list);
1033 spin_lock_init(&dc->lock);
1035 atomic_inc(&dcc->discard_cmd_cnt);
1036 dcc->undiscard_blks += len;
1041 static struct discard_cmd *__attach_discard_cmd(struct f2fs_sb_info *sbi,
1042 struct block_device *bdev, block_t lstart,
1043 block_t start, block_t len,
1044 struct rb_node *parent, struct rb_node **p,
1047 struct discard_cmd_control *dcc = SM_I(sbi)->dcc_info;
1048 struct discard_cmd *dc;
1050 dc = __create_discard_cmd(sbi, bdev, lstart, start, len);
1052 rb_link_node(&dc->rb_node, parent, p);
1053 rb_insert_color_cached(&dc->rb_node, &dcc->root, leftmost);
1058 static void __detach_discard_cmd(struct discard_cmd_control *dcc,
1059 struct discard_cmd *dc)
1061 if (dc->state == D_DONE)
1062 atomic_sub(dc->queued, &dcc->queued_discard);
1064 list_del(&dc->list);
1065 rb_erase_cached(&dc->rb_node, &dcc->root);
1066 dcc->undiscard_blks -= dc->len;
1068 kmem_cache_free(discard_cmd_slab, dc);
1070 atomic_dec(&dcc->discard_cmd_cnt);
1073 static void __remove_discard_cmd(struct f2fs_sb_info *sbi,
1074 struct discard_cmd *dc)
1076 struct discard_cmd_control *dcc = SM_I(sbi)->dcc_info;
1077 unsigned long flags;
1079 trace_f2fs_remove_discard(dc->bdev, dc->start, dc->len);
1081 spin_lock_irqsave(&dc->lock, flags);
1083 spin_unlock_irqrestore(&dc->lock, flags);
1086 spin_unlock_irqrestore(&dc->lock, flags);
1088 f2fs_bug_on(sbi, dc->ref);
1090 if (dc->error == -EOPNOTSUPP)
1095 "%sF2FS-fs (%s): Issue discard(%u, %u, %u) failed, ret: %d",
1096 KERN_INFO, sbi->sb->s_id,
1097 dc->lstart, dc->start, dc->len, dc->error);
1098 __detach_discard_cmd(dcc, dc);
1101 static void f2fs_submit_discard_endio(struct bio *bio)
1103 struct discard_cmd *dc = (struct discard_cmd *)bio->bi_private;
1104 unsigned long flags;
1106 spin_lock_irqsave(&dc->lock, flags);
1108 dc->error = blk_status_to_errno(bio->bi_status);
1110 if (!dc->bio_ref && dc->state == D_SUBMIT) {
1112 complete_all(&dc->wait);
1114 spin_unlock_irqrestore(&dc->lock, flags);
1118 static void __check_sit_bitmap(struct f2fs_sb_info *sbi,
1119 block_t start, block_t end)
1121 #ifdef CONFIG_F2FS_CHECK_FS
1122 struct seg_entry *sentry;
1124 block_t blk = start;
1125 unsigned long offset, size, max_blocks = sbi->blocks_per_seg;
1129 segno = GET_SEGNO(sbi, blk);
1130 sentry = get_seg_entry(sbi, segno);
1131 offset = GET_BLKOFF_FROM_SEG0(sbi, blk);
1133 if (end < START_BLOCK(sbi, segno + 1))
1134 size = GET_BLKOFF_FROM_SEG0(sbi, end);
1137 map = (unsigned long *)(sentry->cur_valid_map);
1138 offset = __find_rev_next_bit(map, size, offset);
1139 f2fs_bug_on(sbi, offset != size);
1140 blk = START_BLOCK(sbi, segno + 1);
1145 static void __init_discard_policy(struct f2fs_sb_info *sbi,
1146 struct discard_policy *dpolicy,
1147 int discard_type, unsigned int granularity)
1149 struct discard_cmd_control *dcc = SM_I(sbi)->dcc_info;
1152 dpolicy->type = discard_type;
1153 dpolicy->sync = true;
1154 dpolicy->ordered = false;
1155 dpolicy->granularity = granularity;
1157 dpolicy->max_requests = dcc->max_discard_request;
1158 dpolicy->io_aware_gran = MAX_PLIST_NUM;
1159 dpolicy->timeout = false;
1161 if (discard_type == DPOLICY_BG) {
1162 dpolicy->min_interval = dcc->min_discard_issue_time;
1163 dpolicy->mid_interval = dcc->mid_discard_issue_time;
1164 dpolicy->max_interval = dcc->max_discard_issue_time;
1165 dpolicy->io_aware = true;
1166 dpolicy->sync = false;
1167 dpolicy->ordered = true;
1168 if (utilization(sbi) > DEF_DISCARD_URGENT_UTIL) {
1169 dpolicy->granularity = 1;
1170 if (atomic_read(&dcc->discard_cmd_cnt))
1171 dpolicy->max_interval =
1172 dcc->min_discard_issue_time;
1174 } else if (discard_type == DPOLICY_FORCE) {
1175 dpolicy->min_interval = dcc->min_discard_issue_time;
1176 dpolicy->mid_interval = dcc->mid_discard_issue_time;
1177 dpolicy->max_interval = dcc->max_discard_issue_time;
1178 dpolicy->io_aware = false;
1179 } else if (discard_type == DPOLICY_FSTRIM) {
1180 dpolicy->io_aware = false;
1181 } else if (discard_type == DPOLICY_UMOUNT) {
1182 dpolicy->io_aware = false;
1183 /* we need to issue all to keep CP_TRIMMED_FLAG */
1184 dpolicy->granularity = 1;
1185 dpolicy->timeout = true;
1189 static void __update_discard_tree_range(struct f2fs_sb_info *sbi,
1190 struct block_device *bdev, block_t lstart,
1191 block_t start, block_t len);
1192 /* this function is copied from blkdev_issue_discard from block/blk-lib.c */
1193 static int __submit_discard_cmd(struct f2fs_sb_info *sbi,
1194 struct discard_policy *dpolicy,
1195 struct discard_cmd *dc,
1196 unsigned int *issued)
1198 struct block_device *bdev = dc->bdev;
1199 struct request_queue *q = bdev_get_queue(bdev);
1200 unsigned int max_discard_blocks =
1201 SECTOR_TO_BLOCK(q->limits.max_discard_sectors);
1202 struct discard_cmd_control *dcc = SM_I(sbi)->dcc_info;
1203 struct list_head *wait_list = (dpolicy->type == DPOLICY_FSTRIM) ?
1204 &(dcc->fstrim_list) : &(dcc->wait_list);
1205 int flag = dpolicy->sync ? REQ_SYNC : 0;
1206 block_t lstart, start, len, total_len;
1209 if (dc->state != D_PREP)
1212 if (is_sbi_flag_set(sbi, SBI_NEED_FSCK))
1215 trace_f2fs_issue_discard(bdev, dc->start, dc->len);
1217 lstart = dc->lstart;
1224 while (total_len && *issued < dpolicy->max_requests && !err) {
1225 struct bio *bio = NULL;
1226 unsigned long flags;
1229 if (len > max_discard_blocks) {
1230 len = max_discard_blocks;
1235 if (*issued == dpolicy->max_requests)
1240 if (time_to_inject(sbi, FAULT_DISCARD)) {
1241 f2fs_show_injection_info(sbi, FAULT_DISCARD);
1245 err = __blkdev_issue_discard(bdev,
1246 SECTOR_FROM_BLOCK(start),
1247 SECTOR_FROM_BLOCK(len),
1251 spin_lock_irqsave(&dc->lock, flags);
1252 if (dc->state == D_PARTIAL)
1253 dc->state = D_SUBMIT;
1254 spin_unlock_irqrestore(&dc->lock, flags);
1259 f2fs_bug_on(sbi, !bio);
1262 * should keep before submission to avoid D_DONE
1265 spin_lock_irqsave(&dc->lock, flags);
1267 dc->state = D_SUBMIT;
1269 dc->state = D_PARTIAL;
1271 spin_unlock_irqrestore(&dc->lock, flags);
1273 atomic_inc(&dcc->queued_discard);
1275 list_move_tail(&dc->list, wait_list);
1277 /* sanity check on discard range */
1278 __check_sit_bitmap(sbi, lstart, lstart + len);
1280 bio->bi_private = dc;
1281 bio->bi_end_io = f2fs_submit_discard_endio;
1282 bio->bi_opf |= flag;
1285 atomic_inc(&dcc->issued_discard);
1287 f2fs_update_iostat(sbi, FS_DISCARD, 1);
1296 dcc->undiscard_blks -= len;
1297 __update_discard_tree_range(sbi, bdev, lstart, start, len);
1302 static void __insert_discard_tree(struct f2fs_sb_info *sbi,
1303 struct block_device *bdev, block_t lstart,
1304 block_t start, block_t len,
1305 struct rb_node **insert_p,
1306 struct rb_node *insert_parent)
1308 struct discard_cmd_control *dcc = SM_I(sbi)->dcc_info;
1310 struct rb_node *parent = NULL;
1311 bool leftmost = true;
1313 if (insert_p && insert_parent) {
1314 parent = insert_parent;
1319 p = f2fs_lookup_rb_tree_for_insert(sbi, &dcc->root, &parent,
1322 __attach_discard_cmd(sbi, bdev, lstart, start, len, parent,
1326 static void __relocate_discard_cmd(struct discard_cmd_control *dcc,
1327 struct discard_cmd *dc)
1329 list_move_tail(&dc->list, &dcc->pend_list[plist_idx(dc->len)]);
1332 static void __punch_discard_cmd(struct f2fs_sb_info *sbi,
1333 struct discard_cmd *dc, block_t blkaddr)
1335 struct discard_cmd_control *dcc = SM_I(sbi)->dcc_info;
1336 struct discard_info di = dc->di;
1337 bool modified = false;
1339 if (dc->state == D_DONE || dc->len == 1) {
1340 __remove_discard_cmd(sbi, dc);
1344 dcc->undiscard_blks -= di.len;
1346 if (blkaddr > di.lstart) {
1347 dc->len = blkaddr - dc->lstart;
1348 dcc->undiscard_blks += dc->len;
1349 __relocate_discard_cmd(dcc, dc);
1353 if (blkaddr < di.lstart + di.len - 1) {
1355 __insert_discard_tree(sbi, dc->bdev, blkaddr + 1,
1356 di.start + blkaddr + 1 - di.lstart,
1357 di.lstart + di.len - 1 - blkaddr,
1363 dcc->undiscard_blks += dc->len;
1364 __relocate_discard_cmd(dcc, dc);
1369 static void __update_discard_tree_range(struct f2fs_sb_info *sbi,
1370 struct block_device *bdev, block_t lstart,
1371 block_t start, block_t len)
1373 struct discard_cmd_control *dcc = SM_I(sbi)->dcc_info;
1374 struct discard_cmd *prev_dc = NULL, *next_dc = NULL;
1375 struct discard_cmd *dc;
1376 struct discard_info di = {0};
1377 struct rb_node **insert_p = NULL, *insert_parent = NULL;
1378 struct request_queue *q = bdev_get_queue(bdev);
1379 unsigned int max_discard_blocks =
1380 SECTOR_TO_BLOCK(q->limits.max_discard_sectors);
1381 block_t end = lstart + len;
1383 dc = (struct discard_cmd *)f2fs_lookup_rb_tree_ret(&dcc->root,
1385 (struct rb_entry **)&prev_dc,
1386 (struct rb_entry **)&next_dc,
1387 &insert_p, &insert_parent, true, NULL);
1393 di.len = next_dc ? next_dc->lstart - lstart : len;
1394 di.len = min(di.len, len);
1399 struct rb_node *node;
1400 bool merged = false;
1401 struct discard_cmd *tdc = NULL;
1404 di.lstart = prev_dc->lstart + prev_dc->len;
1405 if (di.lstart < lstart)
1407 if (di.lstart >= end)
1410 if (!next_dc || next_dc->lstart > end)
1411 di.len = end - di.lstart;
1413 di.len = next_dc->lstart - di.lstart;
1414 di.start = start + di.lstart - lstart;
1420 if (prev_dc && prev_dc->state == D_PREP &&
1421 prev_dc->bdev == bdev &&
1422 __is_discard_back_mergeable(&di, &prev_dc->di,
1423 max_discard_blocks)) {
1424 prev_dc->di.len += di.len;
1425 dcc->undiscard_blks += di.len;
1426 __relocate_discard_cmd(dcc, prev_dc);
1432 if (next_dc && next_dc->state == D_PREP &&
1433 next_dc->bdev == bdev &&
1434 __is_discard_front_mergeable(&di, &next_dc->di,
1435 max_discard_blocks)) {
1436 next_dc->di.lstart = di.lstart;
1437 next_dc->di.len += di.len;
1438 next_dc->di.start = di.start;
1439 dcc->undiscard_blks += di.len;
1440 __relocate_discard_cmd(dcc, next_dc);
1442 __remove_discard_cmd(sbi, tdc);
1447 __insert_discard_tree(sbi, bdev, di.lstart, di.start,
1448 di.len, NULL, NULL);
1455 node = rb_next(&prev_dc->rb_node);
1456 next_dc = rb_entry_safe(node, struct discard_cmd, rb_node);
1460 static int __queue_discard_cmd(struct f2fs_sb_info *sbi,
1461 struct block_device *bdev, block_t blkstart, block_t blklen)
1463 block_t lblkstart = blkstart;
1465 if (!f2fs_bdev_support_discard(bdev))
1468 trace_f2fs_queue_discard(bdev, blkstart, blklen);
1470 if (f2fs_is_multi_device(sbi)) {
1471 int devi = f2fs_target_device_index(sbi, blkstart);
1473 blkstart -= FDEV(devi).start_blk;
1475 mutex_lock(&SM_I(sbi)->dcc_info->cmd_lock);
1476 __update_discard_tree_range(sbi, bdev, lblkstart, blkstart, blklen);
1477 mutex_unlock(&SM_I(sbi)->dcc_info->cmd_lock);
1481 static unsigned int __issue_discard_cmd_orderly(struct f2fs_sb_info *sbi,
1482 struct discard_policy *dpolicy)
1484 struct discard_cmd_control *dcc = SM_I(sbi)->dcc_info;
1485 struct discard_cmd *prev_dc = NULL, *next_dc = NULL;
1486 struct rb_node **insert_p = NULL, *insert_parent = NULL;
1487 struct discard_cmd *dc;
1488 struct blk_plug plug;
1489 unsigned int pos = dcc->next_pos;
1490 unsigned int issued = 0;
1491 bool io_interrupted = false;
1493 mutex_lock(&dcc->cmd_lock);
1494 dc = (struct discard_cmd *)f2fs_lookup_rb_tree_ret(&dcc->root,
1496 (struct rb_entry **)&prev_dc,
1497 (struct rb_entry **)&next_dc,
1498 &insert_p, &insert_parent, true, NULL);
1502 blk_start_plug(&plug);
1505 struct rb_node *node;
1508 if (dc->state != D_PREP)
1511 if (dpolicy->io_aware && !is_idle(sbi, DISCARD_TIME)) {
1512 io_interrupted = true;
1516 dcc->next_pos = dc->lstart + dc->len;
1517 err = __submit_discard_cmd(sbi, dpolicy, dc, &issued);
1519 if (issued >= dpolicy->max_requests)
1522 node = rb_next(&dc->rb_node);
1524 __remove_discard_cmd(sbi, dc);
1525 dc = rb_entry_safe(node, struct discard_cmd, rb_node);
1528 blk_finish_plug(&plug);
1533 mutex_unlock(&dcc->cmd_lock);
1535 if (!issued && io_interrupted)
1540 static unsigned int __wait_all_discard_cmd(struct f2fs_sb_info *sbi,
1541 struct discard_policy *dpolicy);
1543 static int __issue_discard_cmd(struct f2fs_sb_info *sbi,
1544 struct discard_policy *dpolicy)
1546 struct discard_cmd_control *dcc = SM_I(sbi)->dcc_info;
1547 struct list_head *pend_list;
1548 struct discard_cmd *dc, *tmp;
1549 struct blk_plug plug;
1551 bool io_interrupted = false;
1553 if (dpolicy->timeout)
1554 f2fs_update_time(sbi, UMOUNT_DISCARD_TIMEOUT);
1558 for (i = MAX_PLIST_NUM - 1; i >= 0; i--) {
1559 if (dpolicy->timeout &&
1560 f2fs_time_over(sbi, UMOUNT_DISCARD_TIMEOUT))
1563 if (i + 1 < dpolicy->granularity)
1566 if (i < DEFAULT_DISCARD_GRANULARITY && dpolicy->ordered)
1567 return __issue_discard_cmd_orderly(sbi, dpolicy);
1569 pend_list = &dcc->pend_list[i];
1571 mutex_lock(&dcc->cmd_lock);
1572 if (list_empty(pend_list))
1574 if (unlikely(dcc->rbtree_check))
1575 f2fs_bug_on(sbi, !f2fs_check_rb_tree_consistence(sbi,
1576 &dcc->root, false));
1577 blk_start_plug(&plug);
1578 list_for_each_entry_safe(dc, tmp, pend_list, list) {
1579 f2fs_bug_on(sbi, dc->state != D_PREP);
1581 if (dpolicy->timeout &&
1582 f2fs_time_over(sbi, UMOUNT_DISCARD_TIMEOUT))
1585 if (dpolicy->io_aware && i < dpolicy->io_aware_gran &&
1586 !is_idle(sbi, DISCARD_TIME)) {
1587 io_interrupted = true;
1591 __submit_discard_cmd(sbi, dpolicy, dc, &issued);
1593 if (issued >= dpolicy->max_requests)
1596 blk_finish_plug(&plug);
1598 mutex_unlock(&dcc->cmd_lock);
1600 if (issued >= dpolicy->max_requests || io_interrupted)
1604 if (dpolicy->type == DPOLICY_UMOUNT && issued) {
1605 __wait_all_discard_cmd(sbi, dpolicy);
1609 if (!issued && io_interrupted)
1615 static bool __drop_discard_cmd(struct f2fs_sb_info *sbi)
1617 struct discard_cmd_control *dcc = SM_I(sbi)->dcc_info;
1618 struct list_head *pend_list;
1619 struct discard_cmd *dc, *tmp;
1621 bool dropped = false;
1623 mutex_lock(&dcc->cmd_lock);
1624 for (i = MAX_PLIST_NUM - 1; i >= 0; i--) {
1625 pend_list = &dcc->pend_list[i];
1626 list_for_each_entry_safe(dc, tmp, pend_list, list) {
1627 f2fs_bug_on(sbi, dc->state != D_PREP);
1628 __remove_discard_cmd(sbi, dc);
1632 mutex_unlock(&dcc->cmd_lock);
1637 void f2fs_drop_discard_cmd(struct f2fs_sb_info *sbi)
1639 __drop_discard_cmd(sbi);
1642 static unsigned int __wait_one_discard_bio(struct f2fs_sb_info *sbi,
1643 struct discard_cmd *dc)
1645 struct discard_cmd_control *dcc = SM_I(sbi)->dcc_info;
1646 unsigned int len = 0;
1648 wait_for_completion_io(&dc->wait);
1649 mutex_lock(&dcc->cmd_lock);
1650 f2fs_bug_on(sbi, dc->state != D_DONE);
1655 __remove_discard_cmd(sbi, dc);
1657 mutex_unlock(&dcc->cmd_lock);
1662 static unsigned int __wait_discard_cmd_range(struct f2fs_sb_info *sbi,
1663 struct discard_policy *dpolicy,
1664 block_t start, block_t end)
1666 struct discard_cmd_control *dcc = SM_I(sbi)->dcc_info;
1667 struct list_head *wait_list = (dpolicy->type == DPOLICY_FSTRIM) ?
1668 &(dcc->fstrim_list) : &(dcc->wait_list);
1669 struct discard_cmd *dc, *tmp;
1671 unsigned int trimmed = 0;
1676 mutex_lock(&dcc->cmd_lock);
1677 list_for_each_entry_safe(dc, tmp, wait_list, list) {
1678 if (dc->lstart + dc->len <= start || end <= dc->lstart)
1680 if (dc->len < dpolicy->granularity)
1682 if (dc->state == D_DONE && !dc->ref) {
1683 wait_for_completion_io(&dc->wait);
1686 __remove_discard_cmd(sbi, dc);
1693 mutex_unlock(&dcc->cmd_lock);
1696 trimmed += __wait_one_discard_bio(sbi, dc);
1703 static unsigned int __wait_all_discard_cmd(struct f2fs_sb_info *sbi,
1704 struct discard_policy *dpolicy)
1706 struct discard_policy dp;
1707 unsigned int discard_blks;
1710 return __wait_discard_cmd_range(sbi, dpolicy, 0, UINT_MAX);
1713 __init_discard_policy(sbi, &dp, DPOLICY_FSTRIM, 1);
1714 discard_blks = __wait_discard_cmd_range(sbi, &dp, 0, UINT_MAX);
1715 __init_discard_policy(sbi, &dp, DPOLICY_UMOUNT, 1);
1716 discard_blks += __wait_discard_cmd_range(sbi, &dp, 0, UINT_MAX);
1718 return discard_blks;
1721 /* This should be covered by global mutex, &sit_i->sentry_lock */
1722 static void f2fs_wait_discard_bio(struct f2fs_sb_info *sbi, block_t blkaddr)
1724 struct discard_cmd_control *dcc = SM_I(sbi)->dcc_info;
1725 struct discard_cmd *dc;
1726 bool need_wait = false;
1728 mutex_lock(&dcc->cmd_lock);
1729 dc = (struct discard_cmd *)f2fs_lookup_rb_tree(&dcc->root,
1732 if (dc->state == D_PREP) {
1733 __punch_discard_cmd(sbi, dc, blkaddr);
1739 mutex_unlock(&dcc->cmd_lock);
1742 __wait_one_discard_bio(sbi, dc);
1745 void f2fs_stop_discard_thread(struct f2fs_sb_info *sbi)
1747 struct discard_cmd_control *dcc = SM_I(sbi)->dcc_info;
1749 if (dcc && dcc->f2fs_issue_discard) {
1750 struct task_struct *discard_thread = dcc->f2fs_issue_discard;
1752 dcc->f2fs_issue_discard = NULL;
1753 kthread_stop(discard_thread);
1757 /* This comes from f2fs_put_super */
1758 bool f2fs_issue_discard_timeout(struct f2fs_sb_info *sbi)
1760 struct discard_cmd_control *dcc = SM_I(sbi)->dcc_info;
1761 struct discard_policy dpolicy;
1764 __init_discard_policy(sbi, &dpolicy, DPOLICY_UMOUNT,
1765 dcc->discard_granularity);
1766 __issue_discard_cmd(sbi, &dpolicy);
1767 dropped = __drop_discard_cmd(sbi);
1769 /* just to make sure there is no pending discard commands */
1770 __wait_all_discard_cmd(sbi, NULL);
1772 f2fs_bug_on(sbi, atomic_read(&dcc->discard_cmd_cnt));
1776 static int issue_discard_thread(void *data)
1778 struct f2fs_sb_info *sbi = data;
1779 struct discard_cmd_control *dcc = SM_I(sbi)->dcc_info;
1780 wait_queue_head_t *q = &dcc->discard_wait_queue;
1781 struct discard_policy dpolicy;
1782 unsigned int wait_ms = dcc->min_discard_issue_time;
1788 if (sbi->gc_mode == GC_URGENT_HIGH ||
1789 !f2fs_available_free_memory(sbi, DISCARD_CACHE))
1790 __init_discard_policy(sbi, &dpolicy, DPOLICY_FORCE, 1);
1792 __init_discard_policy(sbi, &dpolicy, DPOLICY_BG,
1793 dcc->discard_granularity);
1795 if (!atomic_read(&dcc->discard_cmd_cnt))
1796 wait_ms = dpolicy.max_interval;
1798 wait_event_interruptible_timeout(*q,
1799 kthread_should_stop() || freezing(current) ||
1801 msecs_to_jiffies(wait_ms));
1803 if (dcc->discard_wake)
1804 dcc->discard_wake = 0;
1806 /* clean up pending candidates before going to sleep */
1807 if (atomic_read(&dcc->queued_discard))
1808 __wait_all_discard_cmd(sbi, NULL);
1810 if (try_to_freeze())
1812 if (f2fs_readonly(sbi->sb))
1814 if (kthread_should_stop())
1816 if (is_sbi_flag_set(sbi, SBI_NEED_FSCK)) {
1817 wait_ms = dpolicy.max_interval;
1820 if (!atomic_read(&dcc->discard_cmd_cnt))
1823 sb_start_intwrite(sbi->sb);
1825 issued = __issue_discard_cmd(sbi, &dpolicy);
1827 __wait_all_discard_cmd(sbi, &dpolicy);
1828 wait_ms = dpolicy.min_interval;
1829 } else if (issued == -1) {
1830 wait_ms = f2fs_time_to_wait(sbi, DISCARD_TIME);
1832 wait_ms = dpolicy.mid_interval;
1834 wait_ms = dpolicy.max_interval;
1837 sb_end_intwrite(sbi->sb);
1839 } while (!kthread_should_stop());
1843 #ifdef CONFIG_BLK_DEV_ZONED
1844 static int __f2fs_issue_discard_zone(struct f2fs_sb_info *sbi,
1845 struct block_device *bdev, block_t blkstart, block_t blklen)
1847 sector_t sector, nr_sects;
1848 block_t lblkstart = blkstart;
1851 if (f2fs_is_multi_device(sbi)) {
1852 devi = f2fs_target_device_index(sbi, blkstart);
1853 if (blkstart < FDEV(devi).start_blk ||
1854 blkstart > FDEV(devi).end_blk) {
1855 f2fs_err(sbi, "Invalid block %x", blkstart);
1858 blkstart -= FDEV(devi).start_blk;
1861 /* For sequential zones, reset the zone write pointer */
1862 if (f2fs_blkz_is_seq(sbi, devi, blkstart)) {
1863 sector = SECTOR_FROM_BLOCK(blkstart);
1864 nr_sects = SECTOR_FROM_BLOCK(blklen);
1866 if (sector & (bdev_zone_sectors(bdev) - 1) ||
1867 nr_sects != bdev_zone_sectors(bdev)) {
1868 f2fs_err(sbi, "(%d) %s: Unaligned zone reset attempted (block %x + %x)",
1869 devi, sbi->s_ndevs ? FDEV(devi).path : "",
1873 trace_f2fs_issue_reset_zone(bdev, blkstart);
1874 return blkdev_zone_mgmt(bdev, REQ_OP_ZONE_RESET,
1875 sector, nr_sects, GFP_NOFS);
1878 /* For conventional zones, use regular discard if supported */
1879 return __queue_discard_cmd(sbi, bdev, lblkstart, blklen);
1883 static int __issue_discard_async(struct f2fs_sb_info *sbi,
1884 struct block_device *bdev, block_t blkstart, block_t blklen)
1886 #ifdef CONFIG_BLK_DEV_ZONED
1887 if (f2fs_sb_has_blkzoned(sbi) && bdev_is_zoned(bdev))
1888 return __f2fs_issue_discard_zone(sbi, bdev, blkstart, blklen);
1890 return __queue_discard_cmd(sbi, bdev, blkstart, blklen);
1893 static int f2fs_issue_discard(struct f2fs_sb_info *sbi,
1894 block_t blkstart, block_t blklen)
1896 sector_t start = blkstart, len = 0;
1897 struct block_device *bdev;
1898 struct seg_entry *se;
1899 unsigned int offset;
1903 bdev = f2fs_target_device(sbi, blkstart, NULL);
1905 for (i = blkstart; i < blkstart + blklen; i++, len++) {
1907 struct block_device *bdev2 =
1908 f2fs_target_device(sbi, i, NULL);
1910 if (bdev2 != bdev) {
1911 err = __issue_discard_async(sbi, bdev,
1921 se = get_seg_entry(sbi, GET_SEGNO(sbi, i));
1922 offset = GET_BLKOFF_FROM_SEG0(sbi, i);
1924 if (f2fs_block_unit_discard(sbi) &&
1925 !f2fs_test_and_set_bit(offset, se->discard_map))
1926 sbi->discard_blks--;
1930 err = __issue_discard_async(sbi, bdev, start, len);
1934 static bool add_discard_addrs(struct f2fs_sb_info *sbi, struct cp_control *cpc,
1937 int entries = SIT_VBLOCK_MAP_SIZE / sizeof(unsigned long);
1938 int max_blocks = sbi->blocks_per_seg;
1939 struct seg_entry *se = get_seg_entry(sbi, cpc->trim_start);
1940 unsigned long *cur_map = (unsigned long *)se->cur_valid_map;
1941 unsigned long *ckpt_map = (unsigned long *)se->ckpt_valid_map;
1942 unsigned long *discard_map = (unsigned long *)se->discard_map;
1943 unsigned long *dmap = SIT_I(sbi)->tmp_map;
1944 unsigned int start = 0, end = -1;
1945 bool force = (cpc->reason & CP_DISCARD);
1946 struct discard_entry *de = NULL;
1947 struct list_head *head = &SM_I(sbi)->dcc_info->entry_list;
1950 if (se->valid_blocks == max_blocks || !f2fs_hw_support_discard(sbi) ||
1951 !f2fs_block_unit_discard(sbi))
1955 if (!f2fs_realtime_discard_enable(sbi) || !se->valid_blocks ||
1956 SM_I(sbi)->dcc_info->nr_discards >=
1957 SM_I(sbi)->dcc_info->max_discards)
1961 /* SIT_VBLOCK_MAP_SIZE should be multiple of sizeof(unsigned long) */
1962 for (i = 0; i < entries; i++)
1963 dmap[i] = force ? ~ckpt_map[i] & ~discard_map[i] :
1964 (cur_map[i] ^ ckpt_map[i]) & ckpt_map[i];
1966 while (force || SM_I(sbi)->dcc_info->nr_discards <=
1967 SM_I(sbi)->dcc_info->max_discards) {
1968 start = __find_rev_next_bit(dmap, max_blocks, end + 1);
1969 if (start >= max_blocks)
1972 end = __find_rev_next_zero_bit(dmap, max_blocks, start + 1);
1973 if (force && start && end != max_blocks
1974 && (end - start) < cpc->trim_minlen)
1981 de = f2fs_kmem_cache_alloc(discard_entry_slab,
1982 GFP_F2FS_ZERO, true, NULL);
1983 de->start_blkaddr = START_BLOCK(sbi, cpc->trim_start);
1984 list_add_tail(&de->list, head);
1987 for (i = start; i < end; i++)
1988 __set_bit_le(i, (void *)de->discard_map);
1990 SM_I(sbi)->dcc_info->nr_discards += end - start;
1995 static void release_discard_addr(struct discard_entry *entry)
1997 list_del(&entry->list);
1998 kmem_cache_free(discard_entry_slab, entry);
2001 void f2fs_release_discard_addrs(struct f2fs_sb_info *sbi)
2003 struct list_head *head = &(SM_I(sbi)->dcc_info->entry_list);
2004 struct discard_entry *entry, *this;
2007 list_for_each_entry_safe(entry, this, head, list)
2008 release_discard_addr(entry);
2012 * Should call f2fs_clear_prefree_segments after checkpoint is done.
2014 static void set_prefree_as_free_segments(struct f2fs_sb_info *sbi)
2016 struct dirty_seglist_info *dirty_i = DIRTY_I(sbi);
2019 mutex_lock(&dirty_i->seglist_lock);
2020 for_each_set_bit(segno, dirty_i->dirty_segmap[PRE], MAIN_SEGS(sbi))
2021 __set_test_and_free(sbi, segno, false);
2022 mutex_unlock(&dirty_i->seglist_lock);
2025 void f2fs_clear_prefree_segments(struct f2fs_sb_info *sbi,
2026 struct cp_control *cpc)
2028 struct discard_cmd_control *dcc = SM_I(sbi)->dcc_info;
2029 struct list_head *head = &dcc->entry_list;
2030 struct discard_entry *entry, *this;
2031 struct dirty_seglist_info *dirty_i = DIRTY_I(sbi);
2032 unsigned long *prefree_map = dirty_i->dirty_segmap[PRE];
2033 unsigned int start = 0, end = -1;
2034 unsigned int secno, start_segno;
2035 bool force = (cpc->reason & CP_DISCARD);
2036 bool section_alignment = F2FS_OPTION(sbi).discard_unit ==
2037 DISCARD_UNIT_SECTION;
2039 if (f2fs_lfs_mode(sbi) && __is_large_section(sbi))
2040 section_alignment = true;
2042 mutex_lock(&dirty_i->seglist_lock);
2047 if (section_alignment && end != -1)
2049 start = find_next_bit(prefree_map, MAIN_SEGS(sbi), end + 1);
2050 if (start >= MAIN_SEGS(sbi))
2052 end = find_next_zero_bit(prefree_map, MAIN_SEGS(sbi),
2055 if (section_alignment) {
2056 start = rounddown(start, sbi->segs_per_sec);
2057 end = roundup(end, sbi->segs_per_sec);
2060 for (i = start; i < end; i++) {
2061 if (test_and_clear_bit(i, prefree_map))
2062 dirty_i->nr_dirty[PRE]--;
2065 if (!f2fs_realtime_discard_enable(sbi))
2068 if (force && start >= cpc->trim_start &&
2069 (end - 1) <= cpc->trim_end)
2072 if (!f2fs_lfs_mode(sbi) || !__is_large_section(sbi)) {
2073 f2fs_issue_discard(sbi, START_BLOCK(sbi, start),
2074 (end - start) << sbi->log_blocks_per_seg);
2078 secno = GET_SEC_FROM_SEG(sbi, start);
2079 start_segno = GET_SEG_FROM_SEC(sbi, secno);
2080 if (!IS_CURSEC(sbi, secno) &&
2081 !get_valid_blocks(sbi, start, true))
2082 f2fs_issue_discard(sbi, START_BLOCK(sbi, start_segno),
2083 sbi->segs_per_sec << sbi->log_blocks_per_seg);
2085 start = start_segno + sbi->segs_per_sec;
2091 mutex_unlock(&dirty_i->seglist_lock);
2093 if (!f2fs_block_unit_discard(sbi))
2096 /* send small discards */
2097 list_for_each_entry_safe(entry, this, head, list) {
2098 unsigned int cur_pos = 0, next_pos, len, total_len = 0;
2099 bool is_valid = test_bit_le(0, entry->discard_map);
2103 next_pos = find_next_zero_bit_le(entry->discard_map,
2104 sbi->blocks_per_seg, cur_pos);
2105 len = next_pos - cur_pos;
2107 if (f2fs_sb_has_blkzoned(sbi) ||
2108 (force && len < cpc->trim_minlen))
2111 f2fs_issue_discard(sbi, entry->start_blkaddr + cur_pos,
2115 next_pos = find_next_bit_le(entry->discard_map,
2116 sbi->blocks_per_seg, cur_pos);
2120 is_valid = !is_valid;
2122 if (cur_pos < sbi->blocks_per_seg)
2125 release_discard_addr(entry);
2126 dcc->nr_discards -= total_len;
2130 wake_up_discard_thread(sbi, false);
2133 int f2fs_start_discard_thread(struct f2fs_sb_info *sbi)
2135 dev_t dev = sbi->sb->s_bdev->bd_dev;
2136 struct discard_cmd_control *dcc = SM_I(sbi)->dcc_info;
2139 if (!f2fs_realtime_discard_enable(sbi))
2142 dcc->f2fs_issue_discard = kthread_run(issue_discard_thread, sbi,
2143 "f2fs_discard-%u:%u", MAJOR(dev), MINOR(dev));
2144 if (IS_ERR(dcc->f2fs_issue_discard))
2145 err = PTR_ERR(dcc->f2fs_issue_discard);
2150 static int create_discard_cmd_control(struct f2fs_sb_info *sbi)
2152 struct discard_cmd_control *dcc;
2155 if (SM_I(sbi)->dcc_info) {
2156 dcc = SM_I(sbi)->dcc_info;
2160 dcc = f2fs_kzalloc(sbi, sizeof(struct discard_cmd_control), GFP_KERNEL);
2164 dcc->discard_granularity = DEFAULT_DISCARD_GRANULARITY;
2165 if (F2FS_OPTION(sbi).discard_unit == DISCARD_UNIT_SEGMENT)
2166 dcc->discard_granularity = sbi->blocks_per_seg;
2167 else if (F2FS_OPTION(sbi).discard_unit == DISCARD_UNIT_SECTION)
2168 dcc->discard_granularity = BLKS_PER_SEC(sbi);
2170 INIT_LIST_HEAD(&dcc->entry_list);
2171 for (i = 0; i < MAX_PLIST_NUM; i++)
2172 INIT_LIST_HEAD(&dcc->pend_list[i]);
2173 INIT_LIST_HEAD(&dcc->wait_list);
2174 INIT_LIST_HEAD(&dcc->fstrim_list);
2175 mutex_init(&dcc->cmd_lock);
2176 atomic_set(&dcc->issued_discard, 0);
2177 atomic_set(&dcc->queued_discard, 0);
2178 atomic_set(&dcc->discard_cmd_cnt, 0);
2179 dcc->nr_discards = 0;
2180 dcc->max_discards = MAIN_SEGS(sbi) << sbi->log_blocks_per_seg;
2181 dcc->max_discard_request = DEF_MAX_DISCARD_REQUEST;
2182 dcc->min_discard_issue_time = DEF_MIN_DISCARD_ISSUE_TIME;
2183 dcc->mid_discard_issue_time = DEF_MID_DISCARD_ISSUE_TIME;
2184 dcc->max_discard_issue_time = DEF_MAX_DISCARD_ISSUE_TIME;
2185 dcc->undiscard_blks = 0;
2187 dcc->root = RB_ROOT_CACHED;
2188 dcc->rbtree_check = false;
2190 init_waitqueue_head(&dcc->discard_wait_queue);
2191 SM_I(sbi)->dcc_info = dcc;
2193 err = f2fs_start_discard_thread(sbi);
2196 SM_I(sbi)->dcc_info = NULL;
2202 static void destroy_discard_cmd_control(struct f2fs_sb_info *sbi)
2204 struct discard_cmd_control *dcc = SM_I(sbi)->dcc_info;
2209 f2fs_stop_discard_thread(sbi);
2212 * Recovery can cache discard commands, so in error path of
2213 * fill_super(), it needs to give a chance to handle them.
2215 if (unlikely(atomic_read(&dcc->discard_cmd_cnt)))
2216 f2fs_issue_discard_timeout(sbi);
2219 SM_I(sbi)->dcc_info = NULL;
2222 static bool __mark_sit_entry_dirty(struct f2fs_sb_info *sbi, unsigned int segno)
2224 struct sit_info *sit_i = SIT_I(sbi);
2226 if (!__test_and_set_bit(segno, sit_i->dirty_sentries_bitmap)) {
2227 sit_i->dirty_sentries++;
2234 static void __set_sit_entry_type(struct f2fs_sb_info *sbi, int type,
2235 unsigned int segno, int modified)
2237 struct seg_entry *se = get_seg_entry(sbi, segno);
2241 __mark_sit_entry_dirty(sbi, segno);
2244 static inline unsigned long long get_segment_mtime(struct f2fs_sb_info *sbi,
2247 unsigned int segno = GET_SEGNO(sbi, blkaddr);
2249 if (segno == NULL_SEGNO)
2251 return get_seg_entry(sbi, segno)->mtime;
2254 static void update_segment_mtime(struct f2fs_sb_info *sbi, block_t blkaddr,
2255 unsigned long long old_mtime)
2257 struct seg_entry *se;
2258 unsigned int segno = GET_SEGNO(sbi, blkaddr);
2259 unsigned long long ctime = get_mtime(sbi, false);
2260 unsigned long long mtime = old_mtime ? old_mtime : ctime;
2262 if (segno == NULL_SEGNO)
2265 se = get_seg_entry(sbi, segno);
2270 se->mtime = div_u64(se->mtime * se->valid_blocks + mtime,
2271 se->valid_blocks + 1);
2273 if (ctime > SIT_I(sbi)->max_mtime)
2274 SIT_I(sbi)->max_mtime = ctime;
2277 static void update_sit_entry(struct f2fs_sb_info *sbi, block_t blkaddr, int del)
2279 struct seg_entry *se;
2280 unsigned int segno, offset;
2281 long int new_vblocks;
2283 #ifdef CONFIG_F2FS_CHECK_FS
2287 segno = GET_SEGNO(sbi, blkaddr);
2289 se = get_seg_entry(sbi, segno);
2290 new_vblocks = se->valid_blocks + del;
2291 offset = GET_BLKOFF_FROM_SEG0(sbi, blkaddr);
2293 f2fs_bug_on(sbi, (new_vblocks < 0 ||
2294 (new_vblocks > f2fs_usable_blks_in_seg(sbi, segno))));
2296 se->valid_blocks = new_vblocks;
2298 /* Update valid block bitmap */
2300 exist = f2fs_test_and_set_bit(offset, se->cur_valid_map);
2301 #ifdef CONFIG_F2FS_CHECK_FS
2302 mir_exist = f2fs_test_and_set_bit(offset,
2303 se->cur_valid_map_mir);
2304 if (unlikely(exist != mir_exist)) {
2305 f2fs_err(sbi, "Inconsistent error when setting bitmap, blk:%u, old bit:%d",
2307 f2fs_bug_on(sbi, 1);
2310 if (unlikely(exist)) {
2311 f2fs_err(sbi, "Bitmap was wrongly set, blk:%u",
2313 f2fs_bug_on(sbi, 1);
2318 if (f2fs_block_unit_discard(sbi) &&
2319 !f2fs_test_and_set_bit(offset, se->discard_map))
2320 sbi->discard_blks--;
2323 * SSR should never reuse block which is checkpointed
2324 * or newly invalidated.
2326 if (!is_sbi_flag_set(sbi, SBI_CP_DISABLED)) {
2327 if (!f2fs_test_and_set_bit(offset, se->ckpt_valid_map))
2328 se->ckpt_valid_blocks++;
2331 exist = f2fs_test_and_clear_bit(offset, se->cur_valid_map);
2332 #ifdef CONFIG_F2FS_CHECK_FS
2333 mir_exist = f2fs_test_and_clear_bit(offset,
2334 se->cur_valid_map_mir);
2335 if (unlikely(exist != mir_exist)) {
2336 f2fs_err(sbi, "Inconsistent error when clearing bitmap, blk:%u, old bit:%d",
2338 f2fs_bug_on(sbi, 1);
2341 if (unlikely(!exist)) {
2342 f2fs_err(sbi, "Bitmap was wrongly cleared, blk:%u",
2344 f2fs_bug_on(sbi, 1);
2347 } else if (unlikely(is_sbi_flag_set(sbi, SBI_CP_DISABLED))) {
2349 * If checkpoints are off, we must not reuse data that
2350 * was used in the previous checkpoint. If it was used
2351 * before, we must track that to know how much space we
2354 if (f2fs_test_bit(offset, se->ckpt_valid_map)) {
2355 spin_lock(&sbi->stat_lock);
2356 sbi->unusable_block_count++;
2357 spin_unlock(&sbi->stat_lock);
2361 if (f2fs_block_unit_discard(sbi) &&
2362 f2fs_test_and_clear_bit(offset, se->discard_map))
2363 sbi->discard_blks++;
2365 if (!f2fs_test_bit(offset, se->ckpt_valid_map))
2366 se->ckpt_valid_blocks += del;
2368 __mark_sit_entry_dirty(sbi, segno);
2370 /* update total number of valid blocks to be written in ckpt area */
2371 SIT_I(sbi)->written_valid_blocks += del;
2373 if (__is_large_section(sbi))
2374 get_sec_entry(sbi, segno)->valid_blocks += del;
2377 void f2fs_invalidate_blocks(struct f2fs_sb_info *sbi, block_t addr)
2379 unsigned int segno = GET_SEGNO(sbi, addr);
2380 struct sit_info *sit_i = SIT_I(sbi);
2382 f2fs_bug_on(sbi, addr == NULL_ADDR);
2383 if (addr == NEW_ADDR || addr == COMPRESS_ADDR)
2386 invalidate_mapping_pages(META_MAPPING(sbi), addr, addr);
2387 f2fs_invalidate_compress_page(sbi, addr);
2389 /* add it into sit main buffer */
2390 down_write(&sit_i->sentry_lock);
2392 update_segment_mtime(sbi, addr, 0);
2393 update_sit_entry(sbi, addr, -1);
2395 /* add it into dirty seglist */
2396 locate_dirty_segment(sbi, segno);
2398 up_write(&sit_i->sentry_lock);
2401 bool f2fs_is_checkpointed_data(struct f2fs_sb_info *sbi, block_t blkaddr)
2403 struct sit_info *sit_i = SIT_I(sbi);
2404 unsigned int segno, offset;
2405 struct seg_entry *se;
2408 if (!__is_valid_data_blkaddr(blkaddr))
2411 down_read(&sit_i->sentry_lock);
2413 segno = GET_SEGNO(sbi, blkaddr);
2414 se = get_seg_entry(sbi, segno);
2415 offset = GET_BLKOFF_FROM_SEG0(sbi, blkaddr);
2417 if (f2fs_test_bit(offset, se->ckpt_valid_map))
2420 up_read(&sit_i->sentry_lock);
2426 * This function should be resided under the curseg_mutex lock
2428 static void __add_sum_entry(struct f2fs_sb_info *sbi, int type,
2429 struct f2fs_summary *sum)
2431 struct curseg_info *curseg = CURSEG_I(sbi, type);
2432 void *addr = curseg->sum_blk;
2434 addr += curseg->next_blkoff * sizeof(struct f2fs_summary);
2435 memcpy(addr, sum, sizeof(struct f2fs_summary));
2439 * Calculate the number of current summary pages for writing
2441 int f2fs_npages_for_summary_flush(struct f2fs_sb_info *sbi, bool for_ra)
2443 int valid_sum_count = 0;
2446 for (i = CURSEG_HOT_DATA; i <= CURSEG_COLD_DATA; i++) {
2447 if (sbi->ckpt->alloc_type[i] == SSR)
2448 valid_sum_count += sbi->blocks_per_seg;
2451 valid_sum_count += le16_to_cpu(
2452 F2FS_CKPT(sbi)->cur_data_blkoff[i]);
2454 valid_sum_count += curseg_blkoff(sbi, i);
2458 sum_in_page = (PAGE_SIZE - 2 * SUM_JOURNAL_SIZE -
2459 SUM_FOOTER_SIZE) / SUMMARY_SIZE;
2460 if (valid_sum_count <= sum_in_page)
2462 else if ((valid_sum_count - sum_in_page) <=
2463 (PAGE_SIZE - SUM_FOOTER_SIZE) / SUMMARY_SIZE)
2469 * Caller should put this summary page
2471 struct page *f2fs_get_sum_page(struct f2fs_sb_info *sbi, unsigned int segno)
2473 if (unlikely(f2fs_cp_error(sbi)))
2474 return ERR_PTR(-EIO);
2475 return f2fs_get_meta_page_retry(sbi, GET_SUM_BLOCK(sbi, segno));
2478 void f2fs_update_meta_page(struct f2fs_sb_info *sbi,
2479 void *src, block_t blk_addr)
2481 struct page *page = f2fs_grab_meta_page(sbi, blk_addr);
2483 memcpy(page_address(page), src, PAGE_SIZE);
2484 set_page_dirty(page);
2485 f2fs_put_page(page, 1);
2488 static void write_sum_page(struct f2fs_sb_info *sbi,
2489 struct f2fs_summary_block *sum_blk, block_t blk_addr)
2491 f2fs_update_meta_page(sbi, (void *)sum_blk, blk_addr);
2494 static void write_current_sum_page(struct f2fs_sb_info *sbi,
2495 int type, block_t blk_addr)
2497 struct curseg_info *curseg = CURSEG_I(sbi, type);
2498 struct page *page = f2fs_grab_meta_page(sbi, blk_addr);
2499 struct f2fs_summary_block *src = curseg->sum_blk;
2500 struct f2fs_summary_block *dst;
2502 dst = (struct f2fs_summary_block *)page_address(page);
2503 memset(dst, 0, PAGE_SIZE);
2505 mutex_lock(&curseg->curseg_mutex);
2507 down_read(&curseg->journal_rwsem);
2508 memcpy(&dst->journal, curseg->journal, SUM_JOURNAL_SIZE);
2509 up_read(&curseg->journal_rwsem);
2511 memcpy(dst->entries, src->entries, SUM_ENTRY_SIZE);
2512 memcpy(&dst->footer, &src->footer, SUM_FOOTER_SIZE);
2514 mutex_unlock(&curseg->curseg_mutex);
2516 set_page_dirty(page);
2517 f2fs_put_page(page, 1);
2520 static int is_next_segment_free(struct f2fs_sb_info *sbi,
2521 struct curseg_info *curseg, int type)
2523 unsigned int segno = curseg->segno + 1;
2524 struct free_segmap_info *free_i = FREE_I(sbi);
2526 if (segno < MAIN_SEGS(sbi) && segno % sbi->segs_per_sec)
2527 return !test_bit(segno, free_i->free_segmap);
2532 * Find a new segment from the free segments bitmap to right order
2533 * This function should be returned with success, otherwise BUG
2535 static void get_new_segment(struct f2fs_sb_info *sbi,
2536 unsigned int *newseg, bool new_sec, int dir)
2538 struct free_segmap_info *free_i = FREE_I(sbi);
2539 unsigned int segno, secno, zoneno;
2540 unsigned int total_zones = MAIN_SECS(sbi) / sbi->secs_per_zone;
2541 unsigned int hint = GET_SEC_FROM_SEG(sbi, *newseg);
2542 unsigned int old_zoneno = GET_ZONE_FROM_SEG(sbi, *newseg);
2543 unsigned int left_start = hint;
2548 spin_lock(&free_i->segmap_lock);
2550 if (!new_sec && ((*newseg + 1) % sbi->segs_per_sec)) {
2551 segno = find_next_zero_bit(free_i->free_segmap,
2552 GET_SEG_FROM_SEC(sbi, hint + 1), *newseg + 1);
2553 if (segno < GET_SEG_FROM_SEC(sbi, hint + 1))
2557 secno = find_next_zero_bit(free_i->free_secmap, MAIN_SECS(sbi), hint);
2558 if (secno >= MAIN_SECS(sbi)) {
2559 if (dir == ALLOC_RIGHT) {
2560 secno = find_first_zero_bit(free_i->free_secmap,
2562 f2fs_bug_on(sbi, secno >= MAIN_SECS(sbi));
2565 left_start = hint - 1;
2571 while (test_bit(left_start, free_i->free_secmap)) {
2572 if (left_start > 0) {
2576 left_start = find_first_zero_bit(free_i->free_secmap,
2578 f2fs_bug_on(sbi, left_start >= MAIN_SECS(sbi));
2583 segno = GET_SEG_FROM_SEC(sbi, secno);
2584 zoneno = GET_ZONE_FROM_SEC(sbi, secno);
2586 /* give up on finding another zone */
2589 if (sbi->secs_per_zone == 1)
2591 if (zoneno == old_zoneno)
2593 if (dir == ALLOC_LEFT) {
2594 if (!go_left && zoneno + 1 >= total_zones)
2596 if (go_left && zoneno == 0)
2599 for (i = 0; i < NR_CURSEG_TYPE; i++)
2600 if (CURSEG_I(sbi, i)->zone == zoneno)
2603 if (i < NR_CURSEG_TYPE) {
2604 /* zone is in user, try another */
2606 hint = zoneno * sbi->secs_per_zone - 1;
2607 else if (zoneno + 1 >= total_zones)
2610 hint = (zoneno + 1) * sbi->secs_per_zone;
2612 goto find_other_zone;
2615 /* set it as dirty segment in free segmap */
2616 f2fs_bug_on(sbi, test_bit(segno, free_i->free_segmap));
2617 __set_inuse(sbi, segno);
2619 spin_unlock(&free_i->segmap_lock);
2622 static void reset_curseg(struct f2fs_sb_info *sbi, int type, int modified)
2624 struct curseg_info *curseg = CURSEG_I(sbi, type);
2625 struct summary_footer *sum_footer;
2626 unsigned short seg_type = curseg->seg_type;
2628 curseg->inited = true;
2629 curseg->segno = curseg->next_segno;
2630 curseg->zone = GET_ZONE_FROM_SEG(sbi, curseg->segno);
2631 curseg->next_blkoff = 0;
2632 curseg->next_segno = NULL_SEGNO;
2634 sum_footer = &(curseg->sum_blk->footer);
2635 memset(sum_footer, 0, sizeof(struct summary_footer));
2637 sanity_check_seg_type(sbi, seg_type);
2639 if (IS_DATASEG(seg_type))
2640 SET_SUM_TYPE(sum_footer, SUM_TYPE_DATA);
2641 if (IS_NODESEG(seg_type))
2642 SET_SUM_TYPE(sum_footer, SUM_TYPE_NODE);
2643 __set_sit_entry_type(sbi, seg_type, curseg->segno, modified);
2646 static unsigned int __get_next_segno(struct f2fs_sb_info *sbi, int type)
2648 struct curseg_info *curseg = CURSEG_I(sbi, type);
2649 unsigned short seg_type = curseg->seg_type;
2651 sanity_check_seg_type(sbi, seg_type);
2652 if (f2fs_need_rand_seg(sbi))
2653 return prandom_u32() % (MAIN_SECS(sbi) * sbi->segs_per_sec);
2655 /* if segs_per_sec is large than 1, we need to keep original policy. */
2656 if (__is_large_section(sbi))
2657 return curseg->segno;
2659 /* inmem log may not locate on any segment after mount */
2660 if (!curseg->inited)
2663 if (unlikely(is_sbi_flag_set(sbi, SBI_CP_DISABLED)))
2666 if (test_opt(sbi, NOHEAP) &&
2667 (seg_type == CURSEG_HOT_DATA || IS_NODESEG(seg_type)))
2670 if (SIT_I(sbi)->last_victim[ALLOC_NEXT])
2671 return SIT_I(sbi)->last_victim[ALLOC_NEXT];
2673 /* find segments from 0 to reuse freed segments */
2674 if (F2FS_OPTION(sbi).alloc_mode == ALLOC_MODE_REUSE)
2677 return curseg->segno;
2681 * Allocate a current working segment.
2682 * This function always allocates a free segment in LFS manner.
2684 static void new_curseg(struct f2fs_sb_info *sbi, int type, bool new_sec)
2686 struct curseg_info *curseg = CURSEG_I(sbi, type);
2687 unsigned short seg_type = curseg->seg_type;
2688 unsigned int segno = curseg->segno;
2689 int dir = ALLOC_LEFT;
2692 write_sum_page(sbi, curseg->sum_blk,
2693 GET_SUM_BLOCK(sbi, segno));
2694 if (seg_type == CURSEG_WARM_DATA || seg_type == CURSEG_COLD_DATA)
2697 if (test_opt(sbi, NOHEAP))
2700 segno = __get_next_segno(sbi, type);
2701 get_new_segment(sbi, &segno, new_sec, dir);
2702 curseg->next_segno = segno;
2703 reset_curseg(sbi, type, 1);
2704 curseg->alloc_type = LFS;
2705 if (F2FS_OPTION(sbi).fs_mode == FS_MODE_FRAGMENT_BLK)
2706 curseg->fragment_remained_chunk =
2707 prandom_u32() % sbi->max_fragment_chunk + 1;
2710 static int __next_free_blkoff(struct f2fs_sb_info *sbi,
2711 int segno, block_t start)
2713 struct seg_entry *se = get_seg_entry(sbi, segno);
2714 int entries = SIT_VBLOCK_MAP_SIZE / sizeof(unsigned long);
2715 unsigned long *target_map = SIT_I(sbi)->tmp_map;
2716 unsigned long *ckpt_map = (unsigned long *)se->ckpt_valid_map;
2717 unsigned long *cur_map = (unsigned long *)se->cur_valid_map;
2720 for (i = 0; i < entries; i++)
2721 target_map[i] = ckpt_map[i] | cur_map[i];
2723 return __find_rev_next_zero_bit(target_map, sbi->blocks_per_seg, start);
2727 * If a segment is written by LFS manner, next block offset is just obtained
2728 * by increasing the current block offset. However, if a segment is written by
2729 * SSR manner, next block offset obtained by calling __next_free_blkoff
2731 static void __refresh_next_blkoff(struct f2fs_sb_info *sbi,
2732 struct curseg_info *seg)
2734 if (seg->alloc_type == SSR) {
2736 __next_free_blkoff(sbi, seg->segno,
2737 seg->next_blkoff + 1);
2740 if (F2FS_OPTION(sbi).fs_mode == FS_MODE_FRAGMENT_BLK) {
2741 /* To allocate block chunks in different sizes, use random number */
2742 if (--seg->fragment_remained_chunk <= 0) {
2743 seg->fragment_remained_chunk =
2744 prandom_u32() % sbi->max_fragment_chunk + 1;
2746 prandom_u32() % sbi->max_fragment_hole + 1;
2752 bool f2fs_segment_has_free_slot(struct f2fs_sb_info *sbi, int segno)
2754 return __next_free_blkoff(sbi, segno, 0) < sbi->blocks_per_seg;
2758 * This function always allocates a used segment(from dirty seglist) by SSR
2759 * manner, so it should recover the existing segment information of valid blocks
2761 static void change_curseg(struct f2fs_sb_info *sbi, int type, bool flush)
2763 struct dirty_seglist_info *dirty_i = DIRTY_I(sbi);
2764 struct curseg_info *curseg = CURSEG_I(sbi, type);
2765 unsigned int new_segno = curseg->next_segno;
2766 struct f2fs_summary_block *sum_node;
2767 struct page *sum_page;
2770 write_sum_page(sbi, curseg->sum_blk,
2771 GET_SUM_BLOCK(sbi, curseg->segno));
2773 __set_test_and_inuse(sbi, new_segno);
2775 mutex_lock(&dirty_i->seglist_lock);
2776 __remove_dirty_segment(sbi, new_segno, PRE);
2777 __remove_dirty_segment(sbi, new_segno, DIRTY);
2778 mutex_unlock(&dirty_i->seglist_lock);
2780 reset_curseg(sbi, type, 1);
2781 curseg->alloc_type = SSR;
2782 curseg->next_blkoff = __next_free_blkoff(sbi, curseg->segno, 0);
2784 sum_page = f2fs_get_sum_page(sbi, new_segno);
2785 if (IS_ERR(sum_page)) {
2786 /* GC won't be able to use stale summary pages by cp_error */
2787 memset(curseg->sum_blk, 0, SUM_ENTRY_SIZE);
2790 sum_node = (struct f2fs_summary_block *)page_address(sum_page);
2791 memcpy(curseg->sum_blk, sum_node, SUM_ENTRY_SIZE);
2792 f2fs_put_page(sum_page, 1);
2795 static int get_ssr_segment(struct f2fs_sb_info *sbi, int type,
2796 int alloc_mode, unsigned long long age);
2798 static void get_atssr_segment(struct f2fs_sb_info *sbi, int type,
2799 int target_type, int alloc_mode,
2800 unsigned long long age)
2802 struct curseg_info *curseg = CURSEG_I(sbi, type);
2804 curseg->seg_type = target_type;
2806 if (get_ssr_segment(sbi, type, alloc_mode, age)) {
2807 struct seg_entry *se = get_seg_entry(sbi, curseg->next_segno);
2809 curseg->seg_type = se->type;
2810 change_curseg(sbi, type, true);
2812 /* allocate cold segment by default */
2813 curseg->seg_type = CURSEG_COLD_DATA;
2814 new_curseg(sbi, type, true);
2816 stat_inc_seg_type(sbi, curseg);
2819 static void __f2fs_init_atgc_curseg(struct f2fs_sb_info *sbi)
2821 struct curseg_info *curseg = CURSEG_I(sbi, CURSEG_ALL_DATA_ATGC);
2823 if (!sbi->am.atgc_enabled)
2826 f2fs_down_read(&SM_I(sbi)->curseg_lock);
2828 mutex_lock(&curseg->curseg_mutex);
2829 down_write(&SIT_I(sbi)->sentry_lock);
2831 get_atssr_segment(sbi, CURSEG_ALL_DATA_ATGC, CURSEG_COLD_DATA, SSR, 0);
2833 up_write(&SIT_I(sbi)->sentry_lock);
2834 mutex_unlock(&curseg->curseg_mutex);
2836 f2fs_up_read(&SM_I(sbi)->curseg_lock);
2839 void f2fs_init_inmem_curseg(struct f2fs_sb_info *sbi)
2841 __f2fs_init_atgc_curseg(sbi);
2844 static void __f2fs_save_inmem_curseg(struct f2fs_sb_info *sbi, int type)
2846 struct curseg_info *curseg = CURSEG_I(sbi, type);
2848 mutex_lock(&curseg->curseg_mutex);
2849 if (!curseg->inited)
2852 if (get_valid_blocks(sbi, curseg->segno, false)) {
2853 write_sum_page(sbi, curseg->sum_blk,
2854 GET_SUM_BLOCK(sbi, curseg->segno));
2856 mutex_lock(&DIRTY_I(sbi)->seglist_lock);
2857 __set_test_and_free(sbi, curseg->segno, true);
2858 mutex_unlock(&DIRTY_I(sbi)->seglist_lock);
2861 mutex_unlock(&curseg->curseg_mutex);
2864 void f2fs_save_inmem_curseg(struct f2fs_sb_info *sbi)
2866 __f2fs_save_inmem_curseg(sbi, CURSEG_COLD_DATA_PINNED);
2868 if (sbi->am.atgc_enabled)
2869 __f2fs_save_inmem_curseg(sbi, CURSEG_ALL_DATA_ATGC);
2872 static void __f2fs_restore_inmem_curseg(struct f2fs_sb_info *sbi, int type)
2874 struct curseg_info *curseg = CURSEG_I(sbi, type);
2876 mutex_lock(&curseg->curseg_mutex);
2877 if (!curseg->inited)
2879 if (get_valid_blocks(sbi, curseg->segno, false))
2882 mutex_lock(&DIRTY_I(sbi)->seglist_lock);
2883 __set_test_and_inuse(sbi, curseg->segno);
2884 mutex_unlock(&DIRTY_I(sbi)->seglist_lock);
2886 mutex_unlock(&curseg->curseg_mutex);
2889 void f2fs_restore_inmem_curseg(struct f2fs_sb_info *sbi)
2891 __f2fs_restore_inmem_curseg(sbi, CURSEG_COLD_DATA_PINNED);
2893 if (sbi->am.atgc_enabled)
2894 __f2fs_restore_inmem_curseg(sbi, CURSEG_ALL_DATA_ATGC);
2897 static int get_ssr_segment(struct f2fs_sb_info *sbi, int type,
2898 int alloc_mode, unsigned long long age)
2900 struct curseg_info *curseg = CURSEG_I(sbi, type);
2901 const struct victim_selection *v_ops = DIRTY_I(sbi)->v_ops;
2902 unsigned segno = NULL_SEGNO;
2903 unsigned short seg_type = curseg->seg_type;
2905 bool reversed = false;
2907 sanity_check_seg_type(sbi, seg_type);
2909 /* f2fs_need_SSR() already forces to do this */
2910 if (!v_ops->get_victim(sbi, &segno, BG_GC, seg_type, alloc_mode, age)) {
2911 curseg->next_segno = segno;
2915 /* For node segments, let's do SSR more intensively */
2916 if (IS_NODESEG(seg_type)) {
2917 if (seg_type >= CURSEG_WARM_NODE) {
2919 i = CURSEG_COLD_NODE;
2921 i = CURSEG_HOT_NODE;
2923 cnt = NR_CURSEG_NODE_TYPE;
2925 if (seg_type >= CURSEG_WARM_DATA) {
2927 i = CURSEG_COLD_DATA;
2929 i = CURSEG_HOT_DATA;
2931 cnt = NR_CURSEG_DATA_TYPE;
2934 for (; cnt-- > 0; reversed ? i-- : i++) {
2937 if (!v_ops->get_victim(sbi, &segno, BG_GC, i, alloc_mode, age)) {
2938 curseg->next_segno = segno;
2943 /* find valid_blocks=0 in dirty list */
2944 if (unlikely(is_sbi_flag_set(sbi, SBI_CP_DISABLED))) {
2945 segno = get_free_segment(sbi);
2946 if (segno != NULL_SEGNO) {
2947 curseg->next_segno = segno;
2955 * flush out current segment and replace it with new segment
2956 * This function should be returned with success, otherwise BUG
2958 static void allocate_segment_by_default(struct f2fs_sb_info *sbi,
2959 int type, bool force)
2961 struct curseg_info *curseg = CURSEG_I(sbi, type);
2964 new_curseg(sbi, type, true);
2965 else if (!is_set_ckpt_flags(sbi, CP_CRC_RECOVERY_FLAG) &&
2966 curseg->seg_type == CURSEG_WARM_NODE)
2967 new_curseg(sbi, type, false);
2968 else if (curseg->alloc_type == LFS &&
2969 is_next_segment_free(sbi, curseg, type) &&
2970 likely(!is_sbi_flag_set(sbi, SBI_CP_DISABLED)))
2971 new_curseg(sbi, type, false);
2972 else if (f2fs_need_SSR(sbi) &&
2973 get_ssr_segment(sbi, type, SSR, 0))
2974 change_curseg(sbi, type, true);
2976 new_curseg(sbi, type, false);
2978 stat_inc_seg_type(sbi, curseg);
2981 void f2fs_allocate_segment_for_resize(struct f2fs_sb_info *sbi, int type,
2982 unsigned int start, unsigned int end)
2984 struct curseg_info *curseg = CURSEG_I(sbi, type);
2987 f2fs_down_read(&SM_I(sbi)->curseg_lock);
2988 mutex_lock(&curseg->curseg_mutex);
2989 down_write(&SIT_I(sbi)->sentry_lock);
2991 segno = CURSEG_I(sbi, type)->segno;
2992 if (segno < start || segno > end)
2995 if (f2fs_need_SSR(sbi) && get_ssr_segment(sbi, type, SSR, 0))
2996 change_curseg(sbi, type, true);
2998 new_curseg(sbi, type, true);
3000 stat_inc_seg_type(sbi, curseg);
3002 locate_dirty_segment(sbi, segno);
3004 up_write(&SIT_I(sbi)->sentry_lock);
3006 if (segno != curseg->segno)
3007 f2fs_notice(sbi, "For resize: curseg of type %d: %u ==> %u",
3008 type, segno, curseg->segno);
3010 mutex_unlock(&curseg->curseg_mutex);
3011 f2fs_up_read(&SM_I(sbi)->curseg_lock);
3014 static void __allocate_new_segment(struct f2fs_sb_info *sbi, int type,
3015 bool new_sec, bool force)
3017 struct curseg_info *curseg = CURSEG_I(sbi, type);
3018 unsigned int old_segno;
3020 if (!curseg->inited)
3023 if (force || curseg->next_blkoff ||
3024 get_valid_blocks(sbi, curseg->segno, new_sec))
3027 if (!get_ckpt_valid_blocks(sbi, curseg->segno, new_sec))
3030 old_segno = curseg->segno;
3031 SIT_I(sbi)->s_ops->allocate_segment(sbi, type, true);
3032 locate_dirty_segment(sbi, old_segno);
3035 static void __allocate_new_section(struct f2fs_sb_info *sbi,
3036 int type, bool force)
3038 __allocate_new_segment(sbi, type, true, force);
3041 void f2fs_allocate_new_section(struct f2fs_sb_info *sbi, int type, bool force)
3043 f2fs_down_read(&SM_I(sbi)->curseg_lock);
3044 down_write(&SIT_I(sbi)->sentry_lock);
3045 __allocate_new_section(sbi, type, force);
3046 up_write(&SIT_I(sbi)->sentry_lock);
3047 f2fs_up_read(&SM_I(sbi)->curseg_lock);
3050 void f2fs_allocate_new_segments(struct f2fs_sb_info *sbi)
3054 f2fs_down_read(&SM_I(sbi)->curseg_lock);
3055 down_write(&SIT_I(sbi)->sentry_lock);
3056 for (i = CURSEG_HOT_DATA; i <= CURSEG_COLD_DATA; i++)
3057 __allocate_new_segment(sbi, i, false, false);
3058 up_write(&SIT_I(sbi)->sentry_lock);
3059 f2fs_up_read(&SM_I(sbi)->curseg_lock);
3062 static const struct segment_allocation default_salloc_ops = {
3063 .allocate_segment = allocate_segment_by_default,
3066 bool f2fs_exist_trim_candidates(struct f2fs_sb_info *sbi,
3067 struct cp_control *cpc)
3069 __u64 trim_start = cpc->trim_start;
3070 bool has_candidate = false;
3072 down_write(&SIT_I(sbi)->sentry_lock);
3073 for (; cpc->trim_start <= cpc->trim_end; cpc->trim_start++) {
3074 if (add_discard_addrs(sbi, cpc, true)) {
3075 has_candidate = true;
3079 up_write(&SIT_I(sbi)->sentry_lock);
3081 cpc->trim_start = trim_start;
3082 return has_candidate;
3085 static unsigned int __issue_discard_cmd_range(struct f2fs_sb_info *sbi,
3086 struct discard_policy *dpolicy,
3087 unsigned int start, unsigned int end)
3089 struct discard_cmd_control *dcc = SM_I(sbi)->dcc_info;
3090 struct discard_cmd *prev_dc = NULL, *next_dc = NULL;
3091 struct rb_node **insert_p = NULL, *insert_parent = NULL;
3092 struct discard_cmd *dc;
3093 struct blk_plug plug;
3095 unsigned int trimmed = 0;
3100 mutex_lock(&dcc->cmd_lock);
3101 if (unlikely(dcc->rbtree_check))
3102 f2fs_bug_on(sbi, !f2fs_check_rb_tree_consistence(sbi,
3103 &dcc->root, false));
3105 dc = (struct discard_cmd *)f2fs_lookup_rb_tree_ret(&dcc->root,
3107 (struct rb_entry **)&prev_dc,
3108 (struct rb_entry **)&next_dc,
3109 &insert_p, &insert_parent, true, NULL);
3113 blk_start_plug(&plug);
3115 while (dc && dc->lstart <= end) {
3116 struct rb_node *node;
3119 if (dc->len < dpolicy->granularity)
3122 if (dc->state != D_PREP) {
3123 list_move_tail(&dc->list, &dcc->fstrim_list);
3127 err = __submit_discard_cmd(sbi, dpolicy, dc, &issued);
3129 if (issued >= dpolicy->max_requests) {
3130 start = dc->lstart + dc->len;
3133 __remove_discard_cmd(sbi, dc);
3135 blk_finish_plug(&plug);
3136 mutex_unlock(&dcc->cmd_lock);
3137 trimmed += __wait_all_discard_cmd(sbi, NULL);
3138 f2fs_io_schedule_timeout(DEFAULT_IO_TIMEOUT);
3142 node = rb_next(&dc->rb_node);
3144 __remove_discard_cmd(sbi, dc);
3145 dc = rb_entry_safe(node, struct discard_cmd, rb_node);
3147 if (fatal_signal_pending(current))
3151 blk_finish_plug(&plug);
3152 mutex_unlock(&dcc->cmd_lock);
3157 int f2fs_trim_fs(struct f2fs_sb_info *sbi, struct fstrim_range *range)
3159 __u64 start = F2FS_BYTES_TO_BLK(range->start);
3160 __u64 end = start + F2FS_BYTES_TO_BLK(range->len) - 1;
3161 unsigned int start_segno, end_segno;
3162 block_t start_block, end_block;
3163 struct cp_control cpc;
3164 struct discard_policy dpolicy;
3165 unsigned long long trimmed = 0;
3167 bool need_align = f2fs_lfs_mode(sbi) && __is_large_section(sbi);
3169 if (start >= MAX_BLKADDR(sbi) || range->len < sbi->blocksize)
3172 if (end < MAIN_BLKADDR(sbi))
3175 if (is_sbi_flag_set(sbi, SBI_NEED_FSCK)) {
3176 f2fs_warn(sbi, "Found FS corruption, run fsck to fix.");
3177 return -EFSCORRUPTED;
3180 /* start/end segment number in main_area */
3181 start_segno = (start <= MAIN_BLKADDR(sbi)) ? 0 : GET_SEGNO(sbi, start);
3182 end_segno = (end >= MAX_BLKADDR(sbi)) ? MAIN_SEGS(sbi) - 1 :
3183 GET_SEGNO(sbi, end);
3185 start_segno = rounddown(start_segno, sbi->segs_per_sec);
3186 end_segno = roundup(end_segno + 1, sbi->segs_per_sec) - 1;
3189 cpc.reason = CP_DISCARD;
3190 cpc.trim_minlen = max_t(__u64, 1, F2FS_BYTES_TO_BLK(range->minlen));
3191 cpc.trim_start = start_segno;
3192 cpc.trim_end = end_segno;
3194 if (sbi->discard_blks == 0)
3197 f2fs_down_write(&sbi->gc_lock);
3198 err = f2fs_write_checkpoint(sbi, &cpc);
3199 f2fs_up_write(&sbi->gc_lock);
3204 * We filed discard candidates, but actually we don't need to wait for
3205 * all of them, since they'll be issued in idle time along with runtime
3206 * discard option. User configuration looks like using runtime discard
3207 * or periodic fstrim instead of it.
3209 if (f2fs_realtime_discard_enable(sbi))
3212 start_block = START_BLOCK(sbi, start_segno);
3213 end_block = START_BLOCK(sbi, end_segno + 1);
3215 __init_discard_policy(sbi, &dpolicy, DPOLICY_FSTRIM, cpc.trim_minlen);
3216 trimmed = __issue_discard_cmd_range(sbi, &dpolicy,
3217 start_block, end_block);
3219 trimmed += __wait_discard_cmd_range(sbi, &dpolicy,
3220 start_block, end_block);
3223 range->len = F2FS_BLK_TO_BYTES(trimmed);
3227 static bool __has_curseg_space(struct f2fs_sb_info *sbi,
3228 struct curseg_info *curseg)
3230 return curseg->next_blkoff < f2fs_usable_blks_in_seg(sbi,
3234 int f2fs_rw_hint_to_seg_type(enum rw_hint hint)
3237 case WRITE_LIFE_SHORT:
3238 return CURSEG_HOT_DATA;
3239 case WRITE_LIFE_EXTREME:
3240 return CURSEG_COLD_DATA;
3242 return CURSEG_WARM_DATA;
3246 static int __get_segment_type_2(struct f2fs_io_info *fio)
3248 if (fio->type == DATA)
3249 return CURSEG_HOT_DATA;
3251 return CURSEG_HOT_NODE;
3254 static int __get_segment_type_4(struct f2fs_io_info *fio)
3256 if (fio->type == DATA) {
3257 struct inode *inode = fio->page->mapping->host;
3259 if (S_ISDIR(inode->i_mode))
3260 return CURSEG_HOT_DATA;
3262 return CURSEG_COLD_DATA;
3264 if (IS_DNODE(fio->page) && is_cold_node(fio->page))
3265 return CURSEG_WARM_NODE;
3267 return CURSEG_COLD_NODE;
3271 static int __get_segment_type_6(struct f2fs_io_info *fio)
3273 if (fio->type == DATA) {
3274 struct inode *inode = fio->page->mapping->host;
3276 if (is_inode_flag_set(inode, FI_ALIGNED_WRITE))
3277 return CURSEG_COLD_DATA_PINNED;
3279 if (page_private_gcing(fio->page)) {
3280 if (fio->sbi->am.atgc_enabled &&
3281 (fio->io_type == FS_DATA_IO) &&
3282 (fio->sbi->gc_mode != GC_URGENT_HIGH))
3283 return CURSEG_ALL_DATA_ATGC;
3285 return CURSEG_COLD_DATA;
3287 if (file_is_cold(inode) || f2fs_need_compress_data(inode))
3288 return CURSEG_COLD_DATA;
3289 if (file_is_hot(inode) ||
3290 is_inode_flag_set(inode, FI_HOT_DATA) ||
3291 f2fs_is_atomic_file(inode) ||
3292 f2fs_is_volatile_file(inode))
3293 return CURSEG_HOT_DATA;
3294 return f2fs_rw_hint_to_seg_type(inode->i_write_hint);
3296 if (IS_DNODE(fio->page))
3297 return is_cold_node(fio->page) ? CURSEG_WARM_NODE :
3299 return CURSEG_COLD_NODE;
3303 static int __get_segment_type(struct f2fs_io_info *fio)
3307 switch (F2FS_OPTION(fio->sbi).active_logs) {
3309 type = __get_segment_type_2(fio);
3312 type = __get_segment_type_4(fio);
3315 type = __get_segment_type_6(fio);
3318 f2fs_bug_on(fio->sbi, true);
3323 else if (IS_WARM(type))
3330 void f2fs_allocate_data_block(struct f2fs_sb_info *sbi, struct page *page,
3331 block_t old_blkaddr, block_t *new_blkaddr,
3332 struct f2fs_summary *sum, int type,
3333 struct f2fs_io_info *fio)
3335 struct sit_info *sit_i = SIT_I(sbi);
3336 struct curseg_info *curseg = CURSEG_I(sbi, type);
3337 unsigned long long old_mtime;
3338 bool from_gc = (type == CURSEG_ALL_DATA_ATGC);
3339 struct seg_entry *se = NULL;
3341 f2fs_down_read(&SM_I(sbi)->curseg_lock);
3343 mutex_lock(&curseg->curseg_mutex);
3344 down_write(&sit_i->sentry_lock);
3347 f2fs_bug_on(sbi, GET_SEGNO(sbi, old_blkaddr) == NULL_SEGNO);
3348 se = get_seg_entry(sbi, GET_SEGNO(sbi, old_blkaddr));
3349 sanity_check_seg_type(sbi, se->type);
3350 f2fs_bug_on(sbi, IS_NODESEG(se->type));
3352 *new_blkaddr = NEXT_FREE_BLKADDR(sbi, curseg);
3354 f2fs_bug_on(sbi, curseg->next_blkoff >= sbi->blocks_per_seg);
3356 f2fs_wait_discard_bio(sbi, *new_blkaddr);
3359 * __add_sum_entry should be resided under the curseg_mutex
3360 * because, this function updates a summary entry in the
3361 * current summary block.
3363 __add_sum_entry(sbi, type, sum);
3365 __refresh_next_blkoff(sbi, curseg);
3367 stat_inc_block_count(sbi, curseg);
3370 old_mtime = get_segment_mtime(sbi, old_blkaddr);
3372 update_segment_mtime(sbi, old_blkaddr, 0);
3375 update_segment_mtime(sbi, *new_blkaddr, old_mtime);
3378 * SIT information should be updated before segment allocation,
3379 * since SSR needs latest valid block information.
3381 update_sit_entry(sbi, *new_blkaddr, 1);
3382 if (GET_SEGNO(sbi, old_blkaddr) != NULL_SEGNO)
3383 update_sit_entry(sbi, old_blkaddr, -1);
3385 if (!__has_curseg_space(sbi, curseg)) {
3387 get_atssr_segment(sbi, type, se->type,
3390 sit_i->s_ops->allocate_segment(sbi, type, false);
3393 * segment dirty status should be updated after segment allocation,
3394 * so we just need to update status only one time after previous
3395 * segment being closed.
3397 locate_dirty_segment(sbi, GET_SEGNO(sbi, old_blkaddr));
3398 locate_dirty_segment(sbi, GET_SEGNO(sbi, *new_blkaddr));
3400 up_write(&sit_i->sentry_lock);
3402 if (page && IS_NODESEG(type)) {
3403 fill_node_footer_blkaddr(page, NEXT_FREE_BLKADDR(sbi, curseg));
3405 f2fs_inode_chksum_set(sbi, page);
3409 struct f2fs_bio_info *io;
3411 if (F2FS_IO_ALIGNED(sbi))
3414 INIT_LIST_HEAD(&fio->list);
3415 fio->in_list = true;
3416 io = sbi->write_io[fio->type] + fio->temp;
3417 spin_lock(&io->io_lock);
3418 list_add_tail(&fio->list, &io->io_list);
3419 spin_unlock(&io->io_lock);
3422 mutex_unlock(&curseg->curseg_mutex);
3424 f2fs_up_read(&SM_I(sbi)->curseg_lock);
3427 void f2fs_update_device_state(struct f2fs_sb_info *sbi, nid_t ino,
3428 block_t blkaddr, unsigned int blkcnt)
3430 if (!f2fs_is_multi_device(sbi))
3434 unsigned int devidx = f2fs_target_device_index(sbi, blkaddr);
3435 unsigned int blks = FDEV(devidx).end_blk - blkaddr + 1;
3437 /* update device state for fsync */
3438 f2fs_set_dirty_device(sbi, ino, devidx, FLUSH_INO);
3440 /* update device state for checkpoint */
3441 if (!f2fs_test_bit(devidx, (char *)&sbi->dirty_device)) {
3442 spin_lock(&sbi->dev_lock);
3443 f2fs_set_bit(devidx, (char *)&sbi->dirty_device);
3444 spin_unlock(&sbi->dev_lock);
3454 static void do_write_page(struct f2fs_summary *sum, struct f2fs_io_info *fio)
3456 int type = __get_segment_type(fio);
3457 bool keep_order = (f2fs_lfs_mode(fio->sbi) && type == CURSEG_COLD_DATA);
3460 f2fs_down_read(&fio->sbi->io_order_lock);
3462 f2fs_allocate_data_block(fio->sbi, fio->page, fio->old_blkaddr,
3463 &fio->new_blkaddr, sum, type, fio);
3464 if (GET_SEGNO(fio->sbi, fio->old_blkaddr) != NULL_SEGNO) {
3465 invalidate_mapping_pages(META_MAPPING(fio->sbi),
3466 fio->old_blkaddr, fio->old_blkaddr);
3467 f2fs_invalidate_compress_page(fio->sbi, fio->old_blkaddr);
3470 /* writeout dirty page into bdev */
3471 f2fs_submit_page_write(fio);
3473 fio->old_blkaddr = fio->new_blkaddr;
3477 f2fs_update_device_state(fio->sbi, fio->ino, fio->new_blkaddr, 1);
3480 f2fs_up_read(&fio->sbi->io_order_lock);
3483 void f2fs_do_write_meta_page(struct f2fs_sb_info *sbi, struct page *page,
3484 enum iostat_type io_type)
3486 struct f2fs_io_info fio = {
3491 .op_flags = REQ_SYNC | REQ_META | REQ_PRIO,
3492 .old_blkaddr = page->index,
3493 .new_blkaddr = page->index,
3495 .encrypted_page = NULL,
3499 if (unlikely(page->index >= MAIN_BLKADDR(sbi)))
3500 fio.op_flags &= ~REQ_META;
3502 set_page_writeback(page);
3503 ClearPageError(page);
3504 f2fs_submit_page_write(&fio);
3506 stat_inc_meta_count(sbi, page->index);
3507 f2fs_update_iostat(sbi, io_type, F2FS_BLKSIZE);
3510 void f2fs_do_write_node_page(unsigned int nid, struct f2fs_io_info *fio)
3512 struct f2fs_summary sum;
3514 set_summary(&sum, nid, 0, 0);
3515 do_write_page(&sum, fio);
3517 f2fs_update_iostat(fio->sbi, fio->io_type, F2FS_BLKSIZE);
3520 void f2fs_outplace_write_data(struct dnode_of_data *dn,
3521 struct f2fs_io_info *fio)
3523 struct f2fs_sb_info *sbi = fio->sbi;
3524 struct f2fs_summary sum;
3526 f2fs_bug_on(sbi, dn->data_blkaddr == NULL_ADDR);
3527 set_summary(&sum, dn->nid, dn->ofs_in_node, fio->version);
3528 do_write_page(&sum, fio);
3529 f2fs_update_data_blkaddr(dn, fio->new_blkaddr);
3531 f2fs_update_iostat(sbi, fio->io_type, F2FS_BLKSIZE);
3534 int f2fs_inplace_write_data(struct f2fs_io_info *fio)
3537 struct f2fs_sb_info *sbi = fio->sbi;
3540 fio->new_blkaddr = fio->old_blkaddr;
3541 /* i/o temperature is needed for passing down write hints */
3542 __get_segment_type(fio);
3544 segno = GET_SEGNO(sbi, fio->new_blkaddr);
3546 if (!IS_DATASEG(get_seg_entry(sbi, segno)->type)) {
3547 set_sbi_flag(sbi, SBI_NEED_FSCK);
3548 f2fs_warn(sbi, "%s: incorrect segment(%u) type, run fsck to fix.",
3550 err = -EFSCORRUPTED;
3554 if (f2fs_cp_error(sbi)) {
3559 invalidate_mapping_pages(META_MAPPING(sbi),
3560 fio->new_blkaddr, fio->new_blkaddr);
3562 stat_inc_inplace_blocks(fio->sbi);
3564 if (fio->bio && !(SM_I(sbi)->ipu_policy & (1 << F2FS_IPU_NOCACHE)))
3565 err = f2fs_merge_page_bio(fio);
3567 err = f2fs_submit_page_bio(fio);
3569 f2fs_update_device_state(fio->sbi, fio->ino,
3570 fio->new_blkaddr, 1);
3571 f2fs_update_iostat(fio->sbi, fio->io_type, F2FS_BLKSIZE);
3576 if (fio->bio && *(fio->bio)) {
3577 struct bio *bio = *(fio->bio);
3579 bio->bi_status = BLK_STS_IOERR;
3586 static inline int __f2fs_get_curseg(struct f2fs_sb_info *sbi,
3591 for (i = CURSEG_HOT_DATA; i < NO_CHECK_TYPE; i++) {
3592 if (CURSEG_I(sbi, i)->segno == segno)
3598 void f2fs_do_replace_block(struct f2fs_sb_info *sbi, struct f2fs_summary *sum,
3599 block_t old_blkaddr, block_t new_blkaddr,
3600 bool recover_curseg, bool recover_newaddr,
3603 struct sit_info *sit_i = SIT_I(sbi);
3604 struct curseg_info *curseg;
3605 unsigned int segno, old_cursegno;
3606 struct seg_entry *se;
3608 unsigned short old_blkoff;
3609 unsigned char old_alloc_type;
3611 segno = GET_SEGNO(sbi, new_blkaddr);
3612 se = get_seg_entry(sbi, segno);
3615 f2fs_down_write(&SM_I(sbi)->curseg_lock);
3617 if (!recover_curseg) {
3618 /* for recovery flow */
3619 if (se->valid_blocks == 0 && !IS_CURSEG(sbi, segno)) {
3620 if (old_blkaddr == NULL_ADDR)
3621 type = CURSEG_COLD_DATA;
3623 type = CURSEG_WARM_DATA;
3626 if (IS_CURSEG(sbi, segno)) {
3627 /* se->type is volatile as SSR allocation */
3628 type = __f2fs_get_curseg(sbi, segno);
3629 f2fs_bug_on(sbi, type == NO_CHECK_TYPE);
3631 type = CURSEG_WARM_DATA;
3635 f2fs_bug_on(sbi, !IS_DATASEG(type));
3636 curseg = CURSEG_I(sbi, type);
3638 mutex_lock(&curseg->curseg_mutex);
3639 down_write(&sit_i->sentry_lock);
3641 old_cursegno = curseg->segno;
3642 old_blkoff = curseg->next_blkoff;
3643 old_alloc_type = curseg->alloc_type;
3645 /* change the current segment */
3646 if (segno != curseg->segno) {
3647 curseg->next_segno = segno;
3648 change_curseg(sbi, type, true);
3651 curseg->next_blkoff = GET_BLKOFF_FROM_SEG0(sbi, new_blkaddr);
3652 __add_sum_entry(sbi, type, sum);
3654 if (!recover_curseg || recover_newaddr) {
3656 update_segment_mtime(sbi, new_blkaddr, 0);
3657 update_sit_entry(sbi, new_blkaddr, 1);
3659 if (GET_SEGNO(sbi, old_blkaddr) != NULL_SEGNO) {
3660 invalidate_mapping_pages(META_MAPPING(sbi),
3661 old_blkaddr, old_blkaddr);
3662 f2fs_invalidate_compress_page(sbi, old_blkaddr);
3664 update_segment_mtime(sbi, old_blkaddr, 0);
3665 update_sit_entry(sbi, old_blkaddr, -1);
3668 locate_dirty_segment(sbi, GET_SEGNO(sbi, old_blkaddr));
3669 locate_dirty_segment(sbi, GET_SEGNO(sbi, new_blkaddr));
3671 locate_dirty_segment(sbi, old_cursegno);
3673 if (recover_curseg) {
3674 if (old_cursegno != curseg->segno) {
3675 curseg->next_segno = old_cursegno;
3676 change_curseg(sbi, type, true);
3678 curseg->next_blkoff = old_blkoff;
3679 curseg->alloc_type = old_alloc_type;
3682 up_write(&sit_i->sentry_lock);
3683 mutex_unlock(&curseg->curseg_mutex);
3684 f2fs_up_write(&SM_I(sbi)->curseg_lock);
3687 void f2fs_replace_block(struct f2fs_sb_info *sbi, struct dnode_of_data *dn,
3688 block_t old_addr, block_t new_addr,
3689 unsigned char version, bool recover_curseg,
3690 bool recover_newaddr)
3692 struct f2fs_summary sum;
3694 set_summary(&sum, dn->nid, dn->ofs_in_node, version);
3696 f2fs_do_replace_block(sbi, &sum, old_addr, new_addr,
3697 recover_curseg, recover_newaddr, false);
3699 f2fs_update_data_blkaddr(dn, new_addr);
3702 void f2fs_wait_on_page_writeback(struct page *page,
3703 enum page_type type, bool ordered, bool locked)
3705 if (PageWriteback(page)) {
3706 struct f2fs_sb_info *sbi = F2FS_P_SB(page);
3708 /* submit cached LFS IO */
3709 f2fs_submit_merged_write_cond(sbi, NULL, page, 0, type);
3710 /* sbumit cached IPU IO */
3711 f2fs_submit_merged_ipu_write(sbi, NULL, page);
3713 wait_on_page_writeback(page);
3714 f2fs_bug_on(sbi, locked && PageWriteback(page));
3716 wait_for_stable_page(page);
3721 void f2fs_wait_on_block_writeback(struct inode *inode, block_t blkaddr)
3723 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
3726 if (!f2fs_post_read_required(inode))
3729 if (!__is_valid_data_blkaddr(blkaddr))
3732 cpage = find_lock_page(META_MAPPING(sbi), blkaddr);
3734 f2fs_wait_on_page_writeback(cpage, DATA, true, true);
3735 f2fs_put_page(cpage, 1);
3739 void f2fs_wait_on_block_writeback_range(struct inode *inode, block_t blkaddr,
3744 for (i = 0; i < len; i++)
3745 f2fs_wait_on_block_writeback(inode, blkaddr + i);
3748 static int read_compacted_summaries(struct f2fs_sb_info *sbi)
3750 struct f2fs_checkpoint *ckpt = F2FS_CKPT(sbi);
3751 struct curseg_info *seg_i;
3752 unsigned char *kaddr;
3757 start = start_sum_block(sbi);
3759 page = f2fs_get_meta_page(sbi, start++);
3761 return PTR_ERR(page);
3762 kaddr = (unsigned char *)page_address(page);
3764 /* Step 1: restore nat cache */
3765 seg_i = CURSEG_I(sbi, CURSEG_HOT_DATA);
3766 memcpy(seg_i->journal, kaddr, SUM_JOURNAL_SIZE);
3768 /* Step 2: restore sit cache */
3769 seg_i = CURSEG_I(sbi, CURSEG_COLD_DATA);
3770 memcpy(seg_i->journal, kaddr + SUM_JOURNAL_SIZE, SUM_JOURNAL_SIZE);
3771 offset = 2 * SUM_JOURNAL_SIZE;
3773 /* Step 3: restore summary entries */
3774 for (i = CURSEG_HOT_DATA; i <= CURSEG_COLD_DATA; i++) {
3775 unsigned short blk_off;
3778 seg_i = CURSEG_I(sbi, i);
3779 segno = le32_to_cpu(ckpt->cur_data_segno[i]);
3780 blk_off = le16_to_cpu(ckpt->cur_data_blkoff[i]);
3781 seg_i->next_segno = segno;
3782 reset_curseg(sbi, i, 0);
3783 seg_i->alloc_type = ckpt->alloc_type[i];
3784 seg_i->next_blkoff = blk_off;
3786 if (seg_i->alloc_type == SSR)
3787 blk_off = sbi->blocks_per_seg;
3789 for (j = 0; j < blk_off; j++) {
3790 struct f2fs_summary *s;
3792 s = (struct f2fs_summary *)(kaddr + offset);
3793 seg_i->sum_blk->entries[j] = *s;
3794 offset += SUMMARY_SIZE;
3795 if (offset + SUMMARY_SIZE <= PAGE_SIZE -
3799 f2fs_put_page(page, 1);
3802 page = f2fs_get_meta_page(sbi, start++);
3804 return PTR_ERR(page);
3805 kaddr = (unsigned char *)page_address(page);
3809 f2fs_put_page(page, 1);
3813 static int read_normal_summaries(struct f2fs_sb_info *sbi, int type)
3815 struct f2fs_checkpoint *ckpt = F2FS_CKPT(sbi);
3816 struct f2fs_summary_block *sum;
3817 struct curseg_info *curseg;
3819 unsigned short blk_off;
3820 unsigned int segno = 0;
3821 block_t blk_addr = 0;
3824 /* get segment number and block addr */
3825 if (IS_DATASEG(type)) {
3826 segno = le32_to_cpu(ckpt->cur_data_segno[type]);
3827 blk_off = le16_to_cpu(ckpt->cur_data_blkoff[type -
3829 if (__exist_node_summaries(sbi))
3830 blk_addr = sum_blk_addr(sbi, NR_CURSEG_PERSIST_TYPE, type);
3832 blk_addr = sum_blk_addr(sbi, NR_CURSEG_DATA_TYPE, type);
3834 segno = le32_to_cpu(ckpt->cur_node_segno[type -
3836 blk_off = le16_to_cpu(ckpt->cur_node_blkoff[type -
3838 if (__exist_node_summaries(sbi))
3839 blk_addr = sum_blk_addr(sbi, NR_CURSEG_NODE_TYPE,
3840 type - CURSEG_HOT_NODE);
3842 blk_addr = GET_SUM_BLOCK(sbi, segno);
3845 new = f2fs_get_meta_page(sbi, blk_addr);
3847 return PTR_ERR(new);
3848 sum = (struct f2fs_summary_block *)page_address(new);
3850 if (IS_NODESEG(type)) {
3851 if (__exist_node_summaries(sbi)) {
3852 struct f2fs_summary *ns = &sum->entries[0];
3855 for (i = 0; i < sbi->blocks_per_seg; i++, ns++) {
3857 ns->ofs_in_node = 0;
3860 err = f2fs_restore_node_summary(sbi, segno, sum);
3866 /* set uncompleted segment to curseg */
3867 curseg = CURSEG_I(sbi, type);
3868 mutex_lock(&curseg->curseg_mutex);
3870 /* update journal info */
3871 down_write(&curseg->journal_rwsem);
3872 memcpy(curseg->journal, &sum->journal, SUM_JOURNAL_SIZE);
3873 up_write(&curseg->journal_rwsem);
3875 memcpy(curseg->sum_blk->entries, sum->entries, SUM_ENTRY_SIZE);
3876 memcpy(&curseg->sum_blk->footer, &sum->footer, SUM_FOOTER_SIZE);
3877 curseg->next_segno = segno;
3878 reset_curseg(sbi, type, 0);
3879 curseg->alloc_type = ckpt->alloc_type[type];
3880 curseg->next_blkoff = blk_off;
3881 mutex_unlock(&curseg->curseg_mutex);
3883 f2fs_put_page(new, 1);
3887 static int restore_curseg_summaries(struct f2fs_sb_info *sbi)
3889 struct f2fs_journal *sit_j = CURSEG_I(sbi, CURSEG_COLD_DATA)->journal;
3890 struct f2fs_journal *nat_j = CURSEG_I(sbi, CURSEG_HOT_DATA)->journal;
3891 int type = CURSEG_HOT_DATA;
3894 if (is_set_ckpt_flags(sbi, CP_COMPACT_SUM_FLAG)) {
3895 int npages = f2fs_npages_for_summary_flush(sbi, true);
3898 f2fs_ra_meta_pages(sbi, start_sum_block(sbi), npages,
3901 /* restore for compacted data summary */
3902 err = read_compacted_summaries(sbi);
3905 type = CURSEG_HOT_NODE;
3908 if (__exist_node_summaries(sbi))
3909 f2fs_ra_meta_pages(sbi,
3910 sum_blk_addr(sbi, NR_CURSEG_PERSIST_TYPE, type),
3911 NR_CURSEG_PERSIST_TYPE - type, META_CP, true);
3913 for (; type <= CURSEG_COLD_NODE; type++) {
3914 err = read_normal_summaries(sbi, type);
3919 /* sanity check for summary blocks */
3920 if (nats_in_cursum(nat_j) > NAT_JOURNAL_ENTRIES ||
3921 sits_in_cursum(sit_j) > SIT_JOURNAL_ENTRIES) {
3922 f2fs_err(sbi, "invalid journal entries nats %u sits %u",
3923 nats_in_cursum(nat_j), sits_in_cursum(sit_j));
3930 static void write_compacted_summaries(struct f2fs_sb_info *sbi, block_t blkaddr)
3933 unsigned char *kaddr;
3934 struct f2fs_summary *summary;
3935 struct curseg_info *seg_i;
3936 int written_size = 0;
3939 page = f2fs_grab_meta_page(sbi, blkaddr++);
3940 kaddr = (unsigned char *)page_address(page);
3941 memset(kaddr, 0, PAGE_SIZE);
3943 /* Step 1: write nat cache */
3944 seg_i = CURSEG_I(sbi, CURSEG_HOT_DATA);
3945 memcpy(kaddr, seg_i->journal, SUM_JOURNAL_SIZE);
3946 written_size += SUM_JOURNAL_SIZE;
3948 /* Step 2: write sit cache */
3949 seg_i = CURSEG_I(sbi, CURSEG_COLD_DATA);
3950 memcpy(kaddr + written_size, seg_i->journal, SUM_JOURNAL_SIZE);
3951 written_size += SUM_JOURNAL_SIZE;
3953 /* Step 3: write summary entries */
3954 for (i = CURSEG_HOT_DATA; i <= CURSEG_COLD_DATA; i++) {
3955 unsigned short blkoff;
3957 seg_i = CURSEG_I(sbi, i);
3958 if (sbi->ckpt->alloc_type[i] == SSR)
3959 blkoff = sbi->blocks_per_seg;
3961 blkoff = curseg_blkoff(sbi, i);
3963 for (j = 0; j < blkoff; j++) {
3965 page = f2fs_grab_meta_page(sbi, blkaddr++);
3966 kaddr = (unsigned char *)page_address(page);
3967 memset(kaddr, 0, PAGE_SIZE);
3970 summary = (struct f2fs_summary *)(kaddr + written_size);
3971 *summary = seg_i->sum_blk->entries[j];
3972 written_size += SUMMARY_SIZE;
3974 if (written_size + SUMMARY_SIZE <= PAGE_SIZE -
3978 set_page_dirty(page);
3979 f2fs_put_page(page, 1);
3984 set_page_dirty(page);
3985 f2fs_put_page(page, 1);
3989 static void write_normal_summaries(struct f2fs_sb_info *sbi,
3990 block_t blkaddr, int type)
3994 if (IS_DATASEG(type))
3995 end = type + NR_CURSEG_DATA_TYPE;
3997 end = type + NR_CURSEG_NODE_TYPE;
3999 for (i = type; i < end; i++)
4000 write_current_sum_page(sbi, i, blkaddr + (i - type));
4003 void f2fs_write_data_summaries(struct f2fs_sb_info *sbi, block_t start_blk)
4005 if (is_set_ckpt_flags(sbi, CP_COMPACT_SUM_FLAG))
4006 write_compacted_summaries(sbi, start_blk);
4008 write_normal_summaries(sbi, start_blk, CURSEG_HOT_DATA);
4011 void f2fs_write_node_summaries(struct f2fs_sb_info *sbi, block_t start_blk)
4013 write_normal_summaries(sbi, start_blk, CURSEG_HOT_NODE);
4016 int f2fs_lookup_journal_in_cursum(struct f2fs_journal *journal, int type,
4017 unsigned int val, int alloc)
4021 if (type == NAT_JOURNAL) {
4022 for (i = 0; i < nats_in_cursum(journal); i++) {
4023 if (le32_to_cpu(nid_in_journal(journal, i)) == val)
4026 if (alloc && __has_cursum_space(journal, 1, NAT_JOURNAL))
4027 return update_nats_in_cursum(journal, 1);
4028 } else if (type == SIT_JOURNAL) {
4029 for (i = 0; i < sits_in_cursum(journal); i++)
4030 if (le32_to_cpu(segno_in_journal(journal, i)) == val)
4032 if (alloc && __has_cursum_space(journal, 1, SIT_JOURNAL))
4033 return update_sits_in_cursum(journal, 1);
4038 static struct page *get_current_sit_page(struct f2fs_sb_info *sbi,
4041 return f2fs_get_meta_page(sbi, current_sit_addr(sbi, segno));
4044 static struct page *get_next_sit_page(struct f2fs_sb_info *sbi,
4047 struct sit_info *sit_i = SIT_I(sbi);
4049 pgoff_t src_off, dst_off;
4051 src_off = current_sit_addr(sbi, start);
4052 dst_off = next_sit_addr(sbi, src_off);
4054 page = f2fs_grab_meta_page(sbi, dst_off);
4055 seg_info_to_sit_page(sbi, page, start);
4057 set_page_dirty(page);
4058 set_to_next_sit(sit_i, start);
4063 static struct sit_entry_set *grab_sit_entry_set(void)
4065 struct sit_entry_set *ses =
4066 f2fs_kmem_cache_alloc(sit_entry_set_slab,
4067 GFP_NOFS, true, NULL);
4070 INIT_LIST_HEAD(&ses->set_list);
4074 static void release_sit_entry_set(struct sit_entry_set *ses)
4076 list_del(&ses->set_list);
4077 kmem_cache_free(sit_entry_set_slab, ses);
4080 static void adjust_sit_entry_set(struct sit_entry_set *ses,
4081 struct list_head *head)
4083 struct sit_entry_set *next = ses;
4085 if (list_is_last(&ses->set_list, head))
4088 list_for_each_entry_continue(next, head, set_list)
4089 if (ses->entry_cnt <= next->entry_cnt)
4092 list_move_tail(&ses->set_list, &next->set_list);
4095 static void add_sit_entry(unsigned int segno, struct list_head *head)
4097 struct sit_entry_set *ses;
4098 unsigned int start_segno = START_SEGNO(segno);
4100 list_for_each_entry(ses, head, set_list) {
4101 if (ses->start_segno == start_segno) {
4103 adjust_sit_entry_set(ses, head);
4108 ses = grab_sit_entry_set();
4110 ses->start_segno = start_segno;
4112 list_add(&ses->set_list, head);
4115 static void add_sits_in_set(struct f2fs_sb_info *sbi)
4117 struct f2fs_sm_info *sm_info = SM_I(sbi);
4118 struct list_head *set_list = &sm_info->sit_entry_set;
4119 unsigned long *bitmap = SIT_I(sbi)->dirty_sentries_bitmap;
4122 for_each_set_bit(segno, bitmap, MAIN_SEGS(sbi))
4123 add_sit_entry(segno, set_list);
4126 static void remove_sits_in_journal(struct f2fs_sb_info *sbi)
4128 struct curseg_info *curseg = CURSEG_I(sbi, CURSEG_COLD_DATA);
4129 struct f2fs_journal *journal = curseg->journal;
4132 down_write(&curseg->journal_rwsem);
4133 for (i = 0; i < sits_in_cursum(journal); i++) {
4137 segno = le32_to_cpu(segno_in_journal(journal, i));
4138 dirtied = __mark_sit_entry_dirty(sbi, segno);
4141 add_sit_entry(segno, &SM_I(sbi)->sit_entry_set);
4143 update_sits_in_cursum(journal, -i);
4144 up_write(&curseg->journal_rwsem);
4148 * CP calls this function, which flushes SIT entries including sit_journal,
4149 * and moves prefree segs to free segs.
4151 void f2fs_flush_sit_entries(struct f2fs_sb_info *sbi, struct cp_control *cpc)
4153 struct sit_info *sit_i = SIT_I(sbi);
4154 unsigned long *bitmap = sit_i->dirty_sentries_bitmap;
4155 struct curseg_info *curseg = CURSEG_I(sbi, CURSEG_COLD_DATA);
4156 struct f2fs_journal *journal = curseg->journal;
4157 struct sit_entry_set *ses, *tmp;
4158 struct list_head *head = &SM_I(sbi)->sit_entry_set;
4159 bool to_journal = !is_sbi_flag_set(sbi, SBI_IS_RESIZEFS);
4160 struct seg_entry *se;
4162 down_write(&sit_i->sentry_lock);
4164 if (!sit_i->dirty_sentries)
4168 * add and account sit entries of dirty bitmap in sit entry
4171 add_sits_in_set(sbi);
4174 * if there are no enough space in journal to store dirty sit
4175 * entries, remove all entries from journal and add and account
4176 * them in sit entry set.
4178 if (!__has_cursum_space(journal, sit_i->dirty_sentries, SIT_JOURNAL) ||
4180 remove_sits_in_journal(sbi);
4183 * there are two steps to flush sit entries:
4184 * #1, flush sit entries to journal in current cold data summary block.
4185 * #2, flush sit entries to sit page.
4187 list_for_each_entry_safe(ses, tmp, head, set_list) {
4188 struct page *page = NULL;
4189 struct f2fs_sit_block *raw_sit = NULL;
4190 unsigned int start_segno = ses->start_segno;
4191 unsigned int end = min(start_segno + SIT_ENTRY_PER_BLOCK,
4192 (unsigned long)MAIN_SEGS(sbi));
4193 unsigned int segno = start_segno;
4196 !__has_cursum_space(journal, ses->entry_cnt, SIT_JOURNAL))
4200 down_write(&curseg->journal_rwsem);
4202 page = get_next_sit_page(sbi, start_segno);
4203 raw_sit = page_address(page);
4206 /* flush dirty sit entries in region of current sit set */
4207 for_each_set_bit_from(segno, bitmap, end) {
4208 int offset, sit_offset;
4210 se = get_seg_entry(sbi, segno);
4211 #ifdef CONFIG_F2FS_CHECK_FS
4212 if (memcmp(se->cur_valid_map, se->cur_valid_map_mir,
4213 SIT_VBLOCK_MAP_SIZE))
4214 f2fs_bug_on(sbi, 1);
4217 /* add discard candidates */
4218 if (!(cpc->reason & CP_DISCARD)) {
4219 cpc->trim_start = segno;
4220 add_discard_addrs(sbi, cpc, false);
4224 offset = f2fs_lookup_journal_in_cursum(journal,
4225 SIT_JOURNAL, segno, 1);
4226 f2fs_bug_on(sbi, offset < 0);
4227 segno_in_journal(journal, offset) =
4229 seg_info_to_raw_sit(se,
4230 &sit_in_journal(journal, offset));
4231 check_block_count(sbi, segno,
4232 &sit_in_journal(journal, offset));
4234 sit_offset = SIT_ENTRY_OFFSET(sit_i, segno);
4235 seg_info_to_raw_sit(se,
4236 &raw_sit->entries[sit_offset]);
4237 check_block_count(sbi, segno,
4238 &raw_sit->entries[sit_offset]);
4241 __clear_bit(segno, bitmap);
4242 sit_i->dirty_sentries--;
4247 up_write(&curseg->journal_rwsem);
4249 f2fs_put_page(page, 1);
4251 f2fs_bug_on(sbi, ses->entry_cnt);
4252 release_sit_entry_set(ses);
4255 f2fs_bug_on(sbi, !list_empty(head));
4256 f2fs_bug_on(sbi, sit_i->dirty_sentries);
4258 if (cpc->reason & CP_DISCARD) {
4259 __u64 trim_start = cpc->trim_start;
4261 for (; cpc->trim_start <= cpc->trim_end; cpc->trim_start++)
4262 add_discard_addrs(sbi, cpc, false);
4264 cpc->trim_start = trim_start;
4266 up_write(&sit_i->sentry_lock);
4268 set_prefree_as_free_segments(sbi);
4271 static int build_sit_info(struct f2fs_sb_info *sbi)
4273 struct f2fs_super_block *raw_super = F2FS_RAW_SUPER(sbi);
4274 struct sit_info *sit_i;
4275 unsigned int sit_segs, start;
4276 char *src_bitmap, *bitmap;
4277 unsigned int bitmap_size, main_bitmap_size, sit_bitmap_size;
4278 unsigned int discard_map = f2fs_block_unit_discard(sbi) ? 1 : 0;
4280 /* allocate memory for SIT information */
4281 sit_i = f2fs_kzalloc(sbi, sizeof(struct sit_info), GFP_KERNEL);
4285 SM_I(sbi)->sit_info = sit_i;
4288 f2fs_kvzalloc(sbi, array_size(sizeof(struct seg_entry),
4291 if (!sit_i->sentries)
4294 main_bitmap_size = f2fs_bitmap_size(MAIN_SEGS(sbi));
4295 sit_i->dirty_sentries_bitmap = f2fs_kvzalloc(sbi, main_bitmap_size,
4297 if (!sit_i->dirty_sentries_bitmap)
4300 #ifdef CONFIG_F2FS_CHECK_FS
4301 bitmap_size = MAIN_SEGS(sbi) * SIT_VBLOCK_MAP_SIZE * (3 + discard_map);
4303 bitmap_size = MAIN_SEGS(sbi) * SIT_VBLOCK_MAP_SIZE * (2 + discard_map);
4305 sit_i->bitmap = f2fs_kvzalloc(sbi, bitmap_size, GFP_KERNEL);
4309 bitmap = sit_i->bitmap;
4311 for (start = 0; start < MAIN_SEGS(sbi); start++) {
4312 sit_i->sentries[start].cur_valid_map = bitmap;
4313 bitmap += SIT_VBLOCK_MAP_SIZE;
4315 sit_i->sentries[start].ckpt_valid_map = bitmap;
4316 bitmap += SIT_VBLOCK_MAP_SIZE;
4318 #ifdef CONFIG_F2FS_CHECK_FS
4319 sit_i->sentries[start].cur_valid_map_mir = bitmap;
4320 bitmap += SIT_VBLOCK_MAP_SIZE;
4324 sit_i->sentries[start].discard_map = bitmap;
4325 bitmap += SIT_VBLOCK_MAP_SIZE;
4329 sit_i->tmp_map = f2fs_kzalloc(sbi, SIT_VBLOCK_MAP_SIZE, GFP_KERNEL);
4330 if (!sit_i->tmp_map)
4333 if (__is_large_section(sbi)) {
4334 sit_i->sec_entries =
4335 f2fs_kvzalloc(sbi, array_size(sizeof(struct sec_entry),
4338 if (!sit_i->sec_entries)
4342 /* get information related with SIT */
4343 sit_segs = le32_to_cpu(raw_super->segment_count_sit) >> 1;
4345 /* setup SIT bitmap from ckeckpoint pack */
4346 sit_bitmap_size = __bitmap_size(sbi, SIT_BITMAP);
4347 src_bitmap = __bitmap_ptr(sbi, SIT_BITMAP);
4349 sit_i->sit_bitmap = kmemdup(src_bitmap, sit_bitmap_size, GFP_KERNEL);
4350 if (!sit_i->sit_bitmap)
4353 #ifdef CONFIG_F2FS_CHECK_FS
4354 sit_i->sit_bitmap_mir = kmemdup(src_bitmap,
4355 sit_bitmap_size, GFP_KERNEL);
4356 if (!sit_i->sit_bitmap_mir)
4359 sit_i->invalid_segmap = f2fs_kvzalloc(sbi,
4360 main_bitmap_size, GFP_KERNEL);
4361 if (!sit_i->invalid_segmap)
4365 /* init SIT information */
4366 sit_i->s_ops = &default_salloc_ops;
4368 sit_i->sit_base_addr = le32_to_cpu(raw_super->sit_blkaddr);
4369 sit_i->sit_blocks = sit_segs << sbi->log_blocks_per_seg;
4370 sit_i->written_valid_blocks = 0;
4371 sit_i->bitmap_size = sit_bitmap_size;
4372 sit_i->dirty_sentries = 0;
4373 sit_i->sents_per_block = SIT_ENTRY_PER_BLOCK;
4374 sit_i->elapsed_time = le64_to_cpu(sbi->ckpt->elapsed_time);
4375 sit_i->mounted_time = ktime_get_boottime_seconds();
4376 init_rwsem(&sit_i->sentry_lock);
4380 static int build_free_segmap(struct f2fs_sb_info *sbi)
4382 struct free_segmap_info *free_i;
4383 unsigned int bitmap_size, sec_bitmap_size;
4385 /* allocate memory for free segmap information */
4386 free_i = f2fs_kzalloc(sbi, sizeof(struct free_segmap_info), GFP_KERNEL);
4390 SM_I(sbi)->free_info = free_i;
4392 bitmap_size = f2fs_bitmap_size(MAIN_SEGS(sbi));
4393 free_i->free_segmap = f2fs_kvmalloc(sbi, bitmap_size, GFP_KERNEL);
4394 if (!free_i->free_segmap)
4397 sec_bitmap_size = f2fs_bitmap_size(MAIN_SECS(sbi));
4398 free_i->free_secmap = f2fs_kvmalloc(sbi, sec_bitmap_size, GFP_KERNEL);
4399 if (!free_i->free_secmap)
4402 /* set all segments as dirty temporarily */
4403 memset(free_i->free_segmap, 0xff, bitmap_size);
4404 memset(free_i->free_secmap, 0xff, sec_bitmap_size);
4406 /* init free segmap information */
4407 free_i->start_segno = GET_SEGNO_FROM_SEG0(sbi, MAIN_BLKADDR(sbi));
4408 free_i->free_segments = 0;
4409 free_i->free_sections = 0;
4410 spin_lock_init(&free_i->segmap_lock);
4414 static int build_curseg(struct f2fs_sb_info *sbi)
4416 struct curseg_info *array;
4419 array = f2fs_kzalloc(sbi, array_size(NR_CURSEG_TYPE,
4420 sizeof(*array)), GFP_KERNEL);
4424 SM_I(sbi)->curseg_array = array;
4426 for (i = 0; i < NO_CHECK_TYPE; i++) {
4427 mutex_init(&array[i].curseg_mutex);
4428 array[i].sum_blk = f2fs_kzalloc(sbi, PAGE_SIZE, GFP_KERNEL);
4429 if (!array[i].sum_blk)
4431 init_rwsem(&array[i].journal_rwsem);
4432 array[i].journal = f2fs_kzalloc(sbi,
4433 sizeof(struct f2fs_journal), GFP_KERNEL);
4434 if (!array[i].journal)
4436 if (i < NR_PERSISTENT_LOG)
4437 array[i].seg_type = CURSEG_HOT_DATA + i;
4438 else if (i == CURSEG_COLD_DATA_PINNED)
4439 array[i].seg_type = CURSEG_COLD_DATA;
4440 else if (i == CURSEG_ALL_DATA_ATGC)
4441 array[i].seg_type = CURSEG_COLD_DATA;
4442 array[i].segno = NULL_SEGNO;
4443 array[i].next_blkoff = 0;
4444 array[i].inited = false;
4446 return restore_curseg_summaries(sbi);
4449 static int build_sit_entries(struct f2fs_sb_info *sbi)
4451 struct sit_info *sit_i = SIT_I(sbi);
4452 struct curseg_info *curseg = CURSEG_I(sbi, CURSEG_COLD_DATA);
4453 struct f2fs_journal *journal = curseg->journal;
4454 struct seg_entry *se;
4455 struct f2fs_sit_entry sit;
4456 int sit_blk_cnt = SIT_BLK_CNT(sbi);
4457 unsigned int i, start, end;
4458 unsigned int readed, start_blk = 0;
4460 block_t total_node_blocks = 0;
4463 readed = f2fs_ra_meta_pages(sbi, start_blk, BIO_MAX_VECS,
4466 start = start_blk * sit_i->sents_per_block;
4467 end = (start_blk + readed) * sit_i->sents_per_block;
4469 for (; start < end && start < MAIN_SEGS(sbi); start++) {
4470 struct f2fs_sit_block *sit_blk;
4473 se = &sit_i->sentries[start];
4474 page = get_current_sit_page(sbi, start);
4476 return PTR_ERR(page);
4477 sit_blk = (struct f2fs_sit_block *)page_address(page);
4478 sit = sit_blk->entries[SIT_ENTRY_OFFSET(sit_i, start)];
4479 f2fs_put_page(page, 1);
4481 err = check_block_count(sbi, start, &sit);
4484 seg_info_from_raw_sit(se, &sit);
4485 if (IS_NODESEG(se->type))
4486 total_node_blocks += se->valid_blocks;
4488 if (f2fs_block_unit_discard(sbi)) {
4489 /* build discard map only one time */
4490 if (is_set_ckpt_flags(sbi, CP_TRIMMED_FLAG)) {
4491 memset(se->discard_map, 0xff,
4492 SIT_VBLOCK_MAP_SIZE);
4494 memcpy(se->discard_map,
4496 SIT_VBLOCK_MAP_SIZE);
4497 sbi->discard_blks +=
4498 sbi->blocks_per_seg -
4503 if (__is_large_section(sbi))
4504 get_sec_entry(sbi, start)->valid_blocks +=
4507 start_blk += readed;
4508 } while (start_blk < sit_blk_cnt);
4510 down_read(&curseg->journal_rwsem);
4511 for (i = 0; i < sits_in_cursum(journal); i++) {
4512 unsigned int old_valid_blocks;
4514 start = le32_to_cpu(segno_in_journal(journal, i));
4515 if (start >= MAIN_SEGS(sbi)) {
4516 f2fs_err(sbi, "Wrong journal entry on segno %u",
4518 err = -EFSCORRUPTED;
4522 se = &sit_i->sentries[start];
4523 sit = sit_in_journal(journal, i);
4525 old_valid_blocks = se->valid_blocks;
4526 if (IS_NODESEG(se->type))
4527 total_node_blocks -= old_valid_blocks;
4529 err = check_block_count(sbi, start, &sit);
4532 seg_info_from_raw_sit(se, &sit);
4533 if (IS_NODESEG(se->type))
4534 total_node_blocks += se->valid_blocks;
4536 if (f2fs_block_unit_discard(sbi)) {
4537 if (is_set_ckpt_flags(sbi, CP_TRIMMED_FLAG)) {
4538 memset(se->discard_map, 0xff, SIT_VBLOCK_MAP_SIZE);
4540 memcpy(se->discard_map, se->cur_valid_map,
4541 SIT_VBLOCK_MAP_SIZE);
4542 sbi->discard_blks += old_valid_blocks;
4543 sbi->discard_blks -= se->valid_blocks;
4547 if (__is_large_section(sbi)) {
4548 get_sec_entry(sbi, start)->valid_blocks +=
4550 get_sec_entry(sbi, start)->valid_blocks -=
4554 up_read(&curseg->journal_rwsem);
4556 if (!err && total_node_blocks != valid_node_count(sbi)) {
4557 f2fs_err(sbi, "SIT is corrupted node# %u vs %u",
4558 total_node_blocks, valid_node_count(sbi));
4559 err = -EFSCORRUPTED;
4565 static void init_free_segmap(struct f2fs_sb_info *sbi)
4569 struct seg_entry *sentry;
4571 for (start = 0; start < MAIN_SEGS(sbi); start++) {
4572 if (f2fs_usable_blks_in_seg(sbi, start) == 0)
4574 sentry = get_seg_entry(sbi, start);
4575 if (!sentry->valid_blocks)
4576 __set_free(sbi, start);
4578 SIT_I(sbi)->written_valid_blocks +=
4579 sentry->valid_blocks;
4582 /* set use the current segments */
4583 for (type = CURSEG_HOT_DATA; type <= CURSEG_COLD_NODE; type++) {
4584 struct curseg_info *curseg_t = CURSEG_I(sbi, type);
4586 __set_test_and_inuse(sbi, curseg_t->segno);
4590 static void init_dirty_segmap(struct f2fs_sb_info *sbi)
4592 struct dirty_seglist_info *dirty_i = DIRTY_I(sbi);
4593 struct free_segmap_info *free_i = FREE_I(sbi);
4594 unsigned int segno = 0, offset = 0, secno;
4595 block_t valid_blocks, usable_blks_in_seg;
4596 block_t blks_per_sec = BLKS_PER_SEC(sbi);
4599 /* find dirty segment based on free segmap */
4600 segno = find_next_inuse(free_i, MAIN_SEGS(sbi), offset);
4601 if (segno >= MAIN_SEGS(sbi))
4604 valid_blocks = get_valid_blocks(sbi, segno, false);
4605 usable_blks_in_seg = f2fs_usable_blks_in_seg(sbi, segno);
4606 if (valid_blocks == usable_blks_in_seg || !valid_blocks)
4608 if (valid_blocks > usable_blks_in_seg) {
4609 f2fs_bug_on(sbi, 1);
4612 mutex_lock(&dirty_i->seglist_lock);
4613 __locate_dirty_segment(sbi, segno, DIRTY);
4614 mutex_unlock(&dirty_i->seglist_lock);
4617 if (!__is_large_section(sbi))
4620 mutex_lock(&dirty_i->seglist_lock);
4621 for (segno = 0; segno < MAIN_SEGS(sbi); segno += sbi->segs_per_sec) {
4622 valid_blocks = get_valid_blocks(sbi, segno, true);
4623 secno = GET_SEC_FROM_SEG(sbi, segno);
4625 if (!valid_blocks || valid_blocks == blks_per_sec)
4627 if (IS_CURSEC(sbi, secno))
4629 set_bit(secno, dirty_i->dirty_secmap);
4631 mutex_unlock(&dirty_i->seglist_lock);
4634 static int init_victim_secmap(struct f2fs_sb_info *sbi)
4636 struct dirty_seglist_info *dirty_i = DIRTY_I(sbi);
4637 unsigned int bitmap_size = f2fs_bitmap_size(MAIN_SECS(sbi));
4639 dirty_i->victim_secmap = f2fs_kvzalloc(sbi, bitmap_size, GFP_KERNEL);
4640 if (!dirty_i->victim_secmap)
4645 static int build_dirty_segmap(struct f2fs_sb_info *sbi)
4647 struct dirty_seglist_info *dirty_i;
4648 unsigned int bitmap_size, i;
4650 /* allocate memory for dirty segments list information */
4651 dirty_i = f2fs_kzalloc(sbi, sizeof(struct dirty_seglist_info),
4656 SM_I(sbi)->dirty_info = dirty_i;
4657 mutex_init(&dirty_i->seglist_lock);
4659 bitmap_size = f2fs_bitmap_size(MAIN_SEGS(sbi));
4661 for (i = 0; i < NR_DIRTY_TYPE; i++) {
4662 dirty_i->dirty_segmap[i] = f2fs_kvzalloc(sbi, bitmap_size,
4664 if (!dirty_i->dirty_segmap[i])
4668 if (__is_large_section(sbi)) {
4669 bitmap_size = f2fs_bitmap_size(MAIN_SECS(sbi));
4670 dirty_i->dirty_secmap = f2fs_kvzalloc(sbi,
4671 bitmap_size, GFP_KERNEL);
4672 if (!dirty_i->dirty_secmap)
4676 init_dirty_segmap(sbi);
4677 return init_victim_secmap(sbi);
4680 static int sanity_check_curseg(struct f2fs_sb_info *sbi)
4685 * In LFS/SSR curseg, .next_blkoff should point to an unused blkaddr;
4686 * In LFS curseg, all blkaddr after .next_blkoff should be unused.
4688 for (i = 0; i < NR_PERSISTENT_LOG; i++) {
4689 struct curseg_info *curseg = CURSEG_I(sbi, i);
4690 struct seg_entry *se = get_seg_entry(sbi, curseg->segno);
4691 unsigned int blkofs = curseg->next_blkoff;
4693 if (f2fs_sb_has_readonly(sbi) &&
4694 i != CURSEG_HOT_DATA && i != CURSEG_HOT_NODE)
4697 sanity_check_seg_type(sbi, curseg->seg_type);
4699 if (curseg->alloc_type != LFS && curseg->alloc_type != SSR) {
4701 "Current segment has invalid alloc_type:%d",
4702 curseg->alloc_type);
4703 return -EFSCORRUPTED;
4706 if (f2fs_test_bit(blkofs, se->cur_valid_map))
4709 if (curseg->alloc_type == SSR)
4712 for (blkofs += 1; blkofs < sbi->blocks_per_seg; blkofs++) {
4713 if (!f2fs_test_bit(blkofs, se->cur_valid_map))
4717 "Current segment's next free block offset is inconsistent with bitmap, logtype:%u, segno:%u, type:%u, next_blkoff:%u, blkofs:%u",
4718 i, curseg->segno, curseg->alloc_type,
4719 curseg->next_blkoff, blkofs);
4720 return -EFSCORRUPTED;
4726 #ifdef CONFIG_BLK_DEV_ZONED
4728 static int check_zone_write_pointer(struct f2fs_sb_info *sbi,
4729 struct f2fs_dev_info *fdev,
4730 struct blk_zone *zone)
4732 unsigned int wp_segno, wp_blkoff, zone_secno, zone_segno, segno;
4733 block_t zone_block, wp_block, last_valid_block;
4734 unsigned int log_sectors_per_block = sbi->log_blocksize - SECTOR_SHIFT;
4736 struct seg_entry *se;
4738 if (zone->type != BLK_ZONE_TYPE_SEQWRITE_REQ)
4741 wp_block = fdev->start_blk + (zone->wp >> log_sectors_per_block);
4742 wp_segno = GET_SEGNO(sbi, wp_block);
4743 wp_blkoff = wp_block - START_BLOCK(sbi, wp_segno);
4744 zone_block = fdev->start_blk + (zone->start >> log_sectors_per_block);
4745 zone_segno = GET_SEGNO(sbi, zone_block);
4746 zone_secno = GET_SEC_FROM_SEG(sbi, zone_segno);
4748 if (zone_segno >= MAIN_SEGS(sbi))
4752 * Skip check of zones cursegs point to, since
4753 * fix_curseg_write_pointer() checks them.
4755 for (i = 0; i < NO_CHECK_TYPE; i++)
4756 if (zone_secno == GET_SEC_FROM_SEG(sbi,
4757 CURSEG_I(sbi, i)->segno))
4761 * Get last valid block of the zone.
4763 last_valid_block = zone_block - 1;
4764 for (s = sbi->segs_per_sec - 1; s >= 0; s--) {
4765 segno = zone_segno + s;
4766 se = get_seg_entry(sbi, segno);
4767 for (b = sbi->blocks_per_seg - 1; b >= 0; b--)
4768 if (f2fs_test_bit(b, se->cur_valid_map)) {
4769 last_valid_block = START_BLOCK(sbi, segno) + b;
4772 if (last_valid_block >= zone_block)
4777 * If last valid block is beyond the write pointer, report the
4778 * inconsistency. This inconsistency does not cause write error
4779 * because the zone will not be selected for write operation until
4780 * it get discarded. Just report it.
4782 if (last_valid_block >= wp_block) {
4783 f2fs_notice(sbi, "Valid block beyond write pointer: "
4784 "valid block[0x%x,0x%x] wp[0x%x,0x%x]",
4785 GET_SEGNO(sbi, last_valid_block),
4786 GET_BLKOFF_FROM_SEG0(sbi, last_valid_block),
4787 wp_segno, wp_blkoff);
4792 * If there is no valid block in the zone and if write pointer is
4793 * not at zone start, reset the write pointer.
4795 if (last_valid_block + 1 == zone_block && zone->wp != zone->start) {
4797 "Zone without valid block has non-zero write "
4798 "pointer. Reset the write pointer: wp[0x%x,0x%x]",
4799 wp_segno, wp_blkoff);
4800 ret = __f2fs_issue_discard_zone(sbi, fdev->bdev, zone_block,
4801 zone->len >> log_sectors_per_block);
4803 f2fs_err(sbi, "Discard zone failed: %s (errno=%d)",
4812 static struct f2fs_dev_info *get_target_zoned_dev(struct f2fs_sb_info *sbi,
4813 block_t zone_blkaddr)
4817 for (i = 0; i < sbi->s_ndevs; i++) {
4818 if (!bdev_is_zoned(FDEV(i).bdev))
4820 if (sbi->s_ndevs == 1 || (FDEV(i).start_blk <= zone_blkaddr &&
4821 zone_blkaddr <= FDEV(i).end_blk))
4828 static int report_one_zone_cb(struct blk_zone *zone, unsigned int idx,
4831 memcpy(data, zone, sizeof(struct blk_zone));
4835 static int fix_curseg_write_pointer(struct f2fs_sb_info *sbi, int type)
4837 struct curseg_info *cs = CURSEG_I(sbi, type);
4838 struct f2fs_dev_info *zbd;
4839 struct blk_zone zone;
4840 unsigned int cs_section, wp_segno, wp_blkoff, wp_sector_off;
4841 block_t cs_zone_block, wp_block;
4842 unsigned int log_sectors_per_block = sbi->log_blocksize - SECTOR_SHIFT;
4843 sector_t zone_sector;
4846 cs_section = GET_SEC_FROM_SEG(sbi, cs->segno);
4847 cs_zone_block = START_BLOCK(sbi, GET_SEG_FROM_SEC(sbi, cs_section));
4849 zbd = get_target_zoned_dev(sbi, cs_zone_block);
4853 /* report zone for the sector the curseg points to */
4854 zone_sector = (sector_t)(cs_zone_block - zbd->start_blk)
4855 << log_sectors_per_block;
4856 err = blkdev_report_zones(zbd->bdev, zone_sector, 1,
4857 report_one_zone_cb, &zone);
4859 f2fs_err(sbi, "Report zone failed: %s errno=(%d)",
4864 if (zone.type != BLK_ZONE_TYPE_SEQWRITE_REQ)
4867 wp_block = zbd->start_blk + (zone.wp >> log_sectors_per_block);
4868 wp_segno = GET_SEGNO(sbi, wp_block);
4869 wp_blkoff = wp_block - START_BLOCK(sbi, wp_segno);
4870 wp_sector_off = zone.wp & GENMASK(log_sectors_per_block - 1, 0);
4872 if (cs->segno == wp_segno && cs->next_blkoff == wp_blkoff &&
4876 f2fs_notice(sbi, "Unaligned curseg[%d] with write pointer: "
4877 "curseg[0x%x,0x%x] wp[0x%x,0x%x]",
4878 type, cs->segno, cs->next_blkoff, wp_segno, wp_blkoff);
4880 f2fs_notice(sbi, "Assign new section to curseg[%d]: "
4881 "curseg[0x%x,0x%x]", type, cs->segno, cs->next_blkoff);
4883 f2fs_allocate_new_section(sbi, type, true);
4885 /* check consistency of the zone curseg pointed to */
4886 if (check_zone_write_pointer(sbi, zbd, &zone))
4889 /* check newly assigned zone */
4890 cs_section = GET_SEC_FROM_SEG(sbi, cs->segno);
4891 cs_zone_block = START_BLOCK(sbi, GET_SEG_FROM_SEC(sbi, cs_section));
4893 zbd = get_target_zoned_dev(sbi, cs_zone_block);
4897 zone_sector = (sector_t)(cs_zone_block - zbd->start_blk)
4898 << log_sectors_per_block;
4899 err = blkdev_report_zones(zbd->bdev, zone_sector, 1,
4900 report_one_zone_cb, &zone);
4902 f2fs_err(sbi, "Report zone failed: %s errno=(%d)",
4907 if (zone.type != BLK_ZONE_TYPE_SEQWRITE_REQ)
4910 if (zone.wp != zone.start) {
4912 "New zone for curseg[%d] is not yet discarded. "
4913 "Reset the zone: curseg[0x%x,0x%x]",
4914 type, cs->segno, cs->next_blkoff);
4915 err = __f2fs_issue_discard_zone(sbi, zbd->bdev,
4916 zone_sector >> log_sectors_per_block,
4917 zone.len >> log_sectors_per_block);
4919 f2fs_err(sbi, "Discard zone failed: %s (errno=%d)",
4928 int f2fs_fix_curseg_write_pointer(struct f2fs_sb_info *sbi)
4932 for (i = 0; i < NR_PERSISTENT_LOG; i++) {
4933 ret = fix_curseg_write_pointer(sbi, i);
4941 struct check_zone_write_pointer_args {
4942 struct f2fs_sb_info *sbi;
4943 struct f2fs_dev_info *fdev;
4946 static int check_zone_write_pointer_cb(struct blk_zone *zone, unsigned int idx,
4949 struct check_zone_write_pointer_args *args;
4951 args = (struct check_zone_write_pointer_args *)data;
4953 return check_zone_write_pointer(args->sbi, args->fdev, zone);
4956 int f2fs_check_write_pointer(struct f2fs_sb_info *sbi)
4959 struct check_zone_write_pointer_args args;
4961 for (i = 0; i < sbi->s_ndevs; i++) {
4962 if (!bdev_is_zoned(FDEV(i).bdev))
4966 args.fdev = &FDEV(i);
4967 ret = blkdev_report_zones(FDEV(i).bdev, 0, BLK_ALL_ZONES,
4968 check_zone_write_pointer_cb, &args);
4976 static bool is_conv_zone(struct f2fs_sb_info *sbi, unsigned int zone_idx,
4977 unsigned int dev_idx)
4979 if (!bdev_is_zoned(FDEV(dev_idx).bdev))
4981 return !test_bit(zone_idx, FDEV(dev_idx).blkz_seq);
4984 /* Return the zone index in the given device */
4985 static unsigned int get_zone_idx(struct f2fs_sb_info *sbi, unsigned int secno,
4988 block_t sec_start_blkaddr = START_BLOCK(sbi, GET_SEG_FROM_SEC(sbi, secno));
4990 return (sec_start_blkaddr - FDEV(dev_idx).start_blk) >>
4991 sbi->log_blocks_per_blkz;
4995 * Return the usable segments in a section based on the zone's
4996 * corresponding zone capacity. Zone is equal to a section.
4998 static inline unsigned int f2fs_usable_zone_segs_in_sec(
4999 struct f2fs_sb_info *sbi, unsigned int segno)
5001 unsigned int dev_idx, zone_idx, unusable_segs_in_sec;
5003 dev_idx = f2fs_target_device_index(sbi, START_BLOCK(sbi, segno));
5004 zone_idx = get_zone_idx(sbi, GET_SEC_FROM_SEG(sbi, segno), dev_idx);
5006 /* Conventional zone's capacity is always equal to zone size */
5007 if (is_conv_zone(sbi, zone_idx, dev_idx))
5008 return sbi->segs_per_sec;
5011 * If the zone_capacity_blocks array is NULL, then zone capacity
5012 * is equal to the zone size for all zones
5014 if (!FDEV(dev_idx).zone_capacity_blocks)
5015 return sbi->segs_per_sec;
5017 /* Get the segment count beyond zone capacity block */
5018 unusable_segs_in_sec = (sbi->blocks_per_blkz -
5019 FDEV(dev_idx).zone_capacity_blocks[zone_idx]) >>
5020 sbi->log_blocks_per_seg;
5021 return sbi->segs_per_sec - unusable_segs_in_sec;
5025 * Return the number of usable blocks in a segment. The number of blocks
5026 * returned is always equal to the number of blocks in a segment for
5027 * segments fully contained within a sequential zone capacity or a
5028 * conventional zone. For segments partially contained in a sequential
5029 * zone capacity, the number of usable blocks up to the zone capacity
5030 * is returned. 0 is returned in all other cases.
5032 static inline unsigned int f2fs_usable_zone_blks_in_seg(
5033 struct f2fs_sb_info *sbi, unsigned int segno)
5035 block_t seg_start, sec_start_blkaddr, sec_cap_blkaddr;
5036 unsigned int zone_idx, dev_idx, secno;
5038 secno = GET_SEC_FROM_SEG(sbi, segno);
5039 seg_start = START_BLOCK(sbi, segno);
5040 dev_idx = f2fs_target_device_index(sbi, seg_start);
5041 zone_idx = get_zone_idx(sbi, secno, dev_idx);
5044 * Conventional zone's capacity is always equal to zone size,
5045 * so, blocks per segment is unchanged.
5047 if (is_conv_zone(sbi, zone_idx, dev_idx))
5048 return sbi->blocks_per_seg;
5050 if (!FDEV(dev_idx).zone_capacity_blocks)
5051 return sbi->blocks_per_seg;
5053 sec_start_blkaddr = START_BLOCK(sbi, GET_SEG_FROM_SEC(sbi, secno));
5054 sec_cap_blkaddr = sec_start_blkaddr +
5055 FDEV(dev_idx).zone_capacity_blocks[zone_idx];
5058 * If segment starts before zone capacity and spans beyond
5059 * zone capacity, then usable blocks are from seg start to
5060 * zone capacity. If the segment starts after the zone capacity,
5061 * then there are no usable blocks.
5063 if (seg_start >= sec_cap_blkaddr)
5065 if (seg_start + sbi->blocks_per_seg > sec_cap_blkaddr)
5066 return sec_cap_blkaddr - seg_start;
5068 return sbi->blocks_per_seg;
5071 int f2fs_fix_curseg_write_pointer(struct f2fs_sb_info *sbi)
5076 int f2fs_check_write_pointer(struct f2fs_sb_info *sbi)
5081 static inline unsigned int f2fs_usable_zone_blks_in_seg(struct f2fs_sb_info *sbi,
5087 static inline unsigned int f2fs_usable_zone_segs_in_sec(struct f2fs_sb_info *sbi,
5093 unsigned int f2fs_usable_blks_in_seg(struct f2fs_sb_info *sbi,
5096 if (f2fs_sb_has_blkzoned(sbi))
5097 return f2fs_usable_zone_blks_in_seg(sbi, segno);
5099 return sbi->blocks_per_seg;
5102 unsigned int f2fs_usable_segs_in_sec(struct f2fs_sb_info *sbi,
5105 if (f2fs_sb_has_blkzoned(sbi))
5106 return f2fs_usable_zone_segs_in_sec(sbi, segno);
5108 return sbi->segs_per_sec;
5112 * Update min, max modified time for cost-benefit GC algorithm
5114 static void init_min_max_mtime(struct f2fs_sb_info *sbi)
5116 struct sit_info *sit_i = SIT_I(sbi);
5119 down_write(&sit_i->sentry_lock);
5121 sit_i->min_mtime = ULLONG_MAX;
5123 for (segno = 0; segno < MAIN_SEGS(sbi); segno += sbi->segs_per_sec) {
5125 unsigned long long mtime = 0;
5127 for (i = 0; i < sbi->segs_per_sec; i++)
5128 mtime += get_seg_entry(sbi, segno + i)->mtime;
5130 mtime = div_u64(mtime, sbi->segs_per_sec);
5132 if (sit_i->min_mtime > mtime)
5133 sit_i->min_mtime = mtime;
5135 sit_i->max_mtime = get_mtime(sbi, false);
5136 sit_i->dirty_max_mtime = 0;
5137 up_write(&sit_i->sentry_lock);
5140 int f2fs_build_segment_manager(struct f2fs_sb_info *sbi)
5142 struct f2fs_super_block *raw_super = F2FS_RAW_SUPER(sbi);
5143 struct f2fs_checkpoint *ckpt = F2FS_CKPT(sbi);
5144 struct f2fs_sm_info *sm_info;
5147 sm_info = f2fs_kzalloc(sbi, sizeof(struct f2fs_sm_info), GFP_KERNEL);
5152 sbi->sm_info = sm_info;
5153 sm_info->seg0_blkaddr = le32_to_cpu(raw_super->segment0_blkaddr);
5154 sm_info->main_blkaddr = le32_to_cpu(raw_super->main_blkaddr);
5155 sm_info->segment_count = le32_to_cpu(raw_super->segment_count);
5156 sm_info->reserved_segments = le32_to_cpu(ckpt->rsvd_segment_count);
5157 sm_info->ovp_segments = le32_to_cpu(ckpt->overprov_segment_count);
5158 sm_info->main_segments = le32_to_cpu(raw_super->segment_count_main);
5159 sm_info->ssa_blkaddr = le32_to_cpu(raw_super->ssa_blkaddr);
5160 sm_info->rec_prefree_segments = sm_info->main_segments *
5161 DEF_RECLAIM_PREFREE_SEGMENTS / 100;
5162 if (sm_info->rec_prefree_segments > DEF_MAX_RECLAIM_PREFREE_SEGMENTS)
5163 sm_info->rec_prefree_segments = DEF_MAX_RECLAIM_PREFREE_SEGMENTS;
5165 if (!f2fs_lfs_mode(sbi))
5166 sm_info->ipu_policy = 1 << F2FS_IPU_FSYNC;
5167 sm_info->min_ipu_util = DEF_MIN_IPU_UTIL;
5168 sm_info->min_fsync_blocks = DEF_MIN_FSYNC_BLOCKS;
5169 sm_info->min_seq_blocks = sbi->blocks_per_seg;
5170 sm_info->min_hot_blocks = DEF_MIN_HOT_BLOCKS;
5171 sm_info->min_ssr_sections = reserved_sections(sbi);
5173 INIT_LIST_HEAD(&sm_info->sit_entry_set);
5175 init_f2fs_rwsem(&sm_info->curseg_lock);
5177 if (!f2fs_readonly(sbi->sb)) {
5178 err = f2fs_create_flush_cmd_control(sbi);
5183 err = create_discard_cmd_control(sbi);
5187 err = build_sit_info(sbi);
5190 err = build_free_segmap(sbi);
5193 err = build_curseg(sbi);
5197 /* reinit free segmap based on SIT */
5198 err = build_sit_entries(sbi);
5202 init_free_segmap(sbi);
5203 err = build_dirty_segmap(sbi);
5207 err = sanity_check_curseg(sbi);
5211 init_min_max_mtime(sbi);
5215 static void discard_dirty_segmap(struct f2fs_sb_info *sbi,
5216 enum dirty_type dirty_type)
5218 struct dirty_seglist_info *dirty_i = DIRTY_I(sbi);
5220 mutex_lock(&dirty_i->seglist_lock);
5221 kvfree(dirty_i->dirty_segmap[dirty_type]);
5222 dirty_i->nr_dirty[dirty_type] = 0;
5223 mutex_unlock(&dirty_i->seglist_lock);
5226 static void destroy_victim_secmap(struct f2fs_sb_info *sbi)
5228 struct dirty_seglist_info *dirty_i = DIRTY_I(sbi);
5230 kvfree(dirty_i->victim_secmap);
5233 static void destroy_dirty_segmap(struct f2fs_sb_info *sbi)
5235 struct dirty_seglist_info *dirty_i = DIRTY_I(sbi);
5241 /* discard pre-free/dirty segments list */
5242 for (i = 0; i < NR_DIRTY_TYPE; i++)
5243 discard_dirty_segmap(sbi, i);
5245 if (__is_large_section(sbi)) {
5246 mutex_lock(&dirty_i->seglist_lock);
5247 kvfree(dirty_i->dirty_secmap);
5248 mutex_unlock(&dirty_i->seglist_lock);
5251 destroy_victim_secmap(sbi);
5252 SM_I(sbi)->dirty_info = NULL;
5256 static void destroy_curseg(struct f2fs_sb_info *sbi)
5258 struct curseg_info *array = SM_I(sbi)->curseg_array;
5263 SM_I(sbi)->curseg_array = NULL;
5264 for (i = 0; i < NR_CURSEG_TYPE; i++) {
5265 kfree(array[i].sum_blk);
5266 kfree(array[i].journal);
5271 static void destroy_free_segmap(struct f2fs_sb_info *sbi)
5273 struct free_segmap_info *free_i = SM_I(sbi)->free_info;
5277 SM_I(sbi)->free_info = NULL;
5278 kvfree(free_i->free_segmap);
5279 kvfree(free_i->free_secmap);
5283 static void destroy_sit_info(struct f2fs_sb_info *sbi)
5285 struct sit_info *sit_i = SIT_I(sbi);
5290 if (sit_i->sentries)
5291 kvfree(sit_i->bitmap);
5292 kfree(sit_i->tmp_map);
5294 kvfree(sit_i->sentries);
5295 kvfree(sit_i->sec_entries);
5296 kvfree(sit_i->dirty_sentries_bitmap);
5298 SM_I(sbi)->sit_info = NULL;
5299 kvfree(sit_i->sit_bitmap);
5300 #ifdef CONFIG_F2FS_CHECK_FS
5301 kvfree(sit_i->sit_bitmap_mir);
5302 kvfree(sit_i->invalid_segmap);
5307 void f2fs_destroy_segment_manager(struct f2fs_sb_info *sbi)
5309 struct f2fs_sm_info *sm_info = SM_I(sbi);
5313 f2fs_destroy_flush_cmd_control(sbi, true);
5314 destroy_discard_cmd_control(sbi);
5315 destroy_dirty_segmap(sbi);
5316 destroy_curseg(sbi);
5317 destroy_free_segmap(sbi);
5318 destroy_sit_info(sbi);
5319 sbi->sm_info = NULL;
5323 int __init f2fs_create_segment_manager_caches(void)
5325 discard_entry_slab = f2fs_kmem_cache_create("f2fs_discard_entry",
5326 sizeof(struct discard_entry));
5327 if (!discard_entry_slab)
5330 discard_cmd_slab = f2fs_kmem_cache_create("f2fs_discard_cmd",
5331 sizeof(struct discard_cmd));
5332 if (!discard_cmd_slab)
5333 goto destroy_discard_entry;
5335 sit_entry_set_slab = f2fs_kmem_cache_create("f2fs_sit_entry_set",
5336 sizeof(struct sit_entry_set));
5337 if (!sit_entry_set_slab)
5338 goto destroy_discard_cmd;
5340 inmem_entry_slab = f2fs_kmem_cache_create("f2fs_inmem_page_entry",
5341 sizeof(struct inmem_pages));
5342 if (!inmem_entry_slab)
5343 goto destroy_sit_entry_set;
5346 destroy_sit_entry_set:
5347 kmem_cache_destroy(sit_entry_set_slab);
5348 destroy_discard_cmd:
5349 kmem_cache_destroy(discard_cmd_slab);
5350 destroy_discard_entry:
5351 kmem_cache_destroy(discard_entry_slab);
5356 void f2fs_destroy_segment_manager_caches(void)
5358 kmem_cache_destroy(sit_entry_set_slab);
5359 kmem_cache_destroy(discard_cmd_slab);
5360 kmem_cache_destroy(discard_entry_slab);
5361 kmem_cache_destroy(inmem_entry_slab);