Whamcloud - gitweb
LU-162 Export ext4_discard_preallocations for ldiskfs
[fs/lustre-release.git] / ldiskfs / kernel_patches / patches / ext4-mballoc-extra-checks-rhel6.patch
1 Index: linux-stage/fs/ext4/ext4.h
2 ===================================================================
3 --- linux-stage.orig/fs/ext4/ext4.h     2011-03-14 16:18:28.300241437 +0800
4 +++ linux-stage/fs/ext4/ext4.h  2011-03-14 16:33:17.056087375 +0800
5 @@ -1770,6 +1770,7 @@
6         ext4_grpblk_t   bb_free;        /* total free blocks */
7         ext4_grpblk_t   bb_fragments;   /* nr of freespace fragments */
8         struct          list_head bb_prealloc_list;
9 +       unsigned long   bb_prealloc_nr;
10  #ifdef DOUBLE_CHECK
11         void            *bb_bitmap;
12  #endif
13 Index: linux-stage/fs/ext4/mballoc.c
14 ===================================================================
15 --- linux-stage.orig/fs/ext4/mballoc.c  2011-03-14 16:18:28.336242149 +0800
16 +++ linux-stage/fs/ext4/mballoc.c       2011-03-14 16:33:27.072292006 +0800
17 @@ -337,7 +337,7 @@
18  static struct kmem_cache *ext4_pspace_cachep;
19  static struct kmem_cache *ext4_ac_cachep;
20  static struct kmem_cache *ext4_free_ext_cachep;
21 -static void ext4_mb_generate_from_pa(struct super_block *sb, void *bitmap,
22 +static int ext4_mb_generate_from_pa(struct super_block *sb, void *bitmap,
23                                         ext4_group_t group);
24  static void ext4_mb_generate_from_freelist(struct super_block *sb, void *bitmap,
25                                                 ext4_group_t group);
26 @@ -659,7 +659,7 @@
27  }
28  
29  static noinline_for_stack
30 -void ext4_mb_generate_buddy(struct super_block *sb,
31 +int ext4_mb_generate_buddy(struct super_block *sb,
32                                 void *buddy, void *bitmap, ext4_group_t group)
33  {
34         struct ext4_group_info *grp = ext4_get_group_info(sb, group);
35 @@ -691,14 +691,13 @@
36         grp->bb_fragments = fragments;
37  
38         if (free != grp->bb_free) {
39 -               ext4_grp_locked_error(sb, group,  __func__,
40 -                       "EXT4-fs: group %u: %u blocks in bitmap, %u in gd",
41 -                       group, free, grp->bb_free);
42 -               /*
43 -                * If we intent to continue, we consider group descritor
44 -                * corrupt and update bb_free using bitmap value
45 -                */
46 -               grp->bb_free = free;
47 +               struct ext4_group_desc *gdp;
48 +               gdp = ext4_get_group_desc (sb, group, NULL);
49 +               ext4_error(sb, "group %lu: %u blocks in bitmap, %u in bb, "
50 +                       "%u in gd, %lu pa's\n", (long unsigned int)group,
51 +                       free, grp->bb_free, ext4_free_blks_count(sb, gdp),
52 +                       grp->bb_prealloc_nr);
53 +               return -EIO;
54         }
55  
56         clear_bit(EXT4_GROUP_INFO_NEED_INIT_BIT, &(grp->bb_state));
57 @@ -708,6 +707,8 @@
58         EXT4_SB(sb)->s_mb_buddies_generated++;
59         EXT4_SB(sb)->s_mb_generation_time += period;
60         spin_unlock(&EXT4_SB(sb)->s_bal_lock);
61 +
62 +       return 0;
63  }
64  
65  /* The buddy information is attached the buddy cache inode
66 @@ -839,7 +840,7 @@
67         first_block = page->index * blocks_per_page;
68         /* init the page  */
69         memset(page_address(page), 0xff, PAGE_CACHE_SIZE);
70 -       for (i = 0; i < blocks_per_page; i++) {
71 +       for (i = 0; i < blocks_per_page && err == 0; i++) {
72                 int group;
73                 struct ext4_group_info *grinfo;
74  
75 @@ -874,7 +875,7 @@
76                          * incore got set to the group block bitmap below
77                          */
78                         ext4_lock_group(sb, group);
79 -                       ext4_mb_generate_buddy(sb, data, incore, group);
80 +                       err = ext4_mb_generate_buddy(sb, data, incore, group);
81                         ext4_unlock_group(sb, group);
82                         incore = NULL;
83                 } else {
84 @@ -888,7 +889,7 @@
85                         memcpy(data, bitmap, blocksize);
86  
87                         /* mark all preallocated blks used in in-core bitmap */
88 -                       ext4_mb_generate_from_pa(sb, data, group);
89 +                       err = ext4_mb_generate_from_pa(sb, data, group);
90                         ext4_mb_generate_from_freelist(sb, data, group);
91                         ext4_unlock_group(sb, group);
92  
93 @@ -898,7 +899,8 @@
94                         incore = data;
95                 }
96         }
97 -       SetPageUptodate(page);
98 +       if (likely(err == 0))
99 +               SetPageUptodate(page);
100  
101  out:
102         if (bh) {
103 @@ -2142,9 +2144,11 @@
104  static int ext4_mb_seq_groups_show(struct seq_file *seq, void *v)
105  {
106         struct super_block *sb = seq->private;
107 +       struct ext4_group_desc *gdp;
108         ext4_group_t group = (ext4_group_t) ((unsigned long) v);
109         int i;
110         int err;
111 +       int free = 0;
112         struct ext4_buddy e4b;
113         struct sg {
114                 struct ext4_group_info info;
115 @@ -2153,10 +2157,10 @@
116  
117         group--;
118         if (group == 0)
119 -               seq_printf(seq, "#%-5s: %-5s %-5s %-5s "
120 +               seq_printf(seq, "#%-5s: %-5s %-5s %-5s %-5s %-5s"
121                                 "[ %-5s %-5s %-5s %-5s %-5s %-5s %-5s "
122                                   "%-5s %-5s %-5s %-5s %-5s %-5s %-5s ]\n",
123 -                          "group", "free", "frags", "first",
124 +                          "group", "free", "frags", "first", "first", "pa",
125                            "2^0", "2^1", "2^2", "2^3", "2^4", "2^5", "2^6",
126                            "2^7", "2^8", "2^9", "2^10", "2^11", "2^12", "2^13");
127  
128 @@ -2167,13 +2171,20 @@
129                 seq_printf(seq, "#%-5u: I/O error\n", group);
130                 return 0;
131         }
132 +
133 +       gdp = ext4_get_group_desc(sb, group, NULL);
134 +       if (gdp != NULL)
135 +               free = ext4_free_blks_count(sb, gdp);
136 +
137         ext4_lock_group(sb, group);
138         memcpy(&sg, ext4_get_group_info(sb, group), i);
139         ext4_unlock_group(sb, group);
140         ext4_mb_release_desc(&e4b);
141  
142 -       seq_printf(seq, "#%-5u: %-5u %-5u %-5u [", group, sg.info.bb_free,
143 -                       sg.info.bb_fragments, sg.info.bb_first_free);
144 +       seq_printf(seq, "#%-5lu: %-5u %-5u %-5u %-5u %-5lu [",
145 +                       (long unsigned int)group, sg.info.bb_free, free,
146 +                       sg.info.bb_fragments, sg.info.bb_first_free,
147 +                       sg.info.bb_prealloc_nr);
148         for (i = 0; i <= 13; i++)
149                 seq_printf(seq, " %-5u", i <= sb->s_blocksize_bits + 1 ?
150                                 sg.info.bb_counters[i] : 0);
151 @@ -3354,23 +3365,68 @@
152  }
153  
154  /*
155 + * check free blocks in bitmap match free block in group descriptor
156 + * do this before taking preallocated blocks into account to be able
157 + * to detect on-disk corruptions. The group lock should be hold by the
158 + * caller.
159 + */
160 +int ext4_mb_check_ondisk_bitmap(struct super_block *sb, void *bitmap,
161 +                               struct ext4_group_desc *gdp, int group)
162 +{
163 +       unsigned short max = EXT4_BLOCKS_PER_GROUP(sb);
164 +       unsigned short i, first, free = 0;
165 +
166 +       i = mb_find_next_zero_bit(bitmap, max, 0);
167 +
168 +       while (i < max) {
169 +               first = i;
170 +               i = mb_find_next_bit(bitmap, max, i);
171 +               if (i > max)
172 +                       i = max;
173 +               free += i - first;
174 +               if (i < max)
175 +                       i = mb_find_next_zero_bit(bitmap, max, i);
176 +       }
177 +
178 +       if (free != ext4_free_blks_count(sb, gdp)) {
179 +               ext4_error(sb, "on-disk bitmap for group %d"
180 +                       "corrupted: %u blocks free in bitmap, %u - in gd\n",
181 +                       group, free, ext4_free_blks_count(sb, gdp));
182 +               return -EIO;
183 +       }
184 +       return 0;
185 +}
186 +
187 +/*
188   * the function goes through all preallocation in this group and marks them
189   * used in in-core bitmap. buddy must be generated from this bitmap
190   * Need to be called with ext4 group lock held
191   */
192  static noinline_for_stack
193 -void ext4_mb_generate_from_pa(struct super_block *sb, void *bitmap,
194 +int ext4_mb_generate_from_pa(struct super_block *sb, void *bitmap,
195                                         ext4_group_t group)
196  {
197         struct ext4_group_info *grp = ext4_get_group_info(sb, group);
198         struct ext4_prealloc_space *pa;
199 +       struct ext4_group_desc *gdp;
200         struct list_head *cur;
201         ext4_group_t groupnr;
202         ext4_grpblk_t start;
203         int preallocated = 0;
204         int count = 0;
205 +       int skip = 0;
206 +       int err;
207         int len;
208  
209 +       gdp = ext4_get_group_desc (sb, group, NULL);
210 +       if (gdp == NULL)
211 +               return -EIO;
212 +
213 +       /* before applying preallocations, check bitmap consistency */
214 +       err = ext4_mb_check_ondisk_bitmap(sb, bitmap, gdp, group);
215 +       if (err)
216 +               return err;
217 +
218         /* all form of preallocation discards first load group,
219          * so the only competing code is preallocation use.
220          * we don't need any locking here
221 @@ -3386,14 +3442,23 @@
222                                              &groupnr, &start);
223                 len = pa->pa_len;
224                 spin_unlock(&pa->pa_lock);
225 -               if (unlikely(len == 0))
226 +               if (unlikely(len == 0)) {
227 +                       skip++;
228                         continue;
229 +               }
230                 BUG_ON(groupnr != group);
231                 mb_set_bits(bitmap, start, len);
232                 preallocated += len;
233                 count++;
234         }
235 +       if (count + skip != grp->bb_prealloc_nr) {
236 +               ext4_error(sb, "lost preallocations: "
237 +                          "count %d, bb_prealloc_nr %lu, skip %d\n",
238 +                          count, grp->bb_prealloc_nr, skip);
239 +               return -EIO;
240 +       }
241         mb_debug(1, "prellocated %u for group %u\n", preallocated, group);
242 +       return 0;
243  }
244  
245  static void ext4_mb_pa_callback(struct rcu_head *head)
246 @@ -3452,6 +3517,7 @@
247          */
248         ext4_lock_group(sb, grp);
249         list_del(&pa->pa_group_list);
250 +       ext4_get_group_info(sb, grp)->bb_prealloc_nr--;
251         ext4_unlock_group(sb, grp);
252  
253         spin_lock(pa->pa_obj_lock);
254 @@ -3543,6 +3609,7 @@
255  
256         ext4_lock_group(sb, ac->ac_b_ex.fe_group);
257         list_add(&pa->pa_group_list, &grp->bb_prealloc_list);
258 +       grp->bb_prealloc_nr++;
259         ext4_unlock_group(sb, ac->ac_b_ex.fe_group);
260  
261         spin_lock(pa->pa_obj_lock);
262 @@ -3604,6 +3671,7 @@
263  
264         ext4_lock_group(sb, ac->ac_b_ex.fe_group);
265         list_add(&pa->pa_group_list, &grp->bb_prealloc_list);
266 +       grp->bb_prealloc_nr++;
267         ext4_unlock_group(sb, ac->ac_b_ex.fe_group);
268  
269         /*
270 @@ -3802,6 +3870,8 @@
271  
272                 spin_unlock(&pa->pa_lock);
273  
274 +               BUG_ON(grp->bb_prealloc_nr == 0);
275 +               grp->bb_prealloc_nr--;
276                 list_del(&pa->pa_group_list);
277                 list_add(&pa->u.pa_tmp_list, &list);
278         }
279 @@ -3942,7 +4012,7 @@
280                 if (err) {
281                         ext4_error(sb, "Error loading buddy information for %u",
282                                         group);
283 -                       continue;
284 +                       return;
285                 }
286  
287                 bitmap_bh = ext4_read_block_bitmap(sb, group);
288 @@ -3954,6 +4024,8 @@
289                 }
290  
291                 ext4_lock_group(sb, group);
292 +               BUG_ON(e4b.bd_info->bb_prealloc_nr == 0);
293 +               e4b.bd_info->bb_prealloc_nr--;
294                 list_del(&pa->pa_group_list);
295                 ext4_mb_release_inode_pa(&e4b, bitmap_bh, pa, ac);
296                 ext4_unlock_group(sb, group);
297 @@ -4227,6 +4299,7 @@
298                 }
299                 ext4_lock_group(sb, group);
300                 list_del(&pa->pa_group_list);
301 +               ext4_get_group_info(sb, group)->bb_prealloc_nr--;
302                 ext4_mb_release_group_pa(&e4b, pa, ac);
303                 ext4_unlock_group(sb, group);
304  
305 Index: linux-stage/fs/ext4/mballoc.h
306 ===================================================================
307 --- linux-stage.orig/fs/ext4/mballoc.h  2011-03-14 16:18:26.670209322 +0800
308 +++ linux-stage/fs/ext4/mballoc.h       2011-03-14 16:32:50.859552482 +0800
309 @@ -88,7 +88,7 @@
310  /*
311   * for which requests use 2^N search using buddies
312   */
313 -#define MB_DEFAULT_ORDER2_REQS         2
314 +#define MB_DEFAULT_ORDER2_REQS         8
315  
316  /*
317   * default group prealloc size 512 blocks