btrfs: minor fixes and cleanups on direct IO path
v2 cover btrfs: minor fixes and cleanups on direct IO path
[CHANGELOG]
v2:
- Drop the first 2 patches
The problem with APPEND NOWAIT direct writes needs more consideration. If short writes happened for APPEND NOWAIT write, there are more factors to consider.
Firstly we should return -EGAIN so that the caller can retry in a blocking way.
Secondly we should fully revert what has been written so that isize is not increased.
For the OE conflicts I have not yet hit with upstream, so it should be something wrong in my previous direct locking changes.
There are several small problems inside the direct IO code:
- Duplicated btrfs_dio_data::submitted
The iomap_end() callback has @written parameter which provides the same value.
Fixed in the third patch.
- Badly structured btrfs_dio_iomap_end()
Refactored in the last patch.
Qu Wenruo (2):
btrfs: remove btrfs_dio_data::submitted
btrfs: refactor btrfs_dio_iomap_end()
fs/btrfs/direct-io.c | 131 ++++++++++++++++++++++--------------------- 1 file changed, 66 insertions(+), 65 deletions(-)
Signature · 1 line
2.54.0
PATCH v2 1/2 btrfs: remove btrfs_dio_data::submitted
That member is to record how many bytes are submitted for a direct read/write, utilized by iomap_end() callback to handle short IO cases.
However iomap_end() callback is already providing an internally tracked @written member, which is doing the same accounting and providing the same value as btrfs_dio_data::submitted.
There is no need to duplicate the work, just remove btrfs_dio_data::submitted.
--- fs/btrfs/direct-io.c | 12 ++++-------- 1 file changed, 4 insertions(+), 8 deletions(-)
fs/btrfs/direct-io.c +4 -8
@@ -14,7 +14,6 @@#include "ordered-data.h" struct btrfs_dio_data { -ssize_t submitted;loff_t old_isize;struct extent_changeset *data_reserved;struct btrfs_ordered_extent *ordered;
@@ -619,7 +618,6 @@ static int btrfs_dio_iomap_end(struct inode *inode, loff_t pos, loff_t length, { struct iomap_iter *iter = container_of(iomap, struct iomap_iter, iomap);struct btrfs_dio_data *dio_data = iter->private; -size_t submitted = dio_data->submitted;const bool write = !!(flags &IOMAP_WRITE );int ret =0 ;
@@ -630,9 +628,9 @@ static int btrfs_dio_iomap_end(struct inode *inode, loff_t pos, loff_t length,return 0 ; } -if (submitted < length) { - pos += submitted; - length -= submitted; +if (written < length) { + pos += written; + length -= written;if (write) {/* * Got a short write and have updated the isize, need to
@@ -659,7 +657,7 @@ static int btrfs_dio_iomap_end(struct inode *inode, loff_t pos, loff_t length,if (dio_data->updated_isize) {u64 new_isize; -if (submitted ==0 ) +if (written ==0 ) new_isize = dio_data->old_isize;else new_isize =max (dio_data->old_isize, pos);
@@ -772,8 +770,6 @@ static void btrfs_dio_submit_io(const struct iomap_iter *iter, struct bio *bio, dip->file_offset = file_offset; dip->bytes = bio->bi_iter.bi_size; - dio_data->submitted += bio->bi_iter.bi_size; -/* * Check if we are doing a partial write. If we are, we need to split * the ordered extent to match the submitted bio. Hang on to the
Signature · 1 line
2.54.0
PATCH v2 2/2 btrfs: refactor btrfs_dio_iomap_end()
In reply to Qu WenruoThat function has the following problems:
- Read/write handling scattered across different locations E.g. At the beginning there is a dedicated hole read handling, but later short read handling is at an if() branch.
- Modifying of @pos and @length parameter for short read Although it's completely fine to modify those parameters as they are passed by value, but it can still be confusing to read.
As normally we would assume @pos and @length to be the original range.
But for short IO handling we modify @pos/@length, and completely ignore @written.
- Unnecessary split for ordered extent and changeset handling Both OE and changeset are only for writes, but they are handled in two different if (write) {} blocks.
Refactor the function so that:
- Handling of reads and writes are concentrated in their code block Now the handling of reads are in its own small if () branch.
Leaving the more complex writes handling to take the remaining function, and reduce the indent level.
This also removes all unnecessary "if (write)" checks.
- Do not modify @pos and @length
Let short IO handling to manually calculate the remaining range.
--- fs/btrfs/direct-io.c | 125 ++++++++++++++++++++++--------------------- 1 file changed, 65 insertions(+), 60 deletions(-)
fs/btrfs/direct-io.c +65 -60
@@ -621,74 +621,79 @@ static int btrfs_dio_iomap_end(struct inode *inode, loff_t pos, loff_t length,const bool write = !!(flags &IOMAP_WRITE );int ret =0 ; -if (!write && (iomap->type ==IOMAP_HOLE )) { -/* If reading from a hole, unlock and return */ -btrfs_unlock_dio_extent (&BTRFS_I (inode)->io_tree, pos, - pos + length -1 ,NULL ); +if (!write) { +/* +* Hole read, nothing is submitted, thus we have to unlock +* the whole range. +*/ +if (iomap->type ==IOMAP_HOLE ) { +btrfs_unlock_dio_extent (&BTRFS_I (inode)->io_tree, pos, + pos + length -1 ,NULL ); +return 0 ; + } +/* +* Short read, needs to unlock the remaining range, and +* return -ENOTBLK so we can later fault in the pages and retry. +*/ +if (written < length) { +btrfs_unlock_dio_extent (&BTRFS_I (inode)->io_tree, pos + written, + pos + length -1 ,NULL ); +return -ENOTBLK ; + } +/* The full range is submitted, endio will do the unlock. */ return 0 ; }if (written < length) { - pos += written; - length -= written; -if (write) { -/* -* Got a short write and have updated the isize, need to -* revert the isize change. -* -* Normally we need to update isize with extent lock hold, -* but we're safe due to the following factors: -* -* - Only a single writer can be enlarging isize -* Enlarging isize will take the exclusive inode lock. -* -* - Buffered readers need to wait for the OE we're holding -* Buffered readers will lock extent and wait for OE -* of the folio range, and since page cache is invalidated -* the OE wait can not be skipped. -* -* So here we are safe to revert the isize before -* finishing the OE, and no reader of the remaining range -* can see the enlarged size. -* -* TODO: Extend the DIO_LOCKED lifespan for direct writes, -* and only enlarge isize after a successful write. -*/ -if (dio_data->updated_isize) { -u64 new_isize; +/* +* Got a short write and have updated the isize, need to +* revert the isize change. +* +* Normally we need to update isize with extent lock hold, +* but we're safe due to the following factors: +* +* - Only a single writer can be enlarging isize +* Enlarging isize will take the exclusive inode lock. +* +* - Buffered readers need to wait for the OE we're holding +* Buffered readers will lock extent and wait for OE +* of the folio range, and since page cache is invalidated +* the OE wait can not be skipped. +* +* So here we are safe to revert the isize before +* finishing the OE, and no reader of the remaining range +* can see the enlarged size. +* +* TODO: Extend the DIO_LOCKED lifespan for direct writes, +* and only enlarge isize after a successful write. +*/ +if (dio_data->updated_isize) { +u64 new_isize; -if (written ==0 ) - new_isize = dio_data->old_isize; -else - new_isize =max (dio_data->old_isize, pos); -i_size_write (inode, new_isize); - dio_data->updated_isize = false; - } -/* -* We have a short write, if there is any range -* that is submitted properly, that part will have -* its own OE split from the original one. -* -* So for the OE at dio_data->ordered, it's the part -* that is not submitted, and should be marked -* as fully truncated. -*/ -btrfs_mark_ordered_extent_truncated (dio_data->ordered,0 ); -btrfs_finish_ordered_extent (dio_data->ordered, - pos, length, true); - }else { -btrfs_unlock_dio_extent (&BTRFS_I (inode)->io_tree, pos, - pos + length -1 ,NULL ); +if (written ==0 ) + new_isize = dio_data->old_isize; +else + new_isize =max (dio_data->old_isize, pos + written); +i_size_write (inode, new_isize); + dio_data->updated_isize = false; } +/* +* We have a short write, if there is any range +* that is submitted properly, that part will have +* its own OE split from the original one. +* +* So for the OE at dio_data->ordered, it's the part +* that is not submitted, and should be marked +* as fully truncated. +*/ +btrfs_mark_ordered_extent_truncated (dio_data->ordered,0 ); +btrfs_finish_ordered_extent (dio_data->ordered, + pos + written, length - written, true); ret = -ENOTBLK ; } -if (write) { -btrfs_put_ordered_extent (dio_data->ordered); - dio_data->ordered =NULL ; - } - -if (write) -extent_changeset_free (dio_data->data_reserved); +btrfs_put_ordered_extent (dio_data->ordered); + dio_data->ordered =NULL ; +extent_changeset_free (dio_data->data_reserved);return ret; }
Signature · 1 line
2.54.0
Quoted 24 lines · Qu Wenruo
[CHANGELOG]
v2:
- Drop the first 2 patches
The problem with APPEND NOWAIT direct writes needs more consideration. If short writes happened for APPEND NOWAIT write, there are more factors to consider.Firstly we should return
-EGAINso that the caller can retry in a blocking way.Secondly we should fully revert what has been written so that isize is not increased.
For the OE conflicts I have not yet hit with upstream, so it should be something wrong in my previous direct locking changes.
There are several small problems inside the direct IO code:
- Duplicated
btrfs_dio_data::submitted
Theiomap_end()callback has @written parameter which provides the same value.
Quoted 8 lines
Fixed in the third patch.
- Badly structured
btrfs_dio_iomap_end()
Refactored in the last patch.Qu Wenruo (2):
btrfs: removebtrfs_dio_data::submitted
btrfs: refactorbtrfs_dio_iomap_end()
Thanks.
Quoted 8 lines
fs/btrfs/direct-io.c | 131 ++++++++++++++++++++++--------------------- 1 file changed, 66 insertions(+), 65 deletions(-)--
2.54.0