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 @@ -1242,6 +1242,8 @@ struct ext4_super_block {
6 #define EXT4_MF_MNTDIR_SAMPLED 0x0001
7 #define EXT4_MF_FS_ABORTED 0x0002 /* Fatal error detected */
9 +#define EXT4_MAX_PREALLOC_TABLE 64
12 * fourth extended-fs super-block data in memory
14 @@ -1331,11 +1333,13 @@ struct ext4_sb_info {
17 unsigned long s_stripe;
18 - unsigned int s_mb_stream_request;
19 + unsigned long s_mb_small_req;
20 + unsigned long s_mb_large_req;
21 unsigned int s_mb_max_to_scan;
22 unsigned int s_mb_min_to_scan;
23 unsigned int s_mb_stats;
24 unsigned int s_mb_order2_reqs;
25 + unsigned long *s_mb_prealloc_table;
26 unsigned int s_mb_group_prealloc;
27 unsigned int s_max_dir_size_kb;
28 /* where last allocation was done - for stream allocation */
29 Index: linux-stage/fs/ext4/mballoc.c
30 ===================================================================
31 --- linux-stage.orig/fs/ext4/mballoc.c
32 +++ linux-stage/fs/ext4/mballoc.c
33 @@ -2303,6 +2303,102 @@ static const struct seq_operations ext4_
34 .show = ext4_mb_seq_groups_show,
37 +#define EXT4_MB_PREALLOC_TABLE "prealloc_table"
39 +static int ext4_mb_check_and_update_prealloc(struct ext4_sb_info *sbi,
40 + char *str, size_t cnt,
43 + unsigned long value;
44 + unsigned long prev = 0;
53 + while ((cur < end) && (*cur == ' ')) cur++;
54 + value = simple_strtol(cur, &next, 0);
62 + if (value > (sbi->s_blocks_per_group - 1 - 1 - sbi->s_itb_per_group))
65 + /* they should add values in order */
70 + sbi->s_mb_prealloc_table[num] = value;
76 + if (num > EXT4_MAX_PREALLOC_TABLE - 1)
80 + sbi->s_mb_prealloc_table[num] = 0;
85 +static ssize_t ext4_mb_prealloc_table_proc_write(struct file *file,
86 + const char __user *buf,
87 + size_t cnt, loff_t *pos)
89 + struct ext4_sb_info *sbi = EXT4_SB(PDE_DATA(file_inode(file)));
93 + if (cnt >= sizeof(str))
95 + if (copy_from_user(str, buf, cnt))
98 + rc = ext4_mb_check_and_update_prealloc(sbi, str, cnt, 0);
102 + rc = ext4_mb_check_and_update_prealloc(sbi, str, cnt, 1);
103 + return rc ? rc : cnt;
106 +static int mb_prealloc_table_seq_show(struct seq_file *m, void *v)
108 + struct ext4_sb_info *sbi = EXT4_SB(m->private);
111 + for (i = 0; i < EXT4_MAX_PREALLOC_TABLE &&
112 + sbi->s_mb_prealloc_table[i] != 0; i++)
113 + seq_printf(m, "%ld ", sbi->s_mb_prealloc_table[i]);
114 + seq_printf(m, "\n");
119 +static int mb_prealloc_table_seq_open(struct inode *inode, struct file *file)
121 + return single_open(file, mb_prealloc_table_seq_show, PDE_DATA(inode));
124 +static const struct file_operations ext4_mb_prealloc_seq_fops = {
125 + .owner = THIS_MODULE,
126 + .open = mb_prealloc_table_seq_open,
128 + .llseek = seq_lseek,
129 + .release = single_release,
130 + .write = ext4_mb_prealloc_table_proc_write,
133 static int ext4_mb_seq_groups_open(struct inode *inode, struct file *file)
135 struct super_block *sb = PDE_DATA(inode);
136 @@ -2552,7 +2648,7 @@ static int ext4_groupinfo_create_slab(si
137 int ext4_mb_init(struct super_block *sb)
139 struct ext4_sb_info *sbi = EXT4_SB(sb);
141 + unsigned i, j, k, l;
142 unsigned offset, offset_incr;
145 @@ -2599,7 +2695,6 @@ int ext4_mb_init(struct super_block *sb)
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;
152 * The default group preallocation is 512, which for 4k block
153 @@ -2623,9 +2718,29 @@ int ext4_mb_init(struct super_block *sb)
154 * RAID stripe size so that preallocations don't fragment
157 - if (sbi->s_stripe > 1) {
158 - sbi->s_mb_group_prealloc = roundup(
159 - sbi->s_mb_group_prealloc, sbi->s_stripe);
161 + /* Allocate table once */
162 + sbi->s_mb_prealloc_table = kzalloc(
163 + EXT4_MAX_PREALLOC_TABLE * sizeof(unsigned long), GFP_NOFS);
164 + if (sbi->s_mb_prealloc_table == NULL) {
169 + if (sbi->s_stripe == 0) {
170 + for (k = 0, l = 4; k <= 9; ++k, l *= 2)
171 + sbi->s_mb_prealloc_table[k] = l;
173 + sbi->s_mb_small_req = 256;
174 + sbi->s_mb_large_req = 1024;
175 + sbi->s_mb_group_prealloc = 512;
177 + for (k = 0, l = sbi->s_stripe; k <= 2; ++k, l *= 2)
178 + sbi->s_mb_prealloc_table[k] = l;
180 + sbi->s_mb_small_req = sbi->s_stripe;
181 + sbi->s_mb_large_req = sbi->s_stripe * 8;
182 + sbi->s_mb_group_prealloc = sbi->s_stripe * 4;
185 sbi->s_locality_groups = alloc_percpu(struct ext4_locality_group);
186 @@ -2647,9 +2762,13 @@ int ext4_mb_init(struct super_block *sb)
188 goto out_free_locality_groups;
192 proc_create_data("mb_groups", S_IRUGO, sbi->s_proc,
193 &ext4_mb_seq_groups_fops, sb);
194 + proc_create_data(EXT4_MB_PREALLOC_TABLE, S_IFREG | S_IRUGO |
195 + S_IWUSR, sbi->s_proc,
196 + &ext4_mb_prealloc_seq_fops, sb);
201 @@ -2657,6 +2776,7 @@ out_free_locality_groups:
202 free_percpu(sbi->s_locality_groups);
203 sbi->s_locality_groups = NULL;
205 + kfree(sbi->s_mb_prealloc_table);
206 kfree(sbi->s_mb_offsets);
207 sbi->s_mb_offsets = NULL;
208 kfree(sbi->s_mb_maxs);
209 @@ -2691,8 +2811,10 @@ int ext4_mb_release(struct super_block *
210 struct ext4_sb_info *sbi = EXT4_SB(sb);
211 struct kmem_cache *cachep = get_groupinfo_cache(sb->s_blocksize_bits);
215 remove_proc_entry("mb_groups", sbi->s_proc);
216 + remove_proc_entry(EXT4_MB_PREALLOC_TABLE, sbi->s_proc);
219 if (sbi->s_group_info) {
220 for (i = 0; i < ngroups; i++) {
221 @@ -2877,7 +2999,6 @@ ext4_mb_mark_diskspace_used(struct ext4_
224 BUG_ON(ac->ac_status != AC_STATUS_FOUND);
225 - BUG_ON(ac->ac_b_ex.fe_len <= 0);
229 @@ -3004,13 +3125,14 @@ ext4_mb_normalize_request(struct ext4_al
230 struct ext4_allocation_request *ar)
232 struct ext4_sb_info *sbi = EXT4_SB(ac->ac_sb);
234 + int bsbits, i, wind;
236 - loff_t size, start_off;
238 loff_t orig_size __maybe_unused;
240 struct ext4_inode_info *ei = EXT4_I(ac->ac_inode);
241 struct ext4_prealloc_space *pa;
242 + unsigned long value, last_non_zero;
244 /* do normalize only data requests, metadata requests
245 do not need preallocation */
246 @@ -3039,51 +3161,46 @@ ext4_mb_normalize_request(struct ext4_al
247 size = size << bsbits;
248 if (size < i_size_read(ac->ac_inode))
249 size = i_size_read(ac->ac_inode);
251 + size = (size + ac->ac_sb->s_blocksize - 1) >> bsbits;
253 - /* max size of free chunks */
256 + value = last_non_zero = 0;
258 -#define NRL_CHECK_SIZE(req, size, max, chunk_size) \
259 - (req <= (size) || max <= (chunk_size))
260 + /* let's choose preallocation window depending on file size */
261 + for (i = 0; i < EXT4_MAX_PREALLOC_TABLE; i++) {
262 + value = sbi->s_mb_prealloc_table[i];
266 + last_non_zero = value;
268 - /* first, try to predict filesize */
269 - /* XXX: should this table be tunable? */
271 - if (size <= 16 * 1024) {
273 - } else if (size <= 32 * 1024) {
275 - } else if (size <= 64 * 1024) {
277 - } else if (size <= 128 * 1024) {
279 - } else if (size <= 256 * 1024) {
281 - } else if (size <= 512 * 1024) {
283 - } else if (size <= 1024 * 1024) {
284 - size = 1024 * 1024;
285 - } else if (NRL_CHECK_SIZE(size, 4 * 1024 * 1024, max, 2 * 1024)) {
286 - start_off = ((loff_t)ac->ac_o_ex.fe_logical >>
287 - (21 - bsbits)) << 21;
288 - size = 2 * 1024 * 1024;
289 - } else if (NRL_CHECK_SIZE(size, 8 * 1024 * 1024, max, 4 * 1024)) {
290 - start_off = ((loff_t)ac->ac_o_ex.fe_logical >>
291 - (22 - bsbits)) << 22;
292 - size = 4 * 1024 * 1024;
293 - } else if (NRL_CHECK_SIZE(ac->ac_o_ex.fe_len,
294 - (8<<20)>>bsbits, max, 8 * 1024)) {
295 - start_off = ((loff_t)ac->ac_o_ex.fe_logical >>
296 - (23 - bsbits)) << 23;
297 - size = 8 * 1024 * 1024;
298 + if (size <= value) {
305 + if (last_non_zero != 0) {
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 = last_non_zero;
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;
320 - start_off = (loff_t) ac->ac_o_ex.fe_logical << bsbits;
321 - size = (loff_t) EXT4_C2B(EXT4_SB(ac->ac_sb),
322 - ac->ac_o_ex.fe_len) << bsbits;
325 - size = size >> bsbits;
326 - start = start_off >> bsbits;
331 /* don't cover already allocated blocks in selected range */
332 if (ar->pleft && start <= ar->lleft) {
333 @@ -3165,7 +3282,6 @@ ext4_mb_normalize_request(struct ext4_al
334 (unsigned long) ac->ac_o_ex.fe_logical);
337 - BUG_ON(size <= 0 || size > EXT4_BLOCKS_PER_GROUP(ac->ac_sb));
339 /* now prepare goal request */
341 @@ -4130,11 +4246,19 @@ static void ext4_mb_group_or_file(struct
343 /* don't use group allocation for large files */
344 size = max(size, isize);
345 - if (size > sbi->s_mb_stream_request) {
346 + if ((ac->ac_o_ex.fe_len >= sbi->s_mb_small_req) ||
347 + (size >= sbi->s_mb_large_req)) {
348 ac->ac_flags |= EXT4_MB_STREAM_ALLOC;
353 + * request is so large that we don't care about
354 + * streaming - it overweights any possible seek
356 + if (ac->ac_o_ex.fe_len >= sbi->s_mb_large_req)
359 BUG_ON(ac->ac_lg != NULL);
361 * locality group prealloc space are per cpu. The reason for having
362 Index: linux-stage/fs/ext4/super.c
363 ===================================================================
364 --- linux-stage.orig/fs/ext4/super.c
365 +++ linux-stage/fs/ext4/super.c
366 @@ -2708,7 +2708,8 @@ EXT4_RW_ATTR_SBI_UI(mb_stats, s_mb_stats
367 EXT4_RW_ATTR_SBI_UI(mb_max_to_scan, s_mb_max_to_scan);
368 EXT4_RW_ATTR_SBI_UI(mb_min_to_scan, s_mb_min_to_scan);
369 EXT4_RW_ATTR_SBI_UI(mb_order2_req, s_mb_order2_reqs);
370 -EXT4_RW_ATTR_SBI_UI(mb_stream_req, s_mb_stream_request);
371 +EXT4_RW_ATTR_SBI_UI(mb_small_req, s_mb_small_req);
372 +EXT4_RW_ATTR_SBI_UI(mb_large_req, s_mb_large_req);
373 EXT4_RW_ATTR_SBI_UI(mb_group_prealloc, s_mb_group_prealloc);
374 EXT4_DEPRECATED_ATTR(max_writeback_mb_bump, 128);
375 EXT4_RW_ATTR_SBI_UI(extent_max_zeroout_kb, s_extent_max_zeroout_kb);
376 @@ -2734,7 +2735,8 @@ static struct attribute *ext4_attrs[] =
377 ATTR_LIST(mb_max_to_scan),
378 ATTR_LIST(mb_min_to_scan),
379 ATTR_LIST(mb_order2_req),
380 - ATTR_LIST(mb_stream_req),
381 + ATTR_LIST(mb_small_req),
382 + ATTR_LIST(mb_large_req),
383 ATTR_LIST(mb_group_prealloc),
384 ATTR_LIST(max_writeback_mb_bump),
385 ATTR_LIST(extent_max_zeroout_kb),
386 Index: linux-stage/fs/ext4/inode.c
387 ===================================================================
388 --- linux-stage.orig/fs/ext4/inode.c
389 +++ linux-stage/fs/ext4/inode.c
390 @@ -2457,6 +2457,9 @@ static int ext4_writepages(struct addres
391 ext4_journal_stop(handle);
394 + if (wbc->nr_to_write < sbi->s_mb_small_req)
395 + wbc->nr_to_write = sbi->s_mb_small_req;
397 if (wbc->range_start == 0 && wbc->range_end == LLONG_MAX)