linux-fsdevel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Christoph Hellwig <hch@lst.de>
To: Matthew Wilcox <willy@infradead.org>
Cc: Jens Axboe <axboe@kernel.dk>, Xiubo Li <xiubli@redhat.com>,
	Ilya Dryomov <idryomov@gmail.com>,
	Alexander Viro <viro@zeniv.linux.org.uk>,
	Christian Brauner <brauner@kernel.org>,
	"Theodore Ts'o" <tytso@mit.edu>, Jaegeuk Kim <jaegeuk@kernel.org>,
	Chao Yu <chao@kernel.org>, Miklos Szeredi <miklos@szeredi.hu>,
	Andreas Gruenbacher <agruenba@redhat.com>,
	"Darrick J. Wong" <djwong@kernel.org>,
	Trond Myklebust <trond.myklebust@hammerspace.com>,
	Anna Schumaker <anna@kernel.org>,
	Damien Le Moal <dlemoal@kernel.org>,
	Andrew Morton <akpm@linux-foundation.org>,
	linux-block@vger.kernel.org, ceph-devel@vger.kernel.org,
	linux-fsdevel@vger.kernel.org, linux-ext4@vger.kernel.org,
	linux-f2fs-devel@lists.sourceforge.net (open list:F2FS FILE
	SYSTEM),
	cluster-devel@redhat.com, linux-xfs@vger.kernel.org,
	linux-nfs@vger.kernel.org, linux-mm@kvack.org
Subject: [PATCH 09/13] iomap: use kiocb_write_and_wait and kiocb_invalidate_pages
Date: Fri, 19 May 2023 11:35:17 +0200	[thread overview]
Message-ID: <20230519093521.133226-10-hch@lst.de> (raw)
In-Reply-To: <20230519093521.133226-1-hch@lst.de>

Use the common helpers for direct I/O page invalidation instead of
open coding the logic.  This leads to a slight reordering of checks
in __iomap_dio_rw to keep the logic straight.

Signed-off-by: Christoph Hellwig <hch@lst.de>
---
 fs/iomap/direct-io.c | 55 ++++++++++++++++----------------------------
 1 file changed, 20 insertions(+), 35 deletions(-)

