Whamcloud - gitweb
b=20298 (Merge head ldiskfs and b1_8 ldiskfs)
[fs/lustre-release.git] / ldiskfs / kernel_patches / patches / ext4-mballoc-extra-checks-sles11.patch
1 Index: linux-2.6.27.21-0.1/fs/ext4/mballoc.c
2 ===================================================================
3 --- linux-2.6.27.21-0.1.orig/fs/ext4/mballoc.c
4 +++ linux-2.6.27.21-0.1/fs/ext4/mballoc.c
5 @@ -333,7 +333,7 @@
6  static struct kmem_cache *ext4_pspace_cachep;
7  static struct kmem_cache *ext4_ac_cachep;
8  static struct kmem_cache *ext4_free_ext_cachep;
9 -static void ext4_mb_generate_from_pa(struct super_block *sb, void *bitmap,
10 +static int ext4_mb_generate_from_pa(struct super_block *sb, void *bitmap,
11                                         ext4_group_t group);
12  static void ext4_mb_generate_from_freelist(struct super_block *sb, void *bitmap,
13                                                 ext4_group_t group);
14 @@ -672,7 +672,7 @@ static void ext4_mb_mark_free_simple(str
15         }
16  }
17  
18 -static void ext4_mb_generate_buddy(struct super_block *sb,
19 +static int ext4_mb_generate_buddy(struct super_block *sb,
20                                 void *buddy, void *bitmap, ext4_group_t group)
21  {
22         struct ext4_group_info *grp = ext4_get_group_info(sb, group);
23 @@ -704,14 +704,13 @@ static void ext4_mb_generate_buddy(struc
24         grp->bb_fragments = fragments;
25  
26         if (free != grp->bb_free) {
27 -               ext4_grp_locked_error(sb, group,  __func__,
28 -                       "EXT4-fs: group %u: %u blocks in bitmap, %u in gd\n",
29 -                       group, free, grp->bb_free);
30 -               /*
31 -                * If we intent to continue, we consider group descritor
32 -                * corrupt and update bb_free using bitmap value
33 -                */
34 -               grp->bb_free = free;
35 +               struct ext4_group_desc *gdp;
36 +               gdp = ext4_get_group_desc (sb, group, NULL);
37 +               ext4_grp_locked_error(sb, group, __func__,
38 +                       "group %u: %u blocks in bitmap, %u in bb, "
39 +                       "%u in gd\n", group, free, grp->bb_free,
40 +                       ext4_free_blks_count(sb, gdp));
41 +               return -EIO;
42         }
43  
44         clear_bit(EXT4_GROUP_INFO_NEED_INIT_BIT, &(grp->bb_state));
45 @@ -721,6 +720,8 @@ static void ext4_mb_generate_buddy(struc
46         EXT4_SB(sb)->s_mb_buddies_generated++;
47         EXT4_SB(sb)->s_mb_generation_time += period;
48         spin_unlock(&EXT4_SB(sb)->s_bal_lock);
49 +
50 +       return 0;
51  }
52  
53  /* The buddy information is attached the buddy cache inode
54 @@ -850,7 +851,7 @@ static int ext4_mb_init_cache(struct pag
55         first_block = page->index * blocks_per_page;
56         /* init the page  */
57         memset(page_address(page), 0xff, PAGE_CACHE_SIZE);
58 -       for (i = 0; i < blocks_per_page; i++) {
59 +       for (i = 0; i < blocks_per_page && err == 0; i++) {
60                 int group;
61                 struct ext4_group_info *grinfo;
62  
63 @@ -884,7 +885,7 @@ static int ext4_mb_init_cache(struct pag
64                          * incore got set to the group block bitmap below
65                          */
66                         ext4_lock_group(sb, group);
67 -                       ext4_mb_generate_buddy(sb, data, incore, group);
68 +                       err = ext4_mb_generate_buddy(sb, data, incore, group);
69                         ext4_unlock_group(sb, group);
70                         incore = NULL;
71                 } else {
72 @@ -898,7 +899,7 @@ static int ext4_mb_init_cache(struct pag
73                         memcpy(data, bitmap, blocksize);
74  
75                         /* mark all preallocated blks used in in-core bitmap */
76 -                       ext4_mb_generate_from_pa(sb, data, group);
77 +                       err = ext4_mb_generate_from_pa(sb, data, group);
78                         ext4_mb_generate_from_freelist(sb, data, group);
79                         ext4_unlock_group(sb, group);
80  
81 @@ -908,6 +909,7 @@ static int ext4_mb_init_cache(struct pag
82                         incore = data;
83                 }
84         }
85 +       if (likely(err == 0))
86         SetPageUptodate(page);
87  
88  out:
89 @@ -2217,7 +2219,10 @@ static int ext4_mb_seq_history_show(stru
90                         hs->result.fe_start, hs->result.fe_len);
91                 seq_printf(seq, "%-5u %-8u %-23s free\n",
92                                 hs->pid, hs->ino, buf2);
93 +       } else {
94 +               seq_printf(seq, "unknown op %d\n", hs->op);
95         }
96 +
97         return 0;
98  }
99  
100 @@ -2345,9 +2350,11 @@ static void *ext4_mb_seq_groups_next(str
101  static int ext4_mb_seq_groups_show(struct seq_file *seq, void *v)
102  {
103         struct super_block *sb = seq->private;
104 +       struct ext4_group_desc *gdp;
105         ext4_group_t group = (ext4_group_t) ((unsigned long) v);
106         int i;
107         int err;
108 +       unsigned free = 0;
109         struct ext4_buddy e4b;
110         struct sg {
111                 struct ext4_group_info info;
112 @@ -2356,10 +2363,10 @@ static int ext4_mb_seq_groups_show(struc
113  
114         group--;
115         if (group == 0)
116 -               seq_printf(seq, "#%-5s: %-5s %-5s %-5s "
117 +               seq_printf(seq, "#%-5s: %-5s %-5s %-5s %-5s %-5s"
118                                 "[ %-5s %-5s %-5s %-5s %-5s %-5s %-5s "
119                                   "%-5s %-5s %-5s %-5s %-5s %-5s %-5s ]\n",
120 -                          "group", "free", "frags", "first",
121 +                          "group", "free", "frags", "first", "first", "pa",
122                            "2^0", "2^1", "2^2", "2^3", "2^4", "2^5", "2^6",
123                            "2^7", "2^8", "2^9", "2^10", "2^11", "2^12", "2^13");
124  
125 @@ -2371,12 +2378,18 @@ static int ext4_mb_seq_groups_show(struc
126                 seq_printf(seq, "#%-5u: I/O error\n", group);
127                 return 0;
128         }
129 +
130 +       gdp = ext4_get_group_desc(sb, group, NULL);
131 +       if (gdp != NULL)
132 +               free = ext4_free_blks_count(sb, gdp);
133 +
134         ext4_lock_group(sb, group);
135         memcpy(&sg, ext4_get_group_info(sb, group), i);
136         ext4_unlock_group(sb, group);
137         ext4_mb_release_desc(&e4b);
138  
139 -       seq_printf(seq, "#%-5u: %-5u %-5u %-5u [", group, sg.info.bb_free,
140 +       seq_printf(seq, "#%-5u: %-5u %-5u %-5u %-5u [", group,
141 +                       sg.info.bb_free, free,
142                         sg.info.bb_fragments, sg.info.bb_first_free);
143         for (i = 0; i <= 13; i++)
144                 seq_printf(seq, " %-5u", i <= sb->s_blocksize_bits + 1 ?
145 @@ -2474,6 +2487,7 @@ ext4_mb_store_history(struct ext4_alloca
146         h.tail = ac->ac_tail;
147         h.buddy = ac->ac_buddy;
148         h.merged = 0;
149 +       h.cr = ac->ac_criteria;
150         if (ac->ac_op == EXT4_MB_HISTORY_ALLOC) {
151                 if (ac->ac_g_ex.fe_start == ac->ac_b_ex.fe_start &&
152                                 ac->ac_g_ex.fe_group == ac->ac_b_ex.fe_group)
153 @@ -3695,22 +3709,67 @@ 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, __FUNCTION__, "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 (ext4_lock_group)
193   */
194 -static void ext4_mb_generate_from_pa(struct super_block *sb, void *bitmap,
195 +static int ext4_mb_generate_from_pa(struct super_block *sb, void *bitmap,
196                                         ext4_group_t group)
197  {
198         struct ext4_group_info *grp = ext4_get_group_info(sb, group);
199         struct ext4_prealloc_space *pa;
200 +       struct ext4_group_desc *gdp;
201         struct list_head *cur;
202         ext4_group_t groupnr;
203         ext4_grpblk_t start;
204         int preallocated = 0;
205         int count = 0;
206 +       int skip = 0;
207 +       int err;
208         int len;
209  
210 +       gdp = ext4_get_group_desc (sb, group, NULL);
211 +       if (gdp == NULL)
212 +               return -EIO;
213 +
214 +       /* before applying preallocations, check bitmap consistency */
215 +       err = ext4_mb_check_ondisk_bitmap(sb, bitmap, gdp, group);
216 +       if (err)
217 +               return err;
218 +
219         /* all form of preallocation discards first load group,
220          * so the only competing code is preallocation use.
221          * we don't need any locking here
222 @@ -3720,8 +3778,10 @@ static void ext4_mb_generate_from_pa(str
223                                              &groupnr, &start);
224                 len = pa->pa_len;
225                 spin_unlock(&pa->pa_lock);
226 -               if (unlikely(len == 0))
227 +               if (unlikely(len == 0)) {
228 +                       skip++;
229                         continue;
230 +               }
231                 BUG_ON(groupnr != group);
232                 mb_set_bits(sb_bgl_lock(EXT4_SB(sb), group),
233                                                 bitmap, start, len);
234 @@ -3729,6 +3789,7 @@ static void ext4_mb_generate_from_pa(str
235                 count++;
236         }
237         mb_debug("prellocated %u for group %u\n", preallocated, group);
238 +       return 0;
239  }
240  
241  static void ext4_mb_pa_callback(struct rcu_head *head)
242 @@ -3978,6 +4039,7 @@ ext4_mb_release_inode_pa(struct ext4_bud
243                 ac->ac_sb = sb;
244                 ac->ac_inode = pa->pa_inode;
245                 ac->ac_op = EXT4_MB_HISTORY_DISCARD;
246 +               ac->ac_o_ex.fe_len = 1;
247         }
248  
249         while (bit < end) {
250 @@ -4260,7 +4322,7 @@ repeat:
251                         __release(e4b->alloc_semp);
252                         ext4_error(sb, __func__, "Error in loading buddy "
253                                         "information for %u\n", group);
254 -                       continue;
255 +                       return;
256                 }
257  
258                 bitmap_bh = ext4_read_block_bitmap(sb, group);
259 Index: linux-2.6.27.21-0.1/fs/ext4/mballoc.h
260 ===================================================================
261 --- linux-2.6.27.21-0.1.orig/fs/ext4/mballoc.h
262 +++ linux-2.6.27.21-0.1/fs/ext4/mballoc.h
263 @@ -219,7 +219,7 @@ struct ext4_mb_history {
264         __u16 tail;     /* what tail broke some buddy */
265         __u16 buddy;    /* buddy the tail ^^^ broke */
266         __u16 flags;
267 -       __u8 cr:3;      /* which phase the result extent was found at */
268 +       __u8 cr:8;      /* which phase the result extent was found at */
269         __u8 op:4;
270         __u8 merged:1;
271  };