Whamcloud - gitweb
New tag 2.15.63
[fs/lustre-release.git] / ldiskfs / kernel_patches / patches / linux-5.8 / ext4-prealloc.patch
1 commit d8d8fd9192a54c7b8caef8cca9b7a1eb5e5e3298
2 Author: Alex Zhuravlev <alex.zhuravlev@sun.com>
3 AuthorDate: Thu Oct 23 10:02:19 2008 +0000
4
5 Subject: ext4: support for tunable preallocation window
6 Add support for tunable preallocation window and new tunables
7 for large/small requests.
8
9 Bugzilla-ID: b=12800
10 Signed-off-by: Alex Zhuravlev <alex.zhuravlev@sun.com>
11 Reviewed-by: Kalpak Shah <kalpak@clusterfs.com>
12 Reviewed-by: Andreas Dilger <andreas.dilger@sun.com>
13 ---
14  fs/ext4/ext4.h    |    7 +
15  fs/ext4/inode.c   |    3 
16  fs/ext4/mballoc.c |  215 +++++++++++++++++++++++++++++++++++++++++-------------
17  fs/ext4/sysfs.c   |    8 +-
18  4 files changed, 180 insertions(+), 53 deletions(-)
19
20 --- a/fs/ext4/ext4.h
21 +++ b/fs/ext4/ext4.h
22 @@ -1241,6 +1241,8 @@ extern void ext4_set_bits(void *bm, int
23  /* Metadata checksum algorithm codes */
24  #define EXT4_CRC32C_CHKSUM             1
25  
26 +#define EXT4_MAX_PREALLOC_TABLE        64
27 +
28  /*
29   * Structure of the super block
30   */
31 @@ -1497,11 +1499,13 @@ struct ext4_sb_info {
32  
33         /* tunables */
34         unsigned long s_stripe;
35 -       unsigned int s_mb_stream_request;
36 +       unsigned long s_mb_small_req;
37 +       unsigned long s_mb_large_req;
38         unsigned int s_mb_max_to_scan;
39         unsigned int s_mb_min_to_scan;
40         unsigned int s_mb_stats;
41         unsigned int s_mb_order2_reqs;
42 +       unsigned long *s_mb_prealloc_table;
43         unsigned int s_mb_group_prealloc;
44         unsigned int s_max_dir_size_kb;
45         /* where last allocation was done - for stream allocation */
46 @@ -2645,6 +2649,7 @@ extern int ext4_init_inode_table(struct
47  extern void ext4_end_bitmap_read(struct buffer_head *bh, int uptodate);
48  
49  /* mballoc.c */
50 +extern const struct proc_ops ext4_seq_prealloc_table_fops;
51  extern const struct seq_operations ext4_mb_seq_groups_ops;
52  extern long ext4_mb_stats;
53  extern long ext4_mb_max_to_scan;
54 --- a/fs/ext4/inode.c
55 +++ b/fs/ext4/inode.c
56 @@ -2698,6 +2698,9 @@ static int ext4_writepages(struct addres
57                                                 PAGE_SIZE >> inode->i_blkbits);
58         }
59  
60 +       if (wbc->nr_to_write < sbi->s_mb_small_req)
61 +               wbc->nr_to_write = sbi->s_mb_small_req;
62 +
63         if (wbc->range_start == 0 && wbc->range_end == LLONG_MAX)
64                 range_whole = 1;
65  
66 --- a/fs/ext4/mballoc.c
67 +++ b/fs/ext4/mballoc.c
68 @@ -2455,6 +2455,99 @@ const struct seq_operations ext4_mb_seq_
69         .show   = ext4_mb_seq_groups_show,
70  };
71  
72 +static int ext4_mb_check_and_update_prealloc(struct ext4_sb_info *sbi,
73 +                                                char *str, size_t cnt,
74 +                                                int update)
75 +{
76 +       unsigned long value;
77 +       unsigned long prev = 0;
78 +       char *cur;
79 +       char *next;
80 +       char *end;
81 +       int num = 0;
82 +
83 +       cur = str;
84 +       end = str + cnt;
85 +       while (cur < end) {
86 +               while ((cur < end) && (*cur == ' ')) cur++;
87 +               value = simple_strtol(cur, &next, 0);
88 +               if (value == 0)
89 +                       break;
90 +               if (cur == next)
91 +                       return -EINVAL;
92 +
93 +               cur = next;
94 +
95 +               if (value > (sbi->s_blocks_per_group - 1 - 1 - sbi->s_itb_per_group))
96 +                       return -EINVAL;
97 +
98 +               /* they should add values in order */
99 +               if (value <= prev)
100 +                       return -EINVAL;
101 +
102 +               if (update)
103 +                       sbi->s_mb_prealloc_table[num] = value;
104 +
105 +               prev = value;
106 +               num++;
107 +       }
108 +
109 +       if (num > EXT4_MAX_PREALLOC_TABLE - 1)
110 +               return -EOVERFLOW;
111 +
112 +       if (update)
113 +               sbi->s_mb_prealloc_table[num] = 0;
114 +
115 +       return 0;
116 +}
117 +
118 +static ssize_t ext4_mb_prealloc_table_proc_write(struct file *file,
119 +                                            const char __user *buf,
120 +                                            size_t cnt, loff_t *pos)
121 +{
122 +       struct ext4_sb_info *sbi = EXT4_SB(PDE_DATA(file_inode(file)));
123 +       char str[128];
124 +       int rc;
125 +
126 +       if (cnt >= sizeof(str))
127 +               return -EINVAL;
128 +       if (copy_from_user(str, buf, cnt))
129 +               return -EFAULT;
130 +
131 +       rc = ext4_mb_check_and_update_prealloc(sbi, str, cnt, 0);
132 +       if (rc)
133 +               return rc;
134 +
135 +       rc = ext4_mb_check_and_update_prealloc(sbi, str, cnt, 1);
136 +       return rc ? rc : cnt;
137 +}
138 +
139 +static int mb_prealloc_table_seq_show(struct seq_file *m, void *v)
140 +{
141 +       struct ext4_sb_info *sbi = EXT4_SB(m->private);
142 +       int i;
143 +
144 +       for (i = 0; i < EXT4_MAX_PREALLOC_TABLE &&
145 +                       sbi->s_mb_prealloc_table[i] != 0; i++)
146 +               seq_printf(m, "%ld ", sbi->s_mb_prealloc_table[i]);
147 +       seq_printf(m, "\n");
148 +
149 +       return 0;
150 +}
151 +
152 +static int mb_prealloc_table_seq_open(struct inode *inode, struct file *file)
153 +{
154 +       return single_open(file, mb_prealloc_table_seq_show, PDE_DATA(inode));
155 +}
156 +
157 +const struct proc_ops ext4_seq_prealloc_table_fops = {
158 +       .proc_open      = mb_prealloc_table_seq_open,
159 +       .proc_read      = seq_read,
160 +       .proc_lseek     = seq_lseek,
161 +       .proc_release   = single_release,
162 +       .proc_write     = ext4_mb_prealloc_table_proc_write,
163 +};
164 +
165  static struct kmem_cache *get_groupinfo_cache(int blocksize_bits)
166  {
167         int cache_index = blocksize_bits - EXT4_MIN_BLOCK_LOG_SIZE;
168 @@ -2685,7 +2778,7 @@ static int ext4_groupinfo_create_slab(si
169  int ext4_mb_init(struct super_block *sb)
170  {
171         struct ext4_sb_info *sbi = EXT4_SB(sb);
172 -       unsigned i, j;
173 +       unsigned i, j, k, l;
174         unsigned offset, offset_incr;
175         unsigned max;
176         int ret;
177 @@ -2734,7 +2827,6 @@ int ext4_mb_init(struct super_block *sb)
178         sbi->s_mb_max_to_scan = MB_DEFAULT_MAX_TO_SCAN;
179         sbi->s_mb_min_to_scan = MB_DEFAULT_MIN_TO_SCAN;
180         sbi->s_mb_stats = MB_DEFAULT_STATS;
181 -       sbi->s_mb_stream_request = MB_DEFAULT_STREAM_THRESHOLD;
182         sbi->s_mb_order2_reqs = MB_DEFAULT_ORDER2_REQS;
183         /*
184          * The default group preallocation is 512, which for 4k block
185 @@ -2758,9 +2850,29 @@ int ext4_mb_init(struct super_block *sb)
186          * RAID stripe size so that preallocations don't fragment
187          * the stripes.
188          */
189 -       if (sbi->s_stripe > 1) {
190 -               sbi->s_mb_group_prealloc = roundup(
191 -                       sbi->s_mb_group_prealloc, sbi->s_stripe);
192 +
193 +       /* Allocate table once */
194 +       sbi->s_mb_prealloc_table = kzalloc(
195 +               EXT4_MAX_PREALLOC_TABLE * sizeof(unsigned long), GFP_NOFS);
196 +       if (sbi->s_mb_prealloc_table == NULL) {
197 +               ret = -ENOMEM;
198 +               goto out;
199 +       }
200 +
201 +       if (sbi->s_stripe == 0) {
202 +               for (k = 0, l = 4; k <= 9; ++k, l *= 2)
203 +                       sbi->s_mb_prealloc_table[k] = l;
204 +
205 +               sbi->s_mb_small_req = 256;
206 +               sbi->s_mb_large_req = 1024;
207 +               sbi->s_mb_group_prealloc = 512;
208 +       } else {
209 +               for (k = 0, l = sbi->s_stripe; k <= 2; ++k, l *= 2)
210 +                       sbi->s_mb_prealloc_table[k] = l;
211 +
212 +               sbi->s_mb_small_req = sbi->s_stripe;
213 +               sbi->s_mb_large_req = sbi->s_stripe * 8;
214 +               sbi->s_mb_group_prealloc = sbi->s_stripe * 4;
215         }
216  
217         sbi->s_locality_groups = alloc_percpu(struct ext4_locality_group);
218 @@ -2788,6 +2900,7 @@ out_free_locality_groups:
219         free_percpu(sbi->s_locality_groups);
220         sbi->s_locality_groups = NULL;
221  out:
222 +       kfree(sbi->s_mb_prealloc_table);
223         kfree(sbi->s_mb_offsets);
224         sbi->s_mb_offsets = NULL;
225         kfree(sbi->s_mb_maxs);
226 @@ -2800,6 +2840,7 @@ int ext4_mb_release(struct super_block *sb)
227                 kvfree(group_info);
228                 rcu_read_unlock();
229         }
230 +       kfree(sbi->s_mb_prealloc_table);
231         kfree(sbi->s_mb_offsets);
232         kfree(sbi->s_mb_maxs);
233         iput(sbi->s_buddy_cache);
234 @@ -3057,7 +3170,6 @@ ext4_mb_mark_diskspace_used(struct ext4_
235         int err, len;
236  
237         BUG_ON(ac->ac_status != AC_STATUS_FOUND);
238 -       BUG_ON(ac->ac_b_ex.fe_len <= 0);
239  
240         sb = ac->ac_sb;
241         sbi = EXT4_SB(sb);
242 @@ -3187,13 +3299,14 @@ ext4_mb_normalize_request(struct ext4_al
243                                 struct ext4_allocation_request *ar)
244  {
245         struct ext4_sb_info *sbi = EXT4_SB(ac->ac_sb);
246 -       int bsbits, max;
247 +       int bsbits, i, wind;
248         ext4_lblk_t end;
249 -       loff_t size, start_off;
250 +       loff_t size;
251         loff_t orig_size __maybe_unused;
252         ext4_lblk_t start;
253         struct ext4_inode_info *ei = EXT4_I(ac->ac_inode);
254         struct ext4_prealloc_space *pa;
255 +       unsigned long value, last_non_zero;
256  
257         /* do normalize only data requests, metadata requests
258            do not need preallocation */
259 @@ -3222,51 +3335,46 @@ ext4_mb_normalize_request(struct ext4_al
260         size = size << bsbits;
261         if (size < i_size_read(ac->ac_inode))
262                 size = i_size_read(ac->ac_inode);
263 -       orig_size = size;
264 +       size = (size + ac->ac_sb->s_blocksize - 1) >> bsbits;
265 +
266 +       start = wind = 0;
267 +       value = last_non_zero = 0;
268  
269 -       /* max size of free chunks */
270 -       max = 2 << bsbits;
271 +       /* let's choose preallocation window depending on file size */
272 +       for (i = 0; i < EXT4_MAX_PREALLOC_TABLE; i++) {
273 +               value = sbi->s_mb_prealloc_table[i];
274 +               if (value == 0)
275 +                       break;
276 +               else
277 +                       last_non_zero = value;
278  
279 -#define NRL_CHECK_SIZE(req, size, max, chunk_size)     \
280 -               (req <= (size) || max <= (chunk_size))
281 +               if (size <= value) {
282 +                       wind = value;
283 +                       break;
284 +               }
285 +       }
286  
287 -       /* first, try to predict filesize */
288 -       /* XXX: should this table be tunable? */
289 -       start_off = 0;
290 -       if (size <= 16 * 1024) {
291 -               size = 16 * 1024;
292 -       } else if (size <= 32 * 1024) {
293 -               size = 32 * 1024;
294 -       } else if (size <= 64 * 1024) {
295 -               size = 64 * 1024;
296 -       } else if (size <= 128 * 1024) {
297 -               size = 128 * 1024;
298 -       } else if (size <= 256 * 1024) {
299 -               size = 256 * 1024;
300 -       } else if (size <= 512 * 1024) {
301 -               size = 512 * 1024;
302 -       } else if (size <= 1024 * 1024) {
303 -               size = 1024 * 1024;
304 -       } else if (NRL_CHECK_SIZE(size, 4 * 1024 * 1024, max, 2 * 1024)) {
305 -               start_off = ((loff_t)ac->ac_o_ex.fe_logical >>
306 -                                               (21 - bsbits)) << 21;
307 -               size = 2 * 1024 * 1024;
308 -       } else if (NRL_CHECK_SIZE(size, 8 * 1024 * 1024, max, 4 * 1024)) {
309 -               start_off = ((loff_t)ac->ac_o_ex.fe_logical >>
310 -                                                       (22 - bsbits)) << 22;
311 -               size = 4 * 1024 * 1024;
312 -       } else if (NRL_CHECK_SIZE(ac->ac_o_ex.fe_len,
313 -                                       (8<<20)>>bsbits, max, 8 * 1024)) {
314 -               start_off = ((loff_t)ac->ac_o_ex.fe_logical >>
315 -                                                       (23 - bsbits)) << 23;
316 -               size = 8 * 1024 * 1024;
317 +       if (wind == 0) {
318 +               if (last_non_zero != 0) {
319 +                       __u64 tstart, tend;
320 +                       /* file is quite large, we now preallocate with
321 +                       * the biggest configured window with regart to
322 +                       * logical offset */
323 +                       wind = last_non_zero;
324 +                       tstart = ac->ac_o_ex.fe_logical;
325 +                       do_div(tstart, wind);
326 +                       start = tstart * wind;
327 +                       tend = ac->ac_o_ex.fe_logical + ac->ac_o_ex.fe_len - 1;
328 +                       do_div(tend, wind);
329 +                       tend = tend * wind + wind;
330 +                       size = tend - start;
331 +               }
332         } else {
333 -               start_off = (loff_t) ac->ac_o_ex.fe_logical << bsbits;
334 -               size      = (loff_t) EXT4_C2B(EXT4_SB(ac->ac_sb),
335 -                                             ac->ac_o_ex.fe_len) << bsbits;
336 +               size = wind;
337         }
338 -       size = size >> bsbits;
339 -       start = start_off >> bsbits;
340 +
341 +
342 +       orig_size = size;
343  
344         /* don't cover already allocated blocks in selected range */
345         if (ar->pleft && start <= ar->lleft) {
346 @@ -3348,7 +3456,6 @@ ext4_mb_normalize_request(struct ext4_al
347                          (unsigned long) ac->ac_o_ex.fe_logical);
348                 BUG();
349         }
350 -       BUG_ON(size <= 0 || size > EXT4_BLOCKS_PER_GROUP(ac->ac_sb));
351  
352         /* now prepare goal request */
353  
354 @@ -4341,11 +4448,19 @@ static void ext4_mb_group_or_file(struct
355  
356         /* don't use group allocation for large files */
357         size = max(size, isize);
358 -       if (size > sbi->s_mb_stream_request) {
359 +       if ((ac->ac_o_ex.fe_len >= sbi->s_mb_small_req) ||
360 +           (size >= sbi->s_mb_large_req)) {
361                 ac->ac_flags |= EXT4_MB_STREAM_ALLOC;
362                 return;
363         }
364  
365 +       /*
366 +        * request is so large that we don't care about
367 +        * streaming - it overweights any possible seek
368 +        */
369 +       if (ac->ac_o_ex.fe_len >= sbi->s_mb_large_req)
370 +               return;
371 +
372         BUG_ON(ac->ac_lg != NULL);
373         /*
374          * locality group prealloc space are per cpu. The reason for having
375 --- a/fs/ext4/sysfs.c
376 +++ b/fs/ext4/sysfs.c
377 @@ -213,7 +213,8 @@ EXT4_RW_ATTR_SBI_UI(mb_stats, s_mb_stats
378  EXT4_RW_ATTR_SBI_UI(mb_max_to_scan, s_mb_max_to_scan);
379  EXT4_RW_ATTR_SBI_UI(mb_min_to_scan, s_mb_min_to_scan);
380  EXT4_RW_ATTR_SBI_UI(mb_order2_req, s_mb_order2_reqs);
381 -EXT4_RW_ATTR_SBI_UI(mb_stream_req, s_mb_stream_request);
382 +EXT4_RW_ATTR_SBI_UI(mb_small_req, s_mb_small_req);
383 +EXT4_RW_ATTR_SBI_UI(mb_large_req, s_mb_large_req);
384  EXT4_RW_ATTR_SBI_UI(mb_group_prealloc, s_mb_group_prealloc);
385  EXT4_RW_ATTR_SBI_UI(extent_max_zeroout_kb, s_extent_max_zeroout_kb);
386  EXT4_ATTR(trigger_fs_error, 0200, trigger_test_error);
387 @@ -255,7 +256,8 @@ static struct attribute *ext4_attrs[] =
388         ATTR_LIST(mb_max_to_scan),
389         ATTR_LIST(mb_min_to_scan),
390         ATTR_LIST(mb_order2_req),
391 -       ATTR_LIST(mb_stream_req),
392 +       ATTR_LIST(mb_small_req),
393 +       ATTR_LIST(mb_large_req),
394         ATTR_LIST(mb_group_prealloc),
395         ATTR_LIST(max_writeback_mb_bump),
396         ATTR_LIST(extent_max_zeroout_kb),
397 @@ -510,6 +512,8 @@ int ext4_register_sysfs(struct super_blo
398                                 sb);
399                 proc_create_seq_data("mb_groups", S_IRUGO, sbi->s_proc,
400                                 &ext4_mb_seq_groups_ops, sb);
401 +               proc_create_data("prealloc_table", S_IRUGO, sbi->s_proc,
402 +                               &ext4_seq_prealloc_table_fops, sb);
403         }
404         return 0;
405  }