Whamcloud - gitweb
b=21137 ext4 extent allocation is slower than in ext3
[fs/lustre-release.git] / ldiskfs / kernel_patches / patches / ext4-prealloc-sles11.patch
1 Index: linux-2.6.27.21-0.1/fs/ext4/ext4_sb.h
2 ===================================================================
3 --- linux-2.6.27.21-0.1.orig/fs/ext4/ext4_sb.h  2009-05-28 11:13:24.000000000 +0530
4 +++ linux-2.6.27.21-0.1/fs/ext4/ext4_sb.h       2009-05-28 11:16:48.000000000 +0530
5 @@ -109,11 +109,14 @@
6  
7         /* tunables */
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         /* where last allocation was done - for stream allocation */
20         unsigned long s_mb_last_group;
21 Index: linux-2.6.27.21-0.1/fs/ext4/mballoc.c
22 ===================================================================
23 --- linux-2.6.27.21-0.1.orig/fs/ext4/mballoc.c  2009-05-28 11:12:43.000000000 +0530
24 +++ linux-2.6.27.21-0.1/fs/ext4/mballoc.c       2009-05-28 11:18:09.000000000 +0530
25 @@ -1996,7 +1996,7 @@
26         if (size < isize)
27                 size = isize;
28  
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 @@ -2686,6 +2686,26 @@
35         return -ENOMEM;
36  }
37  
38 +static void ext4_mb_prealloc_table_add(struct ext4_sb_info *sbi, int value)
39 +{
40 +       int i;
41 +
42 +       if (value > (sbi->s_blocks_per_group - 1 - 1 - sbi->s_itb_per_group))
43 +               return;
44 +
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;
48 +                       return;
49 +               }
50 +
51 +               /* they should add values in order */
52 +               if (value <= sbi->s_mb_prealloc_table[i])
53 +                       return;
54 +       }
55 +}
56 +
57 +
58  int ext4_mb_init(struct super_block *sb, int needs_recovery)
59  {
60         struct ext4_sb_info *sbi = EXT4_SB(sb);
61 @@ -2738,13 +2758,57 @@
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;
69 +
70 +       if (sbi->s_stripe == 0) {
71 +               sbi->s_mb_prealloc_table_size = 10;
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 +                       kfree(sbi->s_mb_offsets);
76 +                       kfree(sbi->s_mb_maxs);
77 +                       return -ENOMEM;
78 +               }
79 +               memset(sbi->s_mb_prealloc_table, 0, i);
80 +
81 +               ext4_mb_prealloc_table_add(sbi, 4);
82 +               ext4_mb_prealloc_table_add(sbi, 8);
83 +               ext4_mb_prealloc_table_add(sbi, 16);
84 +               ext4_mb_prealloc_table_add(sbi, 32);
85 +               ext4_mb_prealloc_table_add(sbi, 64);
86 +               ext4_mb_prealloc_table_add(sbi, 128);
87 +               ext4_mb_prealloc_table_add(sbi, 256);
88 +               ext4_mb_prealloc_table_add(sbi, 512);
89 +               ext4_mb_prealloc_table_add(sbi, 1024);
90 +               ext4_mb_prealloc_table_add(sbi, 2048);
91 +
92 +               sbi->s_mb_small_req = 256;
93 +               sbi->s_mb_large_req = 1024;
94 +               sbi->s_mb_group_prealloc = 512;
95 +       } else {
96 +               sbi->s_mb_prealloc_table_size = 3;
97 +               i = sbi->s_mb_prealloc_table_size * sizeof(unsigned long);
98 +               sbi->s_mb_prealloc_table = kmalloc(i, GFP_NOFS);
99 +               if (sbi->s_mb_prealloc_table == NULL) {
100 +                       kfree(sbi->s_mb_offsets);
101 +                       kfree(sbi->s_mb_maxs);
102 +                       return -ENOMEM;
103 +               }
104 +               memset(sbi->s_mb_prealloc_table, 0, i);
105 +
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);
109 +
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;
113 +       }
114  
115         sbi->s_locality_groups = alloc_percpu(struct ext4_locality_group);
116         if (sbi->s_locality_groups == NULL) {
117 +               kfree(sbi->s_mb_prealloc_table);
118                 kfree(sbi->s_mb_offsets);
119                 kfree(sbi->s_mb_maxs);
120                 return -ENOMEM;
121 @@ -2915,15 +2977,90 @@
122  #define EXT4_MB_MAX_TO_SCAN_NAME       "max_to_scan"
123  #define EXT4_MB_MIN_TO_SCAN_NAME       "min_to_scan"
124  #define EXT4_MB_ORDER2_REQ             "order2_req"
125 -#define EXT4_MB_STREAM_REQ             "stream_req"
126 +#define EXT4_MB_SMALL_REQ              "small_req"
127 +#define EXT4_MB_LARGE_REQ              "large_req"
128 +#define EXT4_MB_PREALLOC_TABLE         "prealloc_table"
129  #define EXT4_MB_GROUP_PREALLOC         "group_prealloc"
130  
131 +static int ext4_mb_prealloc_table_proc_read(char *page, char **start, off_t off,
132 +                                           int count, int *eof, void *data)
133 +{
134 +       struct ext4_sb_info *sbi = data;
135 +       int len = 0;
136 +       int i;
137 +
138 +       *eof = 1;
139 +       if (off != 0)
140 +               return 0;
141 +
142 +       for (i = 0; i < sbi->s_mb_prealloc_table_size; i++)
143 +               len += sprintf(page + len, "%ld ",
144 +                              sbi->s_mb_prealloc_table[i]);
145 +       len += sprintf(page + len, "\n");
146 +
147 +       *start = page;
148 +       return len;
149 +}
150 +
151 +static int ext4_mb_prealloc_table_proc_write(struct file *file,
152 +                                            const char __user *buf,
153 +                                            unsigned long cnt, void *data)
154 +{
155 +       struct ext4_sb_info *sbi = data;
156 +       unsigned long value;
157 +       unsigned long prev = 0;
158 +       char str[128];
159 +       char *cur;
160 +       char *end;
161 +       unsigned long *new_table;
162 +       int num = 0;
163 +       int i = 0;
164 +
165 +       if (cnt >= sizeof(str))
166 +               return -EINVAL;
167 +       if (copy_from_user(str, buf, cnt))
168 +               return -EFAULT;
169 +
170 +       num = 0;
171 +       cur = str;
172 +       end = str + cnt;
173 +       while (cur < end) {
174 +               while ((cur < end) && (*cur == ' ')) cur++;
175 +               value = simple_strtol(cur, &cur, 0);
176 +               if (value == 0)
177 +                       break;
178 +               if (value <= prev)
179 +                       return -EINVAL;
180 +               prev = value;
181 +               num++;
182 +       }
183 +
184 +       new_table = kmalloc(num * sizeof(*new_table), GFP_KERNEL);
185 +       if (new_table == NULL)
186 +               return -ENOMEM;
187 +       kfree(sbi->s_mb_prealloc_table);
188 +       memset(new_table, 0, num * sizeof(*new_table));
189 +       sbi->s_mb_prealloc_table = new_table;
190 +       sbi->s_mb_prealloc_table_size = num;
191 +       cur = str;
192 +       end = str + cnt;
193 +       while (cur < end && i < num) {
194 +       while ((cur < end) && (*cur == ' ')) cur++;
195 +               value = simple_strtol(cur, &cur, 0);
196 +               ext4_mb_prealloc_table_add(sbi, value);
197 +               i++;
198 +       }
199 +
200 +       return cnt;
201 +}
202 +
203  static int ext4_mb_init_per_dev_proc(struct super_block *sb)
204  {
205  #ifdef CONFIG_PROC_FS
206         mode_t mode = S_IFREG | S_IRUGO | S_IWUSR;
207         struct ext4_sb_info *sbi = EXT4_SB(sb);
208         struct proc_dir_entry *proc;
209 +       struct proc_dir_entry *proc_entry;
210  
211         if (sbi->s_proc == NULL)
212                 return -EINVAL;
213 @@ -2932,13 +3069,28 @@
214         EXT4_PROC_HANDLER(EXT4_MB_MAX_TO_SCAN_NAME, mb_max_to_scan);
215         EXT4_PROC_HANDLER(EXT4_MB_MIN_TO_SCAN_NAME, mb_min_to_scan);
216         EXT4_PROC_HANDLER(EXT4_MB_ORDER2_REQ, mb_order2_reqs);
217 -       EXT4_PROC_HANDLER(EXT4_MB_STREAM_REQ, mb_stream_request);
218 +       EXT4_PROC_HANDLER(EXT4_MB_SMALL_REQ, mb_small_req);
219 +       EXT4_PROC_HANDLER(EXT4_MB_LARGE_REQ, mb_large_req);
220         EXT4_PROC_HANDLER(EXT4_MB_GROUP_PREALLOC, mb_group_prealloc);
221 +
222 +       proc_entry = create_proc_entry(EXT4_MB_PREALLOC_TABLE, S_IFREG |
223 +                                      S_IRUGO | S_IWUSR, sbi->s_proc);
224 +       if (proc_entry == NULL) {
225 +               printk(KERN_ERR "EXT4-fs: unable to create %s\n",
226 +                      EXT4_MB_PREALLOC_TABLE);
227 +               goto err_out;
228 +       }
229 +       proc_entry->data = sbi;
230 +       proc_entry->read_proc = ext4_mb_prealloc_table_proc_read;
231 +       proc_entry->write_proc = ext4_mb_prealloc_table_proc_write;
232 +
233         return 0;
234  
235  err_out:
236         remove_proc_entry(EXT4_MB_GROUP_PREALLOC, sbi->s_proc);
237 -       remove_proc_entry(EXT4_MB_STREAM_REQ, sbi->s_proc);
238 +       remove_proc_entry(EXT4_MB_PREALLOC_TABLE, sbi->s_proc);
239 +       remove_proc_entry(EXT4_MB_LARGE_REQ, sbi->s_proc);
240 +       remove_proc_entry(EXT4_MB_SMALL_REQ, sbi->s_proc);
241         remove_proc_entry(EXT4_MB_ORDER2_REQ, sbi->s_proc);
242         remove_proc_entry(EXT4_MB_MIN_TO_SCAN_NAME, sbi->s_proc);
243         remove_proc_entry(EXT4_MB_MAX_TO_SCAN_NAME, sbi->s_proc);
244 @@ -2959,7 +3111,9 @@
245                 return -EINVAL;
246  
247         remove_proc_entry(EXT4_MB_GROUP_PREALLOC, sbi->s_proc);
248 -       remove_proc_entry(EXT4_MB_STREAM_REQ, sbi->s_proc);
249 +       remove_proc_entry(EXT4_MB_PREALLOC_TABLE, sbi->s_proc);
250 +       remove_proc_entry(EXT4_MB_LARGE_REQ, sbi->s_proc);
251 +       remove_proc_entry(EXT4_MB_SMALL_REQ, sbi->s_proc);
252         remove_proc_entry(EXT4_MB_ORDER2_REQ, sbi->s_proc);
253         remove_proc_entry(EXT4_MB_MIN_TO_SCAN_NAME, sbi->s_proc);
254         remove_proc_entry(EXT4_MB_MAX_TO_SCAN_NAME, sbi->s_proc);
255 @@ -3162,11 +3316,12 @@
256  ext4_mb_normalize_request(struct ext4_allocation_context *ac,
257                                 struct ext4_allocation_request *ar)
258  {
259 -       int bsbits, max;
260 +       int bsbits, i, wind;
261         ext4_lblk_t end;
262 -       loff_t size, orig_size, start_off;
263 +       loff_t size, orig_size;
264         ext4_lblk_t start, orig_start;
265         struct ext4_inode_info *ei = EXT4_I(ac->ac_inode);
266 +       struct ext4_sb_info *sbi = EXT4_SB(ac->ac_sb);
267         struct ext4_prealloc_space *pa;
268  
269         /* do normalize only data requests, metadata requests
270 @@ -3196,49 +3351,35 @@
271         size = size << bsbits;
272         if (size < i_size_read(ac->ac_inode))
273                 size = i_size_read(ac->ac_inode);
274 +       size = (size + ac->ac_sb->s_blocksize - 1) >> bsbits;
275  
276 -       /* max size of free chunks */
277 -       max = 2 << bsbits;
278 +       start = wind = 0;
279  
280 -#define NRL_CHECK_SIZE(req, size, max, chunk_size)     \
281 -               (req <= (size) || max <= (chunk_size))
282 +       /* let's choose preallocation window depending on file size */
283 +       for (i = 0; i < sbi->s_mb_prealloc_table_size; i++) {
284 +               if (size <= sbi->s_mb_prealloc_table[i]) {
285 +                       wind = sbi->s_mb_prealloc_table[i];
286 +                       break;
287 +               }
288 +       }
289 +       size = wind;
290  
291 -       /* first, try to predict filesize */
292 -       /* XXX: should this table be tunable? */
293 -       start_off = 0;
294 -       if (size <= 16 * 1024) {
295 -               size = 16 * 1024;
296 -       } else if (size <= 32 * 1024) {
297 -               size = 32 * 1024;
298 -       } else if (size <= 64 * 1024) {
299 -               size = 64 * 1024;
300 -       } else if (size <= 128 * 1024) {
301 -               size = 128 * 1024;
302 -       } else if (size <= 256 * 1024) {
303 -               size = 256 * 1024;
304 -       } else if (size <= 512 * 1024) {
305 -               size = 512 * 1024;
306 -       } else if (size <= 1024 * 1024) {
307 -               size = 1024 * 1024;
308 -       } else if (NRL_CHECK_SIZE(size, 4 * 1024 * 1024, max, 2 * 1024)) {
309 -               start_off = ((loff_t)ac->ac_o_ex.fe_logical >>
310 -                                               (21 - bsbits)) << 21;
311 -               size = 2 * 1024 * 1024;
312 -       } else if (NRL_CHECK_SIZE(size, 8 * 1024 * 1024, max, 4 * 1024)) {
313 -               start_off = ((loff_t)ac->ac_o_ex.fe_logical >>
314 -                                                       (22 - bsbits)) << 22;
315 -               size = 4 * 1024 * 1024;
316 -       } else if (NRL_CHECK_SIZE(ac->ac_o_ex.fe_len,
317 -                                       (8<<20)>>bsbits, max, 8 * 1024)) {
318 -               start_off = ((loff_t)ac->ac_o_ex.fe_logical >>
319 -                                                       (23 - bsbits)) << 23;
320 -               size = 8 * 1024 * 1024;
321 -       } else {
322 -               start_off = (loff_t)ac->ac_o_ex.fe_logical << bsbits;
323 -               size      = ac->ac_o_ex.fe_len << bsbits;
324 +       if (wind == 0) {
325 +               __u64 tstart, tend;
326 +               /* file is quite large, we now preallocate with
327 +                * the biggest configured window with regart to
328 +                * logical offset */
329 +               wind = sbi->s_mb_prealloc_table[i - 1];
330 +               tstart = ac->ac_o_ex.fe_logical;
331 +               do_div(tstart, wind);
332 +               start = tstart * wind;
333 +               tend = ac->ac_o_ex.fe_logical + ac->ac_o_ex.fe_len - 1;
334 +               do_div(tend, wind);
335 +               tend = tend * wind + wind;
336 +               size = tend - start;
337         }
338 -       orig_size = size = size >> bsbits;
339 -       orig_start = start = start_off >> bsbits;
340 +       orig_size = size;
341 +       orig_start = start;
342  
343         /* don't cover already allocated blocks in selected range */
344         if (ar->pleft && start <= ar->lleft) {
345 @@ -3315,7 +3456,6 @@
346         }
347         BUG_ON(start + size <= ac->ac_o_ex.fe_logical &&
348                         start > ac->ac_o_ex.fe_logical);
349 -       BUG_ON(size <= 0 || size >= EXT4_BLOCKS_PER_GROUP(ac->ac_sb));
350  
351         /* now prepare goal request */
352  
353 @@ -4236,22 +4376,32 @@
354  {
355         struct ext4_sb_info *sbi = EXT4_SB(ac->ac_sb);
356         int bsbits = ac->ac_sb->s_blocksize_bits;
357 -       loff_t size, isize;
358 +       loff_t size;
359  
360         if (!(ac->ac_flags & EXT4_MB_HINT_DATA))
361                 return;
362  
363 -       size = ac->ac_o_ex.fe_logical + ac->ac_o_ex.fe_len;
364 -       isize = i_size_read(ac->ac_inode) >> bsbits;
365 -       size = max(size, isize);
366 -
367 -       /* don't use group allocation for large files */
368 -       if (size >= sbi->s_mb_stream_request)
369 +       if (ac->ac_o_ex.fe_len >= sbi->s_mb_small_req)
370                 return;
371  
372         if (unlikely(ac->ac_flags & EXT4_MB_HINT_GOAL_ONLY))
373                 return;
374  
375 +       /* request is so large that we don't care about
376 +        * streaming - it overweights any possible seek */
377 +       if (ac->ac_o_ex.fe_len >= sbi->s_mb_large_req)
378 +               return;
379 +
380 +       size = ac->ac_o_ex.fe_logical + ac->ac_o_ex.fe_len;
381 +       size = size << bsbits;
382 +       if (size < i_size_read(ac->ac_inode))
383 +               size = i_size_read(ac->ac_inode);
384 +       size = (size + ac->ac_sb->s_blocksize - 1) >> bsbits;
385 +
386 +       /* don't use group allocation for large files */
387 +       if (size >= sbi->s_mb_large_req)
388 +               return;
389 +
390         BUG_ON(ac->ac_lg != NULL);
391         /*
392          * locality group prealloc space are per cpu. The reason for having
393 Index: linux-2.6.27.21-0.1/fs/ext4/inode.c
394 ===================================================================
395 --- linux-2.6.27.21-0.1.orig/fs/ext4/inode.c    2009-05-28 11:12:42.000000000 +0530
396 +++ linux-2.6.27.21-0.1/fs/ext4/inode.c 2009-05-28 11:16:48.000000000 +0530
397 @@ -2442,14 +2442,14 @@
398                 return -EROFS;
399  
400         /*
401 -        * Make sure nr_to_write is >= sbi->s_mb_stream_request
402 +        * Make sure nr_to_write is >= sbi->s_mb_small_req
403          * This make sure small files blocks are allocated in
404          * single attempt. This ensure that small files
405          * get less fragmented.
406          */
407 -       if (wbc->nr_to_write < sbi->s_mb_stream_request) {
408 -               nr_to_writebump = sbi->s_mb_stream_request - wbc->nr_to_write;
409 -               wbc->nr_to_write = sbi->s_mb_stream_request;
410 +       if (wbc->nr_to_write < sbi->s_mb_small_req) {
411 +               nr_to_writebump = sbi->s_mb_small_req - wbc->nr_to_write;
412 +               wbc->nr_to_write = sbi->s_mb_small_req;
413         }
414         if (wbc->range_start == 0 && wbc->range_end == LLONG_MAX)
415                 range_whole = 1;