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 @@ -2243,6 +1243,7 @@ struct ext4_sb_info {
22 extern void ext4_end_bitmap_read(struct buffer_head *bh, int uptodate);
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
37 +static int ext4_mb_prealloc_table_add(struct ext4_sb_info *sbi, int value)
41 + if (value > (sbi->s_blocks_per_group - 1 - 1 - sbi->s_itb_per_group))
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;
50 + /* they should add values in order */
51 + if (value <= sbi->s_mb_prealloc_table[i])
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,
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)
68 + struct ext4_sb_info *sbi = EXT4_SB(PDE_DATA(file_inode(file)));
69 + unsigned long value;
70 + unsigned long prev = 0;
74 + unsigned long *new_table;
78 + if (cnt >= sizeof(str))
80 + if (copy_from_user(str, buf, cnt))
87 + while ((cur < end) && (*cur == ' '))
89 + value = simple_strtol(cur, &cur, 0);
98 + new_table = kmalloc(num * sizeof(*new_table), GFP_KERNEL);
99 + if (new_table == NULL)
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;
107 + while (cur < end && i < num) {
108 + while (cur < end && *cur == ' ')
110 + value = simple_strtol(cur, &cur, 0);
111 + if (ext4_mb_prealloc_table_add(sbi, value) == 0)
115 + sbi->s_mb_prealloc_table_size = i;
120 +static int mb_prealloc_table_seq_show(struct seq_file *m, void *v)
122 + struct ext4_sb_info *sbi = EXT4_SB(m->private);
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");
132 +static int mb_prealloc_table_seq_open(struct inode *inode, struct file *file)
134 + return single_open(file, mb_prealloc_table_seq_show, PDE_DATA(inode));
137 +const struct file_operations ext4_seq_prealloc_table_fops = {
138 + .owner = THIS_MODULE,
139 + .open = mb_prealloc_table_seq_open,
141 + .llseek = seq_lseek,
142 + .release = single_release,
143 + .write = ext4_mb_prealloc_table_proc_write,
146 static int ext4_mb_seq_groups_open(struct inode *inode, struct file *file)
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)
152 struct ext4_sb_info *sbi = EXT4_SB(sb);
154 + unsigned i, j, k, l;
155 unsigned offset, offset_incr;
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;
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
170 - if (sbi->s_stripe > 1) {
171 - sbi->s_mb_group_prealloc = roundup(
172 - sbi->s_mb_group_prealloc, sbi->s_stripe);
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) {
182 + memset(sbi->s_mb_prealloc_table, 0, i);
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;
191 + sbi->s_mb_small_req = 256;
192 + sbi->s_mb_large_req = 1024;
193 + sbi->s_mb_group_prealloc = 512;
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) {
202 + memset(sbi->s_mb_prealloc_table, 0, i);
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;
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;
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)
220 struct ext4_sb_info *sbi = EXT4_SB(ac->ac_sb);
222 + int bsbits, i, wind;
224 - loff_t size, start_off;
226 loff_t orig_size __maybe_unused;
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);
234 + size = (size + ac->ac_sb->s_blocksize - 1) >> bsbits;
236 - /* max size of free chunks */
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];
251 - /* first, try to predict filesize */
252 - /* XXX: should this table be tunable? */
254 - if (size <= 16 * 1024) {
256 - } else if (size <= 32 * 1024) {
258 - } else if (size <= 64 * 1024) {
260 - } else if (size <= 128 * 1024) {
262 - } else if (size <= 256 * 1024) {
264 - } else if (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;
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;
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;
299 - size = size >> bsbits;
300 - start = start_off >> bsbits;
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);
309 - BUG_ON(size <= 0 || size > EXT4_BLOCKS_PER_GROUP(ac->ac_sb));
311 /* now prepare goal request */
313 @@ -4056,11 +4183,19 @@ static void ext4_mb_group_or_file(struct
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;
325 + * request is so large that we don't care about
326 + * streaming - it overweights any possible seek
328 + if (ac->ac_o_ex.fe_len >= sbi->s_mb_large_req)
331 BUG_ON(ac->ac_lg != NULL);
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))
366 + if (wbc->nr_to_write < sbi->s_mb_small_req)
367 + wbc->nr_to_write = sbi->s_mb_small_req;
369 if (wbc->range_start == 0 && wbc->range_end == LLONG_MAX)
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),