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_advlock_t fuse_vnop_advlock;
123 static vop_close_t fuse_vnop_close;
124 static vop_create_t fuse_vnop_create;
125 static vop_deleteextattr_t fuse_vnop_deleteextattr;
126 static vop_fdatasync_t fuse_vnop_fdatasync;
127 static vop_fsync_t fuse_vnop_fsync;
128 static vop_getattr_t fuse_vnop_getattr;
129 static vop_getextattr_t fuse_vnop_getextattr;
130 static vop_inactive_t fuse_vnop_inactive;
131 static vop_link_t fuse_vnop_link;
132 static vop_listextattr_t fuse_vnop_listextattr;
133 static vop_lookup_t fuse_vnop_lookup;
134 static vop_mkdir_t fuse_vnop_mkdir;
135 static vop_mknod_t fuse_vnop_mknod;
136 static vop_open_t fuse_vnop_open;
137 static vop_pathconf_t fuse_vnop_pathconf;
138 static vop_read_t fuse_vnop_read;
139 static vop_readdir_t fuse_vnop_readdir;
140 static vop_readlink_t fuse_vnop_readlink;
141 static vop_reclaim_t fuse_vnop_reclaim;
142 static vop_remove_t fuse_vnop_remove;
143 static vop_rename_t fuse_vnop_rename;
144 static vop_rmdir_t fuse_vnop_rmdir;
145 static vop_setattr_t fuse_vnop_setattr;
146 static vop_setextattr_t fuse_vnop_setextattr;
147 static vop_strategy_t fuse_vnop_strategy;
148 static vop_symlink_t fuse_vnop_symlink;
149 static vop_write_t fuse_vnop_write;
150 static vop_getpages_t fuse_vnop_getpages;
151 static vop_putpages_t fuse_vnop_putpages;
152 static vop_print_t fuse_vnop_print;
154 struct vop_vector fuse_vnops = {
155 .vop_default = &default_vnodeops,
156 .vop_access = fuse_vnop_access,
157 .vop_advlock = fuse_vnop_advlock,
158 .vop_close = fuse_vnop_close,
159 .vop_create = fuse_vnop_create,
160 .vop_deleteextattr = fuse_vnop_deleteextattr,
161 .vop_fsync = fuse_vnop_fsync,
162 .vop_fdatasync = fuse_vnop_fdatasync,
163 .vop_getattr = fuse_vnop_getattr,
164 .vop_getextattr = fuse_vnop_getextattr,
165 .vop_inactive = fuse_vnop_inactive,
166 .vop_link = fuse_vnop_link,
167 .vop_listextattr = fuse_vnop_listextattr,
168 .vop_lookup = fuse_vnop_lookup,
169 .vop_mkdir = fuse_vnop_mkdir,
170 .vop_mknod = fuse_vnop_mknod,
171 .vop_open = fuse_vnop_open,
172 .vop_pathconf = fuse_vnop_pathconf,
173 .vop_read = fuse_vnop_read,
174 .vop_readdir = fuse_vnop_readdir,
175 .vop_readlink = fuse_vnop_readlink,
176 .vop_reclaim = fuse_vnop_reclaim,
177 .vop_remove = fuse_vnop_remove,
178 .vop_rename = fuse_vnop_rename,
179 .vop_rmdir = fuse_vnop_rmdir,
180 .vop_setattr = fuse_vnop_setattr,
181 .vop_setextattr = fuse_vnop_setextattr,
182 .vop_strategy = fuse_vnop_strategy,
183 .vop_symlink = fuse_vnop_symlink,
184 .vop_write = fuse_vnop_write,
185 .vop_getpages = fuse_vnop_getpages,
186 .vop_putpages = fuse_vnop_putpages,
187 .vop_print = fuse_vnop_print,
190 static u_long fuse_lookup_cache_hits = 0;
192 SYSCTL_ULONG(_vfs_fusefs, OID_AUTO, lookup_cache_hits, CTLFLAG_RD,
193 &fuse_lookup_cache_hits, 0, "number of positive cache hits in lookup");
195 static u_long fuse_lookup_cache_misses = 0;
197 SYSCTL_ULONG(_vfs_fusefs, OID_AUTO, lookup_cache_misses, CTLFLAG_RD,
198 &fuse_lookup_cache_misses, 0, "number of cache misses in lookup");
201 * XXX: This feature is highly experimental and can bring to instabilities,
202 * needs revisiting before to be enabled by default.
204 static int fuse_reclaim_revoked = 0;
206 SYSCTL_INT(_vfs_fusefs, OID_AUTO, reclaim_revoked, CTLFLAG_RW,
207 &fuse_reclaim_revoked, 0, "");
209 uma_zone_t fuse_pbuf_zone;
211 #define fuse_vm_page_lock(m) vm_page_lock((m));
212 #define fuse_vm_page_unlock(m) vm_page_unlock((m));
213 #define fuse_vm_page_lock_queues() ((void)0)
214 #define fuse_vm_page_unlock_queues() ((void)0)
216 /* Check permission for extattr operations, much like extattr_check_cred */
218 fuse_extattr_check_cred(struct vnode *vp, int ns, struct ucred *cred,
219 struct thread *td, accmode_t accmode)
221 struct mount *mp = vnode_mount(vp);
222 struct fuse_data *data = fuse_get_mpdata(mp);
225 * Kernel-invoked always succeeds.
231 * Do not allow privileged processes in jail to directly manipulate
235 case EXTATTR_NAMESPACE_SYSTEM:
236 if (data->dataflags & FSESS_DEFAULT_PERMISSIONS) {
237 return (priv_check_cred(cred, PRIV_VFS_EXTATTR_SYSTEM));
240 case EXTATTR_NAMESPACE_USER:
241 return (fuse_internal_access(vp, accmode, td, cred));
247 /* Get a filehandle for a directory */
249 fuse_filehandle_get_dir(struct vnode *vp, struct fuse_filehandle **fufhp,
250 struct ucred *cred, pid_t pid)
252 if (fuse_filehandle_get(vp, FREAD, fufhp, cred, pid) == 0)
254 return fuse_filehandle_get(vp, FEXEC, fufhp, cred, pid);
257 /* Send FUSE_FLUSH for this vnode */
259 fuse_flush(struct vnode *vp, struct ucred *cred, pid_t pid, int fflag)
261 struct fuse_flush_in *ffi;
262 struct fuse_filehandle *fufh;
263 struct fuse_dispatcher fdi;
264 struct thread *td = curthread;
265 struct mount *mp = vnode_mount(vp);
268 if (!fsess_isimpl(vnode_mount(vp), FUSE_FLUSH))
271 err = fuse_filehandle_getrw(vp, fflag, &fufh, cred, pid);
275 fdisp_init(&fdi, sizeof(*ffi));
276 fdisp_make_vp(&fdi, FUSE_FLUSH, vp, td, cred);
278 ffi->fh = fufh->fh_id;
280 * If the file has a POSIX lock then we're supposed to set lock_owner.
281 * If not, then lock_owner is undefined. So we may as well always set
284 ffi->lock_owner = td->td_proc->p_pid;
286 err = fdisp_wait_answ(&fdi);
288 fsess_set_notimpl(mp, FUSE_FLUSH);
296 struct vnop_access_args {
298 #if VOP_ACCESS_TAKES_ACCMODE_T
303 struct ucred *a_cred;
308 fuse_vnop_access(struct vop_access_args *ap)
310 struct vnode *vp = ap->a_vp;
311 int accmode = ap->a_accmode;
312 struct ucred *cred = ap->a_cred;
314 struct fuse_data *data = fuse_get_mpdata(vnode_mount(vp));
318 if (fuse_isdeadfs(vp)) {
319 if (vnode_isvroot(vp)) {
324 if (!(data->dataflags & FSESS_INITED)) {
325 if (vnode_isvroot(vp)) {
326 if (priv_check_cred(cred, PRIV_VFS_ADMIN) ||
327 (fuse_match_cred(data->daemoncred, cred) == 0)) {
333 if (vnode_islnk(vp)) {
337 err = fuse_internal_access(vp, accmode, ap->a_td, ap->a_cred);
342 * struct vop_advlock_args {
343 * struct vop_generic_args a_gen;
344 * struct vnode *a_vp;
347 * struct flock *a_fl;
352 fuse_vnop_advlock(struct vop_advlock_args *ap)
354 struct vnode *vp = ap->a_vp;
355 struct flock *fl = ap->a_fl;
356 struct thread *td = curthread;
357 struct ucred *cred = td->td_ucred;
358 pid_t pid = td->td_proc->p_pid;
359 struct fuse_filehandle *fufh;
360 struct fuse_dispatcher fdi;
361 struct fuse_lk_in *fli;
362 struct fuse_lk_out *flo;
366 dataflags = fuse_get_mpdata(vnode_mount(vp))->dataflags;
368 if (fuse_isdeadfs(vp)) {
372 if (!(dataflags & FSESS_POSIX_LOCKS))
373 return vop_stdadvlock(ap);
375 err = fuse_filehandle_get_anyflags(vp, &fufh, cred, pid);
379 fdisp_init(&fdi, sizeof(*fli));
395 fdisp_make_vp(&fdi, op, vp, td, cred);
397 fli->fh = fufh->fh_id;
398 fli->owner = fl->l_pid;
399 fli->lk.start = fl->l_start;
401 fli->lk.end = fl->l_start + fl->l_len - 1;
403 fli->lk.end = INT64_MAX;
404 fli->lk.type = fl->l_type;
405 fli->lk.pid = fl->l_pid;
407 err = fdisp_wait_answ(&fdi);
410 if (err == 0 && op == FUSE_GETLK) {
412 fl->l_type = flo->lk.type;
413 fl->l_pid = flo->lk.pid;
414 if (flo->lk.type != F_UNLCK) {
415 fl->l_start = flo->lk.start;
416 if (flo->lk.end == INT64_MAX)
419 fl->l_len = flo->lk.end - flo->lk.start + 1;
420 fl->l_start = flo->lk.start;
428 struct vop_close_args {
431 struct ucred *a_cred;
436 fuse_vnop_close(struct vop_close_args *ap)
438 struct vnode *vp = ap->a_vp;
439 struct ucred *cred = ap->a_cred;
440 int fflag = ap->a_fflag;
441 struct thread *td = ap->a_td;
442 pid_t pid = td->td_proc->p_pid;
445 if (fuse_isdeadfs(vp))
449 if (fflag & IO_NDELAY)
452 err = fuse_flush(vp, cred, pid, fflag);
453 /* TODO: close the file handle, if we're sure it's no longer used */
454 if ((VTOFUD(vp)->flag & FN_SIZECHANGE) != 0) {
455 fuse_vnode_savesize(vp, cred, td->td_proc->p_pid);
461 fdisp_make_mknod_for_fallback(
462 struct fuse_dispatcher *fdip,
463 struct componentname *cnp,
469 enum fuse_opcode *op)
471 struct fuse_mknod_in *fmni;
473 fdisp_init(fdip, sizeof(*fmni) + cnp->cn_namelen + 1);
475 fdisp_make(fdip, *op, vnode_mount(dvp), parentnid, td, cred);
479 memcpy((char *)fdip->indata + sizeof(*fmni), cnp->cn_nameptr,
481 ((char *)fdip->indata)[sizeof(*fmni) + cnp->cn_namelen] = '\0';
484 struct vnop_create_args {
486 struct vnode **a_vpp;
487 struct componentname *a_cnp;
492 fuse_vnop_create(struct vop_create_args *ap)
494 struct vnode *dvp = ap->a_dvp;
495 struct vnode **vpp = ap->a_vpp;
496 struct componentname *cnp = ap->a_cnp;
497 struct vattr *vap = ap->a_vap;
498 struct thread *td = cnp->cn_thread;
499 struct ucred *cred = cnp->cn_cred;
501 struct fuse_open_in *foi;
502 struct fuse_entry_out *feo;
503 struct fuse_open_out *foo;
504 struct fuse_dispatcher fdi, fdi2;
505 struct fuse_dispatcher *fdip = &fdi;
506 struct fuse_dispatcher *fdip2 = NULL;
510 struct mount *mp = vnode_mount(dvp);
511 uint64_t parentnid = VTOFUD(dvp)->nid;
512 mode_t mode = MAKEIMODE(vap->va_type, vap->va_mode);
517 * VOP_CREATE doesn't tell us the open(2) flags, so we guess. Only a
518 * writable mode makes sense, and we might as well include readability
523 if (fuse_isdeadfs(dvp)) {
526 bzero(&fdi, sizeof(fdi));
528 if ((vap->va_type != VREG))
531 if (!fsess_isimpl(mp, FUSE_CREATE)) {
532 /* Fallback to FUSE_MKNOD/FUSE_OPEN */
533 fdisp_make_mknod_for_fallback(fdip, cnp, dvp, parentnid, td,
536 /* Use FUSE_CREATE */
538 fdisp_init(fdip, sizeof(*foi) + cnp->cn_namelen + 1);
539 fdisp_make(fdip, op, vnode_mount(dvp), parentnid, td, cred);
542 foi->flags = O_CREAT | flags;
543 memcpy((char *)fdip->indata + sizeof(*foi), cnp->cn_nameptr,
545 ((char *)fdip->indata)[sizeof(*foi) + cnp->cn_namelen] = '\0';
548 err = fdisp_wait_answ(fdip);
551 if (err == ENOSYS && op == FUSE_CREATE) {
552 fsess_set_notimpl(mp, FUSE_CREATE);
553 fdisp_make_mknod_for_fallback(fdip, cnp, dvp,
554 parentnid, td, cred, mode, &op);
555 err = fdisp_wait_answ(fdip);
563 if ((err = fuse_internal_checkentry(feo, VREG))) {
567 if (op == FUSE_CREATE) {
568 foo = (struct fuse_open_out*)(feo + 1);
570 /* Issue a separate FUSE_OPEN */
572 fdisp_init(fdip2, sizeof(*foi));
573 fdisp_make(fdip2, FUSE_OPEN, vnode_mount(dvp), feo->nodeid, td,
578 err = fdisp_wait_answ(fdip2);
583 err = fuse_vnode_get(mp, feo, feo->nodeid, dvp, vpp, cnp, VREG);
585 struct fuse_release_in *fri;
586 uint64_t nodeid = feo->nodeid;
587 uint64_t fh_id = foo->fh;
589 fdisp_init(fdip, sizeof(*fri));
590 fdisp_make(fdip, FUSE_RELEASE, mp, nodeid, td, cred);
594 fuse_insert_callback(fdip->tick, fuse_internal_forget_callback);
595 fuse_insert_message(fdip->tick);
598 ASSERT_VOP_ELOCKED(*vpp, "fuse_vnop_create");
599 fuse_internal_cache_attrs(*vpp, &feo->attr, feo->attr_valid,
600 feo->attr_valid_nsec, NULL);
602 fuse_filehandle_init(*vpp, FUFH_RDWR, NULL, td, cred, foo);
603 fuse_vnode_open(*vpp, foo->open_flags, td);
604 cache_purge_negative(dvp);
608 fdisp_destroy(fdip2);
614 struct vnop_fdatasync_args {
615 struct vop_generic_args a_gen;
617 struct thread * a_td;
621 fuse_vnop_fdatasync(struct vop_fdatasync_args *ap)
623 struct vnode *vp = ap->a_vp;
624 struct thread *td = ap->a_td;
625 int waitfor = MNT_WAIT;
629 if (fuse_isdeadfs(vp)) {
632 if ((err = vop_stdfdatasync_buf(ap)))
635 return fuse_internal_fsync(vp, td, waitfor, true);
639 struct vnop_fsync_args {
640 struct vop_generic_args a_gen;
643 struct thread * a_td;
647 fuse_vnop_fsync(struct vop_fsync_args *ap)
649 struct vnode *vp = ap->a_vp;
650 struct thread *td = ap->a_td;
651 int waitfor = ap->a_waitfor;
654 if (fuse_isdeadfs(vp)) {
657 if ((err = vop_stdfsync(ap)))
660 return fuse_internal_fsync(vp, td, waitfor, false);
664 struct vnop_getattr_args {
667 struct ucred *a_cred;
672 fuse_vnop_getattr(struct vop_getattr_args *ap)
674 struct vnode *vp = ap->a_vp;
675 struct vattr *vap = ap->a_vap;
676 struct ucred *cred = ap->a_cred;
677 struct thread *td = curthread;
682 dataflags = fuse_get_mpdata(vnode_mount(vp))->dataflags;
684 /* Note that we are not bailing out on a dead file system just yet. */
686 if (!(dataflags & FSESS_INITED)) {
687 if (!vnode_isvroot(vp)) {
688 fdata_set_dead(fuse_get_mpdata(vnode_mount(vp)));
695 err = fuse_internal_getattr(vp, vap, cred, td);
696 if (err == ENOTCONN && vnode_isvroot(vp)) {
697 /* see comment in fuse_vfsop_statfs() */
704 bzero(vap, sizeof(*vap));
705 vap->va_type = vnode_vtype(vp);
711 struct vnop_inactive_args {
717 fuse_vnop_inactive(struct vop_inactive_args *ap)
719 struct vnode *vp = ap->a_vp;
720 struct thread *td = ap->a_td;
722 struct fuse_vnode_data *fvdat = VTOFUD(vp);
723 struct fuse_filehandle *fufh, *fufh_tmp;
727 LIST_FOREACH_SAFE(fufh, &fvdat->handles, next, fufh_tmp) {
728 if (need_flush && vp->v_type == VREG) {
729 if ((VTOFUD(vp)->flag & FN_SIZECHANGE) != 0) {
730 fuse_vnode_savesize(vp, NULL, 0);
732 if (fuse_data_cache_invalidate ||
733 (fvdat->flag & FN_REVOKED) != 0)
734 fuse_io_invalbuf(vp, td);
736 fuse_io_flushbuf(vp, MNT_WAIT, td);
739 fuse_filehandle_close(vp, fufh, td, NULL);
742 if ((fvdat->flag & FN_REVOKED) != 0 && fuse_reclaim_revoked) {
749 struct vnop_link_args {
750 struct vnode *a_tdvp;
752 struct componentname *a_cnp;
756 fuse_vnop_link(struct vop_link_args *ap)
758 struct vnode *vp = ap->a_vp;
759 struct vnode *tdvp = ap->a_tdvp;
760 struct componentname *cnp = ap->a_cnp;
762 struct vattr *vap = VTOVA(vp);
764 struct fuse_dispatcher fdi;
765 struct fuse_entry_out *feo;
766 struct fuse_link_in fli;
770 if (fuse_isdeadfs(vp)) {
773 if (vnode_mount(tdvp) != vnode_mount(vp)) {
778 * This is a seatbelt check to protect naive userspace filesystems from
779 * themselves and the limitations of the FUSE IPC protocol. If a
780 * filesystem does not allow attribute caching, assume it is capable of
781 * validating that nlink does not overflow.
783 if (vap != NULL && vap->va_nlink >= FUSE_LINK_MAX)
785 fli.oldnodeid = VTOI(vp);
788 fuse_internal_newentry_makerequest(vnode_mount(tdvp), VTOI(tdvp), cnp,
789 FUSE_LINK, &fli, sizeof(fli), &fdi);
790 if ((err = fdisp_wait_answ(&fdi))) {
795 err = fuse_internal_checkentry(feo, vnode_vtype(vp));
801 struct fuse_lookup_alloc_arg {
802 struct fuse_entry_out *feo;
803 struct componentname *cnp;
808 /* Callback for vn_get_ino */
810 fuse_lookup_alloc(struct mount *mp, void *arg, int lkflags, struct vnode **vpp)
812 struct fuse_lookup_alloc_arg *flaa = arg;
814 return fuse_vnode_get(mp, flaa->feo, flaa->nid, NULL, vpp, flaa->cnp,
818 SDT_PROBE_DEFINE3(fuse, , vnops, cache_lookup,
819 "int", "struct timespec*", "struct timespec*");
821 struct vnop_lookup_args {
822 struct vnodeop_desc *a_desc;
824 struct vnode **a_vpp;
825 struct componentname *a_cnp;
829 fuse_vnop_lookup(struct vop_lookup_args *ap)
831 struct vnode *dvp = ap->a_dvp;
832 struct vnode **vpp = ap->a_vpp;
833 struct componentname *cnp = ap->a_cnp;
834 struct thread *td = cnp->cn_thread;
835 struct ucred *cred = cnp->cn_cred;
837 int nameiop = cnp->cn_nameiop;
838 int flags = cnp->cn_flags;
839 int wantparent = flags & (LOCKPARENT | WANTPARENT);
840 int islastcn = flags & ISLASTCN;
841 struct mount *mp = vnode_mount(dvp);
845 struct vnode *vp = NULL;
847 struct fuse_dispatcher fdi;
848 bool did_lookup = false;
849 struct fuse_entry_out *feo = NULL;
850 enum vtype vtyp; /* vnode type of target */
851 off_t filesize; /* filesize of target */
855 if (fuse_isdeadfs(dvp)) {
859 if (!vnode_isdir(dvp))
862 if (islastcn && vfs_isrdonly(mp) && (nameiop != LOOKUP))
865 if ((err = fuse_internal_access(dvp, VEXEC, td, cred)))
868 if (flags & ISDOTDOT) {
869 nid = VTOFUD(dvp)->parent_nid;
872 /* .. is obviously a directory */
875 } else if (cnp->cn_namelen == 1 && *(cnp->cn_nameptr) == '.') {
877 /* . is obviously a directory */
881 struct timespec now, timeout;
883 err = cache_lookup(dvp, vpp, cnp, &timeout, NULL);
885 SDT_PROBE3(fuse, , vnops, cache_lookup, err, &timeout, &now);
887 case -1: /* positive match */
888 if (timespeccmp(&timeout, &now, >)) {
889 atomic_add_acq_long(&fuse_lookup_cache_hits, 1);
892 atomic_add_acq_long(&fuse_lookup_cache_misses,
904 case 0: /* no match in cache */
905 atomic_add_acq_long(&fuse_lookup_cache_misses, 1);
908 case ENOENT: /* negative match */
910 if (timespeccmp(&timeout, &now, <=)) {
912 cache_purge_negative(dvp);
921 fdisp_init(&fdi, cnp->cn_namelen + 1);
922 fdisp_make(&fdi, FUSE_LOOKUP, mp, nid, td, cred);
924 memcpy(fdi.indata, cnp->cn_nameptr, cnp->cn_namelen);
925 ((char *)fdi.indata)[cnp->cn_namelen] = '\0';
926 lookup_err = fdisp_wait_answ(&fdi);
930 /* lookup call succeeded */
931 nid = ((struct fuse_entry_out *)fdi.answ)->nodeid;
932 feo = (struct fuse_entry_out *)fdi.answ;
934 /* zero nodeid means ENOENT and cache it */
935 struct timespec timeout;
937 fdi.answ_stat = ENOENT;
939 if (cnp->cn_flags & MAKEENTRY) {
940 fuse_validity_2_timespec(feo, &timeout);
941 cache_enter_time(dvp, *vpp, cnp,
944 } else if (nid == FUSE_ROOT_ID) {
947 vtyp = IFTOVT(feo->attr.mode);
948 filesize = feo->attr.size;
950 if (lookup_err && (!fdi.answ_stat || lookup_err != ENOENT)) {
955 /* lookup_err, if non-zero, must be ENOENT at this point */
958 /* Entry not found */
959 if ((nameiop == CREATE || nameiop == RENAME) && islastcn) {
960 err = fuse_internal_access(dvp, VWRITE, td, cred);
963 * Set the SAVENAME flag to hold onto the
964 * pathname for use later in VOP_CREATE or
967 cnp->cn_flags |= SAVENAME;
975 /* Entry was found */
976 if (flags & ISDOTDOT) {
977 struct fuse_lookup_alloc_arg flaa;
983 err = vn_vget_ino_gen(dvp, fuse_lookup_alloc, &flaa, 0,
986 } else if (nid == VTOI(dvp)) {
990 struct fuse_vnode_data *fvdat;
992 err = fuse_vnode_get(vnode_mount(dvp), feo, nid, dvp,
998 fuse_vnode_setparent(vp, dvp);
1001 * In the case where we are looking up a FUSE node
1002 * represented by an existing cached vnode, and the
1003 * true size reported by FUSE_LOOKUP doesn't match
1004 * the vnode's cached size, fix the vnode cache to
1005 * match the real object size.
1008 * * following attribute cache expiration, or
1009 * * due a bug in the daemon, or
1010 * * the first time that we looked up the file.
1013 if (vnode_isreg(vp) &&
1014 filesize != fvdat->filesize) {
1016 * The FN_SIZECHANGE flag reflects a dirty
1017 * append. If userspace lets us know our cache
1018 * is invalid, that write was lost. (Dirty
1019 * writes that do not cause append are also
1020 * lost, but we don't detect them here.)
1022 * XXX: Maybe disable WB caching on this mount.
1024 if (fvdat->flag & FN_SIZECHANGE)
1025 printf("%s: WB cache incoherent on "
1027 vnode_mount(vp)->mnt_stat.f_mntonname);
1029 (void)fuse_vnode_setsize(vp, cred, filesize);
1030 fvdat->flag &= ~FN_SIZECHANGE;
1034 fuse_internal_cache_attrs(*vpp, &feo->attr,
1035 feo->attr_valid, feo->attr_valid_nsec, NULL);
1037 if ((nameiop == DELETE || nameiop == RENAME) &&
1040 struct vattr dvattr;
1042 err = fuse_internal_access(dvp, VWRITE, td,
1047 * if the parent's sticky bit is set, check
1048 * whether we're allowed to remove the file.
1049 * Need to figure out the vnode locking to make
1052 fuse_internal_getattr(dvp, &dvattr, cred, td);
1053 if ((dvattr.va_mode & S_ISTXT) &&
1054 fuse_internal_access(dvp, VADMIN, td,
1056 fuse_internal_access(*vpp, VADMIN, td,
1064 (nameiop == DELETE) ||
1065 (nameiop == RENAME && wantparent))) {
1066 cnp->cn_flags |= SAVENAME;
1073 if (vp != NULL && dvp != vp)
1075 else if (vp != NULL)
1080 fdisp_destroy(&fdi);
1086 struct vnop_mkdir_args {
1087 struct vnode *a_dvp;
1088 struct vnode **a_vpp;
1089 struct componentname *a_cnp;
1090 struct vattr *a_vap;
1094 fuse_vnop_mkdir(struct vop_mkdir_args *ap)
1096 struct vnode *dvp = ap->a_dvp;
1097 struct vnode **vpp = ap->a_vpp;
1098 struct componentname *cnp = ap->a_cnp;
1099 struct vattr *vap = ap->a_vap;
1101 struct fuse_mkdir_in fmdi;
1103 if (fuse_isdeadfs(dvp)) {
1106 fmdi.mode = MAKEIMODE(vap->va_type, vap->va_mode);
1108 return (fuse_internal_newentry(dvp, vpp, cnp, FUSE_MKDIR, &fmdi,
1109 sizeof(fmdi), VDIR));
1113 struct vnop_mknod_args {
1114 struct vnode *a_dvp;
1115 struct vnode **a_vpp;
1116 struct componentname *a_cnp;
1117 struct vattr *a_vap;
1121 fuse_vnop_mknod(struct vop_mknod_args *ap)
1124 struct vnode *dvp = ap->a_dvp;
1125 struct vnode **vpp = ap->a_vpp;
1126 struct componentname *cnp = ap->a_cnp;
1127 struct vattr *vap = ap->a_vap;
1128 struct fuse_mknod_in fmni;
1130 if (fuse_isdeadfs(dvp))
1133 fmni.mode = MAKEIMODE(vap->va_type, vap->va_mode);
1134 fmni.rdev = vap->va_rdev;
1135 return (fuse_internal_newentry(dvp, vpp, cnp, FUSE_MKNOD, &fmni,
1136 sizeof(fmni), vap->va_type));
1140 struct vnop_open_args {
1143 struct ucred *a_cred;
1144 struct thread *a_td;
1145 int a_fdidx; / struct file *a_fp;
1149 fuse_vnop_open(struct vop_open_args *ap)
1151 struct vnode *vp = ap->a_vp;
1152 int a_mode = ap->a_mode;
1153 struct thread *td = ap->a_td;
1154 struct ucred *cred = ap->a_cred;
1155 pid_t pid = td->td_proc->p_pid;
1156 struct fuse_vnode_data *fvdat;
1158 if (fuse_isdeadfs(vp))
1160 if (vp->v_type == VCHR || vp->v_type == VBLK || vp->v_type == VFIFO)
1161 return (EOPNOTSUPP);
1162 if ((a_mode & (FREAD | FWRITE | FEXEC)) == 0)
1167 if (fuse_filehandle_validrw(vp, a_mode, cred, pid)) {
1168 fuse_vnode_open(vp, 0, td);
1172 return fuse_filehandle_open(vp, a_mode, NULL, td, cred);
1176 fuse_vnop_pathconf(struct vop_pathconf_args *ap)
1179 switch (ap->a_name) {
1180 case _PC_FILESIZEBITS:
1184 *ap->a_retval = NAME_MAX;
1187 *ap->a_retval = MIN(LONG_MAX, FUSE_LINK_MAX);
1189 case _PC_SYMLINK_MAX:
1190 *ap->a_retval = MAXPATHLEN;
1196 return (vop_stdpathconf(ap));
1201 struct vnop_read_args {
1205 struct ucred *a_cred;
1209 fuse_vnop_read(struct vop_read_args *ap)
1211 struct vnode *vp = ap->a_vp;
1212 struct uio *uio = ap->a_uio;
1213 int ioflag = ap->a_ioflag;
1214 struct ucred *cred = ap->a_cred;
1215 pid_t pid = curthread->td_proc->p_pid;
1217 if (fuse_isdeadfs(vp)) {
1221 if (VTOFUD(vp)->flag & FN_DIRECTIO) {
1222 ioflag |= IO_DIRECT;
1225 return fuse_io_dispatch(vp, uio, ioflag, cred, pid);
1229 struct vnop_readdir_args {
1232 struct ucred *a_cred;
1239 fuse_vnop_readdir(struct vop_readdir_args *ap)
1241 struct vnode *vp = ap->a_vp;
1242 struct uio *uio = ap->a_uio;
1243 struct ucred *cred = ap->a_cred;
1244 struct fuse_filehandle *fufh = NULL;
1245 struct fuse_iov cookediov;
1247 pid_t pid = curthread->td_proc->p_pid;
1249 if (fuse_isdeadfs(vp)) {
1252 if ( /* XXXIP ((uio_iovcnt(uio) > 1)) || */
1253 (uio_resid(uio) < sizeof(struct dirent))) {
1257 err = fuse_filehandle_get_dir(vp, &fufh, cred, pid);
1260 #define DIRCOOKEDSIZE FUSE_DIRENT_ALIGN(FUSE_NAME_OFFSET + MAXNAMLEN + 1)
1261 fiov_init(&cookediov, DIRCOOKEDSIZE);
1263 err = fuse_internal_readdir(vp, uio, fufh, &cookediov);
1265 fiov_teardown(&cookediov);
1271 struct vnop_readlink_args {
1274 struct ucred *a_cred;
1278 fuse_vnop_readlink(struct vop_readlink_args *ap)
1280 struct vnode *vp = ap->a_vp;
1281 struct uio *uio = ap->a_uio;
1282 struct ucred *cred = ap->a_cred;
1284 struct fuse_dispatcher fdi;
1287 if (fuse_isdeadfs(vp)) {
1290 if (!vnode_islnk(vp)) {
1293 fdisp_init(&fdi, 0);
1294 err = fdisp_simple_putget_vp(&fdi, FUSE_READLINK, vp, curthread, cred);
1298 if (((char *)fdi.answ)[0] == '/' &&
1299 fuse_get_mpdata(vnode_mount(vp))->dataflags & FSESS_PUSH_SYMLINKS_IN) {
1300 char *mpth = vnode_mount(vp)->mnt_stat.f_mntonname;
1302 err = uiomove(mpth, strlen(mpth), uio);
1305 err = uiomove(fdi.answ, fdi.iosize, uio);
1308 fdisp_destroy(&fdi);
1313 struct vnop_reclaim_args {
1315 struct thread *a_td;
1319 fuse_vnop_reclaim(struct vop_reclaim_args *ap)
1321 struct vnode *vp = ap->a_vp;
1322 struct thread *td = ap->a_td;
1323 struct fuse_vnode_data *fvdat = VTOFUD(vp);
1324 struct fuse_filehandle *fufh, *fufh_tmp;
1327 panic("FUSE: no vnode data during recycling");
1329 LIST_FOREACH_SAFE(fufh, &fvdat->handles, next, fufh_tmp) {
1330 printf("FUSE: vnode being reclaimed with open fufh "
1331 "(type=%#x)", fufh->fufh_type);
1332 fuse_filehandle_close(vp, fufh, td, NULL);
1335 if ((!fuse_isdeadfs(vp)) && (fvdat->nlookup)) {
1336 fuse_internal_forget_send(vnode_mount(vp), td, NULL, VTOI(vp),
1339 fuse_vnode_setparent(vp, NULL);
1341 vfs_hash_remove(vp);
1342 vnode_destroy_vobject(vp);
1343 fuse_vnode_destroy(vp);
1349 struct vnop_remove_args {
1350 struct vnode *a_dvp;
1352 struct componentname *a_cnp;
1356 fuse_vnop_remove(struct vop_remove_args *ap)
1358 struct vnode *dvp = ap->a_dvp;
1359 struct vnode *vp = ap->a_vp;
1360 struct componentname *cnp = ap->a_cnp;
1364 if (fuse_isdeadfs(vp)) {
1367 if (vnode_isdir(vp)) {
1372 err = fuse_internal_remove(dvp, vp, cnp, FUSE_UNLINK);
1375 fuse_internal_vnode_disappear(vp);
1380 struct vnop_rename_args {
1381 struct vnode *a_fdvp;
1382 struct vnode *a_fvp;
1383 struct componentname *a_fcnp;
1384 struct vnode *a_tdvp;
1385 struct vnode *a_tvp;
1386 struct componentname *a_tcnp;
1390 fuse_vnop_rename(struct vop_rename_args *ap)
1392 struct vnode *fdvp = ap->a_fdvp;
1393 struct vnode *fvp = ap->a_fvp;
1394 struct componentname *fcnp = ap->a_fcnp;
1395 struct vnode *tdvp = ap->a_tdvp;
1396 struct vnode *tvp = ap->a_tvp;
1397 struct componentname *tcnp = ap->a_tcnp;
1398 struct fuse_data *data;
1402 if (fuse_isdeadfs(fdvp)) {
1405 if (fvp->v_mount != tdvp->v_mount ||
1406 (tvp && fvp->v_mount != tvp->v_mount)) {
1407 SDT_PROBE2(fuse, , vnops, trace, 1, "cross-device rename");
1414 * FUSE library is expected to check if target directory is not
1415 * under the source directory in the file system tree.
1416 * Linux performs this check at VFS level.
1418 data = fuse_get_mpdata(vnode_mount(tdvp));
1419 sx_xlock(&data->rename_lock);
1420 err = fuse_internal_rename(fdvp, fcnp, tdvp, tcnp);
1423 fuse_vnode_setparent(fvp, tdvp);
1425 fuse_vnode_setparent(tvp, NULL);
1427 sx_unlock(&data->rename_lock);
1429 if (tvp != NULL && tvp != fvp) {
1432 if (vnode_isdir(fvp)) {
1433 if ((tvp != NULL) && vnode_isdir(tvp)) {
1454 struct vnop_rmdir_args {
1455 struct vnode *a_dvp;
1457 struct componentname *a_cnp;
1461 fuse_vnop_rmdir(struct vop_rmdir_args *ap)
1463 struct vnode *dvp = ap->a_dvp;
1464 struct vnode *vp = ap->a_vp;
1468 if (fuse_isdeadfs(vp)) {
1471 if (VTOFUD(vp) == VTOFUD(dvp)) {
1474 err = fuse_internal_remove(dvp, vp, ap->a_cnp, FUSE_RMDIR);
1477 fuse_internal_vnode_disappear(vp);
1482 struct vnop_setattr_args {
1484 struct vattr *a_vap;
1485 struct ucred *a_cred;
1486 struct thread *a_td;
1490 fuse_vnop_setattr(struct vop_setattr_args *ap)
1492 struct vnode *vp = ap->a_vp;
1493 struct vattr *vap = ap->a_vap;
1494 struct ucred *cred = ap->a_cred;
1495 struct thread *td = curthread;
1496 struct fuse_dispatcher fdi;
1497 struct fuse_setattr_in *fsai;
1498 pid_t pid = td->td_proc->p_pid;
1502 int sizechanged = 0;
1503 uint64_t newsize = 0;
1504 accmode_t accmode = 0;
1506 if (fuse_isdeadfs(vp)) {
1509 fdisp_init(&fdi, sizeof(*fsai));
1510 fdisp_make_vp(&fdi, FUSE_SETATTR, vp, td, cred);
1514 if (vap->va_uid != (uid_t)VNOVAL) {
1515 fsai->uid = vap->va_uid;
1516 fsai->valid |= FATTR_UID;
1519 if (vap->va_gid != (gid_t)VNOVAL) {
1520 fsai->gid = vap->va_gid;
1521 fsai->valid |= FATTR_GID;
1524 if (vap->va_size != VNOVAL) {
1526 struct fuse_filehandle *fufh = NULL;
1528 /*Truncate to a new value. */
1529 fsai->size = vap->va_size;
1531 newsize = vap->va_size;
1532 fsai->valid |= FATTR_SIZE;
1535 fuse_filehandle_getrw(vp, FWRITE, &fufh, cred, pid);
1537 fsai->fh = fufh->fh_id;
1538 fsai->valid |= FATTR_FH;
1541 if (vap->va_atime.tv_sec != VNOVAL) {
1542 fsai->atime = vap->va_atime.tv_sec;
1543 fsai->atimensec = vap->va_atime.tv_nsec;
1544 fsai->valid |= FATTR_ATIME;
1547 * TODO: only require VWRITE if UTIMENS_NULL is set. PR 237181
1550 if (vap->va_mtime.tv_sec != VNOVAL) {
1551 fsai->mtime = vap->va_mtime.tv_sec;
1552 fsai->mtimensec = vap->va_mtime.tv_nsec;
1553 fsai->valid |= FATTR_MTIME;
1556 * TODO: only require VWRITE if UTIMENS_NULL is set. PR 237181
1559 if (vap->va_mode != (mode_t)VNOVAL) {
1560 fsai->mode = vap->va_mode & ALLPERMS;
1561 fsai->valid |= FATTR_MODE;
1567 vtyp = vnode_vtype(vp);
1569 if (fsai->valid & FATTR_SIZE && vtyp == VDIR) {
1573 if (vfs_isrdonly(vnode_mount(vp)) && (fsai->valid & ~FATTR_SIZE || vtyp == VREG)) {
1577 err = fuse_internal_access(vp, accmode, td, cred);
1581 if ((err = fdisp_wait_answ(&fdi)))
1583 vtyp = IFTOVT(((struct fuse_attr_out *)fdi.answ)->attr.mode);
1585 if (vnode_vtype(vp) != vtyp) {
1586 if (vnode_vtype(vp) == VNON && vtyp != VNON) {
1587 SDT_PROBE2(fuse, , vnops, trace, 1, "FUSE: Dang! "
1588 "vnode_vtype is VNON and vtype isn't.");
1591 * STALE vnode, ditch
1593 * The vnode has changed its type "behind our back".
1594 * There's nothing really we can do, so let us just
1595 * force an internal revocation and tell the caller to
1596 * try again, if interested.
1598 fuse_internal_vnode_disappear(vp);
1603 struct fuse_attr_out *fao = (struct fuse_attr_out*)fdi.answ;
1604 fuse_internal_cache_attrs(vp, &fao->attr, fao->attr_valid,
1605 fao->attr_valid_nsec, NULL);
1609 fdisp_destroy(&fdi);
1610 if (!err && sizechanged) {
1611 fuse_vnode_setsize(vp, cred, newsize);
1612 VTOFUD(vp)->flag &= ~FN_SIZECHANGE;
1618 struct vnop_strategy_args {
1624 fuse_vnop_strategy(struct vop_strategy_args *ap)
1626 struct vnode *vp = ap->a_vp;
1627 struct buf *bp = ap->a_bp;
1629 if (!vp || fuse_isdeadfs(vp)) {
1630 bp->b_ioflags |= BIO_ERROR;
1631 bp->b_error = ENXIO;
1635 if (bp->b_iocmd == BIO_WRITE) {
1638 err = fuse_vnode_refreshsize(vp, NOCRED);
1640 bp->b_ioflags |= BIO_ERROR;
1648 * VOP_STRATEGY always returns zero and signals error via bp->b_ioflags.
1649 * fuse_io_strategy sets bp's error fields
1651 (void)fuse_io_strategy(vp, bp);
1658 struct vnop_symlink_args {
1659 struct vnode *a_dvp;
1660 struct vnode **a_vpp;
1661 struct componentname *a_cnp;
1662 struct vattr *a_vap;
1667 fuse_vnop_symlink(struct vop_symlink_args *ap)
1669 struct vnode *dvp = ap->a_dvp;
1670 struct vnode **vpp = ap->a_vpp;
1671 struct componentname *cnp = ap->a_cnp;
1672 const char *target = ap->a_target;
1674 struct fuse_dispatcher fdi;
1679 if (fuse_isdeadfs(dvp)) {
1683 * Unlike the other creator type calls, here we have to create a message
1684 * where the name of the new entry comes first, and the data describing
1685 * the entry comes second.
1686 * Hence we can't rely on our handy fuse_internal_newentry() routine,
1687 * but put together the message manually and just call the core part.
1690 len = strlen(target) + 1;
1691 fdisp_init(&fdi, len + cnp->cn_namelen + 1);
1692 fdisp_make_vp(&fdi, FUSE_SYMLINK, dvp, curthread, NULL);
1694 memcpy(fdi.indata, cnp->cn_nameptr, cnp->cn_namelen);
1695 ((char *)fdi.indata)[cnp->cn_namelen] = '\0';
1696 memcpy((char *)fdi.indata + cnp->cn_namelen + 1, target, len);
1698 err = fuse_internal_newentry_core(dvp, vpp, cnp, VLNK, &fdi);
1699 fdisp_destroy(&fdi);
1704 struct vnop_write_args {
1708 struct ucred *a_cred;
1712 fuse_vnop_write(struct vop_write_args *ap)
1714 struct vnode *vp = ap->a_vp;
1715 struct uio *uio = ap->a_uio;
1716 int ioflag = ap->a_ioflag;
1717 struct ucred *cred = ap->a_cred;
1718 pid_t pid = curthread->td_proc->p_pid;
1721 if (fuse_isdeadfs(vp)) {
1724 err = fuse_vnode_refreshsize(vp, cred);
1728 if (VTOFUD(vp)->flag & FN_DIRECTIO) {
1729 ioflag |= IO_DIRECT;
1732 return fuse_io_dispatch(vp, uio, ioflag, cred, pid);
1735 SDT_PROBE_DEFINE1(fuse, , vnops, vnop_getpages_error, "int");
1737 struct vnop_getpages_args {
1745 fuse_vnop_getpages(struct vop_getpages_args *ap)
1747 int i, error, nextoff, size, toff, count, npages;
1756 pid_t pid = curthread->td_proc->p_pid;
1759 KASSERT(vp->v_object, ("objectless vp passed to getpages"));
1760 td = curthread; /* XXX */
1761 cred = curthread->td_ucred; /* XXX */
1763 npages = ap->a_count;
1765 if (!fsess_opt_mmap(vnode_mount(vp))) {
1766 SDT_PROBE2(fuse, , vnops, trace, 1,
1767 "called on non-cacheable vnode??\n");
1768 return (VM_PAGER_ERROR);
1772 * If the last page is partially valid, just return it and allow
1773 * the pager to zero-out the blanks. Partially valid pages can
1774 * only occur at the file EOF.
1776 * XXXGL: is that true for FUSE, which is a local filesystem,
1777 * but still somewhat disconnected from the kernel?
1779 VM_OBJECT_WLOCK(vp->v_object);
1780 if (pages[npages - 1]->valid != 0 && --npages == 0)
1782 VM_OBJECT_WUNLOCK(vp->v_object);
1785 * We use only the kva address for the buffer, but this is extremely
1786 * convenient and fast.
1788 bp = uma_zalloc(fuse_pbuf_zone, M_WAITOK);
1790 kva = (vm_offset_t)bp->b_data;
1791 pmap_qenter(kva, pages, npages);
1792 VM_CNT_INC(v_vnodein);
1793 VM_CNT_ADD(v_vnodepgsin, npages);
1795 count = npages << PAGE_SHIFT;
1796 iov.iov_base = (caddr_t)kva;
1797 iov.iov_len = count;
1800 uio.uio_offset = IDX_TO_OFF(pages[0]->pindex);
1801 uio.uio_resid = count;
1802 uio.uio_segflg = UIO_SYSSPACE;
1803 uio.uio_rw = UIO_READ;
1806 error = fuse_io_dispatch(vp, &uio, IO_DIRECT, cred, pid);
1807 pmap_qremove(kva, npages);
1809 uma_zfree(fuse_pbuf_zone, bp);
1811 if (error && (uio.uio_resid == count)) {
1812 SDT_PROBE1(fuse, , vnops, vnop_getpages_error, error);
1813 return VM_PAGER_ERROR;
1816 * Calculate the number of bytes read and validate only that number
1817 * of bytes. Note that due to pending writes, size may be 0. This
1818 * does not mean that the remaining data is invalid!
1821 size = count - uio.uio_resid;
1822 VM_OBJECT_WLOCK(vp->v_object);
1823 fuse_vm_page_lock_queues();
1824 for (i = 0, toff = 0; i < npages; i++, toff = nextoff) {
1827 nextoff = toff + PAGE_SIZE;
1830 if (nextoff <= size) {
1832 * Read operation filled an entire page
1834 m->valid = VM_PAGE_BITS_ALL;
1835 KASSERT(m->dirty == 0,
1836 ("fuse_getpages: page %p is dirty", m));
1837 } else if (size > toff) {
1839 * Read operation filled a partial page.
1842 vm_page_set_valid_range(m, 0, size - toff);
1843 KASSERT(m->dirty == 0,
1844 ("fuse_getpages: page %p is dirty", m));
1847 * Read operation was short. If no error occurred
1848 * we may have hit a zero-fill section. We simply
1849 * leave valid set to 0.
1854 fuse_vm_page_unlock_queues();
1856 VM_OBJECT_WUNLOCK(vp->v_object);
1861 return (VM_PAGER_OK);
1865 struct vnop_putpages_args {
1871 vm_ooffset_t a_offset;
1875 fuse_vnop_putpages(struct vop_putpages_args *ap)
1881 int i, error, npages, count;
1889 pid_t pid = curthread->td_proc->p_pid;
1892 KASSERT(vp->v_object, ("objectless vp passed to putpages"));
1893 fsize = vp->v_object->un_pager.vnp.vnp_size;
1894 td = curthread; /* XXX */
1895 cred = curthread->td_ucred; /* XXX */
1897 count = ap->a_count;
1898 rtvals = ap->a_rtvals;
1899 npages = btoc(count);
1900 offset = IDX_TO_OFF(pages[0]->pindex);
1902 if (!fsess_opt_mmap(vnode_mount(vp))) {
1903 SDT_PROBE2(fuse, , vnops, trace, 1,
1904 "called on non-cacheable vnode??\n");
1906 for (i = 0; i < npages; i++)
1907 rtvals[i] = VM_PAGER_AGAIN;
1910 * When putting pages, do not extend file past EOF.
1913 if (offset + count > fsize) {
1914 count = fsize - offset;
1919 * We use only the kva address for the buffer, but this is extremely
1920 * convenient and fast.
1922 bp = uma_zalloc(fuse_pbuf_zone, M_WAITOK);
1924 kva = (vm_offset_t)bp->b_data;
1925 pmap_qenter(kva, pages, npages);
1926 VM_CNT_INC(v_vnodeout);
1927 VM_CNT_ADD(v_vnodepgsout, count);
1929 iov.iov_base = (caddr_t)kva;
1930 iov.iov_len = count;
1933 uio.uio_offset = offset;
1934 uio.uio_resid = count;
1935 uio.uio_segflg = UIO_SYSSPACE;
1936 uio.uio_rw = UIO_WRITE;
1939 error = fuse_io_dispatch(vp, &uio, IO_DIRECT, cred, pid);
1941 pmap_qremove(kva, npages);
1942 uma_zfree(fuse_pbuf_zone, bp);
1945 int nwritten = round_page(count - uio.uio_resid) / PAGE_SIZE;
1947 for (i = 0; i < nwritten; i++) {
1948 rtvals[i] = VM_PAGER_OK;
1949 VM_OBJECT_WLOCK(pages[i]->object);
1950 vm_page_undirty(pages[i]);
1951 VM_OBJECT_WUNLOCK(pages[i]->object);
1957 static const char extattr_namespace_separator = '.';
1960 struct vop_getextattr_args {
1961 struct vop_generic_args a_gen;
1963 int a_attrnamespace;
1967 struct ucred *a_cred;
1968 struct thread *a_td;
1972 fuse_vnop_getextattr(struct vop_getextattr_args *ap)
1974 struct vnode *vp = ap->a_vp;
1975 struct uio *uio = ap->a_uio;
1976 struct fuse_dispatcher fdi;
1977 struct fuse_getxattr_in *get_xattr_in;
1978 struct fuse_getxattr_out *get_xattr_out;
1979 struct mount *mp = vnode_mount(vp);
1980 struct thread *td = ap->a_td;
1981 struct ucred *cred = ap->a_cred;
1987 if (fuse_isdeadfs(vp))
1990 if (!fsess_isimpl(mp, FUSE_GETXATTR))
1993 err = fuse_extattr_check_cred(vp, ap->a_attrnamespace, cred, td, VREAD);
1997 /* Default to looking for user attributes. */
1998 if (ap->a_attrnamespace == EXTATTR_NAMESPACE_SYSTEM)
1999 prefix = EXTATTR_NAMESPACE_SYSTEM_STRING;
2001 prefix = EXTATTR_NAMESPACE_USER_STRING;
2003 len = strlen(prefix) + sizeof(extattr_namespace_separator) +
2004 strlen(ap->a_name) + 1;
2006 fdisp_init(&fdi, len + sizeof(*get_xattr_in));
2007 fdisp_make_vp(&fdi, FUSE_GETXATTR, vp, td, cred);
2009 get_xattr_in = fdi.indata;
2011 * Check to see whether we're querying the available size or
2012 * issuing the actual request. If we pass in 0, we get back struct
2013 * fuse_getxattr_out. If we pass in a non-zero size, we get back
2014 * that much data, without the struct fuse_getxattr_out header.
2017 get_xattr_in->size = 0;
2019 get_xattr_in->size = uio->uio_resid;
2021 attr_str = (char *)fdi.indata + sizeof(*get_xattr_in);
2022 snprintf(attr_str, len, "%s%c%s", prefix, extattr_namespace_separator,
2025 err = fdisp_wait_answ(&fdi);
2027 if (err == ENOSYS) {
2028 fsess_set_notimpl(mp, FUSE_GETXATTR);
2034 get_xattr_out = fdi.answ;
2036 if (ap->a_size != NULL)
2037 *ap->a_size = get_xattr_out->size;
2040 err = uiomove(fdi.answ, fdi.iosize, uio);
2043 fdisp_destroy(&fdi);
2048 struct vop_setextattr_args {
2049 struct vop_generic_args a_gen;
2051 int a_attrnamespace;
2054 struct ucred *a_cred;
2055 struct thread *a_td;
2059 fuse_vnop_setextattr(struct vop_setextattr_args *ap)
2061 struct vnode *vp = ap->a_vp;
2062 struct uio *uio = ap->a_uio;
2063 struct fuse_dispatcher fdi;
2064 struct fuse_setxattr_in *set_xattr_in;
2065 struct mount *mp = vnode_mount(vp);
2066 struct thread *td = ap->a_td;
2067 struct ucred *cred = ap->a_cred;
2073 if (fuse_isdeadfs(vp))
2076 if (!fsess_isimpl(mp, FUSE_SETXATTR))
2079 if (vfs_isrdonly(mp))
2082 /* Deleting xattrs must use VOP_DELETEEXTATTR instead */
2083 if (ap->a_uio == NULL) {
2085 * If we got here as fallback from VOP_DELETEEXTATTR, then
2086 * return EOPNOTSUPP.
2088 if (!fsess_isimpl(mp, FUSE_REMOVEXATTR))
2089 return (EOPNOTSUPP);
2094 err = fuse_extattr_check_cred(vp, ap->a_attrnamespace, cred, td,
2099 /* Default to looking for user attributes. */
2100 if (ap->a_attrnamespace == EXTATTR_NAMESPACE_SYSTEM)
2101 prefix = EXTATTR_NAMESPACE_SYSTEM_STRING;
2103 prefix = EXTATTR_NAMESPACE_USER_STRING;
2105 len = strlen(prefix) + sizeof(extattr_namespace_separator) +
2106 strlen(ap->a_name) + 1;
2108 fdisp_init(&fdi, len + sizeof(*set_xattr_in) + uio->uio_resid);
2109 fdisp_make_vp(&fdi, FUSE_SETXATTR, vp, td, cred);
2111 set_xattr_in = fdi.indata;
2112 set_xattr_in->size = uio->uio_resid;
2114 attr_str = (char *)fdi.indata + sizeof(*set_xattr_in);
2115 snprintf(attr_str, len, "%s%c%s", prefix, extattr_namespace_separator,
2118 err = uiomove((char *)fdi.indata + sizeof(*set_xattr_in) + len,
2119 uio->uio_resid, uio);
2124 err = fdisp_wait_answ(&fdi);
2126 if (err == ENOSYS) {
2127 fsess_set_notimpl(mp, FUSE_SETXATTR);
2132 fdisp_destroy(&fdi);
2137 * The Linux / FUSE extended attribute list is simply a collection of
2138 * NUL-terminated strings. The FreeBSD extended attribute list is a single
2139 * byte length followed by a non-NUL terminated string. So, this allows
2140 * conversion of the Linux / FUSE format to the FreeBSD format in place.
2141 * Linux attribute names are reported with the namespace as a prefix (e.g.
2142 * "user.attribute_name"), but in FreeBSD they are reported without the
2143 * namespace prefix (e.g. "attribute_name"). So, we're going from:
2145 * user.attr_name1\0user.attr_name2\0
2149 * <num>attr_name1<num>attr_name2
2151 * Where "<num>" is a single byte number of characters in the attribute name.
2154 * prefix - exattr namespace prefix string
2155 * list, list_len - input list with namespace prefixes
2156 * bsd_list, bsd_list_len - output list compatible with bsd vfs
2159 fuse_xattrlist_convert(char *prefix, const char *list, int list_len,
2160 char *bsd_list, int *bsd_list_len)
2162 int len, pos, dist_to_next, prefix_len;
2166 prefix_len = strlen(prefix);
2168 while (pos < list_len && list[pos] != '\0') {
2169 dist_to_next = strlen(&list[pos]) + 1;
2170 if (bcmp(&list[pos], prefix, prefix_len) == 0 &&
2171 list[pos + prefix_len] == extattr_namespace_separator) {
2172 len = dist_to_next -
2173 (prefix_len + sizeof(extattr_namespace_separator)) - 1;
2174 if (len >= EXTATTR_MAXNAMELEN)
2175 return (ENAMETOOLONG);
2177 bsd_list[*bsd_list_len] = len;
2178 memcpy(&bsd_list[*bsd_list_len + 1],
2179 &list[pos + prefix_len +
2180 sizeof(extattr_namespace_separator)], len);
2182 *bsd_list_len += len + 1;
2185 pos += dist_to_next;
2192 struct vop_listextattr_args {
2193 struct vop_generic_args a_gen;
2195 int a_attrnamespace;
2198 struct ucred *a_cred;
2199 struct thread *a_td;
2203 fuse_vnop_listextattr(struct vop_listextattr_args *ap)
2205 struct vnode *vp = ap->a_vp;
2206 struct uio *uio = ap->a_uio;
2207 struct fuse_dispatcher fdi;
2208 struct fuse_listxattr_in *list_xattr_in;
2209 struct fuse_listxattr_out *list_xattr_out;
2210 struct mount *mp = vnode_mount(vp);
2211 struct thread *td = ap->a_td;
2212 struct ucred *cred = ap->a_cred;
2216 char *bsd_list = NULL;
2222 if (fuse_isdeadfs(vp))
2225 if (!fsess_isimpl(mp, FUSE_LISTXATTR))
2228 err = fuse_extattr_check_cred(vp, ap->a_attrnamespace, cred, td, VREAD);
2233 * Add space for a NUL and the period separator if enabled.
2234 * Default to looking for user attributes.
2236 if (ap->a_attrnamespace == EXTATTR_NAMESPACE_SYSTEM)
2237 prefix = EXTATTR_NAMESPACE_SYSTEM_STRING;
2239 prefix = EXTATTR_NAMESPACE_USER_STRING;
2241 len = strlen(prefix) + sizeof(extattr_namespace_separator) + 1;
2243 fdisp_init(&fdi, sizeof(*list_xattr_in) + len);
2244 fdisp_make_vp(&fdi, FUSE_LISTXATTR, vp, td, cred);
2247 * Retrieve Linux / FUSE compatible list size.
2249 list_xattr_in = fdi.indata;
2250 list_xattr_in->size = 0;
2251 attr_str = (char *)fdi.indata + sizeof(*list_xattr_in);
2252 snprintf(attr_str, len, "%s%c", prefix, extattr_namespace_separator);
2254 err = fdisp_wait_answ(&fdi);
2256 if (err == ENOSYS) {
2257 fsess_set_notimpl(mp, FUSE_LISTXATTR);
2263 list_xattr_out = fdi.answ;
2264 linux_list_len = list_xattr_out->size;
2265 if (linux_list_len == 0) {
2266 if (ap->a_size != NULL)
2267 *ap->a_size = linux_list_len;
2272 * Retrieve Linux / FUSE compatible list values.
2274 fdisp_refresh_vp(&fdi, FUSE_LISTXATTR, vp, td, cred);
2275 list_xattr_in = fdi.indata;
2276 list_xattr_in->size = linux_list_len + sizeof(*list_xattr_out);
2277 list_xattr_in->flags = 0;
2278 attr_str = (char *)fdi.indata + sizeof(*list_xattr_in);
2279 snprintf(attr_str, len, "%s%c", prefix, extattr_namespace_separator);
2281 err = fdisp_wait_answ(&fdi);
2285 linux_list = fdi.answ;
2286 linux_list_len = fdi.iosize;
2289 * Retrieve the BSD compatible list values.
2290 * The Linux / FUSE attribute list format isn't the same
2291 * as FreeBSD's format. So we need to transform it into
2292 * FreeBSD's format before giving it to the user.
2294 bsd_list = malloc(linux_list_len, M_TEMP, M_WAITOK);
2295 err = fuse_xattrlist_convert(prefix, linux_list, linux_list_len,
2296 bsd_list, &bsd_list_len);
2300 if (ap->a_size != NULL)
2301 *ap->a_size = bsd_list_len;
2304 err = uiomove(bsd_list, bsd_list_len, uio);
2307 free(bsd_list, M_TEMP);
2308 fdisp_destroy(&fdi);
2313 struct vop_deleteextattr_args {
2314 struct vop_generic_args a_gen;
2316 int a_attrnamespace;
2318 struct ucred *a_cred;
2319 struct thread *a_td;
2323 fuse_vnop_deleteextattr(struct vop_deleteextattr_args *ap)
2325 struct vnode *vp = ap->a_vp;
2326 struct fuse_dispatcher fdi;
2327 struct mount *mp = vnode_mount(vp);
2328 struct thread *td = ap->a_td;
2329 struct ucred *cred = ap->a_cred;
2335 if (fuse_isdeadfs(vp))
2338 if (!fsess_isimpl(mp, FUSE_REMOVEXATTR))
2341 if (vfs_isrdonly(mp))
2344 err = fuse_extattr_check_cred(vp, ap->a_attrnamespace, cred, td,
2349 /* Default to looking for user attributes. */
2350 if (ap->a_attrnamespace == EXTATTR_NAMESPACE_SYSTEM)
2351 prefix = EXTATTR_NAMESPACE_SYSTEM_STRING;
2353 prefix = EXTATTR_NAMESPACE_USER_STRING;
2355 len = strlen(prefix) + sizeof(extattr_namespace_separator) +
2356 strlen(ap->a_name) + 1;
2358 fdisp_init(&fdi, len);
2359 fdisp_make_vp(&fdi, FUSE_REMOVEXATTR, vp, td, cred);
2361 attr_str = fdi.indata;
2362 snprintf(attr_str, len, "%s%c%s", prefix, extattr_namespace_separator,
2365 err = fdisp_wait_answ(&fdi);
2366 if (err == ENOSYS) {
2367 fsess_set_notimpl(mp, FUSE_REMOVEXATTR);
2371 fdisp_destroy(&fdi);
2376 struct vnop_print_args {
2381 fuse_vnop_print(struct vop_print_args *ap)
2383 struct fuse_vnode_data *fvdat = VTOFUD(ap->a_vp);
2385 printf("nodeid: %ju, parent nodeid: %ju, nlookup: %ju, flag: %#x\n",
2386 (uintmax_t)VTOILLU(ap->a_vp), (uintmax_t)fvdat->parent_nid,
2387 (uintmax_t)fvdat->nlookup,