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 {
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 Index: linux-3.10.0-123.el7.x86_64/fs/ext4/mballoc.c
22 ===================================================================
23 --- linux-3.10.0-123.el7.x86_64.orig/fs/ext4/mballoc.c
24 +++ linux-3.10.0-123.el7.x86_64/fs/ext4/mballoc.c
25 @@ -1828,6 +1828,25 @@ int ext4_mb_find_by_goal(struct ext4_all
29 +static void ext4_mb_prealloc_table_add(struct ext4_sb_info *sbi, int value)
33 + if (value > (sbi->s_blocks_per_group - 1 - 1 - sbi->s_itb_per_group))
36 + for (i = 0; i < sbi->s_mb_prealloc_table_size; i++) {
37 + if (sbi->s_mb_prealloc_table[i] == 0) {
38 + sbi->s_mb_prealloc_table[i] = value;
42 + /* they should add values in order */
43 + if (value <= sbi->s_mb_prealloc_table[i])
49 * The routine scans buddy structures (not bitmap!) from given order
50 * to max order and tries to find big enough chunk to satisfy the req
51 @@ -2263,6 +2282,86 @@ static const struct seq_operations ext4_
52 .show = ext4_mb_seq_groups_show,
55 +#define EXT4_MB_PREALLOC_TABLE "prealloc_table"
57 +static ssize_t ext4_mb_prealloc_table_proc_write(struct file *file,
58 + const char __user *buf,
59 + size_t cnt, loff_t *pos)
61 + struct ext4_sb_info *sbi = EXT4_SB(PDE_DATA(file_inode(file)));
62 + unsigned long value;
63 + unsigned long prev = 0;
67 + unsigned long *new_table;
71 + if (cnt >= sizeof(str))
73 + if (copy_from_user(str, buf, cnt))
80 + while ((cur < end) && (*cur == ' ')) cur++;
81 + value = simple_strtol(cur, &cur, 0);
90 + new_table = kmalloc(num * sizeof(*new_table), GFP_KERNEL);
91 + if (new_table == NULL)
93 + kfree(sbi->s_mb_prealloc_table);
94 + memset(new_table, 0, num * sizeof(*new_table));
95 + sbi->s_mb_prealloc_table = new_table;
96 + sbi->s_mb_prealloc_table_size = num;
99 + while (cur < end && i < num) {
100 + while ((cur < end) && (*cur == ' ')) cur++;
101 + value = simple_strtol(cur, &cur, 0);
102 + ext4_mb_prealloc_table_add(sbi, value);
109 +static int mb_prealloc_table_seq_show(struct seq_file *m, void *v)
111 + struct ext4_sb_info *sbi = EXT4_SB(m->private);
114 + for (i = 0; i < sbi->s_mb_prealloc_table_size; i++)
115 + seq_printf(m, "%ld ", sbi->s_mb_prealloc_table[i]);
116 + seq_printf(m, "\n");
121 +static int mb_prealloc_table_seq_open(struct inode *inode, struct file *file)
123 + return single_open(file, mb_prealloc_table_seq_show, PDE_DATA(inode));
126 +struct file_operations ext4_mb_prealloc_seq_fops = {
127 + .owner = THIS_MODULE,
128 + .open = mb_prealloc_table_seq_open,
130 + .llseek = seq_lseek,
131 + .release = single_release,
132 + .write = ext4_mb_prealloc_table_proc_write,
135 static int ext4_mb_seq_groups_open(struct inode *inode, struct file *file)
137 struct super_block *sb = PDE_DATA(inode);
138 @@ -2557,7 +2656,6 @@ int ext4_mb_init(struct super_block *sb)
139 sbi->s_mb_max_to_scan = MB_DEFAULT_MAX_TO_SCAN;
140 sbi->s_mb_min_to_scan = MB_DEFAULT_MIN_TO_SCAN;
141 sbi->s_mb_stats = MB_DEFAULT_STATS;
142 - sbi->s_mb_stream_request = MB_DEFAULT_STREAM_THRESHOLD;
143 sbi->s_mb_order2_reqs = MB_DEFAULT_ORDER2_REQS;
145 * The default group preallocation is 512, which for 4k block
146 @@ -2581,9 +2679,48 @@ int ext4_mb_init(struct super_block *sb)
147 * RAID stripe size so that preallocations don't fragment
150 - if (sbi->s_stripe > 1) {
151 - sbi->s_mb_group_prealloc = roundup(
152 - sbi->s_mb_group_prealloc, sbi->s_stripe);
154 + if (sbi->s_stripe == 0) {
155 + sbi->s_mb_prealloc_table_size = 10;
156 + i = sbi->s_mb_prealloc_table_size * sizeof(unsigned long);
157 + sbi->s_mb_prealloc_table = kmalloc(i, GFP_NOFS);
158 + if (sbi->s_mb_prealloc_table == NULL) {
162 + memset(sbi->s_mb_prealloc_table, 0, i);
164 + ext4_mb_prealloc_table_add(sbi, 4);
165 + ext4_mb_prealloc_table_add(sbi, 8);
166 + ext4_mb_prealloc_table_add(sbi, 16);
167 + ext4_mb_prealloc_table_add(sbi, 32);
168 + ext4_mb_prealloc_table_add(sbi, 64);
169 + ext4_mb_prealloc_table_add(sbi, 128);
170 + ext4_mb_prealloc_table_add(sbi, 256);
171 + ext4_mb_prealloc_table_add(sbi, 512);
172 + ext4_mb_prealloc_table_add(sbi, 1024);
173 + ext4_mb_prealloc_table_add(sbi, 2048);
175 + sbi->s_mb_small_req = 256;
176 + sbi->s_mb_large_req = 1024;
177 + sbi->s_mb_group_prealloc = 512;
179 + sbi->s_mb_prealloc_table_size = 3;
180 + i = sbi->s_mb_prealloc_table_size * sizeof(unsigned long);
181 + sbi->s_mb_prealloc_table = kmalloc(i, GFP_NOFS);
182 + if (sbi->s_mb_prealloc_table == NULL) {
186 + memset(sbi->s_mb_prealloc_table, 0, i);
188 + ext4_mb_prealloc_table_add(sbi, sbi->s_stripe);
189 + ext4_mb_prealloc_table_add(sbi, sbi->s_stripe * 2);
190 + ext4_mb_prealloc_table_add(sbi, sbi->s_stripe * 4);
192 + sbi->s_mb_small_req = sbi->s_stripe;
193 + sbi->s_mb_large_req = sbi->s_stripe * 8;
194 + sbi->s_mb_group_prealloc = sbi->s_stripe * 4;
197 sbi->s_locality_groups = alloc_percpu(struct ext4_locality_group);
198 @@ -2605,9 +2742,13 @@ int ext4_mb_init(struct super_block *sb)
200 goto out_free_locality_groups;
204 proc_create_data("mb_groups", S_IRUGO, sbi->s_proc,
205 &ext4_mb_seq_groups_fops, sb);
206 + proc_create_data(EXT4_MB_PREALLOC_TABLE, S_IFREG | S_IRUGO |
207 + S_IWUSR, sbi->s_proc,
208 + &ext4_mb_prealloc_seq_fops, sb);
213 @@ -2615,6 +2756,7 @@ out_free_locality_groups:
214 free_percpu(sbi->s_locality_groups);
215 sbi->s_locality_groups = NULL;
217 + kfree(sbi->s_mb_prealloc_table);
218 kfree(sbi->s_mb_offsets);
221 @@ -2651,8 +2793,10 @@ int ext4_mb_release(struct super_block *
222 struct ext4_sb_info *sbi = EXT4_SB(sb);
223 struct kmem_cache *cachep = get_groupinfo_cache(sb->s_blocksize_bits);
227 remove_proc_entry("mb_groups", sbi->s_proc);
228 + remove_proc_entry(EXT4_MB_PREALLOC_TABLE, sbi->s_proc);
231 if (sbi->s_group_info) {
232 for (i = 0; i < ngroups; i++) {
233 @@ -2963,9 +3107,9 @@ ext4_mb_normalize_request(struct ext4_al
234 struct ext4_allocation_request *ar)
236 struct ext4_sb_info *sbi = EXT4_SB(ac->ac_sb);
238 + int bsbits, i, wind;
240 - loff_t size, start_off;
242 loff_t orig_size __maybe_unused;
244 struct ext4_inode_info *ei = EXT4_I(ac->ac_inode);
245 @@ -2998,51 +3142,34 @@ ext4_mb_normalize_request(struct ext4_al
246 size = size << bsbits;
247 if (size < i_size_read(ac->ac_inode))
248 size = i_size_read(ac->ac_inode);
250 + size = (size + ac->ac_sb->s_blocksize - 1) >> bsbits;
252 - /* max size of free chunks */
256 -#define NRL_CHECK_SIZE(req, size, max, chunk_size) \
257 - (req <= (size) || max <= (chunk_size))
258 + /* let's choose preallocation window depending on file size */
259 + for (i = 0; i < sbi->s_mb_prealloc_table_size; i++) {
260 + if (size <= sbi->s_mb_prealloc_table[i]) {
261 + wind = sbi->s_mb_prealloc_table[i];
267 - /* first, try to predict filesize */
268 - /* XXX: should this table be tunable? */
270 - if (size <= 16 * 1024) {
272 - } else if (size <= 32 * 1024) {
274 - } else if (size <= 64 * 1024) {
276 - } else if (size <= 128 * 1024) {
278 - } else if (size <= 256 * 1024) {
280 - } else if (size <= 512 * 1024) {
282 - } else if (size <= 1024 * 1024) {
283 - size = 1024 * 1024;
284 - } else if (NRL_CHECK_SIZE(size, 4 * 1024 * 1024, max, 2 * 1024)) {
285 - start_off = ((loff_t)ac->ac_o_ex.fe_logical >>
286 - (21 - bsbits)) << 21;
287 - size = 2 * 1024 * 1024;
288 - } else if (NRL_CHECK_SIZE(size, 8 * 1024 * 1024, max, 4 * 1024)) {
289 - start_off = ((loff_t)ac->ac_o_ex.fe_logical >>
290 - (22 - bsbits)) << 22;
291 - size = 4 * 1024 * 1024;
292 - } else if (NRL_CHECK_SIZE(ac->ac_o_ex.fe_len,
293 - (8<<20)>>bsbits, max, 8 * 1024)) {
294 - start_off = ((loff_t)ac->ac_o_ex.fe_logical >>
295 - (23 - bsbits)) << 23;
296 - size = 8 * 1024 * 1024;
298 - start_off = (loff_t) ac->ac_o_ex.fe_logical << bsbits;
299 - size = (loff_t) EXT4_C2B(EXT4_SB(ac->ac_sb),
300 - ac->ac_o_ex.fe_len) << bsbits;
302 + __u64 tstart, tend;
303 + /* file is quite large, we now preallocate with
304 + * the biggest configured window with regart to
305 + * logical offset */
306 + wind = sbi->s_mb_prealloc_table[i - 1];
307 + tstart = ac->ac_o_ex.fe_logical;
308 + do_div(tstart, wind);
309 + start = tstart * wind;
310 + tend = ac->ac_o_ex.fe_logical + ac->ac_o_ex.fe_len - 1;
311 + do_div(tend, wind);
312 + tend = tend * wind + wind;
313 + size = tend - start;
315 - size = size >> bsbits;
316 - start = start_off >> bsbits;
319 /* don't cover already allocated blocks in selected range */
320 if (ar->pleft && start <= ar->lleft) {
321 @@ -3117,7 +3245,6 @@ ext4_mb_normalize_request(struct ext4_al
322 BUG_ON(start + size <= ac->ac_o_ex.fe_logical &&
323 start > ac->ac_o_ex.fe_logical);
325 - BUG_ON(size <= 0 || size > EXT4_BLOCKS_PER_GROUP(ac->ac_sb));
327 /* now prepare goal request */
329 @@ -4056,11 +4183,19 @@ static void ext4_mb_group_or_file(struct
331 /* don't use group allocation for large files */
332 size = max(size, isize);
333 - if (size > sbi->s_mb_stream_request) {
334 + if ((ac->ac_o_ex.fe_len >= sbi->s_mb_small_req) ||
335 + (size >= sbi->s_mb_large_req)) {
336 ac->ac_flags |= EXT4_MB_STREAM_ALLOC;
341 + * request is so large that we don't care about
342 + * streaming - it overweights any possible seek
344 + if (ac->ac_o_ex.fe_len >= sbi->s_mb_large_req)
347 BUG_ON(ac->ac_lg != NULL);
349 * locality group prealloc space are per cpu. The reason for having
350 Index: linux-3.10.0-123.el7.x86_64/fs/ext4/super.c
351 ===================================================================
352 --- linux-3.10.0-123.el7.x86_64.orig/fs/ext4/super.c
353 +++ linux-3.10.0-123.el7.x86_64/fs/ext4/super.c
354 @@ -2555,7 +2555,8 @@ EXT4_RW_ATTR_SBI_UI(mb_stats, s_mb_stats
355 EXT4_RW_ATTR_SBI_UI(mb_max_to_scan, s_mb_max_to_scan);
356 EXT4_RW_ATTR_SBI_UI(mb_min_to_scan, s_mb_min_to_scan);
357 EXT4_RW_ATTR_SBI_UI(mb_order2_req, s_mb_order2_reqs);
358 -EXT4_RW_ATTR_SBI_UI(mb_stream_req, s_mb_stream_request);
359 +EXT4_RW_ATTR_SBI_UI(mb_small_req, s_mb_small_req);
360 +EXT4_RW_ATTR_SBI_UI(mb_large_req, s_mb_large_req);
361 EXT4_RW_ATTR_SBI_UI(mb_group_prealloc, s_mb_group_prealloc);
362 EXT4_RW_ATTR_SBI_UI(max_writeback_mb_bump, s_max_writeback_mb_bump);
363 EXT4_RW_ATTR_SBI_UI(extent_max_zeroout_kb, s_extent_max_zeroout_kb);
364 @@ -2578,7 +2579,8 @@ static struct attribute *ext4_attrs[] =
365 ATTR_LIST(mb_max_to_scan),
366 ATTR_LIST(mb_min_to_scan),
367 ATTR_LIST(mb_order2_req),
368 - ATTR_LIST(mb_stream_req),
369 + ATTR_LIST(mb_small_req),
370 + ATTR_LIST(mb_large_req),
371 ATTR_LIST(mb_group_prealloc),
372 ATTR_LIST(max_writeback_mb_bump),
373 ATTR_LIST(extent_max_zeroout_kb),
374 Index: linux-3.10.0-123.el7.x86_64/fs/ext4/inode.c
375 ===================================================================
376 --- linux-3.10.0-123.el7.x86_64.orig/fs/ext4/inode.c
377 +++ linux-3.10.0-123.el7.x86_64/fs/ext4/inode.c
378 @@ -2476,6 +2476,10 @@ static int ext4_da_writepages(struct add
379 if (unlikely(sbi->s_mount_flags & EXT4_MF_FS_ABORTED))
382 + if (wbc->nr_to_write < sbi->s_mb_small_req) {
383 + wbc->nr_to_write = sbi->s_mb_small_req;
386 if (wbc->range_start == 0 && wbc->range_end == LLONG_MAX)