From: Ritesh Harjani <riteshh@linux.ibm.com>
To: linux-ext4@vger.kernel.org
Cc: linux-fsdevel@vger.kernel.org, "Theodore Ts'o" <tytso@mit.edu>,
	Jan Kara <jack@suse.cz>,
	Harshad Shirwadkar <harshadshirwadkar@gmail.com>,
	Ritesh Harjani <riteshh@linux.ibm.com>
Subject: [PATCHv1 1/9] ext4: Correct cluster len and clusters changed accounting in ext4_mb_mark_bb
Date: Sat,  5 Feb 2022 19:39:50 +0530	[thread overview]
Message-ID: <f2ab83d92010375afead88a8e41d7a0e1df94a10.1644062450.git.riteshh@linux.ibm.com> (raw)
In-Reply-To: <cover.1644062450.git.riteshh@linux.ibm.com>
ext4_mb_mark_bb() currently wrongly calculates cluster len (clen) and
flex_group->free_clusters. This patch fixes that.
Identified based on code review of ext4_mb_mark_bb() function.
Signed-off-by: Ritesh Harjani <riteshh@linux.ibm.com>
---
 fs/ext4/mballoc.c | 19 ++++++++++++-------
 1 file changed, 12 insertions(+), 7 deletions(-)
diff --git a/fs/ext4/mballoc.c b/fs/ext4/mballoc.c
index c781974df9d0..2f117ce3bb73 100644
--- a/fs/ext4/mballoc.c
+++ b/fs/ext4/mballoc.c
@@ -3899,10 +3899,11 @@ void ext4_mb_mark_bb(struct super_block *sb, ext4_fsblk_t block,
 	struct ext4_sb_info *sbi = EXT4_SB(sb);
 	ext4_group_t group;
 	ext4_grpblk_t blkoff;
-	int i, clen, err;
+	int i, err;
 	int already;
+	unsigned int clen, clen_changed;
 
-	clen = EXT4_B2C(sbi, len);
+	clen = EXT4_NUM_B2C(sbi, len);
 
 	ext4_get_group_no_and_offset(sb, block, &group, &blkoff);
 	bitmap_bh = ext4_read_block_bitmap(sb, group);
@@ -3923,6 +3924,7 @@ void ext4_mb_mark_bb(struct super_block *sb, ext4_fsblk_t block,
 		if (!mb_test_bit(blkoff + i, bitmap_bh->b_data) == !state)
 			already++;
 
+	clen_changed = clen - already;
 	if (state)
 		ext4_set_bits(bitmap_bh->b_data, blkoff, clen);
 	else
@@ -3935,9 +3937,9 @@ void ext4_mb_mark_bb(struct super_block *sb, ext4_fsblk_t block,
 						group, gdp));
 	}
 	if (state)
-		clen = ext4_free_group_clusters(sb, gdp) - clen + already;
+		clen = ext4_free_group_clusters(sb, gdp) - clen_changed;
 	else
-		clen = ext4_free_group_clusters(sb, gdp) + clen - already;
+		clen = ext4_free_group_clusters(sb, gdp) + clen_changed;
 
 	ext4_free_group_clusters_set(sb, gdp, clen);
 	ext4_block_bitmap_csum_set(sb, group, gdp, bitmap_bh);
@@ -3947,10 +3949,13 @@ void ext4_mb_mark_bb(struct super_block *sb, ext4_fsblk_t block,
 
 	if (sbi->s_log_groups_per_flex) {
 		ext4_group_t flex_group = ext4_flex_group(sbi, group);
+		struct flex_groups *fg = sbi_array_rcu_deref(sbi,
+					   s_flex_groups, flex_group);
 
-		atomic64_sub(len,
-			     &sbi_array_rcu_deref(sbi, s_flex_groups,
-						  flex_group)->free_clusters);
+		if (state)
+			atomic64_sub(clen_changed, &fg->free_clusters);
+		else
+			atomic64_add(clen_changed, &fg->free_clusters);
 	}
 
 	err = ext4_handle_dirty_metadata(NULL, NULL, bitmap_bh);
-- 
2.31.1
next prev parent reply	other threads:[~2022-02-05 14:10 UTC|newest]
Thread overview: 19+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-02-05 14:09 [PATCHv1 0/9] ext4: fast_commit fixes, stricter block checking & cleanups Ritesh Harjani
2022-02-05 14:09 ` Ritesh Harjani [this message]
2022-02-07 15:28   ` [PATCHv1 1/9] ext4: Correct cluster len and clusters changed accounting in ext4_mb_mark_bb Jan Kara
2022-02-05 14:09 ` [PATCHv1 2/9] ext4: Fixes ext4_mb_mark_bb() with flex_bg with fast_commit Ritesh Harjani
2022-02-07 16:37   ` Jan Kara
2022-02-08  3:11     ` Ritesh Harjani
2022-02-08  9:59       ` Jan Kara
2022-02-05 14:09 ` [PATCHv1 3/9] ext4: Refactor ext4_free_blocks() to pull out ext4_mb_clear_bb() Ritesh Harjani
2022-02-05 14:09 ` [PATCHv1 4/9] ext4: Use in_range() for range checking in ext4_fc_replay_check_excluded Ritesh Harjani
2022-02-05 14:09 ` [PATCHv1 5/9] ext4: Rename ext4_set_bits to mb_set_bits Ritesh Harjani
2022-02-07 16:38   ` Jan Kara
2022-02-05 14:09 ` [PATCHv1 6/9] ext4: No need to test for block bitmap bits in ext4_mb_mark_bb() Ritesh Harjani
2022-02-05 14:09 ` [PATCHv1 7/9] ext4: Add ext4_sb_block_valid() refactored out of ext4_inode_block_valid() Ritesh Harjani
2022-02-07 16:42   ` Jan Kara
2022-02-08  3:03     ` Ritesh Harjani
2022-02-05 14:09 ` [PATCHv1 8/9] ext4: Add strict range checks while freeing blocks Ritesh Harjani
2022-02-07 16:44   ` Jan Kara
2022-02-05 14:09 ` [PATCHv1 9/9] ext4: Add extra check in ext4_mb_mark_bb() to prevent against possible corruption Ritesh Harjani
2022-02-07 16:45   ` Jan Kara
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox
  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):
  git send-email \
    --in-reply-to=f2ab83d92010375afead88a8e41d7a0e1df94a10.1644062450.git.riteshh@linux.ibm.com \
    --to=riteshh@linux.ibm.com \
    --cc=harshadshirwadkar@gmail.com \
    --cc=jack@suse.cz \
    --cc=linux-ext4@vger.kernel.org \
    --cc=linux-fsdevel@vger.kernel.org \
    --cc=tytso@mit.edu \
    /path/to/YOUR_REPLY
  https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
  Be sure your reply has a Subject: header at the top and a blank line
  before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).