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_expire = 0;
200 SYSCTL_INT(_vfs_fusefs, OID_AUTO, lookup_cache_expire, CTLFLAG_RW,
201 &fuse_lookup_cache_expire, 0,
202 "if non-zero, expire fuse lookup cache entries at the proper time");
205 * XXX: This feature is highly experimental and can bring to instabilities,
206 * needs revisiting before to be enabled by default.
208 static int fuse_reclaim_revoked = 0;
210 SYSCTL_INT(_vfs_fusefs, OID_AUTO, reclaim_revoked, CTLFLAG_RW,
211 &fuse_reclaim_revoked, 0, "");
213 uma_zone_t fuse_pbuf_zone;
215 #define fuse_vm_page_lock(m) vm_page_lock((m));
216 #define fuse_vm_page_unlock(m) vm_page_unlock((m));
217 #define fuse_vm_page_lock_queues() ((void)0)
218 #define fuse_vm_page_unlock_queues() ((void)0)
220 /* Get a filehandle for a directory */
222 fuse_filehandle_get_dir(struct vnode *vp, struct fuse_filehandle **fufhp,
223 struct ucred *cred, pid_t pid)
225 if (fuse_filehandle_get(vp, FREAD, fufhp, cred, pid) == 0)
227 return fuse_filehandle_get(vp, FEXEC, fufhp, cred, pid);
230 /* Send FUSE_FLUSH for this vnode */
232 fuse_flush(struct vnode *vp, struct ucred *cred, pid_t pid, int fflag)
234 struct fuse_flush_in *ffi;
235 struct fuse_filehandle *fufh;
236 struct fuse_dispatcher fdi;
237 struct thread *td = curthread;
238 struct mount *mp = vnode_mount(vp);
241 if (!fsess_isimpl(vnode_mount(vp), FUSE_FLUSH))
244 err = fuse_filehandle_get(vp, fflag, &fufh, cred, pid);
248 fdisp_init(&fdi, sizeof(*ffi));
249 fdisp_make_vp(&fdi, FUSE_FLUSH, vp, td, cred);
251 ffi->fh = fufh->fh_id;
253 err = fdisp_wait_answ(&fdi);
255 fsess_set_notimpl(mp, FUSE_FLUSH);
263 struct vnop_access_args {
265 #if VOP_ACCESS_TAKES_ACCMODE_T
270 struct ucred *a_cred;
275 fuse_vnop_access(struct vop_access_args *ap)
277 struct vnode *vp = ap->a_vp;
278 int accmode = ap->a_accmode;
279 struct ucred *cred = ap->a_cred;
281 struct fuse_access_param facp;
282 struct fuse_data *data = fuse_get_mpdata(vnode_mount(vp));
286 if (fuse_isdeadfs(vp)) {
287 if (vnode_isvroot(vp)) {
292 if (!(data->dataflags & FSESS_INITED)) {
293 if (vnode_isvroot(vp)) {
294 if (priv_check_cred(cred, PRIV_VFS_ADMIN) ||
295 (fuse_match_cred(data->daemoncred, cred) == 0)) {
301 if (vnode_islnk(vp)) {
304 bzero(&facp, sizeof(facp));
306 err = fuse_internal_access(vp, accmode, &facp, ap->a_td, ap->a_cred);
311 struct vop_close_args {
314 struct ucred *a_cred;
319 fuse_vnop_close(struct vop_close_args *ap)
321 struct vnode *vp = ap->a_vp;
322 struct ucred *cred = ap->a_cred;
323 int fflag = ap->a_fflag;
324 struct thread *td = ap->a_td;
325 pid_t pid = td->td_proc->p_pid;
328 if (fuse_isdeadfs(vp))
332 if (fflag & IO_NDELAY)
335 err = fuse_flush(vp, cred, pid, fflag);
336 /* TODO: close the file handle, if we're sure it's no longer used */
337 if ((VTOFUD(vp)->flag & FN_SIZECHANGE) != 0) {
338 fuse_vnode_savesize(vp, cred, td->td_proc->p_pid);
344 fdisp_make_mknod_for_fallback(
345 struct fuse_dispatcher *fdip,
346 struct componentname *cnp,
352 enum fuse_opcode *op)
354 struct fuse_mknod_in *fmni;
356 fdisp_init(fdip, sizeof(*fmni) + cnp->cn_namelen + 1);
358 fdisp_make(fdip, *op, vnode_mount(dvp), parentnid, td, cred);
362 memcpy((char *)fdip->indata + sizeof(*fmni), cnp->cn_nameptr,
364 ((char *)fdip->indata)[sizeof(*fmni) + cnp->cn_namelen] = '\0';
367 struct vnop_create_args {
369 struct vnode **a_vpp;
370 struct componentname *a_cnp;
375 fuse_vnop_create(struct vop_create_args *ap)
377 struct vnode *dvp = ap->a_dvp;
378 struct vnode **vpp = ap->a_vpp;
379 struct componentname *cnp = ap->a_cnp;
380 struct vattr *vap = ap->a_vap;
381 struct thread *td = cnp->cn_thread;
382 struct ucred *cred = cnp->cn_cred;
384 struct fuse_open_in *foi;
385 struct fuse_entry_out *feo;
386 struct fuse_open_out *foo;
387 struct fuse_dispatcher fdi, fdi2;
388 struct fuse_dispatcher *fdip = &fdi;
389 struct fuse_dispatcher *fdip2 = NULL;
393 struct mount *mp = vnode_mount(dvp);
394 uint64_t parentnid = VTOFUD(dvp)->nid;
395 mode_t mode = MAKEIMODE(vap->va_type, vap->va_mode);
400 * VOP_CREATE doesn't tell us the open(2) flags, so we guess. Only a
401 * writable mode makes sense, and we might as well include readability
406 if (fuse_isdeadfs(dvp)) {
409 bzero(&fdi, sizeof(fdi));
411 if ((vap->va_type != VREG))
414 if (!fsess_isimpl(mp, FUSE_CREATE)) {
415 /* Fallback to FUSE_MKNOD/FUSE_OPEN */
416 fdisp_make_mknod_for_fallback(fdip, cnp, dvp, parentnid, td,
419 /* Use FUSE_CREATE */
421 fdisp_init(fdip, sizeof(*foi) + cnp->cn_namelen + 1);
422 fdisp_make(fdip, op, vnode_mount(dvp), parentnid, td, cred);
425 foi->flags = O_CREAT | flags;
426 memcpy((char *)fdip->indata + sizeof(*foi), cnp->cn_nameptr,
428 ((char *)fdip->indata)[sizeof(*foi) + cnp->cn_namelen] = '\0';
431 err = fdisp_wait_answ(fdip);
434 if (err == ENOSYS && op == FUSE_CREATE) {
435 fsess_set_notimpl(mp, FUSE_CREATE);
436 fdisp_make_mknod_for_fallback(fdip, cnp, dvp,
437 parentnid, td, cred, mode, &op);
438 err = fdisp_wait_answ(fdip);
446 if ((err = fuse_internal_checkentry(feo, VREG))) {
450 if (op == FUSE_CREATE) {
451 foo = (struct fuse_open_out*)(feo + 1);
453 /* Issue a separate FUSE_OPEN */
455 fdisp_init(fdip2, sizeof(*foi));
456 fdisp_make(fdip2, FUSE_OPEN, vnode_mount(dvp), feo->nodeid, td,
461 err = fdisp_wait_answ(fdip2);
466 err = fuse_vnode_get(mp, feo, feo->nodeid, dvp, vpp, cnp, VREG);
468 struct fuse_release_in *fri;
469 uint64_t nodeid = feo->nodeid;
470 uint64_t fh_id = foo->fh;
472 fdisp_init(fdip, sizeof(*fri));
473 fdisp_make(fdip, FUSE_RELEASE, mp, nodeid, td, cred);
477 fuse_insert_callback(fdip->tick, fuse_internal_forget_callback);
478 fuse_insert_message(fdip->tick);
481 ASSERT_VOP_ELOCKED(*vpp, "fuse_vnop_create");
482 fuse_internal_cache_attrs(*vpp, &feo->attr, feo->attr_valid,
483 feo->attr_valid_nsec, NULL);
485 fuse_filehandle_init(*vpp, FUFH_RDWR, NULL, td, cred, foo);
486 fuse_vnode_open(*vpp, foo->open_flags, td);
487 cache_purge_negative(dvp);
491 fdisp_destroy(fdip2);
497 struct vnop_fdatasync_args {
498 struct vop_generic_args a_gen;
500 struct thread * a_td;
504 fuse_vnop_fdatasync(struct vop_fdatasync_args *ap)
506 struct vnode *vp = ap->a_vp;
507 struct thread *td = ap->a_td;
508 int waitfor = MNT_WAIT;
512 if (fuse_isdeadfs(vp)) {
515 if ((err = vop_stdfdatasync_buf(ap)))
518 return fuse_internal_fsync(vp, td, waitfor, true);
522 struct vnop_fsync_args {
523 struct vop_generic_args a_gen;
526 struct thread * a_td;
530 fuse_vnop_fsync(struct vop_fsync_args *ap)
532 struct vnode *vp = ap->a_vp;
533 struct thread *td = ap->a_td;
534 int waitfor = ap->a_waitfor;
537 if (fuse_isdeadfs(vp)) {
540 if ((err = vop_stdfsync(ap)))
543 return fuse_internal_fsync(vp, td, waitfor, false);
547 struct vnop_getattr_args {
550 struct ucred *a_cred;
555 fuse_vnop_getattr(struct vop_getattr_args *ap)
557 struct vnode *vp = ap->a_vp;
558 struct vattr *vap = ap->a_vap;
559 struct ucred *cred = ap->a_cred;
560 struct thread *td = curthread;
565 dataflags = fuse_get_mpdata(vnode_mount(vp))->dataflags;
567 /* Note that we are not bailing out on a dead file system just yet. */
569 if (!(dataflags & FSESS_INITED)) {
570 if (!vnode_isvroot(vp)) {
571 fdata_set_dead(fuse_get_mpdata(vnode_mount(vp)));
578 err = fuse_internal_getattr(vp, vap, cred, td);
579 if (err == ENOTCONN && vnode_isvroot(vp)) {
580 /* see comment in fuse_vfsop_statfs() */
587 bzero(vap, sizeof(*vap));
588 vap->va_type = vnode_vtype(vp);
594 struct vnop_inactive_args {
600 fuse_vnop_inactive(struct vop_inactive_args *ap)
602 struct vnode *vp = ap->a_vp;
603 struct thread *td = ap->a_td;
605 struct fuse_vnode_data *fvdat = VTOFUD(vp);
606 struct fuse_filehandle *fufh, *fufh_tmp;
610 LIST_FOREACH_SAFE(fufh, &fvdat->handles, next, fufh_tmp) {
611 if (need_flush && vp->v_type == VREG) {
612 if ((VTOFUD(vp)->flag & FN_SIZECHANGE) != 0) {
613 fuse_vnode_savesize(vp, NULL, 0);
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, fufh, td, NULL);
625 if ((fvdat->flag & FN_REVOKED) != 0 && fuse_reclaim_revoked) {
632 struct vnop_link_args {
633 struct vnode *a_tdvp;
635 struct componentname *a_cnp;
639 fuse_vnop_link(struct vop_link_args *ap)
641 struct vnode *vp = ap->a_vp;
642 struct vnode *tdvp = ap->a_tdvp;
643 struct componentname *cnp = ap->a_cnp;
645 struct vattr *vap = VTOVA(vp);
647 struct fuse_dispatcher fdi;
648 struct fuse_entry_out *feo;
649 struct fuse_link_in fli;
653 if (fuse_isdeadfs(vp)) {
656 if (vnode_mount(tdvp) != vnode_mount(vp)) {
661 * This is a seatbelt check to protect naive userspace filesystems from
662 * themselves and the limitations of the FUSE IPC protocol. If a
663 * filesystem does not allow attribute caching, assume it is capable of
664 * validating that nlink does not overflow.
666 if (vap != NULL && vap->va_nlink >= FUSE_LINK_MAX)
668 fli.oldnodeid = VTOI(vp);
671 fuse_internal_newentry_makerequest(vnode_mount(tdvp), VTOI(tdvp), cnp,
672 FUSE_LINK, &fli, sizeof(fli), &fdi);
673 if ((err = fdisp_wait_answ(&fdi))) {
678 err = fuse_internal_checkentry(feo, vnode_vtype(vp));
685 struct vnop_lookup_args {
686 struct vnodeop_desc *a_desc;
688 struct vnode **a_vpp;
689 struct componentname *a_cnp;
693 fuse_vnop_lookup(struct vop_lookup_args *ap)
695 struct vnode *dvp = ap->a_dvp;
696 struct vnode **vpp = ap->a_vpp;
697 struct componentname *cnp = ap->a_cnp;
698 struct thread *td = cnp->cn_thread;
699 struct ucred *cred = cnp->cn_cred;
701 int nameiop = cnp->cn_nameiop;
702 int flags = cnp->cn_flags;
703 int wantparent = flags & (LOCKPARENT | WANTPARENT);
704 int islastcn = flags & ISLASTCN;
705 struct mount *mp = vnode_mount(dvp);
709 struct vnode *vp = NULL;
711 struct fuse_dispatcher fdi;
715 struct fuse_access_param facp;
717 if (fuse_isdeadfs(dvp)) {
721 if (!vnode_isdir(dvp)) {
724 if (islastcn && vfs_isrdonly(mp) && (nameiop != LOOKUP)) {
728 * We do access check prior to doing anything else only in the case
729 * when we are at fs root (we'd like to say, "we are at the first
730 * component", but that's not exactly the same... nevermind).
731 * See further comments at further access checks.
734 bzero(&facp, sizeof(facp));
735 if (vnode_isvroot(dvp)) { /* early permission check hack */
736 if ((err = fuse_internal_access(dvp, VEXEC, &facp, td, cred))) {
740 if (flags & ISDOTDOT) {
741 nid = VTOFUD(dvp)->parent_nid;
748 } else if (cnp->cn_namelen == 1 && *(cnp->cn_nameptr) == '.') {
753 } else if (fuse_lookup_cache_expire) {
754 struct timespec now, timeout;
756 err = cache_lookup(dvp, vpp, cnp, &timeout, NULL);
759 case -1: /* positive match */
761 if (timespeccmp(&timeout, &now, <=)) {
762 atomic_add_acq_long(&fuse_lookup_cache_hits, 1);
765 atomic_add_acq_long(&fuse_lookup_cache_misses,
772 case 0: /* no match in cache */
773 atomic_add_acq_long(&fuse_lookup_cache_misses, 1);
776 case ENOENT: /* negative match */
778 if (timespeccmp(&timeout, &now, >)) {
780 printf("Purging vnode %p name=%s\n", *vpp,
782 fuse_internal_vnode_disappear(*vpp);
790 err = cache_lookup(dvp, vpp, cnp, NULL, NULL);
793 case -1: /* positive match */
794 atomic_add_acq_long(&fuse_lookup_cache_hits, 1);
797 case 0: /* no match in cache */
798 atomic_add_acq_long(&fuse_lookup_cache_misses, 1);
801 case ENOENT: /* negative match */
808 fdisp_init(&fdi, cnp->cn_namelen + 1);
812 fdisp_make(&fdi, op, mp, nid, td, cred);
814 if (op == FUSE_LOOKUP) {
815 memcpy(fdi.indata, cnp->cn_nameptr, cnp->cn_namelen);
816 ((char *)fdi.indata)[cnp->cn_namelen] = '\0';
818 lookup_err = fdisp_wait_answ(&fdi);
820 if ((op == FUSE_LOOKUP) && !lookup_err) { /* lookup call succeeded */
821 nid = ((struct fuse_entry_out *)fdi.answ)->nodeid;
824 * zero nodeid is the same as "not found",
825 * but it's also cacheable (which we keep
826 * keep on doing not as of writing this)
828 fdi.answ_stat = ENOENT;
830 } else if (nid == FUSE_ROOT_ID) {
835 (!fdi.answ_stat || lookup_err != ENOENT || op != FUSE_LOOKUP)) {
839 /* lookup_err, if non-zero, must be ENOENT at this point */
843 if ((nameiop == CREATE || nameiop == RENAME) && islastcn
844 /* && directory dvp has not been removed */ ) {
846 if (vfs_isrdonly(mp)) {
850 #if 0 /* THINK_ABOUT_THIS */
851 if ((err = fuse_internal_access(dvp, VWRITE, cred, td, &facp))) {
857 * Possibly record the position of a slot in the
858 * directory large enough for the new component name.
859 * This can be recorded in the vnode private data for
860 * dvp. Set the SAVENAME flag to hold onto the
861 * pathname for use later in VOP_CREATE or VOP_RENAME.
863 cnp->cn_flags |= SAVENAME;
868 /* Consider inserting name into cache. */
871 * No we can't use negative caching, as the fs
872 * changes are out of our control.
873 * False positives' falseness turns out just as things
874 * go by, but false negatives' falseness doesn't.
875 * (and aiding the caching mechanism with extra control
876 * mechanisms comes quite close to beating the whole purpose
880 if ((cnp->cn_flags & MAKEENTRY) != 0) {
881 SDT_PROBE2(fuse, , vnops, trace, 1,
882 "inserting NULL into cache");
883 cache_enter(dvp, NULL, cnp);
893 struct fuse_entry_out *feo = NULL;
894 struct fuse_attr *fattr = NULL;
896 if (op == FUSE_GETATTR) {
897 fattr = &((struct fuse_attr_out *)fdi.answ)->attr;
899 feo = (struct fuse_entry_out *)fdi.answ;
900 fattr = &(feo->attr);
904 * If deleting, and at end of pathname, return parameters
905 * which can be used to remove file. If the wantparent flag
906 * isn't set, we return only the directory, otherwise we go on
907 * and lock the inode, being careful with ".".
909 if (nameiop == DELETE && islastcn) {
911 * Check for write access on directory.
913 facp.xuid = fattr->uid;
914 facp.facc_flags |= FACCESS_STICKY;
915 err = fuse_internal_access(dvp, VWRITE, &facp, td, cred);
916 facp.facc_flags &= ~FACCESS_XQUERIES;
921 if (nid == VTOI(dvp)) {
925 err = fuse_vnode_get(dvp->v_mount, feo, nid,
926 dvp, &vp, cnp, IFTOVT(fattr->mode));
933 * Save the name for use in VOP_RMDIR and VOP_REMOVE
936 cnp->cn_flags |= SAVENAME;
941 * If rewriting (RENAME), return the inode and the
942 * information required to rewrite the present directory
943 * Must get inode of directory entry to verify it's a
944 * regular file, or empty directory.
946 if (nameiop == RENAME && wantparent && islastcn) {
948 #if 0 /* THINK_ABOUT_THIS */
949 if ((err = fuse_internal_access(dvp, VWRITE, cred, td, &facp))) {
957 if (nid == VTOI(dvp)) {
961 err = fuse_vnode_get(vnode_mount(dvp), feo, nid, dvp,
962 &vp, cnp, IFTOVT(fattr->mode));
968 * Save the name for use in VOP_RENAME later.
970 cnp->cn_flags |= SAVENAME;
974 if (flags & ISDOTDOT) {
979 * Expanded copy of vn_vget_ino() so that
980 * fuse_vnode_get() can be used.
983 ltype = VOP_ISLOCKED(dvp);
984 err = vfs_busy(mp, MBF_NOWAIT);
988 err = vfs_busy(mp, 0);
989 vn_lock(dvp, ltype | LK_RETRY);
993 if ((dvp->v_iflag & VI_DOOMED) != 0) {
1000 err = fuse_vnode_get(vnode_mount(dvp), feo, nid, NULL,
1001 &vp, cnp, IFTOVT(fattr->mode));
1003 vn_lock(dvp, ltype | LK_RETRY);
1004 if ((dvp->v_iflag & VI_DOOMED) != 0) {
1012 } else if (nid == VTOI(dvp)) {
1016 struct fuse_vnode_data *fvdat;
1018 err = fuse_vnode_get(vnode_mount(dvp), feo, nid, dvp,
1019 &vp, cnp, IFTOVT(fattr->mode));
1023 fuse_vnode_setparent(vp, dvp);
1026 * In the case where we are looking up a FUSE node
1027 * represented by an existing cached vnode, and the
1028 * true size reported by FUSE_LOOKUP doesn't match
1029 * the vnode's cached size, fix the vnode cache to
1030 * match the real object size.
1032 * This can occur via FUSE distributed filesystems,
1033 * irregular files, etc.
1036 if (vnode_isreg(vp) &&
1037 fattr->size != fvdat->filesize) {
1039 * The FN_SIZECHANGE flag reflects a dirty
1040 * append. If userspace lets us know our cache
1041 * is invalid, that write was lost. (Dirty
1042 * writes that do not cause append are also
1043 * lost, but we don't detect them here.)
1045 * XXX: Maybe disable WB caching on this mount.
1047 if (fvdat->flag & FN_SIZECHANGE)
1048 printf("%s: WB cache incoherent on "
1050 vnode_mount(vp)->mnt_stat.f_mntonname);
1052 (void)fuse_vnode_setsize(vp, cred, fattr->size);
1053 fvdat->flag &= ~FN_SIZECHANGE;
1058 if (op == FUSE_GETATTR) {
1059 struct fuse_attr_out *fao =
1060 (struct fuse_attr_out*)fdi.answ;
1061 fuse_internal_cache_attrs(*vpp,
1062 &fao->attr, fao->attr_valid,
1063 fao->attr_valid_nsec, NULL);
1065 struct fuse_entry_out *feo =
1066 (struct fuse_entry_out*)fdi.answ;
1067 fuse_internal_cache_attrs(*vpp,
1068 &feo->attr, feo->attr_valid,
1069 feo->attr_valid_nsec, NULL);
1072 /* Insert name into cache if appropriate. */
1075 * Nooo, caching is evil. With caching, we can't avoid stale
1076 * information taking over the playground (cached info is not
1077 * just positive/negative, it does have qualitative aspects,
1078 * too). And a (VOP/FUSE)_GETATTR is always thrown anyway, when
1079 * walking down along cached path components, and that's not
1080 * any cheaper than FUSE_LOOKUP. This might change with
1081 * implementing kernel side attr caching, but... In Linux,
1082 * lookup results are not cached, and the daemon is bombarded
1083 * with FUSE_LOOKUPS on and on. This shows that by design, the
1084 * daemon is expected to handle frequent lookup queries
1085 * efficiently, do its caching in userspace, and so on.
1087 * So just leave the name cache alone.
1091 * Well, now I know, Linux caches lookups, but with a
1092 * timeout... So it's the same thing as attribute caching:
1093 * we can deal with it when implement timeouts.
1096 if (cnp->cn_flags & MAKEENTRY) {
1097 cache_enter(dvp, *vpp, cnp);
1104 /* No lookup error; need to clean up. */
1106 if (err) { /* Found inode; exit with no vnode. */
1107 if (op == FUSE_LOOKUP) {
1108 fuse_internal_forget_send(vnode_mount(dvp), td, cred,
1111 fdisp_destroy(&fdi);
1114 #ifndef NO_EARLY_PERM_CHECK_HACK
1117 * We have the attributes of the next item
1118 * *now*, and it's a fact, and we do not
1119 * have to do extra work for it (ie, beg the
1120 * daemon), and it neither depends on such
1121 * accidental things like attr caching. So
1122 * the big idea: check credentials *now*,
1123 * not at the beginning of the next call to
1126 * The first item of the lookup chain (fs root)
1127 * won't be checked then here, of course, as
1128 * its never "the next". But go and see that
1129 * the root is taken care about at the very
1130 * beginning of this function.
1132 * Now, given we want to do the access check
1133 * this way, one might ask: so then why not
1134 * do the access check just after fetching
1135 * the inode and its attributes from the
1136 * daemon? Why bother with producing the
1137 * corresponding vnode at all if something
1138 * is not OK? We know what's the deal as
1139 * soon as we get those attrs... There is
1140 * one bit of info though not given us by
1141 * the daemon: whether his response is
1142 * authoritative or not... His response should
1143 * be ignored if something is mounted over
1144 * the dir in question. But that can be
1145 * known only by having the vnode...
1147 int tmpvtype = vnode_vtype(*vpp);
1149 bzero(&facp, sizeof(facp));
1150 /*the early perm check hack */
1151 facp.facc_flags |= FACCESS_VA_VALID;
1153 if ((tmpvtype != VDIR) && (tmpvtype != VLNK)) {
1156 if (!err && !vnode_mountedhere(*vpp)) {
1157 err = fuse_internal_access(*vpp, VEXEC, &facp, td, cred);
1160 if (tmpvtype == VLNK)
1161 SDT_PROBE2(fuse, , vnops, trace,
1162 1, "weird, permission "
1163 "error with a symlink?");
1171 fdisp_destroy(&fdi);
1177 struct vnop_mkdir_args {
1178 struct vnode *a_dvp;
1179 struct vnode **a_vpp;
1180 struct componentname *a_cnp;
1181 struct vattr *a_vap;
1185 fuse_vnop_mkdir(struct vop_mkdir_args *ap)
1187 struct vnode *dvp = ap->a_dvp;
1188 struct vnode **vpp = ap->a_vpp;
1189 struct componentname *cnp = ap->a_cnp;
1190 struct vattr *vap = ap->a_vap;
1192 struct fuse_mkdir_in fmdi;
1194 if (fuse_isdeadfs(dvp)) {
1197 fmdi.mode = MAKEIMODE(vap->va_type, vap->va_mode);
1199 return (fuse_internal_newentry(dvp, vpp, cnp, FUSE_MKDIR, &fmdi,
1200 sizeof(fmdi), VDIR));
1204 struct vnop_mknod_args {
1205 struct vnode *a_dvp;
1206 struct vnode **a_vpp;
1207 struct componentname *a_cnp;
1208 struct vattr *a_vap;
1212 fuse_vnop_mknod(struct vop_mknod_args *ap)
1215 struct vnode *dvp = ap->a_dvp;
1216 struct vnode **vpp = ap->a_vpp;
1217 struct componentname *cnp = ap->a_cnp;
1218 struct vattr *vap = ap->a_vap;
1219 struct fuse_mknod_in fmni;
1221 if (fuse_isdeadfs(dvp))
1224 fmni.mode = MAKEIMODE(vap->va_type, vap->va_mode);
1225 fmni.rdev = vap->va_rdev;
1226 return (fuse_internal_newentry(dvp, vpp, cnp, FUSE_MKNOD, &fmni,
1227 sizeof(fmni), vap->va_type));
1231 struct vnop_open_args {
1234 struct ucred *a_cred;
1235 struct thread *a_td;
1236 int a_fdidx; / struct file *a_fp;
1240 fuse_vnop_open(struct vop_open_args *ap)
1242 struct vnode *vp = ap->a_vp;
1243 int a_mode = ap->a_mode;
1244 struct thread *td = ap->a_td;
1245 struct ucred *cred = ap->a_cred;
1246 pid_t pid = td->td_proc->p_pid;
1247 struct fuse_vnode_data *fvdat;
1249 if (fuse_isdeadfs(vp))
1251 if (vp->v_type == VCHR || vp->v_type == VBLK || vp->v_type == VFIFO)
1252 return (EOPNOTSUPP);
1253 if ((a_mode & (FREAD | FWRITE | FEXEC)) == 0)
1258 if (fuse_filehandle_validrw(vp, a_mode, cred, pid)) {
1259 fuse_vnode_open(vp, 0, td);
1263 return fuse_filehandle_open(vp, a_mode, NULL, td, cred);
1267 fuse_vnop_pathconf(struct vop_pathconf_args *ap)
1270 switch (ap->a_name) {
1271 case _PC_FILESIZEBITS:
1275 *ap->a_retval = NAME_MAX;
1278 *ap->a_retval = MIN(LONG_MAX, FUSE_LINK_MAX);
1280 case _PC_SYMLINK_MAX:
1281 *ap->a_retval = MAXPATHLEN;
1287 return (vop_stdpathconf(ap));
1292 struct vnop_read_args {
1296 struct ucred *a_cred;
1300 fuse_vnop_read(struct vop_read_args *ap)
1302 struct vnode *vp = ap->a_vp;
1303 struct uio *uio = ap->a_uio;
1304 int ioflag = ap->a_ioflag;
1305 struct ucred *cred = ap->a_cred;
1306 pid_t pid = curthread->td_proc->p_pid;
1308 if (fuse_isdeadfs(vp)) {
1312 if (VTOFUD(vp)->flag & FN_DIRECTIO) {
1313 ioflag |= IO_DIRECT;
1316 return fuse_io_dispatch(vp, uio, ioflag, cred, pid);
1320 struct vnop_readdir_args {
1323 struct ucred *a_cred;
1330 fuse_vnop_readdir(struct vop_readdir_args *ap)
1332 struct vnode *vp = ap->a_vp;
1333 struct uio *uio = ap->a_uio;
1334 struct ucred *cred = ap->a_cred;
1335 struct fuse_filehandle *fufh = NULL;
1336 struct fuse_iov cookediov;
1338 pid_t pid = curthread->td_proc->p_pid;
1340 if (fuse_isdeadfs(vp)) {
1343 if ( /* XXXIP ((uio_iovcnt(uio) > 1)) || */
1344 (uio_resid(uio) < sizeof(struct dirent))) {
1348 err = fuse_filehandle_get_dir(vp, &fufh, cred, pid);
1351 #define DIRCOOKEDSIZE FUSE_DIRENT_ALIGN(FUSE_NAME_OFFSET + MAXNAMLEN + 1)
1352 fiov_init(&cookediov, DIRCOOKEDSIZE);
1354 err = fuse_internal_readdir(vp, uio, fufh, &cookediov);
1356 fiov_teardown(&cookediov);
1362 struct vnop_readlink_args {
1365 struct ucred *a_cred;
1369 fuse_vnop_readlink(struct vop_readlink_args *ap)
1371 struct vnode *vp = ap->a_vp;
1372 struct uio *uio = ap->a_uio;
1373 struct ucred *cred = ap->a_cred;
1375 struct fuse_dispatcher fdi;
1378 if (fuse_isdeadfs(vp)) {
1381 if (!vnode_islnk(vp)) {
1384 fdisp_init(&fdi, 0);
1385 err = fdisp_simple_putget_vp(&fdi, FUSE_READLINK, vp, curthread, cred);
1389 if (((char *)fdi.answ)[0] == '/' &&
1390 fuse_get_mpdata(vnode_mount(vp))->dataflags & FSESS_PUSH_SYMLINKS_IN) {
1391 char *mpth = vnode_mount(vp)->mnt_stat.f_mntonname;
1393 err = uiomove(mpth, strlen(mpth), uio);
1396 err = uiomove(fdi.answ, fdi.iosize, uio);
1399 fdisp_destroy(&fdi);
1404 struct vnop_reclaim_args {
1406 struct thread *a_td;
1410 fuse_vnop_reclaim(struct vop_reclaim_args *ap)
1412 struct vnode *vp = ap->a_vp;
1413 struct thread *td = ap->a_td;
1414 struct fuse_vnode_data *fvdat = VTOFUD(vp);
1415 struct fuse_filehandle *fufh, *fufh_tmp;
1418 panic("FUSE: no vnode data during recycling");
1420 LIST_FOREACH_SAFE(fufh, &fvdat->handles, next, fufh_tmp) {
1421 printf("FUSE: vnode being reclaimed with open fufh "
1422 "(type=%#x)", fufh->fufh_type);
1423 fuse_filehandle_close(vp, fufh, td, NULL);
1426 if ((!fuse_isdeadfs(vp)) && (fvdat->nlookup)) {
1427 fuse_internal_forget_send(vnode_mount(vp), td, NULL, VTOI(vp),
1430 fuse_vnode_setparent(vp, NULL);
1432 vfs_hash_remove(vp);
1433 vnode_destroy_vobject(vp);
1434 fuse_vnode_destroy(vp);
1440 struct vnop_remove_args {
1441 struct vnode *a_dvp;
1443 struct componentname *a_cnp;
1447 fuse_vnop_remove(struct vop_remove_args *ap)
1449 struct vnode *dvp = ap->a_dvp;
1450 struct vnode *vp = ap->a_vp;
1451 struct componentname *cnp = ap->a_cnp;
1455 if (fuse_isdeadfs(vp)) {
1458 if (vnode_isdir(vp)) {
1463 err = fuse_internal_remove(dvp, vp, cnp, FUSE_UNLINK);
1466 fuse_internal_vnode_disappear(vp);
1471 struct vnop_rename_args {
1472 struct vnode *a_fdvp;
1473 struct vnode *a_fvp;
1474 struct componentname *a_fcnp;
1475 struct vnode *a_tdvp;
1476 struct vnode *a_tvp;
1477 struct componentname *a_tcnp;
1481 fuse_vnop_rename(struct vop_rename_args *ap)
1483 struct vnode *fdvp = ap->a_fdvp;
1484 struct vnode *fvp = ap->a_fvp;
1485 struct componentname *fcnp = ap->a_fcnp;
1486 struct vnode *tdvp = ap->a_tdvp;
1487 struct vnode *tvp = ap->a_tvp;
1488 struct componentname *tcnp = ap->a_tcnp;
1489 struct fuse_data *data;
1493 if (fuse_isdeadfs(fdvp)) {
1496 if (fvp->v_mount != tdvp->v_mount ||
1497 (tvp && fvp->v_mount != tvp->v_mount)) {
1498 SDT_PROBE2(fuse, , vnops, trace, 1, "cross-device rename");
1505 * FUSE library is expected to check if target directory is not
1506 * under the source directory in the file system tree.
1507 * Linux performs this check at VFS level.
1509 data = fuse_get_mpdata(vnode_mount(tdvp));
1510 sx_xlock(&data->rename_lock);
1511 err = fuse_internal_rename(fdvp, fcnp, tdvp, tcnp);
1514 fuse_vnode_setparent(fvp, tdvp);
1516 fuse_vnode_setparent(tvp, NULL);
1518 sx_unlock(&data->rename_lock);
1520 if (tvp != NULL && tvp != fvp) {
1523 if (vnode_isdir(fvp)) {
1524 if ((tvp != NULL) && vnode_isdir(tvp)) {
1545 struct vnop_rmdir_args {
1546 struct vnode *a_dvp;
1548 struct componentname *a_cnp;
1552 fuse_vnop_rmdir(struct vop_rmdir_args *ap)
1554 struct vnode *dvp = ap->a_dvp;
1555 struct vnode *vp = ap->a_vp;
1559 if (fuse_isdeadfs(vp)) {
1562 if (VTOFUD(vp) == VTOFUD(dvp)) {
1565 err = fuse_internal_remove(dvp, vp, ap->a_cnp, FUSE_RMDIR);
1568 fuse_internal_vnode_disappear(vp);
1573 struct vnop_setattr_args {
1575 struct vattr *a_vap;
1576 struct ucred *a_cred;
1577 struct thread *a_td;
1581 fuse_vnop_setattr(struct vop_setattr_args *ap)
1583 struct vnode *vp = ap->a_vp;
1584 struct vattr *vap = ap->a_vap;
1585 struct ucred *cred = ap->a_cred;
1586 struct thread *td = curthread;
1587 struct fuse_dispatcher fdi;
1588 struct fuse_setattr_in *fsai;
1589 struct fuse_access_param facp;
1590 pid_t pid = td->td_proc->p_pid;
1594 int sizechanged = 0;
1595 uint64_t newsize = 0;
1597 if (fuse_isdeadfs(vp)) {
1600 fdisp_init(&fdi, sizeof(*fsai));
1601 fdisp_make_vp(&fdi, FUSE_SETATTR, vp, td, cred);
1605 bzero(&facp, sizeof(facp));
1607 facp.xuid = vap->va_uid;
1608 facp.xgid = vap->va_gid;
1610 if (vap->va_uid != (uid_t)VNOVAL) {
1611 facp.facc_flags |= FACCESS_CHOWN;
1612 fsai->uid = vap->va_uid;
1613 fsai->valid |= FATTR_UID;
1615 if (vap->va_gid != (gid_t)VNOVAL) {
1616 facp.facc_flags |= FACCESS_CHOWN;
1617 fsai->gid = vap->va_gid;
1618 fsai->valid |= FATTR_GID;
1620 if (vap->va_size != VNOVAL) {
1622 struct fuse_filehandle *fufh = NULL;
1624 /*Truncate to a new value. */
1625 fsai->size = vap->va_size;
1627 newsize = vap->va_size;
1628 fsai->valid |= FATTR_SIZE;
1630 fuse_filehandle_getrw(vp, FWRITE, &fufh, cred, pid);
1632 fsai->fh = fufh->fh_id;
1633 fsai->valid |= FATTR_FH;
1636 if (vap->va_atime.tv_sec != VNOVAL) {
1637 fsai->atime = vap->va_atime.tv_sec;
1638 fsai->atimensec = vap->va_atime.tv_nsec;
1639 fsai->valid |= FATTR_ATIME;
1641 if (vap->va_mtime.tv_sec != VNOVAL) {
1642 fsai->mtime = vap->va_mtime.tv_sec;
1643 fsai->mtimensec = vap->va_mtime.tv_nsec;
1644 fsai->valid |= FATTR_MTIME;
1646 if (vap->va_mode != (mode_t)VNOVAL) {
1647 fsai->mode = vap->va_mode & ALLPERMS;
1648 fsai->valid |= FATTR_MODE;
1653 vtyp = vnode_vtype(vp);
1655 if (fsai->valid & FATTR_SIZE && vtyp == VDIR) {
1659 if (vfs_isrdonly(vnode_mount(vp)) && (fsai->valid & ~FATTR_SIZE || vtyp == VREG)) {
1664 if ((err = fdisp_wait_answ(&fdi)))
1666 vtyp = IFTOVT(((struct fuse_attr_out *)fdi.answ)->attr.mode);
1668 if (vnode_vtype(vp) != vtyp) {
1669 if (vnode_vtype(vp) == VNON && vtyp != VNON) {
1670 SDT_PROBE2(fuse, , vnops, trace, 1, "FUSE: Dang! "
1671 "vnode_vtype is VNON and vtype isn't.");
1674 * STALE vnode, ditch
1676 * The vnode has changed its type "behind our back".
1677 * There's nothing really we can do, so let us just
1678 * force an internal revocation and tell the caller to
1679 * try again, if interested.
1681 fuse_internal_vnode_disappear(vp);
1686 struct fuse_attr_out *fao = (struct fuse_attr_out*)fdi.answ;
1687 fuse_internal_cache_attrs(vp, &fao->attr, fao->attr_valid,
1688 fao->attr_valid_nsec, NULL);
1692 fdisp_destroy(&fdi);
1693 if (!err && sizechanged) {
1694 fuse_vnode_setsize(vp, cred, newsize);
1695 VTOFUD(vp)->flag &= ~FN_SIZECHANGE;
1701 struct vnop_strategy_args {
1707 fuse_vnop_strategy(struct vop_strategy_args *ap)
1709 struct vnode *vp = ap->a_vp;
1710 struct buf *bp = ap->a_bp;
1712 if (!vp || fuse_isdeadfs(vp)) {
1713 bp->b_ioflags |= BIO_ERROR;
1714 bp->b_error = ENXIO;
1718 if (bp->b_iocmd == BIO_WRITE) {
1721 err = fuse_vnode_refreshsize(vp, NOCRED);
1723 bp->b_ioflags |= BIO_ERROR;
1731 * VOP_STRATEGY always returns zero and signals error via bp->b_ioflags.
1732 * fuse_io_strategy sets bp's error fields
1734 (void)fuse_io_strategy(vp, bp);
1741 struct vnop_symlink_args {
1742 struct vnode *a_dvp;
1743 struct vnode **a_vpp;
1744 struct componentname *a_cnp;
1745 struct vattr *a_vap;
1750 fuse_vnop_symlink(struct vop_symlink_args *ap)
1752 struct vnode *dvp = ap->a_dvp;
1753 struct vnode **vpp = ap->a_vpp;
1754 struct componentname *cnp = ap->a_cnp;
1755 const char *target = ap->a_target;
1757 struct fuse_dispatcher fdi;
1762 if (fuse_isdeadfs(dvp)) {
1766 * Unlike the other creator type calls, here we have to create a message
1767 * where the name of the new entry comes first, and the data describing
1768 * the entry comes second.
1769 * Hence we can't rely on our handy fuse_internal_newentry() routine,
1770 * but put together the message manually and just call the core part.
1773 len = strlen(target) + 1;
1774 fdisp_init(&fdi, len + cnp->cn_namelen + 1);
1775 fdisp_make_vp(&fdi, FUSE_SYMLINK, dvp, curthread, NULL);
1777 memcpy(fdi.indata, cnp->cn_nameptr, cnp->cn_namelen);
1778 ((char *)fdi.indata)[cnp->cn_namelen] = '\0';
1779 memcpy((char *)fdi.indata + cnp->cn_namelen + 1, target, len);
1781 err = fuse_internal_newentry_core(dvp, vpp, cnp, VLNK, &fdi);
1782 fdisp_destroy(&fdi);
1787 struct vnop_write_args {
1791 struct ucred *a_cred;
1795 fuse_vnop_write(struct vop_write_args *ap)
1797 struct vnode *vp = ap->a_vp;
1798 struct uio *uio = ap->a_uio;
1799 int ioflag = ap->a_ioflag;
1800 struct ucred *cred = ap->a_cred;
1801 pid_t pid = curthread->td_proc->p_pid;
1804 if (fuse_isdeadfs(vp)) {
1807 err = fuse_vnode_refreshsize(vp, cred);
1811 if (VTOFUD(vp)->flag & FN_DIRECTIO) {
1812 ioflag |= IO_DIRECT;
1815 return fuse_io_dispatch(vp, uio, ioflag, cred, pid);
1818 SDT_PROBE_DEFINE1(fuse, , vnops, vnop_getpages_error, "int");
1820 struct vnop_getpages_args {
1828 fuse_vnop_getpages(struct vop_getpages_args *ap)
1830 int i, error, nextoff, size, toff, count, npages;
1839 pid_t pid = curthread->td_proc->p_pid;
1842 KASSERT(vp->v_object, ("objectless vp passed to getpages"));
1843 td = curthread; /* XXX */
1844 cred = curthread->td_ucred; /* XXX */
1846 npages = ap->a_count;
1848 if (!fsess_opt_mmap(vnode_mount(vp))) {
1849 SDT_PROBE2(fuse, , vnops, trace, 1,
1850 "called on non-cacheable vnode??\n");
1851 return (VM_PAGER_ERROR);
1855 * If the last page is partially valid, just return it and allow
1856 * the pager to zero-out the blanks. Partially valid pages can
1857 * only occur at the file EOF.
1859 * XXXGL: is that true for FUSE, which is a local filesystem,
1860 * but still somewhat disconnected from the kernel?
1862 VM_OBJECT_WLOCK(vp->v_object);
1863 if (pages[npages - 1]->valid != 0 && --npages == 0)
1865 VM_OBJECT_WUNLOCK(vp->v_object);
1868 * We use only the kva address for the buffer, but this is extremely
1869 * convenient and fast.
1871 bp = uma_zalloc(fuse_pbuf_zone, M_WAITOK);
1873 kva = (vm_offset_t)bp->b_data;
1874 pmap_qenter(kva, pages, npages);
1875 VM_CNT_INC(v_vnodein);
1876 VM_CNT_ADD(v_vnodepgsin, npages);
1878 count = npages << PAGE_SHIFT;
1879 iov.iov_base = (caddr_t)kva;
1880 iov.iov_len = count;
1883 uio.uio_offset = IDX_TO_OFF(pages[0]->pindex);
1884 uio.uio_resid = count;
1885 uio.uio_segflg = UIO_SYSSPACE;
1886 uio.uio_rw = UIO_READ;
1889 error = fuse_io_dispatch(vp, &uio, IO_DIRECT, cred, pid);
1890 pmap_qremove(kva, npages);
1892 uma_zfree(fuse_pbuf_zone, bp);
1894 if (error && (uio.uio_resid == count)) {
1895 SDT_PROBE1(fuse, , vnops, vnop_getpages_error, error);
1896 return VM_PAGER_ERROR;
1899 * Calculate the number of bytes read and validate only that number
1900 * of bytes. Note that due to pending writes, size may be 0. This
1901 * does not mean that the remaining data is invalid!
1904 size = count - uio.uio_resid;
1905 VM_OBJECT_WLOCK(vp->v_object);
1906 fuse_vm_page_lock_queues();
1907 for (i = 0, toff = 0; i < npages; i++, toff = nextoff) {
1910 nextoff = toff + PAGE_SIZE;
1913 if (nextoff <= size) {
1915 * Read operation filled an entire page
1917 m->valid = VM_PAGE_BITS_ALL;
1918 KASSERT(m->dirty == 0,
1919 ("fuse_getpages: page %p is dirty", m));
1920 } else if (size > toff) {
1922 * Read operation filled a partial page.
1925 vm_page_set_valid_range(m, 0, size - toff);
1926 KASSERT(m->dirty == 0,
1927 ("fuse_getpages: page %p is dirty", m));
1930 * Read operation was short. If no error occurred
1931 * we may have hit a zero-fill section. We simply
1932 * leave valid set to 0.
1937 fuse_vm_page_unlock_queues();
1939 VM_OBJECT_WUNLOCK(vp->v_object);
1944 return (VM_PAGER_OK);
1948 struct vnop_putpages_args {
1954 vm_ooffset_t a_offset;
1958 fuse_vnop_putpages(struct vop_putpages_args *ap)
1964 int i, error, npages, count;
1972 pid_t pid = curthread->td_proc->p_pid;
1975 KASSERT(vp->v_object, ("objectless vp passed to putpages"));
1976 fsize = vp->v_object->un_pager.vnp.vnp_size;
1977 td = curthread; /* XXX */
1978 cred = curthread->td_ucred; /* XXX */
1980 count = ap->a_count;
1981 rtvals = ap->a_rtvals;
1982 npages = btoc(count);
1983 offset = IDX_TO_OFF(pages[0]->pindex);
1985 if (!fsess_opt_mmap(vnode_mount(vp))) {
1986 SDT_PROBE2(fuse, , vnops, trace, 1,
1987 "called on non-cacheable vnode??\n");
1989 for (i = 0; i < npages; i++)
1990 rtvals[i] = VM_PAGER_AGAIN;
1993 * When putting pages, do not extend file past EOF.
1996 if (offset + count > fsize) {
1997 count = fsize - offset;
2002 * We use only the kva address for the buffer, but this is extremely
2003 * convenient and fast.
2005 bp = uma_zalloc(fuse_pbuf_zone, M_WAITOK);
2007 kva = (vm_offset_t)bp->b_data;
2008 pmap_qenter(kva, pages, npages);
2009 VM_CNT_INC(v_vnodeout);
2010 VM_CNT_ADD(v_vnodepgsout, count);
2012 iov.iov_base = (caddr_t)kva;
2013 iov.iov_len = count;
2016 uio.uio_offset = offset;
2017 uio.uio_resid = count;
2018 uio.uio_segflg = UIO_SYSSPACE;
2019 uio.uio_rw = UIO_WRITE;
2022 error = fuse_io_dispatch(vp, &uio, IO_DIRECT, cred, pid);
2024 pmap_qremove(kva, npages);
2025 uma_zfree(fuse_pbuf_zone, bp);
2028 int nwritten = round_page(count - uio.uio_resid) / PAGE_SIZE;
2030 for (i = 0; i < nwritten; i++) {
2031 rtvals[i] = VM_PAGER_OK;
2032 VM_OBJECT_WLOCK(pages[i]->object);
2033 vm_page_undirty(pages[i]);
2034 VM_OBJECT_WUNLOCK(pages[i]->object);
2040 static const char extattr_namespace_separator = '.';
2043 struct vop_getextattr_args {
2044 struct vop_generic_args a_gen;
2046 int a_attrnamespace;
2050 struct ucred *a_cred;
2051 struct thread *a_td;
2055 fuse_vnop_getextattr(struct vop_getextattr_args *ap)
2057 struct vnode *vp = ap->a_vp;
2058 struct uio *uio = ap->a_uio;
2059 struct fuse_dispatcher fdi;
2060 struct fuse_getxattr_in *get_xattr_in;
2061 struct fuse_getxattr_out *get_xattr_out;
2062 struct mount *mp = vnode_mount(vp);
2063 struct thread *td = ap->a_td;
2064 struct ucred *cred = ap->a_cred;
2070 if (fuse_isdeadfs(vp))
2073 /* Default to looking for user attributes. */
2074 if (ap->a_attrnamespace == EXTATTR_NAMESPACE_SYSTEM)
2075 prefix = EXTATTR_NAMESPACE_SYSTEM_STRING;
2077 prefix = EXTATTR_NAMESPACE_USER_STRING;
2079 len = strlen(prefix) + sizeof(extattr_namespace_separator) +
2080 strlen(ap->a_name) + 1;
2082 fdisp_init(&fdi, len + sizeof(*get_xattr_in));
2083 fdisp_make_vp(&fdi, FUSE_GETXATTR, vp, td, cred);
2085 get_xattr_in = fdi.indata;
2087 * Check to see whether we're querying the available size or
2088 * issuing the actual request. If we pass in 0, we get back struct
2089 * fuse_getxattr_out. If we pass in a non-zero size, we get back
2090 * that much data, without the struct fuse_getxattr_out header.
2093 get_xattr_in->size = 0;
2095 get_xattr_in->size = uio->uio_resid;
2097 attr_str = (char *)fdi.indata + sizeof(*get_xattr_in);
2098 snprintf(attr_str, len, "%s%c%s", prefix, extattr_namespace_separator,
2101 err = fdisp_wait_answ(&fdi);
2104 fsess_set_notimpl(mp, FUSE_GETXATTR);
2108 get_xattr_out = fdi.answ;
2110 if (ap->a_size != NULL)
2111 *ap->a_size = get_xattr_out->size;
2114 err = uiomove(fdi.answ, fdi.iosize, uio);
2117 fdisp_destroy(&fdi);
2122 struct vop_setextattr_args {
2123 struct vop_generic_args a_gen;
2125 int a_attrnamespace;
2128 struct ucred *a_cred;
2129 struct thread *a_td;
2133 fuse_vnop_setextattr(struct vop_setextattr_args *ap)
2135 struct vnode *vp = ap->a_vp;
2136 struct uio *uio = ap->a_uio;
2137 struct fuse_dispatcher fdi;
2138 struct fuse_setxattr_in *set_xattr_in;
2139 struct mount *mp = vnode_mount(vp);
2140 struct thread *td = ap->a_td;
2141 struct ucred *cred = ap->a_cred;
2147 if (fuse_isdeadfs(vp))
2150 /* Default to looking for user attributes. */
2151 if (ap->a_attrnamespace == EXTATTR_NAMESPACE_SYSTEM)
2152 prefix = EXTATTR_NAMESPACE_SYSTEM_STRING;
2154 prefix = EXTATTR_NAMESPACE_USER_STRING;
2156 len = strlen(prefix) + sizeof(extattr_namespace_separator) +
2157 strlen(ap->a_name) + 1;
2159 fdisp_init(&fdi, len + sizeof(*set_xattr_in) + uio->uio_resid);
2160 fdisp_make_vp(&fdi, FUSE_SETXATTR, vp, td, cred);
2162 set_xattr_in = fdi.indata;
2163 set_xattr_in->size = uio->uio_resid;
2165 attr_str = (char *)fdi.indata + sizeof(*set_xattr_in);
2166 snprintf(attr_str, len, "%s%c%s", prefix, extattr_namespace_separator,
2169 err = uiomove((char *)fdi.indata + sizeof(*set_xattr_in) + len,
2170 uio->uio_resid, uio);
2175 err = fdisp_wait_answ(&fdi);
2179 fsess_set_notimpl(mp, FUSE_SETXATTR);
2184 fdisp_destroy(&fdi);
2189 * The Linux / FUSE extended attribute list is simply a collection of
2190 * NUL-terminated strings. The FreeBSD extended attribute list is a single
2191 * byte length followed by a non-NUL terminated string. So, this allows
2192 * conversion of the Linux / FUSE format to the FreeBSD format in place.
2193 * Linux attribute names are reported with the namespace as a prefix (e.g.
2194 * "user.attribute_name"), but in FreeBSD they are reported without the
2195 * namespace prefix (e.g. "attribute_name"). So, we're going from:
2197 * user.attr_name1\0user.attr_name2\0
2201 * <num>attr_name1<num>attr_name2
2203 * Where "<num>" is a single byte number of characters in the attribute name.
2206 * prefix - exattr namespace prefix string
2207 * list, list_len - input list with namespace prefixes
2208 * bsd_list, bsd_list_len - output list compatible with bsd vfs
2211 fuse_xattrlist_convert(char *prefix, const char *list, int list_len,
2212 char *bsd_list, int *bsd_list_len)
2214 int len, pos, dist_to_next, prefix_len;
2218 prefix_len = strlen(prefix);
2220 while (pos < list_len && list[pos] != '\0') {
2221 dist_to_next = strlen(&list[pos]) + 1;
2222 if (bcmp(&list[pos], prefix, prefix_len) == 0 &&
2223 list[pos + prefix_len] == extattr_namespace_separator) {
2224 len = dist_to_next -
2225 (prefix_len + sizeof(extattr_namespace_separator)) - 1;
2226 if (len >= EXTATTR_MAXNAMELEN)
2227 return (ENAMETOOLONG);
2229 bsd_list[*bsd_list_len] = len;
2230 memcpy(&bsd_list[*bsd_list_len + 1],
2231 &list[pos + prefix_len +
2232 sizeof(extattr_namespace_separator)], len);
2234 *bsd_list_len += len + 1;
2237 pos += dist_to_next;
2244 struct vop_listextattr_args {
2245 struct vop_generic_args a_gen;
2247 int a_attrnamespace;
2250 struct ucred *a_cred;
2251 struct thread *a_td;
2255 fuse_vnop_listextattr(struct vop_listextattr_args *ap)
2257 struct vnode *vp = ap->a_vp;
2258 struct uio *uio = ap->a_uio;
2259 struct fuse_dispatcher fdi;
2260 struct fuse_listxattr_in *list_xattr_in;
2261 struct fuse_listxattr_out *list_xattr_out;
2262 struct mount *mp = vnode_mount(vp);
2263 struct thread *td = ap->a_td;
2264 struct ucred *cred = ap->a_cred;
2268 char *bsd_list = NULL;
2274 if (fuse_isdeadfs(vp))
2278 * Add space for a NUL and the period separator if enabled.
2279 * Default to looking for user attributes.
2281 if (ap->a_attrnamespace == EXTATTR_NAMESPACE_SYSTEM)
2282 prefix = EXTATTR_NAMESPACE_SYSTEM_STRING;
2284 prefix = EXTATTR_NAMESPACE_USER_STRING;
2286 len = strlen(prefix) + sizeof(extattr_namespace_separator) + 1;
2288 fdisp_init(&fdi, sizeof(*list_xattr_in) + len);
2289 fdisp_make_vp(&fdi, FUSE_LISTXATTR, vp, td, cred);
2292 * Retrieve Linux / FUSE compatible list size.
2294 list_xattr_in = fdi.indata;
2295 list_xattr_in->size = 0;
2296 attr_str = (char *)fdi.indata + sizeof(*list_xattr_in);
2297 snprintf(attr_str, len, "%s%c", prefix, extattr_namespace_separator);
2299 err = fdisp_wait_answ(&fdi);
2302 fsess_set_notimpl(mp, FUSE_LISTXATTR);
2306 list_xattr_out = fdi.answ;
2307 linux_list_len = list_xattr_out->size;
2308 if (linux_list_len == 0) {
2309 if (ap->a_size != NULL)
2310 *ap->a_size = linux_list_len;
2315 * Retrieve Linux / FUSE compatible list values.
2317 fdisp_refresh_vp(&fdi, FUSE_LISTXATTR, vp, td, cred);
2318 list_xattr_in = fdi.indata;
2319 list_xattr_in->size = linux_list_len + sizeof(*list_xattr_out);
2320 list_xattr_in->flags = 0;
2321 attr_str = (char *)fdi.indata + sizeof(*list_xattr_in);
2322 snprintf(attr_str, len, "%s%c", prefix, extattr_namespace_separator);
2324 err = fdisp_wait_answ(&fdi);
2328 linux_list = fdi.answ;
2329 linux_list_len = fdi.iosize;
2332 * Retrieve the BSD compatible list values.
2333 * The Linux / FUSE attribute list format isn't the same
2334 * as FreeBSD's format. So we need to transform it into
2335 * FreeBSD's format before giving it to the user.
2337 bsd_list = malloc(linux_list_len, M_TEMP, M_WAITOK);
2338 err = fuse_xattrlist_convert(prefix, linux_list, linux_list_len,
2339 bsd_list, &bsd_list_len);
2343 if (ap->a_size != NULL)
2344 *ap->a_size = bsd_list_len;
2347 err = uiomove(bsd_list, bsd_list_len, uio);
2350 free(bsd_list, M_TEMP);
2351 fdisp_destroy(&fdi);
2356 struct vop_deleteextattr_args {
2357 struct vop_generic_args a_gen;
2359 int a_attrnamespace;
2361 struct ucred *a_cred;
2362 struct thread *a_td;
2366 fuse_vnop_deleteextattr(struct vop_deleteextattr_args *ap)
2368 struct vnode *vp = ap->a_vp;
2369 struct fuse_dispatcher fdi;
2370 struct mount *mp = vnode_mount(vp);
2371 struct thread *td = ap->a_td;
2372 struct ucred *cred = ap->a_cred;
2378 if (fuse_isdeadfs(vp))
2381 /* Default to looking for user attributes. */
2382 if (ap->a_attrnamespace == EXTATTR_NAMESPACE_SYSTEM)
2383 prefix = EXTATTR_NAMESPACE_SYSTEM_STRING;
2385 prefix = EXTATTR_NAMESPACE_USER_STRING;
2387 len = strlen(prefix) + sizeof(extattr_namespace_separator) +
2388 strlen(ap->a_name) + 1;
2390 fdisp_init(&fdi, len);
2391 fdisp_make_vp(&fdi, FUSE_REMOVEXATTR, vp, td, cred);
2393 attr_str = fdi.indata;
2394 snprintf(attr_str, len, "%s%c%s", prefix, extattr_namespace_separator,
2397 err = fdisp_wait_answ(&fdi);
2400 fsess_set_notimpl(mp, FUSE_REMOVEXATTR);
2403 fdisp_destroy(&fdi);
2408 struct vnop_print_args {
2413 fuse_vnop_print(struct vop_print_args *ap)
2415 struct fuse_vnode_data *fvdat = VTOFUD(ap->a_vp);
2417 printf("nodeid: %ju, parent nodeid: %ju, nlookup: %ju, flag: %#x\n",
2418 (uintmax_t)VTOILLU(ap->a_vp), (uintmax_t)fvdat->parent_nid,
2419 (uintmax_t)fvdat->nlookup,