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>
70 #include <sys/limits.h>
72 #include <sys/rwlock.h>
75 #include <sys/mount.h>
76 #include <sys/vnode.h>
77 #include <sys/namei.h>
78 #include <sys/extattr.h>
80 #include <sys/unistd.h>
81 #include <sys/filedesc.h>
83 #include <sys/fcntl.h>
84 #include <sys/dirent.h>
87 #include <sys/sysctl.h>
88 #include <sys/vmmeter.h>
91 #include <vm/vm_extern.h>
93 #include <vm/vm_map.h>
94 #include <vm/vm_page.h>
95 #include <vm/vm_param.h>
96 #include <vm/vm_object.h>
97 #include <vm/vm_pager.h>
98 #include <vm/vnode_pager.h>
99 #include <vm/vm_object.h>
102 #include "fuse_file.h"
103 #include "fuse_internal.h"
104 #include "fuse_ipc.h"
105 #include "fuse_node.h"
106 #include "fuse_param.h"
109 #include <sys/priv.h>
111 SDT_PROVIDER_DECLARE(fuse);
114 * arg0: verbosity. Higher numbers give more verbose messages
115 * arg1: Textual message
117 SDT_PROBE_DEFINE2(fuse, , vnops, trace, "int", "char*");
120 static vop_access_t fuse_vnop_access;
121 static vop_close_t fuse_vnop_close;
122 static vop_create_t fuse_vnop_create;
123 static vop_deleteextattr_t fuse_vnop_deleteextattr;
124 static vop_fsync_t fuse_vnop_fsync;
125 static vop_getattr_t fuse_vnop_getattr;
126 static vop_getextattr_t fuse_vnop_getextattr;
127 static vop_inactive_t fuse_vnop_inactive;
128 static vop_link_t fuse_vnop_link;
129 static vop_listextattr_t fuse_vnop_listextattr;
130 static vop_lookup_t fuse_vnop_lookup;
131 static vop_mkdir_t fuse_vnop_mkdir;
132 static vop_mknod_t fuse_vnop_mknod;
133 static vop_open_t fuse_vnop_open;
134 static vop_pathconf_t fuse_vnop_pathconf;
135 static vop_read_t fuse_vnop_read;
136 static vop_readdir_t fuse_vnop_readdir;
137 static vop_readlink_t fuse_vnop_readlink;
138 static vop_reclaim_t fuse_vnop_reclaim;
139 static vop_remove_t fuse_vnop_remove;
140 static vop_rename_t fuse_vnop_rename;
141 static vop_rmdir_t fuse_vnop_rmdir;
142 static vop_setattr_t fuse_vnop_setattr;
143 static vop_setextattr_t fuse_vnop_setextattr;
144 static vop_strategy_t fuse_vnop_strategy;
145 static vop_symlink_t fuse_vnop_symlink;
146 static vop_write_t fuse_vnop_write;
147 static vop_getpages_t fuse_vnop_getpages;
148 static vop_putpages_t fuse_vnop_putpages;
149 static vop_print_t fuse_vnop_print;
151 struct vop_vector fuse_vnops = {
152 .vop_default = &default_vnodeops,
153 .vop_access = fuse_vnop_access,
154 .vop_close = fuse_vnop_close,
155 .vop_create = fuse_vnop_create,
156 .vop_deleteextattr = fuse_vnop_deleteextattr,
157 .vop_fsync = fuse_vnop_fsync,
158 .vop_getattr = fuse_vnop_getattr,
159 .vop_getextattr = fuse_vnop_getextattr,
160 .vop_inactive = fuse_vnop_inactive,
161 .vop_link = fuse_vnop_link,
162 .vop_listextattr = fuse_vnop_listextattr,
163 .vop_lookup = fuse_vnop_lookup,
164 .vop_mkdir = fuse_vnop_mkdir,
165 .vop_mknod = fuse_vnop_mknod,
166 .vop_open = fuse_vnop_open,
167 .vop_pathconf = fuse_vnop_pathconf,
168 .vop_read = fuse_vnop_read,
169 .vop_readdir = fuse_vnop_readdir,
170 .vop_readlink = fuse_vnop_readlink,
171 .vop_reclaim = fuse_vnop_reclaim,
172 .vop_remove = fuse_vnop_remove,
173 .vop_rename = fuse_vnop_rename,
174 .vop_rmdir = fuse_vnop_rmdir,
175 .vop_setattr = fuse_vnop_setattr,
176 .vop_setextattr = fuse_vnop_setextattr,
177 .vop_strategy = fuse_vnop_strategy,
178 .vop_symlink = fuse_vnop_symlink,
179 .vop_write = fuse_vnop_write,
180 .vop_getpages = fuse_vnop_getpages,
181 .vop_putpages = fuse_vnop_putpages,
182 .vop_print = fuse_vnop_print,
185 static u_long fuse_lookup_cache_hits = 0;
187 SYSCTL_ULONG(_vfs_fusefs, OID_AUTO, lookup_cache_hits, CTLFLAG_RD,
188 &fuse_lookup_cache_hits, 0, "number of positive cache hits in lookup");
190 static u_long fuse_lookup_cache_misses = 0;
192 SYSCTL_ULONG(_vfs_fusefs, OID_AUTO, lookup_cache_misses, CTLFLAG_RD,
193 &fuse_lookup_cache_misses, 0, "number of cache misses in lookup");
195 int fuse_lookup_cache_enable = 1;
197 SYSCTL_INT(_vfs_fusefs, OID_AUTO, lookup_cache_enable, CTLFLAG_RW,
198 &fuse_lookup_cache_enable, 0, "if non-zero, enable lookup cache");
201 * XXX: This feature is highly experimental and can bring to instabilities,
202 * needs revisiting before to be enabled by default.
204 static int fuse_reclaim_revoked = 0;
206 SYSCTL_INT(_vfs_fusefs, OID_AUTO, reclaim_revoked, CTLFLAG_RW,
207 &fuse_reclaim_revoked, 0, "");
209 uma_zone_t fuse_pbuf_zone;
211 #define fuse_vm_page_lock(m) vm_page_lock((m));
212 #define fuse_vm_page_unlock(m) vm_page_unlock((m));
213 #define fuse_vm_page_lock_queues() ((void)0)
214 #define fuse_vm_page_unlock_queues() ((void)0)
217 struct vnop_access_args {
219 #if VOP_ACCESS_TAKES_ACCMODE_T
224 struct ucred *a_cred;
229 fuse_vnop_access(struct vop_access_args *ap)
231 struct vnode *vp = ap->a_vp;
232 int accmode = ap->a_accmode;
233 struct ucred *cred = ap->a_cred;
235 struct fuse_access_param facp;
236 struct fuse_data *data = fuse_get_mpdata(vnode_mount(vp));
240 if (fuse_isdeadfs(vp)) {
241 if (vnode_isvroot(vp)) {
246 if (!(data->dataflags & FSESS_INITED)) {
247 if (vnode_isvroot(vp)) {
248 if (priv_check_cred(cred, PRIV_VFS_ADMIN) ||
249 (fuse_match_cred(data->daemoncred, cred) == 0)) {
255 if (vnode_islnk(vp)) {
258 bzero(&facp, sizeof(facp));
260 err = fuse_internal_access(vp, accmode, &facp, ap->a_td, ap->a_cred);
265 struct vnop_close_args {
268 struct ucred *a_cred;
273 fuse_vnop_close(struct vop_close_args *ap)
275 struct vnode *vp = ap->a_vp;
276 struct ucred *cred = ap->a_cred;
277 int fflag = ap->a_fflag;
278 fufh_type_t fufh_type;
280 if (fuse_isdeadfs(vp)) {
283 if (vnode_isdir(vp)) {
284 if (fuse_filehandle_valid(vp, FUFH_RDONLY)) {
285 fuse_filehandle_close(vp, FUFH_RDONLY, NULL, cred);
289 if (fflag & IO_NDELAY) {
292 fufh_type = fuse_filehandle_xlate_from_fflags(fflag);
294 if (!fuse_filehandle_valid(vp, fufh_type)) {
297 for (i = 0; i < FUFH_MAXTYPE; i++)
298 if (fuse_filehandle_valid(vp, i))
300 if (i == FUFH_MAXTYPE)
301 panic("FUSE: fufh type %d found to be invalid in close"
305 if ((VTOFUD(vp)->flag & FN_SIZECHANGE) != 0) {
306 fuse_vnode_savesize(vp, cred);
312 struct vnop_create_args {
314 struct vnode **a_vpp;
315 struct componentname *a_cnp;
320 fuse_vnop_create(struct vop_create_args *ap)
322 struct vnode *dvp = ap->a_dvp;
323 struct vnode **vpp = ap->a_vpp;
324 struct componentname *cnp = ap->a_cnp;
325 struct vattr *vap = ap->a_vap;
326 struct thread *td = cnp->cn_thread;
327 struct ucred *cred = cnp->cn_cred;
329 struct fuse_open_in *foi;
330 struct fuse_entry_out *feo;
331 struct fuse_dispatcher fdi;
332 struct fuse_dispatcher *fdip = &fdi;
336 struct mount *mp = vnode_mount(dvp);
337 uint64_t parentnid = VTOFUD(dvp)->nid;
338 mode_t mode = MAKEIMODE(vap->va_type, vap->va_mode);
340 uint32_t x_open_flags;
342 if (fuse_isdeadfs(dvp)) {
345 bzero(&fdi, sizeof(fdi));
347 /* XXX: Will we ever want devices ? */
348 if ((vap->va_type != VREG)) {
349 printf("fuse_vnop_create: unsupported va_type %d\n",
354 fdisp_init(fdip, sizeof(*foi) + cnp->cn_namelen + 1);
355 if (!fsess_isimpl(mp, FUSE_CREATE)) {
356 SDT_PROBE2(fuse, , vnops, trace, 1,
357 "eh, daemon doesn't implement create?");
360 fdisp_make(fdip, FUSE_CREATE, vnode_mount(dvp), parentnid, td, cred);
364 foi->flags = O_CREAT | O_RDWR;
366 memcpy((char *)fdip->indata + sizeof(*foi), cnp->cn_nameptr,
368 ((char *)fdip->indata)[sizeof(*foi) + cnp->cn_namelen] = '\0';
370 err = fdisp_wait_answ(fdip);
374 fsess_set_notimpl(mp, FUSE_CREATE);
380 if ((err = fuse_internal_checkentry(feo, VREG))) {
383 err = fuse_vnode_get(mp, feo, feo->nodeid, dvp, vpp, cnp, VREG);
385 struct fuse_release_in *fri;
386 uint64_t nodeid = feo->nodeid;
387 uint64_t fh_id = ((struct fuse_open_out *)(feo + 1))->fh;
389 fdisp_init(fdip, sizeof(*fri));
390 fdisp_make(fdip, FUSE_RELEASE, mp, nodeid, td, cred);
393 fri->flags = OFLAGS(mode);
394 fuse_insert_callback(fdip->tick, fuse_internal_forget_callback);
395 fuse_insert_message(fdip->tick);
398 ASSERT_VOP_ELOCKED(*vpp, "fuse_vnop_create");
400 fdip->answ = feo + 1;
402 x_fh_id = ((struct fuse_open_out *)(feo + 1))->fh;
403 x_open_flags = ((struct fuse_open_out *)(feo + 1))->open_flags;
404 fuse_filehandle_init(*vpp, FUFH_RDWR, NULL, x_fh_id);
405 fuse_vnode_open(*vpp, x_open_flags, td);
406 cache_purge_negative(dvp);
414 * Our vnop_fsync roughly corresponds to the FUSE_FSYNC method. The Linux
415 * version of FUSE also has a FUSE_FLUSH method.
417 * On Linux, fsync() synchronizes a file's complete in-core state with that
418 * on disk. The call is not supposed to return until the system has completed
419 * that action or until an error is detected.
421 * Linux also has an fdatasync() call that is similar to fsync() but is not
422 * required to update the metadata such as access time and modification time.
426 struct vnop_fsync_args {
427 struct vnodeop_desc *a_desc;
429 struct ucred * a_cred;
431 struct thread * a_td;
435 fuse_vnop_fsync(struct vop_fsync_args *ap)
437 struct vnode *vp = ap->a_vp;
438 struct thread *td = ap->a_td;
440 struct fuse_filehandle *fufh;
441 struct fuse_vnode_data *fvdat = VTOFUD(vp);
445 if (fuse_isdeadfs(vp)) {
448 if ((err = vop_stdfsync(ap)))
451 if (!fsess_isimpl(vnode_mount(vp),
452 (vnode_vtype(vp) == VDIR ? FUSE_FSYNCDIR : FUSE_FSYNC))) {
455 for (type = 0; type < FUFH_MAXTYPE; type++) {
456 fufh = &(fvdat->fufh[type]);
457 if (FUFH_IS_VALID(fufh)) {
458 fuse_internal_fsync(vp, td, NULL, fufh);
467 struct vnop_getattr_args {
470 struct ucred *a_cred;
475 fuse_vnop_getattr(struct vop_getattr_args *ap)
477 struct vnode *vp = ap->a_vp;
478 struct vattr *vap = ap->a_vap;
479 struct ucred *cred = ap->a_cred;
480 struct thread *td = curthread;
481 struct fuse_vnode_data *fvdat = VTOFUD(vp);
482 struct fuse_attr_out *fao;
486 struct fuse_dispatcher fdi;
488 dataflags = fuse_get_mpdata(vnode_mount(vp))->dataflags;
490 /* Note that we are not bailing out on a dead file system just yet. */
492 if (!(dataflags & FSESS_INITED)) {
493 if (!vnode_isvroot(vp)) {
494 fdata_set_dead(fuse_get_mpdata(vnode_mount(vp)));
502 if ((err = fdisp_simple_putget_vp(&fdi, FUSE_GETATTR, vp, td, cred))) {
503 if ((err == ENOTCONN) && vnode_isvroot(vp)) {
504 /* see comment in fuse_vfsop_statfs() */
509 fuse_internal_vnode_disappear(vp);
514 fao = (struct fuse_attr_out *)fdi.answ;
515 fuse_internal_cache_attrs(vp, &fao->attr, fao->attr_valid,
516 fao->attr_valid_nsec, vap);
517 if (vap->va_type != vnode_vtype(vp)) {
518 fuse_internal_vnode_disappear(vp);
522 if ((fvdat->flag & FN_SIZECHANGE) != 0)
523 vap->va_size = fvdat->filesize;
525 if (vnode_isreg(vp) && (fvdat->flag & FN_SIZECHANGE) == 0) {
527 * This is for those cases when the file size changed without us
528 * knowing, and we want to catch up.
530 off_t new_filesize = ((struct fuse_attr_out *)
531 fdi.answ)->attr.size;
533 if (fvdat->filesize != new_filesize) {
534 fuse_vnode_setsize(vp, new_filesize);
535 fvdat->flag &= ~FN_SIZECHANGE;
544 bzero(vap, sizeof(*vap));
545 vap->va_type = vnode_vtype(vp);
551 struct vnop_inactive_args {
557 fuse_vnop_inactive(struct vop_inactive_args *ap)
559 struct vnode *vp = ap->a_vp;
560 struct thread *td = ap->a_td;
562 struct fuse_vnode_data *fvdat = VTOFUD(vp);
563 struct fuse_filehandle *fufh = NULL;
565 int type, need_flush = 1;
567 for (type = 0; type < FUFH_MAXTYPE; type++) {
568 fufh = &(fvdat->fufh[type]);
569 if (FUFH_IS_VALID(fufh)) {
570 if (need_flush && vp->v_type == VREG) {
571 if ((VTOFUD(vp)->flag & FN_SIZECHANGE) != 0) {
572 fuse_vnode_savesize(vp, NULL);
574 if (fuse_data_cache_invalidate ||
575 (fvdat->flag & FN_REVOKED) != 0)
576 fuse_io_invalbuf(vp, td);
578 fuse_io_flushbuf(vp, MNT_WAIT, td);
581 fuse_filehandle_close(vp, type, td, NULL);
585 if ((fvdat->flag & FN_REVOKED) != 0 && fuse_reclaim_revoked) {
592 struct vnop_link_args {
593 struct vnode *a_tdvp;
595 struct componentname *a_cnp;
599 fuse_vnop_link(struct vop_link_args *ap)
601 struct vnode *vp = ap->a_vp;
602 struct vnode *tdvp = ap->a_tdvp;
603 struct componentname *cnp = ap->a_cnp;
605 struct vattr *vap = VTOVA(vp);
607 struct fuse_dispatcher fdi;
608 struct fuse_entry_out *feo;
609 struct fuse_link_in fli;
613 if (fuse_isdeadfs(vp)) {
616 if (vnode_mount(tdvp) != vnode_mount(vp)) {
621 * This is a seatbelt check to protect naive userspace filesystems from
622 * themselves and the limitations of the FUSE IPC protocol. If a
623 * filesystem does not allow attribute caching, assume it is capable of
624 * validating that nlink does not overflow.
626 if (vap != NULL && vap->va_nlink >= FUSE_LINK_MAX)
628 fli.oldnodeid = VTOI(vp);
631 fuse_internal_newentry_makerequest(vnode_mount(tdvp), VTOI(tdvp), cnp,
632 FUSE_LINK, &fli, sizeof(fli), &fdi);
633 if ((err = fdisp_wait_answ(&fdi))) {
638 err = fuse_internal_checkentry(feo, vnode_vtype(vp));
645 struct vnop_lookup_args {
646 struct vnodeop_desc *a_desc;
648 struct vnode **a_vpp;
649 struct componentname *a_cnp;
653 fuse_vnop_lookup(struct vop_lookup_args *ap)
655 struct vnode *dvp = ap->a_dvp;
656 struct vnode **vpp = ap->a_vpp;
657 struct componentname *cnp = ap->a_cnp;
658 struct thread *td = cnp->cn_thread;
659 struct ucred *cred = cnp->cn_cred;
661 int nameiop = cnp->cn_nameiop;
662 int flags = cnp->cn_flags;
663 int wantparent = flags & (LOCKPARENT | WANTPARENT);
664 int islastcn = flags & ISLASTCN;
665 struct mount *mp = vnode_mount(dvp);
669 struct vnode *vp = NULL;
671 struct fuse_dispatcher fdi;
675 struct fuse_access_param facp;
677 if (fuse_isdeadfs(dvp)) {
681 if (!vnode_isdir(dvp)) {
684 if (islastcn && vfs_isrdonly(mp) && (nameiop != LOOKUP)) {
688 * We do access check prior to doing anything else only in the case
689 * when we are at fs root (we'd like to say, "we are at the first
690 * component", but that's not exactly the same... nevermind).
691 * See further comments at further access checks.
694 bzero(&facp, sizeof(facp));
695 if (vnode_isvroot(dvp)) { /* early permission check hack */
696 if ((err = fuse_internal_access(dvp, VEXEC, &facp, td, cred))) {
700 if (flags & ISDOTDOT) {
701 nid = VTOFUD(dvp)->parent_nid;
708 } else if (cnp->cn_namelen == 1 && *(cnp->cn_nameptr) == '.') {
713 } else if (fuse_lookup_cache_enable) {
714 err = cache_lookup(dvp, vpp, cnp, NULL, NULL);
717 case -1: /* positive match */
718 atomic_add_acq_long(&fuse_lookup_cache_hits, 1);
721 case 0: /* no match in cache */
722 atomic_add_acq_long(&fuse_lookup_cache_misses, 1);
725 case ENOENT: /* negative match */
732 fdisp_init(&fdi, cnp->cn_namelen + 1);
736 fdisp_make(&fdi, op, mp, nid, td, cred);
738 if (op == FUSE_LOOKUP) {
739 memcpy(fdi.indata, cnp->cn_nameptr, cnp->cn_namelen);
740 ((char *)fdi.indata)[cnp->cn_namelen] = '\0';
742 lookup_err = fdisp_wait_answ(&fdi);
744 if ((op == FUSE_LOOKUP) && !lookup_err) { /* lookup call succeeded */
745 nid = ((struct fuse_entry_out *)fdi.answ)->nodeid;
748 * zero nodeid is the same as "not found",
749 * but it's also cacheable (which we keep
750 * keep on doing not as of writing this)
753 } else if (nid == FUSE_ROOT_ID) {
758 (!fdi.answ_stat || lookup_err != ENOENT || op != FUSE_LOOKUP)) {
762 /* lookup_err, if non-zero, must be ENOENT at this point */
766 if ((nameiop == CREATE || nameiop == RENAME) && islastcn
767 /* && directory dvp has not been removed */ ) {
769 if (vfs_isrdonly(mp)) {
773 #if 0 /* THINK_ABOUT_THIS */
774 if ((err = fuse_internal_access(dvp, VWRITE, cred, td, &facp))) {
780 * Possibly record the position of a slot in the
781 * directory large enough for the new component name.
782 * This can be recorded in the vnode private data for
783 * dvp. Set the SAVENAME flag to hold onto the
784 * pathname for use later in VOP_CREATE or VOP_RENAME.
786 cnp->cn_flags |= SAVENAME;
791 /* Consider inserting name into cache. */
794 * No we can't use negative caching, as the fs
795 * changes are out of our control.
796 * False positives' falseness turns out just as things
797 * go by, but false negatives' falseness doesn't.
798 * (and aiding the caching mechanism with extra control
799 * mechanisms comes quite close to beating the whole purpose
803 if ((cnp->cn_flags & MAKEENTRY) != 0) {
804 SDT_PROBE2(fuse, , vnops, trace, 1,
805 "inserting NULL into cache");
806 cache_enter(dvp, NULL, cnp);
816 struct fuse_entry_out *feo = NULL;
817 struct fuse_attr *fattr = NULL;
819 if (op == FUSE_GETATTR) {
820 fattr = &((struct fuse_attr_out *)fdi.answ)->attr;
822 feo = (struct fuse_entry_out *)fdi.answ;
823 fattr = &(feo->attr);
827 * If deleting, and at end of pathname, return parameters
828 * which can be used to remove file. If the wantparent flag
829 * isn't set, we return only the directory, otherwise we go on
830 * and lock the inode, being careful with ".".
832 if (nameiop == DELETE && islastcn) {
834 * Check for write access on directory.
836 facp.xuid = fattr->uid;
837 facp.facc_flags |= FACCESS_STICKY;
838 err = fuse_internal_access(dvp, VWRITE, &facp, td, cred);
839 facp.facc_flags &= ~FACCESS_XQUERIES;
844 if (nid == VTOI(dvp)) {
848 err = fuse_vnode_get(dvp->v_mount, feo, nid,
849 dvp, &vp, cnp, IFTOVT(fattr->mode));
856 * Save the name for use in VOP_RMDIR and VOP_REMOVE
859 cnp->cn_flags |= SAVENAME;
864 * If rewriting (RENAME), return the inode and the
865 * information required to rewrite the present directory
866 * Must get inode of directory entry to verify it's a
867 * regular file, or empty directory.
869 if (nameiop == RENAME && wantparent && islastcn) {
871 #if 0 /* THINK_ABOUT_THIS */
872 if ((err = fuse_internal_access(dvp, VWRITE, cred, td, &facp))) {
880 if (nid == VTOI(dvp)) {
884 err = fuse_vnode_get(vnode_mount(dvp), feo, nid, dvp,
885 &vp, cnp, IFTOVT(fattr->mode));
891 * Save the name for use in VOP_RENAME later.
893 cnp->cn_flags |= SAVENAME;
897 if (flags & ISDOTDOT) {
902 * Expanded copy of vn_vget_ino() so that
903 * fuse_vnode_get() can be used.
906 ltype = VOP_ISLOCKED(dvp);
907 err = vfs_busy(mp, MBF_NOWAIT);
911 err = vfs_busy(mp, 0);
912 vn_lock(dvp, ltype | LK_RETRY);
916 if ((dvp->v_iflag & VI_DOOMED) != 0) {
923 err = fuse_vnode_get(vnode_mount(dvp), feo, nid, NULL,
924 &vp, cnp, IFTOVT(fattr->mode));
926 vn_lock(dvp, ltype | LK_RETRY);
927 if ((dvp->v_iflag & VI_DOOMED) != 0) {
935 } else if (nid == VTOI(dvp)) {
939 struct fuse_vnode_data *fvdat;
941 err = fuse_vnode_get(vnode_mount(dvp), feo, nid, dvp,
942 &vp, cnp, IFTOVT(fattr->mode));
946 fuse_vnode_setparent(vp, dvp);
949 * In the case where we are looking up a FUSE node
950 * represented by an existing cached vnode, and the
951 * true size reported by FUSE_LOOKUP doesn't match
952 * the vnode's cached size, fix the vnode cache to
953 * match the real object size.
955 * This can occur via FUSE distributed filesystems,
956 * irregular files, etc.
959 if (vnode_isreg(vp) &&
960 fattr->size != fvdat->filesize) {
962 * The FN_SIZECHANGE flag reflects a dirty
963 * append. If userspace lets us know our cache
964 * is invalid, that write was lost. (Dirty
965 * writes that do not cause append are also
966 * lost, but we don't detect them here.)
968 * XXX: Maybe disable WB caching on this mount.
970 if (fvdat->flag & FN_SIZECHANGE)
971 printf("%s: WB cache incoherent on "
973 vnode_mount(vp)->mnt_stat.f_mntonname);
975 (void)fuse_vnode_setsize(vp, fattr->size);
976 fvdat->flag &= ~FN_SIZECHANGE;
981 if (op == FUSE_GETATTR) {
982 struct fuse_attr_out *fao =
983 (struct fuse_attr_out*)fdi.answ;
984 fuse_internal_cache_attrs(*vpp,
985 &fao->attr, fao->attr_valid,
986 fao->attr_valid_nsec, NULL);
988 struct fuse_entry_out *feo =
989 (struct fuse_entry_out*)fdi.answ;
990 fuse_internal_cache_attrs(*vpp,
991 &feo->attr, feo->attr_valid,
992 feo->attr_valid_nsec, NULL);
995 /* Insert name into cache if appropriate. */
998 * Nooo, caching is evil. With caching, we can't avoid stale
999 * information taking over the playground (cached info is not
1000 * just positive/negative, it does have qualitative aspects,
1001 * too). And a (VOP/FUSE)_GETATTR is always thrown anyway, when
1002 * walking down along cached path components, and that's not
1003 * any cheaper than FUSE_LOOKUP. This might change with
1004 * implementing kernel side attr caching, but... In Linux,
1005 * lookup results are not cached, and the daemon is bombarded
1006 * with FUSE_LOOKUPS on and on. This shows that by design, the
1007 * daemon is expected to handle frequent lookup queries
1008 * efficiently, do its caching in userspace, and so on.
1010 * So just leave the name cache alone.
1014 * Well, now I know, Linux caches lookups, but with a
1015 * timeout... So it's the same thing as attribute caching:
1016 * we can deal with it when implement timeouts.
1019 if (cnp->cn_flags & MAKEENTRY) {
1020 cache_enter(dvp, *vpp, cnp);
1027 /* No lookup error; need to clean up. */
1029 if (err) { /* Found inode; exit with no vnode. */
1030 if (op == FUSE_LOOKUP) {
1031 fuse_internal_forget_send(vnode_mount(dvp), td, cred,
1034 fdisp_destroy(&fdi);
1037 #ifndef NO_EARLY_PERM_CHECK_HACK
1040 * We have the attributes of the next item
1041 * *now*, and it's a fact, and we do not
1042 * have to do extra work for it (ie, beg the
1043 * daemon), and it neither depends on such
1044 * accidental things like attr caching. So
1045 * the big idea: check credentials *now*,
1046 * not at the beginning of the next call to
1049 * The first item of the lookup chain (fs root)
1050 * won't be checked then here, of course, as
1051 * its never "the next". But go and see that
1052 * the root is taken care about at the very
1053 * beginning of this function.
1055 * Now, given we want to do the access check
1056 * this way, one might ask: so then why not
1057 * do the access check just after fetching
1058 * the inode and its attributes from the
1059 * daemon? Why bother with producing the
1060 * corresponding vnode at all if something
1061 * is not OK? We know what's the deal as
1062 * soon as we get those attrs... There is
1063 * one bit of info though not given us by
1064 * the daemon: whether his response is
1065 * authoritative or not... His response should
1066 * be ignored if something is mounted over
1067 * the dir in question. But that can be
1068 * known only by having the vnode...
1070 int tmpvtype = vnode_vtype(*vpp);
1072 bzero(&facp, sizeof(facp));
1073 /*the early perm check hack */
1074 facp.facc_flags |= FACCESS_VA_VALID;
1076 if ((tmpvtype != VDIR) && (tmpvtype != VLNK)) {
1079 if (!err && !vnode_mountedhere(*vpp)) {
1080 err = fuse_internal_access(*vpp, VEXEC, &facp, td, cred);
1083 if (tmpvtype == VLNK)
1084 SDT_PROBE2(fuse, , vnops, trace,
1085 1, "weird, permission "
1086 "error with a symlink?");
1094 fdisp_destroy(&fdi);
1100 struct vnop_mkdir_args {
1101 struct vnode *a_dvp;
1102 struct vnode **a_vpp;
1103 struct componentname *a_cnp;
1104 struct vattr *a_vap;
1108 fuse_vnop_mkdir(struct vop_mkdir_args *ap)
1110 struct vnode *dvp = ap->a_dvp;
1111 struct vnode **vpp = ap->a_vpp;
1112 struct componentname *cnp = ap->a_cnp;
1113 struct vattr *vap = ap->a_vap;
1115 struct fuse_mkdir_in fmdi;
1117 if (fuse_isdeadfs(dvp)) {
1120 fmdi.mode = MAKEIMODE(vap->va_type, vap->va_mode);
1122 return (fuse_internal_newentry(dvp, vpp, cnp, FUSE_MKDIR, &fmdi,
1123 sizeof(fmdi), VDIR));
1127 struct vnop_mknod_args {
1128 struct vnode *a_dvp;
1129 struct vnode **a_vpp;
1130 struct componentname *a_cnp;
1131 struct vattr *a_vap;
1135 fuse_vnop_mknod(struct vop_mknod_args *ap)
1143 struct vnop_open_args {
1146 struct ucred *a_cred;
1147 struct thread *a_td;
1148 int a_fdidx; / struct file *a_fp;
1152 fuse_vnop_open(struct vop_open_args *ap)
1154 struct vnode *vp = ap->a_vp;
1155 int mode = ap->a_mode;
1156 struct thread *td = ap->a_td;
1157 struct ucred *cred = ap->a_cred;
1159 fufh_type_t fufh_type;
1160 struct fuse_vnode_data *fvdat;
1162 int error, isdir = 0;
1163 int32_t fuse_open_flags;
1165 if (fuse_isdeadfs(vp)) {
1168 if ((mode & (FREAD | FWRITE)) == 0)
1173 if (vnode_isdir(vp)) {
1176 fuse_open_flags = 0;
1178 fufh_type = FUFH_RDONLY;
1180 fufh_type = fuse_filehandle_xlate_from_fflags(mode);
1182 * For WRONLY opens, force DIRECT_IO. This is necessary
1183 * since writing a partial block through the buffer cache
1184 * will result in a read of the block and that read won't
1185 * be allowed by the WRONLY open.
1187 if (fufh_type == FUFH_WRONLY ||
1188 (fvdat->flag & FN_DIRECTIO) != 0)
1189 fuse_open_flags = FOPEN_DIRECT_IO;
1192 if (fuse_filehandle_validrw(vp, fufh_type) != FUFH_INVALID) {
1193 fuse_vnode_open(vp, fuse_open_flags, td);
1196 error = fuse_filehandle_open(vp, fufh_type, NULL, td, cred);
1202 fuse_vnop_pathconf(struct vop_pathconf_args *ap)
1205 switch (ap->a_name) {
1206 case _PC_FILESIZEBITS:
1210 *ap->a_retval = NAME_MAX;
1213 *ap->a_retval = MIN(LONG_MAX, FUSE_LINK_MAX);
1215 case _PC_SYMLINK_MAX:
1216 *ap->a_retval = MAXPATHLEN;
1222 return (vop_stdpathconf(ap));
1227 struct vnop_read_args {
1231 struct ucred *a_cred;
1235 fuse_vnop_read(struct vop_read_args *ap)
1237 struct vnode *vp = ap->a_vp;
1238 struct uio *uio = ap->a_uio;
1239 int ioflag = ap->a_ioflag;
1240 struct ucred *cred = ap->a_cred;
1242 if (fuse_isdeadfs(vp)) {
1246 if (VTOFUD(vp)->flag & FN_DIRECTIO) {
1247 ioflag |= IO_DIRECT;
1250 return fuse_io_dispatch(vp, uio, ioflag, cred);
1254 struct vnop_readdir_args {
1257 struct ucred *a_cred;
1264 fuse_vnop_readdir(struct vop_readdir_args *ap)
1266 struct vnode *vp = ap->a_vp;
1267 struct uio *uio = ap->a_uio;
1268 struct ucred *cred = ap->a_cred;
1270 struct fuse_filehandle *fufh = NULL;
1271 struct fuse_iov cookediov;
1276 if (fuse_isdeadfs(vp)) {
1279 if ( /* XXXIP ((uio_iovcnt(uio) > 1)) || */
1280 (uio_resid(uio) < sizeof(struct dirent))) {
1284 if (!fuse_filehandle_valid(vp, FUFH_RDONLY)) {
1285 SDT_PROBE2(fuse, , vnops, trace, 1,
1286 "calling readdir() before open()");
1287 err = fuse_filehandle_open(vp, FUFH_RDONLY, &fufh, NULL, cred);
1290 err = fuse_filehandle_get(vp, FUFH_RDONLY, &fufh);
1295 #define DIRCOOKEDSIZE FUSE_DIRENT_ALIGN(FUSE_NAME_OFFSET + MAXNAMLEN + 1)
1296 fiov_init(&cookediov, DIRCOOKEDSIZE);
1298 err = fuse_internal_readdir(vp, uio, fufh, &cookediov);
1300 fiov_teardown(&cookediov);
1302 fuse_filehandle_close(vp, FUFH_RDONLY, NULL, cred);
1308 struct vnop_readlink_args {
1311 struct ucred *a_cred;
1315 fuse_vnop_readlink(struct vop_readlink_args *ap)
1317 struct vnode *vp = ap->a_vp;
1318 struct uio *uio = ap->a_uio;
1319 struct ucred *cred = ap->a_cred;
1321 struct fuse_dispatcher fdi;
1324 if (fuse_isdeadfs(vp)) {
1327 if (!vnode_islnk(vp)) {
1330 fdisp_init(&fdi, 0);
1331 err = fdisp_simple_putget_vp(&fdi, FUSE_READLINK, vp, curthread, cred);
1335 if (((char *)fdi.answ)[0] == '/' &&
1336 fuse_get_mpdata(vnode_mount(vp))->dataflags & FSESS_PUSH_SYMLINKS_IN) {
1337 char *mpth = vnode_mount(vp)->mnt_stat.f_mntonname;
1339 err = uiomove(mpth, strlen(mpth), uio);
1342 err = uiomove(fdi.answ, fdi.iosize, uio);
1345 fdisp_destroy(&fdi);
1350 struct vnop_reclaim_args {
1352 struct thread *a_td;
1356 fuse_vnop_reclaim(struct vop_reclaim_args *ap)
1358 struct vnode *vp = ap->a_vp;
1359 struct thread *td = ap->a_td;
1361 struct fuse_vnode_data *fvdat = VTOFUD(vp);
1362 struct fuse_filehandle *fufh = NULL;
1367 panic("FUSE: no vnode data during recycling");
1369 for (type = 0; type < FUFH_MAXTYPE; type++) {
1370 fufh = &(fvdat->fufh[type]);
1371 if (FUFH_IS_VALID(fufh)) {
1372 printf("FUSE: vnode being reclaimed but fufh (type=%d) is valid",
1374 fuse_filehandle_close(vp, type, td, NULL);
1378 if ((!fuse_isdeadfs(vp)) && (fvdat->nlookup)) {
1379 fuse_internal_forget_send(vnode_mount(vp), td, NULL, VTOI(vp),
1382 fuse_vnode_setparent(vp, NULL);
1384 vfs_hash_remove(vp);
1385 vnode_destroy_vobject(vp);
1386 fuse_vnode_destroy(vp);
1392 struct vnop_remove_args {
1393 struct vnode *a_dvp;
1395 struct componentname *a_cnp;
1399 fuse_vnop_remove(struct vop_remove_args *ap)
1401 struct vnode *dvp = ap->a_dvp;
1402 struct vnode *vp = ap->a_vp;
1403 struct componentname *cnp = ap->a_cnp;
1407 if (fuse_isdeadfs(vp)) {
1410 if (vnode_isdir(vp)) {
1415 err = fuse_internal_remove(dvp, vp, cnp, FUSE_UNLINK);
1418 fuse_internal_vnode_disappear(vp);
1423 struct vnop_rename_args {
1424 struct vnode *a_fdvp;
1425 struct vnode *a_fvp;
1426 struct componentname *a_fcnp;
1427 struct vnode *a_tdvp;
1428 struct vnode *a_tvp;
1429 struct componentname *a_tcnp;
1433 fuse_vnop_rename(struct vop_rename_args *ap)
1435 struct vnode *fdvp = ap->a_fdvp;
1436 struct vnode *fvp = ap->a_fvp;
1437 struct componentname *fcnp = ap->a_fcnp;
1438 struct vnode *tdvp = ap->a_tdvp;
1439 struct vnode *tvp = ap->a_tvp;
1440 struct componentname *tcnp = ap->a_tcnp;
1441 struct fuse_data *data;
1445 if (fuse_isdeadfs(fdvp)) {
1448 if (fvp->v_mount != tdvp->v_mount ||
1449 (tvp && fvp->v_mount != tvp->v_mount)) {
1450 SDT_PROBE2(fuse, , vnops, trace, 1, "cross-device rename");
1457 * FUSE library is expected to check if target directory is not
1458 * under the source directory in the file system tree.
1459 * Linux performs this check at VFS level.
1461 data = fuse_get_mpdata(vnode_mount(tdvp));
1462 sx_xlock(&data->rename_lock);
1463 err = fuse_internal_rename(fdvp, fcnp, tdvp, tcnp);
1466 fuse_vnode_setparent(fvp, tdvp);
1468 fuse_vnode_setparent(tvp, NULL);
1470 sx_unlock(&data->rename_lock);
1472 if (tvp != NULL && tvp != fvp) {
1475 if (vnode_isdir(fvp)) {
1476 if ((tvp != NULL) && vnode_isdir(tvp)) {
1497 struct vnop_rmdir_args {
1498 struct vnode *a_dvp;
1500 struct componentname *a_cnp;
1504 fuse_vnop_rmdir(struct vop_rmdir_args *ap)
1506 struct vnode *dvp = ap->a_dvp;
1507 struct vnode *vp = ap->a_vp;
1511 if (fuse_isdeadfs(vp)) {
1514 if (VTOFUD(vp) == VTOFUD(dvp)) {
1517 err = fuse_internal_remove(dvp, vp, ap->a_cnp, FUSE_RMDIR);
1520 fuse_internal_vnode_disappear(vp);
1525 struct vnop_setattr_args {
1527 struct vattr *a_vap;
1528 struct ucred *a_cred;
1529 struct thread *a_td;
1533 fuse_vnop_setattr(struct vop_setattr_args *ap)
1535 struct vnode *vp = ap->a_vp;
1536 struct vattr *vap = ap->a_vap;
1537 struct ucred *cred = ap->a_cred;
1538 struct thread *td = curthread;
1540 struct fuse_dispatcher fdi;
1541 struct fuse_setattr_in *fsai;
1542 struct fuse_access_param facp;
1546 int sizechanged = 0;
1547 uint64_t newsize = 0;
1549 if (fuse_isdeadfs(vp)) {
1552 fdisp_init(&fdi, sizeof(*fsai));
1553 fdisp_make_vp(&fdi, FUSE_SETATTR, vp, td, cred);
1557 bzero(&facp, sizeof(facp));
1559 facp.xuid = vap->va_uid;
1560 facp.xgid = vap->va_gid;
1562 if (vap->va_uid != (uid_t)VNOVAL) {
1563 facp.facc_flags |= FACCESS_CHOWN;
1564 fsai->uid = vap->va_uid;
1565 fsai->valid |= FATTR_UID;
1567 if (vap->va_gid != (gid_t)VNOVAL) {
1568 facp.facc_flags |= FACCESS_CHOWN;
1569 fsai->gid = vap->va_gid;
1570 fsai->valid |= FATTR_GID;
1572 if (vap->va_size != VNOVAL) {
1574 struct fuse_filehandle *fufh = NULL;
1576 /*Truncate to a new value. */
1577 fsai->size = vap->va_size;
1579 newsize = vap->va_size;
1580 fsai->valid |= FATTR_SIZE;
1582 fuse_filehandle_getrw(vp, FUFH_WRONLY, &fufh);
1584 fsai->fh = fufh->fh_id;
1585 fsai->valid |= FATTR_FH;
1588 if (vap->va_atime.tv_sec != VNOVAL) {
1589 fsai->atime = vap->va_atime.tv_sec;
1590 fsai->atimensec = vap->va_atime.tv_nsec;
1591 fsai->valid |= FATTR_ATIME;
1593 if (vap->va_mtime.tv_sec != VNOVAL) {
1594 fsai->mtime = vap->va_mtime.tv_sec;
1595 fsai->mtimensec = vap->va_mtime.tv_nsec;
1596 fsai->valid |= FATTR_MTIME;
1598 if (vap->va_mode != (mode_t)VNOVAL) {
1599 fsai->mode = vap->va_mode & ALLPERMS;
1600 fsai->valid |= FATTR_MODE;
1605 vtyp = vnode_vtype(vp);
1607 if (fsai->valid & FATTR_SIZE && vtyp == VDIR) {
1611 if (vfs_isrdonly(vnode_mount(vp)) && (fsai->valid & ~FATTR_SIZE || vtyp == VREG)) {
1615 if (fsai->valid & ~FATTR_SIZE) {
1616 /*err = fuse_internal_access(vp, VADMIN, context, &facp); */
1620 facp.facc_flags &= ~FACCESS_XQUERIES;
1622 if (err && !(fsai->valid & ~(FATTR_ATIME | FATTR_MTIME)) &&
1623 vap->va_vaflags & VA_UTIMES_NULL) {
1624 err = fuse_internal_access(vp, VWRITE, &facp, td, cred);
1628 if ((err = fdisp_wait_answ(&fdi)))
1630 vtyp = IFTOVT(((struct fuse_attr_out *)fdi.answ)->attr.mode);
1632 if (vnode_vtype(vp) != vtyp) {
1633 if (vnode_vtype(vp) == VNON && vtyp != VNON) {
1634 SDT_PROBE2(fuse, , vnops, trace, 1, "FUSE: Dang! "
1635 "vnode_vtype is VNON and vtype isn't.");
1638 * STALE vnode, ditch
1640 * The vnode has changed its type "behind our back".
1641 * There's nothing really we can do, so let us just
1642 * force an internal revocation and tell the caller to
1643 * try again, if interested.
1645 fuse_internal_vnode_disappear(vp);
1650 struct fuse_attr_out *fao = (struct fuse_attr_out*)fdi.answ;
1651 fuse_internal_cache_attrs(vp, &fao->attr, fao->attr_valid,
1652 fao->attr_valid_nsec, NULL);
1656 fdisp_destroy(&fdi);
1657 if (!err && sizechanged) {
1658 fuse_vnode_setsize(vp, newsize);
1659 VTOFUD(vp)->flag &= ~FN_SIZECHANGE;
1665 struct vnop_strategy_args {
1671 fuse_vnop_strategy(struct vop_strategy_args *ap)
1673 struct vnode *vp = ap->a_vp;
1674 struct buf *bp = ap->a_bp;
1676 if (!vp || fuse_isdeadfs(vp)) {
1677 bp->b_ioflags |= BIO_ERROR;
1678 bp->b_error = ENXIO;
1682 if (bp->b_iocmd == BIO_WRITE)
1683 fuse_vnode_refreshsize(vp, NOCRED);
1685 (void)fuse_io_strategy(vp, bp);
1688 * This is a dangerous function. If returns error, that might mean a
1689 * panic. We prefer pretty much anything over being forced to panic
1690 * by a malicious daemon (a demon?). So we just return 0 anyway. You
1691 * should never mind this: this function has its own error
1692 * propagation mechanism via the argument buffer, so
1693 * not-that-melodramatic residents of the call chain still will be
1694 * able to know what to do.
1701 struct vnop_symlink_args {
1702 struct vnode *a_dvp;
1703 struct vnode **a_vpp;
1704 struct componentname *a_cnp;
1705 struct vattr *a_vap;
1710 fuse_vnop_symlink(struct vop_symlink_args *ap)
1712 struct vnode *dvp = ap->a_dvp;
1713 struct vnode **vpp = ap->a_vpp;
1714 struct componentname *cnp = ap->a_cnp;
1715 const char *target = ap->a_target;
1717 struct fuse_dispatcher fdi;
1722 if (fuse_isdeadfs(dvp)) {
1726 * Unlike the other creator type calls, here we have to create a message
1727 * where the name of the new entry comes first, and the data describing
1728 * the entry comes second.
1729 * Hence we can't rely on our handy fuse_internal_newentry() routine,
1730 * but put together the message manually and just call the core part.
1733 len = strlen(target) + 1;
1734 fdisp_init(&fdi, len + cnp->cn_namelen + 1);
1735 fdisp_make_vp(&fdi, FUSE_SYMLINK, dvp, curthread, NULL);
1737 memcpy(fdi.indata, cnp->cn_nameptr, cnp->cn_namelen);
1738 ((char *)fdi.indata)[cnp->cn_namelen] = '\0';
1739 memcpy((char *)fdi.indata + cnp->cn_namelen + 1, target, len);
1741 err = fuse_internal_newentry_core(dvp, vpp, cnp, VLNK, &fdi);
1742 fdisp_destroy(&fdi);
1747 struct vnop_write_args {
1751 struct ucred *a_cred;
1755 fuse_vnop_write(struct vop_write_args *ap)
1757 struct vnode *vp = ap->a_vp;
1758 struct uio *uio = ap->a_uio;
1759 int ioflag = ap->a_ioflag;
1760 struct ucred *cred = ap->a_cred;
1762 if (fuse_isdeadfs(vp)) {
1765 fuse_vnode_refreshsize(vp, cred);
1767 if (VTOFUD(vp)->flag & FN_DIRECTIO) {
1768 ioflag |= IO_DIRECT;
1771 return fuse_io_dispatch(vp, uio, ioflag, cred);
1774 SDT_PROBE_DEFINE1(fuse, , vnops, vnop_getpages_error, "int");
1776 struct vnop_getpages_args {
1784 fuse_vnop_getpages(struct vop_getpages_args *ap)
1786 int i, error, nextoff, size, toff, count, npages;
1797 KASSERT(vp->v_object, ("objectless vp passed to getpages"));
1798 td = curthread; /* XXX */
1799 cred = curthread->td_ucred; /* XXX */
1801 npages = ap->a_count;
1803 if (!fsess_opt_mmap(vnode_mount(vp))) {
1804 SDT_PROBE2(fuse, , vnops, trace, 1,
1805 "called on non-cacheable vnode??\n");
1806 return (VM_PAGER_ERROR);
1810 * If the last page is partially valid, just return it and allow
1811 * the pager to zero-out the blanks. Partially valid pages can
1812 * only occur at the file EOF.
1814 * XXXGL: is that true for FUSE, which is a local filesystem,
1815 * but still somewhat disconnected from the kernel?
1817 VM_OBJECT_WLOCK(vp->v_object);
1818 if (pages[npages - 1]->valid != 0 && --npages == 0)
1820 VM_OBJECT_WUNLOCK(vp->v_object);
1823 * We use only the kva address for the buffer, but this is extremely
1824 * convenient and fast.
1826 bp = uma_zalloc(fuse_pbuf_zone, M_WAITOK);
1828 kva = (vm_offset_t)bp->b_data;
1829 pmap_qenter(kva, pages, npages);
1830 VM_CNT_INC(v_vnodein);
1831 VM_CNT_ADD(v_vnodepgsin, npages);
1833 count = npages << PAGE_SHIFT;
1834 iov.iov_base = (caddr_t)kva;
1835 iov.iov_len = count;
1838 uio.uio_offset = IDX_TO_OFF(pages[0]->pindex);
1839 uio.uio_resid = count;
1840 uio.uio_segflg = UIO_SYSSPACE;
1841 uio.uio_rw = UIO_READ;
1844 error = fuse_io_dispatch(vp, &uio, IO_DIRECT, cred);
1845 pmap_qremove(kva, npages);
1847 uma_zfree(fuse_pbuf_zone, bp);
1849 if (error && (uio.uio_resid == count)) {
1850 SDT_PROBE1(fuse, , vnops, vnop_getpages_error, error);
1851 return VM_PAGER_ERROR;
1854 * Calculate the number of bytes read and validate only that number
1855 * of bytes. Note that due to pending writes, size may be 0. This
1856 * does not mean that the remaining data is invalid!
1859 size = count - uio.uio_resid;
1860 VM_OBJECT_WLOCK(vp->v_object);
1861 fuse_vm_page_lock_queues();
1862 for (i = 0, toff = 0; i < npages; i++, toff = nextoff) {
1865 nextoff = toff + PAGE_SIZE;
1868 if (nextoff <= size) {
1870 * Read operation filled an entire page
1872 m->valid = VM_PAGE_BITS_ALL;
1873 KASSERT(m->dirty == 0,
1874 ("fuse_getpages: page %p is dirty", m));
1875 } else if (size > toff) {
1877 * Read operation filled a partial page.
1880 vm_page_set_valid_range(m, 0, size - toff);
1881 KASSERT(m->dirty == 0,
1882 ("fuse_getpages: page %p is dirty", m));
1885 * Read operation was short. If no error occurred
1886 * we may have hit a zero-fill section. We simply
1887 * leave valid set to 0.
1892 fuse_vm_page_unlock_queues();
1894 VM_OBJECT_WUNLOCK(vp->v_object);
1899 return (VM_PAGER_OK);
1903 struct vnop_putpages_args {
1909 vm_ooffset_t a_offset;
1913 fuse_vnop_putpages(struct vop_putpages_args *ap)
1919 int i, error, npages, count;
1929 KASSERT(vp->v_object, ("objectless vp passed to putpages"));
1930 fsize = vp->v_object->un_pager.vnp.vnp_size;
1931 td = curthread; /* XXX */
1932 cred = curthread->td_ucred; /* XXX */
1934 count = ap->a_count;
1935 rtvals = ap->a_rtvals;
1936 npages = btoc(count);
1937 offset = IDX_TO_OFF(pages[0]->pindex);
1939 if (!fsess_opt_mmap(vnode_mount(vp))) {
1940 SDT_PROBE2(fuse, , vnops, trace, 1,
1941 "called on non-cacheable vnode??\n");
1943 for (i = 0; i < npages; i++)
1944 rtvals[i] = VM_PAGER_AGAIN;
1947 * When putting pages, do not extend file past EOF.
1950 if (offset + count > fsize) {
1951 count = fsize - offset;
1956 * We use only the kva address for the buffer, but this is extremely
1957 * convenient and fast.
1959 bp = uma_zalloc(fuse_pbuf_zone, M_WAITOK);
1961 kva = (vm_offset_t)bp->b_data;
1962 pmap_qenter(kva, pages, npages);
1963 VM_CNT_INC(v_vnodeout);
1964 VM_CNT_ADD(v_vnodepgsout, count);
1966 iov.iov_base = (caddr_t)kva;
1967 iov.iov_len = count;
1970 uio.uio_offset = offset;
1971 uio.uio_resid = count;
1972 uio.uio_segflg = UIO_SYSSPACE;
1973 uio.uio_rw = UIO_WRITE;
1976 error = fuse_io_dispatch(vp, &uio, IO_DIRECT, cred);
1978 pmap_qremove(kva, npages);
1979 uma_zfree(fuse_pbuf_zone, bp);
1982 int nwritten = round_page(count - uio.uio_resid) / PAGE_SIZE;
1984 for (i = 0; i < nwritten; i++) {
1985 rtvals[i] = VM_PAGER_OK;
1986 VM_OBJECT_WLOCK(pages[i]->object);
1987 vm_page_undirty(pages[i]);
1988 VM_OBJECT_WUNLOCK(pages[i]->object);
1994 static const char extattr_namespace_separator = '.';
1997 struct vop_getextattr_args {
1998 struct vop_generic_args a_gen;
2000 int a_attrnamespace;
2004 struct ucred *a_cred;
2005 struct thread *a_td;
2009 fuse_vnop_getextattr(struct vop_getextattr_args *ap)
2011 struct vnode *vp = ap->a_vp;
2012 struct uio *uio = ap->a_uio;
2013 struct fuse_dispatcher fdi;
2014 struct fuse_getxattr_in *get_xattr_in;
2015 struct fuse_getxattr_out *get_xattr_out;
2016 struct mount *mp = vnode_mount(vp);
2017 struct thread *td = ap->a_td;
2018 struct ucred *cred = ap->a_cred;
2024 if (fuse_isdeadfs(vp))
2027 /* Default to looking for user attributes. */
2028 if (ap->a_attrnamespace == EXTATTR_NAMESPACE_SYSTEM)
2029 prefix = EXTATTR_NAMESPACE_SYSTEM_STRING;
2031 prefix = EXTATTR_NAMESPACE_USER_STRING;
2033 len = strlen(prefix) + sizeof(extattr_namespace_separator) +
2034 strlen(ap->a_name) + 1;
2036 fdisp_init(&fdi, len + sizeof(*get_xattr_in));
2037 fdisp_make_vp(&fdi, FUSE_GETXATTR, vp, td, cred);
2039 get_xattr_in = fdi.indata;
2041 * Check to see whether we're querying the available size or
2042 * issuing the actual request. If we pass in 0, we get back struct
2043 * fuse_getxattr_out. If we pass in a non-zero size, we get back
2044 * that much data, without the struct fuse_getxattr_out header.
2047 get_xattr_in->size = 0;
2049 get_xattr_in->size = uio->uio_resid;
2051 attr_str = (char *)fdi.indata + sizeof(*get_xattr_in);
2052 snprintf(attr_str, len, "%s%c%s", prefix, extattr_namespace_separator,
2055 err = fdisp_wait_answ(&fdi);
2058 fsess_set_notimpl(mp, FUSE_GETXATTR);
2062 get_xattr_out = fdi.answ;
2064 if (ap->a_size != NULL)
2065 *ap->a_size = get_xattr_out->size;
2068 err = uiomove(fdi.answ, fdi.iosize, uio);
2071 fdisp_destroy(&fdi);
2076 struct vop_setextattr_args {
2077 struct vop_generic_args a_gen;
2079 int a_attrnamespace;
2082 struct ucred *a_cred;
2083 struct thread *a_td;
2087 fuse_vnop_setextattr(struct vop_setextattr_args *ap)
2089 struct vnode *vp = ap->a_vp;
2090 struct uio *uio = ap->a_uio;
2091 struct fuse_dispatcher fdi;
2092 struct fuse_setxattr_in *set_xattr_in;
2093 struct mount *mp = vnode_mount(vp);
2094 struct thread *td = ap->a_td;
2095 struct ucred *cred = ap->a_cred;
2101 if (fuse_isdeadfs(vp))
2104 /* Default to looking for user attributes. */
2105 if (ap->a_attrnamespace == EXTATTR_NAMESPACE_SYSTEM)
2106 prefix = EXTATTR_NAMESPACE_SYSTEM_STRING;
2108 prefix = EXTATTR_NAMESPACE_USER_STRING;
2110 len = strlen(prefix) + sizeof(extattr_namespace_separator) +
2111 strlen(ap->a_name) + 1;
2113 fdisp_init(&fdi, len + sizeof(*set_xattr_in) + uio->uio_resid);
2114 fdisp_make_vp(&fdi, FUSE_SETXATTR, vp, td, cred);
2116 set_xattr_in = fdi.indata;
2117 set_xattr_in->size = uio->uio_resid;
2119 attr_str = (char *)fdi.indata + sizeof(*set_xattr_in);
2120 snprintf(attr_str, len, "%s%c%s", prefix, extattr_namespace_separator,
2123 err = uiomove((char *)fdi.indata + sizeof(*set_xattr_in) + len,
2124 uio->uio_resid, uio);
2129 err = fdisp_wait_answ(&fdi);
2133 fsess_set_notimpl(mp, FUSE_SETXATTR);
2138 fdisp_destroy(&fdi);
2143 * The Linux / FUSE extended attribute list is simply a collection of
2144 * NUL-terminated strings. The FreeBSD extended attribute list is a single
2145 * byte length followed by a non-NUL terminated string. So, this allows
2146 * conversion of the Linux / FUSE format to the FreeBSD format in place.
2147 * Linux attribute names are reported with the namespace as a prefix (e.g.
2148 * "user.attribute_name"), but in FreeBSD they are reported without the
2149 * namespace prefix (e.g. "attribute_name"). So, we're going from:
2151 * user.attr_name1\0user.attr_name2\0
2155 * <num>attr_name1<num>attr_name2
2157 * Where "<num>" is a single byte number of characters in the attribute name.
2160 * prefix - exattr namespace prefix string
2161 * list, list_len - input list with namespace prefixes
2162 * bsd_list, bsd_list_len - output list compatible with bsd vfs
2165 fuse_xattrlist_convert(char *prefix, const char *list, int list_len,
2166 char *bsd_list, int *bsd_list_len)
2168 int len, pos, dist_to_next, prefix_len;
2172 prefix_len = strlen(prefix);
2174 while (pos < list_len && list[pos] != '\0') {
2175 dist_to_next = strlen(&list[pos]) + 1;
2176 if (bcmp(&list[pos], prefix, prefix_len) == 0 &&
2177 list[pos + prefix_len] == extattr_namespace_separator) {
2178 len = dist_to_next -
2179 (prefix_len + sizeof(extattr_namespace_separator)) - 1;
2180 if (len >= EXTATTR_MAXNAMELEN)
2181 return (ENAMETOOLONG);
2183 bsd_list[*bsd_list_len] = len;
2184 memcpy(&bsd_list[*bsd_list_len + 1],
2185 &list[pos + prefix_len +
2186 sizeof(extattr_namespace_separator)], len);
2188 *bsd_list_len += len + 1;
2191 pos += dist_to_next;
2198 struct vop_listextattr_args {
2199 struct vop_generic_args a_gen;
2201 int a_attrnamespace;
2204 struct ucred *a_cred;
2205 struct thread *a_td;
2209 fuse_vnop_listextattr(struct vop_listextattr_args *ap)
2211 struct vnode *vp = ap->a_vp;
2212 struct uio *uio = ap->a_uio;
2213 struct fuse_dispatcher fdi;
2214 struct fuse_listxattr_in *list_xattr_in;
2215 struct fuse_listxattr_out *list_xattr_out;
2216 struct mount *mp = vnode_mount(vp);
2217 struct thread *td = ap->a_td;
2218 struct ucred *cred = ap->a_cred;
2222 char *bsd_list = NULL;
2228 if (fuse_isdeadfs(vp))
2232 * Add space for a NUL and the period separator if enabled.
2233 * Default to looking for user attributes.
2235 if (ap->a_attrnamespace == EXTATTR_NAMESPACE_SYSTEM)
2236 prefix = EXTATTR_NAMESPACE_SYSTEM_STRING;
2238 prefix = EXTATTR_NAMESPACE_USER_STRING;
2240 len = strlen(prefix) + sizeof(extattr_namespace_separator) + 1;
2242 fdisp_init(&fdi, sizeof(*list_xattr_in) + len);
2243 fdisp_make_vp(&fdi, FUSE_LISTXATTR, vp, td, cred);
2246 * Retrieve Linux / FUSE compatible list size.
2248 list_xattr_in = fdi.indata;
2249 list_xattr_in->size = 0;
2250 attr_str = (char *)fdi.indata + sizeof(*list_xattr_in);
2251 snprintf(attr_str, len, "%s%c", prefix, extattr_namespace_separator);
2253 err = fdisp_wait_answ(&fdi);
2256 fsess_set_notimpl(mp, FUSE_LISTXATTR);
2260 list_xattr_out = fdi.answ;
2261 linux_list_len = list_xattr_out->size;
2262 if (linux_list_len == 0) {
2263 if (ap->a_size != NULL)
2264 *ap->a_size = linux_list_len;
2269 * Retrieve Linux / FUSE compatible list values.
2271 fdisp_make_vp(&fdi, FUSE_LISTXATTR, vp, td, cred);
2272 list_xattr_in = fdi.indata;
2273 list_xattr_in->size = linux_list_len + sizeof(*list_xattr_out);
2274 attr_str = (char *)fdi.indata + sizeof(*list_xattr_in);
2275 snprintf(attr_str, len, "%s%c", prefix, extattr_namespace_separator);
2277 err = fdisp_wait_answ(&fdi);
2281 linux_list = fdi.answ;
2282 linux_list_len = fdi.iosize;
2285 * Retrieve the BSD compatible list values.
2286 * The Linux / FUSE attribute list format isn't the same
2287 * as FreeBSD's format. So we need to transform it into
2288 * FreeBSD's format before giving it to the user.
2290 bsd_list = malloc(linux_list_len, M_TEMP, M_WAITOK);
2291 err = fuse_xattrlist_convert(prefix, linux_list, linux_list_len,
2292 bsd_list, &bsd_list_len);
2296 if (ap->a_size != NULL)
2297 *ap->a_size = bsd_list_len;
2300 err = uiomove(bsd_list, bsd_list_len, uio);
2303 free(bsd_list, M_TEMP);
2304 fdisp_destroy(&fdi);
2309 struct vop_deleteextattr_args {
2310 struct vop_generic_args a_gen;
2312 int a_attrnamespace;
2314 struct ucred *a_cred;
2315 struct thread *a_td;
2319 fuse_vnop_deleteextattr(struct vop_deleteextattr_args *ap)
2321 struct vnode *vp = ap->a_vp;
2322 struct fuse_dispatcher fdi;
2323 struct mount *mp = vnode_mount(vp);
2324 struct thread *td = ap->a_td;
2325 struct ucred *cred = ap->a_cred;
2331 if (fuse_isdeadfs(vp))
2334 /* Default to looking for user attributes. */
2335 if (ap->a_attrnamespace == EXTATTR_NAMESPACE_SYSTEM)
2336 prefix = EXTATTR_NAMESPACE_SYSTEM_STRING;
2338 prefix = EXTATTR_NAMESPACE_USER_STRING;
2340 len = strlen(prefix) + sizeof(extattr_namespace_separator) +
2341 strlen(ap->a_name) + 1;
2343 fdisp_init(&fdi, len);
2344 fdisp_make_vp(&fdi, FUSE_REMOVEXATTR, vp, td, cred);
2346 attr_str = fdi.indata;
2347 snprintf(attr_str, len, "%s%c%s", prefix, extattr_namespace_separator,
2350 err = fdisp_wait_answ(&fdi);
2353 fsess_set_notimpl(mp, FUSE_REMOVEXATTR);
2356 fdisp_destroy(&fdi);
2361 struct vnop_print_args {
2366 fuse_vnop_print(struct vop_print_args *ap)
2368 struct fuse_vnode_data *fvdat = VTOFUD(ap->a_vp);
2370 printf("nodeid: %ju, parent nodeid: %ju, nlookup: %ju, flag: %#x\n",
2371 (uintmax_t)VTOILLU(ap->a_vp), (uintmax_t)fvdat->parent_nid,
2372 (uintmax_t)fvdat->nlookup,