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 @@ -1173,11 +1173,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/inode.c
22 ===================================================================
23 --- linux-stage.orig/fs/ext4/inode.c
24 +++ linux-stage/fs/ext4/inode.c
25 @@ -2948,6 +2948,11 @@ static int ext4_da_writepages(struct add
26 if (unlikely(sbi->s_mount_flags & EXT4_MF_FS_ABORTED))
29 + if (wbc->nr_to_write < sbi->s_mb_small_req) {
30 + nr_to_writebump = sbi->s_mb_small_req - wbc->nr_to_write;
31 + wbc->nr_to_write = sbi->s_mb_small_req;
34 if (wbc->range_start == 0 && wbc->range_end == LLONG_MAX)
37 Index: linux-stage/fs/ext4/mballoc.c
38 ===================================================================
39 --- linux-stage.orig/fs/ext4/mballoc.c
40 +++ linux-stage/fs/ext4/mballoc.c
41 @@ -1802,6 +1802,26 @@ void ext4_mb_simple_scan_group(struct ex
45 +static int ext4_mb_prealloc_table_add(struct ext4_sb_info *sbi, int value)
49 + if (value > (sbi->s_blocks_per_group - 1 - 1 - sbi->s_itb_per_group))
52 + for (i = 0; i < sbi->s_mb_prealloc_table_size; i++) {
53 + if (sbi->s_mb_prealloc_table[i] == 0) {
54 + sbi->s_mb_prealloc_table[i] = value;
58 + /* they should add values in order */
59 + if (value <= sbi->s_mb_prealloc_table[i])
66 * The routine scans the group and measures all found extents.
67 * In order to optimize scanning, caller must pass number of
68 @@ -2179,6 +2199,82 @@ static const struct seq_operations ext4_
69 .show = ext4_mb_seq_groups_show,
72 +#define EXT4_MB_PREALLOC_TABLE "prealloc_table"
74 +static int ext4_mb_prealloc_table_proc_read(char *page, char **start, off_t off,
75 + int count, int *eof, void *data)
77 + struct ext4_sb_info *sbi = data;
85 + for (i = 0; i < sbi->s_mb_prealloc_table_size; i++)
86 + len += sprintf(page + len, "%ld ",
87 + sbi->s_mb_prealloc_table[i]);
88 + len += sprintf(page + len, "\n");
94 +static int ext4_mb_prealloc_table_proc_write(struct file *file,
95 + const char __user *buf,
96 + unsigned long cnt, void *data)
98 + struct ext4_sb_info *sbi = data;
99 + unsigned long value;
100 + unsigned long prev = 0;
104 + unsigned long *new_table;
108 + if (cnt >= sizeof(str))
110 + if (copy_from_user(str, buf, cnt))
116 + while (cur < end) {
117 + while ((cur < end) && (*cur == ' ')) cur++;
118 + value = simple_strtol(cur, &cur, 0);
127 + new_table = kmalloc(num * sizeof(*new_table), GFP_KERNEL);
128 + if (new_table == NULL)
130 + kfree(sbi->s_mb_prealloc_table);
131 + memset(new_table, 0, num * sizeof(*new_table));
132 + sbi->s_mb_prealloc_table = new_table;
133 + sbi->s_mb_prealloc_table_size = num;
136 + while (cur < end && i < num) {
137 + while ((cur < end) && (*cur == ' ')) cur++;
138 + value = simple_strtol(cur, &cur, 0);
139 + if (ext4_mb_prealloc_table_add(sbi, value) == 0)
143 + sbi->s_mb_prealloc_table_size = i;
148 static int ext4_mb_seq_groups_open(struct inode *inode, struct file *file)
150 struct super_block *sb = PDE(inode)->data;
151 @@ -2425,7 +2521,7 @@ static int ext4_groupinfo_create_slab(si
152 int ext4_mb_init(struct super_block *sb, int needs_recovery)
154 struct ext4_sb_info *sbi = EXT4_SB(sb);
156 + unsigned i, j, k, l;
160 @@ -2476,9 +2572,51 @@ int ext4_mb_init(struct super_block *sb,
161 sbi->s_mb_max_to_scan = MB_DEFAULT_MAX_TO_SCAN;
162 sbi->s_mb_min_to_scan = MB_DEFAULT_MIN_TO_SCAN;
163 sbi->s_mb_stats = MB_DEFAULT_STATS;
164 - sbi->s_mb_stream_request = MB_DEFAULT_STREAM_THRESHOLD;
165 sbi->s_mb_order2_reqs = MB_DEFAULT_ORDER2_REQS;
166 - sbi->s_mb_group_prealloc = MB_DEFAULT_GROUP_PREALLOC;
168 + if (sbi->s_stripe == 0) {
169 + sbi->s_mb_prealloc_table_size = 10;
170 + i = sbi->s_mb_prealloc_table_size * sizeof(unsigned long);
171 + sbi->s_mb_prealloc_table = kmalloc(i, GFP_NOFS);
172 + if (sbi->s_mb_prealloc_table == NULL) {
173 + kfree(sbi->s_mb_offsets);
174 + kfree(sbi->s_mb_maxs);
177 + memset(sbi->s_mb_prealloc_table, 0, i);
179 + for (k = 0, l = 4; k <= 9; ++k, l *= 2) {
180 + if (ext4_mb_prealloc_table_add(sbi, l) < 0) {
181 + sbi->s_mb_prealloc_table_size = k;
186 + sbi->s_mb_small_req = 256;
187 + sbi->s_mb_large_req = 1024;
188 + sbi->s_mb_group_prealloc = 512;
190 + sbi->s_mb_prealloc_table_size = 3;
191 + i = sbi->s_mb_prealloc_table_size * sizeof(unsigned long);
192 + sbi->s_mb_prealloc_table = kmalloc(i, GFP_NOFS);
193 + if (sbi->s_mb_prealloc_table == NULL) {
194 + kfree(sbi->s_mb_offsets);
195 + kfree(sbi->s_mb_maxs);
198 + memset(sbi->s_mb_prealloc_table, 0, i);
200 + for (k = 0, l = sbi->s_stripe; k <= 2; ++k, l *= 2) {
201 + if (ext4_mb_prealloc_table_add(sbi, l) < 0) {
202 + sbi->s_mb_prealloc_table_size = k;
207 + sbi->s_mb_small_req = sbi->s_stripe;
208 + sbi->s_mb_large_req = sbi->s_stripe * 8;
209 + sbi->s_mb_group_prealloc = sbi->s_stripe * 4;
212 sbi->s_locality_groups = alloc_percpu(struct ext4_locality_group);
213 if (sbi->s_locality_groups == NULL) {
214 @@ -2494,12 +2632,22 @@ int ext4_mb_init(struct super_block *sb,
215 spin_lock_init(&lg->lg_prealloc_lock);
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;
234 + kfree(sbi->s_mb_prealloc_table);
235 kfree(sbi->s_mb_offsets);
236 kfree(sbi->s_mb_maxs);
238 @@ -2533,8 +2681,10 @@ int ext4_mb_release(struct super_block *
239 struct ext4_sb_info *sbi = EXT4_SB(sb);
240 struct kmem_cache *cachep = get_groupinfo_cache(sb->s_blocksize_bits);
244 remove_proc_entry("mb_groups", sbi->s_proc);
245 + remove_proc_entry(EXT4_MB_PREALLOC_TABLE, sbi->s_proc);
248 if (sbi->s_group_info) {
249 for (i = 0; i < ngroups; i++) {
250 @@ -2870,11 +3020,12 @@ static noinline_for_stack void
251 ext4_mb_normalize_request(struct ext4_allocation_context *ac,
252 struct ext4_allocation_request *ar)
255 + int bsbits, i, wind;
257 - loff_t size, orig_size, start_off;
258 + loff_t size, orig_size;
260 struct ext4_inode_info *ei = EXT4_I(ac->ac_inode);
261 + struct ext4_sb_info *sbi = EXT4_SB(ac->ac_sb);
262 struct ext4_prealloc_space *pa;
264 /* do normalize only data requests, metadata requests
265 @@ -2905,49 +3056,34 @@ ext4_mb_normalize_request(struct ext4_al
266 if (size < i_size_read(ac->ac_inode))
267 size = i_size_read(ac->ac_inode);
269 + size = (size + ac->ac_sb->s_blocksize - 1) >> bsbits;
271 - /* max size of free chunks */
275 -#define NRL_CHECK_SIZE(req, size, max, chunk_size) \
276 - (req <= (size) || max <= (chunk_size))
277 + /* let's choose preallocation window depending on file size */
278 + for (i = 0; i < sbi->s_mb_prealloc_table_size; i++) {
279 + if (size <= sbi->s_mb_prealloc_table[i]) {
280 + wind = sbi->s_mb_prealloc_table[i];
286 - /* first, try to predict filesize */
287 - /* XXX: should this table be tunable? */
289 - if (size <= 16 * 1024) {
291 - } else if (size <= 32 * 1024) {
293 - } else if (size <= 64 * 1024) {
295 - } else if (size <= 128 * 1024) {
297 - } else if (size <= 256 * 1024) {
299 - } else if (size <= 512 * 1024) {
301 - } else if (size <= 1024 * 1024) {
302 - size = 1024 * 1024;
303 - } else if (NRL_CHECK_SIZE(size, 4 * 1024 * 1024, max, 2 * 1024)) {
304 - start_off = ((loff_t)ac->ac_o_ex.fe_logical >>
305 - (21 - bsbits)) << 21;
306 - size = 2 * 1024 * 1024;
307 - } else if (NRL_CHECK_SIZE(size, 8 * 1024 * 1024, max, 4 * 1024)) {
308 - start_off = ((loff_t)ac->ac_o_ex.fe_logical >>
309 - (22 - bsbits)) << 22;
310 - size = 4 * 1024 * 1024;
311 - } else if (NRL_CHECK_SIZE(ac->ac_o_ex.fe_len,
312 - (8<<20)>>bsbits, max, 8 * 1024)) {
313 - start_off = ((loff_t)ac->ac_o_ex.fe_logical >>
314 - (23 - bsbits)) << 23;
315 - size = 8 * 1024 * 1024;
317 - start_off = (loff_t)ac->ac_o_ex.fe_logical << bsbits;
318 - size = ac->ac_o_ex.fe_len << bsbits;
320 + __u64 tstart, tend;
321 + /* file is quite large, we now preallocate with
322 + * the biggest configured window with regart to
323 + * logical offset */
324 + wind = sbi->s_mb_prealloc_table[i - 1];
325 + tstart = ac->ac_o_ex.fe_logical;
326 + do_div(tstart, wind);
327 + start = tstart * wind;
328 + tend = ac->ac_o_ex.fe_logical + ac->ac_o_ex.fe_len - 1;
329 + do_div(tend, wind);
330 + tend = tend * wind + wind;
331 + size = tend - start;
333 - size = size >> bsbits;
334 - start = start_off >> bsbits;
337 /* don't cover already allocated blocks in selected range */
338 if (ar->pleft && start <= ar->lleft) {
339 @@ -3020,7 +3156,6 @@ ext4_mb_normalize_request(struct ext4_al
341 BUG_ON(start + size <= ac->ac_o_ex.fe_logical &&
342 start > ac->ac_o_ex.fe_logical);
343 - BUG_ON(size <= 0 || size > EXT4_BLOCKS_PER_GROUP(ac->ac_sb));
345 /* now prepare goal request */
347 @@ -3956,11 +4091,19 @@ static void ext4_mb_group_or_file(struct
349 /* don't use group allocation for large files */
350 size = max(size, isize);
351 - if (size > sbi->s_mb_stream_request) {
352 + if ((ac->ac_o_ex.fe_len >= sbi->s_mb_small_req) ||
353 + (size >= sbi->s_mb_large_req)) {
354 ac->ac_flags |= EXT4_MB_STREAM_ALLOC;
359 + * request is so large that we don't care about
360 + * streaming - it overweights any possible seek
362 + if (ac->ac_o_ex.fe_len >= sbi->s_mb_large_req)
365 BUG_ON(ac->ac_lg != NULL);
367 * locality group prealloc space are per cpu. The reason for having
368 Index: linux-stage/fs/ext4/super.c
369 ===================================================================
370 --- linux-stage.orig/fs/ext4/super.c
371 +++ linux-stage/fs/ext4/super.c
372 @@ -2595,7 +2595,8 @@ EXT4_RW_ATTR_SBI_UI(mb_stats, s_mb_stats
373 EXT4_RW_ATTR_SBI_UI(mb_max_to_scan, s_mb_max_to_scan);
374 EXT4_RW_ATTR_SBI_UI(mb_min_to_scan, s_mb_min_to_scan);
375 EXT4_RW_ATTR_SBI_UI(mb_order2_req, s_mb_order2_reqs);
376 -EXT4_RW_ATTR_SBI_UI(mb_stream_req, s_mb_stream_request);
377 +EXT4_RW_ATTR_SBI_UI(mb_small_req, s_mb_small_req);
378 +EXT4_RW_ATTR_SBI_UI(mb_large_req, s_mb_large_req);
379 EXT4_RW_ATTR_SBI_UI(mb_group_prealloc, s_mb_group_prealloc);
380 EXT4_RW_ATTR_SBI_UI(max_writeback_mb_bump, s_max_writeback_mb_bump);
382 @@ -2611,7 +2612,8 @@ static struct attribute *ext4_attrs[] =
383 ATTR_LIST(mb_max_to_scan),
384 ATTR_LIST(mb_min_to_scan),
385 ATTR_LIST(mb_order2_req),
386 - ATTR_LIST(mb_stream_req),
387 + ATTR_LIST(mb_small_req),
388 + ATTR_LIST(mb_large_req),
389 ATTR_LIST(mb_group_prealloc),
390 ATTR_LIST(max_writeback_mb_bump),