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;
1211 int32_t fuse_open_flags = 0;
1213 fufh_type_t fufh_type;
1214 struct fuse_vnode_data *fvdat;
1216 if (fuse_isdeadfs(vp))
1218 if (vp->v_type == VCHR || vp->v_type == VBLK || vp->v_type == VFIFO)
1219 return (EOPNOTSUPP);
1220 if ((mode & (FREAD | FWRITE)) == 0)
1225 if (vnode_isdir(vp)) {
1226 fufh_type = FUFH_RDONLY;
1228 fufh_type = fuse_filehandle_xlate_from_fflags(mode);
1232 * For WRONLY opens, force DIRECT_IO. This is necessary since writing
1233 * a partial block through the buffer cache will result in a read of
1234 * the block and that read won't be allowed by the WRONLY open.
1236 if (fufh_type == FUFH_WRONLY || (fvdat->flag & FN_DIRECTIO) != 0)
1237 fuse_open_flags = FOPEN_DIRECT_IO;
1239 if (fuse_filehandle_validrw(vp, fufh_type) != FUFH_INVALID) {
1240 fuse_vnode_open(vp, fuse_open_flags, td);
1244 return fuse_filehandle_open(vp, fufh_type, NULL, td, cred);
1248 fuse_vnop_pathconf(struct vop_pathconf_args *ap)
1251 switch (ap->a_name) {
1252 case _PC_FILESIZEBITS:
1256 *ap->a_retval = NAME_MAX;
1259 *ap->a_retval = MIN(LONG_MAX, FUSE_LINK_MAX);
1261 case _PC_SYMLINK_MAX:
1262 *ap->a_retval = MAXPATHLEN;
1268 return (vop_stdpathconf(ap));
1273 struct vnop_read_args {
1277 struct ucred *a_cred;
1281 fuse_vnop_read(struct vop_read_args *ap)
1283 struct vnode *vp = ap->a_vp;
1284 struct uio *uio = ap->a_uio;
1285 int ioflag = ap->a_ioflag;
1286 struct ucred *cred = ap->a_cred;
1288 if (fuse_isdeadfs(vp)) {
1292 if (VTOFUD(vp)->flag & FN_DIRECTIO) {
1293 ioflag |= IO_DIRECT;
1296 return fuse_io_dispatch(vp, uio, ioflag, cred);
1300 struct vnop_readdir_args {
1303 struct ucred *a_cred;
1310 fuse_vnop_readdir(struct vop_readdir_args *ap)
1312 struct vnode *vp = ap->a_vp;
1313 struct uio *uio = ap->a_uio;
1314 struct ucred *cred = ap->a_cred;
1316 struct fuse_filehandle *fufh = NULL;
1317 struct fuse_iov cookediov;
1322 if (fuse_isdeadfs(vp)) {
1325 if ( /* XXXIP ((uio_iovcnt(uio) > 1)) || */
1326 (uio_resid(uio) < sizeof(struct dirent))) {
1330 if (!fuse_filehandle_valid(vp, FUFH_RDONLY)) {
1331 SDT_PROBE2(fuse, , vnops, trace, 1,
1332 "calling readdir() before open()");
1333 err = fuse_filehandle_open(vp, FUFH_RDONLY, &fufh, NULL, cred);
1336 err = fuse_filehandle_get(vp, FUFH_RDONLY, &fufh);
1341 #define DIRCOOKEDSIZE FUSE_DIRENT_ALIGN(FUSE_NAME_OFFSET + MAXNAMLEN + 1)
1342 fiov_init(&cookediov, DIRCOOKEDSIZE);
1344 err = fuse_internal_readdir(vp, uio, fufh, &cookediov);
1346 fiov_teardown(&cookediov);
1348 fuse_filehandle_close(vp, FUFH_RDONLY, NULL, cred);
1354 struct vnop_readlink_args {
1357 struct ucred *a_cred;
1361 fuse_vnop_readlink(struct vop_readlink_args *ap)
1363 struct vnode *vp = ap->a_vp;
1364 struct uio *uio = ap->a_uio;
1365 struct ucred *cred = ap->a_cred;
1367 struct fuse_dispatcher fdi;
1370 if (fuse_isdeadfs(vp)) {
1373 if (!vnode_islnk(vp)) {
1376 fdisp_init(&fdi, 0);
1377 err = fdisp_simple_putget_vp(&fdi, FUSE_READLINK, vp, curthread, cred);
1381 if (((char *)fdi.answ)[0] == '/' &&
1382 fuse_get_mpdata(vnode_mount(vp))->dataflags & FSESS_PUSH_SYMLINKS_IN) {
1383 char *mpth = vnode_mount(vp)->mnt_stat.f_mntonname;
1385 err = uiomove(mpth, strlen(mpth), uio);
1388 err = uiomove(fdi.answ, fdi.iosize, uio);
1391 fdisp_destroy(&fdi);
1396 struct vnop_reclaim_args {
1398 struct thread *a_td;
1402 fuse_vnop_reclaim(struct vop_reclaim_args *ap)
1404 struct vnode *vp = ap->a_vp;
1405 struct thread *td = ap->a_td;
1407 struct fuse_vnode_data *fvdat = VTOFUD(vp);
1408 struct fuse_filehandle *fufh = NULL;
1413 panic("FUSE: no vnode data during recycling");
1415 for (type = 0; type < FUFH_MAXTYPE; type++) {
1416 fufh = &(fvdat->fufh[type]);
1417 if (FUFH_IS_VALID(fufh)) {
1418 printf("FUSE: vnode being reclaimed but fufh (type=%d) is valid",
1420 fuse_filehandle_close(vp, type, td, NULL);
1424 if ((!fuse_isdeadfs(vp)) && (fvdat->nlookup)) {
1425 fuse_internal_forget_send(vnode_mount(vp), td, NULL, VTOI(vp),
1428 fuse_vnode_setparent(vp, NULL);
1430 vfs_hash_remove(vp);
1431 vnode_destroy_vobject(vp);
1432 fuse_vnode_destroy(vp);
1438 struct vnop_remove_args {
1439 struct vnode *a_dvp;
1441 struct componentname *a_cnp;
1445 fuse_vnop_remove(struct vop_remove_args *ap)
1447 struct vnode *dvp = ap->a_dvp;
1448 struct vnode *vp = ap->a_vp;
1449 struct componentname *cnp = ap->a_cnp;
1453 if (fuse_isdeadfs(vp)) {
1456 if (vnode_isdir(vp)) {
1461 err = fuse_internal_remove(dvp, vp, cnp, FUSE_UNLINK);
1464 fuse_internal_vnode_disappear(vp);
1469 struct vnop_rename_args {
1470 struct vnode *a_fdvp;
1471 struct vnode *a_fvp;
1472 struct componentname *a_fcnp;
1473 struct vnode *a_tdvp;
1474 struct vnode *a_tvp;
1475 struct componentname *a_tcnp;
1479 fuse_vnop_rename(struct vop_rename_args *ap)
1481 struct vnode *fdvp = ap->a_fdvp;
1482 struct vnode *fvp = ap->a_fvp;
1483 struct componentname *fcnp = ap->a_fcnp;
1484 struct vnode *tdvp = ap->a_tdvp;
1485 struct vnode *tvp = ap->a_tvp;
1486 struct componentname *tcnp = ap->a_tcnp;
1487 struct fuse_data *data;
1491 if (fuse_isdeadfs(fdvp)) {
1494 if (fvp->v_mount != tdvp->v_mount ||
1495 (tvp && fvp->v_mount != tvp->v_mount)) {
1496 SDT_PROBE2(fuse, , vnops, trace, 1, "cross-device rename");
1503 * FUSE library is expected to check if target directory is not
1504 * under the source directory in the file system tree.
1505 * Linux performs this check at VFS level.
1507 data = fuse_get_mpdata(vnode_mount(tdvp));
1508 sx_xlock(&data->rename_lock);
1509 err = fuse_internal_rename(fdvp, fcnp, tdvp, tcnp);
1512 fuse_vnode_setparent(fvp, tdvp);
1514 fuse_vnode_setparent(tvp, NULL);
1516 sx_unlock(&data->rename_lock);
1518 if (tvp != NULL && tvp != fvp) {
1521 if (vnode_isdir(fvp)) {
1522 if ((tvp != NULL) && vnode_isdir(tvp)) {
1543 struct vnop_rmdir_args {
1544 struct vnode *a_dvp;
1546 struct componentname *a_cnp;
1550 fuse_vnop_rmdir(struct vop_rmdir_args *ap)
1552 struct vnode *dvp = ap->a_dvp;
1553 struct vnode *vp = ap->a_vp;
1557 if (fuse_isdeadfs(vp)) {
1560 if (VTOFUD(vp) == VTOFUD(dvp)) {
1563 err = fuse_internal_remove(dvp, vp, ap->a_cnp, FUSE_RMDIR);
1566 fuse_internal_vnode_disappear(vp);
1571 struct vnop_setattr_args {
1573 struct vattr *a_vap;
1574 struct ucred *a_cred;
1575 struct thread *a_td;
1579 fuse_vnop_setattr(struct vop_setattr_args *ap)
1581 struct vnode *vp = ap->a_vp;
1582 struct vattr *vap = ap->a_vap;
1583 struct ucred *cred = ap->a_cred;
1584 struct thread *td = curthread;
1585 struct fuse_dispatcher fdi;
1586 struct fuse_setattr_in *fsai;
1587 struct fuse_access_param facp;
1591 int sizechanged = 0;
1592 uint64_t newsize = 0;
1594 if (fuse_isdeadfs(vp)) {
1597 fdisp_init(&fdi, sizeof(*fsai));
1598 fdisp_make_vp(&fdi, FUSE_SETATTR, vp, td, cred);
1602 bzero(&facp, sizeof(facp));
1604 facp.xuid = vap->va_uid;
1605 facp.xgid = vap->va_gid;
1607 if (vap->va_uid != (uid_t)VNOVAL) {
1608 facp.facc_flags |= FACCESS_CHOWN;
1609 fsai->uid = vap->va_uid;
1610 fsai->valid |= FATTR_UID;
1612 if (vap->va_gid != (gid_t)VNOVAL) {
1613 facp.facc_flags |= FACCESS_CHOWN;
1614 fsai->gid = vap->va_gid;
1615 fsai->valid |= FATTR_GID;
1617 if (vap->va_size != VNOVAL) {
1619 struct fuse_filehandle *fufh = NULL;
1621 /*Truncate to a new value. */
1622 fsai->size = vap->va_size;
1624 newsize = vap->va_size;
1625 fsai->valid |= FATTR_SIZE;
1627 fuse_filehandle_getrw(vp, FUFH_WRONLY, &fufh);
1629 fsai->fh = fufh->fh_id;
1630 fsai->valid |= FATTR_FH;
1633 if (vap->va_atime.tv_sec != VNOVAL) {
1634 fsai->atime = vap->va_atime.tv_sec;
1635 fsai->atimensec = vap->va_atime.tv_nsec;
1636 fsai->valid |= FATTR_ATIME;
1638 if (vap->va_mtime.tv_sec != VNOVAL) {
1639 fsai->mtime = vap->va_mtime.tv_sec;
1640 fsai->mtimensec = vap->va_mtime.tv_nsec;
1641 fsai->valid |= FATTR_MTIME;
1643 if (vap->va_mode != (mode_t)VNOVAL) {
1644 fsai->mode = vap->va_mode & ALLPERMS;
1645 fsai->valid |= FATTR_MODE;
1650 vtyp = vnode_vtype(vp);
1652 if (fsai->valid & FATTR_SIZE && vtyp == VDIR) {
1656 if (vfs_isrdonly(vnode_mount(vp)) && (fsai->valid & ~FATTR_SIZE || vtyp == VREG)) {
1661 if ((err = fdisp_wait_answ(&fdi)))
1663 vtyp = IFTOVT(((struct fuse_attr_out *)fdi.answ)->attr.mode);
1665 if (vnode_vtype(vp) != vtyp) {
1666 if (vnode_vtype(vp) == VNON && vtyp != VNON) {
1667 SDT_PROBE2(fuse, , vnops, trace, 1, "FUSE: Dang! "
1668 "vnode_vtype is VNON and vtype isn't.");
1671 * STALE vnode, ditch
1673 * The vnode has changed its type "behind our back".
1674 * There's nothing really we can do, so let us just
1675 * force an internal revocation and tell the caller to
1676 * try again, if interested.
1678 fuse_internal_vnode_disappear(vp);
1683 struct fuse_attr_out *fao = (struct fuse_attr_out*)fdi.answ;
1684 fuse_internal_cache_attrs(vp, &fao->attr, fao->attr_valid,
1685 fao->attr_valid_nsec, NULL);
1689 fdisp_destroy(&fdi);
1690 if (!err && sizechanged) {
1691 fuse_vnode_setsize(vp, cred, newsize);
1692 VTOFUD(vp)->flag &= ~FN_SIZECHANGE;
1698 struct vnop_strategy_args {
1704 fuse_vnop_strategy(struct vop_strategy_args *ap)
1706 struct vnode *vp = ap->a_vp;
1707 struct buf *bp = ap->a_bp;
1709 if (!vp || fuse_isdeadfs(vp)) {
1710 bp->b_ioflags |= BIO_ERROR;
1711 bp->b_error = ENXIO;
1715 if (bp->b_iocmd == BIO_WRITE) {
1718 err = fuse_vnode_refreshsize(vp, NOCRED);
1720 bp->b_ioflags |= BIO_ERROR;
1728 * VOP_STRATEGY always returns zero and signals error via bp->b_ioflags.
1729 * fuse_io_strategy sets bp's error fields
1731 (void)fuse_io_strategy(vp, bp);
1738 struct vnop_symlink_args {
1739 struct vnode *a_dvp;
1740 struct vnode **a_vpp;
1741 struct componentname *a_cnp;
1742 struct vattr *a_vap;
1747 fuse_vnop_symlink(struct vop_symlink_args *ap)
1749 struct vnode *dvp = ap->a_dvp;
1750 struct vnode **vpp = ap->a_vpp;
1751 struct componentname *cnp = ap->a_cnp;
1752 const char *target = ap->a_target;
1754 struct fuse_dispatcher fdi;
1759 if (fuse_isdeadfs(dvp)) {
1763 * Unlike the other creator type calls, here we have to create a message
1764 * where the name of the new entry comes first, and the data describing
1765 * the entry comes second.
1766 * Hence we can't rely on our handy fuse_internal_newentry() routine,
1767 * but put together the message manually and just call the core part.
1770 len = strlen(target) + 1;
1771 fdisp_init(&fdi, len + cnp->cn_namelen + 1);
1772 fdisp_make_vp(&fdi, FUSE_SYMLINK, dvp, curthread, NULL);
1774 memcpy(fdi.indata, cnp->cn_nameptr, cnp->cn_namelen);
1775 ((char *)fdi.indata)[cnp->cn_namelen] = '\0';
1776 memcpy((char *)fdi.indata + cnp->cn_namelen + 1, target, len);
1778 err = fuse_internal_newentry_core(dvp, vpp, cnp, VLNK, &fdi);
1779 fdisp_destroy(&fdi);
1784 struct vnop_write_args {
1788 struct ucred *a_cred;
1792 fuse_vnop_write(struct vop_write_args *ap)
1794 struct vnode *vp = ap->a_vp;
1795 struct uio *uio = ap->a_uio;
1796 int ioflag = ap->a_ioflag;
1797 struct ucred *cred = ap->a_cred;
1800 if (fuse_isdeadfs(vp)) {
1803 err = fuse_vnode_refreshsize(vp, cred);
1807 if (VTOFUD(vp)->flag & FN_DIRECTIO) {
1808 ioflag |= IO_DIRECT;
1811 return fuse_io_dispatch(vp, uio, ioflag, cred);
1814 SDT_PROBE_DEFINE1(fuse, , vnops, vnop_getpages_error, "int");
1816 struct vnop_getpages_args {
1824 fuse_vnop_getpages(struct vop_getpages_args *ap)
1826 int i, error, nextoff, size, toff, count, npages;
1837 KASSERT(vp->v_object, ("objectless vp passed to getpages"));
1838 td = curthread; /* XXX */
1839 cred = curthread->td_ucred; /* XXX */
1841 npages = ap->a_count;
1843 if (!fsess_opt_mmap(vnode_mount(vp))) {
1844 SDT_PROBE2(fuse, , vnops, trace, 1,
1845 "called on non-cacheable vnode??\n");
1846 return (VM_PAGER_ERROR);
1850 * If the last page is partially valid, just return it and allow
1851 * the pager to zero-out the blanks. Partially valid pages can
1852 * only occur at the file EOF.
1854 * XXXGL: is that true for FUSE, which is a local filesystem,
1855 * but still somewhat disconnected from the kernel?
1857 VM_OBJECT_WLOCK(vp->v_object);
1858 if (pages[npages - 1]->valid != 0 && --npages == 0)
1860 VM_OBJECT_WUNLOCK(vp->v_object);
1863 * We use only the kva address for the buffer, but this is extremely
1864 * convenient and fast.
1866 bp = uma_zalloc(fuse_pbuf_zone, M_WAITOK);
1868 kva = (vm_offset_t)bp->b_data;
1869 pmap_qenter(kva, pages, npages);
1870 VM_CNT_INC(v_vnodein);
1871 VM_CNT_ADD(v_vnodepgsin, npages);
1873 count = npages << PAGE_SHIFT;
1874 iov.iov_base = (caddr_t)kva;
1875 iov.iov_len = count;
1878 uio.uio_offset = IDX_TO_OFF(pages[0]->pindex);
1879 uio.uio_resid = count;
1880 uio.uio_segflg = UIO_SYSSPACE;
1881 uio.uio_rw = UIO_READ;
1884 error = fuse_io_dispatch(vp, &uio, IO_DIRECT, cred);
1885 pmap_qremove(kva, npages);
1887 uma_zfree(fuse_pbuf_zone, bp);
1889 if (error && (uio.uio_resid == count)) {
1890 SDT_PROBE1(fuse, , vnops, vnop_getpages_error, error);
1891 return VM_PAGER_ERROR;
1894 * Calculate the number of bytes read and validate only that number
1895 * of bytes. Note that due to pending writes, size may be 0. This
1896 * does not mean that the remaining data is invalid!
1899 size = count - uio.uio_resid;
1900 VM_OBJECT_WLOCK(vp->v_object);
1901 fuse_vm_page_lock_queues();
1902 for (i = 0, toff = 0; i < npages; i++, toff = nextoff) {
1905 nextoff = toff + PAGE_SIZE;
1908 if (nextoff <= size) {
1910 * Read operation filled an entire page
1912 m->valid = VM_PAGE_BITS_ALL;
1913 KASSERT(m->dirty == 0,
1914 ("fuse_getpages: page %p is dirty", m));
1915 } else if (size > toff) {
1917 * Read operation filled a partial page.
1920 vm_page_set_valid_range(m, 0, size - toff);
1921 KASSERT(m->dirty == 0,
1922 ("fuse_getpages: page %p is dirty", m));
1925 * Read operation was short. If no error occurred
1926 * we may have hit a zero-fill section. We simply
1927 * leave valid set to 0.
1932 fuse_vm_page_unlock_queues();
1934 VM_OBJECT_WUNLOCK(vp->v_object);
1939 return (VM_PAGER_OK);
1943 struct vnop_putpages_args {
1949 vm_ooffset_t a_offset;
1953 fuse_vnop_putpages(struct vop_putpages_args *ap)
1959 int i, error, npages, count;
1969 KASSERT(vp->v_object, ("objectless vp passed to putpages"));
1970 fsize = vp->v_object->un_pager.vnp.vnp_size;
1971 td = curthread; /* XXX */
1972 cred = curthread->td_ucred; /* XXX */
1974 count = ap->a_count;
1975 rtvals = ap->a_rtvals;
1976 npages = btoc(count);
1977 offset = IDX_TO_OFF(pages[0]->pindex);
1979 if (!fsess_opt_mmap(vnode_mount(vp))) {
1980 SDT_PROBE2(fuse, , vnops, trace, 1,
1981 "called on non-cacheable vnode??\n");
1983 for (i = 0; i < npages; i++)
1984 rtvals[i] = VM_PAGER_AGAIN;
1987 * When putting pages, do not extend file past EOF.
1990 if (offset + count > fsize) {
1991 count = fsize - offset;
1996 * We use only the kva address for the buffer, but this is extremely
1997 * convenient and fast.
1999 bp = uma_zalloc(fuse_pbuf_zone, M_WAITOK);
2001 kva = (vm_offset_t)bp->b_data;
2002 pmap_qenter(kva, pages, npages);
2003 VM_CNT_INC(v_vnodeout);
2004 VM_CNT_ADD(v_vnodepgsout, count);
2006 iov.iov_base = (caddr_t)kva;
2007 iov.iov_len = count;
2010 uio.uio_offset = offset;
2011 uio.uio_resid = count;
2012 uio.uio_segflg = UIO_SYSSPACE;
2013 uio.uio_rw = UIO_WRITE;
2016 error = fuse_io_dispatch(vp, &uio, IO_DIRECT, cred);
2018 pmap_qremove(kva, npages);
2019 uma_zfree(fuse_pbuf_zone, bp);
2022 int nwritten = round_page(count - uio.uio_resid) / PAGE_SIZE;
2024 for (i = 0; i < nwritten; i++) {
2025 rtvals[i] = VM_PAGER_OK;
2026 VM_OBJECT_WLOCK(pages[i]->object);
2027 vm_page_undirty(pages[i]);
2028 VM_OBJECT_WUNLOCK(pages[i]->object);
2034 static const char extattr_namespace_separator = '.';
2037 struct vop_getextattr_args {
2038 struct vop_generic_args a_gen;
2040 int a_attrnamespace;
2044 struct ucred *a_cred;
2045 struct thread *a_td;
2049 fuse_vnop_getextattr(struct vop_getextattr_args *ap)
2051 struct vnode *vp = ap->a_vp;
2052 struct uio *uio = ap->a_uio;
2053 struct fuse_dispatcher fdi;
2054 struct fuse_getxattr_in *get_xattr_in;
2055 struct fuse_getxattr_out *get_xattr_out;
2056 struct mount *mp = vnode_mount(vp);
2057 struct thread *td = ap->a_td;
2058 struct ucred *cred = ap->a_cred;
2064 if (fuse_isdeadfs(vp))
2067 /* Default to looking for user attributes. */
2068 if (ap->a_attrnamespace == EXTATTR_NAMESPACE_SYSTEM)
2069 prefix = EXTATTR_NAMESPACE_SYSTEM_STRING;
2071 prefix = EXTATTR_NAMESPACE_USER_STRING;
2073 len = strlen(prefix) + sizeof(extattr_namespace_separator) +
2074 strlen(ap->a_name) + 1;
2076 fdisp_init(&fdi, len + sizeof(*get_xattr_in));
2077 fdisp_make_vp(&fdi, FUSE_GETXATTR, vp, td, cred);
2079 get_xattr_in = fdi.indata;
2081 * Check to see whether we're querying the available size or
2082 * issuing the actual request. If we pass in 0, we get back struct
2083 * fuse_getxattr_out. If we pass in a non-zero size, we get back
2084 * that much data, without the struct fuse_getxattr_out header.
2087 get_xattr_in->size = 0;
2089 get_xattr_in->size = uio->uio_resid;
2091 attr_str = (char *)fdi.indata + sizeof(*get_xattr_in);
2092 snprintf(attr_str, len, "%s%c%s", prefix, extattr_namespace_separator,
2095 err = fdisp_wait_answ(&fdi);
2098 fsess_set_notimpl(mp, FUSE_GETXATTR);
2102 get_xattr_out = fdi.answ;
2104 if (ap->a_size != NULL)
2105 *ap->a_size = get_xattr_out->size;
2108 err = uiomove(fdi.answ, fdi.iosize, uio);
2111 fdisp_destroy(&fdi);
2116 struct vop_setextattr_args {
2117 struct vop_generic_args a_gen;
2119 int a_attrnamespace;
2122 struct ucred *a_cred;
2123 struct thread *a_td;
2127 fuse_vnop_setextattr(struct vop_setextattr_args *ap)
2129 struct vnode *vp = ap->a_vp;
2130 struct uio *uio = ap->a_uio;
2131 struct fuse_dispatcher fdi;
2132 struct fuse_setxattr_in *set_xattr_in;
2133 struct mount *mp = vnode_mount(vp);
2134 struct thread *td = ap->a_td;
2135 struct ucred *cred = ap->a_cred;
2141 if (fuse_isdeadfs(vp))
2144 /* Default to looking for user attributes. */
2145 if (ap->a_attrnamespace == EXTATTR_NAMESPACE_SYSTEM)
2146 prefix = EXTATTR_NAMESPACE_SYSTEM_STRING;
2148 prefix = EXTATTR_NAMESPACE_USER_STRING;
2150 len = strlen(prefix) + sizeof(extattr_namespace_separator) +
2151 strlen(ap->a_name) + 1;
2153 fdisp_init(&fdi, len + sizeof(*set_xattr_in) + uio->uio_resid);
2154 fdisp_make_vp(&fdi, FUSE_SETXATTR, vp, td, cred);
2156 set_xattr_in = fdi.indata;
2157 set_xattr_in->size = uio->uio_resid;
2159 attr_str = (char *)fdi.indata + sizeof(*set_xattr_in);
2160 snprintf(attr_str, len, "%s%c%s", prefix, extattr_namespace_separator,
2163 err = uiomove((char *)fdi.indata + sizeof(*set_xattr_in) + len,
2164 uio->uio_resid, uio);
2169 err = fdisp_wait_answ(&fdi);
2173 fsess_set_notimpl(mp, FUSE_SETXATTR);
2178 fdisp_destroy(&fdi);
2183 * The Linux / FUSE extended attribute list is simply a collection of
2184 * NUL-terminated strings. The FreeBSD extended attribute list is a single
2185 * byte length followed by a non-NUL terminated string. So, this allows
2186 * conversion of the Linux / FUSE format to the FreeBSD format in place.
2187 * Linux attribute names are reported with the namespace as a prefix (e.g.
2188 * "user.attribute_name"), but in FreeBSD they are reported without the
2189 * namespace prefix (e.g. "attribute_name"). So, we're going from:
2191 * user.attr_name1\0user.attr_name2\0
2195 * <num>attr_name1<num>attr_name2
2197 * Where "<num>" is a single byte number of characters in the attribute name.
2200 * prefix - exattr namespace prefix string
2201 * list, list_len - input list with namespace prefixes
2202 * bsd_list, bsd_list_len - output list compatible with bsd vfs
2205 fuse_xattrlist_convert(char *prefix, const char *list, int list_len,
2206 char *bsd_list, int *bsd_list_len)
2208 int len, pos, dist_to_next, prefix_len;
2212 prefix_len = strlen(prefix);
2214 while (pos < list_len && list[pos] != '\0') {
2215 dist_to_next = strlen(&list[pos]) + 1;
2216 if (bcmp(&list[pos], prefix, prefix_len) == 0 &&
2217 list[pos + prefix_len] == extattr_namespace_separator) {
2218 len = dist_to_next -
2219 (prefix_len + sizeof(extattr_namespace_separator)) - 1;
2220 if (len >= EXTATTR_MAXNAMELEN)
2221 return (ENAMETOOLONG);
2223 bsd_list[*bsd_list_len] = len;
2224 memcpy(&bsd_list[*bsd_list_len + 1],
2225 &list[pos + prefix_len +
2226 sizeof(extattr_namespace_separator)], len);
2228 *bsd_list_len += len + 1;
2231 pos += dist_to_next;
2238 struct vop_listextattr_args {
2239 struct vop_generic_args a_gen;
2241 int a_attrnamespace;
2244 struct ucred *a_cred;
2245 struct thread *a_td;
2249 fuse_vnop_listextattr(struct vop_listextattr_args *ap)
2251 struct vnode *vp = ap->a_vp;
2252 struct uio *uio = ap->a_uio;
2253 struct fuse_dispatcher fdi;
2254 struct fuse_listxattr_in *list_xattr_in;
2255 struct fuse_listxattr_out *list_xattr_out;
2256 struct mount *mp = vnode_mount(vp);
2257 struct thread *td = ap->a_td;
2258 struct ucred *cred = ap->a_cred;
2262 char *bsd_list = NULL;
2268 if (fuse_isdeadfs(vp))
2272 * Add space for a NUL and the period separator if enabled.
2273 * Default to looking for user attributes.
2275 if (ap->a_attrnamespace == EXTATTR_NAMESPACE_SYSTEM)
2276 prefix = EXTATTR_NAMESPACE_SYSTEM_STRING;
2278 prefix = EXTATTR_NAMESPACE_USER_STRING;
2280 len = strlen(prefix) + sizeof(extattr_namespace_separator) + 1;
2282 fdisp_init(&fdi, sizeof(*list_xattr_in) + len);
2283 fdisp_make_vp(&fdi, FUSE_LISTXATTR, vp, td, cred);
2286 * Retrieve Linux / FUSE compatible list size.
2288 list_xattr_in = fdi.indata;
2289 list_xattr_in->size = 0;
2290 attr_str = (char *)fdi.indata + sizeof(*list_xattr_in);
2291 snprintf(attr_str, len, "%s%c", prefix, extattr_namespace_separator);
2293 err = fdisp_wait_answ(&fdi);
2296 fsess_set_notimpl(mp, FUSE_LISTXATTR);
2300 list_xattr_out = fdi.answ;
2301 linux_list_len = list_xattr_out->size;
2302 if (linux_list_len == 0) {
2303 if (ap->a_size != NULL)
2304 *ap->a_size = linux_list_len;
2309 * Retrieve Linux / FUSE compatible list values.
2311 fdisp_make_vp(&fdi, FUSE_LISTXATTR, vp, td, cred);
2312 list_xattr_in = fdi.indata;
2313 list_xattr_in->size = linux_list_len + sizeof(*list_xattr_out);
2314 attr_str = (char *)fdi.indata + sizeof(*list_xattr_in);
2315 snprintf(attr_str, len, "%s%c", prefix, extattr_namespace_separator);
2317 err = fdisp_wait_answ(&fdi);
2321 linux_list = fdi.answ;
2322 linux_list_len = fdi.iosize;
2325 * Retrieve the BSD compatible list values.
2326 * The Linux / FUSE attribute list format isn't the same
2327 * as FreeBSD's format. So we need to transform it into
2328 * FreeBSD's format before giving it to the user.
2330 bsd_list = malloc(linux_list_len, M_TEMP, M_WAITOK);
2331 err = fuse_xattrlist_convert(prefix, linux_list, linux_list_len,
2332 bsd_list, &bsd_list_len);
2336 if (ap->a_size != NULL)
2337 *ap->a_size = bsd_list_len;
2340 err = uiomove(bsd_list, bsd_list_len, uio);
2343 free(bsd_list, M_TEMP);
2344 fdisp_destroy(&fdi);
2349 struct vop_deleteextattr_args {
2350 struct vop_generic_args a_gen;
2352 int a_attrnamespace;
2354 struct ucred *a_cred;
2355 struct thread *a_td;
2359 fuse_vnop_deleteextattr(struct vop_deleteextattr_args *ap)
2361 struct vnode *vp = ap->a_vp;
2362 struct fuse_dispatcher fdi;
2363 struct mount *mp = vnode_mount(vp);
2364 struct thread *td = ap->a_td;
2365 struct ucred *cred = ap->a_cred;
2371 if (fuse_isdeadfs(vp))
2374 /* Default to looking for user attributes. */
2375 if (ap->a_attrnamespace == EXTATTR_NAMESPACE_SYSTEM)
2376 prefix = EXTATTR_NAMESPACE_SYSTEM_STRING;
2378 prefix = EXTATTR_NAMESPACE_USER_STRING;
2380 len = strlen(prefix) + sizeof(extattr_namespace_separator) +
2381 strlen(ap->a_name) + 1;
2383 fdisp_init(&fdi, len);
2384 fdisp_make_vp(&fdi, FUSE_REMOVEXATTR, vp, td, cred);
2386 attr_str = fdi.indata;
2387 snprintf(attr_str, len, "%s%c%s", prefix, extattr_namespace_separator,
2390 err = fdisp_wait_answ(&fdi);
2393 fsess_set_notimpl(mp, FUSE_REMOVEXATTR);
2396 fdisp_destroy(&fdi);
2401 struct vnop_print_args {
2406 fuse_vnop_print(struct vop_print_args *ap)
2408 struct fuse_vnode_data *fvdat = VTOFUD(ap->a_vp);
2410 printf("nodeid: %ju, parent nodeid: %ju, nlookup: %ju, flag: %#x\n",
2411 (uintmax_t)VTOILLU(ap->a_vp), (uintmax_t)fvdat->parent_nid,
2412 (uintmax_t)fvdat->nlookup,