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