2 FUSE: Filesystem in Userspace
3 Copyright (C) 2001-2018 Miklos Szeredi <miklos@szeredi.hu>
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 SetPageUptodate(page
);
84 memcpy(addr
+ offset
, dirent
, reclen
);
86 fi
->rdc
.size
= (index
<< PAGE_SHIFT
) + offset
+ reclen
;
87 fi
->rdc
.pos
= dirent
->off
;
89 spin_unlock(&fi
->rdc
.lock
);
94 static void fuse_readdir_cache_end(struct file
*file
, loff_t pos
)
96 struct fuse_inode
*fi
= get_fuse_inode(file_inode(file
));
99 spin_lock(&fi
->rdc
.lock
);
100 /* does cache end position match current position? */
101 if (fi
->rdc
.pos
!= pos
) {
102 spin_unlock(&fi
->rdc
.lock
);
106 fi
->rdc
.cached
= true;
107 end
= ALIGN(fi
->rdc
.size
, PAGE_SIZE
);
108 spin_unlock(&fi
->rdc
.lock
);
110 /* truncate unused tail of cache */
111 truncate_inode_pages(file
->f_mapping
, end
);
114 static bool fuse_emit(struct file
*file
, struct dir_context
*ctx
,
115 struct fuse_dirent
*dirent
)
117 struct fuse_file
*ff
= file
->private_data
;
119 if (ff
->open_flags
& FOPEN_CACHE_DIR
)
120 fuse_add_dirent_to_cache(file
, dirent
, ctx
->pos
);
122 return dir_emit(ctx
, dirent
->name
, dirent
->namelen
, dirent
->ino
,
126 static int parse_dirfile(char *buf
, size_t nbytes
, struct file
*file
,
127 struct dir_context
*ctx
)
129 while (nbytes
>= FUSE_NAME_OFFSET
) {
130 struct fuse_dirent
*dirent
= (struct fuse_dirent
*) buf
;
131 size_t reclen
= FUSE_DIRENT_SIZE(dirent
);
132 if (!dirent
->namelen
|| dirent
->namelen
> FUSE_NAME_MAX
)
136 if (memchr(dirent
->name
, '/', dirent
->namelen
) != NULL
)
139 if (!fuse_emit(file
, ctx
, dirent
))
144 ctx
->pos
= dirent
->off
;
150 static int fuse_direntplus_link(struct file
*file
,
151 struct fuse_direntplus
*direntplus
,
152 u64 attr_version
, u64 evict_ctr
)
154 struct fuse_entry_out
*o
= &direntplus
->entry_out
;
155 struct fuse_dirent
*dirent
= &direntplus
->dirent
;
156 struct dentry
*parent
= file
->f_path
.dentry
;
157 struct qstr name
= QSTR_INIT(dirent
->name
, dirent
->namelen
);
158 struct dentry
*dentry
;
159 struct dentry
*alias
;
160 struct inode
*dir
= d_inode(parent
);
161 struct fuse_conn
*fc
;
163 DECLARE_WAIT_QUEUE_HEAD_ONSTACK(wq
);
167 * Unlike in the case of fuse_lookup, zero nodeid does not mean
168 * ENOENT. Instead, it only means the userspace filesystem did
169 * not want to return attributes/handle for this entry.
176 if (name
.name
[0] == '.') {
178 * We could potentially refresh the attributes of the directory
183 if (name
.name
[1] == '.' && name
.len
== 2)
187 if (invalid_nodeid(o
->nodeid
))
189 if (fuse_invalid_attr(&o
->attr
))
192 fc
= get_fuse_conn(dir
);
194 name
.hash
= full_name_hash(parent
, name
.name
, name
.len
);
195 dentry
= d_lookup(parent
, &name
);
198 dentry
= d_alloc_parallel(parent
, &name
, &wq
);
200 return PTR_ERR(dentry
);
202 if (!d_in_lookup(dentry
)) {
203 struct fuse_inode
*fi
;
204 inode
= d_inode(dentry
);
205 if (inode
&& get_node_id(inode
) != o
->nodeid
)
208 fuse_stale_inode(inode
, o
->generation
, &o
->attr
)) {
210 fuse_make_bad(inode
);
211 d_invalidate(dentry
);
215 if (fuse_is_bad(inode
)) {
220 fi
= get_fuse_inode(inode
);
221 spin_lock(&fi
->lock
);
223 spin_unlock(&fi
->lock
);
225 forget_all_cached_acls(inode
);
226 fuse_change_attributes(inode
, &o
->attr
, NULL
,
230 * The other branch comes via fuse_iget()
231 * which bumps nlookup inside
234 inode
= fuse_iget(dir
->i_sb
, o
->nodeid
, o
->generation
,
235 &o
->attr
, ATTR_TIMEOUT(o
),
236 attr_version
, evict_ctr
);
238 inode
= ERR_PTR(-ENOMEM
);
240 alias
= d_splice_alias(inode
, dentry
);
241 d_lookup_done(dentry
);
246 if (IS_ERR(dentry
)) {
247 if (!IS_ERR(inode
)) {
248 struct fuse_inode
*fi
= get_fuse_inode(inode
);
250 spin_lock(&fi
->lock
);
252 spin_unlock(&fi
->lock
);
254 return PTR_ERR(dentry
);
257 if (fc
->readdirplus_auto
)
258 set_bit(FUSE_I_INIT_RDPLUS
, &get_fuse_inode(inode
)->state
);
259 fuse_change_entry_timeout(dentry
, o
);
265 static void fuse_force_forget(struct file
*file
, u64 nodeid
)
267 struct inode
*inode
= file_inode(file
);
268 struct fuse_mount
*fm
= get_fuse_mount(inode
);
269 struct fuse_forget_in inarg
;
272 memset(&inarg
, 0, sizeof(inarg
));
274 args
.opcode
= FUSE_FORGET
;
275 args
.nodeid
= nodeid
;
277 args
.in_args
[0].size
= sizeof(inarg
);
278 args
.in_args
[0].value
= &inarg
;
282 fuse_simple_request(fm
, &args
);
286 static int parse_dirplusfile(char *buf
, size_t nbytes
, struct file
*file
,
287 struct dir_context
*ctx
, u64 attr_version
,
290 struct fuse_direntplus
*direntplus
;
291 struct fuse_dirent
*dirent
;
296 while (nbytes
>= FUSE_NAME_OFFSET_DIRENTPLUS
) {
297 direntplus
= (struct fuse_direntplus
*) buf
;
298 dirent
= &direntplus
->dirent
;
299 reclen
= FUSE_DIRENTPLUS_SIZE(direntplus
);
301 if (!dirent
->namelen
|| dirent
->namelen
> FUSE_NAME_MAX
)
305 if (memchr(dirent
->name
, '/', dirent
->namelen
) != NULL
)
309 /* We fill entries into dstbuf only as much as
310 it can hold. But we still continue iterating
311 over remaining entries to link them. If not,
312 we need to send a FORGET for each of those
313 which we did not link.
315 over
= !fuse_emit(file
, ctx
, dirent
);
317 ctx
->pos
= dirent
->off
;
323 ret
= fuse_direntplus_link(file
, direntplus
, attr_version
, evict_ctr
);
325 fuse_force_forget(file
, direntplus
->entry_out
.nodeid
);
331 static int fuse_readdir_uncached(struct file
*file
, struct dir_context
*ctx
)
336 struct inode
*inode
= file_inode(file
);
337 struct fuse_mount
*fm
= get_fuse_mount(inode
);
338 struct fuse_io_args ia
= {};
339 struct fuse_args_pages
*ap
= &ia
.ap
;
340 struct fuse_folio_desc desc
= { .length
= PAGE_SIZE
};
341 u64 attr_version
= 0, evict_ctr
= 0;
344 folio
= folio_alloc(GFP_KERNEL
, 0);
348 plus
= fuse_use_readdirplus(inode
, ctx
);
349 ap
->args
.out_pages
= true;
354 attr_version
= fuse_get_attr_version(fm
->fc
);
355 evict_ctr
= fuse_get_evict_ctr(fm
->fc
);
356 fuse_read_args_fill(&ia
, file
, ctx
->pos
, PAGE_SIZE
,
359 fuse_read_args_fill(&ia
, file
, ctx
->pos
, PAGE_SIZE
,
362 locked
= fuse_lock_inode(inode
);
363 res
= fuse_simple_request(fm
, &ap
->args
);
364 fuse_unlock_inode(inode
, locked
);
367 struct fuse_file
*ff
= file
->private_data
;
369 if (ff
->open_flags
& FOPEN_CACHE_DIR
)
370 fuse_readdir_cache_end(file
, ctx
->pos
);
372 res
= parse_dirplusfile(folio_address(folio
), res
,
373 file
, ctx
, attr_version
,
376 res
= parse_dirfile(folio_address(folio
), res
, file
,
382 fuse_invalidate_atime(inode
);
386 enum fuse_parse_result
{
393 static enum fuse_parse_result
fuse_parse_cache(struct fuse_file
*ff
,
394 void *addr
, unsigned int size
,
395 struct dir_context
*ctx
)
397 unsigned int offset
= ff
->readdir
.cache_off
& ~PAGE_MASK
;
398 enum fuse_parse_result res
= FOUND_NONE
;
400 WARN_ON(offset
>= size
);
403 struct fuse_dirent
*dirent
= addr
+ offset
;
404 unsigned int nbytes
= size
- offset
;
407 if (nbytes
< FUSE_NAME_OFFSET
|| !dirent
->namelen
)
410 reclen
= FUSE_DIRENT_SIZE(dirent
); /* derefs ->namelen */
412 if (WARN_ON(dirent
->namelen
> FUSE_NAME_MAX
))
414 if (WARN_ON(reclen
> nbytes
))
416 if (WARN_ON(memchr(dirent
->name
, '/', dirent
->namelen
) != NULL
))
419 if (ff
->readdir
.pos
== ctx
->pos
) {
421 if (!dir_emit(ctx
, dirent
->name
, dirent
->namelen
,
422 dirent
->ino
, dirent
->type
))
424 ctx
->pos
= dirent
->off
;
426 ff
->readdir
.pos
= dirent
->off
;
427 ff
->readdir
.cache_off
+= reclen
;
435 static void fuse_rdc_reset(struct inode
*inode
)
437 struct fuse_inode
*fi
= get_fuse_inode(inode
);
439 fi
->rdc
.cached
= false;
447 static int fuse_readdir_cached(struct file
*file
, struct dir_context
*ctx
)
449 struct fuse_file
*ff
= file
->private_data
;
450 struct inode
*inode
= file_inode(file
);
451 struct fuse_conn
*fc
= get_fuse_conn(inode
);
452 struct fuse_inode
*fi
= get_fuse_inode(inode
);
453 enum fuse_parse_result res
;
459 /* Seeked? If so, reset the cache stream */
460 if (ff
->readdir
.pos
!= ctx
->pos
) {
462 ff
->readdir
.cache_off
= 0;
466 * We're just about to start reading into the cache or reading the
467 * cache; both cases require an up-to-date mtime value.
469 if (!ctx
->pos
&& fc
->auto_inval_data
) {
470 int err
= fuse_update_attributes(inode
, file
, STATX_MTIME
);
477 spin_lock(&fi
->rdc
.lock
);
479 if (!fi
->rdc
.cached
) {
480 /* Starting cache? Set cache mtime. */
481 if (!ctx
->pos
&& !fi
->rdc
.size
) {
482 fi
->rdc
.mtime
= inode_get_mtime(inode
);
483 fi
->rdc
.iversion
= inode_query_iversion(inode
);
485 spin_unlock(&fi
->rdc
.lock
);
489 * When at the beginning of the directory (i.e. just after opendir(3) or
490 * rewinddir(3)), then need to check whether directory contents have
491 * changed, and reset the cache if so.
494 struct timespec64 mtime
= inode_get_mtime(inode
);
496 if (inode_peek_iversion(inode
) != fi
->rdc
.iversion
||
497 !timespec64_equal(&fi
->rdc
.mtime
, &mtime
)) {
498 fuse_rdc_reset(inode
);
504 * If cache version changed since the last getdents() call, then reset
507 if (ff
->readdir
.version
!= fi
->rdc
.version
) {
509 ff
->readdir
.cache_off
= 0;
512 * If at the beginning of the cache, than reset version to
515 if (ff
->readdir
.pos
== 0)
516 ff
->readdir
.version
= fi
->rdc
.version
;
518 WARN_ON(fi
->rdc
.size
< ff
->readdir
.cache_off
);
520 index
= ff
->readdir
.cache_off
>> PAGE_SHIFT
;
522 if (index
== (fi
->rdc
.size
>> PAGE_SHIFT
))
523 size
= fi
->rdc
.size
& ~PAGE_MASK
;
526 spin_unlock(&fi
->rdc
.lock
);
529 if ((ff
->readdir
.cache_off
& ~PAGE_MASK
) == size
)
532 page
= find_get_page_flags(file
->f_mapping
, index
,
533 FGP_ACCESSED
| FGP_LOCK
);
534 /* Page gone missing, then re-added to cache, but not initialized? */
535 if (page
&& !PageUptodate(page
)) {
540 spin_lock(&fi
->rdc
.lock
);
543 * Uh-oh: page gone missing, cache is useless
545 if (fi
->rdc
.version
== ff
->readdir
.version
)
546 fuse_rdc_reset(inode
);
550 /* Make sure it's still the same version after getting the page. */
551 if (ff
->readdir
.version
!= fi
->rdc
.version
) {
552 spin_unlock(&fi
->rdc
.lock
);
557 spin_unlock(&fi
->rdc
.lock
);
560 * Contents of the page are now protected against changing by holding
563 addr
= kmap_local_page(page
);
564 res
= fuse_parse_cache(ff
, addr
, size
, ctx
);
569 if (res
== FOUND_ERR
)
572 if (res
== FOUND_ALL
)
575 if (size
== PAGE_SIZE
) {
576 /* We hit end of page: skip to next page. */
577 ff
->readdir
.cache_off
= ALIGN(ff
->readdir
.cache_off
, PAGE_SIZE
);
582 * End of cache reached. If found position, then we are done, otherwise
583 * need to fall back to uncached, since the position we were looking for
584 * wasn't in the cache.
586 return res
== FOUND_SOME
? 0 : UNCACHED
;
589 int fuse_readdir(struct file
*file
, struct dir_context
*ctx
)
591 struct fuse_file
*ff
= file
->private_data
;
592 struct inode
*inode
= file_inode(file
);
595 if (fuse_is_bad(inode
))
599 if (ff
->open_flags
& FOPEN_CACHE_DIR
)
600 err
= fuse_readdir_cached(file
, ctx
);
602 err
= fuse_readdir_uncached(file
, ctx
);