Whamcloud - gitweb
LU-17705 ptlrpc: replace synchronize_rcu() with rcu_barrier()
[fs/lustre-release.git] / ldiskfs / kernel_patches / patches / rhel8 / ext4-mballoc-extra-checks.patch
1 commit f2f28f1d09c0a00b3fc569422f881931d857fac9
2 Author:     Alex Zhuravlev <alex.zhuravlev@sun.com>
3 AuthorDate: Tue Oct 28 17:59:09 2008 +0000
4 Subject: ext4: detect on-disk corruption of block bitmap
5
6 Detect on-disk corruption of block bitmap and better checking of
7 preallocated blocks.
8
9 Bugzilla-ID: b=16680
10 Signed-off-by: Alex Zhuravlev <alex.zhuravlev@sun.com>
11 Reviewed-by: Kalpak Shah <kalpak.shah@sun.com>
12 Signed-off-by: Andreas Dilger <andreas.dilger@sun.com>
13
14 Index: linux-4.18.0-80.1.2.el8_0/fs/ext4/ext4.h
15 ===================================================================
16 --- linux-4.18.0-80.1.2.el8_0.orig/fs/ext4/ext4.h
17 +++ linux-4.18.0-80.1.2.el8_0/fs/ext4/ext4.h
18 @@ -2881,6 +2881,7 @@ struct ext4_group_info {
19         ext4_grpblk_t   bb_fragments;   /* nr of freespace fragments */
20         ext4_grpblk_t   bb_largest_free_order;/* order of largest frag in BG */
21         struct          list_head bb_prealloc_list;
22 +       unsigned long   bb_prealloc_nr;
23  #ifdef DOUBLE_CHECK
24         void            *bb_bitmap;
25  #endif
26 Index: linux-4.18.0-80.1.2.el8_0/fs/ext4/mballoc.c
27 ===================================================================
28 --- linux-4.18.0-80.1.2.el8_0.orig/fs/ext4/mballoc.c
29 +++ linux-4.18.0-80.1.2.el8_0/fs/ext4/mballoc.c
30 @@ -352,7 +352,7 @@ static const char * const ext4_groupinfo
31         "ext4_groupinfo_64k", "ext4_groupinfo_128k"
32  };
33  
34 -static void ext4_mb_generate_from_pa(struct super_block *sb, void *bitmap,
35 +static int ext4_mb_generate_from_pa(struct super_block *sb, void *bitmap,
36                                         ext4_group_t group);
37  static void ext4_mb_generate_from_freelist(struct super_block *sb, void *bitmap,
38                                                 ext4_group_t group);
39 @@ -708,7 +708,7 @@ mb_set_largest_free_order(struct super_b
40  }
41  
42  static noinline_for_stack
43 -void ext4_mb_generate_buddy(struct super_block *sb,
44 +int ext4_mb_generate_buddy(struct super_block *sb,
45                                 void *buddy, void *bitmap, ext4_group_t group)
46  {
47         struct ext4_group_info *grp = ext4_get_group_info(sb, group);
48 @@ -752,6 +752,7 @@ void ext4_mb_generate_buddy(struct super
49                 grp->bb_free = free;
50                 ext4_mark_group_bitmap_corrupted(sb, group,
51                                         EXT4_GROUP_INFO_BBITMAP_CORRUPT);
52 +               return -EIO;
53         }
54         mb_set_largest_free_order(sb, grp);
55  
56 @@ -762,6 +763,8 @@ void ext4_mb_generate_buddy(struct super
57         sbi->s_mb_buddies_generated++;
58         sbi->s_mb_generation_time += period;
59         spin_unlock(&sbi->s_bal_lock);
60 +
61 +       return 0;
62  }
63  
64  static void mb_regenerate_buddy(struct ext4_buddy *e4b)
65 @@ -882,7 +885,7 @@ static int ext4_mb_init_cache(struct pag
66         }
67  
68         first_block = page->index * blocks_per_page;
69 -       for (i = 0; i < blocks_per_page; i++) {
70 +       for (i = 0; i < blocks_per_page && err == 0; i++) {
71                 group = (first_block + i) >> 1;
72                 if (group >= ngroups)
73                         break;
74 @@ -926,7 +929,7 @@ static int ext4_mb_init_cache(struct pag
75                         ext4_lock_group(sb, group);
76                         /* init the buddy */
77                         memset(data, 0xff, blocksize);
78 -                       ext4_mb_generate_buddy(sb, data, incore, group);
79 +                       err = ext4_mb_generate_buddy(sb, data, incore, group);
80                         ext4_unlock_group(sb, group);
81                         incore = NULL;
82                 } else {
83 @@ -941,7 +944,7 @@ static int ext4_mb_init_cache(struct pag
84                         memcpy(data, bitmap, blocksize);
85  
86                         /* mark all preallocated blks used in in-core bitmap */
87 -                       ext4_mb_generate_from_pa(sb, data, group);
88 +                       err = ext4_mb_generate_from_pa(sb, data, group);
89                         ext4_mb_generate_from_freelist(sb, data, group);
90                         ext4_unlock_group(sb, group);
91  
92 @@ -951,7 +954,8 @@ static int ext4_mb_init_cache(struct pag
93                         incore = data;
94                 }
95         }
96 -       SetPageUptodate(page);
97 +       if (likely(err == 0))
98 +               SetPageUptodate(page);
99  
100  out:
101         if (bh) {
102 @@ -2280,9 +2284,11 @@ static void *ext4_mb_seq_groups_next(str
103  static int ext4_mb_seq_groups_show(struct seq_file *seq, void *v)
104  {
105         struct super_block *sb = PDE_DATA(file_inode(seq->file));
106 +       struct ext4_group_desc *gdp;
107         ext4_group_t group = (ext4_group_t) ((unsigned long) v);
108         int i;
109         int err, buddy_loaded = 0;
110 +       int free = 0;
111         struct ext4_buddy e4b;
112         struct ext4_group_info *grinfo;
113         unsigned char blocksize_bits = min_t(unsigned char,
114 @@ -2295,7 +2301,7 @@ static int ext4_mb_seq_groups_show(struc
115  
116         group--;
117         if (group == 0)
118 -               seq_puts(seq, "#group: free  frags first ["
119 +               seq_puts(seq, "#group: bfree gfree frags first pa    ["
120                               " 2^0   2^1   2^2   2^3   2^4   2^5   2^6  "
121                               " 2^7   2^8   2^9   2^10  2^11  2^12  2^13  ]\n");
122  
123 @@ -2313,13 +2319,19 @@ static int ext4_mb_seq_groups_show(struc
124                 buddy_loaded = 1;
125         }
126  
127 +       gdp = ext4_get_group_desc(sb, group, NULL);
128 +       if (gdp != NULL)
129 +               free = ext4_free_group_clusters(sb, gdp);
130 +
131         memcpy(&sg, ext4_get_group_info(sb, group), i);
132  
133         if (buddy_loaded)
134                 ext4_mb_unload_buddy(&e4b);
135  
136 -       seq_printf(seq, "#%-5u: %-5u %-5u %-5u [", group, sg.info.bb_free,
137 -                       sg.info.bb_fragments, sg.info.bb_first_free);
138 +       seq_printf(seq, "#%-5lu: %-5u %-5u %-5u %-5u %-5lu [",
139 +                       (long unsigned int)group, sg.info.bb_free, free,
140 +                       sg.info.bb_fragments, sg.info.bb_first_free,
141 +                       sg.info.bb_prealloc_nr);
142         for (i = 0; i <= 13; i++)
143                 seq_printf(seq, " %-5u", i <= blocksize_bits + 1 ?
144                                 sg.info.bb_counters[i] : 0);
145 @@ -3614,22 +3626,71 @@ static void ext4_mb_generate_from_freeli
146  }
147  
148  /*
149 + * check free blocks in bitmap match free block in group descriptor
150 + * do this before taking preallocated blocks into account to be able
151 + * to detect on-disk corruptions. The group lock should be hold by the
152 + * caller.
153 + */
154 +int ext4_mb_check_ondisk_bitmap(struct super_block *sb, void *bitmap,
155 +                               struct ext4_group_desc *gdp, int group)
156 +{
157 +       unsigned short max = EXT4_CLUSTERS_PER_GROUP(sb);
158 +       unsigned short i, first, free = 0;
159 +       unsigned short free_in_gdp = ext4_free_group_clusters(sb, gdp);
160 +
161 +       if (free_in_gdp == 0 && gdp->bg_flags & cpu_to_le16(EXT4_BG_BLOCK_UNINIT))
162 +               return 0;
163 +
164 +       i = mb_find_next_zero_bit(bitmap, max, 0);
165 +
166 +       while (i < max) {
167 +               first = i;
168 +               i = mb_find_next_bit(bitmap, max, i);
169 +               if (i > max)
170 +                       i = max;
171 +               free += i - first;
172 +               if (i < max)
173 +                       i = mb_find_next_zero_bit(bitmap, max, i);
174 +       }
175 +
176 +       if (free != free_in_gdp) {
177 +               ext4_error(sb, "on-disk bitmap for group %d"
178 +                       "corrupted: %u blocks free in bitmap, %u - in gd\n",
179 +                       group, free, free_in_gdp);
180 +               return -EIO;
181 +       }
182 +       return 0;
183 +}
184 +
185 +/*
186   * the function goes through all preallocation in this group and marks them
187   * used in in-core bitmap. buddy must be generated from this bitmap
188   * Need to be called with ext4 group lock held
189   */
190  static noinline_for_stack
191 -void ext4_mb_generate_from_pa(struct super_block *sb, void *bitmap,
192 +int ext4_mb_generate_from_pa(struct super_block *sb, void *bitmap,
193                                         ext4_group_t group)
194  {
195         struct ext4_group_info *grp = ext4_get_group_info(sb, group);
196         struct ext4_prealloc_space *pa;
197 +       struct ext4_group_desc *gdp;
198         struct list_head *cur;
199         ext4_group_t groupnr;
200         ext4_grpblk_t start;
201         int preallocated = 0;
202 +       int skip = 0, count = 0;
203 +       int err;
204         int len;
205  
206 +       gdp = ext4_get_group_desc(sb, group, NULL);
207 +       if (gdp == NULL)
208 +               return -EIO;
209 +
210 +       /* before applying preallocations, check bitmap consistency */
211 +       err = ext4_mb_check_ondisk_bitmap(sb, bitmap, gdp, group);
212 +       if (err)
213 +               return err;
214 +
215         /* all form of preallocation discards first load group,
216          * so the only competing code is preallocation use.
217          * we don't need any locking here
218 @@ -3645,13 +3706,23 @@ void ext4_mb_generate_from_pa(struct sup
219                                              &groupnr, &start);
220                 len = pa->pa_len;
221                 spin_unlock(&pa->pa_lock);
222 -               if (unlikely(len == 0))
223 +               if (unlikely(len == 0)) {
224 +                       skip++;
225                         continue;
226 +               }
227                 BUG_ON(groupnr != group);
228                 ext4_set_bits(bitmap, start, len);
229                 preallocated += len;
230 +               count++;
231 +       }
232 +       if (count + skip != grp->bb_prealloc_nr) {
233 +               ext4_error(sb, "lost preallocations: "
234 +                          "count %d, bb_prealloc_nr %lu, skip %d\n",
235 +                          count, grp->bb_prealloc_nr, skip);
236 +               return -EIO;
237         }
238         mb_debug(1, "preallocated %u for group %u\n", preallocated, group);
239 +       return 0;
240  }
241  
242  static void ext4_mb_pa_callback(struct rcu_head *head)
243 @@ -3715,6 +3786,7 @@ static void ext4_mb_put_pa(struct ext4_a
244          */
245         ext4_lock_group(sb, grp);
246         list_del(&pa->pa_group_list);
247 +       ext4_get_group_info(sb, grp)->bb_prealloc_nr--;
248         ext4_unlock_group(sb, grp);
249  
250         spin_lock(pa->pa_obj_lock);
251 @@ -3809,6 +3881,7 @@ ext4_mb_new_inode_pa(struct ext4_allocat
252  
253         ext4_lock_group(sb, ac->ac_b_ex.fe_group);
254         list_add(&pa->pa_group_list, &grp->bb_prealloc_list);
255 +       grp->bb_prealloc_nr++;
256         ext4_unlock_group(sb, ac->ac_b_ex.fe_group);
257  
258         spin_lock(pa->pa_obj_lock);
259 @@ -3870,6 +3943,7 @@ ext4_mb_new_group_pa(struct ext4_allocat
260  
261         ext4_lock_group(sb, ac->ac_b_ex.fe_group);
262         list_add(&pa->pa_group_list, &grp->bb_prealloc_list);
263 +       grp->bb_prealloc_nr++;
264         ext4_unlock_group(sb, ac->ac_b_ex.fe_group);
265  
266         /*
267 @@ -4041,6 +4115,8 @@ repeat:
268  
269                 spin_unlock(&pa->pa_lock);
270  
271 +               BUG_ON(grp->bb_prealloc_nr == 0);
272 +               grp->bb_prealloc_nr--;
273                 list_del(&pa->pa_group_list);
274                 list_add(&pa->u.pa_tmp_list, &list);
275         }
276 @@ -4171,7 +4247,7 @@ repeat:
277                 if (err) {
278                         ext4_error(sb, "Error %d loading buddy information for %u",
279                                    err, group);
280 -                       continue;
281 +                       return;
282                 }
283  
284                 bitmap_bh = ext4_read_block_bitmap(sb, group);
285 @@ -4184,6 +4260,8 @@ repeat:
286                 }
287  
288                 ext4_lock_group(sb, group);
289 +               BUG_ON(e4b.bd_info->bb_prealloc_nr == 0);
290 +               e4b.bd_info->bb_prealloc_nr--;
291                 list_del(&pa->pa_group_list);
292                 ext4_mb_release_inode_pa(&e4b, bitmap_bh, pa);
293                 ext4_unlock_group(sb, group);
294 @@ -4446,6 +4524,7 @@ ext4_mb_discard_lg_preallocations(struct
295                 }
296                 ext4_lock_group(sb, group);
297                 list_del(&pa->pa_group_list);
298 +               ext4_get_group_info(sb, group)->bb_prealloc_nr--;
299                 ext4_mb_release_group_pa(&e4b, pa);
300                 ext4_unlock_group(sb, group);
301  
302 Index: linux-4.18.0-80.1.2.el8_0/fs/ext4/mballoc.h
303 ===================================================================
304 --- linux-4.18.0-80.1.2.el8_0.orig/fs/ext4/mballoc.h
305 +++ linux-4.18.0-80.1.2.el8_0/fs/ext4/mballoc.h
306 @@ -70,7 +70,7 @@ do {                                                                  \
307  /*
308   * for which requests use 2^N search using buddies
309   */
310 -#define MB_DEFAULT_ORDER2_REQS         2
311 +#define MB_DEFAULT_ORDER2_REQS         8
312  
313  /*
314   * default group prealloc size 512 blocks