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