Skip to content

Commit 69e3455

Browse files
author
Miklos Szeredi
committed
fuse: allow caching readdir
This patch just adds the cache filling functions, which are invoked if FOPEN_CACHE_DIR flag is set in the OPENDIR reply. Cache reading and cache invalidation are added by subsequent patches. The directory cache uses the page cache. Directory entries are packed into a page in the same format as in the READDIR reply. A page only contains whole entries, the space at the end of the page is cleared. The page is locked while being modified. Multiple parallel readdirs on the same directory can fill the cache; the only constraint is that continuity must be maintained (d_off of last entry points to position of current entry). Signed-off-by: Miklos Szeredi <mszeredi@redhat.com>
1 parent 18172b1 commit 69e3455

3 files changed

Lines changed: 109 additions & 1 deletion

File tree

‎fs/fuse/fuse_i.h‎

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,21 @@ struct fuse_inode {
103103
/** List of writepage requestst (pending or sent) */
104104
struct list_head writepages;
105105

106+
/* readdir cache */
107+
struct {
108+
/* true if fully cached */
109+
bool cached;
110+
111+
/* size of cache */
112+
loff_t size;
113+
114+
/* position at end of cache (position of next entry) */
115+
loff_t pos;
116+
117+
/* protects above fields */
118+
spinlock_t lock;
119+
} rdc;
120+
106121
/** Miscellaneous bits describing inode state */
107122
unsigned long state;
108123

‎fs/fuse/inode.c‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,10 @@ static struct inode *fuse_alloc_inode(struct super_block *sb)
100100
INIT_LIST_HEAD(&fi->queued_writes);
101101
INIT_LIST_HEAD(&fi->writepages);
102102
init_waitqueue_head(&fi->page_waitq);
103+
spin_lock_init(&fi->rdc.lock);
104+
fi->rdc.cached = false;
105+
fi->rdc.size = 0;
106+
fi->rdc.pos = 0;
103107
mutex_init(&fi->mutex);
104108
fi->forget = fuse_alloc_forget();
105109
if (!fi->forget) {

‎fs/fuse/readdir.c‎

Lines changed: 90 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@
99

1010
#include "fuse_i.h"
1111
#include <linux/posix_acl.h>
12+
#include <linux/pagemap.h>
13+
#include <linux/highmem.h>
1214

1315
static bool fuse_use_readdirplus(struct inode *dir, struct dir_context *ctx)
1416
{
@@ -26,9 +28,91 @@ static bool fuse_use_readdirplus(struct inode *dir, struct dir_context *ctx)
2628
return false;
2729
}
2830

31+
static void fuse_add_dirent_to_cache(struct file *file,
32+
struct fuse_dirent *dirent, loff_t pos)
33+
{
34+
struct fuse_inode *fi = get_fuse_inode(file_inode(file));
35+
size_t reclen = FUSE_DIRENT_SIZE(dirent);
36+
pgoff_t index;
37+
struct page *page;
38+
loff_t size;
39+
unsigned int offset;
40+
void *addr;
41+
42+
spin_lock(&fi->rdc.lock);
43+
/*
44+
* Is cache already completed? Or this entry does not go at the end of
45+
* cache?
46+
*/
47+
if (fi->rdc.cached || pos != fi->rdc.pos) {
48+
spin_unlock(&fi->rdc.lock);
49+
return;
50+
}
51+
size = fi->rdc.size;
52+
offset = size & ~PAGE_MASK;
53+
index = size >> PAGE_SHIFT;
54+
/* Dirent doesn't fit in current page? Jump to next page. */
55+
if (offset + reclen > PAGE_SIZE) {
56+
index++;
57+
offset = 0;
58+
}
59+
spin_unlock(&fi->rdc.lock);
60+
61+
if (offset) {
62+
page = find_lock_page(file->f_mapping, index);
63+
} else {
64+
page = find_or_create_page(file->f_mapping, index,
65+
mapping_gfp_mask(file->f_mapping));
66+
}
67+
if (!page)
68+
return;
69+
70+
spin_lock(&fi->rdc.lock);
71+
/* Raced with another readdir */
72+
if (fi->rdc.size != size || WARN_ON(fi->rdc.pos != pos))
73+
goto unlock;
74+
75+
addr = kmap_atomic(page);
76+
if (!offset)
77+
clear_page(addr);
78+
memcpy(addr + offset, dirent, reclen);
79+
kunmap_atomic(addr);
80+
fi->rdc.size = (index << PAGE_SHIFT) + offset + reclen;
81+
fi->rdc.pos = dirent->off;
82+
unlock:
83+
spin_unlock(&fi->rdc.lock);
84+
unlock_page(page);
85+
put_page(page);
86+
}
87+
88+
static void fuse_readdir_cache_end(struct file *file, loff_t pos)
89+
{
90+
struct fuse_inode *fi = get_fuse_inode(file_inode(file));
91+
loff_t end;
92+
93+
spin_lock(&fi->rdc.lock);
94+
/* does cache end position match current position? */
95+
if (fi->rdc.pos != pos) {
96+
spin_unlock(&fi->rdc.lock);
97+
return;
98+
}
99+
100+
fi->rdc.cached = true;
101+
end = ALIGN(fi->rdc.size, PAGE_SIZE);
102+
spin_unlock(&fi->rdc.lock);
103+
104+
/* truncate unused tail of cache */
105+
truncate_inode_pages(file->f_mapping, end);
106+
}
107+
29108
static bool fuse_emit(struct file *file, struct dir_context *ctx,
30109
struct fuse_dirent *dirent)
31110
{
111+
struct fuse_file *ff = file->private_data;
112+
113+
if (ff->open_flags & FOPEN_CACHE_DIR)
114+
fuse_add_dirent_to_cache(file, dirent, ctx->pos);
115+
32116
return dir_emit(ctx, dirent->name, dirent->namelen, dirent->ino,
33117
dirent->type);
34118
}
@@ -249,7 +333,12 @@ int fuse_readdir(struct file *file, struct dir_context *ctx)
249333
err = req->out.h.error;
250334
fuse_put_request(fc, req);
251335
if (!err) {
252-
if (plus) {
336+
if (!nbytes) {
337+
struct fuse_file *ff = file->private_data;
338+
339+
if (ff->open_flags & FOPEN_CACHE_DIR)
340+
fuse_readdir_cache_end(file, ctx->pos);
341+
} else if (plus) {
253342
err = parse_dirplusfile(page_address(page), nbytes,
254343
file, ctx, attr_version);
255344
} else {

0 commit comments

Comments
 (0)