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/types.h>
62 #include <sys/module.h>
63 #include <sys/systm.h>
64 #include <sys/errno.h>
65 #include <sys/param.h>
66 #include <sys/kernel.h>
69 #include <sys/malloc.h>
70 #include <sys/queue.h>
72 #include <sys/rwlock.h>
75 #include <sys/mount.h>
76 #include <sys/vnode.h>
77 #include <sys/namei.h>
78 #include <sys/extattr.h>
80 #include <sys/unistd.h>
81 #include <sys/filedesc.h>
83 #include <sys/fcntl.h>
84 #include <sys/dirent.h>
87 #include <sys/sysctl.h>
88 #include <sys/vmmeter.h>
91 #include <vm/vm_extern.h>
93 #include <vm/vm_map.h>
94 #include <vm/vm_page.h>
95 #include <vm/vm_param.h>
96 #include <vm/vm_object.h>
97 #include <vm/vm_pager.h>
98 #include <vm/vnode_pager.h>
99 #include <vm/vm_object.h>
102 #include "fuse_file.h"
103 #include "fuse_internal.h"
104 #include "fuse_ipc.h"
105 #include "fuse_node.h"
106 #include "fuse_param.h"
109 #include <sys/priv.h>
111 #define FUSE_DEBUG_MODULE VNOPS
112 #include "fuse_debug.h"
115 static vop_access_t fuse_vnop_access;
116 static vop_close_t fuse_vnop_close;
117 static vop_create_t fuse_vnop_create;
118 static vop_deleteextattr_t fuse_vnop_deleteextattr;
119 static vop_fsync_t fuse_vnop_fsync;
120 static vop_getattr_t fuse_vnop_getattr;
121 static vop_getextattr_t fuse_vnop_getextattr;
122 static vop_inactive_t fuse_vnop_inactive;
123 static vop_link_t fuse_vnop_link;
124 static vop_listextattr_t fuse_vnop_listextattr;
125 static vop_lookup_t fuse_vnop_lookup;
126 static vop_mkdir_t fuse_vnop_mkdir;
127 static vop_mknod_t fuse_vnop_mknod;
128 static vop_open_t fuse_vnop_open;
129 static vop_pathconf_t fuse_vnop_pathconf;
130 static vop_read_t fuse_vnop_read;
131 static vop_readdir_t fuse_vnop_readdir;
132 static vop_readlink_t fuse_vnop_readlink;
133 static vop_reclaim_t fuse_vnop_reclaim;
134 static vop_remove_t fuse_vnop_remove;
135 static vop_rename_t fuse_vnop_rename;
136 static vop_rmdir_t fuse_vnop_rmdir;
137 static vop_setattr_t fuse_vnop_setattr;
138 static vop_setextattr_t fuse_vnop_setextattr;
139 static vop_strategy_t fuse_vnop_strategy;
140 static vop_symlink_t fuse_vnop_symlink;
141 static vop_write_t fuse_vnop_write;
142 static vop_getpages_t fuse_vnop_getpages;
143 static vop_putpages_t fuse_vnop_putpages;
144 static vop_print_t fuse_vnop_print;
146 struct vop_vector fuse_vnops = {
147 .vop_default = &default_vnodeops,
148 .vop_access = fuse_vnop_access,
149 .vop_close = fuse_vnop_close,
150 .vop_create = fuse_vnop_create,
151 .vop_deleteextattr = fuse_vnop_deleteextattr,
152 .vop_fsync = fuse_vnop_fsync,
153 .vop_getattr = fuse_vnop_getattr,
154 .vop_getextattr = fuse_vnop_getextattr,
155 .vop_inactive = fuse_vnop_inactive,
156 .vop_link = fuse_vnop_link,
157 .vop_listextattr = fuse_vnop_listextattr,
158 .vop_lookup = fuse_vnop_lookup,
159 .vop_mkdir = fuse_vnop_mkdir,
160 .vop_mknod = fuse_vnop_mknod,
161 .vop_open = fuse_vnop_open,
162 .vop_pathconf = fuse_vnop_pathconf,
163 .vop_read = fuse_vnop_read,
164 .vop_readdir = fuse_vnop_readdir,
165 .vop_readlink = fuse_vnop_readlink,
166 .vop_reclaim = fuse_vnop_reclaim,
167 .vop_remove = fuse_vnop_remove,
168 .vop_rename = fuse_vnop_rename,
169 .vop_rmdir = fuse_vnop_rmdir,
170 .vop_setattr = fuse_vnop_setattr,
171 .vop_setextattr = fuse_vnop_setextattr,
172 .vop_strategy = fuse_vnop_strategy,
173 .vop_symlink = fuse_vnop_symlink,
174 .vop_write = fuse_vnop_write,
175 .vop_getpages = fuse_vnop_getpages,
176 .vop_putpages = fuse_vnop_putpages,
177 .vop_print = fuse_vnop_print,
180 static u_long fuse_lookup_cache_hits = 0;
182 SYSCTL_ULONG(_vfs_fuse, OID_AUTO, lookup_cache_hits, CTLFLAG_RD,
183 &fuse_lookup_cache_hits, 0, "number of positive cache hits in lookup");
185 static u_long fuse_lookup_cache_misses = 0;
187 SYSCTL_ULONG(_vfs_fuse, OID_AUTO, lookup_cache_misses, CTLFLAG_RD,
188 &fuse_lookup_cache_misses, 0, "number of cache misses in lookup");
190 int fuse_lookup_cache_enable = 1;
192 SYSCTL_INT(_vfs_fuse, OID_AUTO, lookup_cache_enable, CTLFLAG_RW,
193 &fuse_lookup_cache_enable, 0, "if non-zero, enable lookup cache");
196 * XXX: This feature is highly experimental and can bring to instabilities,
197 * needs revisiting before to be enabled by default.
199 static int fuse_reclaim_revoked = 0;
201 SYSCTL_INT(_vfs_fuse, OID_AUTO, reclaim_revoked, CTLFLAG_RW,
202 &fuse_reclaim_revoked, 0, "");
204 uma_zone_t fuse_pbuf_zone;
206 #define fuse_vm_page_lock(m) vm_page_lock((m));
207 #define fuse_vm_page_unlock(m) vm_page_unlock((m));
208 #define fuse_vm_page_lock_queues() ((void)0)
209 #define fuse_vm_page_unlock_queues() ((void)0)
212 struct vnop_access_args {
214 #if VOP_ACCESS_TAKES_ACCMODE_T
219 struct ucred *a_cred;
224 fuse_vnop_access(struct vop_access_args *ap)
226 struct vnode *vp = ap->a_vp;
227 int accmode = ap->a_accmode;
228 struct ucred *cred = ap->a_cred;
230 struct fuse_access_param facp;
231 struct fuse_data *data = fuse_get_mpdata(vnode_mount(vp));
235 FS_DEBUG2G("inode=%ju\n", (uintmax_t)VTOI(vp));
237 if (fuse_isdeadfs(vp)) {
238 if (vnode_isvroot(vp)) {
243 if (!(data->dataflags & FSESS_INITED)) {
244 if (vnode_isvroot(vp)) {
245 if (priv_check_cred(cred, PRIV_VFS_ADMIN) ||
246 (fuse_match_cred(data->daemoncred, cred) == 0)) {
252 if (vnode_islnk(vp)) {
255 bzero(&facp, sizeof(facp));
257 err = fuse_internal_access(vp, accmode, &facp, ap->a_td, ap->a_cred);
258 FS_DEBUG2G("err=%d accmode=0x%x\n", err, accmode);
263 struct vnop_close_args {
266 struct ucred *a_cred;
271 fuse_vnop_close(struct vop_close_args *ap)
273 struct vnode *vp = ap->a_vp;
274 struct ucred *cred = ap->a_cred;
275 int fflag = ap->a_fflag;
276 fufh_type_t fufh_type;
278 fuse_trace_printf_vnop();
280 if (fuse_isdeadfs(vp)) {
283 if (vnode_isdir(vp)) {
284 if (fuse_filehandle_valid(vp, FUFH_RDONLY)) {
285 fuse_filehandle_close(vp, FUFH_RDONLY, NULL, cred);
289 if (fflag & IO_NDELAY) {
292 fufh_type = fuse_filehandle_xlate_from_fflags(fflag);
294 if (!fuse_filehandle_valid(vp, fufh_type)) {
297 for (i = 0; i < FUFH_MAXTYPE; i++)
298 if (fuse_filehandle_valid(vp, i))
300 if (i == FUFH_MAXTYPE)
301 panic("FUSE: fufh type %d found to be invalid in close"
305 if ((VTOFUD(vp)->flag & FN_SIZECHANGE) != 0) {
306 fuse_vnode_savesize(vp, cred);
312 struct vnop_create_args {
314 struct vnode **a_vpp;
315 struct componentname *a_cnp;
320 fuse_vnop_create(struct vop_create_args *ap)
322 struct vnode *dvp = ap->a_dvp;
323 struct vnode **vpp = ap->a_vpp;
324 struct componentname *cnp = ap->a_cnp;
325 struct vattr *vap = ap->a_vap;
326 struct thread *td = cnp->cn_thread;
327 struct ucred *cred = cnp->cn_cred;
329 struct fuse_open_in *foi;
330 struct fuse_entry_out *feo;
331 struct fuse_dispatcher fdi;
332 struct fuse_dispatcher *fdip = &fdi;
336 struct mount *mp = vnode_mount(dvp);
337 uint64_t parentnid = VTOFUD(dvp)->nid;
338 mode_t mode = MAKEIMODE(vap->va_type, vap->va_mode);
340 uint32_t x_open_flags;
342 fuse_trace_printf_vnop();
344 if (fuse_isdeadfs(dvp)) {
347 bzero(&fdi, sizeof(fdi));
349 /* XXX: Will we ever want devices ? */
350 if ((vap->va_type != VREG)) {
351 printf("fuse_vnop_create: unsupported va_type %d\n",
355 debug_printf("parent nid = %ju, mode = %x\n", (uintmax_t)parentnid,
358 fdisp_init(fdip, sizeof(*foi) + cnp->cn_namelen + 1);
359 if (!fsess_isimpl(mp, FUSE_CREATE)) {
360 debug_printf("eh, daemon doesn't implement create?\n");
363 fdisp_make(fdip, FUSE_CREATE, vnode_mount(dvp), parentnid, td, cred);
367 foi->flags = O_CREAT | O_RDWR;
369 memcpy((char *)fdip->indata + sizeof(*foi), cnp->cn_nameptr,
371 ((char *)fdip->indata)[sizeof(*foi) + cnp->cn_namelen] = '\0';
373 err = fdisp_wait_answ(fdip);
377 fsess_set_notimpl(mp, FUSE_CREATE);
378 debug_printf("create: got err=%d from daemon\n", err);
384 if ((err = fuse_internal_checkentry(feo, VREG))) {
387 err = fuse_vnode_get(mp, feo, feo->nodeid, dvp, vpp, cnp, VREG);
389 struct fuse_release_in *fri;
390 uint64_t nodeid = feo->nodeid;
391 uint64_t fh_id = ((struct fuse_open_out *)(feo + 1))->fh;
393 fdisp_init(fdip, sizeof(*fri));
394 fdisp_make(fdip, FUSE_RELEASE, mp, nodeid, td, cred);
397 fri->flags = OFLAGS(mode);
398 fuse_insert_callback(fdip->tick, fuse_internal_forget_callback);
399 fuse_insert_message(fdip->tick);
402 ASSERT_VOP_ELOCKED(*vpp, "fuse_vnop_create");
404 fdip->answ = feo + 1;
406 x_fh_id = ((struct fuse_open_out *)(feo + 1))->fh;
407 x_open_flags = ((struct fuse_open_out *)(feo + 1))->open_flags;
408 fuse_filehandle_init(*vpp, FUFH_RDWR, NULL, x_fh_id);
409 fuse_vnode_open(*vpp, x_open_flags, td);
410 cache_purge_negative(dvp);
418 * Our vnop_fsync roughly corresponds to the FUSE_FSYNC method. The Linux
419 * version of FUSE also has a FUSE_FLUSH method.
421 * On Linux, fsync() synchronizes a file's complete in-core state with that
422 * on disk. The call is not supposed to return until the system has completed
423 * that action or until an error is detected.
425 * Linux also has an fdatasync() call that is similar to fsync() but is not
426 * required to update the metadata such as access time and modification time.
430 struct vnop_fsync_args {
431 struct vnodeop_desc *a_desc;
433 struct ucred * a_cred;
435 struct thread * a_td;
439 fuse_vnop_fsync(struct vop_fsync_args *ap)
441 struct vnode *vp = ap->a_vp;
442 struct thread *td = ap->a_td;
444 struct fuse_filehandle *fufh;
445 struct fuse_vnode_data *fvdat = VTOFUD(vp);
449 fuse_trace_printf_vnop();
451 if (fuse_isdeadfs(vp)) {
454 if ((err = vop_stdfsync(ap)))
457 if (!fsess_isimpl(vnode_mount(vp),
458 (vnode_vtype(vp) == VDIR ? FUSE_FSYNCDIR : FUSE_FSYNC))) {
461 for (type = 0; type < FUFH_MAXTYPE; type++) {
462 fufh = &(fvdat->fufh[type]);
463 if (FUFH_IS_VALID(fufh)) {
464 fuse_internal_fsync(vp, td, NULL, fufh);
473 struct vnop_getattr_args {
476 struct ucred *a_cred;
481 fuse_vnop_getattr(struct vop_getattr_args *ap)
483 struct vnode *vp = ap->a_vp;
484 struct vattr *vap = ap->a_vap;
485 struct ucred *cred = ap->a_cred;
486 struct thread *td = curthread;
487 struct fuse_vnode_data *fvdat = VTOFUD(vp);
491 struct fuse_dispatcher fdi;
493 FS_DEBUG2G("inode=%ju\n", (uintmax_t)VTOI(vp));
495 dataflags = fuse_get_mpdata(vnode_mount(vp))->dataflags;
497 /* Note that we are not bailing out on a dead file system just yet. */
499 if (!(dataflags & FSESS_INITED)) {
500 if (!vnode_isvroot(vp)) {
501 fdata_set_dead(fuse_get_mpdata(vnode_mount(vp)));
503 debug_printf("fuse_getattr b: returning ENOTCONN\n");
510 if ((err = fdisp_simple_putget_vp(&fdi, FUSE_GETATTR, vp, td, cred))) {
511 if ((err == ENOTCONN) && vnode_isvroot(vp)) {
512 /* see comment at similar place in fuse_statfs() */
517 fuse_internal_vnode_disappear(vp);
522 cache_attrs(vp, (struct fuse_attr_out *)fdi.answ, vap);
523 if (vap->va_type != vnode_vtype(vp)) {
524 fuse_internal_vnode_disappear(vp);
528 if ((fvdat->flag & FN_SIZECHANGE) != 0)
529 vap->va_size = fvdat->filesize;
531 if (vnode_isreg(vp) && (fvdat->flag & FN_SIZECHANGE) == 0) {
533 * This is for those cases when the file size changed without us
534 * knowing, and we want to catch up.
536 off_t new_filesize = ((struct fuse_attr_out *)
537 fdi.answ)->attr.size;
539 if (fvdat->filesize != new_filesize) {
540 fuse_vnode_setsize(vp, cred, new_filesize);
541 fvdat->flag &= ~FN_SIZECHANGE;
544 debug_printf("fuse_getattr e: returning 0\n");
551 bzero(vap, sizeof(*vap));
552 vap->va_type = vnode_vtype(vp);
558 struct vnop_inactive_args {
564 fuse_vnop_inactive(struct vop_inactive_args *ap)
566 struct vnode *vp = ap->a_vp;
567 struct thread *td = ap->a_td;
569 struct fuse_vnode_data *fvdat = VTOFUD(vp);
570 struct fuse_filehandle *fufh = NULL;
572 int type, need_flush = 1;
574 FS_DEBUG("inode=%ju\n", (uintmax_t)VTOI(vp));
576 for (type = 0; type < FUFH_MAXTYPE; type++) {
577 fufh = &(fvdat->fufh[type]);
578 if (FUFH_IS_VALID(fufh)) {
579 if (need_flush && vp->v_type == VREG) {
580 if ((VTOFUD(vp)->flag & FN_SIZECHANGE) != 0) {
581 fuse_vnode_savesize(vp, NULL);
583 if (fuse_data_cache_invalidate ||
584 (fvdat->flag & FN_REVOKED) != 0)
585 fuse_io_invalbuf(vp, td);
587 fuse_io_flushbuf(vp, MNT_WAIT, td);
590 fuse_filehandle_close(vp, type, td, NULL);
594 if ((fvdat->flag & FN_REVOKED) != 0 && fuse_reclaim_revoked) {
601 struct vnop_link_args {
602 struct vnode *a_tdvp;
604 struct componentname *a_cnp;
608 fuse_vnop_link(struct vop_link_args *ap)
610 struct vnode *vp = ap->a_vp;
611 struct vnode *tdvp = ap->a_tdvp;
612 struct componentname *cnp = ap->a_cnp;
614 struct vattr *vap = VTOVA(vp);
616 struct fuse_dispatcher fdi;
617 struct fuse_entry_out *feo;
618 struct fuse_link_in fli;
622 fuse_trace_printf_vnop();
624 if (fuse_isdeadfs(vp)) {
627 if (vnode_mount(tdvp) != vnode_mount(vp)) {
632 * This is a seatbelt check to protect naive userspace filesystems from
633 * themselves and the limitations of the FUSE IPC protocol. If a
634 * filesystem does not allow attribute caching, assume it is capable of
635 * validating that nlink does not overflow.
637 if (vap != NULL && vap->va_nlink >= FUSE_LINK_MAX)
639 fli.oldnodeid = VTOI(vp);
642 fuse_internal_newentry_makerequest(vnode_mount(tdvp), VTOI(tdvp), cnp,
643 FUSE_LINK, &fli, sizeof(fli), &fdi);
644 if ((err = fdisp_wait_answ(&fdi))) {
649 err = fuse_internal_checkentry(feo, vnode_vtype(vp));
656 struct vnop_lookup_args {
657 struct vnodeop_desc *a_desc;
659 struct vnode **a_vpp;
660 struct componentname *a_cnp;
664 fuse_vnop_lookup(struct vop_lookup_args *ap)
666 struct vnode *dvp = ap->a_dvp;
667 struct vnode **vpp = ap->a_vpp;
668 struct componentname *cnp = ap->a_cnp;
669 struct thread *td = cnp->cn_thread;
670 struct ucred *cred = cnp->cn_cred;
672 int nameiop = cnp->cn_nameiop;
673 int flags = cnp->cn_flags;
674 int wantparent = flags & (LOCKPARENT | WANTPARENT);
675 int islastcn = flags & ISLASTCN;
676 struct mount *mp = vnode_mount(dvp);
680 struct vnode *vp = NULL;
682 struct fuse_dispatcher fdi;
686 struct fuse_access_param facp;
688 FS_DEBUG2G("parent_inode=%ju - %*s\n",
689 (uintmax_t)VTOI(dvp), (int)cnp->cn_namelen, cnp->cn_nameptr);
691 if (fuse_isdeadfs(dvp)) {
695 if (!vnode_isdir(dvp)) {
698 if (islastcn && vfs_isrdonly(mp) && (nameiop != LOOKUP)) {
702 * We do access check prior to doing anything else only in the case
703 * when we are at fs root (we'd like to say, "we are at the first
704 * component", but that's not exactly the same... nevermind).
705 * See further comments at further access checks.
708 bzero(&facp, sizeof(facp));
709 if (vnode_isvroot(dvp)) { /* early permission check hack */
710 if ((err = fuse_internal_access(dvp, VEXEC, &facp, td, cred))) {
714 if (flags & ISDOTDOT) {
715 nid = VTOFUD(dvp)->parent_nid;
722 } else if (cnp->cn_namelen == 1 && *(cnp->cn_nameptr) == '.') {
727 } else if (fuse_lookup_cache_enable) {
728 err = cache_lookup(dvp, vpp, cnp, NULL, NULL);
731 case -1: /* positive match */
732 atomic_add_acq_long(&fuse_lookup_cache_hits, 1);
735 case 0: /* no match in cache */
736 atomic_add_acq_long(&fuse_lookup_cache_misses, 1);
739 case ENOENT: /* negative match */
746 fdisp_init(&fdi, cnp->cn_namelen + 1);
750 fdisp_make(&fdi, op, mp, nid, td, cred);
752 if (op == FUSE_LOOKUP) {
753 memcpy(fdi.indata, cnp->cn_nameptr, cnp->cn_namelen);
754 ((char *)fdi.indata)[cnp->cn_namelen] = '\0';
756 lookup_err = fdisp_wait_answ(&fdi);
758 if ((op == FUSE_LOOKUP) && !lookup_err) { /* lookup call succeeded */
759 nid = ((struct fuse_entry_out *)fdi.answ)->nodeid;
762 * zero nodeid is the same as "not found",
763 * but it's also cacheable (which we keep
764 * keep on doing not as of writing this)
767 } else if (nid == FUSE_ROOT_ID) {
772 (!fdi.answ_stat || lookup_err != ENOENT || op != FUSE_LOOKUP)) {
776 /* lookup_err, if non-zero, must be ENOENT at this point */
780 if ((nameiop == CREATE || nameiop == RENAME) && islastcn
781 /* && directory dvp has not been removed */ ) {
783 if (vfs_isrdonly(mp)) {
787 #if 0 /* THINK_ABOUT_THIS */
788 if ((err = fuse_internal_access(dvp, VWRITE, cred, td, &facp))) {
794 * Possibly record the position of a slot in the
795 * directory large enough for the new component name.
796 * This can be recorded in the vnode private data for
797 * dvp. Set the SAVENAME flag to hold onto the
798 * pathname for use later in VOP_CREATE or VOP_RENAME.
800 cnp->cn_flags |= SAVENAME;
805 /* Consider inserting name into cache. */
808 * No we can't use negative caching, as the fs
809 * changes are out of our control.
810 * False positives' falseness turns out just as things
811 * go by, but false negatives' falseness doesn't.
812 * (and aiding the caching mechanism with extra control
813 * mechanisms comes quite close to beating the whole purpose
817 if ((cnp->cn_flags & MAKEENTRY) != 0) {
818 FS_DEBUG("inserting NULL into cache\n");
819 cache_enter(dvp, NULL, cnp);
829 struct fuse_entry_out *feo = NULL;
830 struct fuse_attr *fattr = NULL;
832 if (op == FUSE_GETATTR) {
833 fattr = &((struct fuse_attr_out *)fdi.answ)->attr;
835 feo = (struct fuse_entry_out *)fdi.answ;
836 fattr = &(feo->attr);
840 * If deleting, and at end of pathname, return parameters
841 * which can be used to remove file. If the wantparent flag
842 * isn't set, we return only the directory, otherwise we go on
843 * and lock the inode, being careful with ".".
845 if (nameiop == DELETE && islastcn) {
847 * Check for write access on directory.
849 facp.xuid = fattr->uid;
850 facp.facc_flags |= FACCESS_STICKY;
851 err = fuse_internal_access(dvp, VWRITE, &facp, td, cred);
852 facp.facc_flags &= ~FACCESS_XQUERIES;
857 if (nid == VTOI(dvp)) {
861 err = fuse_vnode_get(dvp->v_mount, feo, nid,
862 dvp, &vp, cnp, IFTOVT(fattr->mode));
869 * Save the name for use in VOP_RMDIR and VOP_REMOVE
872 cnp->cn_flags |= SAVENAME;
877 * If rewriting (RENAME), return the inode and the
878 * information required to rewrite the present directory
879 * Must get inode of directory entry to verify it's a
880 * regular file, or empty directory.
882 if (nameiop == RENAME && wantparent && islastcn) {
884 #if 0 /* THINK_ABOUT_THIS */
885 if ((err = fuse_internal_access(dvp, VWRITE, cred, td, &facp))) {
893 if (nid == VTOI(dvp)) {
897 err = fuse_vnode_get(vnode_mount(dvp), feo, nid, dvp,
898 &vp, cnp, IFTOVT(fattr->mode));
904 * Save the name for use in VOP_RENAME later.
906 cnp->cn_flags |= SAVENAME;
910 if (flags & ISDOTDOT) {
915 * Expanded copy of vn_vget_ino() so that
916 * fuse_vnode_get() can be used.
919 ltype = VOP_ISLOCKED(dvp);
920 err = vfs_busy(mp, MBF_NOWAIT);
924 err = vfs_busy(mp, 0);
925 vn_lock(dvp, ltype | LK_RETRY);
929 if ((dvp->v_iflag & VI_DOOMED) != 0) {
936 err = fuse_vnode_get(vnode_mount(dvp), feo, nid, NULL,
937 &vp, cnp, IFTOVT(fattr->mode));
939 vn_lock(dvp, ltype | LK_RETRY);
940 if ((dvp->v_iflag & VI_DOOMED) != 0) {
948 } else if (nid == VTOI(dvp)) {
952 struct fuse_vnode_data *fvdat;
954 err = fuse_vnode_get(vnode_mount(dvp), feo, nid, dvp,
955 &vp, cnp, IFTOVT(fattr->mode));
959 fuse_vnode_setparent(vp, dvp);
962 * In the case where we are looking up a FUSE node
963 * represented by an existing cached vnode, and the
964 * true size reported by FUSE_LOOKUP doesn't match
965 * the vnode's cached size, fix the vnode cache to
966 * match the real object size.
968 * This can occur via FUSE distributed filesystems,
969 * irregular files, etc.
972 if (vnode_isreg(vp) &&
973 fattr->size != fvdat->filesize) {
975 * The FN_SIZECHANGE flag reflects a dirty
976 * append. If userspace lets us know our cache
977 * is invalid, that write was lost. (Dirty
978 * writes that do not cause append are also
979 * lost, but we don't detect them here.)
981 * XXX: Maybe disable WB caching on this mount.
983 if (fvdat->flag & FN_SIZECHANGE)
984 printf("%s: WB cache incoherent on "
986 vnode_mount(vp)->mnt_stat.f_mntonname);
988 (void)fuse_vnode_setsize(vp, cred, fattr->size);
989 fvdat->flag &= ~FN_SIZECHANGE;
994 if (op == FUSE_GETATTR) {
995 cache_attrs(*vpp, (struct fuse_attr_out *)fdi.answ,
998 cache_attrs(*vpp, (struct fuse_entry_out *)fdi.answ,
1002 /* Insert name into cache if appropriate. */
1005 * Nooo, caching is evil. With caching, we can't avoid stale
1006 * information taking over the playground (cached info is not
1007 * just positive/negative, it does have qualitative aspects,
1008 * too). And a (VOP/FUSE)_GETATTR is always thrown anyway, when
1009 * walking down along cached path components, and that's not
1010 * any cheaper than FUSE_LOOKUP. This might change with
1011 * implementing kernel side attr caching, but... In Linux,
1012 * lookup results are not cached, and the daemon is bombarded
1013 * with FUSE_LOOKUPS on and on. This shows that by design, the
1014 * daemon is expected to handle frequent lookup queries
1015 * efficiently, do its caching in userspace, and so on.
1017 * So just leave the name cache alone.
1021 * Well, now I know, Linux caches lookups, but with a
1022 * timeout... So it's the same thing as attribute caching:
1023 * we can deal with it when implement timeouts.
1026 if (cnp->cn_flags & MAKEENTRY) {
1027 cache_enter(dvp, *vpp, cnp);
1034 /* No lookup error; need to clean up. */
1036 if (err) { /* Found inode; exit with no vnode. */
1037 if (op == FUSE_LOOKUP) {
1038 fuse_internal_forget_send(vnode_mount(dvp), td, cred,
1041 fdisp_destroy(&fdi);
1044 #ifndef NO_EARLY_PERM_CHECK_HACK
1047 * We have the attributes of the next item
1048 * *now*, and it's a fact, and we do not
1049 * have to do extra work for it (ie, beg the
1050 * daemon), and it neither depends on such
1051 * accidental things like attr caching. So
1052 * the big idea: check credentials *now*,
1053 * not at the beginning of the next call to
1056 * The first item of the lookup chain (fs root)
1057 * won't be checked then here, of course, as
1058 * its never "the next". But go and see that
1059 * the root is taken care about at the very
1060 * beginning of this function.
1062 * Now, given we want to do the access check
1063 * this way, one might ask: so then why not
1064 * do the access check just after fetching
1065 * the inode and its attributes from the
1066 * daemon? Why bother with producing the
1067 * corresponding vnode at all if something
1068 * is not OK? We know what's the deal as
1069 * soon as we get those attrs... There is
1070 * one bit of info though not given us by
1071 * the daemon: whether his response is
1072 * authoritative or not... His response should
1073 * be ignored if something is mounted over
1074 * the dir in question. But that can be
1075 * known only by having the vnode...
1077 int tmpvtype = vnode_vtype(*vpp);
1079 bzero(&facp, sizeof(facp));
1080 /*the early perm check hack */
1081 facp.facc_flags |= FACCESS_VA_VALID;
1083 if ((tmpvtype != VDIR) && (tmpvtype != VLNK)) {
1086 if (!err && !vnode_mountedhere(*vpp)) {
1087 err = fuse_internal_access(*vpp, VEXEC, &facp, td, cred);
1090 if (tmpvtype == VLNK)
1091 FS_DEBUG("weird, permission error with a symlink?\n");
1099 fdisp_destroy(&fdi);
1105 struct vnop_mkdir_args {
1106 struct vnode *a_dvp;
1107 struct vnode **a_vpp;
1108 struct componentname *a_cnp;
1109 struct vattr *a_vap;
1113 fuse_vnop_mkdir(struct vop_mkdir_args *ap)
1115 struct vnode *dvp = ap->a_dvp;
1116 struct vnode **vpp = ap->a_vpp;
1117 struct componentname *cnp = ap->a_cnp;
1118 struct vattr *vap = ap->a_vap;
1120 struct fuse_mkdir_in fmdi;
1122 fuse_trace_printf_vnop();
1124 if (fuse_isdeadfs(dvp)) {
1127 fmdi.mode = MAKEIMODE(vap->va_type, vap->va_mode);
1129 return (fuse_internal_newentry(dvp, vpp, cnp, FUSE_MKDIR, &fmdi,
1130 sizeof(fmdi), VDIR));
1134 struct vnop_mknod_args {
1135 struct vnode *a_dvp;
1136 struct vnode **a_vpp;
1137 struct componentname *a_cnp;
1138 struct vattr *a_vap;
1142 fuse_vnop_mknod(struct vop_mknod_args *ap)
1150 struct vnop_open_args {
1153 struct ucred *a_cred;
1154 struct thread *a_td;
1155 int a_fdidx; / struct file *a_fp;
1159 fuse_vnop_open(struct vop_open_args *ap)
1161 struct vnode *vp = ap->a_vp;
1162 int mode = ap->a_mode;
1163 struct thread *td = ap->a_td;
1164 struct ucred *cred = ap->a_cred;
1166 fufh_type_t fufh_type;
1167 struct fuse_vnode_data *fvdat;
1169 int error, isdir = 0;
1170 int32_t fuse_open_flags;
1172 FS_DEBUG2G("inode=%ju mode=0x%x\n", (uintmax_t)VTOI(vp), mode);
1174 if (fuse_isdeadfs(vp)) {
1177 if ((mode & (FREAD | FWRITE)) == 0)
1182 if (vnode_isdir(vp)) {
1185 fuse_open_flags = 0;
1187 fufh_type = FUFH_RDONLY;
1189 fufh_type = fuse_filehandle_xlate_from_fflags(mode);
1191 * For WRONLY opens, force DIRECT_IO. This is necessary
1192 * since writing a partial block through the buffer cache
1193 * will result in a read of the block and that read won't
1194 * be allowed by the WRONLY open.
1196 if (fufh_type == FUFH_WRONLY ||
1197 (fvdat->flag & FN_DIRECTIO) != 0)
1198 fuse_open_flags = FOPEN_DIRECT_IO;
1201 if (fuse_filehandle_validrw(vp, fufh_type) != FUFH_INVALID) {
1202 fuse_vnode_open(vp, fuse_open_flags, td);
1205 error = fuse_filehandle_open(vp, fufh_type, NULL, td, cred);
1211 fuse_vnop_pathconf(struct vop_pathconf_args *ap)
1214 switch (ap->a_name) {
1215 case _PC_FILESIZEBITS:
1219 *ap->a_retval = NAME_MAX;
1222 *ap->a_retval = MIN(LONG_MAX, FUSE_LINK_MAX);
1224 case _PC_SYMLINK_MAX:
1225 *ap->a_retval = MAXPATHLEN;
1231 return (vop_stdpathconf(ap));
1236 struct vnop_read_args {
1240 struct ucred *a_cred;
1244 fuse_vnop_read(struct vop_read_args *ap)
1246 struct vnode *vp = ap->a_vp;
1247 struct uio *uio = ap->a_uio;
1248 int ioflag = ap->a_ioflag;
1249 struct ucred *cred = ap->a_cred;
1251 FS_DEBUG2G("inode=%ju offset=%jd resid=%zd\n",
1252 (uintmax_t)VTOI(vp), uio->uio_offset, uio->uio_resid);
1254 if (fuse_isdeadfs(vp)) {
1258 if (VTOFUD(vp)->flag & FN_DIRECTIO) {
1259 ioflag |= IO_DIRECT;
1262 return fuse_io_dispatch(vp, uio, ioflag, cred);
1266 struct vnop_readdir_args {
1269 struct ucred *a_cred;
1276 fuse_vnop_readdir(struct vop_readdir_args *ap)
1278 struct vnode *vp = ap->a_vp;
1279 struct uio *uio = ap->a_uio;
1280 struct ucred *cred = ap->a_cred;
1282 struct fuse_filehandle *fufh = NULL;
1283 struct fuse_iov cookediov;
1288 FS_DEBUG2G("inode=%ju\n", (uintmax_t)VTOI(vp));
1290 if (fuse_isdeadfs(vp)) {
1293 if ( /* XXXIP ((uio_iovcnt(uio) > 1)) || */
1294 (uio_resid(uio) < sizeof(struct dirent))) {
1298 if (!fuse_filehandle_valid(vp, FUFH_RDONLY)) {
1299 FS_DEBUG("calling readdir() before open()");
1300 err = fuse_filehandle_open(vp, FUFH_RDONLY, &fufh, NULL, cred);
1303 err = fuse_filehandle_get(vp, FUFH_RDONLY, &fufh);
1308 #define DIRCOOKEDSIZE FUSE_DIRENT_ALIGN(FUSE_NAME_OFFSET + MAXNAMLEN + 1)
1309 fiov_init(&cookediov, DIRCOOKEDSIZE);
1311 err = fuse_internal_readdir(vp, uio, fufh, &cookediov);
1313 fiov_teardown(&cookediov);
1315 fuse_filehandle_close(vp, FUFH_RDONLY, NULL, cred);
1321 struct vnop_readlink_args {
1324 struct ucred *a_cred;
1328 fuse_vnop_readlink(struct vop_readlink_args *ap)
1330 struct vnode *vp = ap->a_vp;
1331 struct uio *uio = ap->a_uio;
1332 struct ucred *cred = ap->a_cred;
1334 struct fuse_dispatcher fdi;
1337 FS_DEBUG2G("inode=%ju\n", (uintmax_t)VTOI(vp));
1339 if (fuse_isdeadfs(vp)) {
1342 if (!vnode_islnk(vp)) {
1345 fdisp_init(&fdi, 0);
1346 err = fdisp_simple_putget_vp(&fdi, FUSE_READLINK, vp, curthread, cred);
1350 if (((char *)fdi.answ)[0] == '/' &&
1351 fuse_get_mpdata(vnode_mount(vp))->dataflags & FSESS_PUSH_SYMLINKS_IN) {
1352 char *mpth = vnode_mount(vp)->mnt_stat.f_mntonname;
1354 err = uiomove(mpth, strlen(mpth), uio);
1357 err = uiomove(fdi.answ, fdi.iosize, uio);
1360 fdisp_destroy(&fdi);
1365 struct vnop_reclaim_args {
1367 struct thread *a_td;
1371 fuse_vnop_reclaim(struct vop_reclaim_args *ap)
1373 struct vnode *vp = ap->a_vp;
1374 struct thread *td = ap->a_td;
1376 struct fuse_vnode_data *fvdat = VTOFUD(vp);
1377 struct fuse_filehandle *fufh = NULL;
1382 panic("FUSE: no vnode data during recycling");
1384 FS_DEBUG("inode=%ju\n", (uintmax_t)VTOI(vp));
1386 for (type = 0; type < FUFH_MAXTYPE; type++) {
1387 fufh = &(fvdat->fufh[type]);
1388 if (FUFH_IS_VALID(fufh)) {
1389 printf("FUSE: vnode being reclaimed but fufh (type=%d) is valid",
1391 fuse_filehandle_close(vp, type, td, NULL);
1395 if ((!fuse_isdeadfs(vp)) && (fvdat->nlookup)) {
1396 fuse_internal_forget_send(vnode_mount(vp), td, NULL, VTOI(vp),
1399 fuse_vnode_setparent(vp, NULL);
1401 vfs_hash_remove(vp);
1402 vnode_destroy_vobject(vp);
1403 fuse_vnode_destroy(vp);
1409 struct vnop_remove_args {
1410 struct vnode *a_dvp;
1412 struct componentname *a_cnp;
1416 fuse_vnop_remove(struct vop_remove_args *ap)
1418 struct vnode *dvp = ap->a_dvp;
1419 struct vnode *vp = ap->a_vp;
1420 struct componentname *cnp = ap->a_cnp;
1424 FS_DEBUG2G("inode=%ju name=%*s\n",
1425 (uintmax_t)VTOI(vp), (int)cnp->cn_namelen, cnp->cn_nameptr);
1427 if (fuse_isdeadfs(vp)) {
1430 if (vnode_isdir(vp)) {
1435 err = fuse_internal_remove(dvp, vp, cnp, FUSE_UNLINK);
1438 fuse_internal_vnode_disappear(vp);
1443 struct vnop_rename_args {
1444 struct vnode *a_fdvp;
1445 struct vnode *a_fvp;
1446 struct componentname *a_fcnp;
1447 struct vnode *a_tdvp;
1448 struct vnode *a_tvp;
1449 struct componentname *a_tcnp;
1453 fuse_vnop_rename(struct vop_rename_args *ap)
1455 struct vnode *fdvp = ap->a_fdvp;
1456 struct vnode *fvp = ap->a_fvp;
1457 struct componentname *fcnp = ap->a_fcnp;
1458 struct vnode *tdvp = ap->a_tdvp;
1459 struct vnode *tvp = ap->a_tvp;
1460 struct componentname *tcnp = ap->a_tcnp;
1461 struct fuse_data *data;
1465 FS_DEBUG2G("from: inode=%ju name=%*s -> to: inode=%ju name=%*s\n",
1466 (uintmax_t)VTOI(fvp), (int)fcnp->cn_namelen, fcnp->cn_nameptr,
1467 (uintmax_t)(tvp == NULL ? -1 : VTOI(tvp)),
1468 (int)tcnp->cn_namelen, tcnp->cn_nameptr);
1470 if (fuse_isdeadfs(fdvp)) {
1473 if (fvp->v_mount != tdvp->v_mount ||
1474 (tvp && fvp->v_mount != tvp->v_mount)) {
1475 FS_DEBUG("cross-device rename: %s -> %s\n",
1476 fcnp->cn_nameptr, (tcnp != NULL ? tcnp->cn_nameptr : "(NULL)"));
1483 * FUSE library is expected to check if target directory is not
1484 * under the source directory in the file system tree.
1485 * Linux performs this check at VFS level.
1487 data = fuse_get_mpdata(vnode_mount(tdvp));
1488 sx_xlock(&data->rename_lock);
1489 err = fuse_internal_rename(fdvp, fcnp, tdvp, tcnp);
1492 fuse_vnode_setparent(fvp, tdvp);
1494 fuse_vnode_setparent(tvp, NULL);
1496 sx_unlock(&data->rename_lock);
1498 if (tvp != NULL && tvp != fvp) {
1501 if (vnode_isdir(fvp)) {
1502 if ((tvp != NULL) && vnode_isdir(tvp)) {
1523 struct vnop_rmdir_args {
1524 struct vnode *a_dvp;
1526 struct componentname *a_cnp;
1530 fuse_vnop_rmdir(struct vop_rmdir_args *ap)
1532 struct vnode *dvp = ap->a_dvp;
1533 struct vnode *vp = ap->a_vp;
1537 FS_DEBUG2G("inode=%ju\n", (uintmax_t)VTOI(vp));
1539 if (fuse_isdeadfs(vp)) {
1542 if (VTOFUD(vp) == VTOFUD(dvp)) {
1545 err = fuse_internal_remove(dvp, vp, ap->a_cnp, FUSE_RMDIR);
1548 fuse_internal_vnode_disappear(vp);
1553 struct vnop_setattr_args {
1555 struct vattr *a_vap;
1556 struct ucred *a_cred;
1557 struct thread *a_td;
1561 fuse_vnop_setattr(struct vop_setattr_args *ap)
1563 struct vnode *vp = ap->a_vp;
1564 struct vattr *vap = ap->a_vap;
1565 struct ucred *cred = ap->a_cred;
1566 struct thread *td = curthread;
1568 struct fuse_dispatcher fdi;
1569 struct fuse_setattr_in *fsai;
1570 struct fuse_access_param facp;
1574 int sizechanged = 0;
1575 uint64_t newsize = 0;
1577 FS_DEBUG2G("inode=%ju\n", (uintmax_t)VTOI(vp));
1579 if (fuse_isdeadfs(vp)) {
1582 fdisp_init(&fdi, sizeof(*fsai));
1583 fdisp_make_vp(&fdi, FUSE_SETATTR, vp, td, cred);
1587 bzero(&facp, sizeof(facp));
1589 facp.xuid = vap->va_uid;
1590 facp.xgid = vap->va_gid;
1592 if (vap->va_uid != (uid_t)VNOVAL) {
1593 facp.facc_flags |= FACCESS_CHOWN;
1594 fsai->uid = vap->va_uid;
1595 fsai->valid |= FATTR_UID;
1597 if (vap->va_gid != (gid_t)VNOVAL) {
1598 facp.facc_flags |= FACCESS_CHOWN;
1599 fsai->gid = vap->va_gid;
1600 fsai->valid |= FATTR_GID;
1602 if (vap->va_size != VNOVAL) {
1604 struct fuse_filehandle *fufh = NULL;
1606 /*Truncate to a new value. */
1607 fsai->size = vap->va_size;
1609 newsize = vap->va_size;
1610 fsai->valid |= FATTR_SIZE;
1612 fuse_filehandle_getrw(vp, FUFH_WRONLY, &fufh);
1614 fsai->fh = fufh->fh_id;
1615 fsai->valid |= FATTR_FH;
1618 if (vap->va_atime.tv_sec != VNOVAL) {
1619 fsai->atime = vap->va_atime.tv_sec;
1620 fsai->atimensec = vap->va_atime.tv_nsec;
1621 fsai->valid |= FATTR_ATIME;
1623 if (vap->va_mtime.tv_sec != VNOVAL) {
1624 fsai->mtime = vap->va_mtime.tv_sec;
1625 fsai->mtimensec = vap->va_mtime.tv_nsec;
1626 fsai->valid |= FATTR_MTIME;
1628 if (vap->va_mode != (mode_t)VNOVAL) {
1629 fsai->mode = vap->va_mode & ALLPERMS;
1630 fsai->valid |= FATTR_MODE;
1635 vtyp = vnode_vtype(vp);
1637 if (fsai->valid & FATTR_SIZE && vtyp == VDIR) {
1641 if (vfs_isrdonly(vnode_mount(vp)) && (fsai->valid & ~FATTR_SIZE || vtyp == VREG)) {
1645 if (fsai->valid & ~FATTR_SIZE) {
1646 /*err = fuse_internal_access(vp, VADMIN, context, &facp); */
1650 facp.facc_flags &= ~FACCESS_XQUERIES;
1652 if (err && !(fsai->valid & ~(FATTR_ATIME | FATTR_MTIME)) &&
1653 vap->va_vaflags & VA_UTIMES_NULL) {
1654 err = fuse_internal_access(vp, VWRITE, &facp, td, cred);
1658 if ((err = fdisp_wait_answ(&fdi)))
1660 vtyp = IFTOVT(((struct fuse_attr_out *)fdi.answ)->attr.mode);
1662 if (vnode_vtype(vp) != vtyp) {
1663 if (vnode_vtype(vp) == VNON && vtyp != VNON) {
1664 debug_printf("FUSE: Dang! vnode_vtype is VNON and vtype isn't.\n");
1667 * STALE vnode, ditch
1669 * The vnode has changed its type "behind our back". There's
1670 * nothing really we can do, so let us just force an internal
1671 * revocation and tell the caller to try again, if interested.
1673 fuse_internal_vnode_disappear(vp);
1678 cache_attrs(vp, (struct fuse_attr_out *)fdi.answ, NULL);
1681 fdisp_destroy(&fdi);
1682 if (!err && sizechanged) {
1683 fuse_vnode_setsize(vp, cred, newsize);
1684 VTOFUD(vp)->flag &= ~FN_SIZECHANGE;
1690 struct vnop_strategy_args {
1696 fuse_vnop_strategy(struct vop_strategy_args *ap)
1698 struct vnode *vp = ap->a_vp;
1699 struct buf *bp = ap->a_bp;
1701 fuse_trace_printf_vnop();
1703 if (!vp || fuse_isdeadfs(vp)) {
1704 bp->b_ioflags |= BIO_ERROR;
1705 bp->b_error = ENXIO;
1709 if (bp->b_iocmd == BIO_WRITE)
1710 fuse_vnode_refreshsize(vp, NOCRED);
1712 (void)fuse_io_strategy(vp, bp);
1715 * This is a dangerous function. If returns error, that might mean a
1716 * panic. We prefer pretty much anything over being forced to panic
1717 * by a malicious daemon (a demon?). So we just return 0 anyway. You
1718 * should never mind this: this function has its own error
1719 * propagation mechanism via the argument buffer, so
1720 * not-that-melodramatic residents of the call chain still will be
1721 * able to know what to do.
1728 struct vnop_symlink_args {
1729 struct vnode *a_dvp;
1730 struct vnode **a_vpp;
1731 struct componentname *a_cnp;
1732 struct vattr *a_vap;
1737 fuse_vnop_symlink(struct vop_symlink_args *ap)
1739 struct vnode *dvp = ap->a_dvp;
1740 struct vnode **vpp = ap->a_vpp;
1741 struct componentname *cnp = ap->a_cnp;
1742 const char *target = ap->a_target;
1744 struct fuse_dispatcher fdi;
1749 FS_DEBUG2G("inode=%ju name=%*s\n",
1750 (uintmax_t)VTOI(dvp), (int)cnp->cn_namelen, cnp->cn_nameptr);
1752 if (fuse_isdeadfs(dvp)) {
1756 * Unlike the other creator type calls, here we have to create a message
1757 * where the name of the new entry comes first, and the data describing
1758 * the entry comes second.
1759 * Hence we can't rely on our handy fuse_internal_newentry() routine,
1760 * but put together the message manually and just call the core part.
1763 len = strlen(target) + 1;
1764 fdisp_init(&fdi, len + cnp->cn_namelen + 1);
1765 fdisp_make_vp(&fdi, FUSE_SYMLINK, dvp, curthread, NULL);
1767 memcpy(fdi.indata, cnp->cn_nameptr, cnp->cn_namelen);
1768 ((char *)fdi.indata)[cnp->cn_namelen] = '\0';
1769 memcpy((char *)fdi.indata + cnp->cn_namelen + 1, target, len);
1771 err = fuse_internal_newentry_core(dvp, vpp, cnp, VLNK, &fdi);
1772 fdisp_destroy(&fdi);
1777 struct vnop_write_args {
1781 struct ucred *a_cred;
1785 fuse_vnop_write(struct vop_write_args *ap)
1787 struct vnode *vp = ap->a_vp;
1788 struct uio *uio = ap->a_uio;
1789 int ioflag = ap->a_ioflag;
1790 struct ucred *cred = ap->a_cred;
1792 fuse_trace_printf_vnop();
1794 if (fuse_isdeadfs(vp)) {
1797 fuse_vnode_refreshsize(vp, cred);
1799 if (VTOFUD(vp)->flag & FN_DIRECTIO) {
1800 ioflag |= IO_DIRECT;
1803 return fuse_io_dispatch(vp, uio, ioflag, cred);
1807 struct vnop_getpages_args {
1815 fuse_vnop_getpages(struct vop_getpages_args *ap)
1817 int i, error, nextoff, size, toff, count, npages;
1827 FS_DEBUG2G("heh\n");
1830 KASSERT(vp->v_object, ("objectless vp passed to getpages"));
1831 td = curthread; /* XXX */
1832 cred = curthread->td_ucred; /* XXX */
1834 npages = ap->a_count;
1836 if (!fsess_opt_mmap(vnode_mount(vp))) {
1837 FS_DEBUG("called on non-cacheable vnode??\n");
1838 return (VM_PAGER_ERROR);
1842 * If the last page is partially valid, just return it and allow
1843 * the pager to zero-out the blanks. Partially valid pages can
1844 * only occur at the file EOF.
1846 * XXXGL: is that true for FUSE, which is a local filesystem,
1847 * but still somewhat disconnected from the kernel?
1849 VM_OBJECT_WLOCK(vp->v_object);
1850 if (pages[npages - 1]->valid != 0 && --npages == 0)
1852 VM_OBJECT_WUNLOCK(vp->v_object);
1855 * We use only the kva address for the buffer, but this is extremely
1856 * convenient and fast.
1858 bp = uma_zalloc(fuse_pbuf_zone, M_WAITOK);
1860 kva = (vm_offset_t)bp->b_data;
1861 pmap_qenter(kva, pages, npages);
1862 VM_CNT_INC(v_vnodein);
1863 VM_CNT_ADD(v_vnodepgsin, npages);
1865 count = npages << PAGE_SHIFT;
1866 iov.iov_base = (caddr_t)kva;
1867 iov.iov_len = count;
1870 uio.uio_offset = IDX_TO_OFF(pages[0]->pindex);
1871 uio.uio_resid = count;
1872 uio.uio_segflg = UIO_SYSSPACE;
1873 uio.uio_rw = UIO_READ;
1876 error = fuse_io_dispatch(vp, &uio, IO_DIRECT, cred);
1877 pmap_qremove(kva, npages);
1879 uma_zfree(fuse_pbuf_zone, bp);
1881 if (error && (uio.uio_resid == count)) {
1882 FS_DEBUG("error %d\n", error);
1883 return VM_PAGER_ERROR;
1886 * Calculate the number of bytes read and validate only that number
1887 * of bytes. Note that due to pending writes, size may be 0. This
1888 * does not mean that the remaining data is invalid!
1891 size = count - uio.uio_resid;
1892 VM_OBJECT_WLOCK(vp->v_object);
1893 fuse_vm_page_lock_queues();
1894 for (i = 0, toff = 0; i < npages; i++, toff = nextoff) {
1897 nextoff = toff + PAGE_SIZE;
1900 if (nextoff <= size) {
1902 * Read operation filled an entire page
1904 m->valid = VM_PAGE_BITS_ALL;
1905 KASSERT(m->dirty == 0,
1906 ("fuse_getpages: page %p is dirty", m));
1907 } else if (size > toff) {
1909 * Read operation filled a partial page.
1912 vm_page_set_valid_range(m, 0, size - toff);
1913 KASSERT(m->dirty == 0,
1914 ("fuse_getpages: page %p is dirty", m));
1917 * Read operation was short. If no error occurred
1918 * we may have hit a zero-fill section. We simply
1919 * leave valid set to 0.
1924 fuse_vm_page_unlock_queues();
1926 VM_OBJECT_WUNLOCK(vp->v_object);
1931 return (VM_PAGER_OK);
1935 struct vnop_putpages_args {
1941 vm_ooffset_t a_offset;
1945 fuse_vnop_putpages(struct vop_putpages_args *ap)
1951 int i, error, npages, count;
1960 FS_DEBUG2G("heh\n");
1963 KASSERT(vp->v_object, ("objectless vp passed to putpages"));
1964 fsize = vp->v_object->un_pager.vnp.vnp_size;
1965 td = curthread; /* XXX */
1966 cred = curthread->td_ucred; /* XXX */
1968 count = ap->a_count;
1969 rtvals = ap->a_rtvals;
1970 npages = btoc(count);
1971 offset = IDX_TO_OFF(pages[0]->pindex);
1973 if (!fsess_opt_mmap(vnode_mount(vp))) {
1974 FS_DEBUG("called on non-cacheable vnode??\n");
1976 for (i = 0; i < npages; i++)
1977 rtvals[i] = VM_PAGER_AGAIN;
1980 * When putting pages, do not extend file past EOF.
1983 if (offset + count > fsize) {
1984 count = fsize - offset;
1989 * We use only the kva address for the buffer, but this is extremely
1990 * convenient and fast.
1992 bp = uma_zalloc(fuse_pbuf_zone, M_WAITOK);
1994 kva = (vm_offset_t)bp->b_data;
1995 pmap_qenter(kva, pages, npages);
1996 VM_CNT_INC(v_vnodeout);
1997 VM_CNT_ADD(v_vnodepgsout, count);
1999 iov.iov_base = (caddr_t)kva;
2000 iov.iov_len = count;
2003 uio.uio_offset = offset;
2004 uio.uio_resid = count;
2005 uio.uio_segflg = UIO_SYSSPACE;
2006 uio.uio_rw = UIO_WRITE;
2009 error = fuse_io_dispatch(vp, &uio, IO_DIRECT, cred);
2011 pmap_qremove(kva, npages);
2012 uma_zfree(fuse_pbuf_zone, bp);
2015 int nwritten = round_page(count - uio.uio_resid) / PAGE_SIZE;
2017 for (i = 0; i < nwritten; i++) {
2018 rtvals[i] = VM_PAGER_OK;
2019 VM_OBJECT_WLOCK(pages[i]->object);
2020 vm_page_undirty(pages[i]);
2021 VM_OBJECT_WUNLOCK(pages[i]->object);
2027 static const char extattr_namespace_separator = '.';
2030 struct vop_getextattr_args {
2031 struct vop_generic_args a_gen;
2033 int a_attrnamespace;
2037 struct ucred *a_cred;
2038 struct thread *a_td;
2042 fuse_vnop_getextattr(struct vop_getextattr_args *ap)
2044 struct vnode *vp = ap->a_vp;
2045 struct uio *uio = ap->a_uio;
2046 struct fuse_dispatcher fdi;
2047 struct fuse_getxattr_in *get_xattr_in;
2048 struct fuse_getxattr_out *get_xattr_out;
2049 struct mount *mp = vnode_mount(vp);
2050 struct thread *td = ap->a_td;
2051 struct ucred *cred = ap->a_cred;
2057 fuse_trace_printf_vnop();
2059 if (fuse_isdeadfs(vp))
2062 /* Default to looking for user attributes. */
2063 if (ap->a_attrnamespace == EXTATTR_NAMESPACE_SYSTEM)
2064 prefix = EXTATTR_NAMESPACE_SYSTEM_STRING;
2066 prefix = EXTATTR_NAMESPACE_USER_STRING;
2068 len = strlen(prefix) + sizeof(extattr_namespace_separator) +
2069 strlen(ap->a_name) + 1;
2071 fdisp_init(&fdi, len + sizeof(*get_xattr_in));
2072 fdisp_make_vp(&fdi, FUSE_GETXATTR, vp, td, cred);
2074 get_xattr_in = fdi.indata;
2076 * Check to see whether we're querying the available size or
2077 * issuing the actual request. If we pass in 0, we get back struct
2078 * fuse_getxattr_out. If we pass in a non-zero size, we get back
2079 * that much data, without the struct fuse_getxattr_out header.
2082 get_xattr_in->size = 0;
2084 get_xattr_in->size = uio->uio_resid;
2086 attr_str = (char *)fdi.indata + sizeof(*get_xattr_in);
2087 snprintf(attr_str, len, "%s%c%s", prefix, extattr_namespace_separator,
2090 err = fdisp_wait_answ(&fdi);
2093 fsess_set_notimpl(mp, FUSE_GETXATTR);
2094 debug_printf("getxattr: got err=%d from daemon\n", err);
2098 get_xattr_out = fdi.answ;
2100 if (ap->a_size != NULL)
2101 *ap->a_size = get_xattr_out->size;
2104 err = uiomove(fdi.answ, fdi.iosize, uio);
2107 fdisp_destroy(&fdi);
2112 struct vop_setextattr_args {
2113 struct vop_generic_args a_gen;
2115 int a_attrnamespace;
2118 struct ucred *a_cred;
2119 struct thread *a_td;
2123 fuse_vnop_setextattr(struct vop_setextattr_args *ap)
2125 struct vnode *vp = ap->a_vp;
2126 struct uio *uio = ap->a_uio;
2127 struct fuse_dispatcher fdi;
2128 struct fuse_setxattr_in *set_xattr_in;
2129 struct mount *mp = vnode_mount(vp);
2130 struct thread *td = ap->a_td;
2131 struct ucred *cred = ap->a_cred;
2137 fuse_trace_printf_vnop();
2139 if (fuse_isdeadfs(vp))
2142 /* Default to looking for user attributes. */
2143 if (ap->a_attrnamespace == EXTATTR_NAMESPACE_SYSTEM)
2144 prefix = EXTATTR_NAMESPACE_SYSTEM_STRING;
2146 prefix = EXTATTR_NAMESPACE_USER_STRING;
2148 len = strlen(prefix) + sizeof(extattr_namespace_separator) +
2149 strlen(ap->a_name) + 1;
2151 fdisp_init(&fdi, len + sizeof(*set_xattr_in) + uio->uio_resid);
2152 fdisp_make_vp(&fdi, FUSE_SETXATTR, vp, td, cred);
2154 set_xattr_in = fdi.indata;
2155 set_xattr_in->size = uio->uio_resid;
2157 attr_str = (char *)fdi.indata + sizeof(*set_xattr_in);
2158 snprintf(attr_str, len, "%s%c%s", prefix, extattr_namespace_separator,
2161 err = uiomove((char *)fdi.indata + sizeof(*set_xattr_in) + len,
2162 uio->uio_resid, uio);
2164 debug_printf("setxattr: got error %d from uiomove\n", err);
2168 err = fdisp_wait_answ(&fdi);
2172 fsess_set_notimpl(mp, FUSE_SETXATTR);
2173 debug_printf("setxattr: got err=%d from daemon\n", err);
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 fuse_trace_printf_vnop();
2270 if (fuse_isdeadfs(vp))
2274 * Add space for a NUL and the period separator if enabled.
2275 * Default to looking for user attributes.
2277 if (ap->a_attrnamespace == EXTATTR_NAMESPACE_SYSTEM)
2278 prefix = EXTATTR_NAMESPACE_SYSTEM_STRING;
2280 prefix = EXTATTR_NAMESPACE_USER_STRING;
2282 len = strlen(prefix) + sizeof(extattr_namespace_separator) + 1;
2284 fdisp_init(&fdi, sizeof(*list_xattr_in) + len);
2285 fdisp_make_vp(&fdi, FUSE_LISTXATTR, vp, td, cred);
2288 * Retrieve Linux / FUSE compatible list size.
2290 list_xattr_in = fdi.indata;
2291 list_xattr_in->size = 0;
2292 attr_str = (char *)fdi.indata + sizeof(*list_xattr_in);
2293 snprintf(attr_str, len, "%s%c", prefix, extattr_namespace_separator);
2295 err = fdisp_wait_answ(&fdi);
2298 fsess_set_notimpl(mp, FUSE_LISTXATTR);
2299 debug_printf("listextattr: got err=%d from daemon\n", err);
2303 list_xattr_out = fdi.answ;
2304 linux_list_len = list_xattr_out->size;
2305 if (linux_list_len == 0) {
2306 if (ap->a_size != NULL)
2307 *ap->a_size = linux_list_len;
2312 * Retrieve Linux / FUSE compatible list values.
2314 fdisp_make_vp(&fdi, FUSE_LISTXATTR, vp, td, cred);
2315 list_xattr_in = fdi.indata;
2316 list_xattr_in->size = linux_list_len + sizeof(*list_xattr_out);
2317 attr_str = (char *)fdi.indata + sizeof(*list_xattr_in);
2318 snprintf(attr_str, len, "%s%c", prefix, extattr_namespace_separator);
2320 err = fdisp_wait_answ(&fdi);
2324 linux_list = fdi.answ;
2325 linux_list_len = fdi.iosize;
2328 * Retrieve the BSD compatible list values.
2329 * The Linux / FUSE attribute list format isn't the same
2330 * as FreeBSD's format. So we need to transform it into
2331 * FreeBSD's format before giving it to the user.
2333 bsd_list = malloc(linux_list_len, M_TEMP, M_WAITOK);
2334 err = fuse_xattrlist_convert(prefix, linux_list, linux_list_len,
2335 bsd_list, &bsd_list_len);
2339 if (ap->a_size != NULL)
2340 *ap->a_size = bsd_list_len;
2343 err = uiomove(bsd_list, bsd_list_len, uio);
2346 free(bsd_list, M_TEMP);
2347 fdisp_destroy(&fdi);
2352 struct vop_deleteextattr_args {
2353 struct vop_generic_args a_gen;
2355 int a_attrnamespace;
2357 struct ucred *a_cred;
2358 struct thread *a_td;
2362 fuse_vnop_deleteextattr(struct vop_deleteextattr_args *ap)
2364 struct vnode *vp = ap->a_vp;
2365 struct fuse_dispatcher fdi;
2366 struct mount *mp = vnode_mount(vp);
2367 struct thread *td = ap->a_td;
2368 struct ucred *cred = ap->a_cred;
2374 fuse_trace_printf_vnop();
2376 if (fuse_isdeadfs(vp))
2379 /* Default to looking for user attributes. */
2380 if (ap->a_attrnamespace == EXTATTR_NAMESPACE_SYSTEM)
2381 prefix = EXTATTR_NAMESPACE_SYSTEM_STRING;
2383 prefix = EXTATTR_NAMESPACE_USER_STRING;
2385 len = strlen(prefix) + sizeof(extattr_namespace_separator) +
2386 strlen(ap->a_name) + 1;
2388 fdisp_init(&fdi, len);
2389 fdisp_make_vp(&fdi, FUSE_REMOVEXATTR, vp, td, cred);
2391 attr_str = fdi.indata;
2392 snprintf(attr_str, len, "%s%c%s", prefix, extattr_namespace_separator,
2395 err = fdisp_wait_answ(&fdi);
2398 fsess_set_notimpl(mp, FUSE_REMOVEXATTR);
2399 debug_printf("removexattr: got err=%d from daemon\n", err);
2402 fdisp_destroy(&fdi);
2407 struct vnop_print_args {
2412 fuse_vnop_print(struct vop_print_args *ap)
2414 struct fuse_vnode_data *fvdat = VTOFUD(ap->a_vp);
2416 printf("nodeid: %ju, parent nodeid: %ju, nlookup: %ju, flag: %#x\n",
2417 (uintmax_t)VTOILLU(ap->a_vp), (uintmax_t)fvdat->parent_nid,
2418 (uintmax_t)fvdat->nlookup,