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