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