1 Index: linux-stage/fs/ext4/ext4.h
2 ===================================================================
3 --- linux-stage.orig/fs/ext4/ext4.h
4 +++ linux-stage/fs/ext4/ext4.h
5 @@ -1136,11 +1136,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 /* where last allocation was done - for stream allocation */
21 Index: linux-stage/fs/ext4/mballoc.c
22 ===================================================================
23 --- linux-stage.orig/fs/ext4/mballoc.c
24 +++ linux-stage/fs/ext4/mballoc.c
25 @@ -1838,6 +1838,26 @@ void ext4_mb_complex_scan_group(struct e
26 ext4_mb_check_limits(ac, e4b, 1);
29 +static int 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])
50 * This is a special case for storages like raid5
51 * we try to find stripe-aligned chunks for stripe-size requests
52 @@ -2155,6 +2175,82 @@ static const struct seq_operations ext4_
53 .show = ext4_mb_seq_groups_show,
56 +#define EXT4_MB_PREALLOC_TABLE "prealloc_table"
58 +static int ext4_mb_prealloc_table_proc_read(char *page, char **start, off_t off,
59 + int count, int *eof, void *data)
61 + struct ext4_sb_info *sbi = data;
69 + for (i = 0; i < sbi->s_mb_prealloc_table_size; i++)
70 + len += sprintf(page + len, "%ld ",
71 + sbi->s_mb_prealloc_table[i]);
72 + len += sprintf(page + len, "\n");
78 +static int ext4_mb_prealloc_table_proc_write(struct file *file,
79 + const char __user *buf,
80 + unsigned long cnt, void *data)
82 + struct ext4_sb_info *sbi = data;
83 + unsigned long value;
84 + unsigned long prev = 0;
88 + unsigned long *new_table;
92 + if (cnt >= sizeof(str))
94 + if (copy_from_user(str, buf, cnt))
100 + while (cur < end) {
101 + while ((cur < end) && (*cur == ' ')) cur++;
102 + value = simple_strtol(cur, &cur, 0);
111 + new_table = kmalloc(num * sizeof(*new_table), GFP_KERNEL);
112 + if (new_table == NULL)
114 + kfree(sbi->s_mb_prealloc_table);
115 + memset(new_table, 0, num * sizeof(*new_table));
116 + sbi->s_mb_prealloc_table = new_table;
117 + sbi->s_mb_prealloc_table_size = num;
120 + while (cur < end && i < num) {
121 + while ((cur < end) && (*cur == ' ')) cur++;
122 + value = simple_strtol(cur, &cur, 0);
123 + if (ext4_mb_prealloc_table_add(sbi, value) == 0)
127 + sbi->s_mb_prealloc_table_size = i;
132 static int ext4_mb_seq_groups_open(struct inode *inode, struct file *file)
134 struct super_block *sb = PDE(inode)->data;
135 @@ -2346,7 +2442,7 @@ err_freesgi:
136 int ext4_mb_init(struct super_block *sb, int needs_recovery)
138 struct ext4_sb_info *sbi = EXT4_SB(sb);
140 + unsigned i, j, k, l;
144 @@ -2380,26 +2476,61 @@ int ext4_mb_init(struct super_block *sb,
146 } while (i <= sb->s_blocksize_bits + 1);
148 - /* init file for buddy data */
149 - ret = ext4_mb_init_backend(sb);
151 - kfree(sbi->s_mb_offsets);
152 - kfree(sbi->s_mb_maxs);
156 spin_lock_init(&sbi->s_md_lock);
157 spin_lock_init(&sbi->s_bal_lock);
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;
164 - sbi->s_mb_group_prealloc = MB_DEFAULT_GROUP_PREALLOC;
166 + if (sbi->s_stripe == 0) {
167 + sbi->s_mb_prealloc_table_size = 10;
168 + i = sbi->s_mb_prealloc_table_size * sizeof(unsigned long);
169 + sbi->s_mb_prealloc_table = kmalloc(i, GFP_NOFS);
170 + if (sbi->s_mb_prealloc_table == NULL) {
171 + kfree(sbi->s_mb_offsets);
172 + kfree(sbi->s_mb_maxs);
175 + memset(sbi->s_mb_prealloc_table, 0, i);
177 + for (k = 0, l = 4; k <= 9; ++k, l *= 2) {
178 + if (ext4_mb_prealloc_table_add(sbi, l) < 0) {
179 + sbi->s_mb_prealloc_table_size = k;
184 + sbi->s_mb_small_req = 256;
185 + sbi->s_mb_large_req = 1024;
186 + sbi->s_mb_group_prealloc = 512;
188 + sbi->s_mb_prealloc_table_size = 3;
189 + i = sbi->s_mb_prealloc_table_size * sizeof(unsigned long);
190 + sbi->s_mb_prealloc_table = kmalloc(i, GFP_NOFS);
191 + if (sbi->s_mb_prealloc_table == NULL) {
192 + kfree(sbi->s_mb_offsets);
193 + kfree(sbi->s_mb_maxs);
196 + memset(sbi->s_mb_prealloc_table, 0, i);
198 + for (k = 0, l = sbi->s_stripe; k <= 2; ++k, l *= 2) {
199 + if (ext4_mb_prealloc_table_add(sbi, l) < 0) {
200 + sbi->s_mb_prealloc_table_size = k;
205 + sbi->s_mb_small_req = sbi->s_stripe;
206 + sbi->s_mb_large_req = sbi->s_stripe * 8;
207 + sbi->s_mb_group_prealloc = sbi->s_stripe * 4;
210 sbi->s_locality_groups = alloc_percpu(struct ext4_locality_group);
211 if (sbi->s_locality_groups == NULL) {
212 + kfree(sbi->s_mb_prealloc_table);
213 kfree(sbi->s_mb_offsets);
214 kfree(sbi->s_mb_maxs);
216 @@ -2413,9 +2544,27 @@ int ext4_mb_init(struct super_block *sb,
217 spin_lock_init(&lg->lg_prealloc_lock);
221 + /* init file for buddy data */
222 + ret = ext4_mb_init_backend(sb);
224 + kfree(sbi->s_mb_prealloc_table);
225 + kfree(sbi->s_mb_offsets);
226 + kfree(sbi->s_mb_maxs);
231 + struct proc_dir_entry *p;
232 proc_create_data("mb_groups", S_IRUGO, sbi->s_proc,
233 &ext4_mb_seq_groups_fops, sb);
234 + p = create_proc_entry(EXT4_MB_PREALLOC_TABLE, S_IFREG |
235 + S_IRUGO | S_IWUSR, sbi->s_proc);
238 + p->read_proc = ext4_mb_prealloc_table_proc_read;
239 + p->write_proc = ext4_mb_prealloc_table_proc_write;
244 sbi->s_journal->j_commit_callback = release_blocks_on_commit;
245 @@ -2448,8 +2597,10 @@ int ext4_mb_release(struct super_block *
246 struct ext4_group_info *grinfo;
247 struct ext4_sb_info *sbi = EXT4_SB(sb);
251 remove_proc_entry("mb_groups", sbi->s_proc);
252 + remove_proc_entry(EXT4_MB_PREALLOC_TABLE, sbi->s_proc);
255 if (sbi->s_group_info) {
256 for (i = 0; i < ngroups; i++) {
257 @@ -2469,6 +2620,7 @@ int ext4_mb_release(struct super_block *
258 kfree(sbi->s_group_info[i]);
259 ext4_kvfree(sbi->s_group_info);
261 + kfree(sbi->s_mb_prealloc_table);
262 kfree(sbi->s_mb_offsets);
263 kfree(sbi->s_mb_maxs);
264 if (sbi->s_buddy_cache)
265 @@ -2798,11 +2950,12 @@ static noinline_for_stack void
266 ext4_mb_normalize_request(struct ext4_allocation_context *ac,
267 struct ext4_allocation_request *ar)
270 + int bsbits, i, wind;
272 - loff_t size, orig_size, start_off;
273 + loff_t size, orig_size;
274 ext4_lblk_t start, orig_start;
275 struct ext4_inode_info *ei = EXT4_I(ac->ac_inode);
276 + struct ext4_sb_info *sbi = EXT4_SB(ac->ac_sb);
277 struct ext4_prealloc_space *pa;
279 /* do normalize only data requests, metadata requests
280 @@ -2832,49 +2985,35 @@ ext4_mb_normalize_request(struct ext4_al
281 size = size << bsbits;
282 if (size < i_size_read(ac->ac_inode))
283 size = i_size_read(ac->ac_inode);
284 + size = (size + ac->ac_sb->s_blocksize - 1) >> bsbits;
286 - /* max size of free chunks */
290 -#define NRL_CHECK_SIZE(req, size, max, chunk_size) \
291 - (req <= (size) || max <= (chunk_size))
292 + /* let's choose preallocation window depending on file size */
293 + for (i = 0; i < sbi->s_mb_prealloc_table_size; i++) {
294 + if (size <= sbi->s_mb_prealloc_table[i]) {
295 + wind = sbi->s_mb_prealloc_table[i];
301 - /* first, try to predict filesize */
302 - /* XXX: should this table be tunable? */
304 - if (size <= 16 * 1024) {
306 - } else if (size <= 32 * 1024) {
308 - } else if (size <= 64 * 1024) {
310 - } else if (size <= 128 * 1024) {
312 - } else if (size <= 256 * 1024) {
314 - } else if (size <= 512 * 1024) {
316 - } else if (size <= 1024 * 1024) {
317 - size = 1024 * 1024;
318 - } else if (NRL_CHECK_SIZE(size, 4 * 1024 * 1024, max, 2 * 1024)) {
319 - start_off = ((loff_t)ac->ac_o_ex.fe_logical >>
320 - (21 - bsbits)) << 21;
321 - size = 2 * 1024 * 1024;
322 - } else if (NRL_CHECK_SIZE(size, 8 * 1024 * 1024, max, 4 * 1024)) {
323 - start_off = ((loff_t)ac->ac_o_ex.fe_logical >>
324 - (22 - bsbits)) << 22;
325 - size = 4 * 1024 * 1024;
326 - } else if (NRL_CHECK_SIZE(ac->ac_o_ex.fe_len,
327 - (8<<20)>>bsbits, max, 8 * 1024)) {
328 - start_off = ((loff_t)ac->ac_o_ex.fe_logical >>
329 - (23 - bsbits)) << 23;
330 - size = 8 * 1024 * 1024;
332 - start_off = (loff_t)ac->ac_o_ex.fe_logical << bsbits;
333 - size = ac->ac_o_ex.fe_len << bsbits;
335 + __u64 tstart, tend;
336 + /* file is quite large, we now preallocate with
337 + * the biggest configured window with regart to
338 + * logical offset */
339 + wind = sbi->s_mb_prealloc_table[i - 1];
340 + tstart = ac->ac_o_ex.fe_logical;
341 + do_div(tstart, wind);
342 + start = tstart * wind;
343 + tend = ac->ac_o_ex.fe_logical + ac->ac_o_ex.fe_len - 1;
344 + do_div(tend, wind);
345 + tend = tend * wind + wind;
346 + size = tend - start;
348 - orig_size = size = size >> bsbits;
349 - orig_start = start = start_off >> bsbits;
351 + orig_start = start;
353 /* don't cover already allocated blocks in selected range */
354 if (ar->pleft && start <= ar->lleft) {
355 @@ -2946,7 +3085,6 @@ ext4_mb_normalize_request(struct ext4_al
357 BUG_ON(start + size <= ac->ac_o_ex.fe_logical &&
358 start > ac->ac_o_ex.fe_logical);
359 - BUG_ON(size <= 0 || size > EXT4_BLOCKS_PER_GROUP(ac->ac_sb));
361 /* now prepare goal request */
363 @@ -3930,11 +4068,19 @@ static void ext4_mb_group_or_file(struct
365 /* don't use group allocation for large files */
366 size = max(size, isize);
367 - if (size > sbi->s_mb_stream_request) {
368 + if ((ac->ac_o_ex.fe_len >= sbi->s_mb_small_req) ||
369 + (size >= sbi->s_mb_large_req)) {
370 ac->ac_flags |= EXT4_MB_STREAM_ALLOC;
375 + * request is so large that we don't care about
376 + * streaming - it overweights any possible seek
378 + if (ac->ac_o_ex.fe_len >= sbi->s_mb_large_req)
381 BUG_ON(ac->ac_lg != NULL);
383 * locality group prealloc space are per cpu. The reason for having
384 Index: linux-stage/fs/ext4/super.c
385 ===================================================================
386 --- linux-stage.orig/fs/ext4/super.c
387 +++ linux-stage/fs/ext4/super.c
388 @@ -2377,7 +2377,8 @@ EXT4_RW_ATTR_SBI_UI(mb_stats, s_mb_stats
389 EXT4_RW_ATTR_SBI_UI(mb_max_to_scan, s_mb_max_to_scan);
390 EXT4_RW_ATTR_SBI_UI(mb_min_to_scan, s_mb_min_to_scan);
391 EXT4_RW_ATTR_SBI_UI(mb_order2_req, s_mb_order2_reqs);
392 -EXT4_RW_ATTR_SBI_UI(mb_stream_req, s_mb_stream_request);
393 +EXT4_RW_ATTR_SBI_UI(mb_small_req, s_mb_small_req);
394 +EXT4_RW_ATTR_SBI_UI(mb_large_req, s_mb_large_req);
395 EXT4_RW_ATTR_SBI_UI(mb_group_prealloc, s_mb_group_prealloc);
396 EXT4_RW_ATTR_SBI_UI(max_writeback_mb_bump, s_max_writeback_mb_bump);
398 @@ -2391,7 +2392,8 @@ static struct attribute *ext4_attrs[] =
399 ATTR_LIST(mb_max_to_scan),
400 ATTR_LIST(mb_min_to_scan),
401 ATTR_LIST(mb_order2_req),
402 - ATTR_LIST(mb_stream_req),
403 + ATTR_LIST(mb_small_req),
404 + ATTR_LIST(mb_large_req),
405 ATTR_LIST(mb_group_prealloc),
406 ATTR_LIST(max_writeback_mb_bump),
408 Index: linux-stage/fs/ext4/inode.c
409 ===================================================================
410 --- linux-stage.orig/fs/ext4/inode.c
411 +++ linux-stage/fs/ext4/inode.c
412 @@ -3070,6 +3070,11 @@ static int ext4_da_writepages(struct add
413 if (unlikely(sbi->s_mount_flags & EXT4_MF_FS_ABORTED))
416 + if (wbc->nr_to_write < sbi->s_mb_small_req) {
417 + nr_to_writebump = sbi->s_mb_small_req - wbc->nr_to_write;
418 + wbc->nr_to_write = sbi->s_mb_small_req;
421 if (wbc->range_start == 0 && wbc->range_end == LLONG_MAX)