Skip to contentSkip to conversation

fuse: invalidate readdir cache on epoch bump

PATCH Superseded Jun Wu 4 participants 9 messages Mar 31 – Jun 11 1 message not in the archive

Subsystems (2)
lore
Share & tools
lore

Jun Wu

author
#0 lore raw

PATCH 1/1 fuse: invalidate readdir cache on epoch bump

From: Jun Wu <[email protected]>

FUSE_NOTIFY_INC_EPOCH invalidates dentries, but does not invalidate cached readdir results. A process with cwd inside a FUSE mount can therefore observe stale readdir(".") output after an epoch bump.

Fix this by recording epoch in the readdir cache and checking it on reuse.

Add FUSE_EPOCH_READDIR init flag so userspace can detect this change.

Minimal reproducer:

- mount a tiny FUSE fs with an empty root directory
- on opendir, enable fi->cache_readdir and fi->keep_cache
- chdir into the mount and call readdir(".") to populate readdir cache
- make the FUSE server report one file in the root directory
- send only FUSE_NOTIFY_INC_EPOCH
- call readdir(".") again; before this change it stays stale, after this change it sees the new file

---
 fs/fuse/dev.c             | 6 +++---
 fs/fuse/fuse_i.h          | 3 +++
 fs/fuse/inode.c           | 2 +-
 fs/fuse/readdir.c         | 5 ++++-
 include/uapi/linux/fuse.h | 3 +++
 5 files changed, 14 insertions(+), 5 deletions(-)
fs/fuse/dev.c +3 -3
index 0b0241f47170..595f90f44772 100644
@@ -2039,9 +2039,9 @@ static int fuse_notify_resend(struct fuse_conn *fc)
 }
 
 /*
- * Increments the fuse connection epoch.  This will result of dentries from
- * previous epochs to be invalidated.  Additionally, if inval_wq is set, a work
- * queue is scheduled to trigger the invalidation.
+ * Increments the fuse connection epoch.  This will cause dentries and
+ * readdir caches from previous epochs to be invalidated.  Additionally,
+ * if inval_wq is set, a work queue is scheduled to trigger the invalidation.
  */
 static int fuse_notify_inc_epoch(struct fuse_conn *fc)
 {
fs/fuse/fuse_i.h +3 -0
index 7f16049387d1..9b479f597938 100644
@@ -191,6 +191,9 @@ struct fuse_inode {
 			/* iversion of directory when cache was started */
 			u64 iversion;
 
+			/* epoch of fc when cache was started */
+			int epoch;
+
 			/* protects above fields */
 			spinlock_t lock;
 		} rdc;
fs/fuse/inode.c +1 -1
index c795abe47a4f..5685e696ee2f 100644
@@ -1506,7 +1506,7 @@ static struct fuse_init_args *fuse_new_init(struct fuse_mount *fm)
 		FUSE_SECURITY_CTX | FUSE_CREATE_SUPP_GROUP |
 		FUSE_HAS_EXPIRE_ONLY | FUSE_DIRECT_IO_ALLOW_MMAP |
 		FUSE_NO_EXPORT_SUPPORT | FUSE_HAS_RESEND | FUSE_ALLOW_IDMAP |
-		FUSE_REQUEST_TIMEOUT;
+		FUSE_REQUEST_TIMEOUT | FUSE_EPOCH_READDIR;
 #ifdef CONFIG_FUSE_DAX
 	if (fm->fc->dax)
 		flags |= FUSE_MAP_ALIGNMENT;
fs/fuse/readdir.c +4 -1
index c2aae2eef086..240b0e993780 100644
@@ -438,6 +438,7 @@ static void fuse_rdc_reset(struct inode *inode)
 	fi->rdc.version++;
 	fi->rdc.size = 0;
 	fi->rdc.pos = 0;
+	fi->rdc.epoch = 0;
 }
 
 #define UNCACHED 1
