Whamcloud - gitweb
LU-9236 kernel: new kernel RHEL 6.9 [2.6.32-696.el6]
[fs/lustre-release.git] / ldiskfs / kernel_patches / patches / rhel6.9 / 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 @@ -1136,11 +1136,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_writeback_mb_bump;
20         /* where last allocation was done - for stream allocation */
21 Index: linux-stage/fs/ext4/mballoc.c
22 ===================================================================
23 --- linux-stage.orig/fs/ext4/mballoc.c
24 +++ linux-stage/fs/ext4/mballoc.c
25 @@ -1838,6 +1838,26 @@ void ext4_mb_complex_scan_group(struct e
26         ext4_mb_check_limits(ac, e4b, 1);
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   * This is a special case for storages like raid5
51   * we try to find stripe-aligned chunks for stripe-size requests
52 @@ -2155,6 +2175,82 @@ 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 int ext4_mb_prealloc_table_proc_read(char *page, char **start, off_t off,
59 +                                           int count, int *eof, void *data)
60 +{
61 +       struct ext4_sb_info *sbi = data;
62 +       int len = 0;
63 +       int i;
64 +
65 +       *eof = 1;
66 +       if (off != 0)
67 +               return 0;
68 +
69 +       for (i = 0; i < sbi->s_mb_prealloc_table_size; i++)
70 +               len += sprintf(page + len, "%ld ",
71 +                              sbi->s_mb_prealloc_table[i]);
72 +       len += sprintf(page + len, "\n");
73 +
74 +       *start = page;
75 +       return len;
76 +}
77 +
78 +static int ext4_mb_prealloc_table_proc_write(struct file *file,
79 +                                            const char __user *buf,
80 +                                            unsigned long cnt, void *data)
81 +{
82 +       struct ext4_sb_info *sbi = data;
83 +       unsigned long value;
84 +       unsigned long prev = 0;
85 +       char str[128];
86 +       char *cur;
87 +       char *end;
88 +       unsigned long *new_table;
89 +       int num = 0;
90 +       int i = 0;
91 +
92 +       if (cnt >= sizeof(str))
93 +               return -EINVAL;
94 +       if (copy_from_user(str, buf, cnt))
95 +               return -EFAULT;
96 +
97 +       num = 0;
98 +       cur = str;
99 +       end = str + cnt;
100 +       while (cur < end) {
101 +               while ((cur < end) && (*cur == ' ')) cur++;
102 +               value = simple_strtol(cur, &cur, 0);
103 +               if (value == 0)
104 +                       break;
105 +               if (value <= prev)
106 +                       return -EINVAL;
107 +               prev = value;
108 +               num++;
109 +       }
110 +
111 +       new_table = kmalloc(num * sizeof(*new_table), GFP_KERNEL);
112 +       if (new_table == NULL)
113 +               return -ENOMEM;
114 +       kfree(sbi->s_mb_prealloc_table);
115 +       memset(new_table, 0, num * sizeof(*new_table));
116 +       sbi->s_mb_prealloc_table = new_table;
117 +       sbi->s_mb_prealloc_table_size = num;
118 +       cur = str;
119 +       end = str + cnt;
120 +       while (cur < end && i < num) {
121 +       while ((cur < end) && (*cur == ' ')) cur++;
122 +               value = simple_strtol(cur, &cur, 0);
123 +               if (ext4_mb_prealloc_table_add(sbi, value) == 0)
124 +                       ++i;
125 +       }
126 +       if (i != num)
127 +               sbi->s_mb_prealloc_table_size = i;
128 +
129 +       return cnt;
130 +}
131 +
132  static int ext4_mb_seq_groups_open(struct inode *inode, struct file *file)
133  {
134         struct super_block *sb = PDE(inode)->data;
135 @@ -2346,7 +2442,7 @@ err_freesgi:
136  int ext4_mb_init(struct super_block *sb, int needs_recovery)
137  {
138         struct ext4_sb_info *sbi = EXT4_SB(sb);
139 -       unsigned i, j;
140 +       unsigned i, j, k, l;
141         unsigned offset, offset_incr;
142         unsigned max;
143         int ret;
144 @@ -2380,26 +2476,61 @@ int ext4_mb_init(struct super_block *sb,
145                 i++;
146         } while (i <= sb->s_blocksize_bits + 1);
147  
148 -       /* init file for buddy data */
149 -       ret = ext4_mb_init_backend(sb);
150 -       if (ret != 0) {
151 -               kfree(sbi->s_mb_offsets);
152 -               kfree(sbi->s_mb_maxs);
153 -               return ret;
154 -       }
155 -
156         spin_lock_init(&sbi->s_md_lock);
157         spin_lock_init(&sbi->s_bal_lock);
158  
159         sbi->s_mb_max_to_scan = MB_DEFAULT_MAX_TO_SCAN;
160         sbi->s_mb_min_to_scan = MB_DEFAULT_MIN_TO_SCAN;
161         sbi->s_mb_stats = MB_DEFAULT_STATS;
162 -       sbi->s_mb_stream_request = MB_DEFAULT_STREAM_THRESHOLD;
163         sbi->s_mb_order2_reqs = MB_DEFAULT_ORDER2_REQS;
164 -       sbi->s_mb_group_prealloc = MB_DEFAULT_GROUP_PREALLOC;
165 +
166 +       if (sbi->s_stripe == 0) {
167 +               sbi->s_mb_prealloc_table_size = 10;
168 +               i = sbi->s_mb_prealloc_table_size * sizeof(unsigned long);
169 +               sbi->s_mb_prealloc_table = kmalloc(i, GFP_NOFS);
170 +               if (sbi->s_mb_prealloc_table == NULL) {
171 +                       kfree(sbi->s_mb_offsets);
172 +                       kfree(sbi->s_mb_maxs);
173 +                       return -ENOMEM;
174 +               }
175 +               memset(sbi->s_mb_prealloc_table, 0, i);
176 +
177 +               for (k = 0, l = 4; k <= 9; ++k, l *= 2) {
178 +                       if (ext4_mb_prealloc_table_add(sbi, l) < 0) {
179 +                               sbi->s_mb_prealloc_table_size = k;
180 +                               break;
181 +                       }
182 +               }
183 +
184 +               sbi->s_mb_small_req = 256;
185 +               sbi->s_mb_large_req = 1024;
186 +               sbi->s_mb_group_prealloc = 512;
187 +       } else {
188 +               sbi->s_mb_prealloc_table_size = 3;
189 +               i = sbi->s_mb_prealloc_table_size * sizeof(unsigned long);
190 +               sbi->s_mb_prealloc_table = kmalloc(i, GFP_NOFS);
191 +               if (sbi->s_mb_prealloc_table == NULL) {
192 +                       kfree(sbi->s_mb_offsets);
193 +                       kfree(sbi->s_mb_maxs);
194 +                       return -ENOMEM;
195 +               }
196 +               memset(sbi->s_mb_prealloc_table, 0, i);
197 +
198 +               for (k = 0, l = sbi->s_stripe; k <= 2; ++k, l *= 2) {
199 +                       if (ext4_mb_prealloc_table_add(sbi, l) < 0) {
200 +                               sbi->s_mb_prealloc_table_size = k;
201 +                               break;
202 +                       }
203 +               }
204 +
205 +               sbi->s_mb_small_req = sbi->s_stripe;
206 +               sbi->s_mb_large_req = sbi->s_stripe * 8;
207 +               sbi->s_mb_group_prealloc = sbi->s_stripe * 4;
208 +       }
209  
210         sbi->s_locality_groups = alloc_percpu(struct ext4_locality_group);
211         if (sbi->s_locality_groups == NULL) {
212 +               kfree(sbi->s_mb_prealloc_table);
213                 kfree(sbi->s_mb_offsets);
214                 kfree(sbi->s_mb_maxs);
215                 return -ENOMEM;
216 @@ -2413,9 +2544,27 @@ int ext4_mb_init(struct super_block *sb,
217                 spin_lock_init(&lg->lg_prealloc_lock);
218         }
219  
220 -       if (sbi->s_proc)
221 +       /* init file for buddy data */
222 +       ret = ext4_mb_init_backend(sb);
223 +       if (ret != 0) {
224 +               kfree(sbi->s_mb_prealloc_table);
225 +               kfree(sbi->s_mb_offsets);
226 +               kfree(sbi->s_mb_maxs);
227 +               return ret;
228 +       }
229 +
230 +       if (sbi->s_proc) {
231 +               struct proc_dir_entry *p;
232                 proc_create_data("mb_groups", S_IRUGO, sbi->s_proc,
233                                  &ext4_mb_seq_groups_fops, sb);
234 +               p = create_proc_entry(EXT4_MB_PREALLOC_TABLE, S_IFREG |
235 +                                     S_IRUGO | S_IWUSR, sbi->s_proc);
236 +               if (p) {
237 +                       p->data = sbi;
238 +                       p->read_proc = ext4_mb_prealloc_table_proc_read;
239 +                       p->write_proc = ext4_mb_prealloc_table_proc_write;
240 +               }
241 +       }
242  
243         if (sbi->s_journal)
244                 sbi->s_journal->j_commit_callback = release_blocks_on_commit;
245 @@ -2448,8 +2597,10 @@ int ext4_mb_release(struct super_block *
246         struct ext4_group_info *grinfo;
247         struct ext4_sb_info *sbi = EXT4_SB(sb);
248  
249 -       if (sbi->s_proc)
250 +       if (sbi->s_proc) {
251                 remove_proc_entry("mb_groups", sbi->s_proc);
252 +               remove_proc_entry(EXT4_MB_PREALLOC_TABLE, sbi->s_proc);
253 +       }
254  
255         if (sbi->s_group_info) {
256                 for (i = 0; i < ngroups; i++) {
257 @@ -2469,6 +2620,7 @@ int ext4_mb_release(struct super_block *
258                         kfree(sbi->s_group_info[i]);
259                 ext4_kvfree(sbi->s_group_info);
260         }
261 +       kfree(sbi->s_mb_prealloc_table);
262         kfree(sbi->s_mb_offsets);
263         kfree(sbi->s_mb_maxs);
264         if (sbi->s_buddy_cache)
265 @@ -2798,11 +2950,12 @@ static noinline_for_stack void
266  ext4_mb_normalize_request(struct ext4_allocation_context *ac,
267                                 struct ext4_allocation_request *ar)
268  {
269 -       int bsbits, max;
270 +       int bsbits, i, wind;
271         ext4_lblk_t end;
272 -       loff_t size, orig_size, start_off;
273 +       loff_t size, orig_size;
274         ext4_lblk_t start, orig_start;
275         struct ext4_inode_info *ei = EXT4_I(ac->ac_inode);
276 +       struct ext4_sb_info *sbi = EXT4_SB(ac->ac_sb);
277         struct ext4_prealloc_space *pa;
278  
279         /* do normalize only data requests, metadata requests
280 @@ -2832,49 +2985,35 @@ ext4_mb_normalize_request(struct ext4_al
281         size = size << bsbits;
282         if (size < i_size_read(ac->ac_inode))
283                 size = i_size_read(ac->ac_inode);
284 +       size = (size + ac->ac_sb->s_blocksize - 1) >> bsbits;
285  
286 -       /* max size of free chunks */
287 -       max = 2 << bsbits;
288 +       start = wind = 0;
289  
290 -#define NRL_CHECK_SIZE(req, size, max, chunk_size)     \
291 -               (req <= (size) || max <= (chunk_size))
292 +       /* let's choose preallocation window depending on file size */
293 +       for (i = 0; i < sbi->s_mb_prealloc_table_size; i++) {
294 +               if (size <= sbi->s_mb_prealloc_table[i]) {
295 +                       wind = sbi->s_mb_prealloc_table[i];
296 +                       break;
297 +               }
298 +       }
299 +       size = wind;
300  
301 -       /* first, try to predict filesize */
302 -       /* XXX: should this table be tunable? */
303 -       start_off = 0;
304 -       if (size <= 16 * 1024) {
305 -               size = 16 * 1024;
306 -       } else if (size <= 32 * 1024) {
307 -               size = 32 * 1024;
308 -       } else if (size <= 64 * 1024) {
309 -               size = 64 * 1024;
310 -       } else if (size <= 128 * 1024) {
311 -               size = 128 * 1024;
312 -       } else if (size <= 256 * 1024) {
313 -               size = 256 * 1024;
314 -       } else if (size <= 512 * 1024) {
315 -               size = 512 * 1024;
316 -       } else if (size <= 1024 * 1024) {
317 -               size = 1024 * 1024;
318 -       } else if (NRL_CHECK_SIZE(size, 4 * 1024 * 1024, max, 2 * 1024)) {
319 -               start_off = ((loff_t)ac->ac_o_ex.fe_logical >>
320 -                                               (21 - bsbits)) << 21;
321 -               size = 2 * 1024 * 1024;
322 -       } else if (NRL_CHECK_SIZE(size, 8 * 1024 * 1024, max, 4 * 1024)) {
323 -               start_off = ((loff_t)ac->ac_o_ex.fe_logical >>
324 -                                                       (22 - bsbits)) << 22;
325 -               size = 4 * 1024 * 1024;
326 -       } else if (NRL_CHECK_SIZE(ac->ac_o_ex.fe_len,
327 -                                       (8<<20)>>bsbits, max, 8 * 1024)) {
328 -               start_off = ((loff_t)ac->ac_o_ex.fe_logical >>
329 -                                                       (23 - bsbits)) << 23;
330 -               size = 8 * 1024 * 1024;
331 -       } else {
332 -               start_off = (loff_t)ac->ac_o_ex.fe_logical << bsbits;
333 -               size      = ac->ac_o_ex.fe_len << bsbits;
334 +       if (wind == 0) {
335 +               __u64 tstart, tend;
336 +               /* file is quite large, we now preallocate with
337 +                * the biggest configured window with regart to
338 +                * logical offset */
339 +               wind = sbi->s_mb_prealloc_table[i - 1];
340 +               tstart = ac->ac_o_ex.fe_logical;
341 +               do_div(tstart, wind);
342 +               start = tstart * wind;
343 +               tend = ac->ac_o_ex.fe_logical + ac->ac_o_ex.fe_len - 1;
344 +               do_div(tend, wind);
345 +               tend = tend * wind + wind;
346 +               size = tend - start;
347         }
348 -       orig_size = size = size >> bsbits;
349 -       orig_start = start = start_off >> bsbits;
350 +       orig_size = size;
351 +       orig_start = start;
352  
353         /* don't cover already allocated blocks in selected range */
354         if (ar->pleft && start <= ar->lleft) {
355 @@ -2946,7 +3085,6 @@ ext4_mb_normalize_request(struct ext4_al
356         }
357         BUG_ON(start + size <= ac->ac_o_ex.fe_logical &&
358                         start > ac->ac_o_ex.fe_logical);
359 -       BUG_ON(size <= 0 || size > EXT4_BLOCKS_PER_GROUP(ac->ac_sb));
360  
361         /* now prepare goal request */
362  
363 @@ -3930,11 +4068,19 @@ static void ext4_mb_group_or_file(struct
364  
365         /* don't use group allocation for large files */
366         size = max(size, isize);
367 -       if (size > sbi->s_mb_stream_request) {
368 +       if ((ac->ac_o_ex.fe_len >= sbi->s_mb_small_req) ||
369 +           (size >= sbi->s_mb_large_req)) {
370                 ac->ac_flags |= EXT4_MB_STREAM_ALLOC;
371                 return;
372         }
373  
374 +       /*
375 +        * request is so large that we don't care about
376 +        * streaming - it overweights any possible seek
377 +        */
378 +       if (ac->ac_o_ex.fe_len >= sbi->s_mb_large_req)
379 +               return;
380 +
381         BUG_ON(ac->ac_lg != NULL);
382         /*
383          * locality group prealloc space are per cpu. The reason for having
384 Index: linux-stage/fs/ext4/super.c
385 ===================================================================
386 --- linux-stage.orig/fs/ext4/super.c
387 +++ linux-stage/fs/ext4/super.c
388 @@ -2377,7 +2377,8 @@ EXT4_RW_ATTR_SBI_UI(mb_stats, s_mb_stats
389  EXT4_RW_ATTR_SBI_UI(mb_max_to_scan, s_mb_max_to_scan);
390  EXT4_RW_ATTR_SBI_UI(mb_min_to_scan, s_mb_min_to_scan);
391  EXT4_RW_ATTR_SBI_UI(mb_order2_req, s_mb_order2_reqs);
392 -EXT4_RW_ATTR_SBI_UI(mb_stream_req, s_mb_stream_request);
393 +EXT4_RW_ATTR_SBI_UI(mb_small_req, s_mb_small_req);
394 +EXT4_RW_ATTR_SBI_UI(mb_large_req, s_mb_large_req);
395  EXT4_RW_ATTR_SBI_UI(mb_group_prealloc, s_mb_group_prealloc);
396  EXT4_RW_ATTR_SBI_UI(max_writeback_mb_bump, s_max_writeback_mb_bump);
397  
398 @@ -2391,7 +2392,8 @@ static struct attribute *ext4_attrs[] = 
399         ATTR_LIST(mb_max_to_scan),
400         ATTR_LIST(mb_min_to_scan),
401         ATTR_LIST(mb_order2_req),
402 -       ATTR_LIST(mb_stream_req),
403 +       ATTR_LIST(mb_small_req),
404 +       ATTR_LIST(mb_large_req),
405         ATTR_LIST(mb_group_prealloc),
406         ATTR_LIST(max_writeback_mb_bump),
407         NULL,
408 Index: linux-stage/fs/ext4/inode.c
409 ===================================================================
410 --- linux-stage.orig/fs/ext4/inode.c
411 +++ linux-stage/fs/ext4/inode.c
412 @@ -3070,6 +3070,11 @@ static int ext4_da_writepages(struct add
413         if (unlikely(sbi->s_mount_flags & EXT4_MF_FS_ABORTED))
414                 return -EROFS;
415  
416 +       if (wbc->nr_to_write < sbi->s_mb_small_req) {
417 +               nr_to_writebump = sbi->s_mb_small_req - wbc->nr_to_write;
418 +               wbc->nr_to_write = sbi->s_mb_small_req;
419 +       }
420 +
421         if (wbc->range_start == 0 && wbc->range_end == LLONG_MAX)
422                 range_whole = 1;
423