1 Index: linux-2.6.18-128.1.6/fs/ext4/ext4_sb.h
2 ===================================================================
3 --- linux-2.6.18-128.1.6.orig/fs/ext4/ext4_sb.h 2009-05-28 17:16:51.000000000 +0530
4 +++ linux-2.6.18-128.1.6/fs/ext4/ext4_sb.h 2009-05-28 17:16:52.000000000 +0530
8 unsigned long s_stripe;
9 - unsigned long s_mb_stream_request;
10 + unsigned long s_mb_small_req;
11 + unsigned long s_mb_large_req;
12 unsigned long s_mb_max_to_scan;
13 unsigned long s_mb_min_to_scan;
14 unsigned long s_mb_stats;
15 unsigned long s_mb_order2_reqs;
16 + unsigned long *s_mb_prealloc_table;
17 + unsigned long s_mb_prealloc_table_size;
18 unsigned long s_mb_group_prealloc;
19 /* where last allocation was done - for stream allocation */
20 unsigned long s_mb_last_group;
21 Index: linux-2.6.18-128.1.6/fs/ext4/mballoc.c
22 ===================================================================
23 --- linux-2.6.18-128.1.6.orig/fs/ext4/mballoc.c 2009-05-28 17:16:51.000000000 +0530
24 +++ linux-2.6.18-128.1.6/fs/ext4/mballoc.c 2009-05-28 17:19:57.000000000 +0530
29 - if (size < sbi->s_mb_stream_request &&
30 + if ((ac->ac_g_ex.fe_len < sbi->s_mb_large_req) &&
31 (ac->ac_flags & EXT4_MB_HINT_DATA)) {
32 /* TBD: may be hot point */
33 spin_lock(&sbi->s_md_lock);
34 @@ -2484,6 +2484,26 @@
38 +static void ext4_mb_prealloc_table_add(struct ext4_sb_info *sbi, int value)
42 + if (value > (sbi->s_blocks_per_group - 1 - 1 - sbi->s_itb_per_group))
45 + for (i = 0; i < sbi->s_mb_prealloc_table_size; i++) {
46 + if (sbi->s_mb_prealloc_table[i] == 0) {
47 + sbi->s_mb_prealloc_table[i] = value;
51 + /* they should add values in order */
52 + if (value <= sbi->s_mb_prealloc_table[i])
58 int ext4_mb_init(struct super_block *sb, int needs_recovery)
60 struct ext4_sb_info *sbi = EXT4_SB(sb);
61 @@ -2542,15 +2562,59 @@
62 sbi->s_mb_max_to_scan = MB_DEFAULT_MAX_TO_SCAN;
63 sbi->s_mb_min_to_scan = MB_DEFAULT_MIN_TO_SCAN;
64 sbi->s_mb_stats = MB_DEFAULT_STATS;
65 - sbi->s_mb_stream_request = MB_DEFAULT_STREAM_THRESHOLD;
66 sbi->s_mb_order2_reqs = MB_DEFAULT_ORDER2_REQS;
67 sbi->s_mb_history_filter = EXT4_MB_HISTORY_DEFAULT;
68 - sbi->s_mb_group_prealloc = MB_DEFAULT_GROUP_PREALLOC;
70 + if (sbi->s_stripe == 0) {
71 + sbi->s_mb_prealloc_table_size = 8;
72 + i = sbi->s_mb_prealloc_table_size * sizeof(unsigned long);
73 + sbi->s_mb_prealloc_table = kmalloc(i, GFP_NOFS);
74 + if (sbi->s_mb_prealloc_table == NULL) {
75 + clear_opt(sbi->s_mount_opt, MBALLOC);
76 + kfree(sbi->s_mb_offsets);
77 + kfree(sbi->s_mb_maxs);
80 + memset(sbi->s_mb_prealloc_table, 0, i);
82 + ext4_mb_prealloc_table_add(sbi, 4);
83 + ext4_mb_prealloc_table_add(sbi, 8);
84 + ext4_mb_prealloc_table_add(sbi, 16);
85 + ext4_mb_prealloc_table_add(sbi, 32);
86 + ext4_mb_prealloc_table_add(sbi, 64);
87 + ext4_mb_prealloc_table_add(sbi, 128);
88 + ext4_mb_prealloc_table_add(sbi, 256);
89 + ext4_mb_prealloc_table_add(sbi, 512);
91 + sbi->s_mb_small_req = 256;
92 + sbi->s_mb_large_req = 1024;
93 + sbi->s_mb_group_prealloc = 512;
95 + sbi->s_mb_prealloc_table_size = 3;
96 + i = sbi->s_mb_prealloc_table_size * sizeof(unsigned long);
97 + sbi->s_mb_prealloc_table = kmalloc(i, GFP_NOFS);
98 + if (sbi->s_mb_prealloc_table == NULL) {
99 + clear_opt(sbi->s_mount_opt, MBALLOC);
100 + kfree(sbi->s_mb_offsets);
101 + kfree(sbi->s_mb_maxs);
104 + memset(sbi->s_mb_prealloc_table, 0, i);
106 + ext4_mb_prealloc_table_add(sbi, sbi->s_stripe);
107 + ext4_mb_prealloc_table_add(sbi, sbi->s_stripe * 2);
108 + ext4_mb_prealloc_table_add(sbi, sbi->s_stripe * 4);
110 + sbi->s_mb_small_req = sbi->s_stripe;
111 + sbi->s_mb_large_req = sbi->s_stripe * 8;
112 + sbi->s_mb_group_prealloc = sbi->s_stripe * 4;
115 i = sizeof(struct ext4_locality_group) * num_possible_cpus();
116 sbi->s_locality_groups = kmalloc(i, GFP_KERNEL);
117 if (sbi->s_locality_groups == NULL) {
118 clear_opt(sbi->s_mount_opt, MBALLOC);
119 + kfree(sbi->s_mb_prealloc_table);
120 kfree(sbi->s_mb_offsets);
121 kfree(sbi->s_mb_maxs);
123 @@ -2725,10 +2789,82 @@
124 #define EXT4_MB_MAX_TO_SCAN_NAME "max_to_scan"
125 #define EXT4_MB_MIN_TO_SCAN_NAME "min_to_scan"
126 #define EXT4_MB_ORDER2_REQ "order2_req"
127 -#define EXT4_MB_STREAM_REQ "stream_req"
128 +#define EXT4_MB_SMALL_REQ "small_req"
129 +#define EXT4_MB_LARGE_REQ "large_req"
130 +#define EXT4_MB_PREALLOC_TABLE "prealloc_table"
131 #define EXT4_MB_GROUP_PREALLOC "group_prealloc"
133 +static int ext4_mb_prealloc_table_proc_read(char *page, char **start, off_t off,
134 + int count, int *eof, void *data)
136 + struct ext4_sb_info *sbi = data;
144 + for (i = 0; i < sbi->s_mb_prealloc_table_size; i++)
145 + len += sprintf(page + len, "%ld ",
146 + sbi->s_mb_prealloc_table[i]);
147 + len += sprintf(page + len, "\n");
153 +static int ext4_mb_prealloc_table_proc_write(struct file *file,
154 + const char __user *buf,
155 + unsigned long cnt, void *data)
157 + struct ext4_sb_info *sbi = data;
158 + unsigned long value;
159 + unsigned long prev = 0;
163 + unsigned long *new_table;
167 + if (cnt >= sizeof(str))
169 + if (copy_from_user(str, buf, cnt))
175 + while (cur < end) {
176 + while ((cur < end) && (*cur == ' ')) cur++;
177 + value = simple_strtol(cur, &cur, 0);
186 + new_table = kmalloc(num * sizeof(*new_table), GFP_KERNEL);
187 + if (new_table == NULL)
189 + kfree(sbi->s_mb_prealloc_table);
190 + memset(new_table, 0, num * sizeof(*new_table));
191 + sbi->s_mb_prealloc_table = new_table;
192 + sbi->s_mb_prealloc_table_size = num;
195 + while (cur < end && i < num) {
196 + while ((cur < end) && (*cur == ' ')) cur++;
197 + value = simple_strtol(cur, &cur, 0);
198 + ext4_mb_prealloc_table_add(sbi, value);
205 #define MB_PROC_FOPS(name) \
206 static int ext4_mb_##name##_proc_show(struct seq_file *m, void *v) \
207 @@ -2774,7 +2910,8 @@
208 MB_PROC_FOPS(max_to_scan);
209 MB_PROC_FOPS(min_to_scan);
210 MB_PROC_FOPS(order2_reqs);
211 -MB_PROC_FOPS(stream_request);
212 +MB_PROC_FOPS(small_req);
213 +MB_PROC_FOPS(large_req);
214 MB_PROC_FOPS(group_prealloc);
216 #define MB_PROC_HANDLER(name, var) \
217 @@ -2795,6 +2932,7 @@
218 mode_t mode = S_IFREG | S_IRUGO | S_IWUSR;
219 struct ext4_sb_info *sbi = EXT4_SB(sb);
220 struct proc_dir_entry *proc;
221 + struct proc_dir_entry *proc_entry;
224 if (proc_root_ext4 == NULL) {
225 @@ -2808,15 +2946,29 @@
226 MB_PROC_HANDLER(EXT4_MB_MAX_TO_SCAN_NAME, max_to_scan);
227 MB_PROC_HANDLER(EXT4_MB_MIN_TO_SCAN_NAME, min_to_scan);
228 MB_PROC_HANDLER(EXT4_MB_ORDER2_REQ, order2_reqs);
229 - MB_PROC_HANDLER(EXT4_MB_STREAM_REQ, stream_request);
230 + MB_PROC_HANDLER(EXT4_MB_SMALL_REQ, small_req);
231 + MB_PROC_HANDLER(EXT4_MB_LARGE_REQ, large_req);
232 MB_PROC_HANDLER(EXT4_MB_GROUP_PREALLOC, group_prealloc);
234 + proc_entry = create_proc_entry(EXT4_MB_PREALLOC_TABLE, S_IFREG |
235 + S_IRUGO | S_IWUSR, sbi->s_mb_proc);
236 + if (proc_entry == NULL) {
237 + printk(KERN_ERR "EXT4-fs: unable to create %s\n",
238 + EXT4_MB_PREALLOC_TABLE);
241 + proc_entry->data = sbi;
242 + proc_entry->read_proc = ext4_mb_prealloc_table_proc_read;
243 + proc_entry->write_proc = ext4_mb_prealloc_table_proc_write;
248 printk(KERN_ERR "EXT4-fs: Unable to create %s\n", devname);
249 remove_proc_entry(EXT4_MB_GROUP_PREALLOC, sbi->s_mb_proc);
250 - remove_proc_entry(EXT4_MB_STREAM_REQ, sbi->s_mb_proc);
251 + remove_proc_entry(EXT4_MB_PREALLOC_TABLE, sbi->s_mb_proc);
252 + remove_proc_entry(EXT4_MB_LARGE_REQ, sbi->s_mb_proc);
253 + remove_proc_entry(EXT4_MB_SMALL_REQ, sbi->s_mb_proc);
254 remove_proc_entry(EXT4_MB_ORDER2_REQ, sbi->s_mb_proc);
255 remove_proc_entry(EXT4_MB_MIN_TO_SCAN_NAME, sbi->s_mb_proc);
256 remove_proc_entry(EXT4_MB_MAX_TO_SCAN_NAME, sbi->s_mb_proc);
257 @@ -2838,7 +2990,9 @@
259 bdevname(sb->s_bdev, devname);
260 remove_proc_entry(EXT4_MB_GROUP_PREALLOC, sbi->s_mb_proc);
261 - remove_proc_entry(EXT4_MB_STREAM_REQ, sbi->s_mb_proc);
262 + remove_proc_entry(EXT4_MB_PREALLOC_TABLE, sbi->s_mb_proc);
263 + remove_proc_entry(EXT4_MB_LARGE_REQ, sbi->s_mb_proc);
264 + remove_proc_entry(EXT4_MB_SMALL_REQ, sbi->s_mb_proc);
265 remove_proc_entry(EXT4_MB_ORDER2_REQ, sbi->s_mb_proc);
266 remove_proc_entry(EXT4_MB_MIN_TO_SCAN_NAME, sbi->s_mb_proc);
267 remove_proc_entry(EXT4_MB_MAX_TO_SCAN_NAME, sbi->s_mb_proc);
268 @@ -3032,11 +3186,12 @@
269 ext4_mb_normalize_request(struct ext4_allocation_context *ac,
270 struct ext4_allocation_request *ar)
273 + int bsbits, i, wind;
275 - loff_t size, orig_size, start_off;
276 + loff_t size, orig_size;
277 ext4_lblk_t start, orig_start;
278 struct ext4_inode_info *ei = EXT4_I(ac->ac_inode);
279 + struct ext4_sb_info *sbi = EXT4_SB(ac->ac_sb);
280 struct ext4_prealloc_space *pa;
282 /* do normalize only data requests, metadata requests
283 @@ -3066,49 +3221,35 @@
284 size = size << bsbits;
285 if (size < i_size_read(ac->ac_inode))
286 size = i_size_read(ac->ac_inode);
287 + size = (size + ac->ac_sb->s_blocksize - 1) >> bsbits;
289 - /* max size of free chunks */
293 -#define NRL_CHECK_SIZE(req, size, max, chunk_size) \
294 - (req <= (size) || max <= (chunk_size))
295 + /* let's choose preallocation window depending on file size */
296 + for (i = 0; i < sbi->s_mb_prealloc_table_size; i++) {
297 + if (size <= sbi->s_mb_prealloc_table[i]) {
298 + wind = sbi->s_mb_prealloc_table[i];
304 - /* first, try to predict filesize */
305 - /* XXX: should this table be tunable? */
307 - if (size <= 16 * 1024) {
309 - } else if (size <= 32 * 1024) {
311 - } else if (size <= 64 * 1024) {
313 - } else if (size <= 128 * 1024) {
315 - } else if (size <= 256 * 1024) {
317 - } else if (size <= 512 * 1024) {
319 - } else if (size <= 1024 * 1024) {
320 - size = 1024 * 1024;
321 - } else if (NRL_CHECK_SIZE(size, 4 * 1024 * 1024, max, 2 * 1024)) {
322 - start_off = ((loff_t)ac->ac_o_ex.fe_logical >>
323 - (21 - bsbits)) << 21;
324 - size = 2 * 1024 * 1024;
325 - } else if (NRL_CHECK_SIZE(size, 8 * 1024 * 1024, max, 4 * 1024)) {
326 - start_off = ((loff_t)ac->ac_o_ex.fe_logical >>
327 - (22 - bsbits)) << 22;
328 - size = 4 * 1024 * 1024;
329 - } else if (NRL_CHECK_SIZE(ac->ac_o_ex.fe_len,
330 - (8<<20)>>bsbits, max, 8 * 1024)) {
331 - start_off = ((loff_t)ac->ac_o_ex.fe_logical >>
332 - (23 - bsbits)) << 23;
333 - size = 8 * 1024 * 1024;
335 - start_off = (loff_t)ac->ac_o_ex.fe_logical << bsbits;
336 - size = ac->ac_o_ex.fe_len << bsbits;
338 + __u64 tstart, tend;
339 + /* file is quite large, we now preallocate with
340 + * the biggest configured window with regart to
341 + * logical offset */
342 + wind = sbi->s_mb_prealloc_table[i - 1];
343 + tstart = ac->ac_o_ex.fe_logical;
344 + do_div(tstart, wind);
345 + start = tstart * wind;
346 + tend = ac->ac_o_ex.fe_logical + ac->ac_o_ex.fe_len - 1;
347 + do_div(tend, wind);
348 + tend = tend * wind + wind;
349 + size = tend - start;
351 - orig_size = size = size >> bsbits;
352 - orig_start = start = start_off >> bsbits;
354 + orig_start = start;
356 /* don't cover already allocated blocks in selected range */
357 if (ar->pleft && start <= ar->lleft) {
358 @@ -3185,7 +3326,6 @@
360 BUG_ON(start + size <= ac->ac_o_ex.fe_logical &&
361 start > ac->ac_o_ex.fe_logical);
362 - BUG_ON(size <= 0 || size >= EXT4_BLOCKS_PER_GROUP(ac->ac_sb));
364 /* now prepare goal request */
366 @@ -4077,22 +4217,32 @@
368 struct ext4_sb_info *sbi = EXT4_SB(ac->ac_sb);
369 int bsbits = ac->ac_sb->s_blocksize_bits;
370 - loff_t size, isize;
373 if (!(ac->ac_flags & EXT4_MB_HINT_DATA))
376 - size = ac->ac_o_ex.fe_logical + ac->ac_o_ex.fe_len;
377 - isize = i_size_read(ac->ac_inode) >> bsbits;
378 - size = max(size, isize);
380 - /* don't use group allocation for large files */
381 - if (size >= sbi->s_mb_stream_request)
382 + if (ac->ac_o_ex.fe_len >= sbi->s_mb_small_req)
385 if (unlikely(ac->ac_flags & EXT4_MB_HINT_GOAL_ONLY))
388 + /* request is so large that we don't care about
389 + * streaming - it overweights any possible seek */
390 + if (ac->ac_o_ex.fe_len >= sbi->s_mb_large_req)
393 + size = ac->ac_o_ex.fe_logical + ac->ac_o_ex.fe_len;
394 + size = size << bsbits;
395 + if (size < i_size_read(ac->ac_inode))
396 + size = i_size_read(ac->ac_inode);
397 + size = (size + ac->ac_sb->s_blocksize - 1) >> bsbits;
399 + /* don't use group allocation for large files */
400 + if (size >= sbi->s_mb_large_req)
403 BUG_ON(ac->ac_lg != NULL);
405 * locality group prealloc space are per cpu. The reason for having