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,25 @@ void ext4_mb_complex_scan_group(struct e
26 ext4_mb_check_limits(ac, e4b, 1);
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 * This is a special case for storages like raid5
50 * we try to find stripe-aligned chunks for stripe-size requests
51 @@ -2155,6 +2174,80 @@ static const struct seq_operations ext4_
52 .show = ext4_mb_seq_groups_show,
55 +#define EXT4_MB_PREALLOC_TABLE "prealloc_table"
57 +static int ext4_mb_prealloc_table_proc_read(char *page, char **start, off_t off,
58 + int count, int *eof, void *data)
60 + struct ext4_sb_info *sbi = data;
68 + for (i = 0; i < sbi->s_mb_prealloc_table_size; i++)
69 + len += sprintf(page + len, "%ld ",
70 + sbi->s_mb_prealloc_table[i]);
71 + len += sprintf(page + len, "\n");
77 +static int ext4_mb_prealloc_table_proc_write(struct file *file,
78 + const char __user *buf,
79 + unsigned long cnt, void *data)
81 + struct ext4_sb_info *sbi = data;
82 + unsigned long value;
83 + unsigned long prev = 0;
87 + unsigned long *new_table;
91 + if (cnt >= sizeof(str))
93 + if (copy_from_user(str, buf, cnt))
100 + while ((cur < end) && (*cur == ' ')) cur++;
101 + value = simple_strtol(cur, &cur, 0);
110 + new_table = kmalloc(num * sizeof(*new_table), GFP_KERNEL);
111 + if (new_table == NULL)
113 + kfree(sbi->s_mb_prealloc_table);
114 + memset(new_table, 0, num * sizeof(*new_table));
115 + sbi->s_mb_prealloc_table = new_table;
116 + sbi->s_mb_prealloc_table_size = num;
119 + while (cur < end && i < num) {
120 + while ((cur < end) && (*cur == ' ')) cur++;
121 + value = simple_strtol(cur, &cur, 0);
122 + ext4_mb_prealloc_table_add(sbi, value);
129 static int ext4_mb_seq_groups_open(struct inode *inode, struct file *file)
131 struct super_block *sb = PDE(inode)->data;
132 @@ -2380,26 +2473,62 @@ int ext4_mb_init(struct super_block *sb,
134 } while (i <= sb->s_blocksize_bits + 1);
136 - /* init file for buddy data */
137 - ret = ext4_mb_init_backend(sb);
139 - kfree(sbi->s_mb_offsets);
140 - kfree(sbi->s_mb_maxs);
144 spin_lock_init(&sbi->s_md_lock);
145 spin_lock_init(&sbi->s_bal_lock);
147 sbi->s_mb_max_to_scan = MB_DEFAULT_MAX_TO_SCAN;
148 sbi->s_mb_min_to_scan = MB_DEFAULT_MIN_TO_SCAN;
149 sbi->s_mb_stats = MB_DEFAULT_STATS;
150 - sbi->s_mb_stream_request = MB_DEFAULT_STREAM_THRESHOLD;
151 sbi->s_mb_order2_reqs = MB_DEFAULT_ORDER2_REQS;
152 - sbi->s_mb_group_prealloc = MB_DEFAULT_GROUP_PREALLOC;
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) {
159 + kfree(sbi->s_mb_offsets);
160 + kfree(sbi->s_mb_maxs);
163 + memset(sbi->s_mb_prealloc_table, 0, i);
165 + ext4_mb_prealloc_table_add(sbi, 4);
166 + ext4_mb_prealloc_table_add(sbi, 8);
167 + ext4_mb_prealloc_table_add(sbi, 16);
168 + ext4_mb_prealloc_table_add(sbi, 32);
169 + ext4_mb_prealloc_table_add(sbi, 64);
170 + ext4_mb_prealloc_table_add(sbi, 128);
171 + ext4_mb_prealloc_table_add(sbi, 256);
172 + ext4_mb_prealloc_table_add(sbi, 512);
173 + ext4_mb_prealloc_table_add(sbi, 1024);
174 + ext4_mb_prealloc_table_add(sbi, 2048);
176 + sbi->s_mb_small_req = 256;
177 + sbi->s_mb_large_req = 1024;
178 + sbi->s_mb_group_prealloc = 512;
180 + sbi->s_mb_prealloc_table_size = 3;
181 + i = sbi->s_mb_prealloc_table_size * sizeof(unsigned long);
182 + sbi->s_mb_prealloc_table = kmalloc(i, GFP_NOFS);
183 + if (sbi->s_mb_prealloc_table == NULL) {
184 + kfree(sbi->s_mb_offsets);
185 + kfree(sbi->s_mb_maxs);
188 + memset(sbi->s_mb_prealloc_table, 0, i);
190 + ext4_mb_prealloc_table_add(sbi, sbi->s_stripe);
191 + ext4_mb_prealloc_table_add(sbi, sbi->s_stripe * 2);
192 + ext4_mb_prealloc_table_add(sbi, sbi->s_stripe * 4);
194 + sbi->s_mb_small_req = sbi->s_stripe;
195 + sbi->s_mb_large_req = sbi->s_stripe * 8;
196 + sbi->s_mb_group_prealloc = sbi->s_stripe * 4;
199 sbi->s_locality_groups = alloc_percpu(struct ext4_locality_group);
200 if (sbi->s_locality_groups == NULL) {
201 + kfree(sbi->s_mb_prealloc_table);
202 kfree(sbi->s_mb_offsets);
203 kfree(sbi->s_mb_maxs);
205 @@ -2413,9 +2542,27 @@ int ext4_mb_init(struct super_block *sb,
206 spin_lock_init(&lg->lg_prealloc_lock);
210 + /* init file for buddy data */
211 + ret = ext4_mb_init_backend(sb);
213 + kfree(sbi->s_mb_prealloc_table);
214 + kfree(sbi->s_mb_offsets);
215 + kfree(sbi->s_mb_maxs);
220 + struct proc_dir_entry *p;
221 proc_create_data("mb_groups", S_IRUGO, sbi->s_proc,
222 &ext4_mb_seq_groups_fops, sb);
223 + p = create_proc_entry(EXT4_MB_PREALLOC_TABLE, S_IFREG |
224 + S_IRUGO | S_IWUSR, sbi->s_proc);
227 + p->read_proc = ext4_mb_prealloc_table_proc_read;
228 + p->write_proc = ext4_mb_prealloc_table_proc_write;
233 sbi->s_journal->j_commit_callback = release_blocks_on_commit;
234 @@ -2448,8 +2595,10 @@ int ext4_mb_release(struct super_block *
235 struct ext4_group_info *grinfo;
236 struct ext4_sb_info *sbi = EXT4_SB(sb);
240 remove_proc_entry("mb_groups", sbi->s_proc);
241 + remove_proc_entry(EXT4_MB_PREALLOC_TABLE, sbi->s_proc);
244 if (sbi->s_group_info) {
245 for (i = 0; i < ngroups; i++) {
246 @@ -2469,6 +2618,7 @@ int ext4_mb_release(struct super_block *
247 kfree(sbi->s_group_info[i]);
248 kfree(sbi->s_group_info);
250 + kfree(sbi->s_mb_prealloc_table);
251 kfree(sbi->s_mb_offsets);
252 kfree(sbi->s_mb_maxs);
253 if (sbi->s_buddy_cache)
254 @@ -2798,11 +2948,12 @@ static noinline_for_stack void
255 ext4_mb_normalize_request(struct ext4_allocation_context *ac,
256 struct ext4_allocation_request *ar)
259 + int bsbits, i, wind;
261 - loff_t size, orig_size, start_off;
262 + loff_t size, orig_size;
263 ext4_lblk_t start, orig_start;
264 struct ext4_inode_info *ei = EXT4_I(ac->ac_inode);
265 + struct ext4_sb_info *sbi = EXT4_SB(ac->ac_sb);
266 struct ext4_prealloc_space *pa;
268 /* do normalize only data requests, metadata requests
269 @@ -2832,49 +2983,35 @@ ext4_mb_normalize_request(struct ext4_al
270 size = size << bsbits;
271 if (size < i_size_read(ac->ac_inode))
272 size = i_size_read(ac->ac_inode);
273 + size = (size + ac->ac_sb->s_blocksize - 1) >> bsbits;
275 - /* max size of free chunks */
279 -#define NRL_CHECK_SIZE(req, size, max, chunk_size) \
280 - (req <= (size) || max <= (chunk_size))
281 + /* let's choose preallocation window depending on file size */
282 + for (i = 0; i < sbi->s_mb_prealloc_table_size; i++) {
283 + if (size <= sbi->s_mb_prealloc_table[i]) {
284 + wind = sbi->s_mb_prealloc_table[i];
290 - /* first, try to predict filesize */
291 - /* XXX: should this table be tunable? */
293 - if (size <= 16 * 1024) {
295 - } else if (size <= 32 * 1024) {
297 - } else if (size <= 64 * 1024) {
299 - } else if (size <= 128 * 1024) {
301 - } else if (size <= 256 * 1024) {
303 - } else if (size <= 512 * 1024) {
305 - } else if (size <= 1024 * 1024) {
306 - size = 1024 * 1024;
307 - } else if (NRL_CHECK_SIZE(size, 4 * 1024 * 1024, max, 2 * 1024)) {
308 - start_off = ((loff_t)ac->ac_o_ex.fe_logical >>
309 - (21 - bsbits)) << 21;
310 - size = 2 * 1024 * 1024;
311 - } else if (NRL_CHECK_SIZE(size, 8 * 1024 * 1024, max, 4 * 1024)) {
312 - start_off = ((loff_t)ac->ac_o_ex.fe_logical >>
313 - (22 - bsbits)) << 22;
314 - size = 4 * 1024 * 1024;
315 - } else if (NRL_CHECK_SIZE(ac->ac_o_ex.fe_len,
316 - (8<<20)>>bsbits, max, 8 * 1024)) {
317 - start_off = ((loff_t)ac->ac_o_ex.fe_logical >>
318 - (23 - bsbits)) << 23;
319 - size = 8 * 1024 * 1024;
321 - start_off = (loff_t)ac->ac_o_ex.fe_logical << bsbits;
322 - size = ac->ac_o_ex.fe_len << bsbits;
324 + __u64 tstart, tend;
325 + /* file is quite large, we now preallocate with
326 + * the biggest configured window with regart to
327 + * logical offset */
328 + wind = sbi->s_mb_prealloc_table[i - 1];
329 + tstart = ac->ac_o_ex.fe_logical;
330 + do_div(tstart, wind);
331 + start = tstart * wind;
332 + tend = ac->ac_o_ex.fe_logical + ac->ac_o_ex.fe_len - 1;
333 + do_div(tend, wind);
334 + tend = tend * wind + wind;
335 + size = tend - start;
337 - orig_size = size = size >> bsbits;
338 - orig_start = start = start_off >> bsbits;
340 + orig_start = start;
342 /* don't cover already allocated blocks in selected range */
343 if (ar->pleft && start <= ar->lleft) {
344 @@ -2946,7 +3083,6 @@ ext4_mb_normalize_request(struct ext4_al
346 BUG_ON(start + size <= ac->ac_o_ex.fe_logical &&
347 start > ac->ac_o_ex.fe_logical);
348 - BUG_ON(size <= 0 || size > EXT4_BLOCKS_PER_GROUP(ac->ac_sb));
350 /* now prepare goal request */
352 @@ -3930,11 +4066,19 @@ static void ext4_mb_group_or_file(struct
354 /* don't use group allocation for large files */
355 size = max(size, isize);
356 - if (size > sbi->s_mb_stream_request) {
357 + if ((ac->ac_o_ex.fe_len >= sbi->s_mb_small_req) ||
358 + (size >= sbi->s_mb_large_req)) {
359 ac->ac_flags |= EXT4_MB_STREAM_ALLOC;
364 + * request is so large that we don't care about
365 + * streaming - it overweights any possible seek
367 + if (ac->ac_o_ex.fe_len >= sbi->s_mb_large_req)
370 BUG_ON(ac->ac_lg != NULL);
372 * locality group prealloc space are per cpu. The reason for having
373 Index: linux-stage/fs/ext4/super.c
374 ===================================================================
375 --- linux-stage.orig/fs/ext4/super.c
376 +++ linux-stage/fs/ext4/super.c
377 @@ -2357,7 +2357,8 @@ EXT4_RW_ATTR_SBI_UI(mb_stats, s_mb_stats
378 EXT4_RW_ATTR_SBI_UI(mb_max_to_scan, s_mb_max_to_scan);
379 EXT4_RW_ATTR_SBI_UI(mb_min_to_scan, s_mb_min_to_scan);
380 EXT4_RW_ATTR_SBI_UI(mb_order2_req, s_mb_order2_reqs);
381 -EXT4_RW_ATTR_SBI_UI(mb_stream_req, s_mb_stream_request);
382 +EXT4_RW_ATTR_SBI_UI(mb_small_req, s_mb_small_req);
383 +EXT4_RW_ATTR_SBI_UI(mb_large_req, s_mb_large_req);
384 EXT4_RW_ATTR_SBI_UI(mb_group_prealloc, s_mb_group_prealloc);
385 EXT4_RW_ATTR_SBI_UI(max_writeback_mb_bump, s_max_writeback_mb_bump);
387 @@ -2372,7 +2373,8 @@ static struct attribute *ext4_attrs[] =
388 ATTR_LIST(mb_max_to_scan),
389 ATTR_LIST(mb_min_to_scan),
390 ATTR_LIST(mb_order2_req),
391 - ATTR_LIST(mb_stream_req),
392 + ATTR_LIST(mb_small_req),
393 + ATTR_LIST(mb_large_req),
394 ATTR_LIST(mb_group_prealloc),
395 ATTR_LIST(max_writeback_mb_bump),
397 Index: linux-stage/fs/ext4/inode.c
398 ===================================================================
399 --- linux-stage.orig/fs/ext4/inode.c
400 +++ linux-stage/fs/ext4/inode.c
401 @@ -3070,6 +3070,11 @@ static int ext4_da_writepages(struct add
402 if (unlikely(sbi->s_mount_flags & EXT4_MF_FS_ABORTED))
405 + if (wbc->nr_to_write < sbi->s_mb_small_req) {
406 + nr_to_writebump = sbi->s_mb_small_req - wbc->nr_to_write;
407 + wbc->nr_to_write = sbi->s_mb_small_req;
410 if (wbc->range_start == 0 && wbc->range_end == LLONG_MAX)