Skip to contentSkip to conversation

fuse: fix fuse_create_open() error path

PATCH In review Miklos Szeredi 3 participants 9 messages Nov 26 FUSE filesystem core

lore
Share & tools
lore
#0 lore raw

PATCH 1/6 fuse: fix fuse_create_open() error path

Don't release fuse_file twice if finish_open() already set FMODE_OPENED.

This is triggered by calling open(..., O_CREAT | O_DIRECT) on a inode created with FUSE_ATTR_DAX. Since a_ops->direct_IO is unset, this will trip the late O_DIRECT check in do_dentry_open().

Fixes 2fbe4586cff3 ("fuse: add support for opening dax device as backing")
---
 fs/fuse/dir.c | 12 ++++++++++--
 1 file changed, 10 insertions(+), 2 deletions(-)
fs/fuse/dir.c +10 -2
index a04d93d5fc44..af1d91c7a181 100644
@@ -921,8 +921,16 @@ static int fuse_create_open(const struct mnt_idmap *idmap, struct inode *dir,
 		err = finish_open(file, entry, fuse_finish_open);
 	}
 	if (err) {
-		fi = get_fuse_inode(inode);
-		fuse_sync_release(fi, ff, flags);
+		if (!(file->f_mode & FMODE_OPENED)) {
+			/*
+			 * Only release ff here if the file was not opened.  If
+			 * finish_open() failed after having set FMODE_OPENED, then the
+			 * VFS owns the file and will call fuse_release(), which
+			 * releases ff.
+			 */
+			fi = get_fuse_inode(inode);
+			fuse_sync_release(fi, ff, flags);
+		}
 	} else {
 		if (fm->fc->atomic_o_trunc && trunc)
 			truncate_pagecache(inode, 0);
Signature · 1 line
2.56.1
#1 lore raw

PATCH 2/6 fuse: allow O_DIRECT opens of dax inodes

Set ->direct_IO to noop_direct_IO, as other filesystems do whose ->read_iter/->write_iter handle IOCB_DIRECT themselves. Beyond the gating this has no runtime effect: FOPEN_PASSTHROUGH is mandatory for a dax inode, so reads and writes are served by fuse_direct_*_iter() or fuse_passthrough_*_iter(), neither of which consults the address space operations, and the noop_direct_IO body cannot be reached.

Document why these operations look the way they do while at it.

Fixes 2fbe4586cff3 ("fuse: add support for opening dax device as backing")
---
 fs/fuse/inode.c | 12 ++++++++++++
 1 file changed, 12 insertions(+)
fs/fuse/inode.c +12 -0
index af6d9e0af190..20fbc99f3590 100644
@@ -403,8 +403,20 @@ static void fuse_init_submount_lookup(struct fuse_submount_lookup *sl,
 	refcount_set(&sl->count, 1);
 }
 
+/*
+ * Address space operations for an inode backed by a passthrough extent map on
+ * a dax device.  Such an inode never uses the page cache: FOPEN_PASSTHROUGH is
+ * mandatory for a dax inode (see fuse_file_io_open()), so all I/O goes through
+ * the passthrough handlers and none of the generic page cache helpers can be
+ * reached.  That is why there's no ->read_folio here.
+ *
+ * ->direct_IO is never called for the same reason, but the pointer still needs
+ * to be non-NULL, because that's what do_dentry_open() and fcntl() look at to
+ * decide whether O_DIRECT is allowed on the file.
+ */
 static const struct address_space_operations fuse_dax_aops = {
 	.dirty_folio	= noop_dirty_folio,
+	.direct_IO	= noop_direct_IO,
 };
 
 static void fuse_init_inode(struct inode *inode, struct fuse_attr *attr,
Signature · 1 line
2.56.1
Reviewed-by on v1 2/6
#2 lore raw

Re: [PATCH 2/6] fuse: allow O_DIRECT opens of dax inodes

Quoted 30 lines · Miklos Szeredi

Set ->direct_IO to noop_direct_IO, as other filesystems do whose ->read_iter/->write_iter handle IOCB_DIRECT themselves. Beyond the gating this has no runtime effect: FOPEN_PASSTHROUGH is mandatory for a dax inode, so reads and writes are served by fuse_direct_*_iter() or fuse_passthrough_*_iter(), neither of which consults the address space operations, and the noop_direct_IO body cannot be reached.

Document why these operations look the way they do while at it.

Fixes 2fbe4586cff3 ("fuse: add support for opening dax device as backing")
---
 fs/fuse/inode.c | 12 ++++++++++++
 1 file changed, 12 insertions(+)
fs/fuse/inode.c +7 -0
index af6d9e0af190..20fbc99f3590 100644
@@ -403,8 +403,20 @@ static void fuse_init_submount_lookup(struct fuse_submount_lookup *sl,
 	refcount_set(&sl->count, 1);
 }
 
+/*
+ * Address space operations for an inode backed by a passthrough extent map on
+ * a dax device.  Such an inode never uses the page cache: FOPEN_PASSTHROUGH is
+ * mandatory for a dax inode (see fuse_file_io_open()), so all I/O goes through
+ * the passthrough handlers and none of the generic page cache helpers can be
+ * reached.  That is why there's no ->read_folio here.
+ *

fs/fuse/inode.c static void fuse_init_submount_lookup(struct fuse_submount_lookup *sl, · line 422

Quoted 10 lines
fs/fuse/inode.c +5 -0
+ * ->direct_IO is never called for the same reason, but the pointer still needs
+ * to be non-NULL, because that's what do_dentry_open() and fcntl() look at to
+ * decide whether O_DIRECT is allowed on the file.
+ */
 static const struct address_space_operations fuse_dax_aops = {
 	.dirty_folio	= noop_dirty_folio,
+	.direct_IO	= noop_direct_IO,
 };
 
 static void fuse_init_inode(struct inode *inode, struct fuse_attr *attr,
#3 lore raw

PATCH 3/6 fuse: don't invalidate page cache of dax inodes on create

In reply to Miklos Szeredi

Commit 2fbe4586cff3 ("fuse: add support for opening dax device as backing") added an !IS_DAX() check to the cache invalidation in fuse_open(), but the identical invalidation in fuse_create_open() was left untouched.

Add the missing !IS_DAX() check for symmetry with fuse_open().

Fixes 2fbe4586cff3 ("fuse: add support for opening dax device as backing")
---
 fs/fuse/dir.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
fs/fuse/dir.c +1 -1
index af1d91c7a181..8cbaec16497d 100644
@@ -934,7 +934,7 @@ static int fuse_create_open(const struct mnt_idmap *idmap, struct inode *dir,
 	} else {
 		if (fm->fc->atomic_o_trunc && trunc)
 			truncate_pagecache(inode, 0);
-		else if (!(ff->open_flags & FOPEN_KEEP_CACHE))
+		else if (!(ff->open_flags & FOPEN_KEEP_CACHE) && !IS_DAX(inode))
 			invalidate_inode_pages2(inode->i_mapping);
 	}
 	return err;
Signature · 1 line
2.56.1
#4 lore raw

PATCH 4/6 fuse: don't truncate under a shared inode lock in fuse_direct_IO()

In reply to Miklos Szeredi

On a failed write fuse_direct_IO() rolls back the i_size extension done by a partially completed extending write.

	/* For extending writes we already hold exclusive lock */
	if (ret < 0 && offset + count > i_size)
		fuse_do_truncate(file);

The comment states the safety argument: if the write extends past EOF, then fuse_dio_lock() must have taken the inode lock exclusively, so fuse_do_truncate() -> fuse_do_setattr() -> fuse_set_nowrite() is serialized against other truncates.

That argument does not hold, because the two sides test different snapshots of i_size. fuse_dio_wr_exclusive_lock() decides on the lock mode using fuse_io_past_eof(), and fuse_direct_IO() re-reads i_size_read() afterwards. i_size is not protected by the inode lock in fuse, only by fi->lock, so it can shrink in between.

Fix this by only rolling back when the inode lock is held exclusively, which is what fuse_do_truncate() requires. Pass that down from fuse_direct_write_iter(), which already knows; the ->direct_IO instance is only reached for writes through generic_file_direct_write() from fuse_cache_write_iter(), which always holds the lock exclusively. It is also reached for reads from generic_file_read_iter() with no inode lock held at all, but the rollback is write-only, so the flag is ignored there.

Skipping the rollback in the shared case is correct: the write was non-extending when the lock mode was decided, so there is nothing to roll back. Should a concurrent shrink have left i_size larger than the server's size, fuse_invalidate_attr_mask(FUSE_STATX_MODSIZE) has already been called and the next getattr resyncs it.

Fixes 153524053bbb ("fuse: allow non-extending parallel direct writes on the same file")
---
 fs/fuse/file.c | 35 +++++++++++++++++++++++++++++------
 1 file changed, 29 insertions(+), 6 deletions(-)
fs/fuse/file.c +29 -6
index 993a1d2b3151..545d29dedcd7 100644
@@ -1825,14 +1825,15 @@ static ssize_t __fuse_direct_read(struct fuse_io_priv *io,
 	return res;
 }
 
-static ssize_t fuse_direct_IO(struct kiocb *iocb, struct iov_iter *iter);
+static ssize_t __fuse_direct_IO(struct kiocb *iocb, struct iov_iter *iter,
+				bool exclusive);
 
 static ssize_t fuse_direct_read_iter(struct kiocb *iocb, struct iov_iter *to)
 {
 	ssize_t res;
 
 	if (!is_sync_kiocb(iocb)) {
-		res = fuse_direct_IO(iocb, to);
+		res = __fuse_direct_IO(iocb, to, true);
 	} else {
 		struct fuse_io_priv io = FUSE_IO_PRIV_SYNC(iocb);
 
@@ -1856,7 +1857,7 @@ static ssize_t fuse_direct_write_iter(struct kiocb *iocb, struct iov_iter *from)
 
 		task_io_account_write(res);
 		if (!is_sync_kiocb(iocb)) {
-			res = fuse_direct_IO(iocb, from);
+			res = __fuse_direct_IO(iocb, from, exclusive);
 		} else {
 			struct fuse_io_priv io = FUSE_IO_PRIV_SYNC(iocb);
 
@@ -2774,8 +2775,13 @@ static inline loff_t fuse_round_up(struct fuse_conn *fc, loff_t off)
 	return round_up(off, fc->max_pages << PAGE_SHIFT);
 }
 
+/*
+ * @exclusive tells whether the inode lock is held exclusively.  Only then is
+ * it allowed to truncate the file in the error path below.  It is only looked
+ * at for writes, so read callers may pass anything.
+ */
 static ssize_t
-fuse_direct_IO(struct kiocb *iocb, struct iov_iter *iter)
+__fuse_direct_IO(struct kiocb *iocb, struct iov_iter *iter, bool exclusive)
 {
 	DECLARE_COMPLETION_ONSTACK(wait);
 	ssize_t ret = 0;
@@ -2869,14 +2875,31 @@ fuse_direct_IO(struct kiocb *iocb, struct iov_iter *iter)
 
 	if (iov_iter_rw(iter) == WRITE) {
 		fuse_write_update_attr(inode, pos, ret);
-		/* For extending writes we already hold exclusive lock */
-		if (ret < 0 && offset + count > i_size)
+		/*
+		 * Rolling back a failed extending write needs the exclusive
+		 * inode lock.  The i_size read above may have raced with a
+		 * concurrent shrink by the server, in which case this is not
+		 * an extending write after all and there's nothing to roll
+		 * back; the attributes were invalidated above anyway.
+		 */
+		if (exclusive && ret < 0 && offset + count > i_size)
 			fuse_do_truncate(file);
 	}
 
 	return ret;
 }
 
+/*
+ * Writes only get here through generic_file_direct_write(), which is always
+ * invoked with the inode lock held exclusively.  Reads also get here from
+ * generic_file_read_iter() without any inode lock, but @exclusive is not
+ * looked at in that case.
+ */
+static ssize_t fuse_direct_IO(struct kiocb *iocb, struct iov_iter *iter)
+{
+	return __fuse_direct_IO(iocb, iter, true);
+}
+
 static int fuse_writeback_range(struct inode *inode, loff_t start, loff_t end)
 {
 	int err = filemap_write_and_wait_range(inode->i_mapping, start, LLONG_MAX);
Signature · 1 line
2.56.1
#5 lore raw

PATCH 5/6 fuse: fix readdir cache size overflow on 32-bit

In reply to Miklos Szeredi

fuse_add_dirent_to_cache() computed the new size of the readdir cache as

	fi->rdc.size = (index << PAGE_SHIFT) + offset + reclen;

where index is a pgoff_t. On 32-bit architectures the shift is evaluated in 32-bit arithmetic, so it wraps once the cache grows beyond 4GiB (page index 1048576), and the truncated result is then stored into the 64-bit fi->rdc.size.

Instead of fixing up the shift, keep the position of the new entry in a loff_t from the start and derive the page index from that. The same value is used both for the offset within the folio and for the new cache size, so no 32-bit intermediate remains.

---
 fs/fuse/readdir.c | 12 +++++++-----
 1 file changed, 7 insertions(+), 5 deletions(-)
fs/fuse/readdir.c +7 -5
index c1e2a8fd834e..750e88fbfb4e 100644
@@ -36,7 +36,7 @@ static void fuse_add_dirent_to_cache(struct file *file,
 	size_t reclen = FUSE_DIRENT_SIZE(dirent);
 	pgoff_t index;
 	struct folio *folio;
-	loff_t size;
+	loff_t size, cache_pos;
 	u64 version;
 	unsigned int offset;
 	size_t folio_offset;
@@ -57,12 +57,14 @@ static void fuse_add_dirent_to_cache(struct file *file,
 	version = fi->rdc.version;
 	size = fi->rdc.size;
 	offset = offset_in_page(size);
-	index = size >> PAGE_SHIFT;
 	/* Dirent doesn't fit in current page?  Jump to next page. */
 	if (offset + reclen > PAGE_SIZE) {
-		index++;
+		cache_pos = PAGE_ALIGN(size);
 		offset = 0;
+	} else {
+		cache_pos = size;
 	}
+	index = cache_pos >> PAGE_SHIFT;
 	spin_unlock(&fi->rdc.lock);
 
 	if (offset)
@@ -78,7 +80,7 @@ static void fuse_add_dirent_to_cache(struct file *file,
 	    WARN_ON(fi->rdc.pos != pos))
 		goto unlock;
 
-	folio_offset = offset_in_folio(folio, (index << PAGE_SHIFT) + offset);
+	folio_offset = offset_in_folio(folio, cache_pos);
 	if (!offset) {
 		folio_fill_tail(folio, folio_offset, (const char *)dirent,
 				reclen);
@@ -87,7 +89,7 @@ static void fuse_add_dirent_to_cache(struct file *file,
 		memcpy_to_folio(folio, folio_offset, (const char *)dirent,
 				reclen);
 	}
-	fi->rdc.size = (index << PAGE_SHIFT) + offset + reclen;
+	fi->rdc.size = cache_pos + reclen;
 	fi->rdc.pos = dirent->off;
 unlock:
 	spin_unlock(&fi->rdc.lock);
Signature · 1 line
2.56.1
#6 lore raw

PATCH 6/6 fuse: reject FUSE_NOTIFY_STORE on a DAX inode

In reply to Miklos Szeredi

Reject the notification; a server sending it for a DAX inode is violating the protocol, so fail loudly.

Fixes e8bc74b6ed1b ("fuse: add extent map I/O support")
---
 fs/fuse/notify.c | 8 ++++++++
 1 file changed, 8 insertions(+)
fs/fuse/notify.c +8 -0
index 984e4945e3e3..272423dfd261 100644
@@ -167,6 +167,14 @@ static int fuse_notify_store(struct fuse_conn *fc, unsigned int size,
 		err = -EINVAL;
 		goto out_iput;
 	}
+	/*
+	 * A dax inode has no page cache: its mapping holds dax entries, which
+	 * must never be mixed with folios.
+	 */
+	if (IS_DAX(inode)) {
+		err = fuse_EIO("NOTIFY_STORE on DAX inode");
+		goto out_iput;
+	}
 
 	mapping = inode->i_mapping;
 	file_size = i_size_read(inode);
Signature · 1 line
2.56.1
Reviewed-by on v1 1/6
#7 lore raw
In reply to Miklos Szeredi
Quoted 27 lines · Miklos Szeredi

Don't release fuse_file twice if finish_open() already set FMODE_OPENED.

This is triggered by calling open(..., O_CREAT | O_DIRECT) on a inode created with FUSE_ATTR_DAX. Since a_ops->direct_IO is unset, this will trip the late O_DIRECT check in do_dentry_open().

Fixes 2fbe4586cff3 ("fuse: add support for opening dax device as backing")
---
 fs/fuse/dir.c | 12 ++++++++++--
 1 file changed, 10 insertions(+), 2 deletions(-)
fs/fuse/dir.c +3 -2
index a04d93d5fc44..af1d91c7a181 100644
@@ -921,8 +921,16 @@ static int fuse_create_open(const struct mnt_idmap *idmap, struct inode *dir,
 		err = finish_open(file, entry, fuse_finish_open);
 	}
 	if (err) {
-		fi = get_fuse_inode(inode);
-		fuse_sync_release(fi, ff, flags);
+		if (!(file->f_mode & FMODE_OPENED)) {
+			/*
+			 * Only release ff here if the file was not opened.  If

fs/fuse/dir.c static int fuse_create_open(const struct mnt_idmap *idmap, struct inode *dir, · line 936

Quoted 10 lines
fs/fuse/dir.c +7 -0
+			 * finish_open() failed after having set FMODE_OPENED, then the
+			 * VFS owns the file and will call fuse_release(), which
+			 * releases ff.
+			 */
+			fi = get_fuse_inode(inode);
+			fuse_sync_release(fi, ff, flags);
+		}
 	} else {
 		if (fm->fc->atomic_o_trunc && trunc)
 			truncate_pagecache(inode, 0);

Basically the same fix as submitted by Jimma, just with a more verbose comment

https://lore.kernel.org/r/[email protected]

and still wonder if we should add in fuse_finish_open()

+ if ((file->f_flags & O_DIRECT) && !file->f_mapping->a_ops->direct_IO)
+ return -EINVAL;
+

Still looks good to me,

maintainer
#8 lore raw
Quoted 45 lines · Bernd Schubert
Quoted 27 lines · Miklos Szeredi

Don't release fuse_file twice if finish_open() already set FMODE_OPENED.

This is triggered by calling open(..., O_CREAT | O_DIRECT) on a inode created with FUSE_ATTR_DAX. Since a_ops->direct_IO is unset, this will trip the late O_DIRECT check in do_dentry_open().

Fixes 2fbe4586cff3 ("fuse: add support for opening dax device as backing")
---
 fs/fuse/dir.c | 12 ++++++++++--
 1 file changed, 10 insertions(+), 2 deletions(-)
fs/fuse/dir.c +3 -2
index a04d93d5fc44..af1d91c7a181 100644
@@ -921,8 +921,16 @@ static int fuse_create_open(const struct mnt_idmap *idmap, struct inode *dir,
              err = finish_open(file, entry, fuse_finish_open);
      }
      if (err) {
-             fi = get_fuse_inode(inode);
-             fuse_sync_release(fi, ff, flags);
+             if (!(file->f_mode & FMODE_OPENED)) {
+                     /*
+                      * Only release ff here if the file was not opened.  If

fs/fuse/dir.c static int fuse_create_open(const struct mnt_idmap *idmap, struct inode *dir, · line 936

Quoted 10 lines
fs/fuse/dir.c +7 -0
+                      * finish_open() failed after having set FMODE_OPENED, then the
+                      * VFS owns the file and will call fuse_release(), which
+                      * releases ff.
+                      */
+                     fi = get_fuse_inode(inode);
+                     fuse_sync_release(fi, ff, flags);
+             }
      } else {
              if (fm->fc->atomic_o_trunc && trunc)
                      truncate_pagecache(inode, 0);

Basically the same fix as submitted by Jimma, just with a more verbose comment

Quoted 1 line

https://lore.kernel.org/r/[email protected]

Yeah, sorry. Mailbox crisis still not over...

Thanks for the review.

Miklos