Whamcloud - gitweb
LU-2473 ldiskfs: Reorganize ldiskfs kernel patches
[fs/lustre-release.git] / ldiskfs / kernel_patches / patches / rhel6.3 / ext4-prealloc.patch
1 Index: linux-stage/fs/ext4/ext4.h
2 ===================================================================
3 --- linux-stage.orig/fs/ext4/ext4.h     2011-03-11 14:17:02.000000000 +0800
4 +++ linux-stage/fs/ext4/ext4.h  2011-03-11 14:20:08.269063193 +0800
5 @@ -999,11 +999,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         unsigned int s_max_writeback_mb_bump;
20         /* where last allocation was done - for stream allocation */
21 Index: linux-stage/fs/ext4/inode.c
22 ===================================================================
23 @@ -3028,6 +3028,11 @@ static int ext4_da_writepages(struct add
24         if (unlikely(sbi->s_mount_flags & EXT4_MF_FS_ABORTED))
25                 return -EROFS;
26  
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;
30 +       }
31 +
32         if (wbc->range_start == 0 && wbc->range_end == LLONG_MAX)
33                 range_whole = 1;
34
35 Index: linux-stage/fs/ext4/mballoc.c
36 ===================================================================
37 --- linux-stage.orig/fs/ext4/mballoc.c  2011-03-11 14:03:32.000000000 +0800
38 +++ linux-stage/fs/ext4/mballoc.c       2011-03-11 14:44:49.106543493 +0800
39 @@ -1823,6 +1823,25 @@
40
41         }
42  }
43 +
44 +static void ext4_mb_prealloc_table_add(struct ext4_sb_info *sbi, int value)
45 +{
46 +       int i;
47 +
48 +       if (value > (sbi->s_blocks_per_group - 1 - 1 - sbi->s_itb_per_group))
49 +               return;
50 +
51 +       for (i = 0; i < sbi->s_mb_prealloc_table_size; i++) {
52 +               if (sbi->s_mb_prealloc_table[i] == 0) {
53 +                       sbi->s_mb_prealloc_table[i] = value;
54 +                       return;
55 +               }
56 +
57 +               /* they should add values in order */
58 +               if (value <= sbi->s_mb_prealloc_table[i])
59 +                       return;
60 +       }
61 +}
62  
63  static int ext4_mb_good_group(struct ext4_allocation_context *ac,
64                                 ext4_group_t group, int cr)
65 @@ -2173,6 +2193,80 @@
66         .show   = ext4_mb_seq_groups_show,
67  };
68  
69 +#define EXT4_MB_PREALLOC_TABLE          "prealloc_table"
70 +
71 +static int ext4_mb_prealloc_table_proc_read(char *page, char **start, off_t off,
72 +                                           int count, int *eof, void *data)
73 +{
74 +       struct ext4_sb_info *sbi = data;
75 +       int len = 0;
76 +       int i;
77 +
78 +       *eof = 1;
79 +       if (off != 0)
80 +               return 0;
81 +
82 +       for (i = 0; i < sbi->s_mb_prealloc_table_size; i++)
83 +               len += sprintf(page + len, "%ld ",
84 +                              sbi->s_mb_prealloc_table[i]);
85 +       len += sprintf(page + len, "\n");
86 +
87 +       *start = page;
88 +       return len;
89 +}
90 +
91 +static int ext4_mb_prealloc_table_proc_write(struct file *file,
92 +                                            const char __user *buf,
93 +                                            unsigned long cnt, void *data)
94 +{
95 +       struct ext4_sb_info *sbi = data;
96 +       unsigned long value;
97 +       unsigned long prev = 0;
98 +       char str[128];
99 +       char *cur;
100 +       char *end;
101 +       unsigned long *new_table;
102 +       int num = 0;
103 +       int i = 0;
104 +
105 +       if (cnt >= sizeof(str))
106 +               return -EINVAL;
107 +       if (copy_from_user(str, buf, cnt))
108 +               return -EFAULT;
109 +
110 +       num = 0;
111 +       cur = str;
112 +       end = str + cnt;
113 +       while (cur < end) {
114 +               while ((cur < end) && (*cur == ' ')) cur++;
115 +               value = simple_strtol(cur, &cur, 0);
116 +               if (value == 0)
117 +                       break;
118 +               if (value <= prev)
119 +                       return -EINVAL;
120 +               prev = value;
121 +               num++;
122 +       }
123 +
124 +       new_table = kmalloc(num * sizeof(*new_table), GFP_KERNEL);
125 +       if (new_table == NULL)
126 +               return -ENOMEM;
127 +       kfree(sbi->s_mb_prealloc_table);
128 +       memset(new_table, 0, num * sizeof(*new_table));
129 +       sbi->s_mb_prealloc_table = new_table;
130 +       sbi->s_mb_prealloc_table_size = num;
131 +       cur = str;
132 +       end = str + cnt;
133 +       while (cur < end && i < num) {
134 +       while ((cur < end) && (*cur == ' ')) cur++;
135 +               value = simple_strtol(cur, &cur, 0);
136 +               ext4_mb_prealloc_table_add(sbi, value);
137 +               i++;
138 +       }
139 +
140 +       return cnt;
141 +}
142 +
143  static int ext4_mb_seq_groups_open(struct inode *inode, struct file *file)
144  {
145         struct super_block *sb = PDE(inode)->data;
146 @@ -2397,14 +2497,6 @@ int ext4_mb_init(struct super_block *sb,
147                 i++;
148         } while (i <= sb->s_blocksize_bits + 1);
149  
150 -       /* init file for buddy data */
151 -       ret = ext4_mb_init_backend(sb);
152 -       if (ret != 0) {
153 -               kfree(sbi->s_mb_offsets);
154 -               kfree(sbi->s_mb_maxs);
155 -               return ret;
156 -       }
157 -
158         spin_lock_init(&sbi->s_md_lock);
159         spin_lock_init(&sbi->s_bal_lock);
160  
161 @@ -2411,12 +2505,56 @@
162         sbi->s_mb_max_to_scan = MB_DEFAULT_MAX_TO_SCAN;
163         sbi->s_mb_min_to_scan = MB_DEFAULT_MIN_TO_SCAN;
164         sbi->s_mb_stats = MB_DEFAULT_STATS;
165 -       sbi->s_mb_stream_request = MB_DEFAULT_STREAM_THRESHOLD;
166         sbi->s_mb_order2_reqs = MB_DEFAULT_ORDER2_REQS;
167 -       sbi->s_mb_group_prealloc = MB_DEFAULT_GROUP_PREALLOC;
168 +
169 +       if (sbi->s_stripe == 0) {
170 +               sbi->s_mb_prealloc_table_size = 10;
171 +               i = sbi->s_mb_prealloc_table_size * sizeof(unsigned long);
172 +               sbi->s_mb_prealloc_table = kmalloc(i, GFP_NOFS);
173 +               if (sbi->s_mb_prealloc_table == NULL) {
174 +                       kfree(sbi->s_mb_offsets);
175 +                       kfree(sbi->s_mb_maxs);
176 +                       return -ENOMEM;
177 +               }
178 +               memset(sbi->s_mb_prealloc_table, 0, i);
179 +
180 +               ext4_mb_prealloc_table_add(sbi, 4);
181 +               ext4_mb_prealloc_table_add(sbi, 8);
182 +               ext4_mb_prealloc_table_add(sbi, 16);
183 +               ext4_mb_prealloc_table_add(sbi, 32);
184 +               ext4_mb_prealloc_table_add(sbi, 64);
185 +               ext4_mb_prealloc_table_add(sbi, 128);
186 +               ext4_mb_prealloc_table_add(sbi, 256);
187 +               ext4_mb_prealloc_table_add(sbi, 512);
188 +               ext4_mb_prealloc_table_add(sbi, 1024);
189 +               ext4_mb_prealloc_table_add(sbi, 2048);
190 +
191 +               sbi->s_mb_small_req = 256;
192 +               sbi->s_mb_large_req = 1024;
193 +               sbi->s_mb_group_prealloc = 512;
194 +       } else {
195 +               sbi->s_mb_prealloc_table_size = 3;
196 +               i = sbi->s_mb_prealloc_table_size * sizeof(unsigned long);
197 +               sbi->s_mb_prealloc_table = kmalloc(i, GFP_NOFS);
198 +               if (sbi->s_mb_prealloc_table == NULL) {
199 +                       kfree(sbi->s_mb_offsets);
200 +                       kfree(sbi->s_mb_maxs);
201 +                       return -ENOMEM;
202 +               }
203 +               memset(sbi->s_mb_prealloc_table, 0, i);
204 +
205 +               ext4_mb_prealloc_table_add(sbi, sbi->s_stripe);
206 +               ext4_mb_prealloc_table_add(sbi, sbi->s_stripe * 2);
207 +               ext4_mb_prealloc_table_add(sbi, sbi->s_stripe * 4);
208 +
209 +               sbi->s_mb_small_req = sbi->s_stripe;
210 +               sbi->s_mb_large_req = sbi->s_stripe * 8;
211 +               sbi->s_mb_group_prealloc = sbi->s_stripe * 4;
212 +       }
213  
214         sbi->s_locality_groups = alloc_percpu(struct ext4_locality_group);
215         if (sbi->s_locality_groups == NULL) {
216 +               kfree(sbi->s_mb_prealloc_table);
217                 kfree(sbi->s_mb_offsets);
218                 kfree(sbi->s_mb_maxs);
219                 return -ENOMEM;
220 @@ -2430,9 +2568,27 @@
221                 spin_lock_init(&lg->lg_prealloc_lock);
222         }
223  
224 +       /* init file for buddy data */
225 +       ret = ext4_mb_init_backend(sb);
226 +       if (ret != 0) {
227 +               kfree(sbi->s_mb_prealloc_table);
228 +               kfree(sbi->s_mb_offsets);
229 +               kfree(sbi->s_mb_maxs);
230 +               return ret;
231 +       }
232 +
233 -       if (sbi->s_proc)
234 +       if (sbi->s_proc) {
235 +               struct proc_dir_entry *p;
236                 proc_create_data("mb_groups", S_IRUGO, sbi->s_proc,
237                                  &ext4_mb_seq_groups_fops, sb);
238 +               p = create_proc_entry(EXT4_MB_PREALLOC_TABLE, S_IFREG |
239 +                                     S_IRUGO | S_IWUSR, sbi->s_proc);
240 +               if (p) {
241 +                       p->data = sbi;
242 +                       p->read_proc = ext4_mb_prealloc_table_proc_read;
243 +                       p->write_proc = ext4_mb_prealloc_table_proc_write;
244 +               }
245 +       }
246  
247         if (sbi->s_journal)
248                 sbi->s_journal->j_commit_callback = release_blocks_on_commit;
249 @@ -2483,6 +2639,7 @@
250                         kfree(sbi->s_group_info[i]);
251                 kfree(sbi->s_group_info);
252         }
253 +       kfree(sbi->s_mb_prealloc_table);
254         kfree(sbi->s_mb_offsets);
255         kfree(sbi->s_mb_maxs);
256         if (sbi->s_buddy_cache)
257 @@ -2512,8 +2668,10 @@
258         }
259  
260         free_percpu(sbi->s_locality_groups);
261 -       if (sbi->s_proc)
262 +       if (sbi->s_proc) {
263                 remove_proc_entry("mb_groups", sbi->s_proc);
264 +               remove_proc_entry(EXT4_MB_PREALLOC_TABLE, sbi->s_proc);
265 +       }
266  
267         return 0;
268  }
269 @@ -2807,11 +2965,12 @@
270  ext4_mb_normalize_request(struct ext4_allocation_context *ac,
271                                 struct ext4_allocation_request *ar)
272  {
273 -       int bsbits, max;
274 +       int bsbits, i, wind;
275         ext4_lblk_t end;
276 -       loff_t size, orig_size, start_off;
277 +       loff_t size, orig_size;
278         ext4_lblk_t start, orig_start;
279         struct ext4_inode_info *ei = EXT4_I(ac->ac_inode);
280 +       struct ext4_sb_info *sbi = EXT4_SB(ac->ac_sb);
281         struct ext4_prealloc_space *pa;
282  
283         /* do normalize only data requests, metadata requests
284 @@ -2841,49 +3000,35 @@
285         size = size << bsbits;
286         if (size < i_size_read(ac->ac_inode))
287                 size = i_size_read(ac->ac_inode);
288 +       size = (size + ac->ac_sb->s_blocksize - 1) >> bsbits;
289  
290 -       /* max size of free chunks */
291 -       max = 2 << bsbits;
292 +       start = wind = 0;
293  
294 -#define NRL_CHECK_SIZE(req, size, max, chunk_size)     \
295 -               (req <= (size) || max <= (chunk_size))
296 +       /* let's choose preallocation window depending on file size */
297 +       for (i = 0; i < sbi->s_mb_prealloc_table_size; i++) {
298 +               if (size <= sbi->s_mb_prealloc_table[i]) {
299 +                       wind = sbi->s_mb_prealloc_table[i];
300 +                       break;
301 +               }
302 +       }
303 +       size = wind;
304  
305 -       /* first, try to predict filesize */
306 -       /* XXX: should this table be tunable? */
307 -       start_off = 0;
308 -       if (size <= 16 * 1024) {
309 -               size = 16 * 1024;
310 -       } else if (size <= 32 * 1024) {
311 -               size = 32 * 1024;
312 -       } else if (size <= 64 * 1024) {
313 -               size = 64 * 1024;
314 -       } else if (size <= 128 * 1024) {
315 -               size = 128 * 1024;
316 -       } else if (size <= 256 * 1024) {
317 -               size = 256 * 1024;
318 -       } else if (size <= 512 * 1024) {
319 -               size = 512 * 1024;
320 -       } else if (size <= 1024 * 1024) {
321 -               size = 1024 * 1024;
322 -       } else if (NRL_CHECK_SIZE(size, 4 * 1024 * 1024, max, 2 * 1024)) {
323 -               start_off = ((loff_t)ac->ac_o_ex.fe_logical >>
324 -                                               (21 - bsbits)) << 21;
325 -               size = 2 * 1024 * 1024;
326 -       } else if (NRL_CHECK_SIZE(size, 8 * 1024 * 1024, max, 4 * 1024)) {
327 -               start_off = ((loff_t)ac->ac_o_ex.fe_logical >>
328 -                                                       (22 - bsbits)) << 22;
329 -               size = 4 * 1024 * 1024;
330 -       } else if (NRL_CHECK_SIZE(ac->ac_o_ex.fe_len,
331 -                                       (8<<20)>>bsbits, max, 8 * 1024)) {
332 -               start_off = ((loff_t)ac->ac_o_ex.fe_logical >>
333 -                                                       (23 - bsbits)) << 23;
334 -               size = 8 * 1024 * 1024;
335 -       } else {
336 -               start_off = (loff_t)ac->ac_o_ex.fe_logical << bsbits;
337 -               size      = ac->ac_o_ex.fe_len << bsbits;
338 +       if (wind == 0) {
339 +               __u64 tstart, tend;
340 +               /* file is quite large, we now preallocate with
341 +                * the biggest configured window with regart to
342 +                * logical offset */
343 +               wind = sbi->s_mb_prealloc_table[i - 1];
344 +               tstart = ac->ac_o_ex.fe_logical;
345 +               do_div(tstart, wind);
346 +               start = tstart * wind;
347 +               tend = ac->ac_o_ex.fe_logical + ac->ac_o_ex.fe_len - 1;
348 +               do_div(tend, wind);
349 +               tend = tend * wind + wind;
350 +               size = tend - start;
351         }
352 -       orig_size = size = size >> bsbits;
353 -       orig_start = start = start_off >> bsbits;
354 +       orig_size = size;
355 +       orig_start = start;
356  
357         /* don't cover already allocated blocks in selected range */
358         if (ar->pleft && start <= ar->lleft) {
359 @@ -2955,7 +3100,6 @@
360         }
361         BUG_ON(start + size <= ac->ac_o_ex.fe_logical &&
362                         start > ac->ac_o_ex.fe_logical);
363 -       BUG_ON(size <= 0 || size > EXT4_BLOCKS_PER_GROUP(ac->ac_sb));
364  
365         /* now prepare goal request */
366  
367 @@ -3939,11 +4083,19 @@
368  
369         /* don't use group allocation for large files */
370         size = max(size, isize);
371 -       if (size > sbi->s_mb_stream_request) {
372 +       if ((ac->ac_o_ex.fe_len >= sbi->s_mb_small_req) ||
373 +           (size >= sbi->s_mb_large_req)) {
374                 ac->ac_flags |= EXT4_MB_STREAM_ALLOC;
375                 return;
376         }
377  
378 +       /*
379 +        * request is so large that we don't care about
380 +        * streaming - it overweights any possible seek
381 +        */
382 +       if (ac->ac_o_ex.fe_len >= sbi->s_mb_large_req)
383 +               return;
384 +
385         BUG_ON(ac->ac_lg != NULL);
386         /*
387          * locality group prealloc space are per cpu. The reason for having
388 Index: linux-stage/fs/ext4/super.c
389 ===================================================================
390 --- linux-stage.orig/fs/ext4/super.c    2011-03-11 14:16:56.000000000 +0800
391 +++ linux-stage/fs/ext4/super.c 2011-03-11 14:19:24.664467626 +0800
392 @@ -2632,7 +2632,8 @@
393  EXT4_RW_ATTR_SBI_UI(mb_max_to_scan, s_mb_max_to_scan);
394  EXT4_RW_ATTR_SBI_UI(mb_min_to_scan, s_mb_min_to_scan);
395  EXT4_RW_ATTR_SBI_UI(mb_order2_req, s_mb_order2_reqs);
396 -EXT4_RW_ATTR_SBI_UI(mb_stream_req, s_mb_stream_request);
397 +EXT4_RW_ATTR_SBI_UI(mb_small_req, s_mb_small_req);
398 +EXT4_RW_ATTR_SBI_UI(mb_large_req, s_mb_large_req);
399  EXT4_RW_ATTR_SBI_UI(mb_group_prealloc, s_mb_group_prealloc);
400  EXT4_RW_ATTR_SBI_UI(max_writeback_mb_bump, s_max_writeback_mb_bump);
401  
402 @@ -2647,7 +2648,8 @@
403         ATTR_LIST(mb_max_to_scan),
404         ATTR_LIST(mb_min_to_scan),
405         ATTR_LIST(mb_order2_req),
406 -       ATTR_LIST(mb_stream_req),
407 +       ATTR_LIST(mb_small_req),
408 +       ATTR_LIST(mb_large_req),
409         ATTR_LIST(mb_group_prealloc),
410         ATTR_LIST(max_writeback_mb_bump),
411         NULL,