1 commit d8d8fd9192a54c7b8caef8cca9b7a1eb5e5e3298
2 Author: Alex Zhuravlev <alex.zhuravlev@sun.com>
3 AuthorDate: Thu Oct 23 10:02:19 2008 +0000
5 Subject: ext4: support for tunable preallocation window
6 Add support for tunable preallocation window and new tunables
7 for large/small requests.
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>
14 Index: linux-4.18.0-80.1.2.el8_0/fs/ext4/ext4.h
15 ===================================================================
16 --- linux-4.18.0-80.1.2.el8_0.orig/fs/ext4/ext4.h
17 +++ linux-4.18.0-80.1.2.el8_0/fs/ext4/ext4.h
18 @@ -1185,6 +1185,8 @@ extern void ext4_set_bits(void *bm, int
19 /* Metadata checksum algorithm codes */
20 #define EXT4_CRC32C_CHKSUM 1
22 +#define EXT4_MAX_PREALLOC_TABLE 64
25 * Structure of the super block
27 @@ -1418,11 +1420,13 @@ struct ext4_sb_info {
30 unsigned long s_stripe;
31 - unsigned int s_mb_stream_request;
32 + unsigned long s_mb_small_req;
33 + unsigned long s_mb_large_req;
34 unsigned int s_mb_max_to_scan;
35 unsigned int s_mb_min_to_scan;
36 unsigned int s_mb_stats;
37 unsigned int s_mb_order2_reqs;
38 + unsigned long *s_mb_prealloc_table;
39 unsigned int s_mb_group_prealloc;
40 unsigned int s_max_dir_size_kb;
41 /* where last allocation was done - for stream allocation */
42 @@ -2397,6 +2401,7 @@ extern int ext4_init_inode_table(struct
43 extern void ext4_end_bitmap_read(struct buffer_head *bh, int uptodate);
46 +extern const struct file_operations ext4_seq_prealloc_table_fops;
47 extern const struct seq_operations ext4_mb_seq_groups_ops;
48 extern long ext4_mb_stats;
49 extern long ext4_mb_max_to_scan;
50 Index: linux-4.18.0-80.1.2.el8_0/fs/ext4/inode.c
51 ===================================================================
52 --- linux-4.18.0-80.1.2.el8_0.orig/fs/ext4/inode.c
53 +++ linux-4.18.0-80.1.2.el8_0/fs/ext4/inode.c
54 @@ -2769,6 +2769,9 @@ static int ext4_writepages(struct addres
55 ext4_journal_stop(handle);
58 + if (wbc->nr_to_write < sbi->s_mb_small_req)
59 + wbc->nr_to_write = sbi->s_mb_small_req;
61 if (wbc->range_start == 0 && wbc->range_end == LLONG_MAX)
64 Index: linux-4.18.0-80.1.2.el8_0/fs/ext4/mballoc.c
65 ===================================================================
66 --- linux-4.18.0-80.1.2.el8_0.orig/fs/ext4/mballoc.c
67 +++ linux-4.18.0-80.1.2.el8_0/fs/ext4/mballoc.c
68 @@ -2339,6 +2339,100 @@ const struct seq_operations ext4_mb_seq_
69 .show = ext4_mb_seq_groups_show,
72 +static int ext4_mb_check_and_update_prealloc(struct ext4_sb_info *sbi,
73 + char *str, size_t cnt,
76 + unsigned long value;
77 + unsigned long prev = 0;
86 + while ((cur < end) && (*cur == ' ')) cur++;
87 + value = simple_strtol(cur, &next, 0);
95 + if (value > (sbi->s_blocks_per_group - 1 - 1 - sbi->s_itb_per_group))
98 + /* they should add values in order */
103 + sbi->s_mb_prealloc_table[num] = value;
109 + if (num > EXT4_MAX_PREALLOC_TABLE - 1)
113 + sbi->s_mb_prealloc_table[num] = 0;
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)
122 + struct ext4_sb_info *sbi = EXT4_SB(PDE_DATA(file_inode(file)));
126 + if (cnt >= sizeof(str))
128 + if (copy_from_user(str, buf, cnt))
131 + rc = ext4_mb_check_and_update_prealloc(sbi, str, cnt, 0);
135 + rc = ext4_mb_check_and_update_prealloc(sbi, str, cnt, 1);
136 + return rc ? rc : cnt;
139 +static int mb_prealloc_table_seq_show(struct seq_file *m, void *v)
141 + struct ext4_sb_info *sbi = EXT4_SB(m->private);
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");
152 +static int mb_prealloc_table_seq_open(struct inode *inode, struct file *file)
154 + return single_open(file, mb_prealloc_table_seq_show, PDE_DATA(inode));
157 +const struct file_operations ext4_seq_prealloc_table_fops = {
158 + .owner = THIS_MODULE,
159 + .open = mb_prealloc_table_seq_open,
161 + .llseek = seq_lseek,
162 + .release = single_release,
163 + .write = ext4_mb_prealloc_table_proc_write,
166 static struct kmem_cache *get_groupinfo_cache(int blocksize_bits)
168 int cache_index = blocksize_bits - EXT4_MIN_BLOCK_LOG_SIZE;
169 @@ -2566,7 +2660,7 @@ static int ext4_groupinfo_create_slab(si
170 int ext4_mb_init(struct super_block *sb)
172 struct ext4_sb_info *sbi = EXT4_SB(sb);
174 + unsigned i, j, k, l;
175 unsigned offset, offset_incr;
178 @@ -2615,7 +2709,6 @@ int ext4_mb_init(struct super_block *sb)
179 sbi->s_mb_max_to_scan = MB_DEFAULT_MAX_TO_SCAN;
180 sbi->s_mb_min_to_scan = MB_DEFAULT_MIN_TO_SCAN;
181 sbi->s_mb_stats = MB_DEFAULT_STATS;
182 - sbi->s_mb_stream_request = MB_DEFAULT_STREAM_THRESHOLD;
183 sbi->s_mb_order2_reqs = MB_DEFAULT_ORDER2_REQS;
185 * The default group preallocation is 512, which for 4k block
186 @@ -2639,9 +2732,29 @@ int ext4_mb_init(struct super_block *sb)
187 * RAID stripe size so that preallocations don't fragment
190 - if (sbi->s_stripe > 1) {
191 - sbi->s_mb_group_prealloc = roundup(
192 - sbi->s_mb_group_prealloc, sbi->s_stripe);
194 + /* Allocate table once */
195 + sbi->s_mb_prealloc_table = kzalloc(
196 + EXT4_MAX_PREALLOC_TABLE * sizeof(unsigned long), GFP_NOFS);
197 + if (sbi->s_mb_prealloc_table == NULL) {
202 + if (sbi->s_stripe == 0) {
203 + for (k = 0, l = 4; k <= 9; ++k, l *= 2)
204 + sbi->s_mb_prealloc_table[k] = l;
206 + sbi->s_mb_small_req = 256;
207 + sbi->s_mb_large_req = 1024;
208 + sbi->s_mb_group_prealloc = 512;
210 + for (k = 0, l = sbi->s_stripe; k <= 2; ++k, l *= 2)
211 + sbi->s_mb_prealloc_table[k] = l;
213 + sbi->s_mb_small_req = sbi->s_stripe;
214 + sbi->s_mb_large_req = sbi->s_stripe * 8;
215 + sbi->s_mb_group_prealloc = sbi->s_stripe * 4;
218 sbi->s_locality_groups = alloc_percpu(struct ext4_locality_group);
219 @@ -2669,6 +2782,7 @@ out_free_locality_groups:
220 free_percpu(sbi->s_locality_groups);
221 sbi->s_locality_groups = NULL;
223 + kfree(sbi->s_mb_prealloc_table);
224 kfree(sbi->s_mb_offsets);
225 sbi->s_mb_offsets = NULL;
226 kfree(sbi->s_mb_maxs);
227 @@ -2800,6 +2840,7 @@ int ext4_mb_release(struct super_block *sb)
231 + kfree(sbi->s_mb_prealloc_table);
232 kfree(sbi->s_mb_offsets);
233 kfree(sbi->s_mb_maxs);
234 iput(sbi->s_buddy_cache);
235 @@ -2930,7 +3044,6 @@ ext4_mb_mark_diskspace_used(struct ext4_
238 BUG_ON(ac->ac_status != AC_STATUS_FOUND);
239 - BUG_ON(ac->ac_b_ex.fe_len <= 0);
243 @@ -3060,13 +3173,14 @@ ext4_mb_normalize_request(struct ext4_al
244 struct ext4_allocation_request *ar)
246 struct ext4_sb_info *sbi = EXT4_SB(ac->ac_sb);
248 + int bsbits, i, wind;
250 - loff_t size, start_off;
252 loff_t orig_size __maybe_unused;
254 struct ext4_inode_info *ei = EXT4_I(ac->ac_inode);
255 struct ext4_prealloc_space *pa;
256 + unsigned long value, last_non_zero;
258 /* do normalize only data requests, metadata requests
259 do not need preallocation */
260 @@ -3095,51 +3209,46 @@ ext4_mb_normalize_request(struct ext4_al
261 size = size << bsbits;
262 if (size < i_size_read(ac->ac_inode))
263 size = i_size_read(ac->ac_inode);
265 + size = (size + ac->ac_sb->s_blocksize - 1) >> bsbits;
268 + value = last_non_zero = 0;
270 - /* max size of free chunks */
272 + /* let's choose preallocation window depending on file size */
273 + for (i = 0; i < EXT4_MAX_PREALLOC_TABLE; i++) {
274 + value = sbi->s_mb_prealloc_table[i];
278 + last_non_zero = value;
280 -#define NRL_CHECK_SIZE(req, size, max, chunk_size) \
281 - (req <= (size) || max <= (chunk_size))
282 + if (size <= value) {
288 - /* first, try to predict filesize */
289 - /* XXX: should this table be tunable? */
291 - if (size <= 16 * 1024) {
293 - } else if (size <= 32 * 1024) {
295 - } else if (size <= 64 * 1024) {
297 - } else if (size <= 128 * 1024) {
299 - } else if (size <= 256 * 1024) {
301 - } else if (size <= 512 * 1024) {
303 - } else if (size <= 1024 * 1024) {
304 - size = 1024 * 1024;
305 - } else if (NRL_CHECK_SIZE(size, 4 * 1024 * 1024, max, 2 * 1024)) {
306 - start_off = ((loff_t)ac->ac_o_ex.fe_logical >>
307 - (21 - bsbits)) << 21;
308 - size = 2 * 1024 * 1024;
309 - } else if (NRL_CHECK_SIZE(size, 8 * 1024 * 1024, max, 4 * 1024)) {
310 - start_off = ((loff_t)ac->ac_o_ex.fe_logical >>
311 - (22 - bsbits)) << 22;
312 - size = 4 * 1024 * 1024;
313 - } else if (NRL_CHECK_SIZE(ac->ac_o_ex.fe_len,
314 - (8<<20)>>bsbits, max, 8 * 1024)) {
315 - start_off = ((loff_t)ac->ac_o_ex.fe_logical >>
316 - (23 - bsbits)) << 23;
317 - size = 8 * 1024 * 1024;
319 + if (last_non_zero != 0) {
320 + __u64 tstart, tend;
321 + /* file is quite large, we now preallocate with
322 + * the biggest configured window with regart to
323 + * logical offset */
324 + wind = last_non_zero;
325 + tstart = ac->ac_o_ex.fe_logical;
326 + do_div(tstart, wind);
327 + start = tstart * wind;
328 + tend = ac->ac_o_ex.fe_logical + ac->ac_o_ex.fe_len - 1;
329 + do_div(tend, wind);
330 + tend = tend * wind + wind;
331 + size = tend - start;
334 - start_off = (loff_t) ac->ac_o_ex.fe_logical << bsbits;
335 - size = (loff_t) EXT4_C2B(EXT4_SB(ac->ac_sb),
336 - ac->ac_o_ex.fe_len) << bsbits;
339 - size = size >> bsbits;
340 - start = start_off >> bsbits;
345 /* don't cover already allocated blocks in selected range */
346 if (ar->pleft && start <= ar->lleft) {
347 @@ -3221,7 +3330,6 @@ ext4_mb_normalize_request(struct ext4_al
348 (unsigned long) ac->ac_o_ex.fe_logical);
351 - BUG_ON(size <= 0 || size > EXT4_BLOCKS_PER_GROUP(ac->ac_sb));
353 /* now prepare goal request */
355 @@ -4190,11 +4298,19 @@ static void ext4_mb_group_or_file(struct
357 /* don't use group allocation for large files */
358 size = max(size, isize);
359 - if (size > sbi->s_mb_stream_request) {
360 + if ((ac->ac_o_ex.fe_len >= sbi->s_mb_small_req) ||
361 + (size >= sbi->s_mb_large_req)) {
362 ac->ac_flags |= EXT4_MB_STREAM_ALLOC;
367 + * request is so large that we don't care about
368 + * streaming - it overweights any possible seek
370 + if (ac->ac_o_ex.fe_len >= sbi->s_mb_large_req)
373 BUG_ON(ac->ac_lg != NULL);
375 * locality group prealloc space are per cpu. The reason for having
376 Index: linux-4.18.0-80.1.2.el8_0/fs/ext4/sysfs.c
377 ===================================================================
378 --- linux-4.18.0-80.1.2.el8_0.orig/fs/ext4/sysfs.c
379 +++ linux-4.18.0-80.1.2.el8_0/fs/ext4/sysfs.c
380 @@ -173,7 +173,8 @@ EXT4_RW_ATTR_SBI_UI(mb_stats, s_mb_stats
381 EXT4_RW_ATTR_SBI_UI(mb_max_to_scan, s_mb_max_to_scan);
382 EXT4_RW_ATTR_SBI_UI(mb_min_to_scan, s_mb_min_to_scan);
383 EXT4_RW_ATTR_SBI_UI(mb_order2_req, s_mb_order2_reqs);
384 -EXT4_RW_ATTR_SBI_UI(mb_stream_req, s_mb_stream_request);
385 +EXT4_RW_ATTR_SBI_UI(mb_small_req, s_mb_small_req);
386 +EXT4_RW_ATTR_SBI_UI(mb_large_req, s_mb_large_req);
387 EXT4_RW_ATTR_SBI_UI(mb_group_prealloc, s_mb_group_prealloc);
388 EXT4_RW_ATTR_SBI_UI(extent_max_zeroout_kb, s_extent_max_zeroout_kb);
389 EXT4_ATTR(trigger_fs_error, 0200, trigger_test_error);
390 @@ -201,7 +202,8 @@ static struct attribute *ext4_attrs[] =
391 ATTR_LIST(mb_max_to_scan),
392 ATTR_LIST(mb_min_to_scan),
393 ATTR_LIST(mb_order2_req),
394 - ATTR_LIST(mb_stream_req),
395 + ATTR_LIST(mb_small_req),
396 + ATTR_LIST(mb_large_req),
397 ATTR_LIST(mb_group_prealloc),
398 ATTR_LIST(max_writeback_mb_bump),
399 ATTR_LIST(extent_max_zeroout_kb),
400 @@ -379,6 +381,8 @@ int ext4_register_sysfs(struct super_blo
402 proc_create_seq_data("mb_groups", S_IRUGO, sbi->s_proc,
403 &ext4_mb_seq_groups_ops, sb);
404 + proc_create_data("prealloc_table", S_IRUGO, sbi->s_proc,
405 + &ext4_seq_prealloc_table_fops, sb);