diff --git a/fs/iomap/direct-io.c b/fs/iomap/direct-io.c
index 45accd98344e79..ccf51d57619721 100644
--- a/fs/iomap/direct-io.c
+++ b/fs/iomap/direct-io.c
@@ -472,7 +472,6 @@ __iomap_dio_rw(struct kiocb *iocb, struct iov_iter *iter,
 		const struct iomap_ops *ops, const struct iomap_dio_ops *dops,
 		unsigned int dio_flags, void *private, size_t done_before)
 {
-	struct address_space *mapping = iocb->ki_filp->f_mapping;
 	struct inode *inode = file_inode(iocb->ki_filp);
 	struct iomap_iter iomi = {
 		.inode		= inode,
@@ -481,11 +480,11 @@ __iomap_dio_rw(struct kiocb *iocb, struct iov_iter *iter,
 		.flags		= IOMAP_DIRECT,
 		.private	= private,
 	};
-	loff_t end = iomi.pos + iomi.len - 1, ret = 0;
 	bool wait_for_completion =
 		is_sync_kiocb(iocb) || (dio_flags & IOMAP_DIO_FORCE_WAIT);
 	struct blk_plug plug;
 	struct iomap_dio *dio;
+	loff_t ret = 0;
 
 	trace_iomap_dio_rw_begin(iocb, iter, dio_flags, done_before);
 
@@ -509,31 +508,29 @@ __iomap_dio_rw(struct kiocb *iocb, struct iov_iter *iter,
 	dio->submit.waiter = current;
 	dio->submit.poll_bio = NULL;
 
+	if (iocb->ki_flags & IOCB_NOWAIT)
+		iomi.flags |= IOMAP_NOWAIT;
+
 	if (iov_iter_rw(iter) == READ) {
 		if (iomi.pos >= dio->i_size)
 			goto out_free_dio;
 
-		if (iocb->ki_flags & IOCB_NOWAIT) {
-			if (filemap_range_needs_writeback(mapping, iomi.pos,
-					end)) {
-				ret = -EAGAIN;
-				goto out_free_dio;
-			}
-			iomi.flags |= IOMAP_NOWAIT;
-		}
-
 		if (user_backed_iter(iter))
 			dio->flags |= IOMAP_DIO_DIRTY;
+
+		ret = kiocb_write_and_wait(iocb, iomi.len);
+		if (ret)
+			goto out_free_dio;
 	} else {
 		iomi.flags |= IOMAP_WRITE;
 		dio->flags |= IOMAP_DIO_WRITE;
 
-		if (iocb->ki_flags & IOCB_NOWAIT) {
-			if (filemap_range_has_page(mapping, iomi.pos, end)) {
-				ret = -EAGAIN;
+		if (dio_flags & IOMAP_DIO_OVERWRITE_ONLY) {
+			ret = -EAGAIN;
+			if (iomi.pos >= dio->i_size ||
+			    iomi.pos + iomi.len > dio->i_size)
 				goto out_free_dio;
-			}
-			iomi.flags |= IOMAP_NOWAIT;
+			iomi.flags |= IOMAP_OVERWRITE_ONLY;
 		}
 
 		/* for data sync or sync, we need sync completion processing */
@@ -549,31 +546,19 @@ __iomap_dio_rw(struct kiocb *iocb, struct iov_iter *iter,
 			if (!(iocb->ki_flags & IOCB_SYNC))
 				dio->flags |= IOMAP_DIO_WRITE_FUA;
 		}
-	}
-
-	if (dio_flags & IOMAP_DIO_OVERWRITE_ONLY) {
-		ret = -EAGAIN;
-		if (iomi.pos >= dio->i_size ||
-		    iomi.pos + iomi.len > dio->i_size)
-			goto out_free_dio;
-		iomi.flags |= IOMAP_OVERWRITE_ONLY;
-	}
 
-	ret = filemap_write_and_wait_range(mapping, iomi.pos, end);
-	if (ret)
-		goto out_free_dio;
-
-	if (iov_iter_rw(iter) == WRITE) {
 		/*
 		 * Try to invalidate cache pages for the range we are writing.
 		 * If this invalidation fails, let the caller fall back to
 		 * buffered I/O.
 		 */
-		if (invalidate_inode_pages2_range(mapping,
-				iomi.pos >> PAGE_SHIFT, end >> PAGE_SHIFT)) {
-			trace_iomap_dio_invalidate_fail(inode, iomi.pos,
-							iomi.len);
-			ret = -ENOTBLK;
+		ret = kiocb_invalidate_pages(iocb, iomi.len);
+		if (ret) {
+			if (ret != -EAGAIN) {
+				trace_iomap_dio_invalidate_fail(inode, iomi.pos,
+								iomi.len);
+				ret = -ENOTBLK;
+			}
 			goto out_free_dio;
 		}
 
-- 
2.39.2


  parent reply	other threads:[~2023-05-19  9:38 UTC|newest]

Thread overview: 38+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-05-19  9:35 cleanup the filemap / direct I/O interaction Christoph Hellwig
2023-05-19  9:35 ` [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   ` [f2fs-dev] " patchwork-bot+f2fs
2023-05-19  9:35 ` [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 ` [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 ` [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 ` [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 ` [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 ` [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 ` [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 ` Christoph Hellwig [this message]
2023-05-22  0:07   ` [PATCH 09/13] iomap: use kiocb_write_and_wait and kiocb_invalidate_pages Damien Le Moal
2023-05-19  9:35 ` [PATCH 10/13] fs: factor out a direct_write_fallback helper Christoph Hellwig
2023-05-22  0:16   ` Damien Le Moal
2023-05-22 14:19   ` Miklos Szeredi
2023-05-23 16:03     ` Christoph Hellwig
2023-05-19  9:35 ` [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 ` [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 ` [PATCH 13/13] fuse: use direct_write_fallback Christoph Hellwig
2023-05-22  0:20   ` Damien Le Moal
2023-05-23  1:12 ` 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-10-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=chao@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).