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_fdatasync_t fuse_vnop_fdatasync;
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_fdatasync = fuse_vnop_fdatasync,
159 .vop_getattr = fuse_vnop_getattr,
160 .vop_getextattr = fuse_vnop_getextattr,
161 .vop_inactive = fuse_vnop_inactive,
162 .vop_link = fuse_vnop_link,
163 .vop_listextattr = fuse_vnop_listextattr,
164 .vop_lookup = fuse_vnop_lookup,
165 .vop_mkdir = fuse_vnop_mkdir,
166 .vop_mknod = fuse_vnop_mknod,
167 .vop_open = fuse_vnop_open,
168 .vop_pathconf = fuse_vnop_pathconf,
169 .vop_read = fuse_vnop_read,
170 .vop_readdir = fuse_vnop_readdir,
171 .vop_readlink = fuse_vnop_readlink,
172 .vop_reclaim = fuse_vnop_reclaim,
173 .vop_remove = fuse_vnop_remove,
174 .vop_rename = fuse_vnop_rename,
175 .vop_rmdir = fuse_vnop_rmdir,
176 .vop_setattr = fuse_vnop_setattr,
177 .vop_setextattr = fuse_vnop_setextattr,
178 .vop_strategy = fuse_vnop_strategy,
179 .vop_symlink = fuse_vnop_symlink,
180 .vop_write = fuse_vnop_write,
181 .vop_getpages = fuse_vnop_getpages,
182 .vop_putpages = fuse_vnop_putpages,
183 .vop_print = fuse_vnop_print,
186 static u_long fuse_lookup_cache_hits = 0;
188 SYSCTL_ULONG(_vfs_fusefs, OID_AUTO, lookup_cache_hits, CTLFLAG_RD,
189 &fuse_lookup_cache_hits, 0, "number of positive cache hits in lookup");
191 static u_long fuse_lookup_cache_misses = 0;
193 SYSCTL_ULONG(_vfs_fusefs, OID_AUTO, lookup_cache_misses, CTLFLAG_RD,
194 &fuse_lookup_cache_misses, 0, "number of cache misses in lookup");
196 int fuse_lookup_cache_enable = 1;
198 SYSCTL_INT(_vfs_fusefs, OID_AUTO, lookup_cache_enable, CTLFLAG_RW,
199 &fuse_lookup_cache_enable, 0, "if non-zero, enable lookup cache");
202 * XXX: This feature is highly experimental and can bring to instabilities,
203 * needs revisiting before to be enabled by default.
205 static int fuse_reclaim_revoked = 0;
207 SYSCTL_INT(_vfs_fusefs, OID_AUTO, reclaim_revoked, CTLFLAG_RW,
208 &fuse_reclaim_revoked, 0, "");
210 uma_zone_t fuse_pbuf_zone;
212 #define fuse_vm_page_lock(m) vm_page_lock((m));
213 #define fuse_vm_page_unlock(m) vm_page_unlock((m));
214 #define fuse_vm_page_lock_queues() ((void)0)
215 #define fuse_vm_page_unlock_queues() ((void)0)
218 struct vnop_access_args {
220 #if VOP_ACCESS_TAKES_ACCMODE_T
225 struct ucred *a_cred;
230 fuse_vnop_access(struct vop_access_args *ap)
232 struct vnode *vp = ap->a_vp;
233 int accmode = ap->a_accmode;
234 struct ucred *cred = ap->a_cred;
236 struct fuse_access_param facp;
237 struct fuse_data *data = fuse_get_mpdata(vnode_mount(vp));
241 if (fuse_isdeadfs(vp)) {
242 if (vnode_isvroot(vp)) {
247 if (!(data->dataflags & FSESS_INITED)) {
248 if (vnode_isvroot(vp)) {
249 if (priv_check_cred(cred, PRIV_VFS_ADMIN) ||
250 (fuse_match_cred(data->daemoncred, cred) == 0)) {
256 if (vnode_islnk(vp)) {
259 bzero(&facp, sizeof(facp));
261 err = fuse_internal_access(vp, accmode, &facp, ap->a_td, ap->a_cred);
266 struct vnop_close_args {
269 struct ucred *a_cred;
274 fuse_vnop_close(struct vop_close_args *ap)
276 struct vnode *vp = ap->a_vp;
277 struct ucred *cred = ap->a_cred;
278 int fflag = ap->a_fflag;
279 fufh_type_t fufh_type;
281 if (fuse_isdeadfs(vp)) {
284 if (vnode_isdir(vp)) {
285 if (fuse_filehandle_valid(vp, FUFH_RDONLY)) {
286 fuse_filehandle_close(vp, FUFH_RDONLY, NULL, cred);
290 if (fflag & IO_NDELAY) {
293 fufh_type = fuse_filehandle_xlate_from_fflags(fflag);
295 if (!fuse_filehandle_valid(vp, fufh_type)) {
298 for (i = 0; i < FUFH_MAXTYPE; i++)
299 if (fuse_filehandle_valid(vp, i))
301 if (i == FUFH_MAXTYPE)
302 panic("FUSE: fufh type %d found to be invalid in close"
306 if ((VTOFUD(vp)->flag & FN_SIZECHANGE) != 0) {
307 fuse_vnode_savesize(vp, cred);
313 struct vnop_create_args {
315 struct vnode **a_vpp;
316 struct componentname *a_cnp;
321 fuse_vnop_create(struct vop_create_args *ap)
323 struct vnode *dvp = ap->a_dvp;
324 struct vnode **vpp = ap->a_vpp;
325 struct componentname *cnp = ap->a_cnp;
326 struct vattr *vap = ap->a_vap;
327 struct thread *td = cnp->cn_thread;
328 struct ucred *cred = cnp->cn_cred;
330 struct fuse_open_in *foi;
331 struct fuse_entry_out *feo;
332 struct fuse_dispatcher fdi;
333 struct fuse_dispatcher *fdip = &fdi;
337 struct mount *mp = vnode_mount(dvp);
338 uint64_t parentnid = VTOFUD(dvp)->nid;
339 mode_t mode = MAKEIMODE(vap->va_type, vap->va_mode);
341 uint32_t x_open_flags;
343 if (fuse_isdeadfs(dvp)) {
346 bzero(&fdi, sizeof(fdi));
348 /* XXX: Will we ever want devices ? */
349 if ((vap->va_type != VREG)) {
350 printf("fuse_vnop_create: unsupported va_type %d\n",
355 fdisp_init(fdip, sizeof(*foi) + cnp->cn_namelen + 1);
356 if (!fsess_isimpl(mp, FUSE_CREATE)) {
357 SDT_PROBE2(fuse, , vnops, trace, 1,
358 "eh, daemon doesn't implement create?");
361 fdisp_make(fdip, FUSE_CREATE, vnode_mount(dvp), parentnid, td, cred);
365 foi->flags = O_CREAT | O_RDWR;
367 memcpy((char *)fdip->indata + sizeof(*foi), cnp->cn_nameptr,
369 ((char *)fdip->indata)[sizeof(*foi) + cnp->cn_namelen] = '\0';
371 err = fdisp_wait_answ(fdip);
375 fsess_set_notimpl(mp, FUSE_CREATE);
381 if ((err = fuse_internal_checkentry(feo, VREG))) {
384 err = fuse_vnode_get(mp, feo, feo->nodeid, dvp, vpp, cnp, VREG);
386 struct fuse_release_in *fri;
387 uint64_t nodeid = feo->nodeid;
388 uint64_t fh_id = ((struct fuse_open_out *)(feo + 1))->fh;
390 fdisp_init(fdip, sizeof(*fri));
391 fdisp_make(fdip, FUSE_RELEASE, mp, nodeid, td, cred);
394 fri->flags = OFLAGS(mode);
395 fuse_insert_callback(fdip->tick, fuse_internal_forget_callback);
396 fuse_insert_message(fdip->tick);
399 ASSERT_VOP_ELOCKED(*vpp, "fuse_vnop_create");
401 fdip->answ = feo + 1;
403 x_fh_id = ((struct fuse_open_out *)(feo + 1))->fh;
404 x_open_flags = ((struct fuse_open_out *)(feo + 1))->open_flags;
405 fuse_filehandle_init(*vpp, FUFH_RDWR, NULL, x_fh_id);
406 fuse_vnode_open(*vpp, x_open_flags, td);
407 cache_purge_negative(dvp);
415 struct vnop_fdatasync_args {
416 struct vop_generic_args a_gen;
418 struct thread * a_td;
422 fuse_vnop_fdatasync(struct vop_fdatasync_args *ap)
424 struct vnode *vp = ap->a_vp;
425 struct thread *td = ap->a_td;
426 int waitfor = MNT_WAIT;
430 if (fuse_isdeadfs(vp)) {
433 if ((err = vop_stdfdatasync_buf(ap)))
436 return fuse_internal_fsync(vp, td, waitfor, true);
440 struct vnop_fsync_args {
441 struct vop_generic_args a_gen;
444 struct thread * a_td;
448 fuse_vnop_fsync(struct vop_fsync_args *ap)
450 struct vnode *vp = ap->a_vp;
451 struct thread *td = ap->a_td;
452 int waitfor = ap->a_waitfor;
455 if (fuse_isdeadfs(vp)) {
458 if ((err = vop_stdfsync(ap)))
461 return fuse_internal_fsync(vp, td, waitfor, false);
465 struct vnop_getattr_args {
468 struct ucred *a_cred;
473 fuse_vnop_getattr(struct vop_getattr_args *ap)
475 struct vnode *vp = ap->a_vp;
476 struct vattr *vap = ap->a_vap;
477 struct ucred *cred = ap->a_cred;
478 struct thread *td = curthread;
479 struct fuse_vnode_data *fvdat = VTOFUD(vp);
480 struct fuse_attr_out *fao;
484 struct fuse_dispatcher fdi;
486 dataflags = fuse_get_mpdata(vnode_mount(vp))->dataflags;
488 /* Note that we are not bailing out on a dead file system just yet. */
490 if (!(dataflags & FSESS_INITED)) {
491 if (!vnode_isvroot(vp)) {
492 fdata_set_dead(fuse_get_mpdata(vnode_mount(vp)));
500 if ((err = fdisp_simple_putget_vp(&fdi, FUSE_GETATTR, vp, td, cred))) {
501 if ((err == ENOTCONN) && vnode_isvroot(vp)) {
502 /* see comment in fuse_vfsop_statfs() */
507 fuse_internal_vnode_disappear(vp);
512 fao = (struct fuse_attr_out *)fdi.answ;
513 fuse_internal_cache_attrs(vp, &fao->attr, fao->attr_valid,
514 fao->attr_valid_nsec, vap);
515 if (vap->va_type != vnode_vtype(vp)) {
516 fuse_internal_vnode_disappear(vp);
520 if ((fvdat->flag & FN_SIZECHANGE) != 0)
521 vap->va_size = fvdat->filesize;
523 if (vnode_isreg(vp) && (fvdat->flag & FN_SIZECHANGE) == 0) {
525 * This is for those cases when the file size changed without us
526 * knowing, and we want to catch up.
528 off_t new_filesize = ((struct fuse_attr_out *)
529 fdi.answ)->attr.size;
531 if (fvdat->filesize != new_filesize) {
532 fuse_vnode_setsize(vp, cred, new_filesize);
533 fvdat->flag &= ~FN_SIZECHANGE;
542 bzero(vap, sizeof(*vap));
543 vap->va_type = vnode_vtype(vp);
549 struct vnop_inactive_args {
555 fuse_vnop_inactive(struct vop_inactive_args *ap)
557 struct vnode *vp = ap->a_vp;
558 struct thread *td = ap->a_td;
560 struct fuse_vnode_data *fvdat = VTOFUD(vp);
561 struct fuse_filehandle *fufh = NULL;
563 int type, need_flush = 1;
565 for (type = 0; type < FUFH_MAXTYPE; type++) {
566 fufh = &(fvdat->fufh[type]);
567 if (FUFH_IS_VALID(fufh)) {
568 if (need_flush && vp->v_type == VREG) {
569 if ((VTOFUD(vp)->flag & FN_SIZECHANGE) != 0) {
570 fuse_vnode_savesize(vp, NULL);
572 if (fuse_data_cache_invalidate ||
573 (fvdat->flag & FN_REVOKED) != 0)
574 fuse_io_invalbuf(vp, td);
576 fuse_io_flushbuf(vp, MNT_WAIT, td);
579 fuse_filehandle_close(vp, type, td, NULL);
583 if ((fvdat->flag & FN_REVOKED) != 0 && fuse_reclaim_revoked) {
590 struct vnop_link_args {
591 struct vnode *a_tdvp;
593 struct componentname *a_cnp;
597 fuse_vnop_link(struct vop_link_args *ap)
599 struct vnode *vp = ap->a_vp;
600 struct vnode *tdvp = ap->a_tdvp;
601 struct componentname *cnp = ap->a_cnp;
603 struct vattr *vap = VTOVA(vp);
605 struct fuse_dispatcher fdi;
606 struct fuse_entry_out *feo;
607 struct fuse_link_in fli;
611 if (fuse_isdeadfs(vp)) {
614 if (vnode_mount(tdvp) != vnode_mount(vp)) {
619 * This is a seatbelt check to protect naive userspace filesystems from
620 * themselves and the limitations of the FUSE IPC protocol. If a
621 * filesystem does not allow attribute caching, assume it is capable of
622 * validating that nlink does not overflow.
624 if (vap != NULL && vap->va_nlink >= FUSE_LINK_MAX)
626 fli.oldnodeid = VTOI(vp);
629 fuse_internal_newentry_makerequest(vnode_mount(tdvp), VTOI(tdvp), cnp,
630 FUSE_LINK, &fli, sizeof(fli), &fdi);
631 if ((err = fdisp_wait_answ(&fdi))) {
636 err = fuse_internal_checkentry(feo, vnode_vtype(vp));
643 struct vnop_lookup_args {
644 struct vnodeop_desc *a_desc;
646 struct vnode **a_vpp;
647 struct componentname *a_cnp;
651 fuse_vnop_lookup(struct vop_lookup_args *ap)
653 struct vnode *dvp = ap->a_dvp;
654 struct vnode **vpp = ap->a_vpp;
655 struct componentname *cnp = ap->a_cnp;
656 struct thread *td = cnp->cn_thread;
657 struct ucred *cred = cnp->cn_cred;
659 int nameiop = cnp->cn_nameiop;
660 int flags = cnp->cn_flags;
661 int wantparent = flags & (LOCKPARENT | WANTPARENT);
662 int islastcn = flags & ISLASTCN;
663 struct mount *mp = vnode_mount(dvp);
667 struct vnode *vp = NULL;
669 struct fuse_dispatcher fdi;
673 struct fuse_access_param facp;
675 if (fuse_isdeadfs(dvp)) {
679 if (!vnode_isdir(dvp)) {
682 if (islastcn && vfs_isrdonly(mp) && (nameiop != LOOKUP)) {
686 * We do access check prior to doing anything else only in the case
687 * when we are at fs root (we'd like to say, "we are at the first
688 * component", but that's not exactly the same... nevermind).
689 * See further comments at further access checks.
692 bzero(&facp, sizeof(facp));
693 if (vnode_isvroot(dvp)) { /* early permission check hack */
694 if ((err = fuse_internal_access(dvp, VEXEC, &facp, td, cred))) {
698 if (flags & ISDOTDOT) {
699 nid = VTOFUD(dvp)->parent_nid;
706 } else if (cnp->cn_namelen == 1 && *(cnp->cn_nameptr) == '.') {
711 } else if (fuse_lookup_cache_enable) {
712 err = cache_lookup(dvp, vpp, cnp, NULL, NULL);
715 case -1: /* positive match */
716 atomic_add_acq_long(&fuse_lookup_cache_hits, 1);
719 case 0: /* no match in cache */
720 atomic_add_acq_long(&fuse_lookup_cache_misses, 1);
723 case ENOENT: /* negative match */
730 fdisp_init(&fdi, cnp->cn_namelen + 1);
734 fdisp_make(&fdi, op, mp, nid, td, cred);
736 if (op == FUSE_LOOKUP) {
737 memcpy(fdi.indata, cnp->cn_nameptr, cnp->cn_namelen);
738 ((char *)fdi.indata)[cnp->cn_namelen] = '\0';
740 lookup_err = fdisp_wait_answ(&fdi);
742 if ((op == FUSE_LOOKUP) && !lookup_err) { /* lookup call succeeded */
743 nid = ((struct fuse_entry_out *)fdi.answ)->nodeid;
746 * zero nodeid is the same as "not found",
747 * but it's also cacheable (which we keep
748 * keep on doing not as of writing this)
751 } else if (nid == FUSE_ROOT_ID) {
756 (!fdi.answ_stat || lookup_err != ENOENT || op != FUSE_LOOKUP)) {
760 /* lookup_err, if non-zero, must be ENOENT at this point */
764 if ((nameiop == CREATE || nameiop == RENAME) && islastcn
765 /* && directory dvp has not been removed */ ) {
767 if (vfs_isrdonly(mp)) {
771 #if 0 /* THINK_ABOUT_THIS */
772 if ((err = fuse_internal_access(dvp, VWRITE, cred, td, &facp))) {
778 * Possibly record the position of a slot in the
779 * directory large enough for the new component name.
780 * This can be recorded in the vnode private data for
781 * dvp. Set the SAVENAME flag to hold onto the
782 * pathname for use later in VOP_CREATE or VOP_RENAME.
784 cnp->cn_flags |= SAVENAME;
789 /* Consider inserting name into cache. */
792 * No we can't use negative caching, as the fs
793 * changes are out of our control.
794 * False positives' falseness turns out just as things
795 * go by, but false negatives' falseness doesn't.
796 * (and aiding the caching mechanism with extra control
797 * mechanisms comes quite close to beating the whole purpose
801 if ((cnp->cn_flags & MAKEENTRY) != 0) {
802 SDT_PROBE2(fuse, , vnops, trace, 1,
803 "inserting NULL into cache");
804 cache_enter(dvp, NULL, cnp);
814 struct fuse_entry_out *feo = NULL;
815 struct fuse_attr *fattr = NULL;
817 if (op == FUSE_GETATTR) {
818 fattr = &((struct fuse_attr_out *)fdi.answ)->attr;
820 feo = (struct fuse_entry_out *)fdi.answ;
821 fattr = &(feo->attr);
825 * If deleting, and at end of pathname, return parameters
826 * which can be used to remove file. If the wantparent flag
827 * isn't set, we return only the directory, otherwise we go on
828 * and lock the inode, being careful with ".".
830 if (nameiop == DELETE && islastcn) {
832 * Check for write access on directory.
834 facp.xuid = fattr->uid;
835 facp.facc_flags |= FACCESS_STICKY;
836 err = fuse_internal_access(dvp, VWRITE, &facp, td, cred);
837 facp.facc_flags &= ~FACCESS_XQUERIES;
842 if (nid == VTOI(dvp)) {
846 err = fuse_vnode_get(dvp->v_mount, feo, nid,
847 dvp, &vp, cnp, IFTOVT(fattr->mode));
854 * Save the name for use in VOP_RMDIR and VOP_REMOVE
857 cnp->cn_flags |= SAVENAME;
862 * If rewriting (RENAME), return the inode and the
863 * information required to rewrite the present directory
864 * Must get inode of directory entry to verify it's a
865 * regular file, or empty directory.
867 if (nameiop == RENAME && wantparent && islastcn) {
869 #if 0 /* THINK_ABOUT_THIS */
870 if ((err = fuse_internal_access(dvp, VWRITE, cred, td, &facp))) {
878 if (nid == VTOI(dvp)) {
882 err = fuse_vnode_get(vnode_mount(dvp), feo, nid, dvp,
883 &vp, cnp, IFTOVT(fattr->mode));
889 * Save the name for use in VOP_RENAME later.
891 cnp->cn_flags |= SAVENAME;
895 if (flags & ISDOTDOT) {
900 * Expanded copy of vn_vget_ino() so that
901 * fuse_vnode_get() can be used.
904 ltype = VOP_ISLOCKED(dvp);
905 err = vfs_busy(mp, MBF_NOWAIT);
909 err = vfs_busy(mp, 0);
910 vn_lock(dvp, ltype | LK_RETRY);
914 if ((dvp->v_iflag & VI_DOOMED) != 0) {
921 err = fuse_vnode_get(vnode_mount(dvp), feo, nid, NULL,
922 &vp, cnp, IFTOVT(fattr->mode));
924 vn_lock(dvp, ltype | LK_RETRY);
925 if ((dvp->v_iflag & VI_DOOMED) != 0) {
933 } else if (nid == VTOI(dvp)) {
937 struct fuse_vnode_data *fvdat;
939 err = fuse_vnode_get(vnode_mount(dvp), feo, nid, dvp,
940 &vp, cnp, IFTOVT(fattr->mode));
944 fuse_vnode_setparent(vp, dvp);
947 * In the case where we are looking up a FUSE node
948 * represented by an existing cached vnode, and the
949 * true size reported by FUSE_LOOKUP doesn't match
950 * the vnode's cached size, fix the vnode cache to
951 * match the real object size.
953 * This can occur via FUSE distributed filesystems,
954 * irregular files, etc.
957 if (vnode_isreg(vp) &&
958 fattr->size != fvdat->filesize) {
960 * The FN_SIZECHANGE flag reflects a dirty
961 * append. If userspace lets us know our cache
962 * is invalid, that write was lost. (Dirty
963 * writes that do not cause append are also
964 * lost, but we don't detect them here.)
966 * XXX: Maybe disable WB caching on this mount.
968 if (fvdat->flag & FN_SIZECHANGE)
969 printf("%s: WB cache incoherent on "
971 vnode_mount(vp)->mnt_stat.f_mntonname);
973 (void)fuse_vnode_setsize(vp, cred, fattr->size);
974 fvdat->flag &= ~FN_SIZECHANGE;
979 if (op == FUSE_GETATTR) {
980 struct fuse_attr_out *fao =
981 (struct fuse_attr_out*)fdi.answ;
982 fuse_internal_cache_attrs(*vpp,
983 &fao->attr, fao->attr_valid,
984 fao->attr_valid_nsec, NULL);
986 struct fuse_entry_out *feo =
987 (struct fuse_entry_out*)fdi.answ;
988 fuse_internal_cache_attrs(*vpp,
989 &feo->attr, feo->attr_valid,
990 feo->attr_valid_nsec, NULL);
993 /* Insert name into cache if appropriate. */
996 * Nooo, caching is evil. With caching, we can't avoid stale
997 * information taking over the playground (cached info is not
998 * just positive/negative, it does have qualitative aspects,
999 * too). And a (VOP/FUSE)_GETATTR is always thrown anyway, when
1000 * walking down along cached path components, and that's not
1001 * any cheaper than FUSE_LOOKUP. This might change with
1002 * implementing kernel side attr caching, but... In Linux,
1003 * lookup results are not cached, and the daemon is bombarded
1004 * with FUSE_LOOKUPS on and on. This shows that by design, the
1005 * daemon is expected to handle frequent lookup queries
1006 * efficiently, do its caching in userspace, and so on.
1008 * So just leave the name cache alone.
1012 * Well, now I know, Linux caches lookups, but with a
1013 * timeout... So it's the same thing as attribute caching:
1014 * we can deal with it when implement timeouts.
1017 if (cnp->cn_flags & MAKEENTRY) {
1018 cache_enter(dvp, *vpp, cnp);
1025 /* No lookup error; need to clean up. */
1027 if (err) { /* Found inode; exit with no vnode. */
1028 if (op == FUSE_LOOKUP) {
1029 fuse_internal_forget_send(vnode_mount(dvp), td, cred,
1032 fdisp_destroy(&fdi);
1035 #ifndef NO_EARLY_PERM_CHECK_HACK
1038 * We have the attributes of the next item
1039 * *now*, and it's a fact, and we do not
1040 * have to do extra work for it (ie, beg the
1041 * daemon), and it neither depends on such
1042 * accidental things like attr caching. So
1043 * the big idea: check credentials *now*,
1044 * not at the beginning of the next call to
1047 * The first item of the lookup chain (fs root)
1048 * won't be checked then here, of course, as
1049 * its never "the next". But go and see that
1050 * the root is taken care about at the very
1051 * beginning of this function.
1053 * Now, given we want to do the access check
1054 * this way, one might ask: so then why not
1055 * do the access check just after fetching
1056 * the inode and its attributes from the
1057 * daemon? Why bother with producing the
1058 * corresponding vnode at all if something
1059 * is not OK? We know what's the deal as
1060 * soon as we get those attrs... There is
1061 * one bit of info though not given us by
1062 * the daemon: whether his response is
1063 * authoritative or not... His response should
1064 * be ignored if something is mounted over
1065 * the dir in question. But that can be
1066 * known only by having the vnode...
1068 int tmpvtype = vnode_vtype(*vpp);
1070 bzero(&facp, sizeof(facp));
1071 /*the early perm check hack */
1072 facp.facc_flags |= FACCESS_VA_VALID;
1074 if ((tmpvtype != VDIR) && (tmpvtype != VLNK)) {
1077 if (!err && !vnode_mountedhere(*vpp)) {
1078 err = fuse_internal_access(*vpp, VEXEC, &facp, td, cred);
1081 if (tmpvtype == VLNK)
1082 SDT_PROBE2(fuse, , vnops, trace,
1083 1, "weird, permission "
1084 "error with a symlink?");
1092 fdisp_destroy(&fdi);
1098 struct vnop_mkdir_args {
1099 struct vnode *a_dvp;
1100 struct vnode **a_vpp;
1101 struct componentname *a_cnp;
1102 struct vattr *a_vap;
1106 fuse_vnop_mkdir(struct vop_mkdir_args *ap)
1108 struct vnode *dvp = ap->a_dvp;
1109 struct vnode **vpp = ap->a_vpp;
1110 struct componentname *cnp = ap->a_cnp;
1111 struct vattr *vap = ap->a_vap;
1113 struct fuse_mkdir_in fmdi;
1115 if (fuse_isdeadfs(dvp)) {
1118 fmdi.mode = MAKEIMODE(vap->va_type, vap->va_mode);
1120 return (fuse_internal_newentry(dvp, vpp, cnp, FUSE_MKDIR, &fmdi,
1121 sizeof(fmdi), VDIR));
1125 struct vnop_mknod_args {
1126 struct vnode *a_dvp;
1127 struct vnode **a_vpp;
1128 struct componentname *a_cnp;
1129 struct vattr *a_vap;
1133 fuse_vnop_mknod(struct vop_mknod_args *ap)
1141 struct vnop_open_args {
1144 struct ucred *a_cred;
1145 struct thread *a_td;
1146 int a_fdidx; / struct file *a_fp;
1150 fuse_vnop_open(struct vop_open_args *ap)
1152 struct vnode *vp = ap->a_vp;
1153 int mode = ap->a_mode;
1154 struct thread *td = ap->a_td;
1155 struct ucred *cred = ap->a_cred;
1157 fufh_type_t fufh_type;
1158 struct fuse_vnode_data *fvdat;
1160 int error, isdir = 0;
1161 int32_t fuse_open_flags;
1163 if (fuse_isdeadfs(vp)) {
1166 if ((mode & (FREAD | FWRITE)) == 0)
1171 if (vnode_isdir(vp)) {
1174 fuse_open_flags = 0;
1176 fufh_type = FUFH_RDONLY;
1178 fufh_type = fuse_filehandle_xlate_from_fflags(mode);
1180 * For WRONLY opens, force DIRECT_IO. This is necessary
1181 * since writing a partial block through the buffer cache
1182 * will result in a read of the block and that read won't
1183 * be allowed by the WRONLY open.
1185 if (fufh_type == FUFH_WRONLY ||
1186 (fvdat->flag & FN_DIRECTIO) != 0)
1187 fuse_open_flags = FOPEN_DIRECT_IO;
1190 if (fuse_filehandle_validrw(vp, fufh_type) != FUFH_INVALID) {
1191 fuse_vnode_open(vp, fuse_open_flags, td);
1194 error = fuse_filehandle_open(vp, fufh_type, NULL, td, cred);
1200 fuse_vnop_pathconf(struct vop_pathconf_args *ap)
1203 switch (ap->a_name) {
1204 case _PC_FILESIZEBITS:
1208 *ap->a_retval = NAME_MAX;
1211 *ap->a_retval = MIN(LONG_MAX, FUSE_LINK_MAX);
1213 case _PC_SYMLINK_MAX:
1214 *ap->a_retval = MAXPATHLEN;
1220 return (vop_stdpathconf(ap));
1225 struct vnop_read_args {
1229 struct ucred *a_cred;
1233 fuse_vnop_read(struct vop_read_args *ap)
1235 struct vnode *vp = ap->a_vp;
1236 struct uio *uio = ap->a_uio;
1237 int ioflag = ap->a_ioflag;
1238 struct ucred *cred = ap->a_cred;
1240 if (fuse_isdeadfs(vp)) {
1244 if (VTOFUD(vp)->flag & FN_DIRECTIO) {
1245 ioflag |= IO_DIRECT;
1248 return fuse_io_dispatch(vp, uio, ioflag, cred);
1252 struct vnop_readdir_args {
1255 struct ucred *a_cred;
1262 fuse_vnop_readdir(struct vop_readdir_args *ap)
1264 struct vnode *vp = ap->a_vp;
1265 struct uio *uio = ap->a_uio;
1266 struct ucred *cred = ap->a_cred;
1268 struct fuse_filehandle *fufh = NULL;
1269 struct fuse_iov cookediov;
1274 if (fuse_isdeadfs(vp)) {
1277 if ( /* XXXIP ((uio_iovcnt(uio) > 1)) || */
1278 (uio_resid(uio) < sizeof(struct dirent))) {
1282 if (!fuse_filehandle_valid(vp, FUFH_RDONLY)) {
1283 SDT_PROBE2(fuse, , vnops, trace, 1,
1284 "calling readdir() before open()");
1285 err = fuse_filehandle_open(vp, FUFH_RDONLY, &fufh, NULL, cred);
1288 err = fuse_filehandle_get(vp, FUFH_RDONLY, &fufh);
1293 #define DIRCOOKEDSIZE FUSE_DIRENT_ALIGN(FUSE_NAME_OFFSET + MAXNAMLEN + 1)
1294 fiov_init(&cookediov, DIRCOOKEDSIZE);
1296 err = fuse_internal_readdir(vp, uio, fufh, &cookediov);
1298 fiov_teardown(&cookediov);
1300 fuse_filehandle_close(vp, FUFH_RDONLY, NULL, cred);
1306 struct vnop_readlink_args {
1309 struct ucred *a_cred;
1313 fuse_vnop_readlink(struct vop_readlink_args *ap)
1315 struct vnode *vp = ap->a_vp;
1316 struct uio *uio = ap->a_uio;
1317 struct ucred *cred = ap->a_cred;
1319 struct fuse_dispatcher fdi;
1322 if (fuse_isdeadfs(vp)) {
1325 if (!vnode_islnk(vp)) {
1328 fdisp_init(&fdi, 0);
1329 err = fdisp_simple_putget_vp(&fdi, FUSE_READLINK, vp, curthread, cred);
1333 if (((char *)fdi.answ)[0] == '/' &&
1334 fuse_get_mpdata(vnode_mount(vp))->dataflags & FSESS_PUSH_SYMLINKS_IN) {
1335 char *mpth = vnode_mount(vp)->mnt_stat.f_mntonname;
1337 err = uiomove(mpth, strlen(mpth), uio);
1340 err = uiomove(fdi.answ, fdi.iosize, uio);
1343 fdisp_destroy(&fdi);
1348 struct vnop_reclaim_args {
1350 struct thread *a_td;
1354 fuse_vnop_reclaim(struct vop_reclaim_args *ap)
1356 struct vnode *vp = ap->a_vp;
1357 struct thread *td = ap->a_td;
1359 struct fuse_vnode_data *fvdat = VTOFUD(vp);
1360 struct fuse_filehandle *fufh = NULL;
1365 panic("FUSE: no vnode data during recycling");
1367 for (type = 0; type < FUFH_MAXTYPE; type++) {
1368 fufh = &(fvdat->fufh[type]);
1369 if (FUFH_IS_VALID(fufh)) {
1370 printf("FUSE: vnode being reclaimed but fufh (type=%d) is valid",
1372 fuse_filehandle_close(vp, type, td, NULL);
1376 if ((!fuse_isdeadfs(vp)) && (fvdat->nlookup)) {
1377 fuse_internal_forget_send(vnode_mount(vp), td, NULL, VTOI(vp),
1380 fuse_vnode_setparent(vp, NULL);
1382 vfs_hash_remove(vp);
1383 vnode_destroy_vobject(vp);
1384 fuse_vnode_destroy(vp);
1390 struct vnop_remove_args {
1391 struct vnode *a_dvp;
1393 struct componentname *a_cnp;
1397 fuse_vnop_remove(struct vop_remove_args *ap)
1399 struct vnode *dvp = ap->a_dvp;
1400 struct vnode *vp = ap->a_vp;
1401 struct componentname *cnp = ap->a_cnp;
1405 if (fuse_isdeadfs(vp)) {
1408 if (vnode_isdir(vp)) {
1413 err = fuse_internal_remove(dvp, vp, cnp, FUSE_UNLINK);
1416 fuse_internal_vnode_disappear(vp);
1421 struct vnop_rename_args {
1422 struct vnode *a_fdvp;
1423 struct vnode *a_fvp;
1424 struct componentname *a_fcnp;
1425 struct vnode *a_tdvp;
1426 struct vnode *a_tvp;
1427 struct componentname *a_tcnp;
1431 fuse_vnop_rename(struct vop_rename_args *ap)
1433 struct vnode *fdvp = ap->a_fdvp;
1434 struct vnode *fvp = ap->a_fvp;
1435 struct componentname *fcnp = ap->a_fcnp;
1436 struct vnode *tdvp = ap->a_tdvp;
1437 struct vnode *tvp = ap->a_tvp;
1438 struct componentname *tcnp = ap->a_tcnp;
1439 struct fuse_data *data;
1443 if (fuse_isdeadfs(fdvp)) {
1446 if (fvp->v_mount != tdvp->v_mount ||
1447 (tvp && fvp->v_mount != tvp->v_mount)) {
1448 SDT_PROBE2(fuse, , vnops, trace, 1, "cross-device rename");
1455 * FUSE library is expected to check if target directory is not
1456 * under the source directory in the file system tree.
1457 * Linux performs this check at VFS level.
1459 data = fuse_get_mpdata(vnode_mount(tdvp));
1460 sx_xlock(&data->rename_lock);
1461 err = fuse_internal_rename(fdvp, fcnp, tdvp, tcnp);
1464 fuse_vnode_setparent(fvp, tdvp);
1466 fuse_vnode_setparent(tvp, NULL);
1468 sx_unlock(&data->rename_lock);
1470 if (tvp != NULL && tvp != fvp) {
1473 if (vnode_isdir(fvp)) {
1474 if ((tvp != NULL) && vnode_isdir(tvp)) {
1495 struct vnop_rmdir_args {
1496 struct vnode *a_dvp;
1498 struct componentname *a_cnp;
1502 fuse_vnop_rmdir(struct vop_rmdir_args *ap)
1504 struct vnode *dvp = ap->a_dvp;
1505 struct vnode *vp = ap->a_vp;
1509 if (fuse_isdeadfs(vp)) {
1512 if (VTOFUD(vp) == VTOFUD(dvp)) {
1515 err = fuse_internal_remove(dvp, vp, ap->a_cnp, FUSE_RMDIR);
1518 fuse_internal_vnode_disappear(vp);
1523 struct vnop_setattr_args {
1525 struct vattr *a_vap;
1526 struct ucred *a_cred;
1527 struct thread *a_td;
1531 fuse_vnop_setattr(struct vop_setattr_args *ap)
1533 struct vnode *vp = ap->a_vp;
1534 struct vattr *vap = ap->a_vap;
1535 struct ucred *cred = ap->a_cred;
1536 struct thread *td = curthread;
1538 struct fuse_dispatcher fdi;
1539 struct fuse_setattr_in *fsai;
1540 struct fuse_access_param facp;
1544 int sizechanged = 0;
1545 uint64_t newsize = 0;
1547 if (fuse_isdeadfs(vp)) {
1550 fdisp_init(&fdi, sizeof(*fsai));
1551 fdisp_make_vp(&fdi, FUSE_SETATTR, vp, td, cred);
1555 bzero(&facp, sizeof(facp));
1557 facp.xuid = vap->va_uid;
1558 facp.xgid = vap->va_gid;
1560 if (vap->va_uid != (uid_t)VNOVAL) {
1561 facp.facc_flags |= FACCESS_CHOWN;
1562 fsai->uid = vap->va_uid;
1563 fsai->valid |= FATTR_UID;
1565 if (vap->va_gid != (gid_t)VNOVAL) {
1566 facp.facc_flags |= FACCESS_CHOWN;
1567 fsai->gid = vap->va_gid;
1568 fsai->valid |= FATTR_GID;
1570 if (vap->va_size != VNOVAL) {
1572 struct fuse_filehandle *fufh = NULL;
1574 /*Truncate to a new value. */
1575 fsai->size = vap->va_size;
1577 newsize = vap->va_size;
1578 fsai->valid |= FATTR_SIZE;
1580 fuse_filehandle_getrw(vp, FUFH_WRONLY, &fufh);
1582 fsai->fh = fufh->fh_id;
1583 fsai->valid |= FATTR_FH;
1586 if (vap->va_atime.tv_sec != VNOVAL) {
1587 fsai->atime = vap->va_atime.tv_sec;
1588 fsai->atimensec = vap->va_atime.tv_nsec;
1589 fsai->valid |= FATTR_ATIME;
1591 if (vap->va_mtime.tv_sec != VNOVAL) {
1592 fsai->mtime = vap->va_mtime.tv_sec;
1593 fsai->mtimensec = vap->va_mtime.tv_nsec;
1594 fsai->valid |= FATTR_MTIME;
1596 if (vap->va_mode != (mode_t)VNOVAL) {
1597 fsai->mode = vap->va_mode & ALLPERMS;
1598 fsai->valid |= FATTR_MODE;
1603 vtyp = vnode_vtype(vp);
1605 if (fsai->valid & FATTR_SIZE && vtyp == VDIR) {
1609 if (vfs_isrdonly(vnode_mount(vp)) && (fsai->valid & ~FATTR_SIZE || vtyp == VREG)) {
1613 if (fsai->valid & ~FATTR_SIZE) {
1614 /*err = fuse_internal_access(vp, VADMIN, context, &facp); */
1618 facp.facc_flags &= ~FACCESS_XQUERIES;
1620 if (err && !(fsai->valid & ~(FATTR_ATIME | FATTR_MTIME)) &&
1621 vap->va_vaflags & VA_UTIMES_NULL) {
1622 err = fuse_internal_access(vp, VWRITE, &facp, td, cred);
1626 if ((err = fdisp_wait_answ(&fdi)))
1628 vtyp = IFTOVT(((struct fuse_attr_out *)fdi.answ)->attr.mode);
1630 if (vnode_vtype(vp) != vtyp) {
1631 if (vnode_vtype(vp) == VNON && vtyp != VNON) {
1632 SDT_PROBE2(fuse, , vnops, trace, 1, "FUSE: Dang! "
1633 "vnode_vtype is VNON and vtype isn't.");
1636 * STALE vnode, ditch
1638 * The vnode has changed its type "behind our back".
1639 * There's nothing really we can do, so let us just
1640 * force an internal revocation and tell the caller to
1641 * try again, if interested.
1643 fuse_internal_vnode_disappear(vp);
1648 struct fuse_attr_out *fao = (struct fuse_attr_out*)fdi.answ;
1649 fuse_internal_cache_attrs(vp, &fao->attr, fao->attr_valid,
1650 fao->attr_valid_nsec, NULL);
1654 fdisp_destroy(&fdi);
1655 if (!err && sizechanged) {
1656 fuse_vnode_setsize(vp, cred, newsize);
1657 VTOFUD(vp)->flag &= ~FN_SIZECHANGE;
1663 struct vnop_strategy_args {
1669 fuse_vnop_strategy(struct vop_strategy_args *ap)
1671 struct vnode *vp = ap->a_vp;
1672 struct buf *bp = ap->a_bp;
1674 if (!vp || fuse_isdeadfs(vp)) {
1675 bp->b_ioflags |= BIO_ERROR;
1676 bp->b_error = ENXIO;
1680 if (bp->b_iocmd == BIO_WRITE)
1681 fuse_vnode_refreshsize(vp, NOCRED);
1683 (void)fuse_io_strategy(vp, bp);
1686 * This is a dangerous function. If returns error, that might mean a
1687 * panic. We prefer pretty much anything over being forced to panic
1688 * by a malicious daemon (a demon?). So we just return 0 anyway. You
1689 * should never mind this: this function has its own error
1690 * propagation mechanism via the argument buffer, so
1691 * not-that-melodramatic residents of the call chain still will be
1692 * able to know what to do.
1699 struct vnop_symlink_args {
1700 struct vnode *a_dvp;
1701 struct vnode **a_vpp;
1702 struct componentname *a_cnp;
1703 struct vattr *a_vap;
1708 fuse_vnop_symlink(struct vop_symlink_args *ap)
1710 struct vnode *dvp = ap->a_dvp;
1711 struct vnode **vpp = ap->a_vpp;
1712 struct componentname *cnp = ap->a_cnp;
1713 const char *target = ap->a_target;
1715 struct fuse_dispatcher fdi;
1720 if (fuse_isdeadfs(dvp)) {
1724 * Unlike the other creator type calls, here we have to create a message
1725 * where the name of the new entry comes first, and the data describing
1726 * the entry comes second.
1727 * Hence we can't rely on our handy fuse_internal_newentry() routine,
1728 * but put together the message manually and just call the core part.
1731 len = strlen(target) + 1;
1732 fdisp_init(&fdi, len + cnp->cn_namelen + 1);
1733 fdisp_make_vp(&fdi, FUSE_SYMLINK, dvp, curthread, NULL);
1735 memcpy(fdi.indata, cnp->cn_nameptr, cnp->cn_namelen);
1736 ((char *)fdi.indata)[cnp->cn_namelen] = '\0';
1737 memcpy((char *)fdi.indata + cnp->cn_namelen + 1, target, len);
1739 err = fuse_internal_newentry_core(dvp, vpp, cnp, VLNK, &fdi);
1740 fdisp_destroy(&fdi);
1745 struct vnop_write_args {
1749 struct ucred *a_cred;
1753 fuse_vnop_write(struct vop_write_args *ap)
1755 struct vnode *vp = ap->a_vp;
1756 struct uio *uio = ap->a_uio;
1757 int ioflag = ap->a_ioflag;
1758 struct ucred *cred = ap->a_cred;
1760 if (fuse_isdeadfs(vp)) {
1763 fuse_vnode_refreshsize(vp, cred);
1765 if (VTOFUD(vp)->flag & FN_DIRECTIO) {
1766 ioflag |= IO_DIRECT;
1769 return fuse_io_dispatch(vp, uio, ioflag, cred);
1772 SDT_PROBE_DEFINE1(fuse, , vnops, vnop_getpages_error, "int");
1774 struct vnop_getpages_args {
1782 fuse_vnop_getpages(struct vop_getpages_args *ap)
1784 int i, error, nextoff, size, toff, count, npages;
1795 KASSERT(vp->v_object, ("objectless vp passed to getpages"));
1796 td = curthread; /* XXX */
1797 cred = curthread->td_ucred; /* XXX */
1799 npages = ap->a_count;
1801 if (!fsess_opt_mmap(vnode_mount(vp))) {
1802 SDT_PROBE2(fuse, , vnops, trace, 1,
1803 "called on non-cacheable vnode??\n");
1804 return (VM_PAGER_ERROR);
1808 * If the last page is partially valid, just return it and allow
1809 * the pager to zero-out the blanks. Partially valid pages can
1810 * only occur at the file EOF.
1812 * XXXGL: is that true for FUSE, which is a local filesystem,
1813 * but still somewhat disconnected from the kernel?
1815 VM_OBJECT_WLOCK(vp->v_object);
1816 if (pages[npages - 1]->valid != 0 && --npages == 0)
1818 VM_OBJECT_WUNLOCK(vp->v_object);
1821 * We use only the kva address for the buffer, but this is extremely
1822 * convenient and fast.
1824 bp = uma_zalloc(fuse_pbuf_zone, M_WAITOK);
1826 kva = (vm_offset_t)bp->b_data;
1827 pmap_qenter(kva, pages, npages);
1828 VM_CNT_INC(v_vnodein);
1829 VM_CNT_ADD(v_vnodepgsin, npages);
1831 count = npages << PAGE_SHIFT;
1832 iov.iov_base = (caddr_t)kva;
1833 iov.iov_len = count;
1836 uio.uio_offset = IDX_TO_OFF(pages[0]->pindex);
1837 uio.uio_resid = count;
1838 uio.uio_segflg = UIO_SYSSPACE;
1839 uio.uio_rw = UIO_READ;
1842 error = fuse_io_dispatch(vp, &uio, IO_DIRECT, cred);
1843 pmap_qremove(kva, npages);
1845 uma_zfree(fuse_pbuf_zone, bp);
1847 if (error && (uio.uio_resid == count)) {
1848 SDT_PROBE1(fuse, , vnops, vnop_getpages_error, error);
1849 return VM_PAGER_ERROR;
1852 * Calculate the number of bytes read and validate only that number
1853 * of bytes. Note that due to pending writes, size may be 0. This
1854 * does not mean that the remaining data is invalid!
1857 size = count - uio.uio_resid;
1858 VM_OBJECT_WLOCK(vp->v_object);
1859 fuse_vm_page_lock_queues();
1860 for (i = 0, toff = 0; i < npages; i++, toff = nextoff) {
1863 nextoff = toff + PAGE_SIZE;
1866 if (nextoff <= size) {
1868 * Read operation filled an entire page
1870 m->valid = VM_PAGE_BITS_ALL;
1871 KASSERT(m->dirty == 0,
1872 ("fuse_getpages: page %p is dirty", m));
1873 } else if (size > toff) {
1875 * Read operation filled a partial page.
1878 vm_page_set_valid_range(m, 0, size - toff);
1879 KASSERT(m->dirty == 0,
1880 ("fuse_getpages: page %p is dirty", m));
1883 * Read operation was short. If no error occurred
1884 * we may have hit a zero-fill section. We simply
1885 * leave valid set to 0.
1890 fuse_vm_page_unlock_queues();
1892 VM_OBJECT_WUNLOCK(vp->v_object);
1897 return (VM_PAGER_OK);
1901 struct vnop_putpages_args {
1907 vm_ooffset_t a_offset;
1911 fuse_vnop_putpages(struct vop_putpages_args *ap)
1917 int i, error, npages, count;
1927 KASSERT(vp->v_object, ("objectless vp passed to putpages"));
1928 fsize = vp->v_object->un_pager.vnp.vnp_size;
1929 td = curthread; /* XXX */
1930 cred = curthread->td_ucred; /* XXX */
1932 count = ap->a_count;
1933 rtvals = ap->a_rtvals;
1934 npages = btoc(count);
1935 offset = IDX_TO_OFF(pages[0]->pindex);
1937 if (!fsess_opt_mmap(vnode_mount(vp))) {
1938 SDT_PROBE2(fuse, , vnops, trace, 1,
1939 "called on non-cacheable vnode??\n");
1941 for (i = 0; i < npages; i++)
1942 rtvals[i] = VM_PAGER_AGAIN;
1945 * When putting pages, do not extend file past EOF.
1948 if (offset + count > fsize) {
1949 count = fsize - offset;
1954 * We use only the kva address for the buffer, but this is extremely
1955 * convenient and fast.
1957 bp = uma_zalloc(fuse_pbuf_zone, M_WAITOK);
1959 kva = (vm_offset_t)bp->b_data;
1960 pmap_qenter(kva, pages, npages);
1961 VM_CNT_INC(v_vnodeout);
1962 VM_CNT_ADD(v_vnodepgsout, count);
1964 iov.iov_base = (caddr_t)kva;
1965 iov.iov_len = count;
1968 uio.uio_offset = offset;
1969 uio.uio_resid = count;
1970 uio.uio_segflg = UIO_SYSSPACE;
1971 uio.uio_rw = UIO_WRITE;
1974 error = fuse_io_dispatch(vp, &uio, IO_DIRECT, cred);
1976 pmap_qremove(kva, npages);
1977 uma_zfree(fuse_pbuf_zone, bp);
1980 int nwritten = round_page(count - uio.uio_resid) / PAGE_SIZE;
1982 for (i = 0; i < nwritten; i++) {
1983 rtvals[i] = VM_PAGER_OK;
1984 VM_OBJECT_WLOCK(pages[i]->object);
1985 vm_page_undirty(pages[i]);
1986 VM_OBJECT_WUNLOCK(pages[i]->object);
1992 static const char extattr_namespace_separator = '.';
1995 struct vop_getextattr_args {
1996 struct vop_generic_args a_gen;
1998 int a_attrnamespace;
2002 struct ucred *a_cred;
2003 struct thread *a_td;
2007 fuse_vnop_getextattr(struct vop_getextattr_args *ap)
2009 struct vnode *vp = ap->a_vp;
2010 struct uio *uio = ap->a_uio;
2011 struct fuse_dispatcher fdi;
2012 struct fuse_getxattr_in *get_xattr_in;
2013 struct fuse_getxattr_out *get_xattr_out;
2014 struct mount *mp = vnode_mount(vp);
2015 struct thread *td = ap->a_td;
2016 struct ucred *cred = ap->a_cred;
2022 if (fuse_isdeadfs(vp))
2025 /* Default to looking for user attributes. */
2026 if (ap->a_attrnamespace == EXTATTR_NAMESPACE_SYSTEM)
2027 prefix = EXTATTR_NAMESPACE_SYSTEM_STRING;
2029 prefix = EXTATTR_NAMESPACE_USER_STRING;
2031 len = strlen(prefix) + sizeof(extattr_namespace_separator) +
2032 strlen(ap->a_name) + 1;
2034 fdisp_init(&fdi, len + sizeof(*get_xattr_in));
2035 fdisp_make_vp(&fdi, FUSE_GETXATTR, vp, td, cred);
2037 get_xattr_in = fdi.indata;
2039 * Check to see whether we're querying the available size or
2040 * issuing the actual request. If we pass in 0, we get back struct
2041 * fuse_getxattr_out. If we pass in a non-zero size, we get back
2042 * that much data, without the struct fuse_getxattr_out header.
2045 get_xattr_in->size = 0;
2047 get_xattr_in->size = uio->uio_resid;
2049 attr_str = (char *)fdi.indata + sizeof(*get_xattr_in);
2050 snprintf(attr_str, len, "%s%c%s", prefix, extattr_namespace_separator,
2053 err = fdisp_wait_answ(&fdi);
2056 fsess_set_notimpl(mp, FUSE_GETXATTR);
2060 get_xattr_out = fdi.answ;
2062 if (ap->a_size != NULL)
2063 *ap->a_size = get_xattr_out->size;
2066 err = uiomove(fdi.answ, fdi.iosize, uio);
2069 fdisp_destroy(&fdi);
2074 struct vop_setextattr_args {
2075 struct vop_generic_args a_gen;
2077 int a_attrnamespace;
2080 struct ucred *a_cred;
2081 struct thread *a_td;
2085 fuse_vnop_setextattr(struct vop_setextattr_args *ap)
2087 struct vnode *vp = ap->a_vp;
2088 struct uio *uio = ap->a_uio;
2089 struct fuse_dispatcher fdi;
2090 struct fuse_setxattr_in *set_xattr_in;
2091 struct mount *mp = vnode_mount(vp);
2092 struct thread *td = ap->a_td;
2093 struct ucred *cred = ap->a_cred;
2099 if (fuse_isdeadfs(vp))
2102 /* Default to looking for user attributes. */
2103 if (ap->a_attrnamespace == EXTATTR_NAMESPACE_SYSTEM)
2104 prefix = EXTATTR_NAMESPACE_SYSTEM_STRING;
2106 prefix = EXTATTR_NAMESPACE_USER_STRING;
2108 len = strlen(prefix) + sizeof(extattr_namespace_separator) +
2109 strlen(ap->a_name) + 1;
2111 fdisp_init(&fdi, len + sizeof(*set_xattr_in) + uio->uio_resid);
2112 fdisp_make_vp(&fdi, FUSE_SETXATTR, vp, td, cred);
2114 set_xattr_in = fdi.indata;
2115 set_xattr_in->size = uio->uio_resid;
2117 attr_str = (char *)fdi.indata + sizeof(*set_xattr_in);
2118 snprintf(attr_str, len, "%s%c%s", prefix, extattr_namespace_separator,
2121 err = uiomove((char *)fdi.indata + sizeof(*set_xattr_in) + len,
2122 uio->uio_resid, uio);
2127 err = fdisp_wait_answ(&fdi);
2131 fsess_set_notimpl(mp, FUSE_SETXATTR);
2136 fdisp_destroy(&fdi);
2141 * The Linux / FUSE extended attribute list is simply a collection of
2142 * NUL-terminated strings. The FreeBSD extended attribute list is a single
2143 * byte length followed by a non-NUL terminated string. So, this allows
2144 * conversion of the Linux / FUSE format to the FreeBSD format in place.
2145 * Linux attribute names are reported with the namespace as a prefix (e.g.
2146 * "user.attribute_name"), but in FreeBSD they are reported without the
2147 * namespace prefix (e.g. "attribute_name"). So, we're going from:
2149 * user.attr_name1\0user.attr_name2\0
2153 * <num>attr_name1<num>attr_name2
2155 * Where "<num>" is a single byte number of characters in the attribute name.
2158 * prefix - exattr namespace prefix string
2159 * list, list_len - input list with namespace prefixes
2160 * bsd_list, bsd_list_len - output list compatible with bsd vfs
2163 fuse_xattrlist_convert(char *prefix, const char *list, int list_len,
2164 char *bsd_list, int *bsd_list_len)
2166 int len, pos, dist_to_next, prefix_len;
2170 prefix_len = strlen(prefix);
2172 while (pos < list_len && list[pos] != '\0') {
2173 dist_to_next = strlen(&list[pos]) + 1;
2174 if (bcmp(&list[pos], prefix, prefix_len) == 0 &&
2175 list[pos + prefix_len] == extattr_namespace_separator) {
2176 len = dist_to_next -
2177 (prefix_len + sizeof(extattr_namespace_separator)) - 1;
2178 if (len >= EXTATTR_MAXNAMELEN)
2179 return (ENAMETOOLONG);
2181 bsd_list[*bsd_list_len] = len;
2182 memcpy(&bsd_list[*bsd_list_len + 1],
2183 &list[pos + prefix_len +
2184 sizeof(extattr_namespace_separator)], len);
2186 *bsd_list_len += len + 1;
2189 pos += dist_to_next;
2196 struct vop_listextattr_args {
2197 struct vop_generic_args a_gen;
2199 int a_attrnamespace;
2202 struct ucred *a_cred;
2203 struct thread *a_td;
2207 fuse_vnop_listextattr(struct vop_listextattr_args *ap)
2209 struct vnode *vp = ap->a_vp;
2210 struct uio *uio = ap->a_uio;
2211 struct fuse_dispatcher fdi;
2212 struct fuse_listxattr_in *list_xattr_in;
2213 struct fuse_listxattr_out *list_xattr_out;
2214 struct mount *mp = vnode_mount(vp);
2215 struct thread *td = ap->a_td;
2216 struct ucred *cred = ap->a_cred;
2220 char *bsd_list = NULL;
2226 if (fuse_isdeadfs(vp))
2230 * Add space for a NUL and the period separator if enabled.
2231 * Default to looking for user attributes.
2233 if (ap->a_attrnamespace == EXTATTR_NAMESPACE_SYSTEM)
2234 prefix = EXTATTR_NAMESPACE_SYSTEM_STRING;
2236 prefix = EXTATTR_NAMESPACE_USER_STRING;
2238 len = strlen(prefix) + sizeof(extattr_namespace_separator) + 1;
2240 fdisp_init(&fdi, sizeof(*list_xattr_in) + len);
2241 fdisp_make_vp(&fdi, FUSE_LISTXATTR, vp, td, cred);
2244 * Retrieve Linux / FUSE compatible list size.
2246 list_xattr_in = fdi.indata;
2247 list_xattr_in->size = 0;
2248 attr_str = (char *)fdi.indata + sizeof(*list_xattr_in);
2249 snprintf(attr_str, len, "%s%c", prefix, extattr_namespace_separator);
2251 err = fdisp_wait_answ(&fdi);
2254 fsess_set_notimpl(mp, FUSE_LISTXATTR);
2258 list_xattr_out = fdi.answ;
2259 linux_list_len = list_xattr_out->size;
2260 if (linux_list_len == 0) {
2261 if (ap->a_size != NULL)
2262 *ap->a_size = linux_list_len;
2267 * Retrieve Linux / FUSE compatible list values.
2269 fdisp_make_vp(&fdi, FUSE_LISTXATTR, vp, td, cred);
2270 list_xattr_in = fdi.indata;
2271 list_xattr_in->size = linux_list_len + sizeof(*list_xattr_out);
2272 attr_str = (char *)fdi.indata + sizeof(*list_xattr_in);
2273 snprintf(attr_str, len, "%s%c", prefix, extattr_namespace_separator);
2275 err = fdisp_wait_answ(&fdi);
2279 linux_list = fdi.answ;
2280 linux_list_len = fdi.iosize;
2283 * Retrieve the BSD compatible list values.
2284 * The Linux / FUSE attribute list format isn't the same
2285 * as FreeBSD's format. So we need to transform it into
2286 * FreeBSD's format before giving it to the user.
2288 bsd_list = malloc(linux_list_len, M_TEMP, M_WAITOK);
2289 err = fuse_xattrlist_convert(prefix, linux_list, linux_list_len,
2290 bsd_list, &bsd_list_len);
2294 if (ap->a_size != NULL)
2295 *ap->a_size = bsd_list_len;
2298 err = uiomove(bsd_list, bsd_list_len, uio);
2301 free(bsd_list, M_TEMP);
2302 fdisp_destroy(&fdi);
2307 struct vop_deleteextattr_args {
2308 struct vop_generic_args a_gen;
2310 int a_attrnamespace;
2312 struct ucred *a_cred;
2313 struct thread *a_td;
2317 fuse_vnop_deleteextattr(struct vop_deleteextattr_args *ap)
2319 struct vnode *vp = ap->a_vp;
2320 struct fuse_dispatcher fdi;
2321 struct mount *mp = vnode_mount(vp);
2322 struct thread *td = ap->a_td;
2323 struct ucred *cred = ap->a_cred;
2329 if (fuse_isdeadfs(vp))
2332 /* Default to looking for user attributes. */
2333 if (ap->a_attrnamespace == EXTATTR_NAMESPACE_SYSTEM)
2334 prefix = EXTATTR_NAMESPACE_SYSTEM_STRING;
2336 prefix = EXTATTR_NAMESPACE_USER_STRING;
2338 len = strlen(prefix) + sizeof(extattr_namespace_separator) +
2339 strlen(ap->a_name) + 1;
2341 fdisp_init(&fdi, len);
2342 fdisp_make_vp(&fdi, FUSE_REMOVEXATTR, vp, td, cred);
2344 attr_str = fdi.indata;
2345 snprintf(attr_str, len, "%s%c%s", prefix, extattr_namespace_separator,
2348 err = fdisp_wait_answ(&fdi);
2351 fsess_set_notimpl(mp, FUSE_REMOVEXATTR);
2354 fdisp_destroy(&fdi);
2359 struct vnop_print_args {
2364 fuse_vnop_print(struct vop_print_args *ap)
2366 struct fuse_vnode_data *fvdat = VTOFUD(ap->a_vp);
2368 printf("nodeid: %ju, parent nodeid: %ju, nlookup: %ju, flag: %#x\n",
2369 (uintmax_t)VTOILLU(ap->a_vp), (uintmax_t)fvdat->parent_nid,
2370 (uintmax_t)fvdat->nlookup,