From: Christoph Hellwig <hch@lst.de>
To: Matthew Wilcox <willy@infradead.org>
Cc: "Darrick J. Wong" <djwong@kernel.org>,
linux-mm@kvack.org, Andreas Gruenbacher <agruenba@redhat.com>,
Miklos Szeredi <miklos@szeredi.hu>,
cluster-devel@redhat.com, Ilya Dryomov <idryomov@gmail.com>,
linux-ext4@vger.kernel.org, linux-nfs@vger.kernel.org,
linux-block@vger.kernel.org, Damien Le Moal <dlemoal@kernel.org>,
Alexander Viro <viro@zeniv.linux.org.uk>,
Jaegeuk Kim <jaegeuk@kernel.org>,
ceph-devel@vger.kernel.org, Xiubo Li <xiubli@redhat.com>,
Trond Myklebust <trond.myklebust@hammerspace.com>,
Jens Axboe <axboe@kernel.dk>,
Christian Brauner <brauner@kernel.org>,
Theodore Ts'o <tytso@mit.edu>,
"open list:F2FS FILE SYSTEM"
<linux-f2fs-devel@lists.sourceforge.net>,
linux-xfs@vger.kernel.org, Anna Schumaker <anna@kernel.org>,
linux-fsdevel@vger.kernel.org,
Andrew Morton <akpm@linux-foundation.org>
Subject: [f2fs-dev] [PATCH 10/13] fs: factor out a direct_write_fallback helper
Date: Fri, 19 May 2023 11:35:18 +0200 [thread overview]
Message-ID: <20230519093521.133226-11-hch@lst.de> (raw)
In-Reply-To: <20230519093521.133226-1-hch@lst.de>
Add a helper dealing with handling the syncing of a buffered write fallback
for direct I/O.
Signed-off-by: Christoph Hellwig <hch@lst.de>
---
fs/libfs.c | 36 ++++++++++++++++++++++++++++
include/linux/fs.h | 2 ++
mm/filemap.c | 59 ++++++++++------------------------------------
3 files changed, 50 insertions(+), 47 deletions(-)
diff --git a/fs/libfs.c b/fs/libfs.c
index 89cf614a327158..9f3791fc6e0715 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, end;
+ 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;
+
+ /*
+ * We need to ensure that the page cache pages are written to disk and
+ * invalidated to preserve the expected O_DIRECT semantics.
+ */
+ end = pos + buffered_written - 1;
+ 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;
+ }
+ invalidate_mapping_pages(mapping, pos >> PAGE_SHIFT, end >> PAGE_SHIFT);
+ return direct_written + buffered_written;
+}
+EXPORT_SYMBOL_GPL(direct_write_fallback);
diff --git a/include/linux/fs.h b/include/linux/fs.h
index e4efc1792a877a..576a945db178ef 100644
--- a/include/linux/fs.h
+++ b/include/linux/fs.h
@@ -2738,6 +2738,8 @@ extern ssize_t __generic_file_write_iter(struct kiocb *, struct iov_iter *);
extern ssize_t generic_file_write_iter(struct kiocb *, struct iov_iter *);
extern ssize_t generic_file_direct_write(struct kiocb *, struct iov_iter *);
ssize_t generic_perform_write(struct kiocb *, struct iov_iter *);
+ssize_t direct_write_fallback(struct kiocb *iocb, struct iov_iter *iter,
+ ssize_t direct_written, ssize_t buffered_written);
ssize_t vfs_iter_read(struct file *file, struct iov_iter *iter, loff_t *ppos,
rwf_t flags);
diff --git a/mm/filemap.c b/mm/filemap.c
index c1b988199aece5..875b2108d0a05f 100644
--- a/mm/filemap.c
+++ b/mm/filemap.c
@@ -4008,25 +4008,21 @@ ssize_t __generic_file_write_iter(struct kiocb *iocb, struct iov_iter *from)
{
struct file *file = iocb->ki_filp;
struct address_space *mapping = file->f_mapping;
- struct inode *inode = mapping->host;
- ssize_t written = 0;
- ssize_t err;
- ssize_t status;
+ struct inode *inode = mapping->host;
+ ssize_t ret;
/* We can write back this queue in page reclaim */
current->backing_dev_info = inode_to_bdi(inode);
- err = file_remove_privs(file);
- if (err)
+ ret = file_remove_privs(file);
+ if (ret)
goto out;
- err = file_update_time(file);
- if (err)
+ ret = file_update_time(file);
+ if (ret)
goto out;
if (iocb->ki_flags & IOCB_DIRECT) {
- loff_t pos, endbyte;
-
- written = generic_file_direct_write(iocb, from);
+ ret = generic_file_direct_write(iocb, from);
/*
* If the write stopped short of completing, fall back to
* buffered writes. Some filesystems do this for writes to
@@ -4034,46 +4030,15 @@ ssize_t __generic_file_write_iter(struct kiocb *iocb, struct iov_iter *from)
* not succeed (even if it did, DAX does not handle dirty
* page-cache pages correctly).
*/
- if (written < 0 || !iov_iter_count(from) || IS_DAX(inode))
- goto out;
-
- pos = iocb->ki_pos;
- status = generic_perform_write(iocb, from);
- /*
- * If generic_perform_write() returned a synchronous error
- * then we want to return the number of bytes which were
- * direct-written, 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(status < 0)) {
- err = status;
- goto out;
- }
- /*
- * We need to ensure that the page cache pages are written to
- * disk and invalidated to preserve the expected O_DIRECT
- * semantics.
- */
- endbyte = pos + status - 1;
- err = filemap_write_and_wait_range(mapping, pos, endbyte);
- if (err == 0) {
- written += status;
- invalidate_mapping_pages(mapping,
- pos >> PAGE_SHIFT,
- endbyte >> PAGE_SHIFT);
- } else {
- /*
- * We don't know how much we wrote, so just return
- * the number of bytes which were direct-written
- */
- }
+ if (ret >= 0 && iov_iter_count(from) && !IS_DAX(inode))
+ ret = direct_write_fallback(iocb, from, ret,
+ generic_perform_write(iocb, from));
} else {
- written = generic_perform_write(iocb, from);
+ ret = generic_perform_write(iocb, from);
}
out:
current->backing_dev_info = NULL;
- return written ? written : err;
+ return ret;
}
EXPORT_SYMBOL(__generic_file_write_iter);
--
2.39.2
_______________________________________________
Linux-f2fs-devel mailing list
Linux-f2fs-devel@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/linux-f2fs-devel
next prev parent reply other threads:[~2023-05-19 9:36 UTC|newest]
Thread overview: 38+ messages / expand[flat|nested] mbox.gz Atom feed top
2023-05-19 9:35 [f2fs-dev] cleanup the filemap / direct I/O interaction Christoph Hellwig
2023-05-19 9:35 ` [f2fs-dev] [PATCH 01/13] iomap: update ki_pos a little later in iomap_dio_complete Christoph Hellwig
2023-05-21 23:40 ` Damien Le Moal
2023-07-06 0:18 ` patchwork-bot+f2fs
2023-05-19 9:35 ` [f2fs-dev] [PATCH 02/13] filemap: update ki_pos in generic_perform_write Christoph Hellwig
2023-05-21 23:42 ` Damien Le Moal
2023-05-22 2:20 ` Xiubo Li
2023-05-19 9:35 ` [f2fs-dev] [PATCH 03/13] filemap: assign current->backing_dev_info " Christoph Hellwig
2023-05-21 23:44 ` Damien Le Moal
2023-05-22 2:22 ` Xiubo Li
2023-05-19 9:35 ` [f2fs-dev] [PATCH 04/13] filemap: add a kiocb_write_and_wait helper Christoph Hellwig
2023-05-21 23:46 ` Damien Le Moal
2023-05-19 9:35 ` [f2fs-dev] [PATCH 05/13] filemap: add a kiocb_invalidate_pages helper Christoph Hellwig
2023-05-21 23:51 ` Damien Le Moal
2023-05-19 9:35 ` [f2fs-dev] [PATCH 06/13] filemap: add a kiocb_invalidate_post_write helper Christoph Hellwig
2023-05-21 23:56 ` Damien Le Moal
2023-05-23 16:01 ` Christoph Hellwig
2023-05-19 9:35 ` [f2fs-dev] [PATCH 07/13] iomap: update ki_pos in iomap_file_buffered_write Christoph Hellwig
2023-05-22 0:01 ` Damien Le Moal
2023-05-23 16:02 ` Christoph Hellwig
2023-05-19 9:35 ` [f2fs-dev] [PATCH 08/13] iomap: assign current->backing_dev_info " Christoph Hellwig
2023-05-22 0:05 ` Damien Le Moal
2023-05-23 1:06 ` Darrick J. Wong
2023-05-23 3:30 ` Matthew Wilcox
2023-05-23 16:02 ` Christoph Hellwig
2023-05-19 9:35 ` [f2fs-dev] [PATCH 09/13] iomap: use kiocb_write_and_wait and kiocb_invalidate_pages Christoph Hellwig
2023-05-22 0:07 ` Damien Le Moal
2023-05-19 9:35 ` Christoph Hellwig [this message]
2023-05-22 0:16 ` [f2fs-dev] [PATCH 10/13] fs: factor out a direct_write_fallback helper Damien Le Moal
2023-05-22 14:19 ` Miklos Szeredi via Linux-f2fs-devel
2023-05-23 16:03 ` Christoph Hellwig
2023-05-19 9:35 ` [f2fs-dev] [PATCH 11/13] fuse: update ki_pos in fuse_perform_write Christoph Hellwig
2023-05-22 0:18 ` Damien Le Moal
2023-05-19 9:35 ` [f2fs-dev] [PATCH 12/13] fuse: drop redundant arguments to fuse_perform_write Christoph Hellwig
2023-05-22 0:19 ` Damien Le Moal
2023-05-19 9:35 ` [f2fs-dev] [PATCH 13/13] fuse: use direct_write_fallback Christoph Hellwig
2023-05-22 0:20 ` Damien Le Moal
2023-05-23 1:12 ` [f2fs-dev] cleanup the filemap / direct I/O interaction Darrick J. Wong
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=20230519093521.133226-11-hch@lst.de \
--to=hch@lst.de \
--cc=agruenba@redhat.com \
--cc=akpm@linux-foundation.org \
--cc=anna@kernel.org \
--cc=axboe@kernel.dk \
--cc=brauner@kernel.org \
--cc=ceph-devel@vger.kernel.org \
--cc=cluster-devel@redhat.com \
--cc=djwong@kernel.org \
--cc=dlemoal@kernel.org \
--cc=idryomov@gmail.com \
--cc=jaegeuk@kernel.org \
--cc=linux-block@vger.kernel.org \
--cc=linux-ext4@vger.kernel.org \
--cc=linux-f2fs-devel@lists.sourceforge.net \
--cc=linux-fsdevel@vger.kernel.org \
--cc=linux-mm@kvack.org \
--cc=linux-nfs@vger.kernel.org \
--cc=linux-xfs@vger.kernel.org \
--cc=miklos@szeredi.hu \
--cc=trond.myklebust@hammerspace.com \
--cc=tytso@mit.edu \
--cc=viro@zeniv.linux.org.uk \
--cc=willy@infradead.org \
--cc=xiubli@redhat.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).