Skip to content

Commit 3494927

Browse files
author
Miklos Szeredi
committed
fuse: add readdir cache version
Allow the cache to be invalidated when page(s) have gone missing. In this case increment the version of the cache and reset to an empty state. Add a version number to the directory stream in struct fuse_file as well, indicating the version of the cache it's supposed to be reading. If the cache version doesn't match the stream's version, then reset the stream to the beginning of the cache. Signed-off-by: Miklos Szeredi <mszeredi@redhat.com>
1 parent 5d7bc7e commit 3494927

3 files changed

Lines changed: 52 additions & 1 deletion

File tree

‎fs/fuse/fuse_i.h‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,9 @@ struct fuse_inode {
114114
/* position at end of cache (position of next entry) */
115115
loff_t pos;
116116

117+
/* version of the cache */
118+
u64 version;
119+
117120
/* protects above fields */
118121
spinlock_t lock;
119122
} rdc;
@@ -176,6 +179,10 @@ struct fuse_file {
176179

177180
/* Offset in cache */
178181
loff_t cache_off;
182+
183+
/* Version of cache we are reading */
184+
u64 version;
185+
179186
} readdir;
180187

181188
/** RB node to be linked on fuse_conn->polled_files */

‎fs/fuse/inode.c‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,7 @@ static struct inode *fuse_alloc_inode(struct super_block *sb)
104104
fi->rdc.cached = false;
105105
fi->rdc.size = 0;
106106
fi->rdc.pos = 0;
107+
fi->rdc.version = 0;
107108
mutex_init(&fi->mutex);
108109
fi->forget = fuse_alloc_forget();
109110
if (!fi->forget) {

‎fs/fuse/readdir.c‎

Lines changed: 44 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ static void fuse_add_dirent_to_cache(struct file *file,
3636
pgoff_t index;
3737
struct page *page;
3838
loff_t size;
39+
u64 version;
3940
unsigned int offset;
4041
void *addr;
4142

@@ -48,6 +49,7 @@ static void fuse_add_dirent_to_cache(struct file *file,
4849
spin_unlock(&fi->rdc.lock);
4950
return;
5051
}
52+
version = fi->rdc.version;
5153
size = fi->rdc.size;
5254
offset = size & ~PAGE_MASK;
5355
index = size >> PAGE_SHIFT;
@@ -69,7 +71,8 @@ static void fuse_add_dirent_to_cache(struct file *file,
6971

7072
spin_lock(&fi->rdc.lock);
7173
/* Raced with another readdir */
72-
if (fi->rdc.size != size || WARN_ON(fi->rdc.pos != pos))
74+
if (fi->rdc.version != version || fi->rdc.size != size ||
75+
WARN_ON(fi->rdc.pos != pos))
7376
goto unlock;
7477

7578
addr = kmap_atomic(page);
@@ -396,6 +399,14 @@ static enum fuse_parse_result fuse_parse_cache(struct fuse_file *ff,
396399
return res;
397400
}
398401

402+
static void fuse_rdc_reset(struct fuse_inode *fi)
403+
{
404+
fi->rdc.cached = false;
405+
fi->rdc.version++;
406+
fi->rdc.size = 0;
407+
fi->rdc.pos = 0;
408+
}
409+
399410
#define UNCACHED 1
400411

401412
static int fuse_readdir_cached(struct file *file, struct dir_context *ctx)
@@ -421,6 +432,21 @@ static int fuse_readdir_cached(struct file *file, struct dir_context *ctx)
421432
spin_unlock(&fi->rdc.lock);
422433
return UNCACHED;
423434
}
435+
/*
436+
* If cache version changed since the last getdents() call, then reset
437+
* the cache stream.
438+
*/
439+
if (ff->readdir.version != fi->rdc.version) {
440+
ff->readdir.pos = 0;
441+
ff->readdir.cache_off = 0;
442+
}
443+
/*
444+
* If at the beginning of the cache, than reset version to
445+
* current.
446+
*/
447+
if (ff->readdir.pos == 0)
448+
ff->readdir.version = fi->rdc.version;
449+
424450
WARN_ON(fi->rdc.size < ff->readdir.cache_off);
425451

426452
index = ff->readdir.cache_off >> PAGE_SHIFT;
@@ -437,13 +463,30 @@ static int fuse_readdir_cached(struct file *file, struct dir_context *ctx)
437463

438464
page = find_get_page_flags(file->f_mapping, index,
439465
FGP_ACCESSED | FGP_LOCK);
466+
spin_lock(&fi->rdc.lock);
440467
if (!page) {
441468
/*
442469
* Uh-oh: page gone missing, cache is useless
443470
*/
471+
if (fi->rdc.version == ff->readdir.version)
472+
fuse_rdc_reset(fi);
473+
spin_unlock(&fi->rdc.lock);
444474
return UNCACHED;
445475
}
446476

477+
/* Make sure it's still the same version after getting the page. */
478+
if (ff->readdir.version != fi->rdc.version) {
479+
spin_unlock(&fi->rdc.lock);
480+
unlock_page(page);
481+
put_page(page);
482+
goto retry;
483+
}
484+
spin_unlock(&fi->rdc.lock);
485+
486+
/*
487+
* Contents of the page are now protected against changing by holding
488+
* the page lock.
489+
*/
447490
addr = kmap(page);
448491
res = fuse_parse_cache(ff, addr, size, ctx);
449492
kunmap(page);

0 commit comments

Comments
 (0)