1 diff -u -r linux-stage.orig/fs/ext4/ext4.h linux-stage/fs/ext4/ext4.h
2 --- linux-stage.orig/fs/ext4/ext4.h 2012-12-31 11:12:36.000000000 -0500
3 +++ linux-stage/fs/ext4/ext4.h 2012-12-31 11:12:48.000000000 -0500
4 @@ -1170,11 +1170,14 @@
7 unsigned long s_stripe;
8 - unsigned int s_mb_stream_request;
9 + unsigned long s_mb_small_req;
10 + unsigned long s_mb_large_req;
11 unsigned int s_mb_max_to_scan;
12 unsigned int s_mb_min_to_scan;
13 unsigned int s_mb_stats;
14 unsigned int s_mb_order2_reqs;
15 + unsigned long *s_mb_prealloc_table;
16 + unsigned long s_mb_prealloc_table_size;
17 unsigned int s_mb_group_prealloc;
18 unsigned int s_max_writeback_mb_bump;
19 /* where last allocation was done - for stream allocation */
20 diff -u -r linux-stage.orig/fs/ext4/inode.c linux-stage/fs/ext4/inode.c
21 --- linux-stage.orig/fs/ext4/inode.c 2012-12-31 11:12:36.000000000 -0500
22 +++ linux-stage/fs/ext4/inode.c 2012-12-31 11:12:48.000000000 -0500
23 @@ -2937,6 +2937,11 @@
24 if (unlikely(sbi->s_mount_flags & EXT4_MF_FS_ABORTED))
27 + if (wbc->nr_to_write < sbi->s_mb_small_req) {
28 + nr_to_writebump = sbi->s_mb_small_req - wbc->nr_to_write;
29 + wbc->nr_to_write = sbi->s_mb_small_req;
32 if (wbc->range_start == 0 && wbc->range_end == LLONG_MAX)
35 diff -u -r linux-stage.orig/fs/ext4/mballoc.c linux-stage/fs/ext4/mballoc.c
36 --- linux-stage.orig/fs/ext4/mballoc.c 2012-12-31 11:12:36.000000000 -0500
37 +++ linux-stage/fs/ext4/mballoc.c 2012-12-31 11:20:51.000000000 -0500
38 @@ -1799,6 +1799,25 @@
42 +static void ext4_mb_prealloc_table_add(struct ext4_sb_info *sbi, int value)
46 + if (value > (sbi->s_blocks_per_group - 1 - 1 - sbi->s_itb_per_group))
49 + for (i = 0; i < sbi->s_mb_prealloc_table_size; i++) {
50 + if (sbi->s_mb_prealloc_table[i] == 0) {
51 + sbi->s_mb_prealloc_table[i] = value;
55 + /* they should add values in order */
56 + if (value <= sbi->s_mb_prealloc_table[i])
62 * The routine scans the group and measures all found extents.
63 * In order to optimize scanning, caller must pass number of
64 @@ -2172,6 +2191,80 @@
65 .show = ext4_mb_seq_groups_show,
68 +#define EXT4_MB_PREALLOC_TABLE "prealloc_table"
70 +static int ext4_mb_prealloc_table_proc_read(char *page, char **start, off_t off,
71 + int count, int *eof, void *data)
73 + struct ext4_sb_info *sbi = data;
81 + for (i = 0; i < sbi->s_mb_prealloc_table_size; i++)
82 + len += sprintf(page + len, "%ld ",
83 + sbi->s_mb_prealloc_table[i]);
84 + len += sprintf(page + len, "\n");
90 +static int ext4_mb_prealloc_table_proc_write(struct file *file,
91 + const char __user *buf,
92 + unsigned long cnt, void *data)
94 + struct ext4_sb_info *sbi = data;
95 + unsigned long value;
96 + unsigned long prev = 0;
100 + unsigned long *new_table;
104 + if (cnt >= sizeof(str))
106 + if (copy_from_user(str, buf, cnt))
112 + while (cur < end) {
113 + while ((cur < end) && (*cur == ' ')) cur++;
114 + value = simple_strtol(cur, &cur, 0);
123 + new_table = kmalloc(num * sizeof(*new_table), GFP_KERNEL);
124 + if (new_table == NULL)
126 + kfree(sbi->s_mb_prealloc_table);
127 + memset(new_table, 0, num * sizeof(*new_table));
128 + sbi->s_mb_prealloc_table = new_table;
129 + sbi->s_mb_prealloc_table_size = num;
132 + while (cur < end && i < num) {
133 + while ((cur < end) && (*cur == ' ')) cur++;
134 + value = simple_strtol(cur, &cur, 0);
135 + ext4_mb_prealloc_table_add(sbi, value);
142 static int ext4_mb_seq_groups_open(struct inode *inode, struct file *file)
144 struct super_block *sb = PDE(inode)->data;
145 @@ -2469,9 +2562,52 @@
146 sbi->s_mb_max_to_scan = MB_DEFAULT_MAX_TO_SCAN;
147 sbi->s_mb_min_to_scan = MB_DEFAULT_MIN_TO_SCAN;
148 sbi->s_mb_stats = MB_DEFAULT_STATS;
149 - sbi->s_mb_stream_request = MB_DEFAULT_STREAM_THRESHOLD;
150 sbi->s_mb_order2_reqs = MB_DEFAULT_ORDER2_REQS;
151 - sbi->s_mb_group_prealloc = MB_DEFAULT_GROUP_PREALLOC;
153 + if (sbi->s_stripe == 0) {
154 + sbi->s_mb_prealloc_table_size = 10;
155 + i = sbi->s_mb_prealloc_table_size * sizeof(unsigned long);
156 + sbi->s_mb_prealloc_table = kmalloc(i, GFP_NOFS);
157 + if (sbi->s_mb_prealloc_table == NULL) {
158 + kfree(sbi->s_mb_offsets);
159 + kfree(sbi->s_mb_maxs);
162 + memset(sbi->s_mb_prealloc_table, 0, i);
164 + ext4_mb_prealloc_table_add(sbi, 4);
165 + ext4_mb_prealloc_table_add(sbi, 8);
166 + ext4_mb_prealloc_table_add(sbi, 16);
167 + ext4_mb_prealloc_table_add(sbi, 32);
168 + ext4_mb_prealloc_table_add(sbi, 64);
169 + ext4_mb_prealloc_table_add(sbi, 128);
170 + ext4_mb_prealloc_table_add(sbi, 256);
171 + ext4_mb_prealloc_table_add(sbi, 512);
172 + ext4_mb_prealloc_table_add(sbi, 1024);
173 + ext4_mb_prealloc_table_add(sbi, 2048);
175 + sbi->s_mb_small_req = 256;
176 + sbi->s_mb_large_req = 1024;
177 + sbi->s_mb_group_prealloc = 512;
179 + sbi->s_mb_prealloc_table_size = 3;
180 + i = sbi->s_mb_prealloc_table_size * sizeof(unsigned long);
181 + sbi->s_mb_prealloc_table = kmalloc(i, GFP_NOFS);
182 + if (sbi->s_mb_prealloc_table == NULL) {
183 + kfree(sbi->s_mb_offsets);
184 + kfree(sbi->s_mb_maxs);
187 + memset(sbi->s_mb_prealloc_table, 0, i);
189 + ext4_mb_prealloc_table_add(sbi, sbi->s_stripe);
190 + ext4_mb_prealloc_table_add(sbi, sbi->s_stripe * 2);
191 + ext4_mb_prealloc_table_add(sbi, sbi->s_stripe * 4);
193 + sbi->s_mb_small_req = sbi->s_stripe;
194 + sbi->s_mb_large_req = sbi->s_stripe * 8;
195 + sbi->s_mb_group_prealloc = sbi->s_stripe * 4;
198 sbi->s_locality_groups = alloc_percpu(struct ext4_locality_group);
199 if (sbi->s_locality_groups == NULL) {
200 @@ -2487,12 +2623,22 @@
201 spin_lock_init(&lg->lg_prealloc_lock);
206 + struct proc_dir_entry *p;
207 proc_create_data("mb_groups", S_IRUGO, sbi->s_proc,
208 &ext4_mb_seq_groups_fops, sb);
209 + p = create_proc_entry(EXT4_MB_PREALLOC_TABLE, S_IFREG |
210 + S_IRUGO | S_IWUSR, sbi->s_proc);
213 + p->read_proc = ext4_mb_prealloc_table_proc_read;
214 + p->write_proc = ext4_mb_prealloc_table_proc_write;
220 + kfree(sbi->s_mb_prealloc_table);
221 kfree(sbi->s_mb_offsets);
222 kfree(sbi->s_mb_maxs);
224 @@ -2528,8 +2674,10 @@
225 struct ext4_sb_info *sbi = EXT4_SB(sb);
226 struct kmem_cache *cachep = get_groupinfo_cache(sb->s_blocksize_bits);
230 remove_proc_entry("mb_groups", sbi->s_proc);
231 + remove_proc_entry(EXT4_MB_PREALLOC_TABLE, sbi->s_proc);
234 if (sbi->s_group_info) {
235 for (i = 0; i < ngroups; i++) {
236 @@ -2859,11 +3007,12 @@
237 ext4_mb_normalize_request(struct ext4_allocation_context *ac,
238 struct ext4_allocation_request *ar)
241 + int bsbits, i, wind;
243 - loff_t size, orig_size, start_off;
244 + loff_t size, orig_size;
246 struct ext4_inode_info *ei = EXT4_I(ac->ac_inode);
247 + struct ext4_sb_info *sbi = EXT4_SB(ac->ac_sb);
248 struct ext4_prealloc_space *pa;
250 /* do normalize only data requests, metadata requests
251 @@ -2894,49 +3043,34 @@
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 @@ -3008,7 +3143,6 @@
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 @@ -3940,11 +4074,19 @@
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 diff -u -r linux-stage.orig/fs/ext4/super.c linux-stage/fs/ext4/super.c
355 --- linux-stage.orig/fs/ext4/super.c 2012-12-31 11:12:36.000000000 -0500
356 +++ linux-stage/fs/ext4/super.c 2012-12-31 11:12:48.000000000 -0500
357 @@ -2531,7 +2531,8 @@
358 EXT4_RW_ATTR_SBI_UI(mb_max_to_scan, s_mb_max_to_scan);
359 EXT4_RW_ATTR_SBI_UI(mb_min_to_scan, s_mb_min_to_scan);
360 EXT4_RW_ATTR_SBI_UI(mb_order2_req, s_mb_order2_reqs);
361 -EXT4_RW_ATTR_SBI_UI(mb_stream_req, s_mb_stream_request);
362 +EXT4_RW_ATTR_SBI_UI(mb_small_req, s_mb_small_req);
363 +EXT4_RW_ATTR_SBI_UI(mb_large_req, s_mb_large_req);
364 EXT4_RW_ATTR_SBI_UI(mb_group_prealloc, s_mb_group_prealloc);
365 EXT4_RW_ATTR_SBI_UI(max_writeback_mb_bump, s_max_writeback_mb_bump);
367 @@ -2548,7 +2549,8 @@
368 ATTR_LIST(mb_max_to_scan),
369 ATTR_LIST(mb_min_to_scan),
370 ATTR_LIST(mb_order2_req),
371 - ATTR_LIST(mb_stream_req),
372 + ATTR_LIST(mb_small_req),
373 + ATTR_LIST(mb_large_req),
374 ATTR_LIST(mb_group_prealloc),
375 ATTR_LIST(max_writeback_mb_bump),