linux-ext4.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* cleanup the filemap / direct I/O interaction
@ 2023-05-19  9:35 Christoph Hellwig
  2023-05-19  9:35 ` [PATCH 01/13] iomap: update ki_pos a little later in iomap_dio_complete Christoph Hellwig
                   ` (13 more replies)
  0 siblings, 14 replies; 38+ messages in thread
From: Christoph Hellwig @ 2023-05-19  9:35 UTC (permalink / raw)
  To: Matthew Wilcox
  Cc: Jens Axboe, Xiubo Li, Ilya Dryomov, Alexander Viro,
	Christian Brauner, Theodore Ts'o, Jaegeuk Kim, Chao Yu,
	Miklos Szeredi, Andreas Gruenbacher, Darrick J. Wong,
	Trond Myklebust, Anna Schumaker, Damien Le Moal, Andrew Morton,
	linux-block, ceph-devel, linux-fsdevel, linux-ext4,
	open list:F2FS FILE SYSTEM, cluster-devel, linux-xfs, linux-nfs,
	linux-mm

Hi all,

this series cleans up some of the generic write helper calling
conventions and the page cache writeback / invalidation for
direct I/O.  This is a spinoff from the no-bufferhead kernel
project, for while we'll want to an use iomap based buffered
write path in the block layer.

diffstat:
 block/fops.c            |   18 ----
 fs/ceph/file.c          |    6 -
 fs/direct-io.c          |   10 --
 fs/ext4/file.c          |   12 ---
 fs/f2fs/file.c          |    3 
 fs/fuse/file.c          |   47 ++----------
 fs/gfs2/file.c          |    7 -
 fs/iomap/buffered-io.c  |   12 ++-
 fs/iomap/direct-io.c    |   88 ++++++++--------------
 fs/libfs.c              |   36 +++++++++
 fs/nfs/file.c           |    6 -
 fs/xfs/xfs_file.c       |    7 -
 fs/zonefs/file.c        |    4 -
 include/linux/fs.h      |    7 -
 include/linux/pagemap.h |    4 +
 mm/filemap.c            |  184 +++++++++++++++++++++---------------------------
 16 files changed, 190 insertions(+), 261 deletions(-)

^ permalink raw reply	[flat|nested] 38+ messages in thread

* [PATCH 01/13] iomap: update ki_pos a little later in iomap_dio_complete
  2023-05-19  9:35 cleanup the filemap / direct I/O interaction Christoph Hellwig
@ 2023-05-19  9:35 ` 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
                   ` (12 subsequent siblings)
  13 siblings, 2 replies; 38+ messages in thread
From: Christoph Hellwig @ 2023-05-19  9:35 UTC (permalink / raw)
  To: Matthew Wilcox
  Cc: Jens Axboe, Xiubo Li, Ilya Dryomov, Alexander Viro,
	Christian Brauner, Theodore Ts'o, Jaegeuk Kim, Chao Yu,
	Miklos Szeredi, Andreas Gruenbacher, Darrick J. Wong,
	Trond Myklebust, Anna Schumaker, Damien Le Moal, Andrew Morton,
	linux-block, ceph-devel, linux-fsdevel, linux-ext4,
	open list:F2FS FILE SYSTEM, cluster-devel, linux-xfs, linux-nfs,
	linux-mm

Move the ki_pos update down a bit to prepare for a better common
helper that invalidates pages based of an iocb.

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

diff --git a/fs/iomap/direct-io.c b/fs/iomap/direct-io.c
index 019cc87d0fb339..6207a59d2162e1 100644
--- a/fs/iomap/direct-io.c
+++ b/fs/iomap/direct-io.c
@@ -94,7 +94,6 @@ ssize_t iomap_dio_complete(struct iomap_dio *dio)
 		if (offset + ret > dio->i_size &&
 		    !(dio->flags & IOMAP_DIO_WRITE))
 			ret = dio->i_size - offset;
-		iocb->ki_pos += ret;
 	}
 
 	/*
@@ -120,19 +119,21 @@ ssize_t iomap_dio_complete(struct iomap_dio *dio)
 	}
 
 	inode_dio_end(file_inode(iocb->ki_filp));
-	/*
-	 * If this is a DSYNC write, make sure we push it to stable storage now
-	 * that we've written data.
-	 */
-	if (ret > 0 && (dio->flags & IOMAP_DIO_NEED_SYNC))
-		ret = generic_write_sync(iocb, ret);
 
-	if (ret > 0)
-		ret += dio->done_before;
+	if (ret > 0) {
+		iocb->ki_pos += ret;
 
+		/*
+		 * If this is a DSYNC write, make sure we push it to stable
+		 * storage now that we've written data.
+		 */
+		if (dio->flags & IOMAP_DIO_NEED_SYNC)
+			ret = generic_write_sync(iocb, ret);
+		if (ret > 0)
+			ret += dio->done_before;
+	}
 	trace_iomap_dio_complete(iocb, dio->error, ret);
 	kfree(dio);
-
 	return ret;
 }
 EXPORT_SYMBOL_GPL(iomap_dio_complete);
-- 
2.39.2


^ permalink raw reply related	[flat|nested] 38+ messages in thread

* [PATCH 02/13] filemap: update ki_pos in generic_perform_write
  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-19  9:35 ` 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
                   ` (11 subsequent siblings)
  13 siblings, 2 replies; 38+ messages in thread
From: Christoph Hellwig @ 2023-05-19  9:35 UTC (permalink / raw)
  To: Matthew Wilcox
  Cc: Jens Axboe, Xiubo Li, Ilya Dryomov, Alexander Viro,
	Christian Brauner, Theodore Ts'o, Jaegeuk Kim, Chao Yu,
	Miklos Szeredi, Andreas Gruenbacher, Darrick J. Wong,
	Trond Myklebust, Anna Schumaker, Damien Le Moal, Andrew Morton,
	linux-block, ceph-devel, linux-fsdevel, linux-ext4,
	open list:F2FS FILE SYSTEM, cluster-devel, linux-xfs, linux-nfs,
	linux-mm

All callers of generic_perform_write need to updated ki_pos, move it into
common code.

Signed-off-by: Christoph Hellwig <hch@lst.de>
---
 fs/ceph/file.c | 2 --
 fs/ext4/file.c | 9 +++------
 fs/f2fs/file.c | 1 -
 fs/nfs/file.c  | 1 -
 mm/filemap.c   | 8 ++++----
 5 files changed, 7 insertions(+), 14 deletions(-)

diff --git a/fs/ceph/file.c b/fs/ceph/file.c
index f4d8bf7dec88a8..feeb9882ef635a 100644
--- a/fs/ceph/file.c
+++ b/fs/ceph/file.c
@@ -1894,8 +1894,6 @@ static ssize_t ceph_write_iter(struct kiocb *iocb, struct iov_iter *from)
 		 * can not run at the same time
 		 */
 		written = generic_perform_write(iocb, from);
-		if (likely(written >= 0))
-			iocb->ki_pos = pos + written;
 		ceph_end_io_write(inode);
 	}
 
