hc
2024-02-20 102a0743326a03cd1a1202ceda21e175b7d3575c
kernel/fs/ext4/balloc.c
....@@ -303,6 +303,36 @@
303303 return desc;
304304 }
305305
306
+static ext4_fsblk_t ext4_valid_block_bitmap_padding(struct super_block *sb,
307
+ ext4_group_t block_group,
308
+ struct buffer_head *bh)
309
+{
310
+ ext4_grpblk_t next_zero_bit;
311
+ unsigned long bitmap_size = sb->s_blocksize * 8;
312
+ unsigned int offset = num_clusters_in_group(sb, block_group);
313
+
314
+ if (bitmap_size <= offset)
315
+ return 0;
316
+
317
+ next_zero_bit = ext4_find_next_zero_bit(bh->b_data, bitmap_size, offset);
318
+
319
+ return (next_zero_bit < bitmap_size ? next_zero_bit : 0);
320
+}
321
+
322
+struct ext4_group_info *ext4_get_group_info(struct super_block *sb,
323
+ ext4_group_t group)
324
+{
325
+ struct ext4_group_info **grp_info;
326
+ long indexv, indexh;
327
+
328
+ if (unlikely(group >= EXT4_SB(sb)->s_groups_count))
329
+ return NULL;
330
+ indexv = group >> (EXT4_DESC_PER_BLOCK_BITS(sb));
331
+ indexh = group & ((EXT4_DESC_PER_BLOCK(sb)) - 1);
332
+ grp_info = sbi_array_rcu_deref(EXT4_SB(sb), s_group_info, indexv);
333
+ return grp_info[indexh];
334
+}
335
+
306336 /*
307337 * Return the block number which was discovered to be invalid, or 0 if
308338 * the block bitmap is valid.
....@@ -377,7 +407,7 @@
377407
378408 if (buffer_verified(bh))
379409 return 0;
380
- if (EXT4_MB_GRP_BBITMAP_CORRUPT(grp))
410
+ if (!grp || EXT4_MB_GRP_BBITMAP_CORRUPT(grp))
381411 return -EFSCORRUPTED;
382412
383413 ext4_lock_group(sb, block_group);
....@@ -399,6 +429,15 @@
399429 block_group, blk);
400430 ext4_mark_group_bitmap_corrupted(sb, block_group,
401431 EXT4_GROUP_INFO_BBITMAP_CORRUPT);
432
+ return -EFSCORRUPTED;
433
+ }
434
+ blk = ext4_valid_block_bitmap_padding(sb, block_group, bh);
435
+ if (unlikely(blk != 0)) {
436
+ ext4_unlock_group(sb, block_group);
437
+ ext4_error(sb, "bg %u: block %llu: padding at end of block bitmap is not set",
438
+ block_group, blk);
439
+ ext4_mark_group_bitmap_corrupted(sb, block_group,
440
+ EXT4_GROUP_INFO_BBITMAP_CORRUPT);
402441 return -EFSCORRUPTED;
403442 }
404443 set_buffer_verified(bh);
....@@ -864,11 +903,11 @@
864903 }
865904
866905 /*
867
- * This function returns the number of file system metadata clusters at
906
+ * This function returns the number of file system metadata blocks at
868907 * the beginning of a block group, including the reserved gdt blocks.
869908 */
870
-static unsigned ext4_num_base_meta_clusters(struct super_block *sb,
871
- ext4_group_t block_group)
909
+unsigned int ext4_num_base_meta_blocks(struct super_block *sb,
910
+ ext4_group_t block_group)
872911 {
873912 struct ext4_sb_info *sbi = EXT4_SB(sb);
874913 unsigned num;
....@@ -886,8 +925,15 @@
886925 } else { /* For META_BG_BLOCK_GROUPS */
887926 num += ext4_bg_num_gdb(sb, block_group);
888927 }
889
- return EXT4_NUM_B2C(sbi, num);
928
+ return num;
890929 }
930
+
931
+static unsigned int ext4_num_base_meta_clusters(struct super_block *sb,
932
+ ext4_group_t block_group)
933
+{
934
+ return EXT4_NUM_B2C(EXT4_SB(sb), ext4_num_base_meta_blocks(sb, block_group));
935
+}
936
+
891937 /**
892938 * ext4_inode_to_goal_block - return a hint for block allocation
893939 * @inode: inode for block allocation