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