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(fuse);
115 * arg0: verbosity. Higher numbers give more verbose messages
116 * arg1: Textual message
118 SDT_PROBE_DEFINE2(fuse, , vnops, trace, "int", "char*");
121 static vop_access_t fuse_vnop_access;
122 static vop_close_t fuse_vnop_close;
123 static vop_create_t fuse_vnop_create;
124 static vop_deleteextattr_t fuse_vnop_deleteextattr;
125 static vop_fdatasync_t fuse_vnop_fdatasync;
126 static vop_fsync_t fuse_vnop_fsync;
127 static vop_getattr_t fuse_vnop_getattr;
128 static vop_getextattr_t fuse_vnop_getextattr;
129 static vop_inactive_t fuse_vnop_inactive;
130 static vop_link_t fuse_vnop_link;
131 static vop_listextattr_t fuse_vnop_listextattr;
132 static vop_lookup_t fuse_vnop_lookup;
133 static vop_mkdir_t fuse_vnop_mkdir;
134 static vop_mknod_t fuse_vnop_mknod;
135 static vop_open_t fuse_vnop_open;
136 static vop_pathconf_t fuse_vnop_pathconf;
137 static vop_read_t fuse_vnop_read;
138 static vop_readdir_t fuse_vnop_readdir;
139 static vop_readlink_t fuse_vnop_readlink;
140 static vop_reclaim_t fuse_vnop_reclaim;
141 static vop_remove_t fuse_vnop_remove;
142 static vop_rename_t fuse_vnop_rename;
143 static vop_rmdir_t fuse_vnop_rmdir;
144 static vop_setattr_t fuse_vnop_setattr;
145 static vop_setextattr_t fuse_vnop_setextattr;
146 static vop_strategy_t fuse_vnop_strategy;
147 static vop_symlink_t fuse_vnop_symlink;
148 static vop_write_t fuse_vnop_write;
149 static vop_getpages_t fuse_vnop_getpages;
150 static vop_putpages_t fuse_vnop_putpages;
151 static vop_print_t fuse_vnop_print;
153 struct vop_vector fuse_vnops = {
154 .vop_default = &default_vnodeops,
155 .vop_access = fuse_vnop_access,
156 .vop_close = fuse_vnop_close,
157 .vop_create = fuse_vnop_create,
158 .vop_deleteextattr = fuse_vnop_deleteextattr,
159 .vop_fsync = fuse_vnop_fsync,
160 .vop_fdatasync = fuse_vnop_fdatasync,
161 .vop_getattr = fuse_vnop_getattr,
162 .vop_getextattr = fuse_vnop_getextattr,
163 .vop_inactive = fuse_vnop_inactive,
164 .vop_link = fuse_vnop_link,
165 .vop_listextattr = fuse_vnop_listextattr,
166 .vop_lookup = fuse_vnop_lookup,
167 .vop_mkdir = fuse_vnop_mkdir,
168 .vop_mknod = fuse_vnop_mknod,
169 .vop_open = fuse_vnop_open,
170 .vop_pathconf = fuse_vnop_pathconf,
171 .vop_read = fuse_vnop_read,
172 .vop_readdir = fuse_vnop_readdir,
173 .vop_readlink = fuse_vnop_readlink,
174 .vop_reclaim = fuse_vnop_reclaim,
175 .vop_remove = fuse_vnop_remove,
176 .vop_rename = fuse_vnop_rename,
177 .vop_rmdir = fuse_vnop_rmdir,
178 .vop_setattr = fuse_vnop_setattr,
179 .vop_setextattr = fuse_vnop_setextattr,
180 .vop_strategy = fuse_vnop_strategy,
181 .vop_symlink = fuse_vnop_symlink,
182 .vop_write = fuse_vnop_write,
183 .vop_getpages = fuse_vnop_getpages,
184 .vop_putpages = fuse_vnop_putpages,
185 .vop_print = fuse_vnop_print,
188 static u_long fuse_lookup_cache_hits = 0;
190 SYSCTL_ULONG(_vfs_fusefs, OID_AUTO, lookup_cache_hits, CTLFLAG_RD,
191 &fuse_lookup_cache_hits, 0, "number of positive cache hits in lookup");
193 static u_long fuse_lookup_cache_misses = 0;
195 SYSCTL_ULONG(_vfs_fusefs, OID_AUTO, lookup_cache_misses, CTLFLAG_RD,
196 &fuse_lookup_cache_misses, 0, "number of cache misses in lookup");
198 int fuse_lookup_cache_enable = 1;
200 SYSCTL_INT(_vfs_fusefs, OID_AUTO, lookup_cache_enable, CTLFLAG_RW,
201 &fuse_lookup_cache_enable, 0, "if non-zero, enable lookup cache");
204 * XXX: This feature is highly experimental and can bring to instabilities,
205 * needs revisiting before to be enabled by default.
207 static int fuse_reclaim_revoked = 0;
209 SYSCTL_INT(_vfs_fusefs, OID_AUTO, reclaim_revoked, CTLFLAG_RW,
210 &fuse_reclaim_revoked, 0, "");
212 uma_zone_t fuse_pbuf_zone;
214 #define fuse_vm_page_lock(m) vm_page_lock((m));
215 #define fuse_vm_page_unlock(m) vm_page_unlock((m));
216 #define fuse_vm_page_lock_queues() ((void)0)
217 #define fuse_vm_page_unlock_queues() ((void)0)
219 /* Get a filehandle for a directory */
221 fuse_filehandle_get_dir(struct vnode *vp, struct fuse_filehandle **fufhp,
222 struct ucred *cred, pid_t pid)
224 if (fuse_filehandle_get(vp, FREAD, fufhp, cred, pid) == 0)
226 return fuse_filehandle_get(vp, FEXEC, fufhp, cred, pid);
229 /* Send FUSE_FLUSH for this vnode */
231 fuse_flush(struct vnode *vp, struct ucred *cred, pid_t pid, int fflag)
233 struct fuse_flush_in *ffi;
234 struct fuse_filehandle *fufh;
235 struct fuse_dispatcher fdi;
236 struct thread *td = curthread;
237 struct mount *mp = vnode_mount(vp);
240 if (!fsess_isimpl(vnode_mount(vp), FUSE_FLUSH))
243 err = fuse_filehandle_get(vp, fflag, &fufh, cred, pid);
247 fdisp_init(&fdi, sizeof(*ffi));
248 fdisp_make_vp(&fdi, FUSE_FLUSH, vp, td, cred);
250 ffi->fh = fufh->fh_id;
252 err = fdisp_wait_answ(&fdi);
254 fsess_set_notimpl(mp, FUSE_FLUSH);
262 struct vnop_access_args {
264 #if VOP_ACCESS_TAKES_ACCMODE_T
269 struct ucred *a_cred;
274 fuse_vnop_access(struct vop_access_args *ap)
276 struct vnode *vp = ap->a_vp;
277 int accmode = ap->a_accmode;
278 struct ucred *cred = ap->a_cred;
280 struct fuse_access_param facp;
281 struct fuse_data *data = fuse_get_mpdata(vnode_mount(vp));
285 if (fuse_isdeadfs(vp)) {
286 if (vnode_isvroot(vp)) {
291 if (!(data->dataflags & FSESS_INITED)) {
292 if (vnode_isvroot(vp)) {
293 if (priv_check_cred(cred, PRIV_VFS_ADMIN) ||
294 (fuse_match_cred(data->daemoncred, cred) == 0)) {
300 if (vnode_islnk(vp)) {
303 bzero(&facp, sizeof(facp));
305 err = fuse_internal_access(vp, accmode, &facp, ap->a_td, ap->a_cred);
310 struct vop_close_args {
313 struct ucred *a_cred;
318 fuse_vnop_close(struct vop_close_args *ap)
320 struct vnode *vp = ap->a_vp;
321 struct ucred *cred = ap->a_cred;
322 int fflag = ap->a_fflag;
323 struct thread *td = ap->a_td;
324 pid_t pid = td->td_proc->p_pid;
327 if (fuse_isdeadfs(vp))
331 if (fflag & IO_NDELAY)
334 err = fuse_flush(vp, cred, pid, fflag);
335 /* TODO: close the file handle, if we're sure it's no longer used */
336 if ((VTOFUD(vp)->flag & FN_SIZECHANGE) != 0) {
337 fuse_vnode_savesize(vp, cred, td->td_proc->p_pid);
343 fdisp_make_mknod_for_fallback(
344 struct fuse_dispatcher *fdip,
345 struct componentname *cnp,
351 enum fuse_opcode *op)
353 struct fuse_mknod_in *fmni;
355 fdisp_init(fdip, sizeof(*fmni) + cnp->cn_namelen + 1);
357 fdisp_make(fdip, *op, vnode_mount(dvp), parentnid, td, cred);
361 memcpy((char *)fdip->indata + sizeof(*fmni), cnp->cn_nameptr,
363 ((char *)fdip->indata)[sizeof(*fmni) + cnp->cn_namelen] = '\0';
366 struct vnop_create_args {
368 struct vnode **a_vpp;
369 struct componentname *a_cnp;
374 fuse_vnop_create(struct vop_create_args *ap)
376 struct vnode *dvp = ap->a_dvp;
377 struct vnode **vpp = ap->a_vpp;
378 struct componentname *cnp = ap->a_cnp;
379 struct vattr *vap = ap->a_vap;
380 struct thread *td = cnp->cn_thread;
381 struct ucred *cred = cnp->cn_cred;
383 struct fuse_open_in *foi;
384 struct fuse_entry_out *feo;
385 struct fuse_open_out *foo;
386 struct fuse_dispatcher fdi, fdi2;
387 struct fuse_dispatcher *fdip = &fdi;
388 struct fuse_dispatcher *fdip2 = NULL;
392 struct mount *mp = vnode_mount(dvp);
393 uint64_t parentnid = VTOFUD(dvp)->nid;
394 mode_t mode = MAKEIMODE(vap->va_type, vap->va_mode);
399 * VOP_CREATE doesn't tell us the open(2) flags, so we guess. Only a
400 * writable mode makes sense, and we might as well include readability
405 if (fuse_isdeadfs(dvp)) {
408 bzero(&fdi, sizeof(fdi));
410 if ((vap->va_type != VREG))
413 if (!fsess_isimpl(mp, FUSE_CREATE)) {
414 /* Fallback to FUSE_MKNOD/FUSE_OPEN */
415 fdisp_make_mknod_for_fallback(fdip, cnp, dvp, parentnid, td,
418 /* Use FUSE_CREATE */
420 fdisp_init(fdip, sizeof(*foi) + cnp->cn_namelen + 1);
421 fdisp_make(fdip, op, vnode_mount(dvp), parentnid, td, cred);
424 foi->flags = O_CREAT | flags;
425 memcpy((char *)fdip->indata + sizeof(*foi), cnp->cn_nameptr,
427 ((char *)fdip->indata)[sizeof(*foi) + cnp->cn_namelen] = '\0';
430 err = fdisp_wait_answ(fdip);
433 if (err == ENOSYS && op == FUSE_CREATE) {
434 fsess_set_notimpl(mp, FUSE_CREATE);
435 fdisp_make_mknod_for_fallback(fdip, cnp, dvp,
436 parentnid, td, cred, mode, &op);
437 err = fdisp_wait_answ(fdip);
445 if ((err = fuse_internal_checkentry(feo, VREG))) {
449 if (op == FUSE_CREATE) {
450 foo = (struct fuse_open_out*)(feo + 1);
452 /* Issue a separate FUSE_OPEN */
454 fdisp_init(fdip2, sizeof(*foi));
455 fdisp_make(fdip2, FUSE_OPEN, vnode_mount(dvp), feo->nodeid, td,
460 err = fdisp_wait_answ(fdip2);
465 err = fuse_vnode_get(mp, feo, feo->nodeid, dvp, vpp, cnp, VREG);
467 struct fuse_release_in *fri;
468 uint64_t nodeid = feo->nodeid;
469 uint64_t fh_id = foo->fh;
471 fdisp_init(fdip, sizeof(*fri));
472 fdisp_make(fdip, FUSE_RELEASE, mp, nodeid, td, cred);
476 fuse_insert_callback(fdip->tick, fuse_internal_forget_callback);
477 fuse_insert_message(fdip->tick);
480 ASSERT_VOP_ELOCKED(*vpp, "fuse_vnop_create");
482 fuse_filehandle_init(*vpp, FUFH_RDWR, NULL, td->td_proc->p_pid, cred,
484 fuse_vnode_open(*vpp, foo->open_flags, td);
485 cache_purge_negative(dvp);
489 fdisp_destroy(fdip2);
495 struct vnop_fdatasync_args {
496 struct vop_generic_args a_gen;
498 struct thread * a_td;
502 fuse_vnop_fdatasync(struct vop_fdatasync_args *ap)
504 struct vnode *vp = ap->a_vp;
505 struct thread *td = ap->a_td;
506 int waitfor = MNT_WAIT;
510 if (fuse_isdeadfs(vp)) {
513 if ((err = vop_stdfdatasync_buf(ap)))
516 return fuse_internal_fsync(vp, td, waitfor, true);
520 struct vnop_fsync_args {
521 struct vop_generic_args a_gen;
524 struct thread * a_td;
528 fuse_vnop_fsync(struct vop_fsync_args *ap)
530 struct vnode *vp = ap->a_vp;
531 struct thread *td = ap->a_td;
532 int waitfor = ap->a_waitfor;
535 if (fuse_isdeadfs(vp)) {
538 if ((err = vop_stdfsync(ap)))
541 return fuse_internal_fsync(vp, td, waitfor, false);
545 struct vnop_getattr_args {
548 struct ucred *a_cred;
553 fuse_vnop_getattr(struct vop_getattr_args *ap)
555 struct vnode *vp = ap->a_vp;
556 struct vattr *vap = ap->a_vap;
557 struct ucred *cred = ap->a_cred;
558 struct thread *td = curthread;
559 struct fuse_vnode_data *fvdat = VTOFUD(vp);
560 struct fuse_attr_out *fao;
564 struct fuse_dispatcher fdi;
566 dataflags = fuse_get_mpdata(vnode_mount(vp))->dataflags;
568 /* Note that we are not bailing out on a dead file system just yet. */
570 if (!(dataflags & FSESS_INITED)) {
571 if (!vnode_isvroot(vp)) {
572 fdata_set_dead(fuse_get_mpdata(vnode_mount(vp)));
580 if ((err = fdisp_simple_putget_vp(&fdi, FUSE_GETATTR, vp, td, cred))) {
581 if ((err == ENOTCONN) && vnode_isvroot(vp)) {
582 /* see comment in fuse_vfsop_statfs() */
587 fuse_internal_vnode_disappear(vp);
592 fao = (struct fuse_attr_out *)fdi.answ;
593 fuse_internal_cache_attrs(vp, &fao->attr, fao->attr_valid,
594 fao->attr_valid_nsec, vap);
595 if (vap->va_type != vnode_vtype(vp)) {
596 fuse_internal_vnode_disappear(vp);
600 if ((fvdat->flag & FN_SIZECHANGE) != 0)
601 vap->va_size = fvdat->filesize;
603 if (vnode_isreg(vp) && (fvdat->flag & FN_SIZECHANGE) == 0) {
605 * This is for those cases when the file size changed without us
606 * knowing, and we want to catch up.
608 off_t new_filesize = ((struct fuse_attr_out *)
609 fdi.answ)->attr.size;
611 if (fvdat->filesize != new_filesize) {
612 fuse_vnode_setsize(vp, cred, new_filesize);
613 fvdat->flag &= ~FN_SIZECHANGE;
622 bzero(vap, sizeof(*vap));
623 vap->va_type = vnode_vtype(vp);
629 struct vnop_inactive_args {
635 fuse_vnop_inactive(struct vop_inactive_args *ap)
637 struct vnode *vp = ap->a_vp;
638 struct thread *td = ap->a_td;
640 struct fuse_vnode_data *fvdat = VTOFUD(vp);
641 struct fuse_filehandle *fufh, *fufh_tmp;
645 LIST_FOREACH_SAFE(fufh, &fvdat->handles, next, fufh_tmp) {
646 if (need_flush && vp->v_type == VREG) {
647 if ((VTOFUD(vp)->flag & FN_SIZECHANGE) != 0) {
648 fuse_vnode_savesize(vp, NULL, 0);
650 if (fuse_data_cache_invalidate ||
651 (fvdat->flag & FN_REVOKED) != 0)
652 fuse_io_invalbuf(vp, td);
654 fuse_io_flushbuf(vp, MNT_WAIT, td);
657 fuse_filehandle_close(vp, fufh, td, NULL);
660 if ((fvdat->flag & FN_REVOKED) != 0 && fuse_reclaim_revoked) {
667 struct vnop_link_args {
668 struct vnode *a_tdvp;
670 struct componentname *a_cnp;
674 fuse_vnop_link(struct vop_link_args *ap)
676 struct vnode *vp = ap->a_vp;
677 struct vnode *tdvp = ap->a_tdvp;
678 struct componentname *cnp = ap->a_cnp;
680 struct vattr *vap = VTOVA(vp);
682 struct fuse_dispatcher fdi;
683 struct fuse_entry_out *feo;
684 struct fuse_link_in fli;
688 if (fuse_isdeadfs(vp)) {
691 if (vnode_mount(tdvp) != vnode_mount(vp)) {
696 * This is a seatbelt check to protect naive userspace filesystems from
697 * themselves and the limitations of the FUSE IPC protocol. If a
698 * filesystem does not allow attribute caching, assume it is capable of
699 * validating that nlink does not overflow.
701 if (vap != NULL && vap->va_nlink >= FUSE_LINK_MAX)
703 fli.oldnodeid = VTOI(vp);
706 fuse_internal_newentry_makerequest(vnode_mount(tdvp), VTOI(tdvp), cnp,
707 FUSE_LINK, &fli, sizeof(fli), &fdi);
708 if ((err = fdisp_wait_answ(&fdi))) {
713 err = fuse_internal_checkentry(feo, vnode_vtype(vp));
720 struct vnop_lookup_args {
721 struct vnodeop_desc *a_desc;
723 struct vnode **a_vpp;
724 struct componentname *a_cnp;
728 fuse_vnop_lookup(struct vop_lookup_args *ap)
730 struct vnode *dvp = ap->a_dvp;
731 struct vnode **vpp = ap->a_vpp;
732 struct componentname *cnp = ap->a_cnp;
733 struct thread *td = cnp->cn_thread;
734 struct ucred *cred = cnp->cn_cred;
736 int nameiop = cnp->cn_nameiop;
737 int flags = cnp->cn_flags;
738 int wantparent = flags & (LOCKPARENT | WANTPARENT);
739 int islastcn = flags & ISLASTCN;
740 struct mount *mp = vnode_mount(dvp);
744 struct vnode *vp = NULL;
746 struct fuse_dispatcher fdi;
750 struct fuse_access_param facp;
752 if (fuse_isdeadfs(dvp)) {
756 if (!vnode_isdir(dvp)) {
759 if (islastcn && vfs_isrdonly(mp) && (nameiop != LOOKUP)) {
763 * We do access check prior to doing anything else only in the case
764 * when we are at fs root (we'd like to say, "we are at the first
765 * component", but that's not exactly the same... nevermind).
766 * See further comments at further access checks.
769 bzero(&facp, sizeof(facp));
770 if (vnode_isvroot(dvp)) { /* early permission check hack */
771 if ((err = fuse_internal_access(dvp, VEXEC, &facp, td, cred))) {
775 if (flags & ISDOTDOT) {
776 nid = VTOFUD(dvp)->parent_nid;
783 } else if (cnp->cn_namelen == 1 && *(cnp->cn_nameptr) == '.') {
788 } else if (fuse_lookup_cache_enable) {
789 err = cache_lookup(dvp, vpp, cnp, NULL, NULL);
792 case -1: /* positive match */
793 atomic_add_acq_long(&fuse_lookup_cache_hits, 1);
796 case 0: /* no match in cache */
797 atomic_add_acq_long(&fuse_lookup_cache_misses, 1);
800 case ENOENT: /* negative match */
807 fdisp_init(&fdi, cnp->cn_namelen + 1);
811 fdisp_make(&fdi, op, mp, nid, td, cred);
813 if (op == FUSE_LOOKUP) {
814 memcpy(fdi.indata, cnp->cn_nameptr, cnp->cn_namelen);
815 ((char *)fdi.indata)[cnp->cn_namelen] = '\0';
817 lookup_err = fdisp_wait_answ(&fdi);
819 if ((op == FUSE_LOOKUP) && !lookup_err) { /* lookup call succeeded */
820 nid = ((struct fuse_entry_out *)fdi.answ)->nodeid;
823 * zero nodeid is the same as "not found",
824 * but it's also cacheable (which we keep
825 * keep on doing not as of writing this)
827 fdi.answ_stat = ENOENT;
829 } else if (nid == FUSE_ROOT_ID) {
834 (!fdi.answ_stat || lookup_err != ENOENT || op != FUSE_LOOKUP)) {
838 /* lookup_err, if non-zero, must be ENOENT at this point */
842 if ((nameiop == CREATE || nameiop == RENAME) && islastcn
843 /* && directory dvp has not been removed */ ) {
845 if (vfs_isrdonly(mp)) {
849 #if 0 /* THINK_ABOUT_THIS */
850 if ((err = fuse_internal_access(dvp, VWRITE, cred, td, &facp))) {
856 * Possibly record the position of a slot in the
857 * directory large enough for the new component name.
858 * This can be recorded in the vnode private data for
859 * dvp. Set the SAVENAME flag to hold onto the
860 * pathname for use later in VOP_CREATE or VOP_RENAME.
862 cnp->cn_flags |= SAVENAME;
867 /* Consider inserting name into cache. */
870 * No we can't use negative caching, as the fs
871 * changes are out of our control.
872 * False positives' falseness turns out just as things
873 * go by, but false negatives' falseness doesn't.
874 * (and aiding the caching mechanism with extra control
875 * mechanisms comes quite close to beating the whole purpose
879 if ((cnp->cn_flags & MAKEENTRY) != 0) {
880 SDT_PROBE2(fuse, , vnops, trace, 1,
881 "inserting NULL into cache");
882 cache_enter(dvp, NULL, cnp);
892 struct fuse_entry_out *feo = NULL;
893 struct fuse_attr *fattr = NULL;
895 if (op == FUSE_GETATTR) {
896 fattr = &((struct fuse_attr_out *)fdi.answ)->attr;
898 feo = (struct fuse_entry_out *)fdi.answ;
899 fattr = &(feo->attr);
903 * If deleting, and at end of pathname, return parameters
904 * which can be used to remove file. If the wantparent flag
905 * isn't set, we return only the directory, otherwise we go on
906 * and lock the inode, being careful with ".".
908 if (nameiop == DELETE && islastcn) {
910 * Check for write access on directory.
912 facp.xuid = fattr->uid;
913 facp.facc_flags |= FACCESS_STICKY;
914 err = fuse_internal_access(dvp, VWRITE, &facp, td, cred);
915 facp.facc_flags &= ~FACCESS_XQUERIES;
920 if (nid == VTOI(dvp)) {
924 err = fuse_vnode_get(dvp->v_mount, feo, nid,
925 dvp, &vp, cnp, IFTOVT(fattr->mode));
932 * Save the name for use in VOP_RMDIR and VOP_REMOVE
935 cnp->cn_flags |= SAVENAME;
940 * If rewriting (RENAME), return the inode and the
941 * information required to rewrite the present directory
942 * Must get inode of directory entry to verify it's a
943 * regular file, or empty directory.
945 if (nameiop == RENAME && wantparent && islastcn) {
947 #if 0 /* THINK_ABOUT_THIS */
948 if ((err = fuse_internal_access(dvp, VWRITE, cred, td, &facp))) {
956 if (nid == VTOI(dvp)) {
960 err = fuse_vnode_get(vnode_mount(dvp), feo, nid, dvp,
961 &vp, cnp, IFTOVT(fattr->mode));
967 * Save the name for use in VOP_RENAME later.
969 cnp->cn_flags |= SAVENAME;
973 if (flags & ISDOTDOT) {
978 * Expanded copy of vn_vget_ino() so that
979 * fuse_vnode_get() can be used.
982 ltype = VOP_ISLOCKED(dvp);
983 err = vfs_busy(mp, MBF_NOWAIT);
987 err = vfs_busy(mp, 0);
988 vn_lock(dvp, ltype | LK_RETRY);
992 if ((dvp->v_iflag & VI_DOOMED) != 0) {
999 err = fuse_vnode_get(vnode_mount(dvp), feo, nid, NULL,
1000 &vp, cnp, IFTOVT(fattr->mode));
1002 vn_lock(dvp, ltype | LK_RETRY);
1003 if ((dvp->v_iflag & VI_DOOMED) != 0) {
1011 } else if (nid == VTOI(dvp)) {
1015 struct fuse_vnode_data *fvdat;
1017 err = fuse_vnode_get(vnode_mount(dvp), feo, nid, dvp,
1018 &vp, cnp, IFTOVT(fattr->mode));
1022 fuse_vnode_setparent(vp, dvp);
1025 * In the case where we are looking up a FUSE node
1026 * represented by an existing cached vnode, and the
1027 * true size reported by FUSE_LOOKUP doesn't match
1028 * the vnode's cached size, fix the vnode cache to
1029 * match the real object size.
1031 * This can occur via FUSE distributed filesystems,
1032 * irregular files, etc.
1035 if (vnode_isreg(vp) &&
1036 fattr->size != fvdat->filesize) {
1038 * The FN_SIZECHANGE flag reflects a dirty
1039 * append. If userspace lets us know our cache
1040 * is invalid, that write was lost. (Dirty
1041 * writes that do not cause append are also
1042 * lost, but we don't detect them here.)
1044 * XXX: Maybe disable WB caching on this mount.
1046 if (fvdat->flag & FN_SIZECHANGE)
1047 printf("%s: WB cache incoherent on "
1049 vnode_mount(vp)->mnt_stat.f_mntonname);
1051 (void)fuse_vnode_setsize(vp, cred, fattr->size);
1052 fvdat->flag &= ~FN_SIZECHANGE;
1057 if (op == FUSE_GETATTR) {
1058 struct fuse_attr_out *fao =
1059 (struct fuse_attr_out*)fdi.answ;
1060 fuse_internal_cache_attrs(*vpp,
1061 &fao->attr, fao->attr_valid,
1062 fao->attr_valid_nsec, NULL);
1064 struct fuse_entry_out *feo =
1065 (struct fuse_entry_out*)fdi.answ;
1066 fuse_internal_cache_attrs(*vpp,
1067 &feo->attr, feo->attr_valid,
1068 feo->attr_valid_nsec, NULL);
1071 /* Insert name into cache if appropriate. */
1074 * Nooo, caching is evil. With caching, we can't avoid stale
1075 * information taking over the playground (cached info is not
1076 * just positive/negative, it does have qualitative aspects,
1077 * too). And a (VOP/FUSE)_GETATTR is always thrown anyway, when
1078 * walking down along cached path components, and that's not
1079 * any cheaper than FUSE_LOOKUP. This might change with
1080 * implementing kernel side attr caching, but... In Linux,
1081 * lookup results are not cached, and the daemon is bombarded
1082 * with FUSE_LOOKUPS on and on. This shows that by design, the
1083 * daemon is expected to handle frequent lookup queries
1084 * efficiently, do its caching in userspace, and so on.
1086 * So just leave the name cache alone.
1090 * Well, now I know, Linux caches lookups, but with a
1091 * timeout... So it's the same thing as attribute caching:
1092 * we can deal with it when implement timeouts.
1095 if (cnp->cn_flags & MAKEENTRY) {
1096 cache_enter(dvp, *vpp, cnp);
1103 /* No lookup error; need to clean up. */
1105 if (err) { /* Found inode; exit with no vnode. */
1106 if (op == FUSE_LOOKUP) {
1107 fuse_internal_forget_send(vnode_mount(dvp), td, cred,
1110 fdisp_destroy(&fdi);
1113 #ifndef NO_EARLY_PERM_CHECK_HACK
1116 * We have the attributes of the next item
1117 * *now*, and it's a fact, and we do not
1118 * have to do extra work for it (ie, beg the
1119 * daemon), and it neither depends on such
1120 * accidental things like attr caching. So
1121 * the big idea: check credentials *now*,
1122 * not at the beginning of the next call to
1125 * The first item of the lookup chain (fs root)
1126 * won't be checked then here, of course, as
1127 * its never "the next". But go and see that
1128 * the root is taken care about at the very
1129 * beginning of this function.
1131 * Now, given we want to do the access check
1132 * this way, one might ask: so then why not
1133 * do the access check just after fetching
1134 * the inode and its attributes from the
1135 * daemon? Why bother with producing the
1136 * corresponding vnode at all if something
1137 * is not OK? We know what's the deal as
1138 * soon as we get those attrs... There is
1139 * one bit of info though not given us by
1140 * the daemon: whether his response is
1141 * authoritative or not... His response should
1142 * be ignored if something is mounted over
1143 * the dir in question. But that can be
1144 * known only by having the vnode...
1146 int tmpvtype = vnode_vtype(*vpp);
1148 bzero(&facp, sizeof(facp));
1149 /*the early perm check hack */
1150 facp.facc_flags |= FACCESS_VA_VALID;
1152 if ((tmpvtype != VDIR) && (tmpvtype != VLNK)) {
1155 if (!err && !vnode_mountedhere(*vpp)) {
1156 err = fuse_internal_access(*vpp, VEXEC, &facp, td, cred);
1159 if (tmpvtype == VLNK)
1160 SDT_PROBE2(fuse, , vnops, trace,
1161 1, "weird, permission "
1162 "error with a symlink?");
1170 fdisp_destroy(&fdi);
1176 struct vnop_mkdir_args {
1177 struct vnode *a_dvp;
1178 struct vnode **a_vpp;
1179 struct componentname *a_cnp;
1180 struct vattr *a_vap;
1184 fuse_vnop_mkdir(struct vop_mkdir_args *ap)
1186 struct vnode *dvp = ap->a_dvp;
1187 struct vnode **vpp = ap->a_vpp;
1188 struct componentname *cnp = ap->a_cnp;
1189 struct vattr *vap = ap->a_vap;
1191 struct fuse_mkdir_in fmdi;
1193 if (fuse_isdeadfs(dvp)) {
1196 fmdi.mode = MAKEIMODE(vap->va_type, vap->va_mode);
1198 return (fuse_internal_newentry(dvp, vpp, cnp, FUSE_MKDIR, &fmdi,
1199 sizeof(fmdi), VDIR));
1203 struct vnop_mknod_args {
1204 struct vnode *a_dvp;
1205 struct vnode **a_vpp;
1206 struct componentname *a_cnp;
1207 struct vattr *a_vap;
1211 fuse_vnop_mknod(struct vop_mknod_args *ap)
1214 struct vnode *dvp = ap->a_dvp;
1215 struct vnode **vpp = ap->a_vpp;
1216 struct componentname *cnp = ap->a_cnp;
1217 struct vattr *vap = ap->a_vap;
1218 struct fuse_mknod_in fmni;
1220 if (fuse_isdeadfs(dvp))
1223 fmni.mode = MAKEIMODE(vap->va_type, vap->va_mode);
1224 fmni.rdev = vap->va_rdev;
1225 return (fuse_internal_newentry(dvp, vpp, cnp, FUSE_MKNOD, &fmni,
1226 sizeof(fmni), vap->va_type));
1230 struct vnop_open_args {
1233 struct ucred *a_cred;
1234 struct thread *a_td;
1235 int a_fdidx; / struct file *a_fp;
1239 fuse_vnop_open(struct vop_open_args *ap)
1241 struct vnode *vp = ap->a_vp;
1242 int a_mode = ap->a_mode;
1243 struct thread *td = ap->a_td;
1244 struct ucred *cred = ap->a_cred;
1245 pid_t pid = td->td_proc->p_pid;
1246 struct fuse_vnode_data *fvdat;
1248 if (fuse_isdeadfs(vp))
1250 if (vp->v_type == VCHR || vp->v_type == VBLK || vp->v_type == VFIFO)
1251 return (EOPNOTSUPP);
1252 if ((a_mode & (FREAD | FWRITE | FEXEC)) == 0)
1257 if (fuse_filehandle_validrw(vp, a_mode, cred, pid)) {
1258 fuse_vnode_open(vp, 0, td);
1262 return fuse_filehandle_open(vp, a_mode, NULL, td, cred);
1266 fuse_vnop_pathconf(struct vop_pathconf_args *ap)
1269 switch (ap->a_name) {
1270 case _PC_FILESIZEBITS:
1274 *ap->a_retval = NAME_MAX;
1277 *ap->a_retval = MIN(LONG_MAX, FUSE_LINK_MAX);
1279 case _PC_SYMLINK_MAX:
1280 *ap->a_retval = MAXPATHLEN;
1286 return (vop_stdpathconf(ap));
1291 struct vnop_read_args {
1295 struct ucred *a_cred;
1299 fuse_vnop_read(struct vop_read_args *ap)
1301 struct vnode *vp = ap->a_vp;
1302 struct uio *uio = ap->a_uio;
1303 int ioflag = ap->a_ioflag;
1304 struct ucred *cred = ap->a_cred;
1305 pid_t pid = curthread->td_proc->p_pid;
1307 if (fuse_isdeadfs(vp)) {
1311 if (VTOFUD(vp)->flag & FN_DIRECTIO) {
1312 ioflag |= IO_DIRECT;
1315 return fuse_io_dispatch(vp, uio, ioflag, cred, pid);
1319 struct vnop_readdir_args {
1322 struct ucred *a_cred;
1329 fuse_vnop_readdir(struct vop_readdir_args *ap)
1331 struct vnode *vp = ap->a_vp;
1332 struct uio *uio = ap->a_uio;
1333 struct ucred *cred = ap->a_cred;
1334 struct fuse_filehandle *fufh = NULL;
1335 struct fuse_iov cookediov;
1337 pid_t pid = curthread->td_proc->p_pid;
1339 if (fuse_isdeadfs(vp)) {
1342 if ( /* XXXIP ((uio_iovcnt(uio) > 1)) || */
1343 (uio_resid(uio) < sizeof(struct dirent))) {
1347 err = fuse_filehandle_get_dir(vp, &fufh, cred, pid);
1350 #define DIRCOOKEDSIZE FUSE_DIRENT_ALIGN(FUSE_NAME_OFFSET + MAXNAMLEN + 1)
1351 fiov_init(&cookediov, DIRCOOKEDSIZE);
1353 err = fuse_internal_readdir(vp, uio, fufh, &cookediov);
1355 fiov_teardown(&cookediov);
1361 struct vnop_readlink_args {
1364 struct ucred *a_cred;
1368 fuse_vnop_readlink(struct vop_readlink_args *ap)
1370 struct vnode *vp = ap->a_vp;
1371 struct uio *uio = ap->a_uio;
1372 struct ucred *cred = ap->a_cred;
1374 struct fuse_dispatcher fdi;
1377 if (fuse_isdeadfs(vp)) {
1380 if (!vnode_islnk(vp)) {
1383 fdisp_init(&fdi, 0);
1384 err = fdisp_simple_putget_vp(&fdi, FUSE_READLINK, vp, curthread, cred);
1388 if (((char *)fdi.answ)[0] == '/' &&
1389 fuse_get_mpdata(vnode_mount(vp))->dataflags & FSESS_PUSH_SYMLINKS_IN) {
1390 char *mpth = vnode_mount(vp)->mnt_stat.f_mntonname;
1392 err = uiomove(mpth, strlen(mpth), uio);
1395 err = uiomove(fdi.answ, fdi.iosize, uio);
1398 fdisp_destroy(&fdi);
1403 struct vnop_reclaim_args {
1405 struct thread *a_td;
1409 fuse_vnop_reclaim(struct vop_reclaim_args *ap)
1411 struct vnode *vp = ap->a_vp;
1412 struct thread *td = ap->a_td;
1413 struct fuse_vnode_data *fvdat = VTOFUD(vp);
1414 struct fuse_filehandle *fufh, *fufh_tmp;
1417 panic("FUSE: no vnode data during recycling");
1419 LIST_FOREACH_SAFE(fufh, &fvdat->handles, next, fufh_tmp) {
1420 printf("FUSE: vnode being reclaimed with open fufh "
1421 "(type=%#x)", fufh->fufh_type);
1422 fuse_filehandle_close(vp, fufh, td, NULL);
1425 if ((!fuse_isdeadfs(vp)) && (fvdat->nlookup)) {
1426 fuse_internal_forget_send(vnode_mount(vp), td, NULL, VTOI(vp),
1429 fuse_vnode_setparent(vp, NULL);
1431 vfs_hash_remove(vp);
1432 vnode_destroy_vobject(vp);
1433 fuse_vnode_destroy(vp);
1439 struct vnop_remove_args {
1440 struct vnode *a_dvp;
1442 struct componentname *a_cnp;
1446 fuse_vnop_remove(struct vop_remove_args *ap)
1448 struct vnode *dvp = ap->a_dvp;
1449 struct vnode *vp = ap->a_vp;
1450 struct componentname *cnp = ap->a_cnp;
1454 if (fuse_isdeadfs(vp)) {
1457 if (vnode_isdir(vp)) {
1462 err = fuse_internal_remove(dvp, vp, cnp, FUSE_UNLINK);
1465 fuse_internal_vnode_disappear(vp);
1470 struct vnop_rename_args {
1471 struct vnode *a_fdvp;
1472 struct vnode *a_fvp;
1473 struct componentname *a_fcnp;
1474 struct vnode *a_tdvp;
1475 struct vnode *a_tvp;
1476 struct componentname *a_tcnp;
1480 fuse_vnop_rename(struct vop_rename_args *ap)
1482 struct vnode *fdvp = ap->a_fdvp;
1483 struct vnode *fvp = ap->a_fvp;
1484 struct componentname *fcnp = ap->a_fcnp;
1485 struct vnode *tdvp = ap->a_tdvp;
1486 struct vnode *tvp = ap->a_tvp;
1487 struct componentname *tcnp = ap->a_tcnp;
1488 struct fuse_data *data;
1492 if (fuse_isdeadfs(fdvp)) {
1495 if (fvp->v_mount != tdvp->v_mount ||
1496 (tvp && fvp->v_mount != tvp->v_mount)) {
1497 SDT_PROBE2(fuse, , vnops, trace, 1, "cross-device rename");
1504 * FUSE library is expected to check if target directory is not
1505 * under the source directory in the file system tree.
1506 * Linux performs this check at VFS level.
1508 data = fuse_get_mpdata(vnode_mount(tdvp));
1509 sx_xlock(&data->rename_lock);
1510 err = fuse_internal_rename(fdvp, fcnp, tdvp, tcnp);
1513 fuse_vnode_setparent(fvp, tdvp);
1515 fuse_vnode_setparent(tvp, NULL);
1517 sx_unlock(&data->rename_lock);
1519 if (tvp != NULL && tvp != fvp) {
1522 if (vnode_isdir(fvp)) {
1523 if ((tvp != NULL) && vnode_isdir(tvp)) {
1544 struct vnop_rmdir_args {
1545 struct vnode *a_dvp;
1547 struct componentname *a_cnp;
1551 fuse_vnop_rmdir(struct vop_rmdir_args *ap)
1553 struct vnode *dvp = ap->a_dvp;
1554 struct vnode *vp = ap->a_vp;
1558 if (fuse_isdeadfs(vp)) {
1561 if (VTOFUD(vp) == VTOFUD(dvp)) {
1564 err = fuse_internal_remove(dvp, vp, ap->a_cnp, FUSE_RMDIR);
1567 fuse_internal_vnode_disappear(vp);
1572 struct vnop_setattr_args {
1574 struct vattr *a_vap;
1575 struct ucred *a_cred;
1576 struct thread *a_td;
1580 fuse_vnop_setattr(struct vop_setattr_args *ap)
1582 struct vnode *vp = ap->a_vp;
1583 struct vattr *vap = ap->a_vap;
1584 struct ucred *cred = ap->a_cred;
1585 struct thread *td = curthread;
1586 struct fuse_dispatcher fdi;
1587 struct fuse_setattr_in *fsai;
1588 struct fuse_access_param facp;
1589 pid_t pid = td->td_proc->p_pid;
1593 int sizechanged = 0;
1594 uint64_t newsize = 0;
1596 if (fuse_isdeadfs(vp)) {
1599 fdisp_init(&fdi, sizeof(*fsai));
1600 fdisp_make_vp(&fdi, FUSE_SETATTR, vp, td, cred);
1604 bzero(&facp, sizeof(facp));
1606 facp.xuid = vap->va_uid;
1607 facp.xgid = vap->va_gid;
1609 if (vap->va_uid != (uid_t)VNOVAL) {
1610 facp.facc_flags |= FACCESS_CHOWN;
1611 fsai->uid = vap->va_uid;
1612 fsai->valid |= FATTR_UID;
1614 if (vap->va_gid != (gid_t)VNOVAL) {
1615 facp.facc_flags |= FACCESS_CHOWN;
1616 fsai->gid = vap->va_gid;
1617 fsai->valid |= FATTR_GID;
1619 if (vap->va_size != VNOVAL) {
1621 struct fuse_filehandle *fufh = NULL;
1623 /*Truncate to a new value. */
1624 fsai->size = vap->va_size;
1626 newsize = vap->va_size;
1627 fsai->valid |= FATTR_SIZE;
1629 fuse_filehandle_getrw(vp, FWRITE, &fufh, cred, pid);
1631 fsai->fh = fufh->fh_id;
1632 fsai->valid |= FATTR_FH;
1635 if (vap->va_atime.tv_sec != VNOVAL) {
1636 fsai->atime = vap->va_atime.tv_sec;
1637 fsai->atimensec = vap->va_atime.tv_nsec;
1638 fsai->valid |= FATTR_ATIME;
1640 if (vap->va_mtime.tv_sec != VNOVAL) {
1641 fsai->mtime = vap->va_mtime.tv_sec;
1642 fsai->mtimensec = vap->va_mtime.tv_nsec;
1643 fsai->valid |= FATTR_MTIME;
1645 if (vap->va_mode != (mode_t)VNOVAL) {
1646 fsai->mode = vap->va_mode & ALLPERMS;
1647 fsai->valid |= FATTR_MODE;
1652 vtyp = vnode_vtype(vp);
1654 if (fsai->valid & FATTR_SIZE && vtyp == VDIR) {
1658 if (vfs_isrdonly(vnode_mount(vp)) && (fsai->valid & ~FATTR_SIZE || vtyp == VREG)) {
1663 if ((err = fdisp_wait_answ(&fdi)))
1665 vtyp = IFTOVT(((struct fuse_attr_out *)fdi.answ)->attr.mode);
1667 if (vnode_vtype(vp) != vtyp) {
1668 if (vnode_vtype(vp) == VNON && vtyp != VNON) {
1669 SDT_PROBE2(fuse, , vnops, trace, 1, "FUSE: Dang! "
1670 "vnode_vtype is VNON and vtype isn't.");
1673 * STALE vnode, ditch
1675 * The vnode has changed its type "behind our back".
1676 * There's nothing really we can do, so let us just
1677 * force an internal revocation and tell the caller to
1678 * try again, if interested.
1680 fuse_internal_vnode_disappear(vp);
1685 struct fuse_attr_out *fao = (struct fuse_attr_out*)fdi.answ;
1686 fuse_internal_cache_attrs(vp, &fao->attr, fao->attr_valid,
1687 fao->attr_valid_nsec, NULL);
1691 fdisp_destroy(&fdi);
1692 if (!err && sizechanged) {
1693 fuse_vnode_setsize(vp, cred, newsize);
1694 VTOFUD(vp)->flag &= ~FN_SIZECHANGE;
1700 struct vnop_strategy_args {
1706 fuse_vnop_strategy(struct vop_strategy_args *ap)
1708 struct vnode *vp = ap->a_vp;
1709 struct buf *bp = ap->a_bp;
1711 if (!vp || fuse_isdeadfs(vp)) {
1712 bp->b_ioflags |= BIO_ERROR;
1713 bp->b_error = ENXIO;
1717 if (bp->b_iocmd == BIO_WRITE) {
1720 err = fuse_vnode_refreshsize(vp, NOCRED);
1722 bp->b_ioflags |= BIO_ERROR;
1730 * VOP_STRATEGY always returns zero and signals error via bp->b_ioflags.
1731 * fuse_io_strategy sets bp's error fields
1733 (void)fuse_io_strategy(vp, bp);
1740 struct vnop_symlink_args {
1741 struct vnode *a_dvp;
1742 struct vnode **a_vpp;
1743 struct componentname *a_cnp;
1744 struct vattr *a_vap;
1749 fuse_vnop_symlink(struct vop_symlink_args *ap)
1751 struct vnode *dvp = ap->a_dvp;
1752 struct vnode **vpp = ap->a_vpp;
1753 struct componentname *cnp = ap->a_cnp;
1754 const char *target = ap->a_target;
1756 struct fuse_dispatcher fdi;
1761 if (fuse_isdeadfs(dvp)) {
1765 * Unlike the other creator type calls, here we have to create a message
1766 * where the name of the new entry comes first, and the data describing
1767 * the entry comes second.
1768 * Hence we can't rely on our handy fuse_internal_newentry() routine,
1769 * but put together the message manually and just call the core part.
1772 len = strlen(target) + 1;
1773 fdisp_init(&fdi, len + cnp->cn_namelen + 1);
1774 fdisp_make_vp(&fdi, FUSE_SYMLINK, dvp, curthread, NULL);
1776 memcpy(fdi.indata, cnp->cn_nameptr, cnp->cn_namelen);
1777 ((char *)fdi.indata)[cnp->cn_namelen] = '\0';
1778 memcpy((char *)fdi.indata + cnp->cn_namelen + 1, target, len);
1780 err = fuse_internal_newentry_core(dvp, vpp, cnp, VLNK, &fdi);
1781 fdisp_destroy(&fdi);
1786 struct vnop_write_args {
1790 struct ucred *a_cred;
1794 fuse_vnop_write(struct vop_write_args *ap)
1796 struct vnode *vp = ap->a_vp;
1797 struct uio *uio = ap->a_uio;
1798 int ioflag = ap->a_ioflag;
1799 struct ucred *cred = ap->a_cred;
1800 pid_t pid = curthread->td_proc->p_pid;
1803 if (fuse_isdeadfs(vp)) {
1806 err = fuse_vnode_refreshsize(vp, cred);
1810 if (VTOFUD(vp)->flag & FN_DIRECTIO) {
1811 ioflag |= IO_DIRECT;
1814 return fuse_io_dispatch(vp, uio, ioflag, cred, pid);
1817 SDT_PROBE_DEFINE1(fuse, , vnops, vnop_getpages_error, "int");
1819 struct vnop_getpages_args {
1827 fuse_vnop_getpages(struct vop_getpages_args *ap)
1829 int i, error, nextoff, size, toff, count, npages;
1838 pid_t pid = curthread->td_proc->p_pid;
1841 KASSERT(vp->v_object, ("objectless vp passed to getpages"));
1842 td = curthread; /* XXX */
1843 cred = curthread->td_ucred; /* XXX */
1845 npages = ap->a_count;
1847 if (!fsess_opt_mmap(vnode_mount(vp))) {
1848 SDT_PROBE2(fuse, , vnops, trace, 1,
1849 "called on non-cacheable vnode??\n");
1850 return (VM_PAGER_ERROR);
1854 * If the last page is partially valid, just return it and allow
1855 * the pager to zero-out the blanks. Partially valid pages can
1856 * only occur at the file EOF.
1858 * XXXGL: is that true for FUSE, which is a local filesystem,
1859 * but still somewhat disconnected from the kernel?
1861 VM_OBJECT_WLOCK(vp->v_object);
1862 if (pages[npages - 1]->valid != 0 && --npages == 0)
1864 VM_OBJECT_WUNLOCK(vp->v_object);
1867 * We use only the kva address for the buffer, but this is extremely
1868 * convenient and fast.
1870 bp = uma_zalloc(fuse_pbuf_zone, M_WAITOK);
1872 kva = (vm_offset_t)bp->b_data;
1873 pmap_qenter(kva, pages, npages);
1874 VM_CNT_INC(v_vnodein);
1875 VM_CNT_ADD(v_vnodepgsin, npages);
1877 count = npages << PAGE_SHIFT;
1878 iov.iov_base = (caddr_t)kva;
1879 iov.iov_len = count;
1882 uio.uio_offset = IDX_TO_OFF(pages[0]->pindex);
1883 uio.uio_resid = count;
1884 uio.uio_segflg = UIO_SYSSPACE;
1885 uio.uio_rw = UIO_READ;
1888 error = fuse_io_dispatch(vp, &uio, IO_DIRECT, cred, pid);
1889 pmap_qremove(kva, npages);
1891 uma_zfree(fuse_pbuf_zone, bp);
1893 if (error && (uio.uio_resid == count)) {
1894 SDT_PROBE1(fuse, , vnops, vnop_getpages_error, error);
1895 return VM_PAGER_ERROR;
1898 * Calculate the number of bytes read and validate only that number
1899 * of bytes. Note that due to pending writes, size may be 0. This
1900 * does not mean that the remaining data is invalid!
1903 size = count - uio.uio_resid;
1904 VM_OBJECT_WLOCK(vp->v_object);
1905 fuse_vm_page_lock_queues();
1906 for (i = 0, toff = 0; i < npages; i++, toff = nextoff) {
1909 nextoff = toff + PAGE_SIZE;
1912 if (nextoff <= size) {
1914 * Read operation filled an entire page
1916 m->valid = VM_PAGE_BITS_ALL;
1917 KASSERT(m->dirty == 0,
1918 ("fuse_getpages: page %p is dirty", m));
1919 } else if (size > toff) {
1921 * Read operation filled a partial page.
1924 vm_page_set_valid_range(m, 0, size - toff);
1925 KASSERT(m->dirty == 0,
1926 ("fuse_getpages: page %p is dirty", m));
1929 * Read operation was short. If no error occurred
1930 * we may have hit a zero-fill section. We simply
1931 * leave valid set to 0.
1936 fuse_vm_page_unlock_queues();
1938 VM_OBJECT_WUNLOCK(vp->v_object);
1943 return (VM_PAGER_OK);
1947 struct vnop_putpages_args {
1953 vm_ooffset_t a_offset;
1957 fuse_vnop_putpages(struct vop_putpages_args *ap)
1963 int i, error, npages, count;
1971 pid_t pid = curthread->td_proc->p_pid;
1974 KASSERT(vp->v_object, ("objectless vp passed to putpages"));
1975 fsize = vp->v_object->un_pager.vnp.vnp_size;
1976 td = curthread; /* XXX */
1977 cred = curthread->td_ucred; /* XXX */
1979 count = ap->a_count;
1980 rtvals = ap->a_rtvals;
1981 npages = btoc(count);
1982 offset = IDX_TO_OFF(pages[0]->pindex);
1984 if (!fsess_opt_mmap(vnode_mount(vp))) {
1985 SDT_PROBE2(fuse, , vnops, trace, 1,
1986 "called on non-cacheable vnode??\n");
1988 for (i = 0; i < npages; i++)
1989 rtvals[i] = VM_PAGER_AGAIN;
1992 * When putting pages, do not extend file past EOF.
1995 if (offset + count > fsize) {
1996 count = fsize - offset;
2001 * We use only the kva address for the buffer, but this is extremely
2002 * convenient and fast.
2004 bp = uma_zalloc(fuse_pbuf_zone, M_WAITOK);
2006 kva = (vm_offset_t)bp->b_data;
2007 pmap_qenter(kva, pages, npages);
2008 VM_CNT_INC(v_vnodeout);
2009 VM_CNT_ADD(v_vnodepgsout, count);
2011 iov.iov_base = (caddr_t)kva;
2012 iov.iov_len = count;
2015 uio.uio_offset = offset;
2016 uio.uio_resid = count;
2017 uio.uio_segflg = UIO_SYSSPACE;
2018 uio.uio_rw = UIO_WRITE;
2021 error = fuse_io_dispatch(vp, &uio, IO_DIRECT, cred, pid);
2023 pmap_qremove(kva, npages);
2024 uma_zfree(fuse_pbuf_zone, bp);
2027 int nwritten = round_page(count - uio.uio_resid) / PAGE_SIZE;
2029 for (i = 0; i < nwritten; i++) {
2030 rtvals[i] = VM_PAGER_OK;
2031 VM_OBJECT_WLOCK(pages[i]->object);
2032 vm_page_undirty(pages[i]);
2033 VM_OBJECT_WUNLOCK(pages[i]->object);
2039 static const char extattr_namespace_separator = '.';
2042 struct vop_getextattr_args {
2043 struct vop_generic_args a_gen;
2045 int a_attrnamespace;
2049 struct ucred *a_cred;
2050 struct thread *a_td;
2054 fuse_vnop_getextattr(struct vop_getextattr_args *ap)
2056 struct vnode *vp = ap->a_vp;
2057 struct uio *uio = ap->a_uio;
2058 struct fuse_dispatcher fdi;
2059 struct fuse_getxattr_in *get_xattr_in;
2060 struct fuse_getxattr_out *get_xattr_out;
2061 struct mount *mp = vnode_mount(vp);
2062 struct thread *td = ap->a_td;
2063 struct ucred *cred = ap->a_cred;
2069 if (fuse_isdeadfs(vp))
2072 /* Default to looking for user attributes. */
2073 if (ap->a_attrnamespace == EXTATTR_NAMESPACE_SYSTEM)
2074 prefix = EXTATTR_NAMESPACE_SYSTEM_STRING;
2076 prefix = EXTATTR_NAMESPACE_USER_STRING;
2078 len = strlen(prefix) + sizeof(extattr_namespace_separator) +
2079 strlen(ap->a_name) + 1;
2081 fdisp_init(&fdi, len + sizeof(*get_xattr_in));
2082 fdisp_make_vp(&fdi, FUSE_GETXATTR, vp, td, cred);
2084 get_xattr_in = fdi.indata;
2086 * Check to see whether we're querying the available size or
2087 * issuing the actual request. If we pass in 0, we get back struct
2088 * fuse_getxattr_out. If we pass in a non-zero size, we get back
2089 * that much data, without the struct fuse_getxattr_out header.
2092 get_xattr_in->size = 0;
2094 get_xattr_in->size = uio->uio_resid;
2096 attr_str = (char *)fdi.indata + sizeof(*get_xattr_in);
2097 snprintf(attr_str, len, "%s%c%s", prefix, extattr_namespace_separator,
2100 err = fdisp_wait_answ(&fdi);
2103 fsess_set_notimpl(mp, FUSE_GETXATTR);
2107 get_xattr_out = fdi.answ;
2109 if (ap->a_size != NULL)
2110 *ap->a_size = get_xattr_out->size;
2113 err = uiomove(fdi.answ, fdi.iosize, uio);
2116 fdisp_destroy(&fdi);
2121 struct vop_setextattr_args {
2122 struct vop_generic_args a_gen;
2124 int a_attrnamespace;
2127 struct ucred *a_cred;
2128 struct thread *a_td;
2132 fuse_vnop_setextattr(struct vop_setextattr_args *ap)
2134 struct vnode *vp = ap->a_vp;
2135 struct uio *uio = ap->a_uio;
2136 struct fuse_dispatcher fdi;
2137 struct fuse_setxattr_in *set_xattr_in;
2138 struct mount *mp = vnode_mount(vp);
2139 struct thread *td = ap->a_td;
2140 struct ucred *cred = ap->a_cred;
2146 if (fuse_isdeadfs(vp))
2149 /* Default to looking for user attributes. */
2150 if (ap->a_attrnamespace == EXTATTR_NAMESPACE_SYSTEM)
2151 prefix = EXTATTR_NAMESPACE_SYSTEM_STRING;
2153 prefix = EXTATTR_NAMESPACE_USER_STRING;
2155 len = strlen(prefix) + sizeof(extattr_namespace_separator) +
2156 strlen(ap->a_name) + 1;
2158 fdisp_init(&fdi, len + sizeof(*set_xattr_in) + uio->uio_resid);
2159 fdisp_make_vp(&fdi, FUSE_SETXATTR, vp, td, cred);
2161 set_xattr_in = fdi.indata;
2162 set_xattr_in->size = uio->uio_resid;
2164 attr_str = (char *)fdi.indata + sizeof(*set_xattr_in);
2165 snprintf(attr_str, len, "%s%c%s", prefix, extattr_namespace_separator,
2168 err = uiomove((char *)fdi.indata + sizeof(*set_xattr_in) + len,
2169 uio->uio_resid, uio);
2174 err = fdisp_wait_answ(&fdi);
2178 fsess_set_notimpl(mp, FUSE_SETXATTR);
2183 fdisp_destroy(&fdi);
2188 * The Linux / FUSE extended attribute list is simply a collection of
2189 * NUL-terminated strings. The FreeBSD extended attribute list is a single
2190 * byte length followed by a non-NUL terminated string. So, this allows
2191 * conversion of the Linux / FUSE format to the FreeBSD format in place.
2192 * Linux attribute names are reported with the namespace as a prefix (e.g.
2193 * "user.attribute_name"), but in FreeBSD they are reported without the
2194 * namespace prefix (e.g. "attribute_name"). So, we're going from:
2196 * user.attr_name1\0user.attr_name2\0
2200 * <num>attr_name1<num>attr_name2
2202 * Where "<num>" is a single byte number of characters in the attribute name.
2205 * prefix - exattr namespace prefix string
2206 * list, list_len - input list with namespace prefixes
2207 * bsd_list, bsd_list_len - output list compatible with bsd vfs
2210 fuse_xattrlist_convert(char *prefix, const char *list, int list_len,
2211 char *bsd_list, int *bsd_list_len)
2213 int len, pos, dist_to_next, prefix_len;
2217 prefix_len = strlen(prefix);
2219 while (pos < list_len && list[pos] != '\0') {
2220 dist_to_next = strlen(&list[pos]) + 1;
2221 if (bcmp(&list[pos], prefix, prefix_len) == 0 &&
2222 list[pos + prefix_len] == extattr_namespace_separator) {
2223 len = dist_to_next -
2224 (prefix_len + sizeof(extattr_namespace_separator)) - 1;
2225 if (len >= EXTATTR_MAXNAMELEN)
2226 return (ENAMETOOLONG);
2228 bsd_list[*bsd_list_len] = len;
2229 memcpy(&bsd_list[*bsd_list_len + 1],
2230 &list[pos + prefix_len +
2231 sizeof(extattr_namespace_separator)], len);
2233 *bsd_list_len += len + 1;
2236 pos += dist_to_next;
2243 struct vop_listextattr_args {
2244 struct vop_generic_args a_gen;
2246 int a_attrnamespace;
2249 struct ucred *a_cred;
2250 struct thread *a_td;
2254 fuse_vnop_listextattr(struct vop_listextattr_args *ap)
2256 struct vnode *vp = ap->a_vp;
2257 struct uio *uio = ap->a_uio;
2258 struct fuse_dispatcher fdi;
2259 struct fuse_listxattr_in *list_xattr_in;
2260 struct fuse_listxattr_out *list_xattr_out;
2261 struct mount *mp = vnode_mount(vp);
2262 struct thread *td = ap->a_td;
2263 struct ucred *cred = ap->a_cred;
2267 char *bsd_list = NULL;
2273 if (fuse_isdeadfs(vp))
2277 * Add space for a NUL and the period separator if enabled.
2278 * Default to looking for user attributes.
2280 if (ap->a_attrnamespace == EXTATTR_NAMESPACE_SYSTEM)
2281 prefix = EXTATTR_NAMESPACE_SYSTEM_STRING;
2283 prefix = EXTATTR_NAMESPACE_USER_STRING;
2285 len = strlen(prefix) + sizeof(extattr_namespace_separator) + 1;
2287 fdisp_init(&fdi, sizeof(*list_xattr_in) + len);
2288 fdisp_make_vp(&fdi, FUSE_LISTXATTR, vp, td, cred);
2291 * Retrieve Linux / FUSE compatible list size.
2293 list_xattr_in = fdi.indata;
2294 list_xattr_in->size = 0;
2295 attr_str = (char *)fdi.indata + sizeof(*list_xattr_in);
2296 snprintf(attr_str, len, "%s%c", prefix, extattr_namespace_separator);
2298 err = fdisp_wait_answ(&fdi);
2301 fsess_set_notimpl(mp, FUSE_LISTXATTR);
2305 list_xattr_out = fdi.answ;
2306 linux_list_len = list_xattr_out->size;
2307 if (linux_list_len == 0) {
2308 if (ap->a_size != NULL)
2309 *ap->a_size = linux_list_len;
2314 * Retrieve Linux / FUSE compatible list values.
2316 fdisp_refresh_vp(&fdi, FUSE_LISTXATTR, vp, td, cred);
2317 list_xattr_in = fdi.indata;
2318 list_xattr_in->size = linux_list_len + sizeof(*list_xattr_out);
2319 list_xattr_in->flags = 0;
2320 attr_str = (char *)fdi.indata + sizeof(*list_xattr_in);
2321 snprintf(attr_str, len, "%s%c", prefix, extattr_namespace_separator);
2323 err = fdisp_wait_answ(&fdi);
2327 linux_list = fdi.answ;
2328 linux_list_len = fdi.iosize;
2331 * Retrieve the BSD compatible list values.
2332 * The Linux / FUSE attribute list format isn't the same
2333 * as FreeBSD's format. So we need to transform it into
2334 * FreeBSD's format before giving it to the user.
2336 bsd_list = malloc(linux_list_len, M_TEMP, M_WAITOK);
2337 err = fuse_xattrlist_convert(prefix, linux_list, linux_list_len,
2338 bsd_list, &bsd_list_len);
2342 if (ap->a_size != NULL)
2343 *ap->a_size = bsd_list_len;
2346 err = uiomove(bsd_list, bsd_list_len, uio);
2349 free(bsd_list, M_TEMP);
2350 fdisp_destroy(&fdi);
2355 struct vop_deleteextattr_args {
2356 struct vop_generic_args a_gen;
2358 int a_attrnamespace;
2360 struct ucred *a_cred;
2361 struct thread *a_td;
2365 fuse_vnop_deleteextattr(struct vop_deleteextattr_args *ap)
2367 struct vnode *vp = ap->a_vp;
2368 struct fuse_dispatcher fdi;
2369 struct mount *mp = vnode_mount(vp);
2370 struct thread *td = ap->a_td;
2371 struct ucred *cred = ap->a_cred;
2377 if (fuse_isdeadfs(vp))
2380 /* Default to looking for user attributes. */
2381 if (ap->a_attrnamespace == EXTATTR_NAMESPACE_SYSTEM)
2382 prefix = EXTATTR_NAMESPACE_SYSTEM_STRING;
2384 prefix = EXTATTR_NAMESPACE_USER_STRING;
2386 len = strlen(prefix) + sizeof(extattr_namespace_separator) +
2387 strlen(ap->a_name) + 1;
2389 fdisp_init(&fdi, len);
2390 fdisp_make_vp(&fdi, FUSE_REMOVEXATTR, vp, td, cred);
2392 attr_str = fdi.indata;
2393 snprintf(attr_str, len, "%s%c%s", prefix, extattr_namespace_separator,
2396 err = fdisp_wait_answ(&fdi);
2399 fsess_set_notimpl(mp, FUSE_REMOVEXATTR);
2402 fdisp_destroy(&fdi);
2407 struct vnop_print_args {
2412 fuse_vnop_print(struct vop_print_args *ap)
2414 struct fuse_vnode_data *fvdat = VTOFUD(ap->a_vp);
2416 printf("nodeid: %ju, parent nodeid: %ju, nlookup: %ju, flag: %#x\n",
2417 (uintmax_t)VTOILLU(ap->a_vp), (uintmax_t)fvdat->parent_nid,
2418 (uintmax_t)fvdat->nlookup,