@@ -479,6 +480,7 @@ static int fuse_readdir_cached(struct file *file, struct dir_context *ctx)
 		if (!ctx->pos && !fi->rdc.size) {
 			fi->rdc.mtime = inode_get_mtime(inode);
 			fi->rdc.iversion = inode_query_iversion(inode);
+			fi->rdc.epoch = atomic_read(&fc->epoch);
 		}
 		spin_unlock(&fi->rdc.lock);
 		return UNCACHED;
@@ -492,7 +494,8 @@ static int fuse_readdir_cached(struct file *file, struct dir_context *ctx)
 		struct timespec64 mtime = inode_get_mtime(inode);
 
 		if (inode_peek_iversion(inode) != fi->rdc.iversion ||
-		    !timespec64_equal(&fi->rdc.mtime, &mtime)) {
+		    !timespec64_equal(&fi->rdc.mtime, &mtime) ||
+		    fi->rdc.epoch != atomic_read(&fc->epoch)) {
 			fuse_rdc_reset(inode);
 			goto retry_locked;
 		}
include/uapi/linux/fuse.h +3 -0
index c13e1f9a2f12..b0bd96ac1e84 100644
@@ -240,6 +240,7 @@
  *  - add FUSE_COPY_FILE_RANGE_64
  *  - add struct fuse_copy_file_range_out
  *  - add FUSE_NOTIFY_PRUNE
+ *  - add FUSE_EPOCH_READDIR
  */
 
 #ifndef _LINUX_FUSE_H