diff --git a/fs/ext4/file.c b/fs/ext4/file.c
index d101b3b0c7dad8..50824831d31def 100644
--- a/fs/ext4/file.c
+++ b/fs/ext4/file.c
@@ -291,12 +291,9 @@ static ssize_t ext4_buffered_write_iter(struct kiocb *iocb,
 
 out:
 	inode_unlock(inode);
-	if (likely(ret > 0)) {
-		iocb->ki_pos += ret;
-		ret = generic_write_sync(iocb, ret);
-	}
-
-	return ret;
+	if (unlikely(ret <= 0))
+		return ret;
+	return generic_write_sync(iocb, ret);
 }
 
 static ssize_t ext4_handle_inode_extension(struct inode *inode, loff_t offset,
diff --git a/fs/f2fs/file.c b/fs/f2fs/file.c
index 5ac53d2627d20d..9e3855e43a7a63 100644
--- a/fs/f2fs/file.c
+++ b/fs/f2fs/file.c
@@ -4522,7 +4522,6 @@ static ssize_t f2fs_buffered_write_iter(struct kiocb *iocb,
 	current->backing_dev_info = NULL;
 
 	if (ret > 0) {
-		iocb->ki_pos += ret;
 		f2fs_update_iostat(F2FS_I_SB(inode), inode,
 						APP_BUFFERED_IO, ret);
 	}
diff --git a/fs/nfs/file.c b/fs/nfs/file.c
index f0edf5a36237d1..3cc87ae8473356 100644
--- a/fs/nfs/file.c
+++ b/fs/nfs/file.c
@@ -658,7 +658,6 @@ ssize_t nfs_file_write(struct kiocb *iocb, struct iov_iter *from)
 		goto out;
 
 	written = result;
-	iocb->ki_pos += written;
 	nfs_add_stats(inode, NFSIOS_NORMALWRITTENBYTES, written);
 
 	if (mntflags & NFS_MOUNT_WRITE_EAGER) {
diff --git a/mm/filemap.c b/mm/filemap.c
index b4c9bd368b7e58..4d0ec2fa1c7070 100644
--- a/mm/filemap.c
+++ b/mm/filemap.c
@@ -3957,7 +3957,10 @@ ssize_t generic_perform_write(struct kiocb *iocb, struct iov_iter *i)
 		balance_dirty_pages_ratelimited(mapping);
 	} while (iov_iter_count(i));
 
-	return written ? written : status;
+	if (!written)
+		return status;
+	iocb->ki_pos += written;
+	return written;
 }
 EXPORT_SYMBOL(generic_perform_write);
 
@@ -4036,7 +4039,6 @@ ssize_t __generic_file_write_iter(struct kiocb *iocb, struct iov_iter *from)
 		endbyte = pos + status - 1;
 		err = filemap_write_and_wait_range(mapping, pos, endbyte);
 		if (err == 0) {
-			iocb->ki_pos = endbyte + 1;
 			written += status;
 			invalidate_mapping_pages(mapping,
 						 pos >> PAGE_SHIFT,
@@ -4049,8 +4051,6 @@ ssize_t __generic_file_write_iter(struct kiocb *iocb, struct iov_iter *from)
 		}
 	} else {
 		written = generic_perform_write(iocb, from);
-		if (likely(written > 0))
-			iocb->ki_pos += written;
 	}
 out:
 	current->backing_dev_info = NULL;
-- 
2.39.2


^ permalink raw reply related	[flat|nested] 38+ messages in thread

* [PATCH 03/13] filemap: assign current->backing_dev_info in generic_perform_write
  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-19  9:35 ` [PATCH 02/13] filemap: update ki_pos in generic_perform_write Christoph Hellwig
@ 2023-05-19  9:35 ` 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
                   ` (10 subsequent siblings)
  13 siblings, 2 replies; 38+ messages in thread
From: Christoph Hellwig @ 2023-05-19  9:35 UTC (permalink / raw)
  To: Matthew Wilcox
  Cc: Jens Axboe, Xiubo Li, Ilya Dryomov, Alexander Viro,
	Christian Brauner, Theodore Ts'o, Jaegeuk Kim, Chao Yu,
	Miklos Szeredi, Andreas Gruenbacher, Darrick J. Wong,
	Trond Myklebust, Anna Schumaker, Damien Le Moal, Andrew Morton,
	linux-block, ceph-devel, linux-fsdevel, linux-ext4,
	open list:F2FS FILE SYSTEM, cluster-devel, linux-xfs, linux-nfs,
	linux-mm

Move the assignment to current->backing_dev_info from the callers into
generic_perform_write to reduce boiler plate code and reduce the scope
to just around the page dirtying loop.

Signed-off-by: Christoph Hellwig <hch@lst.de>
---
 fs/ceph/file.c | 4 ----
 fs/ext4/file.c | 3 ---
 fs/f2fs/file.c | 2 --
 fs/nfs/file.c  | 5 +----
 mm/filemap.c   | 2 ++
 5 files changed, 3 insertions(+), 13 deletions(-)

diff --git a/fs/ceph/file.c b/fs/ceph/file.c
index feeb9882ef635a..767f4dfe7def64 100644
--- a/fs/ceph/file.c
+++ b/fs/ceph/file.c
@@ -1791,9 +1791,6 @@ static ssize_t ceph_write_iter(struct kiocb *iocb, struct iov_iter *from)
 	else
 		ceph_start_io_write(inode);
 
-	/* We can write back this queue in page reclaim */
-	current->backing_dev_info = inode_to_bdi(inode);
-
 	if (iocb->ki_flags & IOCB_APPEND) {
 		err = ceph_do_getattr(inode, CEPH_STAT_CAP_SIZE, false);
 		if (err < 0)
@@ -1938,7 +1935,6 @@ static ssize_t ceph_write_iter(struct kiocb *iocb, struct iov_iter *from)
 		ceph_end_io_write(inode);
 out_unlocked:
 	ceph_free_cap_flush(prealloc_cf);
-	current->backing_dev_info = NULL;
 	return written ? written : err;
 }
 
diff --git a/fs/ext4/file.c b/fs/ext4/file.c
index 50824831d31def..3cb83a3e2e4a2a 100644
--- a/fs/ext4/file.c
+++ b/fs/ext4/file.c
@@ -29,7 +29,6 @@
 #include <linux/pagevec.h>
 #include <linux/uio.h>
 #include <linux/mman.h>
-#include <linux/backing-dev.h>
 #include "ext4.h"
 #include "ext4_jbd2.h"
 #include "xattr.h"
@@ -285,9 +284,7 @@ static ssize_t ext4_buffered_write_iter(struct kiocb *iocb,
 	if (ret <= 0)
 		goto out;
 
-	current->backing_dev_info = inode_to_bdi(inode);
 	ret = generic_perform_write(iocb, from);
-	current->backing_dev_info = NULL;
 
 out:
 	inode_unlock(inode);
diff --git a/fs/f2fs/file.c b/fs/f2fs/file.c
index 9e3855e43a7a63..7134fe8bd008cb 100644
--- a/fs/f2fs/file.c
+++ b/fs/f2fs/file.c
@@ -4517,9 +4517,7 @@ static ssize_t f2fs_buffered_write_iter(struct kiocb *iocb,
 	if (iocb->ki_flags & IOCB_NOWAIT)
 		return -EOPNOTSUPP;
 
-	current->backing_dev_info = inode_to_bdi(inode);
 	ret = generic_perform_write(iocb, from);
-	current->backing_dev_info = NULL;
 
 	if (ret > 0) {
 		f2fs_update_iostat(F2FS_I_SB(inode), inode,
diff --git a/fs/nfs/file.c b/fs/nfs/file.c
index 3cc87ae8473356..e8bb4c48a3210a 100644
--- a/fs/nfs/file.c
+++ b/fs/nfs/file.c
@@ -648,11 +648,8 @@ ssize_t nfs_file_write(struct kiocb *iocb, struct iov_iter *from)
 	since = filemap_sample_wb_err(file->f_mapping);
 	nfs_start_io_write(inode);
 	result = generic_write_checks(iocb, from);
-	if (result > 0) {
-		current->backing_dev_info = inode_to_bdi(inode);
+	if (result > 0)
 		result = generic_perform_write(iocb, from);
-		current->backing_dev_info = NULL;
-	}
 	nfs_end_io_write(inode);
 	if (result <= 0)
 		goto out;
diff --git a/mm/filemap.c b/mm/filemap.c
index 4d0ec2fa1c7070..bf693ad1da1ece 100644
--- a/mm/filemap.c
+++ b/mm/filemap.c
@@ -3892,6 +3892,7 @@ ssize_t generic_perform_write(struct kiocb *iocb, struct iov_iter *i)
 	long status = 0;
 	ssize_t written = 0;
 
+	current->backing_dev_info = inode_to_bdi(mapping->host);
 	do {
 		struct page *page;
 		unsigned long offset;	/* Offset into pagecache page */
@@ -3956,6 +3957,7 @@ ssize_t generic_perform_write(struct kiocb *iocb, struct iov_iter *i)
 
 		balance_dirty_pages_ratelimited(mapping);
 	} while (iov_iter_count(i));
+	current->backing_dev_info = NULL;
 
 	if (!written)
 		return status;
-- 
2.39.2


^ permalink raw reply related	[flat|nested] 38+ messages in thread

* [PATCH 04/13] filemap: add a kiocb_write_and_wait helper
  2023-05-19  9:35 cleanup the filemap / direct I/O interaction Christoph Hellwig
                   ` (2 preceding siblings ...)
  2023-05-19  9:35 ` [PATCH 03/13] filemap: assign current->backing_dev_info " Christoph Hellwig
@ 2023-05-19  9:35 ` 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
                   ` (9 subsequent siblings)
  13 siblings, 1 reply; 38+ messages in thread
From: Christoph Hellwig @ 2023-05-19  9:35 UTC (permalink / raw)
  To: Matthew Wilcox
  Cc: Jens Axboe, Xiubo Li, Ilya Dryomov, Alexander Viro,
	Christian Brauner, Theodore Ts'o, Jaegeuk Kim, Chao Yu,
	Miklos Szeredi, Andreas Gruenbacher, Darrick J. Wong,
	Trond Myklebust, Anna Schumaker, Damien Le Moal, Andrew Morton,
	linux-block, ceph-devel, linux-fsdevel, linux-ext4,
	open list:F2FS FILE SYSTEM, cluster-devel, linux-xfs, linux-nfs,
	linux-mm

Factor out a helper that does filemap_write_and_wait_range for a the
range covered by a read kiocb, or returns -EAGAIN if the kiocb
is marked as nowait and there would be pages to write.

Signed-off-by: Christoph Hellwig <hch@lst.de>
---
 block/fops.c            | 18 +++---------------
 include/linux/pagemap.h |  2 ++
 mm/filemap.c            | 30 ++++++++++++++++++------------
 3 files changed, 23 insertions(+), 27 deletions(-)

diff --git a/block/fops.c b/block/fops.c
index d2e6be4e3d1c7d..c194939b851cfb 100644
--- a/block/fops.c
+++ b/block/fops.c
@@ -576,21 +576,9 @@ static ssize_t blkdev_read_iter(struct kiocb *iocb, struct iov_iter *to)
 		goto reexpand; /* skip atime */
 
 	if (iocb->ki_flags & IOCB_DIRECT) {
-		struct address_space *mapping = iocb->ki_filp->f_mapping;
-
-		if (iocb->ki_flags & IOCB_NOWAIT) {
-			if (filemap_range_needs_writeback(mapping, pos,
-							  pos + count - 1)) {
-				ret = -EAGAIN;
-				goto reexpand;
-			}
-		} else {
-			ret = filemap_write_and_wait_range(mapping, pos,
-							   pos + count - 1);
-			if (ret < 0)
-				goto reexpand;
-		}
-
+		ret = kiocb_write_and_wait(iocb, count);
+		if (ret < 0)
+			goto reexpand;
 		file_accessed(iocb->ki_filp);
 
 		ret = blkdev_direct_IO(iocb, to);
diff --git a/include/linux/pagemap.h b/include/linux/pagemap.h
index a56308a9d1a450..36fc2cea13ce20 100644
--- a/include/linux/pagemap.h
+++ b/include/linux/pagemap.h
@@ -30,6 +30,7 @@ static inline void invalidate_remote_inode(struct inode *inode)
 int invalidate_inode_pages2(struct address_space *mapping);
 int invalidate_inode_pages2_range(struct address_space *mapping,
 		pgoff_t start, pgoff_t end);
+
 int write_inode_now(struct inode *, int sync);
 int filemap_fdatawrite(struct address_space *);
 int filemap_flush(struct address_space *);
@@ -54,6 +55,7 @@ int filemap_check_errors(struct address_space *mapping);
 void __filemap_set_wb_err(struct address_space *mapping, int err);
 int filemap_fdatawrite_wbc(struct address_space *mapping,
 			   struct writeback_control *wbc);
+int kiocb_write_and_wait(struct kiocb *iocb, size_t count);
 
 static inline int filemap_write_and_wait(struct address_space *mapping)
 {
diff --git a/mm/filemap.c b/mm/filemap.c
index bf693ad1da1ece..2d7712b13b95c9 100644
--- a/mm/filemap.c
+++ b/mm/filemap.c
@@ -2762,6 +2762,21 @@ ssize_t filemap_read(struct kiocb *iocb, struct iov_iter *iter,
 }
 EXPORT_SYMBOL_GPL(filemap_read);
 
+int kiocb_write_and_wait(struct kiocb *iocb, size_t count)
+{
+	struct address_space *mapping = iocb->ki_filp->f_mapping;
+	loff_t pos = iocb->ki_pos;
+	loff_t end = pos + count - 1;
+
+	if (iocb->ki_flags & IOCB_NOWAIT) {
+		if (filemap_range_needs_writeback(mapping, pos, end))
+			return -EAGAIN;
+		return 0;
+	}
+
+	return filemap_write_and_wait_range(mapping, pos, end);
+}
+
 /**
  * generic_file_read_iter - generic filesystem read routine
  * @iocb:	kernel I/O control block
@@ -2797,18 +2812,9 @@ generic_file_read_iter(struct kiocb *iocb, struct iov_iter *iter)
 		struct address_space *mapping = file->f_mapping;
 		struct inode *inode = mapping->host;
 
-		if (iocb->ki_flags & IOCB_NOWAIT) {
-			if (filemap_range_needs_writeback(mapping, iocb->ki_pos,
-						iocb->ki_pos + count - 1))
-				return -EAGAIN;
-		} else {
-			retval = filemap_write_and_wait_range(mapping,
-						iocb->ki_pos,
-					        iocb->ki_pos + count - 1);
-			if (retval < 0)
-				return retval;
-		}
-
+		retval = kiocb_write_and_wait(iocb, count);
+		if (retval < 0)
+			return retval;
 		file_accessed(file);
 
 		retval = mapping->a_ops->direct_IO(iocb, iter);
-- 
2.39.2


^ permalink raw reply related	[flat|nested] 38+ messages in thread

* [PATCH 05/13] filemap: add a kiocb_invalidate_pages helper
  2023-05-19  9:35 cleanup the filemap / direct I/O interaction Christoph Hellwig
                   ` (3 preceding siblings ...)
  2023-05-19  9:35 ` [PATCH 04/13] filemap: add a kiocb_write_and_wait helper Christoph Hellwig
@ 2023-05-19  9:35 ` 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
                   ` (8 subsequent siblings)
  13 siblings, 1 reply; 38+ messages in thread
From: Christoph Hellwig @ 2023-05-19  9:35 UTC (permalink / raw)
  To: Matthew Wilcox
  Cc: Jens Axboe, Xiubo Li, Ilya Dryomov, Alexander Viro,
	Christian Brauner, Theodore Ts'o, Jaegeuk Kim, Chao Yu,
	Miklos Szeredi, Andreas Gruenbacher, Darrick J. Wong,
	Trond Myklebust, Anna Schumaker, Damien Le Moal, Andrew Morton,
	linux-block, ceph-devel, linux-fsdevel, linux-ext4,
	open list:F2FS FILE SYSTEM, cluster-devel, linux-xfs, linux-nfs,
	linux-mm

Factor out a helper that calls filemap_write_and_wait_range and
invalidate_inode_pages2_rangefor a the range covered by a write kiocb or
returns -EAGAIN if the kiocb is marked as nowait and there would be pages
to write or invalidate.

Signed-off-by: Christoph Hellwig <hch@lst.de>
---
 include/linux/pagemap.h |  1 +
 mm/filemap.c            | 48 ++++++++++++++++++++++++-----------------
 2 files changed, 29 insertions(+), 20 deletions(-)

diff --git a/include/linux/pagemap.h b/include/linux/pagemap.h
index 36fc2cea13ce20..6e4c9ee40baa99 100644
--- a/include/linux/pagemap.h
+++ b/include/linux/pagemap.h
@@ -30,6 +30,7 @@ static inline void invalidate_remote_inode(struct inode *inode)
 int invalidate_inode_pages2(struct address_space *mapping);
 int invalidate_inode_pages2_range(struct address_space *mapping,
 		pgoff_t start, pgoff_t end);
+int kiocb_invalidate_pages(struct kiocb *iocb, size_t count);
 
 int write_inode_now(struct inode *, int sync);
 int filemap_fdatawrite(struct address_space *);
diff --git a/mm/filemap.c b/mm/filemap.c
index 2d7712b13b95c9..8607220e20eae3 100644
--- a/mm/filemap.c
+++ b/mm/filemap.c
@@ -2777,6 +2777,33 @@ int kiocb_write_and_wait(struct kiocb *iocb, size_t count)
 	return filemap_write_and_wait_range(mapping, pos, end);
 }
 
+int kiocb_invalidate_pages(struct kiocb *iocb, size_t count)
+{
+	struct address_space *mapping = iocb->ki_filp->f_mapping;
+	loff_t pos = iocb->ki_pos;
+	loff_t end = pos + count - 1;
+	int ret;
+
+	if (iocb->ki_flags & IOCB_NOWAIT) {
+		/* we could block if there are any pages in the range */
+		if (filemap_range_has_page(mapping, pos, end))
+			return -EAGAIN;
+	} else {
+		ret = filemap_write_and_wait_range(mapping, pos, end);
+		if (ret)
+			return ret;
+	}
+
+	/*
+	 * After a write we want buffered reads to be sure to go to disk to get
+	 * the new data.  We invalidate clean cached page from the region we're
+	 * about to write.  We do this *before* the write so that we can return
+	 * without clobbering -EIOCBQUEUED from ->direct_IO().
+	 */
+	return invalidate_inode_pages2_range(mapping, pos >> PAGE_SHIFT,
+					     end >> PAGE_SHIFT);
+}
+
 /**
  * generic_file_read_iter - generic filesystem read routine
  * @iocb:	kernel I/O control block
@@ -3820,30 +3847,11 @@ generic_file_direct_write(struct kiocb *iocb, struct iov_iter *from)
 	write_len = iov_iter_count(from);
 	end = (pos + write_len - 1) >> PAGE_SHIFT;
 
-	if (iocb->ki_flags & IOCB_NOWAIT) {
-		/* If there are pages to writeback, return */
-		if (filemap_range_has_page(file->f_mapping, pos,
-					   pos + write_len - 1))
-			return -EAGAIN;
-	} else {
-		written = filemap_write_and_wait_range(mapping, pos,
-							pos + write_len - 1);
-		if (written)
-			goto out;
-	}
-
-	/*
-	 * After a write we want buffered reads to be sure to go to disk to get
-	 * the new data.  We invalidate clean cached page from the region we're
-	 * about to write.  We do this *before* the write so that we can return
-	 * without clobbering -EIOCBQUEUED from ->direct_IO().
-	 */
-	written = invalidate_inode_pages2_range(mapping,
-					pos >> PAGE_SHIFT, end);
 	/*
 	 * If a page can not be invalidated, return 0 to fall back
 	 * to buffered write.
 	 */
+	written = kiocb_invalidate_pages(iocb, write_len);
 	if (written) {
 		if (written == -EBUSY)
 			return 0;
-- 
2.39.2


^ permalink raw reply related	[flat|nested] 38+ messages in thread

* [PATCH 06/13] filemap: add a kiocb_invalidate_post_write helper
  2023-05-19  9:35 cleanup the filemap / direct I/O interaction Christoph Hellwig
                   ` (4 preceding siblings ...)
  2023-05-19  9:35 ` [PATCH 05/13] filemap: add a kiocb_invalidate_pages helper Christoph Hellwig
@ 2023-05-19  9:35 ` Christoph Hellwig
  2023-05-21 23:56   ` Damien Le Moal
  2023-05-19  9:35 ` [PATCH 07/13] iomap: update ki_pos in iomap_file_buffered_write Christoph Hellwig
                   ` (7 subsequent siblings)
  13 siblings, 1 reply; 38+ messages in thread
From: Christoph Hellwig @ 2023-05-19  9:35 UTC (permalink / raw)
  To: Matthew Wilcox
  Cc: Jens Axboe, Xiubo Li, Ilya Dryomov, Alexander Viro,
	Christian Brauner, Theodore Ts'o, Jaegeuk Kim, Chao Yu,
	Miklos Szeredi, Andreas Gruenbacher, Darrick J. Wong,
	Trond Myklebust, Anna Schumaker, Damien Le Moal, Andrew Morton,
	linux-block, ceph-devel, linux-fsdevel, linux-ext4,
	open list:F2FS FILE SYSTEM, cluster-devel, linux-xfs, linux-nfs,
	linux-mm

Add a helper to invalidate page cache after a dio write.

Signed-off-by: Christoph Hellwig <hch@lst.de>
---
 fs/direct-io.c          | 10 ++--------
 fs/iomap/direct-io.c    | 12 ++----------
 include/linux/fs.h      |  5 -----
 include/linux/pagemap.h |  1 +
 mm/filemap.c            | 37 ++++++++++++++++++++-----------------
 5 files changed, 25 insertions(+), 40 deletions(-)

diff --git a/fs/direct-io.c b/fs/direct-io.c
index 0b380bb8a81e11..c25d68eabf4281 100644
--- a/fs/direct-io.c
+++ b/fs/direct-io.c
@@ -285,14 +285,8 @@ static ssize_t dio_complete(struct dio *dio, ssize_t ret, unsigned int flags)
 	 * zeros from unwritten extents.
 	 */
 	if (flags & DIO_COMPLETE_INVALIDATE &&
-	    ret > 0 && dio_op == REQ_OP_WRITE &&
-	    dio->inode->i_mapping->nrpages) {
-		err = invalidate_inode_pages2_range(dio->inode->i_mapping,
-					offset >> PAGE_SHIFT,
-					(offset + ret - 1) >> PAGE_SHIFT);
-		if (err)
-			dio_warn_stale_pagecache(dio->iocb->ki_filp);
-	}
+	    ret > 0 && dio_op == REQ_OP_WRITE)
+		kiocb_invalidate_post_write(dio->iocb, ret);
 
 	inode_dio_end(dio->inode);
 
diff --git a/fs/iomap/direct-io.c b/fs/iomap/direct-io.c
index 6207a59d2162e1..45accd98344e79 100644
--- a/fs/iomap/direct-io.c
+++ b/fs/iomap/direct-io.c
@@ -81,7 +81,6 @@ ssize_t iomap_dio_complete(struct iomap_dio *dio)
 {
 	const struct iomap_dio_ops *dops = dio->dops;
 	struct kiocb *iocb = dio->iocb;
-	struct inode *inode = file_inode(iocb->ki_filp);
 	loff_t offset = iocb->ki_pos;
 	ssize_t ret = dio->error;
 
@@ -108,15 +107,8 @@ ssize_t iomap_dio_complete(struct iomap_dio *dio)
 	 * ->end_io() when necessary, otherwise a racing buffer read would cache
 	 * zeros from unwritten extents.
 	 */
-	if (!dio->error && dio->size &&
-	    (dio->flags & IOMAP_DIO_WRITE) && inode->i_mapping->nrpages) {
-		int err;
-		err = invalidate_inode_pages2_range(inode->i_mapping,
-				offset >> PAGE_SHIFT,
-				(offset + dio->size - 1) >> PAGE_SHIFT);
-		if (err)
-			dio_warn_stale_pagecache(iocb->ki_filp);
-	}
+	if (!dio->error && dio->size && (dio->flags & IOMAP_DIO_WRITE))
+		kiocb_invalidate_post_write(iocb, dio->size);
 
 	inode_dio_end(file_inode(iocb->ki_filp));
 
diff --git a/include/linux/fs.h b/include/linux/fs.h
index 21a98168085641..e4efc1792a877a 100644
--- a/include/linux/fs.h
+++ b/include/linux/fs.h
@@ -2837,11 +2837,6 @@ static inline void inode_dio_end(struct inode *inode)
 		wake_up_bit(&inode->i_state, __I_DIO_WAKEUP);
 }
 
-/*
- * Warn about a page cache invalidation failure diring a direct I/O write.
- */
-void dio_warn_stale_pagecache(struct file *filp);
-
 extern void inode_set_flags(struct inode *inode, unsigned int flags,
 			    unsigned int mask);
 
diff --git a/include/linux/pagemap.h b/include/linux/pagemap.h
index 6e4c9ee40baa99..9695730ea86a98 100644
--- a/include/linux/pagemap.h
+++ b/include/linux/pagemap.h
@@ -31,6 +31,7 @@ int invalidate_inode_pages2(struct address_space *mapping);
 int invalidate_inode_pages2_range(struct address_space *mapping,
 		pgoff_t start, pgoff_t end);
 int kiocb_invalidate_pages(struct kiocb *iocb, size_t count);
+void kiocb_invalidate_post_write(struct kiocb *iocb, size_t count);
 
 int write_inode_now(struct inode *, int sync);
 int filemap_fdatawrite(struct address_space *);
diff --git a/mm/filemap.c b/mm/filemap.c
index 8607220e20eae3..c1b988199aece5 100644
--- a/mm/filemap.c
+++ b/mm/filemap.c
@@ -3816,7 +3816,7 @@ EXPORT_SYMBOL(read_cache_page_gfp);
 /*
  * Warn about a page cache invalidation failure during a direct I/O write.
  */
-void dio_warn_stale_pagecache(struct file *filp)
+static void dio_warn_stale_pagecache(struct file *filp)
 {
 	static DEFINE_RATELIMIT_STATE(_rs, 86400 * HZ, DEFAULT_RATELIMIT_BURST);
 	char pathname[128];
@@ -3833,19 +3833,23 @@ void dio_warn_stale_pagecache(struct file *filp)
 	}
 }
 
+void kiocb_invalidate_post_write(struct kiocb *iocb, size_t count)
+{
+	struct address_space *mapping = iocb->ki_filp->f_mapping;
+
+	if (mapping->nrpages &&
+	    invalidate_inode_pages2_range(mapping,
+			iocb->ki_pos >> PAGE_SHIFT,
+			(iocb->ki_pos + count - 1) >> PAGE_SHIFT))
+		dio_warn_stale_pagecache(iocb->ki_filp);
+}
+
 ssize_t
 generic_file_direct_write(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;
-	loff_t		pos = iocb->ki_pos;
-	ssize_t		written;
-	size_t		write_len;
-	pgoff_t		end;
-
-	write_len = iov_iter_count(from);
-	end = (pos + write_len - 1) >> PAGE_SHIFT;
+	struct address_space *mapping = iocb->ki_filp->f_mapping;
+	size_t write_len = iov_iter_count(from);
+	ssize_t written;
 
 	/*
 	 * If a page can not be invalidated, return 0 to fall back
@@ -3855,7 +3859,7 @@ generic_file_direct_write(struct kiocb *iocb, struct iov_iter *from)
 	if (written) {
 		if (written == -EBUSY)
 			return 0;
-		goto out;
+		return written;
 	}
 
 	written = mapping->a_ops->direct_IO(iocb, from);
@@ -3877,11 +3881,11 @@ generic_file_direct_write(struct kiocb *iocb, struct iov_iter *from)
 	 *
 	 * Skip invalidation for async writes or if mapping has no pages.
 	 */
-	if (written > 0 && mapping->nrpages &&
-	    invalidate_inode_pages2_range(mapping, pos >> PAGE_SHIFT, end))
-		dio_warn_stale_pagecache(file);
-
 	if (written > 0) {
+		struct inode *inode = mapping->host;
+		loff_t pos = iocb->ki_pos;
+
+		kiocb_invalidate_post_write(iocb, written);
 		pos += written;
 		write_len -= written;
 		if (pos > i_size_read(inode) && !S_ISBLK(inode->i_mode)) {
@@ -3892,7 +3896,6 @@ generic_file_direct_write(struct kiocb *iocb, struct iov_iter *from)
 	}
 	if (written != -EIOCBQUEUED)
 		iov_iter_revert(from, write_len - iov_iter_count(from));
-out:
 	return written;
 }
 EXPORT_SYMBOL(generic_file_direct_write);
-- 
2.39.2


^ permalink raw reply related	[flat|nested] 38+ messages in thread

* [PATCH 07/13] iomap: update ki_pos in iomap_file_buffered_write
  2023-05-19  9:35 cleanup the filemap / direct I/O interaction Christoph Hellwig
                   ` (5 preceding siblings ...)
  2023-05-19  9:35 ` [PATCH 06/13] filemap: add a kiocb_invalidate_post_write helper Christoph Hellwig
@ 2023-05-19  9:35 ` Christoph Hellwig
  2023-05-22  0:01   ` Damien Le Moal
  2023-05-19  9:35 ` [PATCH 08/13] iomap: assign current->backing_dev_info " Christoph Hellwig
                   ` (6 subsequent siblings)
  13 siblings, 1 reply; 38+ messages in thread
From: Christoph Hellwig @ 2023-05-19  9:35 UTC (permalink / raw)
  To: Matthew Wilcox
  Cc: Jens Axboe, Xiubo Li, Ilya Dryomov, Alexander Viro,
	Christian Brauner, Theodore Ts'o, Jaegeuk Kim, Chao Yu,
	Miklos Szeredi, Andreas Gruenbacher, Darrick J. Wong,
	Trond Myklebust, Anna Schumaker, Damien Le Moal, Andrew Morton,
	linux-block, ceph-devel, linux-fsdevel, linux-ext4,
	open list:F2FS FILE SYSTEM, cluster-devel, linux-xfs, linux-nfs,
	linux-mm

All callers of iomap_file_buffered_write need to updated ki_pos, move it
into common code.

Signed-off-by: Christoph Hellwig <hch@lst.de>
---
 fs/gfs2/file.c         | 4 +---
 fs/iomap/buffered-io.c | 9 ++++++---
 fs/xfs/xfs_file.c      | 2 --
 fs/zonefs/file.c       | 4 +---
 4 files changed, 8 insertions(+), 11 deletions(-)

diff --git a/fs/gfs2/file.c b/fs/gfs2/file.c
index 300844f50dcd28..499ef174dec138 100644
--- a/fs/gfs2/file.c
+++ b/fs/gfs2/file.c
@@ -1046,10 +1046,8 @@ static ssize_t gfs2_file_buffered_write(struct kiocb *iocb,
 	ret = iomap_file_buffered_write(iocb, from, &gfs2_iomap_ops);
 	pagefault_enable();
 	current->backing_dev_info = NULL;
-	if (ret > 0) {
-		iocb->ki_pos += ret;
+	if (ret > 0)
 		written += ret;
-	}
 
 	if (inode == sdp->sd_rindex)
 		gfs2_glock_dq_uninit(statfs_gh);
diff --git a/fs/iomap/buffered-io.c b/fs/iomap/buffered-io.c
index 063133ec77f49e..550525a525c45c 100644
--- a/fs/iomap/buffered-io.c
+++ b/fs/iomap/buffered-io.c
@@ -864,16 +864,19 @@ iomap_file_buffered_write(struct kiocb *iocb, struct iov_iter *i,
 		.len		= iov_iter_count(i),
 		.flags		= IOMAP_WRITE,
 	};
-	int ret;
+	ssize_t ret;
 
 	if (iocb->ki_flags & IOCB_NOWAIT)
 		iter.flags |= IOMAP_NOWAIT;
 
 	while ((ret = iomap_iter(&iter, ops)) > 0)
 		iter.processed = iomap_write_iter(&iter, i);
-	if (iter.pos == iocb->ki_pos)
+
+	if (unlikely(ret < 0))
 		return ret;
-	return iter.pos - iocb->ki_pos;
+	ret = iter.pos - iocb->ki_pos;
+	iocb->ki_pos += ret;
+	return ret;
 }
 EXPORT_SYMBOL_GPL(iomap_file_buffered_write);
 
diff --git a/fs/xfs/xfs_file.c b/fs/xfs/xfs_file.c
index aede746541f8ae..bfba10e0b0f3c2 100644
--- a/fs/xfs/xfs_file.c
+++ b/fs/xfs/xfs_file.c
@@ -723,8 +723,6 @@ xfs_file_buffered_write(
 	trace_xfs_file_buffered_write(iocb, from);
 	ret = iomap_file_buffered_write(iocb, from,
 			&xfs_buffered_write_iomap_ops);
-	if (likely(ret >= 0))
-		iocb->ki_pos += ret;
 
 	/*
 	 * If we hit a space limit, try to free up some lingering preallocated
diff --git a/fs/zonefs/file.c b/fs/zonefs/file.c
index 132f01d3461f14..e212d0636f848e 100644
--- a/fs/zonefs/file.c
+++ b/fs/zonefs/file.c
@@ -643,9 +643,7 @@ static ssize_t zonefs_file_buffered_write(struct kiocb *iocb,
 		goto inode_unlock;
 
 	ret = iomap_file_buffered_write(iocb, from, &zonefs_write_iomap_ops);
-	if (ret > 0)
-		iocb->ki_pos += ret;
-	else if (ret == -EIO)
+	if (ret == -EIO)
 		zonefs_io_error(inode, true);
 
 inode_unlock:
-- 
2.39.2


^ permalink raw reply related	[flat|nested] 38+ messages in thread

* [PATCH 08/13] iomap: assign current->backing_dev_info in iomap_file_buffered_write
  2023-05-19  9:35 cleanup the filemap / direct I/O interaction Christoph Hellwig
                   ` (6 preceding siblings ...)
  2023-05-19  9:35 ` [PATCH 07/13] iomap: update ki_pos in iomap_file_buffered_write Christoph Hellwig
@ 2023-05-19  9:35 ` Christoph Hellwig
  2023-05-22  0:05   ` Damien Le Moal
  2023-05-23  1:06   ` Darrick J. Wong
  2023-05-19  9:35 ` [PATCH 09/13] iomap: use kiocb_write_and_wait and kiocb_invalidate_pages Christoph Hellwig
                   ` (5 subsequent siblings)
  13 siblings, 2 replies; 38+ messages in thread
From: Christoph Hellwig @ 2023-05-19  9:35 UTC (permalink / raw)
  To: Matthew Wilcox
  Cc: Jens Axboe, Xiubo Li, Ilya Dryomov, Alexander Viro,
	Christian Brauner, Theodore Ts'o, Jaegeuk Kim, Chao Yu,
	Miklos Szeredi, Andreas Gruenbacher, Darrick J. Wong,
	Trond Myklebust, Anna Schumaker, Damien Le Moal, Andrew Morton,
	linux-block, ceph-devel, linux-fsdevel, linux-ext4,
	open list:F2FS FILE SYSTEM, cluster-devel, linux-xfs, linux-nfs,
	linux-mm

Move the assignment to current->backing_dev_info from the callers into
iomap_file_buffered_write to reduce boiler plate code and reduce the
scope to just around the page dirtying loop.

Note that zonefs was missing this assignment before.

Signed-off-by: Christoph Hellwig <hch@lst.de>
---
 fs/gfs2/file.c         | 3 ---
 fs/iomap/buffered-io.c | 3 +++
 fs/xfs/xfs_file.c      | 5 -----
 3 files changed, 3 insertions(+), 8 deletions(-)

diff --git a/fs/gfs2/file.c b/fs/gfs2/file.c
index 499ef174dec138..261897fcfbc495 100644
--- a/fs/gfs2/file.c
+++ b/fs/gfs2/file.c
@@ -25,7 +25,6 @@
 #include <linux/dlm.h>
 #include <linux/dlm_plock.h>
 #include <linux/delay.h>
-#include <linux/backing-dev.h>
 #include <linux/fileattr.h>
 
 #include "gfs2.h"
@@ -1041,11 +1040,9 @@ static ssize_t gfs2_file_buffered_write(struct kiocb *iocb,
 			goto out_unlock;
 	}
 
-	current->backing_dev_info = inode_to_bdi(inode);
 	pagefault_disable();
 	ret = iomap_file_buffered_write(iocb, from, &gfs2_iomap_ops);
 	pagefault_enable();
-	current->backing_dev_info = NULL;
 	if (ret > 0)
 		written += ret;
 
diff --git a/fs/iomap/buffered-io.c b/fs/iomap/buffered-io.c
index 550525a525c45c..b2779bd1f10611 100644
--- a/fs/iomap/buffered-io.c
+++ b/fs/iomap/buffered-io.c
@@ -3,6 +3,7 @@
  * Copyright (C) 2010 Red Hat, Inc.
  * Copyright (C) 2016-2019 Christoph Hellwig.
  */
+#include <linux/backing-dev.h>
 #include <linux/module.h>
 #include <linux/compiler.h>
 #include <linux/fs.h>
@@ -869,8 +870,10 @@ iomap_file_buffered_write(struct kiocb *iocb, struct iov_iter *i,
 	if (iocb->ki_flags & IOCB_NOWAIT)
 		iter.flags |= IOMAP_NOWAIT;
 
+	current->backing_dev_info = inode_to_bdi(iter.inode);
 	while ((ret = iomap_iter(&iter, ops)) > 0)
 		iter.processed = iomap_write_iter(&iter, i);
+	current->backing_dev_info = NULL;
 
 	if (unlikely(ret < 0))
 		return ret;
diff --git a/fs/xfs/xfs_file.c b/fs/xfs/xfs_file.c
index bfba10e0b0f3c2..98d763cc3b114c 100644
--- a/fs/xfs/xfs_file.c
+++ b/fs/xfs/xfs_file.c
@@ -27,7 +27,6 @@
 
 #include <linux/dax.h>
 #include <linux/falloc.h>
-#include <linux/backing-dev.h>
 #include <linux/mman.h>
 #include <linux/fadvise.h>
 #include <linux/mount.h>
@@ -717,9 +716,6 @@ xfs_file_buffered_write(
 	if (ret)
 		goto out;
 
-	/* We can write back this queue in page reclaim */
-	current->backing_dev_info = inode_to_bdi(inode);
-
 	trace_xfs_file_buffered_write(iocb, from);
 	ret = iomap_file_buffered_write(iocb, from,
 			&xfs_buffered_write_iomap_ops);
@@ -751,7 +747,6 @@ xfs_file_buffered_write(
 		goto write_retry;
 	}
 
-	current->backing_dev_info = NULL;
 out:
 	if (iolock)
 		xfs_iunlock(ip, iolock);
-- 
2.39.2


^ permalink raw reply related	[flat|nested] 38+ messages in thread

* [PATCH 09/13] iomap: use kiocb_write_and_wait and kiocb_invalidate_pages
  2023-05-19  9:35 cleanup the filemap / direct I/O interaction Christoph Hellwig
                   ` (7 preceding siblings ...)
  2023-05-19  9:35 ` [PATCH 08/13] iomap: assign current->backing_dev_info " Christoph Hellwig
@ 2023-05-19  9:35 ` Christoph Hellwig
  2023-05-22  0:07   ` Damien Le Moal
  2023-05-19  9:35 ` [PATCH 10/13] fs: factor out a direct_write_fallback helper Christoph Hellwig
                   ` (4 subsequent siblings)
  13 siblings, 1 reply; 38+ messages in thread
From: Christoph Hellwig @ 2023-05-19  9:35 UTC (permalink / raw)
  To: Matthew Wilcox
  Cc: Jens Axboe, Xiubo Li, Ilya Dryomov, Alexander Viro,
	Christian Brauner, Theodore Ts'o, Jaegeuk Kim, Chao Yu,
	Miklos Szeredi, Andreas Gruenbacher, Darrick J. Wong,
	Trond Myklebust, Anna Schumaker, Damien Le Moal, Andrew Morton,
	linux-block, ceph-devel, linux-fsdevel, linux-ext4,
	open list:F2FS FILE SYSTEM, cluster-devel, linux-xfs, linux-nfs,
	linux-mm

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


^ permalink raw reply related	[flat|nested] 38+ messages in thread

* [PATCH 10/13] fs: factor out a direct_write_fallback helper
  2023-05-19  9:35 cleanup the filemap / direct I/O interaction Christoph Hellwig
                   ` (8 preceding siblings ...)
  2023-05-19  9:35 ` [PATCH 09/13] iomap: use kiocb_write_and_wait and kiocb_invalidate_pages Christoph Hellwig
@ 2023-05-19  9:35 ` Christoph Hellwig
  2023-05-22  0:16   ` Damien Le Moal
  2023-05-22 14:19   ` Miklos Szeredi
  2023-05-19  9:35 ` [PATCH 11/13] fuse: update ki_pos in fuse_perform_write Christoph Hellwig
                   ` (3 subsequent siblings)
  13 siblings, 2 replies; 38+ messages in thread
From: Christoph Hellwig @ 2023-05-19  9:35 UTC (permalink / raw)
  To: Matthew Wilcox
  Cc: Jens Axboe, Xiubo Li, Ilya Dryomov, Alexander Viro,
	Christian Brauner, Theodore Ts'o, Jaegeuk Kim, Chao Yu,
	Miklos Szeredi, Andreas Gruenbacher, Darrick J. Wong,
	Trond Myklebust, Anna Schumaker, Damien Le Moal, Andrew Morton,
	linux-block, ceph-devel, linux-fsdevel, linux-ext4,
	open list:F2FS FILE SYSTEM, cluster-devel, linux-xfs, linux-nfs,
	linux-mm

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


^ permalink raw reply related	[flat|nested] 38+ messages in thread

* [PATCH 11/13] fuse: update ki_pos in fuse_perform_write
  2023-05-19  9:35 cleanup the filemap / direct I/O interaction Christoph Hellwig
                   ` (9 preceding siblings ...)
  2023-05-19  9:35 ` [PATCH 10/13] fs: factor out a direct_write_fallback helper Christoph Hellwig
@ 2023-05-19  9:35 ` 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
                   ` (2 subsequent siblings)
  13 siblings, 1 reply; 38+ messages in thread
From: Christoph Hellwig @ 2023-05-19  9:35 UTC (permalink / raw)
  To: Matthew Wilcox
  Cc: Jens Axboe, Xiubo Li, Ilya Dryomov, Alexander Viro,
	Christian Brauner, Theodore Ts'o, Jaegeuk Kim, Chao Yu,
	Miklos Szeredi, Andreas Gruenbacher, Darrick J. Wong,
	Trond Myklebust, Anna Schumaker, Damien Le Moal, Andrew Morton,
	linux-block, ceph-devel, linux-fsdevel, linux-ext4,
	open list:F2FS FILE SYSTEM, cluster-devel, linux-xfs, linux-nfs,
	linux-mm

Both callers of fuse_perform_write need to updated ki_pos, move it into
common code.

Signed-off-by: Christoph Hellwig <hch@lst.de>
---
 fs/fuse/file.c | 25 +++++++++++--------------
 1 file changed, 11 insertions(+), 14 deletions(-)

diff --git a/fs/fuse/file.c b/fs/fuse/file.c
index 89d97f6188e05e..fd2f27f2144750 100644
--- a/fs/fuse/file.c
+++ b/fs/fuse/file.c
@@ -1329,7 +1329,10 @@ static ssize_t fuse_perform_write(struct kiocb *iocb,
 	fuse_write_update_attr(inode, pos, res);
 	clear_bit(FUSE_I_SIZE_UNSTABLE, &fi->state);
 
-	return res > 0 ? res : err;
+	if (!res)
+		return err;
+	iocb->ki_pos += res;
+	return res;
 }
 
 static ssize_t fuse_cache_write_iter(struct kiocb *iocb, struct iov_iter *from)
@@ -1378,42 +1381,36 @@ static ssize_t fuse_cache_write_iter(struct kiocb *iocb, struct iov_iter *from)
 		goto out;
 
 	if (iocb->ki_flags & IOCB_DIRECT) {
-		loff_t pos = iocb->ki_pos;
 		written = generic_file_direct_write(iocb, from);
 		if (written < 0 || !iov_iter_count(from))
 			goto out;
 
-		pos += written;
-
-		written_buffered = fuse_perform_write(iocb, mapping, from, pos);
+		written_buffered = fuse_perform_write(iocb, mapping, from,
+						      iocb->ki_pos);
 		if (written_buffered < 0) {
 			err = written_buffered;
 			goto out;
 		}
-		endbyte = pos + written_buffered - 1;
+		endbyte = iocb->ki_pos + written_buffered - 1;
 
-		err = filemap_write_and_wait_range(file->f_mapping, pos,
+		err = filemap_write_and_wait_range(file->f_mapping,
+						   iocb->ki_pos,
 						   endbyte);
 		if (err)
 			goto out;
 
 		invalidate_mapping_pages(file->f_mapping,
-					 pos >> PAGE_SHIFT,
+					 iocb->ki_pos >> PAGE_SHIFT,
 					 endbyte >> PAGE_SHIFT);
 
 		written += written_buffered;
-		iocb->ki_pos = pos + written_buffered;
+		iocb->ki_pos += written_buffered;
 	} else {
 		written = fuse_perform_write(iocb, mapping, from, iocb->ki_pos);
-		if (written >= 0)
-			iocb->ki_pos += written;
 	}
 out:
 	current->backing_dev_info = NULL;
 	inode_unlock(inode);
-	if (written > 0)
-		written = generic_write_sync(iocb, written);
-
 	return written ? written : err;
 }
 
-- 
2.39.2


^ permalink raw reply related	[flat|nested] 38+ messages in thread

* [PATCH 12/13] fuse: drop redundant arguments to fuse_perform_write
  2023-05-19  9:35 cleanup the filemap / direct I/O interaction Christoph Hellwig
                   ` (10 preceding siblings ...)
  2023-05-19  9:35 ` [PATCH 11/13] fuse: update ki_pos in fuse_perform_write Christoph Hellwig
@ 2023-05-19  9:35 ` 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-23  1:12 ` cleanup the filemap / direct I/O interaction Darrick J. Wong
  13 siblings, 1 reply; 38+ messages in thread
From: Christoph Hellwig @ 2023-05-19  9:35 UTC (permalink / raw)
  To: Matthew Wilcox
  Cc: Jens Axboe, Xiubo Li, Ilya Dryomov, Alexander Viro,
	Christian Brauner, Theodore Ts'o, Jaegeuk Kim, Chao Yu,
	Miklos Szeredi, Andreas Gruenbacher, Darrick J. Wong,
	Trond Myklebust, Anna Schumaker, Damien Le Moal, Andrew Morton,
	linux-block, ceph-devel, linux-fsdevel, linux-ext4,
	open list:F2FS FILE SYSTEM, cluster-devel, linux-xfs, linux-nfs,
	linux-mm

pos is always equal to iocb->ki_pos, and mapping is always equal to
iocb->ki_filp->f_mapping.

Signed-off-by: Christoph Hellwig <hch@lst.de>
---
 fs/fuse/file.c | 11 +++++------
 1 file changed, 5 insertions(+), 6 deletions(-)

diff --git a/fs/fuse/file.c b/fs/fuse/file.c
index fd2f27f2144750..5f7b58798f99fc 100644
--- a/fs/fuse/file.c
+++ b/fs/fuse/file.c
@@ -1280,13 +1280,13 @@ static inline unsigned int fuse_wr_pages(loff_t pos, size_t len,
 		     max_pages);
 }
 
-static ssize_t fuse_perform_write(struct kiocb *iocb,
-				  struct address_space *mapping,
-				  struct iov_iter *ii, loff_t pos)
+static ssize_t fuse_perform_write(struct kiocb *iocb, struct iov_iter *ii)
 {
+	struct address_space *mapping = iocb->ki_filp->f_mapping;
 	struct inode *inode = mapping->host;
 	struct fuse_conn *fc = get_fuse_conn(inode);
 	struct fuse_inode *fi = get_fuse_inode(inode);
+	loff_t pos = iocb->ki_pos;
 	int err = 0;
 	ssize_t res = 0;
 
@@ -1385,8 +1385,7 @@ static ssize_t fuse_cache_write_iter(struct kiocb *iocb, struct iov_iter *from)
 		if (written < 0 || !iov_iter_count(from))
 			goto out;
 
-		written_buffered = fuse_perform_write(iocb, mapping, from,
-						      iocb->ki_pos);
+		written_buffered = fuse_perform_write(iocb, from);
 		if (written_buffered < 0) {
 			err = written_buffered;
 			goto out;
@@ -1406,7 +1405,7 @@ static ssize_t fuse_cache_write_iter(struct kiocb *iocb, struct iov_iter *from)
 		written += written_buffered;
 		iocb->ki_pos += written_buffered;
 	} else {
-		written = fuse_perform_write(iocb, mapping, from, iocb->ki_pos);
+		written = fuse_perform_write(iocb, from);
 	}
 out:
 	current->backing_dev_info = NULL;
-- 
2.39.2


^ permalink raw reply related	[flat|nested] 38+ messages in thread

* [PATCH 13/13] fuse: use direct_write_fallback
  2023-05-19  9:35 cleanup the filemap / direct I/O interaction Christoph Hellwig
                   ` (11 preceding siblings ...)
  2023-05-19  9:35 ` [PATCH 12/13] fuse: drop redundant arguments to fuse_perform_write Christoph Hellwig
@ 2023-05-19  9:35 ` 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
  13 siblings, 1 reply; 38+ messages in thread
From: Christoph Hellwig @ 2023-05-19  9:35 UTC (permalink / raw)
  To: Matthew Wilcox
  Cc: Jens Axboe, Xiubo Li, Ilya Dryomov, Alexander Viro,
	Christian Brauner, Theodore Ts'o, Jaegeuk Kim, Chao Yu,
	Miklos Szeredi, Andreas Gruenbacher, Darrick J. Wong,
	Trond Myklebust, Anna Schumaker, Damien Le Moal, Andrew Morton,
	linux-block, ceph-devel, linux-fsdevel, linux-ext4,
	open list:F2FS FILE SYSTEM, cluster-devel, linux-xfs, linux-nfs,
	linux-mm

Use the generic direct_write_fallback helper instead of duplicating the
logic.

Signed-off-by: Christoph Hellwig <hch@lst.de>
---
 fs/fuse/file.c | 27 +++------------------------
 1 file changed, 3 insertions(+), 24 deletions(-)

diff --git a/fs/fuse/file.c b/fs/fuse/file.c
index 5f7b58798f99fc..02ab446ab57f1f 100644
--- a/fs/fuse/file.c
+++ b/fs/fuse/file.c
@@ -1340,11 +1340,9 @@ static ssize_t fuse_cache_write_iter(struct kiocb *iocb, struct iov_iter *from)
 	struct file *file = iocb->ki_filp;
 	struct address_space *mapping = file->f_mapping;
 	ssize_t written = 0;
-	ssize_t written_buffered = 0;
 	struct inode *inode = mapping->host;
 	ssize_t err;
 	struct fuse_conn *fc = get_fuse_conn(inode);
-	loff_t endbyte = 0;
 
 	if (fc->writeback_cache) {
 		/* Update size (EOF optimization) and mode (SUID clearing) */
@@ -1382,28 +1380,9 @@ static ssize_t fuse_cache_write_iter(struct kiocb *iocb, struct iov_iter *from)
 
 	if (iocb->ki_flags & IOCB_DIRECT) {
 		written = generic_file_direct_write(iocb, from);
-		if (written < 0 || !iov_iter_count(from))
-			goto out;
-
-		written_buffered = fuse_perform_write(iocb, from);
-		if (written_buffered < 0) {
-			err = written_buffered;
-			goto out;
-		}
-		endbyte = iocb->ki_pos + written_buffered - 1;
-
-		err = filemap_write_and_wait_range(file->f_mapping,
-						   iocb->ki_pos,
-						   endbyte);
-		if (err)
-			goto out;
-
-		invalidate_mapping_pages(file->f_mapping,
-					 iocb->ki_pos >> PAGE_SHIFT,
-					 endbyte >> PAGE_SHIFT);
-
-		written += written_buffered;
-		iocb->ki_pos += written_buffered;
+		if (written >= 0 && iov_iter_count(from))
+			written = direct_write_fallback(iocb, from, written,
+					fuse_perform_write(iocb, from));
 	} else {
 		written = fuse_perform_write(iocb, from);
 	}
-- 
2.39.2


^ permalink raw reply related	[flat|nested] 38+ messages in thread

* Re: [PATCH 01/13] iomap: update ki_pos a little later in iomap_dio_complete
  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
  1 sibling, 0 replies; 38+ messages in thread
From: Damien Le Moal @ 2023-05-21 23:40 UTC (permalink / raw)
  To: Christoph Hellwig, Matthew Wilcox
  Cc: Jens Axboe, Xiubo Li, Ilya Dryomov, Alexander Viro,
	Christian Brauner, Theodore Ts'o, Jaegeuk Kim, Chao Yu,
	Miklos Szeredi, Andreas Gruenbacher, Darrick J. Wong,
	Trond Myklebust, Anna Schumaker, Andrew Morton, linux-block,
	ceph-devel, linux-fsdevel, linux-ext4,
	open list:F2FS FILE SYSTEM, cluster-devel, linux-xfs, linux-nfs,
	linux-mm

On 5/19/23 18:35, Christoph Hellwig wrote:
> Move the ki_pos update down a bit to prepare for a better common
> helper that invalidates pages based of an iocb.
> 
> Signed-off-by: Christoph Hellwig <hch@lst.de>

Looks OK to me.

Reviewed-by: Damien Le Moal <dlemoal@kernel.org>

> +		if (dio->flags & IOMAP_DIO_NEED_SYNC)
> +			ret = generic_write_sync(iocb, ret);
> +		if (ret > 0)
> +			ret += dio->done_before;
> +	}
>  	trace_iomap_dio_complete(iocb, dio->error, ret);
>  	kfree(dio);
> -

white line change. Personally, I like a blank line before returns to make them
stand out :)

>  	return ret;
>  }
>  EXPORT_SYMBOL_GPL(iomap_dio_complete);

-- 
Damien Le Moal
Western Digital Research


^ permalink raw reply	[flat|nested] 38+ messages in thread

* Re: [PATCH 02/13] filemap: update ki_pos in generic_perform_write
  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
  1 sibling, 0 replies; 38+ messages in thread
From: Damien Le Moal @ 2023-05-21 23:42 UTC (permalink / raw)
  To: Christoph Hellwig, Matthew Wilcox
  Cc: Jens Axboe, Xiubo Li, Ilya Dryomov, Alexander Viro,
	Christian Brauner, Theodore Ts'o, Jaegeuk Kim, Chao Yu,
	Miklos Szeredi, Andreas Gruenbacher, Darrick J. Wong,
	Trond Myklebust, Anna Schumaker, Andrew Morton, linux-block,
	ceph-devel, linux-fsdevel, linux-ext4,
	open list:F2FS FILE SYSTEM, cluster-devel, linux-xfs, linux-nfs,
	linux-mm

On 5/19/23 18:35, Christoph Hellwig wrote:
> All callers of generic_perform_write need to updated ki_pos, move it into
> common code.
> 
> Signed-off-by: Christoph Hellwig <hch@lst.de>

Reviewed-by: Damien Le Moal <dlemoal@kernel.org>

-- 
Damien Le Moal
Western Digital Research


^ permalink raw reply	[flat|nested] 38+ messages in thread

* Re: [PATCH 03/13] filemap: assign current->backing_dev_info in generic_perform_write
  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
  1 sibling, 0 replies; 38+ messages in thread
From: Damien Le Moal @ 2023-05-21 23:44 UTC (permalink / raw)
  To: Christoph Hellwig, Matthew Wilcox
  Cc: Jens Axboe, Xiubo Li, Ilya Dryomov, Alexander Viro,
	Christian Brauner, Theodore Ts'o, Jaegeuk Kim, Chao Yu,
	Miklos Szeredi, Andreas Gruenbacher, Darrick J. Wong,
	Trond Myklebust, Anna Schumaker, Andrew Morton, linux-block,
	ceph-devel, linux-fsdevel, linux-ext4,
	open list:F2FS FILE SYSTEM, cluster-devel, linux-xfs, linux-nfs,
	linux-mm

On 5/19/23 18:35, Christoph Hellwig wrote:
> Move the assignment to current->backing_dev_info from the callers into
> generic_perform_write to reduce boiler plate code and reduce the scope
> to just around the page dirtying loop.
> 
> Signed-off-by: Christoph Hellwig <hch@lst.de>

Reviewed-by: Damien Le Moal <dlemoal@kernel.org>

-- 
Damien Le Moal
Western Digital Research


^ permalink raw reply	[flat|nested] 38+ messages in thread

* Re: [PATCH 04/13] filemap: add a kiocb_write_and_wait helper
  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
  0 siblings, 0 replies; 38+ messages in thread
From: Damien Le Moal @ 2023-05-21 23:46 UTC (permalink / raw)
  To: Christoph Hellwig, Matthew Wilcox
  Cc: Jens Axboe, Xiubo Li, Ilya Dryomov, Alexander Viro,
	Christian Brauner, Theodore Ts'o, Jaegeuk Kim, Chao Yu,
	Miklos Szeredi, Andreas Gruenbacher, Darrick J. Wong,
	Trond Myklebust, Anna Schumaker, Andrew Morton, linux-block,
	ceph-devel, linux-fsdevel, linux-ext4,
	open list:F2FS FILE SYSTEM, cluster-devel, linux-xfs, linux-nfs,
	linux-mm

On 5/19/23 18:35, Christoph Hellwig wrote:
> Factor out a helper that does filemap_write_and_wait_range for a the

for a the -> for the

> range covered by a read kiocb, or returns -EAGAIN if the kiocb
> is marked as nowait and there would be pages to write.
> 
> Signed-off-by: Christoph Hellwig <hch@lst.de>

Reviewed-by: Damien Le Moal <dlemoal@kernel.org>

-- 
Damien Le Moal
Western Digital Research


^ permalink raw reply	[flat|nested] 38+ messages in thread

* Re: [PATCH 05/13] filemap: add a kiocb_invalidate_pages helper
  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
  0 siblings, 0 replies; 38+ messages in thread
From: Damien Le Moal @ 2023-05-21 23:51 UTC (permalink / raw)
  To: Christoph Hellwig, Matthew Wilcox
  Cc: Jens Axboe, Xiubo Li, Ilya Dryomov, Alexander Viro,
	Christian Brauner, Theodore Ts'o, Jaegeuk Kim, Chao Yu,
	Miklos Szeredi, Andreas Gruenbacher, Darrick J. Wong,
	Trond Myklebust, Anna Schumaker, Andrew Morton, linux-block,
	ceph-devel, linux-fsdevel, linux-ext4,
	open list:F2FS FILE SYSTEM, cluster-devel, linux-xfs, linux-nfs,
	linux-mm

On 5/19/23 18:35, Christoph Hellwig wrote:
> Factor out a helper that calls filemap_write_and_wait_range and
> invalidate_inode_pages2_rangefor a the range covered by a write kiocb or

invalidate_inode_pages2_rangefor a the range
->
invalidate_inode_pages2_range for the range

> returns -EAGAIN if the kiocb is marked as nowait and there would be pages
> to write or invalidate.
> 
> Signed-off-by: Christoph Hellwig <hch@lst.de>

Reviewed-by: Damien Le Moal <dlemoal@kernel.org>

-- 
Damien Le Moal
Western Digital Research


^ permalink raw reply	[flat|nested] 38+ messages in thread

* Re: [PATCH 06/13] filemap: add a kiocb_invalidate_post_write helper
  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
  0 siblings, 1 reply; 38+ messages in thread
From: Damien Le Moal @ 2023-05-21 23:56 UTC (permalink / raw)
  To: Christoph Hellwig, Matthew Wilcox
  Cc: Jens Axboe, Xiubo Li, Ilya Dryomov, Alexander Viro,
	Christian Brauner, Theodore Ts'o, Jaegeuk Kim, Chao Yu,
	Miklos Szeredi, Andreas Gruenbacher, Darrick J. Wong,
	Trond Myklebust, Anna Schumaker, Andrew Morton, linux-block,
	ceph-devel, linux-fsdevel, linux-ext4,
	open list:F2FS FILE SYSTEM, cluster-devel, linux-xfs, linux-nfs,
	linux-mm

On 5/19/23 18:35, Christoph Hellwig wrote:
> Add a helper to invalidate page cache after a dio write.
> 
> Signed-off-by: Christoph Hellwig <hch@lst.de>

Nit: kiocb_invalidate_post_dio_write() may be a better name to be explicit about
the fact that this is for DIOs only ?

Otherwise looks ok to me.

Reviewed-by: Damien Le Moal <dlemoal@kernel.org>

-- 
Damien Le Moal
Western Digital Research


^ permalink raw reply	[flat|nested] 38+ messages in thread

* Re: [PATCH 07/13] iomap: update ki_pos in iomap_file_buffered_write
  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
  0 siblings, 1 reply; 38+ messages in thread
From: Damien Le Moal @ 2023-05-22  0:01 UTC (permalink / raw)
  To: Christoph Hellwig, Matthew Wilcox
  Cc: Jens Axboe, Xiubo Li, Ilya Dryomov, Alexander Viro,
	Christian Brauner, Theodore Ts'o, Jaegeuk Kim, Chao Yu,
	Miklos Szeredi, Andreas Gruenbacher, Darrick J. Wong,
	Trond Myklebust, Anna Schumaker, Andrew Morton, linux-block,
	ceph-devel, linux-fsdevel, linux-ext4,
	open list:F2FS FILE SYSTEM, cluster-devel, linux-xfs, linux-nfs,
	linux-mm

On 5/19/23 18:35, Christoph Hellwig wrote:
> All callers of iomap_file_buffered_write need to updated ki_pos, move it
> into common code.
> 
> Signed-off-by: Christoph Hellwig <hch@lst.de>

One nit below.

Acked-by: Damien Le Moal <dlemoal@kernel.org>

> diff --git a/fs/iomap/buffered-io.c b/fs/iomap/buffered-io.c
> index 063133ec77f49e..550525a525c45c 100644
> --- a/fs/iomap/buffered-io.c
> +++ b/fs/iomap/buffered-io.c
> @@ -864,16 +864,19 @@ iomap_file_buffered_write(struct kiocb *iocb, struct iov_iter *i,
>  		.len		= iov_iter_count(i),
>  		.flags		= IOMAP_WRITE,
>  	};
> -	int ret;
> +	ssize_t ret;
>  
>  	if (iocb->ki_flags & IOCB_NOWAIT)
>  		iter.flags |= IOMAP_NOWAIT;
>  
>  	while ((ret = iomap_iter(&iter, ops)) > 0)
>  		iter.processed = iomap_write_iter(&iter, i);
> -	if (iter.pos == iocb->ki_pos)
> +
> +	if (unlikely(ret < 0))

Nit: This could be if (unlikely(ret <= 0)), no ?

>  		return ret;
> -	return iter.pos - iocb->ki_pos;
> +	ret = iter.pos - iocb->ki_pos;
> +	iocb->ki_pos += ret;
> +	return ret;


-- 
Damien Le Moal
Western Digital Research


^ permalink raw reply	[flat|nested] 38+ messages in thread

* Re: [PATCH 08/13] iomap: assign current->backing_dev_info in iomap_file_buffered_write
  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
  1 sibling, 0 replies; 38+ messages in thread
From: Damien Le Moal @ 2023-05-22  0:05 UTC (permalink / raw)
  To: Christoph Hellwig, Matthew Wilcox
  Cc: Jens Axboe, Xiubo Li, Ilya Dryomov, Alexander Viro,
	Christian Brauner, Theodore Ts'o, Jaegeuk Kim, Chao Yu,
	Miklos Szeredi, Andreas Gruenbacher, Darrick J. Wong,
	Trond Myklebust, Anna Schumaker, Andrew Morton, linux-block,
	ceph-devel, linux-fsdevel, linux-ext4,
	open list:F2FS FILE SYSTEM, cluster-devel, linux-xfs, linux-nfs,
	linux-mm

On 5/19/23 18:35, Christoph Hellwig wrote:
> Move the assignment to current->backing_dev_info from the callers into
> iomap_file_buffered_write to reduce boiler plate code and reduce the
> scope to just around the page dirtying loop.
> 
> Note that zonefs was missing this assignment before.

Hu... Shouldn't this be fixed as a separate patch with a Fixes tag for this cycle ?
I have never noticed any issues with this missing though. Not sure how an issue
can be triggered with this assignment missing.

Apart from that, this patch look good to me.

Reviewed-by: Damien Le Moal <dlemoal@kernel.org>

-- 
Damien Le Moal
Western Digital Research


^ permalink raw reply	[flat|nested] 38+ messages in thread

* Re: [PATCH 09/13] iomap: use kiocb_write_and_wait and kiocb_invalidate_pages
  2023-05-19  9:35 ` [PATCH 09/13] iomap: use kiocb_write_and_wait and kiocb_invalidate_pages Christoph Hellwig
@ 2023-05-22  0:07   ` Damien Le Moal
  0 siblings, 0 replies; 38+ messages in thread
From: Damien Le Moal @ 2023-05-22  0:07 UTC (permalink / raw)
  To: Christoph Hellwig, Matthew Wilcox
  Cc: Jens Axboe, Xiubo Li, Ilya Dryomov, Alexander Viro,
	Christian Brauner, Theodore Ts'o, Jaegeuk Kim, Chao Yu,
	Miklos Szeredi, Andreas Gruenbacher, Darrick J. Wong,
	Trond Myklebust, Anna Schumaker, Andrew Morton, linux-block,
	ceph-devel, linux-fsdevel, linux-ext4,
	open list:F2FS FILE SYSTEM, cluster-devel, linux-xfs, linux-nfs,
	linux-mm

On 5/19/23 18:35, Christoph Hellwig wrote:
> 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>

Looks OK to me.

Reviewed-by: Damien Le Moal <dlemoal@kernel.org>

-- 
Damien Le Moal
Western Digital Research


^ permalink raw reply	[flat|nested] 38+ messages in thread

* Re: [PATCH 10/13] fs: factor out a direct_write_fallback helper
  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
  1 sibling, 0 replies; 38+ messages in thread
From: Damien Le Moal @ 2023-05-22  0:16 UTC (permalink / raw)
  To: Christoph Hellwig, Matthew Wilcox
  Cc: Jens Axboe, Xiubo Li, Ilya Dryomov, Alexander Viro,
	Christian Brauner, Theodore Ts'o, Jaegeuk Kim, Chao Yu,
	Miklos Szeredi, Andreas Gruenbacher, Darrick J. Wong,
	Trond Myklebust, Anna Schumaker, Andrew Morton, linux-block,
	ceph-devel, linux-fsdevel, linux-ext4,
	open list:F2FS FILE SYSTEM, cluster-devel, linux-xfs, linux-nfs,
	linux-mm

On 5/19/23 18:35, Christoph Hellwig wrote:
> 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>

Looks OK. One comment below.

Reviewed-by: Damien Le Moal <dlemoal@kernel.org>

> +	/*
> +	 * 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;

Why not adding here something like:

	if (buffered_written != iov_iter_count(from))
		return -EIO;

	return direct_written + buffered_written;

to have the same semantic as plain DIO ?

-- 
Damien Le Moal
Western Digital Research


^ permalink raw reply	[flat|nested] 38+ messages in thread

* Re: [PATCH 11/13] fuse: update ki_pos in fuse_perform_write
  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
  0 siblings, 0 replies; 38+ messages in thread
From: Damien Le Moal @ 2023-05-22  0:18 UTC (permalink / raw)
  To: Christoph Hellwig, Matthew Wilcox
  Cc: Jens Axboe, Xiubo Li, Ilya Dryomov, Alexander Viro,
	Christian Brauner, Theodore Ts'o, Jaegeuk Kim, Chao Yu,
	Miklos Szeredi, Andreas Gruenbacher, Darrick J. Wong,
	Trond Myklebust, Anna Schumaker, Andrew Morton, linux-block,
	ceph-devel, linux-fsdevel, linux-ext4,
	open list:F2FS FILE SYSTEM, cluster-devel, linux-xfs, linux-nfs,
	linux-mm

On 5/19/23 18:35, Christoph Hellwig wrote:
> Both callers of fuse_perform_write need to updated ki_pos, move it into
> common code.
> 
> Signed-off-by: Christoph Hellwig <hch@lst.de>

Looks OK to me.

Reviewed-by: Damien Le Moal <dlemoal@kernel.org>

-- 
Damien Le Moal
Western Digital Research


^ permalink raw reply	[flat|nested] 38+ messages in thread

* Re: [PATCH 12/13] fuse: drop redundant arguments to fuse_perform_write
  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
  0 siblings, 0 replies; 38+ messages in thread
From: Damien Le Moal @ 2023-05-22  0:19 UTC (permalink / raw)
  To: Christoph Hellwig, Matthew Wilcox
  Cc: Jens Axboe, Xiubo Li, Ilya Dryomov, Alexander Viro,
	Christian Brauner, Theodore Ts'o, Jaegeuk Kim, Chao Yu,
	Miklos Szeredi, Andreas Gruenbacher, Darrick J. Wong,
	Trond Myklebust, Anna Schumaker, Andrew Morton, linux-block,
	ceph-devel, linux-fsdevel, linux-ext4,
	open list:F2FS FILE SYSTEM, cluster-devel, linux-xfs, linux-nfs,
	linux-mm

On 5/19/23 18:35, Christoph Hellwig wrote:
> pos is always equal to iocb->ki_pos, and mapping is always equal to
> iocb->ki_filp->f_mapping.
> 
> Signed-off-by: Christoph Hellwig <hch@lst.de>

Reviewed-by: Damien Le Moal <dlemoal@kernel.org>

-- 
Damien Le Moal
Western Digital Research


^ permalink raw reply	[flat|nested] 38+ messages in thread

* Re: [PATCH 13/13] fuse: use direct_write_fallback
  2023-05-19  9:35 ` [PATCH 13/13] fuse: use direct_write_fallback Christoph Hellwig
@ 2023-05-22  0:20   ` Damien Le Moal
  0 siblings, 0 replies; 38+ messages in thread
From: Damien Le Moal @ 2023-05-22  0:20 UTC (permalink / raw)
  To: Christoph Hellwig, Matthew Wilcox
  Cc: Jens Axboe, Xiubo Li, Ilya Dryomov, Alexander Viro,
	Christian Brauner, Theodore Ts'o, Jaegeuk Kim, Chao Yu,
	Miklos Szeredi, Andreas Gruenbacher, Darrick J. Wong,
	Trond Myklebust, Anna Schumaker, Andrew Morton, linux-block,
	ceph-devel, linux-fsdevel, linux-ext4,
	open list:F2FS FILE SYSTEM, cluster-devel, linux-xfs, linux-nfs,
	linux-mm

On 5/19/23 18:35, Christoph Hellwig wrote:
> Use the generic direct_write_fallback helper instead of duplicating the
> logic.
> 
> Signed-off-by: Christoph Hellwig <hch@lst.de>

Reviewed-by: Damien Le Moal <dlemoal@kernel.org>

-- 
Damien Le Moal
Western Digital Research


^ permalink raw reply	[flat|nested] 38+ messages in thread

* Re: [PATCH 02/13] filemap: update ki_pos in generic_perform_write
  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
  1 sibling, 0 replies; 38+ messages in thread
From: Xiubo Li @ 2023-05-22  2:20 UTC (permalink / raw)
  To: Christoph Hellwig, Matthew Wilcox
  Cc: Jens Axboe, Ilya Dryomov, Alexander Viro, Christian Brauner,
	Theodore Ts'o, Jaegeuk Kim, Chao Yu, Miklos Szeredi,
	Andreas Gruenbacher, Darrick J. Wong, Trond Myklebust,
	Anna Schumaker, Damien Le Moal, Andrew Morton, linux-block,
	ceph-devel, linux-fsdevel, linux-ext4,
	open list:F2FS FILE SYSTEM, cluster-devel, linux-xfs, linux-nfs,
	linux-mm


On 5/19/23 17:35, Christoph Hellwig wrote:
> All callers of generic_perform_write need to updated ki_pos, move it into
> common code.
>
> Signed-off-by: Christoph Hellwig <hch@lst.de>
> ---
>   fs/ceph/file.c | 2 --
>   fs/ext4/file.c | 9 +++------
>   fs/f2fs/file.c | 1 -
>   fs/nfs/file.c  | 1 -
>   mm/filemap.c   | 8 ++++----
>   5 files changed, 7 insertions(+), 14 deletions(-)
>
> diff --git a/fs/ceph/file.c b/fs/ceph/file.c
> index f4d8bf7dec88a8..feeb9882ef635a 100644
> --- a/fs/ceph/file.c
> +++ b/fs/ceph/file.c
> @@ -1894,8 +1894,6 @@ static ssize_t ceph_write_iter(struct kiocb *iocb, struct iov_iter *from)
>   		 * can not run at the same time
>   		 */
>   		written = generic_perform_write(iocb, from);
> -		if (likely(written >= 0))
> -			iocb->ki_pos = pos + written;
>   		ceph_end_io_write(inode);
>   	}
>   
> diff --git a/fs/ext4/file.c b/fs/ext4/file.c
> index d101b3b0c7dad8..50824831d31def 100644
> --- a/fs/ext4/file.c
> +++ b/fs/ext4/file.c
> @@ -291,12 +291,9 @@ static ssize_t ext4_buffered_write_iter(struct kiocb *iocb,
>   
>   out:
>   	inode_unlock(inode);
> -	if (likely(ret > 0)) {
> -		iocb->ki_pos += ret;
> -		ret = generic_write_sync(iocb, ret);
> -	}
> -
> -	return ret;
> +	if (unlikely(ret <= 0))
> +		return ret;
> +	return generic_write_sync(iocb, ret);
>   }
>   
>   static ssize_t ext4_handle_inode_extension(struct inode *inode, loff_t offset,
> diff --git a/fs/f2fs/file.c b/fs/f2fs/file.c
> index 5ac53d2627d20d..9e3855e43a7a63 100644
> --- a/fs/f2fs/file.c
> +++ b/fs/f2fs/file.c
> @@ -4522,7 +4522,6 @@ static ssize_t f2fs_buffered_write_iter(struct kiocb *iocb,
>   	current->backing_dev_info = NULL;
>   
>   	if (ret > 0) {
> -		iocb->ki_pos += ret;
>   		f2fs_update_iostat(F2FS_I_SB(inode), inode,
>   						APP_BUFFERED_IO, ret);
>   	}
> diff --git a/fs/nfs/file.c b/fs/nfs/file.c
> index f0edf5a36237d1..3cc87ae8473356 100644
> --- a/fs/nfs/file.c
> +++ b/fs/nfs/file.c
> @@ -658,7 +658,6 @@ ssize_t nfs_file_write(struct kiocb *iocb, struct iov_iter *from)
>   		goto out;
>   
>   	written = result;
> -	iocb->ki_pos += written;
>   	nfs_add_stats(inode, NFSIOS_NORMALWRITTENBYTES, written);
>   
>   	if (mntflags & NFS_MOUNT_WRITE_EAGER) {
> diff --git a/mm/filemap.c b/mm/filemap.c
> index b4c9bd368b7e58..4d0ec2fa1c7070 100644
> --- a/mm/filemap.c
> +++ b/mm/filemap.c
> @@ -3957,7 +3957,10 @@ ssize_t generic_perform_write(struct kiocb *iocb, struct iov_iter *i)
>   		balance_dirty_pages_ratelimited(mapping);
>   	} while (iov_iter_count(i));
>   
> -	return written ? written : status;
> +	if (!written)
> +		return status;
> +	iocb->ki_pos += written;
> +	return written;
>   }
>   EXPORT_SYMBOL(generic_perform_write);
>   
> @@ -4036,7 +4039,6 @@ ssize_t __generic_file_write_iter(struct kiocb *iocb, struct iov_iter *from)
>   		endbyte = pos + status - 1;
>   		err = filemap_write_and_wait_range(mapping, pos, endbyte);
>   		if (err == 0) {
> -			iocb->ki_pos = endbyte + 1;
>   			written += status;
>   			invalidate_mapping_pages(mapping,
>   						 pos >> PAGE_SHIFT,
> @@ -4049,8 +4051,6 @@ ssize_t __generic_file_write_iter(struct kiocb *iocb, struct iov_iter *from)
>   		}
>   	} else {
>   		written = generic_perform_write(iocb, from);
> -		if (likely(written > 0))
> -			iocb->ki_pos += written;
>   	}
>   out:
>   	current->backing_dev_info = NULL;

LGTM.

Reviewed-by: Xiubo Li <xiubli@redhat.com>

Thanks

- Xiubo



^ permalink raw reply	[flat|nested] 38+ messages in thread

* Re: [PATCH 03/13] filemap: assign current->backing_dev_info in generic_perform_write
  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
  1 sibling, 0 replies; 38+ messages in thread
From: Xiubo Li @ 2023-05-22  2:22 UTC (permalink / raw)
  To: Christoph Hellwig, Matthew Wilcox
  Cc: Jens Axboe, Ilya Dryomov, Alexander Viro, Christian Brauner,
	Theodore Ts'o, Jaegeuk Kim, Chao Yu, Miklos Szeredi,
	Andreas Gruenbacher, Darrick J. Wong, Trond Myklebust,
	Anna Schumaker, Damien Le Moal, Andrew Morton, linux-block,
	ceph-devel, linux-fsdevel, linux-ext4,
	open list:F2FS FILE SYSTEM, cluster-devel, linux-xfs, linux-nfs,
	linux-mm


On 5/19/23 17:35, Christoph Hellwig wrote:
> Move the assignment to current->backing_dev_info from the callers into
> generic_perform_write to reduce boiler plate code and reduce the scope
> to just around the page dirtying loop.
>
> Signed-off-by: Christoph Hellwig <hch@lst.de>
> ---
>   fs/ceph/file.c | 4 ----
>   fs/ext4/file.c | 3 ---
>   fs/f2fs/file.c | 2 --
>   fs/nfs/file.c  | 5 +----
>   mm/filemap.c   | 2 ++
>   5 files changed, 3 insertions(+), 13 deletions(-)
>
> diff --git a/fs/ceph/file.c b/fs/ceph/file.c
> index feeb9882ef635a..767f4dfe7def64 100644
> --- a/fs/ceph/file.c
> +++ b/fs/ceph/file.c
> @@ -1791,9 +1791,6 @@ static ssize_t ceph_write_iter(struct kiocb *iocb, struct iov_iter *from)
>   	else
>   		ceph_start_io_write(inode);
>   
> -	/* We can write back this queue in page reclaim */
> -	current->backing_dev_info = inode_to_bdi(inode);
> -
>   	if (iocb->ki_flags & IOCB_APPEND) {
>   		err = ceph_do_getattr(inode, CEPH_STAT_CAP_SIZE, false);
>   		if (err < 0)
> @@ -1938,7 +1935,6 @@ static ssize_t ceph_write_iter(struct kiocb *iocb, struct iov_iter *from)
>   		ceph_end_io_write(inode);
>   out_unlocked:
>   	ceph_free_cap_flush(prealloc_cf);
> -	current->backing_dev_info = NULL;
>   	return written ? written : err;
>   }
>   
> diff --git a/fs/ext4/file.c b/fs/ext4/file.c
> index 50824831d31def..3cb83a3e2e4a2a 100644
> --- a/fs/ext4/file.c
> +++ b/fs/ext4/file.c
> @@ -29,7 +29,6 @@
>   #include <linux/pagevec.h>
>   #include <linux/uio.h>
>   #include <linux/mman.h>
> -#include <linux/backing-dev.h>
>   #include "ext4.h"
>   #include "ext4_jbd2.h"
>   #include "xattr.h"
> @@ -285,9 +284,7 @@ static ssize_t ext4_buffered_write_iter(struct kiocb *iocb,
>   	if (ret <= 0)
>   		goto out;
>   
> -	current->backing_dev_info = inode_to_bdi(inode);
>   	ret = generic_perform_write(iocb, from);
> -	current->backing_dev_info = NULL;
>   
>   out:
>   	inode_unlock(inode);
> diff --git a/fs/f2fs/file.c b/fs/f2fs/file.c
> index 9e3855e43a7a63..7134fe8bd008cb 100644
> --- a/fs/f2fs/file.c
> +++ b/fs/f2fs/file.c
> @@ -4517,9 +4517,7 @@ static ssize_t f2fs_buffered_write_iter(struct kiocb *iocb,
>   	if (iocb->ki_flags & IOCB_NOWAIT)
>   		return -EOPNOTSUPP;
>   
> -	current->backing_dev_info = inode_to_bdi(inode);
>   	ret = generic_perform_write(iocb, from);
> -	current->backing_dev_info = NULL;
>   
>   	if (ret > 0) {
>   		f2fs_update_iostat(F2FS_I_SB(inode), inode,
> diff --git a/fs/nfs/file.c b/fs/nfs/file.c
> index 3cc87ae8473356..e8bb4c48a3210a 100644
> --- a/fs/nfs/file.c
> +++ b/fs/nfs/file.c
> @@ -648,11 +648,8 @@ ssize_t nfs_file_write(struct kiocb *iocb, struct iov_iter *from)
>   	since = filemap_sample_wb_err(file->f_mapping);
>   	nfs_start_io_write(inode);
>   	result = generic_write_checks(iocb, from);
> -	if (result > 0) {
> -		current->backing_dev_info = inode_to_bdi(inode);
> +	if (result > 0)
>   		result = generic_perform_write(iocb, from);
> -		current->backing_dev_info = NULL;
> -	}
>   	nfs_end_io_write(inode);
>   	if (result <= 0)
>   		goto out;
> diff --git a/mm/filemap.c b/mm/filemap.c
> index 4d0ec2fa1c7070..bf693ad1da1ece 100644
> --- a/mm/filemap.c
> +++ b/mm/filemap.c
> @@ -3892,6 +3892,7 @@ ssize_t generic_perform_write(struct kiocb *iocb, struct iov_iter *i)
>   	long status = 0;
>   	ssize_t written = 0;
>   
> +	current->backing_dev_info = inode_to_bdi(mapping->host);
>   	do {
>   		struct page *page;
>   		unsigned long offset;	/* Offset into pagecache page */
> @@ -3956,6 +3957,7 @@ ssize_t generic_perform_write(struct kiocb *iocb, struct iov_iter *i)
>   
>   		balance_dirty_pages_ratelimited(mapping);
>   	} while (iov_iter_count(i));
> +	current->backing_dev_info = NULL;
>   
>   	if (!written)
>   		return status;

LGTM.

Reviewed-by: Xiubo Li <xiubli@redhat.com>

Thanks

- Xiubo



^ permalink raw reply	[flat|nested] 38+ messages in thread

* Re: [PATCH 10/13] fs: factor out a direct_write_fallback helper
  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
  1 sibling, 1 reply; 38+ messages in thread
From: Miklos Szeredi @ 2023-05-22 14:19 UTC (permalink / raw)
  To: Christoph Hellwig
  Cc: Matthew Wilcox, Jens Axboe, Xiubo Li, Ilya Dryomov,
	Alexander Viro, Christian Brauner, Theodore Ts'o,
	Jaegeuk Kim, Chao Yu, Andreas Gruenbacher, Darrick J. Wong,
	Trond Myklebust, Anna Schumaker, Damien Le Moal, Andrew Morton,
	linux-block, ceph-devel, linux-fsdevel, linux-ext4,
	open list:F2FS FILE SYSTEM, cluster-devel, linux-xfs, linux-nfs,
	linux-mm

On Fri, 19 May 2023 at 11:36, Christoph Hellwig <hch@lst.de> wrote:
>
> 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;

At this point pos will point after the end of the buffered write (as
per earlier patches), yes?

> +       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;

So this calculation is wrong.

AFAICS this affects later patches as well.

Thanks,
Miklos

^ permalink raw reply	[flat|nested] 38+ messages in thread

* Re: [PATCH 08/13] iomap: assign current->backing_dev_info in iomap_file_buffered_write
  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
  1 sibling, 1 reply; 38+ messages in thread
From: Darrick J. Wong @ 2023-05-23  1:06 UTC (permalink / raw)
  To: Christoph Hellwig
  Cc: Matthew Wilcox, Jens Axboe, Xiubo Li, Ilya Dryomov,
	Alexander Viro, Christian Brauner, Theodore Ts'o,
	Jaegeuk Kim, Chao Yu, Miklos Szeredi, Andreas Gruenbacher,
	Trond Myklebust, Anna Schumaker, Damien Le Moal, Andrew Morton,
	linux-block, ceph-devel, linux-fsdevel, linux-ext4,
	open list:F2FS FILE SYSTEM, cluster-devel, linux-xfs, linux-nfs,
	linux-mm

On Fri, May 19, 2023 at 11:35:16AM +0200, Christoph Hellwig wrote:
> Move the assignment to current->backing_dev_info from the callers into
> iomap_file_buffered_write to reduce boiler plate code and reduce the
> scope to just around the page dirtying loop.
> 
> Note that zonefs was missing this assignment before.

I'm still wondering (a) what the hell current->backing_dev_info is for,
and (b) if we need it around the iomap_unshare operation.

$ git grep current..backing_dev_info
fs/btrfs/file.c:1148:   current->backing_dev_info = inode_to_bdi(inode);
fs/btrfs/file.c:1169:                   current->backing_dev_info = NULL;
fs/btrfs/file.c:1692:   current->backing_dev_info = NULL;
fs/ceph/file.c:1795:    current->backing_dev_info = inode_to_bdi(inode);
fs/ceph/file.c:1943:    current->backing_dev_info = NULL;
fs/ext4/file.c:288:     current->backing_dev_info = inode_to_bdi(inode);
fs/ext4/file.c:290:     current->backing_dev_info = NULL;
fs/f2fs/file.c:4520:    current->backing_dev_info = inode_to_bdi(inode);
fs/f2fs/file.c:4522:    current->backing_dev_info = NULL;
fs/fuse/file.c:1366:    current->backing_dev_info = inode_to_bdi(inode);
fs/fuse/file.c:1412:    current->backing_dev_info = NULL;
fs/gfs2/file.c:1044:    current->backing_dev_info = inode_to_bdi(inode);
fs/gfs2/file.c:1048:    current->backing_dev_info = NULL;
fs/nfs/file.c:652:              current->backing_dev_info = inode_to_bdi(inode);
fs/nfs/file.c:654:              current->backing_dev_info = NULL;
fs/ntfs/file.c:1914:    current->backing_dev_info = inode_to_bdi(vi);
fs/ntfs/file.c:1918:    current->backing_dev_info = NULL;
fs/ntfs3/file.c:823:    current->backing_dev_info = inode_to_bdi(inode);
fs/ntfs3/file.c:996:    current->backing_dev_info = NULL;
fs/xfs/xfs_file.c:721:  current->backing_dev_info = inode_to_bdi(inode);
fs/xfs/xfs_file.c:756:  current->backing_dev_info = NULL;
mm/filemap.c:3995:      current->backing_dev_info = inode_to_bdi(inode);
mm/filemap.c:4056:      current->backing_dev_info = NULL;

AFAICT nobody uses it at all?  Unless there's some bizarre user that
isn't extracting it from @current?

Oh, hey, new question (c) isn't this set incorrectly for xfs realtime
files?

--D

> Signed-off-by: Christoph Hellwig <hch@lst.de>
> ---
>  fs/gfs2/file.c         | 3 ---
>  fs/iomap/buffered-io.c | 3 +++
>  fs/xfs/xfs_file.c      | 5 -----
>  3 files changed, 3 insertions(+), 8 deletions(-)
> 
> diff --git a/fs/gfs2/file.c b/fs/gfs2/file.c
> index 499ef174dec138..261897fcfbc495 100644
> --- a/fs/gfs2/file.c
> +++ b/fs/gfs2/file.c
> @@ -25,7 +25,6 @@
>  #include <linux/dlm.h>
>  #include <linux/dlm_plock.h>
>  #include <linux/delay.h>
> -#include <linux/backing-dev.h>
>  #include <linux/fileattr.h>
>  
>  #include "gfs2.h"
> @@ -1041,11 +1040,9 @@ static ssize_t gfs2_file_buffered_write(struct kiocb *iocb,
>  			goto out_unlock;
>  	}
>  
> -	current->backing_dev_info = inode_to_bdi(inode);
>  	pagefault_disable();
>  	ret = iomap_file_buffered_write(iocb, from, &gfs2_iomap_ops);
>  	pagefault_enable();
> -	current->backing_dev_info = NULL;
>  	if (ret > 0)
>  		written += ret;
>  
> diff --git a/fs/iomap/buffered-io.c b/fs/iomap/buffered-io.c
> index 550525a525c45c..b2779bd1f10611 100644
> --- a/fs/iomap/buffered-io.c
> +++ b/fs/iomap/buffered-io.c
> @@ -3,6 +3,7 @@
>   * Copyright (C) 2010 Red Hat, Inc.
>   * Copyright (C) 2016-2019 Christoph Hellwig.
>   */
> +#include <linux/backing-dev.h>
>  #include <linux/module.h>
>  #include <linux/compiler.h>
>  #include <linux/fs.h>
> @@ -869,8 +870,10 @@ iomap_file_buffered_write(struct kiocb *iocb, struct iov_iter *i,
>  	if (iocb->ki_flags & IOCB_NOWAIT)
>  		iter.flags |= IOMAP_NOWAIT;
>  
> +	current->backing_dev_info = inode_to_bdi(iter.inode);
>  	while ((ret = iomap_iter(&iter, ops)) > 0)
>  		iter.processed = iomap_write_iter(&iter, i);
> +	current->backing_dev_info = NULL;
>  
>  	if (unlikely(ret < 0))
>  		return ret;
> diff --git a/fs/xfs/xfs_file.c b/fs/xfs/xfs_file.c
> index bfba10e0b0f3c2..98d763cc3b114c 100644
> --- a/fs/xfs/xfs_file.c
> +++ b/fs/xfs/xfs_file.c
> @@ -27,7 +27,6 @@
>  
>  #include <linux/dax.h>
>  #include <linux/falloc.h>
> -#include <linux/backing-dev.h>
>  #include <linux/mman.h>
>  #include <linux/fadvise.h>
>  #include <linux/mount.h>
> @@ -717,9 +716,6 @@ xfs_file_buffered_write(
>  	if (ret)
>  		goto out;
>  
> -	/* We can write back this queue in page reclaim */
> -	current->backing_dev_info = inode_to_bdi(inode);
> -
>  	trace_xfs_file_buffered_write(iocb, from);
>  	ret = iomap_file_buffered_write(iocb, from,
>  			&xfs_buffered_write_iomap_ops);
> @@ -751,7 +747,6 @@ xfs_file_buffered_write(
>  		goto write_retry;
>  	}
>  
> -	current->backing_dev_info = NULL;
>  out:
>  	if (iolock)
>  		xfs_iunlock(ip, iolock);
> -- 
> 2.39.2
> 

^ permalink raw reply	[flat|nested] 38+ messages in thread

* Re: cleanup the filemap / direct I/O interaction
  2023-05-19  9:35 cleanup the filemap / direct I/O interaction Christoph Hellwig
                   ` (12 preceding siblings ...)
  2023-05-19  9:35 ` [PATCH 13/13] fuse: use direct_write_fallback Christoph Hellwig
@ 2023-05-23  1:12 ` Darrick J. Wong
  13 siblings, 0 replies; 38+ messages in thread
From: Darrick J. Wong @ 2023-05-23  1:12 UTC (permalink / raw)
  To: Christoph Hellwig
  Cc: Matthew Wilcox, Jens Axboe, Xiubo Li, Ilya Dryomov,
	Alexander Viro, Christian Brauner, Theodore Ts'o,
	Jaegeuk Kim, Chao Yu, Miklos Szeredi, Andreas Gruenbacher,
	Trond Myklebust, Anna Schumaker, Damien Le Moal, Andrew Morton,
	linux-block, ceph-devel, linux-fsdevel, linux-ext4,
	open list:F2FS FILE SYSTEM, cluster-devel, linux-xfs, linux-nfs,
	linux-mm

On Fri, May 19, 2023 at 11:35:08AM +0200, Christoph Hellwig wrote:
> Hi all,
> 
> this series cleans up some of the generic write helper calling
> conventions and the page cache writeback / invalidation for
> direct I/O.  This is a spinoff from the no-bufferhead kernel
> project, for while we'll want to an use iomap based buffered
> write path in the block layer.

Heh.

For patches 3 and 8, I wonder if you could just get rid of
current->backing_dev_info?

For patches 2, 4-6, and 10:
Acked-by: Darrick J. Wong <djwong@kernel.org>

For patches 1, 7, and 9:
Reviewed-by: Darrick J. Wong <djwong@kernel.org>

The fuse patches I have no idea about. :/

--D

> diffstat:
>  block/fops.c            |   18 ----
>  fs/ceph/file.c          |    6 -
>  fs/direct-io.c          |   10 --
>  fs/ext4/file.c          |   12 ---
>  fs/f2fs/file.c          |    3 
>  fs/fuse/file.c          |   47 ++----------
>  fs/gfs2/file.c          |    7 -
>  fs/iomap/buffered-io.c  |   12 ++-
>  fs/iomap/direct-io.c    |   88 ++++++++--------------
>  fs/libfs.c              |   36 +++++++++
>  fs/nfs/file.c           |    6 -
>  fs/xfs/xfs_file.c       |    7 -
>  fs/zonefs/file.c        |    4 -
>  include/linux/fs.h      |    7 -
>  include/linux/pagemap.h |    4 +
>  mm/filemap.c            |  184 +++++++++++++++++++++---------------------------
>  16 files changed, 190 insertions(+), 261 deletions(-)

^ permalink raw reply	[flat|nested] 38+ messages in thread

* Re: [PATCH 08/13] iomap: assign current->backing_dev_info in iomap_file_buffered_write
  2023-05-23  1:06   ` Darrick J. Wong
@ 2023-05-23  3:30     ` Matthew Wilcox
  2023-05-23 16:02       ` Christoph Hellwig
  0 siblings, 1 reply; 38+ messages in thread
From: Matthew Wilcox @ 2023-05-23  3:30 UTC (permalink / raw)
  To: Darrick J. Wong
  Cc: Christoph Hellwig, Jens Axboe, Xiubo Li, Ilya Dryomov,
	Alexander Viro, Christian Brauner, Theodore Ts'o,
	Jaegeuk Kim, Chao Yu, Miklos Szeredi, Andreas Gruenbacher,
	Trond Myklebust, Anna Schumaker, Damien Le Moal, Andrew Morton,
	linux-block, ceph-devel, linux-fsdevel, linux-ext4,
	open list:F2FS FILE SYSTEM, cluster-devel, linux-xfs, linux-nfs,
	linux-mm, NeilBrown

On Mon, May 22, 2023 at 06:06:27PM -0700, Darrick J. Wong wrote:
> On Fri, May 19, 2023 at 11:35:16AM +0200, Christoph Hellwig wrote:
> > Move the assignment to current->backing_dev_info from the callers into
> > iomap_file_buffered_write to reduce boiler plate code and reduce the
> > scope to just around the page dirtying loop.
> > 
> > Note that zonefs was missing this assignment before.
> 
> I'm still wondering (a) what the hell current->backing_dev_info is for,
> and (b) if we need it around the iomap_unshare operation.
> 
> $ git grep current..backing_dev_info
[results show it only set, never used]
> 
> AFAICT nobody uses it at all?  Unless there's some bizarre user that
> isn't extracting it from @current?
> 
> Oh, hey, new question (c) isn't this set incorrectly for xfs realtime
> files?

Some git archaelogy ...

This was first introduced in commit 2f45a06517a62 (in the
linux-fullhistory tree) in 2002 by one Andrew Morton.  At the time,
it added this check to the page scanner:

+                               if (page->pte.direct ||
+                                       page->mapping->backing_dev_info ==
+                                               current->backing_dev_info) {
+                                       wait_on_page_writeback(page);
+                               }

AFAICT (the code went through some metamorphoses in the intervening
twenty years), the last use of it ended up in current_may_throttle(),
and it was removed in March 2022 by Neil Brown in commit b9b1335e6403.
Since then, there have been no users of task->backing_dev_info, and I'm
pretty sure it can go away.

^ permalink raw reply	[flat|nested] 38+ messages in thread

* Re: [PATCH 06/13] filemap: add a kiocb_invalidate_post_write helper
  2023-05-21 23:56   ` Damien Le Moal
@ 2023-05-23 16:01     ` Christoph Hellwig
  0 siblings, 0 replies; 38+ messages in thread
From: Christoph Hellwig @ 2023-05-23 16:01 UTC (permalink / raw)
  To: Damien Le Moal
  Cc: Christoph Hellwig, Matthew Wilcox, Jens Axboe, Xiubo Li,
	Ilya Dryomov, Alexander Viro, Christian Brauner,
	Theodore Ts'o, Jaegeuk Kim, Chao Yu, Miklos Szeredi,
	Andreas Gruenbacher, Darrick J. Wong, Trond Myklebust,
	Anna Schumaker, Andrew Morton, linux-block, ceph-devel,
	linux-fsdevel, linux-ext4, open list:F2FS FILE SYSTEM,
	cluster-devel, linux-xfs, linux-nfs, linux-mm

On Mon, May 22, 2023 at 08:56:34AM +0900, Damien Le Moal wrote:
> On 5/19/23 18:35, Christoph Hellwig wrote:
> > Add a helper to invalidate page cache after a dio write.
> > 
> > Signed-off-by: Christoph Hellwig <hch@lst.de>
> 
> Nit: kiocb_invalidate_post_dio_write() may be a better name to be explicit about
> the fact that this is for DIOs only ?

I've renamed it to kiocb_invalidate_post_direct_write, thanks.

^ permalink raw reply	[flat|nested] 38+ messages in thread

* Re: [PATCH 07/13] iomap: update ki_pos in iomap_file_buffered_write
  2023-05-22  0:01   ` Damien Le Moal
@ 2023-05-23 16:02     ` Christoph Hellwig
  0 siblings, 0 replies; 38+ messages in thread
From: Christoph Hellwig @ 2023-05-23 16:02 UTC (permalink / raw)
  To: Damien Le Moal
  Cc: Christoph Hellwig, Matthew Wilcox, Jens Axboe, Xiubo Li,
	Ilya Dryomov, Alexander Viro, Christian Brauner,
	Theodore Ts'o, Jaegeuk Kim, Chao Yu, Miklos Szeredi,
	Andreas Gruenbacher, Darrick J. Wong, Trond Myklebust,
	Anna Schumaker, Andrew Morton, linux-block, ceph-devel,
	linux-fsdevel, linux-ext4, open list:F2FS FILE SYSTEM,
	cluster-devel, linux-xfs, linux-nfs, linux-mm

On Mon, May 22, 2023 at 09:01:05AM +0900, Damien Le Moal wrote:
> > -	int ret;
> > +	ssize_t ret;
> >  
> >  	if (iocb->ki_flags & IOCB_NOWAIT)
> >  		iter.flags |= IOMAP_NOWAIT;
> >  
> >  	while ((ret = iomap_iter(&iter, ops)) > 0)
> >  		iter.processed = iomap_write_iter(&iter, i);
> > -	if (iter.pos == iocb->ki_pos)
> > +
> > +	if (unlikely(ret < 0))
> 
> Nit: This could be if (unlikely(ret <= 0)), no ?

No.  iomap_iter does not return te amount of bytes written.

^ permalink raw reply	[flat|nested] 38+ messages in thread

* Re: [PATCH 08/13] iomap: assign current->backing_dev_info in iomap_file_buffered_write
  2023-05-23  3:30     ` Matthew Wilcox
@ 2023-05-23 16:02       ` Christoph Hellwig
  0 siblings, 0 replies; 38+ messages in thread
From: Christoph Hellwig @ 2023-05-23 16:02 UTC (permalink / raw)
  To: Matthew Wilcox
  Cc: Darrick J. Wong, Christoph Hellwig, Jens Axboe, Xiubo Li,
	Ilya Dryomov, Alexander Viro, Christian Brauner,
	Theodore Ts'o, Jaegeuk Kim, Chao Yu, Miklos Szeredi,
	Andreas Gruenbacher, Trond Myklebust, Anna Schumaker,
	Damien Le Moal, Andrew Morton, linux-block, ceph-devel,
	linux-fsdevel, linux-ext4, open list:F2FS FILE SYSTEM,
	cluster-devel, linux-xfs, linux-nfs, linux-mm, NeilBrown

On Tue, May 23, 2023 at 04:30:51AM +0100, Matthew Wilcox wrote:
> AFAICT (the code went through some metamorphoses in the intervening
> twenty years), the last use of it ended up in current_may_throttle(),
> and it was removed in March 2022 by Neil Brown in commit b9b1335e6403.
> Since then, there have been no users of task->backing_dev_info, and I'm
> pretty sure it can go away.

Oh, nice.  I hadn't noticed it finally went away.  The next iteration
of the series will just remove it.

^ permalink raw reply	[flat|nested] 38+ messages in thread

* Re: [PATCH 10/13] fs: factor out a direct_write_fallback helper
  2023-05-22 14:19   ` Miklos Szeredi
@ 2023-05-23 16:03     ` Christoph Hellwig
  0 siblings, 0 replies; 38+ messages in thread
From: Christoph Hellwig @ 2023-05-23 16:03 UTC (permalink / raw)
  To: Miklos Szeredi
  Cc: Christoph Hellwig, Matthew Wilcox, Jens Axboe, Xiubo Li,
	Ilya Dryomov, Alexander Viro, Christian Brauner,
	Theodore Ts'o, Jaegeuk Kim, Chao Yu, Andreas Gruenbacher,
	Darrick J. Wong, Trond Myklebust, Anna Schumaker, Damien Le Moal,
	Andrew Morton, linux-block, ceph-devel, linux-fsdevel,
	linux-ext4, open list:F2FS FILE SYSTEM, cluster-devel, linux-xfs,
	linux-nfs, linux-mm

On Mon, May 22, 2023 at 04:19:38PM +0200, Miklos Szeredi wrote:
> > +               ssize_t direct_written, ssize_t buffered_written)
> > +{
> > +       struct address_space *mapping = iocb->ki_filp->f_mapping;
> > +       loff_t pos = iocb->ki_pos, end;
> 
> At this point pos will point after the end of the buffered write (as
> per earlier patches), yes?

Yes.  I'll fix the pos and end calculation.


^ permalink raw reply	[flat|nested] 38+ messages in thread

* Re: [f2fs-dev] [PATCH 01/13] iomap: update ki_pos a little later in iomap_dio_complete
  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   ` patchwork-bot+f2fs
  1 sibling, 0 replies; 38+ messages in thread
From: patchwork-bot+f2fs @ 2023-07-06  0:18 UTC (permalink / raw)
  To: Christoph Hellwig
  Cc: willy, djwong, linux-mm, agruenba, miklos, cluster-devel,
	idryomov, linux-ext4, linux-nfs, linux-block, dlemoal, viro,
	jaegeuk, ceph-devel, xiubli, trond.myklebust, axboe, brauner,
	tytso, linux-f2fs-devel, linux-xfs, anna, linux-fsdevel, akpm

Hello:

This series was applied to jaegeuk/f2fs.git (dev)
by Andrew Morton <akpm@linux-foundation.org>:

On Fri, 19 May 2023 11:35:09 +0200 you wrote:
> Move the ki_pos update down a bit to prepare for a better common
> helper that invalidates pages based of an iocb.
> 
> Signed-off-by: Christoph Hellwig <hch@lst.de>
> ---
>  fs/iomap/direct-io.c | 21 +++++++++++----------
>  1 file changed, 11 insertions(+), 10 deletions(-)

Here is the summary with links:
  - [f2fs-dev,01/13] iomap: update ki_pos a little later in iomap_dio_complete
    https://git.kernel.org/jaegeuk/f2fs/c/936e114a245b
  - [f2fs-dev,02/13] filemap: update ki_pos in generic_perform_write
    (no matching commit)
  - [f2fs-dev,03/13] filemap: assign current->backing_dev_info in generic_perform_write
    (no matching commit)
  - [f2fs-dev,04/13] filemap: add a kiocb_write_and_wait helper
    https://git.kernel.org/jaegeuk/f2fs/c/3c435a0fe35c
  - [f2fs-dev,05/13] filemap: add a kiocb_invalidate_pages helper
    https://git.kernel.org/jaegeuk/f2fs/c/e003f74afbd2
  - [f2fs-dev,06/13] filemap: add a kiocb_invalidate_post_write helper
    (no matching commit)
  - [f2fs-dev,07/13] iomap: update ki_pos in iomap_file_buffered_write
    (no matching commit)
  - [f2fs-dev,08/13] iomap: assign current->backing_dev_info in iomap_file_buffered_write
    (no matching commit)
  - [f2fs-dev,09/13] iomap: use kiocb_write_and_wait and kiocb_invalidate_pages
    https://git.kernel.org/jaegeuk/f2fs/c/8ee93b4bb626
  - [f2fs-dev,10/13] fs: factor out a direct_write_fallback helper
    (no matching commit)
  - [f2fs-dev,11/13] fuse: update ki_pos in fuse_perform_write
    (no matching commit)
  - [f2fs-dev,12/13] fuse: drop redundant arguments to fuse_perform_write
    (no matching commit)
  - [f2fs-dev,13/13] fuse: use direct_write_fallback
    (no matching commit)

You are awesome, thank you!
-- 
Deet-doot-dot, I am a bot.
https://korg.docs.kernel.org/patchwork/pwbot.html



^ permalink raw reply	[flat|nested] 38+ messages in thread

end of thread, other threads:[~2023-07-06  0:18 UTC | newest]

Thread overview: 38+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
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 ` [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 ` [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

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).