From: Heming Zhao <heming.zhao@suse.com>
To: joseph.qi@linux.alibaba.com
Cc: Heming Zhao <heming.zhao@suse.com>, ocfs2-devel@lists.linux.dev
Subject: [PATCH] ocfs2: fix journal protection issues
Date: Thu, 14 Mar 2024 15:33:53 +0800 [thread overview]
Message-ID: <20240314073353.11489-1-heming.zhao@suse.com> (raw)
This patch resolves 3 journal-related issues:
1. ocfs2_rollback_alloc_dinode_counts() forgets to provide journal
protection for modifying dinode.
2. ocfs2_block_group_alloc() & ocfs2_group_add() forget to provide
journal protection for writing fe->i_size.
3. adjusted journal_dirty scope for ocfs2_update_last_group_and_inode().
This adjustment ensures that only content requiring protection is
included in the journal.
Signed-off-by: Heming Zhao <heming.zhao@suse.com>
---
fs/ocfs2/move_extents.c | 2 +-
fs/ocfs2/resize.c | 7 +++----
fs/ocfs2/suballoc.c | 25 +++++++++++++++++--------
fs/ocfs2/suballoc.h | 3 ++-
4 files changed, 23 insertions(+), 14 deletions(-)
diff --git a/fs/ocfs2/move_extents.c b/fs/ocfs2/move_extents.c
index 1f9ed117e78b..a631f9cf0c05 100644
--- a/fs/ocfs2/move_extents.c
+++ b/fs/ocfs2/move_extents.c
@@ -687,7 +687,7 @@ static int ocfs2_move_extent(struct ocfs2_move_extents_context *context,
ret = ocfs2_block_group_set_bits(handle, gb_inode, gd, gd_bh,
goal_bit, len);
if (ret) {
- ocfs2_rollback_alloc_dinode_counts(gb_inode, gb_bh, len,
+ ocfs2_rollback_alloc_dinode_counts(handle, gb_inode, gb_bh, len,
le16_to_cpu(gd->bg_chain));
mlog_errno(ret);
}
diff --git a/fs/ocfs2/resize.c b/fs/ocfs2/resize.c
index d65d43c61857..2c7e3548ae82 100644
--- a/fs/ocfs2/resize.c
+++ b/fs/ocfs2/resize.c
@@ -143,15 +143,14 @@ static int ocfs2_update_last_group_and_inode(handle_t *handle,
le32_add_cpu(&cr->c_free, -1 * backups);
le32_add_cpu(&fe->id1.bitmap1.i_used, backups);
}
+ le64_add_cpu(&fe->i_size, (u64)new_clusters << osb->s_clustersize_bits);
+ ocfs2_journal_dirty(handle, bm_bh);
spin_lock(&OCFS2_I(bm_inode)->ip_lock);
OCFS2_I(bm_inode)->ip_clusters = le32_to_cpu(fe->i_clusters);
- le64_add_cpu(&fe->i_size, (u64)new_clusters << osb->s_clustersize_bits);
spin_unlock(&OCFS2_I(bm_inode)->ip_lock);
i_size_write(bm_inode, le64_to_cpu(fe->i_size));
- ocfs2_journal_dirty(handle, bm_bh);
-
out_rollback:
if (ret < 0) {
ocfs2_calc_new_backup_super(bm_inode,
@@ -551,12 +550,12 @@ int ocfs2_group_add(struct inode *inode, struct ocfs2_new_group_input *input)
le32_add_cpu(&fe->id1.bitmap1.i_used,
(input->clusters - input->frees) * cl_bpc);
le32_add_cpu(&fe->i_clusters, input->clusters);
+ le64_add_cpu(&fe->i_size, (u64)input->clusters << osb->s_clustersize_bits);
ocfs2_journal_dirty(handle, main_bm_bh);
spin_lock(&OCFS2_I(main_bm_inode)->ip_lock);
OCFS2_I(main_bm_inode)->ip_clusters = le32_to_cpu(fe->i_clusters);
- le64_add_cpu(&fe->i_size, (u64)input->clusters << osb->s_clustersize_bits);
spin_unlock(&OCFS2_I(main_bm_inode)->ip_lock);
i_size_write(main_bm_inode, le64_to_cpu(fe->i_size));
diff --git a/fs/ocfs2/suballoc.c b/fs/ocfs2/suballoc.c
index 166c8918c825..e1168db659df 100644
--- a/fs/ocfs2/suballoc.c
+++ b/fs/ocfs2/suballoc.c
@@ -727,17 +727,16 @@ static int ocfs2_block_group_alloc(struct ocfs2_super *osb,
le16_to_cpu(bg->bg_free_bits_count));
le32_add_cpu(&fe->id1.bitmap1.i_total, le16_to_cpu(bg->bg_bits));
le32_add_cpu(&fe->i_clusters, le16_to_cpu(cl->cl_cpg));
-
+ fe->i_size = cpu_to_le64(ocfs2_clusters_to_bytes(alloc_inode->i_sb,
+ le32_to_cpu(fe->i_clusters)));
ocfs2_journal_dirty(handle, bh);
+ ocfs2_update_inode_fsync_trans(handle, alloc_inode, 0);
spin_lock(&OCFS2_I(alloc_inode)->ip_lock);
OCFS2_I(alloc_inode)->ip_clusters = le32_to_cpu(fe->i_clusters);
- fe->i_size = cpu_to_le64(ocfs2_clusters_to_bytes(alloc_inode->i_sb,
- le32_to_cpu(fe->i_clusters)));
spin_unlock(&OCFS2_I(alloc_inode)->ip_lock);
i_size_write(alloc_inode, le64_to_cpu(fe->i_size));
alloc_inode->i_blocks = ocfs2_inode_sector_count(alloc_inode);
- ocfs2_update_inode_fsync_trans(handle, alloc_inode, 0);
status = 0;
@@ -1601,19 +1600,28 @@ int ocfs2_alloc_dinode_update_counts(struct inode *inode,
return ret;
}
-void ocfs2_rollback_alloc_dinode_counts(struct inode *inode,
+void ocfs2_rollback_alloc_dinode_counts(handle_t *handle,
+ struct inode *inode,
struct buffer_head *di_bh,
u32 num_bits,
u16 chain)
{
+ int ret;
u32 tmp_used;
struct ocfs2_dinode *di = (struct ocfs2_dinode *) di_bh->b_data;
struct ocfs2_chain_list *cl;
+ ret = ocfs2_journal_access_di(handle, INODE_CACHE(inode), di_bh,
+ OCFS2_JOURNAL_ACCESS_WRITE);
+ if (ret < 0) {
+ mlog_errno(ret);
+ return;
+ }
cl = (struct ocfs2_chain_list *)&di->id2.i_chain;
tmp_used = le32_to_cpu(di->id1.bitmap1.i_used);
di->id1.bitmap1.i_used = cpu_to_le32(tmp_used - num_bits);
le32_add_cpu(&cl->cl_recs[chain].c_free, num_bits);
+ ocfs2_journal_dirty(handle, di_bh);
}
static int ocfs2_bg_discontig_fix_by_rec(struct ocfs2_suballoc_result *res,
@@ -1717,7 +1725,8 @@ static int ocfs2_search_one_group(struct ocfs2_alloc_context *ac,
ret = ocfs2_block_group_set_bits(handle, alloc_inode, gd, group_bh,
res->sr_bit_offset, res->sr_bits);
if (ret < 0) {
- ocfs2_rollback_alloc_dinode_counts(alloc_inode, ac->ac_bh,
+ ocfs2_rollback_alloc_dinode_counts(handle,
+ alloc_inode, ac->ac_bh,
res->sr_bits,
le16_to_cpu(gd->bg_chain));
mlog_errno(ret);
@@ -1851,7 +1860,7 @@ static int ocfs2_search_chain(struct ocfs2_alloc_context *ac,
res->sr_bit_offset,
res->sr_bits);
if (status < 0) {
- ocfs2_rollback_alloc_dinode_counts(alloc_inode,
+ ocfs2_rollback_alloc_dinode_counts(handle, alloc_inode,
ac->ac_bh, res->sr_bits, chain);
mlog_errno(status);
goto bail;
@@ -2165,7 +2174,7 @@ int ocfs2_claim_new_inode_at_loc(handle_t *handle,
res->sr_bit_offset,
res->sr_bits);
if (ret < 0) {
- ocfs2_rollback_alloc_dinode_counts(ac->ac_inode,
+ ocfs2_rollback_alloc_dinode_counts(handle, ac->ac_inode,
ac->ac_bh, res->sr_bits, chain);
mlog_errno(ret);
goto out;
diff --git a/fs/ocfs2/suballoc.h b/fs/ocfs2/suballoc.h
index 9c74eace3adc..0c56ddce0752 100644
--- a/fs/ocfs2/suballoc.h
+++ b/fs/ocfs2/suballoc.h
@@ -75,7 +75,8 @@ int ocfs2_alloc_dinode_update_counts(struct inode *inode,
struct buffer_head *di_bh,
u32 num_bits,
u16 chain);
-void ocfs2_rollback_alloc_dinode_counts(struct inode *inode,
+void ocfs2_rollback_alloc_dinode_counts(handle_t *handle,
+ struct inode *inode,
struct buffer_head *di_bh,
u32 num_bits,
u16 chain);
--
2.35.3
next reply other threads:[~2024-03-14 7:34 UTC|newest]
Thread overview: 8+ messages / expand[flat|nested] mbox.gz Atom feed top
2024-03-14 7:33 Heming Zhao [this message]
2024-03-15 1:09 ` [PATCH] ocfs2: fix journal protection issues Joseph Qi
2024-03-15 2:25 ` Heming Zhao
2024-03-15 7:22 ` Joseph Qi
2024-03-15 12:26 ` Heming Zhao
2024-03-18 1:26 ` Joseph Qi
2024-03-18 3:16 ` Heming Zhao
2024-03-18 4:48 ` Joseph Qi
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=20240314073353.11489-1-heming.zhao@suse.com \
--to=heming.zhao@suse.com \
--cc=joseph.qi@linux.alibaba.com \
--cc=ocfs2-devel@lists.linux.dev \
/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 read-only IMAP folder(s) and NNTP newsgroup(s).