xref: /freebsd/sys/fs/fuse/fuse_vnops.c (revision ff4fbdf548c152be65e7f209b2ad8966d238ca2c)
151369649SPedro F. Giffuni /*-
251369649SPedro F. Giffuni  * SPDX-License-Identifier: BSD-3-Clause
351369649SPedro F. Giffuni  *
45fe58019SAttilio Rao  * Copyright (c) 2007-2009 Google Inc. and Amit Singh
55fe58019SAttilio Rao  * All rights reserved.
65fe58019SAttilio Rao  *
75fe58019SAttilio Rao  * Redistribution and use in source and binary forms, with or without
85fe58019SAttilio Rao  * modification, are permitted provided that the following conditions are
95fe58019SAttilio Rao  * met:
105fe58019SAttilio Rao  *
115fe58019SAttilio Rao  * * Redistributions of source code must retain the above copyright
125fe58019SAttilio Rao  *   notice, this list of conditions and the following disclaimer.
135fe58019SAttilio Rao  * * Redistributions in binary form must reproduce the above
145fe58019SAttilio Rao  *   copyright notice, this list of conditions and the following disclaimer
155fe58019SAttilio Rao  *   in the documentation and/or other materials provided with the
165fe58019SAttilio Rao  *   distribution.
175fe58019SAttilio Rao  * * Neither the name of Google Inc. nor the names of its
185fe58019SAttilio Rao  *   contributors may be used to endorse or promote products derived from
195fe58019SAttilio Rao  *   this software without specific prior written permission.
205fe58019SAttilio Rao  *
215fe58019SAttilio Rao  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
225fe58019SAttilio Rao  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
235fe58019SAttilio Rao  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
245fe58019SAttilio Rao  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
255fe58019SAttilio Rao  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
265fe58019SAttilio Rao  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
275fe58019SAttilio Rao  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
285fe58019SAttilio Rao  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
295fe58019SAttilio Rao  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
305fe58019SAttilio Rao  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
315fe58019SAttilio Rao  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
325fe58019SAttilio Rao  *
335fe58019SAttilio Rao  * Copyright (C) 2005 Csaba Henk.
345fe58019SAttilio Rao  * All rights reserved.
355fe58019SAttilio Rao  *
365fe58019SAttilio Rao  * Redistribution and use in source and binary forms, with or without
375fe58019SAttilio Rao  * modification, are permitted provided that the following conditions
385fe58019SAttilio Rao  * are met:
395fe58019SAttilio Rao  * 1. Redistributions of source code must retain the above copyright
405fe58019SAttilio Rao  *    notice, this list of conditions and the following disclaimer.
415fe58019SAttilio Rao  * 2. Redistributions in binary form must reproduce the above copyright
425fe58019SAttilio Rao  *    notice, this list of conditions and the following disclaimer in the
435fe58019SAttilio Rao  *    documentation and/or other materials provided with the distribution.
445fe58019SAttilio Rao  *
455fe58019SAttilio Rao  * THIS SOFTWARE IS PROVIDED BY AUTHOR AND CONTRIBUTORS ``AS IS'' AND
465fe58019SAttilio Rao  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
475fe58019SAttilio Rao  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
485fe58019SAttilio Rao  * ARE DISCLAIMED.  IN NO EVENT SHALL AUTHOR OR CONTRIBUTORS BE LIABLE
495fe58019SAttilio Rao  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
505fe58019SAttilio Rao  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
515fe58019SAttilio Rao  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
525fe58019SAttilio Rao  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
535fe58019SAttilio Rao  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
545fe58019SAttilio Rao  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
555fe58019SAttilio Rao  * SUCH DAMAGE.
565fe58019SAttilio Rao  */
575fe58019SAttilio Rao 
585fe58019SAttilio Rao #include <sys/cdefs.h>
595fe58019SAttilio Rao __FBSDID("$FreeBSD$");
605fe58019SAttilio Rao 
61cf169498SAlan Somers #include <sys/param.h>
625fe58019SAttilio Rao #include <sys/module.h>
635fe58019SAttilio Rao #include <sys/systm.h>
645fe58019SAttilio Rao #include <sys/errno.h>
655fe58019SAttilio Rao #include <sys/kernel.h>
665fe58019SAttilio Rao #include <sys/conf.h>
675fe58019SAttilio Rao #include <sys/uio.h>
685fe58019SAttilio Rao #include <sys/malloc.h>
695fe58019SAttilio Rao #include <sys/queue.h>
705fe58019SAttilio Rao #include <sys/lock.h>
7189f6b863SAttilio Rao #include <sys/rwlock.h>
725fe58019SAttilio Rao #include <sys/sx.h>
735fe58019SAttilio Rao #include <sys/proc.h>
745fe58019SAttilio Rao #include <sys/mount.h>
755fe58019SAttilio Rao #include <sys/vnode.h>
765fe58019SAttilio Rao #include <sys/namei.h>
7704660064SFedor Uporov #include <sys/extattr.h>
785fe58019SAttilio Rao #include <sys/stat.h>
795fe58019SAttilio Rao #include <sys/unistd.h>
805fe58019SAttilio Rao #include <sys/filedesc.h>
815fe58019SAttilio Rao #include <sys/file.h>
825fe58019SAttilio Rao #include <sys/fcntl.h>
835fe58019SAttilio Rao #include <sys/dirent.h>
845fe58019SAttilio Rao #include <sys/bio.h>
855fe58019SAttilio Rao #include <sys/buf.h>
865fe58019SAttilio Rao #include <sys/sysctl.h>
87ca148cdaSGleb Smirnoff #include <sys/vmmeter.h>
885fe58019SAttilio Rao 
895fe58019SAttilio Rao #include <vm/vm.h>
905fe58019SAttilio Rao #include <vm/vm_extern.h>
915fe58019SAttilio Rao #include <vm/pmap.h>
925fe58019SAttilio Rao #include <vm/vm_map.h>
935fe58019SAttilio Rao #include <vm/vm_page.h>
945fe58019SAttilio Rao #include <vm/vm_param.h>
955fe58019SAttilio Rao #include <vm/vm_object.h>
965fe58019SAttilio Rao #include <vm/vm_pager.h>
975fe58019SAttilio Rao #include <vm/vnode_pager.h>
985fe58019SAttilio Rao #include <vm/vm_object.h>
995fe58019SAttilio Rao 
1005fe58019SAttilio Rao #include "fuse.h"
1015fe58019SAttilio Rao #include "fuse_file.h"
1025fe58019SAttilio Rao #include "fuse_internal.h"
1035fe58019SAttilio Rao #include "fuse_ipc.h"
1045fe58019SAttilio Rao #include "fuse_node.h"
1055fe58019SAttilio Rao #include "fuse_io.h"
1065fe58019SAttilio Rao 
1075fe58019SAttilio Rao #include <sys/priv.h>
1085fe58019SAttilio Rao 
109fd2749f2SAlan Somers /* Maximum number of hardlinks to a single FUSE file */
110fd2749f2SAlan Somers #define FUSE_LINK_MAX                      UINT32_MAX
111fd2749f2SAlan Somers 
112cf169498SAlan Somers SDT_PROVIDER_DECLARE(fuse);
113cf169498SAlan Somers /*
114cf169498SAlan Somers  * Fuse trace probe:
115cf169498SAlan Somers  * arg0: verbosity.  Higher numbers give more verbose messages
116cf169498SAlan Somers  * arg1: Textual message
117cf169498SAlan Somers  */
118cf169498SAlan Somers SDT_PROBE_DEFINE2(fuse, , vnops, trace, "int", "char*");
1195fe58019SAttilio Rao 
1205fe58019SAttilio Rao /* vnode ops */
1215fe58019SAttilio Rao static vop_access_t fuse_vnop_access;
1225fe58019SAttilio Rao static vop_close_t fuse_vnop_close;
1235fe58019SAttilio Rao static vop_create_t fuse_vnop_create;
12404660064SFedor Uporov static vop_deleteextattr_t fuse_vnop_deleteextattr;
125915012e0SAlan Somers static vop_fdatasync_t fuse_vnop_fdatasync;
1265fe58019SAttilio Rao static vop_fsync_t fuse_vnop_fsync;
1275fe58019SAttilio Rao static vop_getattr_t fuse_vnop_getattr;
12804660064SFedor Uporov static vop_getextattr_t fuse_vnop_getextattr;
1295fe58019SAttilio Rao static vop_inactive_t fuse_vnop_inactive;
1305fe58019SAttilio Rao static vop_link_t fuse_vnop_link;
13104660064SFedor Uporov static vop_listextattr_t fuse_vnop_listextattr;
1325fe58019SAttilio Rao static vop_lookup_t fuse_vnop_lookup;
1335fe58019SAttilio Rao static vop_mkdir_t fuse_vnop_mkdir;
1345fe58019SAttilio Rao static vop_mknod_t fuse_vnop_mknod;
1355fe58019SAttilio Rao static vop_open_t fuse_vnop_open;
136746c92e0SJohn Baldwin static vop_pathconf_t fuse_vnop_pathconf;
1375fe58019SAttilio Rao static vop_read_t fuse_vnop_read;
1385fe58019SAttilio Rao static vop_readdir_t fuse_vnop_readdir;
1395fe58019SAttilio Rao static vop_readlink_t fuse_vnop_readlink;
1405fe58019SAttilio Rao static vop_reclaim_t fuse_vnop_reclaim;
1415fe58019SAttilio Rao static vop_remove_t fuse_vnop_remove;
1425fe58019SAttilio Rao static vop_rename_t fuse_vnop_rename;
1435fe58019SAttilio Rao static vop_rmdir_t fuse_vnop_rmdir;
1445fe58019SAttilio Rao static vop_setattr_t fuse_vnop_setattr;
14504660064SFedor Uporov static vop_setextattr_t fuse_vnop_setextattr;
1465fe58019SAttilio Rao static vop_strategy_t fuse_vnop_strategy;
1475fe58019SAttilio Rao static vop_symlink_t fuse_vnop_symlink;
1485fe58019SAttilio Rao static vop_write_t fuse_vnop_write;
1495fe58019SAttilio Rao static vop_getpages_t fuse_vnop_getpages;
1505fe58019SAttilio Rao static vop_putpages_t fuse_vnop_putpages;
1515fe58019SAttilio Rao static vop_print_t fuse_vnop_print;
1525fe58019SAttilio Rao 
1535fe58019SAttilio Rao struct vop_vector fuse_vnops = {
1545fe58019SAttilio Rao 	.vop_default = &default_vnodeops,
1555fe58019SAttilio Rao 	.vop_access = fuse_vnop_access,
1565fe58019SAttilio Rao 	.vop_close = fuse_vnop_close,
1575fe58019SAttilio Rao 	.vop_create = fuse_vnop_create,
15804660064SFedor Uporov 	.vop_deleteextattr = fuse_vnop_deleteextattr,
1595fe58019SAttilio Rao 	.vop_fsync = fuse_vnop_fsync,
160915012e0SAlan Somers 	.vop_fdatasync = fuse_vnop_fdatasync,
1615fe58019SAttilio Rao 	.vop_getattr = fuse_vnop_getattr,
16204660064SFedor Uporov 	.vop_getextattr = fuse_vnop_getextattr,
1635fe58019SAttilio Rao 	.vop_inactive = fuse_vnop_inactive,
1645fe58019SAttilio Rao 	.vop_link = fuse_vnop_link,
16504660064SFedor Uporov 	.vop_listextattr = fuse_vnop_listextattr,
1665fe58019SAttilio Rao 	.vop_lookup = fuse_vnop_lookup,
1675fe58019SAttilio Rao 	.vop_mkdir = fuse_vnop_mkdir,
1685fe58019SAttilio Rao 	.vop_mknod = fuse_vnop_mknod,
1695fe58019SAttilio Rao 	.vop_open = fuse_vnop_open,
170746c92e0SJohn Baldwin 	.vop_pathconf = fuse_vnop_pathconf,
1715fe58019SAttilio Rao 	.vop_read = fuse_vnop_read,
1725fe58019SAttilio Rao 	.vop_readdir = fuse_vnop_readdir,
1735fe58019SAttilio Rao 	.vop_readlink = fuse_vnop_readlink,
1745fe58019SAttilio Rao 	.vop_reclaim = fuse_vnop_reclaim,
1755fe58019SAttilio Rao 	.vop_remove = fuse_vnop_remove,
1765fe58019SAttilio Rao 	.vop_rename = fuse_vnop_rename,
1775fe58019SAttilio Rao 	.vop_rmdir = fuse_vnop_rmdir,
1785fe58019SAttilio Rao 	.vop_setattr = fuse_vnop_setattr,
17904660064SFedor Uporov 	.vop_setextattr = fuse_vnop_setextattr,
1805fe58019SAttilio Rao 	.vop_strategy = fuse_vnop_strategy,
1815fe58019SAttilio Rao 	.vop_symlink = fuse_vnop_symlink,
1825fe58019SAttilio Rao 	.vop_write = fuse_vnop_write,
1835fe58019SAttilio Rao 	.vop_getpages = fuse_vnop_getpages,
1845fe58019SAttilio Rao 	.vop_putpages = fuse_vnop_putpages,
1855fe58019SAttilio Rao 	.vop_print = fuse_vnop_print,
1865fe58019SAttilio Rao };
1875fe58019SAttilio Rao 
1885fe58019SAttilio Rao static u_long fuse_lookup_cache_hits = 0;
1895fe58019SAttilio Rao 
190123af6ecSAlan Somers SYSCTL_ULONG(_vfs_fusefs, OID_AUTO, lookup_cache_hits, CTLFLAG_RD,
191bd4cb2a4SConrad Meyer     &fuse_lookup_cache_hits, 0, "number of positive cache hits in lookup");
1925fe58019SAttilio Rao 
1935fe58019SAttilio Rao static u_long fuse_lookup_cache_misses = 0;
1945fe58019SAttilio Rao 
195123af6ecSAlan Somers SYSCTL_ULONG(_vfs_fusefs, OID_AUTO, lookup_cache_misses, CTLFLAG_RD,
196bd4cb2a4SConrad Meyer     &fuse_lookup_cache_misses, 0, "number of cache misses in lookup");
1975fe58019SAttilio Rao 
1985fe58019SAttilio Rao /*
1995fe58019SAttilio Rao  * XXX: This feature is highly experimental and can bring to instabilities,
2005fe58019SAttilio Rao  * needs revisiting before to be enabled by default.
2015fe58019SAttilio Rao  */
2025fe58019SAttilio Rao static int fuse_reclaim_revoked = 0;
2035fe58019SAttilio Rao 
204123af6ecSAlan Somers SYSCTL_INT(_vfs_fusefs, OID_AUTO, reclaim_revoked, CTLFLAG_RW,
2055fe58019SAttilio Rao     &fuse_reclaim_revoked, 0, "");
2065fe58019SAttilio Rao 
207756a5412SGleb Smirnoff uma_zone_t fuse_pbuf_zone;
2085fe58019SAttilio Rao 
2095fe58019SAttilio Rao #define fuse_vm_page_lock(m)		vm_page_lock((m));
2105fe58019SAttilio Rao #define fuse_vm_page_unlock(m)		vm_page_unlock((m));
2115fe58019SAttilio Rao #define fuse_vm_page_lock_queues()	((void)0)
2125fe58019SAttilio Rao #define fuse_vm_page_unlock_queues()	((void)0)
2135fe58019SAttilio Rao 
214f8d4af10SAlan Somers /* Get a filehandle for a directory */
215f8d4af10SAlan Somers static int
216f8d4af10SAlan Somers fuse_filehandle_get_dir(struct vnode *vp, struct fuse_filehandle **fufhp,
217f8d4af10SAlan Somers 	struct ucred *cred, pid_t pid)
218f8d4af10SAlan Somers {
2199f10f423SAlan Somers 	if (fuse_filehandle_get(vp, FREAD, fufhp, cred, pid) == 0)
220f8d4af10SAlan Somers 		return 0;
2219f10f423SAlan Somers 	return fuse_filehandle_get(vp, FEXEC, fufhp, cred, pid);
2229f10f423SAlan Somers }
2239f10f423SAlan Somers 
2249f10f423SAlan Somers /* Send FUSE_FLUSH for this vnode */
2259f10f423SAlan Somers static int
2269f10f423SAlan Somers fuse_flush(struct vnode *vp, struct ucred *cred, pid_t pid, int fflag)
2279f10f423SAlan Somers {
2289f10f423SAlan Somers 	struct fuse_flush_in *ffi;
2299f10f423SAlan Somers 	struct fuse_filehandle *fufh;
2309f10f423SAlan Somers 	struct fuse_dispatcher fdi;
2319f10f423SAlan Somers 	struct thread *td = curthread;
2329f10f423SAlan Somers 	struct mount *mp = vnode_mount(vp);
2339f10f423SAlan Somers 	int err;
2349f10f423SAlan Somers 
2359f10f423SAlan Somers 	if (!fsess_isimpl(vnode_mount(vp), FUSE_FLUSH))
2369f10f423SAlan Somers 		return 0;
2379f10f423SAlan Somers 
2389f10f423SAlan Somers 	err = fuse_filehandle_get(vp, fflag, &fufh, cred, pid);
2399f10f423SAlan Somers 	if (err)
2409f10f423SAlan Somers 		return err;
2419f10f423SAlan Somers 
2429f10f423SAlan Somers 	fdisp_init(&fdi, sizeof(*ffi));
2439f10f423SAlan Somers 	fdisp_make_vp(&fdi, FUSE_FLUSH, vp, td, cred);
2449f10f423SAlan Somers 	ffi = fdi.indata;
2459f10f423SAlan Somers 	ffi->fh = fufh->fh_id;
2469f10f423SAlan Somers 
2479f10f423SAlan Somers 	err = fdisp_wait_answ(&fdi);
2489f10f423SAlan Somers 	if (err == ENOSYS) {
2499f10f423SAlan Somers 		fsess_set_notimpl(mp, FUSE_FLUSH);
2509f10f423SAlan Somers 		err = 0;
2519f10f423SAlan Somers 	}
2529f10f423SAlan Somers 	fdisp_destroy(&fdi);
2539f10f423SAlan Somers 	return err;
254f8d4af10SAlan Somers }
255f8d4af10SAlan Somers 
2565fe58019SAttilio Rao /*
2575fe58019SAttilio Rao     struct vnop_access_args {
2585fe58019SAttilio Rao 	struct vnode *a_vp;
2595fe58019SAttilio Rao #if VOP_ACCESS_TAKES_ACCMODE_T
2605fe58019SAttilio Rao 	accmode_t a_accmode;
2615fe58019SAttilio Rao #else
2625fe58019SAttilio Rao 	int a_mode;
2635fe58019SAttilio Rao #endif
2645fe58019SAttilio Rao 	struct ucred *a_cred;
2655fe58019SAttilio Rao 	struct thread *a_td;
2665fe58019SAttilio Rao     };
2675fe58019SAttilio Rao */
2685fe58019SAttilio Rao static int
2695fe58019SAttilio Rao fuse_vnop_access(struct vop_access_args *ap)
2705fe58019SAttilio Rao {
2715fe58019SAttilio Rao 	struct vnode *vp = ap->a_vp;
2725fe58019SAttilio Rao 	int accmode = ap->a_accmode;
2735fe58019SAttilio Rao 	struct ucred *cred = ap->a_cred;
2745fe58019SAttilio Rao 
2755fe58019SAttilio Rao 	struct fuse_access_param facp;
2765fe58019SAttilio Rao 	struct fuse_data *data = fuse_get_mpdata(vnode_mount(vp));
2775fe58019SAttilio Rao 
2785fe58019SAttilio Rao 	int err;
2795fe58019SAttilio Rao 
2805fe58019SAttilio Rao 	if (fuse_isdeadfs(vp)) {
2815fe58019SAttilio Rao 		if (vnode_isvroot(vp)) {
2825fe58019SAttilio Rao 			return 0;
2835fe58019SAttilio Rao 		}
2845fe58019SAttilio Rao 		return ENXIO;
2855fe58019SAttilio Rao 	}
2865fe58019SAttilio Rao 	if (!(data->dataflags & FSESS_INITED)) {
2875fe58019SAttilio Rao 		if (vnode_isvroot(vp)) {
288cc426dd3SMateusz Guzik 			if (priv_check_cred(cred, PRIV_VFS_ADMIN) ||
2895fe58019SAttilio Rao 			    (fuse_match_cred(data->daemoncred, cred) == 0)) {
2905fe58019SAttilio Rao 				return 0;
2915fe58019SAttilio Rao 			}
2925fe58019SAttilio Rao 		}
2935fe58019SAttilio Rao 		return EBADF;
2945fe58019SAttilio Rao 	}
2955fe58019SAttilio Rao 	if (vnode_islnk(vp)) {
2965fe58019SAttilio Rao 		return 0;
2975fe58019SAttilio Rao 	}
2985fe58019SAttilio Rao 	bzero(&facp, sizeof(facp));
2995fe58019SAttilio Rao 
3005fe58019SAttilio Rao 	err = fuse_internal_access(vp, accmode, &facp, ap->a_td, ap->a_cred);
3015fe58019SAttilio Rao 	return err;
3025fe58019SAttilio Rao }
3035fe58019SAttilio Rao 
3045fe58019SAttilio Rao /*
3059f10f423SAlan Somers     struct vop_close_args {
3065fe58019SAttilio Rao 	struct vnode *a_vp;
3075fe58019SAttilio Rao 	int  a_fflag;
3085fe58019SAttilio Rao 	struct ucred *a_cred;
3095fe58019SAttilio Rao 	struct thread *a_td;
3105fe58019SAttilio Rao     };
3115fe58019SAttilio Rao */
3125fe58019SAttilio Rao static int
3135fe58019SAttilio Rao fuse_vnop_close(struct vop_close_args *ap)
3145fe58019SAttilio Rao {
3155fe58019SAttilio Rao 	struct vnode *vp = ap->a_vp;
3165fe58019SAttilio Rao 	struct ucred *cred = ap->a_cred;
3175fe58019SAttilio Rao 	int fflag = ap->a_fflag;
318f8d4af10SAlan Somers 	struct thread *td = ap->a_td;
319f8d4af10SAlan Somers 	pid_t pid = td->td_proc->p_pid;
3209f10f423SAlan Somers 	int err = 0;
3215fe58019SAttilio Rao 
32235cf0e7eSAlan Somers 	if (fuse_isdeadfs(vp))
3235fe58019SAttilio Rao 		return 0;
32435cf0e7eSAlan Somers 	if (vnode_isdir(vp))
32535cf0e7eSAlan Somers 		return 0;
32635cf0e7eSAlan Somers 	if (fflag & IO_NDELAY)
32735cf0e7eSAlan Somers 		return 0;
3285ec10aa5SAlan Somers 
3299f10f423SAlan Somers 	err = fuse_flush(vp, cred, pid, fflag);
3305ec10aa5SAlan Somers 	/* TODO: close the file handle, if we're sure it's no longer used */
3315fe58019SAttilio Rao 	if ((VTOFUD(vp)->flag & FN_SIZECHANGE) != 0) {
332f8d4af10SAlan Somers 		fuse_vnode_savesize(vp, cred, td->td_proc->p_pid);
3335fe58019SAttilio Rao 	}
3349f10f423SAlan Somers 	return err;
3355fe58019SAttilio Rao }
3365fe58019SAttilio Rao 
33719ef317dSAlan Somers static void
33819ef317dSAlan Somers fdisp_make_mknod_for_fallback(
33919ef317dSAlan Somers 	struct fuse_dispatcher *fdip,
34019ef317dSAlan Somers 	struct componentname *cnp,
34119ef317dSAlan Somers 	struct vnode *dvp,
34219ef317dSAlan Somers 	uint64_t parentnid,
34319ef317dSAlan Somers 	struct thread *td,
34419ef317dSAlan Somers 	struct ucred *cred,
34519ef317dSAlan Somers 	mode_t mode,
34619ef317dSAlan Somers 	enum fuse_opcode *op)
34719ef317dSAlan Somers {
34819ef317dSAlan Somers 	struct fuse_mknod_in *fmni;
34919ef317dSAlan Somers 
35019ef317dSAlan Somers 	fdisp_init(fdip, sizeof(*fmni) + cnp->cn_namelen + 1);
35119ef317dSAlan Somers 	*op = FUSE_MKNOD;
35219ef317dSAlan Somers 	fdisp_make(fdip, *op, vnode_mount(dvp), parentnid, td, cred);
35319ef317dSAlan Somers 	fmni = fdip->indata;
35419ef317dSAlan Somers 	fmni->mode = mode;
35519ef317dSAlan Somers 	fmni->rdev = 0;
35619ef317dSAlan Somers 	memcpy((char *)fdip->indata + sizeof(*fmni), cnp->cn_nameptr,
35719ef317dSAlan Somers 	    cnp->cn_namelen);
35819ef317dSAlan Somers 	((char *)fdip->indata)[sizeof(*fmni) + cnp->cn_namelen] = '\0';
35919ef317dSAlan Somers }
3605fe58019SAttilio Rao /*
3615fe58019SAttilio Rao     struct vnop_create_args {
3625fe58019SAttilio Rao 	struct vnode *a_dvp;
3635fe58019SAttilio Rao 	struct vnode **a_vpp;
3645fe58019SAttilio Rao 	struct componentname *a_cnp;
3655fe58019SAttilio Rao 	struct vattr *a_vap;
3665fe58019SAttilio Rao     };
3675fe58019SAttilio Rao */
3685fe58019SAttilio Rao static int
3695fe58019SAttilio Rao fuse_vnop_create(struct vop_create_args *ap)
3705fe58019SAttilio Rao {
3715fe58019SAttilio Rao 	struct vnode *dvp = ap->a_dvp;
3725fe58019SAttilio Rao 	struct vnode **vpp = ap->a_vpp;
3735fe58019SAttilio Rao 	struct componentname *cnp = ap->a_cnp;
3745fe58019SAttilio Rao 	struct vattr *vap = ap->a_vap;
3755fe58019SAttilio Rao 	struct thread *td = cnp->cn_thread;
3765fe58019SAttilio Rao 	struct ucred *cred = cnp->cn_cred;
3775fe58019SAttilio Rao 
3785fe58019SAttilio Rao 	struct fuse_open_in *foi;
3795fe58019SAttilio Rao 	struct fuse_entry_out *feo;
38019ef317dSAlan Somers 	struct fuse_open_out *foo;
38119ef317dSAlan Somers 	struct fuse_dispatcher fdi, fdi2;
3825fe58019SAttilio Rao 	struct fuse_dispatcher *fdip = &fdi;
38319ef317dSAlan Somers 	struct fuse_dispatcher *fdip2 = NULL;
3845fe58019SAttilio Rao 
3855fe58019SAttilio Rao 	int err;
3865fe58019SAttilio Rao 
3875fe58019SAttilio Rao 	struct mount *mp = vnode_mount(dvp);
3885fe58019SAttilio Rao 	uint64_t parentnid = VTOFUD(dvp)->nid;
3895fe58019SAttilio Rao 	mode_t mode = MAKEIMODE(vap->va_type, vap->va_mode);
39019ef317dSAlan Somers 	enum fuse_opcode op;
3919e444871SAlan Somers 	int flags;
3929e444871SAlan Somers 
3939e444871SAlan Somers 	/*
3949e444871SAlan Somers 	 * VOP_CREATE doesn't tell us the open(2) flags, so we guess.  Only a
3959e444871SAlan Somers 	 * writable mode makes sense, and we might as well include readability
3969e444871SAlan Somers 	 * too.
3979e444871SAlan Somers 	 */
3989e444871SAlan Somers 	flags = O_RDWR;
3995fe58019SAttilio Rao 
4005fe58019SAttilio Rao 	if (fuse_isdeadfs(dvp)) {
4015fe58019SAttilio Rao 		return ENXIO;
4025fe58019SAttilio Rao 	}
4035fe58019SAttilio Rao 	bzero(&fdi, sizeof(fdi));
4045fe58019SAttilio Rao 
40519ef317dSAlan Somers 	if ((vap->va_type != VREG))
406372b97d0SRick Macklem 		return (EINVAL);
4075fe58019SAttilio Rao 
4085fe58019SAttilio Rao 	if (!fsess_isimpl(mp, FUSE_CREATE)) {
40919ef317dSAlan Somers 		/* Fallback to FUSE_MKNOD/FUSE_OPEN */
41019ef317dSAlan Somers 		fdisp_make_mknod_for_fallback(fdip, cnp, dvp, parentnid, td,
41119ef317dSAlan Somers 			cred, mode, &op);
41219ef317dSAlan Somers 	} else {
41319ef317dSAlan Somers 		/* Use FUSE_CREATE */
41419ef317dSAlan Somers 		op = FUSE_CREATE;
41519ef317dSAlan Somers 		fdisp_init(fdip, sizeof(*foi) + cnp->cn_namelen + 1);
41619ef317dSAlan Somers 		fdisp_make(fdip, op, vnode_mount(dvp), parentnid, td, cred);
4175fe58019SAttilio Rao 		foi = fdip->indata;
4185fe58019SAttilio Rao 		foi->mode = mode;
4199e444871SAlan Somers 		foi->flags = O_CREAT | flags;
4205fe58019SAttilio Rao 		memcpy((char *)fdip->indata + sizeof(*foi), cnp->cn_nameptr,
4215fe58019SAttilio Rao 		    cnp->cn_namelen);
4225fe58019SAttilio Rao 		((char *)fdip->indata)[sizeof(*foi) + cnp->cn_namelen] = '\0';
42319ef317dSAlan Somers 	}
4245fe58019SAttilio Rao 
4255fe58019SAttilio Rao 	err = fdisp_wait_answ(fdip);
4265fe58019SAttilio Rao 
4276de3b00dSAttilio Rao 	if (err) {
42819ef317dSAlan Somers 		if (err == ENOSYS && op == FUSE_CREATE) {
4295fe58019SAttilio Rao 			fsess_set_notimpl(mp, FUSE_CREATE);
43019ef317dSAlan Somers 			fdisp_make_mknod_for_fallback(fdip, cnp, dvp,
43119ef317dSAlan Somers 				parentnid, td, cred, mode, &op);
43219ef317dSAlan Somers 			err = fdisp_wait_answ(fdip);
43319ef317dSAlan Somers 		}
43419ef317dSAlan Somers 		if (err)
4355fe58019SAttilio Rao 			goto out;
4365fe58019SAttilio Rao 	}
437372b97d0SRick Macklem 
4385fe58019SAttilio Rao 	feo = fdip->answ;
4395fe58019SAttilio Rao 
4405fe58019SAttilio Rao 	if ((err = fuse_internal_checkentry(feo, VREG))) {
4415fe58019SAttilio Rao 		goto out;
4425fe58019SAttilio Rao 	}
44319ef317dSAlan Somers 
44419ef317dSAlan Somers 	if (op == FUSE_CREATE) {
44519ef317dSAlan Somers 		foo = (struct fuse_open_out*)(feo + 1);
44619ef317dSAlan Somers 	} else {
44719ef317dSAlan Somers 		/* Issue a separate FUSE_OPEN */
44819ef317dSAlan Somers 		fdip2 = &fdi2;
44919ef317dSAlan Somers 		fdisp_init(fdip2, sizeof(*foi));
45019ef317dSAlan Somers 		fdisp_make(fdip2, FUSE_OPEN, vnode_mount(dvp), feo->nodeid, td,
45119ef317dSAlan Somers 			cred);
45219ef317dSAlan Somers 		foi = fdip2->indata;
45319ef317dSAlan Somers 		foi->mode = mode;
4549e444871SAlan Somers 		foi->flags = flags;
45519ef317dSAlan Somers 		err = fdisp_wait_answ(fdip2);
45619ef317dSAlan Somers 		if (err)
45719ef317dSAlan Somers 			goto out;
45819ef317dSAlan Somers 		foo = fdip2->answ;
45919ef317dSAlan Somers 	}
46009176f09SConrad Meyer 	err = fuse_vnode_get(mp, feo, feo->nodeid, dvp, vpp, cnp, VREG);
4615fe58019SAttilio Rao 	if (err) {
4625fe58019SAttilio Rao 		struct fuse_release_in *fri;
4635fe58019SAttilio Rao 		uint64_t nodeid = feo->nodeid;
46419ef317dSAlan Somers 		uint64_t fh_id = foo->fh;
4655fe58019SAttilio Rao 
4665fe58019SAttilio Rao 		fdisp_init(fdip, sizeof(*fri));
4675fe58019SAttilio Rao 		fdisp_make(fdip, FUSE_RELEASE, mp, nodeid, td, cred);
4685fe58019SAttilio Rao 		fri = fdip->indata;
4695fe58019SAttilio Rao 		fri->fh = fh_id;
4709e444871SAlan Somers 		fri->flags = flags;
4715fe58019SAttilio Rao 		fuse_insert_callback(fdip->tick, fuse_internal_forget_callback);
4725fe58019SAttilio Rao 		fuse_insert_message(fdip->tick);
47319ef317dSAlan Somers 		goto out;
4745fe58019SAttilio Rao 	}
4755fe58019SAttilio Rao 	ASSERT_VOP_ELOCKED(*vpp, "fuse_vnop_create");
476cad67791SAlan Somers 	fuse_internal_cache_attrs(*vpp, &feo->attr, feo->attr_valid,
477cad67791SAlan Somers 		feo->attr_valid_nsec, NULL);
4785fe58019SAttilio Rao 
479a7e81cb3SAlan Somers 	fuse_filehandle_init(*vpp, FUFH_RDWR, NULL, td, cred, foo);
48019ef317dSAlan Somers 	fuse_vnode_open(*vpp, foo->open_flags, td);
4815fe58019SAttilio Rao 	cache_purge_negative(dvp);
4825fe58019SAttilio Rao 
4835fe58019SAttilio Rao out:
48419ef317dSAlan Somers 	if (fdip2)
48519ef317dSAlan Somers 		fdisp_destroy(fdip2);
4865fe58019SAttilio Rao 	fdisp_destroy(fdip);
4875fe58019SAttilio Rao 	return err;
4885fe58019SAttilio Rao }
4895fe58019SAttilio Rao 
4905fe58019SAttilio Rao /*
491915012e0SAlan Somers     struct vnop_fdatasync_args {
492915012e0SAlan Somers 	struct vop_generic_args a_gen;
493915012e0SAlan Somers 	struct vnode * a_vp;
494915012e0SAlan Somers 	struct thread * a_td;
495915012e0SAlan Somers     };
4965fe58019SAttilio Rao */
497915012e0SAlan Somers static int
498915012e0SAlan Somers fuse_vnop_fdatasync(struct vop_fdatasync_args *ap)
499915012e0SAlan Somers {
500915012e0SAlan Somers 	struct vnode *vp = ap->a_vp;
501915012e0SAlan Somers 	struct thread *td = ap->a_td;
502915012e0SAlan Somers 	int waitfor = MNT_WAIT;
503915012e0SAlan Somers 
504915012e0SAlan Somers 	int err = 0;
505915012e0SAlan Somers 
506915012e0SAlan Somers 	if (fuse_isdeadfs(vp)) {
507915012e0SAlan Somers 		return 0;
508915012e0SAlan Somers 	}
509915012e0SAlan Somers 	if ((err = vop_stdfdatasync_buf(ap)))
510915012e0SAlan Somers 		return err;
511915012e0SAlan Somers 
512915012e0SAlan Somers 	return fuse_internal_fsync(vp, td, waitfor, true);
513915012e0SAlan Somers }
5145fe58019SAttilio Rao 
5155fe58019SAttilio Rao /*
5165fe58019SAttilio Rao     struct vnop_fsync_args {
517915012e0SAlan Somers 	struct vop_generic_args a_gen;
5185fe58019SAttilio Rao 	struct vnode * a_vp;
5195fe58019SAttilio Rao 	int  a_waitfor;
5205fe58019SAttilio Rao 	struct thread * a_td;
5215fe58019SAttilio Rao     };
5225fe58019SAttilio Rao */
5235fe58019SAttilio Rao static int
5245fe58019SAttilio Rao fuse_vnop_fsync(struct vop_fsync_args *ap)
5255fe58019SAttilio Rao {
5265fe58019SAttilio Rao 	struct vnode *vp = ap->a_vp;
5275fe58019SAttilio Rao 	struct thread *td = ap->a_td;
52890612f3cSAlan Somers 	int waitfor = ap->a_waitfor;
529915012e0SAlan Somers 	int err = 0;
5305fe58019SAttilio Rao 
5315fe58019SAttilio Rao 	if (fuse_isdeadfs(vp)) {
5325fe58019SAttilio Rao 		return 0;
5335fe58019SAttilio Rao 	}
5345fe58019SAttilio Rao 	if ((err = vop_stdfsync(ap)))
5355fe58019SAttilio Rao 		return err;
5365fe58019SAttilio Rao 
537915012e0SAlan Somers 	return fuse_internal_fsync(vp, td, waitfor, false);
5385fe58019SAttilio Rao }
5395fe58019SAttilio Rao 
5405fe58019SAttilio Rao /*
5415fe58019SAttilio Rao     struct vnop_getattr_args {
5425fe58019SAttilio Rao 	struct vnode *a_vp;
5435fe58019SAttilio Rao 	struct vattr *a_vap;
5445fe58019SAttilio Rao 	struct ucred *a_cred;
5455fe58019SAttilio Rao 	struct thread *a_td;
5465fe58019SAttilio Rao     };
5475fe58019SAttilio Rao */
5485fe58019SAttilio Rao static int
5495fe58019SAttilio Rao fuse_vnop_getattr(struct vop_getattr_args *ap)
5505fe58019SAttilio Rao {
5515fe58019SAttilio Rao 	struct vnode *vp = ap->a_vp;
5525fe58019SAttilio Rao 	struct vattr *vap = ap->a_vap;
5535fe58019SAttilio Rao 	struct ucred *cred = ap->a_cred;
5545fe58019SAttilio Rao 	struct thread *td = curthread;
5555fe58019SAttilio Rao 
5565fe58019SAttilio Rao 	int err = 0;
5575fe58019SAttilio Rao 	int dataflags;
5585fe58019SAttilio Rao 
5595fe58019SAttilio Rao 	dataflags = fuse_get_mpdata(vnode_mount(vp))->dataflags;
5605fe58019SAttilio Rao 
5615fe58019SAttilio Rao 	/* Note that we are not bailing out on a dead file system just yet. */
5625fe58019SAttilio Rao 
5635fe58019SAttilio Rao 	if (!(dataflags & FSESS_INITED)) {
5645fe58019SAttilio Rao 		if (!vnode_isvroot(vp)) {
5655fe58019SAttilio Rao 			fdata_set_dead(fuse_get_mpdata(vnode_mount(vp)));
5665fe58019SAttilio Rao 			err = ENOTCONN;
5675fe58019SAttilio Rao 			return err;
5685fe58019SAttilio Rao 		} else {
5695fe58019SAttilio Rao 			goto fake;
5705fe58019SAttilio Rao 		}
5715fe58019SAttilio Rao 	}
572cad67791SAlan Somers 	err = fuse_internal_getattr(vp, vap, cred, td);
573cad67791SAlan Somers 	if (err == ENOTCONN && vnode_isvroot(vp)) {
574cf169498SAlan Somers 		/* see comment in fuse_vfsop_statfs() */
5755fe58019SAttilio Rao 		goto fake;
576cad67791SAlan Somers 	} else {
5775fe58019SAttilio Rao 		return err;
578cad67791SAlan Somers 	}
5795fe58019SAttilio Rao 
5805fe58019SAttilio Rao fake:
5815fe58019SAttilio Rao 	bzero(vap, sizeof(*vap));
5825fe58019SAttilio Rao 	vap->va_type = vnode_vtype(vp);
5835fe58019SAttilio Rao 
5845fe58019SAttilio Rao 	return 0;
5855fe58019SAttilio Rao }
5865fe58019SAttilio Rao 
5875fe58019SAttilio Rao /*
5885fe58019SAttilio Rao     struct vnop_inactive_args {
5895fe58019SAttilio Rao 	struct vnode *a_vp;
5905fe58019SAttilio Rao 	struct thread *a_td;
5915fe58019SAttilio Rao     };
5925fe58019SAttilio Rao */
5935fe58019SAttilio Rao static int
5945fe58019SAttilio Rao fuse_vnop_inactive(struct vop_inactive_args *ap)
5955fe58019SAttilio Rao {
5965fe58019SAttilio Rao 	struct vnode *vp = ap->a_vp;
5975fe58019SAttilio Rao 	struct thread *td = ap->a_td;
5985fe58019SAttilio Rao 
5995fe58019SAttilio Rao 	struct fuse_vnode_data *fvdat = VTOFUD(vp);
6005ec10aa5SAlan Somers 	struct fuse_filehandle *fufh, *fufh_tmp;
6015fe58019SAttilio Rao 
6025ec10aa5SAlan Somers 	int need_flush = 1;
6035fe58019SAttilio Rao 
6045ec10aa5SAlan Somers 	LIST_FOREACH_SAFE(fufh, &fvdat->handles, next, fufh_tmp) {
6055fe58019SAttilio Rao 		if (need_flush && vp->v_type == VREG) {
6065fe58019SAttilio Rao 			if ((VTOFUD(vp)->flag & FN_SIZECHANGE) != 0) {
607f8d4af10SAlan Somers 				fuse_vnode_savesize(vp, NULL, 0);
6085fe58019SAttilio Rao 			}
6095fe58019SAttilio Rao 			if (fuse_data_cache_invalidate ||
6105fe58019SAttilio Rao 			    (fvdat->flag & FN_REVOKED) != 0)
6115fe58019SAttilio Rao 				fuse_io_invalbuf(vp, td);
6125fe58019SAttilio Rao 			else
6135fe58019SAttilio Rao 				fuse_io_flushbuf(vp, MNT_WAIT, td);
6145fe58019SAttilio Rao 			need_flush = 0;
6155fe58019SAttilio Rao 		}
6165ec10aa5SAlan Somers 		fuse_filehandle_close(vp, fufh, td, NULL);
6175fe58019SAttilio Rao 	}
6185fe58019SAttilio Rao 
6195fe58019SAttilio Rao 	if ((fvdat->flag & FN_REVOKED) != 0 && fuse_reclaim_revoked) {
6205fe58019SAttilio Rao 		vrecycle(vp);
6215fe58019SAttilio Rao 	}
6225fe58019SAttilio Rao 	return 0;
6235fe58019SAttilio Rao }
6245fe58019SAttilio Rao 
6255fe58019SAttilio Rao /*
6265fe58019SAttilio Rao     struct vnop_link_args {
6275fe58019SAttilio Rao 	struct vnode *a_tdvp;
6285fe58019SAttilio Rao 	struct vnode *a_vp;
6295fe58019SAttilio Rao 	struct componentname *a_cnp;
6305fe58019SAttilio Rao     };
6315fe58019SAttilio Rao */
6325fe58019SAttilio Rao static int
6335fe58019SAttilio Rao fuse_vnop_link(struct vop_link_args *ap)
6345fe58019SAttilio Rao {
6355fe58019SAttilio Rao 	struct vnode *vp = ap->a_vp;
6365fe58019SAttilio Rao 	struct vnode *tdvp = ap->a_tdvp;
6375fe58019SAttilio Rao 	struct componentname *cnp = ap->a_cnp;
6385fe58019SAttilio Rao 
6395fe58019SAttilio Rao 	struct vattr *vap = VTOVA(vp);
6405fe58019SAttilio Rao 
6415fe58019SAttilio Rao 	struct fuse_dispatcher fdi;
6425fe58019SAttilio Rao 	struct fuse_entry_out *feo;
6435fe58019SAttilio Rao 	struct fuse_link_in fli;
6445fe58019SAttilio Rao 
6455fe58019SAttilio Rao 	int err;
6465fe58019SAttilio Rao 
6475fe58019SAttilio Rao 	if (fuse_isdeadfs(vp)) {
6485fe58019SAttilio Rao 		return ENXIO;
6495fe58019SAttilio Rao 	}
6505fe58019SAttilio Rao 	if (vnode_mount(tdvp) != vnode_mount(vp)) {
6515fe58019SAttilio Rao 		return EXDEV;
6525fe58019SAttilio Rao 	}
65378a7722fSConrad Meyer 
65478a7722fSConrad Meyer 	/*
65578a7722fSConrad Meyer 	 * This is a seatbelt check to protect naive userspace filesystems from
65678a7722fSConrad Meyer 	 * themselves and the limitations of the FUSE IPC protocol.  If a
65778a7722fSConrad Meyer 	 * filesystem does not allow attribute caching, assume it is capable of
65878a7722fSConrad Meyer 	 * validating that nlink does not overflow.
65978a7722fSConrad Meyer 	 */
66078a7722fSConrad Meyer 	if (vap != NULL && vap->va_nlink >= FUSE_LINK_MAX)
6615fe58019SAttilio Rao 		return EMLINK;
6625fe58019SAttilio Rao 	fli.oldnodeid = VTOI(vp);
6635fe58019SAttilio Rao 
6645fe58019SAttilio Rao 	fdisp_init(&fdi, 0);
6655fe58019SAttilio Rao 	fuse_internal_newentry_makerequest(vnode_mount(tdvp), VTOI(tdvp), cnp,
6665fe58019SAttilio Rao 	    FUSE_LINK, &fli, sizeof(fli), &fdi);
6675fe58019SAttilio Rao 	if ((err = fdisp_wait_answ(&fdi))) {
6685fe58019SAttilio Rao 		goto out;
6695fe58019SAttilio Rao 	}
6705fe58019SAttilio Rao 	feo = fdi.answ;
6715fe58019SAttilio Rao 
6725fe58019SAttilio Rao 	err = fuse_internal_checkentry(feo, vnode_vtype(vp));
6735fe58019SAttilio Rao out:
6745fe58019SAttilio Rao 	fdisp_destroy(&fdi);
6755fe58019SAttilio Rao 	return err;
6765fe58019SAttilio Rao }
6775fe58019SAttilio Rao 
678ccb75e49SAlan Somers SDT_PROBE_DEFINE3(fuse, , vnops, cache_lookup,
679ccb75e49SAlan Somers 	"int", "struct timespec*", "struct timespec*");
6805fe58019SAttilio Rao /*
6815fe58019SAttilio Rao     struct vnop_lookup_args {
6825fe58019SAttilio Rao 	struct vnodeop_desc *a_desc;
6835fe58019SAttilio Rao 	struct vnode *a_dvp;
6845fe58019SAttilio Rao 	struct vnode **a_vpp;
6855fe58019SAttilio Rao 	struct componentname *a_cnp;
6865fe58019SAttilio Rao     };
6875fe58019SAttilio Rao */
6885fe58019SAttilio Rao int
6895fe58019SAttilio Rao fuse_vnop_lookup(struct vop_lookup_args *ap)
6905fe58019SAttilio Rao {
6915fe58019SAttilio Rao 	struct vnode *dvp = ap->a_dvp;
6925fe58019SAttilio Rao 	struct vnode **vpp = ap->a_vpp;
6935fe58019SAttilio Rao 	struct componentname *cnp = ap->a_cnp;
6945fe58019SAttilio Rao 	struct thread *td = cnp->cn_thread;
6955fe58019SAttilio Rao 	struct ucred *cred = cnp->cn_cred;
6965fe58019SAttilio Rao 
6975fe58019SAttilio Rao 	int nameiop = cnp->cn_nameiop;
6985fe58019SAttilio Rao 	int flags = cnp->cn_flags;
6995fe58019SAttilio Rao 	int wantparent = flags & (LOCKPARENT | WANTPARENT);
7005fe58019SAttilio Rao 	int islastcn = flags & ISLASTCN;
7015fe58019SAttilio Rao 	struct mount *mp = vnode_mount(dvp);
7025fe58019SAttilio Rao 
7035fe58019SAttilio Rao 	int err = 0;
7045fe58019SAttilio Rao 	int lookup_err = 0;
7055fe58019SAttilio Rao 	struct vnode *vp = NULL;
7065fe58019SAttilio Rao 
7075fe58019SAttilio Rao 	struct fuse_dispatcher fdi;
7085fe58019SAttilio Rao 	enum fuse_opcode op;
70944f10c6eSAlan Somers 	struct fuse_entry_out *feo = NULL;
71044f10c6eSAlan Somers 	struct fuse_attr_out *fao = NULL;
71144f10c6eSAlan Somers 	struct fuse_attr *fattr = NULL;
7125fe58019SAttilio Rao 
7135fe58019SAttilio Rao 	uint64_t nid;
7145fe58019SAttilio Rao 	struct fuse_access_param facp;
7155fe58019SAttilio Rao 
7165fe58019SAttilio Rao 	if (fuse_isdeadfs(dvp)) {
7175fe58019SAttilio Rao 		*vpp = NULL;
7185fe58019SAttilio Rao 		return ENXIO;
7195fe58019SAttilio Rao 	}
7205fe58019SAttilio Rao 	if (!vnode_isdir(dvp)) {
7215fe58019SAttilio Rao 		return ENOTDIR;
7225fe58019SAttilio Rao 	}
7235fe58019SAttilio Rao 	if (islastcn && vfs_isrdonly(mp) && (nameiop != LOOKUP)) {
7245fe58019SAttilio Rao 		return EROFS;
7255fe58019SAttilio Rao 	}
7265fe58019SAttilio Rao 	/*
7275fe58019SAttilio Rao 	 * We do access check prior to doing anything else only in the case
7285fe58019SAttilio Rao 	 * when we are at fs root (we'd like to say, "we are at the first
7295fe58019SAttilio Rao 	 * component", but that's not exactly the same... nevermind).
7305fe58019SAttilio Rao 	 * See further comments at further access checks.
7315fe58019SAttilio Rao 	 */
7325fe58019SAttilio Rao 
7335fe58019SAttilio Rao 	bzero(&facp, sizeof(facp));
7345fe58019SAttilio Rao 	if (vnode_isvroot(dvp)) {	/* early permission check hack */
7355fe58019SAttilio Rao 		if ((err = fuse_internal_access(dvp, VEXEC, &facp, td, cred))) {
7365fe58019SAttilio Rao 			return err;
7375fe58019SAttilio Rao 		}
7385fe58019SAttilio Rao 	}
7395fe58019SAttilio Rao 	if (flags & ISDOTDOT) {
7405fe58019SAttilio Rao 		nid = VTOFUD(dvp)->parent_nid;
7415fe58019SAttilio Rao 		if (nid == 0) {
7425fe58019SAttilio Rao 			return ENOENT;
7435fe58019SAttilio Rao 		}
7445fe58019SAttilio Rao 		fdisp_init(&fdi, 0);
7455fe58019SAttilio Rao 		op = FUSE_GETATTR;
7465fe58019SAttilio Rao 		goto calldaemon;
7475fe58019SAttilio Rao 	} else if (cnp->cn_namelen == 1 && *(cnp->cn_nameptr) == '.') {
7485fe58019SAttilio Rao 		nid = VTOI(dvp);
7495fe58019SAttilio Rao 		fdisp_init(&fdi, 0);
7505fe58019SAttilio Rao 		op = FUSE_GETATTR;
7515fe58019SAttilio Rao 		goto calldaemon;
752ccb75e49SAlan Somers 	} else {
7533f2c630cSAlan Somers 		struct timespec now, timeout;
7543f2c630cSAlan Somers 
7553f2c630cSAlan Somers 		err = cache_lookup(dvp, vpp, cnp, &timeout, NULL);
7563f2c630cSAlan Somers 		getnanouptime(&now);
757ccb75e49SAlan Somers 		SDT_PROBE3(fuse, , vnops, cache_lookup, err, &timeout, &now);
758ccb75e49SAlan Somers 		switch (err) {
759ccb75e49SAlan Somers 		case -1:		/* positive match */
760ccb75e49SAlan Somers 			if (timespeccmp(&timeout, &now, >)) {
7613f2c630cSAlan Somers 				atomic_add_acq_long(&fuse_lookup_cache_hits, 1);
7623f2c630cSAlan Somers 			} else {
7633f2c630cSAlan Somers 				/* Cache timeout */
7643f2c630cSAlan Somers 				atomic_add_acq_long(&fuse_lookup_cache_misses,
7653f2c630cSAlan Somers 					1);
766ccb75e49SAlan Somers 				fuse_internal_vnode_disappear(*vpp);
767ccb75e49SAlan Somers 				if (dvp != *vpp)
768ccb75e49SAlan Somers 					vput(*vpp);
769ccb75e49SAlan Somers 				else
770ccb75e49SAlan Somers 					vrele(*vpp);
771ccb75e49SAlan Somers 				*vpp = NULL;
7723f2c630cSAlan Somers 				break;
7733f2c630cSAlan Somers 			}
7743f2c630cSAlan Somers 			return 0;
7753f2c630cSAlan Somers 
7763f2c630cSAlan Somers 		case 0:		/* no match in cache */
7773f2c630cSAlan Somers 			atomic_add_acq_long(&fuse_lookup_cache_misses, 1);
7783f2c630cSAlan Somers 			break;
7793f2c630cSAlan Somers 
7803f2c630cSAlan Somers 		case ENOENT:		/* negative match */
7813f2c630cSAlan Somers 			getnanouptime(&now);
782ccb75e49SAlan Somers 			if (timespeccmp(&timeout, &now, <=)) {
7833f2c630cSAlan Somers 				/* Cache timeout */
784ccb75e49SAlan Somers 				cache_purge_negative(dvp);
7853f2c630cSAlan Somers 				break;
7863f2c630cSAlan Somers 			}
7873f2c630cSAlan Somers 			/* fall through */
7883f2c630cSAlan Somers 		default:
7893f2c630cSAlan Somers 			return err;
7903f2c630cSAlan Somers 		}
7915fe58019SAttilio Rao 	}
7925fe58019SAttilio Rao 	nid = VTOI(dvp);
7935fe58019SAttilio Rao 	fdisp_init(&fdi, cnp->cn_namelen + 1);
7945fe58019SAttilio Rao 	op = FUSE_LOOKUP;
7955fe58019SAttilio Rao 
7965fe58019SAttilio Rao calldaemon:
7975fe58019SAttilio Rao 	fdisp_make(&fdi, op, mp, nid, td, cred);
7985fe58019SAttilio Rao 
7995fe58019SAttilio Rao 	if (op == FUSE_LOOKUP) {
8005fe58019SAttilio Rao 		memcpy(fdi.indata, cnp->cn_nameptr, cnp->cn_namelen);
8015fe58019SAttilio Rao 		((char *)fdi.indata)[cnp->cn_namelen] = '\0';
8025fe58019SAttilio Rao 	}
8035fe58019SAttilio Rao 	lookup_err = fdisp_wait_answ(&fdi);
8045fe58019SAttilio Rao 
805ccb75e49SAlan Somers 	if ((op == FUSE_LOOKUP) && !lookup_err) {
806ccb75e49SAlan Somers 		/* lookup call succeeded */
8075fe58019SAttilio Rao 		nid = ((struct fuse_entry_out *)fdi.answ)->nodeid;
80844f10c6eSAlan Somers 		if (nid == 0) {
80944f10c6eSAlan Somers 			/* zero nodeid means ENOENT and cache it */
81044f10c6eSAlan Somers 			struct timespec timeout;
81144f10c6eSAlan Somers 
8126248288eSAlan Somers 			fdi.answ_stat = ENOENT;
8135fe58019SAttilio Rao 			lookup_err = ENOENT;
81444f10c6eSAlan Somers 			if (cnp->cn_flags & MAKEENTRY) {
81544f10c6eSAlan Somers 				feo = (struct fuse_entry_out *)fdi.answ;
81644f10c6eSAlan Somers 				fuse_validity_2_timespec(feo, &timeout);
81744f10c6eSAlan Somers 				cache_enter_time(dvp, *vpp, cnp, &timeout,
81844f10c6eSAlan Somers 					NULL);
81944f10c6eSAlan Somers 			}
8205fe58019SAttilio Rao 		} else if (nid == FUSE_ROOT_ID) {
8215fe58019SAttilio Rao 			lookup_err = EINVAL;
8225fe58019SAttilio Rao 		}
8235fe58019SAttilio Rao 	}
8245fe58019SAttilio Rao 	if (lookup_err &&
8255fe58019SAttilio Rao 	    (!fdi.answ_stat || lookup_err != ENOENT || op != FUSE_LOOKUP)) {
8265fe58019SAttilio Rao 		fdisp_destroy(&fdi);
8275fe58019SAttilio Rao 		return lookup_err;
8285fe58019SAttilio Rao 	}
8295fe58019SAttilio Rao 	/* lookup_err, if non-zero, must be ENOENT at this point */
8305fe58019SAttilio Rao 
8315fe58019SAttilio Rao 	if (lookup_err) {
832*ff4fbdf5SAlan Somers 		/* Entry not found */
833*ff4fbdf5SAlan Somers 		if ((nameiop == CREATE || nameiop == RENAME) && islastcn) {
834*ff4fbdf5SAlan Somers 			err = fuse_internal_access(dvp, VWRITE, &facp, td,
835*ff4fbdf5SAlan Somers 				cred);
836*ff4fbdf5SAlan Somers 			if (err)
8375fe58019SAttilio Rao 				goto out;
8385fe58019SAttilio Rao 
8395fe58019SAttilio Rao 			/*
8405fe58019SAttilio Rao 	                 * Possibly record the position of a slot in the
8415fe58019SAttilio Rao 	                 * directory large enough for the new component name.
8425fe58019SAttilio Rao 	                 * This can be recorded in the vnode private data for
8435fe58019SAttilio Rao 	                 * dvp. Set the SAVENAME flag to hold onto the
8445fe58019SAttilio Rao 	                 * pathname for use later in VOP_CREATE or VOP_RENAME.
8455fe58019SAttilio Rao 	                 */
8465fe58019SAttilio Rao 			cnp->cn_flags |= SAVENAME;
8475fe58019SAttilio Rao 
8485fe58019SAttilio Rao 			err = EJUSTRETURN;
8495fe58019SAttilio Rao 			goto out;
8505fe58019SAttilio Rao 		}
8515fe58019SAttilio Rao 
8525fe58019SAttilio Rao 		err = ENOENT;
8535fe58019SAttilio Rao 		goto out;
8545fe58019SAttilio Rao 
8555fe58019SAttilio Rao 	} else {
856*ff4fbdf5SAlan Somers 		/* Entry was found */
8575fe58019SAttilio Rao 		if (op == FUSE_GETATTR) {
8585fe58019SAttilio Rao 			fattr = &((struct fuse_attr_out *)fdi.answ)->attr;
8595fe58019SAttilio Rao 		} else {
8605fe58019SAttilio Rao 			feo = (struct fuse_entry_out *)fdi.answ;
8615fe58019SAttilio Rao 			fattr = &(feo->attr);
8625fe58019SAttilio Rao 		}
8635fe58019SAttilio Rao 
864*ff4fbdf5SAlan Somers 		/* TODO: check for ENOTDIR */
865*ff4fbdf5SAlan Somers 
866*ff4fbdf5SAlan Somers 		if ((nameiop == DELETE || nameiop == RENAME) && islastcn) {
867*ff4fbdf5SAlan Somers 			err = fuse_internal_access(dvp, VWRITE, &facp,
868*ff4fbdf5SAlan Somers 				td, cred);
869*ff4fbdf5SAlan Somers 			if (err != 0)
870*ff4fbdf5SAlan Somers 				goto out;
871*ff4fbdf5SAlan Somers 			/*
872*ff4fbdf5SAlan Somers 			 * TODO: if the parent's sticky bit is set, check
873*ff4fbdf5SAlan Somers 			 * whether we're allowed to remove the file.
874*ff4fbdf5SAlan Somers 			 * Need to figure out the vnode locking to make this
875*ff4fbdf5SAlan Somers 			 * work.
876*ff4fbdf5SAlan Somers 			 */
877*ff4fbdf5SAlan Somers #if defined(NOT_YET)
878*ff4fbdf5SAlan Somers 			struct vattr dvattr;
879*ff4fbdf5SAlan Somers 			fuse_internal_getattr(dvp, &dvattr, cred, td);
880*ff4fbdf5SAlan Somers 			if ((dvattr.va_mode & S_ISTXT) &&
881*ff4fbdf5SAlan Somers 				fuse_internal_access(dvp, VADMIN, &facp,
882*ff4fbdf5SAlan Somers 					cnp->cn_thread, cnp->cn_cred) &&
883*ff4fbdf5SAlan Somers 				fuse_internal_access(*vpp, VADMIN, &facp,
884*ff4fbdf5SAlan Somers 					cnp->cn_thread, cnp->cn_cred)) {
885*ff4fbdf5SAlan Somers 				err = EPERM;
886*ff4fbdf5SAlan Somers 				goto out;
887*ff4fbdf5SAlan Somers 			}
888*ff4fbdf5SAlan Somers #endif
889*ff4fbdf5SAlan Somers 		}
890*ff4fbdf5SAlan Somers 
8915fe58019SAttilio Rao 		/*
8925fe58019SAttilio Rao 	         * If deleting, and at end of pathname, return parameters
8935fe58019SAttilio Rao 	         * which can be used to remove file.  If the wantparent flag
8945fe58019SAttilio Rao 	         * isn't set, we return only the directory, otherwise we go on
8955fe58019SAttilio Rao 	         * and lock the inode, being careful with ".".
8965fe58019SAttilio Rao 	         */
8975fe58019SAttilio Rao 		if (nameiop == DELETE && islastcn) {
8985fe58019SAttilio Rao 			if (nid == VTOI(dvp)) {
8995fe58019SAttilio Rao 				vref(dvp);
9005fe58019SAttilio Rao 				*vpp = dvp;
9015fe58019SAttilio Rao 			} else {
90209176f09SConrad Meyer 				err = fuse_vnode_get(dvp->v_mount, feo, nid,
90309176f09SConrad Meyer 				    dvp, &vp, cnp, IFTOVT(fattr->mode));
9045fe58019SAttilio Rao 				if (err)
9055fe58019SAttilio Rao 					goto out;
9065fe58019SAttilio Rao 				*vpp = vp;
9075fe58019SAttilio Rao 			}
9085fe58019SAttilio Rao 
9095fe58019SAttilio Rao 			/*
9105fe58019SAttilio Rao 			 * Save the name for use in VOP_RMDIR and VOP_REMOVE
9115fe58019SAttilio Rao 			 * later.
9125fe58019SAttilio Rao 			 */
9135fe58019SAttilio Rao 			cnp->cn_flags |= SAVENAME;
9145fe58019SAttilio Rao 			goto out;
9155fe58019SAttilio Rao 
9165fe58019SAttilio Rao 		}
9175fe58019SAttilio Rao 		/*
9185fe58019SAttilio Rao 	         * If rewriting (RENAME), return the inode and the
9195fe58019SAttilio Rao 	         * information required to rewrite the present directory
9205fe58019SAttilio Rao 	         * Must get inode of directory entry to verify it's a
9215fe58019SAttilio Rao 	         * regular file, or empty directory.
9225fe58019SAttilio Rao 	         */
9235fe58019SAttilio Rao 		if (nameiop == RENAME && wantparent && islastcn) {
9245fe58019SAttilio Rao 			/*
9255fe58019SAttilio Rao 	                 * Check for "."
9265fe58019SAttilio Rao 	                 */
9275fe58019SAttilio Rao 			if (nid == VTOI(dvp)) {
9285fe58019SAttilio Rao 				err = EISDIR;
9295fe58019SAttilio Rao 				goto out;
9305fe58019SAttilio Rao 			}
93109176f09SConrad Meyer 			err = fuse_vnode_get(vnode_mount(dvp), feo, nid, dvp,
93209176f09SConrad Meyer 			    &vp, cnp, IFTOVT(fattr->mode));
9335fe58019SAttilio Rao 			if (err) {
9345fe58019SAttilio Rao 				goto out;
9355fe58019SAttilio Rao 			}
936*ff4fbdf5SAlan Somers 			/*err = fuse_lookup_check(dvp, vp, &facp, td, cred,*/
937*ff4fbdf5SAlan Somers 				/*nameiop, islastcn);*/
938*ff4fbdf5SAlan Somers 			/*if (err)*/
939*ff4fbdf5SAlan Somers 				/*goto out;*/
9405fe58019SAttilio Rao 			*vpp = vp;
9415fe58019SAttilio Rao 			/*
9425fe58019SAttilio Rao 	                 * Save the name for use in VOP_RENAME later.
9435fe58019SAttilio Rao 	                 */
9445fe58019SAttilio Rao 			cnp->cn_flags |= SAVENAME;
9455fe58019SAttilio Rao 
9465fe58019SAttilio Rao 			goto out;
9475fe58019SAttilio Rao 		}
9485fe58019SAttilio Rao 		if (flags & ISDOTDOT) {
9495fe58019SAttilio Rao 			struct mount *mp;
9505fe58019SAttilio Rao 			int ltype;
9515fe58019SAttilio Rao 
9525fe58019SAttilio Rao 			/*
9535fe58019SAttilio Rao 			 * Expanded copy of vn_vget_ino() so that
9545fe58019SAttilio Rao 			 * fuse_vnode_get() can be used.
9555fe58019SAttilio Rao 			 */
9565fe58019SAttilio Rao 			mp = dvp->v_mount;
9575fe58019SAttilio Rao 			ltype = VOP_ISLOCKED(dvp);
9585fe58019SAttilio Rao 			err = vfs_busy(mp, MBF_NOWAIT);
9595fe58019SAttilio Rao 			if (err != 0) {
9605fe58019SAttilio Rao 				vfs_ref(mp);
9615fe58019SAttilio Rao 				VOP_UNLOCK(dvp, 0);
9625fe58019SAttilio Rao 				err = vfs_busy(mp, 0);
9635fe58019SAttilio Rao 				vn_lock(dvp, ltype | LK_RETRY);
9645fe58019SAttilio Rao 				vfs_rel(mp);
9655fe58019SAttilio Rao 				if (err)
9665fe58019SAttilio Rao 					goto out;
9675fe58019SAttilio Rao 				if ((dvp->v_iflag & VI_DOOMED) != 0) {
9685fe58019SAttilio Rao 					err = ENOENT;
9695fe58019SAttilio Rao 					vfs_unbusy(mp);
9705fe58019SAttilio Rao 					goto out;
9715fe58019SAttilio Rao 				}
9725fe58019SAttilio Rao 			}
9735fe58019SAttilio Rao 			VOP_UNLOCK(dvp, 0);
97409176f09SConrad Meyer 			err = fuse_vnode_get(vnode_mount(dvp), feo, nid, NULL,
97509176f09SConrad Meyer 			    &vp, cnp, IFTOVT(fattr->mode));
9765fe58019SAttilio Rao 			vfs_unbusy(mp);
9775fe58019SAttilio Rao 			vn_lock(dvp, ltype | LK_RETRY);
9785fe58019SAttilio Rao 			if ((dvp->v_iflag & VI_DOOMED) != 0) {
9795fe58019SAttilio Rao 				if (err == 0)
9805fe58019SAttilio Rao 					vput(vp);
9815fe58019SAttilio Rao 				err = ENOENT;
9825fe58019SAttilio Rao 			}
9835fe58019SAttilio Rao 			if (err)
9845fe58019SAttilio Rao 				goto out;
9855fe58019SAttilio Rao 			*vpp = vp;
9865fe58019SAttilio Rao 		} else if (nid == VTOI(dvp)) {
9875fe58019SAttilio Rao 			vref(dvp);
9885fe58019SAttilio Rao 			*vpp = dvp;
9895fe58019SAttilio Rao 		} else {
9903c324b94SConrad Meyer 			struct fuse_vnode_data *fvdat;
9913c324b94SConrad Meyer 
99209176f09SConrad Meyer 			err = fuse_vnode_get(vnode_mount(dvp), feo, nid, dvp,
99309176f09SConrad Meyer 			    &vp, cnp, IFTOVT(fattr->mode));
9945fe58019SAttilio Rao 			if (err) {
9955fe58019SAttilio Rao 				goto out;
9965fe58019SAttilio Rao 			}
9975fe58019SAttilio Rao 			fuse_vnode_setparent(vp, dvp);
9983c324b94SConrad Meyer 
9993c324b94SConrad Meyer 			/*
10003c324b94SConrad Meyer 			 * In the case where we are looking up a FUSE node
10013c324b94SConrad Meyer 			 * represented by an existing cached vnode, and the
10023c324b94SConrad Meyer 			 * true size reported by FUSE_LOOKUP doesn't match
10033c324b94SConrad Meyer 			 * the vnode's cached size, fix the vnode cache to
10043c324b94SConrad Meyer 			 * match the real object size.
10053c324b94SConrad Meyer 			 *
10063c324b94SConrad Meyer 			 * This can occur via FUSE distributed filesystems,
10073c324b94SConrad Meyer 			 * irregular files, etc.
10083c324b94SConrad Meyer 			 */
10093c324b94SConrad Meyer 			fvdat = VTOFUD(vp);
10103c324b94SConrad Meyer 			if (vnode_isreg(vp) &&
10113c324b94SConrad Meyer 			    fattr->size != fvdat->filesize) {
10123c324b94SConrad Meyer 				/*
10133c324b94SConrad Meyer 				 * The FN_SIZECHANGE flag reflects a dirty
10143c324b94SConrad Meyer 				 * append.  If userspace lets us know our cache
10153c324b94SConrad Meyer 				 * is invalid, that write was lost.  (Dirty
10163c324b94SConrad Meyer 				 * writes that do not cause append are also
10173c324b94SConrad Meyer 				 * lost, but we don't detect them here.)
10183c324b94SConrad Meyer 				 *
10193c324b94SConrad Meyer 				 * XXX: Maybe disable WB caching on this mount.
10203c324b94SConrad Meyer 				 */
10213c324b94SConrad Meyer 				if (fvdat->flag & FN_SIZECHANGE)
10223c324b94SConrad Meyer 					printf("%s: WB cache incoherent on "
10233c324b94SConrad Meyer 					    "%s!\n", __func__,
10243c324b94SConrad Meyer 					    vnode_mount(vp)->mnt_stat.f_mntonname);
10253c324b94SConrad Meyer 
10263c324b94SConrad Meyer 				(void)fuse_vnode_setsize(vp, cred, fattr->size);
10273c324b94SConrad Meyer 				fvdat->flag &= ~FN_SIZECHANGE;
10283c324b94SConrad Meyer 			}
10295fe58019SAttilio Rao 			*vpp = vp;
10305fe58019SAttilio Rao 		}
10315fe58019SAttilio Rao 
10325fe58019SAttilio Rao 		if (op == FUSE_GETATTR) {
103344f10c6eSAlan Somers 			fao = (struct fuse_attr_out*)fdi.answ;
10347e4844f7SAlan Somers 			fuse_internal_cache_attrs(*vpp,
10357e4844f7SAlan Somers 				&fao->attr, fao->attr_valid,
10367e4844f7SAlan Somers 				fao->attr_valid_nsec, NULL);
10375fe58019SAttilio Rao 		} else {
10387e4844f7SAlan Somers 			fuse_internal_cache_attrs(*vpp,
10397e4844f7SAlan Somers 				&feo->attr, feo->attr_valid,
10407e4844f7SAlan Somers 				feo->attr_valid_nsec, NULL);
10415fe58019SAttilio Rao 		}
10425fe58019SAttilio Rao 	}
10435fe58019SAttilio Rao out:
10445fe58019SAttilio Rao 	if (!lookup_err) {
10455fe58019SAttilio Rao 
10465fe58019SAttilio Rao 		/* No lookup error; need to clean up. */
10475fe58019SAttilio Rao 
10485fe58019SAttilio Rao 		if (err) {		/* Found inode; exit with no vnode. */
10495fe58019SAttilio Rao 			if (op == FUSE_LOOKUP) {
10505fe58019SAttilio Rao 				fuse_internal_forget_send(vnode_mount(dvp), td, cred,
10515fe58019SAttilio Rao 				    nid, 1);
10525fe58019SAttilio Rao 			}
10535fe58019SAttilio Rao 			fdisp_destroy(&fdi);
10545fe58019SAttilio Rao 			return err;
10555fe58019SAttilio Rao 		} else {
10565fe58019SAttilio Rao #ifndef NO_EARLY_PERM_CHECK_HACK
10575fe58019SAttilio Rao 			if (!islastcn) {
10585fe58019SAttilio Rao 				/*
10595fe58019SAttilio Rao 				 * We have the attributes of the next item
10605fe58019SAttilio Rao 				 * *now*, and it's a fact, and we do not
10615fe58019SAttilio Rao 				 * have to do extra work for it (ie, beg the
10625fe58019SAttilio Rao 				 * daemon), and it neither depends on such
10635fe58019SAttilio Rao 				 * accidental things like attr caching. So
10645fe58019SAttilio Rao 				 * the big idea: check credentials *now*,
10655fe58019SAttilio Rao 				 * not at the beginning of the next call to
10665fe58019SAttilio Rao 				 * lookup.
10675fe58019SAttilio Rao 				 *
10685fe58019SAttilio Rao 				 * The first item of the lookup chain (fs root)
10695fe58019SAttilio Rao 				 * won't be checked then here, of course, as
10705fe58019SAttilio Rao 				 * its never "the next". But go and see that
10715fe58019SAttilio Rao 				 * the root is taken care about at the very
10725fe58019SAttilio Rao 				 * beginning of this function.
10735fe58019SAttilio Rao 				 *
10745fe58019SAttilio Rao 				 * Now, given we want to do the access check
10755fe58019SAttilio Rao 				 * this way, one might ask: so then why not
10765fe58019SAttilio Rao 				 * do the access check just after fetching
10775fe58019SAttilio Rao 				 * the inode and its attributes from the
10785fe58019SAttilio Rao 				 * daemon? Why bother with producing the
10795fe58019SAttilio Rao 				 * corresponding vnode at all if something
10805fe58019SAttilio Rao 				 * is not OK? We know what's the deal as
10815fe58019SAttilio Rao 				 * soon as we get those attrs... There is
10825fe58019SAttilio Rao 				 * one bit of info though not given us by
10835fe58019SAttilio Rao 				 * the daemon: whether his response is
1084b3a15dddSPedro F. Giffuni 				 * authoritative or not... His response should
10855fe58019SAttilio Rao 				 * be ignored if something is mounted over
10865fe58019SAttilio Rao 				 * the dir in question. But that can be
10875fe58019SAttilio Rao 				 * known only by having the vnode...
10885fe58019SAttilio Rao 				 */
10895fe58019SAttilio Rao 				int tmpvtype = vnode_vtype(*vpp);
10905fe58019SAttilio Rao 
10915fe58019SAttilio Rao 				bzero(&facp, sizeof(facp));
10925fe58019SAttilio Rao 				/*the early perm check hack */
10935fe58019SAttilio Rao 				    facp.facc_flags |= FACCESS_VA_VALID;
10945fe58019SAttilio Rao 
10955fe58019SAttilio Rao 				if ((tmpvtype != VDIR) && (tmpvtype != VLNK)) {
10965fe58019SAttilio Rao 					err = ENOTDIR;
10975fe58019SAttilio Rao 				}
10985fe58019SAttilio Rao 				if (!err && !vnode_mountedhere(*vpp)) {
10995fe58019SAttilio Rao 					err = fuse_internal_access(*vpp, VEXEC, &facp, td, cred);
11005fe58019SAttilio Rao 				}
11015fe58019SAttilio Rao 				if (err) {
11025fe58019SAttilio Rao 					if (tmpvtype == VLNK)
1103cf169498SAlan Somers 						SDT_PROBE2(fuse, , vnops, trace,
1104cf169498SAlan Somers 						    1, "weird, permission "
1105cf169498SAlan Somers 						    "error with a symlink?");
11065fe58019SAttilio Rao 					vput(*vpp);
11075fe58019SAttilio Rao 					*vpp = NULL;
11085fe58019SAttilio Rao 				}
11095fe58019SAttilio Rao 			}
11105fe58019SAttilio Rao #endif
11115fe58019SAttilio Rao 		}
11125fe58019SAttilio Rao 	}
11135fe58019SAttilio Rao 	fdisp_destroy(&fdi);
11145fe58019SAttilio Rao 
11155fe58019SAttilio Rao 	return err;
11165fe58019SAttilio Rao }
11175fe58019SAttilio Rao 
11185fe58019SAttilio Rao /*
11195fe58019SAttilio Rao     struct vnop_mkdir_args {
11205fe58019SAttilio Rao 	struct vnode *a_dvp;
11215fe58019SAttilio Rao 	struct vnode **a_vpp;
11225fe58019SAttilio Rao 	struct componentname *a_cnp;
11235fe58019SAttilio Rao 	struct vattr *a_vap;
11245fe58019SAttilio Rao     };
11255fe58019SAttilio Rao */
11265fe58019SAttilio Rao static int
11275fe58019SAttilio Rao fuse_vnop_mkdir(struct vop_mkdir_args *ap)
11285fe58019SAttilio Rao {
11295fe58019SAttilio Rao 	struct vnode *dvp = ap->a_dvp;
11305fe58019SAttilio Rao 	struct vnode **vpp = ap->a_vpp;
11315fe58019SAttilio Rao 	struct componentname *cnp = ap->a_cnp;
11325fe58019SAttilio Rao 	struct vattr *vap = ap->a_vap;
11335fe58019SAttilio Rao 
11345fe58019SAttilio Rao 	struct fuse_mkdir_in fmdi;
11355fe58019SAttilio Rao 
11365fe58019SAttilio Rao 	if (fuse_isdeadfs(dvp)) {
11375fe58019SAttilio Rao 		return ENXIO;
11385fe58019SAttilio Rao 	}
11395fe58019SAttilio Rao 	fmdi.mode = MAKEIMODE(vap->va_type, vap->va_mode);
11405fe58019SAttilio Rao 
1141d9454fabSAttilio Rao 	return (fuse_internal_newentry(dvp, vpp, cnp, FUSE_MKDIR, &fmdi,
1142d9454fabSAttilio Rao 	    sizeof(fmdi), VDIR));
11435fe58019SAttilio Rao }
11445fe58019SAttilio Rao 
11455fe58019SAttilio Rao /*
11465fe58019SAttilio Rao     struct vnop_mknod_args {
11475fe58019SAttilio Rao 	struct vnode *a_dvp;
11485fe58019SAttilio Rao 	struct vnode **a_vpp;
11495fe58019SAttilio Rao 	struct componentname *a_cnp;
11505fe58019SAttilio Rao 	struct vattr *a_vap;
11515fe58019SAttilio Rao     };
11525fe58019SAttilio Rao */
11535fe58019SAttilio Rao static int
11545fe58019SAttilio Rao fuse_vnop_mknod(struct vop_mknod_args *ap)
11555fe58019SAttilio Rao {
11565fe58019SAttilio Rao 
1157bf4d7084SAlan Somers 	struct vnode *dvp = ap->a_dvp;
1158bf4d7084SAlan Somers 	struct vnode **vpp = ap->a_vpp;
1159bf4d7084SAlan Somers 	struct componentname *cnp = ap->a_cnp;
1160bf4d7084SAlan Somers 	struct vattr *vap = ap->a_vap;
1161bf4d7084SAlan Somers 	struct fuse_mknod_in fmni;
11625fe58019SAttilio Rao 
1163bf4d7084SAlan Somers 	if (fuse_isdeadfs(dvp))
1164bf4d7084SAlan Somers 		return ENXIO;
1165bf4d7084SAlan Somers 
1166bf4d7084SAlan Somers 	fmni.mode = MAKEIMODE(vap->va_type, vap->va_mode);
1167bf4d7084SAlan Somers 	fmni.rdev = vap->va_rdev;
1168bf4d7084SAlan Somers 	return (fuse_internal_newentry(dvp, vpp, cnp, FUSE_MKNOD, &fmni,
1169bf4d7084SAlan Somers 	    sizeof(fmni), vap->va_type));
1170bf4d7084SAlan Somers }
11715fe58019SAttilio Rao 
11725fe58019SAttilio Rao /*
11735fe58019SAttilio Rao     struct vnop_open_args {
11745fe58019SAttilio Rao 	struct vnode *a_vp;
11755fe58019SAttilio Rao 	int  a_mode;
11765fe58019SAttilio Rao 	struct ucred *a_cred;
11775fe58019SAttilio Rao 	struct thread *a_td;
11785fe58019SAttilio Rao 	int a_fdidx; / struct file *a_fp;
11795fe58019SAttilio Rao     };
11805fe58019SAttilio Rao */
11815fe58019SAttilio Rao static int
11825fe58019SAttilio Rao fuse_vnop_open(struct vop_open_args *ap)
11835fe58019SAttilio Rao {
11845fe58019SAttilio Rao 	struct vnode *vp = ap->a_vp;
11859e444871SAlan Somers 	int a_mode = ap->a_mode;
11865fe58019SAttilio Rao 	struct thread *td = ap->a_td;
11875fe58019SAttilio Rao 	struct ucred *cred = ap->a_cred;
1188f8d4af10SAlan Somers 	pid_t pid = td->td_proc->p_pid;
11895fe58019SAttilio Rao 	struct fuse_vnode_data *fvdat;
11905fe58019SAttilio Rao 
1191bf4d7084SAlan Somers 	if (fuse_isdeadfs(vp))
11925fe58019SAttilio Rao 		return ENXIO;
1193bf4d7084SAlan Somers 	if (vp->v_type == VCHR || vp->v_type == VBLK || vp->v_type == VFIFO)
1194bf4d7084SAlan Somers 		return (EOPNOTSUPP);
11959e444871SAlan Somers 	if ((a_mode & (FREAD | FWRITE | FEXEC)) == 0)
1196e7df9886SConrad Meyer 		return EINVAL;
1197e7df9886SConrad Meyer 
11985fe58019SAttilio Rao 	fvdat = VTOFUD(vp);
11995fe58019SAttilio Rao 
12009e444871SAlan Somers 	if (fuse_filehandle_validrw(vp, a_mode, cred, pid)) {
12015fccbf31SAlan Somers 		fuse_vnode_open(vp, 0, td);
12025fe58019SAttilio Rao 		return 0;
12035fe58019SAttilio Rao 	}
12045fe58019SAttilio Rao 
12059e444871SAlan Somers 	return fuse_filehandle_open(vp, a_mode, NULL, td, cred);
12065fe58019SAttilio Rao }
12075fe58019SAttilio Rao 
1208746c92e0SJohn Baldwin static int
1209746c92e0SJohn Baldwin fuse_vnop_pathconf(struct vop_pathconf_args *ap)
1210746c92e0SJohn Baldwin {
1211746c92e0SJohn Baldwin 
1212746c92e0SJohn Baldwin 	switch (ap->a_name) {
1213746c92e0SJohn Baldwin 	case _PC_FILESIZEBITS:
1214746c92e0SJohn Baldwin 		*ap->a_retval = 64;
1215746c92e0SJohn Baldwin 		return (0);
1216599afe53SJohn Baldwin 	case _PC_NAME_MAX:
1217599afe53SJohn Baldwin 		*ap->a_retval = NAME_MAX;
1218599afe53SJohn Baldwin 		return (0);
1219599afe53SJohn Baldwin 	case _PC_LINK_MAX:
1220f83f3d79SJohn Baldwin 		*ap->a_retval = MIN(LONG_MAX, FUSE_LINK_MAX);
1221599afe53SJohn Baldwin 		return (0);
1222746c92e0SJohn Baldwin 	case _PC_SYMLINK_MAX:
1223746c92e0SJohn Baldwin 		*ap->a_retval = MAXPATHLEN;
1224746c92e0SJohn Baldwin 		return (0);
1225746c92e0SJohn Baldwin 	case _PC_NO_TRUNC:
1226746c92e0SJohn Baldwin 		*ap->a_retval = 1;
1227746c92e0SJohn Baldwin 		return (0);
1228746c92e0SJohn Baldwin 	default:
1229746c92e0SJohn Baldwin 		return (vop_stdpathconf(ap));
1230746c92e0SJohn Baldwin 	}
1231746c92e0SJohn Baldwin }
1232746c92e0SJohn Baldwin 
12335fe58019SAttilio Rao /*
12345fe58019SAttilio Rao     struct vnop_read_args {
12355fe58019SAttilio Rao 	struct vnode *a_vp;
12365fe58019SAttilio Rao 	struct uio *a_uio;
12375fe58019SAttilio Rao 	int  a_ioflag;
12385fe58019SAttilio Rao 	struct ucred *a_cred;
12395fe58019SAttilio Rao     };
12405fe58019SAttilio Rao */
12415fe58019SAttilio Rao static int
12425fe58019SAttilio Rao fuse_vnop_read(struct vop_read_args *ap)
12435fe58019SAttilio Rao {
12445fe58019SAttilio Rao 	struct vnode *vp = ap->a_vp;
12455fe58019SAttilio Rao 	struct uio *uio = ap->a_uio;
12465fe58019SAttilio Rao 	int ioflag = ap->a_ioflag;
12475fe58019SAttilio Rao 	struct ucred *cred = ap->a_cred;
1248f8d4af10SAlan Somers 	pid_t pid = curthread->td_proc->p_pid;
12495fe58019SAttilio Rao 
12505fe58019SAttilio Rao 	if (fuse_isdeadfs(vp)) {
12515fe58019SAttilio Rao 		return ENXIO;
12525fe58019SAttilio Rao 	}
1253ead063e0SEdward Tomasz Napierala 
1254ead063e0SEdward Tomasz Napierala 	if (VTOFUD(vp)->flag & FN_DIRECTIO) {
1255ead063e0SEdward Tomasz Napierala 		ioflag |= IO_DIRECT;
1256ead063e0SEdward Tomasz Napierala 	}
1257ead063e0SEdward Tomasz Napierala 
1258f8d4af10SAlan Somers 	return fuse_io_dispatch(vp, uio, ioflag, cred, pid);
12595fe58019SAttilio Rao }
12605fe58019SAttilio Rao 
12615fe58019SAttilio Rao /*
12625fe58019SAttilio Rao     struct vnop_readdir_args {
12635fe58019SAttilio Rao 	struct vnode *a_vp;
12645fe58019SAttilio Rao 	struct uio *a_uio;
12655fe58019SAttilio Rao 	struct ucred *a_cred;
12665fe58019SAttilio Rao 	int *a_eofflag;
12675fe58019SAttilio Rao 	int *ncookies;
12685fe58019SAttilio Rao 	u_long **a_cookies;
12695fe58019SAttilio Rao     };
12705fe58019SAttilio Rao */
12715fe58019SAttilio Rao static int
12725fe58019SAttilio Rao fuse_vnop_readdir(struct vop_readdir_args *ap)
12735fe58019SAttilio Rao {
12745fe58019SAttilio Rao 	struct vnode *vp = ap->a_vp;
12755fe58019SAttilio Rao 	struct uio *uio = ap->a_uio;
12765fe58019SAttilio Rao 	struct ucred *cred = ap->a_cred;
12775fe58019SAttilio Rao 	struct fuse_filehandle *fufh = NULL;
12785fe58019SAttilio Rao 	struct fuse_iov cookediov;
12795fe58019SAttilio Rao 	int err = 0;
1280f8d4af10SAlan Somers 	pid_t pid = curthread->td_proc->p_pid;
12815fe58019SAttilio Rao 
12825fe58019SAttilio Rao 	if (fuse_isdeadfs(vp)) {
12835fe58019SAttilio Rao 		return ENXIO;
12845fe58019SAttilio Rao 	}
12855fe58019SAttilio Rao 	if (				/* XXXIP ((uio_iovcnt(uio) > 1)) || */
12865fe58019SAttilio Rao 	    (uio_resid(uio) < sizeof(struct dirent))) {
12875fe58019SAttilio Rao 		return EINVAL;
12885fe58019SAttilio Rao 	}
12895fe58019SAttilio Rao 
129035cf0e7eSAlan Somers 	err = fuse_filehandle_get_dir(vp, &fufh, cred, pid);
129135cf0e7eSAlan Somers 	if (err)
12925fe58019SAttilio Rao 		return (err);
12935fe58019SAttilio Rao #define DIRCOOKEDSIZE FUSE_DIRENT_ALIGN(FUSE_NAME_OFFSET + MAXNAMLEN + 1)
12945fe58019SAttilio Rao 	fiov_init(&cookediov, DIRCOOKEDSIZE);
12955fe58019SAttilio Rao 
12965fe58019SAttilio Rao 	err = fuse_internal_readdir(vp, uio, fufh, &cookediov);
12975fe58019SAttilio Rao 
12985fe58019SAttilio Rao 	fiov_teardown(&cookediov);
12995ec10aa5SAlan Somers 
13005fe58019SAttilio Rao 	return err;
13015fe58019SAttilio Rao }
13025fe58019SAttilio Rao 
13035fe58019SAttilio Rao /*
13045fe58019SAttilio Rao     struct vnop_readlink_args {
13055fe58019SAttilio Rao 	struct vnode *a_vp;
13065fe58019SAttilio Rao 	struct uio *a_uio;
13075fe58019SAttilio Rao 	struct ucred *a_cred;
13085fe58019SAttilio Rao     };
13095fe58019SAttilio Rao */
13105fe58019SAttilio Rao static int
13115fe58019SAttilio Rao fuse_vnop_readlink(struct vop_readlink_args *ap)
13125fe58019SAttilio Rao {
13135fe58019SAttilio Rao 	struct vnode *vp = ap->a_vp;
13145fe58019SAttilio Rao 	struct uio *uio = ap->a_uio;
13155fe58019SAttilio Rao 	struct ucred *cred = ap->a_cred;
13165fe58019SAttilio Rao 
13175fe58019SAttilio Rao 	struct fuse_dispatcher fdi;
13185fe58019SAttilio Rao 	int err;
13195fe58019SAttilio Rao 
13205fe58019SAttilio Rao 	if (fuse_isdeadfs(vp)) {
13215fe58019SAttilio Rao 		return ENXIO;
13225fe58019SAttilio Rao 	}
13235fe58019SAttilio Rao 	if (!vnode_islnk(vp)) {
13245fe58019SAttilio Rao 		return EINVAL;
13255fe58019SAttilio Rao 	}
13265fe58019SAttilio Rao 	fdisp_init(&fdi, 0);
13275fe58019SAttilio Rao 	err = fdisp_simple_putget_vp(&fdi, FUSE_READLINK, vp, curthread, cred);
13285fe58019SAttilio Rao 	if (err) {
13295fe58019SAttilio Rao 		goto out;
13305fe58019SAttilio Rao 	}
13315fe58019SAttilio Rao 	if (((char *)fdi.answ)[0] == '/' &&
13325fe58019SAttilio Rao 	    fuse_get_mpdata(vnode_mount(vp))->dataflags & FSESS_PUSH_SYMLINKS_IN) {
13335fe58019SAttilio Rao 		char *mpth = vnode_mount(vp)->mnt_stat.f_mntonname;
13345fe58019SAttilio Rao 
13355fe58019SAttilio Rao 		err = uiomove(mpth, strlen(mpth), uio);
13365fe58019SAttilio Rao 	}
13375fe58019SAttilio Rao 	if (!err) {
13385fe58019SAttilio Rao 		err = uiomove(fdi.answ, fdi.iosize, uio);
13395fe58019SAttilio Rao 	}
13405fe58019SAttilio Rao out:
13415fe58019SAttilio Rao 	fdisp_destroy(&fdi);
13425fe58019SAttilio Rao 	return err;
13435fe58019SAttilio Rao }
13445fe58019SAttilio Rao 
13455fe58019SAttilio Rao /*
13465fe58019SAttilio Rao     struct vnop_reclaim_args {
13475fe58019SAttilio Rao 	struct vnode *a_vp;
13485fe58019SAttilio Rao 	struct thread *a_td;
13495fe58019SAttilio Rao     };
13505fe58019SAttilio Rao */
13515fe58019SAttilio Rao static int
13525fe58019SAttilio Rao fuse_vnop_reclaim(struct vop_reclaim_args *ap)
13535fe58019SAttilio Rao {
13545fe58019SAttilio Rao 	struct vnode *vp = ap->a_vp;
13555fe58019SAttilio Rao 	struct thread *td = ap->a_td;
13565fe58019SAttilio Rao 	struct fuse_vnode_data *fvdat = VTOFUD(vp);
13575ec10aa5SAlan Somers 	struct fuse_filehandle *fufh, *fufh_tmp;
13585fe58019SAttilio Rao 
13595fe58019SAttilio Rao 	if (!fvdat) {
13605fe58019SAttilio Rao 		panic("FUSE: no vnode data during recycling");
13615fe58019SAttilio Rao 	}
13625ec10aa5SAlan Somers 	LIST_FOREACH_SAFE(fufh, &fvdat->handles, next, fufh_tmp) {
13635ec10aa5SAlan Somers 		printf("FUSE: vnode being reclaimed with open fufh "
13649e444871SAlan Somers 			"(type=%#x)", fufh->fufh_type);
13655ec10aa5SAlan Somers 		fuse_filehandle_close(vp, fufh, td, NULL);
13665fe58019SAttilio Rao 	}
13675fe58019SAttilio Rao 
13685fe58019SAttilio Rao 	if ((!fuse_isdeadfs(vp)) && (fvdat->nlookup)) {
13695fe58019SAttilio Rao 		fuse_internal_forget_send(vnode_mount(vp), td, NULL, VTOI(vp),
13705fe58019SAttilio Rao 		    fvdat->nlookup);
13715fe58019SAttilio Rao 	}
13725fe58019SAttilio Rao 	fuse_vnode_setparent(vp, NULL);
13735fe58019SAttilio Rao 	cache_purge(vp);
13745fe58019SAttilio Rao 	vfs_hash_remove(vp);
13755fe58019SAttilio Rao 	vnode_destroy_vobject(vp);
13765fe58019SAttilio Rao 	fuse_vnode_destroy(vp);
13775fe58019SAttilio Rao 
13785fe58019SAttilio Rao 	return 0;
13795fe58019SAttilio Rao }
13805fe58019SAttilio Rao 
13815fe58019SAttilio Rao /*
13825fe58019SAttilio Rao     struct vnop_remove_args {
13835fe58019SAttilio Rao 	struct vnode *a_dvp;
13845fe58019SAttilio Rao 	struct vnode *a_vp;
13855fe58019SAttilio Rao 	struct componentname *a_cnp;
13865fe58019SAttilio Rao     };
13875fe58019SAttilio Rao */
13885fe58019SAttilio Rao static int
13895fe58019SAttilio Rao fuse_vnop_remove(struct vop_remove_args *ap)
13905fe58019SAttilio Rao {
13915fe58019SAttilio Rao 	struct vnode *dvp = ap->a_dvp;
13925fe58019SAttilio Rao 	struct vnode *vp = ap->a_vp;
13935fe58019SAttilio Rao 	struct componentname *cnp = ap->a_cnp;
13945fe58019SAttilio Rao 
13955fe58019SAttilio Rao 	int err;
13965fe58019SAttilio Rao 
13975fe58019SAttilio Rao 	if (fuse_isdeadfs(vp)) {
13985fe58019SAttilio Rao 		return ENXIO;
13995fe58019SAttilio Rao 	}
14005fe58019SAttilio Rao 	if (vnode_isdir(vp)) {
14015fe58019SAttilio Rao 		return EPERM;
14025fe58019SAttilio Rao 	}
14035fe58019SAttilio Rao 	cache_purge(vp);
14045fe58019SAttilio Rao 
14055fe58019SAttilio Rao 	err = fuse_internal_remove(dvp, vp, cnp, FUSE_UNLINK);
14065fe58019SAttilio Rao 
1407d9454fabSAttilio Rao 	if (err == 0)
14085fe58019SAttilio Rao 		fuse_internal_vnode_disappear(vp);
14095fe58019SAttilio Rao 	return err;
14105fe58019SAttilio Rao }
14115fe58019SAttilio Rao 
14125fe58019SAttilio Rao /*
14135fe58019SAttilio Rao     struct vnop_rename_args {
14145fe58019SAttilio Rao 	struct vnode *a_fdvp;
14155fe58019SAttilio Rao 	struct vnode *a_fvp;
14165fe58019SAttilio Rao 	struct componentname *a_fcnp;
14175fe58019SAttilio Rao 	struct vnode *a_tdvp;
14185fe58019SAttilio Rao 	struct vnode *a_tvp;
14195fe58019SAttilio Rao 	struct componentname *a_tcnp;
14205fe58019SAttilio Rao     };
14215fe58019SAttilio Rao */
14225fe58019SAttilio Rao static int
14235fe58019SAttilio Rao fuse_vnop_rename(struct vop_rename_args *ap)
14245fe58019SAttilio Rao {
14255fe58019SAttilio Rao 	struct vnode *fdvp = ap->a_fdvp;
14265fe58019SAttilio Rao 	struct vnode *fvp = ap->a_fvp;
14275fe58019SAttilio Rao 	struct componentname *fcnp = ap->a_fcnp;
14285fe58019SAttilio Rao 	struct vnode *tdvp = ap->a_tdvp;
14295fe58019SAttilio Rao 	struct vnode *tvp = ap->a_tvp;
14305fe58019SAttilio Rao 	struct componentname *tcnp = ap->a_tcnp;
14315fe58019SAttilio Rao 	struct fuse_data *data;
14325fe58019SAttilio Rao 
14335fe58019SAttilio Rao 	int err = 0;
14345fe58019SAttilio Rao 
14355fe58019SAttilio Rao 	if (fuse_isdeadfs(fdvp)) {
14365fe58019SAttilio Rao 		return ENXIO;
14375fe58019SAttilio Rao 	}
14385fe58019SAttilio Rao 	if (fvp->v_mount != tdvp->v_mount ||
14395fe58019SAttilio Rao 	    (tvp && fvp->v_mount != tvp->v_mount)) {
1440cf169498SAlan Somers 		SDT_PROBE2(fuse, , vnops, trace, 1, "cross-device rename");
14415fe58019SAttilio Rao 		err = EXDEV;
14425fe58019SAttilio Rao 		goto out;
14435fe58019SAttilio Rao 	}
14445fe58019SAttilio Rao 	cache_purge(fvp);
14455fe58019SAttilio Rao 
14465fe58019SAttilio Rao 	/*
14475fe58019SAttilio Rao 	 * FUSE library is expected to check if target directory is not
14485fe58019SAttilio Rao 	 * under the source directory in the file system tree.
14495fe58019SAttilio Rao 	 * Linux performs this check at VFS level.
14505fe58019SAttilio Rao 	 */
14515fe58019SAttilio Rao 	data = fuse_get_mpdata(vnode_mount(tdvp));
14525fe58019SAttilio Rao 	sx_xlock(&data->rename_lock);
14535fe58019SAttilio Rao 	err = fuse_internal_rename(fdvp, fcnp, tdvp, tcnp);
14545fe58019SAttilio Rao 	if (err == 0) {
1455d9454fabSAttilio Rao 		if (tdvp != fdvp)
14565fe58019SAttilio Rao 			fuse_vnode_setparent(fvp, tdvp);
14575fe58019SAttilio Rao 		if (tvp != NULL)
14585fe58019SAttilio Rao 			fuse_vnode_setparent(tvp, NULL);
14595fe58019SAttilio Rao 	}
14605fe58019SAttilio Rao 	sx_unlock(&data->rename_lock);
14615fe58019SAttilio Rao 
14625fe58019SAttilio Rao 	if (tvp != NULL && tvp != fvp) {
14635fe58019SAttilio Rao 		cache_purge(tvp);
14645fe58019SAttilio Rao 	}
14655fe58019SAttilio Rao 	if (vnode_isdir(fvp)) {
14665fe58019SAttilio Rao 		if ((tvp != NULL) && vnode_isdir(tvp)) {
14675fe58019SAttilio Rao 			cache_purge(tdvp);
14685fe58019SAttilio Rao 		}
14695fe58019SAttilio Rao 		cache_purge(fdvp);
14705fe58019SAttilio Rao 	}
14715fe58019SAttilio Rao out:
14725fe58019SAttilio Rao 	if (tdvp == tvp) {
14735fe58019SAttilio Rao 		vrele(tdvp);
14745fe58019SAttilio Rao 	} else {
14755fe58019SAttilio Rao 		vput(tdvp);
14765fe58019SAttilio Rao 	}
14775fe58019SAttilio Rao 	if (tvp != NULL) {
14785fe58019SAttilio Rao 		vput(tvp);
14795fe58019SAttilio Rao 	}
14805fe58019SAttilio Rao 	vrele(fdvp);
14815fe58019SAttilio Rao 	vrele(fvp);
14825fe58019SAttilio Rao 
14835fe58019SAttilio Rao 	return err;
14845fe58019SAttilio Rao }
14855fe58019SAttilio Rao 
14865fe58019SAttilio Rao /*
14875fe58019SAttilio Rao     struct vnop_rmdir_args {
14885fe58019SAttilio Rao 	    struct vnode *a_dvp;
14895fe58019SAttilio Rao 	    struct vnode *a_vp;
14905fe58019SAttilio Rao 	    struct componentname *a_cnp;
14915fe58019SAttilio Rao     } *ap;
14925fe58019SAttilio Rao */
14935fe58019SAttilio Rao static int
14945fe58019SAttilio Rao fuse_vnop_rmdir(struct vop_rmdir_args *ap)
14955fe58019SAttilio Rao {
14965fe58019SAttilio Rao 	struct vnode *dvp = ap->a_dvp;
14975fe58019SAttilio Rao 	struct vnode *vp = ap->a_vp;
14985fe58019SAttilio Rao 
14995fe58019SAttilio Rao 	int err;
15005fe58019SAttilio Rao 
15015fe58019SAttilio Rao 	if (fuse_isdeadfs(vp)) {
15025fe58019SAttilio Rao 		return ENXIO;
15035fe58019SAttilio Rao 	}
15045fe58019SAttilio Rao 	if (VTOFUD(vp) == VTOFUD(dvp)) {
15055fe58019SAttilio Rao 		return EINVAL;
15065fe58019SAttilio Rao 	}
15075fe58019SAttilio Rao 	err = fuse_internal_remove(dvp, vp, ap->a_cnp, FUSE_RMDIR);
15085fe58019SAttilio Rao 
1509d9454fabSAttilio Rao 	if (err == 0)
15105fe58019SAttilio Rao 		fuse_internal_vnode_disappear(vp);
15115fe58019SAttilio Rao 	return err;
15125fe58019SAttilio Rao }
15135fe58019SAttilio Rao 
15145fe58019SAttilio Rao /*
15155fe58019SAttilio Rao     struct vnop_setattr_args {
15165fe58019SAttilio Rao 	struct vnode *a_vp;
15175fe58019SAttilio Rao 	struct vattr *a_vap;
15185fe58019SAttilio Rao 	struct ucred *a_cred;
15195fe58019SAttilio Rao 	struct thread *a_td;
15205fe58019SAttilio Rao     };
15215fe58019SAttilio Rao */
15225fe58019SAttilio Rao static int
15235fe58019SAttilio Rao fuse_vnop_setattr(struct vop_setattr_args *ap)
15245fe58019SAttilio Rao {
15255fe58019SAttilio Rao 	struct vnode *vp = ap->a_vp;
15265fe58019SAttilio Rao 	struct vattr *vap = ap->a_vap;
15275fe58019SAttilio Rao 	struct ucred *cred = ap->a_cred;
15285fe58019SAttilio Rao 	struct thread *td = curthread;
15295fe58019SAttilio Rao 	struct fuse_dispatcher fdi;
15305fe58019SAttilio Rao 	struct fuse_setattr_in *fsai;
15315fe58019SAttilio Rao 	struct fuse_access_param facp;
1532f8d4af10SAlan Somers 	pid_t pid = td->td_proc->p_pid;
15335fe58019SAttilio Rao 
15345fe58019SAttilio Rao 	int err = 0;
15355fe58019SAttilio Rao 	enum vtype vtyp;
15365fe58019SAttilio Rao 	int sizechanged = 0;
15375fe58019SAttilio Rao 	uint64_t newsize = 0;
1538*ff4fbdf5SAlan Somers 	accmode_t accmode = 0;
15395fe58019SAttilio Rao 
15405fe58019SAttilio Rao 	if (fuse_isdeadfs(vp)) {
15415fe58019SAttilio Rao 		return ENXIO;
15425fe58019SAttilio Rao 	}
15435fe58019SAttilio Rao 	fdisp_init(&fdi, sizeof(*fsai));
15445fe58019SAttilio Rao 	fdisp_make_vp(&fdi, FUSE_SETATTR, vp, td, cred);
15455fe58019SAttilio Rao 	fsai = fdi.indata;
15465fe58019SAttilio Rao 	fsai->valid = 0;
15475fe58019SAttilio Rao 
15485fe58019SAttilio Rao 	bzero(&facp, sizeof(facp));
15495fe58019SAttilio Rao 
15505fe58019SAttilio Rao 	facp.xuid = vap->va_uid;
15515fe58019SAttilio Rao 	facp.xgid = vap->va_gid;
15525fe58019SAttilio Rao 
15535fe58019SAttilio Rao 	if (vap->va_uid != (uid_t)VNOVAL) {
15545fe58019SAttilio Rao 		facp.facc_flags |= FACCESS_CHOWN;
15555fe58019SAttilio Rao 		fsai->uid = vap->va_uid;
15565fe58019SAttilio Rao 		fsai->valid |= FATTR_UID;
1557*ff4fbdf5SAlan Somers 		accmode |= VADMIN;
15585fe58019SAttilio Rao 	}
15595fe58019SAttilio Rao 	if (vap->va_gid != (gid_t)VNOVAL) {
15605fe58019SAttilio Rao 		facp.facc_flags |= FACCESS_CHOWN;
15615fe58019SAttilio Rao 		fsai->gid = vap->va_gid;
15625fe58019SAttilio Rao 		fsai->valid |= FATTR_GID;
1563*ff4fbdf5SAlan Somers 		accmode |= VADMIN;
15645fe58019SAttilio Rao 	}
15655fe58019SAttilio Rao 	if (vap->va_size != VNOVAL) {
15665fe58019SAttilio Rao 
15675fe58019SAttilio Rao 		struct fuse_filehandle *fufh = NULL;
15685fe58019SAttilio Rao 
15695fe58019SAttilio Rao 		/*Truncate to a new value. */
15705fe58019SAttilio Rao 		fsai->size = vap->va_size;
15715fe58019SAttilio Rao 		sizechanged = 1;
15725fe58019SAttilio Rao 		newsize = vap->va_size;
15735fe58019SAttilio Rao 		fsai->valid |= FATTR_SIZE;
1574*ff4fbdf5SAlan Somers 		accmode |= VWRITE;
15755fe58019SAttilio Rao 
15769f10f423SAlan Somers 		fuse_filehandle_getrw(vp, FWRITE, &fufh, cred, pid);
15775fe58019SAttilio Rao 		if (fufh) {
15785fe58019SAttilio Rao 			fsai->fh = fufh->fh_id;
15795fe58019SAttilio Rao 			fsai->valid |= FATTR_FH;
15805fe58019SAttilio Rao 		}
15815fe58019SAttilio Rao 	}
15825fe58019SAttilio Rao 	if (vap->va_atime.tv_sec != VNOVAL) {
15835fe58019SAttilio Rao 		fsai->atime = vap->va_atime.tv_sec;
15845fe58019SAttilio Rao 		fsai->atimensec = vap->va_atime.tv_nsec;
15855fe58019SAttilio Rao 		fsai->valid |= FATTR_ATIME;
1586*ff4fbdf5SAlan Somers 		accmode |= VADMIN;
1587*ff4fbdf5SAlan Somers 		/*
1588*ff4fbdf5SAlan Somers 		 * TODO: only require VWRITE if UTIMENS_NULL is set. PR 237181
1589*ff4fbdf5SAlan Somers 		 */
15905fe58019SAttilio Rao 	}
15915fe58019SAttilio Rao 	if (vap->va_mtime.tv_sec != VNOVAL) {
15925fe58019SAttilio Rao 		fsai->mtime = vap->va_mtime.tv_sec;
15935fe58019SAttilio Rao 		fsai->mtimensec = vap->va_mtime.tv_nsec;
15945fe58019SAttilio Rao 		fsai->valid |= FATTR_MTIME;
1595*ff4fbdf5SAlan Somers 		accmode |= VADMIN;
1596*ff4fbdf5SAlan Somers 		/*
1597*ff4fbdf5SAlan Somers 		 * TODO: only require VWRITE if UTIMENS_NULL is set. PR 237181
1598*ff4fbdf5SAlan Somers 		 */
15995fe58019SAttilio Rao 	}
16005fe58019SAttilio Rao 	if (vap->va_mode != (mode_t)VNOVAL) {
16015fe58019SAttilio Rao 		fsai->mode = vap->va_mode & ALLPERMS;
16025fe58019SAttilio Rao 		fsai->valid |= FATTR_MODE;
1603*ff4fbdf5SAlan Somers 		accmode |= VADMIN;
16045fe58019SAttilio Rao 	}
16055fe58019SAttilio Rao 	if (!fsai->valid) {
16065fe58019SAttilio Rao 		goto out;
16075fe58019SAttilio Rao 	}
16085fe58019SAttilio Rao 	vtyp = vnode_vtype(vp);
16095fe58019SAttilio Rao 
16105fe58019SAttilio Rao 	if (fsai->valid & FATTR_SIZE && vtyp == VDIR) {
16115fe58019SAttilio Rao 		err = EISDIR;
16125fe58019SAttilio Rao 		goto out;
16135fe58019SAttilio Rao 	}
16145fe58019SAttilio Rao 	if (vfs_isrdonly(vnode_mount(vp)) && (fsai->valid & ~FATTR_SIZE || vtyp == VREG)) {
16155fe58019SAttilio Rao 		err = EROFS;
16165fe58019SAttilio Rao 		goto out;
16175fe58019SAttilio Rao 	}
1618*ff4fbdf5SAlan Somers 	err = fuse_internal_access(vp, accmode, &facp, td, cred);
1619*ff4fbdf5SAlan Somers 	if (err)
1620*ff4fbdf5SAlan Somers 		goto out;
16215fe58019SAttilio Rao 
1622d9454fabSAttilio Rao 	if ((err = fdisp_wait_answ(&fdi)))
16235fe58019SAttilio Rao 		goto out;
16245fe58019SAttilio Rao 	vtyp = IFTOVT(((struct fuse_attr_out *)fdi.answ)->attr.mode);
16255fe58019SAttilio Rao 
16265fe58019SAttilio Rao 	if (vnode_vtype(vp) != vtyp) {
16275fe58019SAttilio Rao 		if (vnode_vtype(vp) == VNON && vtyp != VNON) {
1628cf169498SAlan Somers 			SDT_PROBE2(fuse, , vnops, trace, 1, "FUSE: Dang! "
1629cf169498SAlan Somers 				"vnode_vtype is VNON and vtype isn't.");
16305fe58019SAttilio Rao 		} else {
16315fe58019SAttilio Rao 			/*
16325fe58019SAttilio Rao 	                 * STALE vnode, ditch
16335fe58019SAttilio Rao 	                 *
1634cf169498SAlan Somers 			 * The vnode has changed its type "behind our back".
1635cf169498SAlan Somers 			 * There's nothing really we can do, so let us just
1636cf169498SAlan Somers 			 * force an internal revocation and tell the caller to
1637cf169498SAlan Somers 			 * try again, if interested.
16385fe58019SAttilio Rao 	                 */
16395fe58019SAttilio Rao 			fuse_internal_vnode_disappear(vp);
16405fe58019SAttilio Rao 			err = EAGAIN;
16415fe58019SAttilio Rao 		}
16425fe58019SAttilio Rao 	}
16437e4844f7SAlan Somers 	if (err == 0) {
16447e4844f7SAlan Somers 		struct fuse_attr_out *fao = (struct fuse_attr_out*)fdi.answ;
16457e4844f7SAlan Somers 		fuse_internal_cache_attrs(vp, &fao->attr, fao->attr_valid,
16467e4844f7SAlan Somers 			fao->attr_valid_nsec, NULL);
16477e4844f7SAlan Somers 	}
1648194e691aSConrad Meyer 
16495fe58019SAttilio Rao out:
16505fe58019SAttilio Rao 	fdisp_destroy(&fdi);
16515fe58019SAttilio Rao 	if (!err && sizechanged) {
16525fe58019SAttilio Rao 		fuse_vnode_setsize(vp, cred, newsize);
16535fe58019SAttilio Rao 		VTOFUD(vp)->flag &= ~FN_SIZECHANGE;
16545fe58019SAttilio Rao 	}
16555fe58019SAttilio Rao 	return err;
16565fe58019SAttilio Rao }
16575fe58019SAttilio Rao 
16585fe58019SAttilio Rao /*
16595fe58019SAttilio Rao     struct vnop_strategy_args {
16605fe58019SAttilio Rao 	struct vnode *a_vp;
16615fe58019SAttilio Rao 	struct buf *a_bp;
16625fe58019SAttilio Rao     };
16635fe58019SAttilio Rao */
16645fe58019SAttilio Rao static int
16655fe58019SAttilio Rao fuse_vnop_strategy(struct vop_strategy_args *ap)
16665fe58019SAttilio Rao {
16675fe58019SAttilio Rao 	struct vnode *vp = ap->a_vp;
16685fe58019SAttilio Rao 	struct buf *bp = ap->a_bp;
16695fe58019SAttilio Rao 
16705fe58019SAttilio Rao 	if (!vp || fuse_isdeadfs(vp)) {
16715fe58019SAttilio Rao 		bp->b_ioflags |= BIO_ERROR;
16725fe58019SAttilio Rao 		bp->b_error = ENXIO;
16735fe58019SAttilio Rao 		bufdone(bp);
167498852a32SAlan Somers 		return 0;
16755fe58019SAttilio Rao 	}
1676f203d173SAlan Somers 	if (bp->b_iocmd == BIO_WRITE) {
1677f203d173SAlan Somers 		int err;
16785fe58019SAttilio Rao 
1679f203d173SAlan Somers 		err = fuse_vnode_refreshsize(vp, NOCRED);
1680f203d173SAlan Somers 		if (err) {
1681f203d173SAlan Somers 			bp->b_ioflags |= BIO_ERROR;
1682f203d173SAlan Somers 			bp->b_error = err;
168398852a32SAlan Somers 			bufdone(bp);
1684f203d173SAlan Somers 			return 0;
1685f203d173SAlan Somers 		}
1686f203d173SAlan Somers 	}
16875fe58019SAttilio Rao 
16885fe58019SAttilio Rao 	/*
1689f203d173SAlan Somers 	 * VOP_STRATEGY always returns zero and signals error via bp->b_ioflags.
1690f203d173SAlan Somers 	 * fuse_io_strategy sets bp's error fields
16915fe58019SAttilio Rao 	 */
1692f203d173SAlan Somers 	(void)fuse_io_strategy(vp, bp);
1693f203d173SAlan Somers 
16945fe58019SAttilio Rao 	return 0;
16955fe58019SAttilio Rao }
16965fe58019SAttilio Rao 
16975fe58019SAttilio Rao 
16985fe58019SAttilio Rao /*
16995fe58019SAttilio Rao     struct vnop_symlink_args {
17005fe58019SAttilio Rao 	struct vnode *a_dvp;
17015fe58019SAttilio Rao 	struct vnode **a_vpp;
17025fe58019SAttilio Rao 	struct componentname *a_cnp;
17035fe58019SAttilio Rao 	struct vattr *a_vap;
17045fe58019SAttilio Rao 	char *a_target;
17055fe58019SAttilio Rao     };
17065fe58019SAttilio Rao */
17075fe58019SAttilio Rao static int
17085fe58019SAttilio Rao fuse_vnop_symlink(struct vop_symlink_args *ap)
17095fe58019SAttilio Rao {
17105fe58019SAttilio Rao 	struct vnode *dvp = ap->a_dvp;
17115fe58019SAttilio Rao 	struct vnode **vpp = ap->a_vpp;
17125fe58019SAttilio Rao 	struct componentname *cnp = ap->a_cnp;
17131493c2eeSBrooks Davis 	const char *target = ap->a_target;
17145fe58019SAttilio Rao 
17155fe58019SAttilio Rao 	struct fuse_dispatcher fdi;
17165fe58019SAttilio Rao 
17175fe58019SAttilio Rao 	int err;
17185fe58019SAttilio Rao 	size_t len;
17195fe58019SAttilio Rao 
17205fe58019SAttilio Rao 	if (fuse_isdeadfs(dvp)) {
17215fe58019SAttilio Rao 		return ENXIO;
17225fe58019SAttilio Rao 	}
17235fe58019SAttilio Rao 	/*
17245fe58019SAttilio Rao 	 * Unlike the other creator type calls, here we have to create a message
17255fe58019SAttilio Rao 	 * where the name of the new entry comes first, and the data describing
17265fe58019SAttilio Rao 	 * the entry comes second.
17275fe58019SAttilio Rao 	 * Hence we can't rely on our handy fuse_internal_newentry() routine,
17285fe58019SAttilio Rao 	 * but put together the message manually and just call the core part.
17295fe58019SAttilio Rao 	 */
17305fe58019SAttilio Rao 
17315fe58019SAttilio Rao 	len = strlen(target) + 1;
17325fe58019SAttilio Rao 	fdisp_init(&fdi, len + cnp->cn_namelen + 1);
17335fe58019SAttilio Rao 	fdisp_make_vp(&fdi, FUSE_SYMLINK, dvp, curthread, NULL);
17345fe58019SAttilio Rao 
17355fe58019SAttilio Rao 	memcpy(fdi.indata, cnp->cn_nameptr, cnp->cn_namelen);
17365fe58019SAttilio Rao 	((char *)fdi.indata)[cnp->cn_namelen] = '\0';
17375fe58019SAttilio Rao 	memcpy((char *)fdi.indata + cnp->cn_namelen + 1, target, len);
17385fe58019SAttilio Rao 
17395fe58019SAttilio Rao 	err = fuse_internal_newentry_core(dvp, vpp, cnp, VLNK, &fdi);
17405fe58019SAttilio Rao 	fdisp_destroy(&fdi);
17415fe58019SAttilio Rao 	return err;
17425fe58019SAttilio Rao }
17435fe58019SAttilio Rao 
17445fe58019SAttilio Rao /*
17455fe58019SAttilio Rao     struct vnop_write_args {
17465fe58019SAttilio Rao 	struct vnode *a_vp;
17475fe58019SAttilio Rao 	struct uio *a_uio;
17485fe58019SAttilio Rao 	int  a_ioflag;
17495fe58019SAttilio Rao 	struct ucred *a_cred;
17505fe58019SAttilio Rao     };
17515fe58019SAttilio Rao */
17525fe58019SAttilio Rao static int
17535fe58019SAttilio Rao fuse_vnop_write(struct vop_write_args *ap)
17545fe58019SAttilio Rao {
17555fe58019SAttilio Rao 	struct vnode *vp = ap->a_vp;
17565fe58019SAttilio Rao 	struct uio *uio = ap->a_uio;
17575fe58019SAttilio Rao 	int ioflag = ap->a_ioflag;
17585fe58019SAttilio Rao 	struct ucred *cred = ap->a_cred;
1759f8d4af10SAlan Somers 	pid_t pid = curthread->td_proc->p_pid;
1760f203d173SAlan Somers 	int err;
17615fe58019SAttilio Rao 
17625fe58019SAttilio Rao 	if (fuse_isdeadfs(vp)) {
17635fe58019SAttilio Rao 		return ENXIO;
17645fe58019SAttilio Rao 	}
1765f203d173SAlan Somers 	err = fuse_vnode_refreshsize(vp, cred);
1766f203d173SAlan Somers 	if (err)
1767f203d173SAlan Somers 		return err;
17685fe58019SAttilio Rao 
1769ead063e0SEdward Tomasz Napierala 	if (VTOFUD(vp)->flag & FN_DIRECTIO) {
1770ead063e0SEdward Tomasz Napierala 		ioflag |= IO_DIRECT;
1771ead063e0SEdward Tomasz Napierala 	}
1772ead063e0SEdward Tomasz Napierala 
1773f8d4af10SAlan Somers 	return fuse_io_dispatch(vp, uio, ioflag, cred, pid);
17745fe58019SAttilio Rao }
17755fe58019SAttilio Rao 
1776cf169498SAlan Somers SDT_PROBE_DEFINE1(fuse, , vnops, vnop_getpages_error, "int");
17775fe58019SAttilio Rao /*
17785fe58019SAttilio Rao     struct vnop_getpages_args {
17795fe58019SAttilio Rao 	struct vnode *a_vp;
17805fe58019SAttilio Rao 	vm_page_t *a_m;
17815fe58019SAttilio Rao 	int a_count;
17825fe58019SAttilio Rao 	int a_reqpage;
17835fe58019SAttilio Rao     };
17845fe58019SAttilio Rao */
17855fe58019SAttilio Rao static int
17865fe58019SAttilio Rao fuse_vnop_getpages(struct vop_getpages_args *ap)
17875fe58019SAttilio Rao {
17885fe58019SAttilio Rao 	int i, error, nextoff, size, toff, count, npages;
17895fe58019SAttilio Rao 	struct uio uio;
17905fe58019SAttilio Rao 	struct iovec iov;
17915fe58019SAttilio Rao 	vm_offset_t kva;
17925fe58019SAttilio Rao 	struct buf *bp;
17935fe58019SAttilio Rao 	struct vnode *vp;
17945fe58019SAttilio Rao 	struct thread *td;
17955fe58019SAttilio Rao 	struct ucred *cred;
17965fe58019SAttilio Rao 	vm_page_t *pages;
1797f8d4af10SAlan Somers 	pid_t pid = curthread->td_proc->p_pid;
17985fe58019SAttilio Rao 
17995fe58019SAttilio Rao 	vp = ap->a_vp;
18005fe58019SAttilio Rao 	KASSERT(vp->v_object, ("objectless vp passed to getpages"));
18015fe58019SAttilio Rao 	td = curthread;			/* XXX */
18025fe58019SAttilio Rao 	cred = curthread->td_ucred;	/* XXX */
18035fe58019SAttilio Rao 	pages = ap->a_m;
1804f17f88d3SGleb Smirnoff 	npages = ap->a_count;
18055fe58019SAttilio Rao 
18065fe58019SAttilio Rao 	if (!fsess_opt_mmap(vnode_mount(vp))) {
1807cf169498SAlan Somers 		SDT_PROBE2(fuse, , vnops, trace, 1,
1808cf169498SAlan Somers 			"called on non-cacheable vnode??\n");
18095fe58019SAttilio Rao 		return (VM_PAGER_ERROR);
18105fe58019SAttilio Rao 	}
18115fe58019SAttilio Rao 
18125fe58019SAttilio Rao 	/*
1813b0cd2017SGleb Smirnoff 	 * If the last page is partially valid, just return it and allow
1814b0cd2017SGleb Smirnoff 	 * the pager to zero-out the blanks.  Partially valid pages can
1815b0cd2017SGleb Smirnoff 	 * only occur at the file EOF.
1816b0cd2017SGleb Smirnoff 	 *
1817b0cd2017SGleb Smirnoff 	 * XXXGL: is that true for FUSE, which is a local filesystem,
1818b0cd2017SGleb Smirnoff 	 * but still somewhat disconnected from the kernel?
18195fe58019SAttilio Rao 	 */
182089f6b863SAttilio Rao 	VM_OBJECT_WLOCK(vp->v_object);
1821f17f88d3SGleb Smirnoff 	if (pages[npages - 1]->valid != 0 && --npages == 0)
1822f17f88d3SGleb Smirnoff 		goto out;
182389f6b863SAttilio Rao 	VM_OBJECT_WUNLOCK(vp->v_object);
18245fe58019SAttilio Rao 
18255fe58019SAttilio Rao 	/*
18265fe58019SAttilio Rao 	 * We use only the kva address for the buffer, but this is extremely
1827b3a15dddSPedro F. Giffuni 	 * convenient and fast.
18285fe58019SAttilio Rao 	 */
1829756a5412SGleb Smirnoff 	bp = uma_zalloc(fuse_pbuf_zone, M_WAITOK);
18305fe58019SAttilio Rao 
18315fe58019SAttilio Rao 	kva = (vm_offset_t)bp->b_data;
18325fe58019SAttilio Rao 	pmap_qenter(kva, pages, npages);
183383c9dea1SGleb Smirnoff 	VM_CNT_INC(v_vnodein);
183483c9dea1SGleb Smirnoff 	VM_CNT_ADD(v_vnodepgsin, npages);
18355fe58019SAttilio Rao 
1836f17f88d3SGleb Smirnoff 	count = npages << PAGE_SHIFT;
18375fe58019SAttilio Rao 	iov.iov_base = (caddr_t)kva;
18385fe58019SAttilio Rao 	iov.iov_len = count;
18395fe58019SAttilio Rao 	uio.uio_iov = &iov;
18405fe58019SAttilio Rao 	uio.uio_iovcnt = 1;
18415fe58019SAttilio Rao 	uio.uio_offset = IDX_TO_OFF(pages[0]->pindex);
18425fe58019SAttilio Rao 	uio.uio_resid = count;
18435fe58019SAttilio Rao 	uio.uio_segflg = UIO_SYSSPACE;
18445fe58019SAttilio Rao 	uio.uio_rw = UIO_READ;
18455fe58019SAttilio Rao 	uio.uio_td = td;
18465fe58019SAttilio Rao 
1847f8d4af10SAlan Somers 	error = fuse_io_dispatch(vp, &uio, IO_DIRECT, cred, pid);
18485fe58019SAttilio Rao 	pmap_qremove(kva, npages);
18495fe58019SAttilio Rao 
1850756a5412SGleb Smirnoff 	uma_zfree(fuse_pbuf_zone, bp);
18515fe58019SAttilio Rao 
18525fe58019SAttilio Rao 	if (error && (uio.uio_resid == count)) {
1853cf169498SAlan Somers 		SDT_PROBE1(fuse, , vnops, vnop_getpages_error, error);
18545fe58019SAttilio Rao 		return VM_PAGER_ERROR;
18555fe58019SAttilio Rao 	}
18565fe58019SAttilio Rao 	/*
18575fe58019SAttilio Rao 	 * Calculate the number of bytes read and validate only that number
18585fe58019SAttilio Rao 	 * of bytes.  Note that due to pending writes, size may be 0.  This
18595fe58019SAttilio Rao 	 * does not mean that the remaining data is invalid!
18605fe58019SAttilio Rao 	 */
18615fe58019SAttilio Rao 
18625fe58019SAttilio Rao 	size = count - uio.uio_resid;
186389f6b863SAttilio Rao 	VM_OBJECT_WLOCK(vp->v_object);
18645fe58019SAttilio Rao 	fuse_vm_page_lock_queues();
18655fe58019SAttilio Rao 	for (i = 0, toff = 0; i < npages; i++, toff = nextoff) {
18665fe58019SAttilio Rao 		vm_page_t m;
18675fe58019SAttilio Rao 
18685fe58019SAttilio Rao 		nextoff = toff + PAGE_SIZE;
18695fe58019SAttilio Rao 		m = pages[i];
18705fe58019SAttilio Rao 
18715fe58019SAttilio Rao 		if (nextoff <= size) {
18725fe58019SAttilio Rao 			/*
18735fe58019SAttilio Rao 			 * Read operation filled an entire page
18745fe58019SAttilio Rao 			 */
18755fe58019SAttilio Rao 			m->valid = VM_PAGE_BITS_ALL;
18765fe58019SAttilio Rao 			KASSERT(m->dirty == 0,
18775fe58019SAttilio Rao 			    ("fuse_getpages: page %p is dirty", m));
18785fe58019SAttilio Rao 		} else if (size > toff) {
18795fe58019SAttilio Rao 			/*
18805fe58019SAttilio Rao 			 * Read operation filled a partial page.
18815fe58019SAttilio Rao 			 */
18825fe58019SAttilio Rao 			m->valid = 0;
18835fe58019SAttilio Rao 			vm_page_set_valid_range(m, 0, size - toff);
18845fe58019SAttilio Rao 			KASSERT(m->dirty == 0,
18855fe58019SAttilio Rao 			    ("fuse_getpages: page %p is dirty", m));
18865fe58019SAttilio Rao 		} else {
18875fe58019SAttilio Rao 			/*
1888b3a15dddSPedro F. Giffuni 			 * Read operation was short.  If no error occurred
18895fe58019SAttilio Rao 			 * we may have hit a zero-fill section.   We simply
18905fe58019SAttilio Rao 			 * leave valid set to 0.
18915fe58019SAttilio Rao 			 */
18925fe58019SAttilio Rao 			;
18935fe58019SAttilio Rao 		}
18945fe58019SAttilio Rao 	}
18955fe58019SAttilio Rao 	fuse_vm_page_unlock_queues();
1896f17f88d3SGleb Smirnoff out:
189789f6b863SAttilio Rao 	VM_OBJECT_WUNLOCK(vp->v_object);
1898f17f88d3SGleb Smirnoff 	if (ap->a_rbehind)
1899f17f88d3SGleb Smirnoff 		*ap->a_rbehind = 0;
1900f17f88d3SGleb Smirnoff 	if (ap->a_rahead)
1901f17f88d3SGleb Smirnoff 		*ap->a_rahead = 0;
1902f17f88d3SGleb Smirnoff 	return (VM_PAGER_OK);
19035fe58019SAttilio Rao }
19045fe58019SAttilio Rao 
19055fe58019SAttilio Rao /*
19065fe58019SAttilio Rao     struct vnop_putpages_args {
19075fe58019SAttilio Rao 	struct vnode *a_vp;
19085fe58019SAttilio Rao 	vm_page_t *a_m;
19095fe58019SAttilio Rao 	int a_count;
19105fe58019SAttilio Rao 	int a_sync;
19115fe58019SAttilio Rao 	int *a_rtvals;
19125fe58019SAttilio Rao 	vm_ooffset_t a_offset;
19135fe58019SAttilio Rao     };
19145fe58019SAttilio Rao */
19155fe58019SAttilio Rao static int
19165fe58019SAttilio Rao fuse_vnop_putpages(struct vop_putpages_args *ap)
19175fe58019SAttilio Rao {
19185fe58019SAttilio Rao 	struct uio uio;
19195fe58019SAttilio Rao 	struct iovec iov;
19205fe58019SAttilio Rao 	vm_offset_t kva;
19215fe58019SAttilio Rao 	struct buf *bp;
19225fe58019SAttilio Rao 	int i, error, npages, count;
19235fe58019SAttilio Rao 	off_t offset;
19245fe58019SAttilio Rao 	int *rtvals;
19255fe58019SAttilio Rao 	struct vnode *vp;
19265fe58019SAttilio Rao 	struct thread *td;
19275fe58019SAttilio Rao 	struct ucred *cred;
19285fe58019SAttilio Rao 	vm_page_t *pages;
19295fe58019SAttilio Rao 	vm_ooffset_t fsize;
1930f8d4af10SAlan Somers 	pid_t pid = curthread->td_proc->p_pid;
19315fe58019SAttilio Rao 
19325fe58019SAttilio Rao 	vp = ap->a_vp;
19335fe58019SAttilio Rao 	KASSERT(vp->v_object, ("objectless vp passed to putpages"));
19345fe58019SAttilio Rao 	fsize = vp->v_object->un_pager.vnp.vnp_size;
19355fe58019SAttilio Rao 	td = curthread;			/* XXX */
19365fe58019SAttilio Rao 	cred = curthread->td_ucred;	/* XXX */
19375fe58019SAttilio Rao 	pages = ap->a_m;
19385fe58019SAttilio Rao 	count = ap->a_count;
19395fe58019SAttilio Rao 	rtvals = ap->a_rtvals;
19405fe58019SAttilio Rao 	npages = btoc(count);
19415fe58019SAttilio Rao 	offset = IDX_TO_OFF(pages[0]->pindex);
19425fe58019SAttilio Rao 
19435fe58019SAttilio Rao 	if (!fsess_opt_mmap(vnode_mount(vp))) {
1944cf169498SAlan Somers 		SDT_PROBE2(fuse, , vnops, trace, 1,
1945cf169498SAlan Somers 			"called on non-cacheable vnode??\n");
19465fe58019SAttilio Rao 	}
19475fe58019SAttilio Rao 	for (i = 0; i < npages; i++)
19485fe58019SAttilio Rao 		rtvals[i] = VM_PAGER_AGAIN;
19495fe58019SAttilio Rao 
19505fe58019SAttilio Rao 	/*
19515fe58019SAttilio Rao 	 * When putting pages, do not extend file past EOF.
19525fe58019SAttilio Rao 	 */
19535fe58019SAttilio Rao 
19545fe58019SAttilio Rao 	if (offset + count > fsize) {
19555fe58019SAttilio Rao 		count = fsize - offset;
19565fe58019SAttilio Rao 		if (count < 0)
19575fe58019SAttilio Rao 			count = 0;
19585fe58019SAttilio Rao 	}
19595fe58019SAttilio Rao 	/*
19605fe58019SAttilio Rao 	 * We use only the kva address for the buffer, but this is extremely
1961b3a15dddSPedro F. Giffuni 	 * convenient and fast.
19625fe58019SAttilio Rao 	 */
1963756a5412SGleb Smirnoff 	bp = uma_zalloc(fuse_pbuf_zone, M_WAITOK);
19645fe58019SAttilio Rao 
19655fe58019SAttilio Rao 	kva = (vm_offset_t)bp->b_data;
19665fe58019SAttilio Rao 	pmap_qenter(kva, pages, npages);
196783c9dea1SGleb Smirnoff 	VM_CNT_INC(v_vnodeout);
196883c9dea1SGleb Smirnoff 	VM_CNT_ADD(v_vnodepgsout, count);
19695fe58019SAttilio Rao 
19705fe58019SAttilio Rao 	iov.iov_base = (caddr_t)kva;
19715fe58019SAttilio Rao 	iov.iov_len = count;
19725fe58019SAttilio Rao 	uio.uio_iov = &iov;
19735fe58019SAttilio Rao 	uio.uio_iovcnt = 1;
19745fe58019SAttilio Rao 	uio.uio_offset = offset;
19755fe58019SAttilio Rao 	uio.uio_resid = count;
19765fe58019SAttilio Rao 	uio.uio_segflg = UIO_SYSSPACE;
19775fe58019SAttilio Rao 	uio.uio_rw = UIO_WRITE;
19785fe58019SAttilio Rao 	uio.uio_td = td;
19795fe58019SAttilio Rao 
1980f8d4af10SAlan Somers 	error = fuse_io_dispatch(vp, &uio, IO_DIRECT, cred, pid);
19815fe58019SAttilio Rao 
19825fe58019SAttilio Rao 	pmap_qremove(kva, npages);
1983756a5412SGleb Smirnoff 	uma_zfree(fuse_pbuf_zone, bp);
19845fe58019SAttilio Rao 
19855fe58019SAttilio Rao 	if (!error) {
19865fe58019SAttilio Rao 		int nwritten = round_page(count - uio.uio_resid) / PAGE_SIZE;
19875fe58019SAttilio Rao 
19885fe58019SAttilio Rao 		for (i = 0; i < nwritten; i++) {
19895fe58019SAttilio Rao 			rtvals[i] = VM_PAGER_OK;
199089f6b863SAttilio Rao 			VM_OBJECT_WLOCK(pages[i]->object);
19915fe58019SAttilio Rao 			vm_page_undirty(pages[i]);
199289f6b863SAttilio Rao 			VM_OBJECT_WUNLOCK(pages[i]->object);
19935fe58019SAttilio Rao 		}
19945fe58019SAttilio Rao 	}
19955fe58019SAttilio Rao 	return rtvals[0];
19965fe58019SAttilio Rao }
19975fe58019SAttilio Rao 
199804660064SFedor Uporov static const char extattr_namespace_separator = '.';
199904660064SFedor Uporov 
200004660064SFedor Uporov /*
200104660064SFedor Uporov     struct vop_getextattr_args {
200204660064SFedor Uporov 	struct vop_generic_args a_gen;
200304660064SFedor Uporov 	struct vnode *a_vp;
200404660064SFedor Uporov 	int a_attrnamespace;
200504660064SFedor Uporov 	const char *a_name;
200604660064SFedor Uporov 	struct uio *a_uio;
200704660064SFedor Uporov 	size_t *a_size;
200804660064SFedor Uporov 	struct ucred *a_cred;
200904660064SFedor Uporov 	struct thread *a_td;
201004660064SFedor Uporov     };
201104660064SFedor Uporov */
201204660064SFedor Uporov static int
201304660064SFedor Uporov fuse_vnop_getextattr(struct vop_getextattr_args *ap)
201404660064SFedor Uporov {
201504660064SFedor Uporov 	struct vnode *vp = ap->a_vp;
201604660064SFedor Uporov 	struct uio *uio = ap->a_uio;
201728f4f623SFedor Uporov 	struct fuse_dispatcher fdi;
201804660064SFedor Uporov 	struct fuse_getxattr_in *get_xattr_in;
201904660064SFedor Uporov 	struct fuse_getxattr_out *get_xattr_out;
202004660064SFedor Uporov 	struct mount *mp = vnode_mount(vp);
202104660064SFedor Uporov 	struct thread *td = ap->a_td;
202204660064SFedor Uporov 	struct ucred *cred = ap->a_cred;
202328f4f623SFedor Uporov 	char *prefix;
202428f4f623SFedor Uporov 	char *attr_str;
202528f4f623SFedor Uporov 	size_t len;
202628f4f623SFedor Uporov 	int err;
202704660064SFedor Uporov 
202804660064SFedor Uporov 	if (fuse_isdeadfs(vp))
202928f4f623SFedor Uporov 		return (ENXIO);
203004660064SFedor Uporov 
2031*ff4fbdf5SAlan Somers 	err = extattr_check_cred(vp, ap->a_attrnamespace, cred, td, VREAD);
2032*ff4fbdf5SAlan Somers 	if (err)
2033*ff4fbdf5SAlan Somers 		return err;
2034*ff4fbdf5SAlan Somers 
203504660064SFedor Uporov 	/* Default to looking for user attributes. */
203604660064SFedor Uporov 	if (ap->a_attrnamespace == EXTATTR_NAMESPACE_SYSTEM)
203704660064SFedor Uporov 		prefix = EXTATTR_NAMESPACE_SYSTEM_STRING;
203804660064SFedor Uporov 	else
203904660064SFedor Uporov 		prefix = EXTATTR_NAMESPACE_USER_STRING;
204004660064SFedor Uporov 
204104660064SFedor Uporov 	len = strlen(prefix) + sizeof(extattr_namespace_separator) +
204204660064SFedor Uporov 	    strlen(ap->a_name) + 1;
204304660064SFedor Uporov 
204404660064SFedor Uporov 	fdisp_init(&fdi, len + sizeof(*get_xattr_in));
204504660064SFedor Uporov 	fdisp_make_vp(&fdi, FUSE_GETXATTR, vp, td, cred);
204604660064SFedor Uporov 
204704660064SFedor Uporov 	get_xattr_in = fdi.indata;
204804660064SFedor Uporov 	/*
204904660064SFedor Uporov 	 * Check to see whether we're querying the available size or
205004660064SFedor Uporov 	 * issuing the actual request.  If we pass in 0, we get back struct
205104660064SFedor Uporov 	 * fuse_getxattr_out.  If we pass in a non-zero size, we get back
205204660064SFedor Uporov 	 * that much data, without the struct fuse_getxattr_out header.
205304660064SFedor Uporov 	 */
2054493b4a8cSFedor Uporov 	if (uio == NULL)
205504660064SFedor Uporov 		get_xattr_in->size = 0;
205604660064SFedor Uporov 	else
205704660064SFedor Uporov 		get_xattr_in->size = uio->uio_resid;
205804660064SFedor Uporov 
205904660064SFedor Uporov 	attr_str = (char *)fdi.indata + sizeof(*get_xattr_in);
206004660064SFedor Uporov 	snprintf(attr_str, len, "%s%c%s", prefix, extattr_namespace_separator,
206104660064SFedor Uporov 	    ap->a_name);
206204660064SFedor Uporov 
206304660064SFedor Uporov 	err = fdisp_wait_answ(&fdi);
206404660064SFedor Uporov 	if (err != 0) {
206504660064SFedor Uporov 		if (err == ENOSYS)
206604660064SFedor Uporov 			fsess_set_notimpl(mp, FUSE_GETXATTR);
206704660064SFedor Uporov 		goto out;
206804660064SFedor Uporov 	}
206904660064SFedor Uporov 
207004660064SFedor Uporov 	get_xattr_out = fdi.answ;
207104660064SFedor Uporov 
2072493b4a8cSFedor Uporov 	if (ap->a_size != NULL)
207304660064SFedor Uporov 		*ap->a_size = get_xattr_out->size;
2074493b4a8cSFedor Uporov 
2075493b4a8cSFedor Uporov 	if (uio != NULL)
207604660064SFedor Uporov 		err = uiomove(fdi.answ, fdi.iosize, uio);
207704660064SFedor Uporov 
207804660064SFedor Uporov out:
207904660064SFedor Uporov 	fdisp_destroy(&fdi);
208004660064SFedor Uporov 	return (err);
208104660064SFedor Uporov }
208204660064SFedor Uporov 
208304660064SFedor Uporov /*
208404660064SFedor Uporov     struct vop_setextattr_args {
208504660064SFedor Uporov 	struct vop_generic_args a_gen;
208604660064SFedor Uporov 	struct vnode *a_vp;
208704660064SFedor Uporov 	int a_attrnamespace;
208804660064SFedor Uporov 	const char *a_name;
208904660064SFedor Uporov 	struct uio *a_uio;
209004660064SFedor Uporov 	struct ucred *a_cred;
209104660064SFedor Uporov 	struct thread *a_td;
209204660064SFedor Uporov     };
209304660064SFedor Uporov */
209404660064SFedor Uporov static int
209504660064SFedor Uporov fuse_vnop_setextattr(struct vop_setextattr_args *ap)
209604660064SFedor Uporov {
209704660064SFedor Uporov 	struct vnode *vp = ap->a_vp;
209804660064SFedor Uporov 	struct uio *uio = ap->a_uio;
209928f4f623SFedor Uporov 	struct fuse_dispatcher fdi;
210004660064SFedor Uporov 	struct fuse_setxattr_in *set_xattr_in;
210104660064SFedor Uporov 	struct mount *mp = vnode_mount(vp);
210228f4f623SFedor Uporov 	struct thread *td = ap->a_td;
210328f4f623SFedor Uporov 	struct ucred *cred = ap->a_cred;
210404660064SFedor Uporov 	char *prefix;
210504660064SFedor Uporov 	size_t len;
210604660064SFedor Uporov 	char *attr_str;
210728f4f623SFedor Uporov 	int err;
210804660064SFedor Uporov 
210904660064SFedor Uporov 	if (fuse_isdeadfs(vp))
211028f4f623SFedor Uporov 		return (ENXIO);
211104660064SFedor Uporov 
2112*ff4fbdf5SAlan Somers 	/* Deleting xattrs must use VOP_DELETEEXTATTR instead */
2113*ff4fbdf5SAlan Somers 	if (ap->a_uio == NULL)
2114*ff4fbdf5SAlan Somers 		return (EINVAL);
2115*ff4fbdf5SAlan Somers 
2116*ff4fbdf5SAlan Somers 	err = extattr_check_cred(vp, ap->a_attrnamespace, cred, td, VWRITE);
2117*ff4fbdf5SAlan Somers 	if (err)
2118*ff4fbdf5SAlan Somers 		return err;
2119*ff4fbdf5SAlan Somers 
212004660064SFedor Uporov 	/* Default to looking for user attributes. */
212104660064SFedor Uporov 	if (ap->a_attrnamespace == EXTATTR_NAMESPACE_SYSTEM)
212204660064SFedor Uporov 		prefix = EXTATTR_NAMESPACE_SYSTEM_STRING;
212304660064SFedor Uporov 	else
212404660064SFedor Uporov 		prefix = EXTATTR_NAMESPACE_USER_STRING;
212504660064SFedor Uporov 
212604660064SFedor Uporov 	len = strlen(prefix) + sizeof(extattr_namespace_separator) +
212704660064SFedor Uporov 	    strlen(ap->a_name) + 1;
212804660064SFedor Uporov 
212904660064SFedor Uporov 	fdisp_init(&fdi, len + sizeof(*set_xattr_in) + uio->uio_resid);
213004660064SFedor Uporov 	fdisp_make_vp(&fdi, FUSE_SETXATTR, vp, td, cred);
213104660064SFedor Uporov 
213204660064SFedor Uporov 	set_xattr_in = fdi.indata;
213304660064SFedor Uporov 	set_xattr_in->size = uio->uio_resid;
213404660064SFedor Uporov 
213504660064SFedor Uporov 	attr_str = (char *)fdi.indata + sizeof(*set_xattr_in);
213604660064SFedor Uporov 	snprintf(attr_str, len, "%s%c%s", prefix, extattr_namespace_separator,
213704660064SFedor Uporov 	    ap->a_name);
213804660064SFedor Uporov 
213904660064SFedor Uporov 	err = uiomove((char *)fdi.indata + sizeof(*set_xattr_in) + len,
214004660064SFedor Uporov 	    uio->uio_resid, uio);
214104660064SFedor Uporov 	if (err != 0) {
214204660064SFedor Uporov 		goto out;
214304660064SFedor Uporov 	}
214404660064SFedor Uporov 
214504660064SFedor Uporov 	err = fdisp_wait_answ(&fdi);
214604660064SFedor Uporov 
214704660064SFedor Uporov 	if (err != 0) {
214804660064SFedor Uporov 		if (err == ENOSYS)
214904660064SFedor Uporov 			fsess_set_notimpl(mp, FUSE_SETXATTR);
215004660064SFedor Uporov 		goto out;
215104660064SFedor Uporov 	}
215204660064SFedor Uporov 
215304660064SFedor Uporov out:
215404660064SFedor Uporov 	fdisp_destroy(&fdi);
215504660064SFedor Uporov 	return (err);
215604660064SFedor Uporov }
215704660064SFedor Uporov 
215804660064SFedor Uporov /*
215904660064SFedor Uporov  * The Linux / FUSE extended attribute list is simply a collection of
216004660064SFedor Uporov  * NUL-terminated strings.  The FreeBSD extended attribute list is a single
216104660064SFedor Uporov  * byte length followed by a non-NUL terminated string.  So, this allows
216204660064SFedor Uporov  * conversion of the Linux / FUSE format to the FreeBSD format in place.
216304660064SFedor Uporov  * Linux attribute names are reported with the namespace as a prefix (e.g.
216404660064SFedor Uporov  * "user.attribute_name"), but in FreeBSD they are reported without the
216504660064SFedor Uporov  * namespace prefix (e.g. "attribute_name").  So, we're going from:
216604660064SFedor Uporov  *
216704660064SFedor Uporov  * user.attr_name1\0user.attr_name2\0
216804660064SFedor Uporov  *
216904660064SFedor Uporov  * to:
217004660064SFedor Uporov  *
217104660064SFedor Uporov  * <num>attr_name1<num>attr_name2
217204660064SFedor Uporov  *
217304660064SFedor Uporov  * Where "<num>" is a single byte number of characters in the attribute name.
217404660064SFedor Uporov  *
217504660064SFedor Uporov  * Args:
217604660064SFedor Uporov  * prefix - exattr namespace prefix string
217704660064SFedor Uporov  * list, list_len - input list with namespace prefixes
217804660064SFedor Uporov  * bsd_list, bsd_list_len - output list compatible with bsd vfs
217904660064SFedor Uporov  */
218004660064SFedor Uporov static int
218104660064SFedor Uporov fuse_xattrlist_convert(char *prefix, const char *list, int list_len,
218204660064SFedor Uporov     char *bsd_list, int *bsd_list_len)
218304660064SFedor Uporov {
218404660064SFedor Uporov 	int len, pos, dist_to_next, prefix_len;
218504660064SFedor Uporov 
218604660064SFedor Uporov 	pos = 0;
218704660064SFedor Uporov 	*bsd_list_len = 0;
218804660064SFedor Uporov 	prefix_len = strlen(prefix);
218904660064SFedor Uporov 
219004660064SFedor Uporov 	while (pos < list_len && list[pos] != '\0') {
219104660064SFedor Uporov 		dist_to_next = strlen(&list[pos]) + 1;
219204660064SFedor Uporov 		if (bcmp(&list[pos], prefix, prefix_len) == 0 &&
219304660064SFedor Uporov 		    list[pos + prefix_len] == extattr_namespace_separator) {
219404660064SFedor Uporov 			len = dist_to_next -
219504660064SFedor Uporov 			    (prefix_len + sizeof(extattr_namespace_separator)) - 1;
219604660064SFedor Uporov 			if (len >= EXTATTR_MAXNAMELEN)
219704660064SFedor Uporov 				return (ENAMETOOLONG);
219804660064SFedor Uporov 
219904660064SFedor Uporov 			bsd_list[*bsd_list_len] = len;
220004660064SFedor Uporov 			memcpy(&bsd_list[*bsd_list_len + 1],
220104660064SFedor Uporov 			    &list[pos + prefix_len +
220204660064SFedor Uporov 			    sizeof(extattr_namespace_separator)], len);
220304660064SFedor Uporov 
220404660064SFedor Uporov 			*bsd_list_len += len + 1;
220504660064SFedor Uporov 		}
220604660064SFedor Uporov 
220704660064SFedor Uporov 		pos += dist_to_next;
220804660064SFedor Uporov 	}
220904660064SFedor Uporov 
221004660064SFedor Uporov 	return (0);
221104660064SFedor Uporov }
221204660064SFedor Uporov 
221304660064SFedor Uporov /*
221404660064SFedor Uporov     struct vop_listextattr_args {
221504660064SFedor Uporov 	struct vop_generic_args a_gen;
221604660064SFedor Uporov 	struct vnode *a_vp;
221704660064SFedor Uporov 	int a_attrnamespace;
221804660064SFedor Uporov 	struct uio *a_uio;
221904660064SFedor Uporov 	size_t *a_size;
222004660064SFedor Uporov 	struct ucred *a_cred;
222104660064SFedor Uporov 	struct thread *a_td;
222204660064SFedor Uporov     };
222304660064SFedor Uporov */
222404660064SFedor Uporov static int
222504660064SFedor Uporov fuse_vnop_listextattr(struct vop_listextattr_args *ap)
222604660064SFedor Uporov {
222704660064SFedor Uporov 	struct vnode *vp = ap->a_vp;
222804660064SFedor Uporov 	struct uio *uio = ap->a_uio;
222928f4f623SFedor Uporov 	struct fuse_dispatcher fdi;
2230493b4a8cSFedor Uporov 	struct fuse_listxattr_in *list_xattr_in;
2231493b4a8cSFedor Uporov 	struct fuse_listxattr_out *list_xattr_out;
223204660064SFedor Uporov 	struct mount *mp = vnode_mount(vp);
223328f4f623SFedor Uporov 	struct thread *td = ap->a_td;
223428f4f623SFedor Uporov 	struct ucred *cred = ap->a_cred;
223504660064SFedor Uporov 	size_t len;
223604660064SFedor Uporov 	char *prefix;
223704660064SFedor Uporov 	char *attr_str;
223804660064SFedor Uporov 	char *bsd_list = NULL;
2239493b4a8cSFedor Uporov 	char *linux_list;
224004660064SFedor Uporov 	int bsd_list_len;
2241493b4a8cSFedor Uporov 	int linux_list_len;
224228f4f623SFedor Uporov 	int err;
224304660064SFedor Uporov 
224404660064SFedor Uporov 	if (fuse_isdeadfs(vp))
224528f4f623SFedor Uporov 		return (ENXIO);
224604660064SFedor Uporov 
2247*ff4fbdf5SAlan Somers 	err = extattr_check_cred(vp, ap->a_attrnamespace, cred, td, VREAD);
2248*ff4fbdf5SAlan Somers 	if (err)
2249*ff4fbdf5SAlan Somers 		return err;
2250*ff4fbdf5SAlan Somers 
225104660064SFedor Uporov 	/*
225204660064SFedor Uporov 	 * Add space for a NUL and the period separator if enabled.
225304660064SFedor Uporov 	 * Default to looking for user attributes.
225404660064SFedor Uporov 	 */
225504660064SFedor Uporov 	if (ap->a_attrnamespace == EXTATTR_NAMESPACE_SYSTEM)
225604660064SFedor Uporov 		prefix = EXTATTR_NAMESPACE_SYSTEM_STRING;
225704660064SFedor Uporov 	else
225804660064SFedor Uporov 		prefix = EXTATTR_NAMESPACE_USER_STRING;
225904660064SFedor Uporov 
226004660064SFedor Uporov 	len = strlen(prefix) + sizeof(extattr_namespace_separator) + 1;
226104660064SFedor Uporov 
2262493b4a8cSFedor Uporov 	fdisp_init(&fdi, sizeof(*list_xattr_in) + len);
226304660064SFedor Uporov 	fdisp_make_vp(&fdi, FUSE_LISTXATTR, vp, td, cred);
226404660064SFedor Uporov 
2265493b4a8cSFedor Uporov 	/*
2266493b4a8cSFedor Uporov 	 * Retrieve Linux / FUSE compatible list size.
2267493b4a8cSFedor Uporov 	 */
2268493b4a8cSFedor Uporov 	list_xattr_in = fdi.indata;
2269493b4a8cSFedor Uporov 	list_xattr_in->size = 0;
2270493b4a8cSFedor Uporov 	attr_str = (char *)fdi.indata + sizeof(*list_xattr_in);
227104660064SFedor Uporov 	snprintf(attr_str, len, "%s%c", prefix, extattr_namespace_separator);
227204660064SFedor Uporov 
227304660064SFedor Uporov 	err = fdisp_wait_answ(&fdi);
227404660064SFedor Uporov 	if (err != 0) {
227504660064SFedor Uporov 		if (err == ENOSYS)
227604660064SFedor Uporov 			fsess_set_notimpl(mp, FUSE_LISTXATTR);
227704660064SFedor Uporov 		goto out;
227804660064SFedor Uporov 	}
227904660064SFedor Uporov 
2280493b4a8cSFedor Uporov 	list_xattr_out = fdi.answ;
2281493b4a8cSFedor Uporov 	linux_list_len = list_xattr_out->size;
2282493b4a8cSFedor Uporov 	if (linux_list_len == 0) {
2283493b4a8cSFedor Uporov 		if (ap->a_size != NULL)
2284493b4a8cSFedor Uporov 			*ap->a_size = linux_list_len;
228504660064SFedor Uporov 		goto out;
228604660064SFedor Uporov 	}
228704660064SFedor Uporov 
228804660064SFedor Uporov 	/*
2289493b4a8cSFedor Uporov 	 * Retrieve Linux / FUSE compatible list values.
2290493b4a8cSFedor Uporov 	 */
229112292a99SAlan Somers 	fdisp_refresh_vp(&fdi, FUSE_LISTXATTR, vp, td, cred);
2292493b4a8cSFedor Uporov 	list_xattr_in = fdi.indata;
2293493b4a8cSFedor Uporov 	list_xattr_in->size = linux_list_len + sizeof(*list_xattr_out);
229412292a99SAlan Somers 	list_xattr_in->flags = 0;
2295493b4a8cSFedor Uporov 	attr_str = (char *)fdi.indata + sizeof(*list_xattr_in);
2296493b4a8cSFedor Uporov 	snprintf(attr_str, len, "%s%c", prefix, extattr_namespace_separator);
2297493b4a8cSFedor Uporov 
2298493b4a8cSFedor Uporov 	err = fdisp_wait_answ(&fdi);
2299493b4a8cSFedor Uporov 	if (err != 0)
2300493b4a8cSFedor Uporov 		goto out;
2301493b4a8cSFedor Uporov 
2302493b4a8cSFedor Uporov 	linux_list = fdi.answ;
2303493b4a8cSFedor Uporov 	linux_list_len = fdi.iosize;
2304493b4a8cSFedor Uporov 
2305493b4a8cSFedor Uporov 	/*
2306493b4a8cSFedor Uporov 	 * Retrieve the BSD compatible list values.
230704660064SFedor Uporov 	 * The Linux / FUSE attribute list format isn't the same
230804660064SFedor Uporov 	 * as FreeBSD's format. So we need to transform it into
230904660064SFedor Uporov 	 * FreeBSD's format before giving it to the user.
231004660064SFedor Uporov 	 */
2311493b4a8cSFedor Uporov 	bsd_list = malloc(linux_list_len, M_TEMP, M_WAITOK);
2312493b4a8cSFedor Uporov 	err = fuse_xattrlist_convert(prefix, linux_list, linux_list_len,
231304660064SFedor Uporov 	    bsd_list, &bsd_list_len);
231404660064SFedor Uporov 	if (err != 0)
231504660064SFedor Uporov 		goto out;
231604660064SFedor Uporov 
2317493b4a8cSFedor Uporov 	if (ap->a_size != NULL)
2318493b4a8cSFedor Uporov 		*ap->a_size = bsd_list_len;
2319493b4a8cSFedor Uporov 
2320493b4a8cSFedor Uporov 	if (uio != NULL)
232104660064SFedor Uporov 		err = uiomove(bsd_list, bsd_list_len, uio);
232204660064SFedor Uporov 
232304660064SFedor Uporov out:
232404660064SFedor Uporov 	free(bsd_list, M_TEMP);
232504660064SFedor Uporov 	fdisp_destroy(&fdi);
232604660064SFedor Uporov 	return (err);
232704660064SFedor Uporov }
232804660064SFedor Uporov 
232904660064SFedor Uporov /*
233004660064SFedor Uporov     struct vop_deleteextattr_args {
233104660064SFedor Uporov 	struct vop_generic_args a_gen;
233204660064SFedor Uporov 	struct vnode *a_vp;
233304660064SFedor Uporov 	int a_attrnamespace;
233404660064SFedor Uporov 	const char *a_name;
233504660064SFedor Uporov 	struct ucred *a_cred;
233604660064SFedor Uporov 	struct thread *a_td;
233704660064SFedor Uporov     };
233804660064SFedor Uporov */
233904660064SFedor Uporov static int
234004660064SFedor Uporov fuse_vnop_deleteextattr(struct vop_deleteextattr_args *ap)
234104660064SFedor Uporov {
234204660064SFedor Uporov 	struct vnode *vp = ap->a_vp;
234328f4f623SFedor Uporov 	struct fuse_dispatcher fdi;
234404660064SFedor Uporov 	struct mount *mp = vnode_mount(vp);
234528f4f623SFedor Uporov 	struct thread *td = ap->a_td;
234628f4f623SFedor Uporov 	struct ucred *cred = ap->a_cred;
234704660064SFedor Uporov 	char *prefix;
234804660064SFedor Uporov 	size_t len;
234904660064SFedor Uporov 	char *attr_str;
235004660064SFedor Uporov 	int err;
235104660064SFedor Uporov 
235204660064SFedor Uporov 	if (fuse_isdeadfs(vp))
235328f4f623SFedor Uporov 		return (ENXIO);
235404660064SFedor Uporov 
2355*ff4fbdf5SAlan Somers 	err = extattr_check_cred(vp, ap->a_attrnamespace, cred, td, VWRITE);
2356*ff4fbdf5SAlan Somers 	if (err)
2357*ff4fbdf5SAlan Somers 		return err;
2358*ff4fbdf5SAlan Somers 
235904660064SFedor Uporov 	/* Default to looking for user attributes. */
236004660064SFedor Uporov 	if (ap->a_attrnamespace == EXTATTR_NAMESPACE_SYSTEM)
236104660064SFedor Uporov 		prefix = EXTATTR_NAMESPACE_SYSTEM_STRING;
236204660064SFedor Uporov 	else
236304660064SFedor Uporov 		prefix = EXTATTR_NAMESPACE_USER_STRING;
236404660064SFedor Uporov 
236504660064SFedor Uporov 	len = strlen(prefix) + sizeof(extattr_namespace_separator) +
236604660064SFedor Uporov 	    strlen(ap->a_name) + 1;
236704660064SFedor Uporov 
236804660064SFedor Uporov 	fdisp_init(&fdi, len);
236904660064SFedor Uporov 	fdisp_make_vp(&fdi, FUSE_REMOVEXATTR, vp, td, cred);
237004660064SFedor Uporov 
237104660064SFedor Uporov 	attr_str = fdi.indata;
237204660064SFedor Uporov 	snprintf(attr_str, len, "%s%c%s", prefix, extattr_namespace_separator,
237304660064SFedor Uporov 	    ap->a_name);
237404660064SFedor Uporov 
237504660064SFedor Uporov 	err = fdisp_wait_answ(&fdi);
237604660064SFedor Uporov 	if (err != 0) {
237704660064SFedor Uporov 		if (err == ENOSYS)
237804660064SFedor Uporov 			fsess_set_notimpl(mp, FUSE_REMOVEXATTR);
237904660064SFedor Uporov 	}
238004660064SFedor Uporov 
238104660064SFedor Uporov 	fdisp_destroy(&fdi);
238204660064SFedor Uporov 	return (err);
238304660064SFedor Uporov }
238404660064SFedor Uporov 
23855fe58019SAttilio Rao /*
23865fe58019SAttilio Rao     struct vnop_print_args {
23875fe58019SAttilio Rao 	struct vnode *a_vp;
23885fe58019SAttilio Rao     };
23895fe58019SAttilio Rao */
23905fe58019SAttilio Rao static int
23915fe58019SAttilio Rao fuse_vnop_print(struct vop_print_args *ap)
23925fe58019SAttilio Rao {
23935fe58019SAttilio Rao 	struct fuse_vnode_data *fvdat = VTOFUD(ap->a_vp);
23945fe58019SAttilio Rao 
23955fe58019SAttilio Rao 	printf("nodeid: %ju, parent nodeid: %ju, nlookup: %ju, flag: %#x\n",
23965fe58019SAttilio Rao 	    (uintmax_t)VTOILLU(ap->a_vp), (uintmax_t)fvdat->parent_nid,
23975fe58019SAttilio Rao 	    (uintmax_t)fvdat->nlookup,
23985fe58019SAttilio Rao 	    fvdat->flag);
23995fe58019SAttilio Rao 
24005fe58019SAttilio Rao 	return 0;
24015fe58019SAttilio Rao }
2402