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"
105 #include "fuse_param.h"
108 #include <sys/priv.h>
110 SDT_PROVIDER_DECLARE(fuse);
113 * arg0: verbosity. Higher numbers give more verbose messages
114 * arg1: Textual message
116 SDT_PROBE_DEFINE2(fuse, , vnops, trace, "int", "char*");
119 static vop_access_t fuse_vnop_access;
120 static vop_close_t fuse_vnop_close;
121 static vop_create_t fuse_vnop_create;
122 static vop_deleteextattr_t fuse_vnop_deleteextattr;
123 static vop_fsync_t fuse_vnop_fsync;
124 static vop_getattr_t fuse_vnop_getattr;
125 static vop_getextattr_t fuse_vnop_getextattr;
126 static vop_inactive_t fuse_vnop_inactive;
127 static vop_link_t fuse_vnop_link;
128 static vop_listextattr_t fuse_vnop_listextattr;
129 static vop_lookup_t fuse_vnop_lookup;
130 static vop_mkdir_t fuse_vnop_mkdir;
131 static vop_mknod_t fuse_vnop_mknod;
132 static vop_open_t fuse_vnop_open;
133 static vop_pathconf_t fuse_vnop_pathconf;
134 static vop_read_t fuse_vnop_read;
135 static vop_readdir_t fuse_vnop_readdir;
136 static vop_readlink_t fuse_vnop_readlink;
137 static vop_reclaim_t fuse_vnop_reclaim;
138 static vop_remove_t fuse_vnop_remove;
139 static vop_rename_t fuse_vnop_rename;
140 static vop_rmdir_t fuse_vnop_rmdir;
141 static vop_setattr_t fuse_vnop_setattr;
142 static vop_setextattr_t fuse_vnop_setextattr;
143 static vop_strategy_t fuse_vnop_strategy;
144 static vop_symlink_t fuse_vnop_symlink;
145 static vop_write_t fuse_vnop_write;
146 static vop_getpages_t fuse_vnop_getpages;
147 static vop_putpages_t fuse_vnop_putpages;
148 static vop_print_t fuse_vnop_print;
150 struct vop_vector fuse_vnops = {
151 .vop_default = &default_vnodeops,
152 .vop_access = fuse_vnop_access,
153 .vop_close = fuse_vnop_close,
154 .vop_create = fuse_vnop_create,
155 .vop_deleteextattr = fuse_vnop_deleteextattr,
156 .vop_fsync = fuse_vnop_fsync,
157 .vop_getattr = fuse_vnop_getattr,
158 .vop_getextattr = fuse_vnop_getextattr,
159 .vop_inactive = fuse_vnop_inactive,
160 .vop_link = fuse_vnop_link,
161 .vop_listextattr = fuse_vnop_listextattr,
162 .vop_lookup = fuse_vnop_lookup,
163 .vop_mkdir = fuse_vnop_mkdir,
164 .vop_mknod = fuse_vnop_mknod,
165 .vop_open = fuse_vnop_open,
166 .vop_pathconf = fuse_vnop_pathconf,
167 .vop_read = fuse_vnop_read,
168 .vop_readdir = fuse_vnop_readdir,
169 .vop_readlink = fuse_vnop_readlink,
170 .vop_reclaim = fuse_vnop_reclaim,
171 .vop_remove = fuse_vnop_remove,
172 .vop_rename = fuse_vnop_rename,
173 .vop_rmdir = fuse_vnop_rmdir,
174 .vop_setattr = fuse_vnop_setattr,
175 .vop_setextattr = fuse_vnop_setextattr,
176 .vop_strategy = fuse_vnop_strategy,
177 .vop_symlink = fuse_vnop_symlink,
178 .vop_write = fuse_vnop_write,
179 .vop_getpages = fuse_vnop_getpages,
180 .vop_putpages = fuse_vnop_putpages,
181 .vop_print = fuse_vnop_print,
184 static u_long fuse_lookup_cache_hits = 0;
186 SYSCTL_ULONG(_vfs_fusefs, OID_AUTO, lookup_cache_hits, CTLFLAG_RD,
187 &fuse_lookup_cache_hits, 0, "number of positive cache hits in lookup");
189 static u_long fuse_lookup_cache_misses = 0;
191 SYSCTL_ULONG(_vfs_fusefs, OID_AUTO, lookup_cache_misses, CTLFLAG_RD,
192 &fuse_lookup_cache_misses, 0, "number of cache misses in lookup");
194 int fuse_lookup_cache_enable = 1;
196 SYSCTL_INT(_vfs_fusefs, OID_AUTO, lookup_cache_enable, CTLFLAG_RW,
197 &fuse_lookup_cache_enable, 0, "if non-zero, enable lookup cache");
200 * XXX: This feature is highly experimental and can bring to instabilities,
201 * needs revisiting before to be enabled by default.
203 static int fuse_reclaim_revoked = 0;
205 SYSCTL_INT(_vfs_fusefs, OID_AUTO, reclaim_revoked, CTLFLAG_RW,
206 &fuse_reclaim_revoked, 0, "");
208 uma_zone_t fuse_pbuf_zone;
210 #define fuse_vm_page_lock(m) vm_page_lock((m));
211 #define fuse_vm_page_unlock(m) vm_page_unlock((m));
212 #define fuse_vm_page_lock_queues() ((void)0)
213 #define fuse_vm_page_unlock_queues() ((void)0)
216 struct vnop_access_args {
218 #if VOP_ACCESS_TAKES_ACCMODE_T
223 struct ucred *a_cred;
228 fuse_vnop_access(struct vop_access_args *ap)
230 struct vnode *vp = ap->a_vp;
231 int accmode = ap->a_accmode;
232 struct ucred *cred = ap->a_cred;
234 struct fuse_access_param facp;
235 struct fuse_data *data = fuse_get_mpdata(vnode_mount(vp));
239 if (fuse_isdeadfs(vp)) {
240 if (vnode_isvroot(vp)) {
245 if (!(data->dataflags & FSESS_INITED)) {
246 if (vnode_isvroot(vp)) {
247 if (priv_check_cred(cred, PRIV_VFS_ADMIN) ||
248 (fuse_match_cred(data->daemoncred, cred) == 0)) {
254 if (vnode_islnk(vp)) {
257 bzero(&facp, sizeof(facp));
259 err = fuse_internal_access(vp, accmode, &facp, ap->a_td, ap->a_cred);
264 struct vnop_close_args {
267 struct ucred *a_cred;
272 fuse_vnop_close(struct vop_close_args *ap)
274 struct vnode *vp = ap->a_vp;
275 struct ucred *cred = ap->a_cred;
276 int fflag = ap->a_fflag;
277 fufh_type_t fufh_type;
279 if (fuse_isdeadfs(vp)) {
282 if (vnode_isdir(vp)) {
283 if (fuse_filehandle_valid(vp, FUFH_RDONLY)) {
284 fuse_filehandle_close(vp, FUFH_RDONLY, NULL, cred);
288 if (fflag & IO_NDELAY) {
291 fufh_type = fuse_filehandle_xlate_from_fflags(fflag);
293 if (!fuse_filehandle_valid(vp, fufh_type)) {
296 for (i = 0; i < FUFH_MAXTYPE; i++)
297 if (fuse_filehandle_valid(vp, i))
299 if (i == FUFH_MAXTYPE)
300 panic("FUSE: fufh type %d found to be invalid in close"
304 if ((VTOFUD(vp)->flag & FN_SIZECHANGE) != 0) {
305 fuse_vnode_savesize(vp, cred);
311 struct vnop_create_args {
313 struct vnode **a_vpp;
314 struct componentname *a_cnp;
319 fuse_vnop_create(struct vop_create_args *ap)
321 struct vnode *dvp = ap->a_dvp;
322 struct vnode **vpp = ap->a_vpp;
323 struct componentname *cnp = ap->a_cnp;
324 struct vattr *vap = ap->a_vap;
325 struct thread *td = cnp->cn_thread;
326 struct ucred *cred = cnp->cn_cred;
328 struct fuse_open_in *foi;
329 struct fuse_entry_out *feo;
330 struct fuse_dispatcher fdi;
331 struct fuse_dispatcher *fdip = &fdi;
335 struct mount *mp = vnode_mount(dvp);
336 uint64_t parentnid = VTOFUD(dvp)->nid;
337 mode_t mode = MAKEIMODE(vap->va_type, vap->va_mode);
339 uint32_t x_open_flags;
341 if (fuse_isdeadfs(dvp)) {
344 bzero(&fdi, sizeof(fdi));
346 /* XXX: Will we ever want devices ? */
347 if ((vap->va_type != VREG)) {
348 printf("fuse_vnop_create: unsupported va_type %d\n",
353 fdisp_init(fdip, sizeof(*foi) + cnp->cn_namelen + 1);
354 if (!fsess_isimpl(mp, FUSE_CREATE)) {
355 SDT_PROBE2(fuse, , vnops, trace, 1,
356 "eh, daemon doesn't implement create?");
359 fdisp_make(fdip, FUSE_CREATE, vnode_mount(dvp), parentnid, td, cred);
363 foi->flags = O_CREAT | O_RDWR;
365 memcpy((char *)fdip->indata + sizeof(*foi), cnp->cn_nameptr,
367 ((char *)fdip->indata)[sizeof(*foi) + cnp->cn_namelen] = '\0';
369 err = fdisp_wait_answ(fdip);
373 fsess_set_notimpl(mp, FUSE_CREATE);
379 if ((err = fuse_internal_checkentry(feo, VREG))) {
382 err = fuse_vnode_get(mp, feo, feo->nodeid, dvp, vpp, cnp, VREG);
384 struct fuse_release_in *fri;
385 uint64_t nodeid = feo->nodeid;
386 uint64_t fh_id = ((struct fuse_open_out *)(feo + 1))->fh;
388 fdisp_init(fdip, sizeof(*fri));
389 fdisp_make(fdip, FUSE_RELEASE, mp, nodeid, td, cred);
392 fri->flags = OFLAGS(mode);
393 fuse_insert_callback(fdip->tick, fuse_internal_forget_callback);
394 fuse_insert_message(fdip->tick);
397 ASSERT_VOP_ELOCKED(*vpp, "fuse_vnop_create");
399 fdip->answ = feo + 1;
401 x_fh_id = ((struct fuse_open_out *)(feo + 1))->fh;
402 x_open_flags = ((struct fuse_open_out *)(feo + 1))->open_flags;
403 fuse_filehandle_init(*vpp, FUFH_RDWR, NULL, x_fh_id);
404 fuse_vnode_open(*vpp, x_open_flags, td);
405 cache_purge_negative(dvp);
413 * Our vnop_fsync roughly corresponds to the FUSE_FSYNC method. The Linux
414 * version of FUSE also has a FUSE_FLUSH method.
416 * On Linux, fsync() synchronizes a file's complete in-core state with that
417 * on disk. The call is not supposed to return until the system has completed
418 * that action or until an error is detected.
420 * Linux also has an fdatasync() call that is similar to fsync() but is not
421 * required to update the metadata such as access time and modification time.
425 struct vnop_fsync_args {
426 struct vnodeop_desc *a_desc;
428 struct ucred * a_cred;
430 struct thread * a_td;
434 fuse_vnop_fsync(struct vop_fsync_args *ap)
436 struct vnode *vp = ap->a_vp;
437 struct thread *td = ap->a_td;
439 struct fuse_filehandle *fufh;
440 struct fuse_vnode_data *fvdat = VTOFUD(vp);
444 if (fuse_isdeadfs(vp)) {
447 if ((err = vop_stdfsync(ap)))
450 if (!fsess_isimpl(vnode_mount(vp),
451 (vnode_vtype(vp) == VDIR ? FUSE_FSYNCDIR : FUSE_FSYNC))) {
454 for (type = 0; type < FUFH_MAXTYPE; type++) {
455 fufh = &(fvdat->fufh[type]);
456 if (FUFH_IS_VALID(fufh)) {
457 fuse_internal_fsync(vp, td, NULL, fufh);
466 struct vnop_getattr_args {
469 struct ucred *a_cred;
474 fuse_vnop_getattr(struct vop_getattr_args *ap)
476 struct vnode *vp = ap->a_vp;
477 struct vattr *vap = ap->a_vap;
478 struct ucred *cred = ap->a_cred;
479 struct thread *td = curthread;
480 struct fuse_vnode_data *fvdat = VTOFUD(vp);
481 struct fuse_attr_out *fao;
485 struct fuse_dispatcher fdi;
487 dataflags = fuse_get_mpdata(vnode_mount(vp))->dataflags;
489 /* Note that we are not bailing out on a dead file system just yet. */
491 if (!(dataflags & FSESS_INITED)) {
492 if (!vnode_isvroot(vp)) {
493 fdata_set_dead(fuse_get_mpdata(vnode_mount(vp)));
501 if ((err = fdisp_simple_putget_vp(&fdi, FUSE_GETATTR, vp, td, cred))) {
502 if ((err == ENOTCONN) && vnode_isvroot(vp)) {
503 /* see comment in fuse_vfsop_statfs() */
508 fuse_internal_vnode_disappear(vp);
513 fao = (struct fuse_attr_out *)fdi.answ;
514 fuse_internal_cache_attrs(vp, &fao->attr, fao->attr_valid,
515 fao->attr_valid_nsec, vap);
516 if (vap->va_type != vnode_vtype(vp)) {
517 fuse_internal_vnode_disappear(vp);
521 if ((fvdat->flag & FN_SIZECHANGE) != 0)
522 vap->va_size = fvdat->filesize;
524 if (vnode_isreg(vp) && (fvdat->flag & FN_SIZECHANGE) == 0) {
526 * This is for those cases when the file size changed without us
527 * knowing, and we want to catch up.
529 off_t new_filesize = ((struct fuse_attr_out *)
530 fdi.answ)->attr.size;
532 if (fvdat->filesize != new_filesize) {
533 fuse_vnode_setsize(vp, new_filesize);
534 fvdat->flag &= ~FN_SIZECHANGE;
543 bzero(vap, sizeof(*vap));
544 vap->va_type = vnode_vtype(vp);
550 struct vnop_inactive_args {
556 fuse_vnop_inactive(struct vop_inactive_args *ap)
558 struct vnode *vp = ap->a_vp;
559 struct thread *td = ap->a_td;
561 struct fuse_vnode_data *fvdat = VTOFUD(vp);
562 struct fuse_filehandle *fufh = NULL;
564 int type, need_flush = 1;
566 for (type = 0; type < FUFH_MAXTYPE; type++) {
567 fufh = &(fvdat->fufh[type]);
568 if (FUFH_IS_VALID(fufh)) {
569 if (need_flush && vp->v_type == VREG) {
570 if ((VTOFUD(vp)->flag & FN_SIZECHANGE) != 0) {
571 fuse_vnode_savesize(vp, NULL);
573 if (fuse_data_cache_invalidate ||
574 (fvdat->flag & FN_REVOKED) != 0)
575 fuse_io_invalbuf(vp, td);
577 fuse_io_flushbuf(vp, MNT_WAIT, td);
580 fuse_filehandle_close(vp, type, td, NULL);
584 if ((fvdat->flag & FN_REVOKED) != 0 && fuse_reclaim_revoked) {
591 struct vnop_link_args {
592 struct vnode *a_tdvp;
594 struct componentname *a_cnp;
598 fuse_vnop_link(struct vop_link_args *ap)
600 struct vnode *vp = ap->a_vp;
601 struct vnode *tdvp = ap->a_tdvp;
602 struct componentname *cnp = ap->a_cnp;
604 struct vattr *vap = VTOVA(vp);
606 struct fuse_dispatcher fdi;
607 struct fuse_entry_out *feo;
608 struct fuse_link_in fli;
612 if (fuse_isdeadfs(vp)) {
615 if (vnode_mount(tdvp) != vnode_mount(vp)) {
620 * This is a seatbelt check to protect naive userspace filesystems from
621 * themselves and the limitations of the FUSE IPC protocol. If a
622 * filesystem does not allow attribute caching, assume it is capable of
623 * validating that nlink does not overflow.
625 if (vap != NULL && vap->va_nlink >= FUSE_LINK_MAX)
627 fli.oldnodeid = VTOI(vp);
630 fuse_internal_newentry_makerequest(vnode_mount(tdvp), VTOI(tdvp), cnp,
631 FUSE_LINK, &fli, sizeof(fli), &fdi);
632 if ((err = fdisp_wait_answ(&fdi))) {
637 err = fuse_internal_checkentry(feo, vnode_vtype(vp));
644 struct vnop_lookup_args {
645 struct vnodeop_desc *a_desc;
647 struct vnode **a_vpp;
648 struct componentname *a_cnp;
652 fuse_vnop_lookup(struct vop_lookup_args *ap)
654 struct vnode *dvp = ap->a_dvp;
655 struct vnode **vpp = ap->a_vpp;
656 struct componentname *cnp = ap->a_cnp;
657 struct thread *td = cnp->cn_thread;
658 struct ucred *cred = cnp->cn_cred;
660 int nameiop = cnp->cn_nameiop;
661 int flags = cnp->cn_flags;
662 int wantparent = flags & (LOCKPARENT | WANTPARENT);
663 int islastcn = flags & ISLASTCN;
664 struct mount *mp = vnode_mount(dvp);
668 struct vnode *vp = NULL;
670 struct fuse_dispatcher fdi;
674 struct fuse_access_param facp;
676 if (fuse_isdeadfs(dvp)) {
680 if (!vnode_isdir(dvp)) {
683 if (islastcn && vfs_isrdonly(mp) && (nameiop != LOOKUP)) {
687 * We do access check prior to doing anything else only in the case
688 * when we are at fs root (we'd like to say, "we are at the first
689 * component", but that's not exactly the same... nevermind).
690 * See further comments at further access checks.
693 bzero(&facp, sizeof(facp));
694 if (vnode_isvroot(dvp)) { /* early permission check hack */
695 if ((err = fuse_internal_access(dvp, VEXEC, &facp, td, cred))) {
699 if (flags & ISDOTDOT) {
700 nid = VTOFUD(dvp)->parent_nid;
707 } else if (cnp->cn_namelen == 1 && *(cnp->cn_nameptr) == '.') {
712 } else if (fuse_lookup_cache_enable) {
713 err = cache_lookup(dvp, vpp, cnp, NULL, NULL);
716 case -1: /* positive match */
717 atomic_add_acq_long(&fuse_lookup_cache_hits, 1);
720 case 0: /* no match in cache */
721 atomic_add_acq_long(&fuse_lookup_cache_misses, 1);
724 case ENOENT: /* negative match */
731 fdisp_init(&fdi, cnp->cn_namelen + 1);
735 fdisp_make(&fdi, op, mp, nid, td, cred);
737 if (op == FUSE_LOOKUP) {
738 memcpy(fdi.indata, cnp->cn_nameptr, cnp->cn_namelen);
739 ((char *)fdi.indata)[cnp->cn_namelen] = '\0';
741 lookup_err = fdisp_wait_answ(&fdi);
743 if ((op == FUSE_LOOKUP) && !lookup_err) { /* lookup call succeeded */
744 nid = ((struct fuse_entry_out *)fdi.answ)->nodeid;
747 * zero nodeid is the same as "not found",
748 * but it's also cacheable (which we keep
749 * keep on doing not as of writing this)
752 } else if (nid == FUSE_ROOT_ID) {
757 (!fdi.answ_stat || lookup_err != ENOENT || op != FUSE_LOOKUP)) {
761 /* lookup_err, if non-zero, must be ENOENT at this point */
765 if ((nameiop == CREATE || nameiop == RENAME) && islastcn
766 /* && directory dvp has not been removed */ ) {
768 if (vfs_isrdonly(mp)) {
772 #if 0 /* THINK_ABOUT_THIS */
773 if ((err = fuse_internal_access(dvp, VWRITE, cred, td, &facp))) {
779 * Possibly record the position of a slot in the
780 * directory large enough for the new component name.
781 * This can be recorded in the vnode private data for
782 * dvp. Set the SAVENAME flag to hold onto the
783 * pathname for use later in VOP_CREATE or VOP_RENAME.
785 cnp->cn_flags |= SAVENAME;
790 /* Consider inserting name into cache. */
793 * No we can't use negative caching, as the fs
794 * changes are out of our control.
795 * False positives' falseness turns out just as things
796 * go by, but false negatives' falseness doesn't.
797 * (and aiding the caching mechanism with extra control
798 * mechanisms comes quite close to beating the whole purpose
802 if ((cnp->cn_flags & MAKEENTRY) != 0) {
803 SDT_PROBE2(fuse, , vnops, trace, 1,
804 "inserting NULL into cache");
805 cache_enter(dvp, NULL, cnp);
815 struct fuse_entry_out *feo = NULL;
816 struct fuse_attr *fattr = NULL;
818 if (op == FUSE_GETATTR) {
819 fattr = &((struct fuse_attr_out *)fdi.answ)->attr;
821 feo = (struct fuse_entry_out *)fdi.answ;
822 fattr = &(feo->attr);
826 * If deleting, and at end of pathname, return parameters
827 * which can be used to remove file. If the wantparent flag
828 * isn't set, we return only the directory, otherwise we go on
829 * and lock the inode, being careful with ".".
831 if (nameiop == DELETE && islastcn) {
833 * Check for write access on directory.
835 facp.xuid = fattr->uid;
836 facp.facc_flags |= FACCESS_STICKY;
837 err = fuse_internal_access(dvp, VWRITE, &facp, td, cred);
838 facp.facc_flags &= ~FACCESS_XQUERIES;
843 if (nid == VTOI(dvp)) {
847 err = fuse_vnode_get(dvp->v_mount, feo, nid,
848 dvp, &vp, cnp, IFTOVT(fattr->mode));
855 * Save the name for use in VOP_RMDIR and VOP_REMOVE
858 cnp->cn_flags |= SAVENAME;
863 * If rewriting (RENAME), return the inode and the
864 * information required to rewrite the present directory
865 * Must get inode of directory entry to verify it's a
866 * regular file, or empty directory.
868 if (nameiop == RENAME && wantparent && islastcn) {
870 #if 0 /* THINK_ABOUT_THIS */
871 if ((err = fuse_internal_access(dvp, VWRITE, cred, td, &facp))) {
879 if (nid == VTOI(dvp)) {
883 err = fuse_vnode_get(vnode_mount(dvp), feo, nid, dvp,
884 &vp, cnp, IFTOVT(fattr->mode));
890 * Save the name for use in VOP_RENAME later.
892 cnp->cn_flags |= SAVENAME;
896 if (flags & ISDOTDOT) {
901 * Expanded copy of vn_vget_ino() so that
902 * fuse_vnode_get() can be used.
905 ltype = VOP_ISLOCKED(dvp);
906 err = vfs_busy(mp, MBF_NOWAIT);
910 err = vfs_busy(mp, 0);
911 vn_lock(dvp, ltype | LK_RETRY);
915 if ((dvp->v_iflag & VI_DOOMED) != 0) {
922 err = fuse_vnode_get(vnode_mount(dvp), feo, nid, NULL,
923 &vp, cnp, IFTOVT(fattr->mode));
925 vn_lock(dvp, ltype | LK_RETRY);
926 if ((dvp->v_iflag & VI_DOOMED) != 0) {
934 } else if (nid == VTOI(dvp)) {
938 struct fuse_vnode_data *fvdat;
940 err = fuse_vnode_get(vnode_mount(dvp), feo, nid, dvp,
941 &vp, cnp, IFTOVT(fattr->mode));
945 fuse_vnode_setparent(vp, dvp);
948 * In the case where we are looking up a FUSE node
949 * represented by an existing cached vnode, and the
950 * true size reported by FUSE_LOOKUP doesn't match
951 * the vnode's cached size, fix the vnode cache to
952 * match the real object size.
954 * This can occur via FUSE distributed filesystems,
955 * irregular files, etc.
958 if (vnode_isreg(vp) &&
959 fattr->size != fvdat->filesize) {
961 * The FN_SIZECHANGE flag reflects a dirty
962 * append. If userspace lets us know our cache
963 * is invalid, that write was lost. (Dirty
964 * writes that do not cause append are also
965 * lost, but we don't detect them here.)
967 * XXX: Maybe disable WB caching on this mount.
969 if (fvdat->flag & FN_SIZECHANGE)
970 printf("%s: WB cache incoherent on "
972 vnode_mount(vp)->mnt_stat.f_mntonname);
974 (void)fuse_vnode_setsize(vp, fattr->size);
975 fvdat->flag &= ~FN_SIZECHANGE;
980 if (op == FUSE_GETATTR) {
981 struct fuse_attr_out *fao =
982 (struct fuse_attr_out*)fdi.answ;
983 fuse_internal_cache_attrs(*vpp,
984 &fao->attr, fao->attr_valid,
985 fao->attr_valid_nsec, NULL);
987 struct fuse_entry_out *feo =
988 (struct fuse_entry_out*)fdi.answ;
989 fuse_internal_cache_attrs(*vpp,
990 &feo->attr, feo->attr_valid,
991 feo->attr_valid_nsec, NULL);
994 /* Insert name into cache if appropriate. */
997 * Nooo, caching is evil. With caching, we can't avoid stale
998 * information taking over the playground (cached info is not
999 * just positive/negative, it does have qualitative aspects,
1000 * too). And a (VOP/FUSE)_GETATTR is always thrown anyway, when
1001 * walking down along cached path components, and that's not
1002 * any cheaper than FUSE_LOOKUP. This might change with
1003 * implementing kernel side attr caching, but... In Linux,
1004 * lookup results are not cached, and the daemon is bombarded
1005 * with FUSE_LOOKUPS on and on. This shows that by design, the
1006 * daemon is expected to handle frequent lookup queries
1007 * efficiently, do its caching in userspace, and so on.
1009 * So just leave the name cache alone.
1013 * Well, now I know, Linux caches lookups, but with a
1014 * timeout... So it's the same thing as attribute caching:
1015 * we can deal with it when implement timeouts.
1018 if (cnp->cn_flags & MAKEENTRY) {
1019 cache_enter(dvp, *vpp, cnp);
1026 /* No lookup error; need to clean up. */
1028 if (err) { /* Found inode; exit with no vnode. */
1029 if (op == FUSE_LOOKUP) {
1030 fuse_internal_forget_send(vnode_mount(dvp), td, cred,
1033 fdisp_destroy(&fdi);
1036 #ifndef NO_EARLY_PERM_CHECK_HACK
1039 * We have the attributes of the next item
1040 * *now*, and it's a fact, and we do not
1041 * have to do extra work for it (ie, beg the
1042 * daemon), and it neither depends on such
1043 * accidental things like attr caching. So
1044 * the big idea: check credentials *now*,
1045 * not at the beginning of the next call to
1048 * The first item of the lookup chain (fs root)
1049 * won't be checked then here, of course, as
1050 * its never "the next". But go and see that
1051 * the root is taken care about at the very
1052 * beginning of this function.
1054 * Now, given we want to do the access check
1055 * this way, one might ask: so then why not
1056 * do the access check just after fetching
1057 * the inode and its attributes from the
1058 * daemon? Why bother with producing the
1059 * corresponding vnode at all if something
1060 * is not OK? We know what's the deal as
1061 * soon as we get those attrs... There is
1062 * one bit of info though not given us by
1063 * the daemon: whether his response is
1064 * authoritative or not... His response should
1065 * be ignored if something is mounted over
1066 * the dir in question. But that can be
1067 * known only by having the vnode...
1069 int tmpvtype = vnode_vtype(*vpp);
1071 bzero(&facp, sizeof(facp));
1072 /*the early perm check hack */
1073 facp.facc_flags |= FACCESS_VA_VALID;
1075 if ((tmpvtype != VDIR) && (tmpvtype != VLNK)) {
1078 if (!err && !vnode_mountedhere(*vpp)) {
1079 err = fuse_internal_access(*vpp, VEXEC, &facp, td, cred);
1082 if (tmpvtype == VLNK)
1083 SDT_PROBE2(fuse, , vnops, trace,
1084 1, "weird, permission "
1085 "error with a symlink?");
1093 fdisp_destroy(&fdi);
1099 struct vnop_mkdir_args {
1100 struct vnode *a_dvp;
1101 struct vnode **a_vpp;
1102 struct componentname *a_cnp;
1103 struct vattr *a_vap;
1107 fuse_vnop_mkdir(struct vop_mkdir_args *ap)
1109 struct vnode *dvp = ap->a_dvp;
1110 struct vnode **vpp = ap->a_vpp;
1111 struct componentname *cnp = ap->a_cnp;
1112 struct vattr *vap = ap->a_vap;
1114 struct fuse_mkdir_in fmdi;
1116 if (fuse_isdeadfs(dvp)) {
1119 fmdi.mode = MAKEIMODE(vap->va_type, vap->va_mode);
1121 return (fuse_internal_newentry(dvp, vpp, cnp, FUSE_MKDIR, &fmdi,
1122 sizeof(fmdi), VDIR));
1126 struct vnop_mknod_args {
1127 struct vnode *a_dvp;
1128 struct vnode **a_vpp;
1129 struct componentname *a_cnp;
1130 struct vattr *a_vap;
1134 fuse_vnop_mknod(struct vop_mknod_args *ap)
1142 struct vnop_open_args {
1145 struct ucred *a_cred;
1146 struct thread *a_td;
1147 int a_fdidx; / struct file *a_fp;
1151 fuse_vnop_open(struct vop_open_args *ap)
1153 struct vnode *vp = ap->a_vp;
1154 int mode = ap->a_mode;
1155 struct thread *td = ap->a_td;
1156 struct ucred *cred = ap->a_cred;
1158 fufh_type_t fufh_type;
1159 struct fuse_vnode_data *fvdat;
1161 int error, isdir = 0;
1162 int32_t fuse_open_flags;
1164 if (fuse_isdeadfs(vp)) {
1167 if ((mode & (FREAD | FWRITE)) == 0)
1172 if (vnode_isdir(vp)) {
1175 fuse_open_flags = 0;
1177 fufh_type = FUFH_RDONLY;
1179 fufh_type = fuse_filehandle_xlate_from_fflags(mode);
1181 * For WRONLY opens, force DIRECT_IO. This is necessary
1182 * since writing a partial block through the buffer cache
1183 * will result in a read of the block and that read won't
1184 * be allowed by the WRONLY open.
1186 if (fufh_type == FUFH_WRONLY ||
1187 (fvdat->flag & FN_DIRECTIO) != 0)
1188 fuse_open_flags = FOPEN_DIRECT_IO;
1191 if (fuse_filehandle_validrw(vp, fufh_type) != FUFH_INVALID) {
1192 fuse_vnode_open(vp, fuse_open_flags, td);
1195 error = fuse_filehandle_open(vp, fufh_type, NULL, td, cred);
1201 fuse_vnop_pathconf(struct vop_pathconf_args *ap)
1204 switch (ap->a_name) {
1205 case _PC_FILESIZEBITS:
1209 *ap->a_retval = NAME_MAX;
1212 *ap->a_retval = MIN(LONG_MAX, FUSE_LINK_MAX);
1214 case _PC_SYMLINK_MAX:
1215 *ap->a_retval = MAXPATHLEN;
1221 return (vop_stdpathconf(ap));
1226 struct vnop_read_args {
1230 struct ucred *a_cred;
1234 fuse_vnop_read(struct vop_read_args *ap)
1236 struct vnode *vp = ap->a_vp;
1237 struct uio *uio = ap->a_uio;
1238 int ioflag = ap->a_ioflag;
1239 struct ucred *cred = ap->a_cred;
1241 if (fuse_isdeadfs(vp)) {
1245 if (VTOFUD(vp)->flag & FN_DIRECTIO) {
1246 ioflag |= IO_DIRECT;
1249 return fuse_io_dispatch(vp, uio, ioflag, cred);
1253 struct vnop_readdir_args {
1256 struct ucred *a_cred;
1263 fuse_vnop_readdir(struct vop_readdir_args *ap)
1265 struct vnode *vp = ap->a_vp;
1266 struct uio *uio = ap->a_uio;
1267 struct ucred *cred = ap->a_cred;
1269 struct fuse_filehandle *fufh = NULL;
1270 struct fuse_iov cookediov;
1275 if (fuse_isdeadfs(vp)) {
1278 if ( /* XXXIP ((uio_iovcnt(uio) > 1)) || */
1279 (uio_resid(uio) < sizeof(struct dirent))) {
1283 if (!fuse_filehandle_valid(vp, FUFH_RDONLY)) {
1284 SDT_PROBE2(fuse, , vnops, trace, 1,
1285 "calling readdir() before open()");
1286 err = fuse_filehandle_open(vp, FUFH_RDONLY, &fufh, NULL, cred);
1289 err = fuse_filehandle_get(vp, FUFH_RDONLY, &fufh);
1294 #define DIRCOOKEDSIZE FUSE_DIRENT_ALIGN(FUSE_NAME_OFFSET + MAXNAMLEN + 1)
1295 fiov_init(&cookediov, DIRCOOKEDSIZE);
1297 err = fuse_internal_readdir(vp, uio, fufh, &cookediov);
1299 fiov_teardown(&cookediov);
1301 fuse_filehandle_close(vp, FUFH_RDONLY, NULL, cred);
1307 struct vnop_readlink_args {
1310 struct ucred *a_cred;
1314 fuse_vnop_readlink(struct vop_readlink_args *ap)
1316 struct vnode *vp = ap->a_vp;
1317 struct uio *uio = ap->a_uio;
1318 struct ucred *cred = ap->a_cred;
1320 struct fuse_dispatcher fdi;
1323 if (fuse_isdeadfs(vp)) {
1326 if (!vnode_islnk(vp)) {
1329 fdisp_init(&fdi, 0);
1330 err = fdisp_simple_putget_vp(&fdi, FUSE_READLINK, vp, curthread, cred);
1334 if (((char *)fdi.answ)[0] == '/' &&
1335 fuse_get_mpdata(vnode_mount(vp))->dataflags & FSESS_PUSH_SYMLINKS_IN) {
1336 char *mpth = vnode_mount(vp)->mnt_stat.f_mntonname;
1338 err = uiomove(mpth, strlen(mpth), uio);
1341 err = uiomove(fdi.answ, fdi.iosize, uio);
1344 fdisp_destroy(&fdi);
1349 struct vnop_reclaim_args {
1351 struct thread *a_td;
1355 fuse_vnop_reclaim(struct vop_reclaim_args *ap)
1357 struct vnode *vp = ap->a_vp;
1358 struct thread *td = ap->a_td;
1360 struct fuse_vnode_data *fvdat = VTOFUD(vp);
1361 struct fuse_filehandle *fufh = NULL;
1366 panic("FUSE: no vnode data during recycling");
1368 for (type = 0; type < FUFH_MAXTYPE; type++) {
1369 fufh = &(fvdat->fufh[type]);
1370 if (FUFH_IS_VALID(fufh)) {
1371 printf("FUSE: vnode being reclaimed but fufh (type=%d) is valid",
1373 fuse_filehandle_close(vp, type, td, NULL);
1377 if ((!fuse_isdeadfs(vp)) && (fvdat->nlookup)) {
1378 fuse_internal_forget_send(vnode_mount(vp), td, NULL, VTOI(vp),
1381 fuse_vnode_setparent(vp, NULL);
1383 vfs_hash_remove(vp);
1384 vnode_destroy_vobject(vp);
1385 fuse_vnode_destroy(vp);
1391 struct vnop_remove_args {
1392 struct vnode *a_dvp;
1394 struct componentname *a_cnp;
1398 fuse_vnop_remove(struct vop_remove_args *ap)
1400 struct vnode *dvp = ap->a_dvp;
1401 struct vnode *vp = ap->a_vp;
1402 struct componentname *cnp = ap->a_cnp;
1406 if (fuse_isdeadfs(vp)) {
1409 if (vnode_isdir(vp)) {
1414 err = fuse_internal_remove(dvp, vp, cnp, FUSE_UNLINK);
1417 fuse_internal_vnode_disappear(vp);
1422 struct vnop_rename_args {
1423 struct vnode *a_fdvp;
1424 struct vnode *a_fvp;
1425 struct componentname *a_fcnp;
1426 struct vnode *a_tdvp;
1427 struct vnode *a_tvp;
1428 struct componentname *a_tcnp;
1432 fuse_vnop_rename(struct vop_rename_args *ap)
1434 struct vnode *fdvp = ap->a_fdvp;
1435 struct vnode *fvp = ap->a_fvp;
1436 struct componentname *fcnp = ap->a_fcnp;
1437 struct vnode *tdvp = ap->a_tdvp;
1438 struct vnode *tvp = ap->a_tvp;
1439 struct componentname *tcnp = ap->a_tcnp;
1440 struct fuse_data *data;
1444 if (fuse_isdeadfs(fdvp)) {
1447 if (fvp->v_mount != tdvp->v_mount ||
1448 (tvp && fvp->v_mount != tvp->v_mount)) {
1449 SDT_PROBE2(fuse, , vnops, trace, 1, "cross-device rename");
1456 * FUSE library is expected to check if target directory is not
1457 * under the source directory in the file system tree.
1458 * Linux performs this check at VFS level.
1460 data = fuse_get_mpdata(vnode_mount(tdvp));
1461 sx_xlock(&data->rename_lock);
1462 err = fuse_internal_rename(fdvp, fcnp, tdvp, tcnp);
1465 fuse_vnode_setparent(fvp, tdvp);
1467 fuse_vnode_setparent(tvp, NULL);
1469 sx_unlock(&data->rename_lock);
1471 if (tvp != NULL && tvp != fvp) {
1474 if (vnode_isdir(fvp)) {
1475 if ((tvp != NULL) && vnode_isdir(tvp)) {
1496 struct vnop_rmdir_args {
1497 struct vnode *a_dvp;
1499 struct componentname *a_cnp;
1503 fuse_vnop_rmdir(struct vop_rmdir_args *ap)
1505 struct vnode *dvp = ap->a_dvp;
1506 struct vnode *vp = ap->a_vp;
1510 if (fuse_isdeadfs(vp)) {
1513 if (VTOFUD(vp) == VTOFUD(dvp)) {
1516 err = fuse_internal_remove(dvp, vp, ap->a_cnp, FUSE_RMDIR);
1519 fuse_internal_vnode_disappear(vp);
1524 struct vnop_setattr_args {
1526 struct vattr *a_vap;
1527 struct ucred *a_cred;
1528 struct thread *a_td;
1532 fuse_vnop_setattr(struct vop_setattr_args *ap)
1534 struct vnode *vp = ap->a_vp;
1535 struct vattr *vap = ap->a_vap;
1536 struct ucred *cred = ap->a_cred;
1537 struct thread *td = curthread;
1539 struct fuse_dispatcher fdi;
1540 struct fuse_setattr_in *fsai;
1541 struct fuse_access_param facp;
1545 int sizechanged = 0;
1546 uint64_t newsize = 0;
1548 if (fuse_isdeadfs(vp)) {
1551 fdisp_init(&fdi, sizeof(*fsai));
1552 fdisp_make_vp(&fdi, FUSE_SETATTR, vp, td, cred);
1556 bzero(&facp, sizeof(facp));
1558 facp.xuid = vap->va_uid;
1559 facp.xgid = vap->va_gid;
1561 if (vap->va_uid != (uid_t)VNOVAL) {
1562 facp.facc_flags |= FACCESS_CHOWN;
1563 fsai->uid = vap->va_uid;
1564 fsai->valid |= FATTR_UID;
1566 if (vap->va_gid != (gid_t)VNOVAL) {
1567 facp.facc_flags |= FACCESS_CHOWN;
1568 fsai->gid = vap->va_gid;
1569 fsai->valid |= FATTR_GID;
1571 if (vap->va_size != VNOVAL) {
1573 struct fuse_filehandle *fufh = NULL;
1575 /*Truncate to a new value. */
1576 fsai->size = vap->va_size;
1578 newsize = vap->va_size;
1579 fsai->valid |= FATTR_SIZE;
1581 fuse_filehandle_getrw(vp, FUFH_WRONLY, &fufh);
1583 fsai->fh = fufh->fh_id;
1584 fsai->valid |= FATTR_FH;
1587 if (vap->va_atime.tv_sec != VNOVAL) {
1588 fsai->atime = vap->va_atime.tv_sec;
1589 fsai->atimensec = vap->va_atime.tv_nsec;
1590 fsai->valid |= FATTR_ATIME;
1592 if (vap->va_mtime.tv_sec != VNOVAL) {
1593 fsai->mtime = vap->va_mtime.tv_sec;
1594 fsai->mtimensec = vap->va_mtime.tv_nsec;
1595 fsai->valid |= FATTR_MTIME;
1597 if (vap->va_mode != (mode_t)VNOVAL) {
1598 fsai->mode = vap->va_mode & ALLPERMS;
1599 fsai->valid |= FATTR_MODE;
1604 vtyp = vnode_vtype(vp);
1606 if (fsai->valid & FATTR_SIZE && vtyp == VDIR) {
1610 if (vfs_isrdonly(vnode_mount(vp)) && (fsai->valid & ~FATTR_SIZE || vtyp == VREG)) {
1614 if (fsai->valid & ~FATTR_SIZE) {
1615 /*err = fuse_internal_access(vp, VADMIN, context, &facp); */
1619 facp.facc_flags &= ~FACCESS_XQUERIES;
1621 if (err && !(fsai->valid & ~(FATTR_ATIME | FATTR_MTIME)) &&
1622 vap->va_vaflags & VA_UTIMES_NULL) {
1623 err = fuse_internal_access(vp, VWRITE, &facp, td, cred);
1627 if ((err = fdisp_wait_answ(&fdi)))
1629 vtyp = IFTOVT(((struct fuse_attr_out *)fdi.answ)->attr.mode);
1631 if (vnode_vtype(vp) != vtyp) {
1632 if (vnode_vtype(vp) == VNON && vtyp != VNON) {
1633 SDT_PROBE2(fuse, , vnops, trace, 1, "FUSE: Dang! "
1634 "vnode_vtype is VNON and vtype isn't.");
1637 * STALE vnode, ditch
1639 * The vnode has changed its type "behind our back".
1640 * There's nothing really we can do, so let us just
1641 * force an internal revocation and tell the caller to
1642 * try again, if interested.
1644 fuse_internal_vnode_disappear(vp);
1649 struct fuse_attr_out *fao = (struct fuse_attr_out*)fdi.answ;
1650 fuse_internal_cache_attrs(vp, &fao->attr, fao->attr_valid,
1651 fao->attr_valid_nsec, NULL);
1655 fdisp_destroy(&fdi);
1656 if (!err && sizechanged) {
1657 fuse_vnode_setsize(vp, newsize);
1658 VTOFUD(vp)->flag &= ~FN_SIZECHANGE;
1664 struct vnop_strategy_args {
1670 fuse_vnop_strategy(struct vop_strategy_args *ap)
1672 struct vnode *vp = ap->a_vp;
1673 struct buf *bp = ap->a_bp;
1675 if (!vp || fuse_isdeadfs(vp)) {
1676 bp->b_ioflags |= BIO_ERROR;
1677 bp->b_error = ENXIO;
1681 if (bp->b_iocmd == BIO_WRITE)
1682 fuse_vnode_refreshsize(vp, NOCRED);
1684 (void)fuse_io_strategy(vp, bp);
1687 * This is a dangerous function. If returns error, that might mean a
1688 * panic. We prefer pretty much anything over being forced to panic
1689 * by a malicious daemon (a demon?). So we just return 0 anyway. You
1690 * should never mind this: this function has its own error
1691 * propagation mechanism via the argument buffer, so
1692 * not-that-melodramatic residents of the call chain still will be
1693 * able to know what to do.
1700 struct vnop_symlink_args {
1701 struct vnode *a_dvp;
1702 struct vnode **a_vpp;
1703 struct componentname *a_cnp;
1704 struct vattr *a_vap;
1709 fuse_vnop_symlink(struct vop_symlink_args *ap)
1711 struct vnode *dvp = ap->a_dvp;
1712 struct vnode **vpp = ap->a_vpp;
1713 struct componentname *cnp = ap->a_cnp;
1714 const char *target = ap->a_target;
1716 struct fuse_dispatcher fdi;
1721 if (fuse_isdeadfs(dvp)) {
1725 * Unlike the other creator type calls, here we have to create a message
1726 * where the name of the new entry comes first, and the data describing
1727 * the entry comes second.
1728 * Hence we can't rely on our handy fuse_internal_newentry() routine,
1729 * but put together the message manually and just call the core part.
1732 len = strlen(target) + 1;
1733 fdisp_init(&fdi, len + cnp->cn_namelen + 1);
1734 fdisp_make_vp(&fdi, FUSE_SYMLINK, dvp, curthread, NULL);
1736 memcpy(fdi.indata, cnp->cn_nameptr, cnp->cn_namelen);
1737 ((char *)fdi.indata)[cnp->cn_namelen] = '\0';
1738 memcpy((char *)fdi.indata + cnp->cn_namelen + 1, target, len);
1740 err = fuse_internal_newentry_core(dvp, vpp, cnp, VLNK, &fdi);
1741 fdisp_destroy(&fdi);
1746 struct vnop_write_args {
1750 struct ucred *a_cred;
1754 fuse_vnop_write(struct vop_write_args *ap)
1756 struct vnode *vp = ap->a_vp;
1757 struct uio *uio = ap->a_uio;
1758 int ioflag = ap->a_ioflag;
1759 struct ucred *cred = ap->a_cred;
1761 if (fuse_isdeadfs(vp)) {
1764 fuse_vnode_refreshsize(vp, cred);
1766 if (VTOFUD(vp)->flag & FN_DIRECTIO) {
1767 ioflag |= IO_DIRECT;
1770 return fuse_io_dispatch(vp, uio, ioflag, cred);
1773 SDT_PROBE_DEFINE1(fuse, , vnops, vnop_getpages_error, "int");
1775 struct vnop_getpages_args {
1783 fuse_vnop_getpages(struct vop_getpages_args *ap)
1785 int i, error, nextoff, size, toff, count, npages;
1796 KASSERT(vp->v_object, ("objectless vp passed to getpages"));
1797 td = curthread; /* XXX */
1798 cred = curthread->td_ucred; /* XXX */
1800 npages = ap->a_count;
1802 if (!fsess_opt_mmap(vnode_mount(vp))) {
1803 SDT_PROBE2(fuse, , vnops, trace, 1,
1804 "called on non-cacheable vnode??\n");
1805 return (VM_PAGER_ERROR);
1809 * If the last page is partially valid, just return it and allow
1810 * the pager to zero-out the blanks. Partially valid pages can
1811 * only occur at the file EOF.
1813 * XXXGL: is that true for FUSE, which is a local filesystem,
1814 * but still somewhat disconnected from the kernel?
1816 VM_OBJECT_WLOCK(vp->v_object);
1817 if (pages[npages - 1]->valid != 0 && --npages == 0)
1819 VM_OBJECT_WUNLOCK(vp->v_object);
1822 * We use only the kva address for the buffer, but this is extremely
1823 * convenient and fast.
1825 bp = uma_zalloc(fuse_pbuf_zone, M_WAITOK);
1827 kva = (vm_offset_t)bp->b_data;
1828 pmap_qenter(kva, pages, npages);
1829 VM_CNT_INC(v_vnodein);
1830 VM_CNT_ADD(v_vnodepgsin, npages);
1832 count = npages << PAGE_SHIFT;
1833 iov.iov_base = (caddr_t)kva;
1834 iov.iov_len = count;
1837 uio.uio_offset = IDX_TO_OFF(pages[0]->pindex);
1838 uio.uio_resid = count;
1839 uio.uio_segflg = UIO_SYSSPACE;
1840 uio.uio_rw = UIO_READ;
1843 error = fuse_io_dispatch(vp, &uio, IO_DIRECT, cred);
1844 pmap_qremove(kva, npages);
1846 uma_zfree(fuse_pbuf_zone, bp);
1848 if (error && (uio.uio_resid == count)) {
1849 SDT_PROBE1(fuse, , vnops, vnop_getpages_error, error);
1850 return VM_PAGER_ERROR;
1853 * Calculate the number of bytes read and validate only that number
1854 * of bytes. Note that due to pending writes, size may be 0. This
1855 * does not mean that the remaining data is invalid!
1858 size = count - uio.uio_resid;
1859 VM_OBJECT_WLOCK(vp->v_object);
1860 fuse_vm_page_lock_queues();
1861 for (i = 0, toff = 0; i < npages; i++, toff = nextoff) {
1864 nextoff = toff + PAGE_SIZE;
1867 if (nextoff <= size) {
1869 * Read operation filled an entire page
1871 m->valid = VM_PAGE_BITS_ALL;
1872 KASSERT(m->dirty == 0,
1873 ("fuse_getpages: page %p is dirty", m));
1874 } else if (size > toff) {
1876 * Read operation filled a partial page.
1879 vm_page_set_valid_range(m, 0, size - toff);
1880 KASSERT(m->dirty == 0,
1881 ("fuse_getpages: page %p is dirty", m));
1884 * Read operation was short. If no error occurred
1885 * we may have hit a zero-fill section. We simply
1886 * leave valid set to 0.
1891 fuse_vm_page_unlock_queues();
1893 VM_OBJECT_WUNLOCK(vp->v_object);
1898 return (VM_PAGER_OK);
1902 struct vnop_putpages_args {
1908 vm_ooffset_t a_offset;
1912 fuse_vnop_putpages(struct vop_putpages_args *ap)
1918 int i, error, npages, count;
1928 KASSERT(vp->v_object, ("objectless vp passed to putpages"));
1929 fsize = vp->v_object->un_pager.vnp.vnp_size;
1930 td = curthread; /* XXX */
1931 cred = curthread->td_ucred; /* XXX */
1933 count = ap->a_count;
1934 rtvals = ap->a_rtvals;
1935 npages = btoc(count);
1936 offset = IDX_TO_OFF(pages[0]->pindex);
1938 if (!fsess_opt_mmap(vnode_mount(vp))) {
1939 SDT_PROBE2(fuse, , vnops, trace, 1,
1940 "called on non-cacheable vnode??\n");
1942 for (i = 0; i < npages; i++)
1943 rtvals[i] = VM_PAGER_AGAIN;
1946 * When putting pages, do not extend file past EOF.
1949 if (offset + count > fsize) {
1950 count = fsize - offset;
1955 * We use only the kva address for the buffer, but this is extremely
1956 * convenient and fast.
1958 bp = uma_zalloc(fuse_pbuf_zone, M_WAITOK);
1960 kva = (vm_offset_t)bp->b_data;
1961 pmap_qenter(kva, pages, npages);
1962 VM_CNT_INC(v_vnodeout);
1963 VM_CNT_ADD(v_vnodepgsout, count);
1965 iov.iov_base = (caddr_t)kva;
1966 iov.iov_len = count;
1969 uio.uio_offset = offset;
1970 uio.uio_resid = count;
1971 uio.uio_segflg = UIO_SYSSPACE;
1972 uio.uio_rw = UIO_WRITE;
1975 error = fuse_io_dispatch(vp, &uio, IO_DIRECT, cred);
1977 pmap_qremove(kva, npages);
1978 uma_zfree(fuse_pbuf_zone, bp);
1981 int nwritten = round_page(count - uio.uio_resid) / PAGE_SIZE;
1983 for (i = 0; i < nwritten; i++) {
1984 rtvals[i] = VM_PAGER_OK;
1985 VM_OBJECT_WLOCK(pages[i]->object);
1986 vm_page_undirty(pages[i]);
1987 VM_OBJECT_WUNLOCK(pages[i]->object);
1993 static const char extattr_namespace_separator = '.';
1996 struct vop_getextattr_args {
1997 struct vop_generic_args a_gen;
1999 int a_attrnamespace;
2003 struct ucred *a_cred;
2004 struct thread *a_td;
2008 fuse_vnop_getextattr(struct vop_getextattr_args *ap)
2010 struct vnode *vp = ap->a_vp;
2011 struct uio *uio = ap->a_uio;
2012 struct fuse_dispatcher fdi;
2013 struct fuse_getxattr_in *get_xattr_in;
2014 struct fuse_getxattr_out *get_xattr_out;
2015 struct mount *mp = vnode_mount(vp);
2016 struct thread *td = ap->a_td;
2017 struct ucred *cred = ap->a_cred;
2023 if (fuse_isdeadfs(vp))
2026 /* Default to looking for user attributes. */
2027 if (ap->a_attrnamespace == EXTATTR_NAMESPACE_SYSTEM)
2028 prefix = EXTATTR_NAMESPACE_SYSTEM_STRING;
2030 prefix = EXTATTR_NAMESPACE_USER_STRING;
2032 len = strlen(prefix) + sizeof(extattr_namespace_separator) +
2033 strlen(ap->a_name) + 1;
2035 fdisp_init(&fdi, len + sizeof(*get_xattr_in));
2036 fdisp_make_vp(&fdi, FUSE_GETXATTR, vp, td, cred);
2038 get_xattr_in = fdi.indata;
2040 * Check to see whether we're querying the available size or
2041 * issuing the actual request. If we pass in 0, we get back struct
2042 * fuse_getxattr_out. If we pass in a non-zero size, we get back
2043 * that much data, without the struct fuse_getxattr_out header.
2046 get_xattr_in->size = 0;
2048 get_xattr_in->size = uio->uio_resid;
2050 attr_str = (char *)fdi.indata + sizeof(*get_xattr_in);
2051 snprintf(attr_str, len, "%s%c%s", prefix, extattr_namespace_separator,
2054 err = fdisp_wait_answ(&fdi);
2057 fsess_set_notimpl(mp, FUSE_GETXATTR);
2061 get_xattr_out = fdi.answ;
2063 if (ap->a_size != NULL)
2064 *ap->a_size = get_xattr_out->size;
2067 err = uiomove(fdi.answ, fdi.iosize, uio);
2070 fdisp_destroy(&fdi);
2075 struct vop_setextattr_args {
2076 struct vop_generic_args a_gen;
2078 int a_attrnamespace;
2081 struct ucred *a_cred;
2082 struct thread *a_td;
2086 fuse_vnop_setextattr(struct vop_setextattr_args *ap)
2088 struct vnode *vp = ap->a_vp;
2089 struct uio *uio = ap->a_uio;
2090 struct fuse_dispatcher fdi;
2091 struct fuse_setxattr_in *set_xattr_in;
2092 struct mount *mp = vnode_mount(vp);
2093 struct thread *td = ap->a_td;
2094 struct ucred *cred = ap->a_cred;
2100 if (fuse_isdeadfs(vp))
2103 /* Default to looking for user attributes. */
2104 if (ap->a_attrnamespace == EXTATTR_NAMESPACE_SYSTEM)
2105 prefix = EXTATTR_NAMESPACE_SYSTEM_STRING;
2107 prefix = EXTATTR_NAMESPACE_USER_STRING;
2109 len = strlen(prefix) + sizeof(extattr_namespace_separator) +
2110 strlen(ap->a_name) + 1;
2112 fdisp_init(&fdi, len + sizeof(*set_xattr_in) + uio->uio_resid);
2113 fdisp_make_vp(&fdi, FUSE_SETXATTR, vp, td, cred);
2115 set_xattr_in = fdi.indata;
2116 set_xattr_in->size = uio->uio_resid;
2118 attr_str = (char *)fdi.indata + sizeof(*set_xattr_in);
2119 snprintf(attr_str, len, "%s%c%s", prefix, extattr_namespace_separator,
2122 err = uiomove((char *)fdi.indata + sizeof(*set_xattr_in) + len,
2123 uio->uio_resid, uio);
2128 err = fdisp_wait_answ(&fdi);
2132 fsess_set_notimpl(mp, FUSE_SETXATTR);
2137 fdisp_destroy(&fdi);
2142 * The Linux / FUSE extended attribute list is simply a collection of
2143 * NUL-terminated strings. The FreeBSD extended attribute list is a single
2144 * byte length followed by a non-NUL terminated string. So, this allows
2145 * conversion of the Linux / FUSE format to the FreeBSD format in place.
2146 * Linux attribute names are reported with the namespace as a prefix (e.g.
2147 * "user.attribute_name"), but in FreeBSD they are reported without the
2148 * namespace prefix (e.g. "attribute_name"). So, we're going from:
2150 * user.attr_name1\0user.attr_name2\0
2154 * <num>attr_name1<num>attr_name2
2156 * Where "<num>" is a single byte number of characters in the attribute name.
2159 * prefix - exattr namespace prefix string
2160 * list, list_len - input list with namespace prefixes
2161 * bsd_list, bsd_list_len - output list compatible with bsd vfs
2164 fuse_xattrlist_convert(char *prefix, const char *list, int list_len,
2165 char *bsd_list, int *bsd_list_len)
2167 int len, pos, dist_to_next, prefix_len;
2171 prefix_len = strlen(prefix);
2173 while (pos < list_len && list[pos] != '\0') {
2174 dist_to_next = strlen(&list[pos]) + 1;
2175 if (bcmp(&list[pos], prefix, prefix_len) == 0 &&
2176 list[pos + prefix_len] == extattr_namespace_separator) {
2177 len = dist_to_next -
2178 (prefix_len + sizeof(extattr_namespace_separator)) - 1;
2179 if (len >= EXTATTR_MAXNAMELEN)
2180 return (ENAMETOOLONG);
2182 bsd_list[*bsd_list_len] = len;
2183 memcpy(&bsd_list[*bsd_list_len + 1],
2184 &list[pos + prefix_len +
2185 sizeof(extattr_namespace_separator)], len);
2187 *bsd_list_len += len + 1;
2190 pos += dist_to_next;
2197 struct vop_listextattr_args {
2198 struct vop_generic_args a_gen;
2200 int a_attrnamespace;
2203 struct ucred *a_cred;
2204 struct thread *a_td;
2208 fuse_vnop_listextattr(struct vop_listextattr_args *ap)
2210 struct vnode *vp = ap->a_vp;
2211 struct uio *uio = ap->a_uio;
2212 struct fuse_dispatcher fdi;
2213 struct fuse_listxattr_in *list_xattr_in;
2214 struct fuse_listxattr_out *list_xattr_out;
2215 struct mount *mp = vnode_mount(vp);
2216 struct thread *td = ap->a_td;
2217 struct ucred *cred = ap->a_cred;
2221 char *bsd_list = NULL;
2227 if (fuse_isdeadfs(vp))
2231 * Add space for a NUL and the period separator if enabled.
2232 * Default to looking for user attributes.
2234 if (ap->a_attrnamespace == EXTATTR_NAMESPACE_SYSTEM)
2235 prefix = EXTATTR_NAMESPACE_SYSTEM_STRING;
2237 prefix = EXTATTR_NAMESPACE_USER_STRING;
2239 len = strlen(prefix) + sizeof(extattr_namespace_separator) + 1;
2241 fdisp_init(&fdi, sizeof(*list_xattr_in) + len);
2242 fdisp_make_vp(&fdi, FUSE_LISTXATTR, vp, td, cred);
2245 * Retrieve Linux / FUSE compatible list size.
2247 list_xattr_in = fdi.indata;
2248 list_xattr_in->size = 0;
2249 attr_str = (char *)fdi.indata + sizeof(*list_xattr_in);
2250 snprintf(attr_str, len, "%s%c", prefix, extattr_namespace_separator);
2252 err = fdisp_wait_answ(&fdi);
2255 fsess_set_notimpl(mp, FUSE_LISTXATTR);
2259 list_xattr_out = fdi.answ;
2260 linux_list_len = list_xattr_out->size;
2261 if (linux_list_len == 0) {
2262 if (ap->a_size != NULL)
2263 *ap->a_size = linux_list_len;
2268 * Retrieve Linux / FUSE compatible list values.
2270 fdisp_make_vp(&fdi, FUSE_LISTXATTR, vp, td, cred);
2271 list_xattr_in = fdi.indata;
2272 list_xattr_in->size = linux_list_len + sizeof(*list_xattr_out);
2273 attr_str = (char *)fdi.indata + sizeof(*list_xattr_in);
2274 snprintf(attr_str, len, "%s%c", prefix, extattr_namespace_separator);
2276 err = fdisp_wait_answ(&fdi);
2280 linux_list = fdi.answ;
2281 linux_list_len = fdi.iosize;
2284 * Retrieve the BSD compatible list values.
2285 * The Linux / FUSE attribute list format isn't the same
2286 * as FreeBSD's format. So we need to transform it into
2287 * FreeBSD's format before giving it to the user.
2289 bsd_list = malloc(linux_list_len, M_TEMP, M_WAITOK);
2290 err = fuse_xattrlist_convert(prefix, linux_list, linux_list_len,
2291 bsd_list, &bsd_list_len);
2295 if (ap->a_size != NULL)
2296 *ap->a_size = bsd_list_len;
2299 err = uiomove(bsd_list, bsd_list_len, uio);
2302 free(bsd_list, M_TEMP);
2303 fdisp_destroy(&fdi);
2308 struct vop_deleteextattr_args {
2309 struct vop_generic_args a_gen;
2311 int a_attrnamespace;
2313 struct ucred *a_cred;
2314 struct thread *a_td;
2318 fuse_vnop_deleteextattr(struct vop_deleteextattr_args *ap)
2320 struct vnode *vp = ap->a_vp;
2321 struct fuse_dispatcher fdi;
2322 struct mount *mp = vnode_mount(vp);
2323 struct thread *td = ap->a_td;
2324 struct ucred *cred = ap->a_cred;
2330 if (fuse_isdeadfs(vp))
2333 /* Default to looking for user attributes. */
2334 if (ap->a_attrnamespace == EXTATTR_NAMESPACE_SYSTEM)
2335 prefix = EXTATTR_NAMESPACE_SYSTEM_STRING;
2337 prefix = EXTATTR_NAMESPACE_USER_STRING;
2339 len = strlen(prefix) + sizeof(extattr_namespace_separator) +
2340 strlen(ap->a_name) + 1;
2342 fdisp_init(&fdi, len);
2343 fdisp_make_vp(&fdi, FUSE_REMOVEXATTR, vp, td, cred);
2345 attr_str = fdi.indata;
2346 snprintf(attr_str, len, "%s%c%s", prefix, extattr_namespace_separator,
2349 err = fdisp_wait_answ(&fdi);
2352 fsess_set_notimpl(mp, FUSE_REMOVEXATTR);
2355 fdisp_destroy(&fdi);
2360 struct vnop_print_args {
2365 fuse_vnop_print(struct vop_print_args *ap)
2367 struct fuse_vnode_data *fvdat = VTOFUD(ap->a_vp);
2369 printf("nodeid: %ju, parent nodeid: %ju, nlookup: %ju, flag: %#x\n",
2370 (uintmax_t)VTOILLU(ap->a_vp), (uintmax_t)fvdat->parent_nid,
2371 (uintmax_t)fvdat->nlookup,