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)
220 struct vnop_access_args {
222 #if VOP_ACCESS_TAKES_ACCMODE_T
227 struct ucred *a_cred;
232 fuse_vnop_access(struct vop_access_args *ap)
234 struct vnode *vp = ap->a_vp;
235 int accmode = ap->a_accmode;
236 struct ucred *cred = ap->a_cred;
238 struct fuse_access_param facp;
239 struct fuse_data *data = fuse_get_mpdata(vnode_mount(vp));
243 if (fuse_isdeadfs(vp)) {
244 if (vnode_isvroot(vp)) {
249 if (!(data->dataflags & FSESS_INITED)) {
250 if (vnode_isvroot(vp)) {
251 if (priv_check_cred(cred, PRIV_VFS_ADMIN) ||
252 (fuse_match_cred(data->daemoncred, cred) == 0)) {
258 if (vnode_islnk(vp)) {
261 bzero(&facp, sizeof(facp));
263 err = fuse_internal_access(vp, accmode, &facp, ap->a_td, ap->a_cred);
268 struct vnop_close_args {
271 struct ucred *a_cred;
276 fuse_vnop_close(struct vop_close_args *ap)
278 struct vnode *vp = ap->a_vp;
279 struct ucred *cred = ap->a_cred;
280 int fflag = ap->a_fflag;
281 fufh_type_t fufh_type;
283 if (fuse_isdeadfs(vp)) {
286 if (vnode_isdir(vp)) {
287 if (fuse_filehandle_valid(vp, FUFH_RDONLY)) {
288 fuse_filehandle_close(vp, FUFH_RDONLY, NULL, cred);
292 if (fflag & IO_NDELAY) {
295 fufh_type = fuse_filehandle_xlate_from_fflags(fflag);
297 if (!fuse_filehandle_valid(vp, fufh_type)) {
300 for (i = 0; i < FUFH_MAXTYPE; i++)
301 if (fuse_filehandle_valid(vp, i))
303 if (i == FUFH_MAXTYPE)
304 panic("FUSE: fufh type %d found to be invalid in close"
308 if ((VTOFUD(vp)->flag & FN_SIZECHANGE) != 0) {
309 fuse_vnode_savesize(vp, cred);
315 fdisp_make_mknod_for_fallback(
316 struct fuse_dispatcher *fdip,
317 struct componentname *cnp,
323 enum fuse_opcode *op)
325 struct fuse_mknod_in *fmni;
327 fdisp_init(fdip, sizeof(*fmni) + cnp->cn_namelen + 1);
329 fdisp_make(fdip, *op, vnode_mount(dvp), parentnid, td, cred);
333 memcpy((char *)fdip->indata + sizeof(*fmni), cnp->cn_nameptr,
335 ((char *)fdip->indata)[sizeof(*fmni) + cnp->cn_namelen] = '\0';
338 struct vnop_create_args {
340 struct vnode **a_vpp;
341 struct componentname *a_cnp;
346 fuse_vnop_create(struct vop_create_args *ap)
348 struct vnode *dvp = ap->a_dvp;
349 struct vnode **vpp = ap->a_vpp;
350 struct componentname *cnp = ap->a_cnp;
351 struct vattr *vap = ap->a_vap;
352 struct thread *td = cnp->cn_thread;
353 struct ucred *cred = cnp->cn_cred;
355 struct fuse_open_in *foi;
356 struct fuse_entry_out *feo;
357 struct fuse_open_out *foo;
358 struct fuse_dispatcher fdi, fdi2;
359 struct fuse_dispatcher *fdip = &fdi;
360 struct fuse_dispatcher *fdip2 = NULL;
364 struct mount *mp = vnode_mount(dvp);
365 uint64_t parentnid = VTOFUD(dvp)->nid;
366 mode_t mode = MAKEIMODE(vap->va_type, vap->va_mode);
369 if (fuse_isdeadfs(dvp)) {
372 bzero(&fdi, sizeof(fdi));
374 if ((vap->va_type != VREG))
377 if (!fsess_isimpl(mp, FUSE_CREATE)) {
378 /* Fallback to FUSE_MKNOD/FUSE_OPEN */
379 fdisp_make_mknod_for_fallback(fdip, cnp, dvp, parentnid, td,
382 /* Use FUSE_CREATE */
384 fdisp_init(fdip, sizeof(*foi) + cnp->cn_namelen + 1);
385 fdisp_make(fdip, op, vnode_mount(dvp), parentnid, td, cred);
388 foi->flags = O_CREAT | O_RDWR;
389 memcpy((char *)fdip->indata + sizeof(*foi), cnp->cn_nameptr,
391 ((char *)fdip->indata)[sizeof(*foi) + cnp->cn_namelen] = '\0';
394 err = fdisp_wait_answ(fdip);
397 if (err == ENOSYS && op == FUSE_CREATE) {
398 fsess_set_notimpl(mp, FUSE_CREATE);
399 fdisp_make_mknod_for_fallback(fdip, cnp, dvp,
400 parentnid, td, cred, mode, &op);
401 err = fdisp_wait_answ(fdip);
409 if ((err = fuse_internal_checkentry(feo, VREG))) {
413 if (op == FUSE_CREATE) {
414 foo = (struct fuse_open_out*)(feo + 1);
416 /* Issue a separate FUSE_OPEN */
418 fdisp_init(fdip2, sizeof(*foi));
419 fdisp_make(fdip2, FUSE_OPEN, vnode_mount(dvp), feo->nodeid, td,
424 err = fdisp_wait_answ(fdip2);
429 err = fuse_vnode_get(mp, feo, feo->nodeid, dvp, vpp, cnp, VREG);
431 struct fuse_release_in *fri;
432 uint64_t nodeid = feo->nodeid;
433 uint64_t fh_id = foo->fh;
435 fdisp_init(fdip, sizeof(*fri));
436 fdisp_make(fdip, FUSE_RELEASE, mp, nodeid, td, cred);
439 fri->flags = fuse_filehandle_xlate_to_oflags(FUFH_RDWR);
440 fuse_insert_callback(fdip->tick, fuse_internal_forget_callback);
441 fuse_insert_message(fdip->tick);
444 ASSERT_VOP_ELOCKED(*vpp, "fuse_vnop_create");
446 fuse_filehandle_init(*vpp, FUFH_RDWR, NULL, foo->fh);
447 fuse_vnode_open(*vpp, foo->open_flags, td);
448 cache_purge_negative(dvp);
452 fdisp_destroy(fdip2);
458 struct vnop_fdatasync_args {
459 struct vop_generic_args a_gen;
461 struct thread * a_td;
465 fuse_vnop_fdatasync(struct vop_fdatasync_args *ap)
467 struct vnode *vp = ap->a_vp;
468 struct thread *td = ap->a_td;
469 int waitfor = MNT_WAIT;
473 if (fuse_isdeadfs(vp)) {
476 if ((err = vop_stdfdatasync_buf(ap)))
479 return fuse_internal_fsync(vp, td, waitfor, true);
483 struct vnop_fsync_args {
484 struct vop_generic_args a_gen;
487 struct thread * a_td;
491 fuse_vnop_fsync(struct vop_fsync_args *ap)
493 struct vnode *vp = ap->a_vp;
494 struct thread *td = ap->a_td;
495 int waitfor = ap->a_waitfor;
498 if (fuse_isdeadfs(vp)) {
501 if ((err = vop_stdfsync(ap)))
504 return fuse_internal_fsync(vp, td, waitfor, false);
508 struct vnop_getattr_args {
511 struct ucred *a_cred;
516 fuse_vnop_getattr(struct vop_getattr_args *ap)
518 struct vnode *vp = ap->a_vp;
519 struct vattr *vap = ap->a_vap;
520 struct ucred *cred = ap->a_cred;
521 struct thread *td = curthread;
522 struct fuse_vnode_data *fvdat = VTOFUD(vp);
523 struct fuse_attr_out *fao;
527 struct fuse_dispatcher fdi;
529 dataflags = fuse_get_mpdata(vnode_mount(vp))->dataflags;
531 /* Note that we are not bailing out on a dead file system just yet. */
533 if (!(dataflags & FSESS_INITED)) {
534 if (!vnode_isvroot(vp)) {
535 fdata_set_dead(fuse_get_mpdata(vnode_mount(vp)));
543 if ((err = fdisp_simple_putget_vp(&fdi, FUSE_GETATTR, vp, td, cred))) {
544 if ((err == ENOTCONN) && vnode_isvroot(vp)) {
545 /* see comment in fuse_vfsop_statfs() */
550 fuse_internal_vnode_disappear(vp);
555 fao = (struct fuse_attr_out *)fdi.answ;
556 fuse_internal_cache_attrs(vp, &fao->attr, fao->attr_valid,
557 fao->attr_valid_nsec, vap);
558 if (vap->va_type != vnode_vtype(vp)) {
559 fuse_internal_vnode_disappear(vp);
563 if ((fvdat->flag & FN_SIZECHANGE) != 0)
564 vap->va_size = fvdat->filesize;
566 if (vnode_isreg(vp) && (fvdat->flag & FN_SIZECHANGE) == 0) {
568 * This is for those cases when the file size changed without us
569 * knowing, and we want to catch up.
571 off_t new_filesize = ((struct fuse_attr_out *)
572 fdi.answ)->attr.size;
574 if (fvdat->filesize != new_filesize) {
575 fuse_vnode_setsize(vp, cred, new_filesize);
576 fvdat->flag &= ~FN_SIZECHANGE;
585 bzero(vap, sizeof(*vap));
586 vap->va_type = vnode_vtype(vp);
592 struct vnop_inactive_args {
598 fuse_vnop_inactive(struct vop_inactive_args *ap)
600 struct vnode *vp = ap->a_vp;
601 struct thread *td = ap->a_td;
603 struct fuse_vnode_data *fvdat = VTOFUD(vp);
604 struct fuse_filehandle *fufh = NULL;
606 int type, need_flush = 1;
608 for (type = 0; type < FUFH_MAXTYPE; type++) {
609 fufh = &(fvdat->fufh[type]);
610 if (FUFH_IS_VALID(fufh)) {
611 if (need_flush && vp->v_type == VREG) {
612 if ((VTOFUD(vp)->flag & FN_SIZECHANGE) != 0) {
613 fuse_vnode_savesize(vp, NULL);
615 if (fuse_data_cache_invalidate ||
616 (fvdat->flag & FN_REVOKED) != 0)
617 fuse_io_invalbuf(vp, td);
619 fuse_io_flushbuf(vp, MNT_WAIT, td);
622 fuse_filehandle_close(vp, type, td, NULL);
626 if ((fvdat->flag & FN_REVOKED) != 0 && fuse_reclaim_revoked) {
633 struct vnop_link_args {
634 struct vnode *a_tdvp;
636 struct componentname *a_cnp;
640 fuse_vnop_link(struct vop_link_args *ap)
642 struct vnode *vp = ap->a_vp;
643 struct vnode *tdvp = ap->a_tdvp;
644 struct componentname *cnp = ap->a_cnp;
646 struct vattr *vap = VTOVA(vp);
648 struct fuse_dispatcher fdi;
649 struct fuse_entry_out *feo;
650 struct fuse_link_in fli;
654 if (fuse_isdeadfs(vp)) {
657 if (vnode_mount(tdvp) != vnode_mount(vp)) {
662 * This is a seatbelt check to protect naive userspace filesystems from
663 * themselves and the limitations of the FUSE IPC protocol. If a
664 * filesystem does not allow attribute caching, assume it is capable of
665 * validating that nlink does not overflow.
667 if (vap != NULL && vap->va_nlink >= FUSE_LINK_MAX)
669 fli.oldnodeid = VTOI(vp);
672 fuse_internal_newentry_makerequest(vnode_mount(tdvp), VTOI(tdvp), cnp,
673 FUSE_LINK, &fli, sizeof(fli), &fdi);
674 if ((err = fdisp_wait_answ(&fdi))) {
679 err = fuse_internal_checkentry(feo, vnode_vtype(vp));
686 struct vnop_lookup_args {
687 struct vnodeop_desc *a_desc;
689 struct vnode **a_vpp;
690 struct componentname *a_cnp;
694 fuse_vnop_lookup(struct vop_lookup_args *ap)
696 struct vnode *dvp = ap->a_dvp;
697 struct vnode **vpp = ap->a_vpp;
698 struct componentname *cnp = ap->a_cnp;
699 struct thread *td = cnp->cn_thread;
700 struct ucred *cred = cnp->cn_cred;
702 int nameiop = cnp->cn_nameiop;
703 int flags = cnp->cn_flags;
704 int wantparent = flags & (LOCKPARENT | WANTPARENT);
705 int islastcn = flags & ISLASTCN;
706 struct mount *mp = vnode_mount(dvp);
710 struct vnode *vp = NULL;
712 struct fuse_dispatcher fdi;
716 struct fuse_access_param facp;
718 if (fuse_isdeadfs(dvp)) {
722 if (!vnode_isdir(dvp)) {
725 if (islastcn && vfs_isrdonly(mp) && (nameiop != LOOKUP)) {
729 * We do access check prior to doing anything else only in the case
730 * when we are at fs root (we'd like to say, "we are at the first
731 * component", but that's not exactly the same... nevermind).
732 * See further comments at further access checks.
735 bzero(&facp, sizeof(facp));
736 if (vnode_isvroot(dvp)) { /* early permission check hack */
737 if ((err = fuse_internal_access(dvp, VEXEC, &facp, td, cred))) {
741 if (flags & ISDOTDOT) {
742 nid = VTOFUD(dvp)->parent_nid;
749 } else if (cnp->cn_namelen == 1 && *(cnp->cn_nameptr) == '.') {
754 } else if (fuse_lookup_cache_enable) {
755 err = cache_lookup(dvp, vpp, cnp, NULL, NULL);
758 case -1: /* positive match */
759 atomic_add_acq_long(&fuse_lookup_cache_hits, 1);
762 case 0: /* no match in cache */
763 atomic_add_acq_long(&fuse_lookup_cache_misses, 1);
766 case ENOENT: /* negative match */
773 fdisp_init(&fdi, cnp->cn_namelen + 1);
777 fdisp_make(&fdi, op, mp, nid, td, cred);
779 if (op == FUSE_LOOKUP) {
780 memcpy(fdi.indata, cnp->cn_nameptr, cnp->cn_namelen);
781 ((char *)fdi.indata)[cnp->cn_namelen] = '\0';
783 lookup_err = fdisp_wait_answ(&fdi);
785 if ((op == FUSE_LOOKUP) && !lookup_err) { /* lookup call succeeded */
786 nid = ((struct fuse_entry_out *)fdi.answ)->nodeid;
789 * zero nodeid is the same as "not found",
790 * but it's also cacheable (which we keep
791 * keep on doing not as of writing this)
793 fdi.answ_stat = ENOENT;
795 } else if (nid == FUSE_ROOT_ID) {
800 (!fdi.answ_stat || lookup_err != ENOENT || op != FUSE_LOOKUP)) {
804 /* lookup_err, if non-zero, must be ENOENT at this point */
808 if ((nameiop == CREATE || nameiop == RENAME) && islastcn
809 /* && directory dvp has not been removed */ ) {
811 if (vfs_isrdonly(mp)) {
815 #if 0 /* THINK_ABOUT_THIS */
816 if ((err = fuse_internal_access(dvp, VWRITE, cred, td, &facp))) {
822 * Possibly record the position of a slot in the
823 * directory large enough for the new component name.
824 * This can be recorded in the vnode private data for
825 * dvp. Set the SAVENAME flag to hold onto the
826 * pathname for use later in VOP_CREATE or VOP_RENAME.
828 cnp->cn_flags |= SAVENAME;
833 /* Consider inserting name into cache. */
836 * No we can't use negative caching, as the fs
837 * changes are out of our control.
838 * False positives' falseness turns out just as things
839 * go by, but false negatives' falseness doesn't.
840 * (and aiding the caching mechanism with extra control
841 * mechanisms comes quite close to beating the whole purpose
845 if ((cnp->cn_flags & MAKEENTRY) != 0) {
846 SDT_PROBE2(fuse, , vnops, trace, 1,
847 "inserting NULL into cache");
848 cache_enter(dvp, NULL, cnp);
858 struct fuse_entry_out *feo = NULL;
859 struct fuse_attr *fattr = NULL;
861 if (op == FUSE_GETATTR) {
862 fattr = &((struct fuse_attr_out *)fdi.answ)->attr;
864 feo = (struct fuse_entry_out *)fdi.answ;
865 fattr = &(feo->attr);
869 * If deleting, and at end of pathname, return parameters
870 * which can be used to remove file. If the wantparent flag
871 * isn't set, we return only the directory, otherwise we go on
872 * and lock the inode, being careful with ".".
874 if (nameiop == DELETE && islastcn) {
876 * Check for write access on directory.
878 facp.xuid = fattr->uid;
879 facp.facc_flags |= FACCESS_STICKY;
880 err = fuse_internal_access(dvp, VWRITE, &facp, td, cred);
881 facp.facc_flags &= ~FACCESS_XQUERIES;
886 if (nid == VTOI(dvp)) {
890 err = fuse_vnode_get(dvp->v_mount, feo, nid,
891 dvp, &vp, cnp, IFTOVT(fattr->mode));
898 * Save the name for use in VOP_RMDIR and VOP_REMOVE
901 cnp->cn_flags |= SAVENAME;
906 * If rewriting (RENAME), return the inode and the
907 * information required to rewrite the present directory
908 * Must get inode of directory entry to verify it's a
909 * regular file, or empty directory.
911 if (nameiop == RENAME && wantparent && islastcn) {
913 #if 0 /* THINK_ABOUT_THIS */
914 if ((err = fuse_internal_access(dvp, VWRITE, cred, td, &facp))) {
922 if (nid == VTOI(dvp)) {
926 err = fuse_vnode_get(vnode_mount(dvp), feo, nid, dvp,
927 &vp, cnp, IFTOVT(fattr->mode));
933 * Save the name for use in VOP_RENAME later.
935 cnp->cn_flags |= SAVENAME;
939 if (flags & ISDOTDOT) {
944 * Expanded copy of vn_vget_ino() so that
945 * fuse_vnode_get() can be used.
948 ltype = VOP_ISLOCKED(dvp);
949 err = vfs_busy(mp, MBF_NOWAIT);
953 err = vfs_busy(mp, 0);
954 vn_lock(dvp, ltype | LK_RETRY);
958 if ((dvp->v_iflag & VI_DOOMED) != 0) {
965 err = fuse_vnode_get(vnode_mount(dvp), feo, nid, NULL,
966 &vp, cnp, IFTOVT(fattr->mode));
968 vn_lock(dvp, ltype | LK_RETRY);
969 if ((dvp->v_iflag & VI_DOOMED) != 0) {
977 } else if (nid == VTOI(dvp)) {
981 struct fuse_vnode_data *fvdat;
983 err = fuse_vnode_get(vnode_mount(dvp), feo, nid, dvp,
984 &vp, cnp, IFTOVT(fattr->mode));
988 fuse_vnode_setparent(vp, dvp);
991 * In the case where we are looking up a FUSE node
992 * represented by an existing cached vnode, and the
993 * true size reported by FUSE_LOOKUP doesn't match
994 * the vnode's cached size, fix the vnode cache to
995 * match the real object size.
997 * This can occur via FUSE distributed filesystems,
998 * irregular files, etc.
1001 if (vnode_isreg(vp) &&
1002 fattr->size != fvdat->filesize) {
1004 * The FN_SIZECHANGE flag reflects a dirty
1005 * append. If userspace lets us know our cache
1006 * is invalid, that write was lost. (Dirty
1007 * writes that do not cause append are also
1008 * lost, but we don't detect them here.)
1010 * XXX: Maybe disable WB caching on this mount.
1012 if (fvdat->flag & FN_SIZECHANGE)
1013 printf("%s: WB cache incoherent on "
1015 vnode_mount(vp)->mnt_stat.f_mntonname);
1017 (void)fuse_vnode_setsize(vp, cred, fattr->size);
1018 fvdat->flag &= ~FN_SIZECHANGE;
1023 if (op == FUSE_GETATTR) {
1024 struct fuse_attr_out *fao =
1025 (struct fuse_attr_out*)fdi.answ;
1026 fuse_internal_cache_attrs(*vpp,
1027 &fao->attr, fao->attr_valid,
1028 fao->attr_valid_nsec, NULL);
1030 struct fuse_entry_out *feo =
1031 (struct fuse_entry_out*)fdi.answ;
1032 fuse_internal_cache_attrs(*vpp,
1033 &feo->attr, feo->attr_valid,
1034 feo->attr_valid_nsec, NULL);
1037 /* Insert name into cache if appropriate. */
1040 * Nooo, caching is evil. With caching, we can't avoid stale
1041 * information taking over the playground (cached info is not
1042 * just positive/negative, it does have qualitative aspects,
1043 * too). And a (VOP/FUSE)_GETATTR is always thrown anyway, when
1044 * walking down along cached path components, and that's not
1045 * any cheaper than FUSE_LOOKUP. This might change with
1046 * implementing kernel side attr caching, but... In Linux,
1047 * lookup results are not cached, and the daemon is bombarded
1048 * with FUSE_LOOKUPS on and on. This shows that by design, the
1049 * daemon is expected to handle frequent lookup queries
1050 * efficiently, do its caching in userspace, and so on.
1052 * So just leave the name cache alone.
1056 * Well, now I know, Linux caches lookups, but with a
1057 * timeout... So it's the same thing as attribute caching:
1058 * we can deal with it when implement timeouts.
1061 if (cnp->cn_flags & MAKEENTRY) {
1062 cache_enter(dvp, *vpp, cnp);
1069 /* No lookup error; need to clean up. */
1071 if (err) { /* Found inode; exit with no vnode. */
1072 if (op == FUSE_LOOKUP) {
1073 fuse_internal_forget_send(vnode_mount(dvp), td, cred,
1076 fdisp_destroy(&fdi);
1079 #ifndef NO_EARLY_PERM_CHECK_HACK
1082 * We have the attributes of the next item
1083 * *now*, and it's a fact, and we do not
1084 * have to do extra work for it (ie, beg the
1085 * daemon), and it neither depends on such
1086 * accidental things like attr caching. So
1087 * the big idea: check credentials *now*,
1088 * not at the beginning of the next call to
1091 * The first item of the lookup chain (fs root)
1092 * won't be checked then here, of course, as
1093 * its never "the next". But go and see that
1094 * the root is taken care about at the very
1095 * beginning of this function.
1097 * Now, given we want to do the access check
1098 * this way, one might ask: so then why not
1099 * do the access check just after fetching
1100 * the inode and its attributes from the
1101 * daemon? Why bother with producing the
1102 * corresponding vnode at all if something
1103 * is not OK? We know what's the deal as
1104 * soon as we get those attrs... There is
1105 * one bit of info though not given us by
1106 * the daemon: whether his response is
1107 * authoritative or not... His response should
1108 * be ignored if something is mounted over
1109 * the dir in question. But that can be
1110 * known only by having the vnode...
1112 int tmpvtype = vnode_vtype(*vpp);
1114 bzero(&facp, sizeof(facp));
1115 /*the early perm check hack */
1116 facp.facc_flags |= FACCESS_VA_VALID;
1118 if ((tmpvtype != VDIR) && (tmpvtype != VLNK)) {
1121 if (!err && !vnode_mountedhere(*vpp)) {
1122 err = fuse_internal_access(*vpp, VEXEC, &facp, td, cred);
1125 if (tmpvtype == VLNK)
1126 SDT_PROBE2(fuse, , vnops, trace,
1127 1, "weird, permission "
1128 "error with a symlink?");
1136 fdisp_destroy(&fdi);
1142 struct vnop_mkdir_args {
1143 struct vnode *a_dvp;
1144 struct vnode **a_vpp;
1145 struct componentname *a_cnp;
1146 struct vattr *a_vap;
1150 fuse_vnop_mkdir(struct vop_mkdir_args *ap)
1152 struct vnode *dvp = ap->a_dvp;
1153 struct vnode **vpp = ap->a_vpp;
1154 struct componentname *cnp = ap->a_cnp;
1155 struct vattr *vap = ap->a_vap;
1157 struct fuse_mkdir_in fmdi;
1159 if (fuse_isdeadfs(dvp)) {
1162 fmdi.mode = MAKEIMODE(vap->va_type, vap->va_mode);
1164 return (fuse_internal_newentry(dvp, vpp, cnp, FUSE_MKDIR, &fmdi,
1165 sizeof(fmdi), VDIR));
1169 struct vnop_mknod_args {
1170 struct vnode *a_dvp;
1171 struct vnode **a_vpp;
1172 struct componentname *a_cnp;
1173 struct vattr *a_vap;
1177 fuse_vnop_mknod(struct vop_mknod_args *ap)
1180 struct vnode *dvp = ap->a_dvp;
1181 struct vnode **vpp = ap->a_vpp;
1182 struct componentname *cnp = ap->a_cnp;
1183 struct vattr *vap = ap->a_vap;
1184 struct fuse_mknod_in fmni;
1186 if (fuse_isdeadfs(dvp))
1189 fmni.mode = MAKEIMODE(vap->va_type, vap->va_mode);
1190 fmni.rdev = vap->va_rdev;
1191 return (fuse_internal_newentry(dvp, vpp, cnp, FUSE_MKNOD, &fmni,
1192 sizeof(fmni), vap->va_type));
1196 struct vnop_open_args {
1199 struct ucred *a_cred;
1200 struct thread *a_td;
1201 int a_fdidx; / struct file *a_fp;
1205 fuse_vnop_open(struct vop_open_args *ap)
1207 struct vnode *vp = ap->a_vp;
1208 int mode = ap->a_mode;
1209 struct thread *td = ap->a_td;
1210 struct ucred *cred = ap->a_cred;
1212 fufh_type_t fufh_type;
1213 struct fuse_vnode_data *fvdat;
1215 if (fuse_isdeadfs(vp))
1217 if (vp->v_type == VCHR || vp->v_type == VBLK || vp->v_type == VFIFO)
1218 return (EOPNOTSUPP);
1219 if ((mode & (FREAD | FWRITE)) == 0)
1224 if (vnode_isdir(vp)) {
1225 fufh_type = FUFH_RDONLY;
1227 fufh_type = fuse_filehandle_xlate_from_fflags(mode);
1230 if (fuse_filehandle_validrw(vp, fufh_type) != FUFH_INVALID) {
1231 fuse_vnode_open(vp, 0, td);
1235 return fuse_filehandle_open(vp, fufh_type, NULL, td, cred);
1239 fuse_vnop_pathconf(struct vop_pathconf_args *ap)
1242 switch (ap->a_name) {
1243 case _PC_FILESIZEBITS:
1247 *ap->a_retval = NAME_MAX;
1250 *ap->a_retval = MIN(LONG_MAX, FUSE_LINK_MAX);
1252 case _PC_SYMLINK_MAX:
1253 *ap->a_retval = MAXPATHLEN;
1259 return (vop_stdpathconf(ap));
1264 struct vnop_read_args {
1268 struct ucred *a_cred;
1272 fuse_vnop_read(struct vop_read_args *ap)
1274 struct vnode *vp = ap->a_vp;
1275 struct uio *uio = ap->a_uio;
1276 int ioflag = ap->a_ioflag;
1277 struct ucred *cred = ap->a_cred;
1279 if (fuse_isdeadfs(vp)) {
1283 if (VTOFUD(vp)->flag & FN_DIRECTIO) {
1284 ioflag |= IO_DIRECT;
1287 return fuse_io_dispatch(vp, uio, ioflag, cred);
1291 struct vnop_readdir_args {
1294 struct ucred *a_cred;
1301 fuse_vnop_readdir(struct vop_readdir_args *ap)
1303 struct vnode *vp = ap->a_vp;
1304 struct uio *uio = ap->a_uio;
1305 struct ucred *cred = ap->a_cred;
1307 struct fuse_filehandle *fufh = NULL;
1308 struct fuse_iov cookediov;
1313 if (fuse_isdeadfs(vp)) {
1316 if ( /* XXXIP ((uio_iovcnt(uio) > 1)) || */
1317 (uio_resid(uio) < sizeof(struct dirent))) {
1321 if (!fuse_filehandle_valid(vp, FUFH_RDONLY)) {
1322 SDT_PROBE2(fuse, , vnops, trace, 1,
1323 "calling readdir() before open()");
1324 err = fuse_filehandle_open(vp, FUFH_RDONLY, &fufh, NULL, cred);
1327 err = fuse_filehandle_get(vp, FUFH_RDONLY, &fufh);
1332 #define DIRCOOKEDSIZE FUSE_DIRENT_ALIGN(FUSE_NAME_OFFSET + MAXNAMLEN + 1)
1333 fiov_init(&cookediov, DIRCOOKEDSIZE);
1335 err = fuse_internal_readdir(vp, uio, fufh, &cookediov);
1337 fiov_teardown(&cookediov);
1339 fuse_filehandle_close(vp, FUFH_RDONLY, NULL, cred);
1345 struct vnop_readlink_args {
1348 struct ucred *a_cred;
1352 fuse_vnop_readlink(struct vop_readlink_args *ap)
1354 struct vnode *vp = ap->a_vp;
1355 struct uio *uio = ap->a_uio;
1356 struct ucred *cred = ap->a_cred;
1358 struct fuse_dispatcher fdi;
1361 if (fuse_isdeadfs(vp)) {
1364 if (!vnode_islnk(vp)) {
1367 fdisp_init(&fdi, 0);
1368 err = fdisp_simple_putget_vp(&fdi, FUSE_READLINK, vp, curthread, cred);
1372 if (((char *)fdi.answ)[0] == '/' &&
1373 fuse_get_mpdata(vnode_mount(vp))->dataflags & FSESS_PUSH_SYMLINKS_IN) {
1374 char *mpth = vnode_mount(vp)->mnt_stat.f_mntonname;
1376 err = uiomove(mpth, strlen(mpth), uio);
1379 err = uiomove(fdi.answ, fdi.iosize, uio);
1382 fdisp_destroy(&fdi);
1387 struct vnop_reclaim_args {
1389 struct thread *a_td;
1393 fuse_vnop_reclaim(struct vop_reclaim_args *ap)
1395 struct vnode *vp = ap->a_vp;
1396 struct thread *td = ap->a_td;
1398 struct fuse_vnode_data *fvdat = VTOFUD(vp);
1399 struct fuse_filehandle *fufh = NULL;
1404 panic("FUSE: no vnode data during recycling");
1406 for (type = 0; type < FUFH_MAXTYPE; type++) {
1407 fufh = &(fvdat->fufh[type]);
1408 if (FUFH_IS_VALID(fufh)) {
1409 printf("FUSE: vnode being reclaimed but fufh (type=%d) is valid",
1411 fuse_filehandle_close(vp, type, td, NULL);
1415 if ((!fuse_isdeadfs(vp)) && (fvdat->nlookup)) {
1416 fuse_internal_forget_send(vnode_mount(vp), td, NULL, VTOI(vp),
1419 fuse_vnode_setparent(vp, NULL);
1421 vfs_hash_remove(vp);
1422 vnode_destroy_vobject(vp);
1423 fuse_vnode_destroy(vp);
1429 struct vnop_remove_args {
1430 struct vnode *a_dvp;
1432 struct componentname *a_cnp;
1436 fuse_vnop_remove(struct vop_remove_args *ap)
1438 struct vnode *dvp = ap->a_dvp;
1439 struct vnode *vp = ap->a_vp;
1440 struct componentname *cnp = ap->a_cnp;
1444 if (fuse_isdeadfs(vp)) {
1447 if (vnode_isdir(vp)) {
1452 err = fuse_internal_remove(dvp, vp, cnp, FUSE_UNLINK);
1455 fuse_internal_vnode_disappear(vp);
1460 struct vnop_rename_args {
1461 struct vnode *a_fdvp;
1462 struct vnode *a_fvp;
1463 struct componentname *a_fcnp;
1464 struct vnode *a_tdvp;
1465 struct vnode *a_tvp;
1466 struct componentname *a_tcnp;
1470 fuse_vnop_rename(struct vop_rename_args *ap)
1472 struct vnode *fdvp = ap->a_fdvp;
1473 struct vnode *fvp = ap->a_fvp;
1474 struct componentname *fcnp = ap->a_fcnp;
1475 struct vnode *tdvp = ap->a_tdvp;
1476 struct vnode *tvp = ap->a_tvp;
1477 struct componentname *tcnp = ap->a_tcnp;
1478 struct fuse_data *data;
1482 if (fuse_isdeadfs(fdvp)) {
1485 if (fvp->v_mount != tdvp->v_mount ||
1486 (tvp && fvp->v_mount != tvp->v_mount)) {
1487 SDT_PROBE2(fuse, , vnops, trace, 1, "cross-device rename");
1494 * FUSE library is expected to check if target directory is not
1495 * under the source directory in the file system tree.
1496 * Linux performs this check at VFS level.
1498 data = fuse_get_mpdata(vnode_mount(tdvp));
1499 sx_xlock(&data->rename_lock);
1500 err = fuse_internal_rename(fdvp, fcnp, tdvp, tcnp);
1503 fuse_vnode_setparent(fvp, tdvp);
1505 fuse_vnode_setparent(tvp, NULL);
1507 sx_unlock(&data->rename_lock);
1509 if (tvp != NULL && tvp != fvp) {
1512 if (vnode_isdir(fvp)) {
1513 if ((tvp != NULL) && vnode_isdir(tvp)) {
1534 struct vnop_rmdir_args {
1535 struct vnode *a_dvp;
1537 struct componentname *a_cnp;
1541 fuse_vnop_rmdir(struct vop_rmdir_args *ap)
1543 struct vnode *dvp = ap->a_dvp;
1544 struct vnode *vp = ap->a_vp;
1548 if (fuse_isdeadfs(vp)) {
1551 if (VTOFUD(vp) == VTOFUD(dvp)) {
1554 err = fuse_internal_remove(dvp, vp, ap->a_cnp, FUSE_RMDIR);
1557 fuse_internal_vnode_disappear(vp);
1562 struct vnop_setattr_args {
1564 struct vattr *a_vap;
1565 struct ucred *a_cred;
1566 struct thread *a_td;
1570 fuse_vnop_setattr(struct vop_setattr_args *ap)
1572 struct vnode *vp = ap->a_vp;
1573 struct vattr *vap = ap->a_vap;
1574 struct ucred *cred = ap->a_cred;
1575 struct thread *td = curthread;
1576 struct fuse_dispatcher fdi;
1577 struct fuse_setattr_in *fsai;
1578 struct fuse_access_param facp;
1582 int sizechanged = 0;
1583 uint64_t newsize = 0;
1585 if (fuse_isdeadfs(vp)) {
1588 fdisp_init(&fdi, sizeof(*fsai));
1589 fdisp_make_vp(&fdi, FUSE_SETATTR, vp, td, cred);
1593 bzero(&facp, sizeof(facp));
1595 facp.xuid = vap->va_uid;
1596 facp.xgid = vap->va_gid;
1598 if (vap->va_uid != (uid_t)VNOVAL) {
1599 facp.facc_flags |= FACCESS_CHOWN;
1600 fsai->uid = vap->va_uid;
1601 fsai->valid |= FATTR_UID;
1603 if (vap->va_gid != (gid_t)VNOVAL) {
1604 facp.facc_flags |= FACCESS_CHOWN;
1605 fsai->gid = vap->va_gid;
1606 fsai->valid |= FATTR_GID;
1608 if (vap->va_size != VNOVAL) {
1610 struct fuse_filehandle *fufh = NULL;
1612 /*Truncate to a new value. */
1613 fsai->size = vap->va_size;
1615 newsize = vap->va_size;
1616 fsai->valid |= FATTR_SIZE;
1618 fuse_filehandle_getrw(vp, FUFH_WRONLY, &fufh);
1620 fsai->fh = fufh->fh_id;
1621 fsai->valid |= FATTR_FH;
1624 if (vap->va_atime.tv_sec != VNOVAL) {
1625 fsai->atime = vap->va_atime.tv_sec;
1626 fsai->atimensec = vap->va_atime.tv_nsec;
1627 fsai->valid |= FATTR_ATIME;
1629 if (vap->va_mtime.tv_sec != VNOVAL) {
1630 fsai->mtime = vap->va_mtime.tv_sec;
1631 fsai->mtimensec = vap->va_mtime.tv_nsec;
1632 fsai->valid |= FATTR_MTIME;
1634 if (vap->va_mode != (mode_t)VNOVAL) {
1635 fsai->mode = vap->va_mode & ALLPERMS;
1636 fsai->valid |= FATTR_MODE;
1641 vtyp = vnode_vtype(vp);
1643 if (fsai->valid & FATTR_SIZE && vtyp == VDIR) {
1647 if (vfs_isrdonly(vnode_mount(vp)) && (fsai->valid & ~FATTR_SIZE || vtyp == VREG)) {
1652 if ((err = fdisp_wait_answ(&fdi)))
1654 vtyp = IFTOVT(((struct fuse_attr_out *)fdi.answ)->attr.mode);
1656 if (vnode_vtype(vp) != vtyp) {
1657 if (vnode_vtype(vp) == VNON && vtyp != VNON) {
1658 SDT_PROBE2(fuse, , vnops, trace, 1, "FUSE: Dang! "
1659 "vnode_vtype is VNON and vtype isn't.");
1662 * STALE vnode, ditch
1664 * The vnode has changed its type "behind our back".
1665 * There's nothing really we can do, so let us just
1666 * force an internal revocation and tell the caller to
1667 * try again, if interested.
1669 fuse_internal_vnode_disappear(vp);
1674 struct fuse_attr_out *fao = (struct fuse_attr_out*)fdi.answ;
1675 fuse_internal_cache_attrs(vp, &fao->attr, fao->attr_valid,
1676 fao->attr_valid_nsec, NULL);
1680 fdisp_destroy(&fdi);
1681 if (!err && sizechanged) {
1682 fuse_vnode_setsize(vp, cred, newsize);
1683 VTOFUD(vp)->flag &= ~FN_SIZECHANGE;
1689 struct vnop_strategy_args {
1695 fuse_vnop_strategy(struct vop_strategy_args *ap)
1697 struct vnode *vp = ap->a_vp;
1698 struct buf *bp = ap->a_bp;
1700 if (!vp || fuse_isdeadfs(vp)) {
1701 bp->b_ioflags |= BIO_ERROR;
1702 bp->b_error = ENXIO;
1706 if (bp->b_iocmd == BIO_WRITE) {
1709 err = fuse_vnode_refreshsize(vp, NOCRED);
1711 bp->b_ioflags |= BIO_ERROR;
1719 * VOP_STRATEGY always returns zero and signals error via bp->b_ioflags.
1720 * fuse_io_strategy sets bp's error fields
1722 (void)fuse_io_strategy(vp, bp);
1729 struct vnop_symlink_args {
1730 struct vnode *a_dvp;
1731 struct vnode **a_vpp;
1732 struct componentname *a_cnp;
1733 struct vattr *a_vap;
1738 fuse_vnop_symlink(struct vop_symlink_args *ap)
1740 struct vnode *dvp = ap->a_dvp;
1741 struct vnode **vpp = ap->a_vpp;
1742 struct componentname *cnp = ap->a_cnp;
1743 const char *target = ap->a_target;
1745 struct fuse_dispatcher fdi;
1750 if (fuse_isdeadfs(dvp)) {
1754 * Unlike the other creator type calls, here we have to create a message
1755 * where the name of the new entry comes first, and the data describing
1756 * the entry comes second.
1757 * Hence we can't rely on our handy fuse_internal_newentry() routine,
1758 * but put together the message manually and just call the core part.
1761 len = strlen(target) + 1;
1762 fdisp_init(&fdi, len + cnp->cn_namelen + 1);
1763 fdisp_make_vp(&fdi, FUSE_SYMLINK, dvp, curthread, NULL);
1765 memcpy(fdi.indata, cnp->cn_nameptr, cnp->cn_namelen);
1766 ((char *)fdi.indata)[cnp->cn_namelen] = '\0';
1767 memcpy((char *)fdi.indata + cnp->cn_namelen + 1, target, len);
1769 err = fuse_internal_newentry_core(dvp, vpp, cnp, VLNK, &fdi);
1770 fdisp_destroy(&fdi);
1775 struct vnop_write_args {
1779 struct ucred *a_cred;
1783 fuse_vnop_write(struct vop_write_args *ap)
1785 struct vnode *vp = ap->a_vp;
1786 struct uio *uio = ap->a_uio;
1787 int ioflag = ap->a_ioflag;
1788 struct ucred *cred = ap->a_cred;
1791 if (fuse_isdeadfs(vp)) {
1794 err = fuse_vnode_refreshsize(vp, cred);
1798 if (VTOFUD(vp)->flag & FN_DIRECTIO) {
1799 ioflag |= IO_DIRECT;
1802 return fuse_io_dispatch(vp, uio, ioflag, cred);
1805 SDT_PROBE_DEFINE1(fuse, , vnops, vnop_getpages_error, "int");
1807 struct vnop_getpages_args {
1815 fuse_vnop_getpages(struct vop_getpages_args *ap)
1817 int i, error, nextoff, size, toff, count, npages;
1828 KASSERT(vp->v_object, ("objectless vp passed to getpages"));
1829 td = curthread; /* XXX */
1830 cred = curthread->td_ucred; /* XXX */
1832 npages = ap->a_count;
1834 if (!fsess_opt_mmap(vnode_mount(vp))) {
1835 SDT_PROBE2(fuse, , vnops, trace, 1,
1836 "called on non-cacheable vnode??\n");
1837 return (VM_PAGER_ERROR);
1841 * If the last page is partially valid, just return it and allow
1842 * the pager to zero-out the blanks. Partially valid pages can
1843 * only occur at the file EOF.
1845 * XXXGL: is that true for FUSE, which is a local filesystem,
1846 * but still somewhat disconnected from the kernel?
1848 VM_OBJECT_WLOCK(vp->v_object);
1849 if (pages[npages - 1]->valid != 0 && --npages == 0)
1851 VM_OBJECT_WUNLOCK(vp->v_object);
1854 * We use only the kva address for the buffer, but this is extremely
1855 * convenient and fast.
1857 bp = uma_zalloc(fuse_pbuf_zone, M_WAITOK);
1859 kva = (vm_offset_t)bp->b_data;
1860 pmap_qenter(kva, pages, npages);
1861 VM_CNT_INC(v_vnodein);
1862 VM_CNT_ADD(v_vnodepgsin, npages);
1864 count = npages << PAGE_SHIFT;
1865 iov.iov_base = (caddr_t)kva;
1866 iov.iov_len = count;
1869 uio.uio_offset = IDX_TO_OFF(pages[0]->pindex);
1870 uio.uio_resid = count;
1871 uio.uio_segflg = UIO_SYSSPACE;
1872 uio.uio_rw = UIO_READ;
1875 error = fuse_io_dispatch(vp, &uio, IO_DIRECT, cred);
1876 pmap_qremove(kva, npages);
1878 uma_zfree(fuse_pbuf_zone, bp);
1880 if (error && (uio.uio_resid == count)) {
1881 SDT_PROBE1(fuse, , vnops, vnop_getpages_error, error);
1882 return VM_PAGER_ERROR;
1885 * Calculate the number of bytes read and validate only that number
1886 * of bytes. Note that due to pending writes, size may be 0. This
1887 * does not mean that the remaining data is invalid!
1890 size = count - uio.uio_resid;
1891 VM_OBJECT_WLOCK(vp->v_object);
1892 fuse_vm_page_lock_queues();
1893 for (i = 0, toff = 0; i < npages; i++, toff = nextoff) {
1896 nextoff = toff + PAGE_SIZE;
1899 if (nextoff <= size) {
1901 * Read operation filled an entire page
1903 m->valid = VM_PAGE_BITS_ALL;
1904 KASSERT(m->dirty == 0,
1905 ("fuse_getpages: page %p is dirty", m));
1906 } else if (size > toff) {
1908 * Read operation filled a partial page.
1911 vm_page_set_valid_range(m, 0, size - toff);
1912 KASSERT(m->dirty == 0,
1913 ("fuse_getpages: page %p is dirty", m));
1916 * Read operation was short. If no error occurred
1917 * we may have hit a zero-fill section. We simply
1918 * leave valid set to 0.
1923 fuse_vm_page_unlock_queues();
1925 VM_OBJECT_WUNLOCK(vp->v_object);
1930 return (VM_PAGER_OK);
1934 struct vnop_putpages_args {
1940 vm_ooffset_t a_offset;
1944 fuse_vnop_putpages(struct vop_putpages_args *ap)
1950 int i, error, npages, count;
1960 KASSERT(vp->v_object, ("objectless vp passed to putpages"));
1961 fsize = vp->v_object->un_pager.vnp.vnp_size;
1962 td = curthread; /* XXX */
1963 cred = curthread->td_ucred; /* XXX */
1965 count = ap->a_count;
1966 rtvals = ap->a_rtvals;
1967 npages = btoc(count);
1968 offset = IDX_TO_OFF(pages[0]->pindex);
1970 if (!fsess_opt_mmap(vnode_mount(vp))) {
1971 SDT_PROBE2(fuse, , vnops, trace, 1,
1972 "called on non-cacheable vnode??\n");
1974 for (i = 0; i < npages; i++)
1975 rtvals[i] = VM_PAGER_AGAIN;
1978 * When putting pages, do not extend file past EOF.
1981 if (offset + count > fsize) {
1982 count = fsize - offset;
1987 * We use only the kva address for the buffer, but this is extremely
1988 * convenient and fast.
1990 bp = uma_zalloc(fuse_pbuf_zone, M_WAITOK);
1992 kva = (vm_offset_t)bp->b_data;
1993 pmap_qenter(kva, pages, npages);
1994 VM_CNT_INC(v_vnodeout);
1995 VM_CNT_ADD(v_vnodepgsout, count);
1997 iov.iov_base = (caddr_t)kva;
1998 iov.iov_len = count;
2001 uio.uio_offset = offset;
2002 uio.uio_resid = count;
2003 uio.uio_segflg = UIO_SYSSPACE;
2004 uio.uio_rw = UIO_WRITE;
2007 error = fuse_io_dispatch(vp, &uio, IO_DIRECT, cred);
2009 pmap_qremove(kva, npages);
2010 uma_zfree(fuse_pbuf_zone, bp);
2013 int nwritten = round_page(count - uio.uio_resid) / PAGE_SIZE;
2015 for (i = 0; i < nwritten; i++) {
2016 rtvals[i] = VM_PAGER_OK;
2017 VM_OBJECT_WLOCK(pages[i]->object);
2018 vm_page_undirty(pages[i]);
2019 VM_OBJECT_WUNLOCK(pages[i]->object);
2025 static const char extattr_namespace_separator = '.';
2028 struct vop_getextattr_args {
2029 struct vop_generic_args a_gen;
2031 int a_attrnamespace;
2035 struct ucred *a_cred;
2036 struct thread *a_td;
2040 fuse_vnop_getextattr(struct vop_getextattr_args *ap)
2042 struct vnode *vp = ap->a_vp;
2043 struct uio *uio = ap->a_uio;
2044 struct fuse_dispatcher fdi;
2045 struct fuse_getxattr_in *get_xattr_in;
2046 struct fuse_getxattr_out *get_xattr_out;
2047 struct mount *mp = vnode_mount(vp);
2048 struct thread *td = ap->a_td;
2049 struct ucred *cred = ap->a_cred;
2055 if (fuse_isdeadfs(vp))
2058 /* Default to looking for user attributes. */
2059 if (ap->a_attrnamespace == EXTATTR_NAMESPACE_SYSTEM)
2060 prefix = EXTATTR_NAMESPACE_SYSTEM_STRING;
2062 prefix = EXTATTR_NAMESPACE_USER_STRING;
2064 len = strlen(prefix) + sizeof(extattr_namespace_separator) +
2065 strlen(ap->a_name) + 1;
2067 fdisp_init(&fdi, len + sizeof(*get_xattr_in));
2068 fdisp_make_vp(&fdi, FUSE_GETXATTR, vp, td, cred);
2070 get_xattr_in = fdi.indata;
2072 * Check to see whether we're querying the available size or
2073 * issuing the actual request. If we pass in 0, we get back struct
2074 * fuse_getxattr_out. If we pass in a non-zero size, we get back
2075 * that much data, without the struct fuse_getxattr_out header.
2078 get_xattr_in->size = 0;
2080 get_xattr_in->size = uio->uio_resid;
2082 attr_str = (char *)fdi.indata + sizeof(*get_xattr_in);
2083 snprintf(attr_str, len, "%s%c%s", prefix, extattr_namespace_separator,
2086 err = fdisp_wait_answ(&fdi);
2089 fsess_set_notimpl(mp, FUSE_GETXATTR);
2093 get_xattr_out = fdi.answ;
2095 if (ap->a_size != NULL)
2096 *ap->a_size = get_xattr_out->size;
2099 err = uiomove(fdi.answ, fdi.iosize, uio);
2102 fdisp_destroy(&fdi);
2107 struct vop_setextattr_args {
2108 struct vop_generic_args a_gen;
2110 int a_attrnamespace;
2113 struct ucred *a_cred;
2114 struct thread *a_td;
2118 fuse_vnop_setextattr(struct vop_setextattr_args *ap)
2120 struct vnode *vp = ap->a_vp;
2121 struct uio *uio = ap->a_uio;
2122 struct fuse_dispatcher fdi;
2123 struct fuse_setxattr_in *set_xattr_in;
2124 struct mount *mp = vnode_mount(vp);
2125 struct thread *td = ap->a_td;
2126 struct ucred *cred = ap->a_cred;
2132 if (fuse_isdeadfs(vp))
2135 /* Default to looking for user attributes. */
2136 if (ap->a_attrnamespace == EXTATTR_NAMESPACE_SYSTEM)
2137 prefix = EXTATTR_NAMESPACE_SYSTEM_STRING;
2139 prefix = EXTATTR_NAMESPACE_USER_STRING;
2141 len = strlen(prefix) + sizeof(extattr_namespace_separator) +
2142 strlen(ap->a_name) + 1;
2144 fdisp_init(&fdi, len + sizeof(*set_xattr_in) + uio->uio_resid);
2145 fdisp_make_vp(&fdi, FUSE_SETXATTR, vp, td, cred);
2147 set_xattr_in = fdi.indata;
2148 set_xattr_in->size = uio->uio_resid;
2150 attr_str = (char *)fdi.indata + sizeof(*set_xattr_in);
2151 snprintf(attr_str, len, "%s%c%s", prefix, extattr_namespace_separator,
2154 err = uiomove((char *)fdi.indata + sizeof(*set_xattr_in) + len,
2155 uio->uio_resid, uio);
2160 err = fdisp_wait_answ(&fdi);
2164 fsess_set_notimpl(mp, FUSE_SETXATTR);
2169 fdisp_destroy(&fdi);
2174 * The Linux / FUSE extended attribute list is simply a collection of
2175 * NUL-terminated strings. The FreeBSD extended attribute list is a single
2176 * byte length followed by a non-NUL terminated string. So, this allows
2177 * conversion of the Linux / FUSE format to the FreeBSD format in place.
2178 * Linux attribute names are reported with the namespace as a prefix (e.g.
2179 * "user.attribute_name"), but in FreeBSD they are reported without the
2180 * namespace prefix (e.g. "attribute_name"). So, we're going from:
2182 * user.attr_name1\0user.attr_name2\0
2186 * <num>attr_name1<num>attr_name2
2188 * Where "<num>" is a single byte number of characters in the attribute name.
2191 * prefix - exattr namespace prefix string
2192 * list, list_len - input list with namespace prefixes
2193 * bsd_list, bsd_list_len - output list compatible with bsd vfs
2196 fuse_xattrlist_convert(char *prefix, const char *list, int list_len,
2197 char *bsd_list, int *bsd_list_len)
2199 int len, pos, dist_to_next, prefix_len;
2203 prefix_len = strlen(prefix);
2205 while (pos < list_len && list[pos] != '\0') {
2206 dist_to_next = strlen(&list[pos]) + 1;
2207 if (bcmp(&list[pos], prefix, prefix_len) == 0 &&
2208 list[pos + prefix_len] == extattr_namespace_separator) {
2209 len = dist_to_next -
2210 (prefix_len + sizeof(extattr_namespace_separator)) - 1;
2211 if (len >= EXTATTR_MAXNAMELEN)
2212 return (ENAMETOOLONG);
2214 bsd_list[*bsd_list_len] = len;
2215 memcpy(&bsd_list[*bsd_list_len + 1],
2216 &list[pos + prefix_len +
2217 sizeof(extattr_namespace_separator)], len);
2219 *bsd_list_len += len + 1;
2222 pos += dist_to_next;
2229 struct vop_listextattr_args {
2230 struct vop_generic_args a_gen;
2232 int a_attrnamespace;
2235 struct ucred *a_cred;
2236 struct thread *a_td;
2240 fuse_vnop_listextattr(struct vop_listextattr_args *ap)
2242 struct vnode *vp = ap->a_vp;
2243 struct uio *uio = ap->a_uio;
2244 struct fuse_dispatcher fdi;
2245 struct fuse_listxattr_in *list_xattr_in;
2246 struct fuse_listxattr_out *list_xattr_out;
2247 struct mount *mp = vnode_mount(vp);
2248 struct thread *td = ap->a_td;
2249 struct ucred *cred = ap->a_cred;
2253 char *bsd_list = NULL;
2259 if (fuse_isdeadfs(vp))
2263 * Add space for a NUL and the period separator if enabled.
2264 * Default to looking for user attributes.
2266 if (ap->a_attrnamespace == EXTATTR_NAMESPACE_SYSTEM)
2267 prefix = EXTATTR_NAMESPACE_SYSTEM_STRING;
2269 prefix = EXTATTR_NAMESPACE_USER_STRING;
2271 len = strlen(prefix) + sizeof(extattr_namespace_separator) + 1;
2273 fdisp_init(&fdi, sizeof(*list_xattr_in) + len);
2274 fdisp_make_vp(&fdi, FUSE_LISTXATTR, vp, td, cred);
2277 * Retrieve Linux / FUSE compatible list size.
2279 list_xattr_in = fdi.indata;
2280 list_xattr_in->size = 0;
2281 attr_str = (char *)fdi.indata + sizeof(*list_xattr_in);
2282 snprintf(attr_str, len, "%s%c", prefix, extattr_namespace_separator);
2284 err = fdisp_wait_answ(&fdi);
2287 fsess_set_notimpl(mp, FUSE_LISTXATTR);
2291 list_xattr_out = fdi.answ;
2292 linux_list_len = list_xattr_out->size;
2293 if (linux_list_len == 0) {
2294 if (ap->a_size != NULL)
2295 *ap->a_size = linux_list_len;
2300 * Retrieve Linux / FUSE compatible list values.
2302 fdisp_make_vp(&fdi, FUSE_LISTXATTR, vp, td, cred);
2303 list_xattr_in = fdi.indata;
2304 list_xattr_in->size = linux_list_len + sizeof(*list_xattr_out);
2305 attr_str = (char *)fdi.indata + sizeof(*list_xattr_in);
2306 snprintf(attr_str, len, "%s%c", prefix, extattr_namespace_separator);
2308 err = fdisp_wait_answ(&fdi);
2312 linux_list = fdi.answ;
2313 linux_list_len = fdi.iosize;
2316 * Retrieve the BSD compatible list values.
2317 * The Linux / FUSE attribute list format isn't the same
2318 * as FreeBSD's format. So we need to transform it into
2319 * FreeBSD's format before giving it to the user.
2321 bsd_list = malloc(linux_list_len, M_TEMP, M_WAITOK);
2322 err = fuse_xattrlist_convert(prefix, linux_list, linux_list_len,
2323 bsd_list, &bsd_list_len);
2327 if (ap->a_size != NULL)
2328 *ap->a_size = bsd_list_len;
2331 err = uiomove(bsd_list, bsd_list_len, uio);
2334 free(bsd_list, M_TEMP);
2335 fdisp_destroy(&fdi);
2340 struct vop_deleteextattr_args {
2341 struct vop_generic_args a_gen;
2343 int a_attrnamespace;
2345 struct ucred *a_cred;
2346 struct thread *a_td;
2350 fuse_vnop_deleteextattr(struct vop_deleteextattr_args *ap)
2352 struct vnode *vp = ap->a_vp;
2353 struct fuse_dispatcher fdi;
2354 struct mount *mp = vnode_mount(vp);
2355 struct thread *td = ap->a_td;
2356 struct ucred *cred = ap->a_cred;
2362 if (fuse_isdeadfs(vp))
2365 /* Default to looking for user attributes. */
2366 if (ap->a_attrnamespace == EXTATTR_NAMESPACE_SYSTEM)
2367 prefix = EXTATTR_NAMESPACE_SYSTEM_STRING;
2369 prefix = EXTATTR_NAMESPACE_USER_STRING;
2371 len = strlen(prefix) + sizeof(extattr_namespace_separator) +
2372 strlen(ap->a_name) + 1;
2374 fdisp_init(&fdi, len);
2375 fdisp_make_vp(&fdi, FUSE_REMOVEXATTR, vp, td, cred);
2377 attr_str = fdi.indata;
2378 snprintf(attr_str, len, "%s%c%s", prefix, extattr_namespace_separator,
2381 err = fdisp_wait_answ(&fdi);
2384 fsess_set_notimpl(mp, FUSE_REMOVEXATTR);
2387 fdisp_destroy(&fdi);
2392 struct vnop_print_args {
2397 fuse_vnop_print(struct vop_print_args *ap)
2399 struct fuse_vnode_data *fvdat = VTOFUD(ap->a_vp);
2401 printf("nodeid: %ju, parent nodeid: %ju, nlookup: %ju, flag: %#x\n",
2402 (uintmax_t)VTOILLU(ap->a_vp), (uintmax_t)fvdat->parent_nid,
2403 (uintmax_t)fvdat->nlookup,