2 * SPDX-License-Identifier: BSD-3-Clause
4 * Copyright (c) 2007-2009 Google Inc. and Amit Singh
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions are
11 * * Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * * Redistributions in binary form must reproduce the above
14 * copyright notice, this list of conditions and the following disclaimer
15 * in the documentation and/or other materials provided with the
17 * * Neither the name of Google Inc. nor the names of its
18 * contributors may be used to endorse or promote products derived from
19 * this software without specific prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
24 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
25 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
26 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
27 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
31 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33 * Copyright (C) 2005 Csaba Henk.
34 * All rights reserved.
36 * Redistribution and use in source and binary forms, with or without
37 * modification, are permitted provided that the following conditions
39 * 1. Redistributions of source code must retain the above copyright
40 * notice, this list of conditions and the following disclaimer.
41 * 2. Redistributions in binary form must reproduce the above copyright
42 * notice, this list of conditions and the following disclaimer in the
43 * documentation and/or other materials provided with the distribution.
45 * THIS SOFTWARE IS PROVIDED BY AUTHOR AND CONTRIBUTORS ``AS IS'' AND
46 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
47 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
48 * ARE DISCLAIMED. IN NO EVENT SHALL AUTHOR OR CONTRIBUTORS BE LIABLE
49 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
50 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
51 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
52 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
53 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
54 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
58 #include <sys/cdefs.h>
59 __FBSDID("$FreeBSD$");
61 #include <sys/param.h>
62 #include <sys/module.h>
63 #include <sys/systm.h>
64 #include <sys/errno.h>
65 #include <sys/kernel.h>
68 #include <sys/malloc.h>
69 #include <sys/queue.h>
71 #include <sys/rwlock.h>
74 #include <sys/mount.h>
75 #include <sys/vnode.h>
76 #include <sys/namei.h>
77 #include <sys/extattr.h>
79 #include <sys/unistd.h>
80 #include <sys/filedesc.h>
82 #include <sys/fcntl.h>
83 #include <sys/dirent.h>
86 #include <sys/sysctl.h>
87 #include <sys/vmmeter.h>
90 #include <vm/vm_extern.h>
92 #include <vm/vm_map.h>
93 #include <vm/vm_page.h>
94 #include <vm/vm_param.h>
95 #include <vm/vm_object.h>
96 #include <vm/vm_pager.h>
97 #include <vm/vnode_pager.h>
98 #include <vm/vm_object.h>
101 #include "fuse_file.h"
102 #include "fuse_internal.h"
103 #include "fuse_ipc.h"
104 #include "fuse_node.h"
107 #include <sys/priv.h>
109 /* Maximum number of hardlinks to a single FUSE file */
110 #define FUSE_LINK_MAX UINT32_MAX
112 SDT_PROVIDER_DECLARE(fuse);
115 * arg0: verbosity. Higher numbers give more verbose messages
116 * arg1: Textual message
118 SDT_PROBE_DEFINE2(fuse, , vnops, trace, "int", "char*");
121 static vop_access_t fuse_vnop_access;
122 static vop_close_t fuse_vnop_close;
123 static vop_create_t fuse_vnop_create;
124 static vop_deleteextattr_t fuse_vnop_deleteextattr;
125 static vop_fdatasync_t fuse_vnop_fdatasync;
126 static vop_fsync_t fuse_vnop_fsync;
127 static vop_getattr_t fuse_vnop_getattr;
128 static vop_getextattr_t fuse_vnop_getextattr;
129 static vop_inactive_t fuse_vnop_inactive;
130 static vop_link_t fuse_vnop_link;
131 static vop_listextattr_t fuse_vnop_listextattr;
132 static vop_lookup_t fuse_vnop_lookup;
133 static vop_mkdir_t fuse_vnop_mkdir;
134 static vop_mknod_t fuse_vnop_mknod;
135 static vop_open_t fuse_vnop_open;
136 static vop_pathconf_t fuse_vnop_pathconf;
137 static vop_read_t fuse_vnop_read;
138 static vop_readdir_t fuse_vnop_readdir;
139 static vop_readlink_t fuse_vnop_readlink;
140 static vop_reclaim_t fuse_vnop_reclaim;
141 static vop_remove_t fuse_vnop_remove;
142 static vop_rename_t fuse_vnop_rename;
143 static vop_rmdir_t fuse_vnop_rmdir;
144 static vop_setattr_t fuse_vnop_setattr;
145 static vop_setextattr_t fuse_vnop_setextattr;
146 static vop_strategy_t fuse_vnop_strategy;
147 static vop_symlink_t fuse_vnop_symlink;
148 static vop_write_t fuse_vnop_write;
149 static vop_getpages_t fuse_vnop_getpages;
150 static vop_putpages_t fuse_vnop_putpages;
151 static vop_print_t fuse_vnop_print;
153 struct vop_vector fuse_vnops = {
154 .vop_default = &default_vnodeops,
155 .vop_access = fuse_vnop_access,
156 .vop_close = fuse_vnop_close,
157 .vop_create = fuse_vnop_create,
158 .vop_deleteextattr = fuse_vnop_deleteextattr,
159 .vop_fsync = fuse_vnop_fsync,
160 .vop_fdatasync = fuse_vnop_fdatasync,
161 .vop_getattr = fuse_vnop_getattr,
162 .vop_getextattr = fuse_vnop_getextattr,
163 .vop_inactive = fuse_vnop_inactive,
164 .vop_link = fuse_vnop_link,
165 .vop_listextattr = fuse_vnop_listextattr,
166 .vop_lookup = fuse_vnop_lookup,
167 .vop_mkdir = fuse_vnop_mkdir,
168 .vop_mknod = fuse_vnop_mknod,
169 .vop_open = fuse_vnop_open,
170 .vop_pathconf = fuse_vnop_pathconf,
171 .vop_read = fuse_vnop_read,
172 .vop_readdir = fuse_vnop_readdir,
173 .vop_readlink = fuse_vnop_readlink,
174 .vop_reclaim = fuse_vnop_reclaim,
175 .vop_remove = fuse_vnop_remove,
176 .vop_rename = fuse_vnop_rename,
177 .vop_rmdir = fuse_vnop_rmdir,
178 .vop_setattr = fuse_vnop_setattr,
179 .vop_setextattr = fuse_vnop_setextattr,
180 .vop_strategy = fuse_vnop_strategy,
181 .vop_symlink = fuse_vnop_symlink,
182 .vop_write = fuse_vnop_write,
183 .vop_getpages = fuse_vnop_getpages,
184 .vop_putpages = fuse_vnop_putpages,
185 .vop_print = fuse_vnop_print,
188 static u_long fuse_lookup_cache_hits = 0;
190 SYSCTL_ULONG(_vfs_fusefs, OID_AUTO, lookup_cache_hits, CTLFLAG_RD,
191 &fuse_lookup_cache_hits, 0, "number of positive cache hits in lookup");
193 static u_long fuse_lookup_cache_misses = 0;
195 SYSCTL_ULONG(_vfs_fusefs, OID_AUTO, lookup_cache_misses, CTLFLAG_RD,
196 &fuse_lookup_cache_misses, 0, "number of cache misses in lookup");
198 int fuse_lookup_cache_enable = 1;
200 SYSCTL_INT(_vfs_fusefs, OID_AUTO, lookup_cache_enable, CTLFLAG_RW,
201 &fuse_lookup_cache_enable, 0, "if non-zero, enable lookup cache");
204 * XXX: This feature is highly experimental and can bring to instabilities,
205 * needs revisiting before to be enabled by default.
207 static int fuse_reclaim_revoked = 0;
209 SYSCTL_INT(_vfs_fusefs, OID_AUTO, reclaim_revoked, CTLFLAG_RW,
210 &fuse_reclaim_revoked, 0, "");
212 uma_zone_t fuse_pbuf_zone;
214 #define fuse_vm_page_lock(m) vm_page_lock((m));
215 #define fuse_vm_page_unlock(m) vm_page_unlock((m));
216 #define fuse_vm_page_lock_queues() ((void)0)
217 #define fuse_vm_page_unlock_queues() ((void)0)
219 /* Get a filehandle for a directory */
221 fuse_filehandle_get_dir(struct vnode *vp, struct fuse_filehandle **fufhp,
222 struct ucred *cred, pid_t pid)
224 if (fuse_filehandle_get(vp, FUFH_RDONLY, fufhp, cred, pid) == 0)
226 return fuse_filehandle_get(vp, FUFH_EXEC, fufhp, cred, pid);
230 struct vnop_access_args {
232 #if VOP_ACCESS_TAKES_ACCMODE_T
237 struct ucred *a_cred;
242 fuse_vnop_access(struct vop_access_args *ap)
244 struct vnode *vp = ap->a_vp;
245 int accmode = ap->a_accmode;
246 struct ucred *cred = ap->a_cred;
248 struct fuse_access_param facp;
249 struct fuse_data *data = fuse_get_mpdata(vnode_mount(vp));
253 if (fuse_isdeadfs(vp)) {
254 if (vnode_isvroot(vp)) {
259 if (!(data->dataflags & FSESS_INITED)) {
260 if (vnode_isvroot(vp)) {
261 if (priv_check_cred(cred, PRIV_VFS_ADMIN) ||
262 (fuse_match_cred(data->daemoncred, cred) == 0)) {
268 if (vnode_islnk(vp)) {
271 bzero(&facp, sizeof(facp));
273 err = fuse_internal_access(vp, accmode, &facp, ap->a_td, ap->a_cred);
278 struct vnop_close_args {
281 struct ucred *a_cred;
286 fuse_vnop_close(struct vop_close_args *ap)
288 struct vnode *vp = ap->a_vp;
289 struct ucred *cred = ap->a_cred;
290 int fflag = ap->a_fflag;
291 struct thread *td = ap->a_td;
292 pid_t pid = td->td_proc->p_pid;
294 if (fuse_isdeadfs(vp)) {
297 if (vnode_isdir(vp)) {
298 struct fuse_filehandle *fufh;
300 if (fuse_filehandle_get_dir(vp, &fufh, cred, pid) == 0)
301 fuse_filehandle_close(vp, fufh, NULL, cred);
304 if (fflag & IO_NDELAY) {
307 /* TODO: close the file handle, if we're sure it's no longer used */
308 if ((VTOFUD(vp)->flag & FN_SIZECHANGE) != 0) {
309 fuse_vnode_savesize(vp, cred, td->td_proc->p_pid);
315 fdisp_make_mknod_for_fallback(
316 struct fuse_dispatcher *fdip,
317 struct componentname *cnp,
323 enum fuse_opcode *op)
325 struct fuse_mknod_in *fmni;
327 fdisp_init(fdip, sizeof(*fmni) + cnp->cn_namelen + 1);
329 fdisp_make(fdip, *op, vnode_mount(dvp), parentnid, td, cred);
333 memcpy((char *)fdip->indata + sizeof(*fmni), cnp->cn_nameptr,
335 ((char *)fdip->indata)[sizeof(*fmni) + cnp->cn_namelen] = '\0';
338 struct vnop_create_args {
340 struct vnode **a_vpp;
341 struct componentname *a_cnp;
346 fuse_vnop_create(struct vop_create_args *ap)
348 struct vnode *dvp = ap->a_dvp;
349 struct vnode **vpp = ap->a_vpp;
350 struct componentname *cnp = ap->a_cnp;
351 struct vattr *vap = ap->a_vap;
352 struct thread *td = cnp->cn_thread;
353 struct ucred *cred = cnp->cn_cred;
355 struct fuse_open_in *foi;
356 struct fuse_entry_out *feo;
357 struct fuse_open_out *foo;
358 struct fuse_dispatcher fdi, fdi2;
359 struct fuse_dispatcher *fdip = &fdi;
360 struct fuse_dispatcher *fdip2 = NULL;
364 struct mount *mp = vnode_mount(dvp);
365 uint64_t parentnid = VTOFUD(dvp)->nid;
366 mode_t mode = MAKEIMODE(vap->va_type, vap->va_mode);
371 * VOP_CREATE doesn't tell us the open(2) flags, so we guess. Only a
372 * writable mode makes sense, and we might as well include readability
377 if (fuse_isdeadfs(dvp)) {
380 bzero(&fdi, sizeof(fdi));
382 if ((vap->va_type != VREG))
385 if (!fsess_isimpl(mp, FUSE_CREATE)) {
386 /* Fallback to FUSE_MKNOD/FUSE_OPEN */
387 fdisp_make_mknod_for_fallback(fdip, cnp, dvp, parentnid, td,
390 /* Use FUSE_CREATE */
392 fdisp_init(fdip, sizeof(*foi) + cnp->cn_namelen + 1);
393 fdisp_make(fdip, op, vnode_mount(dvp), parentnid, td, cred);
396 foi->flags = O_CREAT | flags;
397 memcpy((char *)fdip->indata + sizeof(*foi), cnp->cn_nameptr,
399 ((char *)fdip->indata)[sizeof(*foi) + cnp->cn_namelen] = '\0';
402 err = fdisp_wait_answ(fdip);
405 if (err == ENOSYS && op == FUSE_CREATE) {
406 fsess_set_notimpl(mp, FUSE_CREATE);
407 fdisp_make_mknod_for_fallback(fdip, cnp, dvp,
408 parentnid, td, cred, mode, &op);
409 err = fdisp_wait_answ(fdip);
417 if ((err = fuse_internal_checkentry(feo, VREG))) {
421 if (op == FUSE_CREATE) {
422 foo = (struct fuse_open_out*)(feo + 1);
424 /* Issue a separate FUSE_OPEN */
426 fdisp_init(fdip2, sizeof(*foi));
427 fdisp_make(fdip2, FUSE_OPEN, vnode_mount(dvp), feo->nodeid, td,
432 err = fdisp_wait_answ(fdip2);
437 err = fuse_vnode_get(mp, feo, feo->nodeid, dvp, vpp, cnp, VREG);
439 struct fuse_release_in *fri;
440 uint64_t nodeid = feo->nodeid;
441 uint64_t fh_id = foo->fh;
443 fdisp_init(fdip, sizeof(*fri));
444 fdisp_make(fdip, FUSE_RELEASE, mp, nodeid, td, cred);
448 fuse_insert_callback(fdip->tick, fuse_internal_forget_callback);
449 fuse_insert_message(fdip->tick);
452 ASSERT_VOP_ELOCKED(*vpp, "fuse_vnop_create");
454 fuse_filehandle_init(*vpp, FUFH_RDWR, NULL, td->td_proc->p_pid, cred,
456 fuse_vnode_open(*vpp, foo->open_flags, td);
457 cache_purge_negative(dvp);
461 fdisp_destroy(fdip2);
467 struct vnop_fdatasync_args {
468 struct vop_generic_args a_gen;
470 struct thread * a_td;
474 fuse_vnop_fdatasync(struct vop_fdatasync_args *ap)
476 struct vnode *vp = ap->a_vp;
477 struct thread *td = ap->a_td;
478 int waitfor = MNT_WAIT;
482 if (fuse_isdeadfs(vp)) {
485 if ((err = vop_stdfdatasync_buf(ap)))
488 return fuse_internal_fsync(vp, td, waitfor, true);
492 struct vnop_fsync_args {
493 struct vop_generic_args a_gen;
496 struct thread * a_td;
500 fuse_vnop_fsync(struct vop_fsync_args *ap)
502 struct vnode *vp = ap->a_vp;
503 struct thread *td = ap->a_td;
504 int waitfor = ap->a_waitfor;
507 if (fuse_isdeadfs(vp)) {
510 if ((err = vop_stdfsync(ap)))
513 return fuse_internal_fsync(vp, td, waitfor, false);
517 struct vnop_getattr_args {
520 struct ucred *a_cred;
525 fuse_vnop_getattr(struct vop_getattr_args *ap)
527 struct vnode *vp = ap->a_vp;
528 struct vattr *vap = ap->a_vap;
529 struct ucred *cred = ap->a_cred;
530 struct thread *td = curthread;
531 struct fuse_vnode_data *fvdat = VTOFUD(vp);
532 struct fuse_attr_out *fao;
536 struct fuse_dispatcher fdi;
538 dataflags = fuse_get_mpdata(vnode_mount(vp))->dataflags;
540 /* Note that we are not bailing out on a dead file system just yet. */
542 if (!(dataflags & FSESS_INITED)) {
543 if (!vnode_isvroot(vp)) {
544 fdata_set_dead(fuse_get_mpdata(vnode_mount(vp)));
552 if ((err = fdisp_simple_putget_vp(&fdi, FUSE_GETATTR, vp, td, cred))) {
553 if ((err == ENOTCONN) && vnode_isvroot(vp)) {
554 /* see comment in fuse_vfsop_statfs() */
559 fuse_internal_vnode_disappear(vp);
564 fao = (struct fuse_attr_out *)fdi.answ;
565 fuse_internal_cache_attrs(vp, &fao->attr, fao->attr_valid,
566 fao->attr_valid_nsec, vap);
567 if (vap->va_type != vnode_vtype(vp)) {
568 fuse_internal_vnode_disappear(vp);
572 if ((fvdat->flag & FN_SIZECHANGE) != 0)
573 vap->va_size = fvdat->filesize;
575 if (vnode_isreg(vp) && (fvdat->flag & FN_SIZECHANGE) == 0) {
577 * This is for those cases when the file size changed without us
578 * knowing, and we want to catch up.
580 off_t new_filesize = ((struct fuse_attr_out *)
581 fdi.answ)->attr.size;
583 if (fvdat->filesize != new_filesize) {
584 fuse_vnode_setsize(vp, cred, new_filesize);
585 fvdat->flag &= ~FN_SIZECHANGE;
594 bzero(vap, sizeof(*vap));
595 vap->va_type = vnode_vtype(vp);
601 struct vnop_inactive_args {
607 fuse_vnop_inactive(struct vop_inactive_args *ap)
609 struct vnode *vp = ap->a_vp;
610 struct thread *td = ap->a_td;
612 struct fuse_vnode_data *fvdat = VTOFUD(vp);
613 struct fuse_filehandle *fufh, *fufh_tmp;
617 LIST_FOREACH_SAFE(fufh, &fvdat->handles, next, fufh_tmp) {
618 if (need_flush && vp->v_type == VREG) {
619 if ((VTOFUD(vp)->flag & FN_SIZECHANGE) != 0) {
620 fuse_vnode_savesize(vp, NULL, 0);
622 if (fuse_data_cache_invalidate ||
623 (fvdat->flag & FN_REVOKED) != 0)
624 fuse_io_invalbuf(vp, td);
626 fuse_io_flushbuf(vp, MNT_WAIT, td);
629 fuse_filehandle_close(vp, fufh, td, NULL);
632 if ((fvdat->flag & FN_REVOKED) != 0 && fuse_reclaim_revoked) {
639 struct vnop_link_args {
640 struct vnode *a_tdvp;
642 struct componentname *a_cnp;
646 fuse_vnop_link(struct vop_link_args *ap)
648 struct vnode *vp = ap->a_vp;
649 struct vnode *tdvp = ap->a_tdvp;
650 struct componentname *cnp = ap->a_cnp;
652 struct vattr *vap = VTOVA(vp);
654 struct fuse_dispatcher fdi;
655 struct fuse_entry_out *feo;
656 struct fuse_link_in fli;
660 if (fuse_isdeadfs(vp)) {
663 if (vnode_mount(tdvp) != vnode_mount(vp)) {
668 * This is a seatbelt check to protect naive userspace filesystems from
669 * themselves and the limitations of the FUSE IPC protocol. If a
670 * filesystem does not allow attribute caching, assume it is capable of
671 * validating that nlink does not overflow.
673 if (vap != NULL && vap->va_nlink >= FUSE_LINK_MAX)
675 fli.oldnodeid = VTOI(vp);
678 fuse_internal_newentry_makerequest(vnode_mount(tdvp), VTOI(tdvp), cnp,
679 FUSE_LINK, &fli, sizeof(fli), &fdi);
680 if ((err = fdisp_wait_answ(&fdi))) {
685 err = fuse_internal_checkentry(feo, vnode_vtype(vp));
692 struct vnop_lookup_args {
693 struct vnodeop_desc *a_desc;
695 struct vnode **a_vpp;
696 struct componentname *a_cnp;
700 fuse_vnop_lookup(struct vop_lookup_args *ap)
702 struct vnode *dvp = ap->a_dvp;
703 struct vnode **vpp = ap->a_vpp;
704 struct componentname *cnp = ap->a_cnp;
705 struct thread *td = cnp->cn_thread;
706 struct ucred *cred = cnp->cn_cred;
708 int nameiop = cnp->cn_nameiop;
709 int flags = cnp->cn_flags;
710 int wantparent = flags & (LOCKPARENT | WANTPARENT);
711 int islastcn = flags & ISLASTCN;
712 struct mount *mp = vnode_mount(dvp);
716 struct vnode *vp = NULL;
718 struct fuse_dispatcher fdi;
722 struct fuse_access_param facp;
724 if (fuse_isdeadfs(dvp)) {
728 if (!vnode_isdir(dvp)) {
731 if (islastcn && vfs_isrdonly(mp) && (nameiop != LOOKUP)) {
735 * We do access check prior to doing anything else only in the case
736 * when we are at fs root (we'd like to say, "we are at the first
737 * component", but that's not exactly the same... nevermind).
738 * See further comments at further access checks.
741 bzero(&facp, sizeof(facp));
742 if (vnode_isvroot(dvp)) { /* early permission check hack */
743 if ((err = fuse_internal_access(dvp, VEXEC, &facp, td, cred))) {
747 if (flags & ISDOTDOT) {
748 nid = VTOFUD(dvp)->parent_nid;
755 } else if (cnp->cn_namelen == 1 && *(cnp->cn_nameptr) == '.') {
760 } else if (fuse_lookup_cache_enable) {
761 err = cache_lookup(dvp, vpp, cnp, NULL, NULL);
764 case -1: /* positive match */
765 atomic_add_acq_long(&fuse_lookup_cache_hits, 1);
768 case 0: /* no match in cache */
769 atomic_add_acq_long(&fuse_lookup_cache_misses, 1);
772 case ENOENT: /* negative match */
779 fdisp_init(&fdi, cnp->cn_namelen + 1);
783 fdisp_make(&fdi, op, mp, nid, td, cred);
785 if (op == FUSE_LOOKUP) {
786 memcpy(fdi.indata, cnp->cn_nameptr, cnp->cn_namelen);
787 ((char *)fdi.indata)[cnp->cn_namelen] = '\0';
789 lookup_err = fdisp_wait_answ(&fdi);
791 if ((op == FUSE_LOOKUP) && !lookup_err) { /* lookup call succeeded */
792 nid = ((struct fuse_entry_out *)fdi.answ)->nodeid;
795 * zero nodeid is the same as "not found",
796 * but it's also cacheable (which we keep
797 * keep on doing not as of writing this)
799 fdi.answ_stat = ENOENT;
801 } else if (nid == FUSE_ROOT_ID) {
806 (!fdi.answ_stat || lookup_err != ENOENT || op != FUSE_LOOKUP)) {
810 /* lookup_err, if non-zero, must be ENOENT at this point */
814 if ((nameiop == CREATE || nameiop == RENAME) && islastcn
815 /* && directory dvp has not been removed */ ) {
817 if (vfs_isrdonly(mp)) {
821 #if 0 /* THINK_ABOUT_THIS */
822 if ((err = fuse_internal_access(dvp, VWRITE, cred, td, &facp))) {
828 * Possibly record the position of a slot in the
829 * directory large enough for the new component name.
830 * This can be recorded in the vnode private data for
831 * dvp. Set the SAVENAME flag to hold onto the
832 * pathname for use later in VOP_CREATE or VOP_RENAME.
834 cnp->cn_flags |= SAVENAME;
839 /* Consider inserting name into cache. */
842 * No we can't use negative caching, as the fs
843 * changes are out of our control.
844 * False positives' falseness turns out just as things
845 * go by, but false negatives' falseness doesn't.
846 * (and aiding the caching mechanism with extra control
847 * mechanisms comes quite close to beating the whole purpose
851 if ((cnp->cn_flags & MAKEENTRY) != 0) {
852 SDT_PROBE2(fuse, , vnops, trace, 1,
853 "inserting NULL into cache");
854 cache_enter(dvp, NULL, cnp);
864 struct fuse_entry_out *feo = NULL;
865 struct fuse_attr *fattr = NULL;
867 if (op == FUSE_GETATTR) {
868 fattr = &((struct fuse_attr_out *)fdi.answ)->attr;
870 feo = (struct fuse_entry_out *)fdi.answ;
871 fattr = &(feo->attr);
875 * If deleting, and at end of pathname, return parameters
876 * which can be used to remove file. If the wantparent flag
877 * isn't set, we return only the directory, otherwise we go on
878 * and lock the inode, being careful with ".".
880 if (nameiop == DELETE && islastcn) {
882 * Check for write access on directory.
884 facp.xuid = fattr->uid;
885 facp.facc_flags |= FACCESS_STICKY;
886 err = fuse_internal_access(dvp, VWRITE, &facp, td, cred);
887 facp.facc_flags &= ~FACCESS_XQUERIES;
892 if (nid == VTOI(dvp)) {
896 err = fuse_vnode_get(dvp->v_mount, feo, nid,
897 dvp, &vp, cnp, IFTOVT(fattr->mode));
904 * Save the name for use in VOP_RMDIR and VOP_REMOVE
907 cnp->cn_flags |= SAVENAME;
912 * If rewriting (RENAME), return the inode and the
913 * information required to rewrite the present directory
914 * Must get inode of directory entry to verify it's a
915 * regular file, or empty directory.
917 if (nameiop == RENAME && wantparent && islastcn) {
919 #if 0 /* THINK_ABOUT_THIS */
920 if ((err = fuse_internal_access(dvp, VWRITE, cred, td, &facp))) {
928 if (nid == VTOI(dvp)) {
932 err = fuse_vnode_get(vnode_mount(dvp), feo, nid, dvp,
933 &vp, cnp, IFTOVT(fattr->mode));
939 * Save the name for use in VOP_RENAME later.
941 cnp->cn_flags |= SAVENAME;
945 if (flags & ISDOTDOT) {
950 * Expanded copy of vn_vget_ino() so that
951 * fuse_vnode_get() can be used.
954 ltype = VOP_ISLOCKED(dvp);
955 err = vfs_busy(mp, MBF_NOWAIT);
959 err = vfs_busy(mp, 0);
960 vn_lock(dvp, ltype | LK_RETRY);
964 if ((dvp->v_iflag & VI_DOOMED) != 0) {
971 err = fuse_vnode_get(vnode_mount(dvp), feo, nid, NULL,
972 &vp, cnp, IFTOVT(fattr->mode));
974 vn_lock(dvp, ltype | LK_RETRY);
975 if ((dvp->v_iflag & VI_DOOMED) != 0) {
983 } else if (nid == VTOI(dvp)) {
987 struct fuse_vnode_data *fvdat;
989 err = fuse_vnode_get(vnode_mount(dvp), feo, nid, dvp,
990 &vp, cnp, IFTOVT(fattr->mode));
994 fuse_vnode_setparent(vp, dvp);
997 * In the case where we are looking up a FUSE node
998 * represented by an existing cached vnode, and the
999 * true size reported by FUSE_LOOKUP doesn't match
1000 * the vnode's cached size, fix the vnode cache to
1001 * match the real object size.
1003 * This can occur via FUSE distributed filesystems,
1004 * irregular files, etc.
1007 if (vnode_isreg(vp) &&
1008 fattr->size != fvdat->filesize) {
1010 * The FN_SIZECHANGE flag reflects a dirty
1011 * append. If userspace lets us know our cache
1012 * is invalid, that write was lost. (Dirty
1013 * writes that do not cause append are also
1014 * lost, but we don't detect them here.)
1016 * XXX: Maybe disable WB caching on this mount.
1018 if (fvdat->flag & FN_SIZECHANGE)
1019 printf("%s: WB cache incoherent on "
1021 vnode_mount(vp)->mnt_stat.f_mntonname);
1023 (void)fuse_vnode_setsize(vp, cred, fattr->size);
1024 fvdat->flag &= ~FN_SIZECHANGE;
1029 if (op == FUSE_GETATTR) {
1030 struct fuse_attr_out *fao =
1031 (struct fuse_attr_out*)fdi.answ;
1032 fuse_internal_cache_attrs(*vpp,
1033 &fao->attr, fao->attr_valid,
1034 fao->attr_valid_nsec, NULL);
1036 struct fuse_entry_out *feo =
1037 (struct fuse_entry_out*)fdi.answ;
1038 fuse_internal_cache_attrs(*vpp,
1039 &feo->attr, feo->attr_valid,
1040 feo->attr_valid_nsec, NULL);
1043 /* Insert name into cache if appropriate. */
1046 * Nooo, caching is evil. With caching, we can't avoid stale
1047 * information taking over the playground (cached info is not
1048 * just positive/negative, it does have qualitative aspects,
1049 * too). And a (VOP/FUSE)_GETATTR is always thrown anyway, when
1050 * walking down along cached path components, and that's not
1051 * any cheaper than FUSE_LOOKUP. This might change with
1052 * implementing kernel side attr caching, but... In Linux,
1053 * lookup results are not cached, and the daemon is bombarded
1054 * with FUSE_LOOKUPS on and on. This shows that by design, the
1055 * daemon is expected to handle frequent lookup queries
1056 * efficiently, do its caching in userspace, and so on.
1058 * So just leave the name cache alone.
1062 * Well, now I know, Linux caches lookups, but with a
1063 * timeout... So it's the same thing as attribute caching:
1064 * we can deal with it when implement timeouts.
1067 if (cnp->cn_flags & MAKEENTRY) {
1068 cache_enter(dvp, *vpp, cnp);
1075 /* No lookup error; need to clean up. */
1077 if (err) { /* Found inode; exit with no vnode. */
1078 if (op == FUSE_LOOKUP) {
1079 fuse_internal_forget_send(vnode_mount(dvp), td, cred,
1082 fdisp_destroy(&fdi);
1085 #ifndef NO_EARLY_PERM_CHECK_HACK
1088 * We have the attributes of the next item
1089 * *now*, and it's a fact, and we do not
1090 * have to do extra work for it (ie, beg the
1091 * daemon), and it neither depends on such
1092 * accidental things like attr caching. So
1093 * the big idea: check credentials *now*,
1094 * not at the beginning of the next call to
1097 * The first item of the lookup chain (fs root)
1098 * won't be checked then here, of course, as
1099 * its never "the next". But go and see that
1100 * the root is taken care about at the very
1101 * beginning of this function.
1103 * Now, given we want to do the access check
1104 * this way, one might ask: so then why not
1105 * do the access check just after fetching
1106 * the inode and its attributes from the
1107 * daemon? Why bother with producing the
1108 * corresponding vnode at all if something
1109 * is not OK? We know what's the deal as
1110 * soon as we get those attrs... There is
1111 * one bit of info though not given us by
1112 * the daemon: whether his response is
1113 * authoritative or not... His response should
1114 * be ignored if something is mounted over
1115 * the dir in question. But that can be
1116 * known only by having the vnode...
1118 int tmpvtype = vnode_vtype(*vpp);
1120 bzero(&facp, sizeof(facp));
1121 /*the early perm check hack */
1122 facp.facc_flags |= FACCESS_VA_VALID;
1124 if ((tmpvtype != VDIR) && (tmpvtype != VLNK)) {
1127 if (!err && !vnode_mountedhere(*vpp)) {
1128 err = fuse_internal_access(*vpp, VEXEC, &facp, td, cred);
1131 if (tmpvtype == VLNK)
1132 SDT_PROBE2(fuse, , vnops, trace,
1133 1, "weird, permission "
1134 "error with a symlink?");
1142 fdisp_destroy(&fdi);
1148 struct vnop_mkdir_args {
1149 struct vnode *a_dvp;
1150 struct vnode **a_vpp;
1151 struct componentname *a_cnp;
1152 struct vattr *a_vap;
1156 fuse_vnop_mkdir(struct vop_mkdir_args *ap)
1158 struct vnode *dvp = ap->a_dvp;
1159 struct vnode **vpp = ap->a_vpp;
1160 struct componentname *cnp = ap->a_cnp;
1161 struct vattr *vap = ap->a_vap;
1163 struct fuse_mkdir_in fmdi;
1165 if (fuse_isdeadfs(dvp)) {
1168 fmdi.mode = MAKEIMODE(vap->va_type, vap->va_mode);
1170 return (fuse_internal_newentry(dvp, vpp, cnp, FUSE_MKDIR, &fmdi,
1171 sizeof(fmdi), VDIR));
1175 struct vnop_mknod_args {
1176 struct vnode *a_dvp;
1177 struct vnode **a_vpp;
1178 struct componentname *a_cnp;
1179 struct vattr *a_vap;
1183 fuse_vnop_mknod(struct vop_mknod_args *ap)
1186 struct vnode *dvp = ap->a_dvp;
1187 struct vnode **vpp = ap->a_vpp;
1188 struct componentname *cnp = ap->a_cnp;
1189 struct vattr *vap = ap->a_vap;
1190 struct fuse_mknod_in fmni;
1192 if (fuse_isdeadfs(dvp))
1195 fmni.mode = MAKEIMODE(vap->va_type, vap->va_mode);
1196 fmni.rdev = vap->va_rdev;
1197 return (fuse_internal_newentry(dvp, vpp, cnp, FUSE_MKNOD, &fmni,
1198 sizeof(fmni), vap->va_type));
1202 struct vnop_open_args {
1205 struct ucred *a_cred;
1206 struct thread *a_td;
1207 int a_fdidx; / struct file *a_fp;
1211 fuse_vnop_open(struct vop_open_args *ap)
1213 struct vnode *vp = ap->a_vp;
1214 int a_mode = ap->a_mode;
1215 struct thread *td = ap->a_td;
1216 struct ucred *cred = ap->a_cred;
1217 pid_t pid = td->td_proc->p_pid;
1218 struct fuse_vnode_data *fvdat;
1220 if (fuse_isdeadfs(vp))
1222 if (vp->v_type == VCHR || vp->v_type == VBLK || vp->v_type == VFIFO)
1223 return (EOPNOTSUPP);
1224 if ((a_mode & (FREAD | FWRITE | FEXEC)) == 0)
1229 if (fuse_filehandle_validrw(vp, a_mode, cred, pid)) {
1230 fuse_vnode_open(vp, 0, td);
1234 return fuse_filehandle_open(vp, a_mode, NULL, td, cred);
1238 fuse_vnop_pathconf(struct vop_pathconf_args *ap)
1241 switch (ap->a_name) {
1242 case _PC_FILESIZEBITS:
1246 *ap->a_retval = NAME_MAX;
1249 *ap->a_retval = MIN(LONG_MAX, FUSE_LINK_MAX);
1251 case _PC_SYMLINK_MAX:
1252 *ap->a_retval = MAXPATHLEN;
1258 return (vop_stdpathconf(ap));
1263 struct vnop_read_args {
1267 struct ucred *a_cred;
1271 fuse_vnop_read(struct vop_read_args *ap)
1273 struct vnode *vp = ap->a_vp;
1274 struct uio *uio = ap->a_uio;
1275 int ioflag = ap->a_ioflag;
1276 struct ucred *cred = ap->a_cred;
1277 pid_t pid = curthread->td_proc->p_pid;
1279 if (fuse_isdeadfs(vp)) {
1283 if (VTOFUD(vp)->flag & FN_DIRECTIO) {
1284 ioflag |= IO_DIRECT;
1287 return fuse_io_dispatch(vp, uio, ioflag, cred, pid);
1291 struct vnop_readdir_args {
1294 struct ucred *a_cred;
1301 fuse_vnop_readdir(struct vop_readdir_args *ap)
1303 struct vnode *vp = ap->a_vp;
1304 struct uio *uio = ap->a_uio;
1305 struct ucred *cred = ap->a_cred;
1306 struct fuse_filehandle *fufh = NULL;
1307 struct fuse_iov cookediov;
1310 pid_t pid = curthread->td_proc->p_pid;
1312 if (fuse_isdeadfs(vp)) {
1315 if ( /* XXXIP ((uio_iovcnt(uio) > 1)) || */
1316 (uio_resid(uio) < sizeof(struct dirent))) {
1320 if ((err = fuse_filehandle_get_dir(vp, &fufh, cred, pid)) != 0) {
1321 SDT_PROBE2(fuse, , vnops, trace, 1,
1322 "calling readdir() before open()");
1324 * This was seen to happen in getdirentries as used by
1325 * shells/fish, but I can't reproduce it.
1327 err = fuse_filehandle_open(vp, FREAD, &fufh, NULL, cred);
1333 #define DIRCOOKEDSIZE FUSE_DIRENT_ALIGN(FUSE_NAME_OFFSET + MAXNAMLEN + 1)
1334 fiov_init(&cookediov, DIRCOOKEDSIZE);
1336 err = fuse_internal_readdir(vp, uio, fufh, &cookediov);
1338 fiov_teardown(&cookediov);
1340 fuse_filehandle_close(vp, fufh, NULL, cred);
1346 struct vnop_readlink_args {
1349 struct ucred *a_cred;
1353 fuse_vnop_readlink(struct vop_readlink_args *ap)
1355 struct vnode *vp = ap->a_vp;
1356 struct uio *uio = ap->a_uio;
1357 struct ucred *cred = ap->a_cred;
1359 struct fuse_dispatcher fdi;
1362 if (fuse_isdeadfs(vp)) {
1365 if (!vnode_islnk(vp)) {
1368 fdisp_init(&fdi, 0);
1369 err = fdisp_simple_putget_vp(&fdi, FUSE_READLINK, vp, curthread, cred);
1373 if (((char *)fdi.answ)[0] == '/' &&
1374 fuse_get_mpdata(vnode_mount(vp))->dataflags & FSESS_PUSH_SYMLINKS_IN) {
1375 char *mpth = vnode_mount(vp)->mnt_stat.f_mntonname;
1377 err = uiomove(mpth, strlen(mpth), uio);
1380 err = uiomove(fdi.answ, fdi.iosize, uio);
1383 fdisp_destroy(&fdi);
1388 struct vnop_reclaim_args {
1390 struct thread *a_td;
1394 fuse_vnop_reclaim(struct vop_reclaim_args *ap)
1396 struct vnode *vp = ap->a_vp;
1397 struct thread *td = ap->a_td;
1398 struct fuse_vnode_data *fvdat = VTOFUD(vp);
1399 struct fuse_filehandle *fufh, *fufh_tmp;
1402 panic("FUSE: no vnode data during recycling");
1404 LIST_FOREACH_SAFE(fufh, &fvdat->handles, next, fufh_tmp) {
1405 printf("FUSE: vnode being reclaimed with open fufh "
1406 "(type=%#x)", fufh->fufh_type);
1407 fuse_filehandle_close(vp, fufh, td, NULL);
1410 if ((!fuse_isdeadfs(vp)) && (fvdat->nlookup)) {
1411 fuse_internal_forget_send(vnode_mount(vp), td, NULL, VTOI(vp),
1414 fuse_vnode_setparent(vp, NULL);
1416 vfs_hash_remove(vp);
1417 vnode_destroy_vobject(vp);
1418 fuse_vnode_destroy(vp);
1424 struct vnop_remove_args {
1425 struct vnode *a_dvp;
1427 struct componentname *a_cnp;
1431 fuse_vnop_remove(struct vop_remove_args *ap)
1433 struct vnode *dvp = ap->a_dvp;
1434 struct vnode *vp = ap->a_vp;
1435 struct componentname *cnp = ap->a_cnp;
1439 if (fuse_isdeadfs(vp)) {
1442 if (vnode_isdir(vp)) {
1447 err = fuse_internal_remove(dvp, vp, cnp, FUSE_UNLINK);
1450 fuse_internal_vnode_disappear(vp);
1455 struct vnop_rename_args {
1456 struct vnode *a_fdvp;
1457 struct vnode *a_fvp;
1458 struct componentname *a_fcnp;
1459 struct vnode *a_tdvp;
1460 struct vnode *a_tvp;
1461 struct componentname *a_tcnp;
1465 fuse_vnop_rename(struct vop_rename_args *ap)
1467 struct vnode *fdvp = ap->a_fdvp;
1468 struct vnode *fvp = ap->a_fvp;
1469 struct componentname *fcnp = ap->a_fcnp;
1470 struct vnode *tdvp = ap->a_tdvp;
1471 struct vnode *tvp = ap->a_tvp;
1472 struct componentname *tcnp = ap->a_tcnp;
1473 struct fuse_data *data;
1477 if (fuse_isdeadfs(fdvp)) {
1480 if (fvp->v_mount != tdvp->v_mount ||
1481 (tvp && fvp->v_mount != tvp->v_mount)) {
1482 SDT_PROBE2(fuse, , vnops, trace, 1, "cross-device rename");
1489 * FUSE library is expected to check if target directory is not
1490 * under the source directory in the file system tree.
1491 * Linux performs this check at VFS level.
1493 data = fuse_get_mpdata(vnode_mount(tdvp));
1494 sx_xlock(&data->rename_lock);
1495 err = fuse_internal_rename(fdvp, fcnp, tdvp, tcnp);
1498 fuse_vnode_setparent(fvp, tdvp);
1500 fuse_vnode_setparent(tvp, NULL);
1502 sx_unlock(&data->rename_lock);
1504 if (tvp != NULL && tvp != fvp) {
1507 if (vnode_isdir(fvp)) {
1508 if ((tvp != NULL) && vnode_isdir(tvp)) {
1529 struct vnop_rmdir_args {
1530 struct vnode *a_dvp;
1532 struct componentname *a_cnp;
1536 fuse_vnop_rmdir(struct vop_rmdir_args *ap)
1538 struct vnode *dvp = ap->a_dvp;
1539 struct vnode *vp = ap->a_vp;
1543 if (fuse_isdeadfs(vp)) {
1546 if (VTOFUD(vp) == VTOFUD(dvp)) {
1549 err = fuse_internal_remove(dvp, vp, ap->a_cnp, FUSE_RMDIR);
1552 fuse_internal_vnode_disappear(vp);
1557 struct vnop_setattr_args {
1559 struct vattr *a_vap;
1560 struct ucred *a_cred;
1561 struct thread *a_td;
1565 fuse_vnop_setattr(struct vop_setattr_args *ap)
1567 struct vnode *vp = ap->a_vp;
1568 struct vattr *vap = ap->a_vap;
1569 struct ucred *cred = ap->a_cred;
1570 struct thread *td = curthread;
1571 struct fuse_dispatcher fdi;
1572 struct fuse_setattr_in *fsai;
1573 struct fuse_access_param facp;
1574 pid_t pid = td->td_proc->p_pid;
1578 int sizechanged = 0;
1579 uint64_t newsize = 0;
1581 if (fuse_isdeadfs(vp)) {
1584 fdisp_init(&fdi, sizeof(*fsai));
1585 fdisp_make_vp(&fdi, FUSE_SETATTR, vp, td, cred);
1589 bzero(&facp, sizeof(facp));
1591 facp.xuid = vap->va_uid;
1592 facp.xgid = vap->va_gid;
1594 if (vap->va_uid != (uid_t)VNOVAL) {
1595 facp.facc_flags |= FACCESS_CHOWN;
1596 fsai->uid = vap->va_uid;
1597 fsai->valid |= FATTR_UID;
1599 if (vap->va_gid != (gid_t)VNOVAL) {
1600 facp.facc_flags |= FACCESS_CHOWN;
1601 fsai->gid = vap->va_gid;
1602 fsai->valid |= FATTR_GID;
1604 if (vap->va_size != VNOVAL) {
1606 struct fuse_filehandle *fufh = NULL;
1608 /*Truncate to a new value. */
1609 fsai->size = vap->va_size;
1611 newsize = vap->va_size;
1612 fsai->valid |= FATTR_SIZE;
1614 fuse_filehandle_getrw(vp, FUFH_WRONLY, &fufh, cred, pid);
1616 fsai->fh = fufh->fh_id;
1617 fsai->valid |= FATTR_FH;
1620 if (vap->va_atime.tv_sec != VNOVAL) {
1621 fsai->atime = vap->va_atime.tv_sec;
1622 fsai->atimensec = vap->va_atime.tv_nsec;
1623 fsai->valid |= FATTR_ATIME;
1625 if (vap->va_mtime.tv_sec != VNOVAL) {
1626 fsai->mtime = vap->va_mtime.tv_sec;
1627 fsai->mtimensec = vap->va_mtime.tv_nsec;
1628 fsai->valid |= FATTR_MTIME;
1630 if (vap->va_mode != (mode_t)VNOVAL) {
1631 fsai->mode = vap->va_mode & ALLPERMS;
1632 fsai->valid |= FATTR_MODE;
1637 vtyp = vnode_vtype(vp);
1639 if (fsai->valid & FATTR_SIZE && vtyp == VDIR) {
1643 if (vfs_isrdonly(vnode_mount(vp)) && (fsai->valid & ~FATTR_SIZE || vtyp == VREG)) {
1648 if ((err = fdisp_wait_answ(&fdi)))
1650 vtyp = IFTOVT(((struct fuse_attr_out *)fdi.answ)->attr.mode);
1652 if (vnode_vtype(vp) != vtyp) {
1653 if (vnode_vtype(vp) == VNON && vtyp != VNON) {
1654 SDT_PROBE2(fuse, , vnops, trace, 1, "FUSE: Dang! "
1655 "vnode_vtype is VNON and vtype isn't.");
1658 * STALE vnode, ditch
1660 * The vnode has changed its type "behind our back".
1661 * There's nothing really we can do, so let us just
1662 * force an internal revocation and tell the caller to
1663 * try again, if interested.
1665 fuse_internal_vnode_disappear(vp);
1670 struct fuse_attr_out *fao = (struct fuse_attr_out*)fdi.answ;
1671 fuse_internal_cache_attrs(vp, &fao->attr, fao->attr_valid,
1672 fao->attr_valid_nsec, NULL);
1676 fdisp_destroy(&fdi);
1677 if (!err && sizechanged) {
1678 fuse_vnode_setsize(vp, cred, newsize);
1679 VTOFUD(vp)->flag &= ~FN_SIZECHANGE;
1685 struct vnop_strategy_args {
1691 fuse_vnop_strategy(struct vop_strategy_args *ap)
1693 struct vnode *vp = ap->a_vp;
1694 struct buf *bp = ap->a_bp;
1696 if (!vp || fuse_isdeadfs(vp)) {
1697 bp->b_ioflags |= BIO_ERROR;
1698 bp->b_error = ENXIO;
1702 if (bp->b_iocmd == BIO_WRITE) {
1705 err = fuse_vnode_refreshsize(vp, NOCRED);
1707 bp->b_ioflags |= BIO_ERROR;
1715 * VOP_STRATEGY always returns zero and signals error via bp->b_ioflags.
1716 * fuse_io_strategy sets bp's error fields
1718 (void)fuse_io_strategy(vp, bp);
1725 struct vnop_symlink_args {
1726 struct vnode *a_dvp;
1727 struct vnode **a_vpp;
1728 struct componentname *a_cnp;
1729 struct vattr *a_vap;
1734 fuse_vnop_symlink(struct vop_symlink_args *ap)
1736 struct vnode *dvp = ap->a_dvp;
1737 struct vnode **vpp = ap->a_vpp;
1738 struct componentname *cnp = ap->a_cnp;
1739 const char *target = ap->a_target;
1741 struct fuse_dispatcher fdi;
1746 if (fuse_isdeadfs(dvp)) {
1750 * Unlike the other creator type calls, here we have to create a message
1751 * where the name of the new entry comes first, and the data describing
1752 * the entry comes second.
1753 * Hence we can't rely on our handy fuse_internal_newentry() routine,
1754 * but put together the message manually and just call the core part.
1757 len = strlen(target) + 1;
1758 fdisp_init(&fdi, len + cnp->cn_namelen + 1);
1759 fdisp_make_vp(&fdi, FUSE_SYMLINK, dvp, curthread, NULL);
1761 memcpy(fdi.indata, cnp->cn_nameptr, cnp->cn_namelen);
1762 ((char *)fdi.indata)[cnp->cn_namelen] = '\0';
1763 memcpy((char *)fdi.indata + cnp->cn_namelen + 1, target, len);
1765 err = fuse_internal_newentry_core(dvp, vpp, cnp, VLNK, &fdi);
1766 fdisp_destroy(&fdi);
1771 struct vnop_write_args {
1775 struct ucred *a_cred;
1779 fuse_vnop_write(struct vop_write_args *ap)
1781 struct vnode *vp = ap->a_vp;
1782 struct uio *uio = ap->a_uio;
1783 int ioflag = ap->a_ioflag;
1784 struct ucred *cred = ap->a_cred;
1785 pid_t pid = curthread->td_proc->p_pid;
1788 if (fuse_isdeadfs(vp)) {
1791 err = fuse_vnode_refreshsize(vp, cred);
1795 if (VTOFUD(vp)->flag & FN_DIRECTIO) {
1796 ioflag |= IO_DIRECT;
1799 return fuse_io_dispatch(vp, uio, ioflag, cred, pid);
1802 SDT_PROBE_DEFINE1(fuse, , vnops, vnop_getpages_error, "int");
1804 struct vnop_getpages_args {
1812 fuse_vnop_getpages(struct vop_getpages_args *ap)
1814 int i, error, nextoff, size, toff, count, npages;
1823 pid_t pid = curthread->td_proc->p_pid;
1826 KASSERT(vp->v_object, ("objectless vp passed to getpages"));
1827 td = curthread; /* XXX */
1828 cred = curthread->td_ucred; /* XXX */
1830 npages = ap->a_count;
1832 if (!fsess_opt_mmap(vnode_mount(vp))) {
1833 SDT_PROBE2(fuse, , vnops, trace, 1,
1834 "called on non-cacheable vnode??\n");
1835 return (VM_PAGER_ERROR);
1839 * If the last page is partially valid, just return it and allow
1840 * the pager to zero-out the blanks. Partially valid pages can
1841 * only occur at the file EOF.
1843 * XXXGL: is that true for FUSE, which is a local filesystem,
1844 * but still somewhat disconnected from the kernel?
1846 VM_OBJECT_WLOCK(vp->v_object);
1847 if (pages[npages - 1]->valid != 0 && --npages == 0)
1849 VM_OBJECT_WUNLOCK(vp->v_object);
1852 * We use only the kva address for the buffer, but this is extremely
1853 * convenient and fast.
1855 bp = uma_zalloc(fuse_pbuf_zone, M_WAITOK);
1857 kva = (vm_offset_t)bp->b_data;
1858 pmap_qenter(kva, pages, npages);
1859 VM_CNT_INC(v_vnodein);
1860 VM_CNT_ADD(v_vnodepgsin, npages);
1862 count = npages << PAGE_SHIFT;
1863 iov.iov_base = (caddr_t)kva;
1864 iov.iov_len = count;
1867 uio.uio_offset = IDX_TO_OFF(pages[0]->pindex);
1868 uio.uio_resid = count;
1869 uio.uio_segflg = UIO_SYSSPACE;
1870 uio.uio_rw = UIO_READ;
1873 error = fuse_io_dispatch(vp, &uio, IO_DIRECT, cred, pid);
1874 pmap_qremove(kva, npages);
1876 uma_zfree(fuse_pbuf_zone, bp);
1878 if (error && (uio.uio_resid == count)) {
1879 SDT_PROBE1(fuse, , vnops, vnop_getpages_error, error);
1880 return VM_PAGER_ERROR;
1883 * Calculate the number of bytes read and validate only that number
1884 * of bytes. Note that due to pending writes, size may be 0. This
1885 * does not mean that the remaining data is invalid!
1888 size = count - uio.uio_resid;
1889 VM_OBJECT_WLOCK(vp->v_object);
1890 fuse_vm_page_lock_queues();
1891 for (i = 0, toff = 0; i < npages; i++, toff = nextoff) {
1894 nextoff = toff + PAGE_SIZE;
1897 if (nextoff <= size) {
1899 * Read operation filled an entire page
1901 m->valid = VM_PAGE_BITS_ALL;
1902 KASSERT(m->dirty == 0,
1903 ("fuse_getpages: page %p is dirty", m));
1904 } else if (size > toff) {
1906 * Read operation filled a partial page.
1909 vm_page_set_valid_range(m, 0, size - toff);
1910 KASSERT(m->dirty == 0,
1911 ("fuse_getpages: page %p is dirty", m));
1914 * Read operation was short. If no error occurred
1915 * we may have hit a zero-fill section. We simply
1916 * leave valid set to 0.
1921 fuse_vm_page_unlock_queues();
1923 VM_OBJECT_WUNLOCK(vp->v_object);
1928 return (VM_PAGER_OK);
1932 struct vnop_putpages_args {
1938 vm_ooffset_t a_offset;
1942 fuse_vnop_putpages(struct vop_putpages_args *ap)
1948 int i, error, npages, count;
1956 pid_t pid = curthread->td_proc->p_pid;
1959 KASSERT(vp->v_object, ("objectless vp passed to putpages"));
1960 fsize = vp->v_object->un_pager.vnp.vnp_size;
1961 td = curthread; /* XXX */
1962 cred = curthread->td_ucred; /* XXX */
1964 count = ap->a_count;
1965 rtvals = ap->a_rtvals;
1966 npages = btoc(count);
1967 offset = IDX_TO_OFF(pages[0]->pindex);
1969 if (!fsess_opt_mmap(vnode_mount(vp))) {
1970 SDT_PROBE2(fuse, , vnops, trace, 1,
1971 "called on non-cacheable vnode??\n");
1973 for (i = 0; i < npages; i++)
1974 rtvals[i] = VM_PAGER_AGAIN;
1977 * When putting pages, do not extend file past EOF.
1980 if (offset + count > fsize) {
1981 count = fsize - offset;
1986 * We use only the kva address for the buffer, but this is extremely
1987 * convenient and fast.
1989 bp = uma_zalloc(fuse_pbuf_zone, M_WAITOK);
1991 kva = (vm_offset_t)bp->b_data;
1992 pmap_qenter(kva, pages, npages);
1993 VM_CNT_INC(v_vnodeout);
1994 VM_CNT_ADD(v_vnodepgsout, count);
1996 iov.iov_base = (caddr_t)kva;
1997 iov.iov_len = count;
2000 uio.uio_offset = offset;
2001 uio.uio_resid = count;
2002 uio.uio_segflg = UIO_SYSSPACE;
2003 uio.uio_rw = UIO_WRITE;
2006 error = fuse_io_dispatch(vp, &uio, IO_DIRECT, cred, pid);
2008 pmap_qremove(kva, npages);
2009 uma_zfree(fuse_pbuf_zone, bp);
2012 int nwritten = round_page(count - uio.uio_resid) / PAGE_SIZE;
2014 for (i = 0; i < nwritten; i++) {
2015 rtvals[i] = VM_PAGER_OK;
2016 VM_OBJECT_WLOCK(pages[i]->object);
2017 vm_page_undirty(pages[i]);
2018 VM_OBJECT_WUNLOCK(pages[i]->object);
2024 static const char extattr_namespace_separator = '.';
2027 struct vop_getextattr_args {
2028 struct vop_generic_args a_gen;
2030 int a_attrnamespace;
2034 struct ucred *a_cred;
2035 struct thread *a_td;
2039 fuse_vnop_getextattr(struct vop_getextattr_args *ap)
2041 struct vnode *vp = ap->a_vp;
2042 struct uio *uio = ap->a_uio;
2043 struct fuse_dispatcher fdi;
2044 struct fuse_getxattr_in *get_xattr_in;
2045 struct fuse_getxattr_out *get_xattr_out;
2046 struct mount *mp = vnode_mount(vp);
2047 struct thread *td = ap->a_td;
2048 struct ucred *cred = ap->a_cred;
2054 if (fuse_isdeadfs(vp))
2057 /* Default to looking for user attributes. */
2058 if (ap->a_attrnamespace == EXTATTR_NAMESPACE_SYSTEM)
2059 prefix = EXTATTR_NAMESPACE_SYSTEM_STRING;
2061 prefix = EXTATTR_NAMESPACE_USER_STRING;
2063 len = strlen(prefix) + sizeof(extattr_namespace_separator) +
2064 strlen(ap->a_name) + 1;
2066 fdisp_init(&fdi, len + sizeof(*get_xattr_in));
2067 fdisp_make_vp(&fdi, FUSE_GETXATTR, vp, td, cred);
2069 get_xattr_in = fdi.indata;
2071 * Check to see whether we're querying the available size or
2072 * issuing the actual request. If we pass in 0, we get back struct
2073 * fuse_getxattr_out. If we pass in a non-zero size, we get back
2074 * that much data, without the struct fuse_getxattr_out header.
2077 get_xattr_in->size = 0;
2079 get_xattr_in->size = uio->uio_resid;
2081 attr_str = (char *)fdi.indata + sizeof(*get_xattr_in);
2082 snprintf(attr_str, len, "%s%c%s", prefix, extattr_namespace_separator,
2085 err = fdisp_wait_answ(&fdi);
2088 fsess_set_notimpl(mp, FUSE_GETXATTR);
2092 get_xattr_out = fdi.answ;
2094 if (ap->a_size != NULL)
2095 *ap->a_size = get_xattr_out->size;
2098 err = uiomove(fdi.answ, fdi.iosize, uio);
2101 fdisp_destroy(&fdi);
2106 struct vop_setextattr_args {
2107 struct vop_generic_args a_gen;
2109 int a_attrnamespace;
2112 struct ucred *a_cred;
2113 struct thread *a_td;
2117 fuse_vnop_setextattr(struct vop_setextattr_args *ap)
2119 struct vnode *vp = ap->a_vp;
2120 struct uio *uio = ap->a_uio;
2121 struct fuse_dispatcher fdi;
2122 struct fuse_setxattr_in *set_xattr_in;
2123 struct mount *mp = vnode_mount(vp);
2124 struct thread *td = ap->a_td;
2125 struct ucred *cred = ap->a_cred;
2131 if (fuse_isdeadfs(vp))
2134 /* Default to looking for user attributes. */
2135 if (ap->a_attrnamespace == EXTATTR_NAMESPACE_SYSTEM)
2136 prefix = EXTATTR_NAMESPACE_SYSTEM_STRING;
2138 prefix = EXTATTR_NAMESPACE_USER_STRING;
2140 len = strlen(prefix) + sizeof(extattr_namespace_separator) +
2141 strlen(ap->a_name) + 1;
2143 fdisp_init(&fdi, len + sizeof(*set_xattr_in) + uio->uio_resid);
2144 fdisp_make_vp(&fdi, FUSE_SETXATTR, vp, td, cred);
2146 set_xattr_in = fdi.indata;
2147 set_xattr_in->size = uio->uio_resid;
2149 attr_str = (char *)fdi.indata + sizeof(*set_xattr_in);
2150 snprintf(attr_str, len, "%s%c%s", prefix, extattr_namespace_separator,
2153 err = uiomove((char *)fdi.indata + sizeof(*set_xattr_in) + len,
2154 uio->uio_resid, uio);
2159 err = fdisp_wait_answ(&fdi);
2163 fsess_set_notimpl(mp, FUSE_SETXATTR);
2168 fdisp_destroy(&fdi);
2173 * The Linux / FUSE extended attribute list is simply a collection of
2174 * NUL-terminated strings. The FreeBSD extended attribute list is a single
2175 * byte length followed by a non-NUL terminated string. So, this allows
2176 * conversion of the Linux / FUSE format to the FreeBSD format in place.
2177 * Linux attribute names are reported with the namespace as a prefix (e.g.
2178 * "user.attribute_name"), but in FreeBSD they are reported without the
2179 * namespace prefix (e.g. "attribute_name"). So, we're going from:
2181 * user.attr_name1\0user.attr_name2\0
2185 * <num>attr_name1<num>attr_name2
2187 * Where "<num>" is a single byte number of characters in the attribute name.
2190 * prefix - exattr namespace prefix string
2191 * list, list_len - input list with namespace prefixes
2192 * bsd_list, bsd_list_len - output list compatible with bsd vfs
2195 fuse_xattrlist_convert(char *prefix, const char *list, int list_len,
2196 char *bsd_list, int *bsd_list_len)
2198 int len, pos, dist_to_next, prefix_len;
2202 prefix_len = strlen(prefix);
2204 while (pos < list_len && list[pos] != '\0') {
2205 dist_to_next = strlen(&list[pos]) + 1;
2206 if (bcmp(&list[pos], prefix, prefix_len) == 0 &&
2207 list[pos + prefix_len] == extattr_namespace_separator) {
2208 len = dist_to_next -
2209 (prefix_len + sizeof(extattr_namespace_separator)) - 1;
2210 if (len >= EXTATTR_MAXNAMELEN)
2211 return (ENAMETOOLONG);
2213 bsd_list[*bsd_list_len] = len;
2214 memcpy(&bsd_list[*bsd_list_len + 1],
2215 &list[pos + prefix_len +
2216 sizeof(extattr_namespace_separator)], len);
2218 *bsd_list_len += len + 1;
2221 pos += dist_to_next;
2228 struct vop_listextattr_args {
2229 struct vop_generic_args a_gen;
2231 int a_attrnamespace;
2234 struct ucred *a_cred;
2235 struct thread *a_td;
2239 fuse_vnop_listextattr(struct vop_listextattr_args *ap)
2241 struct vnode *vp = ap->a_vp;
2242 struct uio *uio = ap->a_uio;
2243 struct fuse_dispatcher fdi;
2244 struct fuse_listxattr_in *list_xattr_in;
2245 struct fuse_listxattr_out *list_xattr_out;
2246 struct mount *mp = vnode_mount(vp);
2247 struct thread *td = ap->a_td;
2248 struct ucred *cred = ap->a_cred;
2252 char *bsd_list = NULL;
2258 if (fuse_isdeadfs(vp))
2262 * Add space for a NUL and the period separator if enabled.
2263 * Default to looking for user attributes.
2265 if (ap->a_attrnamespace == EXTATTR_NAMESPACE_SYSTEM)
2266 prefix = EXTATTR_NAMESPACE_SYSTEM_STRING;
2268 prefix = EXTATTR_NAMESPACE_USER_STRING;
2270 len = strlen(prefix) + sizeof(extattr_namespace_separator) + 1;
2272 fdisp_init(&fdi, sizeof(*list_xattr_in) + len);
2273 fdisp_make_vp(&fdi, FUSE_LISTXATTR, vp, td, cred);
2276 * Retrieve Linux / FUSE compatible list size.
2278 list_xattr_in = fdi.indata;
2279 list_xattr_in->size = 0;
2280 attr_str = (char *)fdi.indata + sizeof(*list_xattr_in);
2281 snprintf(attr_str, len, "%s%c", prefix, extattr_namespace_separator);
2283 err = fdisp_wait_answ(&fdi);
2286 fsess_set_notimpl(mp, FUSE_LISTXATTR);
2290 list_xattr_out = fdi.answ;
2291 linux_list_len = list_xattr_out->size;
2292 if (linux_list_len == 0) {
2293 if (ap->a_size != NULL)
2294 *ap->a_size = linux_list_len;
2299 * Retrieve Linux / FUSE compatible list values.
2301 fdisp_make_vp(&fdi, FUSE_LISTXATTR, vp, td, cred);
2302 list_xattr_in = fdi.indata;
2303 list_xattr_in->size = linux_list_len + sizeof(*list_xattr_out);
2304 attr_str = (char *)fdi.indata + sizeof(*list_xattr_in);
2305 snprintf(attr_str, len, "%s%c", prefix, extattr_namespace_separator);
2307 err = fdisp_wait_answ(&fdi);
2311 linux_list = fdi.answ;
2312 linux_list_len = fdi.iosize;
2315 * Retrieve the BSD compatible list values.
2316 * The Linux / FUSE attribute list format isn't the same
2317 * as FreeBSD's format. So we need to transform it into
2318 * FreeBSD's format before giving it to the user.
2320 bsd_list = malloc(linux_list_len, M_TEMP, M_WAITOK);
2321 err = fuse_xattrlist_convert(prefix, linux_list, linux_list_len,
2322 bsd_list, &bsd_list_len);
2326 if (ap->a_size != NULL)
2327 *ap->a_size = bsd_list_len;
2330 err = uiomove(bsd_list, bsd_list_len, uio);
2333 free(bsd_list, M_TEMP);
2334 fdisp_destroy(&fdi);
2339 struct vop_deleteextattr_args {
2340 struct vop_generic_args a_gen;
2342 int a_attrnamespace;
2344 struct ucred *a_cred;
2345 struct thread *a_td;
2349 fuse_vnop_deleteextattr(struct vop_deleteextattr_args *ap)
2351 struct vnode *vp = ap->a_vp;
2352 struct fuse_dispatcher fdi;
2353 struct mount *mp = vnode_mount(vp);
2354 struct thread *td = ap->a_td;
2355 struct ucred *cred = ap->a_cred;
2361 if (fuse_isdeadfs(vp))
2364 /* Default to looking for user attributes. */
2365 if (ap->a_attrnamespace == EXTATTR_NAMESPACE_SYSTEM)
2366 prefix = EXTATTR_NAMESPACE_SYSTEM_STRING;
2368 prefix = EXTATTR_NAMESPACE_USER_STRING;
2370 len = strlen(prefix) + sizeof(extattr_namespace_separator) +
2371 strlen(ap->a_name) + 1;
2373 fdisp_init(&fdi, len);
2374 fdisp_make_vp(&fdi, FUSE_REMOVEXATTR, vp, td, cred);
2376 attr_str = fdi.indata;
2377 snprintf(attr_str, len, "%s%c%s", prefix, extattr_namespace_separator,
2380 err = fdisp_wait_answ(&fdi);
2383 fsess_set_notimpl(mp, FUSE_REMOVEXATTR);
2386 fdisp_destroy(&fdi);
2391 struct vnop_print_args {
2396 fuse_vnop_print(struct vop_print_args *ap)
2398 struct fuse_vnode_data *fvdat = VTOFUD(ap->a_vp);
2400 printf("nodeid: %ju, parent nodeid: %ju, nlookup: %ju, flag: %#x\n",
2401 (uintmax_t)VTOILLU(ap->a_vp), (uintmax_t)fvdat->parent_nid,
2402 (uintmax_t)fvdat->nlookup,