@@ -448,6 +449,7 @@ struct fuse_file_lock {
  * FUSE_OVER_IO_URING: Indicate that client supports io-uring
  * FUSE_REQUEST_TIMEOUT: kernel supports timing out requests.
  *			 init_out.request_timeout contains the timeout (in secs)
+ * FUSE_EPOCH_READDIR: epoch bump also invalidates readdir caches
  */
 #define FUSE_ASYNC_READ		(1 << 0)
 #define FUSE_POSIX_LOCKS	(1 << 1)
@@ -495,6 +497,7 @@ struct fuse_file_lock {
 #define FUSE_ALLOW_IDMAP	(1ULL << 40)
 #define FUSE_OVER_IO_URING	(1ULL << 41)
 #define FUSE_REQUEST_TIMEOUT	(1ULL << 42)
+#define FUSE_EPOCH_READDIR	(1ULL << 43)
 
 /**
  * CUSE INIT request/reply flags
Signature · 1 line
2.53.0
#1 lore raw
Quoted 10 lines · Jun Wu

From: Jun Wu <[email protected]>

FUSE_NOTIFY_INC_EPOCH invalidates dentries, but does not invalidate cached readdir results. A process with cwd inside a FUSE mount can therefore observe stale readdir(".") output after an epoch bump.

Fix this by recording epoch in the readdir cache and checking it on reuse.

Add FUSE_EPOCH_READDIR init flag so userspace can detect this change.

imo this is more of a fix than a new feature, so I don't think the changes for adding the FUSE_EPOCH_READDIR init flag is necessary. I think the Fixes: 2396356a945b ("fuse: add more control over cache invalidation behaviour") tag fits here.

Other than that, this change makes sense to me.

Thanks,
Joanne

Quoted 126 lines

Minimal reproducer:

- mount a tiny FUSE fs with an empty root directory
- on opendir, enable fi->cache_readdir and fi->keep_cache
- chdir into the mount and call readdir(".") to populate readdir cache
- make the FUSE server report one file in the root directory
- send only FUSE_NOTIFY_INC_EPOCH
- call readdir(".") again; before this change it stays stale, after this change it sees the new file

---
 fs/fuse/dev.c             | 6 +++---
 fs/fuse/fuse_i.h          | 3 +++
 fs/fuse/inode.c           | 2 +-
 fs/fuse/readdir.c         | 5 ++++-
 include/uapi/linux/fuse.h | 3 +++
 5 files changed, 14 insertions(+), 5 deletions(-)
fs/fuse/dev.c +3 -3
index 0b0241f47170..595f90f44772 100644
@@ -2039,9 +2039,9 @@ static int fuse_notify_resend(struct fuse_conn *fc)
 }
 
 /*
- * Increments the fuse connection epoch.  This will result of dentries from
- * previous epochs to be invalidated.  Additionally, if inval_wq is set, a work
- * queue is scheduled to trigger the invalidation.
+ * Increments the fuse connection epoch.  This will cause dentries and
+ * readdir caches from previous epochs to be invalidated.  Additionally,
+ * if inval_wq is set, a work queue is scheduled to trigger the invalidation.
  */
 static int fuse_notify_inc_epoch(struct fuse_conn *fc)
 {
fs/fuse/fuse_i.h +3 -0
index 7f16049387d1..9b479f597938 100644
@@ -191,6 +191,9 @@ struct fuse_inode {
                        /* iversion of directory when cache was started */
                        u64 iversion;
 
+                       /* epoch of fc when cache was started */
+                       int epoch;
+
                        /* protects above fields */
                        spinlock_t lock;
                } rdc;
fs/fuse/inode.c +1 -1
index c795abe47a4f..5685e696ee2f 100644
@@ -1506,7 +1506,7 @@ static struct fuse_init_args *fuse_new_init(struct fuse_mount *fm)
                FUSE_SECURITY_CTX | FUSE_CREATE_SUPP_GROUP |
                FUSE_HAS_EXPIRE_ONLY | FUSE_DIRECT_IO_ALLOW_MMAP |
                FUSE_NO_EXPORT_SUPPORT | FUSE_HAS_RESEND | FUSE_ALLOW_IDMAP |
-               FUSE_REQUEST_TIMEOUT;
+               FUSE_REQUEST_TIMEOUT | FUSE_EPOCH_READDIR;
 #ifdef CONFIG_FUSE_DAX
        if (fm->fc->dax)
                flags |= FUSE_MAP_ALIGNMENT;
fs/fuse/readdir.c +4 -1
index c2aae2eef086..240b0e993780 100644
@@ -438,6 +438,7 @@ static void fuse_rdc_reset(struct inode *inode)
        fi->rdc.version++;
        fi->rdc.size = 0;
        fi->rdc.pos = 0;
+       fi->rdc.epoch = 0;
 }
 
 #define UNCACHED 1
@@ -479,6 +480,7 @@ static int fuse_readdir_cached(struct file *file, struct dir_context *ctx)
                if (!ctx->pos && !fi->rdc.size) {
                        fi->rdc.mtime = inode_get_mtime(inode);
                        fi->rdc.iversion = inode_query_iversion(inode);
+                       fi->rdc.epoch = atomic_read(&fc->epoch);
                }
                spin_unlock(&fi->rdc.lock);
                return UNCACHED;
@@ -492,7 +494,8 @@ static int fuse_readdir_cached(struct file *file, struct dir_context *ctx)
                struct timespec64 mtime = inode_get_mtime(inode);
 
                if (inode_peek_iversion(inode) != fi->rdc.iversion ||
-                   !timespec64_equal(&fi->rdc.mtime, &mtime)) {
+                   !timespec64_equal(&fi->rdc.mtime, &mtime) ||
+                   fi->rdc.epoch != atomic_read(&fc->epoch)) {
                        fuse_rdc_reset(inode);
                        goto retry_locked;
                }
include/uapi/linux/fuse.h +3 -1
index c13e1f9a2f12..b0bd96ac1e84 100644
@@ -240,6 +240,7 @@
  *  - add FUSE_COPY_FILE_RANGE_64
  *  - add struct fuse_copy_file_range_out
  *  - add FUSE_NOTIFY_PRUNE
+ *  - add FUSE_EPOCH_READDIR
  */
 
 #ifndef _LINUX_FUSE_H
@@ -448,6 +449,7 @@ struct fuse_file_lock {
  * FUSE_OVER_IO_URING: Indicate that client supports io-uring
  * FUSE_REQUEST_TIMEOUT: kernel supports timing out requests.
  *                      init_out.request_timeout contains the timeout (in secs)
+ * FUSE_EPOCH_READDIR: epoch bump also invalidates readdir caches
  */
 #define FUSE_ASYNC_READ                (1 << 0)
 #define FUSE_POSIX_LOCKS       (1 << 1)
@@ -495,6 +497,7 @@ struct fuse_file_lock {
 #define FUSE_ALLOW_IDMAP       (1ULL << 40)
 #define FUSE_OVER_IO_URING     (1ULL << 41)
 #define FUSE_REQUEST_TIMEOUT   (1ULL << 42)
+#define FUSE_EPOCH_READDIR     (1ULL << 43)
 
 /**
  * CUSE INIT request/reply flags
--

2.53.0

author
#2 lore raw

PATCH v2 1/1 fuse: invalidate readdir cache on epoch bump

In reply to Jun Wu

From: Jun Wu <[email protected]>

FUSE_NOTIFY_INC_EPOCH invalidates dentries, but does not invalidate cached readdir results. A process with cwd inside a FUSE mount can therefore observe stale readdir(".") output after an epoch bump.

Fix this by recording epoch in the readdir cache and checking it on reuse.

Minimal reproducer:

- mount a tiny FUSE fs with an empty root directory
- on opendir, enable fi->cache_readdir and fi->keep_cache
- chdir into the mount and call readdir(".") to populate readdir cache
- make the FUSE server report one file in the root directory
- send only FUSE_NOTIFY_INC_EPOCH
- call readdir(".") again; before this change it stays stale, after this change it sees the new file

Fixes 2396356a945b ("fuse: add more control over cache invalidation behaviour")

---
Changes in v2:
- Addresses Joanne Koong's suggestions:
- Drop the FUSE_EPOCH_READDIR init flag.
- Add Fixes tag.
- Rebase onto current mainline.

---
 fs/fuse/dev.c     | 6 +++---
 fs/fuse/fuse_i.h  | 3 +++
 fs/fuse/readdir.c | 5 ++++-
 3 files changed, 10 insertions(+), 4 deletions(-)
fs/fuse/dev.c +3 -3
index 5dda7080f4a9..35e04b064f37 100644
@@ -2038,9 +2038,9 @@ static int fuse_notify_resend(struct fuse_conn *fc)
 }
 
 /*
- * Increments the fuse connection epoch.  This will result of dentries from
- * previous epochs to be invalidated.  Additionally, if inval_wq is set, a work
- * queue is scheduled to trigger the invalidation.
+ * Increments the fuse connection epoch.  This will cause dentries and
+ * readdir caches from previous epochs to be invalidated.  Additionally,
+ * if inval_wq is set, a work queue is scheduled to trigger the invalidation.
  */
 static int fuse_notify_inc_epoch(struct fuse_conn *fc)
 {
fs/fuse/fuse_i.h +3 -0
index 17423d4e3cfa..aa8d6dfbbcc7 100644
@@ -191,6 +191,9 @@ struct fuse_inode {
 			/* iversion of directory when cache was started */
 			u64 iversion;
 
+			/* epoch of fc when cache was started */
+			int epoch;
+
 			/* protects above fields */
 			spinlock_t lock;
 		} rdc;
fs/fuse/readdir.c +4 -1
index db5ae8ec1030..b18d971e2bb4 100644
@@ -442,6 +442,7 @@ static void fuse_rdc_reset(struct inode *inode)
 	fi->rdc.version++;
 	fi->rdc.size = 0;
 	fi->rdc.pos = 0;
+	fi->rdc.epoch = 0;
 }
 
 #define UNCACHED 1
@@ -483,6 +484,7 @@ static int fuse_readdir_cached(struct file *file, struct dir_context *ctx)
 		if (!ctx->pos && !fi->rdc.size) {
 			fi->rdc.mtime = inode_get_mtime(inode);
 			fi->rdc.iversion = inode_query_iversion(inode);
+			fi->rdc.epoch = atomic_read(&fc->epoch);
 		}
 		spin_unlock(&fi->rdc.lock);
 		return UNCACHED;
@@ -496,7 +498,8 @@ static int fuse_readdir_cached(struct file *file, struct dir_context *ctx)
 		struct timespec64 mtime = inode_get_mtime(inode);
 
 		if (inode_peek_iversion(inode) != fi->rdc.iversion ||
-		    !timespec64_equal(&fi->rdc.mtime, &mtime)) {
+		    !timespec64_equal(&fi->rdc.mtime, &mtime) ||
+		    fi->rdc.epoch != atomic_read(&fc->epoch)) {
 			fuse_rdc_reset(inode);
 			goto retry_locked;
 		}
Signature · 1 line
2.54.0
Reviewed-by on v2 1/1
#3 lore raw
Quoted 18 lines · Jun Wu

From: Jun Wu <[email protected]>

FUSE_NOTIFY_INC_EPOCH invalidates dentries, but does not invalidate cached readdir results. A process with cwd inside a FUSE mount can therefore observe stale readdir(".") output after an epoch bump.

Fix this by recording epoch in the readdir cache and checking it on reuse.

Minimal reproducer:

- mount a tiny FUSE fs with an empty root directory
- on opendir, enable fi->cache_readdir and fi->keep_cache
- chdir into the mount and call readdir(".") to populate readdir cache
- make the FUSE server report one file in the root directory
- send only FUSE_NOTIFY_INC_EPOCH
- call readdir(".") again; before this change it stays stale, after this change it sees the new file

Quoted 2 lines
Fixes 2396356a945b ("fuse: add more control over cache invalidation behaviour")

Nice catch! The fix looks good to me, so feel free to add my

Cheers,

Signature · 1 line
Luís
Quoted 76 lines

---
Changes in v2:
- Addresses Joanne Koong's suggestions:
- Drop the FUSE_EPOCH_READDIR init flag.
- Add Fixes tag.
- Rebase onto current mainline.

---
 fs/fuse/dev.c     | 6 +++---
 fs/fuse/fuse_i.h  | 3 +++
 fs/fuse/readdir.c | 5 ++++-
 3 files changed, 10 insertions(+), 4 deletions(-)
fs/fuse/dev.c +3 -3
index 5dda7080f4a9..35e04b064f37 100644
@@ -2038,9 +2038,9 @@ static int fuse_notify_resend(struct fuse_conn *fc)
 }
 
 /*
- * Increments the fuse connection epoch.  This will result of dentries from
- * previous epochs to be invalidated.  Additionally, if inval_wq is set, a work
- * queue is scheduled to trigger the invalidation.
+ * Increments the fuse connection epoch.  This will cause dentries and
+ * readdir caches from previous epochs to be invalidated.  Additionally,
+ * if inval_wq is set, a work queue is scheduled to trigger the invalidation.
  */
 static int fuse_notify_inc_epoch(struct fuse_conn *fc)
 {
fs/fuse/fuse_i.h +3 -0
index 17423d4e3cfa..aa8d6dfbbcc7 100644
@@ -191,6 +191,9 @@ struct fuse_inode {
 			/* iversion of directory when cache was started */
 			u64 iversion;
 
+			/* epoch of fc when cache was started */
+			int epoch;
+
 			/* protects above fields */
 			spinlock_t lock;
 		} rdc;
fs/fuse/readdir.c +4 -1
index db5ae8ec1030..b18d971e2bb4 100644
@@ -442,6 +442,7 @@ static void fuse_rdc_reset(struct inode *inode)
 	fi->rdc.version++;
 	fi->rdc.size = 0;
 	fi->rdc.pos = 0;
+	fi->rdc.epoch = 0;
 }
 
 #define UNCACHED 1
@@ -483,6 +484,7 @@ static int fuse_readdir_cached(struct file *file, struct dir_context *ctx)
 		if (!ctx->pos && !fi->rdc.size) {
 			fi->rdc.mtime = inode_get_mtime(inode);
 			fi->rdc.iversion = inode_query_iversion(inode);
+			fi->rdc.epoch = atomic_read(&fc->epoch);
 		}
 		spin_unlock(&fi->rdc.lock);
 		return UNCACHED;
@@ -496,7 +498,8 @@ static int fuse_readdir_cached(struct file *file, struct dir_context *ctx)
 		struct timespec64 mtime = inode_get_mtime(inode);
 
 		if (inode_peek_iversion(inode) != fi->rdc.iversion ||
-		    !timespec64_equal(&fi->rdc.mtime, &mtime)) {
+		    !timespec64_equal(&fi->rdc.mtime, &mtime) ||
+		    fi->rdc.epoch != atomic_read(&fc->epoch)) {
 			fuse_rdc_reset(inode);
 			goto retry_locked;
 		}
Signature · 1 line
2.54.0
Reviewed-by
#4 lore raw
Quoted 21 lines · Luis Henriques
Quoted 18 lines · Jun Wu

From: Jun Wu <[email protected]>

FUSE_NOTIFY_INC_EPOCH invalidates dentries, but does not invalidate cached readdir results. A process with cwd inside a FUSE mount can therefore observe stale readdir(".") output after an epoch bump.

Fix this by recording epoch in the readdir cache and checking it on reuse.

Minimal reproducer:

- mount a tiny FUSE fs with an empty root directory
- on opendir, enable fi->cache_readdir and fi->keep_cache
- chdir into the mount and call readdir(".") to populate readdir cache
- make the FUSE server report one file in the root directory
- send only FUSE_NOTIFY_INC_EPOCH
- call readdir(".") again; before this change it stays stale, after this change it sees the new file

Quoted 2 lines
Quoted 2 lines
Fixes 2396356a945b ("fuse: add more control over cache invalidation behaviour")

(btw, I think your v2 email may have gotten corrupted when sending. I wasn't able to find it in my inbox and had to manually import Luis's. I'm not seeing it show up on lore [1] either)

Thanks,
Joanne

[1] https://lore.kernel.org/fuse-devel/CAJnrk1YOzwNRS+qA74rLMFmx=JgE3VVueu9o7-pghsa4bPzYBw@mail.gmail.com/T/#t

#5 lore raw
Quoted 22 lines · Joanne Koong
Quoted 21 lines · Luis Henriques
Quoted 18 lines · Jun Wu

From: Jun Wu <[email protected]>

FUSE_NOTIFY_INC_EPOCH invalidates dentries, but does not invalidate cached readdir results. A process with cwd inside a FUSE mount can therefore observe stale readdir(".") output after an epoch bump.

Fix this by recording epoch in the readdir cache and checking it on reuse.

Minimal reproducer:

- mount a tiny FUSE fs with an empty root directory
- on opendir, enable fi->cache_readdir and fi->keep_cache
- chdir into the mount and call readdir(".") to populate readdir cache
- make the FUSE server report one file in the root directory
- send only FUSE_NOTIFY_INC_EPOCH
- call readdir(".") again; before this change it stays stale, after this change it sees the new file

Quoted 8 lines
Quoted 2 lines
Quoted 2 lines
Fixes 2396356a945b ("fuse: add more control over cache invalidation behaviour")

(btw, I think your v2 email may have gotten corrupted when sending. I wasn't able to find it in my inbox and had to manually import Luis's. I'm not seeing it show up on lore [1] either)

Hmm... yeah, looks like there's something funny with his email. The mail server I use seems to be more tolerant than lore regarding incorrect email. So maybe lore blocked it because the domain and the smtp server address don't match...? Anyway, maybe it's worth resending so that everyone can see it.

Cheers,

Signature · 1 line
Luís

A message that is not in the archive (sent off-list or not yet received): CAFfKmRW_Dqtb_DwAgK5Oq5kTAnGY6bmRpYP-Gw2+PVBtU4YVtQ@mail.gmail.com

#7 lore raw
Quoted 3 lines · Jun Wu

Thanks for your review! I think my DNS records had expired when sending, and retrying after fixing the DNS didn't help. I will resend with a regular Gmail address.

Awesome, thanks. I can confirm v3 is now on the mailing-list, though this email I'm replying to seems to have been dropped again :-)

Cheers,

Signature · 1 line
Luís
Quoted 58 lines

Regards,
Jun

Quoted 47 lines · Luis Henriques
Quoted 7 lines · Joanne Koong
Quoted 6 lines · Luis Henriques
Quoted 3 lines · Jun Wu

From: Jun Wu <[email protected]>

FUSE_NOTIFY_INC_EPOCH invalidates dentries, but does not invalidate

cached

Quoted 4 lines
Quoted 4 lines
Quoted 4 lines

readdir results. A process with cwd inside a FUSE mount can therefore observe stale readdir(".") output after an epoch bump.

Fix this by recording epoch in the readdir cache and checking it on

reuse.

Quoted 9 lines
Quoted 9 lines
Quoted 9 lines

Minimal reproducer:

- mount a tiny FUSE fs with an empty root directory
- on opendir, enable fi->cache_readdir and fi->keep_cache
- chdir into the mount and call readdir(".") to populate readdir cache
- make the FUSE server report one file in the root directory
- send only FUSE_NOTIFY_INC_EPOCH
- call readdir(".") again; before this change it stays stale, after

this

Quoted 3 lines
Quoted 3 lines
Quoted 3 lines

change it sees the new file

Fixes 2396356a945b ("fuse: add more control over cache invalidation

behaviour")

Quoted 7 lines
Quoted 1 line
Quoted 1 line

(btw, I think your v2 email may have gotten corrupted when sending. I wasn't able to find it in my inbox and had to manually import Luis's. I'm not seeing it show up on lore [1] either)

Hmm... yeah, looks like there's something funny with his email. The mail server I use seems to be more tolerant than lore regarding incorrect email. So maybe lore blocked it because the domain and the smtp server address don't match...? Anyway, maybe it's worth resending so that everyone can see it.

Cheers,
--
Luís

Signature · 2 lines
WU Jun
<[email protected]>
maintainer Applied
#8 lore raw
In reply to Jun Wu
Quoted 19 lines · Jun Wu

From: Jun Wu <[email protected]>

FUSE_NOTIFY_INC_EPOCH invalidates dentries, but does not invalidate cached readdir results. A process with cwd inside a FUSE mount can therefore observe stale readdir(".") output after an epoch bump.

Fix this by recording epoch in the readdir cache and checking it on reuse.

Minimal reproducer:

- mount a tiny FUSE fs with an empty root directory
- on opendir, enable fi->cache_readdir and fi->keep_cache
- chdir into the mount and call readdir(".") to populate readdir cache
- make the FUSE server report one file in the root directory
- send only FUSE_NOTIFY_INC_EPOCH
- call readdir(".") again; before this change it stays stale, after this change it sees the new file

Quoted 2 lines
Fixes 2396356a945b ("fuse: add more control over cache invalidation behaviour")

Applied, thanks.

Miklos

author
#9 lore raw

PATCH v2 1/1 fuse: invalidate readdir cache on epoch bump

In reply to Jun Wu

From: Jun Wu <[email protected]>

FUSE_NOTIFY_INC_EPOCH invalidates dentries, but does not invalidate cached readdir results. A process with cwd inside a FUSE mount can therefore observe stale readdir(".") output after an epoch bump.

Fix this by recording epoch in the readdir cache and checking it on reuse.

Minimal reproducer:

- mount a tiny FUSE fs with an empty root directory
- on opendir, enable fi->cache_readdir and fi->keep_cache
- chdir into the mount and call readdir(".") to populate readdir cache
- make the FUSE server report one file in the root directory
- send only FUSE_NOTIFY_INC_EPOCH
- call readdir(".") again; before this change it stays stale, after this change it sees the new file

Fixes 2396356a945b ("fuse: add more control over cache invalidation behaviour")

---
Changes in v2:
- Addresses Joanne Koong's suggestions:
- Drop the FUSE_EPOCH_READDIR init flag.
- Add Fixes tag.
- Rebase onto current mainline.

---
 fs/fuse/dev.c     | 6 +++---
 fs/fuse/fuse_i.h  | 3 +++
 fs/fuse/readdir.c | 5 ++++-
 3 files changed, 10 insertions(+), 4 deletions(-)
fs/fuse/dev.c +3 -3
index 5dda7080f4a9..35e04b064f37 100644
@@ -2038,9 +2038,9 @@ static int fuse_notify_resend(struct fuse_conn *fc)
 }
 
 /*
- * Increments the fuse connection epoch.  This will result of dentries from
- * previous epochs to be invalidated.  Additionally, if inval_wq is set, a work
- * queue is scheduled to trigger the invalidation.
+ * Increments the fuse connection epoch.  This will cause dentries and
+ * readdir caches from previous epochs to be invalidated.  Additionally,
+ * if inval_wq is set, a work queue is scheduled to trigger the invalidation.
  */
 static int fuse_notify_inc_epoch(struct fuse_conn *fc)
 {
fs/fuse/fuse_i.h +3 -0
index 17423d4e3cfa..aa8d6dfbbcc7 100644
@@ -191,6 +191,9 @@ struct fuse_inode {
 			/* iversion of directory when cache was started */
 			u64 iversion;
 
+			/* epoch of fc when cache was started */
+			int epoch;
+
 			/* protects above fields */
 			spinlock_t lock;
 		} rdc;
fs/fuse/readdir.c +4 -1
index db5ae8ec1030..b18d971e2bb4 100644
@@ -442,6 +442,7 @@ static void fuse_rdc_reset(struct inode *inode)
 	fi->rdc.version++;
 	fi->rdc.size = 0;
 	fi->rdc.pos = 0;
+	fi->rdc.epoch = 0;
 }
 
 #define UNCACHED 1
@@ -483,6 +484,7 @@ static int fuse_readdir_cached(struct file *file, struct dir_context *ctx)
 		if (!ctx->pos && !fi->rdc.size) {
 			fi->rdc.mtime = inode_get_mtime(inode);
 			fi->rdc.iversion = inode_query_iversion(inode);
+			fi->rdc.epoch = atomic_read(&fc->epoch);
 		}
 		spin_unlock(&fi->rdc.lock);
 		return UNCACHED;
@@ -496,7 +498,8 @@ static int fuse_readdir_cached(struct file *file, struct dir_context *ctx)
 		struct timespec64 mtime = inode_get_mtime(inode);
 
 		if (inode_peek_iversion(inode) != fi->rdc.iversion ||
-		    !timespec64_equal(&fi->rdc.mtime, &mtime)) {
+		    !timespec64_equal(&fi->rdc.mtime, &mtime) ||
+		    fi->rdc.epoch != atomic_read(&fc->epoch)) {
 			fuse_rdc_reset(inode);
 			goto retry_locked;
 		}
Signature · 1 line
2.54.0