On Wed, 24 May 2023 at 08:38, Christoph Hellwig <hch@xxxxxx> wrote: > > Add a helper dealing with handling the syncing of a buffered write fallback > for direct I/O. > > Signed-off-by: Christoph Hellwig <hch@xxxxxx> > --- > fs/libfs.c | 36 +++++++++++++++++++++++++ > include/linux/fs.h | 2 ++ > mm/filemap.c | 66 +++++++++++----------------------------------- > 3 files changed, 53 insertions(+), 51 deletions(-) > > diff --git a/fs/libfs.c b/fs/libfs.c > index 89cf614a327158..ad37a49e2ecfb7 100644 > --- a/fs/libfs.c > +++ b/fs/libfs.c > @@ -1613,3 +1613,39 @@ u64 inode_query_iversion(struct inode *inode) > return cur >> I_VERSION_QUERIED_SHIFT; > } > EXPORT_SYMBOL(inode_query_iversion); > + > +ssize_t direct_write_fallback(struct kiocb *iocb, struct iov_iter *iter, > + ssize_t direct_written, ssize_t buffered_written) > +{ > + struct address_space *mapping = iocb->ki_filp->f_mapping; > + loff_t pos = iocb->ki_pos - buffered_written; > + loff_t end = iocb->ki_pos - 1; > + int err; > + > + /* > + * If the buffered write fallback returned an error, we want to return > + * the number of bytes which were written by direct I/O, or the error > + * code if that was zero. > + * > + * Note that this differs from normal direct-io semantics, which will > + * return -EFOO even if some bytes were written. > + */ > + if (unlikely(buffered_written < 0)) > + return buffered_written; Comment/code mismatch. The comment says: if (buffered_written < 0) return direct_written ?: buffered_written; > + > + /* > + * We need to ensure that the page cache pages are written to disk and > + * invalidated to preserve the expected O_DIRECT semantics. > + */ > + err = filemap_write_and_wait_range(mapping, pos, end); > + if (err < 0) { > + /* > + * We don't know how much we wrote, so just return the number of > + * bytes which were direct-written > + */ > + return err; Again comment/code mismatch. Thanks, Miklos