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