Whamcloud - gitweb
LU-9410 ldiskfs: no check mb bitmap if flex_bg enabled
[fs/lustre-release.git] / ldiskfs / kernel_patches / patches / rhel7.3 / ext4-corrupted-inode-block-bitmaps-handling-patches.patch
1 Since we could skip corrupt block groups, this patch
2 use ext4_warning() intead of ext4_error() to make FS not
3 emount RO in default, also fix a leftover from upstream
4 commit 163a203ddb36c36d4a1c942
5 ---
6 Index: linux-stage/fs/ext4/balloc.c
7 ===================================================================
8 --- linux-stage.orig/fs/ext4/balloc.c
9 +++ linux-stage/fs/ext4/balloc.c
10 @@ -185,25 +185,17 @@ static int ext4_init_block_bitmap(struct
11         struct ext4_sb_info *sbi = EXT4_SB(sb);
12         ext4_fsblk_t start, tmp;
13         int flex_bg = 0;
14 -       struct ext4_group_info *grp;
15  
16         J_ASSERT_BH(bh, buffer_locked(bh));
17  
18         /* If checksum is bad mark all blocks used to prevent allocation
19          * essentially implementing a per-group read-only flag. */
20         if (!ext4_group_desc_csum_verify(sb, block_group, gdp)) {
21 -               grp = ext4_get_group_info(sb, block_group);
22 -               if (!EXT4_MB_GRP_BBITMAP_CORRUPT(grp))
23 -                       percpu_counter_sub(&sbi->s_freeclusters_counter,
24 -                                          grp->bb_free);
25 -               set_bit(EXT4_GROUP_INFO_BBITMAP_CORRUPT_BIT, &grp->bb_state);
26 -               if (!EXT4_MB_GRP_IBITMAP_CORRUPT(grp)) {
27 -                       int count;
28 -                       count = ext4_free_inodes_count(sb, gdp);
29 -                       percpu_counter_sub(&sbi->s_freeinodes_counter,
30 -                                          count);
31 -               }
32 -               set_bit(EXT4_GROUP_INFO_IBITMAP_CORRUPT_BIT, &grp->bb_state);
33 +               ext4_corrupted_block_group(sb, block_group,
34 +                               EXT4_GROUP_INFO_BBITMAP_CORRUPT |
35 +                               EXT4_GROUP_INFO_IBITMAP_CORRUPT,
36 +                               "Checksum bad for group %u",
37 +                               block_group);
38                 return -EIO;
39         }
40         memset(bh->b_data, 0, sb->s_blocksize);
41 @@ -368,7 +360,6 @@ static void ext4_validate_block_bitmap(s
42  {
43         ext4_fsblk_t    blk;
44         struct ext4_group_info *grp = ext4_get_group_info(sb, block_group);
45 -       struct ext4_sb_info *sbi = EXT4_SB(sb);
46  
47         if (buffer_verified(bh) || EXT4_MB_GRP_BBITMAP_CORRUPT(grp))
48                 return;
49 @@ -377,22 +368,19 @@ static void ext4_validate_block_bitmap(s
50         blk = ext4_valid_block_bitmap(sb, desc, block_group, bh);
51         if (unlikely(blk != 0)) {
52                 ext4_unlock_group(sb, block_group);
53 -               ext4_error(sb, "bg %u: block %llu: invalid block bitmap",
54 -                          block_group, blk);
55 -               if (!EXT4_MB_GRP_BBITMAP_CORRUPT(grp))
56 -                       percpu_counter_sub(&sbi->s_freeclusters_counter,
57 -                                          grp->bb_free);
58 -               set_bit(EXT4_GROUP_INFO_BBITMAP_CORRUPT_BIT, &grp->bb_state);
59 +               ext4_corrupted_block_group(sb, block_group,
60 +                               EXT4_GROUP_INFO_BBITMAP_CORRUPT,
61 +                               "bg %u: block %llu: invalid block bitmap",
62 +                               block_group, blk);
63                 return;
64         }
65         if (unlikely(!ext4_block_bitmap_csum_verify(sb, block_group,
66                         desc, bh))) {
67                 ext4_unlock_group(sb, block_group);
68 -               ext4_error(sb, "bg %u: bad block bitmap checksum", block_group);
69 -               if (!EXT4_MB_GRP_BBITMAP_CORRUPT(grp))
70 -                       percpu_counter_sub(&sbi->s_freeclusters_counter,
71 -                                          grp->bb_free);
72 -               set_bit(EXT4_GROUP_INFO_BBITMAP_CORRUPT_BIT, &grp->bb_state);
73 +               ext4_corrupted_block_group(sb, block_group,
74 +                               EXT4_GROUP_INFO_BBITMAP_CORRUPT,
75 +                               "bg %u: bad block bitmap checksum",
76 +                               block_group);
77                 return;
78         }
79         set_buffer_verified(bh);
80 @@ -445,8 +433,6 @@ ext4_read_block_bitmap_nowait(struct sup
81                 set_buffer_uptodate(bh);
82                 ext4_unlock_group(sb, block_group);
83                 unlock_buffer(bh);
84 -               if (err)
85 -                       ext4_error(sb, "Checksum bad for grp %u", block_group);
86                 goto verify;
87         }
88         ext4_unlock_group(sb, block_group);
89 Index: linux-stage/fs/ext4/ext4.h
90 ===================================================================
91 --- linux-stage.orig/fs/ext4/ext4.h
92 +++ linux-stage/fs/ext4/ext4.h
93 @@ -91,6 +91,17 @@ typedef __u32 ext4_lblk_t;
94  /* data type for block group number */
95  typedef unsigned int ext4_group_t;
96  
97 +void __ext4_corrupted_block_group(struct super_block *sb,
98 +                                 ext4_group_t group, unsigned int flags,
99 +                                 const char *function, unsigned int line);
100 +
101 +#define ext4_corrupted_block_group(sb, group, flags, fmt, ...)         \
102 +       do {                                                            \
103 +               __ext4_warning(sb, __func__, __LINE__, fmt,             \
104 +                               ##__VA_ARGS__);                         \
105 +               __ext4_corrupted_block_group(sb, group, flags,          \
106 +                                       __func__, __LINE__);            \
107 +       } while (0)
108  /*
109   * Flags used in mballoc's allocation_context flags field.
110   *
111 @@ -2676,7 +2687,11 @@ struct ext4_group_info {
112  #define EXT4_GROUP_INFO_NEED_INIT_BIT          0
113  #define EXT4_GROUP_INFO_WAS_TRIMMED_BIT                1
114  #define EXT4_GROUP_INFO_BBITMAP_CORRUPT_BIT    2
115 +#define EXT4_GROUP_INFO_BBITMAP_CORRUPT                \
116 +       (1 << EXT4_GROUP_INFO_BBITMAP_CORRUPT_BIT)
117  #define EXT4_GROUP_INFO_IBITMAP_CORRUPT_BIT    3
118 +#define EXT4_GROUP_INFO_IBITMAP_CORRUPT                \
119 +       (1 << EXT4_GROUP_INFO_IBITMAP_CORRUPT_BIT)
120  
121  #define EXT4_MB_GRP_NEED_INIT(grp)     \
122         (test_bit(EXT4_GROUP_INFO_NEED_INIT_BIT, &((grp)->bb_state)))
123 Index: linux-stage/fs/ext4/ialloc.c
124 ===================================================================
125 --- linux-stage.orig/fs/ext4/ialloc.c
126 +++ linux-stage/fs/ext4/ialloc.c
127 @@ -70,26 +70,15 @@ static unsigned ext4_init_inode_bitmap(s
128                                        ext4_group_t block_group,
129                                        struct ext4_group_desc *gdp)
130  {
131 -       struct ext4_group_info *grp;
132 -       struct ext4_sb_info *sbi = EXT4_SB(sb);
133         J_ASSERT_BH(bh, buffer_locked(bh));
134  
135         /* If checksum is bad mark all blocks and inodes use to prevent
136          * allocation, essentially implementing a per-group read-only flag. */
137         if (!ext4_group_desc_csum_verify(sb, block_group, gdp)) {
138 -               ext4_error(sb, "Checksum bad for group %u", block_group);
139 -               grp = ext4_get_group_info(sb, block_group);
140 -               if (!EXT4_MB_GRP_BBITMAP_CORRUPT(grp))
141 -                       percpu_counter_sub(&sbi->s_freeclusters_counter,
142 -                                          grp->bb_free);
143 -               set_bit(EXT4_GROUP_INFO_BBITMAP_CORRUPT_BIT, &grp->bb_state);
144 -               if (!EXT4_MB_GRP_IBITMAP_CORRUPT(grp)) {
145 -                       int count;
146 -                       count = ext4_free_inodes_count(sb, gdp);
147 -                       percpu_counter_sub(&sbi->s_freeinodes_counter,
148 -                                          count);
149 -               }
150 -               set_bit(EXT4_GROUP_INFO_IBITMAP_CORRUPT_BIT, &grp->bb_state);
151 +               ext4_corrupted_block_group(sb, block_group,
152 +                               EXT4_GROUP_INFO_BBITMAP_CORRUPT |
153 +                               EXT4_GROUP_INFO_IBITMAP_CORRUPT,
154 +                               "Checksum bad for group %u", block_group);
155                 return 0;
156         }
157  
158 @@ -125,8 +114,6 @@ ext4_read_inode_bitmap(struct super_bloc
159         struct ext4_group_desc *desc;
160         struct buffer_head *bh = NULL;
161         ext4_fsblk_t bitmap_blk;
162 -       struct ext4_group_info *grp;
163 -       struct ext4_sb_info *sbi = EXT4_SB(sb);
164  
165         desc = ext4_get_group_desc(sb, block_group, NULL);
166         if (!desc)
167 @@ -193,16 +180,10 @@ verify:
168                                            EXT4_INODES_PER_GROUP(sb) / 8)) {
169                 ext4_unlock_group(sb, block_group);
170                 put_bh(bh);
171 -               ext4_error(sb, "Corrupt inode bitmap - block_group = %u, "
172 -                          "inode_bitmap = %llu", block_group, bitmap_blk);
173 -               grp = ext4_get_group_info(sb, block_group);
174 -               if (!EXT4_MB_GRP_IBITMAP_CORRUPT(grp)) {
175 -                       int count;
176 -                       count = ext4_free_inodes_count(sb, desc);
177 -                       percpu_counter_sub(&sbi->s_freeinodes_counter,
178 -                                          count);
179 -               }
180 -               set_bit(EXT4_GROUP_INFO_IBITMAP_CORRUPT_BIT, &grp->bb_state);
181 +               ext4_corrupted_block_group(sb, block_group,
182 +                               EXT4_GROUP_INFO_IBITMAP_CORRUPT,
183 +                               "Corrupt inode bitmap - block_group = %u, inode_bitmap = %llu",
184 +                               block_group, bitmap_blk);
185                 return NULL;
186         }
187         ext4_unlock_group(sb, block_group);
188 @@ -337,14 +318,9 @@ out:
189                 if (!fatal)
190                         fatal = err;
191         } else {
192 -               ext4_error(sb, "bit already cleared for inode %lu", ino);
193 -               if (gdp && !EXT4_MB_GRP_IBITMAP_CORRUPT(grp)) {
194 -                       int count;
195 -                       count = ext4_free_inodes_count(sb, gdp);
196 -                       percpu_counter_sub(&sbi->s_freeinodes_counter,
197 -                                          count);
198 -               }
199 -               set_bit(EXT4_GROUP_INFO_IBITMAP_CORRUPT_BIT, &grp->bb_state);
200 +               ext4_corrupted_block_group(sb, block_group,
201 +                               EXT4_GROUP_INFO_IBITMAP_CORRUPT,
202 +                               "bit already cleared for inode %lu", ino);
203         }
204  
205  error_return:
206 Index: linux-stage/fs/ext4/mballoc.c
207 ===================================================================
208 --- linux-stage.orig/fs/ext4/mballoc.c
209 +++ linux-stage/fs/ext4/mballoc.c
210 @@ -752,10 +752,18 @@ int ext4_mb_generate_buddy(struct super_
211         if (free != grp->bb_free) {
212                 struct ext4_group_desc *gdp;
213                 gdp = ext4_get_group_desc(sb, group, NULL);
214 -               ext4_error(sb, "group %lu: %u blocks in bitmap, %u in bb, "
215 -                       "%u in gd, %lu pa's\n", (long unsigned int)group,
216 -                       free, grp->bb_free, ext4_free_group_clusters(sb, gdp),
217 -                       grp->bb_prealloc_nr);
218 +
219 +               ext4_corrupted_block_group(sb, group,
220 +                               EXT4_GROUP_INFO_BBITMAP_CORRUPT,
221 +                               "group %lu: %u blocks in bitmap, %u in bb, %u in gd, %lu pa's block bitmap corrupt",
222 +                               (unsigned long int)group, free, grp->bb_free,
223 +                               ext4_free_group_clusters(sb, gdp),
224 +                               grp->bb_prealloc_nr);
225 +               /*
226 +                * If we intend to continue, we consider group descriptor
227 +                * corrupt and update bb_free using bitmap value
228 +                */
229 +               grp->bb_free = free;
230                 return -EIO;
231         }
232         mb_set_largest_free_order(sb, grp);
233 @@ -1101,7 +1109,7 @@ ext4_mb_load_buddy(struct super_block *s
234         int block;
235         int pnum;
236         int poff;
237 -       struct page *page;
238 +       struct page *page = NULL;
239         int ret;
240         struct ext4_group_info *grp;
241         struct ext4_sb_info *sbi = EXT4_SB(sb);
242 @@ -1127,7 +1135,7 @@ ext4_mb_load_buddy(struct super_block *s
243                  */
244                 ret = ext4_mb_init_group(sb, group);
245                 if (ret)
246 -                       return ret;
247 +                       goto err;
248         }
249  
250         /*
251 @@ -1227,6 +1235,7 @@ err:
252                 page_cache_release(e4b->bd_buddy_page);
253         e4b->bd_buddy = NULL;
254         e4b->bd_bitmap = NULL;
255 +       ext4_warning(sb, "Error loading buddy information for %u", group);
256         return ret;
257  }
258  
259 @@ -3598,9 +3607,11 @@ int ext4_mb_check_ondisk_bitmap(struct s
260         }
261  
262         if (free != free_in_gdp) {
263 -               ext4_error(sb, "on-disk bitmap for group %d"
264 -                       "corrupted: %u blocks free in bitmap, %u - in gd\n",
265 -                       group, free, free_in_gdp);
266 +               ext4_corrupted_block_group(sb, group,
267 +                               EXT4_GROUP_INFO_BBITMAP_CORRUPT,
268 +                               "on-disk bitmap for group %d corrupted: %u blocks free in bitmap, %u - in gd\n",
269 +                               group, free,
270 +                               free_in_gdp);
271                 return -EIO;
272         }
273         return 0;
274 @@ -3961,16 +3972,8 @@ ext4_mb_release_inode_pa(struct ext4_bud
275         /* "free < pa->pa_free" means we maybe double alloc the same blocks,
276          * otherwise maybe leave some free blocks unavailable, no need to BUG.*/
277         if ((free > pa->pa_free && !pa->pa_error) || (free < pa->pa_free)) {
278 -               ext4_error(sb, "pa free mismatch: [pa %p] "
279 -                               "[phy %lu] [logic %lu] [len %u] [free %u] "
280 -                               "[error %u] [inode %lu] [freed %u]", pa,
281 -                               (unsigned long)pa->pa_pstart,
282 -                               (unsigned long)pa->pa_lstart,
283 -                               (unsigned)pa->pa_len, (unsigned)pa->pa_free,
284 -                               (unsigned)pa->pa_error, pa->pa_inode->i_ino,
285 -                               free);
286                 ext4_grp_locked_error(sb, group, 0, 0, "free %u, pa_free %u",
287 -                                       free, pa->pa_free);
288 +                                     free, pa->pa_free);
289                 /*
290                  * pa is already deleted so we use the value obtained
291                  * from the bitmap and continue.
292 @@ -4030,14 +4033,11 @@ ext4_mb_discard_group_preallocations(str
293                 return 0;
294  
295         bitmap_bh = ext4_read_block_bitmap(sb, group);
296 -       if (bitmap_bh == NULL) {
297 -               ext4_error(sb, "Error reading block bitmap for %u", group);
298 +       if (bitmap_bh == NULL)
299                 return 0;
300 -       }
301  
302         err = ext4_mb_load_buddy(sb, group, &e4b);
303         if (err) {
304 -               ext4_error(sb, "Error loading buddy information for %u", group);
305                 put_bh(bitmap_bh);
306                 return 0;
307         }
308 @@ -4197,16 +4197,11 @@ repeat:
309                 group = ext4_get_group_number(sb, pa->pa_pstart);
310  
311                 err = ext4_mb_load_buddy(sb, group, &e4b);
312 -               if (err) {
313 -                       ext4_error(sb, "Error loading buddy information for %u",
314 -                                       group);
315 +               if (err)
316                         return;
317 -               }
318  
319                 bitmap_bh = ext4_read_block_bitmap(sb, group);
320                 if (bitmap_bh == NULL) {
321 -                       ext4_error(sb, "Error reading block bitmap for %u",
322 -                                       group);
323                         ext4_mb_unload_buddy(&e4b);
324                         continue;
325                 }
326 @@ -4466,11 +4461,8 @@ ext4_mb_discard_lg_preallocations(struct
327         list_for_each_entry_safe(pa, tmp, &discard_list, u.pa_tmp_list) {
328  
329                 group = ext4_get_group_number(sb, pa->pa_pstart);
330 -               if (ext4_mb_load_buddy(sb, group, &e4b)) {
331 -                       ext4_error(sb, "Error loading buddy information for %u",
332 -                                       group);
333 +               if (ext4_mb_load_buddy(sb, group, &e4b))
334                         continue;
335 -               }
336                 ext4_lock_group(sb, group);
337                 list_del(&pa->pa_group_list);
338                 ext4_get_group_info(sb, group)->bb_prealloc_nr--;
339 @@ -4741,17 +4733,18 @@ errout:
340                          * been updated or not when fail case. So can
341                          * not revert pa_free back, just mark pa_error*/
342                         pa->pa_error++;
343 -                       ext4_error(sb,
344 -                               "Updating bitmap error: [err %d] "
345 -                               "[pa %p] [phy %lu] [logic %lu] "
346 -                               "[len %u] [free %u] [error %u] "
347 -                               "[inode %lu]", *errp, pa,
348 -                               (unsigned long)pa->pa_pstart,
349 -                               (unsigned long)pa->pa_lstart,
350 -                               (unsigned)pa->pa_len,
351 -                               (unsigned)pa->pa_free,
352 -                               (unsigned)pa->pa_error,
353 -                               pa->pa_inode ? pa->pa_inode->i_ino : 0);
354 +                       ext4_corrupted_block_group(sb, 0, 0,
355 +                                       "Updating bitmap error: [err %d] "
356 +                                       "[pa %p] [phy %lu] [logic %lu] "
357 +                                       "[len %u] [free %u] [error %u] "
358 +                                       "[inode %lu]", *errp, pa,
359 +                                       (unsigned long)pa->pa_pstart,
360 +                                       (unsigned long)pa->pa_lstart,
361 +                                       (unsigned)pa->pa_len,
362 +                                       (unsigned)pa->pa_free,
363 +                                       (unsigned)pa->pa_error,
364 +                                       pa->pa_inode ?
365 +                                       pa->pa_inode->i_ino : 0);
366                 }
367         }
368         ext4_mb_release_context(ac);
369 @@ -5036,7 +5029,7 @@ do_more:
370  
371         err = ext4_mb_load_buddy(sb, block_group, &e4b);
372         if (err)
373 -               goto error_return;
374 +               goto error_brelse;
375  
376         if ((flags & EXT4_FREE_BLOCKS_METADATA) && ext4_handle_valid(handle)) {
377                 struct ext4_free_data *new_entry;
378 @@ -5118,8 +5111,9 @@ do_more:
379                 goto do_more;
380         }
381  error_return:
382 -       brelse(bitmap_bh);
383         ext4_std_error(sb, err);
384 +error_brelse:
385 +       brelse(bitmap_bh);
386         return;
387  }
388  
389 @@ -5215,7 +5209,7 @@ int ext4_group_add_blocks(handle_t *hand
390  
391         err = ext4_mb_load_buddy(sb, block_group, &e4b);
392         if (err)
393 -               goto error_return;
394 +               goto error_brelse;
395  
396         /*
397          * need to update group_info->bb_free and bitmap
398 @@ -5252,8 +5246,9 @@ int ext4_group_add_blocks(handle_t *hand
399                 err = ret;
400  
401  error_return:
402 -       brelse(bitmap_bh);
403         ext4_std_error(sb, err);
404 +error_brelse:
405 +       brelse(bitmap_bh);
406         return err;
407  }
408  
409 @@ -5328,11 +5323,9 @@ ext4_trim_all_free(struct super_block *s
410         trace_ext4_trim_all_free(sb, group, start, max);
411  
412         ret = ext4_mb_load_buddy(sb, group, &e4b);
413 -       if (ret) {
414 -               ext4_error(sb, "Error in loading buddy "
415 -                               "information for %u", group);
416 +       if (ret)
417                 return ret;
418 -       }
419 +
420         bitmap = e4b.bd_bitmap;
421  
422         ext4_lock_group(sb, group);
423 Index: linux-stage/fs/ext4/super.c
424 ===================================================================
425 --- linux-stage.orig/fs/ext4/super.c
426 +++ linux-stage/fs/ext4/super.c
427 @@ -633,6 +633,37 @@ void __ext4_warning(struct super_block *
428         va_end(args);
429  }
430  
431 +void __ext4_corrupted_block_group(struct super_block *sb, ext4_group_t group,
432 +                                 unsigned int flags, const char *function,
433 +                                 unsigned int line)
434 +{
435 +       struct ext4_sb_info *sbi = EXT4_SB(sb);
436 +       struct ext4_group_info *grp = ext4_get_group_info(sb, group);
437 +       struct ext4_group_desc *gdp = ext4_get_group_desc(sb, group, NULL);
438 +
439 +       if (flags & EXT4_GROUP_INFO_BBITMAP_CORRUPT &&
440 +           !EXT4_MB_GRP_BBITMAP_CORRUPT(grp)) {
441 +               percpu_counter_sub(&sbi->s_freeclusters_counter,
442 +                                       grp->bb_free);
443 +               set_bit(EXT4_GROUP_INFO_BBITMAP_CORRUPT_BIT,
444 +                       &grp->bb_state);
445 +       }
446 +
447 +       if (flags & EXT4_GROUP_INFO_IBITMAP_CORRUPT &&
448 +           !EXT4_MB_GRP_IBITMAP_CORRUPT(grp)) {
449 +               if (gdp) {
450 +                       int count;
451 +
452 +                       count = ext4_free_inodes_count(sb, gdp);
453 +                       percpu_counter_sub(&sbi->s_freeinodes_counter,
454 +                                          count);
455 +               }
456 +               set_bit(EXT4_GROUP_INFO_IBITMAP_CORRUPT_BIT,
457 +                       &grp->bb_state);
458 +       }
459 +       save_error_info(sb, function, line);
460 +}
461 +
462  void __ext4_grp_locked_error(const char *function, unsigned int line,
463                              struct super_block *sb, ext4_group_t grp,
464                              unsigned long ino, ext4_fsblk_t block,