xref: /freebsd/sys/fs/fuse/fuse_vnops.c (revision 89f6b8632cc94bca2738b4fcc26e1189ef4f5dde)
15fe58019SAttilio Rao /*
25fe58019SAttilio Rao  * Copyright (c) 2007-2009 Google Inc. and Amit Singh
35fe58019SAttilio Rao  * All rights reserved.
45fe58019SAttilio Rao  *
55fe58019SAttilio Rao  * Redistribution and use in source and binary forms, with or without
65fe58019SAttilio Rao  * modification, are permitted provided that the following conditions are
75fe58019SAttilio Rao  * met:
85fe58019SAttilio Rao  *
95fe58019SAttilio Rao  * * Redistributions of source code must retain the above copyright
105fe58019SAttilio Rao  *   notice, this list of conditions and the following disclaimer.
115fe58019SAttilio Rao  * * Redistributions in binary form must reproduce the above
125fe58019SAttilio Rao  *   copyright notice, this list of conditions and the following disclaimer
135fe58019SAttilio Rao  *   in the documentation and/or other materials provided with the
145fe58019SAttilio Rao  *   distribution.
155fe58019SAttilio Rao  * * Neither the name of Google Inc. nor the names of its
165fe58019SAttilio Rao  *   contributors may be used to endorse or promote products derived from
175fe58019SAttilio Rao  *   this software without specific prior written permission.
185fe58019SAttilio Rao  *
195fe58019SAttilio Rao  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
205fe58019SAttilio Rao  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
215fe58019SAttilio Rao  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
225fe58019SAttilio Rao  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
235fe58019SAttilio Rao  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
245fe58019SAttilio Rao  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
255fe58019SAttilio Rao  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
265fe58019SAttilio Rao  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
275fe58019SAttilio Rao  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
285fe58019SAttilio Rao  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
295fe58019SAttilio Rao  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
305fe58019SAttilio Rao  *
315fe58019SAttilio Rao  * Copyright (C) 2005 Csaba Henk.
325fe58019SAttilio Rao  * All rights reserved.
335fe58019SAttilio Rao  *
345fe58019SAttilio Rao  * Redistribution and use in source and binary forms, with or without
355fe58019SAttilio Rao  * modification, are permitted provided that the following conditions
365fe58019SAttilio Rao  * are met:
375fe58019SAttilio Rao  * 1. Redistributions of source code must retain the above copyright
385fe58019SAttilio Rao  *    notice, this list of conditions and the following disclaimer.
395fe58019SAttilio Rao  * 2. Redistributions in binary form must reproduce the above copyright
405fe58019SAttilio Rao  *    notice, this list of conditions and the following disclaimer in the
415fe58019SAttilio Rao  *    documentation and/or other materials provided with the distribution.
425fe58019SAttilio Rao  *
435fe58019SAttilio Rao  * THIS SOFTWARE IS PROVIDED BY AUTHOR AND CONTRIBUTORS ``AS IS'' AND
445fe58019SAttilio Rao  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
455fe58019SAttilio Rao  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
465fe58019SAttilio Rao  * ARE DISCLAIMED.  IN NO EVENT SHALL AUTHOR OR CONTRIBUTORS BE LIABLE
475fe58019SAttilio Rao  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
485fe58019SAttilio Rao  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
495fe58019SAttilio Rao  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
505fe58019SAttilio Rao  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
515fe58019SAttilio Rao  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
525fe58019SAttilio Rao  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
535fe58019SAttilio Rao  * SUCH DAMAGE.
545fe58019SAttilio Rao  */
555fe58019SAttilio Rao 
565fe58019SAttilio Rao #include <sys/cdefs.h>
575fe58019SAttilio Rao __FBSDID("$FreeBSD$");
585fe58019SAttilio Rao 
595fe58019SAttilio Rao #include <sys/types.h>
605fe58019SAttilio Rao #include <sys/module.h>
615fe58019SAttilio Rao #include <sys/systm.h>
625fe58019SAttilio Rao #include <sys/errno.h>
635fe58019SAttilio Rao #include <sys/param.h>
645fe58019SAttilio Rao #include <sys/kernel.h>
655fe58019SAttilio Rao #include <sys/conf.h>
665fe58019SAttilio Rao #include <sys/uio.h>
675fe58019SAttilio Rao #include <sys/malloc.h>
685fe58019SAttilio Rao #include <sys/queue.h>
695fe58019SAttilio Rao #include <sys/lock.h>
70*89f6b863SAttilio Rao #include <sys/rwlock.h>
715fe58019SAttilio Rao #include <sys/sx.h>
725fe58019SAttilio Rao #include <sys/proc.h>
735fe58019SAttilio Rao #include <sys/mount.h>
745fe58019SAttilio Rao #include <sys/vnode.h>
755fe58019SAttilio Rao #include <sys/namei.h>
765fe58019SAttilio Rao #include <sys/stat.h>
775fe58019SAttilio Rao #include <sys/unistd.h>
785fe58019SAttilio Rao #include <sys/filedesc.h>
795fe58019SAttilio Rao #include <sys/file.h>
805fe58019SAttilio Rao #include <sys/fcntl.h>
815fe58019SAttilio Rao #include <sys/dirent.h>
825fe58019SAttilio Rao #include <sys/bio.h>
835fe58019SAttilio Rao #include <sys/buf.h>
845fe58019SAttilio Rao #include <sys/sysctl.h>
855fe58019SAttilio Rao 
865fe58019SAttilio Rao #include <vm/vm.h>
875fe58019SAttilio Rao #include <vm/vm_extern.h>
885fe58019SAttilio Rao #include <vm/pmap.h>
895fe58019SAttilio Rao #include <vm/vm_map.h>
905fe58019SAttilio Rao #include <vm/vm_page.h>
915fe58019SAttilio Rao #include <vm/vm_param.h>
925fe58019SAttilio Rao #include <vm/vm_object.h>
935fe58019SAttilio Rao #include <vm/vm_pager.h>
945fe58019SAttilio Rao #include <vm/vnode_pager.h>
955fe58019SAttilio Rao #include <vm/vm_object.h>
965fe58019SAttilio Rao 
975fe58019SAttilio Rao #include "fuse.h"
985fe58019SAttilio Rao #include "fuse_file.h"
995fe58019SAttilio Rao #include "fuse_internal.h"
1005fe58019SAttilio Rao #include "fuse_ipc.h"
1015fe58019SAttilio Rao #include "fuse_node.h"
1025fe58019SAttilio Rao #include "fuse_param.h"
1035fe58019SAttilio Rao #include "fuse_io.h"
1045fe58019SAttilio Rao 
1055fe58019SAttilio Rao #include <sys/priv.h>
1065fe58019SAttilio Rao 
1075fe58019SAttilio Rao #define FUSE_DEBUG_MODULE VNOPS
1085fe58019SAttilio Rao #include "fuse_debug.h"
1095fe58019SAttilio Rao 
1105fe58019SAttilio Rao /* vnode ops */
1115fe58019SAttilio Rao static vop_access_t fuse_vnop_access;
1125fe58019SAttilio Rao static vop_close_t fuse_vnop_close;
1135fe58019SAttilio Rao static vop_create_t fuse_vnop_create;
1145fe58019SAttilio Rao static vop_fsync_t fuse_vnop_fsync;
1155fe58019SAttilio Rao static vop_getattr_t fuse_vnop_getattr;
1165fe58019SAttilio Rao static vop_inactive_t fuse_vnop_inactive;
1175fe58019SAttilio Rao static vop_link_t fuse_vnop_link;
1185fe58019SAttilio Rao static vop_lookup_t fuse_vnop_lookup;
1195fe58019SAttilio Rao static vop_mkdir_t fuse_vnop_mkdir;
1205fe58019SAttilio Rao static vop_mknod_t fuse_vnop_mknod;
1215fe58019SAttilio Rao static vop_open_t fuse_vnop_open;
1225fe58019SAttilio Rao static vop_read_t fuse_vnop_read;
1235fe58019SAttilio Rao static vop_readdir_t fuse_vnop_readdir;
1245fe58019SAttilio Rao static vop_readlink_t fuse_vnop_readlink;
1255fe58019SAttilio Rao static vop_reclaim_t fuse_vnop_reclaim;
1265fe58019SAttilio Rao static vop_remove_t fuse_vnop_remove;
1275fe58019SAttilio Rao static vop_rename_t fuse_vnop_rename;
1285fe58019SAttilio Rao static vop_rmdir_t fuse_vnop_rmdir;
1295fe58019SAttilio Rao static vop_setattr_t fuse_vnop_setattr;
1305fe58019SAttilio Rao static vop_strategy_t fuse_vnop_strategy;
1315fe58019SAttilio Rao static vop_symlink_t fuse_vnop_symlink;
1325fe58019SAttilio Rao static vop_write_t fuse_vnop_write;
1335fe58019SAttilio Rao static vop_getpages_t fuse_vnop_getpages;
1345fe58019SAttilio Rao static vop_putpages_t fuse_vnop_putpages;
1355fe58019SAttilio Rao static vop_print_t fuse_vnop_print;
1365fe58019SAttilio Rao 
1375fe58019SAttilio Rao struct vop_vector fuse_vnops = {
1385fe58019SAttilio Rao 	.vop_default = &default_vnodeops,
1395fe58019SAttilio Rao 	.vop_access = fuse_vnop_access,
1405fe58019SAttilio Rao 	.vop_close = fuse_vnop_close,
1415fe58019SAttilio Rao 	.vop_create = fuse_vnop_create,
1425fe58019SAttilio Rao 	.vop_fsync = fuse_vnop_fsync,
1435fe58019SAttilio Rao 	.vop_getattr = fuse_vnop_getattr,
1445fe58019SAttilio Rao 	.vop_inactive = fuse_vnop_inactive,
1455fe58019SAttilio Rao 	.vop_link = fuse_vnop_link,
1465fe58019SAttilio Rao 	.vop_lookup = fuse_vnop_lookup,
1475fe58019SAttilio Rao 	.vop_mkdir = fuse_vnop_mkdir,
1485fe58019SAttilio Rao 	.vop_mknod = fuse_vnop_mknod,
1495fe58019SAttilio Rao 	.vop_open = fuse_vnop_open,
1505fe58019SAttilio Rao 	.vop_pathconf = vop_stdpathconf,
1515fe58019SAttilio Rao 	.vop_read = fuse_vnop_read,
1525fe58019SAttilio Rao 	.vop_readdir = fuse_vnop_readdir,
1535fe58019SAttilio Rao 	.vop_readlink = fuse_vnop_readlink,
1545fe58019SAttilio Rao 	.vop_reclaim = fuse_vnop_reclaim,
1555fe58019SAttilio Rao 	.vop_remove = fuse_vnop_remove,
1565fe58019SAttilio Rao 	.vop_rename = fuse_vnop_rename,
1575fe58019SAttilio Rao 	.vop_rmdir = fuse_vnop_rmdir,
1585fe58019SAttilio Rao 	.vop_setattr = fuse_vnop_setattr,
1595fe58019SAttilio Rao 	.vop_strategy = fuse_vnop_strategy,
1605fe58019SAttilio Rao 	.vop_symlink = fuse_vnop_symlink,
1615fe58019SAttilio Rao 	.vop_write = fuse_vnop_write,
1625fe58019SAttilio Rao 	.vop_getpages = fuse_vnop_getpages,
1635fe58019SAttilio Rao 	.vop_putpages = fuse_vnop_putpages,
1645fe58019SAttilio Rao 	.vop_print = fuse_vnop_print,
1655fe58019SAttilio Rao };
1665fe58019SAttilio Rao 
1675fe58019SAttilio Rao static u_long fuse_lookup_cache_hits = 0;
1685fe58019SAttilio Rao 
1695fe58019SAttilio Rao SYSCTL_ULONG(_vfs_fuse, OID_AUTO, lookup_cache_hits, CTLFLAG_RD,
1705fe58019SAttilio Rao     &fuse_lookup_cache_hits, 0, "");
1715fe58019SAttilio Rao 
1725fe58019SAttilio Rao static u_long fuse_lookup_cache_misses = 0;
1735fe58019SAttilio Rao 
1745fe58019SAttilio Rao SYSCTL_ULONG(_vfs_fuse, OID_AUTO, lookup_cache_misses, CTLFLAG_RD,
1755fe58019SAttilio Rao     &fuse_lookup_cache_misses, 0, "");
1765fe58019SAttilio Rao 
1775fe58019SAttilio Rao int	fuse_lookup_cache_enable = 1;
1785fe58019SAttilio Rao 
1795fe58019SAttilio Rao SYSCTL_INT(_vfs_fuse, OID_AUTO, lookup_cache_enable, CTLFLAG_RW,
1805fe58019SAttilio Rao     &fuse_lookup_cache_enable, 0, "");
1815fe58019SAttilio Rao 
1825fe58019SAttilio Rao /*
1835fe58019SAttilio Rao  * XXX: This feature is highly experimental and can bring to instabilities,
1845fe58019SAttilio Rao  * needs revisiting before to be enabled by default.
1855fe58019SAttilio Rao  */
1865fe58019SAttilio Rao static int fuse_reclaim_revoked = 0;
1875fe58019SAttilio Rao 
1885fe58019SAttilio Rao SYSCTL_INT(_vfs_fuse, OID_AUTO, reclaim_revoked, CTLFLAG_RW,
1895fe58019SAttilio Rao     &fuse_reclaim_revoked, 0, "");
1905fe58019SAttilio Rao 
1915fe58019SAttilio Rao int	fuse_pbuf_freecnt = -1;
1925fe58019SAttilio Rao 
1935fe58019SAttilio Rao #define fuse_vm_page_lock(m)		vm_page_lock((m));
1945fe58019SAttilio Rao #define fuse_vm_page_unlock(m)		vm_page_unlock((m));
1955fe58019SAttilio Rao #define fuse_vm_page_lock_queues()	((void)0)
1965fe58019SAttilio Rao #define fuse_vm_page_unlock_queues()	((void)0)
1975fe58019SAttilio Rao 
1985fe58019SAttilio Rao /*
1995fe58019SAttilio Rao     struct vnop_access_args {
2005fe58019SAttilio Rao         struct vnode *a_vp;
2015fe58019SAttilio Rao #if VOP_ACCESS_TAKES_ACCMODE_T
2025fe58019SAttilio Rao         accmode_t a_accmode;
2035fe58019SAttilio Rao #else
2045fe58019SAttilio Rao         int a_mode;
2055fe58019SAttilio Rao #endif
2065fe58019SAttilio Rao         struct ucred *a_cred;
2075fe58019SAttilio Rao         struct thread *a_td;
2085fe58019SAttilio Rao     };
2095fe58019SAttilio Rao */
2105fe58019SAttilio Rao static int
2115fe58019SAttilio Rao fuse_vnop_access(struct vop_access_args *ap)
2125fe58019SAttilio Rao {
2135fe58019SAttilio Rao 	struct vnode *vp = ap->a_vp;
2145fe58019SAttilio Rao 	int accmode = ap->a_accmode;
2155fe58019SAttilio Rao 	struct ucred *cred = ap->a_cred;
2165fe58019SAttilio Rao 
2175fe58019SAttilio Rao 	struct fuse_access_param facp;
2185fe58019SAttilio Rao 	struct fuse_data *data = fuse_get_mpdata(vnode_mount(vp));
2195fe58019SAttilio Rao 
2205fe58019SAttilio Rao 	int err;
2215fe58019SAttilio Rao 
2224cff153bSAttilio Rao 	FS_DEBUG2G("inode=%ju\n", (uintmax_t)VTOI(vp));
2235fe58019SAttilio Rao 
2245fe58019SAttilio Rao 	if (fuse_isdeadfs(vp)) {
2255fe58019SAttilio Rao 		if (vnode_isvroot(vp)) {
2265fe58019SAttilio Rao 			return 0;
2275fe58019SAttilio Rao 		}
2285fe58019SAttilio Rao 		return ENXIO;
2295fe58019SAttilio Rao 	}
2305fe58019SAttilio Rao 	if (!(data->dataflags & FSESS_INITED)) {
2315fe58019SAttilio Rao 		if (vnode_isvroot(vp)) {
2325fe58019SAttilio Rao 			if (priv_check_cred(cred, PRIV_VFS_ADMIN, 0) ||
2335fe58019SAttilio Rao 			    (fuse_match_cred(data->daemoncred, cred) == 0)) {
2345fe58019SAttilio Rao 				return 0;
2355fe58019SAttilio Rao 			}
2365fe58019SAttilio Rao 		}
2375fe58019SAttilio Rao 		return EBADF;
2385fe58019SAttilio Rao 	}
2395fe58019SAttilio Rao 	if (vnode_islnk(vp)) {
2405fe58019SAttilio Rao 		return 0;
2415fe58019SAttilio Rao 	}
2425fe58019SAttilio Rao 	bzero(&facp, sizeof(facp));
2435fe58019SAttilio Rao 
2445fe58019SAttilio Rao 	err = fuse_internal_access(vp, accmode, &facp, ap->a_td, ap->a_cred);
2454cff153bSAttilio Rao 	FS_DEBUG2G("err=%d accmode=0x%x\n", err, accmode);
2465fe58019SAttilio Rao 	return err;
2475fe58019SAttilio Rao }
2485fe58019SAttilio Rao 
2495fe58019SAttilio Rao /*
2505fe58019SAttilio Rao     struct vnop_close_args {
2515fe58019SAttilio Rao 	struct vnode *a_vp;
2525fe58019SAttilio Rao 	int  a_fflag;
2535fe58019SAttilio Rao 	struct ucred *a_cred;
2545fe58019SAttilio Rao 	struct thread *a_td;
2555fe58019SAttilio Rao     };
2565fe58019SAttilio Rao */
2575fe58019SAttilio Rao static int
2585fe58019SAttilio Rao fuse_vnop_close(struct vop_close_args *ap)
2595fe58019SAttilio Rao {
2605fe58019SAttilio Rao 	struct vnode *vp = ap->a_vp;
2615fe58019SAttilio Rao 	struct ucred *cred = ap->a_cred;
2625fe58019SAttilio Rao 	int fflag = ap->a_fflag;
2635fe58019SAttilio Rao 	fufh_type_t fufh_type;
2645fe58019SAttilio Rao 
2655fe58019SAttilio Rao 	fuse_trace_printf_vnop();
2665fe58019SAttilio Rao 
2675fe58019SAttilio Rao 	if (fuse_isdeadfs(vp)) {
2685fe58019SAttilio Rao 		return 0;
2695fe58019SAttilio Rao 	}
2705fe58019SAttilio Rao 	if (vnode_isdir(vp)) {
2715fe58019SAttilio Rao 		if (fuse_filehandle_valid(vp, FUFH_RDONLY)) {
2725fe58019SAttilio Rao 			fuse_filehandle_close(vp, FUFH_RDONLY, NULL, cred);
2735fe58019SAttilio Rao 		}
2745fe58019SAttilio Rao 		return 0;
2755fe58019SAttilio Rao 	}
2765fe58019SAttilio Rao 	if (fflag & IO_NDELAY) {
2775fe58019SAttilio Rao 		return 0;
2785fe58019SAttilio Rao 	}
2795fe58019SAttilio Rao 	fufh_type = fuse_filehandle_xlate_from_fflags(fflag);
2805fe58019SAttilio Rao 
2815fe58019SAttilio Rao 	if (!fuse_filehandle_valid(vp, fufh_type)) {
2825fe58019SAttilio Rao 		int i;
2835fe58019SAttilio Rao 
2845fe58019SAttilio Rao 		for (i = 0; i < FUFH_MAXTYPE; i++)
2855fe58019SAttilio Rao 			if (fuse_filehandle_valid(vp, i))
2865fe58019SAttilio Rao 				break;
2875fe58019SAttilio Rao 		if (i == FUFH_MAXTYPE)
2885fe58019SAttilio Rao 			panic("FUSE: fufh type %d found to be invalid in close"
2895fe58019SAttilio Rao 			      " (fflag=0x%x)\n",
2905fe58019SAttilio Rao 			      fufh_type, fflag);
2915fe58019SAttilio Rao 	}
2925fe58019SAttilio Rao 	if ((VTOFUD(vp)->flag & FN_SIZECHANGE) != 0) {
2935fe58019SAttilio Rao 		fuse_vnode_savesize(vp, cred);
2945fe58019SAttilio Rao 	}
2955fe58019SAttilio Rao 	return 0;
2965fe58019SAttilio Rao }
2975fe58019SAttilio Rao 
2985fe58019SAttilio Rao /*
2995fe58019SAttilio Rao     struct vnop_create_args {
3005fe58019SAttilio Rao 	struct vnode *a_dvp;
3015fe58019SAttilio Rao 	struct vnode **a_vpp;
3025fe58019SAttilio Rao 	struct componentname *a_cnp;
3035fe58019SAttilio Rao 	struct vattr *a_vap;
3045fe58019SAttilio Rao     };
3055fe58019SAttilio Rao */
3065fe58019SAttilio Rao static int
3075fe58019SAttilio Rao fuse_vnop_create(struct vop_create_args *ap)
3085fe58019SAttilio Rao {
3095fe58019SAttilio Rao 	struct vnode *dvp = ap->a_dvp;
3105fe58019SAttilio Rao 	struct vnode **vpp = ap->a_vpp;
3115fe58019SAttilio Rao 	struct componentname *cnp = ap->a_cnp;
3125fe58019SAttilio Rao 	struct vattr *vap = ap->a_vap;
3135fe58019SAttilio Rao 	struct thread *td = cnp->cn_thread;
3145fe58019SAttilio Rao 	struct ucred *cred = cnp->cn_cred;
3155fe58019SAttilio Rao 
3165fe58019SAttilio Rao 	struct fuse_open_in *foi;
3175fe58019SAttilio Rao 	struct fuse_entry_out *feo;
3185fe58019SAttilio Rao 	struct fuse_dispatcher fdi;
3195fe58019SAttilio Rao 	struct fuse_dispatcher *fdip = &fdi;
3205fe58019SAttilio Rao 
3215fe58019SAttilio Rao 	int err;
3225fe58019SAttilio Rao 
3235fe58019SAttilio Rao 	struct mount *mp = vnode_mount(dvp);
3245fe58019SAttilio Rao 	uint64_t parentnid = VTOFUD(dvp)->nid;
3255fe58019SAttilio Rao 	mode_t mode = MAKEIMODE(vap->va_type, vap->va_mode);
3265fe58019SAttilio Rao 	uint64_t x_fh_id;
3275fe58019SAttilio Rao 	uint32_t x_open_flags;
3285fe58019SAttilio Rao 
3295fe58019SAttilio Rao 	fuse_trace_printf_vnop();
3305fe58019SAttilio Rao 
3315fe58019SAttilio Rao 	if (fuse_isdeadfs(dvp)) {
3325fe58019SAttilio Rao 		return ENXIO;
3335fe58019SAttilio Rao 	}
3345fe58019SAttilio Rao 	bzero(&fdi, sizeof(fdi));
3355fe58019SAttilio Rao 
3365fe58019SAttilio Rao 	/* XXX:	Will we ever want devices ? */
3375fe58019SAttilio Rao 	if ((vap->va_type != VREG)) {
3385fe58019SAttilio Rao 		MPASS(vap->va_type != VFIFO);
3395fe58019SAttilio Rao 		goto bringup;
3405fe58019SAttilio Rao 	}
3415fe58019SAttilio Rao 	debug_printf("parent nid = %ju, mode = %x\n", (uintmax_t)parentnid,
3425fe58019SAttilio Rao 	    mode);
3435fe58019SAttilio Rao 
3445fe58019SAttilio Rao 	fdisp_init(fdip, sizeof(*foi) + cnp->cn_namelen + 1);
3455fe58019SAttilio Rao 	if (!fsess_isimpl(mp, FUSE_CREATE)) {
3465fe58019SAttilio Rao 		debug_printf("eh, daemon doesn't implement create?\n");
3475fe58019SAttilio Rao 		return (EINVAL);
3485fe58019SAttilio Rao 	}
3495fe58019SAttilio Rao 	fdisp_make(fdip, FUSE_CREATE, vnode_mount(dvp), parentnid, td, cred);
3505fe58019SAttilio Rao 
3515fe58019SAttilio Rao 	foi = fdip->indata;
3525fe58019SAttilio Rao 	foi->mode = mode;
3535fe58019SAttilio Rao 	foi->flags = O_CREAT | O_RDWR;
3545fe58019SAttilio Rao 
3555fe58019SAttilio Rao 	memcpy((char *)fdip->indata + sizeof(*foi), cnp->cn_nameptr,
3565fe58019SAttilio Rao 	    cnp->cn_namelen);
3575fe58019SAttilio Rao 	((char *)fdip->indata)[sizeof(*foi) + cnp->cn_namelen] = '\0';
3585fe58019SAttilio Rao 
3595fe58019SAttilio Rao 	err = fdisp_wait_answ(fdip);
3605fe58019SAttilio Rao 
3616de3b00dSAttilio Rao 	if (err) {
3626de3b00dSAttilio Rao 		if (err == ENOSYS)
3635fe58019SAttilio Rao 			fsess_set_notimpl(mp, FUSE_CREATE);
3646de3b00dSAttilio Rao 		debug_printf("create: got err=%d from daemon\n", err);
3655fe58019SAttilio Rao 		goto out;
3665fe58019SAttilio Rao 	}
3675fe58019SAttilio Rao bringup:
3685fe58019SAttilio Rao 	feo = fdip->answ;
3695fe58019SAttilio Rao 
3705fe58019SAttilio Rao 	if ((err = fuse_internal_checkentry(feo, VREG))) {
3715fe58019SAttilio Rao 		goto out;
3725fe58019SAttilio Rao 	}
3735fe58019SAttilio Rao 	err = fuse_vnode_get(mp, feo->nodeid, dvp, vpp, cnp, VREG);
3745fe58019SAttilio Rao 	if (err) {
3755fe58019SAttilio Rao 		struct fuse_release_in *fri;
3765fe58019SAttilio Rao 		uint64_t nodeid = feo->nodeid;
3775fe58019SAttilio Rao 		uint64_t fh_id = ((struct fuse_open_out *)(feo + 1))->fh;
3785fe58019SAttilio Rao 
3795fe58019SAttilio Rao 		fdisp_init(fdip, sizeof(*fri));
3805fe58019SAttilio Rao 		fdisp_make(fdip, FUSE_RELEASE, mp, nodeid, td, cred);
3815fe58019SAttilio Rao 		fri = fdip->indata;
3825fe58019SAttilio Rao 		fri->fh = fh_id;
3835fe58019SAttilio Rao 		fri->flags = OFLAGS(mode);
3845fe58019SAttilio Rao 		fuse_insert_callback(fdip->tick, fuse_internal_forget_callback);
3855fe58019SAttilio Rao 		fuse_insert_message(fdip->tick);
3865fe58019SAttilio Rao 		return err;
3875fe58019SAttilio Rao 	}
3885fe58019SAttilio Rao 	ASSERT_VOP_ELOCKED(*vpp, "fuse_vnop_create");
3895fe58019SAttilio Rao 
3905fe58019SAttilio Rao 	fdip->answ = feo + 1;
3915fe58019SAttilio Rao 
3925fe58019SAttilio Rao 	x_fh_id = ((struct fuse_open_out *)(feo + 1))->fh;
3935fe58019SAttilio Rao 	x_open_flags = ((struct fuse_open_out *)(feo + 1))->open_flags;
3945fe58019SAttilio Rao 	fuse_filehandle_init(*vpp, FUFH_RDWR, NULL, x_fh_id);
3955fe58019SAttilio Rao 	fuse_vnode_open(*vpp, x_open_flags, td);
3965fe58019SAttilio Rao 	cache_purge_negative(dvp);
3975fe58019SAttilio Rao 
3985fe58019SAttilio Rao out:
3995fe58019SAttilio Rao 	fdisp_destroy(fdip);
4005fe58019SAttilio Rao 	return err;
4015fe58019SAttilio Rao }
4025fe58019SAttilio Rao 
4035fe58019SAttilio Rao /*
4045fe58019SAttilio Rao  * Our vnop_fsync roughly corresponds to the FUSE_FSYNC method. The Linux
4055fe58019SAttilio Rao  * version of FUSE also has a FUSE_FLUSH method.
4065fe58019SAttilio Rao  *
4075fe58019SAttilio Rao  * On Linux, fsync() synchronizes a file's complete in-core state with that
4085fe58019SAttilio Rao  * on disk. The call is not supposed to return until the system has completed
4095fe58019SAttilio Rao  * that action or until an error is detected.
4105fe58019SAttilio Rao  *
4115fe58019SAttilio Rao  * Linux also has an fdatasync() call that is similar to fsync() but is not
4125fe58019SAttilio Rao  * required to update the metadata such as access time and modification time.
4135fe58019SAttilio Rao  */
4145fe58019SAttilio Rao 
4155fe58019SAttilio Rao /*
4165fe58019SAttilio Rao     struct vnop_fsync_args {
4175fe58019SAttilio Rao 	struct vnodeop_desc *a_desc;
4185fe58019SAttilio Rao 	struct vnode * a_vp;
4195fe58019SAttilio Rao 	struct ucred * a_cred;
4205fe58019SAttilio Rao 	int  a_waitfor;
4215fe58019SAttilio Rao 	struct thread * a_td;
4225fe58019SAttilio Rao     };
4235fe58019SAttilio Rao */
4245fe58019SAttilio Rao static int
4255fe58019SAttilio Rao fuse_vnop_fsync(struct vop_fsync_args *ap)
4265fe58019SAttilio Rao {
4275fe58019SAttilio Rao 	struct vnode *vp = ap->a_vp;
4285fe58019SAttilio Rao 	struct thread *td = ap->a_td;
4295fe58019SAttilio Rao 
4305fe58019SAttilio Rao 	struct fuse_filehandle *fufh;
4315fe58019SAttilio Rao 	struct fuse_vnode_data *fvdat = VTOFUD(vp);
4325fe58019SAttilio Rao 
4335fe58019SAttilio Rao 	int type, err = 0;
4345fe58019SAttilio Rao 
4355fe58019SAttilio Rao 	fuse_trace_printf_vnop();
4365fe58019SAttilio Rao 
4375fe58019SAttilio Rao 	if (fuse_isdeadfs(vp)) {
4385fe58019SAttilio Rao 		return 0;
4395fe58019SAttilio Rao 	}
4405fe58019SAttilio Rao 	if ((err = vop_stdfsync(ap)))
4415fe58019SAttilio Rao 		return err;
4425fe58019SAttilio Rao 
4435fe58019SAttilio Rao 	if (!fsess_isimpl(vnode_mount(vp),
4445fe58019SAttilio Rao 	    (vnode_vtype(vp) == VDIR ? FUSE_FSYNCDIR : FUSE_FSYNC))) {
4455fe58019SAttilio Rao 		goto out;
4465fe58019SAttilio Rao 	}
4475fe58019SAttilio Rao 	for (type = 0; type < FUFH_MAXTYPE; type++) {
4485fe58019SAttilio Rao 		fufh = &(fvdat->fufh[type]);
4495fe58019SAttilio Rao 		if (FUFH_IS_VALID(fufh)) {
4505fe58019SAttilio Rao 			fuse_internal_fsync(vp, td, NULL, fufh);
4515fe58019SAttilio Rao 		}
4525fe58019SAttilio Rao 	}
4535fe58019SAttilio Rao 
4545fe58019SAttilio Rao out:
4555fe58019SAttilio Rao 	return 0;
4565fe58019SAttilio Rao }
4575fe58019SAttilio Rao 
4585fe58019SAttilio Rao /*
4595fe58019SAttilio Rao     struct vnop_getattr_args {
4605fe58019SAttilio Rao 	struct vnode *a_vp;
4615fe58019SAttilio Rao 	struct vattr *a_vap;
4625fe58019SAttilio Rao 	struct ucred *a_cred;
4635fe58019SAttilio Rao 	struct thread *a_td;
4645fe58019SAttilio Rao     };
4655fe58019SAttilio Rao */
4665fe58019SAttilio Rao static int
4675fe58019SAttilio Rao fuse_vnop_getattr(struct vop_getattr_args *ap)
4685fe58019SAttilio Rao {
4695fe58019SAttilio Rao 	struct vnode *vp = ap->a_vp;
4705fe58019SAttilio Rao 	struct vattr *vap = ap->a_vap;
4715fe58019SAttilio Rao 	struct ucred *cred = ap->a_cred;
4725fe58019SAttilio Rao 	struct thread *td = curthread;
4735fe58019SAttilio Rao 	struct fuse_vnode_data *fvdat = VTOFUD(vp);
4745fe58019SAttilio Rao 
4755fe58019SAttilio Rao 	int err = 0;
4765fe58019SAttilio Rao 	int dataflags;
4775fe58019SAttilio Rao 	struct fuse_dispatcher fdi;
4785fe58019SAttilio Rao 
4794cff153bSAttilio Rao 	FS_DEBUG2G("inode=%ju\n", (uintmax_t)VTOI(vp));
4805fe58019SAttilio Rao 
4815fe58019SAttilio Rao 	dataflags = fuse_get_mpdata(vnode_mount(vp))->dataflags;
4825fe58019SAttilio Rao 
4835fe58019SAttilio Rao 	/* Note that we are not bailing out on a dead file system just yet. */
4845fe58019SAttilio Rao 
4855fe58019SAttilio Rao 	if (!(dataflags & FSESS_INITED)) {
4865fe58019SAttilio Rao 		if (!vnode_isvroot(vp)) {
4875fe58019SAttilio Rao 			fdata_set_dead(fuse_get_mpdata(vnode_mount(vp)));
4885fe58019SAttilio Rao 			err = ENOTCONN;
4895fe58019SAttilio Rao 			debug_printf("fuse_getattr b: returning ENOTCONN\n");
4905fe58019SAttilio Rao 			return err;
4915fe58019SAttilio Rao 		} else {
4925fe58019SAttilio Rao 			goto fake;
4935fe58019SAttilio Rao 		}
4945fe58019SAttilio Rao 	}
4955fe58019SAttilio Rao 	fdisp_init(&fdi, 0);
4965fe58019SAttilio Rao 	if ((err = fdisp_simple_putget_vp(&fdi, FUSE_GETATTR, vp, td, cred))) {
4975fe58019SAttilio Rao 		if ((err == ENOTCONN) && vnode_isvroot(vp)) {
4985fe58019SAttilio Rao 			/* see comment at similar place in fuse_statfs() */
4995fe58019SAttilio Rao 			fdisp_destroy(&fdi);
5005fe58019SAttilio Rao 			goto fake;
5015fe58019SAttilio Rao 		}
5025fe58019SAttilio Rao 		if (err == ENOENT) {
5035fe58019SAttilio Rao 			fuse_internal_vnode_disappear(vp);
5045fe58019SAttilio Rao 		}
5055fe58019SAttilio Rao 		goto out;
5065fe58019SAttilio Rao 	}
5075fe58019SAttilio Rao 	cache_attrs(vp, (struct fuse_attr_out *)fdi.answ);
5085fe58019SAttilio Rao 	if (vap != VTOVA(vp)) {
5095fe58019SAttilio Rao 		memcpy(vap, VTOVA(vp), sizeof(*vap));
5105fe58019SAttilio Rao 	}
511d9454fabSAttilio Rao 	if (vap->va_type != vnode_vtype(vp)) {
512d9454fabSAttilio Rao 		fuse_internal_vnode_disappear(vp);
513d9454fabSAttilio Rao 		err = ENOENT;
514d9454fabSAttilio Rao 		goto out;
515d9454fabSAttilio Rao 	}
5165fe58019SAttilio Rao 	if ((fvdat->flag & FN_SIZECHANGE) != 0)
5175fe58019SAttilio Rao 		vap->va_size = fvdat->filesize;
5185fe58019SAttilio Rao 
5195fe58019SAttilio Rao 	if (vnode_isreg(vp) && (fvdat->flag & FN_SIZECHANGE) == 0) {
5205fe58019SAttilio Rao 		/*
5215fe58019SAttilio Rao 	         * This is for those cases when the file size changed without us
5225fe58019SAttilio Rao 	         * knowing, and we want to catch up.
5235fe58019SAttilio Rao 	         */
5245fe58019SAttilio Rao 		off_t new_filesize = ((struct fuse_attr_out *)
5255fe58019SAttilio Rao 				      fdi.answ)->attr.size;
5265fe58019SAttilio Rao 
5275fe58019SAttilio Rao 		if (fvdat->filesize != new_filesize) {
5285fe58019SAttilio Rao 			fuse_vnode_setsize(vp, cred, new_filesize);
5295fe58019SAttilio Rao 		}
5305fe58019SAttilio Rao 	}
5315fe58019SAttilio Rao 	debug_printf("fuse_getattr e: returning 0\n");
5325fe58019SAttilio Rao 
5335fe58019SAttilio Rao out:
5345fe58019SAttilio Rao 	fdisp_destroy(&fdi);
5355fe58019SAttilio Rao 	return err;
5365fe58019SAttilio Rao 
5375fe58019SAttilio Rao fake:
5385fe58019SAttilio Rao 	bzero(vap, sizeof(*vap));
5395fe58019SAttilio Rao 	vap->va_type = vnode_vtype(vp);
5405fe58019SAttilio Rao 
5415fe58019SAttilio Rao 	return 0;
5425fe58019SAttilio Rao }
5435fe58019SAttilio Rao 
5445fe58019SAttilio Rao /*
5455fe58019SAttilio Rao     struct vnop_inactive_args {
5465fe58019SAttilio Rao 	struct vnode *a_vp;
5475fe58019SAttilio Rao 	struct thread *a_td;
5485fe58019SAttilio Rao     };
5495fe58019SAttilio Rao */
5505fe58019SAttilio Rao static int
5515fe58019SAttilio Rao fuse_vnop_inactive(struct vop_inactive_args *ap)
5525fe58019SAttilio Rao {
5535fe58019SAttilio Rao 	struct vnode *vp = ap->a_vp;
5545fe58019SAttilio Rao 	struct thread *td = ap->a_td;
5555fe58019SAttilio Rao 
5565fe58019SAttilio Rao 	struct fuse_vnode_data *fvdat = VTOFUD(vp);
5575fe58019SAttilio Rao 	struct fuse_filehandle *fufh = NULL;
5585fe58019SAttilio Rao 
5595fe58019SAttilio Rao 	int type, need_flush = 1;
5605fe58019SAttilio Rao 
5614cff153bSAttilio Rao 	FS_DEBUG("inode=%ju\n", (uintmax_t)VTOI(vp));
5625fe58019SAttilio Rao 
5635fe58019SAttilio Rao 	for (type = 0; type < FUFH_MAXTYPE; type++) {
5645fe58019SAttilio Rao 		fufh = &(fvdat->fufh[type]);
5655fe58019SAttilio Rao 		if (FUFH_IS_VALID(fufh)) {
5665fe58019SAttilio Rao 			if (need_flush && vp->v_type == VREG) {
5675fe58019SAttilio Rao 				if ((VTOFUD(vp)->flag & FN_SIZECHANGE) != 0) {
5685fe58019SAttilio Rao 					fuse_vnode_savesize(vp, NULL);
5695fe58019SAttilio Rao 				}
5705fe58019SAttilio Rao 				if (fuse_data_cache_invalidate ||
5715fe58019SAttilio Rao 				    (fvdat->flag & FN_REVOKED) != 0)
5725fe58019SAttilio Rao 					fuse_io_invalbuf(vp, td);
5735fe58019SAttilio Rao 				else
5745fe58019SAttilio Rao 					fuse_io_flushbuf(vp, MNT_WAIT, td);
5755fe58019SAttilio Rao 				need_flush = 0;
5765fe58019SAttilio Rao 			}
5775fe58019SAttilio Rao 			fuse_filehandle_close(vp, type, td, NULL);
5785fe58019SAttilio Rao 		}
5795fe58019SAttilio Rao 	}
5805fe58019SAttilio Rao 
5815fe58019SAttilio Rao 	if ((fvdat->flag & FN_REVOKED) != 0 && fuse_reclaim_revoked) {
5825fe58019SAttilio Rao 		vrecycle(vp);
5835fe58019SAttilio Rao 	}
5845fe58019SAttilio Rao 	return 0;
5855fe58019SAttilio Rao }
5865fe58019SAttilio Rao 
5875fe58019SAttilio Rao /*
5885fe58019SAttilio Rao     struct vnop_link_args {
5895fe58019SAttilio Rao 	struct vnode *a_tdvp;
5905fe58019SAttilio Rao 	struct vnode *a_vp;
5915fe58019SAttilio Rao 	struct componentname *a_cnp;
5925fe58019SAttilio Rao     };
5935fe58019SAttilio Rao */
5945fe58019SAttilio Rao static int
5955fe58019SAttilio Rao fuse_vnop_link(struct vop_link_args *ap)
5965fe58019SAttilio Rao {
5975fe58019SAttilio Rao 	struct vnode *vp = ap->a_vp;
5985fe58019SAttilio Rao 	struct vnode *tdvp = ap->a_tdvp;
5995fe58019SAttilio Rao 	struct componentname *cnp = ap->a_cnp;
6005fe58019SAttilio Rao 
6015fe58019SAttilio Rao 	struct vattr *vap = VTOVA(vp);
6025fe58019SAttilio Rao 
6035fe58019SAttilio Rao 	struct fuse_dispatcher fdi;
6045fe58019SAttilio Rao 	struct fuse_entry_out *feo;
6055fe58019SAttilio Rao 	struct fuse_link_in fli;
6065fe58019SAttilio Rao 
6075fe58019SAttilio Rao 	int err;
6085fe58019SAttilio Rao 
6095fe58019SAttilio Rao 	fuse_trace_printf_vnop();
6105fe58019SAttilio Rao 
6115fe58019SAttilio Rao 	if (fuse_isdeadfs(vp)) {
6125fe58019SAttilio Rao 		return ENXIO;
6135fe58019SAttilio Rao 	}
6145fe58019SAttilio Rao 	if (vnode_mount(tdvp) != vnode_mount(vp)) {
6155fe58019SAttilio Rao 		return EXDEV;
6165fe58019SAttilio Rao 	}
6175fe58019SAttilio Rao 	if (vap->va_nlink >= FUSE_LINK_MAX) {
6185fe58019SAttilio Rao 		return EMLINK;
6195fe58019SAttilio Rao 	}
6205fe58019SAttilio Rao 	fli.oldnodeid = VTOI(vp);
6215fe58019SAttilio Rao 
6225fe58019SAttilio Rao 	fdisp_init(&fdi, 0);
6235fe58019SAttilio Rao 	fuse_internal_newentry_makerequest(vnode_mount(tdvp), VTOI(tdvp), cnp,
6245fe58019SAttilio Rao 	    FUSE_LINK, &fli, sizeof(fli), &fdi);
6255fe58019SAttilio Rao 	if ((err = fdisp_wait_answ(&fdi))) {
6265fe58019SAttilio Rao 		goto out;
6275fe58019SAttilio Rao 	}
6285fe58019SAttilio Rao 	feo = fdi.answ;
6295fe58019SAttilio Rao 
6305fe58019SAttilio Rao 	err = fuse_internal_checkentry(feo, vnode_vtype(vp));
6315fe58019SAttilio Rao out:
6325fe58019SAttilio Rao 	fdisp_destroy(&fdi);
6335fe58019SAttilio Rao 	return err;
6345fe58019SAttilio Rao }
6355fe58019SAttilio Rao 
6365fe58019SAttilio Rao /*
6375fe58019SAttilio Rao     struct vnop_lookup_args {
6385fe58019SAttilio Rao 	struct vnodeop_desc *a_desc;
6395fe58019SAttilio Rao 	struct vnode *a_dvp;
6405fe58019SAttilio Rao 	struct vnode **a_vpp;
6415fe58019SAttilio Rao 	struct componentname *a_cnp;
6425fe58019SAttilio Rao     };
6435fe58019SAttilio Rao */
6445fe58019SAttilio Rao int
6455fe58019SAttilio Rao fuse_vnop_lookup(struct vop_lookup_args *ap)
6465fe58019SAttilio Rao {
6475fe58019SAttilio Rao 	struct vnode *dvp = ap->a_dvp;
6485fe58019SAttilio Rao 	struct vnode **vpp = ap->a_vpp;
6495fe58019SAttilio Rao 	struct componentname *cnp = ap->a_cnp;
6505fe58019SAttilio Rao 	struct thread *td = cnp->cn_thread;
6515fe58019SAttilio Rao 	struct ucred *cred = cnp->cn_cred;
6525fe58019SAttilio Rao 
6535fe58019SAttilio Rao 	int nameiop = cnp->cn_nameiop;
6545fe58019SAttilio Rao 	int flags = cnp->cn_flags;
6555fe58019SAttilio Rao 	int wantparent = flags & (LOCKPARENT | WANTPARENT);
6565fe58019SAttilio Rao 	int islastcn = flags & ISLASTCN;
6575fe58019SAttilio Rao 	struct mount *mp = vnode_mount(dvp);
6585fe58019SAttilio Rao 
6595fe58019SAttilio Rao 	int err = 0;
6605fe58019SAttilio Rao 	int lookup_err = 0;
6615fe58019SAttilio Rao 	struct vnode *vp = NULL;
6625fe58019SAttilio Rao 
6635fe58019SAttilio Rao 	struct fuse_dispatcher fdi;
6645fe58019SAttilio Rao 	enum fuse_opcode op;
6655fe58019SAttilio Rao 
6665fe58019SAttilio Rao 	uint64_t nid;
6675fe58019SAttilio Rao 	struct fuse_access_param facp;
6685fe58019SAttilio Rao 
6694cff153bSAttilio Rao 	FS_DEBUG2G("parent_inode=%ju - %*s\n",
6705fe58019SAttilio Rao 	    (uintmax_t)VTOI(dvp), (int)cnp->cn_namelen, cnp->cn_nameptr);
6715fe58019SAttilio Rao 
6725fe58019SAttilio Rao 	if (fuse_isdeadfs(dvp)) {
6735fe58019SAttilio Rao 		*vpp = NULL;
6745fe58019SAttilio Rao 		return ENXIO;
6755fe58019SAttilio Rao 	}
6765fe58019SAttilio Rao 	if (!vnode_isdir(dvp)) {
6775fe58019SAttilio Rao 		return ENOTDIR;
6785fe58019SAttilio Rao 	}
6795fe58019SAttilio Rao 	if (islastcn && vfs_isrdonly(mp) && (nameiop != LOOKUP)) {
6805fe58019SAttilio Rao 		return EROFS;
6815fe58019SAttilio Rao 	}
6825fe58019SAttilio Rao 	/*
6835fe58019SAttilio Rao          * We do access check prior to doing anything else only in the case
6845fe58019SAttilio Rao          * when we are at fs root (we'd like to say, "we are at the first
6855fe58019SAttilio Rao          * component", but that's not exactly the same... nevermind).
6865fe58019SAttilio Rao          * See further comments at further access checks.
6875fe58019SAttilio Rao          */
6885fe58019SAttilio Rao 
6895fe58019SAttilio Rao 	bzero(&facp, sizeof(facp));
6905fe58019SAttilio Rao 	if (vnode_isvroot(dvp)) {	/* early permission check hack */
6915fe58019SAttilio Rao 		if ((err = fuse_internal_access(dvp, VEXEC, &facp, td, cred))) {
6925fe58019SAttilio Rao 			return err;
6935fe58019SAttilio Rao 		}
6945fe58019SAttilio Rao 	}
6955fe58019SAttilio Rao 	if (flags & ISDOTDOT) {
6965fe58019SAttilio Rao 		nid = VTOFUD(dvp)->parent_nid;
6975fe58019SAttilio Rao 		if (nid == 0) {
6985fe58019SAttilio Rao 			return ENOENT;
6995fe58019SAttilio Rao 		}
7005fe58019SAttilio Rao 		fdisp_init(&fdi, 0);
7015fe58019SAttilio Rao 		op = FUSE_GETATTR;
7025fe58019SAttilio Rao 		goto calldaemon;
7035fe58019SAttilio Rao 	} else if (cnp->cn_namelen == 1 && *(cnp->cn_nameptr) == '.') {
7045fe58019SAttilio Rao 		nid = VTOI(dvp);
7055fe58019SAttilio Rao 		fdisp_init(&fdi, 0);
7065fe58019SAttilio Rao 		op = FUSE_GETATTR;
7075fe58019SAttilio Rao 		goto calldaemon;
7085fe58019SAttilio Rao 	} else if (fuse_lookup_cache_enable) {
7095fe58019SAttilio Rao 		err = cache_lookup(dvp, vpp, cnp, NULL, NULL);
7105fe58019SAttilio Rao 		switch (err) {
7115fe58019SAttilio Rao 
7125fe58019SAttilio Rao 		case -1:		/* positive match */
7135fe58019SAttilio Rao 			atomic_add_acq_long(&fuse_lookup_cache_hits, 1);
7145fe58019SAttilio Rao 			return 0;
7155fe58019SAttilio Rao 
7165fe58019SAttilio Rao 		case 0:		/* no match in cache */
7175fe58019SAttilio Rao 			atomic_add_acq_long(&fuse_lookup_cache_misses, 1);
7185fe58019SAttilio Rao 			break;
7195fe58019SAttilio Rao 
7205fe58019SAttilio Rao 		case ENOENT:		/* negative match */
7215fe58019SAttilio Rao 			/* fall through */
7225fe58019SAttilio Rao 		default:
7235fe58019SAttilio Rao 			return err;
7245fe58019SAttilio Rao 		}
7255fe58019SAttilio Rao 	}
7265fe58019SAttilio Rao 	nid = VTOI(dvp);
7275fe58019SAttilio Rao 	fdisp_init(&fdi, cnp->cn_namelen + 1);
7285fe58019SAttilio Rao 	op = FUSE_LOOKUP;
7295fe58019SAttilio Rao 
7305fe58019SAttilio Rao calldaemon:
7315fe58019SAttilio Rao 	fdisp_make(&fdi, op, mp, nid, td, cred);
7325fe58019SAttilio Rao 
7335fe58019SAttilio Rao 	if (op == FUSE_LOOKUP) {
7345fe58019SAttilio Rao 		memcpy(fdi.indata, cnp->cn_nameptr, cnp->cn_namelen);
7355fe58019SAttilio Rao 		((char *)fdi.indata)[cnp->cn_namelen] = '\0';
7365fe58019SAttilio Rao 	}
7375fe58019SAttilio Rao 	lookup_err = fdisp_wait_answ(&fdi);
7385fe58019SAttilio Rao 
7395fe58019SAttilio Rao 	if ((op == FUSE_LOOKUP) && !lookup_err) {	/* lookup call succeeded */
7405fe58019SAttilio Rao 		nid = ((struct fuse_entry_out *)fdi.answ)->nodeid;
7415fe58019SAttilio Rao 		if (!nid) {
7425fe58019SAttilio Rao 			/*
7435fe58019SAttilio Rao 	                 * zero nodeid is the same as "not found",
7445fe58019SAttilio Rao 	                 * but it's also cacheable (which we keep
7455fe58019SAttilio Rao 	                 * keep on doing not as of writing this)
7465fe58019SAttilio Rao 	                 */
7475fe58019SAttilio Rao 			lookup_err = ENOENT;
7485fe58019SAttilio Rao 		} else if (nid == FUSE_ROOT_ID) {
7495fe58019SAttilio Rao 			lookup_err = EINVAL;
7505fe58019SAttilio Rao 		}
7515fe58019SAttilio Rao 	}
7525fe58019SAttilio Rao 	if (lookup_err &&
7535fe58019SAttilio Rao 	    (!fdi.answ_stat || lookup_err != ENOENT || op != FUSE_LOOKUP)) {
7545fe58019SAttilio Rao 		fdisp_destroy(&fdi);
7555fe58019SAttilio Rao 		return lookup_err;
7565fe58019SAttilio Rao 	}
7575fe58019SAttilio Rao 	/* lookup_err, if non-zero, must be ENOENT at this point */
7585fe58019SAttilio Rao 
7595fe58019SAttilio Rao 	if (lookup_err) {
7605fe58019SAttilio Rao 
7615fe58019SAttilio Rao 		if ((nameiop == CREATE || nameiop == RENAME) && islastcn
7625fe58019SAttilio Rao 		     /* && directory dvp has not been removed */ ) {
7635fe58019SAttilio Rao 
7645fe58019SAttilio Rao 			if (vfs_isrdonly(mp)) {
7655fe58019SAttilio Rao 				err = EROFS;
7665fe58019SAttilio Rao 				goto out;
7675fe58019SAttilio Rao 			}
7685fe58019SAttilio Rao #if 0 /* THINK_ABOUT_THIS */
7695fe58019SAttilio Rao 			if ((err = fuse_internal_access(dvp, VWRITE, cred, td, &facp))) {
7705fe58019SAttilio Rao 				goto out;
7715fe58019SAttilio Rao 			}
7725fe58019SAttilio Rao #endif
7735fe58019SAttilio Rao 
7745fe58019SAttilio Rao 			/*
7755fe58019SAttilio Rao 	                 * Possibly record the position of a slot in the
7765fe58019SAttilio Rao 	                 * directory large enough for the new component name.
7775fe58019SAttilio Rao 	                 * This can be recorded in the vnode private data for
7785fe58019SAttilio Rao 	                 * dvp. Set the SAVENAME flag to hold onto the
7795fe58019SAttilio Rao 	                 * pathname for use later in VOP_CREATE or VOP_RENAME.
7805fe58019SAttilio Rao 	                 */
7815fe58019SAttilio Rao 			cnp->cn_flags |= SAVENAME;
7825fe58019SAttilio Rao 
7835fe58019SAttilio Rao 			err = EJUSTRETURN;
7845fe58019SAttilio Rao 			goto out;
7855fe58019SAttilio Rao 		}
7865fe58019SAttilio Rao 		/* Consider inserting name into cache. */
7875fe58019SAttilio Rao 
7885fe58019SAttilio Rao 		/*
7895fe58019SAttilio Rao 	         * No we can't use negative caching, as the fs
7905fe58019SAttilio Rao 	         * changes are out of our control.
7915fe58019SAttilio Rao 	         * False positives' falseness turns out just as things
7925fe58019SAttilio Rao 	         * go by, but false negatives' falseness doesn't.
7935fe58019SAttilio Rao 	         * (and aiding the caching mechanism with extra control
7945fe58019SAttilio Rao 	         * mechanisms comes quite close to beating the whole purpose
7955fe58019SAttilio Rao 	         * caching...)
7965fe58019SAttilio Rao 	         */
7975fe58019SAttilio Rao #if 0
7985fe58019SAttilio Rao 		if ((cnp->cn_flags & MAKEENTRY) && nameiop != CREATE) {
7994cff153bSAttilio Rao 			FS_DEBUG("inserting NULL into cache\n");
8005fe58019SAttilio Rao 			cache_enter(dvp, NULL, cnp);
8015fe58019SAttilio Rao 		}
8025fe58019SAttilio Rao #endif
8035fe58019SAttilio Rao 		err = ENOENT;
8045fe58019SAttilio Rao 		goto out;
8055fe58019SAttilio Rao 
8065fe58019SAttilio Rao 	} else {
8075fe58019SAttilio Rao 
8085fe58019SAttilio Rao 		/* !lookup_err */
8095fe58019SAttilio Rao 
8105fe58019SAttilio Rao 		struct fuse_entry_out *feo = NULL;
8115fe58019SAttilio Rao 		struct fuse_attr *fattr = NULL;
8125fe58019SAttilio Rao 
8135fe58019SAttilio Rao 		if (op == FUSE_GETATTR) {
8145fe58019SAttilio Rao 			fattr = &((struct fuse_attr_out *)fdi.answ)->attr;
8155fe58019SAttilio Rao 		} else {
8165fe58019SAttilio Rao 			feo = (struct fuse_entry_out *)fdi.answ;
8175fe58019SAttilio Rao 			fattr = &(feo->attr);
8185fe58019SAttilio Rao 		}
8195fe58019SAttilio Rao 
8205fe58019SAttilio Rao 		/*
8215fe58019SAttilio Rao 	         * If deleting, and at end of pathname, return parameters
8225fe58019SAttilio Rao 	         * which can be used to remove file.  If the wantparent flag
8235fe58019SAttilio Rao 	         * isn't set, we return only the directory, otherwise we go on
8245fe58019SAttilio Rao 	         * and lock the inode, being careful with ".".
8255fe58019SAttilio Rao 	         */
8265fe58019SAttilio Rao 		if (nameiop == DELETE && islastcn) {
8275fe58019SAttilio Rao 			/*
8285fe58019SAttilio Rao 	                 * Check for write access on directory.
8295fe58019SAttilio Rao 	                 */
8305fe58019SAttilio Rao 			facp.xuid = fattr->uid;
8315fe58019SAttilio Rao 			facp.facc_flags |= FACCESS_STICKY;
8325fe58019SAttilio Rao 			err = fuse_internal_access(dvp, VWRITE, &facp, td, cred);
8335fe58019SAttilio Rao 			facp.facc_flags &= ~FACCESS_XQUERIES;
8345fe58019SAttilio Rao 
8355fe58019SAttilio Rao 			if (err) {
8365fe58019SAttilio Rao 				goto out;
8375fe58019SAttilio Rao 			}
8385fe58019SAttilio Rao 			if (nid == VTOI(dvp)) {
8395fe58019SAttilio Rao 				vref(dvp);
8405fe58019SAttilio Rao 				*vpp = dvp;
8415fe58019SAttilio Rao 			} else {
8425fe58019SAttilio Rao 				err = fuse_vnode_get(dvp->v_mount, nid, dvp,
8435fe58019SAttilio Rao 				    &vp, cnp, IFTOVT(fattr->mode));
8445fe58019SAttilio Rao 				if (err)
8455fe58019SAttilio Rao 					goto out;
8465fe58019SAttilio Rao 				*vpp = vp;
8475fe58019SAttilio Rao 			}
8485fe58019SAttilio Rao 
8495fe58019SAttilio Rao 			/*
8505fe58019SAttilio Rao 			 * Save the name for use in VOP_RMDIR and VOP_REMOVE
8515fe58019SAttilio Rao 			 * later.
8525fe58019SAttilio Rao 			 */
8535fe58019SAttilio Rao 			cnp->cn_flags |= SAVENAME;
8545fe58019SAttilio Rao 			goto out;
8555fe58019SAttilio Rao 
8565fe58019SAttilio Rao 		}
8575fe58019SAttilio Rao 		/*
8585fe58019SAttilio Rao 	         * If rewriting (RENAME), return the inode and the
8595fe58019SAttilio Rao 	         * information required to rewrite the present directory
8605fe58019SAttilio Rao 	         * Must get inode of directory entry to verify it's a
8615fe58019SAttilio Rao 	         * regular file, or empty directory.
8625fe58019SAttilio Rao 	         */
8635fe58019SAttilio Rao 		if (nameiop == RENAME && wantparent && islastcn) {
8645fe58019SAttilio Rao 
8655fe58019SAttilio Rao #if 0 /* THINK_ABOUT_THIS */
8665fe58019SAttilio Rao 			if ((err = fuse_internal_access(dvp, VWRITE, cred, td, &facp))) {
8675fe58019SAttilio Rao 				goto out;
8685fe58019SAttilio Rao 			}
8695fe58019SAttilio Rao #endif
8705fe58019SAttilio Rao 
8715fe58019SAttilio Rao 			/*
8725fe58019SAttilio Rao 	                 * Check for "."
8735fe58019SAttilio Rao 	                 */
8745fe58019SAttilio Rao 			if (nid == VTOI(dvp)) {
8755fe58019SAttilio Rao 				err = EISDIR;
8765fe58019SAttilio Rao 				goto out;
8775fe58019SAttilio Rao 			}
8785fe58019SAttilio Rao 			err = fuse_vnode_get(vnode_mount(dvp),
8795fe58019SAttilio Rao 			    nid,
8805fe58019SAttilio Rao 			    dvp,
8815fe58019SAttilio Rao 			    &vp,
8825fe58019SAttilio Rao 			    cnp,
8835fe58019SAttilio Rao 			    IFTOVT(fattr->mode));
8845fe58019SAttilio Rao 			if (err) {
8855fe58019SAttilio Rao 				goto out;
8865fe58019SAttilio Rao 			}
8875fe58019SAttilio Rao 			*vpp = vp;
8885fe58019SAttilio Rao 			/*
8895fe58019SAttilio Rao 	                 * Save the name for use in VOP_RENAME later.
8905fe58019SAttilio Rao 	                 */
8915fe58019SAttilio Rao 			cnp->cn_flags |= SAVENAME;
8925fe58019SAttilio Rao 
8935fe58019SAttilio Rao 			goto out;
8945fe58019SAttilio Rao 		}
8955fe58019SAttilio Rao 		if (flags & ISDOTDOT) {
8965fe58019SAttilio Rao 			struct mount *mp;
8975fe58019SAttilio Rao 			int ltype;
8985fe58019SAttilio Rao 
8995fe58019SAttilio Rao 			/*
9005fe58019SAttilio Rao 			 * Expanded copy of vn_vget_ino() so that
9015fe58019SAttilio Rao 			 * fuse_vnode_get() can be used.
9025fe58019SAttilio Rao 			 */
9035fe58019SAttilio Rao 			mp = dvp->v_mount;
9045fe58019SAttilio Rao 			ltype = VOP_ISLOCKED(dvp);
9055fe58019SAttilio Rao 			err = vfs_busy(mp, MBF_NOWAIT);
9065fe58019SAttilio Rao 			if (err != 0) {
9075fe58019SAttilio Rao 				vfs_ref(mp);
9085fe58019SAttilio Rao 				VOP_UNLOCK(dvp, 0);
9095fe58019SAttilio Rao 				err = vfs_busy(mp, 0);
9105fe58019SAttilio Rao 				vn_lock(dvp, ltype | LK_RETRY);
9115fe58019SAttilio Rao 				vfs_rel(mp);
9125fe58019SAttilio Rao 				if (err)
9135fe58019SAttilio Rao 					goto out;
9145fe58019SAttilio Rao 				if ((dvp->v_iflag & VI_DOOMED) != 0) {
9155fe58019SAttilio Rao 					err = ENOENT;
9165fe58019SAttilio Rao 					vfs_unbusy(mp);
9175fe58019SAttilio Rao 					goto out;
9185fe58019SAttilio Rao 				}
9195fe58019SAttilio Rao 			}
9205fe58019SAttilio Rao 			VOP_UNLOCK(dvp, 0);
9215fe58019SAttilio Rao 			err = fuse_vnode_get(vnode_mount(dvp),
9225fe58019SAttilio Rao 			    nid,
9235fe58019SAttilio Rao 			    NULL,
9245fe58019SAttilio Rao 			    &vp,
9255fe58019SAttilio Rao 			    cnp,
9265fe58019SAttilio Rao 			    IFTOVT(fattr->mode));
9275fe58019SAttilio Rao 			vfs_unbusy(mp);
9285fe58019SAttilio Rao 			vn_lock(dvp, ltype | LK_RETRY);
9295fe58019SAttilio Rao 			if ((dvp->v_iflag & VI_DOOMED) != 0) {
9305fe58019SAttilio Rao 				if (err == 0)
9315fe58019SAttilio Rao 					vput(vp);
9325fe58019SAttilio Rao 				err = ENOENT;
9335fe58019SAttilio Rao 			}
9345fe58019SAttilio Rao 			if (err)
9355fe58019SAttilio Rao 				goto out;
9365fe58019SAttilio Rao 			*vpp = vp;
9375fe58019SAttilio Rao 		} else if (nid == VTOI(dvp)) {
9385fe58019SAttilio Rao 			vref(dvp);
9395fe58019SAttilio Rao 			*vpp = dvp;
9405fe58019SAttilio Rao 		} else {
9415fe58019SAttilio Rao 			err = fuse_vnode_get(vnode_mount(dvp),
9425fe58019SAttilio Rao 			    nid,
9435fe58019SAttilio Rao 			    dvp,
9445fe58019SAttilio Rao 			    &vp,
9455fe58019SAttilio Rao 			    cnp,
9465fe58019SAttilio Rao 			    IFTOVT(fattr->mode));
9475fe58019SAttilio Rao 			if (err) {
9485fe58019SAttilio Rao 				goto out;
9495fe58019SAttilio Rao 			}
9505fe58019SAttilio Rao 			fuse_vnode_setparent(vp, dvp);
9515fe58019SAttilio Rao 			*vpp = vp;
9525fe58019SAttilio Rao 		}
9535fe58019SAttilio Rao 
9545fe58019SAttilio Rao 		if (op == FUSE_GETATTR) {
9555fe58019SAttilio Rao 			cache_attrs(*vpp, (struct fuse_attr_out *)fdi.answ);
9565fe58019SAttilio Rao 		} else {
9575fe58019SAttilio Rao 			cache_attrs(*vpp, (struct fuse_entry_out *)fdi.answ);
9585fe58019SAttilio Rao 		}
9595fe58019SAttilio Rao 
9605fe58019SAttilio Rao 		/* Insert name into cache if appropriate. */
9615fe58019SAttilio Rao 
9625fe58019SAttilio Rao 		/*
9635fe58019SAttilio Rao 	         * Nooo, caching is evil. With caching, we can't avoid stale
9645fe58019SAttilio Rao 	         * information taking over the playground (cached info is not
9655fe58019SAttilio Rao 	         * just positive/negative, it does have qualitative aspects,
9665fe58019SAttilio Rao 	         * too). And a (VOP/FUSE)_GETATTR is always thrown anyway, when
9675fe58019SAttilio Rao 	         * walking down along cached path components, and that's not
9685fe58019SAttilio Rao 	         * any cheaper than FUSE_LOOKUP. This might change with
9695fe58019SAttilio Rao 	         * implementing kernel side attr caching, but... In Linux,
9705fe58019SAttilio Rao 	         * lookup results are not cached, and the daemon is bombarded
9715fe58019SAttilio Rao 	         * with FUSE_LOOKUPS on and on. This shows that by design, the
9725fe58019SAttilio Rao 	         * daemon is expected to handle frequent lookup queries
9735fe58019SAttilio Rao 	         * efficiently, do its caching in userspace, and so on.
9745fe58019SAttilio Rao 	         *
9755fe58019SAttilio Rao 	         * So just leave the name cache alone.
9765fe58019SAttilio Rao 	         */
9775fe58019SAttilio Rao 
9785fe58019SAttilio Rao 		/*
9795fe58019SAttilio Rao 	         * Well, now I know, Linux caches lookups, but with a
9805fe58019SAttilio Rao 	         * timeout... So it's the same thing as attribute caching:
9815fe58019SAttilio Rao 	         * we can deal with it when implement timeouts.
9825fe58019SAttilio Rao 	         */
9835fe58019SAttilio Rao #if 0
9845fe58019SAttilio Rao 		if (cnp->cn_flags & MAKEENTRY) {
9855fe58019SAttilio Rao 			cache_enter(dvp, *vpp, cnp);
9865fe58019SAttilio Rao 		}
9875fe58019SAttilio Rao #endif
9885fe58019SAttilio Rao 	}
9895fe58019SAttilio Rao out:
9905fe58019SAttilio Rao 	if (!lookup_err) {
9915fe58019SAttilio Rao 
9925fe58019SAttilio Rao 		/* No lookup error; need to clean up. */
9935fe58019SAttilio Rao 
9945fe58019SAttilio Rao 		if (err) {		/* Found inode; exit with no vnode. */
9955fe58019SAttilio Rao 			if (op == FUSE_LOOKUP) {
9965fe58019SAttilio Rao 				fuse_internal_forget_send(vnode_mount(dvp), td, cred,
9975fe58019SAttilio Rao 				    nid, 1);
9985fe58019SAttilio Rao 			}
9995fe58019SAttilio Rao 			fdisp_destroy(&fdi);
10005fe58019SAttilio Rao 			return err;
10015fe58019SAttilio Rao 		} else {
10025fe58019SAttilio Rao #ifndef NO_EARLY_PERM_CHECK_HACK
10035fe58019SAttilio Rao 			if (!islastcn) {
10045fe58019SAttilio Rao 				/*
10055fe58019SAttilio Rao 				 * We have the attributes of the next item
10065fe58019SAttilio Rao 				 * *now*, and it's a fact, and we do not
10075fe58019SAttilio Rao 				 * have to do extra work for it (ie, beg the
10085fe58019SAttilio Rao 				 * daemon), and it neither depends on such
10095fe58019SAttilio Rao 				 * accidental things like attr caching. So
10105fe58019SAttilio Rao 				 * the big idea: check credentials *now*,
10115fe58019SAttilio Rao 				 * not at the beginning of the next call to
10125fe58019SAttilio Rao 				 * lookup.
10135fe58019SAttilio Rao 				 *
10145fe58019SAttilio Rao 				 * The first item of the lookup chain (fs root)
10155fe58019SAttilio Rao 				 * won't be checked then here, of course, as
10165fe58019SAttilio Rao 				 * its never "the next". But go and see that
10175fe58019SAttilio Rao 				 * the root is taken care about at the very
10185fe58019SAttilio Rao 				 * beginning of this function.
10195fe58019SAttilio Rao 				 *
10205fe58019SAttilio Rao 				 * Now, given we want to do the access check
10215fe58019SAttilio Rao 				 * this way, one might ask: so then why not
10225fe58019SAttilio Rao 				 * do the access check just after fetching
10235fe58019SAttilio Rao 				 * the inode and its attributes from the
10245fe58019SAttilio Rao 				 * daemon? Why bother with producing the
10255fe58019SAttilio Rao 				 * corresponding vnode at all if something
10265fe58019SAttilio Rao 				 * is not OK? We know what's the deal as
10275fe58019SAttilio Rao 				 * soon as we get those attrs... There is
10285fe58019SAttilio Rao 				 * one bit of info though not given us by
10295fe58019SAttilio Rao 				 * the daemon: whether his response is
10305fe58019SAttilio Rao 				 * authorative or not... His response should
10315fe58019SAttilio Rao 				 * be ignored if something is mounted over
10325fe58019SAttilio Rao 				 * the dir in question. But that can be
10335fe58019SAttilio Rao 				 * known only by having the vnode...
10345fe58019SAttilio Rao 				 */
10355fe58019SAttilio Rao 				int tmpvtype = vnode_vtype(*vpp);
10365fe58019SAttilio Rao 
10375fe58019SAttilio Rao 				bzero(&facp, sizeof(facp));
10385fe58019SAttilio Rao 				/*the early perm check hack */
10395fe58019SAttilio Rao 				    facp.facc_flags |= FACCESS_VA_VALID;
10405fe58019SAttilio Rao 
10415fe58019SAttilio Rao 				if ((tmpvtype != VDIR) && (tmpvtype != VLNK)) {
10425fe58019SAttilio Rao 					err = ENOTDIR;
10435fe58019SAttilio Rao 				}
10445fe58019SAttilio Rao 				if (!err && !vnode_mountedhere(*vpp)) {
10455fe58019SAttilio Rao 					err = fuse_internal_access(*vpp, VEXEC, &facp, td, cred);
10465fe58019SAttilio Rao 				}
10475fe58019SAttilio Rao 				if (err) {
10485fe58019SAttilio Rao 					if (tmpvtype == VLNK)
10494cff153bSAttilio Rao 						FS_DEBUG("weird, permission error with a symlink?\n");
10505fe58019SAttilio Rao 					vput(*vpp);
10515fe58019SAttilio Rao 					*vpp = NULL;
10525fe58019SAttilio Rao 				}
10535fe58019SAttilio Rao 			}
10545fe58019SAttilio Rao #endif
10555fe58019SAttilio Rao 		}
10565fe58019SAttilio Rao 	}
10575fe58019SAttilio Rao 	fdisp_destroy(&fdi);
10585fe58019SAttilio Rao 
10595fe58019SAttilio Rao 	return err;
10605fe58019SAttilio Rao }
10615fe58019SAttilio Rao 
10625fe58019SAttilio Rao /*
10635fe58019SAttilio Rao     struct vnop_mkdir_args {
10645fe58019SAttilio Rao 	struct vnode *a_dvp;
10655fe58019SAttilio Rao 	struct vnode **a_vpp;
10665fe58019SAttilio Rao 	struct componentname *a_cnp;
10675fe58019SAttilio Rao 	struct vattr *a_vap;
10685fe58019SAttilio Rao     };
10695fe58019SAttilio Rao */
10705fe58019SAttilio Rao static int
10715fe58019SAttilio Rao fuse_vnop_mkdir(struct vop_mkdir_args *ap)
10725fe58019SAttilio Rao {
10735fe58019SAttilio Rao 	struct vnode *dvp = ap->a_dvp;
10745fe58019SAttilio Rao 	struct vnode **vpp = ap->a_vpp;
10755fe58019SAttilio Rao 	struct componentname *cnp = ap->a_cnp;
10765fe58019SAttilio Rao 	struct vattr *vap = ap->a_vap;
10775fe58019SAttilio Rao 
10785fe58019SAttilio Rao 	struct fuse_mkdir_in fmdi;
10795fe58019SAttilio Rao 
10805fe58019SAttilio Rao 	fuse_trace_printf_vnop();
10815fe58019SAttilio Rao 
10825fe58019SAttilio Rao 	if (fuse_isdeadfs(dvp)) {
10835fe58019SAttilio Rao 		return ENXIO;
10845fe58019SAttilio Rao 	}
10855fe58019SAttilio Rao 	fmdi.mode = MAKEIMODE(vap->va_type, vap->va_mode);
10865fe58019SAttilio Rao 
1087d9454fabSAttilio Rao 	return (fuse_internal_newentry(dvp, vpp, cnp, FUSE_MKDIR, &fmdi,
1088d9454fabSAttilio Rao 	    sizeof(fmdi), VDIR));
10895fe58019SAttilio Rao }
10905fe58019SAttilio Rao 
10915fe58019SAttilio Rao /*
10925fe58019SAttilio Rao     struct vnop_mknod_args {
10935fe58019SAttilio Rao 	struct vnode *a_dvp;
10945fe58019SAttilio Rao 	struct vnode **a_vpp;
10955fe58019SAttilio Rao 	struct componentname *a_cnp;
10965fe58019SAttilio Rao 	struct vattr *a_vap;
10975fe58019SAttilio Rao     };
10985fe58019SAttilio Rao */
10995fe58019SAttilio Rao static int
11005fe58019SAttilio Rao fuse_vnop_mknod(struct vop_mknod_args *ap)
11015fe58019SAttilio Rao {
11025fe58019SAttilio Rao 
11035fe58019SAttilio Rao 	return (EINVAL);
11045fe58019SAttilio Rao }
11055fe58019SAttilio Rao 
11065fe58019SAttilio Rao 
11075fe58019SAttilio Rao /*
11085fe58019SAttilio Rao     struct vnop_open_args {
11095fe58019SAttilio Rao 	struct vnode *a_vp;
11105fe58019SAttilio Rao 	int  a_mode;
11115fe58019SAttilio Rao 	struct ucred *a_cred;
11125fe58019SAttilio Rao 	struct thread *a_td;
11135fe58019SAttilio Rao 	int a_fdidx; / struct file *a_fp;
11145fe58019SAttilio Rao     };
11155fe58019SAttilio Rao */
11165fe58019SAttilio Rao static int
11175fe58019SAttilio Rao fuse_vnop_open(struct vop_open_args *ap)
11185fe58019SAttilio Rao {
11195fe58019SAttilio Rao 	struct vnode *vp = ap->a_vp;
11205fe58019SAttilio Rao 	int mode = ap->a_mode;
11215fe58019SAttilio Rao 	struct thread *td = ap->a_td;
11225fe58019SAttilio Rao 	struct ucred *cred = ap->a_cred;
11235fe58019SAttilio Rao 
11245fe58019SAttilio Rao 	fufh_type_t fufh_type;
11255fe58019SAttilio Rao 	struct fuse_vnode_data *fvdat;
11265fe58019SAttilio Rao 
11275fe58019SAttilio Rao 	int error, isdir = 0;
11285fe58019SAttilio Rao 
11294cff153bSAttilio Rao 	FS_DEBUG2G("inode=%ju mode=0x%x\n", (uintmax_t)VTOI(vp), mode);
11305fe58019SAttilio Rao 
11315fe58019SAttilio Rao 	if (fuse_isdeadfs(vp)) {
11325fe58019SAttilio Rao 		return ENXIO;
11335fe58019SAttilio Rao 	}
11345fe58019SAttilio Rao 	fvdat = VTOFUD(vp);
11355fe58019SAttilio Rao 
11365fe58019SAttilio Rao 	if (vnode_isdir(vp)) {
11375fe58019SAttilio Rao 		isdir = 1;
11385fe58019SAttilio Rao 	}
11395fe58019SAttilio Rao 	if (isdir) {
11405fe58019SAttilio Rao 		fufh_type = FUFH_RDONLY;
11415fe58019SAttilio Rao 	} else {
11425fe58019SAttilio Rao 		fufh_type = fuse_filehandle_xlate_from_fflags(mode);
11435fe58019SAttilio Rao 	}
11445fe58019SAttilio Rao 
11455fe58019SAttilio Rao 	if (fuse_filehandle_valid(vp, fufh_type)) {
11465fe58019SAttilio Rao 		fuse_vnode_open(vp, 0, td);
11475fe58019SAttilio Rao 		return 0;
11485fe58019SAttilio Rao 	}
11495fe58019SAttilio Rao 	error = fuse_filehandle_open(vp, fufh_type, NULL, td, cred);
11505fe58019SAttilio Rao 
11515fe58019SAttilio Rao 	return error;
11525fe58019SAttilio Rao }
11535fe58019SAttilio Rao 
11545fe58019SAttilio Rao /*
11555fe58019SAttilio Rao     struct vnop_read_args {
11565fe58019SAttilio Rao 	struct vnode *a_vp;
11575fe58019SAttilio Rao 	struct uio *a_uio;
11585fe58019SAttilio Rao 	int  a_ioflag;
11595fe58019SAttilio Rao 	struct ucred *a_cred;
11605fe58019SAttilio Rao     };
11615fe58019SAttilio Rao */
11625fe58019SAttilio Rao static int
11635fe58019SAttilio Rao fuse_vnop_read(struct vop_read_args *ap)
11645fe58019SAttilio Rao {
11655fe58019SAttilio Rao 	struct vnode *vp = ap->a_vp;
11665fe58019SAttilio Rao 	struct uio *uio = ap->a_uio;
11675fe58019SAttilio Rao 	int ioflag = ap->a_ioflag;
11685fe58019SAttilio Rao 	struct ucred *cred = ap->a_cred;
11695fe58019SAttilio Rao 
11704cff153bSAttilio Rao 	FS_DEBUG2G("inode=%ju offset=%jd resid=%zd\n",
11715fe58019SAttilio Rao 	    (uintmax_t)VTOI(vp), uio->uio_offset, uio->uio_resid);
11725fe58019SAttilio Rao 
11735fe58019SAttilio Rao 	if (fuse_isdeadfs(vp)) {
11745fe58019SAttilio Rao 		return ENXIO;
11755fe58019SAttilio Rao 	}
11765fe58019SAttilio Rao 	return fuse_io_dispatch(vp, uio, ioflag, cred);
11775fe58019SAttilio Rao }
11785fe58019SAttilio Rao 
11795fe58019SAttilio Rao /*
11805fe58019SAttilio Rao     struct vnop_readdir_args {
11815fe58019SAttilio Rao 	struct vnode *a_vp;
11825fe58019SAttilio Rao 	struct uio *a_uio;
11835fe58019SAttilio Rao 	struct ucred *a_cred;
11845fe58019SAttilio Rao 	int *a_eofflag;
11855fe58019SAttilio Rao 	int *ncookies;
11865fe58019SAttilio Rao 	u_long **a_cookies;
11875fe58019SAttilio Rao     };
11885fe58019SAttilio Rao */
11895fe58019SAttilio Rao static int
11905fe58019SAttilio Rao fuse_vnop_readdir(struct vop_readdir_args *ap)
11915fe58019SAttilio Rao {
11925fe58019SAttilio Rao 	struct vnode *vp = ap->a_vp;
11935fe58019SAttilio Rao 	struct uio *uio = ap->a_uio;
11945fe58019SAttilio Rao 	struct ucred *cred = ap->a_cred;
11955fe58019SAttilio Rao 
11965fe58019SAttilio Rao 	struct fuse_filehandle *fufh = NULL;
11975fe58019SAttilio Rao 	struct fuse_vnode_data *fvdat;
11985fe58019SAttilio Rao 	struct fuse_iov cookediov;
11995fe58019SAttilio Rao 
12005fe58019SAttilio Rao 	int err = 0;
12015fe58019SAttilio Rao 	int freefufh = 0;
12025fe58019SAttilio Rao 
12034cff153bSAttilio Rao 	FS_DEBUG2G("inode=%ju\n", (uintmax_t)VTOI(vp));
12045fe58019SAttilio Rao 
12055fe58019SAttilio Rao 	if (fuse_isdeadfs(vp)) {
12065fe58019SAttilio Rao 		return ENXIO;
12075fe58019SAttilio Rao 	}
12085fe58019SAttilio Rao 	if (				/* XXXIP ((uio_iovcnt(uio) > 1)) || */
12095fe58019SAttilio Rao 	    (uio_resid(uio) < sizeof(struct dirent))) {
12105fe58019SAttilio Rao 		return EINVAL;
12115fe58019SAttilio Rao 	}
12125fe58019SAttilio Rao 	fvdat = VTOFUD(vp);
12135fe58019SAttilio Rao 
12145fe58019SAttilio Rao 	if (!fuse_filehandle_valid(vp, FUFH_RDONLY)) {
12154cff153bSAttilio Rao 		FS_DEBUG("calling readdir() before open()");
12165fe58019SAttilio Rao 		err = fuse_filehandle_open(vp, FUFH_RDONLY, &fufh, NULL, cred);
12175fe58019SAttilio Rao 		freefufh = 1;
12185fe58019SAttilio Rao 	} else {
12195fe58019SAttilio Rao 		err = fuse_filehandle_get(vp, FUFH_RDONLY, &fufh);
12205fe58019SAttilio Rao 	}
12215fe58019SAttilio Rao 	if (err) {
12225fe58019SAttilio Rao 		return (err);
12235fe58019SAttilio Rao 	}
12245fe58019SAttilio Rao #define DIRCOOKEDSIZE FUSE_DIRENT_ALIGN(FUSE_NAME_OFFSET + MAXNAMLEN + 1)
12255fe58019SAttilio Rao 	fiov_init(&cookediov, DIRCOOKEDSIZE);
12265fe58019SAttilio Rao 
12275fe58019SAttilio Rao 	err = fuse_internal_readdir(vp, uio, fufh, &cookediov);
12285fe58019SAttilio Rao 
12295fe58019SAttilio Rao 	fiov_teardown(&cookediov);
12305fe58019SAttilio Rao 	if (freefufh) {
12315fe58019SAttilio Rao 		fuse_filehandle_close(vp, FUFH_RDONLY, NULL, cred);
12325fe58019SAttilio Rao 	}
12335fe58019SAttilio Rao 	return err;
12345fe58019SAttilio Rao }
12355fe58019SAttilio Rao 
12365fe58019SAttilio Rao /*
12375fe58019SAttilio Rao     struct vnop_readlink_args {
12385fe58019SAttilio Rao 	struct vnode *a_vp;
12395fe58019SAttilio Rao 	struct uio *a_uio;
12405fe58019SAttilio Rao 	struct ucred *a_cred;
12415fe58019SAttilio Rao     };
12425fe58019SAttilio Rao */
12435fe58019SAttilio Rao static int
12445fe58019SAttilio Rao fuse_vnop_readlink(struct vop_readlink_args *ap)
12455fe58019SAttilio Rao {
12465fe58019SAttilio Rao 	struct vnode *vp = ap->a_vp;
12475fe58019SAttilio Rao 	struct uio *uio = ap->a_uio;
12485fe58019SAttilio Rao 	struct ucred *cred = ap->a_cred;
12495fe58019SAttilio Rao 
12505fe58019SAttilio Rao 	struct fuse_dispatcher fdi;
12515fe58019SAttilio Rao 	int err;
12525fe58019SAttilio Rao 
12534cff153bSAttilio Rao 	FS_DEBUG2G("inode=%ju\n", (uintmax_t)VTOI(vp));
12545fe58019SAttilio Rao 
12555fe58019SAttilio Rao 	if (fuse_isdeadfs(vp)) {
12565fe58019SAttilio Rao 		return ENXIO;
12575fe58019SAttilio Rao 	}
12585fe58019SAttilio Rao 	if (!vnode_islnk(vp)) {
12595fe58019SAttilio Rao 		return EINVAL;
12605fe58019SAttilio Rao 	}
12615fe58019SAttilio Rao 	fdisp_init(&fdi, 0);
12625fe58019SAttilio Rao 	err = fdisp_simple_putget_vp(&fdi, FUSE_READLINK, vp, curthread, cred);
12635fe58019SAttilio Rao 	if (err) {
12645fe58019SAttilio Rao 		goto out;
12655fe58019SAttilio Rao 	}
12665fe58019SAttilio Rao 	if (((char *)fdi.answ)[0] == '/' &&
12675fe58019SAttilio Rao 	    fuse_get_mpdata(vnode_mount(vp))->dataflags & FSESS_PUSH_SYMLINKS_IN) {
12685fe58019SAttilio Rao 		char *mpth = vnode_mount(vp)->mnt_stat.f_mntonname;
12695fe58019SAttilio Rao 
12705fe58019SAttilio Rao 		err = uiomove(mpth, strlen(mpth), uio);
12715fe58019SAttilio Rao 	}
12725fe58019SAttilio Rao 	if (!err) {
12735fe58019SAttilio Rao 		err = uiomove(fdi.answ, fdi.iosize, uio);
12745fe58019SAttilio Rao 	}
12755fe58019SAttilio Rao out:
12765fe58019SAttilio Rao 	fdisp_destroy(&fdi);
12775fe58019SAttilio Rao 	return err;
12785fe58019SAttilio Rao }
12795fe58019SAttilio Rao 
12805fe58019SAttilio Rao /*
12815fe58019SAttilio Rao     struct vnop_reclaim_args {
12825fe58019SAttilio Rao 	struct vnode *a_vp;
12835fe58019SAttilio Rao 	struct thread *a_td;
12845fe58019SAttilio Rao     };
12855fe58019SAttilio Rao */
12865fe58019SAttilio Rao static int
12875fe58019SAttilio Rao fuse_vnop_reclaim(struct vop_reclaim_args *ap)
12885fe58019SAttilio Rao {
12895fe58019SAttilio Rao 	struct vnode *vp = ap->a_vp;
12905fe58019SAttilio Rao 	struct thread *td = ap->a_td;
12915fe58019SAttilio Rao 
12925fe58019SAttilio Rao 	struct fuse_vnode_data *fvdat = VTOFUD(vp);
12935fe58019SAttilio Rao 	struct fuse_filehandle *fufh = NULL;
12945fe58019SAttilio Rao 
12955fe58019SAttilio Rao 	int type;
12965fe58019SAttilio Rao 
12975fe58019SAttilio Rao 	if (!fvdat) {
12985fe58019SAttilio Rao 		panic("FUSE: no vnode data during recycling");
12995fe58019SAttilio Rao 	}
13004cff153bSAttilio Rao 	FS_DEBUG("inode=%ju\n", (uintmax_t)VTOI(vp));
13015fe58019SAttilio Rao 
13025fe58019SAttilio Rao 	for (type = 0; type < FUFH_MAXTYPE; type++) {
13035fe58019SAttilio Rao 		fufh = &(fvdat->fufh[type]);
13045fe58019SAttilio Rao 		if (FUFH_IS_VALID(fufh)) {
13055fe58019SAttilio Rao 			printf("FUSE: vnode being reclaimed but fufh (type=%d) is valid",
13065fe58019SAttilio Rao 			    type);
13075fe58019SAttilio Rao 			fuse_filehandle_close(vp, type, td, NULL);
13085fe58019SAttilio Rao 		}
13095fe58019SAttilio Rao 	}
13105fe58019SAttilio Rao 
13115fe58019SAttilio Rao 	if ((!fuse_isdeadfs(vp)) && (fvdat->nlookup)) {
13125fe58019SAttilio Rao 		fuse_internal_forget_send(vnode_mount(vp), td, NULL, VTOI(vp),
13135fe58019SAttilio Rao 		    fvdat->nlookup);
13145fe58019SAttilio Rao 	}
13155fe58019SAttilio Rao 	fuse_vnode_setparent(vp, NULL);
13165fe58019SAttilio Rao 	cache_purge(vp);
13175fe58019SAttilio Rao 	vfs_hash_remove(vp);
13185fe58019SAttilio Rao 	vnode_destroy_vobject(vp);
13195fe58019SAttilio Rao 	fuse_vnode_destroy(vp);
13205fe58019SAttilio Rao 
13215fe58019SAttilio Rao 	return 0;
13225fe58019SAttilio Rao }
13235fe58019SAttilio Rao 
13245fe58019SAttilio Rao /*
13255fe58019SAttilio Rao     struct vnop_remove_args {
13265fe58019SAttilio Rao 	struct vnode *a_dvp;
13275fe58019SAttilio Rao 	struct vnode *a_vp;
13285fe58019SAttilio Rao 	struct componentname *a_cnp;
13295fe58019SAttilio Rao     };
13305fe58019SAttilio Rao */
13315fe58019SAttilio Rao static int
13325fe58019SAttilio Rao fuse_vnop_remove(struct vop_remove_args *ap)
13335fe58019SAttilio Rao {
13345fe58019SAttilio Rao 	struct vnode *dvp = ap->a_dvp;
13355fe58019SAttilio Rao 	struct vnode *vp = ap->a_vp;
13365fe58019SAttilio Rao 	struct componentname *cnp = ap->a_cnp;
13375fe58019SAttilio Rao 
13385fe58019SAttilio Rao 	int err;
13395fe58019SAttilio Rao 
13404cff153bSAttilio Rao 	FS_DEBUG2G("inode=%ju name=%*s\n",
13415fe58019SAttilio Rao 	    (uintmax_t)VTOI(vp), (int)cnp->cn_namelen, cnp->cn_nameptr);
13425fe58019SAttilio Rao 
13435fe58019SAttilio Rao 	if (fuse_isdeadfs(vp)) {
13445fe58019SAttilio Rao 		return ENXIO;
13455fe58019SAttilio Rao 	}
13465fe58019SAttilio Rao 	if (vnode_isdir(vp)) {
13475fe58019SAttilio Rao 		return EPERM;
13485fe58019SAttilio Rao 	}
13495fe58019SAttilio Rao 	cache_purge(vp);
13505fe58019SAttilio Rao 
13515fe58019SAttilio Rao 	err = fuse_internal_remove(dvp, vp, cnp, FUSE_UNLINK);
13525fe58019SAttilio Rao 
1353d9454fabSAttilio Rao 	if (err == 0)
13545fe58019SAttilio Rao 		fuse_internal_vnode_disappear(vp);
13555fe58019SAttilio Rao 	return err;
13565fe58019SAttilio Rao }
13575fe58019SAttilio Rao 
13585fe58019SAttilio Rao /*
13595fe58019SAttilio Rao     struct vnop_rename_args {
13605fe58019SAttilio Rao 	struct vnode *a_fdvp;
13615fe58019SAttilio Rao 	struct vnode *a_fvp;
13625fe58019SAttilio Rao 	struct componentname *a_fcnp;
13635fe58019SAttilio Rao 	struct vnode *a_tdvp;
13645fe58019SAttilio Rao 	struct vnode *a_tvp;
13655fe58019SAttilio Rao 	struct componentname *a_tcnp;
13665fe58019SAttilio Rao     };
13675fe58019SAttilio Rao */
13685fe58019SAttilio Rao static int
13695fe58019SAttilio Rao fuse_vnop_rename(struct vop_rename_args *ap)
13705fe58019SAttilio Rao {
13715fe58019SAttilio Rao 	struct vnode *fdvp = ap->a_fdvp;
13725fe58019SAttilio Rao 	struct vnode *fvp = ap->a_fvp;
13735fe58019SAttilio Rao 	struct componentname *fcnp = ap->a_fcnp;
13745fe58019SAttilio Rao 	struct vnode *tdvp = ap->a_tdvp;
13755fe58019SAttilio Rao 	struct vnode *tvp = ap->a_tvp;
13765fe58019SAttilio Rao 	struct componentname *tcnp = ap->a_tcnp;
13775fe58019SAttilio Rao 	struct fuse_data *data;
13785fe58019SAttilio Rao 
13795fe58019SAttilio Rao 	int err = 0;
13805fe58019SAttilio Rao 
13814cff153bSAttilio Rao 	FS_DEBUG2G("from: inode=%ju name=%*s -> to: inode=%ju name=%*s\n",
13825fe58019SAttilio Rao 	    (uintmax_t)VTOI(fvp), (int)fcnp->cn_namelen, fcnp->cn_nameptr,
13835fe58019SAttilio Rao 	    (uintmax_t)(tvp == NULL ? -1 : VTOI(tvp)),
13845fe58019SAttilio Rao 	    (int)tcnp->cn_namelen, tcnp->cn_nameptr);
13855fe58019SAttilio Rao 
13865fe58019SAttilio Rao 	if (fuse_isdeadfs(fdvp)) {
13875fe58019SAttilio Rao 		return ENXIO;
13885fe58019SAttilio Rao 	}
13895fe58019SAttilio Rao 	if (fvp->v_mount != tdvp->v_mount ||
13905fe58019SAttilio Rao 	    (tvp && fvp->v_mount != tvp->v_mount)) {
13914cff153bSAttilio Rao 		FS_DEBUG("cross-device rename: %s -> %s\n",
13925fe58019SAttilio Rao 		    fcnp->cn_nameptr, (tcnp != NULL ? tcnp->cn_nameptr : "(NULL)"));
13935fe58019SAttilio Rao 		err = EXDEV;
13945fe58019SAttilio Rao 		goto out;
13955fe58019SAttilio Rao 	}
13965fe58019SAttilio Rao 	cache_purge(fvp);
13975fe58019SAttilio Rao 
13985fe58019SAttilio Rao 	/*
13995fe58019SAttilio Rao          * FUSE library is expected to check if target directory is not
14005fe58019SAttilio Rao          * under the source directory in the file system tree.
14015fe58019SAttilio Rao          * Linux performs this check at VFS level.
14025fe58019SAttilio Rao          */
14035fe58019SAttilio Rao 	data = fuse_get_mpdata(vnode_mount(tdvp));
14045fe58019SAttilio Rao 	sx_xlock(&data->rename_lock);
14055fe58019SAttilio Rao 	err = fuse_internal_rename(fdvp, fcnp, tdvp, tcnp);
14065fe58019SAttilio Rao 	if (err == 0) {
1407d9454fabSAttilio Rao 		if (tdvp != fdvp)
14085fe58019SAttilio Rao 			fuse_vnode_setparent(fvp, tdvp);
14095fe58019SAttilio Rao 		if (tvp != NULL)
14105fe58019SAttilio Rao 			fuse_vnode_setparent(tvp, NULL);
14115fe58019SAttilio Rao 	}
14125fe58019SAttilio Rao 	sx_unlock(&data->rename_lock);
14135fe58019SAttilio Rao 
14145fe58019SAttilio Rao 	if (tvp != NULL && tvp != fvp) {
14155fe58019SAttilio Rao 		cache_purge(tvp);
14165fe58019SAttilio Rao 	}
14175fe58019SAttilio Rao 	if (vnode_isdir(fvp)) {
14185fe58019SAttilio Rao 		if ((tvp != NULL) && vnode_isdir(tvp)) {
14195fe58019SAttilio Rao 			cache_purge(tdvp);
14205fe58019SAttilio Rao 		}
14215fe58019SAttilio Rao 		cache_purge(fdvp);
14225fe58019SAttilio Rao 	}
14235fe58019SAttilio Rao out:
14245fe58019SAttilio Rao 	if (tdvp == tvp) {
14255fe58019SAttilio Rao 		vrele(tdvp);
14265fe58019SAttilio Rao 	} else {
14275fe58019SAttilio Rao 		vput(tdvp);
14285fe58019SAttilio Rao 	}
14295fe58019SAttilio Rao 	if (tvp != NULL) {
14305fe58019SAttilio Rao 		vput(tvp);
14315fe58019SAttilio Rao 	}
14325fe58019SAttilio Rao 	vrele(fdvp);
14335fe58019SAttilio Rao 	vrele(fvp);
14345fe58019SAttilio Rao 
14355fe58019SAttilio Rao 	return err;
14365fe58019SAttilio Rao }
14375fe58019SAttilio Rao 
14385fe58019SAttilio Rao /*
14395fe58019SAttilio Rao     struct vnop_rmdir_args {
14405fe58019SAttilio Rao 	    struct vnode *a_dvp;
14415fe58019SAttilio Rao 	    struct vnode *a_vp;
14425fe58019SAttilio Rao 	    struct componentname *a_cnp;
14435fe58019SAttilio Rao     } *ap;
14445fe58019SAttilio Rao */
14455fe58019SAttilio Rao static int
14465fe58019SAttilio Rao fuse_vnop_rmdir(struct vop_rmdir_args *ap)
14475fe58019SAttilio Rao {
14485fe58019SAttilio Rao 	struct vnode *dvp = ap->a_dvp;
14495fe58019SAttilio Rao 	struct vnode *vp = ap->a_vp;
14505fe58019SAttilio Rao 
14515fe58019SAttilio Rao 	int err;
14525fe58019SAttilio Rao 
14534cff153bSAttilio Rao 	FS_DEBUG2G("inode=%ju\n", (uintmax_t)VTOI(vp));
14545fe58019SAttilio Rao 
14555fe58019SAttilio Rao 	if (fuse_isdeadfs(vp)) {
14565fe58019SAttilio Rao 		return ENXIO;
14575fe58019SAttilio Rao 	}
14585fe58019SAttilio Rao 	if (VTOFUD(vp) == VTOFUD(dvp)) {
14595fe58019SAttilio Rao 		return EINVAL;
14605fe58019SAttilio Rao 	}
14615fe58019SAttilio Rao 	err = fuse_internal_remove(dvp, vp, ap->a_cnp, FUSE_RMDIR);
14625fe58019SAttilio Rao 
1463d9454fabSAttilio Rao 	if (err == 0)
14645fe58019SAttilio Rao 		fuse_internal_vnode_disappear(vp);
14655fe58019SAttilio Rao 	return err;
14665fe58019SAttilio Rao }
14675fe58019SAttilio Rao 
14685fe58019SAttilio Rao /*
14695fe58019SAttilio Rao     struct vnop_setattr_args {
14705fe58019SAttilio Rao 	struct vnode *a_vp;
14715fe58019SAttilio Rao 	struct vattr *a_vap;
14725fe58019SAttilio Rao 	struct ucred *a_cred;
14735fe58019SAttilio Rao 	struct thread *a_td;
14745fe58019SAttilio Rao     };
14755fe58019SAttilio Rao */
14765fe58019SAttilio Rao static int
14775fe58019SAttilio Rao fuse_vnop_setattr(struct vop_setattr_args *ap)
14785fe58019SAttilio Rao {
14795fe58019SAttilio Rao 	struct vnode *vp = ap->a_vp;
14805fe58019SAttilio Rao 	struct vattr *vap = ap->a_vap;
14815fe58019SAttilio Rao 	struct ucred *cred = ap->a_cred;
14825fe58019SAttilio Rao 	struct thread *td = curthread;
14835fe58019SAttilio Rao 
14845fe58019SAttilio Rao 	struct fuse_dispatcher fdi;
14855fe58019SAttilio Rao 	struct fuse_setattr_in *fsai;
14865fe58019SAttilio Rao 	struct fuse_access_param facp;
14875fe58019SAttilio Rao 
14885fe58019SAttilio Rao 	int err = 0;
14895fe58019SAttilio Rao 	enum vtype vtyp;
14905fe58019SAttilio Rao 	int sizechanged = 0;
14915fe58019SAttilio Rao 	uint64_t newsize = 0;
14925fe58019SAttilio Rao 
14934cff153bSAttilio Rao 	FS_DEBUG2G("inode=%ju\n", (uintmax_t)VTOI(vp));
14945fe58019SAttilio Rao 
14955fe58019SAttilio Rao 	if (fuse_isdeadfs(vp)) {
14965fe58019SAttilio Rao 		return ENXIO;
14975fe58019SAttilio Rao 	}
14985fe58019SAttilio Rao 	fdisp_init(&fdi, sizeof(*fsai));
14995fe58019SAttilio Rao 	fdisp_make_vp(&fdi, FUSE_SETATTR, vp, td, cred);
15005fe58019SAttilio Rao 	fsai = fdi.indata;
15015fe58019SAttilio Rao 	fsai->valid = 0;
15025fe58019SAttilio Rao 
15035fe58019SAttilio Rao 	bzero(&facp, sizeof(facp));
15045fe58019SAttilio Rao 
15055fe58019SAttilio Rao 	facp.xuid = vap->va_uid;
15065fe58019SAttilio Rao 	facp.xgid = vap->va_gid;
15075fe58019SAttilio Rao 
15085fe58019SAttilio Rao 	if (vap->va_uid != (uid_t)VNOVAL) {
15095fe58019SAttilio Rao 		facp.facc_flags |= FACCESS_CHOWN;
15105fe58019SAttilio Rao 		fsai->uid = vap->va_uid;
15115fe58019SAttilio Rao 		fsai->valid |= FATTR_UID;
15125fe58019SAttilio Rao 	}
15135fe58019SAttilio Rao 	if (vap->va_gid != (gid_t)VNOVAL) {
15145fe58019SAttilio Rao 		facp.facc_flags |= FACCESS_CHOWN;
15155fe58019SAttilio Rao 		fsai->gid = vap->va_gid;
15165fe58019SAttilio Rao 		fsai->valid |= FATTR_GID;
15175fe58019SAttilio Rao 	}
15185fe58019SAttilio Rao 	if (vap->va_size != VNOVAL) {
15195fe58019SAttilio Rao 
15205fe58019SAttilio Rao 		struct fuse_filehandle *fufh = NULL;
15215fe58019SAttilio Rao 
15225fe58019SAttilio Rao 		/*Truncate to a new value. */
15235fe58019SAttilio Rao 		    fsai->size = vap->va_size;
15245fe58019SAttilio Rao 		sizechanged = 1;
15255fe58019SAttilio Rao 		newsize = vap->va_size;
15265fe58019SAttilio Rao 		fsai->valid |= FATTR_SIZE;
15275fe58019SAttilio Rao 
15285fe58019SAttilio Rao 		fuse_filehandle_getrw(vp, FUFH_WRONLY, &fufh);
15295fe58019SAttilio Rao 		if (fufh) {
15305fe58019SAttilio Rao 			fsai->fh = fufh->fh_id;
15315fe58019SAttilio Rao 			fsai->valid |= FATTR_FH;
15325fe58019SAttilio Rao 		}
15335fe58019SAttilio Rao 	}
15345fe58019SAttilio Rao 	if (vap->va_atime.tv_sec != VNOVAL) {
15355fe58019SAttilio Rao 		fsai->atime = vap->va_atime.tv_sec;
15365fe58019SAttilio Rao 		fsai->atimensec = vap->va_atime.tv_nsec;
15375fe58019SAttilio Rao 		fsai->valid |= FATTR_ATIME;
15385fe58019SAttilio Rao 	}
15395fe58019SAttilio Rao 	if (vap->va_mtime.tv_sec != VNOVAL) {
15405fe58019SAttilio Rao 		fsai->mtime = vap->va_mtime.tv_sec;
15415fe58019SAttilio Rao 		fsai->mtimensec = vap->va_mtime.tv_nsec;
15425fe58019SAttilio Rao 		fsai->valid |= FATTR_MTIME;
15435fe58019SAttilio Rao 	}
15445fe58019SAttilio Rao 	if (vap->va_mode != (mode_t)VNOVAL) {
15455fe58019SAttilio Rao 		fsai->mode = vap->va_mode & ALLPERMS;
15465fe58019SAttilio Rao 		fsai->valid |= FATTR_MODE;
15475fe58019SAttilio Rao 	}
15485fe58019SAttilio Rao 	if (!fsai->valid) {
15495fe58019SAttilio Rao 		goto out;
15505fe58019SAttilio Rao 	}
15515fe58019SAttilio Rao 	vtyp = vnode_vtype(vp);
15525fe58019SAttilio Rao 
15535fe58019SAttilio Rao 	if (fsai->valid & FATTR_SIZE && vtyp == VDIR) {
15545fe58019SAttilio Rao 		err = EISDIR;
15555fe58019SAttilio Rao 		goto out;
15565fe58019SAttilio Rao 	}
15575fe58019SAttilio Rao 	if (vfs_isrdonly(vnode_mount(vp)) && (fsai->valid & ~FATTR_SIZE || vtyp == VREG)) {
15585fe58019SAttilio Rao 		err = EROFS;
15595fe58019SAttilio Rao 		goto out;
15605fe58019SAttilio Rao 	}
15615fe58019SAttilio Rao 	if (fsai->valid & ~FATTR_SIZE) {
15625fe58019SAttilio Rao 	  /*err = fuse_internal_access(vp, VADMIN, context, &facp); */
15635fe58019SAttilio Rao 	  /*XXX */
15645fe58019SAttilio Rao 		    err = 0;
15655fe58019SAttilio Rao 	}
15665fe58019SAttilio Rao 	facp.facc_flags &= ~FACCESS_XQUERIES;
15675fe58019SAttilio Rao 
15685fe58019SAttilio Rao 	if (err && !(fsai->valid & ~(FATTR_ATIME | FATTR_MTIME)) &&
15695fe58019SAttilio Rao 	    vap->va_vaflags & VA_UTIMES_NULL) {
15705fe58019SAttilio Rao 		err = fuse_internal_access(vp, VWRITE, &facp, td, cred);
15715fe58019SAttilio Rao 	}
1572d9454fabSAttilio Rao 	if (err)
15735fe58019SAttilio Rao 		goto out;
1574d9454fabSAttilio Rao 	if ((err = fdisp_wait_answ(&fdi)))
15755fe58019SAttilio Rao 		goto out;
15765fe58019SAttilio Rao 	vtyp = IFTOVT(((struct fuse_attr_out *)fdi.answ)->attr.mode);
15775fe58019SAttilio Rao 
15785fe58019SAttilio Rao 	if (vnode_vtype(vp) != vtyp) {
15795fe58019SAttilio Rao 		if (vnode_vtype(vp) == VNON && vtyp != VNON) {
15805fe58019SAttilio Rao 			debug_printf("FUSE: Dang! vnode_vtype is VNON and vtype isn't.\n");
15815fe58019SAttilio Rao 		} else {
15825fe58019SAttilio Rao 			/*
15835fe58019SAttilio Rao 	                 * STALE vnode, ditch
15845fe58019SAttilio Rao 	                 *
15855fe58019SAttilio Rao 	                 * The vnode has changed its type "behind our back". There's
15865fe58019SAttilio Rao 	                 * nothing really we can do, so let us just force an internal
15875fe58019SAttilio Rao 	                 * revocation and tell the caller to try again, if interested.
15885fe58019SAttilio Rao 	                 */
15895fe58019SAttilio Rao 			fuse_internal_vnode_disappear(vp);
15905fe58019SAttilio Rao 			err = EAGAIN;
15915fe58019SAttilio Rao 		}
15925fe58019SAttilio Rao 	}
15935fe58019SAttilio Rao 	if (!err && !sizechanged) {
15945fe58019SAttilio Rao 		cache_attrs(vp, (struct fuse_attr_out *)fdi.answ);
15955fe58019SAttilio Rao 	}
15965fe58019SAttilio Rao out:
15975fe58019SAttilio Rao 	fdisp_destroy(&fdi);
15985fe58019SAttilio Rao 	if (!err && sizechanged) {
15995fe58019SAttilio Rao 		fuse_vnode_setsize(vp, cred, newsize);
16005fe58019SAttilio Rao 		VTOFUD(vp)->flag &= ~FN_SIZECHANGE;
16015fe58019SAttilio Rao 	}
16025fe58019SAttilio Rao 	return err;
16035fe58019SAttilio Rao }
16045fe58019SAttilio Rao 
16055fe58019SAttilio Rao /*
16065fe58019SAttilio Rao     struct vnop_strategy_args {
16075fe58019SAttilio Rao 	struct vnode *a_vp;
16085fe58019SAttilio Rao 	struct buf *a_bp;
16095fe58019SAttilio Rao     };
16105fe58019SAttilio Rao */
16115fe58019SAttilio Rao static int
16125fe58019SAttilio Rao fuse_vnop_strategy(struct vop_strategy_args *ap)
16135fe58019SAttilio Rao {
16145fe58019SAttilio Rao 	struct vnode *vp = ap->a_vp;
16155fe58019SAttilio Rao 	struct buf *bp = ap->a_bp;
16165fe58019SAttilio Rao 
16175fe58019SAttilio Rao 	fuse_trace_printf_vnop();
16185fe58019SAttilio Rao 
16195fe58019SAttilio Rao 	if (!vp || fuse_isdeadfs(vp)) {
16205fe58019SAttilio Rao 		bp->b_ioflags |= BIO_ERROR;
16215fe58019SAttilio Rao 		bp->b_error = ENXIO;
16225fe58019SAttilio Rao 		bufdone(bp);
16235fe58019SAttilio Rao 		return ENXIO;
16245fe58019SAttilio Rao 	}
16255fe58019SAttilio Rao 	if (bp->b_iocmd == BIO_WRITE)
16265fe58019SAttilio Rao 		fuse_vnode_refreshsize(vp, NOCRED);
16275fe58019SAttilio Rao 
16285fe58019SAttilio Rao 	(void)fuse_io_strategy(vp, bp);
16295fe58019SAttilio Rao 
16305fe58019SAttilio Rao 	/*
16315fe58019SAttilio Rao 	 * This is a dangerous function. If returns error, that might mean a
16325fe58019SAttilio Rao 	 * panic. We prefer pretty much anything over being forced to panic
16335fe58019SAttilio Rao 	 * by a malicious daemon (a demon?). So we just return 0 anyway. You
16345fe58019SAttilio Rao 	 * should never mind this: this function has its own error
16355fe58019SAttilio Rao 	 * propagation mechanism via the argument buffer, so
16365fe58019SAttilio Rao 	 * not-that-melodramatic residents of the call chain still will be
16375fe58019SAttilio Rao 	 * able to know what to do.
16385fe58019SAttilio Rao 	 */
16395fe58019SAttilio Rao 	return 0;
16405fe58019SAttilio Rao }
16415fe58019SAttilio Rao 
16425fe58019SAttilio Rao 
16435fe58019SAttilio Rao /*
16445fe58019SAttilio Rao     struct vnop_symlink_args {
16455fe58019SAttilio Rao 	struct vnode *a_dvp;
16465fe58019SAttilio Rao 	struct vnode **a_vpp;
16475fe58019SAttilio Rao 	struct componentname *a_cnp;
16485fe58019SAttilio Rao 	struct vattr *a_vap;
16495fe58019SAttilio Rao 	char *a_target;
16505fe58019SAttilio Rao     };
16515fe58019SAttilio Rao */
16525fe58019SAttilio Rao static int
16535fe58019SAttilio Rao fuse_vnop_symlink(struct vop_symlink_args *ap)
16545fe58019SAttilio Rao {
16555fe58019SAttilio Rao 	struct vnode *dvp = ap->a_dvp;
16565fe58019SAttilio Rao 	struct vnode **vpp = ap->a_vpp;
16575fe58019SAttilio Rao 	struct componentname *cnp = ap->a_cnp;
16585fe58019SAttilio Rao 	char *target = ap->a_target;
16595fe58019SAttilio Rao 
16605fe58019SAttilio Rao 	struct fuse_dispatcher fdi;
16615fe58019SAttilio Rao 
16625fe58019SAttilio Rao 	int err;
16635fe58019SAttilio Rao 	size_t len;
16645fe58019SAttilio Rao 
16654cff153bSAttilio Rao 	FS_DEBUG2G("inode=%ju name=%*s\n",
16665fe58019SAttilio Rao 	    (uintmax_t)VTOI(dvp), (int)cnp->cn_namelen, cnp->cn_nameptr);
16675fe58019SAttilio Rao 
16685fe58019SAttilio Rao 	if (fuse_isdeadfs(dvp)) {
16695fe58019SAttilio Rao 		return ENXIO;
16705fe58019SAttilio Rao 	}
16715fe58019SAttilio Rao 	/*
16725fe58019SAttilio Rao          * Unlike the other creator type calls, here we have to create a message
16735fe58019SAttilio Rao          * where the name of the new entry comes first, and the data describing
16745fe58019SAttilio Rao          * the entry comes second.
16755fe58019SAttilio Rao          * Hence we can't rely on our handy fuse_internal_newentry() routine,
16765fe58019SAttilio Rao          * but put together the message manually and just call the core part.
16775fe58019SAttilio Rao          */
16785fe58019SAttilio Rao 
16795fe58019SAttilio Rao 	len = strlen(target) + 1;
16805fe58019SAttilio Rao 	fdisp_init(&fdi, len + cnp->cn_namelen + 1);
16815fe58019SAttilio Rao 	fdisp_make_vp(&fdi, FUSE_SYMLINK, dvp, curthread, NULL);
16825fe58019SAttilio Rao 
16835fe58019SAttilio Rao 	memcpy(fdi.indata, cnp->cn_nameptr, cnp->cn_namelen);
16845fe58019SAttilio Rao 	((char *)fdi.indata)[cnp->cn_namelen] = '\0';
16855fe58019SAttilio Rao 	memcpy((char *)fdi.indata + cnp->cn_namelen + 1, target, len);
16865fe58019SAttilio Rao 
16875fe58019SAttilio Rao 	err = fuse_internal_newentry_core(dvp, vpp, cnp, VLNK, &fdi);
16885fe58019SAttilio Rao 	fdisp_destroy(&fdi);
16895fe58019SAttilio Rao 	return err;
16905fe58019SAttilio Rao }
16915fe58019SAttilio Rao 
16925fe58019SAttilio Rao /*
16935fe58019SAttilio Rao     struct vnop_write_args {
16945fe58019SAttilio Rao 	struct vnode *a_vp;
16955fe58019SAttilio Rao 	struct uio *a_uio;
16965fe58019SAttilio Rao 	int  a_ioflag;
16975fe58019SAttilio Rao 	struct ucred *a_cred;
16985fe58019SAttilio Rao     };
16995fe58019SAttilio Rao */
17005fe58019SAttilio Rao static int
17015fe58019SAttilio Rao fuse_vnop_write(struct vop_write_args *ap)
17025fe58019SAttilio Rao {
17035fe58019SAttilio Rao 	struct vnode *vp = ap->a_vp;
17045fe58019SAttilio Rao 	struct uio *uio = ap->a_uio;
17055fe58019SAttilio Rao 	int ioflag = ap->a_ioflag;
17065fe58019SAttilio Rao 	struct ucred *cred = ap->a_cred;
17075fe58019SAttilio Rao 
17085fe58019SAttilio Rao 	fuse_trace_printf_vnop();
17095fe58019SAttilio Rao 
17105fe58019SAttilio Rao 	if (fuse_isdeadfs(vp)) {
17115fe58019SAttilio Rao 		return ENXIO;
17125fe58019SAttilio Rao 	}
17135fe58019SAttilio Rao 	fuse_vnode_refreshsize(vp, cred);
17145fe58019SAttilio Rao 
17155fe58019SAttilio Rao 	return fuse_io_dispatch(vp, uio, ioflag, cred);
17165fe58019SAttilio Rao }
17175fe58019SAttilio Rao 
17185fe58019SAttilio Rao /*
17195fe58019SAttilio Rao     struct vnop_getpages_args {
17205fe58019SAttilio Rao         struct vnode *a_vp;
17215fe58019SAttilio Rao         vm_page_t *a_m;
17225fe58019SAttilio Rao         int a_count;
17235fe58019SAttilio Rao         int a_reqpage;
17245fe58019SAttilio Rao         vm_ooffset_t a_offset;
17255fe58019SAttilio Rao     };
17265fe58019SAttilio Rao */
17275fe58019SAttilio Rao static int
17285fe58019SAttilio Rao fuse_vnop_getpages(struct vop_getpages_args *ap)
17295fe58019SAttilio Rao {
17305fe58019SAttilio Rao 	int i, error, nextoff, size, toff, count, npages;
17315fe58019SAttilio Rao 	struct uio uio;
17325fe58019SAttilio Rao 	struct iovec iov;
17335fe58019SAttilio Rao 	vm_offset_t kva;
17345fe58019SAttilio Rao 	struct buf *bp;
17355fe58019SAttilio Rao 	struct vnode *vp;
17365fe58019SAttilio Rao 	struct thread *td;
17375fe58019SAttilio Rao 	struct ucred *cred;
17385fe58019SAttilio Rao 	vm_page_t *pages;
17395fe58019SAttilio Rao 
17404cff153bSAttilio Rao 	FS_DEBUG2G("heh\n");
17415fe58019SAttilio Rao 
17425fe58019SAttilio Rao 	vp = ap->a_vp;
17435fe58019SAttilio Rao 	KASSERT(vp->v_object, ("objectless vp passed to getpages"));
17445fe58019SAttilio Rao 	td = curthread;			/* XXX */
17455fe58019SAttilio Rao 	cred = curthread->td_ucred;	/* XXX */
17465fe58019SAttilio Rao 	pages = ap->a_m;
17475fe58019SAttilio Rao 	count = ap->a_count;
17485fe58019SAttilio Rao 
17495fe58019SAttilio Rao 	if (!fsess_opt_mmap(vnode_mount(vp))) {
17504cff153bSAttilio Rao 		FS_DEBUG("called on non-cacheable vnode??\n");
17515fe58019SAttilio Rao 		return (VM_PAGER_ERROR);
17525fe58019SAttilio Rao 	}
17535fe58019SAttilio Rao 	npages = btoc(count);
17545fe58019SAttilio Rao 
17555fe58019SAttilio Rao 	/*
17565fe58019SAttilio Rao 	 * If the requested page is partially valid, just return it and
17575fe58019SAttilio Rao 	 * allow the pager to zero-out the blanks.  Partially valid pages
17585fe58019SAttilio Rao 	 * can only occur at the file EOF.
17595fe58019SAttilio Rao 	 */
17605fe58019SAttilio Rao 
1761*89f6b863SAttilio Rao 	VM_OBJECT_WLOCK(vp->v_object);
17625fe58019SAttilio Rao 	fuse_vm_page_lock_queues();
17635fe58019SAttilio Rao 	if (pages[ap->a_reqpage]->valid != 0) {
17645fe58019SAttilio Rao 		for (i = 0; i < npages; ++i) {
17655fe58019SAttilio Rao 			if (i != ap->a_reqpage) {
17665fe58019SAttilio Rao 				fuse_vm_page_lock(pages[i]);
17675fe58019SAttilio Rao 				vm_page_free(pages[i]);
17685fe58019SAttilio Rao 				fuse_vm_page_unlock(pages[i]);
17695fe58019SAttilio Rao 			}
17705fe58019SAttilio Rao 		}
17715fe58019SAttilio Rao 		fuse_vm_page_unlock_queues();
1772*89f6b863SAttilio Rao 		VM_OBJECT_WUNLOCK(vp->v_object);
17735fe58019SAttilio Rao 		return 0;
17745fe58019SAttilio Rao 	}
17755fe58019SAttilio Rao 	fuse_vm_page_unlock_queues();
1776*89f6b863SAttilio Rao 	VM_OBJECT_WUNLOCK(vp->v_object);
17775fe58019SAttilio Rao 
17785fe58019SAttilio Rao 	/*
17795fe58019SAttilio Rao 	 * We use only the kva address for the buffer, but this is extremely
17805fe58019SAttilio Rao 	 * convienient and fast.
17815fe58019SAttilio Rao 	 */
17825fe58019SAttilio Rao 	bp = getpbuf(&fuse_pbuf_freecnt);
17835fe58019SAttilio Rao 
17845fe58019SAttilio Rao 	kva = (vm_offset_t)bp->b_data;
17855fe58019SAttilio Rao 	pmap_qenter(kva, pages, npages);
17865fe58019SAttilio Rao 	PCPU_INC(cnt.v_vnodein);
17875fe58019SAttilio Rao 	PCPU_ADD(cnt.v_vnodepgsin, npages);
17885fe58019SAttilio Rao 
17895fe58019SAttilio Rao 	iov.iov_base = (caddr_t)kva;
17905fe58019SAttilio Rao 	iov.iov_len = count;
17915fe58019SAttilio Rao 	uio.uio_iov = &iov;
17925fe58019SAttilio Rao 	uio.uio_iovcnt = 1;
17935fe58019SAttilio Rao 	uio.uio_offset = IDX_TO_OFF(pages[0]->pindex);
17945fe58019SAttilio Rao 	uio.uio_resid = count;
17955fe58019SAttilio Rao 	uio.uio_segflg = UIO_SYSSPACE;
17965fe58019SAttilio Rao 	uio.uio_rw = UIO_READ;
17975fe58019SAttilio Rao 	uio.uio_td = td;
17985fe58019SAttilio Rao 
17995fe58019SAttilio Rao 	error = fuse_io_dispatch(vp, &uio, IO_DIRECT, cred);
18005fe58019SAttilio Rao 	pmap_qremove(kva, npages);
18015fe58019SAttilio Rao 
18025fe58019SAttilio Rao 	relpbuf(bp, &fuse_pbuf_freecnt);
18035fe58019SAttilio Rao 
18045fe58019SAttilio Rao 	if (error && (uio.uio_resid == count)) {
18054cff153bSAttilio Rao 		FS_DEBUG("error %d\n", error);
1806*89f6b863SAttilio Rao 		VM_OBJECT_WLOCK(vp->v_object);
18075fe58019SAttilio Rao 		fuse_vm_page_lock_queues();
18085fe58019SAttilio Rao 		for (i = 0; i < npages; ++i) {
18095fe58019SAttilio Rao 			if (i != ap->a_reqpage) {
18105fe58019SAttilio Rao 				fuse_vm_page_lock(pages[i]);
18115fe58019SAttilio Rao 				vm_page_free(pages[i]);
18125fe58019SAttilio Rao 				fuse_vm_page_unlock(pages[i]);
18135fe58019SAttilio Rao 			}
18145fe58019SAttilio Rao 		}
18155fe58019SAttilio Rao 		fuse_vm_page_unlock_queues();
1816*89f6b863SAttilio Rao 		VM_OBJECT_WUNLOCK(vp->v_object);
18175fe58019SAttilio Rao 		return VM_PAGER_ERROR;
18185fe58019SAttilio Rao 	}
18195fe58019SAttilio Rao 	/*
18205fe58019SAttilio Rao 	 * Calculate the number of bytes read and validate only that number
18215fe58019SAttilio Rao 	 * of bytes.  Note that due to pending writes, size may be 0.  This
18225fe58019SAttilio Rao 	 * does not mean that the remaining data is invalid!
18235fe58019SAttilio Rao 	 */
18245fe58019SAttilio Rao 
18255fe58019SAttilio Rao 	size = count - uio.uio_resid;
1826*89f6b863SAttilio Rao 	VM_OBJECT_WLOCK(vp->v_object);
18275fe58019SAttilio Rao 	fuse_vm_page_lock_queues();
18285fe58019SAttilio Rao 	for (i = 0, toff = 0; i < npages; i++, toff = nextoff) {
18295fe58019SAttilio Rao 		vm_page_t m;
18305fe58019SAttilio Rao 
18315fe58019SAttilio Rao 		nextoff = toff + PAGE_SIZE;
18325fe58019SAttilio Rao 		m = pages[i];
18335fe58019SAttilio Rao 
18345fe58019SAttilio Rao 		if (nextoff <= size) {
18355fe58019SAttilio Rao 			/*
18365fe58019SAttilio Rao 			 * Read operation filled an entire page
18375fe58019SAttilio Rao 			 */
18385fe58019SAttilio Rao 			m->valid = VM_PAGE_BITS_ALL;
18395fe58019SAttilio Rao 			KASSERT(m->dirty == 0,
18405fe58019SAttilio Rao 			    ("fuse_getpages: page %p is dirty", m));
18415fe58019SAttilio Rao 		} else if (size > toff) {
18425fe58019SAttilio Rao 			/*
18435fe58019SAttilio Rao 			 * Read operation filled a partial page.
18445fe58019SAttilio Rao 			 */
18455fe58019SAttilio Rao 			m->valid = 0;
18465fe58019SAttilio Rao 			vm_page_set_valid_range(m, 0, size - toff);
18475fe58019SAttilio Rao 			KASSERT(m->dirty == 0,
18485fe58019SAttilio Rao 			    ("fuse_getpages: page %p is dirty", m));
18495fe58019SAttilio Rao 		} else {
18505fe58019SAttilio Rao 			/*
18515fe58019SAttilio Rao 			 * Read operation was short.  If no error occured
18525fe58019SAttilio Rao 			 * we may have hit a zero-fill section.   We simply
18535fe58019SAttilio Rao 			 * leave valid set to 0.
18545fe58019SAttilio Rao 			 */
18555fe58019SAttilio Rao 			;
18565fe58019SAttilio Rao 		}
18575fe58019SAttilio Rao 		if (i != ap->a_reqpage) {
18585fe58019SAttilio Rao 			/*
18595fe58019SAttilio Rao 			 * Whether or not to leave the page activated is up in
18605fe58019SAttilio Rao 			 * the air, but we should put the page on a page queue
18615fe58019SAttilio Rao 			 * somewhere (it already is in the object).  Result:
18625fe58019SAttilio Rao 			 * It appears that emperical results show that
18635fe58019SAttilio Rao 			 * deactivating pages is best.
18645fe58019SAttilio Rao 			 */
18655fe58019SAttilio Rao 
18665fe58019SAttilio Rao 			/*
18675fe58019SAttilio Rao 			 * Just in case someone was asking for this page we
18685fe58019SAttilio Rao 			 * now tell them that it is ok to use.
18695fe58019SAttilio Rao 			 */
18705fe58019SAttilio Rao 			if (!error) {
18715fe58019SAttilio Rao 				if (m->oflags & VPO_WANTED) {
18725fe58019SAttilio Rao 					fuse_vm_page_lock(m);
18735fe58019SAttilio Rao 					vm_page_activate(m);
18745fe58019SAttilio Rao 					fuse_vm_page_unlock(m);
18755fe58019SAttilio Rao 				} else {
18765fe58019SAttilio Rao 					fuse_vm_page_lock(m);
18775fe58019SAttilio Rao 					vm_page_deactivate(m);
18785fe58019SAttilio Rao 					fuse_vm_page_unlock(m);
18795fe58019SAttilio Rao 				}
18805fe58019SAttilio Rao 				vm_page_wakeup(m);
18815fe58019SAttilio Rao 			} else {
18825fe58019SAttilio Rao 				fuse_vm_page_lock(m);
18835fe58019SAttilio Rao 				vm_page_free(m);
18845fe58019SAttilio Rao 				fuse_vm_page_unlock(m);
18855fe58019SAttilio Rao 			}
18865fe58019SAttilio Rao 		}
18875fe58019SAttilio Rao 	}
18885fe58019SAttilio Rao 	fuse_vm_page_unlock_queues();
1889*89f6b863SAttilio Rao 	VM_OBJECT_WUNLOCK(vp->v_object);
18905fe58019SAttilio Rao 	return 0;
18915fe58019SAttilio Rao }
18925fe58019SAttilio Rao 
18935fe58019SAttilio Rao /*
18945fe58019SAttilio Rao     struct vnop_putpages_args {
18955fe58019SAttilio Rao         struct vnode *a_vp;
18965fe58019SAttilio Rao         vm_page_t *a_m;
18975fe58019SAttilio Rao         int a_count;
18985fe58019SAttilio Rao         int a_sync;
18995fe58019SAttilio Rao         int *a_rtvals;
19005fe58019SAttilio Rao         vm_ooffset_t a_offset;
19015fe58019SAttilio Rao     };
19025fe58019SAttilio Rao */
19035fe58019SAttilio Rao static int
19045fe58019SAttilio Rao fuse_vnop_putpages(struct vop_putpages_args *ap)
19055fe58019SAttilio Rao {
19065fe58019SAttilio Rao 	struct uio uio;
19075fe58019SAttilio Rao 	struct iovec iov;
19085fe58019SAttilio Rao 	vm_offset_t kva;
19095fe58019SAttilio Rao 	struct buf *bp;
19105fe58019SAttilio Rao 	int i, error, npages, count;
19115fe58019SAttilio Rao 	off_t offset;
19125fe58019SAttilio Rao 	int *rtvals;
19135fe58019SAttilio Rao 	struct vnode *vp;
19145fe58019SAttilio Rao 	struct thread *td;
19155fe58019SAttilio Rao 	struct ucred *cred;
19165fe58019SAttilio Rao 	vm_page_t *pages;
19175fe58019SAttilio Rao 	vm_ooffset_t fsize;
19185fe58019SAttilio Rao 
19194cff153bSAttilio Rao 	FS_DEBUG2G("heh\n");
19205fe58019SAttilio Rao 
19215fe58019SAttilio Rao 	vp = ap->a_vp;
19225fe58019SAttilio Rao 	KASSERT(vp->v_object, ("objectless vp passed to putpages"));
19235fe58019SAttilio Rao 	fsize = vp->v_object->un_pager.vnp.vnp_size;
19245fe58019SAttilio Rao 	td = curthread;			/* XXX */
19255fe58019SAttilio Rao 	cred = curthread->td_ucred;	/* XXX */
19265fe58019SAttilio Rao 	pages = ap->a_m;
19275fe58019SAttilio Rao 	count = ap->a_count;
19285fe58019SAttilio Rao 	rtvals = ap->a_rtvals;
19295fe58019SAttilio Rao 	npages = btoc(count);
19305fe58019SAttilio Rao 	offset = IDX_TO_OFF(pages[0]->pindex);
19315fe58019SAttilio Rao 
19325fe58019SAttilio Rao 	if (!fsess_opt_mmap(vnode_mount(vp))) {
19334cff153bSAttilio Rao 		FS_DEBUG("called on non-cacheable vnode??\n");
19345fe58019SAttilio Rao 	}
19355fe58019SAttilio Rao 	for (i = 0; i < npages; i++)
19365fe58019SAttilio Rao 		rtvals[i] = VM_PAGER_AGAIN;
19375fe58019SAttilio Rao 
19385fe58019SAttilio Rao 	/*
19395fe58019SAttilio Rao 	 * When putting pages, do not extend file past EOF.
19405fe58019SAttilio Rao 	 */
19415fe58019SAttilio Rao 
19425fe58019SAttilio Rao 	if (offset + count > fsize) {
19435fe58019SAttilio Rao 		count = fsize - offset;
19445fe58019SAttilio Rao 		if (count < 0)
19455fe58019SAttilio Rao 			count = 0;
19465fe58019SAttilio Rao 	}
19475fe58019SAttilio Rao 	/*
19485fe58019SAttilio Rao 	 * We use only the kva address for the buffer, but this is extremely
19495fe58019SAttilio Rao 	 * convienient and fast.
19505fe58019SAttilio Rao 	 */
19515fe58019SAttilio Rao 	bp = getpbuf(&fuse_pbuf_freecnt);
19525fe58019SAttilio Rao 
19535fe58019SAttilio Rao 	kva = (vm_offset_t)bp->b_data;
19545fe58019SAttilio Rao 	pmap_qenter(kva, pages, npages);
19555fe58019SAttilio Rao 	PCPU_INC(cnt.v_vnodeout);
19565fe58019SAttilio Rao 	PCPU_ADD(cnt.v_vnodepgsout, count);
19575fe58019SAttilio Rao 
19585fe58019SAttilio Rao 	iov.iov_base = (caddr_t)kva;
19595fe58019SAttilio Rao 	iov.iov_len = count;
19605fe58019SAttilio Rao 	uio.uio_iov = &iov;
19615fe58019SAttilio Rao 	uio.uio_iovcnt = 1;
19625fe58019SAttilio Rao 	uio.uio_offset = offset;
19635fe58019SAttilio Rao 	uio.uio_resid = count;
19645fe58019SAttilio Rao 	uio.uio_segflg = UIO_SYSSPACE;
19655fe58019SAttilio Rao 	uio.uio_rw = UIO_WRITE;
19665fe58019SAttilio Rao 	uio.uio_td = td;
19675fe58019SAttilio Rao 
19685fe58019SAttilio Rao 	error = fuse_io_dispatch(vp, &uio, IO_DIRECT, cred);
19695fe58019SAttilio Rao 
19705fe58019SAttilio Rao 	pmap_qremove(kva, npages);
19715fe58019SAttilio Rao 	relpbuf(bp, &fuse_pbuf_freecnt);
19725fe58019SAttilio Rao 
19735fe58019SAttilio Rao 	if (!error) {
19745fe58019SAttilio Rao 		int nwritten = round_page(count - uio.uio_resid) / PAGE_SIZE;
19755fe58019SAttilio Rao 
19765fe58019SAttilio Rao 		for (i = 0; i < nwritten; i++) {
19775fe58019SAttilio Rao 			rtvals[i] = VM_PAGER_OK;
1978*89f6b863SAttilio Rao 			VM_OBJECT_WLOCK(pages[i]->object);
19795fe58019SAttilio Rao 			vm_page_undirty(pages[i]);
1980*89f6b863SAttilio Rao 			VM_OBJECT_WUNLOCK(pages[i]->object);
19815fe58019SAttilio Rao 		}
19825fe58019SAttilio Rao 	}
19835fe58019SAttilio Rao 	return rtvals[0];
19845fe58019SAttilio Rao }
19855fe58019SAttilio Rao 
19865fe58019SAttilio Rao /*
19875fe58019SAttilio Rao     struct vnop_print_args {
19885fe58019SAttilio Rao         struct vnode *a_vp;
19895fe58019SAttilio Rao     };
19905fe58019SAttilio Rao */
19915fe58019SAttilio Rao static int
19925fe58019SAttilio Rao fuse_vnop_print(struct vop_print_args *ap)
19935fe58019SAttilio Rao {
19945fe58019SAttilio Rao 	struct fuse_vnode_data *fvdat = VTOFUD(ap->a_vp);
19955fe58019SAttilio Rao 
19965fe58019SAttilio Rao 	printf("nodeid: %ju, parent nodeid: %ju, nlookup: %ju, flag: %#x\n",
19975fe58019SAttilio Rao 	    (uintmax_t)VTOILLU(ap->a_vp), (uintmax_t)fvdat->parent_nid,
19985fe58019SAttilio Rao 	    (uintmax_t)fvdat->nlookup,
19995fe58019SAttilio Rao 	    fvdat->flag);
20005fe58019SAttilio Rao 
20015fe58019SAttilio Rao 	return 0;
20025fe58019SAttilio Rao }
2003