Skip to contentSkip to conversation

ext4: use ASSERT() to replace J_ASSERT()

PATCH In review Chunguang Xu 2 participants 10 messages Oct 19, 2020 – Oct 20, 2020 ext4 file system

lore
Share & tools
lore
#0 lore raw

PATCH v2 1/8 ext4: use ASSERT() to replace J_ASSERT()

There are currently multiple forms of assertion, such as J_ASSERT(). J_ASEERT is provided for the jbd module, which is a public module. Maybe we should use custom ASSERT() like other file systems, such as xfs, which would be better.

---
 fs/ext4/balloc.c   |  2 +-
 fs/ext4/ext4.h     | 10 ++++++++++
 fs/ext4/fsync.c    |  2 +-
 fs/ext4/indirect.c |  4 ++--
 fs/ext4/inode.c    |  6 +++---
 fs/ext4/namei.c    | 12 ++++--------
 fs/ext4/super.c    |  2 +-
 7 files changed, 22 insertions(+), 16 deletions(-)
fs/ext4/balloc.c +1 -1
index dea738b..386cfc3 100644
@@ -185,7 +185,7 @@ static int ext4_init_block_bitmap(struct super_block *sb,
 	struct ext4_sb_info *sbi = EXT4_SB(sb);
 	ext4_fsblk_t start, tmp;
 
-	J_ASSERT_BH(bh, buffer_locked(bh));
+	ASSERT(buffer_locked(bh));
 
 	/* If checksum is bad mark all blocks used to prevent allocation
 	 * essentially implementing a per-group read-only flag. */
fs/ext4/ext4.h +10 -0
index 250e905..512f833 100644
@@ -98,6 +98,16 @@
 #define ext_debug(ino, fmt, ...)	no_printk(fmt, ##__VA_ARGS__)
 #endif
 
+#define ASSERT(assert)							\
+do {									\
+	if (unlikely(!(assert))) {					\
+		printk(KERN_EMERG					\
+		       "Assertion failure in %s() at %s:%d: \"%s\"\n",	\
+		       __func__, __FILE__, __LINE__, #assert);		\
+		BUG();							\
+	}								\
+} while (0)
+
 /* data type for block offset of block group */
 typedef int ext4_grpblk_t;
 
fs/ext4/fsync.c +1 -1
index 6476994..f9e33c7 100644
@@ -136,7 +136,7 @@ int ext4_sync_file(struct file *file, loff_t start, loff_t end, int datasync)
 	if (unlikely(ext4_forced_shutdown(sbi)))
 		return -EIO;
 
-	J_ASSERT(ext4_journal_current_handle() == NULL);
+	ASSERT(ext4_journal_current_handle() == NULL);
 
 	trace_ext4_sync_file_enter(file, datasync);
 
fs/ext4/indirect.c +2 -2
index 05efa682..1223a18 100644
@@ -534,8 +534,8 @@ int ext4_ind_map_blocks(handle_t *handle, struct inode *inode,
 	ext4_fsblk_t first_block = 0;
 
 	trace_ext4_ind_map_blocks_enter(inode, map->m_lblk, map->m_len, flags);
-	J_ASSERT(!(ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS)));
-	J_ASSERT(handle != NULL || (flags & EXT4_GET_BLOCKS_CREATE) == 0);
+	ASSERT(!(ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS)));
+	ASSERT(handle != NULL || (flags & EXT4_GET_BLOCKS_CREATE) == 0);
 	depth = ext4_block_to_path(inode, map->m_lblk, offsets,
 				   &blocks_to_boundary);
 
fs/ext4/inode.c +3 -3
index 09096fe..8113898 100644
@@ -825,7 +825,7 @@ struct buffer_head *ext4_getblk(handle_t *handle, struct inode *inode,
 	int create = map_flags & EXT4_GET_BLOCKS_CREATE;
 	int err;
 
-	J_ASSERT(handle != NULL || create == 0);
+	ASSERT(handle != NULL || create == 0);
 
 	map.m_lblk = block;
 	map.m_len = 1;
@@ -840,8 +840,8 @@ struct buffer_head *ext4_getblk(handle_t *handle, struct inode *inode,
 	if (unlikely(!bh))
 		return ERR_PTR(-ENOMEM);
 	if (map.m_flags & EXT4_MAP_NEW) {
-		J_ASSERT(create != 0);
-		J_ASSERT(handle != NULL);
+		ASSERT(create != 0);
+		ASSERT(handle != NULL);
 
 		/*
 		 * Now that we do not always journal data, we should
fs/ext4/namei.c +4 -8
index b735372..1cb9424 100644
@@ -182,10 +182,6 @@ static struct buffer_head *__ext4_read_dirblock(struct inode *inode,
 	return bh;
 }
 
-#ifndef assert
-#define assert(test) J_ASSERT(test)
-#endif
-
 #ifdef DX_DEBUG
 #define dxtrace(command) command
 #else
@@ -849,7 +845,7 @@ struct stats dx_show_entries(struct dx_hash_info *hinfo, struct inode *dir,
 					break;
 				}
 			}
-			assert (at == p - 1);
+			ASSERT(at == p - 1);
 		}
 
 		at = p - 1;
@@ -1265,8 +1261,8 @@ static void dx_insert_block(struct dx_frame *frame, u32 hash, ext4_lblk_t block)
 	struct dx_entry *old = frame->at, *new = old + 1;
 	int count = dx_get_count(entries);
 
-	assert(count < dx_get_limit(entries));
-	assert(old < entries + count);
+	ASSERT(count < dx_get_limit(entries));
+	ASSERT(old < entries + count);
 	memmove(new + 1, new, (char *)(entries + count) - (char *)(new));
 	dx_set_hash(new, hash);
 	dx_set_block(new, block);
@@ -2959,7 +2955,7 @@ int ext4_orphan_add(handle_t *handle, struct inode *inode)
 	 * hold i_mutex, or the inode can not be referenced from outside,
 	 * so i_nlink should not be bumped due to race
 	 */
-	J_ASSERT((S_ISREG(inode->i_mode) || S_ISDIR(inode->i_mode) ||
+	ASSERT((S_ISREG(inode->i_mode) || S_ISDIR(inode->i_mode) ||
 		  S_ISLNK(inode->i_mode)) || inode->i_nlink == 0);
 
 	BUFFER_TRACE(sbi->s_sbh, "get_write_access");
fs/ext4/super.c +1 -1
index 9d01318..8aa163a 100644
@@ -1251,7 +1251,7 @@ static void ext4_put_super(struct super_block *sb)
 	 * in-memory list had better be clean by this point. */
 	if (!list_empty(&sbi->s_orphan))
 		dump_orphan_list(sb, sbi);
-	J_ASSERT(list_empty(&sbi->s_orphan));
+	ASSERT(list_empty(&sbi->s_orphan));
 
 	sync_blockdev(sb->s_bdev);
 	invalidate_bdev(sb->s_bdev);
Signature · 1 line
1.8.3.1
#1 lore raw

PATCH v2 2/8 ext4: remove redundant mb_regenerate_buddy()

After this patch (163a203), if an abnormal bitmap is detected, we will mark the group as corrupt, and we will not use this group in the future. Therefore, it should be meaningless to regenerate the buddy bitmap of this group, It might be better to delete it.

---
 fs/ext4/mballoc.c | 19 -------------------
 1 file changed, 19 deletions(-)
fs/ext4/mballoc.c +0 -19
index 74a48d6..03337c8 100644
@@ -822,24 +822,6 @@ void ext4_mb_generate_buddy(struct super_block *sb,
 	spin_unlock(&sbi->s_bal_lock);
 }
 
-static void mb_regenerate_buddy(struct ext4_buddy *e4b)
-{
-	int count;
-	int order = 1;
-	void *buddy;
-
-	while ((buddy = mb_find_buddy(e4b, order++, &count))) {
-		ext4_set_bits(buddy, 0, count);
-	}
-	e4b->bd_info->bb_fragments = 0;
-	memset(e4b->bd_info->bb_counters, 0,
-		sizeof(*e4b->bd_info->bb_counters) *
-		(e4b->bd_sb->s_blocksize_bits + 2));
-
-	ext4_mb_generate_buddy(e4b->bd_sb, e4b->bd_buddy,
-		e4b->bd_bitmap, e4b->bd_group);
-}
-
 /* The buddy information is attached the buddy cache inode
  * for convenience. The information regarding each group
  * is loaded via ext4_mb_load_buddy. The information involve
@@ -1510,7 +1492,6 @@ static void mb_free_blocks(struct inode *inode, struct ext4_buddy *e4b,
 				      block);
 		ext4_mark_group_bitmap_corrupted(sb, e4b->bd_group,
 				EXT4_GROUP_INFO_BBITMAP_CORRUPT);
-		mb_regenerate_buddy(e4b);
 		goto done;
 	}
 
Signature · 1 line
1.8.3.1
#2 lore raw

PATCH v2 3/8 ext4: simplify the code of mb_find_order_for_block

In reply to Chunguang Xu

The code of mb_find_order_for_block is a bit obscure, but we can simplify it with mb_find_buddy(), make the code more concise.

---
 fs/ext4/mballoc.c | 10 +++-------
 1 file changed, 3 insertions(+), 7 deletions(-)
fs/ext4/mballoc.c +3 -7
index 03337c8..56075ce 100644
@@ -1289,22 +1289,18 @@ static void ext4_mb_unload_buddy(struct ext4_buddy *e4b)
 
 static int mb_find_order_for_block(struct ext4_buddy *e4b, int block)
 {
-	int order = 1;
-	int bb_incr = 1 << (e4b->bd_blkbits - 1);
+	int order = 1, max;
 	void *bb;
 
 	BUG_ON(e4b->bd_bitmap == e4b->bd_buddy);
 	BUG_ON(block >= (1 << (e4b->bd_blkbits + 3)));
 
-	bb = e4b->bd_buddy;
 	while (order <= e4b->bd_blkbits + 1) {
-		block = block >> 1;
-		if (!mb_test_bit(block, bb)) {
+		bb = mb_find_buddy(e4b, order, &max);
+		if (!mb_test_bit(block >> order, bb)) {
 			/* this block is part of buddy of order 'order' */
 			return order;
 		}
-		bb += bb_incr;
-		bb_incr >>= 1;
 		order++;
 	}
 	return 0;
Signature · 1 line
1.8.3.1
#3 lore raw

PATCH v2 4/8 ext4: add the gdt block of meta_bg to system_zone

In reply to Chunguang Xu

In order to avoid poor search efficiency of system_zone, the system only adds metadata of some sparse group to system_zone. In the meta_bg scenario, the non-sparse group may contain gdt blocks. Perhaps we should add these blocks to system_zone to improve fault tolerance without significantly reducing system performance.

---
 fs/ext4/block_validity.c | 18 +++++++++++-------
 1 file changed, 11 insertions(+), 7 deletions(-)
fs/ext4/block_validity.c +11 -7
index 8e6ca23..37025e3 100644
@@ -218,6 +218,7 @@ int ext4_setup_system_zone(struct super_block *sb)
 	struct ext4_group_desc *gdp;
 	ext4_group_t i;
 	int flex_size = ext4_flex_bg_size(sbi);
+	int gd_blks;
 	int ret;
 
 	system_blks = kzalloc(sizeof(*system_blks), GFP_KERNEL);
@@ -226,13 +227,16 @@ int ext4_setup_system_zone(struct super_block *sb)
 
 	for (i=0; i < ngroups; i++) {
 		cond_resched();
-		if (ext4_bg_has_super(sb, i) &&
-		    ((i < 5) || ((i % flex_size) == 0))) {
-			ret = add_system_zone(system_blks,
-					ext4_group_first_block_no(sb, i),
-					ext4_bg_num_gdb(sb, i) + 1, 0);
-			if (ret)
-				goto err;
+		if ((i < 5) || ((i % flex_size) == 0)) {
+			gd_blks = ext4_bg_has_super(sb, i) +
+				ext4_bg_num_gdb(sb, i);
+			if (gd_blks) {
+				ret = add_system_zone(system_blks,
+						ext4_group_first_block_no(sb, i),
+						gd_blks, 0);
+				if (ret)
+					goto err;
+			}
 		}
 		gdp = ext4_get_group_desc(sb, i, NULL);
 		ret = add_system_zone(system_blks,
Signature · 1 line
1.8.3.1
#4 lore raw

PATCH v2 5/8 ext4: update ext4_data_block_valid related comments

In reply to Chunguang Xu

From: Chunguang Xu <[email protected]>

Since ext4_data_block_valid() has been renamed to ext4_inode_block_valid(), the related comments need to be updated.

---
 fs/ext4/block_validity.c | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)
fs/ext4/block_validity.c +3 -3
index 37025e3..07e9dc3 100644
@@ -206,7 +206,7 @@ static void ext4_destroy_system_zone(struct rcu_head *rcu)
  *
  * The update of system_blks pointer in this function is protected by
  * sb->s_umount semaphore. However we have to be careful as we can be
- * racing with ext4_data_block_valid() calls reading system_blks rbtree
+ * racing with ext4_inode_block_valid() calls reading system_blks rbtree
  * protected only by RCU. That's why we first build the rbtree and then
  * swap it in place.
  */
@@ -262,7 +262,7 @@ int ext4_setup_system_zone(struct super_block *sb)
 
 	/*
 	 * System blks rbtree complete, announce it once to prevent racing
-	 * with ext4_data_block_valid() accessing the rbtree at the same
+	 * with ext4_inode_block_valid() accessing the rbtree at the same
 	 * time.
 	 */
 	rcu_assign_pointer(sbi->s_system_blks, system_blks);
@@ -282,7 +282,7 @@ int ext4_setup_system_zone(struct super_block *sb)
  *
  * The update of system_blks pointer in this function is protected by
  * sb->s_umount semaphore. However we have to be careful as we can be
- * racing with ext4_data_block_valid() calls reading system_blks rbtree
+ * racing with ext4_inode_block_valid() calls reading system_blks rbtree
  * protected only by RCU. So we first clear the system_blks pointer and
  * then free the rbtree only after RCU grace period expires.
  */
Signature · 1 line
1.8.3.1
#5 lore raw

PATCH v2 6/8 ext4: add a helper function to validate metadata block

In reply to Chunguang Xu

There is a need to check whether a block or a segment overlaps with metadata, since information of system_zone is incomplete, we need a more accurate function. Now we check whether it overlaps with block bitmap, inode bitmap, and inode table. Perhaps it is better to add a check of super_block and block group descriptor and provide a helper function.

---
 fs/ext4/ext4.h    |  5 ++++-
 fs/ext4/mballoc.c | 57 +++++++++++++++++++++++++++++++++++++++++++------------
 2 files changed, 49 insertions(+), 13 deletions(-)
fs/ext4/ext4.h +4 -1
index 512f833..1ea700d 100644
@@ -2701,7 +2701,10 @@ extern ext4_group_t ext4_mb_prefetch(struct super_block *sb,
 				     unsigned int nr, int *cnt);
 extern void ext4_mb_prefetch_fini(struct super_block *sb, ext4_group_t group,
 				  unsigned int nr);
-
+extern int ext4_metadata_block_overlaps(struct super_block *sb,
+					ext4_group_t block_group,
+					ext4_fsblk_t block,
+					unsigned long count);
 extern void ext4_free_blocks(handle_t *handle, struct inode *inode,
 			     struct buffer_head *bh, ext4_fsblk_t block,
 			     unsigned long count, int flags);
fs/ext4/mballoc.c +45 -12

75 lines not on this page · raw message

Signature · 1 line
1.8.3.1
#6 lore raw

PATCH v2 7/8 ext4: delete invalid code inside ext4_xattr_block_set()

In reply to Chunguang Xu

Delete invalid code inside ext4_xattr_block_set().

---
 fs/ext4/xattr.c | 1 -
 1 file changed, 1 deletion(-)
fs/ext4/xattr.c +0 -1
index cba4b87..56728f3 100644
@@ -1927,7 +1927,6 @@ struct ext4_xattr_block_find {
 	} else {
 		/* Allocate a buffer where we construct the new block. */
 		s->base = kzalloc(sb->s_blocksize, GFP_NOFS);
-		/* assert(header == s->base) */
 		error = -ENOMEM;
 		if (s->base == NULL)
 			goto cleanup;
Signature · 1 line
1.8.3.1
#7 lore raw

PATCH v2 8/8 ext4: fix a memory leak of ext4_free_data

In reply to Chunguang Xu

From: Chunguang Xu <[email protected]>

When freeing metadata, we will create an ext4_free_data and insert it into the pending free list. After the current transaction is committed, the object will be freed.

ext4_mb_free_metadata() will check whether the area to be freed overlaps with the pending free list. If true, return directly. At this time, ext4_free_data is leaked. Fortunately, the probability of this problem is relatively small, maybe we should fix this problem.

---
 fs/ext4/mballoc.c | 1 +
 1 file changed, 1 insertion(+)
fs/ext4/mballoc.c +1 -0
index e0a4265..aa6732a 100644
@@ -5015,6 +5015,7 @@ static void ext4_try_merge_freed_extent(struct ext4_sb_info *sbi,
 				ext4_group_first_block_no(sb, group) +
 				EXT4_C2B(sbi, cluster),
 				"Block already on to-be-freed list");
+			kmem_cache_free(ext4_free_data_cachep, new_entry);
 			return 0;
 		}
 	}
Signature · 1 line
1.8.3.1
Reviewed-by on v2 1/8
#8 lore raw
In reply to Chunguang Xu
Quoted 3 lines · Chunguang Xu

There are currently multiple forms of assertion, such as J_ASSERT(). J_ASEERT is provided for the jbd module, which is a public module.

(typo) "J_ASSERT()"

Quoted 2 lines

Maybe we should use custom ASSERT() like other file systems, such as xfs, which would be better.

My one minor complaint is that "ASSERT()" is a very generic name and is likely to cause conflicts with ASSERT in other headers. That said, I also see many other filesystems using their own ASSERT() macro, so I guess they are all in private headers only?

Some minor comments/questions below, but not worth changing the patch unless you think they are important...

You can add:

fs/ext4/balloc.c static int ext4_init_block_bitmap(struct super_block *sb, · line 188

Quoted 6 lines
fs/ext4/balloc.c +1 -1
@@ -185,7 +185,7 @@ static int ext4_init_block_bitmap(struct super_block *sb,
 	struct ext4_sb_info *sbi = EXT4_SB(sb);
 	ext4_fsblk_t start, tmp;
 
-	J_ASSERT_BH(bh, buffer_locked(bh));
+	ASSERT(buffer_locked(bh));

I thought J_ASSERT_BH() did something useful, but J_ASSERT_BH() just maps to J_ASSERT() internally anyway.

fs/ext4/ext4.h line 105

Quoted 5 lines
fs/ext4/ext4.h +5 -0
+#define ASSERT(assert)							\
+do {									\
+	if (unlikely(!(assert))) {					\
+		printk(KERN_EMERG					\
+		       "Assertion failure in %s() at %s:%d: \"%s\"\n",	\

(style) better to use single quotes '%s' to avoid the need to escape \".

Cheers, Andreas

author
#9 lore raw

Thanks for your reply.

Andreas Dilger wrote on 2020/10/20 11:37:

Quoted 6 lines · Chunguang Xu
Quoted 3 lines

There are currently multiple forms of assertion, such as J_ASSERT(). J_ASEERT is provided for the jbd module, which is a public module.

(typo) "J_ASSERT()"

Thanks, I will Fixed that.

Quoted 7 lines
Quoted 2 lines

Maybe we should use custom ASSERT() like other file systems, such as xfs, which would be better.

My one minor complaint is that "ASSERT()" is a very generic name and is likely to cause conflicts with ASSERT in other headers. That said, I also see many other filesystems using their own ASSERT() macro, so I guess they are all in private headers only?

I also thought about this before, but even if we define it in a private header file, because we still include several header files in a certain file, it seems that the conflict cannot be resolved. However, maybe it is safest to use a name with ext4 prefix. I will try to fix it in next version. Thanks.

Quoted 17 lines

Some minor comments/questions below, but not worth changing the patch unless you think they are important...

You can add:

fs/ext4/balloc.c static int ext4_init_block_bitmap(struct super_block *sb, · line 188

Quoted 6 lines
fs/ext4/balloc.c +1 -1
@@ -185,7 +185,7 @@ static int ext4_init_block_bitmap(struct super_block *sb,
 	struct ext4_sb_info *sbi = EXT4_SB(sb);
 	ext4_fsblk_t start, tmp;
 
-	J_ASSERT_BH(bh, buffer_locked(bh));
+	ASSERT(buffer_locked(bh));

I thought J_ASSERT_BH() did something useful, but J_ASSERT_BH() just maps to J_ASSERT() internally anyway.

Quoted 7 lines

fs/ext4/ext4.h line 105

Quoted 5 lines
fs/ext4/ext4.h +5 -0
+#define ASSERT(assert)							\
+do {									\
+	if (unlikely(!(assert))) {					\
+		printk(KERN_EMERG					\
+		       "Assertion failure in %s() at %s:%d: \"%s\"\n",	\

(style) better to use single quotes '%s' to avoid the need to escape \".

Thanks, this is a good suggestion, I will fix it next version.

Quoted 6 lines

Cheers, Andreas