On 26.11.19 г. 5:14 ч., Goldwyn Rodrigues wrote: > From: Goldwyn Rodrigues <rgoldwyn@xxxxxxxx> > > 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. > > Signed-off-by: Goldwyn Rodrigues <rgoldwyn@xxxxxxxx> > --- > fs/btrfs/ctree.h | 2 + > fs/btrfs/file.c | 15 ++++- > fs/btrfs/inode.c | 171 ++++++++++++++++++++++++++----------------------------- > 3 files changed, 97 insertions(+), 91 deletions(-) > <snip> > -static int btrfs_get_blocks_direct(struct inode *inode, sector_t iblock, > - struct buffer_head *bh_result, int create) > +static int btrfs_dio_iomap_begin(struct inode *inode, loff_t start, > + loff_t length, unsigned flags, struct iomap *iomap, > + struct iomap *srcmap) > { > struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); > struct extent_map *em; > struct extent_state *cached_state = NULL; > struct btrfs_dio_data *dio_data = NULL; > - u64 start = iblock << inode->i_blkbits; > u64 lockstart, lockend; > - u64 len = bh_result->b_size; > + int create = flags & IOMAP_WRITE; nit: Imo this should be turned into a bool and renamed to write or is_write. Create implies we are always creating blocks which is not true if we are doing overwrite. This has been a misnomer ever since it was introduced. We really care to distinguish read vs write. <snip> > @@ -8636,28 +8637,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 > @@ -8688,17 +8674,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)) { This is the sole reader of BTRFS_INODE_READDIO_NEED_LOCK flag. Have you verified this is correct w.r.t btrfs_setsize. I'm very much in favor or removing the subtle behavior this flag introduced. On the other hand, with iomap we no longer have control over when inode_dio_end is called e.g. inode_dio_begin is called before calling iomap_apply and then it's finished in iomap_dio_complete. Also for DIO reads you now hold the inode lock which is also held during setattr (notify_change calls ->setattr callback and it has a WARN_ON_ONCE(!inode_is_locked(inode)); at the beginning) so perhaps you can simply delete relevant code in btrfs_setattr as well. > - inode_dio_end(inode); > - flags = DIO_LOCKING | DIO_SKIP_HOLES; > - wakeup = false; > } <snip>