2 FUSE: Filesystem in Userspace
5 This program can be distributed under the terms of the GNU GPL.
11 #include <linux/iversion.h>
12 #include <linux/posix_acl.h>
13 #include <linux/pagemap.h>
14 #include <linux/highmem.h>
16 static bool fuse_use_readdirplus(struct inode *dir, struct dir_context *ctx)
18 struct fuse_conn *fc = get_fuse_conn(dir);
19 struct fuse_inode *fi = get_fuse_inode(dir);
21 if (!fc->do_readdirplus)
23 if (!fc->readdirplus_auto)
25 if (test_and_clear_bit(FUSE_I_ADVISE_RDPLUS, &fi->state))
32 static void fuse_add_dirent_to_cache(struct file *file,
33 struct fuse_dirent *dirent, loff_t pos)
35 struct fuse_inode *fi = get_fuse_inode(file_inode(file));
36 size_t reclen = FUSE_DIRENT_SIZE(dirent);
44 spin_lock(&fi->rdc.lock);
46 * Is cache already completed? Or this entry does not go at the end of
49 if (fi->rdc.cached || pos != fi->rdc.pos) {
50 spin_unlock(&fi->rdc.lock);
53 version = fi->rdc.version;
55 offset = size & ~PAGE_MASK;
56 index = size >> PAGE_SHIFT;
57 /* Dirent doesn't fit in current page? Jump to next page. */
58 if (offset + reclen > PAGE_SIZE) {
62 spin_unlock(&fi->rdc.lock);
65 page = find_lock_page(file->f_mapping, index);
67 page = find_or_create_page(file->f_mapping, index,
68 mapping_gfp_mask(file->f_mapping));
73 spin_lock(&fi->rdc.lock);
74 /* Raced with another readdir */
75 if (fi->rdc.version != version || fi->rdc.size != size ||
76 WARN_ON(fi->rdc.pos != pos))
79 addr = kmap_local_page(page);
82 memcpy(addr + offset, dirent, reclen);
84 fi->rdc.size = (index << PAGE_SHIFT) + offset + reclen;
85 fi->rdc.pos = dirent->off;
87 spin_unlock(&fi->rdc.lock);
92 static void fuse_readdir_cache_end(struct file *file, loff_t pos)
94 struct fuse_inode *fi = get_fuse_inode(file_inode(file));
97 spin_lock(&fi->rdc.lock);
98 /* does cache end position match current position? */
99 if (fi->rdc.pos != pos) {
100 spin_unlock(&fi->rdc.lock);
104 fi->rdc.cached = true;
105 end = ALIGN(fi->rdc.size, PAGE_SIZE);
106 spin_unlock(&fi->rdc.lock);
108 /* truncate unused tail of cache */
109 truncate_inode_pages(file->f_mapping, end);
112 static bool fuse_emit(struct file *file, struct dir_context *ctx,
113 struct fuse_dirent *dirent)
115 struct fuse_file *ff = file->private_data;
117 if (ff->open_flags & FOPEN_CACHE_DIR)
118 fuse_add_dirent_to_cache(file, dirent, ctx->pos);
120 return dir_emit(ctx, dirent->name, dirent->namelen, dirent->ino,
124 static int parse_dirfile(char *buf, size_t nbytes, struct file *file,
125 struct dir_context *ctx)
127 while (nbytes >= FUSE_NAME_OFFSET) {
128 struct fuse_dirent *dirent = (struct fuse_dirent *) buf;
129 size_t reclen = FUSE_DIRENT_SIZE(dirent);
130 if (!dirent->namelen || dirent->namelen > FUSE_NAME_MAX)
134 if (memchr(dirent->name, '/', dirent->namelen) != NULL)
137 if (!fuse_emit(file, ctx, dirent))
142 ctx->pos = dirent->off;
148 static int fuse_direntplus_link(struct file *file,
149 struct fuse_direntplus *direntplus,
152 struct fuse_entry_out *o = &direntplus->entry_out;
153 struct fuse_dirent *dirent = &direntplus->dirent;
154 struct dentry *parent = file->f_path.dentry;
155 struct qstr name = QSTR_INIT(dirent->name, dirent->namelen);
156 struct dentry *dentry;
157 struct dentry *alias;
158 struct inode *dir = d_inode(parent);
159 struct fuse_conn *fc;
161 DECLARE_WAIT_QUEUE_HEAD_ONSTACK(wq);
165 * Unlike in the case of fuse_lookup, zero nodeid does not mean
166 * ENOENT. Instead, it only means the userspace filesystem did
167 * not want to return attributes/handle for this entry.
174 if (name.name[0] == '.') {
176 * We could potentially refresh the attributes of the directory
181 if (name.name[1] == '.' && name.len == 2)
185 if (invalid_nodeid(o->nodeid))
187 if (fuse_invalid_attr(&o->attr))
190 fc = get_fuse_conn(dir);
192 name.hash = full_name_hash(parent, name.name, name.len);
193 dentry = d_lookup(parent, &name);
196 dentry = d_alloc_parallel(parent, &name, &wq);
198 return PTR_ERR(dentry);
200 if (!d_in_lookup(dentry)) {
201 struct fuse_inode *fi;
202 inode = d_inode(dentry);
203 if (inode && get_node_id(inode) != o->nodeid)
206 fuse_stale_inode(inode, o->generation, &o->attr)) {
208 fuse_make_bad(inode);
209 d_invalidate(dentry);
213 if (fuse_is_bad(inode)) {
218 fi = get_fuse_inode(inode);
219 spin_lock(&fi->lock);
221 spin_unlock(&fi->lock);
223 forget_all_cached_acls(inode);
224 fuse_change_attributes(inode, &o->attr,
225 entry_attr_timeout(o),
228 * The other branch comes via fuse_iget()
229 * which bumps nlookup inside
232 inode = fuse_iget(dir->i_sb, o->nodeid, o->generation,
233 &o->attr, entry_attr_timeout(o),
236 inode = ERR_PTR(-ENOMEM);
238 alias = d_splice_alias(inode, dentry);
239 d_lookup_done(dentry);
245 return PTR_ERR(dentry);
247 if (fc->readdirplus_auto)
248 set_bit(FUSE_I_INIT_RDPLUS, &get_fuse_inode(inode)->state);
249 fuse_change_entry_timeout(dentry, o);
255 static void fuse_force_forget(struct file *file, u64 nodeid)
257 struct inode *inode = file_inode(file);
258 struct fuse_mount *fm = get_fuse_mount(inode);
259 struct fuse_forget_in inarg;
262 memset(&inarg, 0, sizeof(inarg));
264 args.opcode = FUSE_FORGET;
265 args.nodeid = nodeid;
267 args.in_args[0].size = sizeof(inarg);
268 args.in_args[0].value = &inarg;
272 fuse_simple_request(fm, &args);
276 static int parse_dirplusfile(char *buf, size_t nbytes, struct file *file,
277 struct dir_context *ctx, u64 attr_version)
279 struct fuse_direntplus *direntplus;
280 struct fuse_dirent *dirent;
285 while (nbytes >= FUSE_NAME_OFFSET_DIRENTPLUS) {
286 direntplus = (struct fuse_direntplus *) buf;
287 dirent = &direntplus->dirent;
288 reclen = FUSE_DIRENTPLUS_SIZE(direntplus);
290 if (!dirent->namelen || dirent->namelen > FUSE_NAME_MAX)
294 if (memchr(dirent->name, '/', dirent->namelen) != NULL)
298 /* We fill entries into dstbuf only as much as
299 it can hold. But we still continue iterating
300 over remaining entries to link them. If not,
301 we need to send a FORGET for each of those
302 which we did not link.
304 over = !fuse_emit(file, ctx, dirent);
306 ctx->pos = dirent->off;
312 ret = fuse_direntplus_link(file, direntplus, attr_version);
314 fuse_force_forget(file, direntplus->entry_out.nodeid);
320 static int fuse_readdir_uncached(struct file *file, struct dir_context *ctx)
325 struct inode *inode = file_inode(file);
326 struct fuse_mount *fm = get_fuse_mount(inode);
327 struct fuse_io_args ia = {};
328 struct fuse_args_pages *ap = &ia.ap;
329 struct fuse_page_desc desc = { .length = PAGE_SIZE };
330 u64 attr_version = 0;
333 page = alloc_page(GFP_KERNEL);
337 plus = fuse_use_readdirplus(inode, ctx);
338 ap->args.out_pages = true;
343 attr_version = fuse_get_attr_version(fm->fc);
344 fuse_read_args_fill(&ia, file, ctx->pos, PAGE_SIZE,
347 fuse_read_args_fill(&ia, file, ctx->pos, PAGE_SIZE,
350 locked = fuse_lock_inode(inode);
351 res = fuse_simple_request(fm, &ap->args);
352 fuse_unlock_inode(inode, locked);
355 struct fuse_file *ff = file->private_data;
357 if (ff->open_flags & FOPEN_CACHE_DIR)
358 fuse_readdir_cache_end(file, ctx->pos);
360 res = parse_dirplusfile(page_address(page), res,
361 file, ctx, attr_version);
363 res = parse_dirfile(page_address(page), res, file,
369 fuse_invalidate_atime(inode);
373 enum fuse_parse_result {
380 static enum fuse_parse_result fuse_parse_cache(struct fuse_file *ff,
381 void *addr, unsigned int size,
382 struct dir_context *ctx)
384 unsigned int offset = ff->readdir.cache_off & ~PAGE_MASK;
385 enum fuse_parse_result res = FOUND_NONE;
387 WARN_ON(offset >= size);
390 struct fuse_dirent *dirent = addr + offset;
391 unsigned int nbytes = size - offset;
394 if (nbytes < FUSE_NAME_OFFSET || !dirent->namelen)
397 reclen = FUSE_DIRENT_SIZE(dirent); /* derefs ->namelen */
399 if (WARN_ON(dirent->namelen > FUSE_NAME_MAX))
401 if (WARN_ON(reclen > nbytes))
403 if (WARN_ON(memchr(dirent->name, '/', dirent->namelen) != NULL))
406 if (ff->readdir.pos == ctx->pos) {
408 if (!dir_emit(ctx, dirent->name, dirent->namelen,
409 dirent->ino, dirent->type))
411 ctx->pos = dirent->off;
413 ff->readdir.pos = dirent->off;
414 ff->readdir.cache_off += reclen;
422 static void fuse_rdc_reset(struct inode *inode)
424 struct fuse_inode *fi = get_fuse_inode(inode);
426 fi->rdc.cached = false;
434 static int fuse_readdir_cached(struct file *file, struct dir_context *ctx)
436 struct fuse_file *ff = file->private_data;
437 struct inode *inode = file_inode(file);
438 struct fuse_conn *fc = get_fuse_conn(inode);
439 struct fuse_inode *fi = get_fuse_inode(inode);
440 enum fuse_parse_result res;
446 /* Seeked? If so, reset the cache stream */
447 if (ff->readdir.pos != ctx->pos) {
449 ff->readdir.cache_off = 0;
453 * We're just about to start reading into the cache or reading the
454 * cache; both cases require an up-to-date mtime value.
456 if (!ctx->pos && fc->auto_inval_data) {
457 int err = fuse_update_attributes(inode, file, STATX_MTIME);
464 spin_lock(&fi->rdc.lock);
466 if (!fi->rdc.cached) {
467 /* Starting cache? Set cache mtime. */
468 if (!ctx->pos && !fi->rdc.size) {
469 fi->rdc.mtime = inode->i_mtime;
470 fi->rdc.iversion = inode_query_iversion(inode);
472 spin_unlock(&fi->rdc.lock);
476 * When at the beginning of the directory (i.e. just after opendir(3) or
477 * rewinddir(3)), then need to check whether directory contents have
478 * changed, and reset the cache if so.
481 if (inode_peek_iversion(inode) != fi->rdc.iversion ||
482 !timespec64_equal(&fi->rdc.mtime, &inode->i_mtime)) {
483 fuse_rdc_reset(inode);
489 * If cache version changed since the last getdents() call, then reset
492 if (ff->readdir.version != fi->rdc.version) {
494 ff->readdir.cache_off = 0;
497 * If at the beginning of the cache, than reset version to
500 if (ff->readdir.pos == 0)
501 ff->readdir.version = fi->rdc.version;
503 WARN_ON(fi->rdc.size < ff->readdir.cache_off);
505 index = ff->readdir.cache_off >> PAGE_SHIFT;
507 if (index == (fi->rdc.size >> PAGE_SHIFT))
508 size = fi->rdc.size & ~PAGE_MASK;
511 spin_unlock(&fi->rdc.lock);
514 if ((ff->readdir.cache_off & ~PAGE_MASK) == size)
517 page = find_get_page_flags(file->f_mapping, index,
518 FGP_ACCESSED | FGP_LOCK);
519 spin_lock(&fi->rdc.lock);
522 * Uh-oh: page gone missing, cache is useless
524 if (fi->rdc.version == ff->readdir.version)
525 fuse_rdc_reset(inode);
529 /* Make sure it's still the same version after getting the page. */
530 if (ff->readdir.version != fi->rdc.version) {
531 spin_unlock(&fi->rdc.lock);
536 spin_unlock(&fi->rdc.lock);
539 * Contents of the page are now protected against changing by holding
543 res = fuse_parse_cache(ff, addr, size, ctx);
548 if (res == FOUND_ERR)
551 if (res == FOUND_ALL)
554 if (size == PAGE_SIZE) {
555 /* We hit end of page: skip to next page. */
556 ff->readdir.cache_off = ALIGN(ff->readdir.cache_off, PAGE_SIZE);
561 * End of cache reached. If found position, then we are done, otherwise
562 * need to fall back to uncached, since the position we were looking for
563 * wasn't in the cache.
565 return res == FOUND_SOME ? 0 : UNCACHED;
568 int fuse_readdir(struct file *file, struct dir_context *ctx)
570 struct fuse_file *ff = file->private_data;
571 struct inode *inode = file_inode(file);
574 if (fuse_is_bad(inode))
577 mutex_lock(&ff->readdir.lock);
580 if (ff->open_flags & FOPEN_CACHE_DIR)
581 err = fuse_readdir_cached(file, ctx);
583 err = fuse_readdir_uncached(file, ctx);
585 mutex_unlock(&ff->readdir.lock);