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)) {
330 if (vnode_isdir(vp)) {
331 struct fuse_filehandle *fufh;
333 // XXX: what if two file descriptors have the same directory
334 // opened? We shouldn't close the file handle too soon.
335 if (fuse_filehandle_get_dir(vp, &fufh, cred, pid) == 0)
336 fuse_filehandle_close(vp, fufh, NULL, cred);
339 if (fflag & IO_NDELAY) {
342 err = fuse_flush(vp, cred, pid, fflag);
343 /* TODO: close the file handle, if we're sure it's no longer used */
344 if ((VTOFUD(vp)->flag & FN_SIZECHANGE) != 0) {
345 fuse_vnode_savesize(vp, cred, td->td_proc->p_pid);
351 fdisp_make_mknod_for_fallback(
352 struct fuse_dispatcher *fdip,
353 struct componentname *cnp,
359 enum fuse_opcode *op)
361 struct fuse_mknod_in *fmni;
363 fdisp_init(fdip, sizeof(*fmni) + cnp->cn_namelen + 1);
365 fdisp_make(fdip, *op, vnode_mount(dvp), parentnid, td, cred);
369 memcpy((char *)fdip->indata + sizeof(*fmni), cnp->cn_nameptr,
371 ((char *)fdip->indata)[sizeof(*fmni) + cnp->cn_namelen] = '\0';
374 struct vnop_create_args {
376 struct vnode **a_vpp;
377 struct componentname *a_cnp;
382 fuse_vnop_create(struct vop_create_args *ap)
384 struct vnode *dvp = ap->a_dvp;
385 struct vnode **vpp = ap->a_vpp;
386 struct componentname *cnp = ap->a_cnp;
387 struct vattr *vap = ap->a_vap;
388 struct thread *td = cnp->cn_thread;
389 struct ucred *cred = cnp->cn_cred;
391 struct fuse_open_in *foi;
392 struct fuse_entry_out *feo;
393 struct fuse_open_out *foo;
394 struct fuse_dispatcher fdi, fdi2;
395 struct fuse_dispatcher *fdip = &fdi;
396 struct fuse_dispatcher *fdip2 = NULL;
400 struct mount *mp = vnode_mount(dvp);
401 uint64_t parentnid = VTOFUD(dvp)->nid;
402 mode_t mode = MAKEIMODE(vap->va_type, vap->va_mode);
407 * VOP_CREATE doesn't tell us the open(2) flags, so we guess. Only a
408 * writable mode makes sense, and we might as well include readability
413 if (fuse_isdeadfs(dvp)) {
416 bzero(&fdi, sizeof(fdi));
418 if ((vap->va_type != VREG))
421 if (!fsess_isimpl(mp, FUSE_CREATE)) {
422 /* Fallback to FUSE_MKNOD/FUSE_OPEN */
423 fdisp_make_mknod_for_fallback(fdip, cnp, dvp, parentnid, td,
426 /* Use FUSE_CREATE */
428 fdisp_init(fdip, sizeof(*foi) + cnp->cn_namelen + 1);
429 fdisp_make(fdip, op, vnode_mount(dvp), parentnid, td, cred);
432 foi->flags = O_CREAT | flags;
433 memcpy((char *)fdip->indata + sizeof(*foi), cnp->cn_nameptr,
435 ((char *)fdip->indata)[sizeof(*foi) + cnp->cn_namelen] = '\0';
438 err = fdisp_wait_answ(fdip);
441 if (err == ENOSYS && op == FUSE_CREATE) {
442 fsess_set_notimpl(mp, FUSE_CREATE);
443 fdisp_make_mknod_for_fallback(fdip, cnp, dvp,
444 parentnid, td, cred, mode, &op);
445 err = fdisp_wait_answ(fdip);
453 if ((err = fuse_internal_checkentry(feo, VREG))) {
457 if (op == FUSE_CREATE) {
458 foo = (struct fuse_open_out*)(feo + 1);
460 /* Issue a separate FUSE_OPEN */
462 fdisp_init(fdip2, sizeof(*foi));
463 fdisp_make(fdip2, FUSE_OPEN, vnode_mount(dvp), feo->nodeid, td,
468 err = fdisp_wait_answ(fdip2);
473 err = fuse_vnode_get(mp, feo, feo->nodeid, dvp, vpp, cnp, VREG);
475 struct fuse_release_in *fri;
476 uint64_t nodeid = feo->nodeid;
477 uint64_t fh_id = foo->fh;
479 fdisp_init(fdip, sizeof(*fri));
480 fdisp_make(fdip, FUSE_RELEASE, mp, nodeid, td, cred);
484 fuse_insert_callback(fdip->tick, fuse_internal_forget_callback);
485 fuse_insert_message(fdip->tick);
488 ASSERT_VOP_ELOCKED(*vpp, "fuse_vnop_create");
490 fuse_filehandle_init(*vpp, FUFH_RDWR, NULL, td->td_proc->p_pid, cred,
492 fuse_vnode_open(*vpp, foo->open_flags, td);
493 cache_purge_negative(dvp);
497 fdisp_destroy(fdip2);
503 struct vnop_fdatasync_args {
504 struct vop_generic_args a_gen;
506 struct thread * a_td;
510 fuse_vnop_fdatasync(struct vop_fdatasync_args *ap)
512 struct vnode *vp = ap->a_vp;
513 struct thread *td = ap->a_td;
514 int waitfor = MNT_WAIT;
518 if (fuse_isdeadfs(vp)) {
521 if ((err = vop_stdfdatasync_buf(ap)))
524 return fuse_internal_fsync(vp, td, waitfor, true);
528 struct vnop_fsync_args {
529 struct vop_generic_args a_gen;
532 struct thread * a_td;
536 fuse_vnop_fsync(struct vop_fsync_args *ap)
538 struct vnode *vp = ap->a_vp;
539 struct thread *td = ap->a_td;
540 int waitfor = ap->a_waitfor;
543 if (fuse_isdeadfs(vp)) {
546 if ((err = vop_stdfsync(ap)))
549 return fuse_internal_fsync(vp, td, waitfor, false);
553 struct vnop_getattr_args {
556 struct ucred *a_cred;
561 fuse_vnop_getattr(struct vop_getattr_args *ap)
563 struct vnode *vp = ap->a_vp;
564 struct vattr *vap = ap->a_vap;
565 struct ucred *cred = ap->a_cred;
566 struct thread *td = curthread;
567 struct fuse_vnode_data *fvdat = VTOFUD(vp);
568 struct fuse_attr_out *fao;
572 struct fuse_dispatcher fdi;
574 dataflags = fuse_get_mpdata(vnode_mount(vp))->dataflags;
576 /* Note that we are not bailing out on a dead file system just yet. */
578 if (!(dataflags & FSESS_INITED)) {
579 if (!vnode_isvroot(vp)) {
580 fdata_set_dead(fuse_get_mpdata(vnode_mount(vp)));
588 if ((err = fdisp_simple_putget_vp(&fdi, FUSE_GETATTR, vp, td, cred))) {
589 if ((err == ENOTCONN) && vnode_isvroot(vp)) {
590 /* see comment in fuse_vfsop_statfs() */
595 fuse_internal_vnode_disappear(vp);
600 fao = (struct fuse_attr_out *)fdi.answ;
601 fuse_internal_cache_attrs(vp, &fao->attr, fao->attr_valid,
602 fao->attr_valid_nsec, vap);
603 if (vap->va_type != vnode_vtype(vp)) {
604 fuse_internal_vnode_disappear(vp);
608 if ((fvdat->flag & FN_SIZECHANGE) != 0)
609 vap->va_size = fvdat->filesize;
611 if (vnode_isreg(vp) && (fvdat->flag & FN_SIZECHANGE) == 0) {
613 * This is for those cases when the file size changed without us
614 * knowing, and we want to catch up.
616 off_t new_filesize = ((struct fuse_attr_out *)
617 fdi.answ)->attr.size;
619 if (fvdat->filesize != new_filesize) {
620 fuse_vnode_setsize(vp, cred, new_filesize);
621 fvdat->flag &= ~FN_SIZECHANGE;
630 bzero(vap, sizeof(*vap));
631 vap->va_type = vnode_vtype(vp);
637 struct vnop_inactive_args {
643 fuse_vnop_inactive(struct vop_inactive_args *ap)
645 struct vnode *vp = ap->a_vp;
646 struct thread *td = ap->a_td;
648 struct fuse_vnode_data *fvdat = VTOFUD(vp);
649 struct fuse_filehandle *fufh, *fufh_tmp;
653 LIST_FOREACH_SAFE(fufh, &fvdat->handles, next, fufh_tmp) {
654 if (need_flush && vp->v_type == VREG) {
655 if ((VTOFUD(vp)->flag & FN_SIZECHANGE) != 0) {
656 fuse_vnode_savesize(vp, NULL, 0);
658 if (fuse_data_cache_invalidate ||
659 (fvdat->flag & FN_REVOKED) != 0)
660 fuse_io_invalbuf(vp, td);
662 fuse_io_flushbuf(vp, MNT_WAIT, td);
665 fuse_filehandle_close(vp, fufh, td, NULL);
668 if ((fvdat->flag & FN_REVOKED) != 0 && fuse_reclaim_revoked) {
675 struct vnop_link_args {
676 struct vnode *a_tdvp;
678 struct componentname *a_cnp;
682 fuse_vnop_link(struct vop_link_args *ap)
684 struct vnode *vp = ap->a_vp;
685 struct vnode *tdvp = ap->a_tdvp;
686 struct componentname *cnp = ap->a_cnp;
688 struct vattr *vap = VTOVA(vp);
690 struct fuse_dispatcher fdi;
691 struct fuse_entry_out *feo;
692 struct fuse_link_in fli;
696 if (fuse_isdeadfs(vp)) {
699 if (vnode_mount(tdvp) != vnode_mount(vp)) {
704 * This is a seatbelt check to protect naive userspace filesystems from
705 * themselves and the limitations of the FUSE IPC protocol. If a
706 * filesystem does not allow attribute caching, assume it is capable of
707 * validating that nlink does not overflow.
709 if (vap != NULL && vap->va_nlink >= FUSE_LINK_MAX)
711 fli.oldnodeid = VTOI(vp);
714 fuse_internal_newentry_makerequest(vnode_mount(tdvp), VTOI(tdvp), cnp,
715 FUSE_LINK, &fli, sizeof(fli), &fdi);
716 if ((err = fdisp_wait_answ(&fdi))) {
721 err = fuse_internal_checkentry(feo, vnode_vtype(vp));
728 struct vnop_lookup_args {
729 struct vnodeop_desc *a_desc;
731 struct vnode **a_vpp;
732 struct componentname *a_cnp;
736 fuse_vnop_lookup(struct vop_lookup_args *ap)
738 struct vnode *dvp = ap->a_dvp;
739 struct vnode **vpp = ap->a_vpp;
740 struct componentname *cnp = ap->a_cnp;
741 struct thread *td = cnp->cn_thread;
742 struct ucred *cred = cnp->cn_cred;
744 int nameiop = cnp->cn_nameiop;
745 int flags = cnp->cn_flags;
746 int wantparent = flags & (LOCKPARENT | WANTPARENT);
747 int islastcn = flags & ISLASTCN;
748 struct mount *mp = vnode_mount(dvp);
752 struct vnode *vp = NULL;
754 struct fuse_dispatcher fdi;
758 struct fuse_access_param facp;
760 if (fuse_isdeadfs(dvp)) {
764 if (!vnode_isdir(dvp)) {
767 if (islastcn && vfs_isrdonly(mp) && (nameiop != LOOKUP)) {
771 * We do access check prior to doing anything else only in the case
772 * when we are at fs root (we'd like to say, "we are at the first
773 * component", but that's not exactly the same... nevermind).
774 * See further comments at further access checks.
777 bzero(&facp, sizeof(facp));
778 if (vnode_isvroot(dvp)) { /* early permission check hack */
779 if ((err = fuse_internal_access(dvp, VEXEC, &facp, td, cred))) {
783 if (flags & ISDOTDOT) {
784 nid = VTOFUD(dvp)->parent_nid;
791 } else if (cnp->cn_namelen == 1 && *(cnp->cn_nameptr) == '.') {
796 } else if (fuse_lookup_cache_enable) {
797 err = cache_lookup(dvp, vpp, cnp, NULL, NULL);
800 case -1: /* positive match */
801 atomic_add_acq_long(&fuse_lookup_cache_hits, 1);
804 case 0: /* no match in cache */
805 atomic_add_acq_long(&fuse_lookup_cache_misses, 1);
808 case ENOENT: /* negative match */
815 fdisp_init(&fdi, cnp->cn_namelen + 1);
819 fdisp_make(&fdi, op, mp, nid, td, cred);
821 if (op == FUSE_LOOKUP) {
822 memcpy(fdi.indata, cnp->cn_nameptr, cnp->cn_namelen);
823 ((char *)fdi.indata)[cnp->cn_namelen] = '\0';
825 lookup_err = fdisp_wait_answ(&fdi);
827 if ((op == FUSE_LOOKUP) && !lookup_err) { /* lookup call succeeded */
828 nid = ((struct fuse_entry_out *)fdi.answ)->nodeid;
831 * zero nodeid is the same as "not found",
832 * but it's also cacheable (which we keep
833 * keep on doing not as of writing this)
835 fdi.answ_stat = ENOENT;
837 } else if (nid == FUSE_ROOT_ID) {
842 (!fdi.answ_stat || lookup_err != ENOENT || op != FUSE_LOOKUP)) {
846 /* lookup_err, if non-zero, must be ENOENT at this point */
850 if ((nameiop == CREATE || nameiop == RENAME) && islastcn
851 /* && directory dvp has not been removed */ ) {
853 if (vfs_isrdonly(mp)) {
857 #if 0 /* THINK_ABOUT_THIS */
858 if ((err = fuse_internal_access(dvp, VWRITE, cred, td, &facp))) {
864 * Possibly record the position of a slot in the
865 * directory large enough for the new component name.
866 * This can be recorded in the vnode private data for
867 * dvp. Set the SAVENAME flag to hold onto the
868 * pathname for use later in VOP_CREATE or VOP_RENAME.
870 cnp->cn_flags |= SAVENAME;
875 /* Consider inserting name into cache. */
878 * No we can't use negative caching, as the fs
879 * changes are out of our control.
880 * False positives' falseness turns out just as things
881 * go by, but false negatives' falseness doesn't.
882 * (and aiding the caching mechanism with extra control
883 * mechanisms comes quite close to beating the whole purpose
887 if ((cnp->cn_flags & MAKEENTRY) != 0) {
888 SDT_PROBE2(fuse, , vnops, trace, 1,
889 "inserting NULL into cache");
890 cache_enter(dvp, NULL, cnp);
900 struct fuse_entry_out *feo = NULL;
901 struct fuse_attr *fattr = NULL;
903 if (op == FUSE_GETATTR) {
904 fattr = &((struct fuse_attr_out *)fdi.answ)->attr;
906 feo = (struct fuse_entry_out *)fdi.answ;
907 fattr = &(feo->attr);
911 * If deleting, and at end of pathname, return parameters
912 * which can be used to remove file. If the wantparent flag
913 * isn't set, we return only the directory, otherwise we go on
914 * and lock the inode, being careful with ".".
916 if (nameiop == DELETE && islastcn) {
918 * Check for write access on directory.
920 facp.xuid = fattr->uid;
921 facp.facc_flags |= FACCESS_STICKY;
922 err = fuse_internal_access(dvp, VWRITE, &facp, td, cred);
923 facp.facc_flags &= ~FACCESS_XQUERIES;
928 if (nid == VTOI(dvp)) {
932 err = fuse_vnode_get(dvp->v_mount, feo, nid,
933 dvp, &vp, cnp, IFTOVT(fattr->mode));
940 * Save the name for use in VOP_RMDIR and VOP_REMOVE
943 cnp->cn_flags |= SAVENAME;
948 * If rewriting (RENAME), return the inode and the
949 * information required to rewrite the present directory
950 * Must get inode of directory entry to verify it's a
951 * regular file, or empty directory.
953 if (nameiop == RENAME && wantparent && islastcn) {
955 #if 0 /* THINK_ABOUT_THIS */
956 if ((err = fuse_internal_access(dvp, VWRITE, cred, td, &facp))) {
964 if (nid == VTOI(dvp)) {
968 err = fuse_vnode_get(vnode_mount(dvp), feo, nid, dvp,
969 &vp, cnp, IFTOVT(fattr->mode));
975 * Save the name for use in VOP_RENAME later.
977 cnp->cn_flags |= SAVENAME;
981 if (flags & ISDOTDOT) {
986 * Expanded copy of vn_vget_ino() so that
987 * fuse_vnode_get() can be used.
990 ltype = VOP_ISLOCKED(dvp);
991 err = vfs_busy(mp, MBF_NOWAIT);
995 err = vfs_busy(mp, 0);
996 vn_lock(dvp, ltype | LK_RETRY);
1000 if ((dvp->v_iflag & VI_DOOMED) != 0) {
1007 err = fuse_vnode_get(vnode_mount(dvp), feo, nid, NULL,
1008 &vp, cnp, IFTOVT(fattr->mode));
1010 vn_lock(dvp, ltype | LK_RETRY);
1011 if ((dvp->v_iflag & VI_DOOMED) != 0) {
1019 } else if (nid == VTOI(dvp)) {
1023 struct fuse_vnode_data *fvdat;
1025 err = fuse_vnode_get(vnode_mount(dvp), feo, nid, dvp,
1026 &vp, cnp, IFTOVT(fattr->mode));
1030 fuse_vnode_setparent(vp, dvp);
1033 * In the case where we are looking up a FUSE node
1034 * represented by an existing cached vnode, and the
1035 * true size reported by FUSE_LOOKUP doesn't match
1036 * the vnode's cached size, fix the vnode cache to
1037 * match the real object size.
1039 * This can occur via FUSE distributed filesystems,
1040 * irregular files, etc.
1043 if (vnode_isreg(vp) &&
1044 fattr->size != fvdat->filesize) {
1046 * The FN_SIZECHANGE flag reflects a dirty
1047 * append. If userspace lets us know our cache
1048 * is invalid, that write was lost. (Dirty
1049 * writes that do not cause append are also
1050 * lost, but we don't detect them here.)
1052 * XXX: Maybe disable WB caching on this mount.
1054 if (fvdat->flag & FN_SIZECHANGE)
1055 printf("%s: WB cache incoherent on "
1057 vnode_mount(vp)->mnt_stat.f_mntonname);
1059 (void)fuse_vnode_setsize(vp, cred, fattr->size);
1060 fvdat->flag &= ~FN_SIZECHANGE;
1065 if (op == FUSE_GETATTR) {
1066 struct fuse_attr_out *fao =
1067 (struct fuse_attr_out*)fdi.answ;
1068 fuse_internal_cache_attrs(*vpp,
1069 &fao->attr, fao->attr_valid,
1070 fao->attr_valid_nsec, NULL);
1072 struct fuse_entry_out *feo =
1073 (struct fuse_entry_out*)fdi.answ;
1074 fuse_internal_cache_attrs(*vpp,
1075 &feo->attr, feo->attr_valid,
1076 feo->attr_valid_nsec, NULL);
1079 /* Insert name into cache if appropriate. */
1082 * Nooo, caching is evil. With caching, we can't avoid stale
1083 * information taking over the playground (cached info is not
1084 * just positive/negative, it does have qualitative aspects,
1085 * too). And a (VOP/FUSE)_GETATTR is always thrown anyway, when
1086 * walking down along cached path components, and that's not
1087 * any cheaper than FUSE_LOOKUP. This might change with
1088 * implementing kernel side attr caching, but... In Linux,
1089 * lookup results are not cached, and the daemon is bombarded
1090 * with FUSE_LOOKUPS on and on. This shows that by design, the
1091 * daemon is expected to handle frequent lookup queries
1092 * efficiently, do its caching in userspace, and so on.
1094 * So just leave the name cache alone.
1098 * Well, now I know, Linux caches lookups, but with a
1099 * timeout... So it's the same thing as attribute caching:
1100 * we can deal with it when implement timeouts.
1103 if (cnp->cn_flags & MAKEENTRY) {
1104 cache_enter(dvp, *vpp, cnp);
1111 /* No lookup error; need to clean up. */
1113 if (err) { /* Found inode; exit with no vnode. */
1114 if (op == FUSE_LOOKUP) {
1115 fuse_internal_forget_send(vnode_mount(dvp), td, cred,
1118 fdisp_destroy(&fdi);
1121 #ifndef NO_EARLY_PERM_CHECK_HACK
1124 * We have the attributes of the next item
1125 * *now*, and it's a fact, and we do not
1126 * have to do extra work for it (ie, beg the
1127 * daemon), and it neither depends on such
1128 * accidental things like attr caching. So
1129 * the big idea: check credentials *now*,
1130 * not at the beginning of the next call to
1133 * The first item of the lookup chain (fs root)
1134 * won't be checked then here, of course, as
1135 * its never "the next". But go and see that
1136 * the root is taken care about at the very
1137 * beginning of this function.
1139 * Now, given we want to do the access check
1140 * this way, one might ask: so then why not
1141 * do the access check just after fetching
1142 * the inode and its attributes from the
1143 * daemon? Why bother with producing the
1144 * corresponding vnode at all if something
1145 * is not OK? We know what's the deal as
1146 * soon as we get those attrs... There is
1147 * one bit of info though not given us by
1148 * the daemon: whether his response is
1149 * authoritative or not... His response should
1150 * be ignored if something is mounted over
1151 * the dir in question. But that can be
1152 * known only by having the vnode...
1154 int tmpvtype = vnode_vtype(*vpp);
1156 bzero(&facp, sizeof(facp));
1157 /*the early perm check hack */
1158 facp.facc_flags |= FACCESS_VA_VALID;
1160 if ((tmpvtype != VDIR) && (tmpvtype != VLNK)) {
1163 if (!err && !vnode_mountedhere(*vpp)) {
1164 err = fuse_internal_access(*vpp, VEXEC, &facp, td, cred);
1167 if (tmpvtype == VLNK)
1168 SDT_PROBE2(fuse, , vnops, trace,
1169 1, "weird, permission "
1170 "error with a symlink?");
1178 fdisp_destroy(&fdi);
1184 struct vnop_mkdir_args {
1185 struct vnode *a_dvp;
1186 struct vnode **a_vpp;
1187 struct componentname *a_cnp;
1188 struct vattr *a_vap;
1192 fuse_vnop_mkdir(struct vop_mkdir_args *ap)
1194 struct vnode *dvp = ap->a_dvp;
1195 struct vnode **vpp = ap->a_vpp;
1196 struct componentname *cnp = ap->a_cnp;
1197 struct vattr *vap = ap->a_vap;
1199 struct fuse_mkdir_in fmdi;
1201 if (fuse_isdeadfs(dvp)) {
1204 fmdi.mode = MAKEIMODE(vap->va_type, vap->va_mode);
1206 return (fuse_internal_newentry(dvp, vpp, cnp, FUSE_MKDIR, &fmdi,
1207 sizeof(fmdi), VDIR));
1211 struct vnop_mknod_args {
1212 struct vnode *a_dvp;
1213 struct vnode **a_vpp;
1214 struct componentname *a_cnp;
1215 struct vattr *a_vap;
1219 fuse_vnop_mknod(struct vop_mknod_args *ap)
1222 struct vnode *dvp = ap->a_dvp;
1223 struct vnode **vpp = ap->a_vpp;
1224 struct componentname *cnp = ap->a_cnp;
1225 struct vattr *vap = ap->a_vap;
1226 struct fuse_mknod_in fmni;
1228 if (fuse_isdeadfs(dvp))
1231 fmni.mode = MAKEIMODE(vap->va_type, vap->va_mode);
1232 fmni.rdev = vap->va_rdev;
1233 return (fuse_internal_newentry(dvp, vpp, cnp, FUSE_MKNOD, &fmni,
1234 sizeof(fmni), vap->va_type));
1238 struct vnop_open_args {
1241 struct ucred *a_cred;
1242 struct thread *a_td;
1243 int a_fdidx; / struct file *a_fp;
1247 fuse_vnop_open(struct vop_open_args *ap)
1249 struct vnode *vp = ap->a_vp;
1250 int a_mode = ap->a_mode;
1251 struct thread *td = ap->a_td;
1252 struct ucred *cred = ap->a_cred;
1253 pid_t pid = td->td_proc->p_pid;
1254 struct fuse_vnode_data *fvdat;
1256 if (fuse_isdeadfs(vp))
1258 if (vp->v_type == VCHR || vp->v_type == VBLK || vp->v_type == VFIFO)
1259 return (EOPNOTSUPP);
1260 if ((a_mode & (FREAD | FWRITE | FEXEC)) == 0)
1265 if (fuse_filehandle_validrw(vp, a_mode, cred, pid)) {
1266 fuse_vnode_open(vp, 0, td);
1270 return fuse_filehandle_open(vp, a_mode, NULL, td, cred);
1274 fuse_vnop_pathconf(struct vop_pathconf_args *ap)
1277 switch (ap->a_name) {
1278 case _PC_FILESIZEBITS:
1282 *ap->a_retval = NAME_MAX;
1285 *ap->a_retval = MIN(LONG_MAX, FUSE_LINK_MAX);
1287 case _PC_SYMLINK_MAX:
1288 *ap->a_retval = MAXPATHLEN;
1294 return (vop_stdpathconf(ap));
1299 struct vnop_read_args {
1303 struct ucred *a_cred;
1307 fuse_vnop_read(struct vop_read_args *ap)
1309 struct vnode *vp = ap->a_vp;
1310 struct uio *uio = ap->a_uio;
1311 int ioflag = ap->a_ioflag;
1312 struct ucred *cred = ap->a_cred;
1313 pid_t pid = curthread->td_proc->p_pid;
1315 if (fuse_isdeadfs(vp)) {
1319 if (VTOFUD(vp)->flag & FN_DIRECTIO) {
1320 ioflag |= IO_DIRECT;
1323 return fuse_io_dispatch(vp, uio, ioflag, cred, pid);
1327 struct vnop_readdir_args {
1330 struct ucred *a_cred;
1337 fuse_vnop_readdir(struct vop_readdir_args *ap)
1339 struct vnode *vp = ap->a_vp;
1340 struct uio *uio = ap->a_uio;
1341 struct ucred *cred = ap->a_cred;
1342 struct fuse_filehandle *fufh = NULL;
1343 struct fuse_iov cookediov;
1346 pid_t pid = curthread->td_proc->p_pid;
1348 if (fuse_isdeadfs(vp)) {
1351 if ( /* XXXIP ((uio_iovcnt(uio) > 1)) || */
1352 (uio_resid(uio) < sizeof(struct dirent))) {
1356 if ((err = fuse_filehandle_get_dir(vp, &fufh, cred, pid)) != 0) {
1357 SDT_PROBE2(fuse, , vnops, trace, 1,
1358 "calling readdir() before open()");
1360 * This was seen to happen in getdirentries as used by
1361 * shells/fish, but I can't reproduce it.
1363 err = fuse_filehandle_open(vp, FREAD, &fufh, NULL, cred);
1369 #define DIRCOOKEDSIZE FUSE_DIRENT_ALIGN(FUSE_NAME_OFFSET + MAXNAMLEN + 1)
1370 fiov_init(&cookediov, DIRCOOKEDSIZE);
1372 err = fuse_internal_readdir(vp, uio, fufh, &cookediov);
1374 fiov_teardown(&cookediov);
1376 fuse_filehandle_close(vp, fufh, NULL, cred);
1382 struct vnop_readlink_args {
1385 struct ucred *a_cred;
1389 fuse_vnop_readlink(struct vop_readlink_args *ap)
1391 struct vnode *vp = ap->a_vp;
1392 struct uio *uio = ap->a_uio;
1393 struct ucred *cred = ap->a_cred;
1395 struct fuse_dispatcher fdi;
1398 if (fuse_isdeadfs(vp)) {
1401 if (!vnode_islnk(vp)) {
1404 fdisp_init(&fdi, 0);
1405 err = fdisp_simple_putget_vp(&fdi, FUSE_READLINK, vp, curthread, cred);
1409 if (((char *)fdi.answ)[0] == '/' &&
1410 fuse_get_mpdata(vnode_mount(vp))->dataflags & FSESS_PUSH_SYMLINKS_IN) {
1411 char *mpth = vnode_mount(vp)->mnt_stat.f_mntonname;
1413 err = uiomove(mpth, strlen(mpth), uio);
1416 err = uiomove(fdi.answ, fdi.iosize, uio);
1419 fdisp_destroy(&fdi);
1424 struct vnop_reclaim_args {
1426 struct thread *a_td;
1430 fuse_vnop_reclaim(struct vop_reclaim_args *ap)
1432 struct vnode *vp = ap->a_vp;
1433 struct thread *td = ap->a_td;
1434 struct fuse_vnode_data *fvdat = VTOFUD(vp);
1435 struct fuse_filehandle *fufh, *fufh_tmp;
1438 panic("FUSE: no vnode data during recycling");
1440 LIST_FOREACH_SAFE(fufh, &fvdat->handles, next, fufh_tmp) {
1441 printf("FUSE: vnode being reclaimed with open fufh "
1442 "(type=%#x)", fufh->fufh_type);
1443 fuse_filehandle_close(vp, fufh, td, NULL);
1446 if ((!fuse_isdeadfs(vp)) && (fvdat->nlookup)) {
1447 fuse_internal_forget_send(vnode_mount(vp), td, NULL, VTOI(vp),
1450 fuse_vnode_setparent(vp, NULL);
1452 vfs_hash_remove(vp);
1453 vnode_destroy_vobject(vp);
1454 fuse_vnode_destroy(vp);
1460 struct vnop_remove_args {
1461 struct vnode *a_dvp;
1463 struct componentname *a_cnp;
1467 fuse_vnop_remove(struct vop_remove_args *ap)
1469 struct vnode *dvp = ap->a_dvp;
1470 struct vnode *vp = ap->a_vp;
1471 struct componentname *cnp = ap->a_cnp;
1475 if (fuse_isdeadfs(vp)) {
1478 if (vnode_isdir(vp)) {
1483 err = fuse_internal_remove(dvp, vp, cnp, FUSE_UNLINK);
1486 fuse_internal_vnode_disappear(vp);
1491 struct vnop_rename_args {
1492 struct vnode *a_fdvp;
1493 struct vnode *a_fvp;
1494 struct componentname *a_fcnp;
1495 struct vnode *a_tdvp;
1496 struct vnode *a_tvp;
1497 struct componentname *a_tcnp;
1501 fuse_vnop_rename(struct vop_rename_args *ap)
1503 struct vnode *fdvp = ap->a_fdvp;
1504 struct vnode *fvp = ap->a_fvp;
1505 struct componentname *fcnp = ap->a_fcnp;
1506 struct vnode *tdvp = ap->a_tdvp;
1507 struct vnode *tvp = ap->a_tvp;
1508 struct componentname *tcnp = ap->a_tcnp;
1509 struct fuse_data *data;
1513 if (fuse_isdeadfs(fdvp)) {
1516 if (fvp->v_mount != tdvp->v_mount ||
1517 (tvp && fvp->v_mount != tvp->v_mount)) {
1518 SDT_PROBE2(fuse, , vnops, trace, 1, "cross-device rename");
1525 * FUSE library is expected to check if target directory is not
1526 * under the source directory in the file system tree.
1527 * Linux performs this check at VFS level.
1529 data = fuse_get_mpdata(vnode_mount(tdvp));
1530 sx_xlock(&data->rename_lock);
1531 err = fuse_internal_rename(fdvp, fcnp, tdvp, tcnp);
1534 fuse_vnode_setparent(fvp, tdvp);
1536 fuse_vnode_setparent(tvp, NULL);
1538 sx_unlock(&data->rename_lock);
1540 if (tvp != NULL && tvp != fvp) {
1543 if (vnode_isdir(fvp)) {
1544 if ((tvp != NULL) && vnode_isdir(tvp)) {
1565 struct vnop_rmdir_args {
1566 struct vnode *a_dvp;
1568 struct componentname *a_cnp;
1572 fuse_vnop_rmdir(struct vop_rmdir_args *ap)
1574 struct vnode *dvp = ap->a_dvp;
1575 struct vnode *vp = ap->a_vp;
1579 if (fuse_isdeadfs(vp)) {
1582 if (VTOFUD(vp) == VTOFUD(dvp)) {
1585 err = fuse_internal_remove(dvp, vp, ap->a_cnp, FUSE_RMDIR);
1588 fuse_internal_vnode_disappear(vp);
1593 struct vnop_setattr_args {
1595 struct vattr *a_vap;
1596 struct ucred *a_cred;
1597 struct thread *a_td;
1601 fuse_vnop_setattr(struct vop_setattr_args *ap)
1603 struct vnode *vp = ap->a_vp;
1604 struct vattr *vap = ap->a_vap;
1605 struct ucred *cred = ap->a_cred;
1606 struct thread *td = curthread;
1607 struct fuse_dispatcher fdi;
1608 struct fuse_setattr_in *fsai;
1609 struct fuse_access_param facp;
1610 pid_t pid = td->td_proc->p_pid;
1614 int sizechanged = 0;
1615 uint64_t newsize = 0;
1617 if (fuse_isdeadfs(vp)) {
1620 fdisp_init(&fdi, sizeof(*fsai));
1621 fdisp_make_vp(&fdi, FUSE_SETATTR, vp, td, cred);
1625 bzero(&facp, sizeof(facp));
1627 facp.xuid = vap->va_uid;
1628 facp.xgid = vap->va_gid;
1630 if (vap->va_uid != (uid_t)VNOVAL) {
1631 facp.facc_flags |= FACCESS_CHOWN;
1632 fsai->uid = vap->va_uid;
1633 fsai->valid |= FATTR_UID;
1635 if (vap->va_gid != (gid_t)VNOVAL) {
1636 facp.facc_flags |= FACCESS_CHOWN;
1637 fsai->gid = vap->va_gid;
1638 fsai->valid |= FATTR_GID;
1640 if (vap->va_size != VNOVAL) {
1642 struct fuse_filehandle *fufh = NULL;
1644 /*Truncate to a new value. */
1645 fsai->size = vap->va_size;
1647 newsize = vap->va_size;
1648 fsai->valid |= FATTR_SIZE;
1650 fuse_filehandle_getrw(vp, FWRITE, &fufh, cred, pid);
1652 fsai->fh = fufh->fh_id;
1653 fsai->valid |= FATTR_FH;
1656 if (vap->va_atime.tv_sec != VNOVAL) {
1657 fsai->atime = vap->va_atime.tv_sec;
1658 fsai->atimensec = vap->va_atime.tv_nsec;
1659 fsai->valid |= FATTR_ATIME;
1661 if (vap->va_mtime.tv_sec != VNOVAL) {
1662 fsai->mtime = vap->va_mtime.tv_sec;
1663 fsai->mtimensec = vap->va_mtime.tv_nsec;
1664 fsai->valid |= FATTR_MTIME;
1666 if (vap->va_mode != (mode_t)VNOVAL) {
1667 fsai->mode = vap->va_mode & ALLPERMS;
1668 fsai->valid |= FATTR_MODE;
1673 vtyp = vnode_vtype(vp);
1675 if (fsai->valid & FATTR_SIZE && vtyp == VDIR) {
1679 if (vfs_isrdonly(vnode_mount(vp)) && (fsai->valid & ~FATTR_SIZE || vtyp == VREG)) {
1684 if ((err = fdisp_wait_answ(&fdi)))
1686 vtyp = IFTOVT(((struct fuse_attr_out *)fdi.answ)->attr.mode);
1688 if (vnode_vtype(vp) != vtyp) {
1689 if (vnode_vtype(vp) == VNON && vtyp != VNON) {
1690 SDT_PROBE2(fuse, , vnops, trace, 1, "FUSE: Dang! "
1691 "vnode_vtype is VNON and vtype isn't.");
1694 * STALE vnode, ditch
1696 * The vnode has changed its type "behind our back".
1697 * There's nothing really we can do, so let us just
1698 * force an internal revocation and tell the caller to
1699 * try again, if interested.
1701 fuse_internal_vnode_disappear(vp);
1706 struct fuse_attr_out *fao = (struct fuse_attr_out*)fdi.answ;
1707 fuse_internal_cache_attrs(vp, &fao->attr, fao->attr_valid,
1708 fao->attr_valid_nsec, NULL);
1712 fdisp_destroy(&fdi);
1713 if (!err && sizechanged) {
1714 fuse_vnode_setsize(vp, cred, newsize);
1715 VTOFUD(vp)->flag &= ~FN_SIZECHANGE;
1721 struct vnop_strategy_args {
1727 fuse_vnop_strategy(struct vop_strategy_args *ap)
1729 struct vnode *vp = ap->a_vp;
1730 struct buf *bp = ap->a_bp;
1732 if (!vp || fuse_isdeadfs(vp)) {
1733 bp->b_ioflags |= BIO_ERROR;
1734 bp->b_error = ENXIO;
1738 if (bp->b_iocmd == BIO_WRITE) {
1741 err = fuse_vnode_refreshsize(vp, NOCRED);
1743 bp->b_ioflags |= BIO_ERROR;
1751 * VOP_STRATEGY always returns zero and signals error via bp->b_ioflags.
1752 * fuse_io_strategy sets bp's error fields
1754 (void)fuse_io_strategy(vp, bp);
1761 struct vnop_symlink_args {
1762 struct vnode *a_dvp;
1763 struct vnode **a_vpp;
1764 struct componentname *a_cnp;
1765 struct vattr *a_vap;
1770 fuse_vnop_symlink(struct vop_symlink_args *ap)
1772 struct vnode *dvp = ap->a_dvp;
1773 struct vnode **vpp = ap->a_vpp;
1774 struct componentname *cnp = ap->a_cnp;
1775 const char *target = ap->a_target;
1777 struct fuse_dispatcher fdi;
1782 if (fuse_isdeadfs(dvp)) {
1786 * Unlike the other creator type calls, here we have to create a message
1787 * where the name of the new entry comes first, and the data describing
1788 * the entry comes second.
1789 * Hence we can't rely on our handy fuse_internal_newentry() routine,
1790 * but put together the message manually and just call the core part.
1793 len = strlen(target) + 1;
1794 fdisp_init(&fdi, len + cnp->cn_namelen + 1);
1795 fdisp_make_vp(&fdi, FUSE_SYMLINK, dvp, curthread, NULL);
1797 memcpy(fdi.indata, cnp->cn_nameptr, cnp->cn_namelen);
1798 ((char *)fdi.indata)[cnp->cn_namelen] = '\0';
1799 memcpy((char *)fdi.indata + cnp->cn_namelen + 1, target, len);
1801 err = fuse_internal_newentry_core(dvp, vpp, cnp, VLNK, &fdi);
1802 fdisp_destroy(&fdi);
1807 struct vnop_write_args {
1811 struct ucred *a_cred;
1815 fuse_vnop_write(struct vop_write_args *ap)
1817 struct vnode *vp = ap->a_vp;
1818 struct uio *uio = ap->a_uio;
1819 int ioflag = ap->a_ioflag;
1820 struct ucred *cred = ap->a_cred;
1821 pid_t pid = curthread->td_proc->p_pid;
1824 if (fuse_isdeadfs(vp)) {
1827 err = fuse_vnode_refreshsize(vp, cred);
1831 if (VTOFUD(vp)->flag & FN_DIRECTIO) {
1832 ioflag |= IO_DIRECT;
1835 return fuse_io_dispatch(vp, uio, ioflag, cred, pid);
1838 SDT_PROBE_DEFINE1(fuse, , vnops, vnop_getpages_error, "int");
1840 struct vnop_getpages_args {
1848 fuse_vnop_getpages(struct vop_getpages_args *ap)
1850 int i, error, nextoff, size, toff, count, npages;
1859 pid_t pid = curthread->td_proc->p_pid;
1862 KASSERT(vp->v_object, ("objectless vp passed to getpages"));
1863 td = curthread; /* XXX */
1864 cred = curthread->td_ucred; /* XXX */
1866 npages = ap->a_count;
1868 if (!fsess_opt_mmap(vnode_mount(vp))) {
1869 SDT_PROBE2(fuse, , vnops, trace, 1,
1870 "called on non-cacheable vnode??\n");
1871 return (VM_PAGER_ERROR);
1875 * If the last page is partially valid, just return it and allow
1876 * the pager to zero-out the blanks. Partially valid pages can
1877 * only occur at the file EOF.
1879 * XXXGL: is that true for FUSE, which is a local filesystem,
1880 * but still somewhat disconnected from the kernel?
1882 VM_OBJECT_WLOCK(vp->v_object);
1883 if (pages[npages - 1]->valid != 0 && --npages == 0)
1885 VM_OBJECT_WUNLOCK(vp->v_object);
1888 * We use only the kva address for the buffer, but this is extremely
1889 * convenient and fast.
1891 bp = uma_zalloc(fuse_pbuf_zone, M_WAITOK);
1893 kva = (vm_offset_t)bp->b_data;
1894 pmap_qenter(kva, pages, npages);
1895 VM_CNT_INC(v_vnodein);
1896 VM_CNT_ADD(v_vnodepgsin, npages);
1898 count = npages << PAGE_SHIFT;
1899 iov.iov_base = (caddr_t)kva;
1900 iov.iov_len = count;
1903 uio.uio_offset = IDX_TO_OFF(pages[0]->pindex);
1904 uio.uio_resid = count;
1905 uio.uio_segflg = UIO_SYSSPACE;
1906 uio.uio_rw = UIO_READ;
1909 error = fuse_io_dispatch(vp, &uio, IO_DIRECT, cred, pid);
1910 pmap_qremove(kva, npages);
1912 uma_zfree(fuse_pbuf_zone, bp);
1914 if (error && (uio.uio_resid == count)) {
1915 SDT_PROBE1(fuse, , vnops, vnop_getpages_error, error);
1916 return VM_PAGER_ERROR;
1919 * Calculate the number of bytes read and validate only that number
1920 * of bytes. Note that due to pending writes, size may be 0. This
1921 * does not mean that the remaining data is invalid!
1924 size = count - uio.uio_resid;
1925 VM_OBJECT_WLOCK(vp->v_object);
1926 fuse_vm_page_lock_queues();
1927 for (i = 0, toff = 0; i < npages; i++, toff = nextoff) {
1930 nextoff = toff + PAGE_SIZE;
1933 if (nextoff <= size) {
1935 * Read operation filled an entire page
1937 m->valid = VM_PAGE_BITS_ALL;
1938 KASSERT(m->dirty == 0,
1939 ("fuse_getpages: page %p is dirty", m));
1940 } else if (size > toff) {
1942 * Read operation filled a partial page.
1945 vm_page_set_valid_range(m, 0, size - toff);
1946 KASSERT(m->dirty == 0,
1947 ("fuse_getpages: page %p is dirty", m));
1950 * Read operation was short. If no error occurred
1951 * we may have hit a zero-fill section. We simply
1952 * leave valid set to 0.
1957 fuse_vm_page_unlock_queues();
1959 VM_OBJECT_WUNLOCK(vp->v_object);
1964 return (VM_PAGER_OK);
1968 struct vnop_putpages_args {
1974 vm_ooffset_t a_offset;
1978 fuse_vnop_putpages(struct vop_putpages_args *ap)
1984 int i, error, npages, count;
1992 pid_t pid = curthread->td_proc->p_pid;
1995 KASSERT(vp->v_object, ("objectless vp passed to putpages"));
1996 fsize = vp->v_object->un_pager.vnp.vnp_size;
1997 td = curthread; /* XXX */
1998 cred = curthread->td_ucred; /* XXX */
2000 count = ap->a_count;
2001 rtvals = ap->a_rtvals;
2002 npages = btoc(count);
2003 offset = IDX_TO_OFF(pages[0]->pindex);
2005 if (!fsess_opt_mmap(vnode_mount(vp))) {
2006 SDT_PROBE2(fuse, , vnops, trace, 1,
2007 "called on non-cacheable vnode??\n");
2009 for (i = 0; i < npages; i++)
2010 rtvals[i] = VM_PAGER_AGAIN;
2013 * When putting pages, do not extend file past EOF.
2016 if (offset + count > fsize) {
2017 count = fsize - offset;
2022 * We use only the kva address for the buffer, but this is extremely
2023 * convenient and fast.
2025 bp = uma_zalloc(fuse_pbuf_zone, M_WAITOK);
2027 kva = (vm_offset_t)bp->b_data;
2028 pmap_qenter(kva, pages, npages);
2029 VM_CNT_INC(v_vnodeout);
2030 VM_CNT_ADD(v_vnodepgsout, count);
2032 iov.iov_base = (caddr_t)kva;
2033 iov.iov_len = count;
2036 uio.uio_offset = offset;
2037 uio.uio_resid = count;
2038 uio.uio_segflg = UIO_SYSSPACE;
2039 uio.uio_rw = UIO_WRITE;
2042 error = fuse_io_dispatch(vp, &uio, IO_DIRECT, cred, pid);
2044 pmap_qremove(kva, npages);
2045 uma_zfree(fuse_pbuf_zone, bp);
2048 int nwritten = round_page(count - uio.uio_resid) / PAGE_SIZE;
2050 for (i = 0; i < nwritten; i++) {
2051 rtvals[i] = VM_PAGER_OK;
2052 VM_OBJECT_WLOCK(pages[i]->object);
2053 vm_page_undirty(pages[i]);
2054 VM_OBJECT_WUNLOCK(pages[i]->object);
2060 static const char extattr_namespace_separator = '.';
2063 struct vop_getextattr_args {
2064 struct vop_generic_args a_gen;
2066 int a_attrnamespace;
2070 struct ucred *a_cred;
2071 struct thread *a_td;
2075 fuse_vnop_getextattr(struct vop_getextattr_args *ap)
2077 struct vnode *vp = ap->a_vp;
2078 struct uio *uio = ap->a_uio;
2079 struct fuse_dispatcher fdi;
2080 struct fuse_getxattr_in *get_xattr_in;
2081 struct fuse_getxattr_out *get_xattr_out;
2082 struct mount *mp = vnode_mount(vp);
2083 struct thread *td = ap->a_td;
2084 struct ucred *cred = ap->a_cred;
2090 if (fuse_isdeadfs(vp))
2093 /* Default to looking for user attributes. */
2094 if (ap->a_attrnamespace == EXTATTR_NAMESPACE_SYSTEM)
2095 prefix = EXTATTR_NAMESPACE_SYSTEM_STRING;
2097 prefix = EXTATTR_NAMESPACE_USER_STRING;
2099 len = strlen(prefix) + sizeof(extattr_namespace_separator) +
2100 strlen(ap->a_name) + 1;
2102 fdisp_init(&fdi, len + sizeof(*get_xattr_in));
2103 fdisp_make_vp(&fdi, FUSE_GETXATTR, vp, td, cred);
2105 get_xattr_in = fdi.indata;
2107 * Check to see whether we're querying the available size or
2108 * issuing the actual request. If we pass in 0, we get back struct
2109 * fuse_getxattr_out. If we pass in a non-zero size, we get back
2110 * that much data, without the struct fuse_getxattr_out header.
2113 get_xattr_in->size = 0;
2115 get_xattr_in->size = uio->uio_resid;
2117 attr_str = (char *)fdi.indata + sizeof(*get_xattr_in);
2118 snprintf(attr_str, len, "%s%c%s", prefix, extattr_namespace_separator,
2121 err = fdisp_wait_answ(&fdi);
2124 fsess_set_notimpl(mp, FUSE_GETXATTR);
2128 get_xattr_out = fdi.answ;
2130 if (ap->a_size != NULL)
2131 *ap->a_size = get_xattr_out->size;
2134 err = uiomove(fdi.answ, fdi.iosize, uio);
2137 fdisp_destroy(&fdi);
2142 struct vop_setextattr_args {
2143 struct vop_generic_args a_gen;
2145 int a_attrnamespace;
2148 struct ucred *a_cred;
2149 struct thread *a_td;
2153 fuse_vnop_setextattr(struct vop_setextattr_args *ap)
2155 struct vnode *vp = ap->a_vp;
2156 struct uio *uio = ap->a_uio;
2157 struct fuse_dispatcher fdi;
2158 struct fuse_setxattr_in *set_xattr_in;
2159 struct mount *mp = vnode_mount(vp);
2160 struct thread *td = ap->a_td;
2161 struct ucred *cred = ap->a_cred;
2167 if (fuse_isdeadfs(vp))
2170 /* Default to looking for user attributes. */
2171 if (ap->a_attrnamespace == EXTATTR_NAMESPACE_SYSTEM)
2172 prefix = EXTATTR_NAMESPACE_SYSTEM_STRING;
2174 prefix = EXTATTR_NAMESPACE_USER_STRING;
2176 len = strlen(prefix) + sizeof(extattr_namespace_separator) +
2177 strlen(ap->a_name) + 1;
2179 fdisp_init(&fdi, len + sizeof(*set_xattr_in) + uio->uio_resid);
2180 fdisp_make_vp(&fdi, FUSE_SETXATTR, vp, td, cred);
2182 set_xattr_in = fdi.indata;
2183 set_xattr_in->size = uio->uio_resid;
2185 attr_str = (char *)fdi.indata + sizeof(*set_xattr_in);
2186 snprintf(attr_str, len, "%s%c%s", prefix, extattr_namespace_separator,
2189 err = uiomove((char *)fdi.indata + sizeof(*set_xattr_in) + len,
2190 uio->uio_resid, uio);
2195 err = fdisp_wait_answ(&fdi);
2199 fsess_set_notimpl(mp, FUSE_SETXATTR);
2204 fdisp_destroy(&fdi);
2209 * The Linux / FUSE extended attribute list is simply a collection of
2210 * NUL-terminated strings. The FreeBSD extended attribute list is a single
2211 * byte length followed by a non-NUL terminated string. So, this allows
2212 * conversion of the Linux / FUSE format to the FreeBSD format in place.
2213 * Linux attribute names are reported with the namespace as a prefix (e.g.
2214 * "user.attribute_name"), but in FreeBSD they are reported without the
2215 * namespace prefix (e.g. "attribute_name"). So, we're going from:
2217 * user.attr_name1\0user.attr_name2\0
2221 * <num>attr_name1<num>attr_name2
2223 * Where "<num>" is a single byte number of characters in the attribute name.
2226 * prefix - exattr namespace prefix string
2227 * list, list_len - input list with namespace prefixes
2228 * bsd_list, bsd_list_len - output list compatible with bsd vfs
2231 fuse_xattrlist_convert(char *prefix, const char *list, int list_len,
2232 char *bsd_list, int *bsd_list_len)
2234 int len, pos, dist_to_next, prefix_len;
2238 prefix_len = strlen(prefix);
2240 while (pos < list_len && list[pos] != '\0') {
2241 dist_to_next = strlen(&list[pos]) + 1;
2242 if (bcmp(&list[pos], prefix, prefix_len) == 0 &&
2243 list[pos + prefix_len] == extattr_namespace_separator) {
2244 len = dist_to_next -
2245 (prefix_len + sizeof(extattr_namespace_separator)) - 1;
2246 if (len >= EXTATTR_MAXNAMELEN)
2247 return (ENAMETOOLONG);
2249 bsd_list[*bsd_list_len] = len;
2250 memcpy(&bsd_list[*bsd_list_len + 1],
2251 &list[pos + prefix_len +
2252 sizeof(extattr_namespace_separator)], len);
2254 *bsd_list_len += len + 1;
2257 pos += dist_to_next;
2264 struct vop_listextattr_args {
2265 struct vop_generic_args a_gen;
2267 int a_attrnamespace;
2270 struct ucred *a_cred;
2271 struct thread *a_td;
2275 fuse_vnop_listextattr(struct vop_listextattr_args *ap)
2277 struct vnode *vp = ap->a_vp;
2278 struct uio *uio = ap->a_uio;
2279 struct fuse_dispatcher fdi;
2280 struct fuse_listxattr_in *list_xattr_in;
2281 struct fuse_listxattr_out *list_xattr_out;
2282 struct mount *mp = vnode_mount(vp);
2283 struct thread *td = ap->a_td;
2284 struct ucred *cred = ap->a_cred;
2288 char *bsd_list = NULL;
2294 if (fuse_isdeadfs(vp))
2298 * Add space for a NUL and the period separator if enabled.
2299 * Default to looking for user attributes.
2301 if (ap->a_attrnamespace == EXTATTR_NAMESPACE_SYSTEM)
2302 prefix = EXTATTR_NAMESPACE_SYSTEM_STRING;
2304 prefix = EXTATTR_NAMESPACE_USER_STRING;
2306 len = strlen(prefix) + sizeof(extattr_namespace_separator) + 1;
2308 fdisp_init(&fdi, sizeof(*list_xattr_in) + len);
2309 fdisp_make_vp(&fdi, FUSE_LISTXATTR, vp, td, cred);
2312 * Retrieve Linux / FUSE compatible list size.
2314 list_xattr_in = fdi.indata;
2315 list_xattr_in->size = 0;
2316 attr_str = (char *)fdi.indata + sizeof(*list_xattr_in);
2317 snprintf(attr_str, len, "%s%c", prefix, extattr_namespace_separator);
2319 err = fdisp_wait_answ(&fdi);
2322 fsess_set_notimpl(mp, FUSE_LISTXATTR);
2326 list_xattr_out = fdi.answ;
2327 linux_list_len = list_xattr_out->size;
2328 if (linux_list_len == 0) {
2329 if (ap->a_size != NULL)
2330 *ap->a_size = linux_list_len;
2335 * Retrieve Linux / FUSE compatible list values.
2337 fdisp_make_vp(&fdi, FUSE_LISTXATTR, vp, td, cred);
2338 list_xattr_in = fdi.indata;
2339 list_xattr_in->size = linux_list_len + sizeof(*list_xattr_out);
2340 attr_str = (char *)fdi.indata + sizeof(*list_xattr_in);
2341 snprintf(attr_str, len, "%s%c", prefix, extattr_namespace_separator);
2343 err = fdisp_wait_answ(&fdi);
2347 linux_list = fdi.answ;
2348 linux_list_len = fdi.iosize;
2351 * Retrieve the BSD compatible list values.
2352 * The Linux / FUSE attribute list format isn't the same
2353 * as FreeBSD's format. So we need to transform it into
2354 * FreeBSD's format before giving it to the user.
2356 bsd_list = malloc(linux_list_len, M_TEMP, M_WAITOK);
2357 err = fuse_xattrlist_convert(prefix, linux_list, linux_list_len,
2358 bsd_list, &bsd_list_len);
2362 if (ap->a_size != NULL)
2363 *ap->a_size = bsd_list_len;
2366 err = uiomove(bsd_list, bsd_list_len, uio);
2369 free(bsd_list, M_TEMP);
2370 fdisp_destroy(&fdi);
2375 struct vop_deleteextattr_args {
2376 struct vop_generic_args a_gen;
2378 int a_attrnamespace;
2380 struct ucred *a_cred;
2381 struct thread *a_td;
2385 fuse_vnop_deleteextattr(struct vop_deleteextattr_args *ap)
2387 struct vnode *vp = ap->a_vp;
2388 struct fuse_dispatcher fdi;
2389 struct mount *mp = vnode_mount(vp);
2390 struct thread *td = ap->a_td;
2391 struct ucred *cred = ap->a_cred;
2397 if (fuse_isdeadfs(vp))
2400 /* Default to looking for user attributes. */
2401 if (ap->a_attrnamespace == EXTATTR_NAMESPACE_SYSTEM)
2402 prefix = EXTATTR_NAMESPACE_SYSTEM_STRING;
2404 prefix = EXTATTR_NAMESPACE_USER_STRING;
2406 len = strlen(prefix) + sizeof(extattr_namespace_separator) +
2407 strlen(ap->a_name) + 1;
2409 fdisp_init(&fdi, len);
2410 fdisp_make_vp(&fdi, FUSE_REMOVEXATTR, vp, td, cred);
2412 attr_str = fdi.indata;
2413 snprintf(attr_str, len, "%s%c%s", prefix, extattr_namespace_separator,
2416 err = fdisp_wait_answ(&fdi);
2419 fsess_set_notimpl(mp, FUSE_REMOVEXATTR);
2422 fdisp_destroy(&fdi);
2427 struct vnop_print_args {
2432 fuse_vnop_print(struct vop_print_args *ap)
2434 struct fuse_vnode_data *fvdat = VTOFUD(ap->a_vp);
2436 printf("nodeid: %ju, parent nodeid: %ju, nlookup: %ju, flag: %#x\n",
2437 (uintmax_t)VTOILLU(ap->a_vp), (uintmax_t)fvdat->parent_nid,
2438 (uintmax_t)fvdat->nlookup,