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;
154 static vop_vptofh_t fuse_vnop_vptofh;
156 struct vop_vector fuse_fifoops = {
157 .vop_default = &fifo_specops,
158 .vop_access = fuse_vnop_access,
159 .vop_close = fuse_fifo_close,
160 .vop_fsync = fuse_vnop_fsync,
161 .vop_getattr = fuse_vnop_getattr,
162 .vop_inactive = fuse_vnop_inactive,
163 .vop_pathconf = fuse_vnop_pathconf,
164 .vop_print = fuse_vnop_print,
165 .vop_read = VOP_PANIC,
166 .vop_reclaim = fuse_vnop_reclaim,
167 .vop_setattr = fuse_vnop_setattr,
168 .vop_write = VOP_PANIC,
169 .vop_vptofh = fuse_vnop_vptofh,
172 struct vop_vector fuse_vnops = {
173 .vop_allocate = VOP_EINVAL,
174 .vop_default = &default_vnodeops,
175 .vop_access = fuse_vnop_access,
176 .vop_advlock = fuse_vnop_advlock,
177 .vop_close = fuse_vnop_close,
178 .vop_create = fuse_vnop_create,
179 .vop_deleteextattr = fuse_vnop_deleteextattr,
180 .vop_fsync = fuse_vnop_fsync,
181 .vop_fdatasync = fuse_vnop_fdatasync,
182 .vop_getattr = fuse_vnop_getattr,
183 .vop_getextattr = fuse_vnop_getextattr,
184 .vop_inactive = fuse_vnop_inactive,
185 .vop_link = fuse_vnop_link,
186 .vop_listextattr = fuse_vnop_listextattr,
187 .vop_lookup = fuse_vnop_lookup,
188 .vop_mkdir = fuse_vnop_mkdir,
189 .vop_mknod = fuse_vnop_mknod,
190 .vop_open = fuse_vnop_open,
191 .vop_pathconf = fuse_vnop_pathconf,
192 .vop_read = fuse_vnop_read,
193 .vop_readdir = fuse_vnop_readdir,
194 .vop_readlink = fuse_vnop_readlink,
195 .vop_reclaim = fuse_vnop_reclaim,
196 .vop_remove = fuse_vnop_remove,
197 .vop_rename = fuse_vnop_rename,
198 .vop_rmdir = fuse_vnop_rmdir,
199 .vop_setattr = fuse_vnop_setattr,
200 .vop_setextattr = fuse_vnop_setextattr,
201 .vop_strategy = fuse_vnop_strategy,
202 .vop_symlink = fuse_vnop_symlink,
203 .vop_write = fuse_vnop_write,
204 .vop_getpages = fuse_vnop_getpages,
205 .vop_putpages = fuse_vnop_putpages,
206 .vop_print = fuse_vnop_print,
207 .vop_vptofh = fuse_vnop_vptofh,
210 static u_long fuse_lookup_cache_hits = 0;
212 SYSCTL_ULONG(_vfs_fusefs, OID_AUTO, lookup_cache_hits, CTLFLAG_RD,
213 &fuse_lookup_cache_hits, 0, "number of positive cache hits in lookup");
215 static u_long fuse_lookup_cache_misses = 0;
217 SYSCTL_ULONG(_vfs_fusefs, OID_AUTO, lookup_cache_misses, CTLFLAG_RD,
218 &fuse_lookup_cache_misses, 0, "number of cache misses in lookup");
221 * XXX: This feature is highly experimental and can bring to instabilities,
222 * needs revisiting before to be enabled by default.
224 static int fuse_reclaim_revoked = 0;
226 SYSCTL_INT(_vfs_fusefs, OID_AUTO, reclaim_revoked, CTLFLAG_RW,
227 &fuse_reclaim_revoked, 0, "");
229 uma_zone_t fuse_pbuf_zone;
231 #define fuse_vm_page_lock(m) vm_page_lock((m));
232 #define fuse_vm_page_unlock(m) vm_page_unlock((m));
233 #define fuse_vm_page_lock_queues() ((void)0)
234 #define fuse_vm_page_unlock_queues() ((void)0)
236 /* Check permission for extattr operations, much like extattr_check_cred */
238 fuse_extattr_check_cred(struct vnode *vp, int ns, struct ucred *cred,
239 struct thread *td, accmode_t accmode)
241 struct mount *mp = vnode_mount(vp);
242 struct fuse_data *data = fuse_get_mpdata(mp);
245 * Kernel-invoked always succeeds.
251 * Do not allow privileged processes in jail to directly manipulate
255 case EXTATTR_NAMESPACE_SYSTEM:
256 if (data->dataflags & FSESS_DEFAULT_PERMISSIONS) {
257 return (priv_check_cred(cred, PRIV_VFS_EXTATTR_SYSTEM));
260 case EXTATTR_NAMESPACE_USER:
261 return (fuse_internal_access(vp, accmode, td, cred));
267 /* Get a filehandle for a directory */
269 fuse_filehandle_get_dir(struct vnode *vp, struct fuse_filehandle **fufhp,
270 struct ucred *cred, pid_t pid)
272 if (fuse_filehandle_get(vp, FREAD, fufhp, cred, pid) == 0)
274 return fuse_filehandle_get(vp, FEXEC, fufhp, cred, pid);
277 /* Send FUSE_FLUSH for this vnode */
279 fuse_flush(struct vnode *vp, struct ucred *cred, pid_t pid, int fflag)
281 struct fuse_flush_in *ffi;
282 struct fuse_filehandle *fufh;
283 struct fuse_dispatcher fdi;
284 struct thread *td = curthread;
285 struct mount *mp = vnode_mount(vp);
288 if (!fsess_isimpl(vnode_mount(vp), FUSE_FLUSH))
291 err = fuse_filehandle_getrw(vp, fflag, &fufh, cred, pid);
295 fdisp_init(&fdi, sizeof(*ffi));
296 fdisp_make_vp(&fdi, FUSE_FLUSH, vp, td, cred);
298 ffi->fh = fufh->fh_id;
300 * If the file has a POSIX lock then we're supposed to set lock_owner.
301 * If not, then lock_owner is undefined. So we may as well always set
304 ffi->lock_owner = td->td_proc->p_pid;
306 err = fdisp_wait_answ(&fdi);
308 fsess_set_notimpl(mp, FUSE_FLUSH);
315 /* Close wrapper for fifos. */
317 fuse_fifo_close(struct vop_close_args *ap)
319 return (fifo_specops.vop_close(ap));
323 struct vnop_access_args {
325 #if VOP_ACCESS_TAKES_ACCMODE_T
330 struct ucred *a_cred;
335 fuse_vnop_access(struct vop_access_args *ap)
337 struct vnode *vp = ap->a_vp;
338 int accmode = ap->a_accmode;
339 struct ucred *cred = ap->a_cred;
341 struct fuse_data *data = fuse_get_mpdata(vnode_mount(vp));
345 if (fuse_isdeadfs(vp)) {
346 if (vnode_isvroot(vp)) {
351 if (!(data->dataflags & FSESS_INITED)) {
352 if (vnode_isvroot(vp)) {
353 if (priv_check_cred(cred, PRIV_VFS_ADMIN) ||
354 (fuse_match_cred(data->daemoncred, cred) == 0)) {
360 if (vnode_islnk(vp)) {
364 err = fuse_internal_access(vp, accmode, ap->a_td, ap->a_cred);
369 * struct vop_advlock_args {
370 * struct vop_generic_args a_gen;
371 * struct vnode *a_vp;
374 * struct flock *a_fl;
379 fuse_vnop_advlock(struct vop_advlock_args *ap)
381 struct vnode *vp = ap->a_vp;
382 struct flock *fl = ap->a_fl;
383 struct thread *td = curthread;
384 struct ucred *cred = td->td_ucred;
385 pid_t pid = td->td_proc->p_pid;
386 struct fuse_filehandle *fufh;
387 struct fuse_dispatcher fdi;
388 struct fuse_lk_in *fli;
389 struct fuse_lk_out *flo;
393 dataflags = fuse_get_mpdata(vnode_mount(vp))->dataflags;
395 if (fuse_isdeadfs(vp)) {
399 if (!(dataflags & FSESS_POSIX_LOCKS))
400 return vop_stdadvlock(ap);
402 err = fuse_filehandle_get_anyflags(vp, &fufh, cred, pid);
406 fdisp_init(&fdi, sizeof(*fli));
422 fdisp_make_vp(&fdi, op, vp, td, cred);
424 fli->fh = fufh->fh_id;
425 fli->owner = fl->l_pid;
426 fli->lk.start = fl->l_start;
428 fli->lk.end = fl->l_start + fl->l_len - 1;
430 fli->lk.end = INT64_MAX;
431 fli->lk.type = fl->l_type;
432 fli->lk.pid = fl->l_pid;
434 err = fdisp_wait_answ(&fdi);
437 if (err == 0 && op == FUSE_GETLK) {
439 fl->l_type = flo->lk.type;
440 fl->l_pid = flo->lk.pid;
441 if (flo->lk.type != F_UNLCK) {
442 fl->l_start = flo->lk.start;
443 if (flo->lk.end == INT64_MAX)
446 fl->l_len = flo->lk.end - flo->lk.start + 1;
447 fl->l_start = flo->lk.start;
455 struct vop_close_args {
458 struct ucred *a_cred;
463 fuse_vnop_close(struct vop_close_args *ap)
465 struct vnode *vp = ap->a_vp;
466 struct ucred *cred = ap->a_cred;
467 int fflag = ap->a_fflag;
468 struct thread *td = ap->a_td;
469 pid_t pid = td->td_proc->p_pid;
472 if (fuse_isdeadfs(vp))
476 if (fflag & IO_NDELAY)
479 err = fuse_flush(vp, cred, pid, fflag);
480 /* TODO: close the file handle, if we're sure it's no longer used */
481 if ((VTOFUD(vp)->flag & FN_SIZECHANGE) != 0) {
482 fuse_vnode_savesize(vp, cred, td->td_proc->p_pid);
488 fdisp_make_mknod_for_fallback(
489 struct fuse_dispatcher *fdip,
490 struct componentname *cnp,
496 enum fuse_opcode *op)
498 struct fuse_mknod_in *fmni;
500 fdisp_init(fdip, sizeof(*fmni) + cnp->cn_namelen + 1);
502 fdisp_make(fdip, *op, vnode_mount(dvp), parentnid, td, cred);
506 memcpy((char *)fdip->indata + sizeof(*fmni), cnp->cn_nameptr,
508 ((char *)fdip->indata)[sizeof(*fmni) + cnp->cn_namelen] = '\0';
511 struct vnop_create_args {
513 struct vnode **a_vpp;
514 struct componentname *a_cnp;
519 fuse_vnop_create(struct vop_create_args *ap)
521 struct vnode *dvp = ap->a_dvp;
522 struct vnode **vpp = ap->a_vpp;
523 struct componentname *cnp = ap->a_cnp;
524 struct vattr *vap = ap->a_vap;
525 struct thread *td = cnp->cn_thread;
526 struct ucred *cred = cnp->cn_cred;
528 struct fuse_open_in *foi;
529 struct fuse_entry_out *feo;
530 struct fuse_open_out *foo;
531 struct fuse_dispatcher fdi, fdi2;
532 struct fuse_dispatcher *fdip = &fdi;
533 struct fuse_dispatcher *fdip2 = NULL;
537 struct mount *mp = vnode_mount(dvp);
538 uint64_t parentnid = VTOFUD(dvp)->nid;
539 mode_t mode = MAKEIMODE(vap->va_type, vap->va_mode);
543 if (fuse_isdeadfs(dvp))
546 /* FUSE expects sockets to be created with FUSE_MKNOD */
547 if (vap->va_type == VSOCK)
548 return fuse_internal_mknod(dvp, vpp, cnp, vap);
551 * VOP_CREATE doesn't tell us the open(2) flags, so we guess. Only a
552 * writable mode makes sense, and we might as well include readability
557 bzero(&fdi, sizeof(fdi));
559 if (vap->va_type != VREG)
562 if (!fsess_isimpl(mp, FUSE_CREATE) || vap->va_type == VSOCK) {
563 /* Fallback to FUSE_MKNOD/FUSE_OPEN */
564 fdisp_make_mknod_for_fallback(fdip, cnp, dvp, parentnid, td,
567 /* Use FUSE_CREATE */
569 fdisp_init(fdip, sizeof(*foi) + cnp->cn_namelen + 1);
570 fdisp_make(fdip, op, vnode_mount(dvp), parentnid, td, cred);
573 foi->flags = O_CREAT | flags;
574 memcpy((char *)fdip->indata + sizeof(*foi), cnp->cn_nameptr,
576 ((char *)fdip->indata)[sizeof(*foi) + cnp->cn_namelen] = '\0';
579 err = fdisp_wait_answ(fdip);
582 if (err == ENOSYS && op == FUSE_CREATE) {
583 fsess_set_notimpl(mp, FUSE_CREATE);
584 fdisp_make_mknod_for_fallback(fdip, cnp, dvp,
585 parentnid, td, cred, mode, &op);
586 err = fdisp_wait_answ(fdip);
594 if ((err = fuse_internal_checkentry(feo, vap->va_type))) {
598 if (op == FUSE_CREATE) {
599 foo = (struct fuse_open_out*)(feo + 1);
601 /* Issue a separate FUSE_OPEN */
603 fdisp_init(fdip2, sizeof(*foi));
604 fdisp_make(fdip2, FUSE_OPEN, vnode_mount(dvp), feo->nodeid, td,
609 err = fdisp_wait_answ(fdip2);
614 err = fuse_vnode_get(mp, feo, feo->nodeid, dvp, vpp, cnp, vap->va_type);
616 struct fuse_release_in *fri;
617 uint64_t nodeid = feo->nodeid;
618 uint64_t fh_id = foo->fh;
620 fdisp_init(fdip, sizeof(*fri));
621 fdisp_make(fdip, FUSE_RELEASE, mp, nodeid, td, cred);
625 fuse_insert_callback(fdip->tick, fuse_internal_forget_callback);
626 fuse_insert_message(fdip->tick, false);
629 ASSERT_VOP_ELOCKED(*vpp, "fuse_vnop_create");
630 fuse_internal_cache_attrs(*vpp, cred, &feo->attr, feo->attr_valid,
631 feo->attr_valid_nsec, NULL);
633 fuse_filehandle_init(*vpp, FUFH_RDWR, NULL, td, cred, foo);
634 fuse_vnode_open(*vpp, foo->open_flags, td);
636 * Purge the parent's attribute cache because the daemon should've
637 * updated its mtime and ctime
639 fuse_vnode_clear_attr_cache(dvp);
640 cache_purge_negative(dvp);
644 fdisp_destroy(fdip2);
650 struct vnop_fdatasync_args {
651 struct vop_generic_args a_gen;
653 struct thread * a_td;
657 fuse_vnop_fdatasync(struct vop_fdatasync_args *ap)
659 struct vnode *vp = ap->a_vp;
660 struct thread *td = ap->a_td;
661 int waitfor = MNT_WAIT;
665 if (fuse_isdeadfs(vp)) {
668 if ((err = vop_stdfdatasync_buf(ap)))
671 return fuse_internal_fsync(vp, td, waitfor, true);
675 struct vnop_fsync_args {
676 struct vop_generic_args a_gen;
679 struct thread * a_td;
683 fuse_vnop_fsync(struct vop_fsync_args *ap)
685 struct vnode *vp = ap->a_vp;
686 struct thread *td = ap->a_td;
687 int waitfor = ap->a_waitfor;
690 if (fuse_isdeadfs(vp)) {
693 if ((err = vop_stdfsync(ap)))
696 return fuse_internal_fsync(vp, td, waitfor, false);
700 struct vnop_getattr_args {
703 struct ucred *a_cred;
708 fuse_vnop_getattr(struct vop_getattr_args *ap)
710 struct vnode *vp = ap->a_vp;
711 struct vattr *vap = ap->a_vap;
712 struct ucred *cred = ap->a_cred;
713 struct thread *td = curthread;
718 dataflags = fuse_get_mpdata(vnode_mount(vp))->dataflags;
720 /* Note that we are not bailing out on a dead file system just yet. */
722 if (!(dataflags & FSESS_INITED)) {
723 if (!vnode_isvroot(vp)) {
724 fdata_set_dead(fuse_get_mpdata(vnode_mount(vp)));
731 err = fuse_internal_getattr(vp, vap, cred, td);
732 if (err == ENOTCONN && vnode_isvroot(vp)) {
733 /* see comment in fuse_vfsop_statfs() */
740 bzero(vap, sizeof(*vap));
741 vap->va_type = vnode_vtype(vp);
747 struct vnop_inactive_args {
753 fuse_vnop_inactive(struct vop_inactive_args *ap)
755 struct vnode *vp = ap->a_vp;
756 struct thread *td = ap->a_td;
758 struct fuse_vnode_data *fvdat = VTOFUD(vp);
759 struct fuse_filehandle *fufh, *fufh_tmp;
763 LIST_FOREACH_SAFE(fufh, &fvdat->handles, next, fufh_tmp) {
764 if (need_flush && vp->v_type == VREG) {
765 if ((VTOFUD(vp)->flag & FN_SIZECHANGE) != 0) {
766 fuse_vnode_savesize(vp, NULL, 0);
768 if ((fvdat->flag & FN_REVOKED) != 0)
769 fuse_io_invalbuf(vp, td);
771 fuse_io_flushbuf(vp, MNT_WAIT, td);
774 fuse_filehandle_close(vp, fufh, td, NULL);
777 if ((fvdat->flag & FN_REVOKED) != 0 && fuse_reclaim_revoked) {
784 struct vnop_link_args {
785 struct vnode *a_tdvp;
787 struct componentname *a_cnp;
791 fuse_vnop_link(struct vop_link_args *ap)
793 struct vnode *vp = ap->a_vp;
794 struct vnode *tdvp = ap->a_tdvp;
795 struct componentname *cnp = ap->a_cnp;
796 struct ucred *cred = cnp->cn_cred;
798 struct vattr *vap = VTOVA(vp);
800 struct fuse_dispatcher fdi;
801 struct fuse_entry_out *feo;
802 struct fuse_link_in fli;
806 if (fuse_isdeadfs(vp)) {
809 if (vnode_mount(tdvp) != vnode_mount(vp)) {
814 * This is a seatbelt check to protect naive userspace filesystems from
815 * themselves and the limitations of the FUSE IPC protocol. If a
816 * filesystem does not allow attribute caching, assume it is capable of
817 * validating that nlink does not overflow.
819 if (vap != NULL && vap->va_nlink >= FUSE_LINK_MAX)
821 fli.oldnodeid = VTOI(vp);
824 fuse_internal_newentry_makerequest(vnode_mount(tdvp), VTOI(tdvp), cnp,
825 FUSE_LINK, &fli, sizeof(fli), &fdi);
826 if ((err = fdisp_wait_answ(&fdi))) {
831 err = fuse_internal_checkentry(feo, vnode_vtype(vp));
834 * Purge the parent's attribute cache because the daemon
835 * should've updated its mtime and ctime
837 fuse_vnode_clear_attr_cache(tdvp);
838 fuse_internal_cache_attrs(vp, cred, &feo->attr, feo->attr_valid,
839 feo->attr_valid_nsec, NULL);
846 struct fuse_lookup_alloc_arg {
847 struct fuse_entry_out *feo;
848 struct componentname *cnp;
853 /* Callback for vn_get_ino */
855 fuse_lookup_alloc(struct mount *mp, void *arg, int lkflags, struct vnode **vpp)
857 struct fuse_lookup_alloc_arg *flaa = arg;
859 return fuse_vnode_get(mp, flaa->feo, flaa->nid, NULL, vpp, flaa->cnp,
863 SDT_PROBE_DEFINE3(fusefs, , vnops, cache_lookup,
864 "int", "struct timespec*", "struct timespec*");
866 struct vnop_lookup_args {
867 struct vnodeop_desc *a_desc;
869 struct vnode **a_vpp;
870 struct componentname *a_cnp;
874 fuse_vnop_lookup(struct vop_lookup_args *ap)
876 struct vnode *dvp = ap->a_dvp;
877 struct vnode **vpp = ap->a_vpp;
878 struct componentname *cnp = ap->a_cnp;
879 struct thread *td = cnp->cn_thread;
880 struct ucred *cred = cnp->cn_cred;
882 int nameiop = cnp->cn_nameiop;
883 int flags = cnp->cn_flags;
884 int wantparent = flags & (LOCKPARENT | WANTPARENT);
885 int islastcn = flags & ISLASTCN;
886 struct mount *mp = vnode_mount(dvp);
890 struct vnode *vp = NULL;
892 struct fuse_dispatcher fdi;
893 bool did_lookup = false;
894 struct fuse_entry_out *feo = NULL;
895 enum vtype vtyp; /* vnode type of target */
896 off_t filesize; /* filesize of target */
900 if (fuse_isdeadfs(dvp)) {
904 if (!vnode_isdir(dvp))
907 if (islastcn && vfs_isrdonly(mp) && (nameiop != LOOKUP))
910 if ((err = fuse_internal_access(dvp, VEXEC, td, cred)))
913 if (flags & ISDOTDOT) {
914 KASSERT(VTOFUD(dvp)->flag & FN_PARENT_NID,
915 ("Looking up .. is TODO"));
916 nid = VTOFUD(dvp)->parent_nid;
919 /* .. is obviously a directory */
922 } else if (cnp->cn_namelen == 1 && *(cnp->cn_nameptr) == '.') {
924 /* . is obviously a directory */
928 struct timespec now, timeout;
930 err = cache_lookup(dvp, vpp, cnp, &timeout, NULL);
932 SDT_PROBE3(fusefs, , vnops, cache_lookup, err, &timeout, &now);
934 case -1: /* positive match */
935 if (timespeccmp(&timeout, &now, >)) {
936 atomic_add_acq_long(&fuse_lookup_cache_hits, 1);
939 atomic_add_acq_long(&fuse_lookup_cache_misses,
951 case 0: /* no match in cache */
952 atomic_add_acq_long(&fuse_lookup_cache_misses, 1);
955 case ENOENT: /* negative match */
957 if (timespeccmp(&timeout, &now, <=)) {
959 cache_purge_negative(dvp);
968 fdisp_init(&fdi, cnp->cn_namelen + 1);
969 fdisp_make(&fdi, FUSE_LOOKUP, mp, nid, td, cred);
971 memcpy(fdi.indata, cnp->cn_nameptr, cnp->cn_namelen);
972 ((char *)fdi.indata)[cnp->cn_namelen] = '\0';
973 lookup_err = fdisp_wait_answ(&fdi);
977 /* lookup call succeeded */
978 feo = (struct fuse_entry_out *)fdi.answ;
981 /* zero nodeid means ENOENT and cache it */
982 struct timespec timeout;
984 fdi.answ_stat = ENOENT;
986 if (cnp->cn_flags & MAKEENTRY) {
987 fuse_validity_2_timespec(feo, &timeout);
988 cache_enter_time(dvp, *vpp, cnp,
991 } else if (nid == FUSE_ROOT_ID) {
994 vtyp = IFTOVT(feo->attr.mode);
995 filesize = feo->attr.size;
997 if (lookup_err && (!fdi.answ_stat || lookup_err != ENOENT)) {
1002 /* lookup_err, if non-zero, must be ENOENT at this point */
1005 /* Entry not found */
1006 if ((nameiop == CREATE || nameiop == RENAME) && islastcn) {
1007 err = fuse_internal_access(dvp, VWRITE, td, cred);
1010 * Set the SAVENAME flag to hold onto the
1011 * pathname for use later in VOP_CREATE or
1014 cnp->cn_flags |= SAVENAME;
1022 /* Entry was found */
1023 if (flags & ISDOTDOT) {
1024 struct fuse_lookup_alloc_arg flaa;
1030 err = vn_vget_ino_gen(dvp, fuse_lookup_alloc, &flaa, 0,
1033 } else if (nid == VTOI(dvp)) {
1037 struct fuse_vnode_data *fvdat;
1039 err = fuse_vnode_get(vnode_mount(dvp), feo, nid, dvp,
1046 * In the case where we are looking up a FUSE node
1047 * represented by an existing cached vnode, and the
1048 * true size reported by FUSE_LOOKUP doesn't match
1049 * the vnode's cached size, then any cached writes
1050 * beyond the file's current size are lost.
1053 * * following attribute cache expiration, or
1054 * * due a bug in the daemon, or
1057 if (vnode_isreg(vp) &&
1058 filesize != fvdat->cached_attrs.va_size &&
1059 fvdat->flag & FN_SIZECHANGE) {
1061 * The FN_SIZECHANGE flag reflects a dirty
1062 * append. If userspace lets us know our cache
1063 * is invalid, that write was lost. (Dirty
1064 * writes that do not cause append are also
1065 * lost, but we don't detect them here.)
1067 * XXX: Maybe disable WB caching on this mount.
1069 printf("%s: WB cache incoherent on %s!\n",
1071 vnode_mount(vp)->mnt_stat.f_mntonname);
1073 fvdat->flag &= ~FN_SIZECHANGE;
1077 fuse_internal_cache_attrs(*vpp, cred, &feo->attr,
1078 feo->attr_valid, feo->attr_valid_nsec, NULL);
1080 if ((nameiop == DELETE || nameiop == RENAME) &&
1083 struct vattr dvattr;
1085 err = fuse_internal_access(dvp, VWRITE, td,
1090 * if the parent's sticky bit is set, check
1091 * whether we're allowed to remove the file.
1092 * Need to figure out the vnode locking to make
1095 fuse_internal_getattr(dvp, &dvattr, cred, td);
1096 if ((dvattr.va_mode & S_ISTXT) &&
1097 fuse_internal_access(dvp, VADMIN, td,
1099 fuse_internal_access(*vpp, VADMIN, td,
1107 (nameiop == DELETE) ||
1108 (nameiop == RENAME && wantparent))) {
1109 cnp->cn_flags |= SAVENAME;
1116 if (vp != NULL && dvp != vp)
1118 else if (vp != NULL)
1123 fdisp_destroy(&fdi);
1129 struct vnop_mkdir_args {
1130 struct vnode *a_dvp;
1131 struct vnode **a_vpp;
1132 struct componentname *a_cnp;
1133 struct vattr *a_vap;
1137 fuse_vnop_mkdir(struct vop_mkdir_args *ap)
1139 struct vnode *dvp = ap->a_dvp;
1140 struct vnode **vpp = ap->a_vpp;
1141 struct componentname *cnp = ap->a_cnp;
1142 struct vattr *vap = ap->a_vap;
1144 struct fuse_mkdir_in fmdi;
1146 if (fuse_isdeadfs(dvp)) {
1149 fmdi.mode = MAKEIMODE(vap->va_type, vap->va_mode);
1151 return (fuse_internal_newentry(dvp, vpp, cnp, FUSE_MKDIR, &fmdi,
1152 sizeof(fmdi), VDIR));
1156 struct vnop_mknod_args {
1157 struct vnode *a_dvp;
1158 struct vnode **a_vpp;
1159 struct componentname *a_cnp;
1160 struct vattr *a_vap;
1164 fuse_vnop_mknod(struct vop_mknod_args *ap)
1167 struct vnode *dvp = ap->a_dvp;
1168 struct vnode **vpp = ap->a_vpp;
1169 struct componentname *cnp = ap->a_cnp;
1170 struct vattr *vap = ap->a_vap;
1172 if (fuse_isdeadfs(dvp))
1175 return fuse_internal_mknod(dvp, vpp, cnp, vap);
1179 struct vnop_open_args {
1182 struct ucred *a_cred;
1183 struct thread *a_td;
1184 int a_fdidx; / struct file *a_fp;
1188 fuse_vnop_open(struct vop_open_args *ap)
1190 struct vnode *vp = ap->a_vp;
1191 int a_mode = ap->a_mode;
1192 struct thread *td = ap->a_td;
1193 struct ucred *cred = ap->a_cred;
1194 pid_t pid = td->td_proc->p_pid;
1195 struct fuse_vnode_data *fvdat;
1197 if (fuse_isdeadfs(vp))
1199 if (vp->v_type == VCHR || vp->v_type == VBLK || vp->v_type == VFIFO)
1200 return (EOPNOTSUPP);
1201 if ((a_mode & (FREAD | FWRITE | FEXEC)) == 0)
1206 if (fuse_filehandle_validrw(vp, a_mode, cred, pid)) {
1207 fuse_vnode_open(vp, 0, td);
1211 return fuse_filehandle_open(vp, a_mode, NULL, td, cred);
1215 fuse_vnop_pathconf(struct vop_pathconf_args *ap)
1218 switch (ap->a_name) {
1219 case _PC_FILESIZEBITS:
1223 *ap->a_retval = NAME_MAX;
1226 *ap->a_retval = MIN(LONG_MAX, FUSE_LINK_MAX);
1228 case _PC_SYMLINK_MAX:
1229 *ap->a_retval = MAXPATHLEN;
1235 return (vop_stdpathconf(ap));
1240 struct vnop_read_args {
1244 struct ucred *a_cred;
1248 fuse_vnop_read(struct vop_read_args *ap)
1250 struct vnode *vp = ap->a_vp;
1251 struct uio *uio = ap->a_uio;
1252 int ioflag = ap->a_ioflag;
1253 struct ucred *cred = ap->a_cred;
1254 pid_t pid = curthread->td_proc->p_pid;
1256 if (fuse_isdeadfs(vp)) {
1260 if (VTOFUD(vp)->flag & FN_DIRECTIO) {
1261 ioflag |= IO_DIRECT;
1264 return fuse_io_dispatch(vp, uio, ioflag, false, cred, pid);
1268 struct vnop_readdir_args {
1271 struct ucred *a_cred;
1278 fuse_vnop_readdir(struct vop_readdir_args *ap)
1280 struct vnode *vp = ap->a_vp;
1281 struct uio *uio = ap->a_uio;
1282 struct ucred *cred = ap->a_cred;
1283 struct fuse_filehandle *fufh = NULL;
1284 struct fuse_iov cookediov;
1286 pid_t pid = curthread->td_proc->p_pid;
1288 if (fuse_isdeadfs(vp)) {
1291 if ( /* XXXIP ((uio_iovcnt(uio) > 1)) || */
1292 (uio_resid(uio) < sizeof(struct dirent))) {
1296 err = fuse_filehandle_get_dir(vp, &fufh, cred, pid);
1299 #define DIRCOOKEDSIZE FUSE_DIRENT_ALIGN(FUSE_NAME_OFFSET + MAXNAMLEN + 1)
1300 fiov_init(&cookediov, DIRCOOKEDSIZE);
1302 err = fuse_internal_readdir(vp, uio, fufh, &cookediov);
1304 fiov_teardown(&cookediov);
1310 struct vnop_readlink_args {
1313 struct ucred *a_cred;
1317 fuse_vnop_readlink(struct vop_readlink_args *ap)
1319 struct vnode *vp = ap->a_vp;
1320 struct uio *uio = ap->a_uio;
1321 struct ucred *cred = ap->a_cred;
1323 struct fuse_dispatcher fdi;
1326 if (fuse_isdeadfs(vp)) {
1329 if (!vnode_islnk(vp)) {
1332 fdisp_init(&fdi, 0);
1333 err = fdisp_simple_putget_vp(&fdi, FUSE_READLINK, vp, curthread, cred);
1337 if (((char *)fdi.answ)[0] == '/' &&
1338 fuse_get_mpdata(vnode_mount(vp))->dataflags & FSESS_PUSH_SYMLINKS_IN) {
1339 char *mpth = vnode_mount(vp)->mnt_stat.f_mntonname;
1341 err = uiomove(mpth, strlen(mpth), uio);
1344 err = uiomove(fdi.answ, fdi.iosize, uio);
1347 fdisp_destroy(&fdi);
1352 struct vnop_reclaim_args {
1354 struct thread *a_td;
1358 fuse_vnop_reclaim(struct vop_reclaim_args *ap)
1360 struct vnode *vp = ap->a_vp;
1361 struct thread *td = ap->a_td;
1362 struct fuse_vnode_data *fvdat = VTOFUD(vp);
1363 struct fuse_filehandle *fufh, *fufh_tmp;
1366 panic("FUSE: no vnode data during recycling");
1368 LIST_FOREACH_SAFE(fufh, &fvdat->handles, next, fufh_tmp) {
1369 printf("FUSE: vnode being reclaimed with open fufh "
1370 "(type=%#x)", fufh->fufh_type);
1371 fuse_filehandle_close(vp, fufh, td, NULL);
1374 if ((!fuse_isdeadfs(vp)) && (fvdat->nlookup)) {
1375 fuse_internal_forget_send(vnode_mount(vp), td, NULL, VTOI(vp),
1378 fuse_vnode_setparent(vp, NULL);
1380 vfs_hash_remove(vp);
1381 vnode_destroy_vobject(vp);
1382 fuse_vnode_destroy(vp);
1388 struct vnop_remove_args {
1389 struct vnode *a_dvp;
1391 struct componentname *a_cnp;
1395 fuse_vnop_remove(struct vop_remove_args *ap)
1397 struct vnode *dvp = ap->a_dvp;
1398 struct vnode *vp = ap->a_vp;
1399 struct componentname *cnp = ap->a_cnp;
1403 if (fuse_isdeadfs(vp)) {
1406 if (vnode_isdir(vp)) {
1411 err = fuse_internal_remove(dvp, vp, cnp, FUSE_UNLINK);
1414 fuse_internal_vnode_disappear(vp);
1416 * Purge the parent's attribute cache because the daemon
1417 * should've updated its mtime and ctime
1419 fuse_vnode_clear_attr_cache(dvp);
1425 struct vnop_rename_args {
1426 struct vnode *a_fdvp;
1427 struct vnode *a_fvp;
1428 struct componentname *a_fcnp;
1429 struct vnode *a_tdvp;
1430 struct vnode *a_tvp;
1431 struct componentname *a_tcnp;
1435 fuse_vnop_rename(struct vop_rename_args *ap)
1437 struct vnode *fdvp = ap->a_fdvp;
1438 struct vnode *fvp = ap->a_fvp;
1439 struct componentname *fcnp = ap->a_fcnp;
1440 struct vnode *tdvp = ap->a_tdvp;
1441 struct vnode *tvp = ap->a_tvp;
1442 struct componentname *tcnp = ap->a_tcnp;
1443 struct fuse_data *data;
1444 bool newparent = fdvp != tdvp;
1445 bool isdir = fvp->v_type == VDIR;
1448 if (fuse_isdeadfs(fdvp)) {
1451 if (fvp->v_mount != tdvp->v_mount ||
1452 (tvp && fvp->v_mount != tvp->v_mount)) {
1453 SDT_PROBE2(fusefs, , vnops, trace, 1, "cross-device rename");
1460 * FUSE library is expected to check if target directory is not
1461 * under the source directory in the file system tree.
1462 * Linux performs this check at VFS level.
1465 * If source is a directory, and it will get a new parent, user must
1466 * have write permission to it, so ".." can be modified.
1468 data = fuse_get_mpdata(vnode_mount(tdvp));
1469 if (data->dataflags & FSESS_DEFAULT_PERMISSIONS && isdir && newparent) {
1470 err = fuse_internal_access(fvp, VWRITE,
1471 tcnp->cn_thread, tcnp->cn_cred);
1475 sx_xlock(&data->rename_lock);
1476 err = fuse_internal_rename(fdvp, fcnp, tdvp, tcnp);
1479 fuse_vnode_setparent(fvp, tdvp);
1481 fuse_vnode_setparent(tvp, NULL);
1483 sx_unlock(&data->rename_lock);
1485 if (tvp != NULL && tvp != fvp) {
1488 if (vnode_isdir(fvp)) {
1489 if ((tvp != NULL) && vnode_isdir(tvp)) {
1510 struct vnop_rmdir_args {
1511 struct vnode *a_dvp;
1513 struct componentname *a_cnp;
1517 fuse_vnop_rmdir(struct vop_rmdir_args *ap)
1519 struct vnode *dvp = ap->a_dvp;
1520 struct vnode *vp = ap->a_vp;
1524 if (fuse_isdeadfs(vp)) {
1527 if (VTOFUD(vp) == VTOFUD(dvp)) {
1530 err = fuse_internal_remove(dvp, vp, ap->a_cnp, FUSE_RMDIR);
1533 fuse_internal_vnode_disappear(vp);
1535 * Purge the parent's attribute cache because the daemon
1536 * should've updated its mtime and ctime
1538 fuse_vnode_clear_attr_cache(dvp);
1544 struct vnop_setattr_args {
1546 struct vattr *a_vap;
1547 struct ucred *a_cred;
1548 struct thread *a_td;
1552 fuse_vnop_setattr(struct vop_setattr_args *ap)
1554 struct vnode *vp = ap->a_vp;
1555 struct vattr *vap = ap->a_vap;
1556 struct ucred *cred = ap->a_cred;
1557 struct thread *td = curthread;
1559 struct fuse_data *data;
1560 struct vattr old_va;
1563 accmode_t accmode = 0;
1565 bool drop_suid = false;
1568 mp = vnode_mount(vp);
1569 data = fuse_get_mpdata(mp);
1570 dataflags = data->dataflags;
1571 checkperm = dataflags & FSESS_DEFAULT_PERMISSIONS;
1572 if (cred->cr_ngroups > 0)
1573 cr_gid = cred->cr_groups[0];
1577 if (fuse_isdeadfs(vp)) {
1581 if (vap->va_uid != (uid_t)VNOVAL) {
1583 /* Only root may change a file's owner */
1584 err = priv_check_cred(cred, PRIV_VFS_CHOWN);
1586 /* As a special case, allow the null chown */
1587 err2 = fuse_internal_getattr(vp, &old_va, cred,
1591 if (vap->va_uid != old_va.va_uid)
1601 if (vap->va_gid != (gid_t)VNOVAL) {
1602 if (checkperm && priv_check_cred(cred, PRIV_VFS_CHOWN))
1604 if (checkperm && !groupmember(vap->va_gid, cred))
1607 * Non-root users may only chgrp to one of their own
1610 err = priv_check_cred(cred, PRIV_VFS_CHOWN);
1612 /* As a special case, allow the null chgrp */
1613 err2 = fuse_internal_getattr(vp, &old_va, cred,
1617 if (vap->va_gid != old_va.va_gid)
1625 if (vap->va_size != VNOVAL) {
1626 switch (vp->v_type) {
1631 if (vfs_isrdonly(mp))
1636 * According to POSIX, the result is unspecified
1637 * for file types other than regular files,
1638 * directories and shared memory objects. We
1639 * don't support shared memory objects in the file
1640 * system, and have dubious support for truncating
1641 * symlinks. Just ignore the request in other cases.
1645 /* Don't set accmode. Permission to trunc is checked upstack */
1647 if (vap->va_atime.tv_sec != VNOVAL || vap->va_mtime.tv_sec != VNOVAL) {
1648 if (vap->va_vaflags & VA_UTIMES_NULL)
1654 if (vap->va_mode != (mode_t)VNOVAL)
1655 vap->va_mode &= ~(S_ISUID | S_ISGID);
1657 err = fuse_internal_getattr(vp, &old_va, cred, td);
1660 vap->va_mode = old_va.va_mode & ~(S_ISUID | S_ISGID);
1663 if (vap->va_mode != (mode_t)VNOVAL) {
1664 /* Only root may set the sticky bit on non-directories */
1665 if (checkperm && vp->v_type != VDIR && (vap->va_mode & S_ISTXT)
1666 && priv_check_cred(cred, PRIV_VFS_STICKYFILE))
1668 if (checkperm && (vap->va_mode & S_ISGID)) {
1669 err = fuse_internal_getattr(vp, &old_va, cred, td);
1672 if (!groupmember(old_va.va_gid, cred)) {
1673 err = priv_check_cred(cred, PRIV_VFS_SETGID);
1681 if (vfs_isrdonly(mp))
1684 err = fuse_internal_access(vp, accmode, td, cred);
1688 return fuse_internal_setattr(vp, vap, td, cred);
1692 struct vnop_strategy_args {
1698 fuse_vnop_strategy(struct vop_strategy_args *ap)
1700 struct vnode *vp = ap->a_vp;
1701 struct buf *bp = ap->a_bp;
1703 if (!vp || fuse_isdeadfs(vp)) {
1704 bp->b_ioflags |= BIO_ERROR;
1705 bp->b_error = ENXIO;
1711 * VOP_STRATEGY always returns zero and signals error via bp->b_ioflags.
1712 * fuse_io_strategy sets bp's error fields
1714 (void)fuse_io_strategy(vp, bp);
1721 struct vnop_symlink_args {
1722 struct vnode *a_dvp;
1723 struct vnode **a_vpp;
1724 struct componentname *a_cnp;
1725 struct vattr *a_vap;
1730 fuse_vnop_symlink(struct vop_symlink_args *ap)
1732 struct vnode *dvp = ap->a_dvp;
1733 struct vnode **vpp = ap->a_vpp;
1734 struct componentname *cnp = ap->a_cnp;
1735 const char *target = ap->a_target;
1737 struct fuse_dispatcher fdi;
1742 if (fuse_isdeadfs(dvp)) {
1746 * Unlike the other creator type calls, here we have to create a message
1747 * where the name of the new entry comes first, and the data describing
1748 * the entry comes second.
1749 * Hence we can't rely on our handy fuse_internal_newentry() routine,
1750 * but put together the message manually and just call the core part.
1753 len = strlen(target) + 1;
1754 fdisp_init(&fdi, len + cnp->cn_namelen + 1);
1755 fdisp_make_vp(&fdi, FUSE_SYMLINK, dvp, curthread, NULL);
1757 memcpy(fdi.indata, cnp->cn_nameptr, cnp->cn_namelen);
1758 ((char *)fdi.indata)[cnp->cn_namelen] = '\0';
1759 memcpy((char *)fdi.indata + cnp->cn_namelen + 1, target, len);
1761 err = fuse_internal_newentry_core(dvp, vpp, cnp, VLNK, &fdi);
1762 fdisp_destroy(&fdi);
1767 struct vnop_write_args {
1771 struct ucred *a_cred;
1775 fuse_vnop_write(struct vop_write_args *ap)
1777 struct vnode *vp = ap->a_vp;
1778 struct uio *uio = ap->a_uio;
1779 int ioflag = ap->a_ioflag;
1780 struct ucred *cred = ap->a_cred;
1781 pid_t pid = curthread->td_proc->p_pid;
1783 if (fuse_isdeadfs(vp)) {
1787 if (VTOFUD(vp)->flag & FN_DIRECTIO) {
1788 ioflag |= IO_DIRECT;
1791 return fuse_io_dispatch(vp, uio, ioflag, false, cred, pid);
1794 SDT_PROBE_DEFINE1(fusefs, , vnops, vnop_getpages_error, "int");
1796 struct vnop_getpages_args {
1804 fuse_vnop_getpages(struct vop_getpages_args *ap)
1806 int i, error, nextoff, size, toff, count, npages;
1815 pid_t pid = curthread->td_proc->p_pid;
1818 KASSERT(vp->v_object, ("objectless vp passed to getpages"));
1819 td = curthread; /* XXX */
1820 cred = curthread->td_ucred; /* XXX */
1822 npages = ap->a_count;
1824 if (!fsess_opt_mmap(vnode_mount(vp))) {
1825 SDT_PROBE2(fusefs, , vnops, trace, 1,
1826 "called on non-cacheable vnode??\n");
1827 return (VM_PAGER_ERROR);
1831 * If the last page is partially valid, just return it and allow
1832 * the pager to zero-out the blanks. Partially valid pages can
1833 * only occur at the file EOF.
1835 * XXXGL: is that true for FUSE, which is a local filesystem,
1836 * but still somewhat disconnected from the kernel?
1838 VM_OBJECT_WLOCK(vp->v_object);
1839 if (pages[npages - 1]->valid != 0 && --npages == 0)
1841 VM_OBJECT_WUNLOCK(vp->v_object);
1844 * We use only the kva address for the buffer, but this is extremely
1845 * convenient and fast.
1847 bp = uma_zalloc(fuse_pbuf_zone, M_WAITOK);
1849 kva = (vm_offset_t)bp->b_data;
1850 pmap_qenter(kva, pages, npages);
1851 VM_CNT_INC(v_vnodein);
1852 VM_CNT_ADD(v_vnodepgsin, npages);
1854 count = npages << PAGE_SHIFT;
1855 iov.iov_base = (caddr_t)kva;
1856 iov.iov_len = count;
1859 uio.uio_offset = IDX_TO_OFF(pages[0]->pindex);
1860 uio.uio_resid = count;
1861 uio.uio_segflg = UIO_SYSSPACE;
1862 uio.uio_rw = UIO_READ;
1865 error = fuse_io_dispatch(vp, &uio, IO_DIRECT, true, cred, pid);
1866 pmap_qremove(kva, npages);
1868 uma_zfree(fuse_pbuf_zone, bp);
1870 if (error && (uio.uio_resid == count)) {
1871 SDT_PROBE1(fusefs, , vnops, vnop_getpages_error, error);
1872 return VM_PAGER_ERROR;
1875 * Calculate the number of bytes read and validate only that number
1876 * of bytes. Note that due to pending writes, size may be 0. This
1877 * does not mean that the remaining data is invalid!
1880 size = count - uio.uio_resid;
1881 VM_OBJECT_WLOCK(vp->v_object);
1882 fuse_vm_page_lock_queues();
1883 for (i = 0, toff = 0; i < npages; i++, toff = nextoff) {
1886 nextoff = toff + PAGE_SIZE;
1889 if (nextoff <= size) {
1891 * Read operation filled an entire page
1893 m->valid = VM_PAGE_BITS_ALL;
1894 KASSERT(m->dirty == 0,
1895 ("fuse_getpages: page %p is dirty", m));
1896 } else if (size > toff) {
1898 * Read operation filled a partial page.
1901 vm_page_set_valid_range(m, 0, size - toff);
1902 KASSERT(m->dirty == 0,
1903 ("fuse_getpages: page %p is dirty", m));
1906 * Read operation was short. If no error occurred
1907 * we may have hit a zero-fill section. We simply
1908 * leave valid set to 0.
1913 fuse_vm_page_unlock_queues();
1915 VM_OBJECT_WUNLOCK(vp->v_object);
1920 return (VM_PAGER_OK);
1924 struct vnop_putpages_args {
1930 vm_ooffset_t a_offset;
1934 fuse_vnop_putpages(struct vop_putpages_args *ap)
1940 int i, error, npages, count;
1948 pid_t pid = curthread->td_proc->p_pid;
1951 KASSERT(vp->v_object, ("objectless vp passed to putpages"));
1952 fsize = vp->v_object->un_pager.vnp.vnp_size;
1953 td = curthread; /* XXX */
1954 cred = curthread->td_ucred; /* XXX */
1956 count = ap->a_count;
1957 rtvals = ap->a_rtvals;
1958 npages = btoc(count);
1959 offset = IDX_TO_OFF(pages[0]->pindex);
1961 if (!fsess_opt_mmap(vnode_mount(vp))) {
1962 SDT_PROBE2(fusefs, , vnops, trace, 1,
1963 "called on non-cacheable vnode??\n");
1965 for (i = 0; i < npages; i++)
1966 rtvals[i] = VM_PAGER_AGAIN;
1969 * When putting pages, do not extend file past EOF.
1972 if (offset + count > fsize) {
1973 count = fsize - offset;
1978 * We use only the kva address for the buffer, but this is extremely
1979 * convenient and fast.
1981 bp = uma_zalloc(fuse_pbuf_zone, M_WAITOK);
1983 kva = (vm_offset_t)bp->b_data;
1984 pmap_qenter(kva, pages, npages);
1985 VM_CNT_INC(v_vnodeout);
1986 VM_CNT_ADD(v_vnodepgsout, count);
1988 iov.iov_base = (caddr_t)kva;
1989 iov.iov_len = count;
1992 uio.uio_offset = offset;
1993 uio.uio_resid = count;
1994 uio.uio_segflg = UIO_SYSSPACE;
1995 uio.uio_rw = UIO_WRITE;
1998 error = fuse_io_dispatch(vp, &uio, IO_DIRECT, true, cred, pid);
2000 pmap_qremove(kva, npages);
2001 uma_zfree(fuse_pbuf_zone, bp);
2004 int nwritten = round_page(count - uio.uio_resid) / PAGE_SIZE;
2006 for (i = 0; i < nwritten; i++) {
2007 rtvals[i] = VM_PAGER_OK;
2008 VM_OBJECT_WLOCK(pages[i]->object);
2009 vm_page_undirty(pages[i]);
2010 VM_OBJECT_WUNLOCK(pages[i]->object);
2016 static const char extattr_namespace_separator = '.';
2019 struct vop_getextattr_args {
2020 struct vop_generic_args a_gen;
2022 int a_attrnamespace;
2026 struct ucred *a_cred;
2027 struct thread *a_td;
2031 fuse_vnop_getextattr(struct vop_getextattr_args *ap)
2033 struct vnode *vp = ap->a_vp;
2034 struct uio *uio = ap->a_uio;
2035 struct fuse_dispatcher fdi;
2036 struct fuse_getxattr_in *get_xattr_in;
2037 struct fuse_getxattr_out *get_xattr_out;
2038 struct mount *mp = vnode_mount(vp);
2039 struct thread *td = ap->a_td;
2040 struct ucred *cred = ap->a_cred;
2046 if (fuse_isdeadfs(vp))
2049 if (!fsess_isimpl(mp, FUSE_GETXATTR))
2052 err = fuse_extattr_check_cred(vp, ap->a_attrnamespace, cred, td, VREAD);
2056 /* Default to looking for user attributes. */
2057 if (ap->a_attrnamespace == EXTATTR_NAMESPACE_SYSTEM)
2058 prefix = EXTATTR_NAMESPACE_SYSTEM_STRING;
2060 prefix = EXTATTR_NAMESPACE_USER_STRING;
2062 len = strlen(prefix) + sizeof(extattr_namespace_separator) +
2063 strlen(ap->a_name) + 1;
2065 fdisp_init(&fdi, len + sizeof(*get_xattr_in));
2066 fdisp_make_vp(&fdi, FUSE_GETXATTR, vp, td, cred);
2068 get_xattr_in = fdi.indata;
2070 * Check to see whether we're querying the available size or
2071 * issuing the actual request. If we pass in 0, we get back struct
2072 * fuse_getxattr_out. If we pass in a non-zero size, we get back
2073 * that much data, without the struct fuse_getxattr_out header.
2076 get_xattr_in->size = 0;
2078 get_xattr_in->size = uio->uio_resid;
2080 attr_str = (char *)fdi.indata + sizeof(*get_xattr_in);
2081 snprintf(attr_str, len, "%s%c%s", prefix, extattr_namespace_separator,
2084 err = fdisp_wait_answ(&fdi);
2086 if (err == ENOSYS) {
2087 fsess_set_notimpl(mp, FUSE_GETXATTR);
2093 get_xattr_out = fdi.answ;
2095 if (ap->a_size != NULL)
2096 *ap->a_size = get_xattr_out->size;
2099 err = uiomove(fdi.answ, fdi.iosize, uio);
2102 fdisp_destroy(&fdi);
2107 struct vop_setextattr_args {
2108 struct vop_generic_args a_gen;
2110 int a_attrnamespace;
2113 struct ucred *a_cred;
2114 struct thread *a_td;
2118 fuse_vnop_setextattr(struct vop_setextattr_args *ap)
2120 struct vnode *vp = ap->a_vp;
2121 struct uio *uio = ap->a_uio;
2122 struct fuse_dispatcher fdi;
2123 struct fuse_setxattr_in *set_xattr_in;
2124 struct mount *mp = vnode_mount(vp);
2125 struct thread *td = ap->a_td;
2126 struct ucred *cred = ap->a_cred;
2132 if (fuse_isdeadfs(vp))
2135 if (!fsess_isimpl(mp, FUSE_SETXATTR))
2138 if (vfs_isrdonly(mp))
2141 /* Deleting xattrs must use VOP_DELETEEXTATTR instead */
2142 if (ap->a_uio == NULL) {
2144 * If we got here as fallback from VOP_DELETEEXTATTR, then
2145 * return EOPNOTSUPP.
2147 if (!fsess_isimpl(mp, FUSE_REMOVEXATTR))
2148 return (EOPNOTSUPP);
2153 err = fuse_extattr_check_cred(vp, ap->a_attrnamespace, cred, td,
2158 /* Default to looking for user attributes. */
2159 if (ap->a_attrnamespace == EXTATTR_NAMESPACE_SYSTEM)
2160 prefix = EXTATTR_NAMESPACE_SYSTEM_STRING;
2162 prefix = EXTATTR_NAMESPACE_USER_STRING;
2164 len = strlen(prefix) + sizeof(extattr_namespace_separator) +
2165 strlen(ap->a_name) + 1;
2167 fdisp_init(&fdi, len + sizeof(*set_xattr_in) + uio->uio_resid);
2168 fdisp_make_vp(&fdi, FUSE_SETXATTR, vp, td, cred);
2170 set_xattr_in = fdi.indata;
2171 set_xattr_in->size = uio->uio_resid;
2173 attr_str = (char *)fdi.indata + sizeof(*set_xattr_in);
2174 snprintf(attr_str, len, "%s%c%s", prefix, extattr_namespace_separator,
2177 err = uiomove((char *)fdi.indata + sizeof(*set_xattr_in) + len,
2178 uio->uio_resid, uio);
2183 err = fdisp_wait_answ(&fdi);
2185 if (err == ENOSYS) {
2186 fsess_set_notimpl(mp, FUSE_SETXATTR);
2189 if (err == ERESTART) {
2190 /* Can't restart after calling uiomove */
2195 fdisp_destroy(&fdi);
2200 * The Linux / FUSE extended attribute list is simply a collection of
2201 * NUL-terminated strings. The FreeBSD extended attribute list is a single
2202 * byte length followed by a non-NUL terminated string. So, this allows
2203 * conversion of the Linux / FUSE format to the FreeBSD format in place.
2204 * Linux attribute names are reported with the namespace as a prefix (e.g.
2205 * "user.attribute_name"), but in FreeBSD they are reported without the
2206 * namespace prefix (e.g. "attribute_name"). So, we're going from:
2208 * user.attr_name1\0user.attr_name2\0
2212 * <num>attr_name1<num>attr_name2
2214 * Where "<num>" is a single byte number of characters in the attribute name.
2217 * prefix - exattr namespace prefix string
2218 * list, list_len - input list with namespace prefixes
2219 * bsd_list, bsd_list_len - output list compatible with bsd vfs
2222 fuse_xattrlist_convert(char *prefix, const char *list, int list_len,
2223 char *bsd_list, int *bsd_list_len)
2225 int len, pos, dist_to_next, prefix_len;
2229 prefix_len = strlen(prefix);
2231 while (pos < list_len && list[pos] != '\0') {
2232 dist_to_next = strlen(&list[pos]) + 1;
2233 if (bcmp(&list[pos], prefix, prefix_len) == 0 &&
2234 list[pos + prefix_len] == extattr_namespace_separator) {
2235 len = dist_to_next -
2236 (prefix_len + sizeof(extattr_namespace_separator)) - 1;
2237 if (len >= EXTATTR_MAXNAMELEN)
2238 return (ENAMETOOLONG);
2240 bsd_list[*bsd_list_len] = len;
2241 memcpy(&bsd_list[*bsd_list_len + 1],
2242 &list[pos + prefix_len +
2243 sizeof(extattr_namespace_separator)], len);
2245 *bsd_list_len += len + 1;
2248 pos += dist_to_next;
2255 struct vop_listextattr_args {
2256 struct vop_generic_args a_gen;
2258 int a_attrnamespace;
2261 struct ucred *a_cred;
2262 struct thread *a_td;
2266 fuse_vnop_listextattr(struct vop_listextattr_args *ap)
2268 struct vnode *vp = ap->a_vp;
2269 struct uio *uio = ap->a_uio;
2270 struct fuse_dispatcher fdi;
2271 struct fuse_listxattr_in *list_xattr_in;
2272 struct fuse_listxattr_out *list_xattr_out;
2273 struct mount *mp = vnode_mount(vp);
2274 struct thread *td = ap->a_td;
2275 struct ucred *cred = ap->a_cred;
2279 char *bsd_list = NULL;
2285 if (fuse_isdeadfs(vp))
2288 if (!fsess_isimpl(mp, FUSE_LISTXATTR))
2291 err = fuse_extattr_check_cred(vp, ap->a_attrnamespace, cred, td, VREAD);
2296 * Add space for a NUL and the period separator if enabled.
2297 * Default to looking for user attributes.
2299 if (ap->a_attrnamespace == EXTATTR_NAMESPACE_SYSTEM)
2300 prefix = EXTATTR_NAMESPACE_SYSTEM_STRING;
2302 prefix = EXTATTR_NAMESPACE_USER_STRING;
2304 len = strlen(prefix) + sizeof(extattr_namespace_separator) + 1;
2306 fdisp_init(&fdi, sizeof(*list_xattr_in) + len);
2307 fdisp_make_vp(&fdi, FUSE_LISTXATTR, vp, td, cred);
2310 * Retrieve Linux / FUSE compatible list size.
2312 list_xattr_in = fdi.indata;
2313 list_xattr_in->size = 0;
2314 attr_str = (char *)fdi.indata + sizeof(*list_xattr_in);
2315 snprintf(attr_str, len, "%s%c", prefix, extattr_namespace_separator);
2317 err = fdisp_wait_answ(&fdi);
2319 if (err == ENOSYS) {
2320 fsess_set_notimpl(mp, FUSE_LISTXATTR);
2326 list_xattr_out = fdi.answ;
2327 linux_list_len = list_xattr_out->size;
2328 if (linux_list_len == 0) {
2329 if (ap->a_size != NULL)
2330 *ap->a_size = linux_list_len;
2335 * Retrieve Linux / FUSE compatible list values.
2337 fdisp_refresh_vp(&fdi, FUSE_LISTXATTR, vp, td, cred);
2338 list_xattr_in = fdi.indata;
2339 list_xattr_in->size = linux_list_len + sizeof(*list_xattr_out);
2340 attr_str = (char *)fdi.indata + sizeof(*list_xattr_in);
2341 snprintf(attr_str, len, "%s%c", prefix, extattr_namespace_separator);
2343 err = fdisp_wait_answ(&fdi);
2347 linux_list = fdi.answ;
2348 linux_list_len = fdi.iosize;
2351 * Retrieve the BSD compatible list values.
2352 * The Linux / FUSE attribute list format isn't the same
2353 * as FreeBSD's format. So we need to transform it into
2354 * FreeBSD's format before giving it to the user.
2356 bsd_list = malloc(linux_list_len, M_TEMP, M_WAITOK);
2357 err = fuse_xattrlist_convert(prefix, linux_list, linux_list_len,
2358 bsd_list, &bsd_list_len);
2362 if (ap->a_size != NULL)
2363 *ap->a_size = bsd_list_len;
2366 err = uiomove(bsd_list, bsd_list_len, uio);
2369 free(bsd_list, M_TEMP);
2370 fdisp_destroy(&fdi);
2375 struct vop_deleteextattr_args {
2376 struct vop_generic_args a_gen;
2378 int a_attrnamespace;
2380 struct ucred *a_cred;
2381 struct thread *a_td;
2385 fuse_vnop_deleteextattr(struct vop_deleteextattr_args *ap)
2387 struct vnode *vp = ap->a_vp;
2388 struct fuse_dispatcher fdi;
2389 struct mount *mp = vnode_mount(vp);
2390 struct thread *td = ap->a_td;
2391 struct ucred *cred = ap->a_cred;
2397 if (fuse_isdeadfs(vp))
2400 if (!fsess_isimpl(mp, FUSE_REMOVEXATTR))
2403 if (vfs_isrdonly(mp))
2406 err = fuse_extattr_check_cred(vp, ap->a_attrnamespace, cred, td,
2411 /* Default to looking for user attributes. */
2412 if (ap->a_attrnamespace == EXTATTR_NAMESPACE_SYSTEM)
2413 prefix = EXTATTR_NAMESPACE_SYSTEM_STRING;
2415 prefix = EXTATTR_NAMESPACE_USER_STRING;
2417 len = strlen(prefix) + sizeof(extattr_namespace_separator) +
2418 strlen(ap->a_name) + 1;
2420 fdisp_init(&fdi, len);
2421 fdisp_make_vp(&fdi, FUSE_REMOVEXATTR, vp, td, cred);
2423 attr_str = fdi.indata;
2424 snprintf(attr_str, len, "%s%c%s", prefix, extattr_namespace_separator,
2427 err = fdisp_wait_answ(&fdi);
2428 if (err == ENOSYS) {
2429 fsess_set_notimpl(mp, FUSE_REMOVEXATTR);
2433 fdisp_destroy(&fdi);
2438 struct vnop_print_args {
2443 fuse_vnop_print(struct vop_print_args *ap)
2445 struct fuse_vnode_data *fvdat = VTOFUD(ap->a_vp);
2447 printf("nodeid: %ju, parent nodeid: %ju, nlookup: %ju, flag: %#x\n",
2448 (uintmax_t)VTOILLU(ap->a_vp), (uintmax_t)fvdat->parent_nid,
2449 (uintmax_t)fvdat->nlookup,
2456 * Get an NFS filehandle for a FUSE file.
2458 * This will only work for FUSE file systems that guarantee the uniqueness of
2459 * nodeid:generation, which most don't
2463 IN struct vnode *a_vp;
2464 IN struct fid *a_fhp;
2468 fuse_vnop_vptofh(struct vop_vptofh_args *ap)
2470 struct vnode *vp = ap->a_vp;
2471 struct fuse_vnode_data *fvdat = VTOFUD(vp);
2472 struct fuse_fid *fhp = (struct fuse_fid *)(ap->a_fhp);
2473 _Static_assert(sizeof(struct fuse_fid) <= sizeof(struct fid),
2474 "FUSE fid type is too big");
2475 struct mount *mp = vnode_mount(vp);
2476 struct fuse_data *data = fuse_get_mpdata(mp);
2480 if (!(data->dataflags & FSESS_EXPORT_SUPPORT))
2483 err = fuse_internal_getattr(vp, &va, curthread->td_ucred, curthread);
2487 /*ip = VTOI(ap->a_vp);*/
2488 /*ufhp = (struct ufid *)ap->a_fhp;*/
2489 fhp->len = sizeof(struct fuse_fid);
2490 fhp->nid = fvdat->nid;
2491 if (fvdat->generation <= UINT32_MAX)
2492 fhp->gen = fvdat->generation;