Re: [PATCH 4/8] btrfs: Switch to iomap_dio_rw() for dio

Nikolay Borisov
2019-12-11T10:44:06Z

On 11.12.19 г. 1:01 ч., Goldwyn Rodrigues wrote:
> From: Goldwyn Rodrigues <rgoldwyn@suse.com>
>
> Switch from __blockdev_direct_IO() to iomap_dio_rw().
> Rename btrfs_get_blocks_direct() to btrfs_dio_iomap_begin() and use it
> as iomap_begin() for iomap direct I/O functions. This function
> allocates and locks all the blocks required for the I/O.
> btrfs_submit_direct() is used as the submit_io() hook for direct I/O
> ops.
>
> Since we need direct I/O reads to go through iomap_dio_rw(), we change
> file_operations.read_iter() to a btrfs_file_read_iter() which calls
> btrfs_direct_IO() for direct reads and falls back to
> generic_file_buffered_read() for incomplete reads and buffered reads.
>
> We don't need address_space.direct_IO() anymore so set it to noop.
> Similarly, we don't need flags used in __blockdev_direct_IO(). iomap is
> capable of direct I/O reads from a hole, so we don't need to return
> -ENOENT.
>
> BTRFS direct I/O is now done under i_rwsem, shared in case of
> reads and exclusive in case of writes. This guards against simultaneous
> truncates.
>
> Signed-off-by: Goldwyn Rodrigues <rgoldwyn@suse.com>
> ---
> fs/btrfs/ctree.h | 1 +
> fs/btrfs/file.c | 21 ++++++-
> fs/btrfs/inode.c | 184 ++++++++++++++++++++++++++-----------------------------
> 3 files changed, 107 insertions(+), 99 deletions(-)
>

<snip>

> +
> +/*
> + * btrfs_direct_IO - perform direct I/O
> + * inode->i_rwsem must be locked before calling this function, shared or exclusive.

Instead of having this as a comment which cannot easily be checked
automatically I'd rather it gets codified via lockdep_assert_held. I
believe this is in line with Dave's comment comment on patch 3.

> + * @iocb - kernel iocb
> + * @iter - iter to/from data is copied
> + */
> +
> +ssize_t btrfs_direct_IO(struct kiocb *iocb, struct iov_iter *iter)
> {
> struct file *file = iocb->ki_filp;
> struct inode *inode = file->f_mapping->host;
> @@ -8662,28 +8676,13 @@ static ssize_t btrfs_direct_IO(struct kiocb *iocb, struct iov_iter *iter)
> struct extent_changeset *data_reserved = NULL;
> loff_t offset = iocb->ki_pos;
> size_t count = 0;
> - int flags = 0;
> - bool wakeup = true;
> bool relock = false;
> ssize_t ret;
>
> if (check_direct_IO(fs_info, iter, offset))
> return 0;
>
> - inode_dio_begin(inode);
> -
> - /*
> - * The generic stuff only does filemap_write_and_wait_range, which
> - * isn't enough if we've written compressed pages to this area, so
> - * we need to flush the dirty pages again to make absolutely sure
> - * that any outstanding dirty pages are on disk.
> - */
> count = iov_iter_count(iter);
> - if (test_bit(BTRFS_INODE_HAS_ASYNC_EXTENT,
> - &BTRFS_I(inode)->runtime_flags))
> - filemap_fdatawrite_range(inode->i_mapping, offset,
> - offset + count - 1);
> -
> if (iov_iter_rw(iter) == WRITE) {
> /*
> * If the write DIO is beyond the EOF, we need update
> @@ -8714,17 +8713,11 @@ static ssize_t btrfs_direct_IO(struct kiocb *iocb, struct iov_iter *iter)
> dio_data.unsubmitted_oe_range_end = (u64)offset;
> current->journal_info = &dio_data;
> down_read(&BTRFS_I(inode)->dio_sem);
> - } else if (test_bit(BTRFS_INODE_READDIO_NEED_LOCK,
> - &BTRFS_I(inode)->runtime_flags)) {
> - inode_dio_end(inode);
> - flags = DIO_LOCKING | DIO_SKIP_HOLES;
> - wakeup = false;
> }
>
> - ret = __blockdev_direct_IO(iocb, inode,
> - fs_info->fs_devices->latest_bdev,
> - iter, btrfs_get_blocks_direct, NULL,
> - btrfs_submit_direct, flags);
> + ret = iomap_dio_rw(iocb, iter, &btrfs_dio_iomap_ops, &btrfs_dops,
> + is_sync_kiocb(iocb));
> +
> if (iov_iter_rw(iter) == WRITE) {
> up_read(&BTRFS_I(inode)->dio_sem);
> current->journal_info = NULL;
> @@ -8751,11 +8744,8 @@ static ssize_t btrfs_direct_IO(struct kiocb *iocb, struct iov_iter *iter)
> btrfs_delalloc_release_extents(BTRFS_I(inode), count);
> }
> out:
> - if (wakeup)
> - inode_dio_end(inode);
> if (relock)
> inode_lock(inode);
> -
> extent_changeset_free(data_reserved);
> return ret;
> }
> @@ -11045,7 +11035,7 @@ static const struct address_space_operations btrfs_aops = {
> .writepage = btrfs_writepage,
> .writepages = btrfs_writepages,
> .readpages = btrfs_readpages,
> - .direct_IO = btrfs_direct_IO,
> + .direct_IO = noop_direct_IO,
> .invalidatepage = btrfs_invalidatepage,
> .releasepage = btrfs_releasepage,
> .set_page_dirty = btrfs_set_page_dirty,
>