2 * SPDX-License-Identifier: BSD-3-Clause
4 * Copyright (c) 2007-2009 Google Inc. and Amit Singh
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions are
11 * * Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * * Redistributions in binary form must reproduce the above
14 * copyright notice, this list of conditions and the following disclaimer
15 * in the documentation and/or other materials provided with the
17 * * Neither the name of Google Inc. nor the names of its
18 * contributors may be used to endorse or promote products derived from
19 * this software without specific prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
24 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
25 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
26 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
27 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
31 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33 * Copyright (C) 2005 Csaba Henk.
34 * All rights reserved.
36 * Redistribution and use in source and binary forms, with or without
37 * modification, are permitted provided that the following conditions
39 * 1. Redistributions of source code must retain the above copyright
40 * notice, this list of conditions and the following disclaimer.
41 * 2. Redistributions in binary form must reproduce the above copyright
42 * notice, this list of conditions and the following disclaimer in the
43 * documentation and/or other materials provided with the distribution.
45 * THIS SOFTWARE IS PROVIDED BY AUTHOR AND CONTRIBUTORS ``AS IS'' AND
46 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
47 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
48 * ARE DISCLAIMED. IN NO EVENT SHALL AUTHOR OR CONTRIBUTORS BE LIABLE
49 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
50 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
51 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
52 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
53 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
54 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
58 #include <sys/cdefs.h>
59 __FBSDID("$FreeBSD$");
61 #include <sys/param.h>
62 #include <sys/module.h>
63 #include <sys/systm.h>
64 #include <sys/errno.h>
65 #include <sys/kernel.h>
68 #include <sys/malloc.h>
69 #include <sys/queue.h>
71 #include <sys/rwlock.h>
74 #include <sys/mount.h>
75 #include <sys/vnode.h>
76 #include <sys/namei.h>
77 #include <sys/extattr.h>
79 #include <sys/unistd.h>
80 #include <sys/filedesc.h>
82 #include <sys/fcntl.h>
83 #include <sys/dirent.h>
86 #include <sys/sysctl.h>
87 #include <sys/vmmeter.h>
90 #include <vm/vm_extern.h>
92 #include <vm/vm_map.h>
93 #include <vm/vm_page.h>
94 #include <vm/vm_param.h>
95 #include <vm/vm_object.h>
96 #include <vm/vm_pager.h>
97 #include <vm/vnode_pager.h>
98 #include <vm/vm_object.h>
101 #include "fuse_file.h"
102 #include "fuse_internal.h"
103 #include "fuse_ipc.h"
104 #include "fuse_node.h"
107 #include <sys/priv.h>
109 /* Maximum number of hardlinks to a single FUSE file */
110 #define FUSE_LINK_MAX UINT32_MAX
112 SDT_PROVIDER_DECLARE(fuse);
115 * arg0: verbosity. Higher numbers give more verbose messages
116 * arg1: Textual message
118 SDT_PROBE_DEFINE2(fuse, , vnops, trace, "int", "char*");
121 static vop_access_t fuse_vnop_access;
122 static vop_close_t fuse_vnop_close;
123 static vop_create_t fuse_vnop_create;
124 static vop_deleteextattr_t fuse_vnop_deleteextattr;
125 static vop_fdatasync_t fuse_vnop_fdatasync;
126 static vop_fsync_t fuse_vnop_fsync;
127 static vop_getattr_t fuse_vnop_getattr;
128 static vop_getextattr_t fuse_vnop_getextattr;
129 static vop_inactive_t fuse_vnop_inactive;
130 static vop_link_t fuse_vnop_link;
131 static vop_listextattr_t fuse_vnop_listextattr;
132 static vop_lookup_t fuse_vnop_lookup;
133 static vop_mkdir_t fuse_vnop_mkdir;
134 static vop_mknod_t fuse_vnop_mknod;
135 static vop_open_t fuse_vnop_open;
136 static vop_pathconf_t fuse_vnop_pathconf;
137 static vop_read_t fuse_vnop_read;
138 static vop_readdir_t fuse_vnop_readdir;
139 static vop_readlink_t fuse_vnop_readlink;
140 static vop_reclaim_t fuse_vnop_reclaim;
141 static vop_remove_t fuse_vnop_remove;
142 static vop_rename_t fuse_vnop_rename;
143 static vop_rmdir_t fuse_vnop_rmdir;
144 static vop_setattr_t fuse_vnop_setattr;
145 static vop_setextattr_t fuse_vnop_setextattr;
146 static vop_strategy_t fuse_vnop_strategy;
147 static vop_symlink_t fuse_vnop_symlink;
148 static vop_write_t fuse_vnop_write;
149 static vop_getpages_t fuse_vnop_getpages;
150 static vop_putpages_t fuse_vnop_putpages;
151 static vop_print_t fuse_vnop_print;
153 struct vop_vector fuse_vnops = {
154 .vop_default = &default_vnodeops,
155 .vop_access = fuse_vnop_access,
156 .vop_close = fuse_vnop_close,
157 .vop_create = fuse_vnop_create,
158 .vop_deleteextattr = fuse_vnop_deleteextattr,
159 .vop_fsync = fuse_vnop_fsync,
160 .vop_fdatasync = fuse_vnop_fdatasync,
161 .vop_getattr = fuse_vnop_getattr,
162 .vop_getextattr = fuse_vnop_getextattr,
163 .vop_inactive = fuse_vnop_inactive,
164 .vop_link = fuse_vnop_link,
165 .vop_listextattr = fuse_vnop_listextattr,
166 .vop_lookup = fuse_vnop_lookup,
167 .vop_mkdir = fuse_vnop_mkdir,
168 .vop_mknod = fuse_vnop_mknod,
169 .vop_open = fuse_vnop_open,
170 .vop_pathconf = fuse_vnop_pathconf,
171 .vop_read = fuse_vnop_read,
172 .vop_readdir = fuse_vnop_readdir,
173 .vop_readlink = fuse_vnop_readlink,
174 .vop_reclaim = fuse_vnop_reclaim,
175 .vop_remove = fuse_vnop_remove,
176 .vop_rename = fuse_vnop_rename,
177 .vop_rmdir = fuse_vnop_rmdir,
178 .vop_setattr = fuse_vnop_setattr,
179 .vop_setextattr = fuse_vnop_setextattr,
180 .vop_strategy = fuse_vnop_strategy,
181 .vop_symlink = fuse_vnop_symlink,
182 .vop_write = fuse_vnop_write,
183 .vop_getpages = fuse_vnop_getpages,
184 .vop_putpages = fuse_vnop_putpages,
185 .vop_print = fuse_vnop_print,
188 static u_long fuse_lookup_cache_hits = 0;
190 SYSCTL_ULONG(_vfs_fusefs, OID_AUTO, lookup_cache_hits, CTLFLAG_RD,
191 &fuse_lookup_cache_hits, 0, "number of positive cache hits in lookup");
193 static u_long fuse_lookup_cache_misses = 0;
195 SYSCTL_ULONG(_vfs_fusefs, OID_AUTO, lookup_cache_misses, CTLFLAG_RD,
196 &fuse_lookup_cache_misses, 0, "number of cache misses in lookup");
199 * XXX: This feature is highly experimental and can bring to instabilities,
200 * needs revisiting before to be enabled by default.
202 static int fuse_reclaim_revoked = 0;
204 SYSCTL_INT(_vfs_fusefs, OID_AUTO, reclaim_revoked, CTLFLAG_RW,
205 &fuse_reclaim_revoked, 0, "");
207 uma_zone_t fuse_pbuf_zone;
209 #define fuse_vm_page_lock(m) vm_page_lock((m));
210 #define fuse_vm_page_unlock(m) vm_page_unlock((m));
211 #define fuse_vm_page_lock_queues() ((void)0)
212 #define fuse_vm_page_unlock_queues() ((void)0)
214 /* Check permission for extattr operations, much like extattr_check_cred */
216 fuse_extattr_check_cred(struct vnode *vp, int ns, struct ucred *cred,
217 struct thread *td, accmode_t accmode)
219 struct mount *mp = vnode_mount(vp);
220 struct fuse_data *data = fuse_get_mpdata(mp);
223 * Kernel-invoked always succeeds.
229 * Do not allow privileged processes in jail to directly manipulate
233 case EXTATTR_NAMESPACE_SYSTEM:
234 if (data->dataflags & FSESS_DEFAULT_PERMISSIONS) {
235 return (priv_check_cred(cred, PRIV_VFS_EXTATTR_SYSTEM));
238 case EXTATTR_NAMESPACE_USER:
239 return (fuse_internal_access(vp, accmode, td, cred));
245 /* Get a filehandle for a directory */
247 fuse_filehandle_get_dir(struct vnode *vp, struct fuse_filehandle **fufhp,
248 struct ucred *cred, pid_t pid)
250 if (fuse_filehandle_get(vp, FREAD, fufhp, cred, pid) == 0)
252 return fuse_filehandle_get(vp, FEXEC, fufhp, cred, pid);
255 /* Send FUSE_FLUSH for this vnode */
257 fuse_flush(struct vnode *vp, struct ucred *cred, pid_t pid, int fflag)
259 struct fuse_flush_in *ffi;
260 struct fuse_filehandle *fufh;
261 struct fuse_dispatcher fdi;
262 struct thread *td = curthread;
263 struct mount *mp = vnode_mount(vp);
266 if (!fsess_isimpl(vnode_mount(vp), FUSE_FLUSH))
269 err = fuse_filehandle_get(vp, fflag, &fufh, cred, pid);
273 fdisp_init(&fdi, sizeof(*ffi));
274 fdisp_make_vp(&fdi, FUSE_FLUSH, vp, td, cred);
276 ffi->fh = fufh->fh_id;
278 err = fdisp_wait_answ(&fdi);
280 fsess_set_notimpl(mp, FUSE_FLUSH);
288 struct vnop_access_args {
290 #if VOP_ACCESS_TAKES_ACCMODE_T
295 struct ucred *a_cred;
300 fuse_vnop_access(struct vop_access_args *ap)
302 struct vnode *vp = ap->a_vp;
303 int accmode = ap->a_accmode;
304 struct ucred *cred = ap->a_cred;
306 struct fuse_data *data = fuse_get_mpdata(vnode_mount(vp));
310 if (fuse_isdeadfs(vp)) {
311 if (vnode_isvroot(vp)) {
316 if (!(data->dataflags & FSESS_INITED)) {
317 if (vnode_isvroot(vp)) {
318 if (priv_check_cred(cred, PRIV_VFS_ADMIN) ||
319 (fuse_match_cred(data->daemoncred, cred) == 0)) {
325 if (vnode_islnk(vp)) {
329 err = fuse_internal_access(vp, accmode, ap->a_td, ap->a_cred);
334 struct vop_close_args {
337 struct ucred *a_cred;
342 fuse_vnop_close(struct vop_close_args *ap)
344 struct vnode *vp = ap->a_vp;
345 struct ucred *cred = ap->a_cred;
346 int fflag = ap->a_fflag;
347 struct thread *td = ap->a_td;
348 pid_t pid = td->td_proc->p_pid;
351 if (fuse_isdeadfs(vp))
355 if (fflag & IO_NDELAY)
358 err = fuse_flush(vp, cred, pid, fflag);
359 /* TODO: close the file handle, if we're sure it's no longer used */
360 if ((VTOFUD(vp)->flag & FN_SIZECHANGE) != 0) {
361 fuse_vnode_savesize(vp, cred, td->td_proc->p_pid);
367 fdisp_make_mknod_for_fallback(
368 struct fuse_dispatcher *fdip,
369 struct componentname *cnp,
375 enum fuse_opcode *op)
377 struct fuse_mknod_in *fmni;
379 fdisp_init(fdip, sizeof(*fmni) + cnp->cn_namelen + 1);
381 fdisp_make(fdip, *op, vnode_mount(dvp), parentnid, td, cred);
385 memcpy((char *)fdip->indata + sizeof(*fmni), cnp->cn_nameptr,
387 ((char *)fdip->indata)[sizeof(*fmni) + cnp->cn_namelen] = '\0';
390 struct vnop_create_args {
392 struct vnode **a_vpp;
393 struct componentname *a_cnp;
398 fuse_vnop_create(struct vop_create_args *ap)
400 struct vnode *dvp = ap->a_dvp;
401 struct vnode **vpp = ap->a_vpp;
402 struct componentname *cnp = ap->a_cnp;
403 struct vattr *vap = ap->a_vap;
404 struct thread *td = cnp->cn_thread;
405 struct ucred *cred = cnp->cn_cred;
407 struct fuse_open_in *foi;
408 struct fuse_entry_out *feo;
409 struct fuse_open_out *foo;
410 struct fuse_dispatcher fdi, fdi2;
411 struct fuse_dispatcher *fdip = &fdi;
412 struct fuse_dispatcher *fdip2 = NULL;
416 struct mount *mp = vnode_mount(dvp);
417 uint64_t parentnid = VTOFUD(dvp)->nid;
418 mode_t mode = MAKEIMODE(vap->va_type, vap->va_mode);
423 * VOP_CREATE doesn't tell us the open(2) flags, so we guess. Only a
424 * writable mode makes sense, and we might as well include readability
429 if (fuse_isdeadfs(dvp)) {
432 bzero(&fdi, sizeof(fdi));
434 if ((vap->va_type != VREG))
437 if (!fsess_isimpl(mp, FUSE_CREATE)) {
438 /* Fallback to FUSE_MKNOD/FUSE_OPEN */
439 fdisp_make_mknod_for_fallback(fdip, cnp, dvp, parentnid, td,
442 /* Use FUSE_CREATE */
444 fdisp_init(fdip, sizeof(*foi) + cnp->cn_namelen + 1);
445 fdisp_make(fdip, op, vnode_mount(dvp), parentnid, td, cred);
448 foi->flags = O_CREAT | flags;
449 memcpy((char *)fdip->indata + sizeof(*foi), cnp->cn_nameptr,
451 ((char *)fdip->indata)[sizeof(*foi) + cnp->cn_namelen] = '\0';
454 err = fdisp_wait_answ(fdip);
457 if (err == ENOSYS && op == FUSE_CREATE) {
458 fsess_set_notimpl(mp, FUSE_CREATE);
459 fdisp_make_mknod_for_fallback(fdip, cnp, dvp,
460 parentnid, td, cred, mode, &op);
461 err = fdisp_wait_answ(fdip);
469 if ((err = fuse_internal_checkentry(feo, VREG))) {
473 if (op == FUSE_CREATE) {
474 foo = (struct fuse_open_out*)(feo + 1);
476 /* Issue a separate FUSE_OPEN */
478 fdisp_init(fdip2, sizeof(*foi));
479 fdisp_make(fdip2, FUSE_OPEN, vnode_mount(dvp), feo->nodeid, td,
484 err = fdisp_wait_answ(fdip2);
489 err = fuse_vnode_get(mp, feo, feo->nodeid, dvp, vpp, cnp, VREG);
491 struct fuse_release_in *fri;
492 uint64_t nodeid = feo->nodeid;
493 uint64_t fh_id = foo->fh;
495 fdisp_init(fdip, sizeof(*fri));
496 fdisp_make(fdip, FUSE_RELEASE, mp, nodeid, td, cred);
500 fuse_insert_callback(fdip->tick, fuse_internal_forget_callback);
501 fuse_insert_message(fdip->tick);
504 ASSERT_VOP_ELOCKED(*vpp, "fuse_vnop_create");
505 fuse_internal_cache_attrs(*vpp, &feo->attr, feo->attr_valid,
506 feo->attr_valid_nsec, NULL);
508 fuse_filehandle_init(*vpp, FUFH_RDWR, NULL, td, cred, foo);
509 fuse_vnode_open(*vpp, foo->open_flags, td);
510 cache_purge_negative(dvp);
514 fdisp_destroy(fdip2);
520 struct vnop_fdatasync_args {
521 struct vop_generic_args a_gen;
523 struct thread * a_td;
527 fuse_vnop_fdatasync(struct vop_fdatasync_args *ap)
529 struct vnode *vp = ap->a_vp;
530 struct thread *td = ap->a_td;
531 int waitfor = MNT_WAIT;
535 if (fuse_isdeadfs(vp)) {
538 if ((err = vop_stdfdatasync_buf(ap)))
541 return fuse_internal_fsync(vp, td, waitfor, true);
545 struct vnop_fsync_args {
546 struct vop_generic_args a_gen;
549 struct thread * a_td;
553 fuse_vnop_fsync(struct vop_fsync_args *ap)
555 struct vnode *vp = ap->a_vp;
556 struct thread *td = ap->a_td;
557 int waitfor = ap->a_waitfor;
560 if (fuse_isdeadfs(vp)) {
563 if ((err = vop_stdfsync(ap)))
566 return fuse_internal_fsync(vp, td, waitfor, false);
570 struct vnop_getattr_args {
573 struct ucred *a_cred;
578 fuse_vnop_getattr(struct vop_getattr_args *ap)
580 struct vnode *vp = ap->a_vp;
581 struct vattr *vap = ap->a_vap;
582 struct ucred *cred = ap->a_cred;
583 struct thread *td = curthread;
588 dataflags = fuse_get_mpdata(vnode_mount(vp))->dataflags;
590 /* Note that we are not bailing out on a dead file system just yet. */
592 if (!(dataflags & FSESS_INITED)) {
593 if (!vnode_isvroot(vp)) {
594 fdata_set_dead(fuse_get_mpdata(vnode_mount(vp)));
601 err = fuse_internal_getattr(vp, vap, cred, td);
602 if (err == ENOTCONN && vnode_isvroot(vp)) {
603 /* see comment in fuse_vfsop_statfs() */
610 bzero(vap, sizeof(*vap));
611 vap->va_type = vnode_vtype(vp);
617 struct vnop_inactive_args {
623 fuse_vnop_inactive(struct vop_inactive_args *ap)
625 struct vnode *vp = ap->a_vp;
626 struct thread *td = ap->a_td;
628 struct fuse_vnode_data *fvdat = VTOFUD(vp);
629 struct fuse_filehandle *fufh, *fufh_tmp;
633 LIST_FOREACH_SAFE(fufh, &fvdat->handles, next, fufh_tmp) {
634 if (need_flush && vp->v_type == VREG) {
635 if ((VTOFUD(vp)->flag & FN_SIZECHANGE) != 0) {
636 fuse_vnode_savesize(vp, NULL, 0);
638 if (fuse_data_cache_invalidate ||
639 (fvdat->flag & FN_REVOKED) != 0)
640 fuse_io_invalbuf(vp, td);
642 fuse_io_flushbuf(vp, MNT_WAIT, td);
645 fuse_filehandle_close(vp, fufh, td, NULL);
648 if ((fvdat->flag & FN_REVOKED) != 0 && fuse_reclaim_revoked) {
655 struct vnop_link_args {
656 struct vnode *a_tdvp;
658 struct componentname *a_cnp;
662 fuse_vnop_link(struct vop_link_args *ap)
664 struct vnode *vp = ap->a_vp;
665 struct vnode *tdvp = ap->a_tdvp;
666 struct componentname *cnp = ap->a_cnp;
668 struct vattr *vap = VTOVA(vp);
670 struct fuse_dispatcher fdi;
671 struct fuse_entry_out *feo;
672 struct fuse_link_in fli;
676 if (fuse_isdeadfs(vp)) {
679 if (vnode_mount(tdvp) != vnode_mount(vp)) {
684 * This is a seatbelt check to protect naive userspace filesystems from
685 * themselves and the limitations of the FUSE IPC protocol. If a
686 * filesystem does not allow attribute caching, assume it is capable of
687 * validating that nlink does not overflow.
689 if (vap != NULL && vap->va_nlink >= FUSE_LINK_MAX)
691 fli.oldnodeid = VTOI(vp);
694 fuse_internal_newentry_makerequest(vnode_mount(tdvp), VTOI(tdvp), cnp,
695 FUSE_LINK, &fli, sizeof(fli), &fdi);
696 if ((err = fdisp_wait_answ(&fdi))) {
701 err = fuse_internal_checkentry(feo, vnode_vtype(vp));
707 struct fuse_lookup_alloc_arg {
708 struct fuse_entry_out *feo;
709 struct componentname *cnp;
714 /* Callback for vn_get_ino */
716 fuse_lookup_alloc(struct mount *mp, void *arg, int lkflags, struct vnode **vpp)
718 struct fuse_lookup_alloc_arg *flaa = arg;
720 return fuse_vnode_get(mp, flaa->feo, flaa->nid, NULL, vpp, flaa->cnp,
724 SDT_PROBE_DEFINE3(fuse, , vnops, cache_lookup,
725 "int", "struct timespec*", "struct timespec*");
727 struct vnop_lookup_args {
728 struct vnodeop_desc *a_desc;
730 struct vnode **a_vpp;
731 struct componentname *a_cnp;
735 fuse_vnop_lookup(struct vop_lookup_args *ap)
737 struct vnode *dvp = ap->a_dvp;
738 struct vnode **vpp = ap->a_vpp;
739 struct componentname *cnp = ap->a_cnp;
740 struct thread *td = cnp->cn_thread;
741 struct ucred *cred = cnp->cn_cred;
743 int nameiop = cnp->cn_nameiop;
744 int flags = cnp->cn_flags;
745 int wantparent = flags & (LOCKPARENT | WANTPARENT);
746 int islastcn = flags & ISLASTCN;
747 struct mount *mp = vnode_mount(dvp);
751 struct vnode *vp = NULL;
753 struct fuse_dispatcher fdi;
754 bool did_lookup = false;
755 struct fuse_entry_out *feo = NULL;
756 enum vtype vtyp; /* vnode type of target */
757 off_t filesize; /* filesize of target */
761 if (fuse_isdeadfs(dvp)) {
765 if (!vnode_isdir(dvp))
768 if (islastcn && vfs_isrdonly(mp) && (nameiop != LOOKUP))
771 if ((err = fuse_internal_access(dvp, VEXEC, td, cred)))
774 if (flags & ISDOTDOT) {
775 nid = VTOFUD(dvp)->parent_nid;
778 /* .. is obviously a directory */
781 } else if (cnp->cn_namelen == 1 && *(cnp->cn_nameptr) == '.') {
783 /* . is obviously a directory */
787 struct timespec now, timeout;
789 err = cache_lookup(dvp, vpp, cnp, &timeout, NULL);
791 SDT_PROBE3(fuse, , vnops, cache_lookup, err, &timeout, &now);
793 case -1: /* positive match */
794 if (timespeccmp(&timeout, &now, >)) {
795 atomic_add_acq_long(&fuse_lookup_cache_hits, 1);
798 atomic_add_acq_long(&fuse_lookup_cache_misses,
810 case 0: /* no match in cache */
811 atomic_add_acq_long(&fuse_lookup_cache_misses, 1);
814 case ENOENT: /* negative match */
816 if (timespeccmp(&timeout, &now, <=)) {
818 cache_purge_negative(dvp);
827 fdisp_init(&fdi, cnp->cn_namelen + 1);
828 fdisp_make(&fdi, FUSE_LOOKUP, mp, nid, td, cred);
830 memcpy(fdi.indata, cnp->cn_nameptr, cnp->cn_namelen);
831 ((char *)fdi.indata)[cnp->cn_namelen] = '\0';
832 lookup_err = fdisp_wait_answ(&fdi);
836 /* lookup call succeeded */
837 nid = ((struct fuse_entry_out *)fdi.answ)->nodeid;
838 feo = (struct fuse_entry_out *)fdi.answ;
840 /* zero nodeid means ENOENT and cache it */
841 struct timespec timeout;
843 fdi.answ_stat = ENOENT;
845 if (cnp->cn_flags & MAKEENTRY) {
846 fuse_validity_2_timespec(feo, &timeout);
847 cache_enter_time(dvp, *vpp, cnp,
850 } else if (nid == FUSE_ROOT_ID) {
853 vtyp = IFTOVT(feo->attr.mode);
854 filesize = feo->attr.size;
856 if (lookup_err && (!fdi.answ_stat || lookup_err != ENOENT)) {
861 /* lookup_err, if non-zero, must be ENOENT at this point */
864 /* Entry not found */
865 if ((nameiop == CREATE || nameiop == RENAME) && islastcn) {
866 err = fuse_internal_access(dvp, VWRITE, td, cred);
869 * Set the SAVENAME flag to hold onto the
870 * pathname for use later in VOP_CREATE or
873 cnp->cn_flags |= SAVENAME;
881 /* Entry was found */
882 if (flags & ISDOTDOT) {
883 struct fuse_lookup_alloc_arg flaa;
889 err = vn_vget_ino_gen(dvp, fuse_lookup_alloc, &flaa, 0,
892 } else if (nid == VTOI(dvp)) {
896 struct fuse_vnode_data *fvdat;
898 err = fuse_vnode_get(vnode_mount(dvp), feo, nid, dvp,
904 fuse_vnode_setparent(vp, dvp);
907 * In the case where we are looking up a FUSE node
908 * represented by an existing cached vnode, and the
909 * true size reported by FUSE_LOOKUP doesn't match
910 * the vnode's cached size, fix the vnode cache to
911 * match the real object size.
914 * * following attribute cache expiration, or
915 * * due a bug in the daemon, or
916 * * the first time that we looked up the file.
919 if (vnode_isreg(vp) &&
920 filesize != fvdat->filesize) {
922 * The FN_SIZECHANGE flag reflects a dirty
923 * append. If userspace lets us know our cache
924 * is invalid, that write was lost. (Dirty
925 * writes that do not cause append are also
926 * lost, but we don't detect them here.)
928 * XXX: Maybe disable WB caching on this mount.
930 if (fvdat->flag & FN_SIZECHANGE)
931 printf("%s: WB cache incoherent on "
933 vnode_mount(vp)->mnt_stat.f_mntonname);
935 (void)fuse_vnode_setsize(vp, cred, filesize);
936 fvdat->flag &= ~FN_SIZECHANGE;
940 fuse_internal_cache_attrs(*vpp, &feo->attr,
941 feo->attr_valid, feo->attr_valid_nsec, NULL);
943 if ((nameiop == DELETE || nameiop == RENAME) &&
948 err = fuse_internal_access(dvp, VWRITE, td,
953 * if the parent's sticky bit is set, check
954 * whether we're allowed to remove the file.
955 * Need to figure out the vnode locking to make
958 fuse_internal_getattr(dvp, &dvattr, cred, td);
959 if ((dvattr.va_mode & S_ISTXT) &&
960 fuse_internal_access(dvp, VADMIN, td,
962 fuse_internal_access(*vpp, VADMIN, td,
970 (nameiop == DELETE) ||
971 (nameiop == RENAME && wantparent))) {
972 cnp->cn_flags |= SAVENAME;
979 if (vp != NULL && dvp != vp)
992 struct vnop_mkdir_args {
994 struct vnode **a_vpp;
995 struct componentname *a_cnp;
1000 fuse_vnop_mkdir(struct vop_mkdir_args *ap)
1002 struct vnode *dvp = ap->a_dvp;
1003 struct vnode **vpp = ap->a_vpp;
1004 struct componentname *cnp = ap->a_cnp;
1005 struct vattr *vap = ap->a_vap;
1007 struct fuse_mkdir_in fmdi;
1009 if (fuse_isdeadfs(dvp)) {
1012 fmdi.mode = MAKEIMODE(vap->va_type, vap->va_mode);
1014 return (fuse_internal_newentry(dvp, vpp, cnp, FUSE_MKDIR, &fmdi,
1015 sizeof(fmdi), VDIR));
1019 struct vnop_mknod_args {
1020 struct vnode *a_dvp;
1021 struct vnode **a_vpp;
1022 struct componentname *a_cnp;
1023 struct vattr *a_vap;
1027 fuse_vnop_mknod(struct vop_mknod_args *ap)
1030 struct vnode *dvp = ap->a_dvp;
1031 struct vnode **vpp = ap->a_vpp;
1032 struct componentname *cnp = ap->a_cnp;
1033 struct vattr *vap = ap->a_vap;
1034 struct fuse_mknod_in fmni;
1036 if (fuse_isdeadfs(dvp))
1039 fmni.mode = MAKEIMODE(vap->va_type, vap->va_mode);
1040 fmni.rdev = vap->va_rdev;
1041 return (fuse_internal_newentry(dvp, vpp, cnp, FUSE_MKNOD, &fmni,
1042 sizeof(fmni), vap->va_type));
1046 struct vnop_open_args {
1049 struct ucred *a_cred;
1050 struct thread *a_td;
1051 int a_fdidx; / struct file *a_fp;
1055 fuse_vnop_open(struct vop_open_args *ap)
1057 struct vnode *vp = ap->a_vp;
1058 int a_mode = ap->a_mode;
1059 struct thread *td = ap->a_td;
1060 struct ucred *cred = ap->a_cred;
1061 pid_t pid = td->td_proc->p_pid;
1062 struct fuse_vnode_data *fvdat;
1064 if (fuse_isdeadfs(vp))
1066 if (vp->v_type == VCHR || vp->v_type == VBLK || vp->v_type == VFIFO)
1067 return (EOPNOTSUPP);
1068 if ((a_mode & (FREAD | FWRITE | FEXEC)) == 0)
1073 if (fuse_filehandle_validrw(vp, a_mode, cred, pid)) {
1074 fuse_vnode_open(vp, 0, td);
1078 return fuse_filehandle_open(vp, a_mode, NULL, td, cred);
1082 fuse_vnop_pathconf(struct vop_pathconf_args *ap)
1085 switch (ap->a_name) {
1086 case _PC_FILESIZEBITS:
1090 *ap->a_retval = NAME_MAX;
1093 *ap->a_retval = MIN(LONG_MAX, FUSE_LINK_MAX);
1095 case _PC_SYMLINK_MAX:
1096 *ap->a_retval = MAXPATHLEN;
1102 return (vop_stdpathconf(ap));
1107 struct vnop_read_args {
1111 struct ucred *a_cred;
1115 fuse_vnop_read(struct vop_read_args *ap)
1117 struct vnode *vp = ap->a_vp;
1118 struct uio *uio = ap->a_uio;
1119 int ioflag = ap->a_ioflag;
1120 struct ucred *cred = ap->a_cred;
1121 pid_t pid = curthread->td_proc->p_pid;
1123 if (fuse_isdeadfs(vp)) {
1127 if (VTOFUD(vp)->flag & FN_DIRECTIO) {
1128 ioflag |= IO_DIRECT;
1131 return fuse_io_dispatch(vp, uio, ioflag, cred, pid);
1135 struct vnop_readdir_args {
1138 struct ucred *a_cred;
1145 fuse_vnop_readdir(struct vop_readdir_args *ap)
1147 struct vnode *vp = ap->a_vp;
1148 struct uio *uio = ap->a_uio;
1149 struct ucred *cred = ap->a_cred;
1150 struct fuse_filehandle *fufh = NULL;
1151 struct fuse_iov cookediov;
1153 pid_t pid = curthread->td_proc->p_pid;
1155 if (fuse_isdeadfs(vp)) {
1158 if ( /* XXXIP ((uio_iovcnt(uio) > 1)) || */
1159 (uio_resid(uio) < sizeof(struct dirent))) {
1163 err = fuse_filehandle_get_dir(vp, &fufh, cred, pid);
1166 #define DIRCOOKEDSIZE FUSE_DIRENT_ALIGN(FUSE_NAME_OFFSET + MAXNAMLEN + 1)
1167 fiov_init(&cookediov, DIRCOOKEDSIZE);
1169 err = fuse_internal_readdir(vp, uio, fufh, &cookediov);
1171 fiov_teardown(&cookediov);
1177 struct vnop_readlink_args {
1180 struct ucred *a_cred;
1184 fuse_vnop_readlink(struct vop_readlink_args *ap)
1186 struct vnode *vp = ap->a_vp;
1187 struct uio *uio = ap->a_uio;
1188 struct ucred *cred = ap->a_cred;
1190 struct fuse_dispatcher fdi;
1193 if (fuse_isdeadfs(vp)) {
1196 if (!vnode_islnk(vp)) {
1199 fdisp_init(&fdi, 0);
1200 err = fdisp_simple_putget_vp(&fdi, FUSE_READLINK, vp, curthread, cred);
1204 if (((char *)fdi.answ)[0] == '/' &&
1205 fuse_get_mpdata(vnode_mount(vp))->dataflags & FSESS_PUSH_SYMLINKS_IN) {
1206 char *mpth = vnode_mount(vp)->mnt_stat.f_mntonname;
1208 err = uiomove(mpth, strlen(mpth), uio);
1211 err = uiomove(fdi.answ, fdi.iosize, uio);
1214 fdisp_destroy(&fdi);
1219 struct vnop_reclaim_args {
1221 struct thread *a_td;
1225 fuse_vnop_reclaim(struct vop_reclaim_args *ap)
1227 struct vnode *vp = ap->a_vp;
1228 struct thread *td = ap->a_td;
1229 struct fuse_vnode_data *fvdat = VTOFUD(vp);
1230 struct fuse_filehandle *fufh, *fufh_tmp;
1233 panic("FUSE: no vnode data during recycling");
1235 LIST_FOREACH_SAFE(fufh, &fvdat->handles, next, fufh_tmp) {
1236 printf("FUSE: vnode being reclaimed with open fufh "
1237 "(type=%#x)", fufh->fufh_type);
1238 fuse_filehandle_close(vp, fufh, td, NULL);
1241 if ((!fuse_isdeadfs(vp)) && (fvdat->nlookup)) {
1242 fuse_internal_forget_send(vnode_mount(vp), td, NULL, VTOI(vp),
1245 fuse_vnode_setparent(vp, NULL);
1247 vfs_hash_remove(vp);
1248 vnode_destroy_vobject(vp);
1249 fuse_vnode_destroy(vp);
1255 struct vnop_remove_args {
1256 struct vnode *a_dvp;
1258 struct componentname *a_cnp;
1262 fuse_vnop_remove(struct vop_remove_args *ap)
1264 struct vnode *dvp = ap->a_dvp;
1265 struct vnode *vp = ap->a_vp;
1266 struct componentname *cnp = ap->a_cnp;
1270 if (fuse_isdeadfs(vp)) {
1273 if (vnode_isdir(vp)) {
1278 err = fuse_internal_remove(dvp, vp, cnp, FUSE_UNLINK);
1281 fuse_internal_vnode_disappear(vp);
1286 struct vnop_rename_args {
1287 struct vnode *a_fdvp;
1288 struct vnode *a_fvp;
1289 struct componentname *a_fcnp;
1290 struct vnode *a_tdvp;
1291 struct vnode *a_tvp;
1292 struct componentname *a_tcnp;
1296 fuse_vnop_rename(struct vop_rename_args *ap)
1298 struct vnode *fdvp = ap->a_fdvp;
1299 struct vnode *fvp = ap->a_fvp;
1300 struct componentname *fcnp = ap->a_fcnp;
1301 struct vnode *tdvp = ap->a_tdvp;
1302 struct vnode *tvp = ap->a_tvp;
1303 struct componentname *tcnp = ap->a_tcnp;
1304 struct fuse_data *data;
1308 if (fuse_isdeadfs(fdvp)) {
1311 if (fvp->v_mount != tdvp->v_mount ||
1312 (tvp && fvp->v_mount != tvp->v_mount)) {
1313 SDT_PROBE2(fuse, , vnops, trace, 1, "cross-device rename");
1320 * FUSE library is expected to check if target directory is not
1321 * under the source directory in the file system tree.
1322 * Linux performs this check at VFS level.
1324 data = fuse_get_mpdata(vnode_mount(tdvp));
1325 sx_xlock(&data->rename_lock);
1326 err = fuse_internal_rename(fdvp, fcnp, tdvp, tcnp);
1329 fuse_vnode_setparent(fvp, tdvp);
1331 fuse_vnode_setparent(tvp, NULL);
1333 sx_unlock(&data->rename_lock);
1335 if (tvp != NULL && tvp != fvp) {
1338 if (vnode_isdir(fvp)) {
1339 if ((tvp != NULL) && vnode_isdir(tvp)) {
1360 struct vnop_rmdir_args {
1361 struct vnode *a_dvp;
1363 struct componentname *a_cnp;
1367 fuse_vnop_rmdir(struct vop_rmdir_args *ap)
1369 struct vnode *dvp = ap->a_dvp;
1370 struct vnode *vp = ap->a_vp;
1374 if (fuse_isdeadfs(vp)) {
1377 if (VTOFUD(vp) == VTOFUD(dvp)) {
1380 err = fuse_internal_remove(dvp, vp, ap->a_cnp, FUSE_RMDIR);
1383 fuse_internal_vnode_disappear(vp);
1388 struct vnop_setattr_args {
1390 struct vattr *a_vap;
1391 struct ucred *a_cred;
1392 struct thread *a_td;
1396 fuse_vnop_setattr(struct vop_setattr_args *ap)
1398 struct vnode *vp = ap->a_vp;
1399 struct vattr *vap = ap->a_vap;
1400 struct ucred *cred = ap->a_cred;
1401 struct thread *td = curthread;
1402 struct fuse_dispatcher fdi;
1403 struct fuse_setattr_in *fsai;
1404 pid_t pid = td->td_proc->p_pid;
1408 int sizechanged = 0;
1409 uint64_t newsize = 0;
1410 accmode_t accmode = 0;
1412 if (fuse_isdeadfs(vp)) {
1415 fdisp_init(&fdi, sizeof(*fsai));
1416 fdisp_make_vp(&fdi, FUSE_SETATTR, vp, td, cred);
1420 if (vap->va_uid != (uid_t)VNOVAL) {
1421 fsai->uid = vap->va_uid;
1422 fsai->valid |= FATTR_UID;
1425 if (vap->va_gid != (gid_t)VNOVAL) {
1426 fsai->gid = vap->va_gid;
1427 fsai->valid |= FATTR_GID;
1430 if (vap->va_size != VNOVAL) {
1432 struct fuse_filehandle *fufh = NULL;
1434 /*Truncate to a new value. */
1435 fsai->size = vap->va_size;
1437 newsize = vap->va_size;
1438 fsai->valid |= FATTR_SIZE;
1441 fuse_filehandle_getrw(vp, FWRITE, &fufh, cred, pid);
1443 fsai->fh = fufh->fh_id;
1444 fsai->valid |= FATTR_FH;
1447 if (vap->va_atime.tv_sec != VNOVAL) {
1448 fsai->atime = vap->va_atime.tv_sec;
1449 fsai->atimensec = vap->va_atime.tv_nsec;
1450 fsai->valid |= FATTR_ATIME;
1453 * TODO: only require VWRITE if UTIMENS_NULL is set. PR 237181
1456 if (vap->va_mtime.tv_sec != VNOVAL) {
1457 fsai->mtime = vap->va_mtime.tv_sec;
1458 fsai->mtimensec = vap->va_mtime.tv_nsec;
1459 fsai->valid |= FATTR_MTIME;
1462 * TODO: only require VWRITE if UTIMENS_NULL is set. PR 237181
1465 if (vap->va_mode != (mode_t)VNOVAL) {
1466 fsai->mode = vap->va_mode & ALLPERMS;
1467 fsai->valid |= FATTR_MODE;
1473 vtyp = vnode_vtype(vp);
1475 if (fsai->valid & FATTR_SIZE && vtyp == VDIR) {
1479 if (vfs_isrdonly(vnode_mount(vp)) && (fsai->valid & ~FATTR_SIZE || vtyp == VREG)) {
1483 err = fuse_internal_access(vp, accmode, td, cred);
1487 if ((err = fdisp_wait_answ(&fdi)))
1489 vtyp = IFTOVT(((struct fuse_attr_out *)fdi.answ)->attr.mode);
1491 if (vnode_vtype(vp) != vtyp) {
1492 if (vnode_vtype(vp) == VNON && vtyp != VNON) {
1493 SDT_PROBE2(fuse, , vnops, trace, 1, "FUSE: Dang! "
1494 "vnode_vtype is VNON and vtype isn't.");
1497 * STALE vnode, ditch
1499 * The vnode has changed its type "behind our back".
1500 * There's nothing really we can do, so let us just
1501 * force an internal revocation and tell the caller to
1502 * try again, if interested.
1504 fuse_internal_vnode_disappear(vp);
1509 struct fuse_attr_out *fao = (struct fuse_attr_out*)fdi.answ;
1510 fuse_internal_cache_attrs(vp, &fao->attr, fao->attr_valid,
1511 fao->attr_valid_nsec, NULL);
1515 fdisp_destroy(&fdi);
1516 if (!err && sizechanged) {
1517 fuse_vnode_setsize(vp, cred, newsize);
1518 VTOFUD(vp)->flag &= ~FN_SIZECHANGE;
1524 struct vnop_strategy_args {
1530 fuse_vnop_strategy(struct vop_strategy_args *ap)
1532 struct vnode *vp = ap->a_vp;
1533 struct buf *bp = ap->a_bp;
1535 if (!vp || fuse_isdeadfs(vp)) {
1536 bp->b_ioflags |= BIO_ERROR;
1537 bp->b_error = ENXIO;
1541 if (bp->b_iocmd == BIO_WRITE) {
1544 err = fuse_vnode_refreshsize(vp, NOCRED);
1546 bp->b_ioflags |= BIO_ERROR;
1554 * VOP_STRATEGY always returns zero and signals error via bp->b_ioflags.
1555 * fuse_io_strategy sets bp's error fields
1557 (void)fuse_io_strategy(vp, bp);
1564 struct vnop_symlink_args {
1565 struct vnode *a_dvp;
1566 struct vnode **a_vpp;
1567 struct componentname *a_cnp;
1568 struct vattr *a_vap;
1573 fuse_vnop_symlink(struct vop_symlink_args *ap)
1575 struct vnode *dvp = ap->a_dvp;
1576 struct vnode **vpp = ap->a_vpp;
1577 struct componentname *cnp = ap->a_cnp;
1578 const char *target = ap->a_target;
1580 struct fuse_dispatcher fdi;
1585 if (fuse_isdeadfs(dvp)) {
1589 * Unlike the other creator type calls, here we have to create a message
1590 * where the name of the new entry comes first, and the data describing
1591 * the entry comes second.
1592 * Hence we can't rely on our handy fuse_internal_newentry() routine,
1593 * but put together the message manually and just call the core part.
1596 len = strlen(target) + 1;
1597 fdisp_init(&fdi, len + cnp->cn_namelen + 1);
1598 fdisp_make_vp(&fdi, FUSE_SYMLINK, dvp, curthread, NULL);
1600 memcpy(fdi.indata, cnp->cn_nameptr, cnp->cn_namelen);
1601 ((char *)fdi.indata)[cnp->cn_namelen] = '\0';
1602 memcpy((char *)fdi.indata + cnp->cn_namelen + 1, target, len);
1604 err = fuse_internal_newentry_core(dvp, vpp, cnp, VLNK, &fdi);
1605 fdisp_destroy(&fdi);
1610 struct vnop_write_args {
1614 struct ucred *a_cred;
1618 fuse_vnop_write(struct vop_write_args *ap)
1620 struct vnode *vp = ap->a_vp;
1621 struct uio *uio = ap->a_uio;
1622 int ioflag = ap->a_ioflag;
1623 struct ucred *cred = ap->a_cred;
1624 pid_t pid = curthread->td_proc->p_pid;
1627 if (fuse_isdeadfs(vp)) {
1630 err = fuse_vnode_refreshsize(vp, cred);
1634 if (VTOFUD(vp)->flag & FN_DIRECTIO) {
1635 ioflag |= IO_DIRECT;
1638 return fuse_io_dispatch(vp, uio, ioflag, cred, pid);
1641 SDT_PROBE_DEFINE1(fuse, , vnops, vnop_getpages_error, "int");
1643 struct vnop_getpages_args {
1651 fuse_vnop_getpages(struct vop_getpages_args *ap)
1653 int i, error, nextoff, size, toff, count, npages;
1662 pid_t pid = curthread->td_proc->p_pid;
1665 KASSERT(vp->v_object, ("objectless vp passed to getpages"));
1666 td = curthread; /* XXX */
1667 cred = curthread->td_ucred; /* XXX */
1669 npages = ap->a_count;
1671 if (!fsess_opt_mmap(vnode_mount(vp))) {
1672 SDT_PROBE2(fuse, , vnops, trace, 1,
1673 "called on non-cacheable vnode??\n");
1674 return (VM_PAGER_ERROR);
1678 * If the last page is partially valid, just return it and allow
1679 * the pager to zero-out the blanks. Partially valid pages can
1680 * only occur at the file EOF.
1682 * XXXGL: is that true for FUSE, which is a local filesystem,
1683 * but still somewhat disconnected from the kernel?
1685 VM_OBJECT_WLOCK(vp->v_object);
1686 if (pages[npages - 1]->valid != 0 && --npages == 0)
1688 VM_OBJECT_WUNLOCK(vp->v_object);
1691 * We use only the kva address for the buffer, but this is extremely
1692 * convenient and fast.
1694 bp = uma_zalloc(fuse_pbuf_zone, M_WAITOK);
1696 kva = (vm_offset_t)bp->b_data;
1697 pmap_qenter(kva, pages, npages);
1698 VM_CNT_INC(v_vnodein);
1699 VM_CNT_ADD(v_vnodepgsin, npages);
1701 count = npages << PAGE_SHIFT;
1702 iov.iov_base = (caddr_t)kva;
1703 iov.iov_len = count;
1706 uio.uio_offset = IDX_TO_OFF(pages[0]->pindex);
1707 uio.uio_resid = count;
1708 uio.uio_segflg = UIO_SYSSPACE;
1709 uio.uio_rw = UIO_READ;
1712 error = fuse_io_dispatch(vp, &uio, IO_DIRECT, cred, pid);
1713 pmap_qremove(kva, npages);
1715 uma_zfree(fuse_pbuf_zone, bp);
1717 if (error && (uio.uio_resid == count)) {
1718 SDT_PROBE1(fuse, , vnops, vnop_getpages_error, error);
1719 return VM_PAGER_ERROR;
1722 * Calculate the number of bytes read and validate only that number
1723 * of bytes. Note that due to pending writes, size may be 0. This
1724 * does not mean that the remaining data is invalid!
1727 size = count - uio.uio_resid;
1728 VM_OBJECT_WLOCK(vp->v_object);
1729 fuse_vm_page_lock_queues();
1730 for (i = 0, toff = 0; i < npages; i++, toff = nextoff) {
1733 nextoff = toff + PAGE_SIZE;
1736 if (nextoff <= size) {
1738 * Read operation filled an entire page
1740 m->valid = VM_PAGE_BITS_ALL;
1741 KASSERT(m->dirty == 0,
1742 ("fuse_getpages: page %p is dirty", m));
1743 } else if (size > toff) {
1745 * Read operation filled a partial page.
1748 vm_page_set_valid_range(m, 0, size - toff);
1749 KASSERT(m->dirty == 0,
1750 ("fuse_getpages: page %p is dirty", m));
1753 * Read operation was short. If no error occurred
1754 * we may have hit a zero-fill section. We simply
1755 * leave valid set to 0.
1760 fuse_vm_page_unlock_queues();
1762 VM_OBJECT_WUNLOCK(vp->v_object);
1767 return (VM_PAGER_OK);
1771 struct vnop_putpages_args {
1777 vm_ooffset_t a_offset;
1781 fuse_vnop_putpages(struct vop_putpages_args *ap)
1787 int i, error, npages, count;
1795 pid_t pid = curthread->td_proc->p_pid;
1798 KASSERT(vp->v_object, ("objectless vp passed to putpages"));
1799 fsize = vp->v_object->un_pager.vnp.vnp_size;
1800 td = curthread; /* XXX */
1801 cred = curthread->td_ucred; /* XXX */
1803 count = ap->a_count;
1804 rtvals = ap->a_rtvals;
1805 npages = btoc(count);
1806 offset = IDX_TO_OFF(pages[0]->pindex);
1808 if (!fsess_opt_mmap(vnode_mount(vp))) {
1809 SDT_PROBE2(fuse, , vnops, trace, 1,
1810 "called on non-cacheable vnode??\n");
1812 for (i = 0; i < npages; i++)
1813 rtvals[i] = VM_PAGER_AGAIN;
1816 * When putting pages, do not extend file past EOF.
1819 if (offset + count > fsize) {
1820 count = fsize - offset;
1825 * We use only the kva address for the buffer, but this is extremely
1826 * convenient and fast.
1828 bp = uma_zalloc(fuse_pbuf_zone, M_WAITOK);
1830 kva = (vm_offset_t)bp->b_data;
1831 pmap_qenter(kva, pages, npages);
1832 VM_CNT_INC(v_vnodeout);
1833 VM_CNT_ADD(v_vnodepgsout, count);
1835 iov.iov_base = (caddr_t)kva;
1836 iov.iov_len = count;
1839 uio.uio_offset = offset;
1840 uio.uio_resid = count;
1841 uio.uio_segflg = UIO_SYSSPACE;
1842 uio.uio_rw = UIO_WRITE;
1845 error = fuse_io_dispatch(vp, &uio, IO_DIRECT, cred, pid);
1847 pmap_qremove(kva, npages);
1848 uma_zfree(fuse_pbuf_zone, bp);
1851 int nwritten = round_page(count - uio.uio_resid) / PAGE_SIZE;
1853 for (i = 0; i < nwritten; i++) {
1854 rtvals[i] = VM_PAGER_OK;
1855 VM_OBJECT_WLOCK(pages[i]->object);
1856 vm_page_undirty(pages[i]);
1857 VM_OBJECT_WUNLOCK(pages[i]->object);
1863 static const char extattr_namespace_separator = '.';
1866 struct vop_getextattr_args {
1867 struct vop_generic_args a_gen;
1869 int a_attrnamespace;
1873 struct ucred *a_cred;
1874 struct thread *a_td;
1878 fuse_vnop_getextattr(struct vop_getextattr_args *ap)
1880 struct vnode *vp = ap->a_vp;
1881 struct uio *uio = ap->a_uio;
1882 struct fuse_dispatcher fdi;
1883 struct fuse_getxattr_in *get_xattr_in;
1884 struct fuse_getxattr_out *get_xattr_out;
1885 struct mount *mp = vnode_mount(vp);
1886 struct thread *td = ap->a_td;
1887 struct ucred *cred = ap->a_cred;
1893 if (fuse_isdeadfs(vp))
1896 if (!fsess_isimpl(mp, FUSE_GETXATTR))
1899 err = fuse_extattr_check_cred(vp, ap->a_attrnamespace, cred, td, VREAD);
1903 /* Default to looking for user attributes. */
1904 if (ap->a_attrnamespace == EXTATTR_NAMESPACE_SYSTEM)
1905 prefix = EXTATTR_NAMESPACE_SYSTEM_STRING;
1907 prefix = EXTATTR_NAMESPACE_USER_STRING;
1909 len = strlen(prefix) + sizeof(extattr_namespace_separator) +
1910 strlen(ap->a_name) + 1;
1912 fdisp_init(&fdi, len + sizeof(*get_xattr_in));
1913 fdisp_make_vp(&fdi, FUSE_GETXATTR, vp, td, cred);
1915 get_xattr_in = fdi.indata;
1917 * Check to see whether we're querying the available size or
1918 * issuing the actual request. If we pass in 0, we get back struct
1919 * fuse_getxattr_out. If we pass in a non-zero size, we get back
1920 * that much data, without the struct fuse_getxattr_out header.
1923 get_xattr_in->size = 0;
1925 get_xattr_in->size = uio->uio_resid;
1927 attr_str = (char *)fdi.indata + sizeof(*get_xattr_in);
1928 snprintf(attr_str, len, "%s%c%s", prefix, extattr_namespace_separator,
1931 err = fdisp_wait_answ(&fdi);
1933 if (err == ENOSYS) {
1934 fsess_set_notimpl(mp, FUSE_GETXATTR);
1940 get_xattr_out = fdi.answ;
1942 if (ap->a_size != NULL)
1943 *ap->a_size = get_xattr_out->size;
1946 err = uiomove(fdi.answ, fdi.iosize, uio);
1949 fdisp_destroy(&fdi);
1954 struct vop_setextattr_args {
1955 struct vop_generic_args a_gen;
1957 int a_attrnamespace;
1960 struct ucred *a_cred;
1961 struct thread *a_td;
1965 fuse_vnop_setextattr(struct vop_setextattr_args *ap)
1967 struct vnode *vp = ap->a_vp;
1968 struct uio *uio = ap->a_uio;
1969 struct fuse_dispatcher fdi;
1970 struct fuse_setxattr_in *set_xattr_in;
1971 struct mount *mp = vnode_mount(vp);
1972 struct thread *td = ap->a_td;
1973 struct ucred *cred = ap->a_cred;
1979 if (fuse_isdeadfs(vp))
1982 if (!fsess_isimpl(mp, FUSE_SETXATTR))
1985 if (vfs_isrdonly(mp))
1988 /* Deleting xattrs must use VOP_DELETEEXTATTR instead */
1989 if (ap->a_uio == NULL) {
1991 * If we got here as fallback from VOP_DELETEEXTATTR, then
1992 * return EOPNOTSUPP.
1994 if (!fsess_isimpl(mp, FUSE_REMOVEXATTR))
1995 return (EOPNOTSUPP);
2000 err = fuse_extattr_check_cred(vp, ap->a_attrnamespace, cred, td,
2005 /* Default to looking for user attributes. */
2006 if (ap->a_attrnamespace == EXTATTR_NAMESPACE_SYSTEM)
2007 prefix = EXTATTR_NAMESPACE_SYSTEM_STRING;
2009 prefix = EXTATTR_NAMESPACE_USER_STRING;
2011 len = strlen(prefix) + sizeof(extattr_namespace_separator) +
2012 strlen(ap->a_name) + 1;
2014 fdisp_init(&fdi, len + sizeof(*set_xattr_in) + uio->uio_resid);
2015 fdisp_make_vp(&fdi, FUSE_SETXATTR, vp, td, cred);
2017 set_xattr_in = fdi.indata;
2018 set_xattr_in->size = uio->uio_resid;
2020 attr_str = (char *)fdi.indata + sizeof(*set_xattr_in);
2021 snprintf(attr_str, len, "%s%c%s", prefix, extattr_namespace_separator,
2024 err = uiomove((char *)fdi.indata + sizeof(*set_xattr_in) + len,
2025 uio->uio_resid, uio);
2030 err = fdisp_wait_answ(&fdi);
2032 if (err == ENOSYS) {
2033 fsess_set_notimpl(mp, FUSE_SETXATTR);
2038 fdisp_destroy(&fdi);
2043 * The Linux / FUSE extended attribute list is simply a collection of
2044 * NUL-terminated strings. The FreeBSD extended attribute list is a single
2045 * byte length followed by a non-NUL terminated string. So, this allows
2046 * conversion of the Linux / FUSE format to the FreeBSD format in place.
2047 * Linux attribute names are reported with the namespace as a prefix (e.g.
2048 * "user.attribute_name"), but in FreeBSD they are reported without the
2049 * namespace prefix (e.g. "attribute_name"). So, we're going from:
2051 * user.attr_name1\0user.attr_name2\0
2055 * <num>attr_name1<num>attr_name2
2057 * Where "<num>" is a single byte number of characters in the attribute name.
2060 * prefix - exattr namespace prefix string
2061 * list, list_len - input list with namespace prefixes
2062 * bsd_list, bsd_list_len - output list compatible with bsd vfs
2065 fuse_xattrlist_convert(char *prefix, const char *list, int list_len,
2066 char *bsd_list, int *bsd_list_len)
2068 int len, pos, dist_to_next, prefix_len;
2072 prefix_len = strlen(prefix);
2074 while (pos < list_len && list[pos] != '\0') {
2075 dist_to_next = strlen(&list[pos]) + 1;
2076 if (bcmp(&list[pos], prefix, prefix_len) == 0 &&
2077 list[pos + prefix_len] == extattr_namespace_separator) {
2078 len = dist_to_next -
2079 (prefix_len + sizeof(extattr_namespace_separator)) - 1;
2080 if (len >= EXTATTR_MAXNAMELEN)
2081 return (ENAMETOOLONG);
2083 bsd_list[*bsd_list_len] = len;
2084 memcpy(&bsd_list[*bsd_list_len + 1],
2085 &list[pos + prefix_len +
2086 sizeof(extattr_namespace_separator)], len);
2088 *bsd_list_len += len + 1;
2091 pos += dist_to_next;
2098 struct vop_listextattr_args {
2099 struct vop_generic_args a_gen;
2101 int a_attrnamespace;
2104 struct ucred *a_cred;
2105 struct thread *a_td;
2109 fuse_vnop_listextattr(struct vop_listextattr_args *ap)
2111 struct vnode *vp = ap->a_vp;
2112 struct uio *uio = ap->a_uio;
2113 struct fuse_dispatcher fdi;
2114 struct fuse_listxattr_in *list_xattr_in;
2115 struct fuse_listxattr_out *list_xattr_out;
2116 struct mount *mp = vnode_mount(vp);
2117 struct thread *td = ap->a_td;
2118 struct ucred *cred = ap->a_cred;
2122 char *bsd_list = NULL;
2128 if (fuse_isdeadfs(vp))
2131 if (!fsess_isimpl(mp, FUSE_LISTXATTR))
2134 err = fuse_extattr_check_cred(vp, ap->a_attrnamespace, cred, td, VREAD);
2139 * Add space for a NUL and the period separator if enabled.
2140 * Default to looking for user attributes.
2142 if (ap->a_attrnamespace == EXTATTR_NAMESPACE_SYSTEM)
2143 prefix = EXTATTR_NAMESPACE_SYSTEM_STRING;
2145 prefix = EXTATTR_NAMESPACE_USER_STRING;
2147 len = strlen(prefix) + sizeof(extattr_namespace_separator) + 1;
2149 fdisp_init(&fdi, sizeof(*list_xattr_in) + len);
2150 fdisp_make_vp(&fdi, FUSE_LISTXATTR, vp, td, cred);
2153 * Retrieve Linux / FUSE compatible list size.
2155 list_xattr_in = fdi.indata;
2156 list_xattr_in->size = 0;
2157 attr_str = (char *)fdi.indata + sizeof(*list_xattr_in);
2158 snprintf(attr_str, len, "%s%c", prefix, extattr_namespace_separator);
2160 err = fdisp_wait_answ(&fdi);
2162 if (err == ENOSYS) {
2163 fsess_set_notimpl(mp, FUSE_LISTXATTR);
2169 list_xattr_out = fdi.answ;
2170 linux_list_len = list_xattr_out->size;
2171 if (linux_list_len == 0) {
2172 if (ap->a_size != NULL)
2173 *ap->a_size = linux_list_len;
2178 * Retrieve Linux / FUSE compatible list values.
2180 fdisp_refresh_vp(&fdi, FUSE_LISTXATTR, vp, td, cred);
2181 list_xattr_in = fdi.indata;
2182 list_xattr_in->size = linux_list_len + sizeof(*list_xattr_out);
2183 list_xattr_in->flags = 0;
2184 attr_str = (char *)fdi.indata + sizeof(*list_xattr_in);
2185 snprintf(attr_str, len, "%s%c", prefix, extattr_namespace_separator);
2187 err = fdisp_wait_answ(&fdi);
2191 linux_list = fdi.answ;
2192 linux_list_len = fdi.iosize;
2195 * Retrieve the BSD compatible list values.
2196 * The Linux / FUSE attribute list format isn't the same
2197 * as FreeBSD's format. So we need to transform it into
2198 * FreeBSD's format before giving it to the user.
2200 bsd_list = malloc(linux_list_len, M_TEMP, M_WAITOK);
2201 err = fuse_xattrlist_convert(prefix, linux_list, linux_list_len,
2202 bsd_list, &bsd_list_len);
2206 if (ap->a_size != NULL)
2207 *ap->a_size = bsd_list_len;
2210 err = uiomove(bsd_list, bsd_list_len, uio);
2213 free(bsd_list, M_TEMP);
2214 fdisp_destroy(&fdi);
2219 struct vop_deleteextattr_args {
2220 struct vop_generic_args a_gen;
2222 int a_attrnamespace;
2224 struct ucred *a_cred;
2225 struct thread *a_td;
2229 fuse_vnop_deleteextattr(struct vop_deleteextattr_args *ap)
2231 struct vnode *vp = ap->a_vp;
2232 struct fuse_dispatcher fdi;
2233 struct mount *mp = vnode_mount(vp);
2234 struct thread *td = ap->a_td;
2235 struct ucred *cred = ap->a_cred;
2241 if (fuse_isdeadfs(vp))
2244 if (!fsess_isimpl(mp, FUSE_REMOVEXATTR))
2247 if (vfs_isrdonly(mp))
2250 err = fuse_extattr_check_cred(vp, ap->a_attrnamespace, cred, td,
2255 /* Default to looking for user attributes. */
2256 if (ap->a_attrnamespace == EXTATTR_NAMESPACE_SYSTEM)
2257 prefix = EXTATTR_NAMESPACE_SYSTEM_STRING;
2259 prefix = EXTATTR_NAMESPACE_USER_STRING;
2261 len = strlen(prefix) + sizeof(extattr_namespace_separator) +
2262 strlen(ap->a_name) + 1;
2264 fdisp_init(&fdi, len);
2265 fdisp_make_vp(&fdi, FUSE_REMOVEXATTR, vp, td, cred);
2267 attr_str = fdi.indata;
2268 snprintf(attr_str, len, "%s%c%s", prefix, extattr_namespace_separator,
2271 err = fdisp_wait_answ(&fdi);
2272 if (err == ENOSYS) {
2273 fsess_set_notimpl(mp, FUSE_REMOVEXATTR);
2277 fdisp_destroy(&fdi);
2282 struct vnop_print_args {
2287 fuse_vnop_print(struct vop_print_args *ap)
2289 struct fuse_vnode_data *fvdat = VTOFUD(ap->a_vp);
2291 printf("nodeid: %ju, parent nodeid: %ju, nlookup: %ju, flag: %#x\n",
2292 (uintmax_t)VTOILLU(ap->a_vp), (uintmax_t)fvdat->parent_nid,
2293 (uintmax_t)fvdat->nlookup,