2 * SPDX-License-Identifier: BSD-3-Clause
4 * Copyright (c) 2007-2009 Google Inc. and Amit Singh
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions are
11 * * Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * * Redistributions in binary form must reproduce the above
14 * copyright notice, this list of conditions and the following disclaimer
15 * in the documentation and/or other materials provided with the
17 * * Neither the name of Google Inc. nor the names of its
18 * contributors may be used to endorse or promote products derived from
19 * this software without specific prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
24 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
25 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
26 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
27 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
31 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33 * Copyright (C) 2005 Csaba Henk.
34 * All rights reserved.
36 * Redistribution and use in source and binary forms, with or without
37 * modification, are permitted provided that the following conditions
39 * 1. Redistributions of source code must retain the above copyright
40 * notice, this list of conditions and the following disclaimer.
41 * 2. Redistributions in binary form must reproduce the above copyright
42 * notice, this list of conditions and the following disclaimer in the
43 * documentation and/or other materials provided with the distribution.
45 * THIS SOFTWARE IS PROVIDED BY AUTHOR AND CONTRIBUTORS ``AS IS'' AND
46 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
47 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
48 * ARE DISCLAIMED. IN NO EVENT SHALL AUTHOR OR CONTRIBUTORS BE LIABLE
49 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
50 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
51 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
52 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
53 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
54 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
58 #include <sys/cdefs.h>
59 __FBSDID("$FreeBSD$");
61 #include <sys/param.h>
62 #include <sys/module.h>
63 #include <sys/systm.h>
64 #include <sys/errno.h>
65 #include <sys/kernel.h>
68 #include <sys/malloc.h>
69 #include <sys/queue.h>
71 #include <sys/rwlock.h>
74 #include <sys/mount.h>
75 #include <sys/vnode.h>
76 #include <sys/namei.h>
77 #include <sys/extattr.h>
79 #include <sys/unistd.h>
80 #include <sys/filedesc.h>
82 #include <sys/fcntl.h>
83 #include <sys/dirent.h>
86 #include <sys/sysctl.h>
87 #include <sys/vmmeter.h>
90 #include <vm/vm_extern.h>
92 #include <vm/vm_map.h>
93 #include <vm/vm_page.h>
94 #include <vm/vm_param.h>
95 #include <vm/vm_object.h>
96 #include <vm/vm_pager.h>
97 #include <vm/vnode_pager.h>
98 #include <vm/vm_object.h>
101 #include "fuse_file.h"
102 #include "fuse_internal.h"
103 #include "fuse_ipc.h"
104 #include "fuse_node.h"
107 #include <sys/priv.h>
109 /* Maximum number of hardlinks to a single FUSE file */
110 #define FUSE_LINK_MAX UINT32_MAX
112 SDT_PROVIDER_DECLARE(fusefs);
115 * arg0: verbosity. Higher numbers give more verbose messages
116 * arg1: Textual message
118 SDT_PROBE_DEFINE2(fusefs, , vnops, trace, "int", "char*");
121 static vop_access_t fuse_vnop_access;
122 static vop_advlock_t fuse_vnop_advlock;
123 static vop_close_t fuse_fifo_close;
124 static vop_close_t fuse_vnop_close;
125 static vop_create_t fuse_vnop_create;
126 static vop_deleteextattr_t fuse_vnop_deleteextattr;
127 static vop_fdatasync_t fuse_vnop_fdatasync;
128 static vop_fsync_t fuse_vnop_fsync;
129 static vop_getattr_t fuse_vnop_getattr;
130 static vop_getextattr_t fuse_vnop_getextattr;
131 static vop_inactive_t fuse_vnop_inactive;
132 static vop_link_t fuse_vnop_link;
133 static vop_listextattr_t fuse_vnop_listextattr;
134 static vop_lookup_t fuse_vnop_lookup;
135 static vop_mkdir_t fuse_vnop_mkdir;
136 static vop_mknod_t fuse_vnop_mknod;
137 static vop_open_t fuse_vnop_open;
138 static vop_pathconf_t fuse_vnop_pathconf;
139 static vop_read_t fuse_vnop_read;
140 static vop_readdir_t fuse_vnop_readdir;
141 static vop_readlink_t fuse_vnop_readlink;
142 static vop_reclaim_t fuse_vnop_reclaim;
143 static vop_remove_t fuse_vnop_remove;
144 static vop_rename_t fuse_vnop_rename;
145 static vop_rmdir_t fuse_vnop_rmdir;
146 static vop_setattr_t fuse_vnop_setattr;
147 static vop_setextattr_t fuse_vnop_setextattr;
148 static vop_strategy_t fuse_vnop_strategy;
149 static vop_symlink_t fuse_vnop_symlink;
150 static vop_write_t fuse_vnop_write;
151 static vop_getpages_t fuse_vnop_getpages;
152 static vop_putpages_t fuse_vnop_putpages;
153 static vop_print_t fuse_vnop_print;
155 struct vop_vector fuse_fifoops = {
156 .vop_default = &fifo_specops,
157 .vop_access = fuse_vnop_access,
158 .vop_close = fuse_fifo_close,
159 .vop_fsync = fuse_vnop_fsync,
160 .vop_getattr = fuse_vnop_getattr,
161 .vop_inactive = fuse_vnop_inactive,
162 .vop_pathconf = fuse_vnop_pathconf,
163 .vop_print = fuse_vnop_print,
164 .vop_read = VOP_PANIC,
165 .vop_reclaim = fuse_vnop_reclaim,
166 .vop_setattr = fuse_vnop_setattr,
167 .vop_write = VOP_PANIC,
170 struct vop_vector fuse_vnops = {
171 .vop_default = &default_vnodeops,
172 .vop_access = fuse_vnop_access,
173 .vop_advlock = fuse_vnop_advlock,
174 .vop_close = fuse_vnop_close,
175 .vop_create = fuse_vnop_create,
176 .vop_deleteextattr = fuse_vnop_deleteextattr,
177 .vop_fsync = fuse_vnop_fsync,
178 .vop_fdatasync = fuse_vnop_fdatasync,
179 .vop_getattr = fuse_vnop_getattr,
180 .vop_getextattr = fuse_vnop_getextattr,
181 .vop_inactive = fuse_vnop_inactive,
182 .vop_link = fuse_vnop_link,
183 .vop_listextattr = fuse_vnop_listextattr,
184 .vop_lookup = fuse_vnop_lookup,
185 .vop_mkdir = fuse_vnop_mkdir,
186 .vop_mknod = fuse_vnop_mknod,
187 .vop_open = fuse_vnop_open,
188 .vop_pathconf = fuse_vnop_pathconf,
189 .vop_read = fuse_vnop_read,
190 .vop_readdir = fuse_vnop_readdir,
191 .vop_readlink = fuse_vnop_readlink,
192 .vop_reclaim = fuse_vnop_reclaim,
193 .vop_remove = fuse_vnop_remove,
194 .vop_rename = fuse_vnop_rename,
195 .vop_rmdir = fuse_vnop_rmdir,
196 .vop_setattr = fuse_vnop_setattr,
197 .vop_setextattr = fuse_vnop_setextattr,
198 .vop_strategy = fuse_vnop_strategy,
199 .vop_symlink = fuse_vnop_symlink,
200 .vop_write = fuse_vnop_write,
201 .vop_getpages = fuse_vnop_getpages,
202 .vop_putpages = fuse_vnop_putpages,
203 .vop_print = fuse_vnop_print,
206 static u_long fuse_lookup_cache_hits = 0;
208 SYSCTL_ULONG(_vfs_fusefs, OID_AUTO, lookup_cache_hits, CTLFLAG_RD,
209 &fuse_lookup_cache_hits, 0, "number of positive cache hits in lookup");
211 static u_long fuse_lookup_cache_misses = 0;
213 SYSCTL_ULONG(_vfs_fusefs, OID_AUTO, lookup_cache_misses, CTLFLAG_RD,
214 &fuse_lookup_cache_misses, 0, "number of cache misses in lookup");
217 * XXX: This feature is highly experimental and can bring to instabilities,
218 * needs revisiting before to be enabled by default.
220 static int fuse_reclaim_revoked = 0;
222 SYSCTL_INT(_vfs_fusefs, OID_AUTO, reclaim_revoked, CTLFLAG_RW,
223 &fuse_reclaim_revoked, 0, "");
225 uma_zone_t fuse_pbuf_zone;
227 #define fuse_vm_page_lock(m) vm_page_lock((m));
228 #define fuse_vm_page_unlock(m) vm_page_unlock((m));
229 #define fuse_vm_page_lock_queues() ((void)0)
230 #define fuse_vm_page_unlock_queues() ((void)0)
232 /* Check permission for extattr operations, much like extattr_check_cred */
234 fuse_extattr_check_cred(struct vnode *vp, int ns, struct ucred *cred,
235 struct thread *td, accmode_t accmode)
237 struct mount *mp = vnode_mount(vp);
238 struct fuse_data *data = fuse_get_mpdata(mp);
241 * Kernel-invoked always succeeds.
247 * Do not allow privileged processes in jail to directly manipulate
251 case EXTATTR_NAMESPACE_SYSTEM:
252 if (data->dataflags & FSESS_DEFAULT_PERMISSIONS) {
253 return (priv_check_cred(cred, PRIV_VFS_EXTATTR_SYSTEM));
256 case EXTATTR_NAMESPACE_USER:
257 return (fuse_internal_access(vp, accmode, td, cred));
263 /* Get a filehandle for a directory */
265 fuse_filehandle_get_dir(struct vnode *vp, struct fuse_filehandle **fufhp,
266 struct ucred *cred, pid_t pid)
268 if (fuse_filehandle_get(vp, FREAD, fufhp, cred, pid) == 0)
270 return fuse_filehandle_get(vp, FEXEC, fufhp, cred, pid);
273 /* Send FUSE_FLUSH for this vnode */
275 fuse_flush(struct vnode *vp, struct ucred *cred, pid_t pid, int fflag)
277 struct fuse_flush_in *ffi;
278 struct fuse_filehandle *fufh;
279 struct fuse_dispatcher fdi;
280 struct thread *td = curthread;
281 struct mount *mp = vnode_mount(vp);
284 if (!fsess_isimpl(vnode_mount(vp), FUSE_FLUSH))
287 err = fuse_filehandle_getrw(vp, fflag, &fufh, cred, pid);
291 fdisp_init(&fdi, sizeof(*ffi));
292 fdisp_make_vp(&fdi, FUSE_FLUSH, vp, td, cred);
294 ffi->fh = fufh->fh_id;
296 * If the file has a POSIX lock then we're supposed to set lock_owner.
297 * If not, then lock_owner is undefined. So we may as well always set
300 ffi->lock_owner = td->td_proc->p_pid;
302 err = fdisp_wait_answ(&fdi);
304 fsess_set_notimpl(mp, FUSE_FLUSH);
311 /* Close wrapper for fifos. */
313 fuse_fifo_close(struct vop_close_args *ap)
315 return (fifo_specops.vop_close(ap));
319 struct vnop_access_args {
321 #if VOP_ACCESS_TAKES_ACCMODE_T
326 struct ucred *a_cred;
331 fuse_vnop_access(struct vop_access_args *ap)
333 struct vnode *vp = ap->a_vp;
334 int accmode = ap->a_accmode;
335 struct ucred *cred = ap->a_cred;
337 struct fuse_data *data = fuse_get_mpdata(vnode_mount(vp));
341 if (fuse_isdeadfs(vp)) {
342 if (vnode_isvroot(vp)) {
347 if (!(data->dataflags & FSESS_INITED)) {
348 if (vnode_isvroot(vp)) {
349 if (priv_check_cred(cred, PRIV_VFS_ADMIN) ||
350 (fuse_match_cred(data->daemoncred, cred) == 0)) {
356 if (vnode_islnk(vp)) {
360 err = fuse_internal_access(vp, accmode, ap->a_td, ap->a_cred);
365 * struct vop_advlock_args {
366 * struct vop_generic_args a_gen;
367 * struct vnode *a_vp;
370 * struct flock *a_fl;
375 fuse_vnop_advlock(struct vop_advlock_args *ap)
377 struct vnode *vp = ap->a_vp;
378 struct flock *fl = ap->a_fl;
379 struct thread *td = curthread;
380 struct ucred *cred = td->td_ucred;
381 pid_t pid = td->td_proc->p_pid;
382 struct fuse_filehandle *fufh;
383 struct fuse_dispatcher fdi;
384 struct fuse_lk_in *fli;
385 struct fuse_lk_out *flo;
389 dataflags = fuse_get_mpdata(vnode_mount(vp))->dataflags;
391 if (fuse_isdeadfs(vp)) {
395 if (!(dataflags & FSESS_POSIX_LOCKS))
396 return vop_stdadvlock(ap);
398 err = fuse_filehandle_get_anyflags(vp, &fufh, cred, pid);
402 fdisp_init(&fdi, sizeof(*fli));
418 fdisp_make_vp(&fdi, op, vp, td, cred);
420 fli->fh = fufh->fh_id;
421 fli->owner = fl->l_pid;
422 fli->lk.start = fl->l_start;
424 fli->lk.end = fl->l_start + fl->l_len - 1;
426 fli->lk.end = INT64_MAX;
427 fli->lk.type = fl->l_type;
428 fli->lk.pid = fl->l_pid;
430 err = fdisp_wait_answ(&fdi);
433 if (err == 0 && op == FUSE_GETLK) {
435 fl->l_type = flo->lk.type;
436 fl->l_pid = flo->lk.pid;
437 if (flo->lk.type != F_UNLCK) {
438 fl->l_start = flo->lk.start;
439 if (flo->lk.end == INT64_MAX)
442 fl->l_len = flo->lk.end - flo->lk.start + 1;
443 fl->l_start = flo->lk.start;
451 struct vop_close_args {
454 struct ucred *a_cred;
459 fuse_vnop_close(struct vop_close_args *ap)
461 struct vnode *vp = ap->a_vp;
462 struct ucred *cred = ap->a_cred;
463 int fflag = ap->a_fflag;
464 struct thread *td = ap->a_td;
465 pid_t pid = td->td_proc->p_pid;
468 if (fuse_isdeadfs(vp))
472 if (fflag & IO_NDELAY)
475 err = fuse_flush(vp, cred, pid, fflag);
476 /* TODO: close the file handle, if we're sure it's no longer used */
477 if ((VTOFUD(vp)->flag & FN_SIZECHANGE) != 0) {
478 fuse_vnode_savesize(vp, cred, td->td_proc->p_pid);
484 fdisp_make_mknod_for_fallback(
485 struct fuse_dispatcher *fdip,
486 struct componentname *cnp,
492 enum fuse_opcode *op)
494 struct fuse_mknod_in *fmni;
496 fdisp_init(fdip, sizeof(*fmni) + cnp->cn_namelen + 1);
498 fdisp_make(fdip, *op, vnode_mount(dvp), parentnid, td, cred);
502 memcpy((char *)fdip->indata + sizeof(*fmni), cnp->cn_nameptr,
504 ((char *)fdip->indata)[sizeof(*fmni) + cnp->cn_namelen] = '\0';
507 struct vnop_create_args {
509 struct vnode **a_vpp;
510 struct componentname *a_cnp;
515 fuse_vnop_create(struct vop_create_args *ap)
517 struct vnode *dvp = ap->a_dvp;
518 struct vnode **vpp = ap->a_vpp;
519 struct componentname *cnp = ap->a_cnp;
520 struct vattr *vap = ap->a_vap;
521 struct thread *td = cnp->cn_thread;
522 struct ucred *cred = cnp->cn_cred;
524 struct fuse_open_in *foi;
525 struct fuse_entry_out *feo;
526 struct fuse_open_out *foo;
527 struct fuse_dispatcher fdi, fdi2;
528 struct fuse_dispatcher *fdip = &fdi;
529 struct fuse_dispatcher *fdip2 = NULL;
533 struct mount *mp = vnode_mount(dvp);
534 uint64_t parentnid = VTOFUD(dvp)->nid;
535 mode_t mode = MAKEIMODE(vap->va_type, vap->va_mode);
540 * VOP_CREATE doesn't tell us the open(2) flags, so we guess. Only a
541 * writable mode makes sense, and we might as well include readability
546 if (fuse_isdeadfs(dvp)) {
549 bzero(&fdi, sizeof(fdi));
551 if ((vap->va_type != VREG))
554 if (!fsess_isimpl(mp, FUSE_CREATE)) {
555 /* Fallback to FUSE_MKNOD/FUSE_OPEN */
556 fdisp_make_mknod_for_fallback(fdip, cnp, dvp, parentnid, td,
559 /* Use FUSE_CREATE */
561 fdisp_init(fdip, sizeof(*foi) + cnp->cn_namelen + 1);
562 fdisp_make(fdip, op, vnode_mount(dvp), parentnid, td, cred);
565 foi->flags = O_CREAT | flags;
566 memcpy((char *)fdip->indata + sizeof(*foi), cnp->cn_nameptr,
568 ((char *)fdip->indata)[sizeof(*foi) + cnp->cn_namelen] = '\0';
571 err = fdisp_wait_answ(fdip);
574 if (err == ENOSYS && op == FUSE_CREATE) {
575 fsess_set_notimpl(mp, FUSE_CREATE);
576 fdisp_make_mknod_for_fallback(fdip, cnp, dvp,
577 parentnid, td, cred, mode, &op);
578 err = fdisp_wait_answ(fdip);
586 if ((err = fuse_internal_checkentry(feo, VREG))) {
590 if (op == FUSE_CREATE) {
591 foo = (struct fuse_open_out*)(feo + 1);
593 /* Issue a separate FUSE_OPEN */
595 fdisp_init(fdip2, sizeof(*foi));
596 fdisp_make(fdip2, FUSE_OPEN, vnode_mount(dvp), feo->nodeid, td,
601 err = fdisp_wait_answ(fdip2);
606 err = fuse_vnode_get(mp, feo, feo->nodeid, dvp, vpp, cnp, VREG);
608 struct fuse_release_in *fri;
609 uint64_t nodeid = feo->nodeid;
610 uint64_t fh_id = foo->fh;
612 fdisp_init(fdip, sizeof(*fri));
613 fdisp_make(fdip, FUSE_RELEASE, mp, nodeid, td, cred);
617 fuse_insert_callback(fdip->tick, fuse_internal_forget_callback);
618 fuse_insert_message(fdip->tick, false);
621 ASSERT_VOP_ELOCKED(*vpp, "fuse_vnop_create");
622 fuse_internal_cache_attrs(*vpp, &feo->attr, feo->attr_valid,
623 feo->attr_valid_nsec, NULL);
625 fuse_filehandle_init(*vpp, FUFH_RDWR, NULL, td, cred, foo);
626 fuse_vnode_open(*vpp, foo->open_flags, td);
627 cache_purge_negative(dvp);
631 fdisp_destroy(fdip2);
637 struct vnop_fdatasync_args {
638 struct vop_generic_args a_gen;
640 struct thread * a_td;
644 fuse_vnop_fdatasync(struct vop_fdatasync_args *ap)
646 struct vnode *vp = ap->a_vp;
647 struct thread *td = ap->a_td;
648 int waitfor = MNT_WAIT;
652 if (fuse_isdeadfs(vp)) {
655 if ((err = vop_stdfdatasync_buf(ap)))
658 return fuse_internal_fsync(vp, td, waitfor, true);
662 struct vnop_fsync_args {
663 struct vop_generic_args a_gen;
666 struct thread * a_td;
670 fuse_vnop_fsync(struct vop_fsync_args *ap)
672 struct vnode *vp = ap->a_vp;
673 struct thread *td = ap->a_td;
674 int waitfor = ap->a_waitfor;
677 if (fuse_isdeadfs(vp)) {
680 if ((err = vop_stdfsync(ap)))
683 return fuse_internal_fsync(vp, td, waitfor, false);
687 struct vnop_getattr_args {
690 struct ucred *a_cred;
695 fuse_vnop_getattr(struct vop_getattr_args *ap)
697 struct vnode *vp = ap->a_vp;
698 struct vattr *vap = ap->a_vap;
699 struct ucred *cred = ap->a_cred;
700 struct thread *td = curthread;
705 dataflags = fuse_get_mpdata(vnode_mount(vp))->dataflags;
707 /* Note that we are not bailing out on a dead file system just yet. */
709 if (!(dataflags & FSESS_INITED)) {
710 if (!vnode_isvroot(vp)) {
711 fdata_set_dead(fuse_get_mpdata(vnode_mount(vp)));
718 err = fuse_internal_getattr(vp, vap, cred, td);
719 if (err == ENOTCONN && vnode_isvroot(vp)) {
720 /* see comment in fuse_vfsop_statfs() */
727 bzero(vap, sizeof(*vap));
728 vap->va_type = vnode_vtype(vp);
734 struct vnop_inactive_args {
740 fuse_vnop_inactive(struct vop_inactive_args *ap)
742 struct vnode *vp = ap->a_vp;
743 struct thread *td = ap->a_td;
745 struct fuse_vnode_data *fvdat = VTOFUD(vp);
746 struct fuse_filehandle *fufh, *fufh_tmp;
750 LIST_FOREACH_SAFE(fufh, &fvdat->handles, next, fufh_tmp) {
751 if (need_flush && vp->v_type == VREG) {
752 if ((VTOFUD(vp)->flag & FN_SIZECHANGE) != 0) {
753 fuse_vnode_savesize(vp, NULL, 0);
755 if (fuse_data_cache_invalidate ||
756 (fvdat->flag & FN_REVOKED) != 0)
757 fuse_io_invalbuf(vp, td);
759 fuse_io_flushbuf(vp, MNT_WAIT, td);
762 fuse_filehandle_close(vp, fufh, td, NULL);
765 if ((fvdat->flag & FN_REVOKED) != 0 && fuse_reclaim_revoked) {
772 struct vnop_link_args {
773 struct vnode *a_tdvp;
775 struct componentname *a_cnp;
779 fuse_vnop_link(struct vop_link_args *ap)
781 struct vnode *vp = ap->a_vp;
782 struct vnode *tdvp = ap->a_tdvp;
783 struct componentname *cnp = ap->a_cnp;
785 struct vattr *vap = VTOVA(vp);
787 struct fuse_dispatcher fdi;
788 struct fuse_entry_out *feo;
789 struct fuse_link_in fli;
793 if (fuse_isdeadfs(vp)) {
796 if (vnode_mount(tdvp) != vnode_mount(vp)) {
801 * This is a seatbelt check to protect naive userspace filesystems from
802 * themselves and the limitations of the FUSE IPC protocol. If a
803 * filesystem does not allow attribute caching, assume it is capable of
804 * validating that nlink does not overflow.
806 if (vap != NULL && vap->va_nlink >= FUSE_LINK_MAX)
808 fli.oldnodeid = VTOI(vp);
811 fuse_internal_newentry_makerequest(vnode_mount(tdvp), VTOI(tdvp), cnp,
812 FUSE_LINK, &fli, sizeof(fli), &fdi);
813 if ((err = fdisp_wait_answ(&fdi))) {
818 err = fuse_internal_checkentry(feo, vnode_vtype(vp));
824 struct fuse_lookup_alloc_arg {
825 struct fuse_entry_out *feo;
826 struct componentname *cnp;
831 /* Callback for vn_get_ino */
833 fuse_lookup_alloc(struct mount *mp, void *arg, int lkflags, struct vnode **vpp)
835 struct fuse_lookup_alloc_arg *flaa = arg;
837 return fuse_vnode_get(mp, flaa->feo, flaa->nid, NULL, vpp, flaa->cnp,
841 SDT_PROBE_DEFINE3(fusefs, , vnops, cache_lookup,
842 "int", "struct timespec*", "struct timespec*");
844 struct vnop_lookup_args {
845 struct vnodeop_desc *a_desc;
847 struct vnode **a_vpp;
848 struct componentname *a_cnp;
852 fuse_vnop_lookup(struct vop_lookup_args *ap)
854 struct vnode *dvp = ap->a_dvp;
855 struct vnode **vpp = ap->a_vpp;
856 struct componentname *cnp = ap->a_cnp;
857 struct thread *td = cnp->cn_thread;
858 struct ucred *cred = cnp->cn_cred;
860 int nameiop = cnp->cn_nameiop;
861 int flags = cnp->cn_flags;
862 int wantparent = flags & (LOCKPARENT | WANTPARENT);
863 int islastcn = flags & ISLASTCN;
864 struct mount *mp = vnode_mount(dvp);
868 struct vnode *vp = NULL;
870 struct fuse_dispatcher fdi;
871 bool did_lookup = false;
872 struct fuse_entry_out *feo = NULL;
873 enum vtype vtyp; /* vnode type of target */
874 off_t filesize; /* filesize of target */
878 if (fuse_isdeadfs(dvp)) {
882 if (!vnode_isdir(dvp))
885 if (islastcn && vfs_isrdonly(mp) && (nameiop != LOOKUP))
888 if ((err = fuse_internal_access(dvp, VEXEC, td, cred)))
891 if (flags & ISDOTDOT) {
892 nid = VTOFUD(dvp)->parent_nid;
895 /* .. is obviously a directory */
898 } else if (cnp->cn_namelen == 1 && *(cnp->cn_nameptr) == '.') {
900 /* . is obviously a directory */
904 struct timespec now, timeout;
906 err = cache_lookup(dvp, vpp, cnp, &timeout, NULL);
908 SDT_PROBE3(fusefs, , vnops, cache_lookup, err, &timeout, &now);
910 case -1: /* positive match */
911 if (timespeccmp(&timeout, &now, >)) {
912 atomic_add_acq_long(&fuse_lookup_cache_hits, 1);
915 atomic_add_acq_long(&fuse_lookup_cache_misses,
927 case 0: /* no match in cache */
928 atomic_add_acq_long(&fuse_lookup_cache_misses, 1);
931 case ENOENT: /* negative match */
933 if (timespeccmp(&timeout, &now, <=)) {
935 cache_purge_negative(dvp);
944 fdisp_init(&fdi, cnp->cn_namelen + 1);
945 fdisp_make(&fdi, FUSE_LOOKUP, mp, nid, td, cred);
947 memcpy(fdi.indata, cnp->cn_nameptr, cnp->cn_namelen);
948 ((char *)fdi.indata)[cnp->cn_namelen] = '\0';
949 lookup_err = fdisp_wait_answ(&fdi);
953 /* lookup call succeeded */
954 nid = ((struct fuse_entry_out *)fdi.answ)->nodeid;
955 feo = (struct fuse_entry_out *)fdi.answ;
957 /* zero nodeid means ENOENT and cache it */
958 struct timespec timeout;
960 fdi.answ_stat = ENOENT;
962 if (cnp->cn_flags & MAKEENTRY) {
963 fuse_validity_2_timespec(feo, &timeout);
964 cache_enter_time(dvp, *vpp, cnp,
967 } else if (nid == FUSE_ROOT_ID) {
970 vtyp = IFTOVT(feo->attr.mode);
971 filesize = feo->attr.size;
973 if (lookup_err && (!fdi.answ_stat || lookup_err != ENOENT)) {
978 /* lookup_err, if non-zero, must be ENOENT at this point */
981 /* Entry not found */
982 if ((nameiop == CREATE || nameiop == RENAME) && islastcn) {
983 err = fuse_internal_access(dvp, VWRITE, td, cred);
986 * Set the SAVENAME flag to hold onto the
987 * pathname for use later in VOP_CREATE or
990 cnp->cn_flags |= SAVENAME;
998 /* Entry was found */
999 if (flags & ISDOTDOT) {
1000 struct fuse_lookup_alloc_arg flaa;
1006 err = vn_vget_ino_gen(dvp, fuse_lookup_alloc, &flaa, 0,
1009 } else if (nid == VTOI(dvp)) {
1013 struct fuse_vnode_data *fvdat;
1015 err = fuse_vnode_get(vnode_mount(dvp), feo, nid, dvp,
1021 fuse_vnode_setparent(vp, dvp);
1024 * In the case where we are looking up a FUSE node
1025 * represented by an existing cached vnode, and the
1026 * true size reported by FUSE_LOOKUP doesn't match
1027 * the vnode's cached size, fix the vnode cache to
1028 * match the real object size.
1031 * * following attribute cache expiration, or
1032 * * due a bug in the daemon, or
1033 * * the first time that we looked up the file.
1036 if (vnode_isreg(vp) &&
1037 filesize != fvdat->filesize) {
1039 * The FN_SIZECHANGE flag reflects a dirty
1040 * append. If userspace lets us know our cache
1041 * is invalid, that write was lost. (Dirty
1042 * writes that do not cause append are also
1043 * lost, but we don't detect them here.)
1045 * XXX: Maybe disable WB caching on this mount.
1047 if (fvdat->flag & FN_SIZECHANGE)
1048 printf("%s: WB cache incoherent on "
1050 vnode_mount(vp)->mnt_stat.f_mntonname);
1052 (void)fuse_vnode_setsize(vp, cred, filesize);
1053 fvdat->flag &= ~FN_SIZECHANGE;
1057 fuse_internal_cache_attrs(*vpp, &feo->attr,
1058 feo->attr_valid, feo->attr_valid_nsec, NULL);
1060 if ((nameiop == DELETE || nameiop == RENAME) &&
1063 struct vattr dvattr;
1065 err = fuse_internal_access(dvp, VWRITE, td,
1070 * if the parent's sticky bit is set, check
1071 * whether we're allowed to remove the file.
1072 * Need to figure out the vnode locking to make
1075 fuse_internal_getattr(dvp, &dvattr, cred, td);
1076 if ((dvattr.va_mode & S_ISTXT) &&
1077 fuse_internal_access(dvp, VADMIN, td,
1079 fuse_internal_access(*vpp, VADMIN, td,
1087 (nameiop == DELETE) ||
1088 (nameiop == RENAME && wantparent))) {
1089 cnp->cn_flags |= SAVENAME;
1096 if (vp != NULL && dvp != vp)
1098 else if (vp != NULL)
1103 fdisp_destroy(&fdi);
1109 struct vnop_mkdir_args {
1110 struct vnode *a_dvp;
1111 struct vnode **a_vpp;
1112 struct componentname *a_cnp;
1113 struct vattr *a_vap;
1117 fuse_vnop_mkdir(struct vop_mkdir_args *ap)
1119 struct vnode *dvp = ap->a_dvp;
1120 struct vnode **vpp = ap->a_vpp;
1121 struct componentname *cnp = ap->a_cnp;
1122 struct vattr *vap = ap->a_vap;
1124 struct fuse_mkdir_in fmdi;
1126 if (fuse_isdeadfs(dvp)) {
1129 fmdi.mode = MAKEIMODE(vap->va_type, vap->va_mode);
1131 return (fuse_internal_newentry(dvp, vpp, cnp, FUSE_MKDIR, &fmdi,
1132 sizeof(fmdi), VDIR));
1136 struct vnop_mknod_args {
1137 struct vnode *a_dvp;
1138 struct vnode **a_vpp;
1139 struct componentname *a_cnp;
1140 struct vattr *a_vap;
1144 fuse_vnop_mknod(struct vop_mknod_args *ap)
1147 struct vnode *dvp = ap->a_dvp;
1148 struct vnode **vpp = ap->a_vpp;
1149 struct componentname *cnp = ap->a_cnp;
1150 struct vattr *vap = ap->a_vap;
1151 struct fuse_mknod_in fmni;
1153 if (fuse_isdeadfs(dvp))
1156 fmni.mode = MAKEIMODE(vap->va_type, vap->va_mode);
1157 fmni.rdev = vap->va_rdev;
1158 return (fuse_internal_newentry(dvp, vpp, cnp, FUSE_MKNOD, &fmni,
1159 sizeof(fmni), vap->va_type));
1163 struct vnop_open_args {
1166 struct ucred *a_cred;
1167 struct thread *a_td;
1168 int a_fdidx; / struct file *a_fp;
1172 fuse_vnop_open(struct vop_open_args *ap)
1174 struct vnode *vp = ap->a_vp;
1175 int a_mode = ap->a_mode;
1176 struct thread *td = ap->a_td;
1177 struct ucred *cred = ap->a_cred;
1178 pid_t pid = td->td_proc->p_pid;
1179 struct fuse_vnode_data *fvdat;
1181 if (fuse_isdeadfs(vp))
1183 if (vp->v_type == VCHR || vp->v_type == VBLK || vp->v_type == VFIFO)
1184 return (EOPNOTSUPP);
1185 if ((a_mode & (FREAD | FWRITE | FEXEC)) == 0)
1190 if (fuse_filehandle_validrw(vp, a_mode, cred, pid)) {
1191 fuse_vnode_open(vp, 0, td);
1195 return fuse_filehandle_open(vp, a_mode, NULL, td, cred);
1199 fuse_vnop_pathconf(struct vop_pathconf_args *ap)
1202 switch (ap->a_name) {
1203 case _PC_FILESIZEBITS:
1207 *ap->a_retval = NAME_MAX;
1210 *ap->a_retval = MIN(LONG_MAX, FUSE_LINK_MAX);
1212 case _PC_SYMLINK_MAX:
1213 *ap->a_retval = MAXPATHLEN;
1219 return (vop_stdpathconf(ap));
1224 struct vnop_read_args {
1228 struct ucred *a_cred;
1232 fuse_vnop_read(struct vop_read_args *ap)
1234 struct vnode *vp = ap->a_vp;
1235 struct uio *uio = ap->a_uio;
1236 int ioflag = ap->a_ioflag;
1237 struct ucred *cred = ap->a_cred;
1238 pid_t pid = curthread->td_proc->p_pid;
1240 if (fuse_isdeadfs(vp)) {
1244 if (VTOFUD(vp)->flag & FN_DIRECTIO) {
1245 ioflag |= IO_DIRECT;
1248 return fuse_io_dispatch(vp, uio, ioflag, false, cred, pid);
1252 struct vnop_readdir_args {
1255 struct ucred *a_cred;
1262 fuse_vnop_readdir(struct vop_readdir_args *ap)
1264 struct vnode *vp = ap->a_vp;
1265 struct uio *uio = ap->a_uio;
1266 struct ucred *cred = ap->a_cred;
1267 struct fuse_filehandle *fufh = NULL;
1268 struct fuse_iov cookediov;
1270 pid_t pid = curthread->td_proc->p_pid;
1272 if (fuse_isdeadfs(vp)) {
1275 if ( /* XXXIP ((uio_iovcnt(uio) > 1)) || */
1276 (uio_resid(uio) < sizeof(struct dirent))) {
1280 err = fuse_filehandle_get_dir(vp, &fufh, cred, pid);
1283 #define DIRCOOKEDSIZE FUSE_DIRENT_ALIGN(FUSE_NAME_OFFSET + MAXNAMLEN + 1)
1284 fiov_init(&cookediov, DIRCOOKEDSIZE);
1286 err = fuse_internal_readdir(vp, uio, fufh, &cookediov);
1288 fiov_teardown(&cookediov);
1294 struct vnop_readlink_args {
1297 struct ucred *a_cred;
1301 fuse_vnop_readlink(struct vop_readlink_args *ap)
1303 struct vnode *vp = ap->a_vp;
1304 struct uio *uio = ap->a_uio;
1305 struct ucred *cred = ap->a_cred;
1307 struct fuse_dispatcher fdi;
1310 if (fuse_isdeadfs(vp)) {
1313 if (!vnode_islnk(vp)) {
1316 fdisp_init(&fdi, 0);
1317 err = fdisp_simple_putget_vp(&fdi, FUSE_READLINK, vp, curthread, cred);
1321 if (((char *)fdi.answ)[0] == '/' &&
1322 fuse_get_mpdata(vnode_mount(vp))->dataflags & FSESS_PUSH_SYMLINKS_IN) {
1323 char *mpth = vnode_mount(vp)->mnt_stat.f_mntonname;
1325 err = uiomove(mpth, strlen(mpth), uio);
1328 err = uiomove(fdi.answ, fdi.iosize, uio);
1331 fdisp_destroy(&fdi);
1336 struct vnop_reclaim_args {
1338 struct thread *a_td;
1342 fuse_vnop_reclaim(struct vop_reclaim_args *ap)
1344 struct vnode *vp = ap->a_vp;
1345 struct thread *td = ap->a_td;
1346 struct fuse_vnode_data *fvdat = VTOFUD(vp);
1347 struct fuse_filehandle *fufh, *fufh_tmp;
1350 panic("FUSE: no vnode data during recycling");
1352 LIST_FOREACH_SAFE(fufh, &fvdat->handles, next, fufh_tmp) {
1353 printf("FUSE: vnode being reclaimed with open fufh "
1354 "(type=%#x)", fufh->fufh_type);
1355 fuse_filehandle_close(vp, fufh, td, NULL);
1358 if ((!fuse_isdeadfs(vp)) && (fvdat->nlookup)) {
1359 fuse_internal_forget_send(vnode_mount(vp), td, NULL, VTOI(vp),
1362 fuse_vnode_setparent(vp, NULL);
1364 vfs_hash_remove(vp);
1365 vnode_destroy_vobject(vp);
1366 fuse_vnode_destroy(vp);
1372 struct vnop_remove_args {
1373 struct vnode *a_dvp;
1375 struct componentname *a_cnp;
1379 fuse_vnop_remove(struct vop_remove_args *ap)
1381 struct vnode *dvp = ap->a_dvp;
1382 struct vnode *vp = ap->a_vp;
1383 struct componentname *cnp = ap->a_cnp;
1387 if (fuse_isdeadfs(vp)) {
1390 if (vnode_isdir(vp)) {
1395 err = fuse_internal_remove(dvp, vp, cnp, FUSE_UNLINK);
1398 fuse_internal_vnode_disappear(vp);
1403 struct vnop_rename_args {
1404 struct vnode *a_fdvp;
1405 struct vnode *a_fvp;
1406 struct componentname *a_fcnp;
1407 struct vnode *a_tdvp;
1408 struct vnode *a_tvp;
1409 struct componentname *a_tcnp;
1413 fuse_vnop_rename(struct vop_rename_args *ap)
1415 struct vnode *fdvp = ap->a_fdvp;
1416 struct vnode *fvp = ap->a_fvp;
1417 struct componentname *fcnp = ap->a_fcnp;
1418 struct vnode *tdvp = ap->a_tdvp;
1419 struct vnode *tvp = ap->a_tvp;
1420 struct componentname *tcnp = ap->a_tcnp;
1421 struct fuse_data *data;
1425 if (fuse_isdeadfs(fdvp)) {
1428 if (fvp->v_mount != tdvp->v_mount ||
1429 (tvp && fvp->v_mount != tvp->v_mount)) {
1430 SDT_PROBE2(fusefs, , vnops, trace, 1, "cross-device rename");
1437 * FUSE library is expected to check if target directory is not
1438 * under the source directory in the file system tree.
1439 * Linux performs this check at VFS level.
1441 data = fuse_get_mpdata(vnode_mount(tdvp));
1442 sx_xlock(&data->rename_lock);
1443 err = fuse_internal_rename(fdvp, fcnp, tdvp, tcnp);
1446 fuse_vnode_setparent(fvp, tdvp);
1448 fuse_vnode_setparent(tvp, NULL);
1450 sx_unlock(&data->rename_lock);
1452 if (tvp != NULL && tvp != fvp) {
1455 if (vnode_isdir(fvp)) {
1456 if ((tvp != NULL) && vnode_isdir(tvp)) {
1477 struct vnop_rmdir_args {
1478 struct vnode *a_dvp;
1480 struct componentname *a_cnp;
1484 fuse_vnop_rmdir(struct vop_rmdir_args *ap)
1486 struct vnode *dvp = ap->a_dvp;
1487 struct vnode *vp = ap->a_vp;
1491 if (fuse_isdeadfs(vp)) {
1494 if (VTOFUD(vp) == VTOFUD(dvp)) {
1497 err = fuse_internal_remove(dvp, vp, ap->a_cnp, FUSE_RMDIR);
1500 fuse_internal_vnode_disappear(vp);
1505 struct vnop_setattr_args {
1507 struct vattr *a_vap;
1508 struct ucred *a_cred;
1509 struct thread *a_td;
1513 fuse_vnop_setattr(struct vop_setattr_args *ap)
1515 struct vnode *vp = ap->a_vp;
1516 struct vattr *vap = ap->a_vap;
1517 struct ucred *cred = ap->a_cred;
1518 struct thread *td = curthread;
1519 struct fuse_dispatcher fdi;
1520 struct fuse_setattr_in *fsai;
1521 pid_t pid = td->td_proc->p_pid;
1525 int sizechanged = 0;
1526 uint64_t newsize = 0;
1527 accmode_t accmode = 0;
1529 if (fuse_isdeadfs(vp)) {
1532 fdisp_init(&fdi, sizeof(*fsai));
1533 fdisp_make_vp(&fdi, FUSE_SETATTR, vp, td, cred);
1537 if (vap->va_uid != (uid_t)VNOVAL) {
1538 fsai->uid = vap->va_uid;
1539 fsai->valid |= FATTR_UID;
1542 if (vap->va_gid != (gid_t)VNOVAL) {
1543 fsai->gid = vap->va_gid;
1544 fsai->valid |= FATTR_GID;
1547 if (vap->va_size != VNOVAL) {
1549 struct fuse_filehandle *fufh = NULL;
1551 /*Truncate to a new value. */
1552 fsai->size = vap->va_size;
1554 newsize = vap->va_size;
1555 fsai->valid |= FATTR_SIZE;
1558 fuse_filehandle_getrw(vp, FWRITE, &fufh, cred, pid);
1560 fsai->fh = fufh->fh_id;
1561 fsai->valid |= FATTR_FH;
1564 if (vap->va_atime.tv_sec != VNOVAL) {
1565 fsai->atime = vap->va_atime.tv_sec;
1566 fsai->atimensec = vap->va_atime.tv_nsec;
1567 fsai->valid |= FATTR_ATIME;
1570 * TODO: only require VWRITE if UTIMENS_NULL is set. PR 237181
1573 if (vap->va_mtime.tv_sec != VNOVAL) {
1574 fsai->mtime = vap->va_mtime.tv_sec;
1575 fsai->mtimensec = vap->va_mtime.tv_nsec;
1576 fsai->valid |= FATTR_MTIME;
1579 * TODO: only require VWRITE if UTIMENS_NULL is set. PR 237181
1582 if (vap->va_mode != (mode_t)VNOVAL) {
1583 fsai->mode = vap->va_mode & ALLPERMS;
1584 fsai->valid |= FATTR_MODE;
1590 vtyp = vnode_vtype(vp);
1592 if (fsai->valid & FATTR_SIZE && vtyp == VDIR) {
1596 if (vfs_isrdonly(vnode_mount(vp)) && (fsai->valid & ~FATTR_SIZE || vtyp == VREG)) {
1600 err = fuse_internal_access(vp, accmode, td, cred);
1604 if ((err = fdisp_wait_answ(&fdi)))
1606 vtyp = IFTOVT(((struct fuse_attr_out *)fdi.answ)->attr.mode);
1608 if (vnode_vtype(vp) != vtyp) {
1609 if (vnode_vtype(vp) == VNON && vtyp != VNON) {
1610 SDT_PROBE2(fusefs, , vnops, trace, 1, "FUSE: Dang! "
1611 "vnode_vtype is VNON and vtype isn't.");
1614 * STALE vnode, ditch
1616 * The vnode has changed its type "behind our back".
1617 * There's nothing really we can do, so let us just
1618 * force an internal revocation and tell the caller to
1619 * try again, if interested.
1621 fuse_internal_vnode_disappear(vp);
1626 struct fuse_attr_out *fao = (struct fuse_attr_out*)fdi.answ;
1627 fuse_internal_cache_attrs(vp, &fao->attr, fao->attr_valid,
1628 fao->attr_valid_nsec, NULL);
1632 fdisp_destroy(&fdi);
1633 if (!err && sizechanged) {
1634 fuse_vnode_setsize(vp, cred, newsize);
1635 VTOFUD(vp)->flag &= ~FN_SIZECHANGE;
1641 struct vnop_strategy_args {
1647 fuse_vnop_strategy(struct vop_strategy_args *ap)
1649 struct vnode *vp = ap->a_vp;
1650 struct buf *bp = ap->a_bp;
1652 if (!vp || fuse_isdeadfs(vp)) {
1653 bp->b_ioflags |= BIO_ERROR;
1654 bp->b_error = ENXIO;
1658 if (bp->b_iocmd == BIO_WRITE) {
1661 err = fuse_vnode_refreshsize(vp, NOCRED);
1663 bp->b_ioflags |= BIO_ERROR;
1671 * VOP_STRATEGY always returns zero and signals error via bp->b_ioflags.
1672 * fuse_io_strategy sets bp's error fields
1674 (void)fuse_io_strategy(vp, bp);
1681 struct vnop_symlink_args {
1682 struct vnode *a_dvp;
1683 struct vnode **a_vpp;
1684 struct componentname *a_cnp;
1685 struct vattr *a_vap;
1690 fuse_vnop_symlink(struct vop_symlink_args *ap)
1692 struct vnode *dvp = ap->a_dvp;
1693 struct vnode **vpp = ap->a_vpp;
1694 struct componentname *cnp = ap->a_cnp;
1695 const char *target = ap->a_target;
1697 struct fuse_dispatcher fdi;
1702 if (fuse_isdeadfs(dvp)) {
1706 * Unlike the other creator type calls, here we have to create a message
1707 * where the name of the new entry comes first, and the data describing
1708 * the entry comes second.
1709 * Hence we can't rely on our handy fuse_internal_newentry() routine,
1710 * but put together the message manually and just call the core part.
1713 len = strlen(target) + 1;
1714 fdisp_init(&fdi, len + cnp->cn_namelen + 1);
1715 fdisp_make_vp(&fdi, FUSE_SYMLINK, dvp, curthread, NULL);
1717 memcpy(fdi.indata, cnp->cn_nameptr, cnp->cn_namelen);
1718 ((char *)fdi.indata)[cnp->cn_namelen] = '\0';
1719 memcpy((char *)fdi.indata + cnp->cn_namelen + 1, target, len);
1721 err = fuse_internal_newentry_core(dvp, vpp, cnp, VLNK, &fdi);
1722 fdisp_destroy(&fdi);
1727 struct vnop_write_args {
1731 struct ucred *a_cred;
1735 fuse_vnop_write(struct vop_write_args *ap)
1737 struct vnode *vp = ap->a_vp;
1738 struct uio *uio = ap->a_uio;
1739 int ioflag = ap->a_ioflag;
1740 struct ucred *cred = ap->a_cred;
1741 pid_t pid = curthread->td_proc->p_pid;
1744 if (fuse_isdeadfs(vp)) {
1747 err = fuse_vnode_refreshsize(vp, cred);
1751 if (VTOFUD(vp)->flag & FN_DIRECTIO) {
1752 ioflag |= IO_DIRECT;
1755 return fuse_io_dispatch(vp, uio, ioflag, false, cred, pid);
1758 SDT_PROBE_DEFINE1(fusefs, , vnops, vnop_getpages_error, "int");
1760 struct vnop_getpages_args {
1768 fuse_vnop_getpages(struct vop_getpages_args *ap)
1770 int i, error, nextoff, size, toff, count, npages;
1779 pid_t pid = curthread->td_proc->p_pid;
1782 KASSERT(vp->v_object, ("objectless vp passed to getpages"));
1783 td = curthread; /* XXX */
1784 cred = curthread->td_ucred; /* XXX */
1786 npages = ap->a_count;
1788 if (!fsess_opt_mmap(vnode_mount(vp))) {
1789 SDT_PROBE2(fusefs, , vnops, trace, 1,
1790 "called on non-cacheable vnode??\n");
1791 return (VM_PAGER_ERROR);
1795 * If the last page is partially valid, just return it and allow
1796 * the pager to zero-out the blanks. Partially valid pages can
1797 * only occur at the file EOF.
1799 * XXXGL: is that true for FUSE, which is a local filesystem,
1800 * but still somewhat disconnected from the kernel?
1802 VM_OBJECT_WLOCK(vp->v_object);
1803 if (pages[npages - 1]->valid != 0 && --npages == 0)
1805 VM_OBJECT_WUNLOCK(vp->v_object);
1808 * We use only the kva address for the buffer, but this is extremely
1809 * convenient and fast.
1811 bp = uma_zalloc(fuse_pbuf_zone, M_WAITOK);
1813 kva = (vm_offset_t)bp->b_data;
1814 pmap_qenter(kva, pages, npages);
1815 VM_CNT_INC(v_vnodein);
1816 VM_CNT_ADD(v_vnodepgsin, npages);
1818 count = npages << PAGE_SHIFT;
1819 iov.iov_base = (caddr_t)kva;
1820 iov.iov_len = count;
1823 uio.uio_offset = IDX_TO_OFF(pages[0]->pindex);
1824 uio.uio_resid = count;
1825 uio.uio_segflg = UIO_SYSSPACE;
1826 uio.uio_rw = UIO_READ;
1829 error = fuse_io_dispatch(vp, &uio, IO_DIRECT, true, cred, pid);
1830 pmap_qremove(kva, npages);
1832 uma_zfree(fuse_pbuf_zone, bp);
1834 if (error && (uio.uio_resid == count)) {
1835 SDT_PROBE1(fusefs, , vnops, vnop_getpages_error, error);
1836 return VM_PAGER_ERROR;
1839 * Calculate the number of bytes read and validate only that number
1840 * of bytes. Note that due to pending writes, size may be 0. This
1841 * does not mean that the remaining data is invalid!
1844 size = count - uio.uio_resid;
1845 VM_OBJECT_WLOCK(vp->v_object);
1846 fuse_vm_page_lock_queues();
1847 for (i = 0, toff = 0; i < npages; i++, toff = nextoff) {
1850 nextoff = toff + PAGE_SIZE;
1853 if (nextoff <= size) {
1855 * Read operation filled an entire page
1857 m->valid = VM_PAGE_BITS_ALL;
1858 KASSERT(m->dirty == 0,
1859 ("fuse_getpages: page %p is dirty", m));
1860 } else if (size > toff) {
1862 * Read operation filled a partial page.
1865 vm_page_set_valid_range(m, 0, size - toff);
1866 KASSERT(m->dirty == 0,
1867 ("fuse_getpages: page %p is dirty", m));
1870 * Read operation was short. If no error occurred
1871 * we may have hit a zero-fill section. We simply
1872 * leave valid set to 0.
1877 fuse_vm_page_unlock_queues();
1879 VM_OBJECT_WUNLOCK(vp->v_object);
1884 return (VM_PAGER_OK);
1888 struct vnop_putpages_args {
1894 vm_ooffset_t a_offset;
1898 fuse_vnop_putpages(struct vop_putpages_args *ap)
1904 int i, error, npages, count;
1912 pid_t pid = curthread->td_proc->p_pid;
1915 KASSERT(vp->v_object, ("objectless vp passed to putpages"));
1916 fsize = vp->v_object->un_pager.vnp.vnp_size;
1917 td = curthread; /* XXX */
1918 cred = curthread->td_ucred; /* XXX */
1920 count = ap->a_count;
1921 rtvals = ap->a_rtvals;
1922 npages = btoc(count);
1923 offset = IDX_TO_OFF(pages[0]->pindex);
1925 if (!fsess_opt_mmap(vnode_mount(vp))) {
1926 SDT_PROBE2(fusefs, , vnops, trace, 1,
1927 "called on non-cacheable vnode??\n");
1929 for (i = 0; i < npages; i++)
1930 rtvals[i] = VM_PAGER_AGAIN;
1933 * When putting pages, do not extend file past EOF.
1936 if (offset + count > fsize) {
1937 count = fsize - offset;
1942 * We use only the kva address for the buffer, but this is extremely
1943 * convenient and fast.
1945 bp = uma_zalloc(fuse_pbuf_zone, M_WAITOK);
1947 kva = (vm_offset_t)bp->b_data;
1948 pmap_qenter(kva, pages, npages);
1949 VM_CNT_INC(v_vnodeout);
1950 VM_CNT_ADD(v_vnodepgsout, count);
1952 iov.iov_base = (caddr_t)kva;
1953 iov.iov_len = count;
1956 uio.uio_offset = offset;
1957 uio.uio_resid = count;
1958 uio.uio_segflg = UIO_SYSSPACE;
1959 uio.uio_rw = UIO_WRITE;
1962 error = fuse_io_dispatch(vp, &uio, IO_DIRECT, true, cred, pid);
1964 pmap_qremove(kva, npages);
1965 uma_zfree(fuse_pbuf_zone, bp);
1968 int nwritten = round_page(count - uio.uio_resid) / PAGE_SIZE;
1970 for (i = 0; i < nwritten; i++) {
1971 rtvals[i] = VM_PAGER_OK;
1972 VM_OBJECT_WLOCK(pages[i]->object);
1973 vm_page_undirty(pages[i]);
1974 VM_OBJECT_WUNLOCK(pages[i]->object);
1980 static const char extattr_namespace_separator = '.';
1983 struct vop_getextattr_args {
1984 struct vop_generic_args a_gen;
1986 int a_attrnamespace;
1990 struct ucred *a_cred;
1991 struct thread *a_td;
1995 fuse_vnop_getextattr(struct vop_getextattr_args *ap)
1997 struct vnode *vp = ap->a_vp;
1998 struct uio *uio = ap->a_uio;
1999 struct fuse_dispatcher fdi;
2000 struct fuse_getxattr_in *get_xattr_in;
2001 struct fuse_getxattr_out *get_xattr_out;
2002 struct mount *mp = vnode_mount(vp);
2003 struct thread *td = ap->a_td;
2004 struct ucred *cred = ap->a_cred;
2010 if (fuse_isdeadfs(vp))
2013 if (!fsess_isimpl(mp, FUSE_GETXATTR))
2016 err = fuse_extattr_check_cred(vp, ap->a_attrnamespace, cred, td, VREAD);
2020 /* Default to looking for user attributes. */
2021 if (ap->a_attrnamespace == EXTATTR_NAMESPACE_SYSTEM)
2022 prefix = EXTATTR_NAMESPACE_SYSTEM_STRING;
2024 prefix = EXTATTR_NAMESPACE_USER_STRING;
2026 len = strlen(prefix) + sizeof(extattr_namespace_separator) +
2027 strlen(ap->a_name) + 1;
2029 fdisp_init(&fdi, len + sizeof(*get_xattr_in));
2030 fdisp_make_vp(&fdi, FUSE_GETXATTR, vp, td, cred);
2032 get_xattr_in = fdi.indata;
2034 * Check to see whether we're querying the available size or
2035 * issuing the actual request. If we pass in 0, we get back struct
2036 * fuse_getxattr_out. If we pass in a non-zero size, we get back
2037 * that much data, without the struct fuse_getxattr_out header.
2040 get_xattr_in->size = 0;
2042 get_xattr_in->size = uio->uio_resid;
2044 attr_str = (char *)fdi.indata + sizeof(*get_xattr_in);
2045 snprintf(attr_str, len, "%s%c%s", prefix, extattr_namespace_separator,
2048 err = fdisp_wait_answ(&fdi);
2050 if (err == ENOSYS) {
2051 fsess_set_notimpl(mp, FUSE_GETXATTR);
2057 get_xattr_out = fdi.answ;
2059 if (ap->a_size != NULL)
2060 *ap->a_size = get_xattr_out->size;
2063 err = uiomove(fdi.answ, fdi.iosize, uio);
2066 fdisp_destroy(&fdi);
2071 struct vop_setextattr_args {
2072 struct vop_generic_args a_gen;
2074 int a_attrnamespace;
2077 struct ucred *a_cred;
2078 struct thread *a_td;
2082 fuse_vnop_setextattr(struct vop_setextattr_args *ap)
2084 struct vnode *vp = ap->a_vp;
2085 struct uio *uio = ap->a_uio;
2086 struct fuse_dispatcher fdi;
2087 struct fuse_setxattr_in *set_xattr_in;
2088 struct mount *mp = vnode_mount(vp);
2089 struct thread *td = ap->a_td;
2090 struct ucred *cred = ap->a_cred;
2096 if (fuse_isdeadfs(vp))
2099 if (!fsess_isimpl(mp, FUSE_SETXATTR))
2102 if (vfs_isrdonly(mp))
2105 /* Deleting xattrs must use VOP_DELETEEXTATTR instead */
2106 if (ap->a_uio == NULL) {
2108 * If we got here as fallback from VOP_DELETEEXTATTR, then
2109 * return EOPNOTSUPP.
2111 if (!fsess_isimpl(mp, FUSE_REMOVEXATTR))
2112 return (EOPNOTSUPP);
2117 err = fuse_extattr_check_cred(vp, ap->a_attrnamespace, cred, td,
2122 /* Default to looking for user attributes. */
2123 if (ap->a_attrnamespace == EXTATTR_NAMESPACE_SYSTEM)
2124 prefix = EXTATTR_NAMESPACE_SYSTEM_STRING;
2126 prefix = EXTATTR_NAMESPACE_USER_STRING;
2128 len = strlen(prefix) + sizeof(extattr_namespace_separator) +
2129 strlen(ap->a_name) + 1;
2131 fdisp_init(&fdi, len + sizeof(*set_xattr_in) + uio->uio_resid);
2132 fdisp_make_vp(&fdi, FUSE_SETXATTR, vp, td, cred);
2134 set_xattr_in = fdi.indata;
2135 set_xattr_in->size = uio->uio_resid;
2137 attr_str = (char *)fdi.indata + sizeof(*set_xattr_in);
2138 snprintf(attr_str, len, "%s%c%s", prefix, extattr_namespace_separator,
2141 err = uiomove((char *)fdi.indata + sizeof(*set_xattr_in) + len,
2142 uio->uio_resid, uio);
2147 err = fdisp_wait_answ(&fdi);
2149 if (err == ENOSYS) {
2150 fsess_set_notimpl(mp, FUSE_SETXATTR);
2153 if (err == ERESTART) {
2154 /* Can't restart after calling uiomove */
2159 fdisp_destroy(&fdi);
2164 * The Linux / FUSE extended attribute list is simply a collection of
2165 * NUL-terminated strings. The FreeBSD extended attribute list is a single
2166 * byte length followed by a non-NUL terminated string. So, this allows
2167 * conversion of the Linux / FUSE format to the FreeBSD format in place.
2168 * Linux attribute names are reported with the namespace as a prefix (e.g.
2169 * "user.attribute_name"), but in FreeBSD they are reported without the
2170 * namespace prefix (e.g. "attribute_name"). So, we're going from:
2172 * user.attr_name1\0user.attr_name2\0
2176 * <num>attr_name1<num>attr_name2
2178 * Where "<num>" is a single byte number of characters in the attribute name.
2181 * prefix - exattr namespace prefix string
2182 * list, list_len - input list with namespace prefixes
2183 * bsd_list, bsd_list_len - output list compatible with bsd vfs
2186 fuse_xattrlist_convert(char *prefix, const char *list, int list_len,
2187 char *bsd_list, int *bsd_list_len)
2189 int len, pos, dist_to_next, prefix_len;
2193 prefix_len = strlen(prefix);
2195 while (pos < list_len && list[pos] != '\0') {
2196 dist_to_next = strlen(&list[pos]) + 1;
2197 if (bcmp(&list[pos], prefix, prefix_len) == 0 &&
2198 list[pos + prefix_len] == extattr_namespace_separator) {
2199 len = dist_to_next -
2200 (prefix_len + sizeof(extattr_namespace_separator)) - 1;
2201 if (len >= EXTATTR_MAXNAMELEN)
2202 return (ENAMETOOLONG);
2204 bsd_list[*bsd_list_len] = len;
2205 memcpy(&bsd_list[*bsd_list_len + 1],
2206 &list[pos + prefix_len +
2207 sizeof(extattr_namespace_separator)], len);
2209 *bsd_list_len += len + 1;
2212 pos += dist_to_next;
2219 struct vop_listextattr_args {
2220 struct vop_generic_args a_gen;
2222 int a_attrnamespace;
2225 struct ucred *a_cred;
2226 struct thread *a_td;
2230 fuse_vnop_listextattr(struct vop_listextattr_args *ap)
2232 struct vnode *vp = ap->a_vp;
2233 struct uio *uio = ap->a_uio;
2234 struct fuse_dispatcher fdi;
2235 struct fuse_listxattr_in *list_xattr_in;
2236 struct fuse_listxattr_out *list_xattr_out;
2237 struct mount *mp = vnode_mount(vp);
2238 struct thread *td = ap->a_td;
2239 struct ucred *cred = ap->a_cred;
2243 char *bsd_list = NULL;
2249 if (fuse_isdeadfs(vp))
2252 if (!fsess_isimpl(mp, FUSE_LISTXATTR))
2255 err = fuse_extattr_check_cred(vp, ap->a_attrnamespace, cred, td, VREAD);
2260 * Add space for a NUL and the period separator if enabled.
2261 * Default to looking for user attributes.
2263 if (ap->a_attrnamespace == EXTATTR_NAMESPACE_SYSTEM)
2264 prefix = EXTATTR_NAMESPACE_SYSTEM_STRING;
2266 prefix = EXTATTR_NAMESPACE_USER_STRING;
2268 len = strlen(prefix) + sizeof(extattr_namespace_separator) + 1;
2270 fdisp_init(&fdi, sizeof(*list_xattr_in) + len);
2271 fdisp_make_vp(&fdi, FUSE_LISTXATTR, vp, td, cred);
2274 * Retrieve Linux / FUSE compatible list size.
2276 list_xattr_in = fdi.indata;
2277 list_xattr_in->size = 0;
2278 attr_str = (char *)fdi.indata + sizeof(*list_xattr_in);
2279 snprintf(attr_str, len, "%s%c", prefix, extattr_namespace_separator);
2281 err = fdisp_wait_answ(&fdi);
2283 if (err == ENOSYS) {
2284 fsess_set_notimpl(mp, FUSE_LISTXATTR);
2290 list_xattr_out = fdi.answ;
2291 linux_list_len = list_xattr_out->size;
2292 if (linux_list_len == 0) {
2293 if (ap->a_size != NULL)
2294 *ap->a_size = linux_list_len;
2299 * Retrieve Linux / FUSE compatible list values.
2301 fdisp_refresh_vp(&fdi, FUSE_LISTXATTR, vp, td, cred);
2302 list_xattr_in = fdi.indata;
2303 list_xattr_in->size = linux_list_len + sizeof(*list_xattr_out);
2304 list_xattr_in->flags = 0;
2305 attr_str = (char *)fdi.indata + sizeof(*list_xattr_in);
2306 snprintf(attr_str, len, "%s%c", prefix, extattr_namespace_separator);
2308 err = fdisp_wait_answ(&fdi);
2312 linux_list = fdi.answ;
2313 linux_list_len = fdi.iosize;
2316 * Retrieve the BSD compatible list values.
2317 * The Linux / FUSE attribute list format isn't the same
2318 * as FreeBSD's format. So we need to transform it into
2319 * FreeBSD's format before giving it to the user.
2321 bsd_list = malloc(linux_list_len, M_TEMP, M_WAITOK);
2322 err = fuse_xattrlist_convert(prefix, linux_list, linux_list_len,
2323 bsd_list, &bsd_list_len);
2327 if (ap->a_size != NULL)
2328 *ap->a_size = bsd_list_len;
2331 err = uiomove(bsd_list, bsd_list_len, uio);
2334 free(bsd_list, M_TEMP);
2335 fdisp_destroy(&fdi);
2340 struct vop_deleteextattr_args {
2341 struct vop_generic_args a_gen;
2343 int a_attrnamespace;
2345 struct ucred *a_cred;
2346 struct thread *a_td;
2350 fuse_vnop_deleteextattr(struct vop_deleteextattr_args *ap)
2352 struct vnode *vp = ap->a_vp;
2353 struct fuse_dispatcher fdi;
2354 struct mount *mp = vnode_mount(vp);
2355 struct thread *td = ap->a_td;
2356 struct ucred *cred = ap->a_cred;
2362 if (fuse_isdeadfs(vp))
2365 if (!fsess_isimpl(mp, FUSE_REMOVEXATTR))
2368 if (vfs_isrdonly(mp))
2371 err = fuse_extattr_check_cred(vp, ap->a_attrnamespace, cred, td,
2376 /* Default to looking for user attributes. */
2377 if (ap->a_attrnamespace == EXTATTR_NAMESPACE_SYSTEM)
2378 prefix = EXTATTR_NAMESPACE_SYSTEM_STRING;
2380 prefix = EXTATTR_NAMESPACE_USER_STRING;
2382 len = strlen(prefix) + sizeof(extattr_namespace_separator) +
2383 strlen(ap->a_name) + 1;
2385 fdisp_init(&fdi, len);
2386 fdisp_make_vp(&fdi, FUSE_REMOVEXATTR, vp, td, cred);
2388 attr_str = fdi.indata;
2389 snprintf(attr_str, len, "%s%c%s", prefix, extattr_namespace_separator,
2392 err = fdisp_wait_answ(&fdi);
2393 if (err == ENOSYS) {
2394 fsess_set_notimpl(mp, FUSE_REMOVEXATTR);
2398 fdisp_destroy(&fdi);
2403 struct vnop_print_args {
2408 fuse_vnop_print(struct vop_print_args *ap)
2410 struct fuse_vnode_data *fvdat = VTOFUD(ap->a_vp);
2412 printf("nodeid: %ju, parent nodeid: %ju, nlookup: %ju, flag: %#x\n",
2413 (uintmax_t)VTOILLU(ap->a_vp), (uintmax_t)fvdat->parent_nid,
2414 (uintmax_t)fvdat->nlookup,