From: Zhang Yi <yi.zhang@huaweicloud.com>
To: linux-ext4@vger.kernel.org
Cc: linux-fsdevel@vger.kernel.org, linux-kernel@vger.kernel.org,
tytso@mit.edu, adilger.kernel@dilger.ca, jack@suse.cz,
yi.zhang@huawei.com, yi.zhang@huaweicloud.com,
chengzhihao1@huawei.com, yukuai3@huawei.com,
yangerkun@huawei.com
Subject: [PATCH v4 06/10] ext4: refactor ext4_collapse_range()
Date: Mon, 16 Dec 2024 09:39:11 +0800 [thread overview]
Message-ID: <20241216013915.3392419-7-yi.zhang@huaweicloud.com> (raw)
In-Reply-To: <20241216013915.3392419-1-yi.zhang@huaweicloud.com>
From: Zhang Yi <yi.zhang@huawei.com>
Simplify ext4_collapse_range() and align its code style with that of
ext4_zero_range() and ext4_punch_hole(). Refactor it by: a) renaming
variables, b) removing redundant input parameter checks and moving
the remaining checks under i_rwsem in preparation for future
refactoring, and c) renaming the three stale error tags.
Signed-off-by: Zhang Yi <yi.zhang@huawei.com>
Reviewed-by: Jan Kara <jack@suse.cz>
---
fs/ext4/extents.c | 103 +++++++++++++++++++++-------------------------
1 file changed, 48 insertions(+), 55 deletions(-)
diff --git a/fs/ext4/extents.c b/fs/ext4/extents.c
index 97ad6fea58d3..8a0a720803a8 100644
--- a/fs/ext4/extents.c
+++ b/fs/ext4/extents.c
@@ -5292,43 +5292,36 @@ static int ext4_collapse_range(struct file *file, loff_t offset, loff_t len)
struct inode *inode = file_inode(file);
struct super_block *sb = inode->i_sb;
struct address_space *mapping = inode->i_mapping;
- ext4_lblk_t punch_start, punch_stop;
+ loff_t end = offset + len;
+ ext4_lblk_t start_lblk, end_lblk;
handle_t *handle;
unsigned int credits;
- loff_t new_size, ioffset;
+ loff_t start, new_size;
int ret;
- /*
- * We need to test this early because xfstests assumes that a
- * collapse range of (0, 1) will return EOPNOTSUPP if the file
- * system does not support collapse range.
- */
- if (!ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS))
- return -EOPNOTSUPP;
+ trace_ext4_collapse_range(inode, offset, len);
- /* Collapse range works only on fs cluster size aligned regions. */
- if (!IS_ALIGNED(offset | len, EXT4_CLUSTER_SIZE(sb)))
- return -EINVAL;
+ inode_lock(inode);
- trace_ext4_collapse_range(inode, offset, len);
+ /* Currently just for extent based files */
+ if (!ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS)) {
+ ret = -EOPNOTSUPP;
+ goto out;
+ }
- punch_start = offset >> EXT4_BLOCK_SIZE_BITS(sb);
- punch_stop = (offset + len) >> EXT4_BLOCK_SIZE_BITS(sb);
+ /* Collapse range works only on fs cluster size aligned regions. */
+ if (!IS_ALIGNED(offset | len, EXT4_CLUSTER_SIZE(sb))) {
+ ret = -EINVAL;
+ goto out;
+ }
- inode_lock(inode);
/*
* There is no need to overlap collapse range with EOF, in which case
* it is effectively a truncate operation
*/
- if (offset + len >= inode->i_size) {
+ if (end >= inode->i_size) {
ret = -EINVAL;
- goto out_mutex;
- }
-
- /* Currently just for extent based files */
- if (!ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS)) {
- ret = -EOPNOTSUPP;
- goto out_mutex;
+ goto out;
}
/* Wait for existing dio to complete */
@@ -5336,7 +5329,7 @@ static int ext4_collapse_range(struct file *file, loff_t offset, loff_t len)
ret = file_modified(file);
if (ret)
- goto out_mutex;
+ goto out;
/*
* Prevent page faults from reinstantiating pages we have released from
@@ -5346,55 +5339,52 @@ static int ext4_collapse_range(struct file *file, loff_t offset, loff_t len)
ret = ext4_break_layouts(inode);
if (ret)
- goto out_mmap;
+ goto out_invalidate_lock;
/*
+ * Write tail of the last page before removed range and data that
+ * will be shifted since they will get removed from the page cache
+ * below. We are also protected from pages becoming dirty by
+ * i_rwsem and invalidate_lock.
* Need to round down offset to be aligned with page size boundary
* for page size > block size.
*/
- ioffset = round_down(offset, PAGE_SIZE);
- /*
- * Write tail of the last page before removed range since it will get
- * removed from the page cache below.
- */
- ret = filemap_write_and_wait_range(mapping, ioffset, offset);
- if (ret)
- goto out_mmap;
- /*
- * Write data that will be shifted to preserve them when discarding
- * page cache below. We are also protected from pages becoming dirty
- * by i_rwsem and invalidate_lock.
- */
- ret = filemap_write_and_wait_range(mapping, offset + len,
- LLONG_MAX);
+ start = round_down(offset, PAGE_SIZE);
+ ret = filemap_write_and_wait_range(mapping, start, offset);
+ if (!ret)
+ ret = filemap_write_and_wait_range(mapping, end, LLONG_MAX);
if (ret)
- goto out_mmap;
- truncate_pagecache(inode, ioffset);
+ goto out_invalidate_lock;
+
+ truncate_pagecache(inode, start);
credits = ext4_writepage_trans_blocks(inode);
handle = ext4_journal_start(inode, EXT4_HT_TRUNCATE, credits);
if (IS_ERR(handle)) {
ret = PTR_ERR(handle);
- goto out_mmap;
+ goto out_invalidate_lock;
}
ext4_fc_mark_ineligible(sb, EXT4_FC_REASON_FALLOC_RANGE, handle);
+ start_lblk = offset >> inode->i_blkbits;
+ end_lblk = (offset + len) >> inode->i_blkbits;
+
down_write(&EXT4_I(inode)->i_data_sem);
ext4_discard_preallocations(inode);
- ext4_es_remove_extent(inode, punch_start, EXT_MAX_BLOCKS - punch_start);
+ ext4_es_remove_extent(inode, start_lblk, EXT_MAX_BLOCKS - start_lblk);
- ret = ext4_ext_remove_space(inode, punch_start, punch_stop - 1);
+ ret = ext4_ext_remove_space(inode, start_lblk, end_lblk - 1);
if (ret) {
up_write(&EXT4_I(inode)->i_data_sem);
- goto out_stop;
+ goto out_handle;
}
ext4_discard_preallocations(inode);
- ret = ext4_ext_shift_extents(inode, handle, punch_stop,
- punch_stop - punch_start, SHIFT_LEFT);
+ ret = ext4_ext_shift_extents(inode, handle, end_lblk,
+ end_lblk - start_lblk, SHIFT_LEFT);
if (ret) {
up_write(&EXT4_I(inode)->i_data_sem);
- goto out_stop;
+ goto out_handle;
}
new_size = inode->i_size - len;
@@ -5402,16 +5392,19 @@ static int ext4_collapse_range(struct file *file, loff_t offset, loff_t len)
EXT4_I(inode)->i_disksize = new_size;
up_write(&EXT4_I(inode)->i_data_sem);
- if (IS_SYNC(inode))
- ext4_handle_sync(handle);
ret = ext4_mark_inode_dirty(handle, inode);
+ if (ret)
+ goto out_handle;
+
ext4_update_inode_fsync_trans(handle, inode, 1);
+ if (IS_SYNC(inode))
+ ext4_handle_sync(handle);
-out_stop:
+out_handle:
ext4_journal_stop(handle);
-out_mmap:
+out_invalidate_lock:
filemap_invalidate_unlock(mapping);
-out_mutex:
+out:
inode_unlock(inode);
return ret;
}
--
2.46.1
next prev parent reply other threads:[~2024-12-16 1:42 UTC|newest]
Thread overview: 35+ messages / expand[flat|nested] mbox.gz Atom feed top
2024-12-16 1:39 [PATCH v4 00/10] ext4: clean up and refactor fallocate Zhang Yi
2024-12-16 1:39 ` [PATCH v4 01/10] ext4: remove writable userspace mappings before truncating page cache Zhang Yi
2024-12-16 15:00 ` Jan Kara
2024-12-17 7:05 ` Zhang Yi
2024-12-16 15:15 ` Matthew Wilcox
2024-12-17 7:38 ` Zhang Yi
2024-12-18 9:56 ` Ojaswin Mujoo
2024-12-18 13:02 ` Zhang Yi
2024-12-19 7:19 ` Ojaswin Mujoo
2024-12-16 1:39 ` [PATCH v4 02/10] ext4: don't explicit update times in ext4_fallocate() Zhang Yi
2024-12-18 9:58 ` Ojaswin Mujoo
2024-12-16 1:39 ` [PATCH v4 03/10] ext4: don't write back data before punch hole in nojournal mode Zhang Yi
2024-12-16 15:02 ` Jan Kara
2024-12-17 14:31 ` Ojaswin Mujoo
2024-12-17 14:50 ` Ojaswin Mujoo
2024-12-18 7:10 ` Zhang Yi
2024-12-18 10:13 ` Ojaswin Mujoo
2024-12-16 1:39 ` [PATCH v4 04/10] ext4: refactor ext4_punch_hole() Zhang Yi
2024-12-16 15:07 ` Jan Kara
2024-12-18 10:17 ` Ojaswin Mujoo
2024-12-18 13:13 ` Zhang Yi
2024-12-19 7:11 ` Ojaswin Mujoo
2024-12-16 1:39 ` [PATCH v4 05/10] ext4: refactor ext4_zero_range() Zhang Yi
2024-12-16 15:24 ` Jan Kara
2024-12-19 7:12 ` Ojaswin Mujoo
2024-12-16 1:39 ` Zhang Yi [this message]
2024-12-18 10:18 ` [PATCH v4 06/10] ext4: refactor ext4_collapse_range() Ojaswin Mujoo
2024-12-16 1:39 ` [PATCH v4 07/10] ext4: refactor ext4_insert_range() Zhang Yi
2024-12-18 10:18 ` Ojaswin Mujoo
2024-12-16 1:39 ` [PATCH v4 08/10] ext4: factor out ext4_do_fallocate() Zhang Yi
2024-12-18 10:18 ` Ojaswin Mujoo
2024-12-16 1:39 ` [PATCH v4 09/10] ext4: move out inode_lock into ext4_fallocate() Zhang Yi
2024-12-18 10:19 ` Ojaswin Mujoo
2024-12-16 1:39 ` [PATCH v4 10/10] ext4: move out common parts " Zhang Yi
2024-12-18 10:20 ` Ojaswin Mujoo
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=20241216013915.3392419-7-yi.zhang@huaweicloud.com \
--to=yi.zhang@huaweicloud.com \
--cc=adilger.kernel@dilger.ca \
--cc=chengzhihao1@huawei.com \
--cc=jack@suse.cz \
--cc=linux-ext4@vger.kernel.org \
--cc=linux-fsdevel@vger.kernel.org \
--cc=linux-kernel@vger.kernel.org \
--cc=tytso@mit.edu \
--cc=yangerkun@huawei.com \
--cc=yi.zhang@huawei.com \
--cc=yukuai3@huawei.com \
/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).