Nishad Kamdar | d29fbcd | 2020-04-25 18:49:08 +0530 | [diff] [blame] | 1 | /* SPDX-License-Identifier: GPL-2.0 */ |
Jaegeuk Kim | 0a8165d | 2012-11-29 13:28:09 +0900 | [diff] [blame] | 2 | /* |
Jaegeuk Kim | 7bc0900 | 2012-11-02 17:13:01 +0900 | [diff] [blame] | 3 | * fs/f2fs/gc.h |
| 4 | * |
| 5 | * Copyright (c) 2012 Samsung Electronics Co., Ltd. |
| 6 | * http://www.samsung.com/ |
Jaegeuk Kim | 7bc0900 | 2012-11-02 17:13:01 +0900 | [diff] [blame] | 7 | */ |
Jaegeuk Kim | 7bc0900 | 2012-11-02 17:13:01 +0900 | [diff] [blame] | 8 | #define GC_THREAD_MIN_WB_PAGES 1 /* |
| 9 | * a threshold to determine |
| 10 | * whether IO subsystem is idle |
| 11 | * or not |
| 12 | */ |
Jaegeuk Kim | d9872a6 | 2017-08-06 22:09:00 -0700 | [diff] [blame] | 13 | #define DEF_GC_THREAD_URGENT_SLEEP_TIME 500 /* 500 ms */ |
Namjae Jeon | b59d0ba | 2013-08-04 23:09:40 +0900 | [diff] [blame] | 14 | #define DEF_GC_THREAD_MIN_SLEEP_TIME 30000 /* milliseconds */ |
| 15 | #define DEF_GC_THREAD_MAX_SLEEP_TIME 60000 |
| 16 | #define DEF_GC_THREAD_NOGC_SLEEP_TIME 300000 /* wait 5 min */ |
Chao Yu | 093749e | 2020-08-04 21:14:49 +0800 | [diff] [blame] | 17 | |
| 18 | /* choose candidates from sections which has age of more than 7 days */ |
| 19 | #define DEF_GC_THREAD_AGE_THRESHOLD (60 * 60 * 24 * 7) |
| 20 | #define DEF_GC_THREAD_CANDIDATE_RATIO 20 /* select 20% oldest sections as candidates */ |
| 21 | #define DEF_GC_THREAD_MAX_CANDIDATE_COUNT 10 /* select at most 10 sections as candidates */ |
| 22 | #define DEF_GC_THREAD_AGE_WEIGHT 60 /* age weight */ |
| 23 | #define DEFAULT_ACCURACY_CLASS 10000 /* accuracy class */ |
| 24 | |
Jaegeuk Kim | 7bc0900 | 2012-11-02 17:13:01 +0900 | [diff] [blame] | 25 | #define LIMIT_INVALID_BLOCK 40 /* percentage over total user space */ |
| 26 | #define LIMIT_FREE_BLOCK 40 /* percentage over invalid + free space */ |
| 27 | |
Jaegeuk Kim | 1ad71a2 | 2017-12-07 16:25:39 -0800 | [diff] [blame] | 28 | #define DEF_GC_FAILED_PINNED_FILES 2048 |
| 29 | |
Jaegeuk Kim | 7bc0900 | 2012-11-02 17:13:01 +0900 | [diff] [blame] | 30 | /* Search max. number of dirty segments to select a victim segment */ |
Jaegeuk Kim | b1c57c1 | 2014-01-08 13:45:08 +0900 | [diff] [blame] | 31 | #define DEF_MAX_VICTIM_SEARCH 4096 /* covers 8GB */ |
Jaegeuk Kim | 7bc0900 | 2012-11-02 17:13:01 +0900 | [diff] [blame] | 32 | |
Jaegeuk Kim | 7bc0900 | 2012-11-02 17:13:01 +0900 | [diff] [blame] | 33 | struct f2fs_gc_kthread { |
| 34 | struct task_struct *f2fs_gc_task; |
| 35 | wait_queue_head_t gc_wait_queue_head; |
Namjae Jeon | b59d0ba | 2013-08-04 23:09:40 +0900 | [diff] [blame] | 36 | |
| 37 | /* for gc sleep time */ |
Jaegeuk Kim | d9872a6 | 2017-08-06 22:09:00 -0700 | [diff] [blame] | 38 | unsigned int urgent_sleep_time; |
Namjae Jeon | b59d0ba | 2013-08-04 23:09:40 +0900 | [diff] [blame] | 39 | unsigned int min_sleep_time; |
| 40 | unsigned int max_sleep_time; |
| 41 | unsigned int no_gc_sleep_time; |
Namjae Jeon | d2dc095 | 2013-08-04 23:10:15 +0900 | [diff] [blame] | 42 | |
| 43 | /* for changing gc mode */ |
Jaegeuk Kim | d9872a6 | 2017-08-06 22:09:00 -0700 | [diff] [blame] | 44 | unsigned int gc_wake; |
Chao Yu | 5911d2d | 2021-03-27 17:57:06 +0800 | [diff] [blame] | 45 | |
| 46 | /* for GC_MERGE mount option */ |
| 47 | wait_queue_head_t fggc_wq; /* |
| 48 | * caller of f2fs_balance_fs() |
| 49 | * will wait on this wait queue. |
| 50 | */ |
Jaegeuk Kim | 7bc0900 | 2012-11-02 17:13:01 +0900 | [diff] [blame] | 51 | }; |
| 52 | |
Changman Lee | 7dda2af | 2014-11-28 15:49:40 +0000 | [diff] [blame] | 53 | struct gc_inode_list { |
| 54 | struct list_head ilist; |
| 55 | struct radix_tree_root iroot; |
| 56 | }; |
| 57 | |
Chao Yu | 093749e | 2020-08-04 21:14:49 +0800 | [diff] [blame] | 58 | struct victim_info { |
| 59 | unsigned long long mtime; /* mtime of section */ |
| 60 | unsigned int segno; /* section No. */ |
| 61 | }; |
| 62 | |
| 63 | struct victim_entry { |
| 64 | struct rb_node rb_node; /* rb node located in rb-tree */ |
| 65 | union { |
| 66 | struct { |
| 67 | unsigned long long mtime; /* mtime of section */ |
| 68 | unsigned int segno; /* segment No. */ |
| 69 | }; |
| 70 | struct victim_info vi; /* victim info */ |
| 71 | }; |
| 72 | struct list_head list; |
| 73 | }; |
| 74 | |
Jaegeuk Kim | 0a8165d | 2012-11-29 13:28:09 +0900 | [diff] [blame] | 75 | /* |
Jaegeuk Kim | 7bc0900 | 2012-11-02 17:13:01 +0900 | [diff] [blame] | 76 | * inline functions |
| 77 | */ |
Aravind Ramesh | de881df | 2020-07-16 18:26:56 +0530 | [diff] [blame] | 78 | |
| 79 | /* |
| 80 | * On a Zoned device zone-capacity can be less than zone-size and if |
| 81 | * zone-capacity is not aligned to f2fs segment size(2MB), then the segment |
| 82 | * starting just before zone-capacity has some blocks spanning across the |
| 83 | * zone-capacity, these blocks are not usable. |
| 84 | * Such spanning segments can be in free list so calculate the sum of usable |
| 85 | * blocks in currently free segments including normal and spanning segments. |
| 86 | */ |
| 87 | static inline block_t free_segs_blk_count_zoned(struct f2fs_sb_info *sbi) |
| 88 | { |
| 89 | block_t free_seg_blks = 0; |
| 90 | struct free_segmap_info *free_i = FREE_I(sbi); |
| 91 | int j; |
| 92 | |
| 93 | spin_lock(&free_i->segmap_lock); |
| 94 | for (j = 0; j < MAIN_SEGS(sbi); j++) |
| 95 | if (!test_bit(j, free_i->free_segmap)) |
| 96 | free_seg_blks += f2fs_usable_blks_in_seg(sbi, j); |
| 97 | spin_unlock(&free_i->segmap_lock); |
| 98 | |
| 99 | return free_seg_blks; |
| 100 | } |
| 101 | |
| 102 | static inline block_t free_segs_blk_count(struct f2fs_sb_info *sbi) |
| 103 | { |
| 104 | if (f2fs_sb_has_blkzoned(sbi)) |
| 105 | return free_segs_blk_count_zoned(sbi); |
| 106 | |
| 107 | return free_segments(sbi) << sbi->log_blocks_per_seg; |
| 108 | } |
| 109 | |
Jaegeuk Kim | 7bc0900 | 2012-11-02 17:13:01 +0900 | [diff] [blame] | 110 | static inline block_t free_user_blocks(struct f2fs_sb_info *sbi) |
| 111 | { |
Aravind Ramesh | de881df | 2020-07-16 18:26:56 +0530 | [diff] [blame] | 112 | block_t free_blks, ovp_blks; |
| 113 | |
| 114 | free_blks = free_segs_blk_count(sbi); |
| 115 | ovp_blks = overprovision_segments(sbi) << sbi->log_blocks_per_seg; |
| 116 | |
| 117 | if (free_blks < ovp_blks) |
Jaegeuk Kim | 7bc0900 | 2012-11-02 17:13:01 +0900 | [diff] [blame] | 118 | return 0; |
Aravind Ramesh | de881df | 2020-07-16 18:26:56 +0530 | [diff] [blame] | 119 | |
| 120 | return free_blks - ovp_blks; |
Jaegeuk Kim | 7bc0900 | 2012-11-02 17:13:01 +0900 | [diff] [blame] | 121 | } |
| 122 | |
| 123 | static inline block_t limit_invalid_user_blocks(struct f2fs_sb_info *sbi) |
| 124 | { |
| 125 | return (long)(sbi->user_block_count * LIMIT_INVALID_BLOCK) / 100; |
| 126 | } |
| 127 | |
| 128 | static inline block_t limit_free_user_blocks(struct f2fs_sb_info *sbi) |
| 129 | { |
| 130 | block_t reclaimable_user_blocks = sbi->user_block_count - |
| 131 | written_block_count(sbi); |
| 132 | return (long)(reclaimable_user_blocks * LIMIT_FREE_BLOCK) / 100; |
| 133 | } |
| 134 | |
Chao Yu | 88dd893 | 2015-01-26 20:24:21 +0800 | [diff] [blame] | 135 | static inline void increase_sleep_time(struct f2fs_gc_kthread *gc_th, |
Chao Yu | b8c502b | 2017-08-07 23:12:46 +0800 | [diff] [blame] | 136 | unsigned int *wait) |
Jaegeuk Kim | 7bc0900 | 2012-11-02 17:13:01 +0900 | [diff] [blame] | 137 | { |
Chao Yu | b8c502b | 2017-08-07 23:12:46 +0800 | [diff] [blame] | 138 | unsigned int min_time = gc_th->min_sleep_time; |
| 139 | unsigned int max_time = gc_th->max_sleep_time; |
| 140 | |
Chao Yu | 88dd893 | 2015-01-26 20:24:21 +0800 | [diff] [blame] | 141 | if (*wait == gc_th->no_gc_sleep_time) |
| 142 | return; |
Jaegeuk Kim | 6cb968d | 2013-04-24 13:00:14 +0900 | [diff] [blame] | 143 | |
Chao Yu | b8c502b | 2017-08-07 23:12:46 +0800 | [diff] [blame] | 144 | if ((long long)*wait + (long long)min_time > (long long)max_time) |
| 145 | *wait = max_time; |
| 146 | else |
| 147 | *wait += min_time; |
Jaegeuk Kim | 7bc0900 | 2012-11-02 17:13:01 +0900 | [diff] [blame] | 148 | } |
| 149 | |
Chao Yu | 88dd893 | 2015-01-26 20:24:21 +0800 | [diff] [blame] | 150 | static inline void decrease_sleep_time(struct f2fs_gc_kthread *gc_th, |
Chao Yu | b8c502b | 2017-08-07 23:12:46 +0800 | [diff] [blame] | 151 | unsigned int *wait) |
Jaegeuk Kim | 7bc0900 | 2012-11-02 17:13:01 +0900 | [diff] [blame] | 152 | { |
Chao Yu | b8c502b | 2017-08-07 23:12:46 +0800 | [diff] [blame] | 153 | unsigned int min_time = gc_th->min_sleep_time; |
| 154 | |
Chao Yu | 88dd893 | 2015-01-26 20:24:21 +0800 | [diff] [blame] | 155 | if (*wait == gc_th->no_gc_sleep_time) |
| 156 | *wait = gc_th->max_sleep_time; |
Jaegeuk Kim | 6cb968d | 2013-04-24 13:00:14 +0900 | [diff] [blame] | 157 | |
Chao Yu | b8c502b | 2017-08-07 23:12:46 +0800 | [diff] [blame] | 158 | if ((long long)*wait - (long long)min_time < (long long)min_time) |
| 159 | *wait = min_time; |
| 160 | else |
| 161 | *wait -= min_time; |
Jaegeuk Kim | 7bc0900 | 2012-11-02 17:13:01 +0900 | [diff] [blame] | 162 | } |
| 163 | |
| 164 | static inline bool has_enough_invalid_blocks(struct f2fs_sb_info *sbi) |
| 165 | { |
| 166 | block_t invalid_user_blocks = sbi->user_block_count - |
| 167 | written_block_count(sbi); |
| 168 | /* |
arter97 | e1c4204 | 2014-08-06 23:22:50 +0900 | [diff] [blame] | 169 | * Background GC is triggered with the following conditions. |
Jaegeuk Kim | 7bc0900 | 2012-11-02 17:13:01 +0900 | [diff] [blame] | 170 | * 1. There are a number of invalid blocks. |
| 171 | * 2. There is not enough free space. |
| 172 | */ |
| 173 | if (invalid_user_blocks > limit_invalid_user_blocks(sbi) && |
| 174 | free_user_blocks(sbi) < limit_free_user_blocks(sbi)) |
| 175 | return true; |
| 176 | return false; |
| 177 | } |