Whamcloud - gitweb
5de7cefd1e6d477aea42cd10af61736c002e81b2
[fs/lustre-release.git] / ldiskfs / kernel_patches / patches / sles12 / ext4-prealloc.patch
1 Index: linux-3.12.39-47.1/fs/ext4/ext4.h
2 ===================================================================
3 --- linux-3.12.39-47.1.orig/fs/ext4/ext4.h
4 +++ linux-3.12.39-47.1/fs/ext4/ext4.h
5 @@ -1251,11 +1251,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.12.39-47.1/fs/ext4/mballoc.c
22 ===================================================================
23 --- linux-3.12.39-47.1.orig/fs/ext4/mballoc.c
24 +++ linux-3.12.39-47.1/fs/ext4/mballoc.c
25 @@ -1847,6 +1847,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_clusters_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 @@ -2285,6 +2304,90 @@ 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 +               while ((cur < end) && (*cur == ' '))
82 +                       cur++;
83 +               value = simple_strtol(cur, &cur, 0);
84 +               if (value == 0)
85 +                       break;
86 +               if (value <= prev)
87 +                       return -EINVAL;
88 +               prev = value;
89 +               num++;
90 +       }
91 +
92 +       new_table = kmalloc(num * sizeof(*new_table), GFP_KERNEL);
93 +       if (new_table == NULL)
94 +               return -ENOMEM;
95 +       kfree(sbi->s_mb_prealloc_table);
96 +       memset(new_table, 0, num * sizeof(*new_table));
97 +       sbi->s_mb_prealloc_table = new_table;
98 +       sbi->s_mb_prealloc_table_size = num;
99 +       cur = str;
100 +       end = str + cnt;
101 +       while (cur < end && i < num) {
102 +               while (cur < end && *cur == ' ')
103 +                       cur++;
104 +               value = simple_strtol(cur, &cur, 0);
105 +               if (ext4_mb_prealloc_table_add(sbi, value) == 0)
106 +                       ++i;
107 +       }
108 +       if (i != num)
109 +               sbi->s_mb_prealloc_table_size = i;
110 +
111 +       return cnt;
112 +}
113 +
114 +static int mb_prealloc_table_seq_show(struct seq_file *m, void *v)
115 +{
116 +       struct ext4_sb_info *sbi = EXT4_SB(m->private);
117 +       int i;
118 +
119 +       for (i = 0; i < sbi->s_mb_prealloc_table_size; i++)
120 +               seq_printf(m, "%ld ", sbi->s_mb_prealloc_table[i]);
121 +       seq_printf(m, "\n");
122 +
123 +       return 0;
124 +}
125 +
126 +static int mb_prealloc_table_seq_open(struct inode *inode, struct file *file)
127 +{
128 +       return single_open(file, mb_prealloc_table_seq_show, PDE_DATA(inode));
129 +}
130 +
131 +static const struct file_operations ext4_mb_prealloc_seq_fops = {
132 +       .owner   = THIS_MODULE,
133 +       .open    = mb_prealloc_table_seq_open,
134 +       .read    = seq_read,
135 +       .llseek  = seq_lseek,
136 +       .release = single_release,
137 +       .write   = ext4_mb_prealloc_table_proc_write,
138 +};
139 +
140  static int ext4_mb_seq_groups_open(struct inode *inode, struct file *file)
141  {
142         struct super_block *sb = PDE_DATA(inode);
143 @@ -2550,7 +2657,7 @@ static int ext4_groupinfo_create_slab(si
144  int ext4_mb_init(struct super_block *sb)
145  {
146         struct ext4_sb_info *sbi = EXT4_SB(sb);
147 -       unsigned i, j;
148 +       unsigned i, j, k, l;
149         unsigned offset, offset_incr;
150         unsigned max;
151         int ret;
152 @@ -2595,7 +2702,6 @@ int ext4_mb_init(struct super_block *sb)
153         sbi->s_mb_max_to_scan = MB_DEFAULT_MAX_TO_SCAN;
154         sbi->s_mb_min_to_scan = MB_DEFAULT_MIN_TO_SCAN;
155         sbi->s_mb_stats = MB_DEFAULT_STATS;
156 -       sbi->s_mb_stream_request = MB_DEFAULT_STREAM_THRESHOLD;
157         sbi->s_mb_order2_reqs = MB_DEFAULT_ORDER2_REQS;
158         /*
159          * The default group preallocation is 512, which for 4k block
160 @@ -2619,9 +2725,47 @@ int ext4_mb_init(struct super_block *sb)
161          * RAID stripe size so that preallocations don't fragment
162          * the stripes.
163          */
164 -       if (sbi->s_stripe > 1) {
165 -               sbi->s_mb_group_prealloc = roundup(
166 -                       sbi->s_mb_group_prealloc, sbi->s_stripe);
167 +
168 +       if (sbi->s_stripe == 0) {
169 +               sbi->s_mb_prealloc_table_size = 10;
170 +               i = sbi->s_mb_prealloc_table_size * sizeof(unsigned long);
171 +               sbi->s_mb_prealloc_table = kmalloc(i, GFP_NOFS);
172 +               if (sbi->s_mb_prealloc_table == NULL) {
173 +                       ret = -ENOMEM;
174 +                       goto out;
175 +               }
176 +               memset(sbi->s_mb_prealloc_table, 0, i);
177 +
178 +               for (k = 0, l = 4; k <= 9; ++k, l *= 2) {
179 +                       if (ext4_mb_prealloc_table_add(sbi, l) < 0) {
180 +                               sbi->s_mb_prealloc_table_size = k;
181 +                               break;
182 +                       }
183 +               }
184 +
185 +               sbi->s_mb_small_req = 256;
186 +               sbi->s_mb_large_req = 1024;
187 +               sbi->s_mb_group_prealloc = 512;
188 +       } else {
189 +               sbi->s_mb_prealloc_table_size = 3;
190 +               i = sbi->s_mb_prealloc_table_size * sizeof(unsigned long);
191 +               sbi->s_mb_prealloc_table = kmalloc(i, GFP_NOFS);
192 +               if (sbi->s_mb_prealloc_table == NULL) {
193 +                       ret = -ENOMEM;
194 +                       goto out;
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 @@ -2627,9 +2769,13 @@ int ext4_mb_init(struct super_block *sb)
212         if (ret != 0)
213                 goto out_free_locality_groups;
214  
215 -       if (sbi->s_proc)
216 +       if (sbi->s_proc) {
217                 proc_create_data("mb_groups", S_IRUGO, sbi->s_proc,
218                                  &ext4_mb_seq_groups_fops, sb);
219 +               proc_create_data(EXT4_MB_PREALLOC_TABLE, S_IFREG | S_IRUGO |
220 +                                S_IWUSR, sbi->s_proc,
221 +                                &ext4_mb_prealloc_seq_fops, sb);
222 +       }
223  
224         return 0;
225  
226 @@ -2639,6 +2785,7 @@ out_free_locality_groups:
227  out_free_groupinfo_slab:
228         ext4_groupinfo_destroy_slabs();
229  out:
230 +       kfree(sbi->s_mb_prealloc_table);
231         kfree(sbi->s_mb_offsets);
232         sbi->s_mb_offsets = NULL;
233         kfree(sbi->s_mb_maxs);
234 @@ -2673,8 +2820,10 @@ int ext4_mb_release(struct super_block *
235         struct ext4_sb_info *sbi = EXT4_SB(sb);
236         struct kmem_cache *cachep = get_groupinfo_cache(sb->s_blocksize_bits);
237  
238 -       if (sbi->s_proc)
239 +       if (sbi->s_proc) {
240                 remove_proc_entry("mb_groups", sbi->s_proc);
241 +               remove_proc_entry(EXT4_MB_PREALLOC_TABLE, sbi->s_proc);
242 +       }
243  
244         if (sbi->s_group_info) {
245                 for (i = 0; i < ngroups; i++) {
246 @@ -2985,9 +3134,9 @@ ext4_mb_normalize_request(struct ext4_al
247                                 struct ext4_allocation_request *ar)
248  {
249         struct ext4_sb_info *sbi = EXT4_SB(ac->ac_sb);
250 -       int bsbits, max;
251 +       int bsbits, i, wind;
252         ext4_lblk_t end;
253 -       loff_t size, start_off;
254 +       loff_t size;
255         loff_t orig_size __maybe_unused;
256         ext4_lblk_t start;
257         struct ext4_inode_info *ei = EXT4_I(ac->ac_inode);
258 @@ -3020,50 +3169,34 @@ ext4_mb_normalize_request(struct ext4_al
259         size = size << bsbits;
260         if (size < i_size_read(ac->ac_inode))
261                 size = i_size_read(ac->ac_inode);
262 -       orig_size = size;
263 +       size = (size + ac->ac_sb->s_blocksize - 1) >> bsbits;
264  
265 -       /* max size of free chunks */
266 -       max = 2 << bsbits;
267 +       start = wind = 0;
268  
269 -#define NRL_CHECK_SIZE(req, size, max, chunk_size)     \
270 -               (req <= (size) || max <= (chunk_size))
271 +       /* let's choose preallocation window depending on file size */
272 +       for (i = 0; i < sbi->s_mb_prealloc_table_size; i++) {
273 +               if (size <= sbi->s_mb_prealloc_table[i]) {
274 +                       wind = sbi->s_mb_prealloc_table[i];
275 +                       break;
276 +               }
277 +       }
278 +       size = wind;
279  
280 -       /* first, try to predict filesize */
281 -       /* XXX: should this table be tunable? */
282 -       start_off = 0;
283 -       if (size <= 16 * 1024) {
284 -               size = 16 * 1024;
285 -       } else if (size <= 32 * 1024) {
286 -               size = 32 * 1024;
287 -       } else if (size <= 64 * 1024) {
288 -               size = 64 * 1024;
289 -       } else if (size <= 128 * 1024) {
290 -               size = 128 * 1024;
291 -       } else if (size <= 256 * 1024) {
292 -               size = 256 * 1024;
293 -       } else if (size <= 512 * 1024) {
294 -               size = 512 * 1024;
295 -       } else if (size <= 1024 * 1024) {
296 -               size = 1024 * 1024;
297 -       } else if (NRL_CHECK_SIZE(size, 4 * 1024 * 1024, max, 2 * 1024)) {
298 -               start_off = ((loff_t)ac->ac_o_ex.fe_logical >>
299 -                                               (21 - bsbits)) << 21;
300 -               size = 2 * 1024 * 1024;
301 -       } else if (NRL_CHECK_SIZE(size, 8 * 1024 * 1024, max, 4 * 1024)) {
302 -               start_off = ((loff_t)ac->ac_o_ex.fe_logical >>
303 -                                                       (22 - bsbits)) << 22;
304 -               size = 4 * 1024 * 1024;
305 -       } else if (NRL_CHECK_SIZE(ac->ac_o_ex.fe_len,
306 -                                       (8<<20)>>bsbits, max, 8 * 1024)) {
307 -               start_off = ((loff_t)ac->ac_o_ex.fe_logical >>
308 -                                                       (23 - bsbits)) << 23;
309 -               size = 8 * 1024 * 1024;
310 -       } else {
311 -               start_off = (loff_t)ac->ac_o_ex.fe_logical << bsbits;
312 -               size      = ac->ac_o_ex.fe_len << bsbits;
313 +       if (wind == 0) {
314 +               __u64 tstart, tend;
315 +               /* file is quite large, we now preallocate with
316 +                * the biggest configured window with regart to
317 +                * logical offset */
318 +               wind = sbi->s_mb_prealloc_table[i - 1];
319 +               tstart = ac->ac_o_ex.fe_logical;
320 +               do_div(tstart, wind);
321 +               start = tstart * wind;
322 +               tend = ac->ac_o_ex.fe_logical + ac->ac_o_ex.fe_len - 1;
323 +               do_div(tend, wind);
324 +               tend = tend * wind + wind;
325 +               size = tend - start;
326         }
327 -       size = size >> bsbits;
328 -       start = start_off >> bsbits;
329 +       orig_size = size;
330  
331         /* don't cover already allocated blocks in selected range */
332         if (ar->pleft && start <= ar->lleft) {
333 @@ -3139,7 +3272,6 @@ ext4_mb_normalize_request(struct ext4_al
334         }
335         BUG_ON(start + size <= ac->ac_o_ex.fe_logical &&
336                         start > ac->ac_o_ex.fe_logical);
337 -       BUG_ON(size <= 0 || size > EXT4_BLOCKS_PER_GROUP(ac->ac_sb));
338  
339         /* now prepare goal request */
340  
341 @@ -4105,11 +4237,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-3.12.39-47.1/fs/ext4/super.c
363 ===================================================================
364 --- linux-3.12.39-47.1.orig/fs/ext4/super.c
365 +++ linux-3.12.39-47.1/fs/ext4/super.c
366 @@ -2592,7 +2592,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 @@ -2609,7 +2610,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-3.12.39-47.1/fs/ext4/inode.c
387 ===================================================================
388 --- linux-3.12.39-47.1.orig/fs/ext4/inode.c
389 +++ linux-3.12.39-47.1/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