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_allocate = VOP_EINVAL,
172 .vop_default = &default_vnodeops,
173 .vop_access = fuse_vnop_access,
174 .vop_advlock = fuse_vnop_advlock,
175 .vop_close = fuse_vnop_close,
176 .vop_create = fuse_vnop_create,
177 .vop_deleteextattr = fuse_vnop_deleteextattr,
178 .vop_fsync = fuse_vnop_fsync,
179 .vop_fdatasync = fuse_vnop_fdatasync,
180 .vop_getattr = fuse_vnop_getattr,
181 .vop_getextattr = fuse_vnop_getextattr,
182 .vop_inactive = fuse_vnop_inactive,
183 .vop_link = fuse_vnop_link,
184 .vop_listextattr = fuse_vnop_listextattr,
185 .vop_lookup = fuse_vnop_lookup,
186 .vop_mkdir = fuse_vnop_mkdir,
187 .vop_mknod = fuse_vnop_mknod,
188 .vop_open = fuse_vnop_open,
189 .vop_pathconf = fuse_vnop_pathconf,
190 .vop_read = fuse_vnop_read,
191 .vop_readdir = fuse_vnop_readdir,
192 .vop_readlink = fuse_vnop_readlink,
193 .vop_reclaim = fuse_vnop_reclaim,
194 .vop_remove = fuse_vnop_remove,
195 .vop_rename = fuse_vnop_rename,
196 .vop_rmdir = fuse_vnop_rmdir,
197 .vop_setattr = fuse_vnop_setattr,
198 .vop_setextattr = fuse_vnop_setextattr,
199 .vop_strategy = fuse_vnop_strategy,
200 .vop_symlink = fuse_vnop_symlink,
201 .vop_write = fuse_vnop_write,
202 .vop_getpages = fuse_vnop_getpages,
203 .vop_putpages = fuse_vnop_putpages,
204 .vop_print = fuse_vnop_print,
207 static u_long fuse_lookup_cache_hits = 0;
209 SYSCTL_ULONG(_vfs_fusefs, OID_AUTO, lookup_cache_hits, CTLFLAG_RD,
210 &fuse_lookup_cache_hits, 0, "number of positive cache hits in lookup");
212 static u_long fuse_lookup_cache_misses = 0;
214 SYSCTL_ULONG(_vfs_fusefs, OID_AUTO, lookup_cache_misses, CTLFLAG_RD,
215 &fuse_lookup_cache_misses, 0, "number of cache misses in lookup");
218 * XXX: This feature is highly experimental and can bring to instabilities,
219 * needs revisiting before to be enabled by default.
221 static int fuse_reclaim_revoked = 0;
223 SYSCTL_INT(_vfs_fusefs, OID_AUTO, reclaim_revoked, CTLFLAG_RW,
224 &fuse_reclaim_revoked, 0, "");
226 uma_zone_t fuse_pbuf_zone;
228 #define fuse_vm_page_lock(m) vm_page_lock((m));
229 #define fuse_vm_page_unlock(m) vm_page_unlock((m));
230 #define fuse_vm_page_lock_queues() ((void)0)
231 #define fuse_vm_page_unlock_queues() ((void)0)
233 /* Check permission for extattr operations, much like extattr_check_cred */
235 fuse_extattr_check_cred(struct vnode *vp, int ns, struct ucred *cred,
236 struct thread *td, accmode_t accmode)
238 struct mount *mp = vnode_mount(vp);
239 struct fuse_data *data = fuse_get_mpdata(mp);
242 * Kernel-invoked always succeeds.
248 * Do not allow privileged processes in jail to directly manipulate
252 case EXTATTR_NAMESPACE_SYSTEM:
253 if (data->dataflags & FSESS_DEFAULT_PERMISSIONS) {
254 return (priv_check_cred(cred, PRIV_VFS_EXTATTR_SYSTEM));
257 case EXTATTR_NAMESPACE_USER:
258 return (fuse_internal_access(vp, accmode, td, cred));
264 /* Get a filehandle for a directory */
266 fuse_filehandle_get_dir(struct vnode *vp, struct fuse_filehandle **fufhp,
267 struct ucred *cred, pid_t pid)
269 if (fuse_filehandle_get(vp, FREAD, fufhp, cred, pid) == 0)
271 return fuse_filehandle_get(vp, FEXEC, fufhp, cred, pid);
274 /* Send FUSE_FLUSH for this vnode */
276 fuse_flush(struct vnode *vp, struct ucred *cred, pid_t pid, int fflag)
278 struct fuse_flush_in *ffi;
279 struct fuse_filehandle *fufh;
280 struct fuse_dispatcher fdi;
281 struct thread *td = curthread;
282 struct mount *mp = vnode_mount(vp);
285 if (!fsess_isimpl(vnode_mount(vp), FUSE_FLUSH))
288 err = fuse_filehandle_getrw(vp, fflag, &fufh, cred, pid);
292 fdisp_init(&fdi, sizeof(*ffi));
293 fdisp_make_vp(&fdi, FUSE_FLUSH, vp, td, cred);
295 ffi->fh = fufh->fh_id;
297 * If the file has a POSIX lock then we're supposed to set lock_owner.
298 * If not, then lock_owner is undefined. So we may as well always set
301 ffi->lock_owner = td->td_proc->p_pid;
303 err = fdisp_wait_answ(&fdi);
305 fsess_set_notimpl(mp, FUSE_FLUSH);
312 /* Close wrapper for fifos. */
314 fuse_fifo_close(struct vop_close_args *ap)
316 return (fifo_specops.vop_close(ap));
320 struct vnop_access_args {
322 #if VOP_ACCESS_TAKES_ACCMODE_T
327 struct ucred *a_cred;
332 fuse_vnop_access(struct vop_access_args *ap)
334 struct vnode *vp = ap->a_vp;
335 int accmode = ap->a_accmode;
336 struct ucred *cred = ap->a_cred;
338 struct fuse_data *data = fuse_get_mpdata(vnode_mount(vp));
342 if (fuse_isdeadfs(vp)) {
343 if (vnode_isvroot(vp)) {
348 if (!(data->dataflags & FSESS_INITED)) {
349 if (vnode_isvroot(vp)) {
350 if (priv_check_cred(cred, PRIV_VFS_ADMIN) ||
351 (fuse_match_cred(data->daemoncred, cred) == 0)) {
357 if (vnode_islnk(vp)) {
361 err = fuse_internal_access(vp, accmode, ap->a_td, ap->a_cred);
366 * struct vop_advlock_args {
367 * struct vop_generic_args a_gen;
368 * struct vnode *a_vp;
371 * struct flock *a_fl;
376 fuse_vnop_advlock(struct vop_advlock_args *ap)
378 struct vnode *vp = ap->a_vp;
379 struct flock *fl = ap->a_fl;
380 struct thread *td = curthread;
381 struct ucred *cred = td->td_ucred;
382 pid_t pid = td->td_proc->p_pid;
383 struct fuse_filehandle *fufh;
384 struct fuse_dispatcher fdi;
385 struct fuse_lk_in *fli;
386 struct fuse_lk_out *flo;
390 dataflags = fuse_get_mpdata(vnode_mount(vp))->dataflags;
392 if (fuse_isdeadfs(vp)) {
396 if (!(dataflags & FSESS_POSIX_LOCKS))
397 return vop_stdadvlock(ap);
399 err = fuse_filehandle_get_anyflags(vp, &fufh, cred, pid);
403 fdisp_init(&fdi, sizeof(*fli));
419 fdisp_make_vp(&fdi, op, vp, td, cred);
421 fli->fh = fufh->fh_id;
422 fli->owner = fl->l_pid;
423 fli->lk.start = fl->l_start;
425 fli->lk.end = fl->l_start + fl->l_len - 1;
427 fli->lk.end = INT64_MAX;
428 fli->lk.type = fl->l_type;
429 fli->lk.pid = fl->l_pid;
431 err = fdisp_wait_answ(&fdi);
434 if (err == 0 && op == FUSE_GETLK) {
436 fl->l_type = flo->lk.type;
437 fl->l_pid = flo->lk.pid;
438 if (flo->lk.type != F_UNLCK) {
439 fl->l_start = flo->lk.start;
440 if (flo->lk.end == INT64_MAX)
443 fl->l_len = flo->lk.end - flo->lk.start + 1;
444 fl->l_start = flo->lk.start;
452 struct vop_close_args {
455 struct ucred *a_cred;
460 fuse_vnop_close(struct vop_close_args *ap)
462 struct vnode *vp = ap->a_vp;
463 struct ucred *cred = ap->a_cred;
464 int fflag = ap->a_fflag;
465 struct thread *td = ap->a_td;
466 pid_t pid = td->td_proc->p_pid;
469 if (fuse_isdeadfs(vp))
473 if (fflag & IO_NDELAY)
476 err = fuse_flush(vp, cred, pid, fflag);
477 /* TODO: close the file handle, if we're sure it's no longer used */
478 if ((VTOFUD(vp)->flag & FN_SIZECHANGE) != 0) {
479 fuse_vnode_savesize(vp, cred, td->td_proc->p_pid);
485 fdisp_make_mknod_for_fallback(
486 struct fuse_dispatcher *fdip,
487 struct componentname *cnp,
493 enum fuse_opcode *op)
495 struct fuse_mknod_in *fmni;
497 fdisp_init(fdip, sizeof(*fmni) + cnp->cn_namelen + 1);
499 fdisp_make(fdip, *op, vnode_mount(dvp), parentnid, td, cred);
503 memcpy((char *)fdip->indata + sizeof(*fmni), cnp->cn_nameptr,
505 ((char *)fdip->indata)[sizeof(*fmni) + cnp->cn_namelen] = '\0';
508 struct vnop_create_args {
510 struct vnode **a_vpp;
511 struct componentname *a_cnp;
516 fuse_vnop_create(struct vop_create_args *ap)
518 struct vnode *dvp = ap->a_dvp;
519 struct vnode **vpp = ap->a_vpp;
520 struct componentname *cnp = ap->a_cnp;
521 struct vattr *vap = ap->a_vap;
522 struct thread *td = cnp->cn_thread;
523 struct ucred *cred = cnp->cn_cred;
525 struct fuse_open_in *foi;
526 struct fuse_entry_out *feo;
527 struct fuse_open_out *foo;
528 struct fuse_dispatcher fdi, fdi2;
529 struct fuse_dispatcher *fdip = &fdi;
530 struct fuse_dispatcher *fdip2 = NULL;
534 struct mount *mp = vnode_mount(dvp);
535 uint64_t parentnid = VTOFUD(dvp)->nid;
536 mode_t mode = MAKEIMODE(vap->va_type, vap->va_mode);
540 if (fuse_isdeadfs(dvp))
543 /* FUSE expects sockets to be created with FUSE_MKNOD */
544 if (vap->va_type == VSOCK)
545 return fuse_internal_mknod(dvp, vpp, cnp, vap);
548 * VOP_CREATE doesn't tell us the open(2) flags, so we guess. Only a
549 * writable mode makes sense, and we might as well include readability
554 bzero(&fdi, sizeof(fdi));
556 if (vap->va_type != VREG)
559 if (!fsess_isimpl(mp, FUSE_CREATE) || vap->va_type == VSOCK) {
560 /* Fallback to FUSE_MKNOD/FUSE_OPEN */
561 fdisp_make_mknod_for_fallback(fdip, cnp, dvp, parentnid, td,
564 /* Use FUSE_CREATE */
566 fdisp_init(fdip, sizeof(*foi) + cnp->cn_namelen + 1);
567 fdisp_make(fdip, op, vnode_mount(dvp), parentnid, td, cred);
570 foi->flags = O_CREAT | flags;
571 memcpy((char *)fdip->indata + sizeof(*foi), cnp->cn_nameptr,
573 ((char *)fdip->indata)[sizeof(*foi) + cnp->cn_namelen] = '\0';
576 err = fdisp_wait_answ(fdip);
579 if (err == ENOSYS && op == FUSE_CREATE) {
580 fsess_set_notimpl(mp, FUSE_CREATE);
581 fdisp_make_mknod_for_fallback(fdip, cnp, dvp,
582 parentnid, td, cred, mode, &op);
583 err = fdisp_wait_answ(fdip);
591 if ((err = fuse_internal_checkentry(feo, vap->va_type))) {
595 if (op == FUSE_CREATE) {
596 foo = (struct fuse_open_out*)(feo + 1);
598 /* Issue a separate FUSE_OPEN */
600 fdisp_init(fdip2, sizeof(*foi));
601 fdisp_make(fdip2, FUSE_OPEN, vnode_mount(dvp), feo->nodeid, td,
606 err = fdisp_wait_answ(fdip2);
611 err = fuse_vnode_get(mp, feo, feo->nodeid, dvp, vpp, cnp, vap->va_type);
613 struct fuse_release_in *fri;
614 uint64_t nodeid = feo->nodeid;
615 uint64_t fh_id = foo->fh;
617 fdisp_init(fdip, sizeof(*fri));
618 fdisp_make(fdip, FUSE_RELEASE, mp, nodeid, td, cred);
622 fuse_insert_callback(fdip->tick, fuse_internal_forget_callback);
623 fuse_insert_message(fdip->tick, false);
626 ASSERT_VOP_ELOCKED(*vpp, "fuse_vnop_create");
627 fuse_internal_cache_attrs(*vpp, cred, &feo->attr, feo->attr_valid,
628 feo->attr_valid_nsec, NULL);
630 fuse_filehandle_init(*vpp, FUFH_RDWR, NULL, td, cred, foo);
631 fuse_vnode_open(*vpp, foo->open_flags, td);
633 * Purge the parent's attribute cache because the daemon should've
634 * updated its mtime and ctime
636 fuse_vnode_clear_attr_cache(dvp);
637 cache_purge_negative(dvp);
641 fdisp_destroy(fdip2);
647 struct vnop_fdatasync_args {
648 struct vop_generic_args a_gen;
650 struct thread * a_td;
654 fuse_vnop_fdatasync(struct vop_fdatasync_args *ap)
656 struct vnode *vp = ap->a_vp;
657 struct thread *td = ap->a_td;
658 int waitfor = MNT_WAIT;
662 if (fuse_isdeadfs(vp)) {
665 if ((err = vop_stdfdatasync_buf(ap)))
668 return fuse_internal_fsync(vp, td, waitfor, true);
672 struct vnop_fsync_args {
673 struct vop_generic_args a_gen;
676 struct thread * a_td;
680 fuse_vnop_fsync(struct vop_fsync_args *ap)
682 struct vnode *vp = ap->a_vp;
683 struct thread *td = ap->a_td;
684 int waitfor = ap->a_waitfor;
687 if (fuse_isdeadfs(vp)) {
690 if ((err = vop_stdfsync(ap)))
693 return fuse_internal_fsync(vp, td, waitfor, false);
697 struct vnop_getattr_args {
700 struct ucred *a_cred;
705 fuse_vnop_getattr(struct vop_getattr_args *ap)
707 struct vnode *vp = ap->a_vp;
708 struct vattr *vap = ap->a_vap;
709 struct ucred *cred = ap->a_cred;
710 struct thread *td = curthread;
715 dataflags = fuse_get_mpdata(vnode_mount(vp))->dataflags;
717 /* Note that we are not bailing out on a dead file system just yet. */
719 if (!(dataflags & FSESS_INITED)) {
720 if (!vnode_isvroot(vp)) {
721 fdata_set_dead(fuse_get_mpdata(vnode_mount(vp)));
728 err = fuse_internal_getattr(vp, vap, cred, td);
729 if (err == ENOTCONN && vnode_isvroot(vp)) {
730 /* see comment in fuse_vfsop_statfs() */
737 bzero(vap, sizeof(*vap));
738 vap->va_type = vnode_vtype(vp);
744 struct vnop_inactive_args {
750 fuse_vnop_inactive(struct vop_inactive_args *ap)
752 struct vnode *vp = ap->a_vp;
753 struct thread *td = ap->a_td;
755 struct fuse_vnode_data *fvdat = VTOFUD(vp);
756 struct fuse_filehandle *fufh, *fufh_tmp;
760 LIST_FOREACH_SAFE(fufh, &fvdat->handles, next, fufh_tmp) {
761 if (need_flush && vp->v_type == VREG) {
762 if ((VTOFUD(vp)->flag & FN_SIZECHANGE) != 0) {
763 fuse_vnode_savesize(vp, NULL, 0);
765 if ((fvdat->flag & FN_REVOKED) != 0)
766 fuse_io_invalbuf(vp, td);
768 fuse_io_flushbuf(vp, MNT_WAIT, td);
771 fuse_filehandle_close(vp, fufh, td, NULL);
774 if ((fvdat->flag & FN_REVOKED) != 0 && fuse_reclaim_revoked) {
781 struct vnop_link_args {
782 struct vnode *a_tdvp;
784 struct componentname *a_cnp;
788 fuse_vnop_link(struct vop_link_args *ap)
790 struct vnode *vp = ap->a_vp;
791 struct vnode *tdvp = ap->a_tdvp;
792 struct componentname *cnp = ap->a_cnp;
793 struct ucred *cred = cnp->cn_cred;
795 struct vattr *vap = VTOVA(vp);
797 struct fuse_dispatcher fdi;
798 struct fuse_entry_out *feo;
799 struct fuse_link_in fli;
803 if (fuse_isdeadfs(vp)) {
806 if (vnode_mount(tdvp) != vnode_mount(vp)) {
811 * This is a seatbelt check to protect naive userspace filesystems from
812 * themselves and the limitations of the FUSE IPC protocol. If a
813 * filesystem does not allow attribute caching, assume it is capable of
814 * validating that nlink does not overflow.
816 if (vap != NULL && vap->va_nlink >= FUSE_LINK_MAX)
818 fli.oldnodeid = VTOI(vp);
821 fuse_internal_newentry_makerequest(vnode_mount(tdvp), VTOI(tdvp), cnp,
822 FUSE_LINK, &fli, sizeof(fli), &fdi);
823 if ((err = fdisp_wait_answ(&fdi))) {
828 err = fuse_internal_checkentry(feo, vnode_vtype(vp));
831 * Purge the parent's attribute cache because the daemon
832 * should've updated its mtime and ctime
834 fuse_vnode_clear_attr_cache(tdvp);
835 fuse_internal_cache_attrs(vp, cred, &feo->attr, feo->attr_valid,
836 feo->attr_valid_nsec, NULL);
843 struct fuse_lookup_alloc_arg {
844 struct fuse_entry_out *feo;
845 struct componentname *cnp;
850 /* Callback for vn_get_ino */
852 fuse_lookup_alloc(struct mount *mp, void *arg, int lkflags, struct vnode **vpp)
854 struct fuse_lookup_alloc_arg *flaa = arg;
856 return fuse_vnode_get(mp, flaa->feo, flaa->nid, NULL, vpp, flaa->cnp,
860 SDT_PROBE_DEFINE3(fusefs, , vnops, cache_lookup,
861 "int", "struct timespec*", "struct timespec*");
863 struct vnop_lookup_args {
864 struct vnodeop_desc *a_desc;
866 struct vnode **a_vpp;
867 struct componentname *a_cnp;
871 fuse_vnop_lookup(struct vop_lookup_args *ap)
873 struct vnode *dvp = ap->a_dvp;
874 struct vnode **vpp = ap->a_vpp;
875 struct componentname *cnp = ap->a_cnp;
876 struct thread *td = cnp->cn_thread;
877 struct ucred *cred = cnp->cn_cred;
879 int nameiop = cnp->cn_nameiop;
880 int flags = cnp->cn_flags;
881 int wantparent = flags & (LOCKPARENT | WANTPARENT);
882 int islastcn = flags & ISLASTCN;
883 struct mount *mp = vnode_mount(dvp);
887 struct vnode *vp = NULL;
889 struct fuse_dispatcher fdi;
890 bool did_lookup = false;
891 struct fuse_entry_out *feo = NULL;
892 enum vtype vtyp; /* vnode type of target */
893 off_t filesize; /* filesize of target */
897 if (fuse_isdeadfs(dvp)) {
901 if (!vnode_isdir(dvp))
904 if (islastcn && vfs_isrdonly(mp) && (nameiop != LOOKUP))
907 if ((err = fuse_internal_access(dvp, VEXEC, td, cred)))
910 if (flags & ISDOTDOT) {
911 nid = VTOFUD(dvp)->parent_nid;
914 /* .. is obviously a directory */
917 } else if (cnp->cn_namelen == 1 && *(cnp->cn_nameptr) == '.') {
919 /* . is obviously a directory */
923 struct timespec now, timeout;
925 err = cache_lookup(dvp, vpp, cnp, &timeout, NULL);
927 SDT_PROBE3(fusefs, , vnops, cache_lookup, err, &timeout, &now);
929 case -1: /* positive match */
930 if (timespeccmp(&timeout, &now, >)) {
931 atomic_add_acq_long(&fuse_lookup_cache_hits, 1);
934 atomic_add_acq_long(&fuse_lookup_cache_misses,
946 case 0: /* no match in cache */
947 atomic_add_acq_long(&fuse_lookup_cache_misses, 1);
950 case ENOENT: /* negative match */
952 if (timespeccmp(&timeout, &now, <=)) {
954 cache_purge_negative(dvp);
963 fdisp_init(&fdi, cnp->cn_namelen + 1);
964 fdisp_make(&fdi, FUSE_LOOKUP, mp, nid, td, cred);
966 memcpy(fdi.indata, cnp->cn_nameptr, cnp->cn_namelen);
967 ((char *)fdi.indata)[cnp->cn_namelen] = '\0';
968 lookup_err = fdisp_wait_answ(&fdi);
972 /* lookup call succeeded */
973 nid = ((struct fuse_entry_out *)fdi.answ)->nodeid;
974 feo = (struct fuse_entry_out *)fdi.answ;
976 /* zero nodeid means ENOENT and cache it */
977 struct timespec timeout;
979 fdi.answ_stat = ENOENT;
981 if (cnp->cn_flags & MAKEENTRY) {
982 fuse_validity_2_timespec(feo, &timeout);
983 cache_enter_time(dvp, *vpp, cnp,
986 } else if (nid == FUSE_ROOT_ID) {
989 vtyp = IFTOVT(feo->attr.mode);
990 filesize = feo->attr.size;
992 if (lookup_err && (!fdi.answ_stat || lookup_err != ENOENT)) {
997 /* lookup_err, if non-zero, must be ENOENT at this point */
1000 /* Entry not found */
1001 if ((nameiop == CREATE || nameiop == RENAME) && islastcn) {
1002 err = fuse_internal_access(dvp, VWRITE, td, cred);
1005 * Set the SAVENAME flag to hold onto the
1006 * pathname for use later in VOP_CREATE or
1009 cnp->cn_flags |= SAVENAME;
1017 /* Entry was found */
1018 if (flags & ISDOTDOT) {
1019 struct fuse_lookup_alloc_arg flaa;
1025 err = vn_vget_ino_gen(dvp, fuse_lookup_alloc, &flaa, 0,
1028 } else if (nid == VTOI(dvp)) {
1032 struct fuse_vnode_data *fvdat;
1034 err = fuse_vnode_get(vnode_mount(dvp), feo, nid, dvp,
1041 * In the case where we are looking up a FUSE node
1042 * represented by an existing cached vnode, and the
1043 * true size reported by FUSE_LOOKUP doesn't match
1044 * the vnode's cached size, then any cached writes
1045 * beyond the file's current size are lost.
1048 * * following attribute cache expiration, or
1049 * * due a bug in the daemon, or
1052 if (vnode_isreg(vp) &&
1053 filesize != fvdat->cached_attrs.va_size &&
1054 fvdat->flag & FN_SIZECHANGE) {
1056 * The FN_SIZECHANGE flag reflects a dirty
1057 * append. If userspace lets us know our cache
1058 * is invalid, that write was lost. (Dirty
1059 * writes that do not cause append are also
1060 * lost, but we don't detect them here.)
1062 * XXX: Maybe disable WB caching on this mount.
1064 printf("%s: WB cache incoherent on %s!\n",
1066 vnode_mount(vp)->mnt_stat.f_mntonname);
1068 fvdat->flag &= ~FN_SIZECHANGE;
1072 fuse_internal_cache_attrs(*vpp, cred, &feo->attr,
1073 feo->attr_valid, feo->attr_valid_nsec, NULL);
1075 if ((nameiop == DELETE || nameiop == RENAME) &&
1078 struct vattr dvattr;
1080 err = fuse_internal_access(dvp, VWRITE, td,
1085 * if the parent's sticky bit is set, check
1086 * whether we're allowed to remove the file.
1087 * Need to figure out the vnode locking to make
1090 fuse_internal_getattr(dvp, &dvattr, cred, td);
1091 if ((dvattr.va_mode & S_ISTXT) &&
1092 fuse_internal_access(dvp, VADMIN, td,
1094 fuse_internal_access(*vpp, VADMIN, td,
1102 (nameiop == DELETE) ||
1103 (nameiop == RENAME && wantparent))) {
1104 cnp->cn_flags |= SAVENAME;
1111 if (vp != NULL && dvp != vp)
1113 else if (vp != NULL)
1118 fdisp_destroy(&fdi);
1124 struct vnop_mkdir_args {
1125 struct vnode *a_dvp;
1126 struct vnode **a_vpp;
1127 struct componentname *a_cnp;
1128 struct vattr *a_vap;
1132 fuse_vnop_mkdir(struct vop_mkdir_args *ap)
1134 struct vnode *dvp = ap->a_dvp;
1135 struct vnode **vpp = ap->a_vpp;
1136 struct componentname *cnp = ap->a_cnp;
1137 struct vattr *vap = ap->a_vap;
1139 struct fuse_mkdir_in fmdi;
1141 if (fuse_isdeadfs(dvp)) {
1144 fmdi.mode = MAKEIMODE(vap->va_type, vap->va_mode);
1146 return (fuse_internal_newentry(dvp, vpp, cnp, FUSE_MKDIR, &fmdi,
1147 sizeof(fmdi), VDIR));
1151 struct vnop_mknod_args {
1152 struct vnode *a_dvp;
1153 struct vnode **a_vpp;
1154 struct componentname *a_cnp;
1155 struct vattr *a_vap;
1159 fuse_vnop_mknod(struct vop_mknod_args *ap)
1162 struct vnode *dvp = ap->a_dvp;
1163 struct vnode **vpp = ap->a_vpp;
1164 struct componentname *cnp = ap->a_cnp;
1165 struct vattr *vap = ap->a_vap;
1167 if (fuse_isdeadfs(dvp))
1170 return fuse_internal_mknod(dvp, vpp, cnp, vap);
1174 struct vnop_open_args {
1177 struct ucred *a_cred;
1178 struct thread *a_td;
1179 int a_fdidx; / struct file *a_fp;
1183 fuse_vnop_open(struct vop_open_args *ap)
1185 struct vnode *vp = ap->a_vp;
1186 int a_mode = ap->a_mode;
1187 struct thread *td = ap->a_td;
1188 struct ucred *cred = ap->a_cred;
1189 pid_t pid = td->td_proc->p_pid;
1190 struct fuse_vnode_data *fvdat;
1192 if (fuse_isdeadfs(vp))
1194 if (vp->v_type == VCHR || vp->v_type == VBLK || vp->v_type == VFIFO)
1195 return (EOPNOTSUPP);
1196 if ((a_mode & (FREAD | FWRITE | FEXEC)) == 0)
1201 if (fuse_filehandle_validrw(vp, a_mode, cred, pid)) {
1202 fuse_vnode_open(vp, 0, td);
1206 return fuse_filehandle_open(vp, a_mode, NULL, td, cred);
1210 fuse_vnop_pathconf(struct vop_pathconf_args *ap)
1213 switch (ap->a_name) {
1214 case _PC_FILESIZEBITS:
1218 *ap->a_retval = NAME_MAX;
1221 *ap->a_retval = MIN(LONG_MAX, FUSE_LINK_MAX);
1223 case _PC_SYMLINK_MAX:
1224 *ap->a_retval = MAXPATHLEN;
1230 return (vop_stdpathconf(ap));
1235 struct vnop_read_args {
1239 struct ucred *a_cred;
1243 fuse_vnop_read(struct vop_read_args *ap)
1245 struct vnode *vp = ap->a_vp;
1246 struct uio *uio = ap->a_uio;
1247 int ioflag = ap->a_ioflag;
1248 struct ucred *cred = ap->a_cred;
1249 pid_t pid = curthread->td_proc->p_pid;
1251 if (fuse_isdeadfs(vp)) {
1255 if (VTOFUD(vp)->flag & FN_DIRECTIO) {
1256 ioflag |= IO_DIRECT;
1259 return fuse_io_dispatch(vp, uio, ioflag, false, cred, pid);
1263 struct vnop_readdir_args {
1266 struct ucred *a_cred;
1273 fuse_vnop_readdir(struct vop_readdir_args *ap)
1275 struct vnode *vp = ap->a_vp;
1276 struct uio *uio = ap->a_uio;
1277 struct ucred *cred = ap->a_cred;
1278 struct fuse_filehandle *fufh = NULL;
1279 struct fuse_iov cookediov;
1281 pid_t pid = curthread->td_proc->p_pid;
1283 if (fuse_isdeadfs(vp)) {
1286 if ( /* XXXIP ((uio_iovcnt(uio) > 1)) || */
1287 (uio_resid(uio) < sizeof(struct dirent))) {
1291 err = fuse_filehandle_get_dir(vp, &fufh, cred, pid);
1294 #define DIRCOOKEDSIZE FUSE_DIRENT_ALIGN(FUSE_NAME_OFFSET + MAXNAMLEN + 1)
1295 fiov_init(&cookediov, DIRCOOKEDSIZE);
1297 err = fuse_internal_readdir(vp, uio, fufh, &cookediov);
1299 fiov_teardown(&cookediov);
1305 struct vnop_readlink_args {
1308 struct ucred *a_cred;
1312 fuse_vnop_readlink(struct vop_readlink_args *ap)
1314 struct vnode *vp = ap->a_vp;
1315 struct uio *uio = ap->a_uio;
1316 struct ucred *cred = ap->a_cred;
1318 struct fuse_dispatcher fdi;
1321 if (fuse_isdeadfs(vp)) {
1324 if (!vnode_islnk(vp)) {
1327 fdisp_init(&fdi, 0);
1328 err = fdisp_simple_putget_vp(&fdi, FUSE_READLINK, vp, curthread, cred);
1332 if (((char *)fdi.answ)[0] == '/' &&
1333 fuse_get_mpdata(vnode_mount(vp))->dataflags & FSESS_PUSH_SYMLINKS_IN) {
1334 char *mpth = vnode_mount(vp)->mnt_stat.f_mntonname;
1336 err = uiomove(mpth, strlen(mpth), uio);
1339 err = uiomove(fdi.answ, fdi.iosize, uio);
1342 fdisp_destroy(&fdi);
1347 struct vnop_reclaim_args {
1349 struct thread *a_td;
1353 fuse_vnop_reclaim(struct vop_reclaim_args *ap)
1355 struct vnode *vp = ap->a_vp;
1356 struct thread *td = ap->a_td;
1357 struct fuse_vnode_data *fvdat = VTOFUD(vp);
1358 struct fuse_filehandle *fufh, *fufh_tmp;
1361 panic("FUSE: no vnode data during recycling");
1363 LIST_FOREACH_SAFE(fufh, &fvdat->handles, next, fufh_tmp) {
1364 printf("FUSE: vnode being reclaimed with open fufh "
1365 "(type=%#x)", fufh->fufh_type);
1366 fuse_filehandle_close(vp, fufh, td, NULL);
1369 if ((!fuse_isdeadfs(vp)) && (fvdat->nlookup)) {
1370 fuse_internal_forget_send(vnode_mount(vp), td, NULL, VTOI(vp),
1373 fuse_vnode_setparent(vp, NULL);
1375 vfs_hash_remove(vp);
1376 vnode_destroy_vobject(vp);
1377 fuse_vnode_destroy(vp);
1383 struct vnop_remove_args {
1384 struct vnode *a_dvp;
1386 struct componentname *a_cnp;
1390 fuse_vnop_remove(struct vop_remove_args *ap)
1392 struct vnode *dvp = ap->a_dvp;
1393 struct vnode *vp = ap->a_vp;
1394 struct componentname *cnp = ap->a_cnp;
1398 if (fuse_isdeadfs(vp)) {
1401 if (vnode_isdir(vp)) {
1406 err = fuse_internal_remove(dvp, vp, cnp, FUSE_UNLINK);
1409 fuse_internal_vnode_disappear(vp);
1411 * Purge the parent's attribute cache because the daemon
1412 * should've updated its mtime and ctime
1414 fuse_vnode_clear_attr_cache(dvp);
1420 struct vnop_rename_args {
1421 struct vnode *a_fdvp;
1422 struct vnode *a_fvp;
1423 struct componentname *a_fcnp;
1424 struct vnode *a_tdvp;
1425 struct vnode *a_tvp;
1426 struct componentname *a_tcnp;
1430 fuse_vnop_rename(struct vop_rename_args *ap)
1432 struct vnode *fdvp = ap->a_fdvp;
1433 struct vnode *fvp = ap->a_fvp;
1434 struct componentname *fcnp = ap->a_fcnp;
1435 struct vnode *tdvp = ap->a_tdvp;
1436 struct vnode *tvp = ap->a_tvp;
1437 struct componentname *tcnp = ap->a_tcnp;
1438 struct fuse_data *data;
1439 bool newparent = fdvp != tdvp;
1440 bool isdir = fvp->v_type == VDIR;
1443 if (fuse_isdeadfs(fdvp)) {
1446 if (fvp->v_mount != tdvp->v_mount ||
1447 (tvp && fvp->v_mount != tvp->v_mount)) {
1448 SDT_PROBE2(fusefs, , vnops, trace, 1, "cross-device rename");
1455 * FUSE library is expected to check if target directory is not
1456 * under the source directory in the file system tree.
1457 * Linux performs this check at VFS level.
1460 * If source is a directory, and it will get a new parent, user must
1461 * have write permission to it, so ".." can be modified.
1463 data = fuse_get_mpdata(vnode_mount(tdvp));
1464 if (data->dataflags & FSESS_DEFAULT_PERMISSIONS && isdir && newparent) {
1465 err = fuse_internal_access(fvp, VWRITE,
1466 tcnp->cn_thread, tcnp->cn_cred);
1470 sx_xlock(&data->rename_lock);
1471 err = fuse_internal_rename(fdvp, fcnp, tdvp, tcnp);
1474 fuse_vnode_setparent(fvp, tdvp);
1476 fuse_vnode_setparent(tvp, NULL);
1478 sx_unlock(&data->rename_lock);
1480 if (tvp != NULL && tvp != fvp) {
1483 if (vnode_isdir(fvp)) {
1484 if ((tvp != NULL) && vnode_isdir(tvp)) {
1505 struct vnop_rmdir_args {
1506 struct vnode *a_dvp;
1508 struct componentname *a_cnp;
1512 fuse_vnop_rmdir(struct vop_rmdir_args *ap)
1514 struct vnode *dvp = ap->a_dvp;
1515 struct vnode *vp = ap->a_vp;
1519 if (fuse_isdeadfs(vp)) {
1522 if (VTOFUD(vp) == VTOFUD(dvp)) {
1525 err = fuse_internal_remove(dvp, vp, ap->a_cnp, FUSE_RMDIR);
1528 fuse_internal_vnode_disappear(vp);
1530 * Purge the parent's attribute cache because the daemon
1531 * should've updated its mtime and ctime
1533 fuse_vnode_clear_attr_cache(dvp);
1539 struct vnop_setattr_args {
1541 struct vattr *a_vap;
1542 struct ucred *a_cred;
1543 struct thread *a_td;
1547 fuse_vnop_setattr(struct vop_setattr_args *ap)
1549 struct vnode *vp = ap->a_vp;
1550 struct vattr *vap = ap->a_vap;
1551 struct ucred *cred = ap->a_cred;
1552 struct thread *td = curthread;
1554 struct fuse_data *data;
1555 struct vattr old_va;
1558 accmode_t accmode = 0;
1560 bool drop_suid = false;
1563 mp = vnode_mount(vp);
1564 data = fuse_get_mpdata(mp);
1565 dataflags = data->dataflags;
1566 checkperm = dataflags & FSESS_DEFAULT_PERMISSIONS;
1567 if (cred->cr_ngroups > 0)
1568 cr_gid = cred->cr_groups[0];
1572 if (fuse_isdeadfs(vp)) {
1576 if (vap->va_uid != (uid_t)VNOVAL) {
1578 /* Only root may change a file's owner */
1579 err = priv_check_cred(cred, PRIV_VFS_CHOWN);
1581 /* As a special case, allow the null chown */
1582 err2 = fuse_internal_getattr(vp, &old_va, cred,
1586 if (vap->va_uid != old_va.va_uid)
1596 if (vap->va_gid != (gid_t)VNOVAL) {
1597 if (checkperm && priv_check_cred(cred, PRIV_VFS_CHOWN))
1599 if (checkperm && !groupmember(vap->va_gid, cred))
1602 * Non-root users may only chgrp to one of their own
1605 err = priv_check_cred(cred, PRIV_VFS_CHOWN);
1607 /* As a special case, allow the null chgrp */
1608 err2 = fuse_internal_getattr(vp, &old_va, cred,
1612 if (vap->va_gid != old_va.va_gid)
1620 if (vap->va_size != VNOVAL) {
1621 switch (vp->v_type) {
1626 if (vfs_isrdonly(mp))
1631 * According to POSIX, the result is unspecified
1632 * for file types other than regular files,
1633 * directories and shared memory objects. We
1634 * don't support shared memory objects in the file
1635 * system, and have dubious support for truncating
1636 * symlinks. Just ignore the request in other cases.
1640 /* Don't set accmode. Permission to trunc is checked upstack */
1642 if (vap->va_atime.tv_sec != VNOVAL || vap->va_mtime.tv_sec != VNOVAL) {
1643 if (vap->va_vaflags & VA_UTIMES_NULL)
1649 if (vap->va_mode != (mode_t)VNOVAL)
1650 vap->va_mode &= ~(S_ISUID | S_ISGID);
1652 err = fuse_internal_getattr(vp, &old_va, cred, td);
1655 vap->va_mode = old_va.va_mode & ~(S_ISUID | S_ISGID);
1658 if (vap->va_mode != (mode_t)VNOVAL) {
1659 /* Only root may set the sticky bit on non-directories */
1660 if (checkperm && vp->v_type != VDIR && (vap->va_mode & S_ISTXT)
1661 && priv_check_cred(cred, PRIV_VFS_STICKYFILE))
1663 if (checkperm && (vap->va_mode & S_ISGID)) {
1664 err = fuse_internal_getattr(vp, &old_va, cred, td);
1667 if (!groupmember(old_va.va_gid, cred)) {
1668 err = priv_check_cred(cred, PRIV_VFS_SETGID);
1676 if (vfs_isrdonly(mp))
1679 err = fuse_internal_access(vp, accmode, td, cred);
1683 return fuse_internal_setattr(vp, vap, td, cred);
1687 struct vnop_strategy_args {
1693 fuse_vnop_strategy(struct vop_strategy_args *ap)
1695 struct vnode *vp = ap->a_vp;
1696 struct buf *bp = ap->a_bp;
1698 if (!vp || fuse_isdeadfs(vp)) {
1699 bp->b_ioflags |= BIO_ERROR;
1700 bp->b_error = ENXIO;
1706 * VOP_STRATEGY always returns zero and signals error via bp->b_ioflags.
1707 * fuse_io_strategy sets bp's error fields
1709 (void)fuse_io_strategy(vp, bp);
1716 struct vnop_symlink_args {
1717 struct vnode *a_dvp;
1718 struct vnode **a_vpp;
1719 struct componentname *a_cnp;
1720 struct vattr *a_vap;
1725 fuse_vnop_symlink(struct vop_symlink_args *ap)
1727 struct vnode *dvp = ap->a_dvp;
1728 struct vnode **vpp = ap->a_vpp;
1729 struct componentname *cnp = ap->a_cnp;
1730 const char *target = ap->a_target;
1732 struct fuse_dispatcher fdi;
1737 if (fuse_isdeadfs(dvp)) {
1741 * Unlike the other creator type calls, here we have to create a message
1742 * where the name of the new entry comes first, and the data describing
1743 * the entry comes second.
1744 * Hence we can't rely on our handy fuse_internal_newentry() routine,
1745 * but put together the message manually and just call the core part.
1748 len = strlen(target) + 1;
1749 fdisp_init(&fdi, len + cnp->cn_namelen + 1);
1750 fdisp_make_vp(&fdi, FUSE_SYMLINK, dvp, curthread, NULL);
1752 memcpy(fdi.indata, cnp->cn_nameptr, cnp->cn_namelen);
1753 ((char *)fdi.indata)[cnp->cn_namelen] = '\0';
1754 memcpy((char *)fdi.indata + cnp->cn_namelen + 1, target, len);
1756 err = fuse_internal_newentry_core(dvp, vpp, cnp, VLNK, &fdi);
1757 fdisp_destroy(&fdi);
1762 struct vnop_write_args {
1766 struct ucred *a_cred;
1770 fuse_vnop_write(struct vop_write_args *ap)
1772 struct vnode *vp = ap->a_vp;
1773 struct uio *uio = ap->a_uio;
1774 int ioflag = ap->a_ioflag;
1775 struct ucred *cred = ap->a_cred;
1776 pid_t pid = curthread->td_proc->p_pid;
1778 if (fuse_isdeadfs(vp)) {
1782 if (VTOFUD(vp)->flag & FN_DIRECTIO) {
1783 ioflag |= IO_DIRECT;
1786 return fuse_io_dispatch(vp, uio, ioflag, false, cred, pid);
1789 SDT_PROBE_DEFINE1(fusefs, , vnops, vnop_getpages_error, "int");
1791 struct vnop_getpages_args {
1799 fuse_vnop_getpages(struct vop_getpages_args *ap)
1801 int i, error, nextoff, size, toff, count, npages;
1810 pid_t pid = curthread->td_proc->p_pid;
1813 KASSERT(vp->v_object, ("objectless vp passed to getpages"));
1814 td = curthread; /* XXX */
1815 cred = curthread->td_ucred; /* XXX */
1817 npages = ap->a_count;
1819 if (!fsess_opt_mmap(vnode_mount(vp))) {
1820 SDT_PROBE2(fusefs, , vnops, trace, 1,
1821 "called on non-cacheable vnode??\n");
1822 return (VM_PAGER_ERROR);
1826 * If the last page is partially valid, just return it and allow
1827 * the pager to zero-out the blanks. Partially valid pages can
1828 * only occur at the file EOF.
1830 * XXXGL: is that true for FUSE, which is a local filesystem,
1831 * but still somewhat disconnected from the kernel?
1833 VM_OBJECT_WLOCK(vp->v_object);
1834 if (pages[npages - 1]->valid != 0 && --npages == 0)
1836 VM_OBJECT_WUNLOCK(vp->v_object);
1839 * We use only the kva address for the buffer, but this is extremely
1840 * convenient and fast.
1842 bp = uma_zalloc(fuse_pbuf_zone, M_WAITOK);
1844 kva = (vm_offset_t)bp->b_data;
1845 pmap_qenter(kva, pages, npages);
1846 VM_CNT_INC(v_vnodein);
1847 VM_CNT_ADD(v_vnodepgsin, npages);
1849 count = npages << PAGE_SHIFT;
1850 iov.iov_base = (caddr_t)kva;
1851 iov.iov_len = count;
1854 uio.uio_offset = IDX_TO_OFF(pages[0]->pindex);
1855 uio.uio_resid = count;
1856 uio.uio_segflg = UIO_SYSSPACE;
1857 uio.uio_rw = UIO_READ;
1860 error = fuse_io_dispatch(vp, &uio, IO_DIRECT, true, cred, pid);
1861 pmap_qremove(kva, npages);
1863 uma_zfree(fuse_pbuf_zone, bp);
1865 if (error && (uio.uio_resid == count)) {
1866 SDT_PROBE1(fusefs, , vnops, vnop_getpages_error, error);
1867 return VM_PAGER_ERROR;
1870 * Calculate the number of bytes read and validate only that number
1871 * of bytes. Note that due to pending writes, size may be 0. This
1872 * does not mean that the remaining data is invalid!
1875 size = count - uio.uio_resid;
1876 VM_OBJECT_WLOCK(vp->v_object);
1877 fuse_vm_page_lock_queues();
1878 for (i = 0, toff = 0; i < npages; i++, toff = nextoff) {
1881 nextoff = toff + PAGE_SIZE;
1884 if (nextoff <= size) {
1886 * Read operation filled an entire page
1888 m->valid = VM_PAGE_BITS_ALL;
1889 KASSERT(m->dirty == 0,
1890 ("fuse_getpages: page %p is dirty", m));
1891 } else if (size > toff) {
1893 * Read operation filled a partial page.
1896 vm_page_set_valid_range(m, 0, size - toff);
1897 KASSERT(m->dirty == 0,
1898 ("fuse_getpages: page %p is dirty", m));
1901 * Read operation was short. If no error occurred
1902 * we may have hit a zero-fill section. We simply
1903 * leave valid set to 0.
1908 fuse_vm_page_unlock_queues();
1910 VM_OBJECT_WUNLOCK(vp->v_object);
1915 return (VM_PAGER_OK);
1919 struct vnop_putpages_args {
1925 vm_ooffset_t a_offset;
1929 fuse_vnop_putpages(struct vop_putpages_args *ap)
1935 int i, error, npages, count;
1943 pid_t pid = curthread->td_proc->p_pid;
1946 KASSERT(vp->v_object, ("objectless vp passed to putpages"));
1947 fsize = vp->v_object->un_pager.vnp.vnp_size;
1948 td = curthread; /* XXX */
1949 cred = curthread->td_ucred; /* XXX */
1951 count = ap->a_count;
1952 rtvals = ap->a_rtvals;
1953 npages = btoc(count);
1954 offset = IDX_TO_OFF(pages[0]->pindex);
1956 if (!fsess_opt_mmap(vnode_mount(vp))) {
1957 SDT_PROBE2(fusefs, , vnops, trace, 1,
1958 "called on non-cacheable vnode??\n");
1960 for (i = 0; i < npages; i++)
1961 rtvals[i] = VM_PAGER_AGAIN;
1964 * When putting pages, do not extend file past EOF.
1967 if (offset + count > fsize) {
1968 count = fsize - offset;
1973 * We use only the kva address for the buffer, but this is extremely
1974 * convenient and fast.
1976 bp = uma_zalloc(fuse_pbuf_zone, M_WAITOK);
1978 kva = (vm_offset_t)bp->b_data;
1979 pmap_qenter(kva, pages, npages);
1980 VM_CNT_INC(v_vnodeout);
1981 VM_CNT_ADD(v_vnodepgsout, count);
1983 iov.iov_base = (caddr_t)kva;
1984 iov.iov_len = count;
1987 uio.uio_offset = offset;
1988 uio.uio_resid = count;
1989 uio.uio_segflg = UIO_SYSSPACE;
1990 uio.uio_rw = UIO_WRITE;
1993 error = fuse_io_dispatch(vp, &uio, IO_DIRECT, true, cred, pid);
1995 pmap_qremove(kva, npages);
1996 uma_zfree(fuse_pbuf_zone, bp);
1999 int nwritten = round_page(count - uio.uio_resid) / PAGE_SIZE;
2001 for (i = 0; i < nwritten; i++) {
2002 rtvals[i] = VM_PAGER_OK;
2003 VM_OBJECT_WLOCK(pages[i]->object);
2004 vm_page_undirty(pages[i]);
2005 VM_OBJECT_WUNLOCK(pages[i]->object);
2011 static const char extattr_namespace_separator = '.';
2014 struct vop_getextattr_args {
2015 struct vop_generic_args a_gen;
2017 int a_attrnamespace;
2021 struct ucred *a_cred;
2022 struct thread *a_td;
2026 fuse_vnop_getextattr(struct vop_getextattr_args *ap)
2028 struct vnode *vp = ap->a_vp;
2029 struct uio *uio = ap->a_uio;
2030 struct fuse_dispatcher fdi;
2031 struct fuse_getxattr_in *get_xattr_in;
2032 struct fuse_getxattr_out *get_xattr_out;
2033 struct mount *mp = vnode_mount(vp);
2034 struct thread *td = ap->a_td;
2035 struct ucred *cred = ap->a_cred;
2041 if (fuse_isdeadfs(vp))
2044 if (!fsess_isimpl(mp, FUSE_GETXATTR))
2047 err = fuse_extattr_check_cred(vp, ap->a_attrnamespace, cred, td, VREAD);
2051 /* Default to looking for user attributes. */
2052 if (ap->a_attrnamespace == EXTATTR_NAMESPACE_SYSTEM)
2053 prefix = EXTATTR_NAMESPACE_SYSTEM_STRING;
2055 prefix = EXTATTR_NAMESPACE_USER_STRING;
2057 len = strlen(prefix) + sizeof(extattr_namespace_separator) +
2058 strlen(ap->a_name) + 1;
2060 fdisp_init(&fdi, len + sizeof(*get_xattr_in));
2061 fdisp_make_vp(&fdi, FUSE_GETXATTR, vp, td, cred);
2063 get_xattr_in = fdi.indata;
2065 * Check to see whether we're querying the available size or
2066 * issuing the actual request. If we pass in 0, we get back struct
2067 * fuse_getxattr_out. If we pass in a non-zero size, we get back
2068 * that much data, without the struct fuse_getxattr_out header.
2071 get_xattr_in->size = 0;
2073 get_xattr_in->size = uio->uio_resid;
2075 attr_str = (char *)fdi.indata + sizeof(*get_xattr_in);
2076 snprintf(attr_str, len, "%s%c%s", prefix, extattr_namespace_separator,
2079 err = fdisp_wait_answ(&fdi);
2081 if (err == ENOSYS) {
2082 fsess_set_notimpl(mp, FUSE_GETXATTR);
2088 get_xattr_out = fdi.answ;
2090 if (ap->a_size != NULL)
2091 *ap->a_size = get_xattr_out->size;
2094 err = uiomove(fdi.answ, fdi.iosize, uio);
2097 fdisp_destroy(&fdi);
2102 struct vop_setextattr_args {
2103 struct vop_generic_args a_gen;
2105 int a_attrnamespace;
2108 struct ucred *a_cred;
2109 struct thread *a_td;
2113 fuse_vnop_setextattr(struct vop_setextattr_args *ap)
2115 struct vnode *vp = ap->a_vp;
2116 struct uio *uio = ap->a_uio;
2117 struct fuse_dispatcher fdi;
2118 struct fuse_setxattr_in *set_xattr_in;
2119 struct mount *mp = vnode_mount(vp);
2120 struct thread *td = ap->a_td;
2121 struct ucred *cred = ap->a_cred;
2127 if (fuse_isdeadfs(vp))
2130 if (!fsess_isimpl(mp, FUSE_SETXATTR))
2133 if (vfs_isrdonly(mp))
2136 /* Deleting xattrs must use VOP_DELETEEXTATTR instead */
2137 if (ap->a_uio == NULL) {
2139 * If we got here as fallback from VOP_DELETEEXTATTR, then
2140 * return EOPNOTSUPP.
2142 if (!fsess_isimpl(mp, FUSE_REMOVEXATTR))
2143 return (EOPNOTSUPP);
2148 err = fuse_extattr_check_cred(vp, ap->a_attrnamespace, cred, td,
2153 /* Default to looking for user attributes. */
2154 if (ap->a_attrnamespace == EXTATTR_NAMESPACE_SYSTEM)
2155 prefix = EXTATTR_NAMESPACE_SYSTEM_STRING;
2157 prefix = EXTATTR_NAMESPACE_USER_STRING;
2159 len = strlen(prefix) + sizeof(extattr_namespace_separator) +
2160 strlen(ap->a_name) + 1;
2162 fdisp_init(&fdi, len + sizeof(*set_xattr_in) + uio->uio_resid);
2163 fdisp_make_vp(&fdi, FUSE_SETXATTR, vp, td, cred);
2165 set_xattr_in = fdi.indata;
2166 set_xattr_in->size = uio->uio_resid;
2168 attr_str = (char *)fdi.indata + sizeof(*set_xattr_in);
2169 snprintf(attr_str, len, "%s%c%s", prefix, extattr_namespace_separator,
2172 err = uiomove((char *)fdi.indata + sizeof(*set_xattr_in) + len,
2173 uio->uio_resid, uio);
2178 err = fdisp_wait_answ(&fdi);
2180 if (err == ENOSYS) {
2181 fsess_set_notimpl(mp, FUSE_SETXATTR);
2184 if (err == ERESTART) {
2185 /* Can't restart after calling uiomove */
2190 fdisp_destroy(&fdi);
2195 * The Linux / FUSE extended attribute list is simply a collection of
2196 * NUL-terminated strings. The FreeBSD extended attribute list is a single
2197 * byte length followed by a non-NUL terminated string. So, this allows
2198 * conversion of the Linux / FUSE format to the FreeBSD format in place.
2199 * Linux attribute names are reported with the namespace as a prefix (e.g.
2200 * "user.attribute_name"), but in FreeBSD they are reported without the
2201 * namespace prefix (e.g. "attribute_name"). So, we're going from:
2203 * user.attr_name1\0user.attr_name2\0
2207 * <num>attr_name1<num>attr_name2
2209 * Where "<num>" is a single byte number of characters in the attribute name.
2212 * prefix - exattr namespace prefix string
2213 * list, list_len - input list with namespace prefixes
2214 * bsd_list, bsd_list_len - output list compatible with bsd vfs
2217 fuse_xattrlist_convert(char *prefix, const char *list, int list_len,
2218 char *bsd_list, int *bsd_list_len)
2220 int len, pos, dist_to_next, prefix_len;
2224 prefix_len = strlen(prefix);
2226 while (pos < list_len && list[pos] != '\0') {
2227 dist_to_next = strlen(&list[pos]) + 1;
2228 if (bcmp(&list[pos], prefix, prefix_len) == 0 &&
2229 list[pos + prefix_len] == extattr_namespace_separator) {
2230 len = dist_to_next -
2231 (prefix_len + sizeof(extattr_namespace_separator)) - 1;
2232 if (len >= EXTATTR_MAXNAMELEN)
2233 return (ENAMETOOLONG);
2235 bsd_list[*bsd_list_len] = len;
2236 memcpy(&bsd_list[*bsd_list_len + 1],
2237 &list[pos + prefix_len +
2238 sizeof(extattr_namespace_separator)], len);
2240 *bsd_list_len += len + 1;
2243 pos += dist_to_next;
2250 struct vop_listextattr_args {
2251 struct vop_generic_args a_gen;
2253 int a_attrnamespace;
2256 struct ucred *a_cred;
2257 struct thread *a_td;
2261 fuse_vnop_listextattr(struct vop_listextattr_args *ap)
2263 struct vnode *vp = ap->a_vp;
2264 struct uio *uio = ap->a_uio;
2265 struct fuse_dispatcher fdi;
2266 struct fuse_listxattr_in *list_xattr_in;
2267 struct fuse_listxattr_out *list_xattr_out;
2268 struct mount *mp = vnode_mount(vp);
2269 struct thread *td = ap->a_td;
2270 struct ucred *cred = ap->a_cred;
2274 char *bsd_list = NULL;
2280 if (fuse_isdeadfs(vp))
2283 if (!fsess_isimpl(mp, FUSE_LISTXATTR))
2286 err = fuse_extattr_check_cred(vp, ap->a_attrnamespace, cred, td, VREAD);
2291 * Add space for a NUL and the period separator if enabled.
2292 * Default to looking for user attributes.
2294 if (ap->a_attrnamespace == EXTATTR_NAMESPACE_SYSTEM)
2295 prefix = EXTATTR_NAMESPACE_SYSTEM_STRING;
2297 prefix = EXTATTR_NAMESPACE_USER_STRING;
2299 len = strlen(prefix) + sizeof(extattr_namespace_separator) + 1;
2301 fdisp_init(&fdi, sizeof(*list_xattr_in) + len);
2302 fdisp_make_vp(&fdi, FUSE_LISTXATTR, vp, td, cred);
2305 * Retrieve Linux / FUSE compatible list size.
2307 list_xattr_in = fdi.indata;
2308 list_xattr_in->size = 0;
2309 attr_str = (char *)fdi.indata + sizeof(*list_xattr_in);
2310 snprintf(attr_str, len, "%s%c", prefix, extattr_namespace_separator);
2312 err = fdisp_wait_answ(&fdi);
2314 if (err == ENOSYS) {
2315 fsess_set_notimpl(mp, FUSE_LISTXATTR);
2321 list_xattr_out = fdi.answ;
2322 linux_list_len = list_xattr_out->size;
2323 if (linux_list_len == 0) {
2324 if (ap->a_size != NULL)
2325 *ap->a_size = linux_list_len;
2330 * Retrieve Linux / FUSE compatible list values.
2332 fdisp_refresh_vp(&fdi, FUSE_LISTXATTR, vp, td, cred);
2333 list_xattr_in = fdi.indata;
2334 list_xattr_in->size = linux_list_len + sizeof(*list_xattr_out);
2335 attr_str = (char *)fdi.indata + sizeof(*list_xattr_in);
2336 snprintf(attr_str, len, "%s%c", prefix, extattr_namespace_separator);
2338 err = fdisp_wait_answ(&fdi);
2342 linux_list = fdi.answ;
2343 linux_list_len = fdi.iosize;
2346 * Retrieve the BSD compatible list values.
2347 * The Linux / FUSE attribute list format isn't the same
2348 * as FreeBSD's format. So we need to transform it into
2349 * FreeBSD's format before giving it to the user.
2351 bsd_list = malloc(linux_list_len, M_TEMP, M_WAITOK);
2352 err = fuse_xattrlist_convert(prefix, linux_list, linux_list_len,
2353 bsd_list, &bsd_list_len);
2357 if (ap->a_size != NULL)
2358 *ap->a_size = bsd_list_len;
2361 err = uiomove(bsd_list, bsd_list_len, uio);
2364 free(bsd_list, M_TEMP);
2365 fdisp_destroy(&fdi);
2370 struct vop_deleteextattr_args {
2371 struct vop_generic_args a_gen;
2373 int a_attrnamespace;
2375 struct ucred *a_cred;
2376 struct thread *a_td;
2380 fuse_vnop_deleteextattr(struct vop_deleteextattr_args *ap)
2382 struct vnode *vp = ap->a_vp;
2383 struct fuse_dispatcher fdi;
2384 struct mount *mp = vnode_mount(vp);
2385 struct thread *td = ap->a_td;
2386 struct ucred *cred = ap->a_cred;
2392 if (fuse_isdeadfs(vp))
2395 if (!fsess_isimpl(mp, FUSE_REMOVEXATTR))
2398 if (vfs_isrdonly(mp))
2401 err = fuse_extattr_check_cred(vp, ap->a_attrnamespace, cred, td,
2406 /* Default to looking for user attributes. */
2407 if (ap->a_attrnamespace == EXTATTR_NAMESPACE_SYSTEM)
2408 prefix = EXTATTR_NAMESPACE_SYSTEM_STRING;
2410 prefix = EXTATTR_NAMESPACE_USER_STRING;
2412 len = strlen(prefix) + sizeof(extattr_namespace_separator) +
2413 strlen(ap->a_name) + 1;
2415 fdisp_init(&fdi, len);
2416 fdisp_make_vp(&fdi, FUSE_REMOVEXATTR, vp, td, cred);
2418 attr_str = fdi.indata;
2419 snprintf(attr_str, len, "%s%c%s", prefix, extattr_namespace_separator,
2422 err = fdisp_wait_answ(&fdi);
2423 if (err == ENOSYS) {
2424 fsess_set_notimpl(mp, FUSE_REMOVEXATTR);
2428 fdisp_destroy(&fdi);
2433 struct vnop_print_args {
2438 fuse_vnop_print(struct vop_print_args *ap)
2440 struct fuse_vnode_data *fvdat = VTOFUD(ap->a_vp);
2442 printf("nodeid: %ju, parent nodeid: %ju, nlookup: %ju, flag: %#x\n",
2443 (uintmax_t)VTOILLU(ap->a_vp), (uintmax_t)fvdat->parent_nid,
2444 (uintmax_t)fvdat->nlookup,