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, cred, foo);
483 fuse_vnode_open(*vpp, foo->open_flags, td);
484 cache_purge_negative(dvp);
488 fdisp_destroy(fdip2);
494 struct vnop_fdatasync_args {
495 struct vop_generic_args a_gen;
497 struct thread * a_td;
501 fuse_vnop_fdatasync(struct vop_fdatasync_args *ap)
503 struct vnode *vp = ap->a_vp;
504 struct thread *td = ap->a_td;
505 int waitfor = MNT_WAIT;
509 if (fuse_isdeadfs(vp)) {
512 if ((err = vop_stdfdatasync_buf(ap)))
515 return fuse_internal_fsync(vp, td, waitfor, true);
519 struct vnop_fsync_args {
520 struct vop_generic_args a_gen;
523 struct thread * a_td;
527 fuse_vnop_fsync(struct vop_fsync_args *ap)
529 struct vnode *vp = ap->a_vp;
530 struct thread *td = ap->a_td;
531 int waitfor = ap->a_waitfor;
534 if (fuse_isdeadfs(vp)) {
537 if ((err = vop_stdfsync(ap)))
540 return fuse_internal_fsync(vp, td, waitfor, false);
544 struct vnop_getattr_args {
547 struct ucred *a_cred;
552 fuse_vnop_getattr(struct vop_getattr_args *ap)
554 struct vnode *vp = ap->a_vp;
555 struct vattr *vap = ap->a_vap;
556 struct ucred *cred = ap->a_cred;
557 struct thread *td = curthread;
558 struct fuse_vnode_data *fvdat = VTOFUD(vp);
559 struct fuse_attr_out *fao;
563 struct fuse_dispatcher fdi;
565 dataflags = fuse_get_mpdata(vnode_mount(vp))->dataflags;
567 /* Note that we are not bailing out on a dead file system just yet. */
569 if (!(dataflags & FSESS_INITED)) {
570 if (!vnode_isvroot(vp)) {
571 fdata_set_dead(fuse_get_mpdata(vnode_mount(vp)));
579 if ((err = fdisp_simple_putget_vp(&fdi, FUSE_GETATTR, vp, td, cred))) {
580 if ((err == ENOTCONN) && vnode_isvroot(vp)) {
581 /* see comment in fuse_vfsop_statfs() */
586 fuse_internal_vnode_disappear(vp);
591 fao = (struct fuse_attr_out *)fdi.answ;
592 fuse_internal_cache_attrs(vp, &fao->attr, fao->attr_valid,
593 fao->attr_valid_nsec, vap);
594 if (vap->va_type != vnode_vtype(vp)) {
595 fuse_internal_vnode_disappear(vp);
599 if ((fvdat->flag & FN_SIZECHANGE) != 0)
600 vap->va_size = fvdat->filesize;
602 if (vnode_isreg(vp) && (fvdat->flag & FN_SIZECHANGE) == 0) {
604 * This is for those cases when the file size changed without us
605 * knowing, and we want to catch up.
607 off_t new_filesize = ((struct fuse_attr_out *)
608 fdi.answ)->attr.size;
610 if (fvdat->filesize != new_filesize) {
611 fuse_vnode_setsize(vp, cred, new_filesize);
612 fvdat->flag &= ~FN_SIZECHANGE;
621 bzero(vap, sizeof(*vap));
622 vap->va_type = vnode_vtype(vp);
628 struct vnop_inactive_args {
634 fuse_vnop_inactive(struct vop_inactive_args *ap)
636 struct vnode *vp = ap->a_vp;
637 struct thread *td = ap->a_td;
639 struct fuse_vnode_data *fvdat = VTOFUD(vp);
640 struct fuse_filehandle *fufh, *fufh_tmp;
644 LIST_FOREACH_SAFE(fufh, &fvdat->handles, next, fufh_tmp) {
645 if (need_flush && vp->v_type == VREG) {
646 if ((VTOFUD(vp)->flag & FN_SIZECHANGE) != 0) {
647 fuse_vnode_savesize(vp, NULL, 0);
649 if (fuse_data_cache_invalidate ||
650 (fvdat->flag & FN_REVOKED) != 0)
651 fuse_io_invalbuf(vp, td);
653 fuse_io_flushbuf(vp, MNT_WAIT, td);
656 fuse_filehandle_close(vp, fufh, td, NULL);
659 if ((fvdat->flag & FN_REVOKED) != 0 && fuse_reclaim_revoked) {
666 struct vnop_link_args {
667 struct vnode *a_tdvp;
669 struct componentname *a_cnp;
673 fuse_vnop_link(struct vop_link_args *ap)
675 struct vnode *vp = ap->a_vp;
676 struct vnode *tdvp = ap->a_tdvp;
677 struct componentname *cnp = ap->a_cnp;
679 struct vattr *vap = VTOVA(vp);
681 struct fuse_dispatcher fdi;
682 struct fuse_entry_out *feo;
683 struct fuse_link_in fli;
687 if (fuse_isdeadfs(vp)) {
690 if (vnode_mount(tdvp) != vnode_mount(vp)) {
695 * This is a seatbelt check to protect naive userspace filesystems from
696 * themselves and the limitations of the FUSE IPC protocol. If a
697 * filesystem does not allow attribute caching, assume it is capable of
698 * validating that nlink does not overflow.
700 if (vap != NULL && vap->va_nlink >= FUSE_LINK_MAX)
702 fli.oldnodeid = VTOI(vp);
705 fuse_internal_newentry_makerequest(vnode_mount(tdvp), VTOI(tdvp), cnp,
706 FUSE_LINK, &fli, sizeof(fli), &fdi);
707 if ((err = fdisp_wait_answ(&fdi))) {
712 err = fuse_internal_checkentry(feo, vnode_vtype(vp));
719 struct vnop_lookup_args {
720 struct vnodeop_desc *a_desc;
722 struct vnode **a_vpp;
723 struct componentname *a_cnp;
727 fuse_vnop_lookup(struct vop_lookup_args *ap)
729 struct vnode *dvp = ap->a_dvp;
730 struct vnode **vpp = ap->a_vpp;
731 struct componentname *cnp = ap->a_cnp;
732 struct thread *td = cnp->cn_thread;
733 struct ucred *cred = cnp->cn_cred;
735 int nameiop = cnp->cn_nameiop;
736 int flags = cnp->cn_flags;
737 int wantparent = flags & (LOCKPARENT | WANTPARENT);
738 int islastcn = flags & ISLASTCN;
739 struct mount *mp = vnode_mount(dvp);
743 struct vnode *vp = NULL;
745 struct fuse_dispatcher fdi;
749 struct fuse_access_param facp;
751 if (fuse_isdeadfs(dvp)) {
755 if (!vnode_isdir(dvp)) {
758 if (islastcn && vfs_isrdonly(mp) && (nameiop != LOOKUP)) {
762 * We do access check prior to doing anything else only in the case
763 * when we are at fs root (we'd like to say, "we are at the first
764 * component", but that's not exactly the same... nevermind).
765 * See further comments at further access checks.
768 bzero(&facp, sizeof(facp));
769 if (vnode_isvroot(dvp)) { /* early permission check hack */
770 if ((err = fuse_internal_access(dvp, VEXEC, &facp, td, cred))) {
774 if (flags & ISDOTDOT) {
775 nid = VTOFUD(dvp)->parent_nid;
782 } else if (cnp->cn_namelen == 1 && *(cnp->cn_nameptr) == '.') {
787 } else if (fuse_lookup_cache_enable) {
788 err = cache_lookup(dvp, vpp, cnp, NULL, NULL);
791 case -1: /* positive match */
792 atomic_add_acq_long(&fuse_lookup_cache_hits, 1);
795 case 0: /* no match in cache */
796 atomic_add_acq_long(&fuse_lookup_cache_misses, 1);
799 case ENOENT: /* negative match */
806 fdisp_init(&fdi, cnp->cn_namelen + 1);
810 fdisp_make(&fdi, op, mp, nid, td, cred);
812 if (op == FUSE_LOOKUP) {
813 memcpy(fdi.indata, cnp->cn_nameptr, cnp->cn_namelen);
814 ((char *)fdi.indata)[cnp->cn_namelen] = '\0';
816 lookup_err = fdisp_wait_answ(&fdi);
818 if ((op == FUSE_LOOKUP) && !lookup_err) { /* lookup call succeeded */
819 nid = ((struct fuse_entry_out *)fdi.answ)->nodeid;
822 * zero nodeid is the same as "not found",
823 * but it's also cacheable (which we keep
824 * keep on doing not as of writing this)
826 fdi.answ_stat = ENOENT;
828 } else if (nid == FUSE_ROOT_ID) {
833 (!fdi.answ_stat || lookup_err != ENOENT || op != FUSE_LOOKUP)) {
837 /* lookup_err, if non-zero, must be ENOENT at this point */
841 if ((nameiop == CREATE || nameiop == RENAME) && islastcn
842 /* && directory dvp has not been removed */ ) {
844 if (vfs_isrdonly(mp)) {
848 #if 0 /* THINK_ABOUT_THIS */
849 if ((err = fuse_internal_access(dvp, VWRITE, cred, td, &facp))) {
855 * Possibly record the position of a slot in the
856 * directory large enough for the new component name.
857 * This can be recorded in the vnode private data for
858 * dvp. Set the SAVENAME flag to hold onto the
859 * pathname for use later in VOP_CREATE or VOP_RENAME.
861 cnp->cn_flags |= SAVENAME;
866 /* Consider inserting name into cache. */
869 * No we can't use negative caching, as the fs
870 * changes are out of our control.
871 * False positives' falseness turns out just as things
872 * go by, but false negatives' falseness doesn't.
873 * (and aiding the caching mechanism with extra control
874 * mechanisms comes quite close to beating the whole purpose
878 if ((cnp->cn_flags & MAKEENTRY) != 0) {
879 SDT_PROBE2(fuse, , vnops, trace, 1,
880 "inserting NULL into cache");
881 cache_enter(dvp, NULL, cnp);
891 struct fuse_entry_out *feo = NULL;
892 struct fuse_attr *fattr = NULL;
894 if (op == FUSE_GETATTR) {
895 fattr = &((struct fuse_attr_out *)fdi.answ)->attr;
897 feo = (struct fuse_entry_out *)fdi.answ;
898 fattr = &(feo->attr);
902 * If deleting, and at end of pathname, return parameters
903 * which can be used to remove file. If the wantparent flag
904 * isn't set, we return only the directory, otherwise we go on
905 * and lock the inode, being careful with ".".
907 if (nameiop == DELETE && islastcn) {
909 * Check for write access on directory.
911 facp.xuid = fattr->uid;
912 facp.facc_flags |= FACCESS_STICKY;
913 err = fuse_internal_access(dvp, VWRITE, &facp, td, cred);
914 facp.facc_flags &= ~FACCESS_XQUERIES;
919 if (nid == VTOI(dvp)) {
923 err = fuse_vnode_get(dvp->v_mount, feo, nid,
924 dvp, &vp, cnp, IFTOVT(fattr->mode));
931 * Save the name for use in VOP_RMDIR and VOP_REMOVE
934 cnp->cn_flags |= SAVENAME;
939 * If rewriting (RENAME), return the inode and the
940 * information required to rewrite the present directory
941 * Must get inode of directory entry to verify it's a
942 * regular file, or empty directory.
944 if (nameiop == RENAME && wantparent && islastcn) {
946 #if 0 /* THINK_ABOUT_THIS */
947 if ((err = fuse_internal_access(dvp, VWRITE, cred, td, &facp))) {
955 if (nid == VTOI(dvp)) {
959 err = fuse_vnode_get(vnode_mount(dvp), feo, nid, dvp,
960 &vp, cnp, IFTOVT(fattr->mode));
966 * Save the name for use in VOP_RENAME later.
968 cnp->cn_flags |= SAVENAME;
972 if (flags & ISDOTDOT) {
977 * Expanded copy of vn_vget_ino() so that
978 * fuse_vnode_get() can be used.
981 ltype = VOP_ISLOCKED(dvp);
982 err = vfs_busy(mp, MBF_NOWAIT);
986 err = vfs_busy(mp, 0);
987 vn_lock(dvp, ltype | LK_RETRY);
991 if ((dvp->v_iflag & VI_DOOMED) != 0) {
998 err = fuse_vnode_get(vnode_mount(dvp), feo, nid, NULL,
999 &vp, cnp, IFTOVT(fattr->mode));
1001 vn_lock(dvp, ltype | LK_RETRY);
1002 if ((dvp->v_iflag & VI_DOOMED) != 0) {
1010 } else if (nid == VTOI(dvp)) {
1014 struct fuse_vnode_data *fvdat;
1016 err = fuse_vnode_get(vnode_mount(dvp), feo, nid, dvp,
1017 &vp, cnp, IFTOVT(fattr->mode));
1021 fuse_vnode_setparent(vp, dvp);
1024 * In the case where we are looking up a FUSE node
1025 * represented by an existing cached vnode, and the
1026 * true size reported by FUSE_LOOKUP doesn't match
1027 * the vnode's cached size, fix the vnode cache to
1028 * match the real object size.
1030 * This can occur via FUSE distributed filesystems,
1031 * irregular files, etc.
1034 if (vnode_isreg(vp) &&
1035 fattr->size != fvdat->filesize) {
1037 * The FN_SIZECHANGE flag reflects a dirty
1038 * append. If userspace lets us know our cache
1039 * is invalid, that write was lost. (Dirty
1040 * writes that do not cause append are also
1041 * lost, but we don't detect them here.)
1043 * XXX: Maybe disable WB caching on this mount.
1045 if (fvdat->flag & FN_SIZECHANGE)
1046 printf("%s: WB cache incoherent on "
1048 vnode_mount(vp)->mnt_stat.f_mntonname);
1050 (void)fuse_vnode_setsize(vp, cred, fattr->size);
1051 fvdat->flag &= ~FN_SIZECHANGE;
1056 if (op == FUSE_GETATTR) {
1057 struct fuse_attr_out *fao =
1058 (struct fuse_attr_out*)fdi.answ;
1059 fuse_internal_cache_attrs(*vpp,
1060 &fao->attr, fao->attr_valid,
1061 fao->attr_valid_nsec, NULL);
1063 struct fuse_entry_out *feo =
1064 (struct fuse_entry_out*)fdi.answ;
1065 fuse_internal_cache_attrs(*vpp,
1066 &feo->attr, feo->attr_valid,
1067 feo->attr_valid_nsec, NULL);
1070 /* Insert name into cache if appropriate. */
1073 * Nooo, caching is evil. With caching, we can't avoid stale
1074 * information taking over the playground (cached info is not
1075 * just positive/negative, it does have qualitative aspects,
1076 * too). And a (VOP/FUSE)_GETATTR is always thrown anyway, when
1077 * walking down along cached path components, and that's not
1078 * any cheaper than FUSE_LOOKUP. This might change with
1079 * implementing kernel side attr caching, but... In Linux,
1080 * lookup results are not cached, and the daemon is bombarded
1081 * with FUSE_LOOKUPS on and on. This shows that by design, the
1082 * daemon is expected to handle frequent lookup queries
1083 * efficiently, do its caching in userspace, and so on.
1085 * So just leave the name cache alone.
1089 * Well, now I know, Linux caches lookups, but with a
1090 * timeout... So it's the same thing as attribute caching:
1091 * we can deal with it when implement timeouts.
1094 if (cnp->cn_flags & MAKEENTRY) {
1095 cache_enter(dvp, *vpp, cnp);
1102 /* No lookup error; need to clean up. */
1104 if (err) { /* Found inode; exit with no vnode. */
1105 if (op == FUSE_LOOKUP) {
1106 fuse_internal_forget_send(vnode_mount(dvp), td, cred,
1109 fdisp_destroy(&fdi);
1112 #ifndef NO_EARLY_PERM_CHECK_HACK
1115 * We have the attributes of the next item
1116 * *now*, and it's a fact, and we do not
1117 * have to do extra work for it (ie, beg the
1118 * daemon), and it neither depends on such
1119 * accidental things like attr caching. So
1120 * the big idea: check credentials *now*,
1121 * not at the beginning of the next call to
1124 * The first item of the lookup chain (fs root)
1125 * won't be checked then here, of course, as
1126 * its never "the next". But go and see that
1127 * the root is taken care about at the very
1128 * beginning of this function.
1130 * Now, given we want to do the access check
1131 * this way, one might ask: so then why not
1132 * do the access check just after fetching
1133 * the inode and its attributes from the
1134 * daemon? Why bother with producing the
1135 * corresponding vnode at all if something
1136 * is not OK? We know what's the deal as
1137 * soon as we get those attrs... There is
1138 * one bit of info though not given us by
1139 * the daemon: whether his response is
1140 * authoritative or not... His response should
1141 * be ignored if something is mounted over
1142 * the dir in question. But that can be
1143 * known only by having the vnode...
1145 int tmpvtype = vnode_vtype(*vpp);
1147 bzero(&facp, sizeof(facp));
1148 /*the early perm check hack */
1149 facp.facc_flags |= FACCESS_VA_VALID;
1151 if ((tmpvtype != VDIR) && (tmpvtype != VLNK)) {
1154 if (!err && !vnode_mountedhere(*vpp)) {
1155 err = fuse_internal_access(*vpp, VEXEC, &facp, td, cred);
1158 if (tmpvtype == VLNK)
1159 SDT_PROBE2(fuse, , vnops, trace,
1160 1, "weird, permission "
1161 "error with a symlink?");
1169 fdisp_destroy(&fdi);
1175 struct vnop_mkdir_args {
1176 struct vnode *a_dvp;
1177 struct vnode **a_vpp;
1178 struct componentname *a_cnp;
1179 struct vattr *a_vap;
1183 fuse_vnop_mkdir(struct vop_mkdir_args *ap)
1185 struct vnode *dvp = ap->a_dvp;
1186 struct vnode **vpp = ap->a_vpp;
1187 struct componentname *cnp = ap->a_cnp;
1188 struct vattr *vap = ap->a_vap;
1190 struct fuse_mkdir_in fmdi;
1192 if (fuse_isdeadfs(dvp)) {
1195 fmdi.mode = MAKEIMODE(vap->va_type, vap->va_mode);
1197 return (fuse_internal_newentry(dvp, vpp, cnp, FUSE_MKDIR, &fmdi,
1198 sizeof(fmdi), VDIR));
1202 struct vnop_mknod_args {
1203 struct vnode *a_dvp;
1204 struct vnode **a_vpp;
1205 struct componentname *a_cnp;
1206 struct vattr *a_vap;
1210 fuse_vnop_mknod(struct vop_mknod_args *ap)
1213 struct vnode *dvp = ap->a_dvp;
1214 struct vnode **vpp = ap->a_vpp;
1215 struct componentname *cnp = ap->a_cnp;
1216 struct vattr *vap = ap->a_vap;
1217 struct fuse_mknod_in fmni;
1219 if (fuse_isdeadfs(dvp))
1222 fmni.mode = MAKEIMODE(vap->va_type, vap->va_mode);
1223 fmni.rdev = vap->va_rdev;
1224 return (fuse_internal_newentry(dvp, vpp, cnp, FUSE_MKNOD, &fmni,
1225 sizeof(fmni), vap->va_type));
1229 struct vnop_open_args {
1232 struct ucred *a_cred;
1233 struct thread *a_td;
1234 int a_fdidx; / struct file *a_fp;
1238 fuse_vnop_open(struct vop_open_args *ap)
1240 struct vnode *vp = ap->a_vp;
1241 int a_mode = ap->a_mode;
1242 struct thread *td = ap->a_td;
1243 struct ucred *cred = ap->a_cred;
1244 pid_t pid = td->td_proc->p_pid;
1245 struct fuse_vnode_data *fvdat;
1247 if (fuse_isdeadfs(vp))
1249 if (vp->v_type == VCHR || vp->v_type == VBLK || vp->v_type == VFIFO)
1250 return (EOPNOTSUPP);
1251 if ((a_mode & (FREAD | FWRITE | FEXEC)) == 0)
1256 if (fuse_filehandle_validrw(vp, a_mode, cred, pid)) {
1257 fuse_vnode_open(vp, 0, td);
1261 return fuse_filehandle_open(vp, a_mode, NULL, td, cred);
1265 fuse_vnop_pathconf(struct vop_pathconf_args *ap)
1268 switch (ap->a_name) {
1269 case _PC_FILESIZEBITS:
1273 *ap->a_retval = NAME_MAX;
1276 *ap->a_retval = MIN(LONG_MAX, FUSE_LINK_MAX);
1278 case _PC_SYMLINK_MAX:
1279 *ap->a_retval = MAXPATHLEN;
1285 return (vop_stdpathconf(ap));
1290 struct vnop_read_args {
1294 struct ucred *a_cred;
1298 fuse_vnop_read(struct vop_read_args *ap)
1300 struct vnode *vp = ap->a_vp;
1301 struct uio *uio = ap->a_uio;
1302 int ioflag = ap->a_ioflag;
1303 struct ucred *cred = ap->a_cred;
1304 pid_t pid = curthread->td_proc->p_pid;
1306 if (fuse_isdeadfs(vp)) {
1310 if (VTOFUD(vp)->flag & FN_DIRECTIO) {
1311 ioflag |= IO_DIRECT;
1314 return fuse_io_dispatch(vp, uio, ioflag, cred, pid);
1318 struct vnop_readdir_args {
1321 struct ucred *a_cred;
1328 fuse_vnop_readdir(struct vop_readdir_args *ap)
1330 struct vnode *vp = ap->a_vp;
1331 struct uio *uio = ap->a_uio;
1332 struct ucred *cred = ap->a_cred;
1333 struct fuse_filehandle *fufh = NULL;
1334 struct fuse_iov cookediov;
1336 pid_t pid = curthread->td_proc->p_pid;
1338 if (fuse_isdeadfs(vp)) {
1341 if ( /* XXXIP ((uio_iovcnt(uio) > 1)) || */
1342 (uio_resid(uio) < sizeof(struct dirent))) {
1346 err = fuse_filehandle_get_dir(vp, &fufh, cred, pid);
1349 #define DIRCOOKEDSIZE FUSE_DIRENT_ALIGN(FUSE_NAME_OFFSET + MAXNAMLEN + 1)
1350 fiov_init(&cookediov, DIRCOOKEDSIZE);
1352 err = fuse_internal_readdir(vp, uio, fufh, &cookediov);
1354 fiov_teardown(&cookediov);
1360 struct vnop_readlink_args {
1363 struct ucred *a_cred;
1367 fuse_vnop_readlink(struct vop_readlink_args *ap)
1369 struct vnode *vp = ap->a_vp;
1370 struct uio *uio = ap->a_uio;
1371 struct ucred *cred = ap->a_cred;
1373 struct fuse_dispatcher fdi;
1376 if (fuse_isdeadfs(vp)) {
1379 if (!vnode_islnk(vp)) {
1382 fdisp_init(&fdi, 0);
1383 err = fdisp_simple_putget_vp(&fdi, FUSE_READLINK, vp, curthread, cred);
1387 if (((char *)fdi.answ)[0] == '/' &&
1388 fuse_get_mpdata(vnode_mount(vp))->dataflags & FSESS_PUSH_SYMLINKS_IN) {
1389 char *mpth = vnode_mount(vp)->mnt_stat.f_mntonname;
1391 err = uiomove(mpth, strlen(mpth), uio);
1394 err = uiomove(fdi.answ, fdi.iosize, uio);
1397 fdisp_destroy(&fdi);
1402 struct vnop_reclaim_args {
1404 struct thread *a_td;
1408 fuse_vnop_reclaim(struct vop_reclaim_args *ap)
1410 struct vnode *vp = ap->a_vp;
1411 struct thread *td = ap->a_td;
1412 struct fuse_vnode_data *fvdat = VTOFUD(vp);
1413 struct fuse_filehandle *fufh, *fufh_tmp;
1416 panic("FUSE: no vnode data during recycling");
1418 LIST_FOREACH_SAFE(fufh, &fvdat->handles, next, fufh_tmp) {
1419 printf("FUSE: vnode being reclaimed with open fufh "
1420 "(type=%#x)", fufh->fufh_type);
1421 fuse_filehandle_close(vp, fufh, td, NULL);
1424 if ((!fuse_isdeadfs(vp)) && (fvdat->nlookup)) {
1425 fuse_internal_forget_send(vnode_mount(vp), td, NULL, VTOI(vp),
1428 fuse_vnode_setparent(vp, NULL);
1430 vfs_hash_remove(vp);
1431 vnode_destroy_vobject(vp);
1432 fuse_vnode_destroy(vp);
1438 struct vnop_remove_args {
1439 struct vnode *a_dvp;
1441 struct componentname *a_cnp;
1445 fuse_vnop_remove(struct vop_remove_args *ap)
1447 struct vnode *dvp = ap->a_dvp;
1448 struct vnode *vp = ap->a_vp;
1449 struct componentname *cnp = ap->a_cnp;
1453 if (fuse_isdeadfs(vp)) {
1456 if (vnode_isdir(vp)) {
1461 err = fuse_internal_remove(dvp, vp, cnp, FUSE_UNLINK);
1464 fuse_internal_vnode_disappear(vp);
1469 struct vnop_rename_args {
1470 struct vnode *a_fdvp;
1471 struct vnode *a_fvp;
1472 struct componentname *a_fcnp;
1473 struct vnode *a_tdvp;
1474 struct vnode *a_tvp;
1475 struct componentname *a_tcnp;
1479 fuse_vnop_rename(struct vop_rename_args *ap)
1481 struct vnode *fdvp = ap->a_fdvp;
1482 struct vnode *fvp = ap->a_fvp;
1483 struct componentname *fcnp = ap->a_fcnp;
1484 struct vnode *tdvp = ap->a_tdvp;
1485 struct vnode *tvp = ap->a_tvp;
1486 struct componentname *tcnp = ap->a_tcnp;
1487 struct fuse_data *data;
1491 if (fuse_isdeadfs(fdvp)) {
1494 if (fvp->v_mount != tdvp->v_mount ||
1495 (tvp && fvp->v_mount != tvp->v_mount)) {
1496 SDT_PROBE2(fuse, , vnops, trace, 1, "cross-device rename");
1503 * FUSE library is expected to check if target directory is not
1504 * under the source directory in the file system tree.
1505 * Linux performs this check at VFS level.
1507 data = fuse_get_mpdata(vnode_mount(tdvp));
1508 sx_xlock(&data->rename_lock);
1509 err = fuse_internal_rename(fdvp, fcnp, tdvp, tcnp);
1512 fuse_vnode_setparent(fvp, tdvp);
1514 fuse_vnode_setparent(tvp, NULL);
1516 sx_unlock(&data->rename_lock);
1518 if (tvp != NULL && tvp != fvp) {
1521 if (vnode_isdir(fvp)) {
1522 if ((tvp != NULL) && vnode_isdir(tvp)) {
1543 struct vnop_rmdir_args {
1544 struct vnode *a_dvp;
1546 struct componentname *a_cnp;
1550 fuse_vnop_rmdir(struct vop_rmdir_args *ap)
1552 struct vnode *dvp = ap->a_dvp;
1553 struct vnode *vp = ap->a_vp;
1557 if (fuse_isdeadfs(vp)) {
1560 if (VTOFUD(vp) == VTOFUD(dvp)) {
1563 err = fuse_internal_remove(dvp, vp, ap->a_cnp, FUSE_RMDIR);
1566 fuse_internal_vnode_disappear(vp);
1571 struct vnop_setattr_args {
1573 struct vattr *a_vap;
1574 struct ucred *a_cred;
1575 struct thread *a_td;
1579 fuse_vnop_setattr(struct vop_setattr_args *ap)
1581 struct vnode *vp = ap->a_vp;
1582 struct vattr *vap = ap->a_vap;
1583 struct ucred *cred = ap->a_cred;
1584 struct thread *td = curthread;
1585 struct fuse_dispatcher fdi;
1586 struct fuse_setattr_in *fsai;
1587 struct fuse_access_param facp;
1588 pid_t pid = td->td_proc->p_pid;
1592 int sizechanged = 0;
1593 uint64_t newsize = 0;
1595 if (fuse_isdeadfs(vp)) {
1598 fdisp_init(&fdi, sizeof(*fsai));
1599 fdisp_make_vp(&fdi, FUSE_SETATTR, vp, td, cred);
1603 bzero(&facp, sizeof(facp));
1605 facp.xuid = vap->va_uid;
1606 facp.xgid = vap->va_gid;
1608 if (vap->va_uid != (uid_t)VNOVAL) {
1609 facp.facc_flags |= FACCESS_CHOWN;
1610 fsai->uid = vap->va_uid;
1611 fsai->valid |= FATTR_UID;
1613 if (vap->va_gid != (gid_t)VNOVAL) {
1614 facp.facc_flags |= FACCESS_CHOWN;
1615 fsai->gid = vap->va_gid;
1616 fsai->valid |= FATTR_GID;
1618 if (vap->va_size != VNOVAL) {
1620 struct fuse_filehandle *fufh = NULL;
1622 /*Truncate to a new value. */
1623 fsai->size = vap->va_size;
1625 newsize = vap->va_size;
1626 fsai->valid |= FATTR_SIZE;
1628 fuse_filehandle_getrw(vp, FWRITE, &fufh, cred, pid);
1630 fsai->fh = fufh->fh_id;
1631 fsai->valid |= FATTR_FH;
1634 if (vap->va_atime.tv_sec != VNOVAL) {
1635 fsai->atime = vap->va_atime.tv_sec;
1636 fsai->atimensec = vap->va_atime.tv_nsec;
1637 fsai->valid |= FATTR_ATIME;
1639 if (vap->va_mtime.tv_sec != VNOVAL) {
1640 fsai->mtime = vap->va_mtime.tv_sec;
1641 fsai->mtimensec = vap->va_mtime.tv_nsec;
1642 fsai->valid |= FATTR_MTIME;
1644 if (vap->va_mode != (mode_t)VNOVAL) {
1645 fsai->mode = vap->va_mode & ALLPERMS;
1646 fsai->valid |= FATTR_MODE;
1651 vtyp = vnode_vtype(vp);
1653 if (fsai->valid & FATTR_SIZE && vtyp == VDIR) {
1657 if (vfs_isrdonly(vnode_mount(vp)) && (fsai->valid & ~FATTR_SIZE || vtyp == VREG)) {
1662 if ((err = fdisp_wait_answ(&fdi)))
1664 vtyp = IFTOVT(((struct fuse_attr_out *)fdi.answ)->attr.mode);
1666 if (vnode_vtype(vp) != vtyp) {
1667 if (vnode_vtype(vp) == VNON && vtyp != VNON) {
1668 SDT_PROBE2(fuse, , vnops, trace, 1, "FUSE: Dang! "
1669 "vnode_vtype is VNON and vtype isn't.");
1672 * STALE vnode, ditch
1674 * The vnode has changed its type "behind our back".
1675 * There's nothing really we can do, so let us just
1676 * force an internal revocation and tell the caller to
1677 * try again, if interested.
1679 fuse_internal_vnode_disappear(vp);
1684 struct fuse_attr_out *fao = (struct fuse_attr_out*)fdi.answ;
1685 fuse_internal_cache_attrs(vp, &fao->attr, fao->attr_valid,
1686 fao->attr_valid_nsec, NULL);
1690 fdisp_destroy(&fdi);
1691 if (!err && sizechanged) {
1692 fuse_vnode_setsize(vp, cred, newsize);
1693 VTOFUD(vp)->flag &= ~FN_SIZECHANGE;
1699 struct vnop_strategy_args {
1705 fuse_vnop_strategy(struct vop_strategy_args *ap)
1707 struct vnode *vp = ap->a_vp;
1708 struct buf *bp = ap->a_bp;
1710 if (!vp || fuse_isdeadfs(vp)) {
1711 bp->b_ioflags |= BIO_ERROR;
1712 bp->b_error = ENXIO;
1716 if (bp->b_iocmd == BIO_WRITE) {
1719 err = fuse_vnode_refreshsize(vp, NOCRED);
1721 bp->b_ioflags |= BIO_ERROR;
1729 * VOP_STRATEGY always returns zero and signals error via bp->b_ioflags.
1730 * fuse_io_strategy sets bp's error fields
1732 (void)fuse_io_strategy(vp, bp);
1739 struct vnop_symlink_args {
1740 struct vnode *a_dvp;
1741 struct vnode **a_vpp;
1742 struct componentname *a_cnp;
1743 struct vattr *a_vap;
1748 fuse_vnop_symlink(struct vop_symlink_args *ap)
1750 struct vnode *dvp = ap->a_dvp;
1751 struct vnode **vpp = ap->a_vpp;
1752 struct componentname *cnp = ap->a_cnp;
1753 const char *target = ap->a_target;
1755 struct fuse_dispatcher fdi;
1760 if (fuse_isdeadfs(dvp)) {
1764 * Unlike the other creator type calls, here we have to create a message
1765 * where the name of the new entry comes first, and the data describing
1766 * the entry comes second.
1767 * Hence we can't rely on our handy fuse_internal_newentry() routine,
1768 * but put together the message manually and just call the core part.
1771 len = strlen(target) + 1;
1772 fdisp_init(&fdi, len + cnp->cn_namelen + 1);
1773 fdisp_make_vp(&fdi, FUSE_SYMLINK, dvp, curthread, NULL);
1775 memcpy(fdi.indata, cnp->cn_nameptr, cnp->cn_namelen);
1776 ((char *)fdi.indata)[cnp->cn_namelen] = '\0';
1777 memcpy((char *)fdi.indata + cnp->cn_namelen + 1, target, len);
1779 err = fuse_internal_newentry_core(dvp, vpp, cnp, VLNK, &fdi);
1780 fdisp_destroy(&fdi);
1785 struct vnop_write_args {
1789 struct ucred *a_cred;
1793 fuse_vnop_write(struct vop_write_args *ap)
1795 struct vnode *vp = ap->a_vp;
1796 struct uio *uio = ap->a_uio;
1797 int ioflag = ap->a_ioflag;
1798 struct ucred *cred = ap->a_cred;
1799 pid_t pid = curthread->td_proc->p_pid;
1802 if (fuse_isdeadfs(vp)) {
1805 err = fuse_vnode_refreshsize(vp, cred);
1809 if (VTOFUD(vp)->flag & FN_DIRECTIO) {
1810 ioflag |= IO_DIRECT;
1813 return fuse_io_dispatch(vp, uio, ioflag, cred, pid);
1816 SDT_PROBE_DEFINE1(fuse, , vnops, vnop_getpages_error, "int");
1818 struct vnop_getpages_args {
1826 fuse_vnop_getpages(struct vop_getpages_args *ap)
1828 int i, error, nextoff, size, toff, count, npages;
1837 pid_t pid = curthread->td_proc->p_pid;
1840 KASSERT(vp->v_object, ("objectless vp passed to getpages"));
1841 td = curthread; /* XXX */
1842 cred = curthread->td_ucred; /* XXX */
1844 npages = ap->a_count;
1846 if (!fsess_opt_mmap(vnode_mount(vp))) {
1847 SDT_PROBE2(fuse, , vnops, trace, 1,
1848 "called on non-cacheable vnode??\n");
1849 return (VM_PAGER_ERROR);
1853 * If the last page is partially valid, just return it and allow
1854 * the pager to zero-out the blanks. Partially valid pages can
1855 * only occur at the file EOF.
1857 * XXXGL: is that true for FUSE, which is a local filesystem,
1858 * but still somewhat disconnected from the kernel?
1860 VM_OBJECT_WLOCK(vp->v_object);
1861 if (pages[npages - 1]->valid != 0 && --npages == 0)
1863 VM_OBJECT_WUNLOCK(vp->v_object);
1866 * We use only the kva address for the buffer, but this is extremely
1867 * convenient and fast.
1869 bp = uma_zalloc(fuse_pbuf_zone, M_WAITOK);
1871 kva = (vm_offset_t)bp->b_data;
1872 pmap_qenter(kva, pages, npages);
1873 VM_CNT_INC(v_vnodein);
1874 VM_CNT_ADD(v_vnodepgsin, npages);
1876 count = npages << PAGE_SHIFT;
1877 iov.iov_base = (caddr_t)kva;
1878 iov.iov_len = count;
1881 uio.uio_offset = IDX_TO_OFF(pages[0]->pindex);
1882 uio.uio_resid = count;
1883 uio.uio_segflg = UIO_SYSSPACE;
1884 uio.uio_rw = UIO_READ;
1887 error = fuse_io_dispatch(vp, &uio, IO_DIRECT, cred, pid);
1888 pmap_qremove(kva, npages);
1890 uma_zfree(fuse_pbuf_zone, bp);
1892 if (error && (uio.uio_resid == count)) {
1893 SDT_PROBE1(fuse, , vnops, vnop_getpages_error, error);
1894 return VM_PAGER_ERROR;
1897 * Calculate the number of bytes read and validate only that number
1898 * of bytes. Note that due to pending writes, size may be 0. This
1899 * does not mean that the remaining data is invalid!
1902 size = count - uio.uio_resid;
1903 VM_OBJECT_WLOCK(vp->v_object);
1904 fuse_vm_page_lock_queues();
1905 for (i = 0, toff = 0; i < npages; i++, toff = nextoff) {
1908 nextoff = toff + PAGE_SIZE;
1911 if (nextoff <= size) {
1913 * Read operation filled an entire page
1915 m->valid = VM_PAGE_BITS_ALL;
1916 KASSERT(m->dirty == 0,
1917 ("fuse_getpages: page %p is dirty", m));
1918 } else if (size > toff) {
1920 * Read operation filled a partial page.
1923 vm_page_set_valid_range(m, 0, size - toff);
1924 KASSERT(m->dirty == 0,
1925 ("fuse_getpages: page %p is dirty", m));
1928 * Read operation was short. If no error occurred
1929 * we may have hit a zero-fill section. We simply
1930 * leave valid set to 0.
1935 fuse_vm_page_unlock_queues();
1937 VM_OBJECT_WUNLOCK(vp->v_object);
1942 return (VM_PAGER_OK);
1946 struct vnop_putpages_args {
1952 vm_ooffset_t a_offset;
1956 fuse_vnop_putpages(struct vop_putpages_args *ap)
1962 int i, error, npages, count;
1970 pid_t pid = curthread->td_proc->p_pid;
1973 KASSERT(vp->v_object, ("objectless vp passed to putpages"));
1974 fsize = vp->v_object->un_pager.vnp.vnp_size;
1975 td = curthread; /* XXX */
1976 cred = curthread->td_ucred; /* XXX */
1978 count = ap->a_count;
1979 rtvals = ap->a_rtvals;
1980 npages = btoc(count);
1981 offset = IDX_TO_OFF(pages[0]->pindex);
1983 if (!fsess_opt_mmap(vnode_mount(vp))) {
1984 SDT_PROBE2(fuse, , vnops, trace, 1,
1985 "called on non-cacheable vnode??\n");
1987 for (i = 0; i < npages; i++)
1988 rtvals[i] = VM_PAGER_AGAIN;
1991 * When putting pages, do not extend file past EOF.
1994 if (offset + count > fsize) {
1995 count = fsize - offset;
2000 * We use only the kva address for the buffer, but this is extremely
2001 * convenient and fast.
2003 bp = uma_zalloc(fuse_pbuf_zone, M_WAITOK);
2005 kva = (vm_offset_t)bp->b_data;
2006 pmap_qenter(kva, pages, npages);
2007 VM_CNT_INC(v_vnodeout);
2008 VM_CNT_ADD(v_vnodepgsout, count);
2010 iov.iov_base = (caddr_t)kva;
2011 iov.iov_len = count;
2014 uio.uio_offset = offset;
2015 uio.uio_resid = count;
2016 uio.uio_segflg = UIO_SYSSPACE;
2017 uio.uio_rw = UIO_WRITE;
2020 error = fuse_io_dispatch(vp, &uio, IO_DIRECT, cred, pid);
2022 pmap_qremove(kva, npages);
2023 uma_zfree(fuse_pbuf_zone, bp);
2026 int nwritten = round_page(count - uio.uio_resid) / PAGE_SIZE;
2028 for (i = 0; i < nwritten; i++) {
2029 rtvals[i] = VM_PAGER_OK;
2030 VM_OBJECT_WLOCK(pages[i]->object);
2031 vm_page_undirty(pages[i]);
2032 VM_OBJECT_WUNLOCK(pages[i]->object);
2038 static const char extattr_namespace_separator = '.';
2041 struct vop_getextattr_args {
2042 struct vop_generic_args a_gen;
2044 int a_attrnamespace;
2048 struct ucred *a_cred;
2049 struct thread *a_td;
2053 fuse_vnop_getextattr(struct vop_getextattr_args *ap)
2055 struct vnode *vp = ap->a_vp;
2056 struct uio *uio = ap->a_uio;
2057 struct fuse_dispatcher fdi;
2058 struct fuse_getxattr_in *get_xattr_in;
2059 struct fuse_getxattr_out *get_xattr_out;
2060 struct mount *mp = vnode_mount(vp);
2061 struct thread *td = ap->a_td;
2062 struct ucred *cred = ap->a_cred;
2068 if (fuse_isdeadfs(vp))
2071 /* Default to looking for user attributes. */
2072 if (ap->a_attrnamespace == EXTATTR_NAMESPACE_SYSTEM)
2073 prefix = EXTATTR_NAMESPACE_SYSTEM_STRING;
2075 prefix = EXTATTR_NAMESPACE_USER_STRING;
2077 len = strlen(prefix) + sizeof(extattr_namespace_separator) +
2078 strlen(ap->a_name) + 1;
2080 fdisp_init(&fdi, len + sizeof(*get_xattr_in));
2081 fdisp_make_vp(&fdi, FUSE_GETXATTR, vp, td, cred);
2083 get_xattr_in = fdi.indata;
2085 * Check to see whether we're querying the available size or
2086 * issuing the actual request. If we pass in 0, we get back struct
2087 * fuse_getxattr_out. If we pass in a non-zero size, we get back
2088 * that much data, without the struct fuse_getxattr_out header.
2091 get_xattr_in->size = 0;
2093 get_xattr_in->size = uio->uio_resid;
2095 attr_str = (char *)fdi.indata + sizeof(*get_xattr_in);
2096 snprintf(attr_str, len, "%s%c%s", prefix, extattr_namespace_separator,
2099 err = fdisp_wait_answ(&fdi);
2102 fsess_set_notimpl(mp, FUSE_GETXATTR);
2106 get_xattr_out = fdi.answ;
2108 if (ap->a_size != NULL)
2109 *ap->a_size = get_xattr_out->size;
2112 err = uiomove(fdi.answ, fdi.iosize, uio);
2115 fdisp_destroy(&fdi);
2120 struct vop_setextattr_args {
2121 struct vop_generic_args a_gen;
2123 int a_attrnamespace;
2126 struct ucred *a_cred;
2127 struct thread *a_td;
2131 fuse_vnop_setextattr(struct vop_setextattr_args *ap)
2133 struct vnode *vp = ap->a_vp;
2134 struct uio *uio = ap->a_uio;
2135 struct fuse_dispatcher fdi;
2136 struct fuse_setxattr_in *set_xattr_in;
2137 struct mount *mp = vnode_mount(vp);
2138 struct thread *td = ap->a_td;
2139 struct ucred *cred = ap->a_cred;
2145 if (fuse_isdeadfs(vp))
2148 /* Default to looking for user attributes. */
2149 if (ap->a_attrnamespace == EXTATTR_NAMESPACE_SYSTEM)
2150 prefix = EXTATTR_NAMESPACE_SYSTEM_STRING;
2152 prefix = EXTATTR_NAMESPACE_USER_STRING;
2154 len = strlen(prefix) + sizeof(extattr_namespace_separator) +
2155 strlen(ap->a_name) + 1;
2157 fdisp_init(&fdi, len + sizeof(*set_xattr_in) + uio->uio_resid);
2158 fdisp_make_vp(&fdi, FUSE_SETXATTR, vp, td, cred);
2160 set_xattr_in = fdi.indata;
2161 set_xattr_in->size = uio->uio_resid;
2163 attr_str = (char *)fdi.indata + sizeof(*set_xattr_in);
2164 snprintf(attr_str, len, "%s%c%s", prefix, extattr_namespace_separator,
2167 err = uiomove((char *)fdi.indata + sizeof(*set_xattr_in) + len,
2168 uio->uio_resid, uio);
2173 err = fdisp_wait_answ(&fdi);
2177 fsess_set_notimpl(mp, FUSE_SETXATTR);
2182 fdisp_destroy(&fdi);
2187 * The Linux / FUSE extended attribute list is simply a collection of
2188 * NUL-terminated strings. The FreeBSD extended attribute list is a single
2189 * byte length followed by a non-NUL terminated string. So, this allows
2190 * conversion of the Linux / FUSE format to the FreeBSD format in place.
2191 * Linux attribute names are reported with the namespace as a prefix (e.g.
2192 * "user.attribute_name"), but in FreeBSD they are reported without the
2193 * namespace prefix (e.g. "attribute_name"). So, we're going from:
2195 * user.attr_name1\0user.attr_name2\0
2199 * <num>attr_name1<num>attr_name2
2201 * Where "<num>" is a single byte number of characters in the attribute name.
2204 * prefix - exattr namespace prefix string
2205 * list, list_len - input list with namespace prefixes
2206 * bsd_list, bsd_list_len - output list compatible with bsd vfs
2209 fuse_xattrlist_convert(char *prefix, const char *list, int list_len,
2210 char *bsd_list, int *bsd_list_len)
2212 int len, pos, dist_to_next, prefix_len;
2216 prefix_len = strlen(prefix);
2218 while (pos < list_len && list[pos] != '\0') {
2219 dist_to_next = strlen(&list[pos]) + 1;
2220 if (bcmp(&list[pos], prefix, prefix_len) == 0 &&
2221 list[pos + prefix_len] == extattr_namespace_separator) {
2222 len = dist_to_next -
2223 (prefix_len + sizeof(extattr_namespace_separator)) - 1;
2224 if (len >= EXTATTR_MAXNAMELEN)
2225 return (ENAMETOOLONG);
2227 bsd_list[*bsd_list_len] = len;
2228 memcpy(&bsd_list[*bsd_list_len + 1],
2229 &list[pos + prefix_len +
2230 sizeof(extattr_namespace_separator)], len);
2232 *bsd_list_len += len + 1;
2235 pos += dist_to_next;
2242 struct vop_listextattr_args {
2243 struct vop_generic_args a_gen;
2245 int a_attrnamespace;
2248 struct ucred *a_cred;
2249 struct thread *a_td;
2253 fuse_vnop_listextattr(struct vop_listextattr_args *ap)
2255 struct vnode *vp = ap->a_vp;
2256 struct uio *uio = ap->a_uio;
2257 struct fuse_dispatcher fdi;
2258 struct fuse_listxattr_in *list_xattr_in;
2259 struct fuse_listxattr_out *list_xattr_out;
2260 struct mount *mp = vnode_mount(vp);
2261 struct thread *td = ap->a_td;
2262 struct ucred *cred = ap->a_cred;
2266 char *bsd_list = NULL;
2272 if (fuse_isdeadfs(vp))
2276 * Add space for a NUL and the period separator if enabled.
2277 * Default to looking for user attributes.
2279 if (ap->a_attrnamespace == EXTATTR_NAMESPACE_SYSTEM)
2280 prefix = EXTATTR_NAMESPACE_SYSTEM_STRING;
2282 prefix = EXTATTR_NAMESPACE_USER_STRING;
2284 len = strlen(prefix) + sizeof(extattr_namespace_separator) + 1;
2286 fdisp_init(&fdi, sizeof(*list_xattr_in) + len);
2287 fdisp_make_vp(&fdi, FUSE_LISTXATTR, vp, td, cred);
2290 * Retrieve Linux / FUSE compatible list size.
2292 list_xattr_in = fdi.indata;
2293 list_xattr_in->size = 0;
2294 attr_str = (char *)fdi.indata + sizeof(*list_xattr_in);
2295 snprintf(attr_str, len, "%s%c", prefix, extattr_namespace_separator);
2297 err = fdisp_wait_answ(&fdi);
2300 fsess_set_notimpl(mp, FUSE_LISTXATTR);
2304 list_xattr_out = fdi.answ;
2305 linux_list_len = list_xattr_out->size;
2306 if (linux_list_len == 0) {
2307 if (ap->a_size != NULL)
2308 *ap->a_size = linux_list_len;
2313 * Retrieve Linux / FUSE compatible list values.
2315 fdisp_refresh_vp(&fdi, FUSE_LISTXATTR, vp, td, cred);
2316 list_xattr_in = fdi.indata;
2317 list_xattr_in->size = linux_list_len + sizeof(*list_xattr_out);
2318 list_xattr_in->flags = 0;
2319 attr_str = (char *)fdi.indata + sizeof(*list_xattr_in);
2320 snprintf(attr_str, len, "%s%c", prefix, extattr_namespace_separator);
2322 err = fdisp_wait_answ(&fdi);
2326 linux_list = fdi.answ;
2327 linux_list_len = fdi.iosize;
2330 * Retrieve the BSD compatible list values.
2331 * The Linux / FUSE attribute list format isn't the same
2332 * as FreeBSD's format. So we need to transform it into
2333 * FreeBSD's format before giving it to the user.
2335 bsd_list = malloc(linux_list_len, M_TEMP, M_WAITOK);
2336 err = fuse_xattrlist_convert(prefix, linux_list, linux_list_len,
2337 bsd_list, &bsd_list_len);
2341 if (ap->a_size != NULL)
2342 *ap->a_size = bsd_list_len;
2345 err = uiomove(bsd_list, bsd_list_len, uio);
2348 free(bsd_list, M_TEMP);
2349 fdisp_destroy(&fdi);
2354 struct vop_deleteextattr_args {
2355 struct vop_generic_args a_gen;
2357 int a_attrnamespace;
2359 struct ucred *a_cred;
2360 struct thread *a_td;
2364 fuse_vnop_deleteextattr(struct vop_deleteextattr_args *ap)
2366 struct vnode *vp = ap->a_vp;
2367 struct fuse_dispatcher fdi;
2368 struct mount *mp = vnode_mount(vp);
2369 struct thread *td = ap->a_td;
2370 struct ucred *cred = ap->a_cred;
2376 if (fuse_isdeadfs(vp))
2379 /* Default to looking for user attributes. */
2380 if (ap->a_attrnamespace == EXTATTR_NAMESPACE_SYSTEM)
2381 prefix = EXTATTR_NAMESPACE_SYSTEM_STRING;
2383 prefix = EXTATTR_NAMESPACE_USER_STRING;
2385 len = strlen(prefix) + sizeof(extattr_namespace_separator) +
2386 strlen(ap->a_name) + 1;
2388 fdisp_init(&fdi, len);
2389 fdisp_make_vp(&fdi, FUSE_REMOVEXATTR, vp, td, cred);
2391 attr_str = fdi.indata;
2392 snprintf(attr_str, len, "%s%c%s", prefix, extattr_namespace_separator,
2395 err = fdisp_wait_answ(&fdi);
2398 fsess_set_notimpl(mp, FUSE_REMOVEXATTR);
2401 fdisp_destroy(&fdi);
2406 struct vnop_print_args {
2411 fuse_vnop_print(struct vop_print_args *ap)
2413 struct fuse_vnode_data *fvdat = VTOFUD(ap->a_vp);
2415 printf("nodeid: %ju, parent nodeid: %ju, nlookup: %ju, flag: %#x\n",
2416 (uintmax_t)VTOILLU(ap->a_vp), (uintmax_t)fvdat->parent_nid,
2417 (uintmax_t)fvdat->nlookup,