1 Index: linux-3.12.39-47.1/fs/ext4/ext4.h
2 ===================================================================
3 --- linux-3.12.39-47.1.orig/fs/ext4/ext4.h
4 +++ linux-3.12.39-47.1/fs/ext4/ext4.h
5 @@ -1251,11 +1251,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_dir_size_kb;
20 /* where last allocation was done - for stream allocation */
21 Index: linux-3.12.39-47.1/fs/ext4/mballoc.c
22 ===================================================================
23 --- linux-3.12.39-47.1.orig/fs/ext4/mballoc.c
24 +++ linux-3.12.39-47.1/fs/ext4/mballoc.c
25 @@ -1847,6 +1847,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_clusters_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 @@ -2285,6 +2304,91 @@ 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))
81 + while ((cur < end) && (*cur == ' '))
83 + rc = kstrtol(cur, 0, &value);
94 + new_table = kmalloc(num * sizeof(*new_table), GFP_KERNEL);
95 + if (new_table == NULL)
97 + kfree(sbi->s_mb_prealloc_table);
98 + memset(new_table, 0, num * sizeof(*new_table));
99 + sbi->s_mb_prealloc_table = new_table;
100 + sbi->s_mb_prealloc_table_size = num;
103 + while (cur < end && i < num) {
104 + while (cur < end && *cur == ' ')
106 + value = simple_strtol(cur, &cur, 0);
107 + ext4_mb_prealloc_table_add(sbi, value);
114 +static int mb_prealloc_table_seq_show(struct seq_file *m, void *v)
116 + struct ext4_sb_info *sbi = EXT4_SB(m->private);
119 + for (i = 0; i < sbi->s_mb_prealloc_table_size; i++)
120 + seq_printf(m, "%ld ", sbi->s_mb_prealloc_table[i]);
121 + seq_printf(m, "\n");
126 +static int mb_prealloc_table_seq_open(struct inode *inode, struct file *file)
128 + return single_open(file, mb_prealloc_table_seq_show, PDE_DATA(inode));
131 +static const struct file_operations ext4_mb_prealloc_seq_fops = {
132 + .owner = THIS_MODULE,
133 + .open = mb_prealloc_table_seq_open,
135 + .llseek = seq_lseek,
136 + .release = single_release,
137 + .write = ext4_mb_prealloc_table_proc_write,
140 static int ext4_mb_seq_groups_open(struct inode *inode, struct file *file)
142 struct super_block *sb = PDE_DATA(inode);
143 @@ -2579,7 +2683,6 @@ int ext4_mb_init(struct super_block *sb)
144 sbi->s_mb_max_to_scan = MB_DEFAULT_MAX_TO_SCAN;
145 sbi->s_mb_min_to_scan = MB_DEFAULT_MIN_TO_SCAN;
146 sbi->s_mb_stats = MB_DEFAULT_STATS;
147 - sbi->s_mb_stream_request = MB_DEFAULT_STREAM_THRESHOLD;
148 sbi->s_mb_order2_reqs = MB_DEFAULT_ORDER2_REQS;
150 * The default group preallocation is 512, which for 4k block
151 @@ -2603,9 +2706,48 @@ int ext4_mb_init(struct super_block *sb)
152 * RAID stripe size so that preallocations don't fragment
155 - if (sbi->s_stripe > 1) {
156 - sbi->s_mb_group_prealloc = roundup(
157 - sbi->s_mb_group_prealloc, sbi->s_stripe);
159 + if (sbi->s_stripe == 0) {
160 + sbi->s_mb_prealloc_table_size = 10;
161 + i = sbi->s_mb_prealloc_table_size * sizeof(unsigned long);
162 + sbi->s_mb_prealloc_table = kmalloc(i, GFP_NOFS);
163 + if (sbi->s_mb_prealloc_table == NULL) {
167 + memset(sbi->s_mb_prealloc_table, 0, i);
169 + ext4_mb_prealloc_table_add(sbi, 4);
170 + ext4_mb_prealloc_table_add(sbi, 8);
171 + ext4_mb_prealloc_table_add(sbi, 16);
172 + ext4_mb_prealloc_table_add(sbi, 32);
173 + ext4_mb_prealloc_table_add(sbi, 64);
174 + ext4_mb_prealloc_table_add(sbi, 128);
175 + ext4_mb_prealloc_table_add(sbi, 256);
176 + ext4_mb_prealloc_table_add(sbi, 512);
177 + ext4_mb_prealloc_table_add(sbi, 1024);
178 + ext4_mb_prealloc_table_add(sbi, 2048);
180 + sbi->s_mb_small_req = 256;
181 + sbi->s_mb_large_req = 1024;
182 + sbi->s_mb_group_prealloc = 512;
184 + sbi->s_mb_prealloc_table_size = 3;
185 + i = sbi->s_mb_prealloc_table_size * sizeof(unsigned long);
186 + sbi->s_mb_prealloc_table = kmalloc(i, GFP_NOFS);
187 + if (sbi->s_mb_prealloc_table == NULL) {
191 + memset(sbi->s_mb_prealloc_table, 0, i);
193 + ext4_mb_prealloc_table_add(sbi, sbi->s_stripe);
194 + ext4_mb_prealloc_table_add(sbi, sbi->s_stripe * 2);
195 + ext4_mb_prealloc_table_add(sbi, sbi->s_stripe * 4);
197 + sbi->s_mb_small_req = sbi->s_stripe;
198 + sbi->s_mb_large_req = sbi->s_stripe * 8;
199 + sbi->s_mb_group_prealloc = sbi->s_stripe * 4;
202 sbi->s_locality_groups = alloc_percpu(struct ext4_locality_group);
203 @@ -2627,9 +2769,13 @@ int ext4_mb_init(struct super_block *sb)
205 goto out_free_locality_groups;
209 proc_create_data("mb_groups", S_IRUGO, sbi->s_proc,
210 &ext4_mb_seq_groups_fops, sb);
211 + proc_create_data(EXT4_MB_PREALLOC_TABLE, S_IFREG | S_IRUGO |
212 + S_IWUSR, sbi->s_proc,
213 + &ext4_mb_prealloc_seq_fops, sb);
218 @@ -2639,6 +2785,7 @@ out_free_locality_groups:
219 out_free_groupinfo_slab:
220 ext4_groupinfo_destroy_slabs();
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 @@ -2673,8 +2820,10 @@ int ext4_mb_release(struct super_block *
227 struct ext4_sb_info *sbi = EXT4_SB(sb);
228 struct kmem_cache *cachep = get_groupinfo_cache(sb->s_blocksize_bits);
232 remove_proc_entry("mb_groups", sbi->s_proc);
233 + remove_proc_entry(EXT4_MB_PREALLOC_TABLE, sbi->s_proc);
236 if (sbi->s_group_info) {
237 for (i = 0; i < ngroups; i++) {
238 @@ -2985,9 +3134,9 @@ ext4_mb_normalize_request(struct ext4_al
239 struct ext4_allocation_request *ar)
241 struct ext4_sb_info *sbi = EXT4_SB(ac->ac_sb);
243 + int bsbits, i, wind;
245 - loff_t size, start_off;
247 loff_t orig_size __maybe_unused;
249 struct ext4_inode_info *ei = EXT4_I(ac->ac_inode);
250 @@ -3020,50 +3169,34 @@ ext4_mb_normalize_request(struct ext4_al
251 size = size << bsbits;
252 if (size < i_size_read(ac->ac_inode))
253 size = i_size_read(ac->ac_inode);
255 + size = (size + ac->ac_sb->s_blocksize - 1) >> bsbits;
257 - /* max size of free chunks */
261 -#define NRL_CHECK_SIZE(req, size, max, chunk_size) \
262 - (req <= (size) || max <= (chunk_size))
263 + /* let's choose preallocation window depending on file size */
264 + for (i = 0; i < sbi->s_mb_prealloc_table_size; i++) {
265 + if (size <= sbi->s_mb_prealloc_table[i]) {
266 + wind = sbi->s_mb_prealloc_table[i];
272 - /* first, try to predict filesize */
273 - /* XXX: should this table be tunable? */
275 - if (size <= 16 * 1024) {
277 - } else if (size <= 32 * 1024) {
279 - } else if (size <= 64 * 1024) {
281 - } else if (size <= 128 * 1024) {
283 - } else if (size <= 256 * 1024) {
285 - } else if (size <= 512 * 1024) {
287 - } else if (size <= 1024 * 1024) {
288 - size = 1024 * 1024;
289 - } else if (NRL_CHECK_SIZE(size, 4 * 1024 * 1024, max, 2 * 1024)) {
290 - start_off = ((loff_t)ac->ac_o_ex.fe_logical >>
291 - (21 - bsbits)) << 21;
292 - size = 2 * 1024 * 1024;
293 - } else if (NRL_CHECK_SIZE(size, 8 * 1024 * 1024, max, 4 * 1024)) {
294 - start_off = ((loff_t)ac->ac_o_ex.fe_logical >>
295 - (22 - bsbits)) << 22;
296 - size = 4 * 1024 * 1024;
297 - } else if (NRL_CHECK_SIZE(ac->ac_o_ex.fe_len,
298 - (8<<20)>>bsbits, max, 8 * 1024)) {
299 - start_off = ((loff_t)ac->ac_o_ex.fe_logical >>
300 - (23 - bsbits)) << 23;
301 - size = 8 * 1024 * 1024;
303 - start_off = (loff_t)ac->ac_o_ex.fe_logical << bsbits;
304 - size = ac->ac_o_ex.fe_len << bsbits;
306 + __u64 tstart, tend;
307 + /* file is quite large, we now preallocate with
308 + * the biggest configured window with regart to
309 + * logical offset */
310 + wind = sbi->s_mb_prealloc_table[i - 1];
311 + tstart = ac->ac_o_ex.fe_logical;
312 + do_div(tstart, wind);
313 + start = tstart * wind;
314 + tend = ac->ac_o_ex.fe_logical + ac->ac_o_ex.fe_len - 1;
315 + do_div(tend, wind);
316 + tend = tend * wind + wind;
317 + size = tend - start;
319 - size = size >> bsbits;
320 - start = start_off >> bsbits;
323 /* don't cover already allocated blocks in selected range */
324 if (ar->pleft && start <= ar->lleft) {
325 @@ -3139,7 +3272,6 @@ ext4_mb_normalize_request(struct ext4_al
327 BUG_ON(start + size <= ac->ac_o_ex.fe_logical &&
328 start > ac->ac_o_ex.fe_logical);
329 - BUG_ON(size <= 0 || size > EXT4_BLOCKS_PER_GROUP(ac->ac_sb));
331 /* now prepare goal request */
333 @@ -4105,11 +4237,19 @@ static void ext4_mb_group_or_file(struct
335 /* don't use group allocation for large files */
336 size = max(size, isize);
337 - if (size > sbi->s_mb_stream_request) {
338 + if ((ac->ac_o_ex.fe_len >= sbi->s_mb_small_req) ||
339 + (size >= sbi->s_mb_large_req)) {
340 ac->ac_flags |= EXT4_MB_STREAM_ALLOC;
345 + * request is so large that we don't care about
346 + * streaming - it overweights any possible seek
348 + if (ac->ac_o_ex.fe_len >= sbi->s_mb_large_req)
351 BUG_ON(ac->ac_lg != NULL);
353 * locality group prealloc space are per cpu. The reason for having
354 Index: linux-3.12.39-47.1/fs/ext4/super.c
355 ===================================================================
356 --- linux-3.12.39-47.1.orig/fs/ext4/super.c
357 +++ linux-3.12.39-47.1/fs/ext4/super.c
358 @@ -2592,7 +2592,8 @@ EXT4_RW_ATTR_SBI_UI(mb_stats, s_mb_stats
359 EXT4_RW_ATTR_SBI_UI(mb_max_to_scan, s_mb_max_to_scan);
360 EXT4_RW_ATTR_SBI_UI(mb_min_to_scan, s_mb_min_to_scan);
361 EXT4_RW_ATTR_SBI_UI(mb_order2_req, s_mb_order2_reqs);
362 -EXT4_RW_ATTR_SBI_UI(mb_stream_req, s_mb_stream_request);
363 +EXT4_RW_ATTR_SBI_UI(mb_small_req, s_mb_small_req);
364 +EXT4_RW_ATTR_SBI_UI(mb_large_req, s_mb_large_req);
365 EXT4_RW_ATTR_SBI_UI(mb_group_prealloc, s_mb_group_prealloc);
366 EXT4_DEPRECATED_ATTR(max_writeback_mb_bump, 128);
367 EXT4_RW_ATTR_SBI_UI(extent_max_zeroout_kb, s_extent_max_zeroout_kb);
368 @@ -2609,7 +2610,8 @@ static struct attribute *ext4_attrs[] =
369 ATTR_LIST(mb_max_to_scan),
370 ATTR_LIST(mb_min_to_scan),
371 ATTR_LIST(mb_order2_req),
372 - ATTR_LIST(mb_stream_req),
373 + ATTR_LIST(mb_small_req),
374 + ATTR_LIST(mb_large_req),
375 ATTR_LIST(mb_group_prealloc),
376 ATTR_LIST(max_writeback_mb_bump),
377 ATTR_LIST(extent_max_zeroout_kb),
378 Index: linux-3.12.39-47.1/fs/ext4/inode.c
379 ===================================================================
380 --- linux-3.12.39-47.1.orig/fs/ext4/inode.c
381 +++ linux-3.12.39-47.1/fs/ext4/inode.c
382 @@ -2457,6 +2457,9 @@ static int ext4_writepages(struct addres
383 ext4_journal_stop(handle);
386 + if (wbc->nr_to_write < sbi->s_mb_small_req)
387 + wbc->nr_to_write = sbi->s_mb_small_req;
389 if (wbc->range_start == 0 && wbc->range_end == LLONG_MAX)