Whamcloud - gitweb
LU-17599 ldiskfs: restore ldiskfs patch attribution
[fs/lustre-release.git] / ldiskfs / kernel_patches / patches / linux-5.18 / 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 | 219 +++++++++++++++++++++++++++++++++++-----------
17  fs/ext4/sysfs.c   |   8 +-
18  4 files changed, 182 insertions(+), 55 deletions(-)
19
20 diff --git a/fs/ext4/ext4.h b/fs/ext4/ext4.h
21 index b03dbd7..78d3176 100644
22 --- a/fs/ext4/ext4.h
23 +++ b/fs/ext4/ext4.h
24 @@ -1291,6 +1291,8 @@ extern void mb_set_bits(void *bm, int cur, int len);
25  #define EXT4_DFL_MAX_MNT_COUNT         20      /* Allow 20 mounts */
26  #define EXT4_DFL_CHECKINTERVAL         0       /* Don't use interval check */
27  
28 +#define EXT4_MAX_PREALLOC_TABLE        64
29 +
30  /*
31   * Behaviour when detecting errors
32   */
33 @@ -1597,11 +1599,13 @@ struct ext4_sb_info {
34         /* tunables */
35         unsigned long s_stripe;
36         unsigned int s_mb_max_linear_groups;
37 -       unsigned int s_mb_stream_request;
38 +       unsigned long s_mb_small_req;
39 +       unsigned long s_mb_large_req;
40         unsigned int s_mb_max_to_scan;
41         unsigned int s_mb_min_to_scan;
42         unsigned int s_mb_stats;
43         unsigned int s_mb_order2_reqs;
44 +       unsigned long *s_mb_prealloc_table;
45         unsigned int s_mb_group_prealloc;
46         unsigned int s_mb_max_inode_prealloc;
47         unsigned int s_max_dir_size_kb;
48 @@ -2940,6 +2944,7 @@ int ext4_fc_record_regions(struct super_block *sb, int ino,
49                            int len, int replay);
50  
51  /* mballoc.c */
52 +extern const struct proc_ops ext4_seq_prealloc_table_fops;
53  extern const struct seq_operations ext4_mb_seq_groups_ops;
54  extern const struct seq_operations ext4_mb_seq_structs_summary_ops;
55  extern long ext4_mb_stats;
56 diff --git a/fs/ext4/inode.c b/fs/ext4/inode.c
57 index 6723622..2ed00ae 100644
58 --- a/fs/ext4/inode.c
59 +++ b/fs/ext4/inode.c
60 @@ -2720,6 +2720,9 @@ static int ext4_writepages(struct address_space *mapping,
61                                                 PAGE_SIZE >> inode->i_blkbits);
62         }
63  
64 +       if (wbc->nr_to_write < sbi->s_mb_small_req)
65 +               wbc->nr_to_write = sbi->s_mb_small_req;
66 +
67         if (wbc->range_start == 0 && wbc->range_end == LLONG_MAX)
68                 range_whole = 1;
69  
70 diff --git a/fs/ext4/mballoc.c b/fs/ext4/mballoc.c
71 index 8164480..94a299b 100644
72 --- a/fs/ext4/mballoc.c
73 +++ b/fs/ext4/mballoc.c
74 @@ -3070,6 +3070,99 @@ const struct seq_operations ext4_mb_seq_structs_summary_ops = {
75         .show   = ext4_mb_seq_structs_summary_show,
76  };
77  
78 +static int ext4_mb_check_and_update_prealloc(struct ext4_sb_info *sbi,
79 +                                                char *str, size_t cnt,
80 +                                                int update)
81 +{
82 +       unsigned long value;
83 +       unsigned long prev = 0;
84 +       char *cur;
85 +       char *next;
86 +       char *end;
87 +       int num = 0;
88 +
89 +       cur = str;
90 +       end = str + cnt;
91 +       while (cur < end) {
92 +               while ((cur < end) && (*cur == ' ')) cur++;
93 +               value = simple_strtol(cur, &next, 0);
94 +               if (value == 0)
95 +                       break;
96 +               if (cur == next)
97 +                       return -EINVAL;
98 +
99 +               cur = next;
100 +
101 +               if (value > (sbi->s_blocks_per_group - 1 - 1 - sbi->s_itb_per_group))
102 +                       return -EINVAL;
103 +
104 +               /* they should add values in order */
105 +               if (value <= prev)
106 +                       return -EINVAL;
107 +
108 +               if (update)
109 +                       sbi->s_mb_prealloc_table[num] = value;
110 +
111 +               prev = value;
112 +               num++;
113 +       }
114 +
115 +       if (num > EXT4_MAX_PREALLOC_TABLE - 1)
116 +               return -EOVERFLOW;
117 +
118 +       if (update)
119 +               sbi->s_mb_prealloc_table[num] = 0;
120 +
121 +       return 0;
122 +}
123 +
124 +static ssize_t ext4_mb_prealloc_table_proc_write(struct file *file,
125 +                                            const char __user *buf,
126 +                                            size_t cnt, loff_t *pos)
127 +{
128 +       struct ext4_sb_info *sbi = EXT4_SB(pde_data(file_inode(file)));
129 +       char str[128];
130 +       int rc;
131 +
132 +       if (cnt >= sizeof(str))
133 +               return -EINVAL;
134 +       if (copy_from_user(str, buf, cnt))
135 +               return -EFAULT;
136 +
137 +       rc = ext4_mb_check_and_update_prealloc(sbi, str, cnt, 0);
138 +       if (rc)
139 +               return rc;
140 +
141 +       rc = ext4_mb_check_and_update_prealloc(sbi, str, cnt, 1);
142 +       return rc ? rc : cnt;
143 +}
144 +
145 +static int mb_prealloc_table_seq_show(struct seq_file *m, void *v)
146 +{
147 +       struct ext4_sb_info *sbi = EXT4_SB(m->private);
148 +       int i;
149 +
150 +       for (i = 0; i < EXT4_MAX_PREALLOC_TABLE &&
151 +                       sbi->s_mb_prealloc_table[i] != 0; i++)
152 +               seq_printf(m, "%ld ", sbi->s_mb_prealloc_table[i]);
153 +       seq_printf(m, "\n");
154 +
155 +       return 0;
156 +}
157 +
158 +static int mb_prealloc_table_seq_open(struct inode *inode, struct file *file)
159 +{
160 +       return single_open(file, mb_prealloc_table_seq_show, pde_data(inode));
161 +}
162 +
163 +const struct proc_ops ext4_seq_prealloc_table_fops = {
164 +       .proc_open      = mb_prealloc_table_seq_open,
165 +       .proc_read      = seq_read,
166 +       .proc_lseek     = seq_lseek,
167 +       .proc_release   = single_release,
168 +       .proc_write     = ext4_mb_prealloc_table_proc_write,
169 +};
170 +
171  static struct kmem_cache *get_groupinfo_cache(int blocksize_bits)
172  {
173         int cache_index = blocksize_bits - EXT4_MIN_BLOCK_LOG_SIZE;
174 @@ -3382,7 +3475,7 @@ static void ext4_discard_work(struct work_struct *work)
175  int ext4_mb_init(struct super_block *sb)
176  {
177         struct ext4_sb_info *sbi = EXT4_SB(sb);
178 -       unsigned i, j;
179 +       unsigned i, j, k, l;
180         unsigned offset, offset_incr;
181         unsigned max;
182         int ret;
183 @@ -3454,7 +3547,6 @@ int ext4_mb_init(struct super_block *sb)
184         sbi->s_mb_max_to_scan = MB_DEFAULT_MAX_TO_SCAN;
185         sbi->s_mb_min_to_scan = MB_DEFAULT_MIN_TO_SCAN;
186         sbi->s_mb_stats = MB_DEFAULT_STATS;
187 -       sbi->s_mb_stream_request = MB_DEFAULT_STREAM_THRESHOLD;
188         sbi->s_mb_order2_reqs = MB_DEFAULT_ORDER2_REQS;
189         sbi->s_mb_max_inode_prealloc = MB_DEFAULT_MAX_INODE_PREALLOC;
190         /*
191 @@ -3479,9 +3571,29 @@ int ext4_mb_init(struct super_block *sb)
192          * RAID stripe size so that preallocations don't fragment
193          * the stripes.
194          */
195 -       if (sbi->s_stripe > 1) {
196 -               sbi->s_mb_group_prealloc = roundup(
197 -                       sbi->s_mb_group_prealloc, sbi->s_stripe);
198 +
199 +       /* Allocate table once */
200 +       sbi->s_mb_prealloc_table = kzalloc(
201 +               EXT4_MAX_PREALLOC_TABLE * sizeof(unsigned long), GFP_NOFS);
202 +       if (sbi->s_mb_prealloc_table == NULL) {
203 +               ret = -ENOMEM;
204 +               goto out;
205 +       }
206 +
207 +       if (sbi->s_stripe == 0) {
208 +               for (k = 0, l = 4; k <= 9; ++k, l *= 2)
209 +                       sbi->s_mb_prealloc_table[k] = l;
210 +
211 +               sbi->s_mb_small_req = 256;
212 +               sbi->s_mb_large_req = 1024;
213 +               sbi->s_mb_group_prealloc = 512;
214 +       } else {
215 +               for (k = 0, l = sbi->s_stripe; k <= 2; ++k, l *= 2)
216 +                       sbi->s_mb_prealloc_table[k] = l;
217 +
218 +               sbi->s_mb_small_req = sbi->s_stripe;
219 +               sbi->s_mb_large_req = sbi->s_stripe * 8;
220 +               sbi->s_mb_group_prealloc = sbi->s_stripe * 4;
221         }
222  
223         sbi->s_locality_groups = alloc_percpu(struct ext4_locality_group);
224 @@ -3515,6 +3627,7 @@ out_free_locality_groups:
225  out:
226         kfree(sbi->s_mb_largest_free_orders);
227         kfree(sbi->s_mb_largest_free_orders_locks);
228 +       kfree(sbi->s_mb_prealloc_table);
229         kfree(sbi->s_mb_offsets);
230         sbi->s_mb_offsets = NULL;
231         kfree(sbi->s_mb_maxs);
232 @@ -3783,7 +3896,6 @@ ext4_mb_mark_diskspace_used(struct ext4_allocation_context *ac,
233         int err, len;
234  
235         BUG_ON(ac->ac_status != AC_STATUS_FOUND);
236 -       BUG_ON(ac->ac_b_ex.fe_len <= 0);
237  
238         sb = ac->ac_sb;
239         sbi = EXT4_SB(sb);
240 @@ -4026,13 +4138,14 @@ ext4_mb_normalize_request(struct ext4_allocation_context *ac,
241                                 struct ext4_allocation_request *ar)
242  {
243         struct ext4_sb_info *sbi = EXT4_SB(ac->ac_sb);
244 -       int bsbits, max;
245 +       int bsbits, i, wind;
246         ext4_lblk_t end;
247 -       loff_t size, start_off;
248 +       loff_t size;
249         loff_t orig_size __maybe_unused;
250         ext4_lblk_t start;
251         struct ext4_inode_info *ei = EXT4_I(ac->ac_inode);
252         struct ext4_prealloc_space *pa;
253 +       unsigned long value, last_non_zero;
254  
255         /* do normalize only data requests, metadata requests
256            do not need preallocation */
257 @@ -4061,51 +4174,46 @@ ext4_mb_normalize_request(struct ext4_allocation_context *ac,
258         size = size << bsbits;
259         if (size < i_size_read(ac->ac_inode))
260                 size = i_size_read(ac->ac_inode);
261 -       orig_size = size;
262 +       size = (size + ac->ac_sb->s_blocksize - 1) >> bsbits;
263 +
264 +       start = wind = 0;
265 +       value = last_non_zero = 0;
266 +
267 +       /* let's choose preallocation window depending on file size */
268 +       for (i = 0; i < EXT4_MAX_PREALLOC_TABLE; i++) {
269 +               value = sbi->s_mb_prealloc_table[i];
270 +               if (value == 0)
271 +                       break;
272 +               else
273 +                       last_non_zero = value;
274  
275 -       /* max size of free chunks */
276 -       max = 2 << bsbits;
277 -
278 -#define NRL_CHECK_SIZE(req, size, max, chunk_size)     \
279 -               (req <= (size) || max <= (chunk_size))
280 -
281 -       /* first, try to predict filesize */
282 -       /* XXX: should this table be tunable? */
283 -       start_off = 0;
284 -       if (size <= 16 * 1024) {
285 -               size = 16 * 1024;
286 -       } else if (size <= 32 * 1024) {
287 -               size = 32 * 1024;
288 -       } else if (size <= 64 * 1024) {
289 -               size = 64 * 1024;
290 -       } else if (size <= 128 * 1024) {
291 -               size = 128 * 1024;
292 -       } else if (size <= 256 * 1024) {
293 -               size = 256 * 1024;
294 -       } else if (size <= 512 * 1024) {
295 -               size = 512 * 1024;
296 -       } else if (size <= 1024 * 1024) {
297 -               size = 1024 * 1024;
298 -       } else if (NRL_CHECK_SIZE(size, 4 * 1024 * 1024, max, 2 * 1024)) {
299 -               start_off = ((loff_t)ac->ac_o_ex.fe_logical >>
300 -                                               (21 - bsbits)) << 21;
301 -               size = 2 * 1024 * 1024;
302 -       } else if (NRL_CHECK_SIZE(size, 8 * 1024 * 1024, max, 4 * 1024)) {
303 -               start_off = ((loff_t)ac->ac_o_ex.fe_logical >>
304 -                                                       (22 - bsbits)) << 22;
305 -               size = 4 * 1024 * 1024;
306 -       } else if (NRL_CHECK_SIZE(ac->ac_o_ex.fe_len,
307 -                                       (8<<20)>>bsbits, max, 8 * 1024)) {
308 -               start_off = ((loff_t)ac->ac_o_ex.fe_logical >>
309 -                                                       (23 - bsbits)) << 23;
310 -               size = 8 * 1024 * 1024;
311 +               if (size <= value) {
312 +                       wind = value;
313 +                       break;
314 +               }
315 +       }
316 +
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         /*
345          * For tiny groups (smaller than 8MB) the chosen allocation
346 @@ -4196,7 +4304,6 @@ ext4_mb_normalize_request(struct ext4_allocation_context *ac,
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 @@ -5199,11 +5306,19 @@ static void ext4_mb_group_or_file(struct ext4_allocation_context *ac)
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 diff --git a/fs/ext4/sysfs.c b/fs/ext4/sysfs.c
376 index d233c24..9d2254f 100644
377 --- a/fs/ext4/sysfs.c
378 +++ b/fs/ext4/sysfs.c
379 @@ -212,7 +212,8 @@ EXT4_RW_ATTR_SBI_UI(mb_stats, s_mb_stats);
380  EXT4_RW_ATTR_SBI_UI(mb_max_to_scan, s_mb_max_to_scan);
381  EXT4_RW_ATTR_SBI_UI(mb_min_to_scan, s_mb_min_to_scan);
382  EXT4_RW_ATTR_SBI_UI(mb_order2_req, s_mb_order2_reqs);
383 -EXT4_RW_ATTR_SBI_UI(mb_stream_req, s_mb_stream_request);
384 +EXT4_RW_ATTR_SBI_UI(mb_small_req, s_mb_small_req);
385 +EXT4_RW_ATTR_SBI_UI(mb_large_req, s_mb_large_req);
386  EXT4_RW_ATTR_SBI_UI(mb_group_prealloc, s_mb_group_prealloc);
387  EXT4_RW_ATTR_SBI_UI(mb_max_inode_prealloc, s_mb_max_inode_prealloc);
388  EXT4_RW_ATTR_SBI_UI(mb_max_linear_groups, s_mb_max_linear_groups);
389 @@ -262,7 +263,8 @@ static struct attribute *ext4_attrs[] = {
390         ATTR_LIST(mb_max_to_scan),
391         ATTR_LIST(mb_min_to_scan),
392         ATTR_LIST(mb_order2_req),
393 -       ATTR_LIST(mb_stream_req),
394 +       ATTR_LIST(mb_small_req),
395 +       ATTR_LIST(mb_large_req),
396         ATTR_LIST(mb_group_prealloc),
397         ATTR_LIST(mb_max_inode_prealloc),
398         ATTR_LIST(mb_max_linear_groups),
399 @@ -543,6 +545,8 @@ int ext4_register_sysfs(struct super_block *sb)
400                                         ext4_fc_info_show, sb);
401                 proc_create_seq_data("mb_groups", S_IRUGO, sbi->s_proc,
402                                 &ext4_mb_seq_groups_ops, sb);
403 +               proc_create_data("prealloc_table", S_IRUGO, sbi->s_proc,
404 +                               &ext4_seq_prealloc_table_fops, sb);
405                 proc_create_single_data("mb_stats", 0444, sbi->s_proc,
406                                 ext4_seq_mb_stats_show, sb);
407                 proc_create_seq_data("mb_structs_summary", 0444, sbi->s_proc,
408 -- 
409 2.34.1
410