Whamcloud - gitweb
LU-3674 ldiskfs: fix mb_groups column names
[fs/lustre-release.git] / ldiskfs / kernel_patches / patches / sles11sp2 / ext4-mballoc-extra-checks.patch
1 ---
2  fs/ext4/ext4.h    |    1 
3  fs/ext4/mballoc.c |  108 ++++++++++++++++++++++++++++++++++++++++++++++--------
4  fs/ext4/mballoc.h |    2 -
5  3 files changed, 95 insertions(+), 16 deletions(-)
6
7 --- a/fs/ext4/ext4.h
8 +++ b/fs/ext4/ext4.h
9 @@ -2098,6 +2098,7 @@ struct ext4_group_info {
10         ext4_grpblk_t   bb_fragments;   /* nr of freespace fragments */
11         ext4_grpblk_t   bb_largest_free_order;/* order of largest frag in BG */
12         struct          list_head bb_prealloc_list;
13 +       unsigned long   bb_prealloc_nr;
14  #ifdef DOUBLE_CHECK
15         void            *bb_bitmap;
16  #endif
17 --- a/fs/ext4/mballoc.c
18 +++ b/fs/ext4/mballoc.c
19 @@ -352,7 +352,7 @@ static const char *ext4_groupinfo_slab_n
20         "ext4_groupinfo_64k", "ext4_groupinfo_128k"
21  };
22  
23 -static void ext4_mb_generate_from_pa(struct super_block *sb, void *bitmap,
24 +static int ext4_mb_generate_from_pa(struct super_block *sb, void *bitmap,
25                                         ext4_group_t group);
26  static void ext4_mb_generate_from_freelist(struct super_block *sb, void *bitmap,
27                                                 ext4_group_t group);
28 @@ -702,7 +702,7 @@ mb_set_largest_free_order(struct super_b
29  }
30  
31  static noinline_for_stack
32 -void ext4_mb_generate_buddy(struct super_block *sb,
33 +int ext4_mb_generate_buddy(struct super_block *sb,
34                                 void *buddy, void *bitmap, ext4_group_t group)
35  {
36         struct ext4_group_info *grp = ext4_get_group_info(sb, group);
37 @@ -734,14 +734,19 @@ void ext4_mb_generate_buddy(struct super
38         grp->bb_fragments = fragments;
39  
40         if (free != grp->bb_free) {
41 +               struct ext4_group_desc *gdp;
42 +               gdp = ext4_get_group_desc (sb, group, NULL);
43                 ext4_grp_locked_error(sb, group, 0, 0,
44 -                                     "%u blocks in bitmap, %u in gd",
45 -                                     free, grp->bb_free);
46 +                                     "%u blocks in bitmap, %u in bb, %u in gd",
47 +                                     free, grp->bb_free,
48 +                                     ext4_free_blks_count(sb, gdp));
49 +
50                 /*
51                  * If we intent to continue, we consider group descritor
52                  * corrupt and update bb_free using bitmap value
53                  */
54                 grp->bb_free = free;
55 +               return -EIO;
56         }
57         mb_set_largest_free_order(sb, grp);
58  
59 @@ -752,6 +757,8 @@ void ext4_mb_generate_buddy(struct super
60         EXT4_SB(sb)->s_mb_buddies_generated++;
61         EXT4_SB(sb)->s_mb_generation_time += period;
62         spin_unlock(&EXT4_SB(sb)->s_bal_lock);
63 +
64 +       return 0;
65  }
66  
67  /* The buddy information is attached the buddy cache inode
68 @@ -898,7 +905,7 @@ static int ext4_mb_init_cache(struct pag
69  
70         err = 0;
71         first_block = page->index * blocks_per_page;
72 -       for (i = 0; i < blocks_per_page; i++) {
73 +       for (i = 0; i < blocks_per_page && err == 0; i++) {
74                 int group;
75  
76                 group = (first_block + i) >> 1;
77 @@ -939,7 +946,7 @@ static int ext4_mb_init_cache(struct pag
78                         ext4_lock_group(sb, group);
79                         /* init the buddy */
80                         memset(data, 0xff, blocksize);
81 -                       ext4_mb_generate_buddy(sb, data, incore, group);
82 +                       err = ext4_mb_generate_buddy(sb, data, incore, group);
83                         ext4_unlock_group(sb, group);
84                         incore = NULL;
85                 } else {
86 @@ -954,7 +961,7 @@ static int ext4_mb_init_cache(struct pag
87                         memcpy(data, bitmap, blocksize);
88  
89                         /* mark all preallocated blks used in in-core bitmap */
90 -                       ext4_mb_generate_from_pa(sb, data, group);
91 +                       err = ext4_mb_generate_from_pa(sb, data, group);
92                         ext4_mb_generate_from_freelist(sb, data, group);
93                         ext4_unlock_group(sb, group);
94  
95 @@ -964,7 +971,8 @@ static int ext4_mb_init_cache(struct pag
96                         incore = data;
97                 }
98         }
99 -       SetPageUptodate(page);
100 +       if (likely(err == 0))
101 +               SetPageUptodate(page);
102  
103  out:
104         if (bh) {
105 @@ -2143,9 +2151,11 @@ static void *ext4_mb_seq_groups_next(str
106  static int ext4_mb_seq_groups_show(struct seq_file *seq, void *v)
107  {
108         struct super_block *sb = seq->private;
109 +       struct ext4_group_desc *gdp;
110         ext4_group_t group = (ext4_group_t) ((unsigned long) v);
111         int i;
112         int err;
113 +       int free = 0;
114         struct ext4_buddy e4b;
115         struct sg {
116                 struct ext4_group_info info;
117 @@ -2154,10 +2164,10 @@ static int ext4_mb_seq_groups_show(struc
118  
119         group--;
120         if (group == 0)
121 -               seq_printf(seq, "#%-5s: %-5s %-5s %-5s "
122 +               seq_printf(seq, "#%-5s: %-5s %-5s %-5s %-5s %-5s"
123                                 "[ %-5s %-5s %-5s %-5s %-5s %-5s %-5s "
124                                   "%-5s %-5s %-5s %-5s %-5s %-5s %-5s ]\n",
125 -                          "group", "free", "frags", "first",
126 +                          "group", "free", "free", "frags", "first", "pa",
127                            "2^0", "2^1", "2^2", "2^3", "2^4", "2^5", "2^6",
128                            "2^7", "2^8", "2^9", "2^10", "2^11", "2^12", "2^13");
129  
130 @@ -2168,13 +2178,20 @@ static int ext4_mb_seq_groups_show(struc
131                 seq_printf(seq, "#%-5u: I/O error\n", group);
132                 return 0;
133         }
134 +
135 +       gdp = ext4_get_group_desc(sb, group, NULL);
136 +       if (gdp != NULL)
137 +               free = ext4_free_blks_count(sb, gdp);
138 +
139         ext4_lock_group(sb, group);
140         memcpy(&sg, ext4_get_group_info(sb, group), i);
141         ext4_unlock_group(sb, group);
142         ext4_mb_unload_buddy(&e4b);
143  
144 -       seq_printf(seq, "#%-5u: %-5u %-5u %-5u [", group, sg.info.bb_free,
145 -                       sg.info.bb_fragments, sg.info.bb_first_free);
146 +       seq_printf(seq, "#%-5lu: %-5u %-5u %-5u %-5u %-5lu [",
147 +                       (long unsigned int)group, sg.info.bb_free, free,
148 +                       sg.info.bb_fragments, sg.info.bb_first_free,
149 +                       sg.info.bb_prealloc_nr);
150         for (i = 0; i <= 13; i++)
151                 seq_printf(seq, " %-5u", i <= sb->s_blocksize_bits + 1 ?
152                                 sg.info.bb_counters[i] : 0);
153 @@ -3411,23 +3428,68 @@ static void ext4_mb_generate_from_freeli
154  }
155  
156  /*
157 + * check free blocks in bitmap match free block in group descriptor
158 + * do this before taking preallocated blocks into account to be able
159 + * to detect on-disk corruptions. The group lock should be hold by the
160 + * caller.
161 + */
162 +int ext4_mb_check_ondisk_bitmap(struct super_block *sb, void *bitmap,
163 +                               struct ext4_group_desc *gdp, int group)
164 +{
165 +       unsigned short max = EXT4_BLOCKS_PER_GROUP(sb);
166 +       unsigned short i, first, free = 0;
167 +
168 +       i = mb_find_next_zero_bit(bitmap, max, 0);
169 +
170 +       while (i < max) {
171 +               first = i;
172 +               i = mb_find_next_bit(bitmap, max, i);
173 +               if (i > max)
174 +                       i = max;
175 +               free += i - first;
176 +               if (i < max)
177 +                       i = mb_find_next_zero_bit(bitmap, max, i);
178 +       }
179 +
180 +       if (free != ext4_free_blks_count(sb, gdp)) {
181 +               ext4_error(sb, "on-disk bitmap for group %d"
182 +                       "corrupted: %u blocks free in bitmap, %u - in gd\n",
183 +                       group, free, ext4_free_blks_count(sb, gdp));
184 +               return -EIO;
185 +       }
186 +       return 0;
187 +}
188 +
189 +/*
190   * the function goes through all preallocation in this group and marks them
191   * used in in-core bitmap. buddy must be generated from this bitmap
192   * Need to be called with ext4 group lock held
193   */
194  static noinline_for_stack
195 -void ext4_mb_generate_from_pa(struct super_block *sb, void *bitmap,
196 +int ext4_mb_generate_from_pa(struct super_block *sb, void *bitmap,
197                                         ext4_group_t group)
198  {
199         struct ext4_group_info *grp = ext4_get_group_info(sb, group);
200         struct ext4_prealloc_space *pa;
201 +       struct ext4_group_desc *gdp;
202         struct list_head *cur;
203         ext4_group_t groupnr;
204         ext4_grpblk_t start;
205         int preallocated = 0;
206         int count = 0;
207 +       int skip = 0;
208 +       int err;
209         int len;
210  
211 +       gdp = ext4_get_group_desc (sb, group, NULL);
212 +       if (gdp == NULL)
213 +               return -EIO;
214 +
215 +       /* before applying preallocations, check bitmap consistency */
216 +       err = ext4_mb_check_ondisk_bitmap(sb, bitmap, gdp, group);
217 +       if (err)
218 +               return err;
219 +
220         /* all form of preallocation discards first load group,
221          * so the only competing code is preallocation use.
222          * we don't need any locking here
223 @@ -3443,14 +3505,23 @@ void ext4_mb_generate_from_pa(struct sup
224                                              &groupnr, &start);
225                 len = pa->pa_len;
226                 spin_unlock(&pa->pa_lock);
227 -               if (unlikely(len == 0))
228 +               if (unlikely(len == 0)) {
229 +                       skip++;
230                         continue;
231 +               }
232                 BUG_ON(groupnr != group);
233                 mb_set_bits(bitmap, start, len);
234                 preallocated += len;
235                 count++;
236         }
237 +       if (count + skip != grp->bb_prealloc_nr) {
238 +               ext4_error(sb, "lost preallocations: "
239 +                          "count %d, bb_prealloc_nr %lu, skip %d\n",
240 +                          count, grp->bb_prealloc_nr, skip);
241 +               return -EIO;
242 +       }
243         mb_debug(1, "prellocated %u for group %u\n", preallocated, group);
244 +       return 0;
245  }
246  
247  static void ext4_mb_pa_callback(struct rcu_head *head)
248 @@ -3509,6 +3580,7 @@ static void ext4_mb_put_pa(struct ext4_a
249          */
250         ext4_lock_group(sb, grp);
251         list_del(&pa->pa_group_list);
252 +       ext4_get_group_info(sb, grp)->bb_prealloc_nr--;
253         ext4_unlock_group(sb, grp);
254  
255         spin_lock(pa->pa_obj_lock);
256 @@ -3600,6 +3672,7 @@ ext4_mb_new_inode_pa(struct ext4_allocat
257  
258         ext4_lock_group(sb, ac->ac_b_ex.fe_group);
259         list_add(&pa->pa_group_list, &grp->bb_prealloc_list);
260 +       grp->bb_prealloc_nr++;
261         ext4_unlock_group(sb, ac->ac_b_ex.fe_group);
262  
263         spin_lock(pa->pa_obj_lock);
264 @@ -3661,6 +3734,7 @@ ext4_mb_new_group_pa(struct ext4_allocat
265  
266         ext4_lock_group(sb, ac->ac_b_ex.fe_group);
267         list_add(&pa->pa_group_list, &grp->bb_prealloc_list);
268 +       grp->bb_prealloc_nr++;
269         ext4_unlock_group(sb, ac->ac_b_ex.fe_group);
270  
271         /*
272 @@ -3829,6 +3903,8 @@ repeat:
273  
274                 spin_unlock(&pa->pa_lock);
275  
276 +               BUG_ON(grp->bb_prealloc_nr == 0);
277 +               grp->bb_prealloc_nr--;
278                 list_del(&pa->pa_group_list);
279                 list_add(&pa->u.pa_tmp_list, &list);
280         }
281 @@ -3962,7 +4038,7 @@ repeat:
282                 if (err) {
283                         ext4_error(sb, "Error loading buddy information for %u",
284                                         group);
285 -                       continue;
286 +                       return;
287                 }
288  
289                 bitmap_bh = ext4_read_block_bitmap(sb, group);
290 @@ -3974,6 +4050,8 @@ repeat:
291                 }
292  
293                 ext4_lock_group(sb, group);
294 +               BUG_ON(e4b.bd_info->bb_prealloc_nr == 0);
295 +               e4b.bd_info->bb_prealloc_nr--;
296                 list_del(&pa->pa_group_list);
297                 ext4_mb_release_inode_pa(&e4b, bitmap_bh, pa);
298                 ext4_unlock_group(sb, group);
299 --- a/fs/ext4/mballoc.h
300 +++ b/fs/ext4/mballoc.h
301 @@ -87,7 +87,7 @@ extern u8 mb_enable_debug;
302  /*
303   * for which requests use 2^N search using buddies
304   */
305 -#define MB_DEFAULT_ORDER2_REQS         2
306 +#define MB_DEFAULT_ORDER2_REQS         8
307  
308  /*
309   * default group prealloc size 512 blocks