Whamcloud - gitweb
LU-9816 kernel: kernel upgrade RHEL7.4 [3.10.0-693.el7]
[fs/lustre-release.git] / ldiskfs / kernel_patches / patches / rhel7.4 / ext4-prealloc.patch
1 Index: linux-3.10.0-514.16.1.el7.x86_64/fs/ext4/ext4.h
2 ===================================================================
3 --- linux-3.10.0-514.16.1.el7.x86_64.orig/fs/ext4/ext4.h
4 +++ linux-3.10.0-514.16.1.el7.x86_64/fs/ext4/ext4.h
5 @@ -1270,11 +1270,14 @@ struct ext4_sb_info {
6  
7         /* tunables */
8         unsigned long s_stripe;
9 -       unsigned int s_mb_stream_request;
10 +       unsigned long s_mb_small_req;
11 +       unsigned long s_mb_large_req;
12         unsigned int s_mb_max_to_scan;
13         unsigned int s_mb_min_to_scan;
14         unsigned int s_mb_stats;
15         unsigned int s_mb_order2_reqs;
16 +       unsigned long *s_mb_prealloc_table;
17 +       unsigned long s_mb_prealloc_table_size;
18         unsigned int s_mb_group_prealloc;
19         unsigned int s_max_dir_size_kb;
20         /* where last allocation was done - for stream allocation */
21 Index: linux-3.10.0-514.16.1.el7.x86_64/fs/ext4/mballoc.c
22 ===================================================================
23 --- linux-3.10.0-514.16.1.el7.x86_64.orig/fs/ext4/mballoc.c
24 +++ linux-3.10.0-514.16.1.el7.x86_64/fs/ext4/mballoc.c
25 @@ -1862,6 +1862,26 @@ int ext4_mb_find_by_goal(struct ext4_all
26         return 0;
27  }
28  
29 +static int ext4_mb_prealloc_table_add(struct ext4_sb_info *sbi, int value)
30 +{
31 +       int i;
32 +
33 +       if (value > (sbi->s_blocks_per_group - 1 - 1 - sbi->s_itb_per_group))
34 +               return -1;
35 +
36 +       for (i = 0; i < sbi->s_mb_prealloc_table_size; i++) {
37 +               if (sbi->s_mb_prealloc_table[i] == 0) {
38 +                       sbi->s_mb_prealloc_table[i] = value;
39 +                       return 0;
40 +               }
41 +
42 +               /* they should add values in order */
43 +               if (value <= sbi->s_mb_prealloc_table[i])
44 +                       return -1;
45 +       }
46 +       return -1;
47 +}
48 +
49  /*
50   * The routine scans buddy structures (not bitmap!) from given order
51   * to max order and tries to find big enough chunk to satisfy the req
52 @@ -2301,6 +2321,93 @@ static const struct seq_operations ext4_
53         .show   = ext4_mb_seq_groups_show,
54  };
55  
56 +#define EXT4_MB_PREALLOC_TABLE          "prealloc_table"
57 +
58 +static ssize_t ext4_mb_prealloc_table_proc_write(struct file *file,
59 +                                            const char __user *buf,
60 +                                            size_t cnt, loff_t *pos)
61 +{
62 +       struct ext4_sb_info *sbi = EXT4_SB(PDE_DATA(file_inode(file)));
63 +       unsigned long value;
64 +       unsigned long prev = 0;
65 +       char str[128];
66 +       char *cur;
67 +       char *end;
68 +       unsigned long *new_table;
69 +       int num = 0;
70 +       int i = 0;
71 +
72 +       if (cnt >= sizeof(str))
73 +               return -EINVAL;
74 +       if (copy_from_user(str, buf, cnt))
75 +               return -EFAULT;
76 +
77 +       num = 0;
78 +       cur = str;
79 +       end = str + cnt;
80 +       while (cur < end) {
81 +               int rc;
82 +               while ((cur < end) && (*cur == ' '))
83 +                       cur++;
84 +               rc = kstrtol(cur, 0, &value);
85 +               if (rc != 0)
86 +                       return -EINVAL;
87 +               if (value == 0)
88 +                       break;
89 +               if (value <= prev)
90 +                       return -EINVAL;
91 +               prev = value;
92 +               num++;
93 +       }
94 +
95 +       new_table = kmalloc(num * sizeof(*new_table), GFP_KERNEL);
96 +       if (new_table == NULL)
97 +               return -ENOMEM;
98 +       kfree(sbi->s_mb_prealloc_table);
99 +       memset(new_table, 0, num * sizeof(*new_table));
100 +       sbi->s_mb_prealloc_table = new_table;
101 +       sbi->s_mb_prealloc_table_size = num;
102 +       cur = str;
103 +       end = str + cnt;
104 +       while (cur < end && i < num) {
105 +               while (cur < end && *cur == ' ')
106 +                       cur++;
107 +               value = simple_strtol(cur, &cur, 0);
108 +               if (ext4_mb_prealloc_table_add(sbi, value) == 0)
109 +                       ++i;
110 +       }
111 +       if (i != num)
112 +               sbi->s_mb_prealloc_table_size = i;
113 +
114 +       return cnt;
115 +}
116 +
117 +static int mb_prealloc_table_seq_show(struct seq_file *m, void *v)
118 +{
119 +       struct ext4_sb_info *sbi = EXT4_SB(m->private);
120 +       int i;
121 +
122 +       for (i = 0; i < sbi->s_mb_prealloc_table_size; i++)
123 +               seq_printf(m, "%ld ", sbi->s_mb_prealloc_table[i]);
124 +       seq_printf(m, "\n");
125 +
126 +       return 0;
127 +}
128 +
129 +static int mb_prealloc_table_seq_open(struct inode *inode, struct file *file)
130 +{
131 +       return single_open(file, mb_prealloc_table_seq_show, PDE_DATA(inode));
132 +}
133 +
134 +static const struct file_operations ext4_mb_prealloc_seq_fops = {
135 +       .owner   = THIS_MODULE,
136 +       .open    = mb_prealloc_table_seq_open,
137 +       .read    = seq_read,
138 +       .llseek  = seq_lseek,
139 +       .release = single_release,
140 +       .write   = ext4_mb_prealloc_table_proc_write,
141 +};
142 +
143  static int ext4_mb_seq_groups_open(struct inode *inode, struct file *file)
144  {
145         struct super_block *sb = PDE_DATA(inode);
146 @@ -2550,7 +2657,7 @@ static int ext4_groupinfo_create_slab(si
147  int ext4_mb_init(struct super_block *sb)
148  {
149         struct ext4_sb_info *sbi = EXT4_SB(sb);
150 -       unsigned i, j;
151 +       unsigned i, j, k, l;
152         unsigned offset, offset_incr;
153         unsigned max;
154         int ret;
155 @@ -2595,7 +2702,6 @@ int ext4_mb_init(struct super_block *sb)
156         sbi->s_mb_max_to_scan = MB_DEFAULT_MAX_TO_SCAN;
157         sbi->s_mb_min_to_scan = MB_DEFAULT_MIN_TO_SCAN;
158         sbi->s_mb_stats = MB_DEFAULT_STATS;
159 -       sbi->s_mb_stream_request = MB_DEFAULT_STREAM_THRESHOLD;
160         sbi->s_mb_order2_reqs = MB_DEFAULT_ORDER2_REQS;
161         /*
162          * The default group preallocation is 512, which for 4k block
163 @@ -2619,9 +2725,47 @@ int ext4_mb_init(struct super_block *sb)
164          * RAID stripe size so that preallocations don't fragment
165          * the stripes.
166          */
167 -       if (sbi->s_stripe > 1) {
168 -               sbi->s_mb_group_prealloc = roundup(
169 -                       sbi->s_mb_group_prealloc, sbi->s_stripe);
170 +
171 +       if (sbi->s_stripe == 0) {
172 +               sbi->s_mb_prealloc_table_size = 10;
173 +               i = sbi->s_mb_prealloc_table_size * sizeof(unsigned long);
174 +               sbi->s_mb_prealloc_table = kmalloc(i, GFP_NOFS);
175 +               if (sbi->s_mb_prealloc_table == NULL) {
176 +                       ret = -ENOMEM;
177 +                       goto out;
178 +               }
179 +               memset(sbi->s_mb_prealloc_table, 0, i);
180 +
181 +               for (k = 0, l = 4; k <= 9; ++k, l *= 2) {
182 +                       if (ext4_mb_prealloc_table_add(sbi, l) < 0) {
183 +                               sbi->s_mb_prealloc_table_size = k;
184 +                               break;
185 +                       }
186 +               }
187 +
188 +               sbi->s_mb_small_req = 256;
189 +               sbi->s_mb_large_req = 1024;
190 +               sbi->s_mb_group_prealloc = 512;
191 +       } else {
192 +               sbi->s_mb_prealloc_table_size = 3;
193 +               i = sbi->s_mb_prealloc_table_size * sizeof(unsigned long);
194 +               sbi->s_mb_prealloc_table = kmalloc(i, GFP_NOFS);
195 +               if (sbi->s_mb_prealloc_table == NULL) {
196 +                       ret = -ENOMEM;
197 +                       goto out;
198 +               }
199 +               memset(sbi->s_mb_prealloc_table, 0, i);
200 +
201 +               for (k = 0, l = sbi->s_stripe; k <= 2; ++k, l *= 2) {
202 +                       if (ext4_mb_prealloc_table_add(sbi, l) < 0) {
203 +                               sbi->s_mb_prealloc_table_size = k;
204 +                               break;
205 +                       }
206 +               }
207 +
208 +               sbi->s_mb_small_req = sbi->s_stripe;
209 +               sbi->s_mb_large_req = sbi->s_stripe * 8;
210 +               sbi->s_mb_group_prealloc = sbi->s_stripe * 4;
211         }
212  
213         sbi->s_locality_groups = alloc_percpu(struct ext4_locality_group);
214 @@ -2643,9 +2787,13 @@ int ext4_mb_init(struct super_block *sb)
215         if (ret != 0)
216                 goto out_free_locality_groups;
217  
218 -       if (sbi->s_proc)
219 +       if (sbi->s_proc) {
220                 proc_create_data("mb_groups", S_IRUGO, sbi->s_proc,
221                                  &ext4_mb_seq_groups_fops, sb);
222 +               proc_create_data(EXT4_MB_PREALLOC_TABLE, S_IFREG | S_IRUGO |
223 +                                S_IWUSR, sbi->s_proc,
224 +                                &ext4_mb_prealloc_seq_fops, sb);
225 +       }
226  
227         return 0;
228  
229 @@ -2653,6 +2801,7 @@ out_free_locality_groups:
230         free_percpu(sbi->s_locality_groups);
231         sbi->s_locality_groups = NULL;
232  out:
233 +       kfree(sbi->s_mb_prealloc_table);
234         kfree(sbi->s_mb_offsets);
235         sbi->s_mb_offsets = NULL;
236         kfree(sbi->s_mb_maxs);
237 @@ -2687,8 +2836,10 @@ int ext4_mb_release(struct super_block *
238         struct ext4_sb_info *sbi = EXT4_SB(sb);
239         struct kmem_cache *cachep = get_groupinfo_cache(sb->s_blocksize_bits);
240  
241 -       if (sbi->s_proc)
242 +       if (sbi->s_proc) {
243                 remove_proc_entry("mb_groups", sbi->s_proc);
244 +               remove_proc_entry(EXT4_MB_PREALLOC_TABLE, sbi->s_proc);
245 +       }
246  
247         if (sbi->s_group_info) {
248                 for (i = 0; i < ngroups; i++) {
249 @@ -3000,9 +3151,9 @@ ext4_mb_normalize_request(struct ext4_al
250                                 struct ext4_allocation_request *ar)
251  {
252         struct ext4_sb_info *sbi = EXT4_SB(ac->ac_sb);
253 -       int bsbits, max;
254 +       int bsbits, i, wind;
255         ext4_lblk_t end;
256 -       loff_t size, start_off;
257 +       loff_t size;
258         loff_t orig_size __maybe_unused;
259         ext4_lblk_t start;
260         struct ext4_inode_info *ei = EXT4_I(ac->ac_inode);
261 @@ -3035,51 +3186,34 @@ ext4_mb_normalize_request(struct ext4_al
262         size = size << bsbits;
263         if (size < i_size_read(ac->ac_inode))
264                 size = i_size_read(ac->ac_inode);
265 -       orig_size = size;
266 +       size = (size + ac->ac_sb->s_blocksize - 1) >> bsbits;
267  
268 -       /* max size of free chunks */
269 -       max = 2 << bsbits;
270 +       start = wind = 0;
271  
272 -#define NRL_CHECK_SIZE(req, size, max, chunk_size)     \
273 -               (req <= (size) || max <= (chunk_size))
274 +       /* let's choose preallocation window depending on file size */
275 +       for (i = 0; i < sbi->s_mb_prealloc_table_size; i++) {
276 +               if (size <= sbi->s_mb_prealloc_table[i]) {
277 +                       wind = sbi->s_mb_prealloc_table[i];
278 +                       break;
279 +               }
280 +       }
281 +       size = wind;
282  
283 -       /* first, try to predict filesize */
284 -       /* XXX: should this table be tunable? */
285 -       start_off = 0;
286 -       if (size <= 16 * 1024) {
287 -               size = 16 * 1024;
288 -       } else if (size <= 32 * 1024) {
289 -               size = 32 * 1024;
290 -       } else if (size <= 64 * 1024) {
291 -               size = 64 * 1024;
292 -       } else if (size <= 128 * 1024) {
293 -               size = 128 * 1024;
294 -       } else if (size <= 256 * 1024) {
295 -               size = 256 * 1024;
296 -       } else if (size <= 512 * 1024) {
297 -               size = 512 * 1024;
298 -       } else if (size <= 1024 * 1024) {
299 -               size = 1024 * 1024;
300 -       } else if (NRL_CHECK_SIZE(size, 4 * 1024 * 1024, max, 2 * 1024)) {
301 -               start_off = ((loff_t)ac->ac_o_ex.fe_logical >>
302 -                                               (21 - bsbits)) << 21;
303 -               size = 2 * 1024 * 1024;
304 -       } else if (NRL_CHECK_SIZE(size, 8 * 1024 * 1024, max, 4 * 1024)) {
305 -               start_off = ((loff_t)ac->ac_o_ex.fe_logical >>
306 -                                                       (22 - bsbits)) << 22;
307 -               size = 4 * 1024 * 1024;
308 -       } else if (NRL_CHECK_SIZE(ac->ac_o_ex.fe_len,
309 -                                       (8<<20)>>bsbits, max, 8 * 1024)) {
310 -               start_off = ((loff_t)ac->ac_o_ex.fe_logical >>
311 -                                                       (23 - bsbits)) << 23;
312 -               size = 8 * 1024 * 1024;
313 -       } else {
314 -               start_off = (loff_t) ac->ac_o_ex.fe_logical << bsbits;
315 -               size      = (loff_t) EXT4_C2B(EXT4_SB(ac->ac_sb),
316 -                                             ac->ac_o_ex.fe_len) << bsbits;
317 +       if (wind == 0) {
318 +               __u64 tstart, tend;
319 +               /* file is quite large, we now preallocate with
320 +                * the biggest configured window with regart to
321 +                * logical offset */
322 +               wind = sbi->s_mb_prealloc_table[i - 1];
323 +               tstart = ac->ac_o_ex.fe_logical;
324 +               do_div(tstart, wind);
325 +               start = tstart * wind;
326 +               tend = ac->ac_o_ex.fe_logical + ac->ac_o_ex.fe_len - 1;
327 +               do_div(tend, wind);
328 +               tend = tend * wind + wind;
329 +               size = tend - start;
330         }
331 -       size = size >> bsbits;
332 -       start = start_off >> bsbits;
333 +       orig_size = size;
334  
335         /* don't cover already allocated blocks in selected range */
336         if (ar->pleft && start <= ar->lleft) {
337 @@ -3154,7 +3288,6 @@ ext4_mb_normalize_request(struct ext4_al
338                          (unsigned long) ac->ac_o_ex.fe_logical);
339                 BUG();
340         }
341 -       BUG_ON(size <= 0 || size > EXT4_BLOCKS_PER_GROUP(ac->ac_sb));
342  
343         /* now prepare goal request */
344  
345 @@ -4119,11 +4252,19 @@ static void ext4_mb_group_or_file(struct
346  
347         /* don't use group allocation for large files */
348         size = max(size, isize);
349 -       if (size > sbi->s_mb_stream_request) {
350 +       if ((ac->ac_o_ex.fe_len >= sbi->s_mb_small_req) ||
351 +           (size >= sbi->s_mb_large_req)) {
352                 ac->ac_flags |= EXT4_MB_STREAM_ALLOC;
353                 return;
354         }
355  
356 +       /*
357 +        * request is so large that we don't care about
358 +        * streaming - it overweights any possible seek
359 +        */
360 +       if (ac->ac_o_ex.fe_len >= sbi->s_mb_large_req)
361 +               return;
362 +
363         BUG_ON(ac->ac_lg != NULL);
364         /*
365          * locality group prealloc space are per cpu. The reason for having
366 Index: linux-3.10.0-514.16.1.el7.x86_64/fs/ext4/super.c
367 ===================================================================
368 --- linux-3.10.0-514.16.1.el7.x86_64.orig/fs/ext4/super.c
369 +++ linux-3.10.0-514.16.1.el7.x86_64/fs/ext4/super.c
370 @@ -2672,7 +2672,8 @@ EXT4_RW_ATTR_SBI_UI(mb_stats, s_mb_stats
371  EXT4_RW_ATTR_SBI_UI(mb_max_to_scan, s_mb_max_to_scan);
372  EXT4_RW_ATTR_SBI_UI(mb_min_to_scan, s_mb_min_to_scan);
373  EXT4_RW_ATTR_SBI_UI(mb_order2_req, s_mb_order2_reqs);
374 -EXT4_RW_ATTR_SBI_UI(mb_stream_req, s_mb_stream_request);
375 +EXT4_RW_ATTR_SBI_UI(mb_small_req, s_mb_small_req);
376 +EXT4_RW_ATTR_SBI_UI(mb_large_req, s_mb_large_req);
377  EXT4_RW_ATTR_SBI_UI(mb_group_prealloc, s_mb_group_prealloc);
378  EXT4_DEPRECATED_ATTR(max_writeback_mb_bump, 128);
379  EXT4_RW_ATTR_SBI_UI(extent_max_zeroout_kb, s_extent_max_zeroout_kb);
380 @@ -2698,7 +2699,8 @@ static struct attribute *ext4_attrs[] =
381         ATTR_LIST(mb_max_to_scan),
382         ATTR_LIST(mb_min_to_scan),
383         ATTR_LIST(mb_order2_req),
384 -       ATTR_LIST(mb_stream_req),
385 +       ATTR_LIST(mb_small_req),
386 +       ATTR_LIST(mb_large_req),
387         ATTR_LIST(mb_group_prealloc),
388         ATTR_LIST(max_writeback_mb_bump),
389         ATTR_LIST(extent_max_zeroout_kb),
390 Index: linux-3.10.0-514.16.1.el7.x86_64/fs/ext4/inode.c
391 ===================================================================
392 --- linux-3.10.0-514.16.1.el7.x86_64.orig/fs/ext4/inode.c
393 +++ linux-3.10.0-514.16.1.el7.x86_64/fs/ext4/inode.c
394 @@ -2399,6 +2399,9 @@ static int ext4_writepages(struct addres
395                 ext4_journal_stop(handle);
396         }
397  
398 +       if (wbc->nr_to_write < sbi->s_mb_small_req)
399 +               wbc->nr_to_write = sbi->s_mb_small_req;
400 +
401         if (wbc->range_start == 0 && wbc->range_end == LLONG_MAX)
402                 range_whole = 1;
403