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,
ritesh.list@gmail.com, yi.zhang@huawei.com,
yi.zhang@huaweicloud.com, chengzhihao1@huawei.com,
yukuai3@huawei.com
Subject: [PATCH v2 10/10] ext4: factor out a common helper to lock and flush data before fallocate
Date: Wed, 4 Sep 2024 14:29:25 +0800 [thread overview]
Message-ID: <20240904062925.716856-11-yi.zhang@huaweicloud.com> (raw)
In-Reply-To: <20240904062925.716856-1-yi.zhang@huaweicloud.com>
From: Zhang Yi <yi.zhang@huawei.com>
Now the beginning of the first four functions in ext4_fallocate() (punch
hole, zero range, insert range and collapse range) are almost the same,
they need to wait for the dio to finish, get filemap invalidate lock,
write back dirty data and finally drop page cache. Factor out a common
helper to do these work can reduce a lot of the redundant code.
Signed-off-by: Zhang Yi <yi.zhang@huawei.com>
---
fs/ext4/ext4.h | 2 +
fs/ext4/extents.c | 125 ++++++++++++++++++++--------------------------
fs/ext4/inode.c | 25 +---------
3 files changed, 57 insertions(+), 95 deletions(-)
diff --git a/fs/ext4/ext4.h b/fs/ext4/ext4.h
index e8d7965f62c4..281fab9abc42 100644
--- a/fs/ext4/ext4.h
+++ b/fs/ext4/ext4.h
@@ -3696,6 +3696,8 @@ extern int ext4_ext_remove_space(struct inode *inode, ext4_lblk_t start,
ext4_lblk_t end);
extern void ext4_ext_init(struct super_block *);
extern void ext4_ext_release(struct super_block *);
+extern int ext4_prepare_falloc(struct file *file, loff_t start, loff_t end,
+ int mode);
extern long ext4_fallocate(struct file *file, int mode, loff_t offset,
loff_t len);
extern int ext4_convert_unwritten_extents(handle_t *handle, struct inode *inode,
diff --git a/fs/ext4/extents.c b/fs/ext4/extents.c
index 91e509201915..eee63e92dcc6 100644
--- a/fs/ext4/extents.c
+++ b/fs/ext4/extents.c
@@ -4558,34 +4558,10 @@ static long ext4_zero_range(struct file *file, loff_t offset,
return ret;
}
- /* Wait all existing dio workers, newcomers will block on i_rwsem */
- inode_dio_wait(inode);
-
- ret = file_modified(file);
+ ret = ext4_prepare_falloc(file, offset, end - 1, FALLOC_FL_ZERO_RANGE);
if (ret)
return ret;
- /*
- * Prevent page faults from reinstantiating pages we have released
- * from page cache.
- */
- filemap_invalidate_lock(mapping);
-
- ret = ext4_break_layouts(inode);
- if (ret)
- goto out_invalidate_lock;
-
- /*
- * Write data that will be zeroed to preserve them when successfully
- * discarding page cache below but fail to convert extents.
- */
- ret = filemap_write_and_wait_range(mapping, offset, end - 1);
- if (ret)
- goto out_invalidate_lock;
-
- /* Now release the pages and zero block aligned part of pages */
- truncate_pagecache_range(inode, offset, end - 1);
-
flags = EXT4_GET_BLOCKS_CREATE_UNWRIT_EXT;
/* Preallocate the range including the unaligned edges */
if (offset & (blocksize - 1) || end & (blocksize - 1)) {
@@ -4731,6 +4707,52 @@ static int ext4_fallocate_check(struct inode *inode, int mode,
return 0;
}
+int ext4_prepare_falloc(struct file *file, loff_t start, loff_t end, int mode)
+{
+ struct inode *inode = file_inode(file);
+ struct address_space *mapping = inode->i_mapping;
+ int ret;
+
+ /* Wait all existing dio workers, newcomers will block on i_rwsem */
+ inode_dio_wait(inode);
+ ret = file_modified(file);
+ if (ret)
+ return ret;
+
+ /*
+ * Prevent page faults from reinstantiating pages we have released
+ * from page cache.
+ */
+ filemap_invalidate_lock(mapping);
+
+ ret = ext4_break_layouts(inode);
+ if (ret)
+ goto failed;
+
+ /*
+ * Write data that will be zeroed to preserve them when successfully
+ * discarding page cache below but fail to convert extents.
+ */
+ ret = filemap_write_and_wait_range(mapping, start, end);
+ if (ret)
+ goto failed;
+
+ /*
+ * For insert range and collapse range, COWed private pages should
+ * be removed since the file's logical offset will be changed, but
+ * punch hole and zero range doesn't.
+ */
+ if (mode & (FALLOC_FL_INSERT_RANGE | FALLOC_FL_COLLAPSE_RANGE))
+ truncate_pagecache(inode, start);
+ else
+ truncate_pagecache_range(inode, start, end);
+
+ return 0;
+failed:
+ filemap_invalidate_unlock(mapping);
+ return ret;
+}
+
/*
* preallocate space for a file. This implements ext4's fallocate file
* operation, which gets called from sys_fallocate system call.
@@ -5284,39 +5306,20 @@ static int ext4_collapse_range(struct file *file, loff_t offset, loff_t len)
ext4_lblk_t start_lblk, end_lblk;
handle_t *handle;
unsigned int credits;
- loff_t start, new_size;
+ loff_t new_size;
int ret;
trace_ext4_collapse_range(inode, offset, len);
WARN_ON_ONCE(!inode_is_locked(inode));
- /* Wait for existing dio to complete */
- inode_dio_wait(inode);
-
- ret = file_modified(file);
- if (ret)
- return ret;
-
- /*
- * Prevent page faults from reinstantiating pages we have released from
- * page cache.
- */
- filemap_invalidate_lock(mapping);
-
- ret = ext4_break_layouts(inode);
- if (ret)
- goto out_invalidate_lock;
-
/*
* Need to round down offset to be aligned with page size boundary
* for page size > block size.
*/
- start = round_down(offset, PAGE_SIZE);
- /* Write out all dirty pages */
- ret = filemap_write_and_wait_range(mapping, start, LLONG_MAX);
+ ret = ext4_prepare_falloc(file, round_down(offset, PAGE_SIZE),
+ LLONG_MAX, FALLOC_FL_COLLAPSE_RANGE);
if (ret)
- goto out_invalidate_lock;
- truncate_pagecache(inode, start);
+ return ret;
credits = ext4_writepage_trans_blocks(inode);
handle = ext4_journal_start(inode, EXT4_HT_TRUNCATE, credits);
@@ -5386,38 +5389,18 @@ static int ext4_insert_range(struct file *file, loff_t offset, loff_t len)
ext4_lblk_t start_lblk, len_lblk, ee_start_lblk = 0;
unsigned int credits, ee_len;
int ret, depth, split_flag = 0;
- loff_t start;
trace_ext4_insert_range(inode, offset, len);
WARN_ON_ONCE(!inode_is_locked(inode));
- /* Wait for existing dio to complete */
- inode_dio_wait(inode);
-
- ret = file_modified(file);
- if (ret)
- return ret;
-
- /*
- * Prevent page faults from reinstantiating pages we have released from
- * page cache.
- */
- filemap_invalidate_lock(mapping);
-
- ret = ext4_break_layouts(inode);
- if (ret)
- goto out_invalidate_lock;
-
/*
* Need to round down to align start offset to page size boundary
* for page size > block size.
*/
- start = round_down(offset, PAGE_SIZE);
- /* Write out all dirty pages */
- ret = filemap_write_and_wait_range(mapping, start, LLONG_MAX);
+ ret = ext4_prepare_falloc(file, round_down(offset, PAGE_SIZE),
+ LLONG_MAX, FALLOC_FL_INSERT_RANGE);
if (ret)
- goto out_invalidate_lock;
- truncate_pagecache(inode, start);
+ return ret;
credits = ext4_writepage_trans_blocks(inode);
handle = ext4_journal_start(inode, EXT4_HT_TRUNCATE, credits);
diff --git a/fs/ext4/inode.c b/fs/ext4/inode.c
index 57636c656fa5..4b7f8fcaa5c2 100644
--- a/fs/ext4/inode.c
+++ b/fs/ext4/inode.c
@@ -3953,33 +3953,10 @@ int ext4_punch_hole(struct file *file, loff_t offset, loff_t length)
return ret;
}
- /* Wait all existing dio workers, newcomers will block on i_rwsem */
- inode_dio_wait(inode);
-
- ret = file_modified(file);
+ ret = ext4_prepare_falloc(file, offset, end - 1, FALLOC_FL_PUNCH_HOLE);
if (ret)
return ret;
- /*
- * Prevent page faults from reinstantiating pages we have released from
- * page cache.
- */
- filemap_invalidate_lock(mapping);
-
- ret = ext4_break_layouts(inode);
- if (ret)
- goto out_invalidate_lock;
-
- /* Write out all dirty pages to avoid race conditions */
- if (mapping_tagged(mapping, PAGECACHE_TAG_DIRTY)) {
- ret = filemap_write_and_wait_range(mapping, offset, end - 1);
- if (ret)
- goto out_invalidate_lock;
- }
-
- /* Now release the pages and zero block aligned part of pages*/
- truncate_pagecache_range(inode, offset, end - 1);
-
if (ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS))
credits = ext4_writepage_trans_blocks(inode);
else
--
2.39.2
next prev parent reply other threads:[~2024-09-04 6:31 UTC|newest]
Thread overview: 28+ messages / expand[flat|nested] mbox.gz Atom feed top
2024-09-04 6:29 [PATCH v2 00/10] ext4: clean up and refactor fallocate Zhang Yi
2024-09-04 6:29 ` [PATCH v2 01/10] ext4: write out dirty data before dropping pages Zhang Yi
2024-09-17 16:50 ` Jan Kara
2024-09-18 12:27 ` Zhang Yi
2024-09-04 6:29 ` [PATCH v2 02/10] ext4: don't explicit update times in ext4_fallocate() Zhang Yi
2024-09-20 16:04 ` Jan Kara
2024-09-04 6:29 ` [PATCH v2 03/10] ext4: drop ext4_update_disksize_before_punch() Zhang Yi
2024-09-20 16:13 ` Jan Kara
2024-09-24 7:43 ` Zhang Yi
2024-09-24 10:11 ` Jan Kara
2024-09-24 11:09 ` Zhang Yi
2024-09-04 6:29 ` [PATCH v2 04/10] ext4: refactor ext4_zero_range() Zhang Yi
2024-09-20 16:24 ` Jan Kara
2024-09-04 6:29 ` [PATCH v2 05/10] ext4: refactor ext4_punch_hole() Zhang Yi
2024-09-20 16:31 ` Jan Kara
2024-09-04 6:29 ` [PATCH v2 06/10] ext4: refactor ext4_collapse_range() Zhang Yi
2024-09-20 16:35 ` Jan Kara
2024-09-04 6:29 ` [PATCH v2 07/10] ext4: refactor ext4_insert_range() Zhang Yi
2024-09-23 8:17 ` Jan Kara
2024-09-04 6:29 ` [PATCH v2 08/10] ext4: factor out ext4_do_fallocate() Zhang Yi
2024-09-23 8:20 ` Jan Kara
2024-09-04 6:29 ` [PATCH v2 09/10] ext4: factor out the common checking part of all fallocate operations Zhang Yi
2024-09-23 8:31 ` Jan Kara
2024-09-24 7:52 ` Zhang Yi
2024-09-04 6:29 ` Zhang Yi [this message]
2024-09-23 8:54 ` [PATCH v2 10/10] ext4: factor out a common helper to lock and flush data before fallocate Jan Kara
2024-09-24 8:11 ` Zhang Yi
2024-09-24 10:05 ` 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=20240904062925.716856-11-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=ritesh.list@gmail.com \
--cc=tytso@mit.edu \
--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).