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
6 Detect on-disk corruption of block bitmap and better checking of
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>
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;
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"
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,
37 static void ext4_mb_generate_from_freelist(struct super_block *sb, void *bitmap,
39 @@ -708,7 +708,7 @@ mb_set_largest_free_order(struct super_b
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)
47 struct ext4_group_info *grp = ext4_get_group_info(sb, group);
48 @@ -752,6 +752,7 @@ void ext4_mb_generate_buddy(struct super
50 ext4_mark_group_bitmap_corrupted(sb, group,
51 EXT4_GROUP_INFO_BBITMAP_CORRUPT);
54 mb_set_largest_free_order(sb, grp);
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);
64 static void mb_regenerate_buddy(struct ext4_buddy *e4b)
65 @@ -882,7 +885,7 @@ static int ext4_mb_init_cache(struct pag
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;
74 @@ -926,7 +929,7 @@ static int ext4_mb_init_cache(struct pag
75 ext4_lock_group(sb, group);
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);
83 @@ -941,7 +944,7 @@ static int ext4_mb_init_cache(struct pag
84 memcpy(data, bitmap, blocksize);
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);
92 @@ -951,7 +954,8 @@ static int ext4_mb_init_cache(struct pag
96 - SetPageUptodate(page);
97 + if (likely(err == 0))
98 + SetPageUptodate(page);
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)
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);
109 int err, buddy_loaded = 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
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");
123 @@ -2313,13 +2319,19 @@ static int ext4_mb_seq_groups_show(struc
127 + gdp = ext4_get_group_desc(sb, group, NULL);
129 + free = ext4_free_group_clusters(sb, gdp);
131 memcpy(&sg, ext4_get_group_info(sb, group), i);
134 ext4_mb_unload_buddy(&e4b);
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
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
154 +int ext4_mb_check_ondisk_bitmap(struct super_block *sb, void *bitmap,
155 + struct ext4_group_desc *gdp, int group)
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);
161 + if (free_in_gdp == 0 && gdp->bg_flags & cpu_to_le16(EXT4_BG_BLOCK_UNINIT))
164 + i = mb_find_next_zero_bit(bitmap, max, 0);
168 + i = mb_find_next_bit(bitmap, max, i);
173 + i = mb_find_next_zero_bit(bitmap, max, i);
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);
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
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,
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;
201 int preallocated = 0;
202 + int skip = 0, count = 0;
206 + gdp = ext4_get_group_desc(sb, group, NULL);
210 + /* before applying preallocations, check bitmap consistency */
211 + err = ext4_mb_check_ondisk_bitmap(sb, bitmap, gdp, group);
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
221 spin_unlock(&pa->pa_lock);
222 - if (unlikely(len == 0))
223 + if (unlikely(len == 0)) {
227 BUG_ON(groupnr != group);
228 ext4_set_bits(bitmap, start, len);
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);
238 mb_debug(1, "preallocated %u for group %u\n", preallocated, group);
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
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);
250 spin_lock(pa->pa_obj_lock);
251 @@ -3809,6 +3881,7 @@ ext4_mb_new_inode_pa(struct ext4_allocat
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);
258 spin_lock(pa->pa_obj_lock);
259 @@ -3870,6 +3943,7 @@ ext4_mb_new_group_pa(struct ext4_allocat
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);
267 @@ -4041,6 +4115,8 @@ repeat:
269 spin_unlock(&pa->pa_lock);
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);
276 @@ -4171,7 +4247,7 @@ repeat:
278 ext4_error(sb, "Error %d loading buddy information for %u",
284 bitmap_bh = ext4_read_block_bitmap(sb, group);
285 @@ -4184,6 +4260,8 @@ repeat:
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
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);
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 { \
308 * for which requests use 2^N search using buddies
310 -#define MB_DEFAULT_ORDER2_REQS 2
311 +#define MB_DEFAULT_ORDER2_REQS 8
314 * default group prealloc size 512 blocks