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