Whamcloud - gitweb
LU-12477 ldiskfs: remove obsolete ext4 patches
[fs/lustre-release.git] / ldiskfs / kernel_patches / patches / rhel7.6 / ext4-simple-blockalloc.patch
1 Index: linux-stage/fs/ext4/mballoc.c
2 ===================================================================
3 --- linux-stage.orig/fs/ext4/mballoc.c
4 +++ linux-stage/fs/ext4/mballoc.c
5 @@ -2078,6 +2078,21 @@ static int ext4_mb_good_group(struct ext
6         return 0;
7  }
8  
9 +static u64 available_blocks_count(struct ext4_sb_info *sbi)
10 +{
11 +       ext4_fsblk_t resv_blocks;
12 +       u64 bfree;
13 +       struct ext4_super_block *es = sbi->s_es;
14 +
15 +       resv_blocks = EXT4_C2B(sbi, atomic64_read(&sbi->s_resv_clusters));
16 +       bfree = percpu_counter_sum_positive(&sbi->s_freeclusters_counter) -
17 +                percpu_counter_sum_positive(&sbi->s_dirtyclusters_counter);
18 +
19 +       bfree = EXT4_C2B(sbi, max_t(s64, bfree, 0));
20 +       return bfree - (ext4_r_blocks_count(es) + resv_blocks);
21 +}
22 +
23 +
24  static noinline_for_stack int
25  ext4_mb_regular_allocator(struct ext4_allocation_context *ac)
26  {
27 @@ -2087,6 +2102,7 @@ ext4_mb_regular_allocator(struct ext4_al
28         struct ext4_sb_info *sbi;
29         struct super_block *sb;
30         struct ext4_buddy e4b;
31 +       ext4_fsblk_t avail_blocks;
32  
33         sb = ac->ac_sb;
34         sbi = EXT4_SB(sb);
35 @@ -2136,6 +2152,21 @@ ext4_mb_regular_allocator(struct ext4_al
36  
37         /* Let's just scan groups to find more-less suitable blocks */
38         cr = ac->ac_2order ? 0 : 1;
39 +
40 +       /* Choose what loop to pass based on disk fullness */
41 +       avail_blocks = available_blocks_count(sbi) ;
42 +
43 +       if (avail_blocks < sbi->s_mb_c3_blocks) {
44 +               cr = 3;
45 +               atomic64_inc(&sbi->s_bal_cX_skipped[2]);
46 +       } else if(avail_blocks < sbi->s_mb_c2_blocks) {
47 +               cr = 2;
48 +               atomic64_inc(&sbi->s_bal_cX_skipped[1]);
49 +       } else if(avail_blocks < sbi->s_mb_c1_blocks) {
50 +               cr = 1;
51 +               atomic64_inc(&sbi->s_bal_cX_skipped[0]);
52 +       }
53 +
54         /*
55          * cr == 0 try to get exact allocation,
56          * cr == 3  try to get anything
57 @@ -2193,6 +2224,9 @@ repeat:
58                         if (ac->ac_status != AC_STATUS_CONTINUE)
59                                 break;
60                 }
61 +               /* Processed all groups and haven't found blocks */
62 +               if (i == ngroups)
63 +                       atomic64_inc(&sbi->s_bal_cX_failed[cr]);
64         }
65  
66         if (ac->ac_b_ex.fe_len > 0 && ac->ac_status != AC_STATUS_FOUND &&
67 @@ -2316,6 +2350,93 @@ static const struct seq_operations ext4_
68         .show   = ext4_mb_seq_groups_show,
69  };
70  
71 +static int mb_seq_alloc_show(struct seq_file *seq, void *v)
72 +{
73 +       struct super_block *sb = seq->private;
74 +       struct ext4_sb_info *sbi = EXT4_SB(sb);
75 +
76 +       seq_printf(seq, "mballoc:\n");
77 +       seq_printf(seq, "\tblocks: %u\n", atomic_read(&sbi->s_bal_allocated));
78 +       seq_printf(seq, "\treqs: %u\n", atomic_read(&sbi->s_bal_reqs));
79 +       seq_printf(seq, "\tsuccess: %u\n", atomic_read(&sbi->s_bal_success));
80 +
81 +       seq_printf(seq, "\textents_scanned: %u\n",
82 +                  atomic_read(&sbi->s_bal_ex_scanned));
83 +       seq_printf(seq, "\t\tgoal_hits: %u\n", atomic_read(&sbi->s_bal_goals));
84 +       seq_printf(seq, "\t\t2^n_hits: %u\n", atomic_read(&sbi->s_bal_2orders));
85 +       seq_printf(seq, "\t\tbreaks: %u\n", atomic_read(&sbi->s_bal_breaks));
86 +       seq_printf(seq, "\t\tlost: %u\n", atomic_read(&sbi->s_mb_lost_chunks));
87 +
88 +       seq_printf(seq, "\tuseless_c1_loops: %lu\n",
89 +                  atomic64_read(&sbi->s_bal_cX_failed[0]));
90 +       seq_printf(seq, "\tuseless_c2_loops: %lu\n",
91 +                  atomic64_read(&sbi->s_bal_cX_failed[1]));
92 +       seq_printf(seq, "\tuseless_c3_loops: %lu\n",
93 +                  atomic64_read(&sbi->s_bal_cX_failed[2]));
94 +       seq_printf(seq, "\tskipped_c1_loops: %lu\n",
95 +                  atomic64_read(&sbi->s_bal_cX_skipped[0]));
96 +       seq_printf(seq, "\tskipped_c2_loops: %lu\n",
97 +                  atomic64_read(&sbi->s_bal_cX_skipped[1]));
98 +       seq_printf(seq, "\tskipped_c3_loops: %lu\n",
99 +                  atomic64_read(&sbi->s_bal_cX_skipped[2]));
100 +       seq_printf(seq, "\tbuddies_generated: %lu\n",
101 +                  sbi->s_mb_buddies_generated);
102 +       seq_printf(seq, "\tbuddies_time_used: %llu\n", sbi->s_mb_generation_time);
103 +       seq_printf(seq, "\tpreallocated: %u\n",
104 +                  atomic_read(&sbi->s_mb_preallocated));
105 +       seq_printf(seq, "\tdiscarded: %u\n",
106 +                  atomic_read(&sbi->s_mb_discarded));
107 +       return 0;
108 +}
109 +
110 +static ssize_t mb_seq_alloc_write(struct file *file,
111 +                             const char __user *buf,
112 +                             size_t cnt, loff_t *pos)
113 +{
114 +       struct ext4_sb_info *sbi = EXT4_SB(PDE_DATA(file_inode(file)));
115 +
116 +       atomic_set(&sbi->s_bal_allocated, 0),
117 +       atomic_set(&sbi->s_bal_reqs, 0),
118 +       atomic_set(&sbi->s_bal_success, 0);
119 +
120 +       atomic_set(&sbi->s_bal_ex_scanned, 0),
121 +       atomic_set(&sbi->s_bal_goals, 0),
122 +       atomic_set(&sbi->s_bal_2orders, 0),
123 +       atomic_set(&sbi->s_bal_breaks, 0),
124 +       atomic_set(&sbi->s_mb_lost_chunks, 0);
125 +
126 +       atomic64_set(&sbi->s_bal_cX_failed[0], 0),
127 +       atomic64_set(&sbi->s_bal_cX_failed[1], 0),
128 +       atomic64_set(&sbi->s_bal_cX_failed[2], 0);
129 +
130 +       atomic64_set(&sbi->s_bal_cX_skipped[0], 0),
131 +       atomic64_set(&sbi->s_bal_cX_skipped[1], 0),
132 +       atomic64_set(&sbi->s_bal_cX_skipped[2], 0);
133 +
134 +
135 +       sbi->s_mb_buddies_generated = 0;
136 +       sbi->s_mb_generation_time = 0;
137 +
138 +       atomic_set(&sbi->s_mb_preallocated, 0),
139 +       atomic_set(&sbi->s_mb_discarded, 0);
140 +
141 +       return cnt;
142 +}
143 +
144 +static int mb_seq_alloc_open(struct inode *inode, struct file *file)
145 +{
146 +       return single_open(file, mb_seq_alloc_show, PDE_DATA(inode));
147 +}
148 +
149 +static const struct file_operations ext4_mb_seq_alloc_fops = {
150 +       .owner          = THIS_MODULE,
151 +       .open           = mb_seq_alloc_open,
152 +       .read           = seq_read,
153 +       .llseek         = seq_lseek,
154 +       .release        = single_release,
155 +       .write          = mb_seq_alloc_write,
156 +};
157 +
158  #define EXT4_MB_PREALLOC_TABLE          "prealloc_table"
159  
160  static int ext4_mb_check_and_update_prealloc(struct ext4_sb_info *sbi,
161 @@ -2730,6 +2851,7 @@ static int ext4_groupinfo_create_slab(si
162         return 0;
163  }
164  
165 +#define THRESHOLD_BLOCKS(ts) (ext4_blocks_count(sbi->s_es) / 100 * ts)
166  int ext4_mb_init(struct super_block *sb)
167  {
168         struct ext4_sb_info *sbi = EXT4_SB(sb);
169 @@ -2781,6 +2903,9 @@ int ext4_mb_init(struct super_block *sb)
170         sbi->s_mb_min_to_scan = MB_DEFAULT_MIN_TO_SCAN;
171         sbi->s_mb_stats = MB_DEFAULT_STATS;
172         sbi->s_mb_order2_reqs = MB_DEFAULT_ORDER2_REQS;
173 +       sbi->s_mb_c1_blocks = THRESHOLD_BLOCKS(MB_DEFAULT_C1_THRESHOLD);
174 +       sbi->s_mb_c2_blocks = THRESHOLD_BLOCKS(MB_DEFAULT_C2_THRESHOLD);
175 +       sbi->s_mb_c3_blocks = THRESHOLD_BLOCKS(MB_DEFAULT_C3_THRESHOLD);
176         /*
177          * The default group preallocation is 512, which for 4k block
178          * sizes translates to 2 megabytes.  However for bigalloc file
179 @@ -2853,6 +2978,8 @@ int ext4_mb_init(struct super_block *sb)
180                 proc_create_data(EXT4_MB_PREALLOC_TABLE, S_IFREG | S_IRUGO |
181                                  S_IWUSR, sbi->s_proc,
182                                  &ext4_mb_prealloc_seq_fops, sb);
183 +               proc_create_data("mb_alloc_stats", S_IFREG | S_IRUGO | S_IWUSR,
184 +                                sbi->s_proc, &ext4_mb_seq_alloc_fops, sb);
185                 proc_create_data("mb_last_group", S_IFREG | S_IRUGO |
186                                  S_IWUSR, sbi->s_proc,
187                                  &ext4_mb_seq_last_group_fops, sb);
188 @@ -2906,6 +3033,7 @@ int ext4_mb_release(struct super_block *
189                 remove_proc_entry("mb_last_group", sbi->s_proc);
190                 remove_proc_entry("mb_last_start", sbi->s_proc);
191                 remove_proc_entry(EXT4_MB_PREALLOC_TABLE, sbi->s_proc);
192 +               remove_proc_entry("mb_alloc_stats", sbi->s_proc);
193         }
194  
195         if (sbi->s_group_info) {
196 @@ -2936,6 +3064,16 @@ int ext4_mb_release(struct super_block *
197                                 atomic_read(&sbi->s_bal_reqs),
198                                 atomic_read(&sbi->s_bal_success));
199                 ext4_msg(sb, KERN_INFO,
200 +                       "mballoc: (%lu, %lu, %lu) useless c(0,1,2) loops",
201 +                               atomic64_read(&sbi->s_bal_cX_failed[0]),
202 +                               atomic64_read(&sbi->s_bal_cX_failed[1]),
203 +                               atomic64_read(&sbi->s_bal_cX_failed[2]));
204 +               ext4_msg(sb, KERN_INFO,
205 +                       "mballoc: (%lu, %lu, %lu) skipped c(0,1,2) loops",
206 +                               atomic64_read(&sbi->s_bal_cX_skipped[0]),
207 +                               atomic64_read(&sbi->s_bal_cX_skipped[1]),
208 +                               atomic64_read(&sbi->s_bal_cX_skipped[2]));
209 +               ext4_msg(sb, KERN_INFO,
210                       "mballoc: %u extents scanned, %u goal hits, "
211                                 "%u 2^N hits, %u breaks, %u lost",
212                                 atomic_read(&sbi->s_bal_ex_scanned),
213 Index: linux-stage/fs/ext4/ext4.h
214 ===================================================================
215 --- linux-stage.orig/fs/ext4/ext4.h
216 +++ linux-stage/fs/ext4/ext4.h
217 @@ -1409,6 +1409,9 @@ struct ext4_sb_info {
218         unsigned int s_mb_min_to_scan;
219         unsigned int s_mb_stats;
220         unsigned int s_mb_order2_reqs;
221 +       ext4_fsblk_t s_mb_c1_blocks;
222 +       ext4_fsblk_t s_mb_c2_blocks;
223 +       ext4_fsblk_t s_mb_c3_blocks;
224         unsigned long *s_mb_prealloc_table;
225         unsigned int s_mb_group_prealloc;
226         unsigned int s_max_dir_size_kb;
227 @@ -1425,6 +1428,9 @@ struct ext4_sb_info {
228         atomic_t s_bal_goals;   /* goal hits */
229         atomic_t s_bal_breaks;  /* too long searches */
230         atomic_t s_bal_2orders; /* 2^order hits */
231 +       /* cX loop didn't find blocks */
232 +       atomic64_t s_bal_cX_failed[3];
233 +       atomic64_t s_bal_cX_skipped[3];
234         spinlock_t s_bal_lock;
235         unsigned long s_mb_buddies_generated;
236         unsigned long long s_mb_generation_time;
237 Index: linux-stage/fs/ext4/super.c
238 ===================================================================
239 --- linux-stage.orig/fs/ext4/super.c
240 +++ linux-stage/fs/ext4/super.c
241 @@ -2734,6 +2734,73 @@ static ssize_t sbi_deprecated_show(struc
242         return snprintf(buf, PAGE_SIZE, "%d\n", a->u.deprecated_val);
243  }
244  
245 +static int save_threshold(struct ext4_sb_info *sbi, const char *buf,
246 +                        ext4_fsblk_t *blocks) {
247 +       unsigned long long val;
248 +
249 +       if (!parse_strtoull(buf, 100, &val)) {
250 +               *blocks = val * ext4_blocks_count(sbi->s_es) / 100;
251 +               return 0;
252 +       }
253 +
254 +       return -EINVAL;
255 +}
256 +
257 +#define THRESHOLD_PERCENT(ts) (ts * 100 / ext4_blocks_count(sbi->s_es))
258 +static ssize_t mb_c1_threshold_store(struct ext4_attr *a,
259 +                                   struct ext4_sb_info *sbi,
260 +                                   const char *buf, size_t count)
261 +{
262 +       int ret;
263 +
264 +       ret = save_threshold(sbi, buf, &sbi->s_mb_c1_blocks);
265 +
266 +       return ret ? ret : count;
267 +}
268 +
269 +static ssize_t mb_c1_threshold_show(struct ext4_attr *a,
270 +                                  struct ext4_sb_info *sbi, char *buf)
271 +{
272 +       return snprintf(buf, PAGE_SIZE, "%llu\n",
273 +                       THRESHOLD_PERCENT(sbi->s_mb_c1_blocks));
274 +}
275 +
276 +static ssize_t mb_c2_threshold_store(struct ext4_attr *a,
277 +                                   struct ext4_sb_info *sbi,
278 +                                   const char *buf, size_t count)
279 +{
280 +       int ret;
281 +
282 +       ret = save_threshold(sbi, buf, &sbi->s_mb_c2_blocks);
283 +       return ret ? ret : count;
284 +}
285 +
286 +static ssize_t mb_c2_threshold_show(struct ext4_attr *a,
287 +                                  struct ext4_sb_info *sbi, char *buf)
288 +{
289 +               return snprintf(buf, PAGE_SIZE, "%llu\n",
290 +                               THRESHOLD_PERCENT(sbi->s_mb_c2_blocks));
291 +}
292 +
293 +static ssize_t mb_c3_threshold_store(struct ext4_attr *a,
294 +                                   struct ext4_sb_info *sbi,
295 +                                   const char *buf, size_t count)
296 +{
297 +       int ret;
298 +
299 +       ret = save_threshold(sbi, buf, &sbi->s_mb_c3_blocks);
300 +
301 +       return ret ? ret : count;
302 +}
303 +
304 +static ssize_t mb_c3_threshold_show(struct ext4_attr *a,
305 +                                  struct ext4_sb_info *sbi, char *buf)
306 +{
307 +               return snprintf(buf, PAGE_SIZE, "%llu\n",
308 +                               THRESHOLD_PERCENT(sbi->s_mb_c3_blocks));
309 +}
310 +
311 +
312  #define EXT4_ATTR_OFFSET(_name,_mode,_show,_store,_elname) \
313  static struct ext4_attr ext4_attr_##_name = {                  \
314         .attr = {.name = __stringify(_name), .mode = _mode },   \
315 @@ -2790,6 +2857,9 @@ EXT4_RW_ATTR_SBI_UI(mb_stats, s_mb_stats
316  EXT4_RW_ATTR_SBI_UI(mb_max_to_scan, s_mb_max_to_scan);
317  EXT4_RW_ATTR_SBI_UI(mb_min_to_scan, s_mb_min_to_scan);
318  EXT4_RW_ATTR_SBI_UI(mb_order2_req, s_mb_order2_reqs);
319 +EXT4_RW_ATTR(mb_c1_threshold);
320 +EXT4_RW_ATTR(mb_c2_threshold);
321 +EXT4_RW_ATTR(mb_c3_threshold);
322  EXT4_RW_ATTR_SBI_UI(mb_small_req, s_mb_small_req);
323  EXT4_RW_ATTR_SBI_UI(mb_large_req, s_mb_large_req);
324  EXT4_RW_ATTR_SBI_UI(mb_group_prealloc, s_mb_group_prealloc);
325 @@ -2820,6 +2890,9 @@ static struct attribute *ext4_attrs[] =
326         ATTR_LIST(mb_max_to_scan),
327         ATTR_LIST(mb_min_to_scan),
328         ATTR_LIST(mb_order2_req),
329 +       ATTR_LIST(mb_c1_threshold),
330 +       ATTR_LIST(mb_c2_threshold),
331 +       ATTR_LIST(mb_c3_threshold),
332         ATTR_LIST(mb_small_req),
333         ATTR_LIST(mb_large_req),
334         ATTR_LIST(mb_group_prealloc),
335 Index: linux-stage/fs/ext4/mballoc.h
336 ===================================================================
337 --- linux-stage.orig/fs/ext4/mballoc.h
338 +++ linux-stage/fs/ext4/mballoc.h
339 @@ -84,6 +84,9 @@ extern ushort ext4_mballoc_debug;
340   * for which requests use 2^N search using buddies
341   */
342  #define MB_DEFAULT_ORDER2_REQS         8
343 +#define MB_DEFAULT_C1_THRESHOLD                25
344 +#define MB_DEFAULT_C2_THRESHOLD                15
345 +#define MB_DEFAULT_C3_THRESHOLD                5
346  
347  /*
348   * default group prealloc size 512 blocks