fuse: invalidate readdir cache on epoch bump
PATCH v3 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.
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
---
Changes in v3:
- Add Reviewed-by tags from Luis and Joanne.
- Resend from a working email setup.
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
@@ -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
@@ -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
@@ -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