xref: /freebsd/sys/fs/fuse/fuse_vnops.c (revision e7df98863bace56d070d78d309ec635bb1e86c52)
151369649SPedro F. Giffuni /*-
251369649SPedro F. Giffuni  * SPDX-License-Identifier: BSD-3-Clause
351369649SPedro F. Giffuni  *
45fe58019SAttilio Rao  * Copyright (c) 2007-2009 Google Inc. and Amit Singh
55fe58019SAttilio Rao  * All rights reserved.
65fe58019SAttilio Rao  *
75fe58019SAttilio Rao  * Redistribution and use in source and binary forms, with or without
85fe58019SAttilio Rao  * modification, are permitted provided that the following conditions are
95fe58019SAttilio Rao  * met:
105fe58019SAttilio Rao  *
115fe58019SAttilio Rao  * * Redistributions of source code must retain the above copyright
125fe58019SAttilio Rao  *   notice, this list of conditions and the following disclaimer.
135fe58019SAttilio Rao  * * Redistributions in binary form must reproduce the above
145fe58019SAttilio Rao  *   copyright notice, this list of conditions and the following disclaimer
155fe58019SAttilio Rao  *   in the documentation and/or other materials provided with the
165fe58019SAttilio Rao  *   distribution.
175fe58019SAttilio Rao  * * Neither the name of Google Inc. nor the names of its
185fe58019SAttilio Rao  *   contributors may be used to endorse or promote products derived from
195fe58019SAttilio Rao  *   this software without specific prior written permission.
205fe58019SAttilio Rao  *
215fe58019SAttilio Rao  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
225fe58019SAttilio Rao  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
235fe58019SAttilio Rao  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
245fe58019SAttilio Rao  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
255fe58019SAttilio Rao  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
265fe58019SAttilio Rao  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
275fe58019SAttilio Rao  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
285fe58019SAttilio Rao  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
295fe58019SAttilio Rao  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
305fe58019SAttilio Rao  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
315fe58019SAttilio Rao  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
325fe58019SAttilio Rao  *
335fe58019SAttilio Rao  * Copyright (C) 2005 Csaba Henk.
345fe58019SAttilio Rao  * All rights reserved.
355fe58019SAttilio Rao  *
365fe58019SAttilio Rao  * Redistribution and use in source and binary forms, with or without
375fe58019SAttilio Rao  * modification, are permitted provided that the following conditions
385fe58019SAttilio Rao  * are met:
395fe58019SAttilio Rao  * 1. Redistributions of source code must retain the above copyright
405fe58019SAttilio Rao  *    notice, this list of conditions and the following disclaimer.
415fe58019SAttilio Rao  * 2. Redistributions in binary form must reproduce the above copyright
425fe58019SAttilio Rao  *    notice, this list of conditions and the following disclaimer in the
435fe58019SAttilio Rao  *    documentation and/or other materials provided with the distribution.
445fe58019SAttilio Rao  *
455fe58019SAttilio Rao  * THIS SOFTWARE IS PROVIDED BY AUTHOR AND CONTRIBUTORS ``AS IS'' AND
465fe58019SAttilio Rao  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
475fe58019SAttilio Rao  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
485fe58019SAttilio Rao  * ARE DISCLAIMED.  IN NO EVENT SHALL AUTHOR OR CONTRIBUTORS BE LIABLE
495fe58019SAttilio Rao  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
505fe58019SAttilio Rao  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
515fe58019SAttilio Rao  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
525fe58019SAttilio Rao  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
535fe58019SAttilio Rao  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
545fe58019SAttilio Rao  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
555fe58019SAttilio Rao  * SUCH DAMAGE.
565fe58019SAttilio Rao  */
575fe58019SAttilio Rao 
585fe58019SAttilio Rao #include <sys/cdefs.h>
595fe58019SAttilio Rao __FBSDID("$FreeBSD$");
605fe58019SAttilio Rao 
615fe58019SAttilio Rao #include <sys/types.h>
625fe58019SAttilio Rao #include <sys/module.h>
635fe58019SAttilio Rao #include <sys/systm.h>
645fe58019SAttilio Rao #include <sys/errno.h>
655fe58019SAttilio Rao #include <sys/param.h>
665fe58019SAttilio Rao #include <sys/kernel.h>
675fe58019SAttilio Rao #include <sys/conf.h>
685fe58019SAttilio Rao #include <sys/uio.h>
695fe58019SAttilio Rao #include <sys/malloc.h>
705fe58019SAttilio Rao #include <sys/queue.h>
715fe58019SAttilio Rao #include <sys/lock.h>
7289f6b863SAttilio Rao #include <sys/rwlock.h>
735fe58019SAttilio Rao #include <sys/sx.h>
745fe58019SAttilio Rao #include <sys/proc.h>
755fe58019SAttilio Rao #include <sys/mount.h>
765fe58019SAttilio Rao #include <sys/vnode.h>
775fe58019SAttilio Rao #include <sys/namei.h>
7804660064SFedor Uporov #include <sys/extattr.h>
795fe58019SAttilio Rao #include <sys/stat.h>
805fe58019SAttilio Rao #include <sys/unistd.h>
815fe58019SAttilio Rao #include <sys/filedesc.h>
825fe58019SAttilio Rao #include <sys/file.h>
835fe58019SAttilio Rao #include <sys/fcntl.h>
845fe58019SAttilio Rao #include <sys/dirent.h>
855fe58019SAttilio Rao #include <sys/bio.h>
865fe58019SAttilio Rao #include <sys/buf.h>
875fe58019SAttilio Rao #include <sys/sysctl.h>
88ca148cdaSGleb Smirnoff #include <sys/vmmeter.h>
895fe58019SAttilio Rao 
905fe58019SAttilio Rao #include <vm/vm.h>
915fe58019SAttilio Rao #include <vm/vm_extern.h>
925fe58019SAttilio Rao #include <vm/pmap.h>
935fe58019SAttilio Rao #include <vm/vm_map.h>
945fe58019SAttilio Rao #include <vm/vm_page.h>
955fe58019SAttilio Rao #include <vm/vm_param.h>
965fe58019SAttilio Rao #include <vm/vm_object.h>
975fe58019SAttilio Rao #include <vm/vm_pager.h>
985fe58019SAttilio Rao #include <vm/vnode_pager.h>
995fe58019SAttilio Rao #include <vm/vm_object.h>
1005fe58019SAttilio Rao 
1015fe58019SAttilio Rao #include "fuse.h"
1025fe58019SAttilio Rao #include "fuse_file.h"
1035fe58019SAttilio Rao #include "fuse_internal.h"
1045fe58019SAttilio Rao #include "fuse_ipc.h"
1055fe58019SAttilio Rao #include "fuse_node.h"
1065fe58019SAttilio Rao #include "fuse_param.h"
1075fe58019SAttilio Rao #include "fuse_io.h"
1085fe58019SAttilio Rao 
1095fe58019SAttilio Rao #include <sys/priv.h>
1105fe58019SAttilio Rao 
1115fe58019SAttilio Rao #define FUSE_DEBUG_MODULE VNOPS
1125fe58019SAttilio Rao #include "fuse_debug.h"
1135fe58019SAttilio Rao 
1145fe58019SAttilio Rao /* vnode ops */
1155fe58019SAttilio Rao static vop_access_t fuse_vnop_access;
1165fe58019SAttilio Rao static vop_close_t fuse_vnop_close;
1175fe58019SAttilio Rao static vop_create_t fuse_vnop_create;
11804660064SFedor Uporov static vop_deleteextattr_t fuse_vnop_deleteextattr;
1195fe58019SAttilio Rao static vop_fsync_t fuse_vnop_fsync;
1205fe58019SAttilio Rao static vop_getattr_t fuse_vnop_getattr;
12104660064SFedor Uporov static vop_getextattr_t fuse_vnop_getextattr;
1225fe58019SAttilio Rao static vop_inactive_t fuse_vnop_inactive;
1235fe58019SAttilio Rao static vop_link_t fuse_vnop_link;
12404660064SFedor Uporov static vop_listextattr_t fuse_vnop_listextattr;
1255fe58019SAttilio Rao static vop_lookup_t fuse_vnop_lookup;
1265fe58019SAttilio Rao static vop_mkdir_t fuse_vnop_mkdir;
1275fe58019SAttilio Rao static vop_mknod_t fuse_vnop_mknod;
1285fe58019SAttilio Rao static vop_open_t fuse_vnop_open;
129746c92e0SJohn Baldwin static vop_pathconf_t fuse_vnop_pathconf;
1305fe58019SAttilio Rao static vop_read_t fuse_vnop_read;
1315fe58019SAttilio Rao static vop_readdir_t fuse_vnop_readdir;
1325fe58019SAttilio Rao static vop_readlink_t fuse_vnop_readlink;
1335fe58019SAttilio Rao static vop_reclaim_t fuse_vnop_reclaim;
1345fe58019SAttilio Rao static vop_remove_t fuse_vnop_remove;
1355fe58019SAttilio Rao static vop_rename_t fuse_vnop_rename;
1365fe58019SAttilio Rao static vop_rmdir_t fuse_vnop_rmdir;
1375fe58019SAttilio Rao static vop_setattr_t fuse_vnop_setattr;
13804660064SFedor Uporov static vop_setextattr_t fuse_vnop_setextattr;
1395fe58019SAttilio Rao static vop_strategy_t fuse_vnop_strategy;
1405fe58019SAttilio Rao static vop_symlink_t fuse_vnop_symlink;
1415fe58019SAttilio Rao static vop_write_t fuse_vnop_write;
1425fe58019SAttilio Rao static vop_getpages_t fuse_vnop_getpages;
1435fe58019SAttilio Rao static vop_putpages_t fuse_vnop_putpages;
1445fe58019SAttilio Rao static vop_print_t fuse_vnop_print;
1455fe58019SAttilio Rao 
1465fe58019SAttilio Rao struct vop_vector fuse_vnops = {
1475fe58019SAttilio Rao 	.vop_default = &default_vnodeops,
1485fe58019SAttilio Rao 	.vop_access = fuse_vnop_access,
1495fe58019SAttilio Rao 	.vop_close = fuse_vnop_close,
1505fe58019SAttilio Rao 	.vop_create = fuse_vnop_create,
15104660064SFedor Uporov 	.vop_deleteextattr = fuse_vnop_deleteextattr,
1525fe58019SAttilio Rao 	.vop_fsync = fuse_vnop_fsync,
1535fe58019SAttilio Rao 	.vop_getattr = fuse_vnop_getattr,
15404660064SFedor Uporov 	.vop_getextattr = fuse_vnop_getextattr,
1555fe58019SAttilio Rao 	.vop_inactive = fuse_vnop_inactive,
1565fe58019SAttilio Rao 	.vop_link = fuse_vnop_link,
15704660064SFedor Uporov 	.vop_listextattr = fuse_vnop_listextattr,
1585fe58019SAttilio Rao 	.vop_lookup = fuse_vnop_lookup,
1595fe58019SAttilio Rao 	.vop_mkdir = fuse_vnop_mkdir,
1605fe58019SAttilio Rao 	.vop_mknod = fuse_vnop_mknod,
1615fe58019SAttilio Rao 	.vop_open = fuse_vnop_open,
162746c92e0SJohn Baldwin 	.vop_pathconf = fuse_vnop_pathconf,
1635fe58019SAttilio Rao 	.vop_read = fuse_vnop_read,
1645fe58019SAttilio Rao 	.vop_readdir = fuse_vnop_readdir,
1655fe58019SAttilio Rao 	.vop_readlink = fuse_vnop_readlink,
1665fe58019SAttilio Rao 	.vop_reclaim = fuse_vnop_reclaim,
1675fe58019SAttilio Rao 	.vop_remove = fuse_vnop_remove,
1685fe58019SAttilio Rao 	.vop_rename = fuse_vnop_rename,
1695fe58019SAttilio Rao 	.vop_rmdir = fuse_vnop_rmdir,
1705fe58019SAttilio Rao 	.vop_setattr = fuse_vnop_setattr,
17104660064SFedor Uporov 	.vop_setextattr = fuse_vnop_setextattr,
1725fe58019SAttilio Rao 	.vop_strategy = fuse_vnop_strategy,
1735fe58019SAttilio Rao 	.vop_symlink = fuse_vnop_symlink,
1745fe58019SAttilio Rao 	.vop_write = fuse_vnop_write,
1755fe58019SAttilio Rao 	.vop_getpages = fuse_vnop_getpages,
1765fe58019SAttilio Rao 	.vop_putpages = fuse_vnop_putpages,
1775fe58019SAttilio Rao 	.vop_print = fuse_vnop_print,
1785fe58019SAttilio Rao };
1795fe58019SAttilio Rao 
1805fe58019SAttilio Rao static u_long fuse_lookup_cache_hits = 0;
1815fe58019SAttilio Rao 
1825fe58019SAttilio Rao SYSCTL_ULONG(_vfs_fuse, OID_AUTO, lookup_cache_hits, CTLFLAG_RD,
183bd4cb2a4SConrad Meyer     &fuse_lookup_cache_hits, 0, "number of positive cache hits in lookup");
1845fe58019SAttilio Rao 
1855fe58019SAttilio Rao static u_long fuse_lookup_cache_misses = 0;
1865fe58019SAttilio Rao 
1875fe58019SAttilio Rao SYSCTL_ULONG(_vfs_fuse, OID_AUTO, lookup_cache_misses, CTLFLAG_RD,
188bd4cb2a4SConrad Meyer     &fuse_lookup_cache_misses, 0, "number of cache misses in lookup");
1895fe58019SAttilio Rao 
1905fe58019SAttilio Rao int	fuse_lookup_cache_enable = 1;
1915fe58019SAttilio Rao 
1925fe58019SAttilio Rao SYSCTL_INT(_vfs_fuse, OID_AUTO, lookup_cache_enable, CTLFLAG_RW,
193bd4cb2a4SConrad Meyer     &fuse_lookup_cache_enable, 0, "if non-zero, enable lookup cache");
1945fe58019SAttilio Rao 
1955fe58019SAttilio Rao /*
1965fe58019SAttilio Rao  * XXX: This feature is highly experimental and can bring to instabilities,
1975fe58019SAttilio Rao  * needs revisiting before to be enabled by default.
1985fe58019SAttilio Rao  */
1995fe58019SAttilio Rao static int fuse_reclaim_revoked = 0;
2005fe58019SAttilio Rao 
2015fe58019SAttilio Rao SYSCTL_INT(_vfs_fuse, OID_AUTO, reclaim_revoked, CTLFLAG_RW,
2025fe58019SAttilio Rao     &fuse_reclaim_revoked, 0, "");
2035fe58019SAttilio Rao 
204756a5412SGleb Smirnoff uma_zone_t fuse_pbuf_zone;
2055fe58019SAttilio Rao 
2065fe58019SAttilio Rao #define fuse_vm_page_lock(m)		vm_page_lock((m));
2075fe58019SAttilio Rao #define fuse_vm_page_unlock(m)		vm_page_unlock((m));
2085fe58019SAttilio Rao #define fuse_vm_page_lock_queues()	((void)0)
2095fe58019SAttilio Rao #define fuse_vm_page_unlock_queues()	((void)0)
2105fe58019SAttilio Rao 
2115fe58019SAttilio Rao /*
2125fe58019SAttilio Rao     struct vnop_access_args {
2135fe58019SAttilio Rao 	struct vnode *a_vp;
2145fe58019SAttilio Rao #if VOP_ACCESS_TAKES_ACCMODE_T
2155fe58019SAttilio Rao 	accmode_t a_accmode;
2165fe58019SAttilio Rao #else
2175fe58019SAttilio Rao 	int a_mode;
2185fe58019SAttilio Rao #endif
2195fe58019SAttilio Rao 	struct ucred *a_cred;
2205fe58019SAttilio Rao 	struct thread *a_td;
2215fe58019SAttilio Rao     };
2225fe58019SAttilio Rao */
2235fe58019SAttilio Rao static int
2245fe58019SAttilio Rao fuse_vnop_access(struct vop_access_args *ap)
2255fe58019SAttilio Rao {
2265fe58019SAttilio Rao 	struct vnode *vp = ap->a_vp;
2275fe58019SAttilio Rao 	int accmode = ap->a_accmode;
2285fe58019SAttilio Rao 	struct ucred *cred = ap->a_cred;
2295fe58019SAttilio Rao 
2305fe58019SAttilio Rao 	struct fuse_access_param facp;
2315fe58019SAttilio Rao 	struct fuse_data *data = fuse_get_mpdata(vnode_mount(vp));
2325fe58019SAttilio Rao 
2335fe58019SAttilio Rao 	int err;
2345fe58019SAttilio Rao 
2354cff153bSAttilio Rao 	FS_DEBUG2G("inode=%ju\n", (uintmax_t)VTOI(vp));
2365fe58019SAttilio Rao 
2375fe58019SAttilio Rao 	if (fuse_isdeadfs(vp)) {
2385fe58019SAttilio Rao 		if (vnode_isvroot(vp)) {
2395fe58019SAttilio Rao 			return 0;
2405fe58019SAttilio Rao 		}
2415fe58019SAttilio Rao 		return ENXIO;
2425fe58019SAttilio Rao 	}
2435fe58019SAttilio Rao 	if (!(data->dataflags & FSESS_INITED)) {
2445fe58019SAttilio Rao 		if (vnode_isvroot(vp)) {
245cc426dd3SMateusz Guzik 			if (priv_check_cred(cred, PRIV_VFS_ADMIN) ||
2465fe58019SAttilio Rao 			    (fuse_match_cred(data->daemoncred, cred) == 0)) {
2475fe58019SAttilio Rao 				return 0;
2485fe58019SAttilio Rao 			}
2495fe58019SAttilio Rao 		}
2505fe58019SAttilio Rao 		return EBADF;
2515fe58019SAttilio Rao 	}
2525fe58019SAttilio Rao 	if (vnode_islnk(vp)) {
2535fe58019SAttilio Rao 		return 0;
2545fe58019SAttilio Rao 	}
2555fe58019SAttilio Rao 	bzero(&facp, sizeof(facp));
2565fe58019SAttilio Rao 
2575fe58019SAttilio Rao 	err = fuse_internal_access(vp, accmode, &facp, ap->a_td, ap->a_cred);
2584cff153bSAttilio Rao 	FS_DEBUG2G("err=%d accmode=0x%x\n", err, accmode);
2595fe58019SAttilio Rao 	return err;
2605fe58019SAttilio Rao }
2615fe58019SAttilio Rao 
2625fe58019SAttilio Rao /*
2635fe58019SAttilio Rao     struct vnop_close_args {
2645fe58019SAttilio Rao 	struct vnode *a_vp;
2655fe58019SAttilio Rao 	int  a_fflag;
2665fe58019SAttilio Rao 	struct ucred *a_cred;
2675fe58019SAttilio Rao 	struct thread *a_td;
2685fe58019SAttilio Rao     };
2695fe58019SAttilio Rao */
2705fe58019SAttilio Rao static int
2715fe58019SAttilio Rao fuse_vnop_close(struct vop_close_args *ap)
2725fe58019SAttilio Rao {
2735fe58019SAttilio Rao 	struct vnode *vp = ap->a_vp;
2745fe58019SAttilio Rao 	struct ucred *cred = ap->a_cred;
2755fe58019SAttilio Rao 	int fflag = ap->a_fflag;
2765fe58019SAttilio Rao 	fufh_type_t fufh_type;
2775fe58019SAttilio Rao 
2785fe58019SAttilio Rao 	fuse_trace_printf_vnop();
2795fe58019SAttilio Rao 
2805fe58019SAttilio Rao 	if (fuse_isdeadfs(vp)) {
2815fe58019SAttilio Rao 		return 0;
2825fe58019SAttilio Rao 	}
2835fe58019SAttilio Rao 	if (vnode_isdir(vp)) {
2845fe58019SAttilio Rao 		if (fuse_filehandle_valid(vp, FUFH_RDONLY)) {
2855fe58019SAttilio Rao 			fuse_filehandle_close(vp, FUFH_RDONLY, NULL, cred);
2865fe58019SAttilio Rao 		}
2875fe58019SAttilio Rao 		return 0;
2885fe58019SAttilio Rao 	}
2895fe58019SAttilio Rao 	if (fflag & IO_NDELAY) {
2905fe58019SAttilio Rao 		return 0;
2915fe58019SAttilio Rao 	}
2925fe58019SAttilio Rao 	fufh_type = fuse_filehandle_xlate_from_fflags(fflag);
2935fe58019SAttilio Rao 
2945fe58019SAttilio Rao 	if (!fuse_filehandle_valid(vp, fufh_type)) {
2955fe58019SAttilio Rao 		int i;
2965fe58019SAttilio Rao 
2975fe58019SAttilio Rao 		for (i = 0; i < FUFH_MAXTYPE; i++)
2985fe58019SAttilio Rao 			if (fuse_filehandle_valid(vp, i))
2995fe58019SAttilio Rao 				break;
3005fe58019SAttilio Rao 		if (i == FUFH_MAXTYPE)
3015fe58019SAttilio Rao 			panic("FUSE: fufh type %d found to be invalid in close"
3025fe58019SAttilio Rao 			      " (fflag=0x%x)\n",
3035fe58019SAttilio Rao 			      fufh_type, fflag);
3045fe58019SAttilio Rao 	}
3055fe58019SAttilio Rao 	if ((VTOFUD(vp)->flag & FN_SIZECHANGE) != 0) {
3065fe58019SAttilio Rao 		fuse_vnode_savesize(vp, cred);
3075fe58019SAttilio Rao 	}
3085fe58019SAttilio Rao 	return 0;
3095fe58019SAttilio Rao }
3105fe58019SAttilio Rao 
3115fe58019SAttilio Rao /*
3125fe58019SAttilio Rao     struct vnop_create_args {
3135fe58019SAttilio Rao 	struct vnode *a_dvp;
3145fe58019SAttilio Rao 	struct vnode **a_vpp;
3155fe58019SAttilio Rao 	struct componentname *a_cnp;
3165fe58019SAttilio Rao 	struct vattr *a_vap;
3175fe58019SAttilio Rao     };
3185fe58019SAttilio Rao */
3195fe58019SAttilio Rao static int
3205fe58019SAttilio Rao fuse_vnop_create(struct vop_create_args *ap)
3215fe58019SAttilio Rao {
3225fe58019SAttilio Rao 	struct vnode *dvp = ap->a_dvp;
3235fe58019SAttilio Rao 	struct vnode **vpp = ap->a_vpp;
3245fe58019SAttilio Rao 	struct componentname *cnp = ap->a_cnp;
3255fe58019SAttilio Rao 	struct vattr *vap = ap->a_vap;
3265fe58019SAttilio Rao 	struct thread *td = cnp->cn_thread;
3275fe58019SAttilio Rao 	struct ucred *cred = cnp->cn_cred;
3285fe58019SAttilio Rao 
3295fe58019SAttilio Rao 	struct fuse_open_in *foi;
3305fe58019SAttilio Rao 	struct fuse_entry_out *feo;
3315fe58019SAttilio Rao 	struct fuse_dispatcher fdi;
3325fe58019SAttilio Rao 	struct fuse_dispatcher *fdip = &fdi;
3335fe58019SAttilio Rao 
3345fe58019SAttilio Rao 	int err;
3355fe58019SAttilio Rao 
3365fe58019SAttilio Rao 	struct mount *mp = vnode_mount(dvp);
3375fe58019SAttilio Rao 	uint64_t parentnid = VTOFUD(dvp)->nid;
3385fe58019SAttilio Rao 	mode_t mode = MAKEIMODE(vap->va_type, vap->va_mode);
3395fe58019SAttilio Rao 	uint64_t x_fh_id;
3405fe58019SAttilio Rao 	uint32_t x_open_flags;
3415fe58019SAttilio Rao 
3425fe58019SAttilio Rao 	fuse_trace_printf_vnop();
3435fe58019SAttilio Rao 
3445fe58019SAttilio Rao 	if (fuse_isdeadfs(dvp)) {
3455fe58019SAttilio Rao 		return ENXIO;
3465fe58019SAttilio Rao 	}
3475fe58019SAttilio Rao 	bzero(&fdi, sizeof(fdi));
3485fe58019SAttilio Rao 
3495fe58019SAttilio Rao 	/* XXX:	Will we ever want devices ? */
3505fe58019SAttilio Rao 	if ((vap->va_type != VREG)) {
351372b97d0SRick Macklem 		printf("fuse_vnop_create: unsupported va_type %d\n",
352372b97d0SRick Macklem 		    vap->va_type);
353372b97d0SRick Macklem 		return (EINVAL);
3545fe58019SAttilio Rao 	}
3555fe58019SAttilio Rao 	debug_printf("parent nid = %ju, mode = %x\n", (uintmax_t)parentnid,
3565fe58019SAttilio Rao 	    mode);
3575fe58019SAttilio Rao 
3585fe58019SAttilio Rao 	fdisp_init(fdip, sizeof(*foi) + cnp->cn_namelen + 1);
3595fe58019SAttilio Rao 	if (!fsess_isimpl(mp, FUSE_CREATE)) {
3605fe58019SAttilio Rao 		debug_printf("eh, daemon doesn't implement create?\n");
3615fe58019SAttilio Rao 		return (EINVAL);
3625fe58019SAttilio Rao 	}
3635fe58019SAttilio Rao 	fdisp_make(fdip, FUSE_CREATE, vnode_mount(dvp), parentnid, td, cred);
3645fe58019SAttilio Rao 
3655fe58019SAttilio Rao 	foi = fdip->indata;
3665fe58019SAttilio Rao 	foi->mode = mode;
3675fe58019SAttilio Rao 	foi->flags = O_CREAT | O_RDWR;
3685fe58019SAttilio Rao 
3695fe58019SAttilio Rao 	memcpy((char *)fdip->indata + sizeof(*foi), cnp->cn_nameptr,
3705fe58019SAttilio Rao 	    cnp->cn_namelen);
3715fe58019SAttilio Rao 	((char *)fdip->indata)[sizeof(*foi) + cnp->cn_namelen] = '\0';
3725fe58019SAttilio Rao 
3735fe58019SAttilio Rao 	err = fdisp_wait_answ(fdip);
3745fe58019SAttilio Rao 
3756de3b00dSAttilio Rao 	if (err) {
3766de3b00dSAttilio Rao 		if (err == ENOSYS)
3775fe58019SAttilio Rao 			fsess_set_notimpl(mp, FUSE_CREATE);
3786de3b00dSAttilio Rao 		debug_printf("create: got err=%d from daemon\n", err);
3795fe58019SAttilio Rao 		goto out;
3805fe58019SAttilio Rao 	}
381372b97d0SRick Macklem 
3825fe58019SAttilio Rao 	feo = fdip->answ;
3835fe58019SAttilio Rao 
3845fe58019SAttilio Rao 	if ((err = fuse_internal_checkentry(feo, VREG))) {
3855fe58019SAttilio Rao 		goto out;
3865fe58019SAttilio Rao 	}
38709176f09SConrad Meyer 	err = fuse_vnode_get(mp, feo, feo->nodeid, dvp, vpp, cnp, VREG);
3885fe58019SAttilio Rao 	if (err) {
3895fe58019SAttilio Rao 		struct fuse_release_in *fri;
3905fe58019SAttilio Rao 		uint64_t nodeid = feo->nodeid;
3915fe58019SAttilio Rao 		uint64_t fh_id = ((struct fuse_open_out *)(feo + 1))->fh;
3925fe58019SAttilio Rao 
3935fe58019SAttilio Rao 		fdisp_init(fdip, sizeof(*fri));
3945fe58019SAttilio Rao 		fdisp_make(fdip, FUSE_RELEASE, mp, nodeid, td, cred);
3955fe58019SAttilio Rao 		fri = fdip->indata;
3965fe58019SAttilio Rao 		fri->fh = fh_id;
3975fe58019SAttilio Rao 		fri->flags = OFLAGS(mode);
3985fe58019SAttilio Rao 		fuse_insert_callback(fdip->tick, fuse_internal_forget_callback);
3995fe58019SAttilio Rao 		fuse_insert_message(fdip->tick);
4005fe58019SAttilio Rao 		return err;
4015fe58019SAttilio Rao 	}
4025fe58019SAttilio Rao 	ASSERT_VOP_ELOCKED(*vpp, "fuse_vnop_create");
4035fe58019SAttilio Rao 
4045fe58019SAttilio Rao 	fdip->answ = feo + 1;
4055fe58019SAttilio Rao 
4065fe58019SAttilio Rao 	x_fh_id = ((struct fuse_open_out *)(feo + 1))->fh;
4075fe58019SAttilio Rao 	x_open_flags = ((struct fuse_open_out *)(feo + 1))->open_flags;
4085fe58019SAttilio Rao 	fuse_filehandle_init(*vpp, FUFH_RDWR, NULL, x_fh_id);
4095fe58019SAttilio Rao 	fuse_vnode_open(*vpp, x_open_flags, td);
4105fe58019SAttilio Rao 	cache_purge_negative(dvp);
4115fe58019SAttilio Rao 
4125fe58019SAttilio Rao out:
4135fe58019SAttilio Rao 	fdisp_destroy(fdip);
4145fe58019SAttilio Rao 	return err;
4155fe58019SAttilio Rao }
4165fe58019SAttilio Rao 
4175fe58019SAttilio Rao /*
4185fe58019SAttilio Rao  * Our vnop_fsync roughly corresponds to the FUSE_FSYNC method. The Linux
4195fe58019SAttilio Rao  * version of FUSE also has a FUSE_FLUSH method.
4205fe58019SAttilio Rao  *
4215fe58019SAttilio Rao  * On Linux, fsync() synchronizes a file's complete in-core state with that
4225fe58019SAttilio Rao  * on disk. The call is not supposed to return until the system has completed
4235fe58019SAttilio Rao  * that action or until an error is detected.
4245fe58019SAttilio Rao  *
4255fe58019SAttilio Rao  * Linux also has an fdatasync() call that is similar to fsync() but is not
4265fe58019SAttilio Rao  * required to update the metadata such as access time and modification time.
4275fe58019SAttilio Rao  */
4285fe58019SAttilio Rao 
4295fe58019SAttilio Rao /*
4305fe58019SAttilio Rao     struct vnop_fsync_args {
4315fe58019SAttilio Rao 	struct vnodeop_desc *a_desc;
4325fe58019SAttilio Rao 	struct vnode * a_vp;
4335fe58019SAttilio Rao 	struct ucred * a_cred;
4345fe58019SAttilio Rao 	int  a_waitfor;
4355fe58019SAttilio Rao 	struct thread * a_td;
4365fe58019SAttilio Rao     };
4375fe58019SAttilio Rao */
4385fe58019SAttilio Rao static int
4395fe58019SAttilio Rao fuse_vnop_fsync(struct vop_fsync_args *ap)
4405fe58019SAttilio Rao {
4415fe58019SAttilio Rao 	struct vnode *vp = ap->a_vp;
4425fe58019SAttilio Rao 	struct thread *td = ap->a_td;
4435fe58019SAttilio Rao 
4445fe58019SAttilio Rao 	struct fuse_filehandle *fufh;
4455fe58019SAttilio Rao 	struct fuse_vnode_data *fvdat = VTOFUD(vp);
4465fe58019SAttilio Rao 
4475fe58019SAttilio Rao 	int type, err = 0;
4485fe58019SAttilio Rao 
4495fe58019SAttilio Rao 	fuse_trace_printf_vnop();
4505fe58019SAttilio Rao 
4515fe58019SAttilio Rao 	if (fuse_isdeadfs(vp)) {
4525fe58019SAttilio Rao 		return 0;
4535fe58019SAttilio Rao 	}
4545fe58019SAttilio Rao 	if ((err = vop_stdfsync(ap)))
4555fe58019SAttilio Rao 		return err;
4565fe58019SAttilio Rao 
4575fe58019SAttilio Rao 	if (!fsess_isimpl(vnode_mount(vp),
4585fe58019SAttilio Rao 	    (vnode_vtype(vp) == VDIR ? FUSE_FSYNCDIR : FUSE_FSYNC))) {
4595fe58019SAttilio Rao 		goto out;
4605fe58019SAttilio Rao 	}
4615fe58019SAttilio Rao 	for (type = 0; type < FUFH_MAXTYPE; type++) {
4625fe58019SAttilio Rao 		fufh = &(fvdat->fufh[type]);
4635fe58019SAttilio Rao 		if (FUFH_IS_VALID(fufh)) {
4645fe58019SAttilio Rao 			fuse_internal_fsync(vp, td, NULL, fufh);
4655fe58019SAttilio Rao 		}
4665fe58019SAttilio Rao 	}
4675fe58019SAttilio Rao 
4685fe58019SAttilio Rao out:
4695fe58019SAttilio Rao 	return 0;
4705fe58019SAttilio Rao }
4715fe58019SAttilio Rao 
4725fe58019SAttilio Rao /*
4735fe58019SAttilio Rao     struct vnop_getattr_args {
4745fe58019SAttilio Rao 	struct vnode *a_vp;
4755fe58019SAttilio Rao 	struct vattr *a_vap;
4765fe58019SAttilio Rao 	struct ucred *a_cred;
4775fe58019SAttilio Rao 	struct thread *a_td;
4785fe58019SAttilio Rao     };
4795fe58019SAttilio Rao */
4805fe58019SAttilio Rao static int
4815fe58019SAttilio Rao fuse_vnop_getattr(struct vop_getattr_args *ap)
4825fe58019SAttilio Rao {
4835fe58019SAttilio Rao 	struct vnode *vp = ap->a_vp;
4845fe58019SAttilio Rao 	struct vattr *vap = ap->a_vap;
4855fe58019SAttilio Rao 	struct ucred *cred = ap->a_cred;
4865fe58019SAttilio Rao 	struct thread *td = curthread;
4875fe58019SAttilio Rao 	struct fuse_vnode_data *fvdat = VTOFUD(vp);
4885fe58019SAttilio Rao 
4895fe58019SAttilio Rao 	int err = 0;
4905fe58019SAttilio Rao 	int dataflags;
4915fe58019SAttilio Rao 	struct fuse_dispatcher fdi;
4925fe58019SAttilio Rao 
4934cff153bSAttilio Rao 	FS_DEBUG2G("inode=%ju\n", (uintmax_t)VTOI(vp));
4945fe58019SAttilio Rao 
4955fe58019SAttilio Rao 	dataflags = fuse_get_mpdata(vnode_mount(vp))->dataflags;
4965fe58019SAttilio Rao 
4975fe58019SAttilio Rao 	/* Note that we are not bailing out on a dead file system just yet. */
4985fe58019SAttilio Rao 
4995fe58019SAttilio Rao 	if (!(dataflags & FSESS_INITED)) {
5005fe58019SAttilio Rao 		if (!vnode_isvroot(vp)) {
5015fe58019SAttilio Rao 			fdata_set_dead(fuse_get_mpdata(vnode_mount(vp)));
5025fe58019SAttilio Rao 			err = ENOTCONN;
5035fe58019SAttilio Rao 			debug_printf("fuse_getattr b: returning ENOTCONN\n");
5045fe58019SAttilio Rao 			return err;
5055fe58019SAttilio Rao 		} else {
5065fe58019SAttilio Rao 			goto fake;
5075fe58019SAttilio Rao 		}
5085fe58019SAttilio Rao 	}
5095fe58019SAttilio Rao 	fdisp_init(&fdi, 0);
5105fe58019SAttilio Rao 	if ((err = fdisp_simple_putget_vp(&fdi, FUSE_GETATTR, vp, td, cred))) {
5115fe58019SAttilio Rao 		if ((err == ENOTCONN) && vnode_isvroot(vp)) {
5125fe58019SAttilio Rao 			/* see comment at similar place in fuse_statfs() */
5135fe58019SAttilio Rao 			fdisp_destroy(&fdi);
5145fe58019SAttilio Rao 			goto fake;
5155fe58019SAttilio Rao 		}
5165fe58019SAttilio Rao 		if (err == ENOENT) {
5175fe58019SAttilio Rao 			fuse_internal_vnode_disappear(vp);
5185fe58019SAttilio Rao 		}
5195fe58019SAttilio Rao 		goto out;
5205fe58019SAttilio Rao 	}
52178a7722fSConrad Meyer 
52278a7722fSConrad Meyer 	cache_attrs(vp, (struct fuse_attr_out *)fdi.answ, vap);
523d9454fabSAttilio Rao 	if (vap->va_type != vnode_vtype(vp)) {
524d9454fabSAttilio Rao 		fuse_internal_vnode_disappear(vp);
525d9454fabSAttilio Rao 		err = ENOENT;
526d9454fabSAttilio Rao 		goto out;
527d9454fabSAttilio Rao 	}
5285fe58019SAttilio Rao 	if ((fvdat->flag & FN_SIZECHANGE) != 0)
5295fe58019SAttilio Rao 		vap->va_size = fvdat->filesize;
5305fe58019SAttilio Rao 
5315fe58019SAttilio Rao 	if (vnode_isreg(vp) && (fvdat->flag & FN_SIZECHANGE) == 0) {
5325fe58019SAttilio Rao 		/*
5335fe58019SAttilio Rao 	         * This is for those cases when the file size changed without us
5345fe58019SAttilio Rao 	         * knowing, and we want to catch up.
5355fe58019SAttilio Rao 	         */
5365fe58019SAttilio Rao 		off_t new_filesize = ((struct fuse_attr_out *)
5375fe58019SAttilio Rao 				      fdi.answ)->attr.size;
5385fe58019SAttilio Rao 
5395fe58019SAttilio Rao 		if (fvdat->filesize != new_filesize) {
5405fe58019SAttilio Rao 			fuse_vnode_setsize(vp, cred, new_filesize);
541194e691aSConrad Meyer 			fvdat->flag &= ~FN_SIZECHANGE;
5425fe58019SAttilio Rao 		}
5435fe58019SAttilio Rao 	}
5445fe58019SAttilio Rao 	debug_printf("fuse_getattr e: returning 0\n");
5455fe58019SAttilio Rao 
5465fe58019SAttilio Rao out:
5475fe58019SAttilio Rao 	fdisp_destroy(&fdi);
5485fe58019SAttilio Rao 	return err;
5495fe58019SAttilio Rao 
5505fe58019SAttilio Rao fake:
5515fe58019SAttilio Rao 	bzero(vap, sizeof(*vap));
5525fe58019SAttilio Rao 	vap->va_type = vnode_vtype(vp);
5535fe58019SAttilio Rao 
5545fe58019SAttilio Rao 	return 0;
5555fe58019SAttilio Rao }
5565fe58019SAttilio Rao 
5575fe58019SAttilio Rao /*
5585fe58019SAttilio Rao     struct vnop_inactive_args {
5595fe58019SAttilio Rao 	struct vnode *a_vp;
5605fe58019SAttilio Rao 	struct thread *a_td;
5615fe58019SAttilio Rao     };
5625fe58019SAttilio Rao */
5635fe58019SAttilio Rao static int
5645fe58019SAttilio Rao fuse_vnop_inactive(struct vop_inactive_args *ap)
5655fe58019SAttilio Rao {
5665fe58019SAttilio Rao 	struct vnode *vp = ap->a_vp;
5675fe58019SAttilio Rao 	struct thread *td = ap->a_td;
5685fe58019SAttilio Rao 
5695fe58019SAttilio Rao 	struct fuse_vnode_data *fvdat = VTOFUD(vp);
5705fe58019SAttilio Rao 	struct fuse_filehandle *fufh = NULL;
5715fe58019SAttilio Rao 
5725fe58019SAttilio Rao 	int type, need_flush = 1;
5735fe58019SAttilio Rao 
5744cff153bSAttilio Rao 	FS_DEBUG("inode=%ju\n", (uintmax_t)VTOI(vp));
5755fe58019SAttilio Rao 
5765fe58019SAttilio Rao 	for (type = 0; type < FUFH_MAXTYPE; type++) {
5775fe58019SAttilio Rao 		fufh = &(fvdat->fufh[type]);
5785fe58019SAttilio Rao 		if (FUFH_IS_VALID(fufh)) {
5795fe58019SAttilio Rao 			if (need_flush && vp->v_type == VREG) {
5805fe58019SAttilio Rao 				if ((VTOFUD(vp)->flag & FN_SIZECHANGE) != 0) {
5815fe58019SAttilio Rao 					fuse_vnode_savesize(vp, NULL);
5825fe58019SAttilio Rao 				}
5835fe58019SAttilio Rao 				if (fuse_data_cache_invalidate ||
5845fe58019SAttilio Rao 				    (fvdat->flag & FN_REVOKED) != 0)
5855fe58019SAttilio Rao 					fuse_io_invalbuf(vp, td);
5865fe58019SAttilio Rao 				else
5875fe58019SAttilio Rao 					fuse_io_flushbuf(vp, MNT_WAIT, td);
5885fe58019SAttilio Rao 				need_flush = 0;
5895fe58019SAttilio Rao 			}
5905fe58019SAttilio Rao 			fuse_filehandle_close(vp, type, td, NULL);
5915fe58019SAttilio Rao 		}
5925fe58019SAttilio Rao 	}
5935fe58019SAttilio Rao 
5945fe58019SAttilio Rao 	if ((fvdat->flag & FN_REVOKED) != 0 && fuse_reclaim_revoked) {
5955fe58019SAttilio Rao 		vrecycle(vp);
5965fe58019SAttilio Rao 	}
5975fe58019SAttilio Rao 	return 0;
5985fe58019SAttilio Rao }
5995fe58019SAttilio Rao 
6005fe58019SAttilio Rao /*
6015fe58019SAttilio Rao     struct vnop_link_args {
6025fe58019SAttilio Rao 	struct vnode *a_tdvp;
6035fe58019SAttilio Rao 	struct vnode *a_vp;
6045fe58019SAttilio Rao 	struct componentname *a_cnp;
6055fe58019SAttilio Rao     };
6065fe58019SAttilio Rao */
6075fe58019SAttilio Rao static int
6085fe58019SAttilio Rao fuse_vnop_link(struct vop_link_args *ap)
6095fe58019SAttilio Rao {
6105fe58019SAttilio Rao 	struct vnode *vp = ap->a_vp;
6115fe58019SAttilio Rao 	struct vnode *tdvp = ap->a_tdvp;
6125fe58019SAttilio Rao 	struct componentname *cnp = ap->a_cnp;
6135fe58019SAttilio Rao 
6145fe58019SAttilio Rao 	struct vattr *vap = VTOVA(vp);
6155fe58019SAttilio Rao 
6165fe58019SAttilio Rao 	struct fuse_dispatcher fdi;
6175fe58019SAttilio Rao 	struct fuse_entry_out *feo;
6185fe58019SAttilio Rao 	struct fuse_link_in fli;
6195fe58019SAttilio Rao 
6205fe58019SAttilio Rao 	int err;
6215fe58019SAttilio Rao 
6225fe58019SAttilio Rao 	fuse_trace_printf_vnop();
6235fe58019SAttilio Rao 
6245fe58019SAttilio Rao 	if (fuse_isdeadfs(vp)) {
6255fe58019SAttilio Rao 		return ENXIO;
6265fe58019SAttilio Rao 	}
6275fe58019SAttilio Rao 	if (vnode_mount(tdvp) != vnode_mount(vp)) {
6285fe58019SAttilio Rao 		return EXDEV;
6295fe58019SAttilio Rao 	}
63078a7722fSConrad Meyer 
63178a7722fSConrad Meyer 	/*
63278a7722fSConrad Meyer 	 * This is a seatbelt check to protect naive userspace filesystems from
63378a7722fSConrad Meyer 	 * themselves and the limitations of the FUSE IPC protocol.  If a
63478a7722fSConrad Meyer 	 * filesystem does not allow attribute caching, assume it is capable of
63578a7722fSConrad Meyer 	 * validating that nlink does not overflow.
63678a7722fSConrad Meyer 	 */
63778a7722fSConrad Meyer 	if (vap != NULL && vap->va_nlink >= FUSE_LINK_MAX)
6385fe58019SAttilio Rao 		return EMLINK;
6395fe58019SAttilio Rao 	fli.oldnodeid = VTOI(vp);
6405fe58019SAttilio Rao 
6415fe58019SAttilio Rao 	fdisp_init(&fdi, 0);
6425fe58019SAttilio Rao 	fuse_internal_newentry_makerequest(vnode_mount(tdvp), VTOI(tdvp), cnp,
6435fe58019SAttilio Rao 	    FUSE_LINK, &fli, sizeof(fli), &fdi);
6445fe58019SAttilio Rao 	if ((err = fdisp_wait_answ(&fdi))) {
6455fe58019SAttilio Rao 		goto out;
6465fe58019SAttilio Rao 	}
6475fe58019SAttilio Rao 	feo = fdi.answ;
6485fe58019SAttilio Rao 
6495fe58019SAttilio Rao 	err = fuse_internal_checkentry(feo, vnode_vtype(vp));
6505fe58019SAttilio Rao out:
6515fe58019SAttilio Rao 	fdisp_destroy(&fdi);
6525fe58019SAttilio Rao 	return err;
6535fe58019SAttilio Rao }
6545fe58019SAttilio Rao 
6555fe58019SAttilio Rao /*
6565fe58019SAttilio Rao     struct vnop_lookup_args {
6575fe58019SAttilio Rao 	struct vnodeop_desc *a_desc;
6585fe58019SAttilio Rao 	struct vnode *a_dvp;
6595fe58019SAttilio Rao 	struct vnode **a_vpp;
6605fe58019SAttilio Rao 	struct componentname *a_cnp;
6615fe58019SAttilio Rao     };
6625fe58019SAttilio Rao */
6635fe58019SAttilio Rao int
6645fe58019SAttilio Rao fuse_vnop_lookup(struct vop_lookup_args *ap)
6655fe58019SAttilio Rao {
6665fe58019SAttilio Rao 	struct vnode *dvp = ap->a_dvp;
6675fe58019SAttilio Rao 	struct vnode **vpp = ap->a_vpp;
6685fe58019SAttilio Rao 	struct componentname *cnp = ap->a_cnp;
6695fe58019SAttilio Rao 	struct thread *td = cnp->cn_thread;
6705fe58019SAttilio Rao 	struct ucred *cred = cnp->cn_cred;
6715fe58019SAttilio Rao 
6725fe58019SAttilio Rao 	int nameiop = cnp->cn_nameiop;
6735fe58019SAttilio Rao 	int flags = cnp->cn_flags;
6745fe58019SAttilio Rao 	int wantparent = flags & (LOCKPARENT | WANTPARENT);
6755fe58019SAttilio Rao 	int islastcn = flags & ISLASTCN;
6765fe58019SAttilio Rao 	struct mount *mp = vnode_mount(dvp);
6775fe58019SAttilio Rao 
6785fe58019SAttilio Rao 	int err = 0;
6795fe58019SAttilio Rao 	int lookup_err = 0;
6805fe58019SAttilio Rao 	struct vnode *vp = NULL;
6815fe58019SAttilio Rao 
6825fe58019SAttilio Rao 	struct fuse_dispatcher fdi;
6835fe58019SAttilio Rao 	enum fuse_opcode op;
6845fe58019SAttilio Rao 
6855fe58019SAttilio Rao 	uint64_t nid;
6865fe58019SAttilio Rao 	struct fuse_access_param facp;
6875fe58019SAttilio Rao 
6884cff153bSAttilio Rao 	FS_DEBUG2G("parent_inode=%ju - %*s\n",
6895fe58019SAttilio Rao 	    (uintmax_t)VTOI(dvp), (int)cnp->cn_namelen, cnp->cn_nameptr);
6905fe58019SAttilio Rao 
6915fe58019SAttilio Rao 	if (fuse_isdeadfs(dvp)) {
6925fe58019SAttilio Rao 		*vpp = NULL;
6935fe58019SAttilio Rao 		return ENXIO;
6945fe58019SAttilio Rao 	}
6955fe58019SAttilio Rao 	if (!vnode_isdir(dvp)) {
6965fe58019SAttilio Rao 		return ENOTDIR;
6975fe58019SAttilio Rao 	}
6985fe58019SAttilio Rao 	if (islastcn && vfs_isrdonly(mp) && (nameiop != LOOKUP)) {
6995fe58019SAttilio Rao 		return EROFS;
7005fe58019SAttilio Rao 	}
7015fe58019SAttilio Rao 	/*
7025fe58019SAttilio Rao 	 * We do access check prior to doing anything else only in the case
7035fe58019SAttilio Rao 	 * when we are at fs root (we'd like to say, "we are at the first
7045fe58019SAttilio Rao 	 * component", but that's not exactly the same... nevermind).
7055fe58019SAttilio Rao 	 * See further comments at further access checks.
7065fe58019SAttilio Rao 	 */
7075fe58019SAttilio Rao 
7085fe58019SAttilio Rao 	bzero(&facp, sizeof(facp));
7095fe58019SAttilio Rao 	if (vnode_isvroot(dvp)) {	/* early permission check hack */
7105fe58019SAttilio Rao 		if ((err = fuse_internal_access(dvp, VEXEC, &facp, td, cred))) {
7115fe58019SAttilio Rao 			return err;
7125fe58019SAttilio Rao 		}
7135fe58019SAttilio Rao 	}
7145fe58019SAttilio Rao 	if (flags & ISDOTDOT) {
7155fe58019SAttilio Rao 		nid = VTOFUD(dvp)->parent_nid;
7165fe58019SAttilio Rao 		if (nid == 0) {
7175fe58019SAttilio Rao 			return ENOENT;
7185fe58019SAttilio Rao 		}
7195fe58019SAttilio Rao 		fdisp_init(&fdi, 0);
7205fe58019SAttilio Rao 		op = FUSE_GETATTR;
7215fe58019SAttilio Rao 		goto calldaemon;
7225fe58019SAttilio Rao 	} else if (cnp->cn_namelen == 1 && *(cnp->cn_nameptr) == '.') {
7235fe58019SAttilio Rao 		nid = VTOI(dvp);
7245fe58019SAttilio Rao 		fdisp_init(&fdi, 0);
7255fe58019SAttilio Rao 		op = FUSE_GETATTR;
7265fe58019SAttilio Rao 		goto calldaemon;
7275fe58019SAttilio Rao 	} else if (fuse_lookup_cache_enable) {
7285fe58019SAttilio Rao 		err = cache_lookup(dvp, vpp, cnp, NULL, NULL);
7295fe58019SAttilio Rao 		switch (err) {
7305fe58019SAttilio Rao 
7315fe58019SAttilio Rao 		case -1:		/* positive match */
7325fe58019SAttilio Rao 			atomic_add_acq_long(&fuse_lookup_cache_hits, 1);
7335fe58019SAttilio Rao 			return 0;
7345fe58019SAttilio Rao 
7355fe58019SAttilio Rao 		case 0:		/* no match in cache */
7365fe58019SAttilio Rao 			atomic_add_acq_long(&fuse_lookup_cache_misses, 1);
7375fe58019SAttilio Rao 			break;
7385fe58019SAttilio Rao 
7395fe58019SAttilio Rao 		case ENOENT:		/* negative match */
7405fe58019SAttilio Rao 			/* fall through */
7415fe58019SAttilio Rao 		default:
7425fe58019SAttilio Rao 			return err;
7435fe58019SAttilio Rao 		}
7445fe58019SAttilio Rao 	}
7455fe58019SAttilio Rao 	nid = VTOI(dvp);
7465fe58019SAttilio Rao 	fdisp_init(&fdi, cnp->cn_namelen + 1);
7475fe58019SAttilio Rao 	op = FUSE_LOOKUP;
7485fe58019SAttilio Rao 
7495fe58019SAttilio Rao calldaemon:
7505fe58019SAttilio Rao 	fdisp_make(&fdi, op, mp, nid, td, cred);
7515fe58019SAttilio Rao 
7525fe58019SAttilio Rao 	if (op == FUSE_LOOKUP) {
7535fe58019SAttilio Rao 		memcpy(fdi.indata, cnp->cn_nameptr, cnp->cn_namelen);
7545fe58019SAttilio Rao 		((char *)fdi.indata)[cnp->cn_namelen] = '\0';
7555fe58019SAttilio Rao 	}
7565fe58019SAttilio Rao 	lookup_err = fdisp_wait_answ(&fdi);
7575fe58019SAttilio Rao 
7585fe58019SAttilio Rao 	if ((op == FUSE_LOOKUP) && !lookup_err) {	/* lookup call succeeded */
7595fe58019SAttilio Rao 		nid = ((struct fuse_entry_out *)fdi.answ)->nodeid;
7605fe58019SAttilio Rao 		if (!nid) {
7615fe58019SAttilio Rao 			/*
7625fe58019SAttilio Rao 	                 * zero nodeid is the same as "not found",
7635fe58019SAttilio Rao 	                 * but it's also cacheable (which we keep
7645fe58019SAttilio Rao 	                 * keep on doing not as of writing this)
7655fe58019SAttilio Rao 	                 */
7665fe58019SAttilio Rao 			lookup_err = ENOENT;
7675fe58019SAttilio Rao 		} else if (nid == FUSE_ROOT_ID) {
7685fe58019SAttilio Rao 			lookup_err = EINVAL;
7695fe58019SAttilio Rao 		}
7705fe58019SAttilio Rao 	}
7715fe58019SAttilio Rao 	if (lookup_err &&
7725fe58019SAttilio Rao 	    (!fdi.answ_stat || lookup_err != ENOENT || op != FUSE_LOOKUP)) {
7735fe58019SAttilio Rao 		fdisp_destroy(&fdi);
7745fe58019SAttilio Rao 		return lookup_err;
7755fe58019SAttilio Rao 	}
7765fe58019SAttilio Rao 	/* lookup_err, if non-zero, must be ENOENT at this point */
7775fe58019SAttilio Rao 
7785fe58019SAttilio Rao 	if (lookup_err) {
7795fe58019SAttilio Rao 
7805fe58019SAttilio Rao 		if ((nameiop == CREATE || nameiop == RENAME) && islastcn
7815fe58019SAttilio Rao 		     /* && directory dvp has not been removed */ ) {
7825fe58019SAttilio Rao 
7835fe58019SAttilio Rao 			if (vfs_isrdonly(mp)) {
7845fe58019SAttilio Rao 				err = EROFS;
7855fe58019SAttilio Rao 				goto out;
7865fe58019SAttilio Rao 			}
7875fe58019SAttilio Rao #if 0 /* THINK_ABOUT_THIS */
7885fe58019SAttilio Rao 			if ((err = fuse_internal_access(dvp, VWRITE, cred, td, &facp))) {
7895fe58019SAttilio Rao 				goto out;
7905fe58019SAttilio Rao 			}
7915fe58019SAttilio Rao #endif
7925fe58019SAttilio Rao 
7935fe58019SAttilio Rao 			/*
7945fe58019SAttilio Rao 	                 * Possibly record the position of a slot in the
7955fe58019SAttilio Rao 	                 * directory large enough for the new component name.
7965fe58019SAttilio Rao 	                 * This can be recorded in the vnode private data for
7975fe58019SAttilio Rao 	                 * dvp. Set the SAVENAME flag to hold onto the
7985fe58019SAttilio Rao 	                 * pathname for use later in VOP_CREATE or VOP_RENAME.
7995fe58019SAttilio Rao 	                 */
8005fe58019SAttilio Rao 			cnp->cn_flags |= SAVENAME;
8015fe58019SAttilio Rao 
8025fe58019SAttilio Rao 			err = EJUSTRETURN;
8035fe58019SAttilio Rao 			goto out;
8045fe58019SAttilio Rao 		}
8055fe58019SAttilio Rao 		/* Consider inserting name into cache. */
8065fe58019SAttilio Rao 
8075fe58019SAttilio Rao 		/*
8085fe58019SAttilio Rao 	         * No we can't use negative caching, as the fs
8095fe58019SAttilio Rao 	         * changes are out of our control.
8105fe58019SAttilio Rao 	         * False positives' falseness turns out just as things
8115fe58019SAttilio Rao 	         * go by, but false negatives' falseness doesn't.
8125fe58019SAttilio Rao 	         * (and aiding the caching mechanism with extra control
8135fe58019SAttilio Rao 	         * mechanisms comes quite close to beating the whole purpose
8145fe58019SAttilio Rao 	         * caching...)
8155fe58019SAttilio Rao 	         */
8165fe58019SAttilio Rao #if 0
8176c21f6edSKonstantin Belousov 		if ((cnp->cn_flags & MAKEENTRY) != 0) {
8184cff153bSAttilio Rao 			FS_DEBUG("inserting NULL into cache\n");
8195fe58019SAttilio Rao 			cache_enter(dvp, NULL, cnp);
8205fe58019SAttilio Rao 		}
8215fe58019SAttilio Rao #endif
8225fe58019SAttilio Rao 		err = ENOENT;
8235fe58019SAttilio Rao 		goto out;
8245fe58019SAttilio Rao 
8255fe58019SAttilio Rao 	} else {
8265fe58019SAttilio Rao 
8275fe58019SAttilio Rao 		/* !lookup_err */
8285fe58019SAttilio Rao 
8295fe58019SAttilio Rao 		struct fuse_entry_out *feo = NULL;
8305fe58019SAttilio Rao 		struct fuse_attr *fattr = NULL;
8315fe58019SAttilio Rao 
8325fe58019SAttilio Rao 		if (op == FUSE_GETATTR) {
8335fe58019SAttilio Rao 			fattr = &((struct fuse_attr_out *)fdi.answ)->attr;
8345fe58019SAttilio Rao 		} else {
8355fe58019SAttilio Rao 			feo = (struct fuse_entry_out *)fdi.answ;
8365fe58019SAttilio Rao 			fattr = &(feo->attr);
8375fe58019SAttilio Rao 		}
8385fe58019SAttilio Rao 
8395fe58019SAttilio Rao 		/*
8405fe58019SAttilio Rao 	         * If deleting, and at end of pathname, return parameters
8415fe58019SAttilio Rao 	         * which can be used to remove file.  If the wantparent flag
8425fe58019SAttilio Rao 	         * isn't set, we return only the directory, otherwise we go on
8435fe58019SAttilio Rao 	         * and lock the inode, being careful with ".".
8445fe58019SAttilio Rao 	         */
8455fe58019SAttilio Rao 		if (nameiop == DELETE && islastcn) {
8465fe58019SAttilio Rao 			/*
8475fe58019SAttilio Rao 	                 * Check for write access on directory.
8485fe58019SAttilio Rao 	                 */
8495fe58019SAttilio Rao 			facp.xuid = fattr->uid;
8505fe58019SAttilio Rao 			facp.facc_flags |= FACCESS_STICKY;
8515fe58019SAttilio Rao 			err = fuse_internal_access(dvp, VWRITE, &facp, td, cred);
8525fe58019SAttilio Rao 			facp.facc_flags &= ~FACCESS_XQUERIES;
8535fe58019SAttilio Rao 
8545fe58019SAttilio Rao 			if (err) {
8555fe58019SAttilio Rao 				goto out;
8565fe58019SAttilio Rao 			}
8575fe58019SAttilio Rao 			if (nid == VTOI(dvp)) {
8585fe58019SAttilio Rao 				vref(dvp);
8595fe58019SAttilio Rao 				*vpp = dvp;
8605fe58019SAttilio Rao 			} else {
86109176f09SConrad Meyer 				err = fuse_vnode_get(dvp->v_mount, feo, nid,
86209176f09SConrad Meyer 				    dvp, &vp, cnp, IFTOVT(fattr->mode));
8635fe58019SAttilio Rao 				if (err)
8645fe58019SAttilio Rao 					goto out;
8655fe58019SAttilio Rao 				*vpp = vp;
8665fe58019SAttilio Rao 			}
8675fe58019SAttilio Rao 
8685fe58019SAttilio Rao 			/*
8695fe58019SAttilio Rao 			 * Save the name for use in VOP_RMDIR and VOP_REMOVE
8705fe58019SAttilio Rao 			 * later.
8715fe58019SAttilio Rao 			 */
8725fe58019SAttilio Rao 			cnp->cn_flags |= SAVENAME;
8735fe58019SAttilio Rao 			goto out;
8745fe58019SAttilio Rao 
8755fe58019SAttilio Rao 		}
8765fe58019SAttilio Rao 		/*
8775fe58019SAttilio Rao 	         * If rewriting (RENAME), return the inode and the
8785fe58019SAttilio Rao 	         * information required to rewrite the present directory
8795fe58019SAttilio Rao 	         * Must get inode of directory entry to verify it's a
8805fe58019SAttilio Rao 	         * regular file, or empty directory.
8815fe58019SAttilio Rao 	         */
8825fe58019SAttilio Rao 		if (nameiop == RENAME && wantparent && islastcn) {
8835fe58019SAttilio Rao 
8845fe58019SAttilio Rao #if 0 /* THINK_ABOUT_THIS */
8855fe58019SAttilio Rao 			if ((err = fuse_internal_access(dvp, VWRITE, cred, td, &facp))) {
8865fe58019SAttilio Rao 				goto out;
8875fe58019SAttilio Rao 			}
8885fe58019SAttilio Rao #endif
8895fe58019SAttilio Rao 
8905fe58019SAttilio Rao 			/*
8915fe58019SAttilio Rao 	                 * Check for "."
8925fe58019SAttilio Rao 	                 */
8935fe58019SAttilio Rao 			if (nid == VTOI(dvp)) {
8945fe58019SAttilio Rao 				err = EISDIR;
8955fe58019SAttilio Rao 				goto out;
8965fe58019SAttilio Rao 			}
89709176f09SConrad Meyer 			err = fuse_vnode_get(vnode_mount(dvp), feo, nid, dvp,
89809176f09SConrad Meyer 			    &vp, cnp, IFTOVT(fattr->mode));
8995fe58019SAttilio Rao 			if (err) {
9005fe58019SAttilio Rao 				goto out;
9015fe58019SAttilio Rao 			}
9025fe58019SAttilio Rao 			*vpp = vp;
9035fe58019SAttilio Rao 			/*
9045fe58019SAttilio Rao 	                 * Save the name for use in VOP_RENAME later.
9055fe58019SAttilio Rao 	                 */
9065fe58019SAttilio Rao 			cnp->cn_flags |= SAVENAME;
9075fe58019SAttilio Rao 
9085fe58019SAttilio Rao 			goto out;
9095fe58019SAttilio Rao 		}
9105fe58019SAttilio Rao 		if (flags & ISDOTDOT) {
9115fe58019SAttilio Rao 			struct mount *mp;
9125fe58019SAttilio Rao 			int ltype;
9135fe58019SAttilio Rao 
9145fe58019SAttilio Rao 			/*
9155fe58019SAttilio Rao 			 * Expanded copy of vn_vget_ino() so that
9165fe58019SAttilio Rao 			 * fuse_vnode_get() can be used.
9175fe58019SAttilio Rao 			 */
9185fe58019SAttilio Rao 			mp = dvp->v_mount;
9195fe58019SAttilio Rao 			ltype = VOP_ISLOCKED(dvp);
9205fe58019SAttilio Rao 			err = vfs_busy(mp, MBF_NOWAIT);
9215fe58019SAttilio Rao 			if (err != 0) {
9225fe58019SAttilio Rao 				vfs_ref(mp);
9235fe58019SAttilio Rao 				VOP_UNLOCK(dvp, 0);
9245fe58019SAttilio Rao 				err = vfs_busy(mp, 0);
9255fe58019SAttilio Rao 				vn_lock(dvp, ltype | LK_RETRY);
9265fe58019SAttilio Rao 				vfs_rel(mp);
9275fe58019SAttilio Rao 				if (err)
9285fe58019SAttilio Rao 					goto out;
9295fe58019SAttilio Rao 				if ((dvp->v_iflag & VI_DOOMED) != 0) {
9305fe58019SAttilio Rao 					err = ENOENT;
9315fe58019SAttilio Rao 					vfs_unbusy(mp);
9325fe58019SAttilio Rao 					goto out;
9335fe58019SAttilio Rao 				}
9345fe58019SAttilio Rao 			}
9355fe58019SAttilio Rao 			VOP_UNLOCK(dvp, 0);
93609176f09SConrad Meyer 			err = fuse_vnode_get(vnode_mount(dvp), feo, nid, NULL,
93709176f09SConrad Meyer 			    &vp, cnp, IFTOVT(fattr->mode));
9385fe58019SAttilio Rao 			vfs_unbusy(mp);
9395fe58019SAttilio Rao 			vn_lock(dvp, ltype | LK_RETRY);
9405fe58019SAttilio Rao 			if ((dvp->v_iflag & VI_DOOMED) != 0) {
9415fe58019SAttilio Rao 				if (err == 0)
9425fe58019SAttilio Rao 					vput(vp);
9435fe58019SAttilio Rao 				err = ENOENT;
9445fe58019SAttilio Rao 			}
9455fe58019SAttilio Rao 			if (err)
9465fe58019SAttilio Rao 				goto out;
9475fe58019SAttilio Rao 			*vpp = vp;
9485fe58019SAttilio Rao 		} else if (nid == VTOI(dvp)) {
9495fe58019SAttilio Rao 			vref(dvp);
9505fe58019SAttilio Rao 			*vpp = dvp;
9515fe58019SAttilio Rao 		} else {
9523c324b94SConrad Meyer 			struct fuse_vnode_data *fvdat;
9533c324b94SConrad Meyer 
95409176f09SConrad Meyer 			err = fuse_vnode_get(vnode_mount(dvp), feo, nid, dvp,
95509176f09SConrad Meyer 			    &vp, cnp, IFTOVT(fattr->mode));
9565fe58019SAttilio Rao 			if (err) {
9575fe58019SAttilio Rao 				goto out;
9585fe58019SAttilio Rao 			}
9595fe58019SAttilio Rao 			fuse_vnode_setparent(vp, dvp);
9603c324b94SConrad Meyer 
9613c324b94SConrad Meyer 			/*
9623c324b94SConrad Meyer 			 * In the case where we are looking up a FUSE node
9633c324b94SConrad Meyer 			 * represented by an existing cached vnode, and the
9643c324b94SConrad Meyer 			 * true size reported by FUSE_LOOKUP doesn't match
9653c324b94SConrad Meyer 			 * the vnode's cached size, fix the vnode cache to
9663c324b94SConrad Meyer 			 * match the real object size.
9673c324b94SConrad Meyer 			 *
9683c324b94SConrad Meyer 			 * This can occur via FUSE distributed filesystems,
9693c324b94SConrad Meyer 			 * irregular files, etc.
9703c324b94SConrad Meyer 			 */
9713c324b94SConrad Meyer 			fvdat = VTOFUD(vp);
9723c324b94SConrad Meyer 			if (vnode_isreg(vp) &&
9733c324b94SConrad Meyer 			    fattr->size != fvdat->filesize) {
9743c324b94SConrad Meyer 				/*
9753c324b94SConrad Meyer 				 * The FN_SIZECHANGE flag reflects a dirty
9763c324b94SConrad Meyer 				 * append.  If userspace lets us know our cache
9773c324b94SConrad Meyer 				 * is invalid, that write was lost.  (Dirty
9783c324b94SConrad Meyer 				 * writes that do not cause append are also
9793c324b94SConrad Meyer 				 * lost, but we don't detect them here.)
9803c324b94SConrad Meyer 				 *
9813c324b94SConrad Meyer 				 * XXX: Maybe disable WB caching on this mount.
9823c324b94SConrad Meyer 				 */
9833c324b94SConrad Meyer 				if (fvdat->flag & FN_SIZECHANGE)
9843c324b94SConrad Meyer 					printf("%s: WB cache incoherent on "
9853c324b94SConrad Meyer 					    "%s!\n", __func__,
9863c324b94SConrad Meyer 					    vnode_mount(vp)->mnt_stat.f_mntonname);
9873c324b94SConrad Meyer 
9883c324b94SConrad Meyer 				(void)fuse_vnode_setsize(vp, cred, fattr->size);
9893c324b94SConrad Meyer 				fvdat->flag &= ~FN_SIZECHANGE;
9903c324b94SConrad Meyer 			}
9915fe58019SAttilio Rao 			*vpp = vp;
9925fe58019SAttilio Rao 		}
9935fe58019SAttilio Rao 
9945fe58019SAttilio Rao 		if (op == FUSE_GETATTR) {
99578a7722fSConrad Meyer 			cache_attrs(*vpp, (struct fuse_attr_out *)fdi.answ,
99678a7722fSConrad Meyer 			    NULL);
9975fe58019SAttilio Rao 		} else {
99878a7722fSConrad Meyer 			cache_attrs(*vpp, (struct fuse_entry_out *)fdi.answ,
99978a7722fSConrad Meyer 			    NULL);
10005fe58019SAttilio Rao 		}
10015fe58019SAttilio Rao 
10025fe58019SAttilio Rao 		/* Insert name into cache if appropriate. */
10035fe58019SAttilio Rao 
10045fe58019SAttilio Rao 		/*
10055fe58019SAttilio Rao 	         * Nooo, caching is evil. With caching, we can't avoid stale
10065fe58019SAttilio Rao 	         * information taking over the playground (cached info is not
10075fe58019SAttilio Rao 	         * just positive/negative, it does have qualitative aspects,
10085fe58019SAttilio Rao 	         * too). And a (VOP/FUSE)_GETATTR is always thrown anyway, when
10095fe58019SAttilio Rao 	         * walking down along cached path components, and that's not
10105fe58019SAttilio Rao 	         * any cheaper than FUSE_LOOKUP. This might change with
10115fe58019SAttilio Rao 	         * implementing kernel side attr caching, but... In Linux,
10125fe58019SAttilio Rao 	         * lookup results are not cached, and the daemon is bombarded
10135fe58019SAttilio Rao 	         * with FUSE_LOOKUPS on and on. This shows that by design, the
10145fe58019SAttilio Rao 	         * daemon is expected to handle frequent lookup queries
10155fe58019SAttilio Rao 	         * efficiently, do its caching in userspace, and so on.
10165fe58019SAttilio Rao 	         *
10175fe58019SAttilio Rao 	         * So just leave the name cache alone.
10185fe58019SAttilio Rao 	         */
10195fe58019SAttilio Rao 
10205fe58019SAttilio Rao 		/*
10215fe58019SAttilio Rao 	         * Well, now I know, Linux caches lookups, but with a
10225fe58019SAttilio Rao 	         * timeout... So it's the same thing as attribute caching:
10235fe58019SAttilio Rao 	         * we can deal with it when implement timeouts.
10245fe58019SAttilio Rao 	         */
10255fe58019SAttilio Rao #if 0
10265fe58019SAttilio Rao 		if (cnp->cn_flags & MAKEENTRY) {
10275fe58019SAttilio Rao 			cache_enter(dvp, *vpp, cnp);
10285fe58019SAttilio Rao 		}
10295fe58019SAttilio Rao #endif
10305fe58019SAttilio Rao 	}
10315fe58019SAttilio Rao out:
10325fe58019SAttilio Rao 	if (!lookup_err) {
10335fe58019SAttilio Rao 
10345fe58019SAttilio Rao 		/* No lookup error; need to clean up. */
10355fe58019SAttilio Rao 
10365fe58019SAttilio Rao 		if (err) {		/* Found inode; exit with no vnode. */
10375fe58019SAttilio Rao 			if (op == FUSE_LOOKUP) {
10385fe58019SAttilio Rao 				fuse_internal_forget_send(vnode_mount(dvp), td, cred,
10395fe58019SAttilio Rao 				    nid, 1);
10405fe58019SAttilio Rao 			}
10415fe58019SAttilio Rao 			fdisp_destroy(&fdi);
10425fe58019SAttilio Rao 			return err;
10435fe58019SAttilio Rao 		} else {
10445fe58019SAttilio Rao #ifndef NO_EARLY_PERM_CHECK_HACK
10455fe58019SAttilio Rao 			if (!islastcn) {
10465fe58019SAttilio Rao 				/*
10475fe58019SAttilio Rao 				 * We have the attributes of the next item
10485fe58019SAttilio Rao 				 * *now*, and it's a fact, and we do not
10495fe58019SAttilio Rao 				 * have to do extra work for it (ie, beg the
10505fe58019SAttilio Rao 				 * daemon), and it neither depends on such
10515fe58019SAttilio Rao 				 * accidental things like attr caching. So
10525fe58019SAttilio Rao 				 * the big idea: check credentials *now*,
10535fe58019SAttilio Rao 				 * not at the beginning of the next call to
10545fe58019SAttilio Rao 				 * lookup.
10555fe58019SAttilio Rao 				 *
10565fe58019SAttilio Rao 				 * The first item of the lookup chain (fs root)
10575fe58019SAttilio Rao 				 * won't be checked then here, of course, as
10585fe58019SAttilio Rao 				 * its never "the next". But go and see that
10595fe58019SAttilio Rao 				 * the root is taken care about at the very
10605fe58019SAttilio Rao 				 * beginning of this function.
10615fe58019SAttilio Rao 				 *
10625fe58019SAttilio Rao 				 * Now, given we want to do the access check
10635fe58019SAttilio Rao 				 * this way, one might ask: so then why not
10645fe58019SAttilio Rao 				 * do the access check just after fetching
10655fe58019SAttilio Rao 				 * the inode and its attributes from the
10665fe58019SAttilio Rao 				 * daemon? Why bother with producing the
10675fe58019SAttilio Rao 				 * corresponding vnode at all if something
10685fe58019SAttilio Rao 				 * is not OK? We know what's the deal as
10695fe58019SAttilio Rao 				 * soon as we get those attrs... There is
10705fe58019SAttilio Rao 				 * one bit of info though not given us by
10715fe58019SAttilio Rao 				 * the daemon: whether his response is
1072b3a15dddSPedro F. Giffuni 				 * authoritative or not... His response should
10735fe58019SAttilio Rao 				 * be ignored if something is mounted over
10745fe58019SAttilio Rao 				 * the dir in question. But that can be
10755fe58019SAttilio Rao 				 * known only by having the vnode...
10765fe58019SAttilio Rao 				 */
10775fe58019SAttilio Rao 				int tmpvtype = vnode_vtype(*vpp);
10785fe58019SAttilio Rao 
10795fe58019SAttilio Rao 				bzero(&facp, sizeof(facp));
10805fe58019SAttilio Rao 				/*the early perm check hack */
10815fe58019SAttilio Rao 				    facp.facc_flags |= FACCESS_VA_VALID;
10825fe58019SAttilio Rao 
10835fe58019SAttilio Rao 				if ((tmpvtype != VDIR) && (tmpvtype != VLNK)) {
10845fe58019SAttilio Rao 					err = ENOTDIR;
10855fe58019SAttilio Rao 				}
10865fe58019SAttilio Rao 				if (!err && !vnode_mountedhere(*vpp)) {
10875fe58019SAttilio Rao 					err = fuse_internal_access(*vpp, VEXEC, &facp, td, cred);
10885fe58019SAttilio Rao 				}
10895fe58019SAttilio Rao 				if (err) {
10905fe58019SAttilio Rao 					if (tmpvtype == VLNK)
10914cff153bSAttilio Rao 						FS_DEBUG("weird, permission error with a symlink?\n");
10925fe58019SAttilio Rao 					vput(*vpp);
10935fe58019SAttilio Rao 					*vpp = NULL;
10945fe58019SAttilio Rao 				}
10955fe58019SAttilio Rao 			}
10965fe58019SAttilio Rao #endif
10975fe58019SAttilio Rao 		}
10985fe58019SAttilio Rao 	}
10995fe58019SAttilio Rao 	fdisp_destroy(&fdi);
11005fe58019SAttilio Rao 
11015fe58019SAttilio Rao 	return err;
11025fe58019SAttilio Rao }
11035fe58019SAttilio Rao 
11045fe58019SAttilio Rao /*
11055fe58019SAttilio Rao     struct vnop_mkdir_args {
11065fe58019SAttilio Rao 	struct vnode *a_dvp;
11075fe58019SAttilio Rao 	struct vnode **a_vpp;
11085fe58019SAttilio Rao 	struct componentname *a_cnp;
11095fe58019SAttilio Rao 	struct vattr *a_vap;
11105fe58019SAttilio Rao     };
11115fe58019SAttilio Rao */
11125fe58019SAttilio Rao static int
11135fe58019SAttilio Rao fuse_vnop_mkdir(struct vop_mkdir_args *ap)
11145fe58019SAttilio Rao {
11155fe58019SAttilio Rao 	struct vnode *dvp = ap->a_dvp;
11165fe58019SAttilio Rao 	struct vnode **vpp = ap->a_vpp;
11175fe58019SAttilio Rao 	struct componentname *cnp = ap->a_cnp;
11185fe58019SAttilio Rao 	struct vattr *vap = ap->a_vap;
11195fe58019SAttilio Rao 
11205fe58019SAttilio Rao 	struct fuse_mkdir_in fmdi;
11215fe58019SAttilio Rao 
11225fe58019SAttilio Rao 	fuse_trace_printf_vnop();
11235fe58019SAttilio Rao 
11245fe58019SAttilio Rao 	if (fuse_isdeadfs(dvp)) {
11255fe58019SAttilio Rao 		return ENXIO;
11265fe58019SAttilio Rao 	}
11275fe58019SAttilio Rao 	fmdi.mode = MAKEIMODE(vap->va_type, vap->va_mode);
11285fe58019SAttilio Rao 
1129d9454fabSAttilio Rao 	return (fuse_internal_newentry(dvp, vpp, cnp, FUSE_MKDIR, &fmdi,
1130d9454fabSAttilio Rao 	    sizeof(fmdi), VDIR));
11315fe58019SAttilio Rao }
11325fe58019SAttilio Rao 
11335fe58019SAttilio Rao /*
11345fe58019SAttilio Rao     struct vnop_mknod_args {
11355fe58019SAttilio Rao 	struct vnode *a_dvp;
11365fe58019SAttilio Rao 	struct vnode **a_vpp;
11375fe58019SAttilio Rao 	struct componentname *a_cnp;
11385fe58019SAttilio Rao 	struct vattr *a_vap;
11395fe58019SAttilio Rao     };
11405fe58019SAttilio Rao */
11415fe58019SAttilio Rao static int
11425fe58019SAttilio Rao fuse_vnop_mknod(struct vop_mknod_args *ap)
11435fe58019SAttilio Rao {
11445fe58019SAttilio Rao 
11455fe58019SAttilio Rao 	return (EINVAL);
11465fe58019SAttilio Rao }
11475fe58019SAttilio Rao 
11485fe58019SAttilio Rao 
11495fe58019SAttilio Rao /*
11505fe58019SAttilio Rao     struct vnop_open_args {
11515fe58019SAttilio Rao 	struct vnode *a_vp;
11525fe58019SAttilio Rao 	int  a_mode;
11535fe58019SAttilio Rao 	struct ucred *a_cred;
11545fe58019SAttilio Rao 	struct thread *a_td;
11555fe58019SAttilio Rao 	int a_fdidx; / struct file *a_fp;
11565fe58019SAttilio Rao     };
11575fe58019SAttilio Rao */
11585fe58019SAttilio Rao static int
11595fe58019SAttilio Rao fuse_vnop_open(struct vop_open_args *ap)
11605fe58019SAttilio Rao {
11615fe58019SAttilio Rao 	struct vnode *vp = ap->a_vp;
11625fe58019SAttilio Rao 	int mode = ap->a_mode;
11635fe58019SAttilio Rao 	struct thread *td = ap->a_td;
11645fe58019SAttilio Rao 	struct ucred *cred = ap->a_cred;
11655fe58019SAttilio Rao 
11665fe58019SAttilio Rao 	fufh_type_t fufh_type;
11675fe58019SAttilio Rao 	struct fuse_vnode_data *fvdat;
11685fe58019SAttilio Rao 
11695fe58019SAttilio Rao 	int error, isdir = 0;
11701390cca2SRick Macklem 	int32_t fuse_open_flags;
11715fe58019SAttilio Rao 
11724cff153bSAttilio Rao 	FS_DEBUG2G("inode=%ju mode=0x%x\n", (uintmax_t)VTOI(vp), mode);
11735fe58019SAttilio Rao 
11745fe58019SAttilio Rao 	if (fuse_isdeadfs(vp)) {
11755fe58019SAttilio Rao 		return ENXIO;
11765fe58019SAttilio Rao 	}
1177*e7df9886SConrad Meyer 	if ((mode & (FREAD | FWRITE)) == 0)
1178*e7df9886SConrad Meyer 		return EINVAL;
1179*e7df9886SConrad Meyer 
11805fe58019SAttilio Rao 	fvdat = VTOFUD(vp);
11815fe58019SAttilio Rao 
11825fe58019SAttilio Rao 	if (vnode_isdir(vp)) {
11835fe58019SAttilio Rao 		isdir = 1;
11845fe58019SAttilio Rao 	}
11851390cca2SRick Macklem 	fuse_open_flags = 0;
11865fe58019SAttilio Rao 	if (isdir) {
11875fe58019SAttilio Rao 		fufh_type = FUFH_RDONLY;
11885fe58019SAttilio Rao 	} else {
11895fe58019SAttilio Rao 		fufh_type = fuse_filehandle_xlate_from_fflags(mode);
11901390cca2SRick Macklem 		/*
11911390cca2SRick Macklem 		 * For WRONLY opens, force DIRECT_IO.  This is necessary
11921390cca2SRick Macklem 		 * since writing a partial block through the buffer cache
11931390cca2SRick Macklem 		 * will result in a read of the block and that read won't
11941390cca2SRick Macklem 		 * be allowed by the WRONLY open.
11951390cca2SRick Macklem 		 */
11961390cca2SRick Macklem 		if (fufh_type == FUFH_WRONLY ||
11971390cca2SRick Macklem 		    (fvdat->flag & FN_DIRECTIO) != 0)
11981390cca2SRick Macklem 			fuse_open_flags = FOPEN_DIRECT_IO;
11995fe58019SAttilio Rao 	}
12005fe58019SAttilio Rao 
1201e6e24456SRick Macklem 	if (fuse_filehandle_validrw(vp, fufh_type) != FUFH_INVALID) {
12021390cca2SRick Macklem 		fuse_vnode_open(vp, fuse_open_flags, td);
12035fe58019SAttilio Rao 		return 0;
12045fe58019SAttilio Rao 	}
12055fe58019SAttilio Rao 	error = fuse_filehandle_open(vp, fufh_type, NULL, td, cred);
12065fe58019SAttilio Rao 
12075fe58019SAttilio Rao 	return error;
12085fe58019SAttilio Rao }
12095fe58019SAttilio Rao 
1210746c92e0SJohn Baldwin static int
1211746c92e0SJohn Baldwin fuse_vnop_pathconf(struct vop_pathconf_args *ap)
1212746c92e0SJohn Baldwin {
1213746c92e0SJohn Baldwin 
1214746c92e0SJohn Baldwin 	switch (ap->a_name) {
1215746c92e0SJohn Baldwin 	case _PC_FILESIZEBITS:
1216746c92e0SJohn Baldwin 		*ap->a_retval = 64;
1217746c92e0SJohn Baldwin 		return (0);
1218599afe53SJohn Baldwin 	case _PC_NAME_MAX:
1219599afe53SJohn Baldwin 		*ap->a_retval = NAME_MAX;
1220599afe53SJohn Baldwin 		return (0);
1221599afe53SJohn Baldwin 	case _PC_LINK_MAX:
1222f83f3d79SJohn Baldwin 		*ap->a_retval = MIN(LONG_MAX, FUSE_LINK_MAX);
1223599afe53SJohn Baldwin 		return (0);
1224746c92e0SJohn Baldwin 	case _PC_SYMLINK_MAX:
1225746c92e0SJohn Baldwin 		*ap->a_retval = MAXPATHLEN;
1226746c92e0SJohn Baldwin 		return (0);
1227746c92e0SJohn Baldwin 	case _PC_NO_TRUNC:
1228746c92e0SJohn Baldwin 		*ap->a_retval = 1;
1229746c92e0SJohn Baldwin 		return (0);
1230746c92e0SJohn Baldwin 	default:
1231746c92e0SJohn Baldwin 		return (vop_stdpathconf(ap));
1232746c92e0SJohn Baldwin 	}
1233746c92e0SJohn Baldwin }
1234746c92e0SJohn Baldwin 
12355fe58019SAttilio Rao /*
12365fe58019SAttilio Rao     struct vnop_read_args {
12375fe58019SAttilio Rao 	struct vnode *a_vp;
12385fe58019SAttilio Rao 	struct uio *a_uio;
12395fe58019SAttilio Rao 	int  a_ioflag;
12405fe58019SAttilio Rao 	struct ucred *a_cred;
12415fe58019SAttilio Rao     };
12425fe58019SAttilio Rao */
12435fe58019SAttilio Rao static int
12445fe58019SAttilio Rao fuse_vnop_read(struct vop_read_args *ap)
12455fe58019SAttilio Rao {
12465fe58019SAttilio Rao 	struct vnode *vp = ap->a_vp;
12475fe58019SAttilio Rao 	struct uio *uio = ap->a_uio;
12485fe58019SAttilio Rao 	int ioflag = ap->a_ioflag;
12495fe58019SAttilio Rao 	struct ucred *cred = ap->a_cred;
12505fe58019SAttilio Rao 
12514cff153bSAttilio Rao 	FS_DEBUG2G("inode=%ju offset=%jd resid=%zd\n",
12525fe58019SAttilio Rao 	    (uintmax_t)VTOI(vp), uio->uio_offset, uio->uio_resid);
12535fe58019SAttilio Rao 
12545fe58019SAttilio Rao 	if (fuse_isdeadfs(vp)) {
12555fe58019SAttilio Rao 		return ENXIO;
12565fe58019SAttilio Rao 	}
1257ead063e0SEdward Tomasz Napierala 
1258ead063e0SEdward Tomasz Napierala 	if (VTOFUD(vp)->flag & FN_DIRECTIO) {
1259ead063e0SEdward Tomasz Napierala 		ioflag |= IO_DIRECT;
1260ead063e0SEdward Tomasz Napierala 	}
1261ead063e0SEdward Tomasz Napierala 
12625fe58019SAttilio Rao 	return fuse_io_dispatch(vp, uio, ioflag, cred);
12635fe58019SAttilio Rao }
12645fe58019SAttilio Rao 
12655fe58019SAttilio Rao /*
12665fe58019SAttilio Rao     struct vnop_readdir_args {
12675fe58019SAttilio Rao 	struct vnode *a_vp;
12685fe58019SAttilio Rao 	struct uio *a_uio;
12695fe58019SAttilio Rao 	struct ucred *a_cred;
12705fe58019SAttilio Rao 	int *a_eofflag;
12715fe58019SAttilio Rao 	int *ncookies;
12725fe58019SAttilio Rao 	u_long **a_cookies;
12735fe58019SAttilio Rao     };
12745fe58019SAttilio Rao */
12755fe58019SAttilio Rao static int
12765fe58019SAttilio Rao fuse_vnop_readdir(struct vop_readdir_args *ap)
12775fe58019SAttilio Rao {
12785fe58019SAttilio Rao 	struct vnode *vp = ap->a_vp;
12795fe58019SAttilio Rao 	struct uio *uio = ap->a_uio;
12805fe58019SAttilio Rao 	struct ucred *cred = ap->a_cred;
12815fe58019SAttilio Rao 
12825fe58019SAttilio Rao 	struct fuse_filehandle *fufh = NULL;
12835fe58019SAttilio Rao 	struct fuse_iov cookediov;
12845fe58019SAttilio Rao 
12855fe58019SAttilio Rao 	int err = 0;
12865fe58019SAttilio Rao 	int freefufh = 0;
12875fe58019SAttilio Rao 
12884cff153bSAttilio Rao 	FS_DEBUG2G("inode=%ju\n", (uintmax_t)VTOI(vp));
12895fe58019SAttilio Rao 
12905fe58019SAttilio Rao 	if (fuse_isdeadfs(vp)) {
12915fe58019SAttilio Rao 		return ENXIO;
12925fe58019SAttilio Rao 	}
12935fe58019SAttilio Rao 	if (				/* XXXIP ((uio_iovcnt(uio) > 1)) || */
12945fe58019SAttilio Rao 	    (uio_resid(uio) < sizeof(struct dirent))) {
12955fe58019SAttilio Rao 		return EINVAL;
12965fe58019SAttilio Rao 	}
12975fe58019SAttilio Rao 
12985fe58019SAttilio Rao 	if (!fuse_filehandle_valid(vp, FUFH_RDONLY)) {
12994cff153bSAttilio Rao 		FS_DEBUG("calling readdir() before open()");
13005fe58019SAttilio Rao 		err = fuse_filehandle_open(vp, FUFH_RDONLY, &fufh, NULL, cred);
13015fe58019SAttilio Rao 		freefufh = 1;
13025fe58019SAttilio Rao 	} else {
13035fe58019SAttilio Rao 		err = fuse_filehandle_get(vp, FUFH_RDONLY, &fufh);
13045fe58019SAttilio Rao 	}
13055fe58019SAttilio Rao 	if (err) {
13065fe58019SAttilio Rao 		return (err);
13075fe58019SAttilio Rao 	}
13085fe58019SAttilio Rao #define DIRCOOKEDSIZE FUSE_DIRENT_ALIGN(FUSE_NAME_OFFSET + MAXNAMLEN + 1)
13095fe58019SAttilio Rao 	fiov_init(&cookediov, DIRCOOKEDSIZE);
13105fe58019SAttilio Rao 
13115fe58019SAttilio Rao 	err = fuse_internal_readdir(vp, uio, fufh, &cookediov);
13125fe58019SAttilio Rao 
13135fe58019SAttilio Rao 	fiov_teardown(&cookediov);
13145fe58019SAttilio Rao 	if (freefufh) {
13155fe58019SAttilio Rao 		fuse_filehandle_close(vp, FUFH_RDONLY, NULL, cred);
13165fe58019SAttilio Rao 	}
13175fe58019SAttilio Rao 	return err;
13185fe58019SAttilio Rao }
13195fe58019SAttilio Rao 
13205fe58019SAttilio Rao /*
13215fe58019SAttilio Rao     struct vnop_readlink_args {
13225fe58019SAttilio Rao 	struct vnode *a_vp;
13235fe58019SAttilio Rao 	struct uio *a_uio;
13245fe58019SAttilio Rao 	struct ucred *a_cred;
13255fe58019SAttilio Rao     };
13265fe58019SAttilio Rao */
13275fe58019SAttilio Rao static int
13285fe58019SAttilio Rao fuse_vnop_readlink(struct vop_readlink_args *ap)
13295fe58019SAttilio Rao {
13305fe58019SAttilio Rao 	struct vnode *vp = ap->a_vp;
13315fe58019SAttilio Rao 	struct uio *uio = ap->a_uio;
13325fe58019SAttilio Rao 	struct ucred *cred = ap->a_cred;
13335fe58019SAttilio Rao 
13345fe58019SAttilio Rao 	struct fuse_dispatcher fdi;
13355fe58019SAttilio Rao 	int err;
13365fe58019SAttilio Rao 
13374cff153bSAttilio Rao 	FS_DEBUG2G("inode=%ju\n", (uintmax_t)VTOI(vp));
13385fe58019SAttilio Rao 
13395fe58019SAttilio Rao 	if (fuse_isdeadfs(vp)) {
13405fe58019SAttilio Rao 		return ENXIO;
13415fe58019SAttilio Rao 	}
13425fe58019SAttilio Rao 	if (!vnode_islnk(vp)) {
13435fe58019SAttilio Rao 		return EINVAL;
13445fe58019SAttilio Rao 	}
13455fe58019SAttilio Rao 	fdisp_init(&fdi, 0);
13465fe58019SAttilio Rao 	err = fdisp_simple_putget_vp(&fdi, FUSE_READLINK, vp, curthread, cred);
13475fe58019SAttilio Rao 	if (err) {
13485fe58019SAttilio Rao 		goto out;
13495fe58019SAttilio Rao 	}
13505fe58019SAttilio Rao 	if (((char *)fdi.answ)[0] == '/' &&
13515fe58019SAttilio Rao 	    fuse_get_mpdata(vnode_mount(vp))->dataflags & FSESS_PUSH_SYMLINKS_IN) {
13525fe58019SAttilio Rao 		char *mpth = vnode_mount(vp)->mnt_stat.f_mntonname;
13535fe58019SAttilio Rao 
13545fe58019SAttilio Rao 		err = uiomove(mpth, strlen(mpth), uio);
13555fe58019SAttilio Rao 	}
13565fe58019SAttilio Rao 	if (!err) {
13575fe58019SAttilio Rao 		err = uiomove(fdi.answ, fdi.iosize, uio);
13585fe58019SAttilio Rao 	}
13595fe58019SAttilio Rao out:
13605fe58019SAttilio Rao 	fdisp_destroy(&fdi);
13615fe58019SAttilio Rao 	return err;
13625fe58019SAttilio Rao }
13635fe58019SAttilio Rao 
13645fe58019SAttilio Rao /*
13655fe58019SAttilio Rao     struct vnop_reclaim_args {
13665fe58019SAttilio Rao 	struct vnode *a_vp;
13675fe58019SAttilio Rao 	struct thread *a_td;
13685fe58019SAttilio Rao     };
13695fe58019SAttilio Rao */
13705fe58019SAttilio Rao static int
13715fe58019SAttilio Rao fuse_vnop_reclaim(struct vop_reclaim_args *ap)
13725fe58019SAttilio Rao {
13735fe58019SAttilio Rao 	struct vnode *vp = ap->a_vp;
13745fe58019SAttilio Rao 	struct thread *td = ap->a_td;
13755fe58019SAttilio Rao 
13765fe58019SAttilio Rao 	struct fuse_vnode_data *fvdat = VTOFUD(vp);
13775fe58019SAttilio Rao 	struct fuse_filehandle *fufh = NULL;
13785fe58019SAttilio Rao 
13795fe58019SAttilio Rao 	int type;
13805fe58019SAttilio Rao 
13815fe58019SAttilio Rao 	if (!fvdat) {
13825fe58019SAttilio Rao 		panic("FUSE: no vnode data during recycling");
13835fe58019SAttilio Rao 	}
13844cff153bSAttilio Rao 	FS_DEBUG("inode=%ju\n", (uintmax_t)VTOI(vp));
13855fe58019SAttilio Rao 
13865fe58019SAttilio Rao 	for (type = 0; type < FUFH_MAXTYPE; type++) {
13875fe58019SAttilio Rao 		fufh = &(fvdat->fufh[type]);
13885fe58019SAttilio Rao 		if (FUFH_IS_VALID(fufh)) {
13895fe58019SAttilio Rao 			printf("FUSE: vnode being reclaimed but fufh (type=%d) is valid",
13905fe58019SAttilio Rao 			    type);
13915fe58019SAttilio Rao 			fuse_filehandle_close(vp, type, td, NULL);
13925fe58019SAttilio Rao 		}
13935fe58019SAttilio Rao 	}
13945fe58019SAttilio Rao 
13955fe58019SAttilio Rao 	if ((!fuse_isdeadfs(vp)) && (fvdat->nlookup)) {
13965fe58019SAttilio Rao 		fuse_internal_forget_send(vnode_mount(vp), td, NULL, VTOI(vp),
13975fe58019SAttilio Rao 		    fvdat->nlookup);
13985fe58019SAttilio Rao 	}
13995fe58019SAttilio Rao 	fuse_vnode_setparent(vp, NULL);
14005fe58019SAttilio Rao 	cache_purge(vp);
14015fe58019SAttilio Rao 	vfs_hash_remove(vp);
14025fe58019SAttilio Rao 	vnode_destroy_vobject(vp);
14035fe58019SAttilio Rao 	fuse_vnode_destroy(vp);
14045fe58019SAttilio Rao 
14055fe58019SAttilio Rao 	return 0;
14065fe58019SAttilio Rao }
14075fe58019SAttilio Rao 
14085fe58019SAttilio Rao /*
14095fe58019SAttilio Rao     struct vnop_remove_args {
14105fe58019SAttilio Rao 	struct vnode *a_dvp;
14115fe58019SAttilio Rao 	struct vnode *a_vp;
14125fe58019SAttilio Rao 	struct componentname *a_cnp;
14135fe58019SAttilio Rao     };
14145fe58019SAttilio Rao */
14155fe58019SAttilio Rao static int
14165fe58019SAttilio Rao fuse_vnop_remove(struct vop_remove_args *ap)
14175fe58019SAttilio Rao {
14185fe58019SAttilio Rao 	struct vnode *dvp = ap->a_dvp;
14195fe58019SAttilio Rao 	struct vnode *vp = ap->a_vp;
14205fe58019SAttilio Rao 	struct componentname *cnp = ap->a_cnp;
14215fe58019SAttilio Rao 
14225fe58019SAttilio Rao 	int err;
14235fe58019SAttilio Rao 
14244cff153bSAttilio Rao 	FS_DEBUG2G("inode=%ju name=%*s\n",
14255fe58019SAttilio Rao 	    (uintmax_t)VTOI(vp), (int)cnp->cn_namelen, cnp->cn_nameptr);
14265fe58019SAttilio Rao 
14275fe58019SAttilio Rao 	if (fuse_isdeadfs(vp)) {
14285fe58019SAttilio Rao 		return ENXIO;
14295fe58019SAttilio Rao 	}
14305fe58019SAttilio Rao 	if (vnode_isdir(vp)) {
14315fe58019SAttilio Rao 		return EPERM;
14325fe58019SAttilio Rao 	}
14335fe58019SAttilio Rao 	cache_purge(vp);
14345fe58019SAttilio Rao 
14355fe58019SAttilio Rao 	err = fuse_internal_remove(dvp, vp, cnp, FUSE_UNLINK);
14365fe58019SAttilio Rao 
1437d9454fabSAttilio Rao 	if (err == 0)
14385fe58019SAttilio Rao 		fuse_internal_vnode_disappear(vp);
14395fe58019SAttilio Rao 	return err;
14405fe58019SAttilio Rao }
14415fe58019SAttilio Rao 
14425fe58019SAttilio Rao /*
14435fe58019SAttilio Rao     struct vnop_rename_args {
14445fe58019SAttilio Rao 	struct vnode *a_fdvp;
14455fe58019SAttilio Rao 	struct vnode *a_fvp;
14465fe58019SAttilio Rao 	struct componentname *a_fcnp;
14475fe58019SAttilio Rao 	struct vnode *a_tdvp;
14485fe58019SAttilio Rao 	struct vnode *a_tvp;
14495fe58019SAttilio Rao 	struct componentname *a_tcnp;
14505fe58019SAttilio Rao     };
14515fe58019SAttilio Rao */
14525fe58019SAttilio Rao static int
14535fe58019SAttilio Rao fuse_vnop_rename(struct vop_rename_args *ap)
14545fe58019SAttilio Rao {
14555fe58019SAttilio Rao 	struct vnode *fdvp = ap->a_fdvp;
14565fe58019SAttilio Rao 	struct vnode *fvp = ap->a_fvp;
14575fe58019SAttilio Rao 	struct componentname *fcnp = ap->a_fcnp;
14585fe58019SAttilio Rao 	struct vnode *tdvp = ap->a_tdvp;
14595fe58019SAttilio Rao 	struct vnode *tvp = ap->a_tvp;
14605fe58019SAttilio Rao 	struct componentname *tcnp = ap->a_tcnp;
14615fe58019SAttilio Rao 	struct fuse_data *data;
14625fe58019SAttilio Rao 
14635fe58019SAttilio Rao 	int err = 0;
14645fe58019SAttilio Rao 
14654cff153bSAttilio Rao 	FS_DEBUG2G("from: inode=%ju name=%*s -> to: inode=%ju name=%*s\n",
14665fe58019SAttilio Rao 	    (uintmax_t)VTOI(fvp), (int)fcnp->cn_namelen, fcnp->cn_nameptr,
14675fe58019SAttilio Rao 	    (uintmax_t)(tvp == NULL ? -1 : VTOI(tvp)),
14685fe58019SAttilio Rao 	    (int)tcnp->cn_namelen, tcnp->cn_nameptr);
14695fe58019SAttilio Rao 
14705fe58019SAttilio Rao 	if (fuse_isdeadfs(fdvp)) {
14715fe58019SAttilio Rao 		return ENXIO;
14725fe58019SAttilio Rao 	}
14735fe58019SAttilio Rao 	if (fvp->v_mount != tdvp->v_mount ||
14745fe58019SAttilio Rao 	    (tvp && fvp->v_mount != tvp->v_mount)) {
14754cff153bSAttilio Rao 		FS_DEBUG("cross-device rename: %s -> %s\n",
14765fe58019SAttilio Rao 		    fcnp->cn_nameptr, (tcnp != NULL ? tcnp->cn_nameptr : "(NULL)"));
14775fe58019SAttilio Rao 		err = EXDEV;
14785fe58019SAttilio Rao 		goto out;
14795fe58019SAttilio Rao 	}
14805fe58019SAttilio Rao 	cache_purge(fvp);
14815fe58019SAttilio Rao 
14825fe58019SAttilio Rao 	/*
14835fe58019SAttilio Rao 	 * FUSE library is expected to check if target directory is not
14845fe58019SAttilio Rao 	 * under the source directory in the file system tree.
14855fe58019SAttilio Rao 	 * Linux performs this check at VFS level.
14865fe58019SAttilio Rao 	 */
14875fe58019SAttilio Rao 	data = fuse_get_mpdata(vnode_mount(tdvp));
14885fe58019SAttilio Rao 	sx_xlock(&data->rename_lock);
14895fe58019SAttilio Rao 	err = fuse_internal_rename(fdvp, fcnp, tdvp, tcnp);
14905fe58019SAttilio Rao 	if (err == 0) {
1491d9454fabSAttilio Rao 		if (tdvp != fdvp)
14925fe58019SAttilio Rao 			fuse_vnode_setparent(fvp, tdvp);
14935fe58019SAttilio Rao 		if (tvp != NULL)
14945fe58019SAttilio Rao 			fuse_vnode_setparent(tvp, NULL);
14955fe58019SAttilio Rao 	}
14965fe58019SAttilio Rao 	sx_unlock(&data->rename_lock);
14975fe58019SAttilio Rao 
14985fe58019SAttilio Rao 	if (tvp != NULL && tvp != fvp) {
14995fe58019SAttilio Rao 		cache_purge(tvp);
15005fe58019SAttilio Rao 	}
15015fe58019SAttilio Rao 	if (vnode_isdir(fvp)) {
15025fe58019SAttilio Rao 		if ((tvp != NULL) && vnode_isdir(tvp)) {
15035fe58019SAttilio Rao 			cache_purge(tdvp);
15045fe58019SAttilio Rao 		}
15055fe58019SAttilio Rao 		cache_purge(fdvp);
15065fe58019SAttilio Rao 	}
15075fe58019SAttilio Rao out:
15085fe58019SAttilio Rao 	if (tdvp == tvp) {
15095fe58019SAttilio Rao 		vrele(tdvp);
15105fe58019SAttilio Rao 	} else {
15115fe58019SAttilio Rao 		vput(tdvp);
15125fe58019SAttilio Rao 	}
15135fe58019SAttilio Rao 	if (tvp != NULL) {
15145fe58019SAttilio Rao 		vput(tvp);
15155fe58019SAttilio Rao 	}
15165fe58019SAttilio Rao 	vrele(fdvp);
15175fe58019SAttilio Rao 	vrele(fvp);
15185fe58019SAttilio Rao 
15195fe58019SAttilio Rao 	return err;
15205fe58019SAttilio Rao }
15215fe58019SAttilio Rao 
15225fe58019SAttilio Rao /*
15235fe58019SAttilio Rao     struct vnop_rmdir_args {
15245fe58019SAttilio Rao 	    struct vnode *a_dvp;
15255fe58019SAttilio Rao 	    struct vnode *a_vp;
15265fe58019SAttilio Rao 	    struct componentname *a_cnp;
15275fe58019SAttilio Rao     } *ap;
15285fe58019SAttilio Rao */
15295fe58019SAttilio Rao static int
15305fe58019SAttilio Rao fuse_vnop_rmdir(struct vop_rmdir_args *ap)
15315fe58019SAttilio Rao {
15325fe58019SAttilio Rao 	struct vnode *dvp = ap->a_dvp;
15335fe58019SAttilio Rao 	struct vnode *vp = ap->a_vp;
15345fe58019SAttilio Rao 
15355fe58019SAttilio Rao 	int err;
15365fe58019SAttilio Rao 
15374cff153bSAttilio Rao 	FS_DEBUG2G("inode=%ju\n", (uintmax_t)VTOI(vp));
15385fe58019SAttilio Rao 
15395fe58019SAttilio Rao 	if (fuse_isdeadfs(vp)) {
15405fe58019SAttilio Rao 		return ENXIO;
15415fe58019SAttilio Rao 	}
15425fe58019SAttilio Rao 	if (VTOFUD(vp) == VTOFUD(dvp)) {
15435fe58019SAttilio Rao 		return EINVAL;
15445fe58019SAttilio Rao 	}
15455fe58019SAttilio Rao 	err = fuse_internal_remove(dvp, vp, ap->a_cnp, FUSE_RMDIR);
15465fe58019SAttilio Rao 
1547d9454fabSAttilio Rao 	if (err == 0)
15485fe58019SAttilio Rao 		fuse_internal_vnode_disappear(vp);
15495fe58019SAttilio Rao 	return err;
15505fe58019SAttilio Rao }
15515fe58019SAttilio Rao 
15525fe58019SAttilio Rao /*
15535fe58019SAttilio Rao     struct vnop_setattr_args {
15545fe58019SAttilio Rao 	struct vnode *a_vp;
15555fe58019SAttilio Rao 	struct vattr *a_vap;
15565fe58019SAttilio Rao 	struct ucred *a_cred;
15575fe58019SAttilio Rao 	struct thread *a_td;
15585fe58019SAttilio Rao     };
15595fe58019SAttilio Rao */
15605fe58019SAttilio Rao static int
15615fe58019SAttilio Rao fuse_vnop_setattr(struct vop_setattr_args *ap)
15625fe58019SAttilio Rao {
15635fe58019SAttilio Rao 	struct vnode *vp = ap->a_vp;
15645fe58019SAttilio Rao 	struct vattr *vap = ap->a_vap;
15655fe58019SAttilio Rao 	struct ucred *cred = ap->a_cred;
15665fe58019SAttilio Rao 	struct thread *td = curthread;
15675fe58019SAttilio Rao 
15685fe58019SAttilio Rao 	struct fuse_dispatcher fdi;
15695fe58019SAttilio Rao 	struct fuse_setattr_in *fsai;
15705fe58019SAttilio Rao 	struct fuse_access_param facp;
15715fe58019SAttilio Rao 
15725fe58019SAttilio Rao 	int err = 0;
15735fe58019SAttilio Rao 	enum vtype vtyp;
15745fe58019SAttilio Rao 	int sizechanged = 0;
15755fe58019SAttilio Rao 	uint64_t newsize = 0;
15765fe58019SAttilio Rao 
15774cff153bSAttilio Rao 	FS_DEBUG2G("inode=%ju\n", (uintmax_t)VTOI(vp));
15785fe58019SAttilio Rao 
15795fe58019SAttilio Rao 	if (fuse_isdeadfs(vp)) {
15805fe58019SAttilio Rao 		return ENXIO;
15815fe58019SAttilio Rao 	}
15825fe58019SAttilio Rao 	fdisp_init(&fdi, sizeof(*fsai));
15835fe58019SAttilio Rao 	fdisp_make_vp(&fdi, FUSE_SETATTR, vp, td, cred);
15845fe58019SAttilio Rao 	fsai = fdi.indata;
15855fe58019SAttilio Rao 	fsai->valid = 0;
15865fe58019SAttilio Rao 
15875fe58019SAttilio Rao 	bzero(&facp, sizeof(facp));
15885fe58019SAttilio Rao 
15895fe58019SAttilio Rao 	facp.xuid = vap->va_uid;
15905fe58019SAttilio Rao 	facp.xgid = vap->va_gid;
15915fe58019SAttilio Rao 
15925fe58019SAttilio Rao 	if (vap->va_uid != (uid_t)VNOVAL) {
15935fe58019SAttilio Rao 		facp.facc_flags |= FACCESS_CHOWN;
15945fe58019SAttilio Rao 		fsai->uid = vap->va_uid;
15955fe58019SAttilio Rao 		fsai->valid |= FATTR_UID;
15965fe58019SAttilio Rao 	}
15975fe58019SAttilio Rao 	if (vap->va_gid != (gid_t)VNOVAL) {
15985fe58019SAttilio Rao 		facp.facc_flags |= FACCESS_CHOWN;
15995fe58019SAttilio Rao 		fsai->gid = vap->va_gid;
16005fe58019SAttilio Rao 		fsai->valid |= FATTR_GID;
16015fe58019SAttilio Rao 	}
16025fe58019SAttilio Rao 	if (vap->va_size != VNOVAL) {
16035fe58019SAttilio Rao 
16045fe58019SAttilio Rao 		struct fuse_filehandle *fufh = NULL;
16055fe58019SAttilio Rao 
16065fe58019SAttilio Rao 		/*Truncate to a new value. */
16075fe58019SAttilio Rao 		    fsai->size = vap->va_size;
16085fe58019SAttilio Rao 		sizechanged = 1;
16095fe58019SAttilio Rao 		newsize = vap->va_size;
16105fe58019SAttilio Rao 		fsai->valid |= FATTR_SIZE;
16115fe58019SAttilio Rao 
16125fe58019SAttilio Rao 		fuse_filehandle_getrw(vp, FUFH_WRONLY, &fufh);
16135fe58019SAttilio Rao 		if (fufh) {
16145fe58019SAttilio Rao 			fsai->fh = fufh->fh_id;
16155fe58019SAttilio Rao 			fsai->valid |= FATTR_FH;
16165fe58019SAttilio Rao 		}
16175fe58019SAttilio Rao 	}
16185fe58019SAttilio Rao 	if (vap->va_atime.tv_sec != VNOVAL) {
16195fe58019SAttilio Rao 		fsai->atime = vap->va_atime.tv_sec;
16205fe58019SAttilio Rao 		fsai->atimensec = vap->va_atime.tv_nsec;
16215fe58019SAttilio Rao 		fsai->valid |= FATTR_ATIME;
16225fe58019SAttilio Rao 	}
16235fe58019SAttilio Rao 	if (vap->va_mtime.tv_sec != VNOVAL) {
16245fe58019SAttilio Rao 		fsai->mtime = vap->va_mtime.tv_sec;
16255fe58019SAttilio Rao 		fsai->mtimensec = vap->va_mtime.tv_nsec;
16265fe58019SAttilio Rao 		fsai->valid |= FATTR_MTIME;
16275fe58019SAttilio Rao 	}
16285fe58019SAttilio Rao 	if (vap->va_mode != (mode_t)VNOVAL) {
16295fe58019SAttilio Rao 		fsai->mode = vap->va_mode & ALLPERMS;
16305fe58019SAttilio Rao 		fsai->valid |= FATTR_MODE;
16315fe58019SAttilio Rao 	}
16325fe58019SAttilio Rao 	if (!fsai->valid) {
16335fe58019SAttilio Rao 		goto out;
16345fe58019SAttilio Rao 	}
16355fe58019SAttilio Rao 	vtyp = vnode_vtype(vp);
16365fe58019SAttilio Rao 
16375fe58019SAttilio Rao 	if (fsai->valid & FATTR_SIZE && vtyp == VDIR) {
16385fe58019SAttilio Rao 		err = EISDIR;
16395fe58019SAttilio Rao 		goto out;
16405fe58019SAttilio Rao 	}
16415fe58019SAttilio Rao 	if (vfs_isrdonly(vnode_mount(vp)) && (fsai->valid & ~FATTR_SIZE || vtyp == VREG)) {
16425fe58019SAttilio Rao 		err = EROFS;
16435fe58019SAttilio Rao 		goto out;
16445fe58019SAttilio Rao 	}
16455fe58019SAttilio Rao 	if (fsai->valid & ~FATTR_SIZE) {
16465fe58019SAttilio Rao 	  /*err = fuse_internal_access(vp, VADMIN, context, &facp); */
16475fe58019SAttilio Rao 	  /*XXX */
16485fe58019SAttilio Rao 		    err = 0;
16495fe58019SAttilio Rao 	}
16505fe58019SAttilio Rao 	facp.facc_flags &= ~FACCESS_XQUERIES;
16515fe58019SAttilio Rao 
16525fe58019SAttilio Rao 	if (err && !(fsai->valid & ~(FATTR_ATIME | FATTR_MTIME)) &&
16535fe58019SAttilio Rao 	    vap->va_vaflags & VA_UTIMES_NULL) {
16545fe58019SAttilio Rao 		err = fuse_internal_access(vp, VWRITE, &facp, td, cred);
16555fe58019SAttilio Rao 	}
1656d9454fabSAttilio Rao 	if (err)
16575fe58019SAttilio Rao 		goto out;
1658d9454fabSAttilio Rao 	if ((err = fdisp_wait_answ(&fdi)))
16595fe58019SAttilio Rao 		goto out;
16605fe58019SAttilio Rao 	vtyp = IFTOVT(((struct fuse_attr_out *)fdi.answ)->attr.mode);
16615fe58019SAttilio Rao 
16625fe58019SAttilio Rao 	if (vnode_vtype(vp) != vtyp) {
16635fe58019SAttilio Rao 		if (vnode_vtype(vp) == VNON && vtyp != VNON) {
16645fe58019SAttilio Rao 			debug_printf("FUSE: Dang! vnode_vtype is VNON and vtype isn't.\n");
16655fe58019SAttilio Rao 		} else {
16665fe58019SAttilio Rao 			/*
16675fe58019SAttilio Rao 	                 * STALE vnode, ditch
16685fe58019SAttilio Rao 	                 *
16695fe58019SAttilio Rao 	                 * The vnode has changed its type "behind our back". There's
16705fe58019SAttilio Rao 	                 * nothing really we can do, so let us just force an internal
16715fe58019SAttilio Rao 	                 * revocation and tell the caller to try again, if interested.
16725fe58019SAttilio Rao 	                 */
16735fe58019SAttilio Rao 			fuse_internal_vnode_disappear(vp);
16745fe58019SAttilio Rao 			err = EAGAIN;
16755fe58019SAttilio Rao 		}
16765fe58019SAttilio Rao 	}
1677194e691aSConrad Meyer 	if (err == 0)
167878a7722fSConrad Meyer 		cache_attrs(vp, (struct fuse_attr_out *)fdi.answ, NULL);
1679194e691aSConrad Meyer 
16805fe58019SAttilio Rao out:
16815fe58019SAttilio Rao 	fdisp_destroy(&fdi);
16825fe58019SAttilio Rao 	if (!err && sizechanged) {
16835fe58019SAttilio Rao 		fuse_vnode_setsize(vp, cred, newsize);
16845fe58019SAttilio Rao 		VTOFUD(vp)->flag &= ~FN_SIZECHANGE;
16855fe58019SAttilio Rao 	}
16865fe58019SAttilio Rao 	return err;
16875fe58019SAttilio Rao }
16885fe58019SAttilio Rao 
16895fe58019SAttilio Rao /*
16905fe58019SAttilio Rao     struct vnop_strategy_args {
16915fe58019SAttilio Rao 	struct vnode *a_vp;
16925fe58019SAttilio Rao 	struct buf *a_bp;
16935fe58019SAttilio Rao     };
16945fe58019SAttilio Rao */
16955fe58019SAttilio Rao static int
16965fe58019SAttilio Rao fuse_vnop_strategy(struct vop_strategy_args *ap)
16975fe58019SAttilio Rao {
16985fe58019SAttilio Rao 	struct vnode *vp = ap->a_vp;
16995fe58019SAttilio Rao 	struct buf *bp = ap->a_bp;
17005fe58019SAttilio Rao 
17015fe58019SAttilio Rao 	fuse_trace_printf_vnop();
17025fe58019SAttilio Rao 
17035fe58019SAttilio Rao 	if (!vp || fuse_isdeadfs(vp)) {
17045fe58019SAttilio Rao 		bp->b_ioflags |= BIO_ERROR;
17055fe58019SAttilio Rao 		bp->b_error = ENXIO;
17065fe58019SAttilio Rao 		bufdone(bp);
17075fe58019SAttilio Rao 		return ENXIO;
17085fe58019SAttilio Rao 	}
17095fe58019SAttilio Rao 	if (bp->b_iocmd == BIO_WRITE)
17105fe58019SAttilio Rao 		fuse_vnode_refreshsize(vp, NOCRED);
17115fe58019SAttilio Rao 
17125fe58019SAttilio Rao 	(void)fuse_io_strategy(vp, bp);
17135fe58019SAttilio Rao 
17145fe58019SAttilio Rao 	/*
17155fe58019SAttilio Rao 	 * This is a dangerous function. If returns error, that might mean a
17165fe58019SAttilio Rao 	 * panic. We prefer pretty much anything over being forced to panic
17175fe58019SAttilio Rao 	 * by a malicious daemon (a demon?). So we just return 0 anyway. You
17185fe58019SAttilio Rao 	 * should never mind this: this function has its own error
17195fe58019SAttilio Rao 	 * propagation mechanism via the argument buffer, so
17205fe58019SAttilio Rao 	 * not-that-melodramatic residents of the call chain still will be
17215fe58019SAttilio Rao 	 * able to know what to do.
17225fe58019SAttilio Rao 	 */
17235fe58019SAttilio Rao 	return 0;
17245fe58019SAttilio Rao }
17255fe58019SAttilio Rao 
17265fe58019SAttilio Rao 
17275fe58019SAttilio Rao /*
17285fe58019SAttilio Rao     struct vnop_symlink_args {
17295fe58019SAttilio Rao 	struct vnode *a_dvp;
17305fe58019SAttilio Rao 	struct vnode **a_vpp;
17315fe58019SAttilio Rao 	struct componentname *a_cnp;
17325fe58019SAttilio Rao 	struct vattr *a_vap;
17335fe58019SAttilio Rao 	char *a_target;
17345fe58019SAttilio Rao     };
17355fe58019SAttilio Rao */
17365fe58019SAttilio Rao static int
17375fe58019SAttilio Rao fuse_vnop_symlink(struct vop_symlink_args *ap)
17385fe58019SAttilio Rao {
17395fe58019SAttilio Rao 	struct vnode *dvp = ap->a_dvp;
17405fe58019SAttilio Rao 	struct vnode **vpp = ap->a_vpp;
17415fe58019SAttilio Rao 	struct componentname *cnp = ap->a_cnp;
17421493c2eeSBrooks Davis 	const char *target = ap->a_target;
17435fe58019SAttilio Rao 
17445fe58019SAttilio Rao 	struct fuse_dispatcher fdi;
17455fe58019SAttilio Rao 
17465fe58019SAttilio Rao 	int err;
17475fe58019SAttilio Rao 	size_t len;
17485fe58019SAttilio Rao 
17494cff153bSAttilio Rao 	FS_DEBUG2G("inode=%ju name=%*s\n",
17505fe58019SAttilio Rao 	    (uintmax_t)VTOI(dvp), (int)cnp->cn_namelen, cnp->cn_nameptr);
17515fe58019SAttilio Rao 
17525fe58019SAttilio Rao 	if (fuse_isdeadfs(dvp)) {
17535fe58019SAttilio Rao 		return ENXIO;
17545fe58019SAttilio Rao 	}
17555fe58019SAttilio Rao 	/*
17565fe58019SAttilio Rao 	 * Unlike the other creator type calls, here we have to create a message
17575fe58019SAttilio Rao 	 * where the name of the new entry comes first, and the data describing
17585fe58019SAttilio Rao 	 * the entry comes second.
17595fe58019SAttilio Rao 	 * Hence we can't rely on our handy fuse_internal_newentry() routine,
17605fe58019SAttilio Rao 	 * but put together the message manually and just call the core part.
17615fe58019SAttilio Rao 	 */
17625fe58019SAttilio Rao 
17635fe58019SAttilio Rao 	len = strlen(target) + 1;
17645fe58019SAttilio Rao 	fdisp_init(&fdi, len + cnp->cn_namelen + 1);
17655fe58019SAttilio Rao 	fdisp_make_vp(&fdi, FUSE_SYMLINK, dvp, curthread, NULL);
17665fe58019SAttilio Rao 
17675fe58019SAttilio Rao 	memcpy(fdi.indata, cnp->cn_nameptr, cnp->cn_namelen);
17685fe58019SAttilio Rao 	((char *)fdi.indata)[cnp->cn_namelen] = '\0';
17695fe58019SAttilio Rao 	memcpy((char *)fdi.indata + cnp->cn_namelen + 1, target, len);
17705fe58019SAttilio Rao 
17715fe58019SAttilio Rao 	err = fuse_internal_newentry_core(dvp, vpp, cnp, VLNK, &fdi);
17725fe58019SAttilio Rao 	fdisp_destroy(&fdi);
17735fe58019SAttilio Rao 	return err;
17745fe58019SAttilio Rao }
17755fe58019SAttilio Rao 
17765fe58019SAttilio Rao /*
17775fe58019SAttilio Rao     struct vnop_write_args {
17785fe58019SAttilio Rao 	struct vnode *a_vp;
17795fe58019SAttilio Rao 	struct uio *a_uio;
17805fe58019SAttilio Rao 	int  a_ioflag;
17815fe58019SAttilio Rao 	struct ucred *a_cred;
17825fe58019SAttilio Rao     };
17835fe58019SAttilio Rao */
17845fe58019SAttilio Rao static int
17855fe58019SAttilio Rao fuse_vnop_write(struct vop_write_args *ap)
17865fe58019SAttilio Rao {
17875fe58019SAttilio Rao 	struct vnode *vp = ap->a_vp;
17885fe58019SAttilio Rao 	struct uio *uio = ap->a_uio;
17895fe58019SAttilio Rao 	int ioflag = ap->a_ioflag;
17905fe58019SAttilio Rao 	struct ucred *cred = ap->a_cred;
17915fe58019SAttilio Rao 
17925fe58019SAttilio Rao 	fuse_trace_printf_vnop();
17935fe58019SAttilio Rao 
17945fe58019SAttilio Rao 	if (fuse_isdeadfs(vp)) {
17955fe58019SAttilio Rao 		return ENXIO;
17965fe58019SAttilio Rao 	}
17975fe58019SAttilio Rao 	fuse_vnode_refreshsize(vp, cred);
17985fe58019SAttilio Rao 
1799ead063e0SEdward Tomasz Napierala 	if (VTOFUD(vp)->flag & FN_DIRECTIO) {
1800ead063e0SEdward Tomasz Napierala 		ioflag |= IO_DIRECT;
1801ead063e0SEdward Tomasz Napierala 	}
1802ead063e0SEdward Tomasz Napierala 
18035fe58019SAttilio Rao 	return fuse_io_dispatch(vp, uio, ioflag, cred);
18045fe58019SAttilio Rao }
18055fe58019SAttilio Rao 
18065fe58019SAttilio Rao /*
18075fe58019SAttilio Rao     struct vnop_getpages_args {
18085fe58019SAttilio Rao 	struct vnode *a_vp;
18095fe58019SAttilio Rao 	vm_page_t *a_m;
18105fe58019SAttilio Rao 	int a_count;
18115fe58019SAttilio Rao 	int a_reqpage;
18125fe58019SAttilio Rao     };
18135fe58019SAttilio Rao */
18145fe58019SAttilio Rao static int
18155fe58019SAttilio Rao fuse_vnop_getpages(struct vop_getpages_args *ap)
18165fe58019SAttilio Rao {
18175fe58019SAttilio Rao 	int i, error, nextoff, size, toff, count, npages;
18185fe58019SAttilio Rao 	struct uio uio;
18195fe58019SAttilio Rao 	struct iovec iov;
18205fe58019SAttilio Rao 	vm_offset_t kva;
18215fe58019SAttilio Rao 	struct buf *bp;
18225fe58019SAttilio Rao 	struct vnode *vp;
18235fe58019SAttilio Rao 	struct thread *td;
18245fe58019SAttilio Rao 	struct ucred *cred;
18255fe58019SAttilio Rao 	vm_page_t *pages;
18265fe58019SAttilio Rao 
18274cff153bSAttilio Rao 	FS_DEBUG2G("heh\n");
18285fe58019SAttilio Rao 
18295fe58019SAttilio Rao 	vp = ap->a_vp;
18305fe58019SAttilio Rao 	KASSERT(vp->v_object, ("objectless vp passed to getpages"));
18315fe58019SAttilio Rao 	td = curthread;			/* XXX */
18325fe58019SAttilio Rao 	cred = curthread->td_ucred;	/* XXX */
18335fe58019SAttilio Rao 	pages = ap->a_m;
1834f17f88d3SGleb Smirnoff 	npages = ap->a_count;
18355fe58019SAttilio Rao 
18365fe58019SAttilio Rao 	if (!fsess_opt_mmap(vnode_mount(vp))) {
18374cff153bSAttilio Rao 		FS_DEBUG("called on non-cacheable vnode??\n");
18385fe58019SAttilio Rao 		return (VM_PAGER_ERROR);
18395fe58019SAttilio Rao 	}
18405fe58019SAttilio Rao 
18415fe58019SAttilio Rao 	/*
1842b0cd2017SGleb Smirnoff 	 * If the last page is partially valid, just return it and allow
1843b0cd2017SGleb Smirnoff 	 * the pager to zero-out the blanks.  Partially valid pages can
1844b0cd2017SGleb Smirnoff 	 * only occur at the file EOF.
1845b0cd2017SGleb Smirnoff 	 *
1846b0cd2017SGleb Smirnoff 	 * XXXGL: is that true for FUSE, which is a local filesystem,
1847b0cd2017SGleb Smirnoff 	 * but still somewhat disconnected from the kernel?
18485fe58019SAttilio Rao 	 */
184989f6b863SAttilio Rao 	VM_OBJECT_WLOCK(vp->v_object);
1850f17f88d3SGleb Smirnoff 	if (pages[npages - 1]->valid != 0 && --npages == 0)
1851f17f88d3SGleb Smirnoff 		goto out;
185289f6b863SAttilio Rao 	VM_OBJECT_WUNLOCK(vp->v_object);
18535fe58019SAttilio Rao 
18545fe58019SAttilio Rao 	/*
18555fe58019SAttilio Rao 	 * We use only the kva address for the buffer, but this is extremely
1856b3a15dddSPedro F. Giffuni 	 * convenient and fast.
18575fe58019SAttilio Rao 	 */
1858756a5412SGleb Smirnoff 	bp = uma_zalloc(fuse_pbuf_zone, M_WAITOK);
18595fe58019SAttilio Rao 
18605fe58019SAttilio Rao 	kva = (vm_offset_t)bp->b_data;
18615fe58019SAttilio Rao 	pmap_qenter(kva, pages, npages);
186283c9dea1SGleb Smirnoff 	VM_CNT_INC(v_vnodein);
186383c9dea1SGleb Smirnoff 	VM_CNT_ADD(v_vnodepgsin, npages);
18645fe58019SAttilio Rao 
1865f17f88d3SGleb Smirnoff 	count = npages << PAGE_SHIFT;
18665fe58019SAttilio Rao 	iov.iov_base = (caddr_t)kva;
18675fe58019SAttilio Rao 	iov.iov_len = count;
18685fe58019SAttilio Rao 	uio.uio_iov = &iov;
18695fe58019SAttilio Rao 	uio.uio_iovcnt = 1;
18705fe58019SAttilio Rao 	uio.uio_offset = IDX_TO_OFF(pages[0]->pindex);
18715fe58019SAttilio Rao 	uio.uio_resid = count;
18725fe58019SAttilio Rao 	uio.uio_segflg = UIO_SYSSPACE;
18735fe58019SAttilio Rao 	uio.uio_rw = UIO_READ;
18745fe58019SAttilio Rao 	uio.uio_td = td;
18755fe58019SAttilio Rao 
18765fe58019SAttilio Rao 	error = fuse_io_dispatch(vp, &uio, IO_DIRECT, cred);
18775fe58019SAttilio Rao 	pmap_qremove(kva, npages);
18785fe58019SAttilio Rao 
1879756a5412SGleb Smirnoff 	uma_zfree(fuse_pbuf_zone, bp);
18805fe58019SAttilio Rao 
18815fe58019SAttilio Rao 	if (error && (uio.uio_resid == count)) {
18824cff153bSAttilio Rao 		FS_DEBUG("error %d\n", error);
18835fe58019SAttilio Rao 		return VM_PAGER_ERROR;
18845fe58019SAttilio Rao 	}
18855fe58019SAttilio Rao 	/*
18865fe58019SAttilio Rao 	 * Calculate the number of bytes read and validate only that number
18875fe58019SAttilio Rao 	 * of bytes.  Note that due to pending writes, size may be 0.  This
18885fe58019SAttilio Rao 	 * does not mean that the remaining data is invalid!
18895fe58019SAttilio Rao 	 */
18905fe58019SAttilio Rao 
18915fe58019SAttilio Rao 	size = count - uio.uio_resid;
189289f6b863SAttilio Rao 	VM_OBJECT_WLOCK(vp->v_object);
18935fe58019SAttilio Rao 	fuse_vm_page_lock_queues();
18945fe58019SAttilio Rao 	for (i = 0, toff = 0; i < npages; i++, toff = nextoff) {
18955fe58019SAttilio Rao 		vm_page_t m;
18965fe58019SAttilio Rao 
18975fe58019SAttilio Rao 		nextoff = toff + PAGE_SIZE;
18985fe58019SAttilio Rao 		m = pages[i];
18995fe58019SAttilio Rao 
19005fe58019SAttilio Rao 		if (nextoff <= size) {
19015fe58019SAttilio Rao 			/*
19025fe58019SAttilio Rao 			 * Read operation filled an entire page
19035fe58019SAttilio Rao 			 */
19045fe58019SAttilio Rao 			m->valid = VM_PAGE_BITS_ALL;
19055fe58019SAttilio Rao 			KASSERT(m->dirty == 0,
19065fe58019SAttilio Rao 			    ("fuse_getpages: page %p is dirty", m));
19075fe58019SAttilio Rao 		} else if (size > toff) {
19085fe58019SAttilio Rao 			/*
19095fe58019SAttilio Rao 			 * Read operation filled a partial page.
19105fe58019SAttilio Rao 			 */
19115fe58019SAttilio Rao 			m->valid = 0;
19125fe58019SAttilio Rao 			vm_page_set_valid_range(m, 0, size - toff);
19135fe58019SAttilio Rao 			KASSERT(m->dirty == 0,
19145fe58019SAttilio Rao 			    ("fuse_getpages: page %p is dirty", m));
19155fe58019SAttilio Rao 		} else {
19165fe58019SAttilio Rao 			/*
1917b3a15dddSPedro F. Giffuni 			 * Read operation was short.  If no error occurred
19185fe58019SAttilio Rao 			 * we may have hit a zero-fill section.   We simply
19195fe58019SAttilio Rao 			 * leave valid set to 0.
19205fe58019SAttilio Rao 			 */
19215fe58019SAttilio Rao 			;
19225fe58019SAttilio Rao 		}
19235fe58019SAttilio Rao 	}
19245fe58019SAttilio Rao 	fuse_vm_page_unlock_queues();
1925f17f88d3SGleb Smirnoff out:
192689f6b863SAttilio Rao 	VM_OBJECT_WUNLOCK(vp->v_object);
1927f17f88d3SGleb Smirnoff 	if (ap->a_rbehind)
1928f17f88d3SGleb Smirnoff 		*ap->a_rbehind = 0;
1929f17f88d3SGleb Smirnoff 	if (ap->a_rahead)
1930f17f88d3SGleb Smirnoff 		*ap->a_rahead = 0;
1931f17f88d3SGleb Smirnoff 	return (VM_PAGER_OK);
19325fe58019SAttilio Rao }
19335fe58019SAttilio Rao 
19345fe58019SAttilio Rao /*
19355fe58019SAttilio Rao     struct vnop_putpages_args {
19365fe58019SAttilio Rao 	struct vnode *a_vp;
19375fe58019SAttilio Rao 	vm_page_t *a_m;
19385fe58019SAttilio Rao 	int a_count;
19395fe58019SAttilio Rao 	int a_sync;
19405fe58019SAttilio Rao 	int *a_rtvals;
19415fe58019SAttilio Rao 	vm_ooffset_t a_offset;
19425fe58019SAttilio Rao     };
19435fe58019SAttilio Rao */
19445fe58019SAttilio Rao static int
19455fe58019SAttilio Rao fuse_vnop_putpages(struct vop_putpages_args *ap)
19465fe58019SAttilio Rao {
19475fe58019SAttilio Rao 	struct uio uio;
19485fe58019SAttilio Rao 	struct iovec iov;
19495fe58019SAttilio Rao 	vm_offset_t kva;
19505fe58019SAttilio Rao 	struct buf *bp;
19515fe58019SAttilio Rao 	int i, error, npages, count;
19525fe58019SAttilio Rao 	off_t offset;
19535fe58019SAttilio Rao 	int *rtvals;
19545fe58019SAttilio Rao 	struct vnode *vp;
19555fe58019SAttilio Rao 	struct thread *td;
19565fe58019SAttilio Rao 	struct ucred *cred;
19575fe58019SAttilio Rao 	vm_page_t *pages;
19585fe58019SAttilio Rao 	vm_ooffset_t fsize;
19595fe58019SAttilio Rao 
19604cff153bSAttilio Rao 	FS_DEBUG2G("heh\n");
19615fe58019SAttilio Rao 
19625fe58019SAttilio Rao 	vp = ap->a_vp;
19635fe58019SAttilio Rao 	KASSERT(vp->v_object, ("objectless vp passed to putpages"));
19645fe58019SAttilio Rao 	fsize = vp->v_object->un_pager.vnp.vnp_size;
19655fe58019SAttilio Rao 	td = curthread;			/* XXX */
19665fe58019SAttilio Rao 	cred = curthread->td_ucred;	/* XXX */
19675fe58019SAttilio Rao 	pages = ap->a_m;
19685fe58019SAttilio Rao 	count = ap->a_count;
19695fe58019SAttilio Rao 	rtvals = ap->a_rtvals;
19705fe58019SAttilio Rao 	npages = btoc(count);
19715fe58019SAttilio Rao 	offset = IDX_TO_OFF(pages[0]->pindex);
19725fe58019SAttilio Rao 
19735fe58019SAttilio Rao 	if (!fsess_opt_mmap(vnode_mount(vp))) {
19744cff153bSAttilio Rao 		FS_DEBUG("called on non-cacheable vnode??\n");
19755fe58019SAttilio Rao 	}
19765fe58019SAttilio Rao 	for (i = 0; i < npages; i++)
19775fe58019SAttilio Rao 		rtvals[i] = VM_PAGER_AGAIN;
19785fe58019SAttilio Rao 
19795fe58019SAttilio Rao 	/*
19805fe58019SAttilio Rao 	 * When putting pages, do not extend file past EOF.
19815fe58019SAttilio Rao 	 */
19825fe58019SAttilio Rao 
19835fe58019SAttilio Rao 	if (offset + count > fsize) {
19845fe58019SAttilio Rao 		count = fsize - offset;
19855fe58019SAttilio Rao 		if (count < 0)
19865fe58019SAttilio Rao 			count = 0;
19875fe58019SAttilio Rao 	}
19885fe58019SAttilio Rao 	/*
19895fe58019SAttilio Rao 	 * We use only the kva address for the buffer, but this is extremely
1990b3a15dddSPedro F. Giffuni 	 * convenient and fast.
19915fe58019SAttilio Rao 	 */
1992756a5412SGleb Smirnoff 	bp = uma_zalloc(fuse_pbuf_zone, M_WAITOK);
19935fe58019SAttilio Rao 
19945fe58019SAttilio Rao 	kva = (vm_offset_t)bp->b_data;
19955fe58019SAttilio Rao 	pmap_qenter(kva, pages, npages);
199683c9dea1SGleb Smirnoff 	VM_CNT_INC(v_vnodeout);
199783c9dea1SGleb Smirnoff 	VM_CNT_ADD(v_vnodepgsout, count);
19985fe58019SAttilio Rao 
19995fe58019SAttilio Rao 	iov.iov_base = (caddr_t)kva;
20005fe58019SAttilio Rao 	iov.iov_len = count;
20015fe58019SAttilio Rao 	uio.uio_iov = &iov;
20025fe58019SAttilio Rao 	uio.uio_iovcnt = 1;
20035fe58019SAttilio Rao 	uio.uio_offset = offset;
20045fe58019SAttilio Rao 	uio.uio_resid = count;
20055fe58019SAttilio Rao 	uio.uio_segflg = UIO_SYSSPACE;
20065fe58019SAttilio Rao 	uio.uio_rw = UIO_WRITE;
20075fe58019SAttilio Rao 	uio.uio_td = td;
20085fe58019SAttilio Rao 
20095fe58019SAttilio Rao 	error = fuse_io_dispatch(vp, &uio, IO_DIRECT, cred);
20105fe58019SAttilio Rao 
20115fe58019SAttilio Rao 	pmap_qremove(kva, npages);
2012756a5412SGleb Smirnoff 	uma_zfree(fuse_pbuf_zone, bp);
20135fe58019SAttilio Rao 
20145fe58019SAttilio Rao 	if (!error) {
20155fe58019SAttilio Rao 		int nwritten = round_page(count - uio.uio_resid) / PAGE_SIZE;
20165fe58019SAttilio Rao 
20175fe58019SAttilio Rao 		for (i = 0; i < nwritten; i++) {
20185fe58019SAttilio Rao 			rtvals[i] = VM_PAGER_OK;
201989f6b863SAttilio Rao 			VM_OBJECT_WLOCK(pages[i]->object);
20205fe58019SAttilio Rao 			vm_page_undirty(pages[i]);
202189f6b863SAttilio Rao 			VM_OBJECT_WUNLOCK(pages[i]->object);
20225fe58019SAttilio Rao 		}
20235fe58019SAttilio Rao 	}
20245fe58019SAttilio Rao 	return rtvals[0];
20255fe58019SAttilio Rao }
20265fe58019SAttilio Rao 
202704660064SFedor Uporov static const char extattr_namespace_separator = '.';
202804660064SFedor Uporov 
202904660064SFedor Uporov /*
203004660064SFedor Uporov     struct vop_getextattr_args {
203104660064SFedor Uporov 	struct vop_generic_args a_gen;
203204660064SFedor Uporov 	struct vnode *a_vp;
203304660064SFedor Uporov 	int a_attrnamespace;
203404660064SFedor Uporov 	const char *a_name;
203504660064SFedor Uporov 	struct uio *a_uio;
203604660064SFedor Uporov 	size_t *a_size;
203704660064SFedor Uporov 	struct ucred *a_cred;
203804660064SFedor Uporov 	struct thread *a_td;
203904660064SFedor Uporov     };
204004660064SFedor Uporov */
204104660064SFedor Uporov static int
204204660064SFedor Uporov fuse_vnop_getextattr(struct vop_getextattr_args *ap)
204304660064SFedor Uporov {
204404660064SFedor Uporov 	struct vnode *vp = ap->a_vp;
204504660064SFedor Uporov 	struct uio *uio = ap->a_uio;
204628f4f623SFedor Uporov 	struct fuse_dispatcher fdi;
204704660064SFedor Uporov 	struct fuse_getxattr_in *get_xattr_in;
204804660064SFedor Uporov 	struct fuse_getxattr_out *get_xattr_out;
204904660064SFedor Uporov 	struct mount *mp = vnode_mount(vp);
205004660064SFedor Uporov 	struct thread *td = ap->a_td;
205104660064SFedor Uporov 	struct ucred *cred = ap->a_cred;
205228f4f623SFedor Uporov 	char *prefix;
205328f4f623SFedor Uporov 	char *attr_str;
205428f4f623SFedor Uporov 	size_t len;
205528f4f623SFedor Uporov 	int err;
205604660064SFedor Uporov 
205704660064SFedor Uporov 	fuse_trace_printf_vnop();
205804660064SFedor Uporov 
205904660064SFedor Uporov 	if (fuse_isdeadfs(vp))
206028f4f623SFedor Uporov 		return (ENXIO);
206104660064SFedor Uporov 
206204660064SFedor Uporov 	/* Default to looking for user attributes. */
206304660064SFedor Uporov 	if (ap->a_attrnamespace == EXTATTR_NAMESPACE_SYSTEM)
206404660064SFedor Uporov 		prefix = EXTATTR_NAMESPACE_SYSTEM_STRING;
206504660064SFedor Uporov 	else
206604660064SFedor Uporov 		prefix = EXTATTR_NAMESPACE_USER_STRING;
206704660064SFedor Uporov 
206804660064SFedor Uporov 	len = strlen(prefix) + sizeof(extattr_namespace_separator) +
206904660064SFedor Uporov 	    strlen(ap->a_name) + 1;
207004660064SFedor Uporov 
207104660064SFedor Uporov 	fdisp_init(&fdi, len + sizeof(*get_xattr_in));
207204660064SFedor Uporov 	fdisp_make_vp(&fdi, FUSE_GETXATTR, vp, td, cred);
207304660064SFedor Uporov 
207404660064SFedor Uporov 	get_xattr_in = fdi.indata;
207504660064SFedor Uporov 	/*
207604660064SFedor Uporov 	 * Check to see whether we're querying the available size or
207704660064SFedor Uporov 	 * issuing the actual request.  If we pass in 0, we get back struct
207804660064SFedor Uporov 	 * fuse_getxattr_out.  If we pass in a non-zero size, we get back
207904660064SFedor Uporov 	 * that much data, without the struct fuse_getxattr_out header.
208004660064SFedor Uporov 	 */
2081493b4a8cSFedor Uporov 	if (uio == NULL)
208204660064SFedor Uporov 		get_xattr_in->size = 0;
208304660064SFedor Uporov 	else
208404660064SFedor Uporov 		get_xattr_in->size = uio->uio_resid;
208504660064SFedor Uporov 
208604660064SFedor Uporov 	attr_str = (char *)fdi.indata + sizeof(*get_xattr_in);
208704660064SFedor Uporov 	snprintf(attr_str, len, "%s%c%s", prefix, extattr_namespace_separator,
208804660064SFedor Uporov 	    ap->a_name);
208904660064SFedor Uporov 
209004660064SFedor Uporov 	err = fdisp_wait_answ(&fdi);
209104660064SFedor Uporov 	if (err != 0) {
209204660064SFedor Uporov 		if (err == ENOSYS)
209304660064SFedor Uporov 			fsess_set_notimpl(mp, FUSE_GETXATTR);
209404660064SFedor Uporov 		debug_printf("getxattr: got err=%d from daemon\n", err);
209504660064SFedor Uporov 		goto out;
209604660064SFedor Uporov 	}
209704660064SFedor Uporov 
209804660064SFedor Uporov 	get_xattr_out = fdi.answ;
209904660064SFedor Uporov 
2100493b4a8cSFedor Uporov 	if (ap->a_size != NULL)
210104660064SFedor Uporov 		*ap->a_size = get_xattr_out->size;
2102493b4a8cSFedor Uporov 
2103493b4a8cSFedor Uporov 	if (uio != NULL)
210404660064SFedor Uporov 		err = uiomove(fdi.answ, fdi.iosize, uio);
210504660064SFedor Uporov 
210604660064SFedor Uporov out:
210704660064SFedor Uporov 	fdisp_destroy(&fdi);
210804660064SFedor Uporov 	return (err);
210904660064SFedor Uporov }
211004660064SFedor Uporov 
211104660064SFedor Uporov /*
211204660064SFedor Uporov     struct vop_setextattr_args {
211304660064SFedor Uporov 	struct vop_generic_args a_gen;
211404660064SFedor Uporov 	struct vnode *a_vp;
211504660064SFedor Uporov 	int a_attrnamespace;
211604660064SFedor Uporov 	const char *a_name;
211704660064SFedor Uporov 	struct uio *a_uio;
211804660064SFedor Uporov 	struct ucred *a_cred;
211904660064SFedor Uporov 	struct thread *a_td;
212004660064SFedor Uporov     };
212104660064SFedor Uporov */
212204660064SFedor Uporov static int
212304660064SFedor Uporov fuse_vnop_setextattr(struct vop_setextattr_args *ap)
212404660064SFedor Uporov {
212504660064SFedor Uporov 	struct vnode *vp = ap->a_vp;
212604660064SFedor Uporov 	struct uio *uio = ap->a_uio;
212728f4f623SFedor Uporov 	struct fuse_dispatcher fdi;
212804660064SFedor Uporov 	struct fuse_setxattr_in *set_xattr_in;
212904660064SFedor Uporov 	struct mount *mp = vnode_mount(vp);
213028f4f623SFedor Uporov 	struct thread *td = ap->a_td;
213128f4f623SFedor Uporov 	struct ucred *cred = ap->a_cred;
213204660064SFedor Uporov 	char *prefix;
213304660064SFedor Uporov 	size_t len;
213404660064SFedor Uporov 	char *attr_str;
213528f4f623SFedor Uporov 	int err;
213604660064SFedor Uporov 
213704660064SFedor Uporov 	fuse_trace_printf_vnop();
213804660064SFedor Uporov 
213904660064SFedor Uporov 	if (fuse_isdeadfs(vp))
214028f4f623SFedor Uporov 		return (ENXIO);
214104660064SFedor Uporov 
214204660064SFedor Uporov 	/* Default to looking for user attributes. */
214304660064SFedor Uporov 	if (ap->a_attrnamespace == EXTATTR_NAMESPACE_SYSTEM)
214404660064SFedor Uporov 		prefix = EXTATTR_NAMESPACE_SYSTEM_STRING;
214504660064SFedor Uporov 	else
214604660064SFedor Uporov 		prefix = EXTATTR_NAMESPACE_USER_STRING;
214704660064SFedor Uporov 
214804660064SFedor Uporov 	len = strlen(prefix) + sizeof(extattr_namespace_separator) +
214904660064SFedor Uporov 	    strlen(ap->a_name) + 1;
215004660064SFedor Uporov 
215104660064SFedor Uporov 	fdisp_init(&fdi, len + sizeof(*set_xattr_in) + uio->uio_resid);
215204660064SFedor Uporov 	fdisp_make_vp(&fdi, FUSE_SETXATTR, vp, td, cred);
215304660064SFedor Uporov 
215404660064SFedor Uporov 	set_xattr_in = fdi.indata;
215504660064SFedor Uporov 	set_xattr_in->size = uio->uio_resid;
215604660064SFedor Uporov 
215704660064SFedor Uporov 	attr_str = (char *)fdi.indata + sizeof(*set_xattr_in);
215804660064SFedor Uporov 	snprintf(attr_str, len, "%s%c%s", prefix, extattr_namespace_separator,
215904660064SFedor Uporov 	    ap->a_name);
216004660064SFedor Uporov 
216104660064SFedor Uporov 	err = uiomove((char *)fdi.indata + sizeof(*set_xattr_in) + len,
216204660064SFedor Uporov 	    uio->uio_resid, uio);
216304660064SFedor Uporov 	if (err != 0) {
216404660064SFedor Uporov 		debug_printf("setxattr: got error %d from uiomove\n", err);
216504660064SFedor Uporov 		goto out;
216604660064SFedor Uporov 	}
216704660064SFedor Uporov 
216804660064SFedor Uporov 	err = fdisp_wait_answ(&fdi);
216904660064SFedor Uporov 
217004660064SFedor Uporov 	if (err != 0) {
217104660064SFedor Uporov 		if (err == ENOSYS)
217204660064SFedor Uporov 			fsess_set_notimpl(mp, FUSE_SETXATTR);
217304660064SFedor Uporov 		debug_printf("setxattr: got err=%d from daemon\n", err);
217404660064SFedor Uporov 		goto out;
217504660064SFedor Uporov 	}
217604660064SFedor Uporov 
217704660064SFedor Uporov out:
217804660064SFedor Uporov 	fdisp_destroy(&fdi);
217904660064SFedor Uporov 	return (err);
218004660064SFedor Uporov }
218104660064SFedor Uporov 
218204660064SFedor Uporov /*
218304660064SFedor Uporov  * The Linux / FUSE extended attribute list is simply a collection of
218404660064SFedor Uporov  * NUL-terminated strings.  The FreeBSD extended attribute list is a single
218504660064SFedor Uporov  * byte length followed by a non-NUL terminated string.  So, this allows
218604660064SFedor Uporov  * conversion of the Linux / FUSE format to the FreeBSD format in place.
218704660064SFedor Uporov  * Linux attribute names are reported with the namespace as a prefix (e.g.
218804660064SFedor Uporov  * "user.attribute_name"), but in FreeBSD they are reported without the
218904660064SFedor Uporov  * namespace prefix (e.g. "attribute_name").  So, we're going from:
219004660064SFedor Uporov  *
219104660064SFedor Uporov  * user.attr_name1\0user.attr_name2\0
219204660064SFedor Uporov  *
219304660064SFedor Uporov  * to:
219404660064SFedor Uporov  *
219504660064SFedor Uporov  * <num>attr_name1<num>attr_name2
219604660064SFedor Uporov  *
219704660064SFedor Uporov  * Where "<num>" is a single byte number of characters in the attribute name.
219804660064SFedor Uporov  *
219904660064SFedor Uporov  * Args:
220004660064SFedor Uporov  * prefix - exattr namespace prefix string
220104660064SFedor Uporov  * list, list_len - input list with namespace prefixes
220204660064SFedor Uporov  * bsd_list, bsd_list_len - output list compatible with bsd vfs
220304660064SFedor Uporov  */
220404660064SFedor Uporov static int
220504660064SFedor Uporov fuse_xattrlist_convert(char *prefix, const char *list, int list_len,
220604660064SFedor Uporov     char *bsd_list, int *bsd_list_len)
220704660064SFedor Uporov {
220804660064SFedor Uporov 	int len, pos, dist_to_next, prefix_len;
220904660064SFedor Uporov 
221004660064SFedor Uporov 	pos = 0;
221104660064SFedor Uporov 	*bsd_list_len = 0;
221204660064SFedor Uporov 	prefix_len = strlen(prefix);
221304660064SFedor Uporov 
221404660064SFedor Uporov 	while (pos < list_len && list[pos] != '\0') {
221504660064SFedor Uporov 		dist_to_next = strlen(&list[pos]) + 1;
221604660064SFedor Uporov 		if (bcmp(&list[pos], prefix, prefix_len) == 0 &&
221704660064SFedor Uporov 		    list[pos + prefix_len] == extattr_namespace_separator) {
221804660064SFedor Uporov 			len = dist_to_next -
221904660064SFedor Uporov 			    (prefix_len + sizeof(extattr_namespace_separator)) - 1;
222004660064SFedor Uporov 			if (len >= EXTATTR_MAXNAMELEN)
222104660064SFedor Uporov 				return (ENAMETOOLONG);
222204660064SFedor Uporov 
222304660064SFedor Uporov 			bsd_list[*bsd_list_len] = len;
222404660064SFedor Uporov 			memcpy(&bsd_list[*bsd_list_len + 1],
222504660064SFedor Uporov 			    &list[pos + prefix_len +
222604660064SFedor Uporov 			    sizeof(extattr_namespace_separator)], len);
222704660064SFedor Uporov 
222804660064SFedor Uporov 			*bsd_list_len += len + 1;
222904660064SFedor Uporov 		}
223004660064SFedor Uporov 
223104660064SFedor Uporov 		pos += dist_to_next;
223204660064SFedor Uporov 	}
223304660064SFedor Uporov 
223404660064SFedor Uporov 	return (0);
223504660064SFedor Uporov }
223604660064SFedor Uporov 
223704660064SFedor Uporov /*
223804660064SFedor Uporov     struct vop_listextattr_args {
223904660064SFedor Uporov 	struct vop_generic_args a_gen;
224004660064SFedor Uporov 	struct vnode *a_vp;
224104660064SFedor Uporov 	int a_attrnamespace;
224204660064SFedor Uporov 	struct uio *a_uio;
224304660064SFedor Uporov 	size_t *a_size;
224404660064SFedor Uporov 	struct ucred *a_cred;
224504660064SFedor Uporov 	struct thread *a_td;
224604660064SFedor Uporov     };
224704660064SFedor Uporov */
224804660064SFedor Uporov static int
224904660064SFedor Uporov fuse_vnop_listextattr(struct vop_listextattr_args *ap)
225004660064SFedor Uporov {
225104660064SFedor Uporov 	struct vnode *vp = ap->a_vp;
225204660064SFedor Uporov 	struct uio *uio = ap->a_uio;
225328f4f623SFedor Uporov 	struct fuse_dispatcher fdi;
2254493b4a8cSFedor Uporov 	struct fuse_listxattr_in *list_xattr_in;
2255493b4a8cSFedor Uporov 	struct fuse_listxattr_out *list_xattr_out;
225604660064SFedor Uporov 	struct mount *mp = vnode_mount(vp);
225728f4f623SFedor Uporov 	struct thread *td = ap->a_td;
225828f4f623SFedor Uporov 	struct ucred *cred = ap->a_cred;
225904660064SFedor Uporov 	size_t len;
226004660064SFedor Uporov 	char *prefix;
226104660064SFedor Uporov 	char *attr_str;
226204660064SFedor Uporov 	char *bsd_list = NULL;
2263493b4a8cSFedor Uporov 	char *linux_list;
226404660064SFedor Uporov 	int bsd_list_len;
2265493b4a8cSFedor Uporov 	int linux_list_len;
226628f4f623SFedor Uporov 	int err;
226704660064SFedor Uporov 
226804660064SFedor Uporov 	fuse_trace_printf_vnop();
226904660064SFedor Uporov 
227004660064SFedor Uporov 	if (fuse_isdeadfs(vp))
227128f4f623SFedor Uporov 		return (ENXIO);
227204660064SFedor Uporov 
227304660064SFedor Uporov 	/*
227404660064SFedor Uporov 	 * Add space for a NUL and the period separator if enabled.
227504660064SFedor Uporov 	 * Default to looking for user attributes.
227604660064SFedor Uporov 	 */
227704660064SFedor Uporov 	if (ap->a_attrnamespace == EXTATTR_NAMESPACE_SYSTEM)
227804660064SFedor Uporov 		prefix = EXTATTR_NAMESPACE_SYSTEM_STRING;
227904660064SFedor Uporov 	else
228004660064SFedor Uporov 		prefix = EXTATTR_NAMESPACE_USER_STRING;
228104660064SFedor Uporov 
228204660064SFedor Uporov 	len = strlen(prefix) + sizeof(extattr_namespace_separator) + 1;
228304660064SFedor Uporov 
2284493b4a8cSFedor Uporov 	fdisp_init(&fdi, sizeof(*list_xattr_in) + len);
228504660064SFedor Uporov 	fdisp_make_vp(&fdi, FUSE_LISTXATTR, vp, td, cred);
228604660064SFedor Uporov 
2287493b4a8cSFedor Uporov 	/*
2288493b4a8cSFedor Uporov 	 * Retrieve Linux / FUSE compatible list size.
2289493b4a8cSFedor Uporov 	 */
2290493b4a8cSFedor Uporov 	list_xattr_in = fdi.indata;
2291493b4a8cSFedor Uporov 	list_xattr_in->size = 0;
2292493b4a8cSFedor Uporov 	attr_str = (char *)fdi.indata + sizeof(*list_xattr_in);
229304660064SFedor Uporov 	snprintf(attr_str, len, "%s%c", prefix, extattr_namespace_separator);
229404660064SFedor Uporov 
229504660064SFedor Uporov 	err = fdisp_wait_answ(&fdi);
229604660064SFedor Uporov 	if (err != 0) {
229704660064SFedor Uporov 		if (err == ENOSYS)
229804660064SFedor Uporov 			fsess_set_notimpl(mp, FUSE_LISTXATTR);
229904660064SFedor Uporov 		debug_printf("listextattr: got err=%d from daemon\n", err);
230004660064SFedor Uporov 		goto out;
230104660064SFedor Uporov 	}
230204660064SFedor Uporov 
2303493b4a8cSFedor Uporov 	list_xattr_out = fdi.answ;
2304493b4a8cSFedor Uporov 	linux_list_len = list_xattr_out->size;
2305493b4a8cSFedor Uporov 	if (linux_list_len == 0) {
2306493b4a8cSFedor Uporov 		if (ap->a_size != NULL)
2307493b4a8cSFedor Uporov 			*ap->a_size = linux_list_len;
230804660064SFedor Uporov 		goto out;
230904660064SFedor Uporov 	}
231004660064SFedor Uporov 
231104660064SFedor Uporov 	/*
2312493b4a8cSFedor Uporov 	 * Retrieve Linux / FUSE compatible list values.
2313493b4a8cSFedor Uporov 	 */
2314493b4a8cSFedor Uporov 	fdisp_make_vp(&fdi, FUSE_LISTXATTR, vp, td, cred);
2315493b4a8cSFedor Uporov 	list_xattr_in = fdi.indata;
2316493b4a8cSFedor Uporov 	list_xattr_in->size = linux_list_len + sizeof(*list_xattr_out);
2317493b4a8cSFedor Uporov 	attr_str = (char *)fdi.indata + sizeof(*list_xattr_in);
2318493b4a8cSFedor Uporov 	snprintf(attr_str, len, "%s%c", prefix, extattr_namespace_separator);
2319493b4a8cSFedor Uporov 
2320493b4a8cSFedor Uporov 	err = fdisp_wait_answ(&fdi);
2321493b4a8cSFedor Uporov 	if (err != 0)
2322493b4a8cSFedor Uporov 		goto out;
2323493b4a8cSFedor Uporov 
2324493b4a8cSFedor Uporov 	linux_list = fdi.answ;
2325493b4a8cSFedor Uporov 	linux_list_len = fdi.iosize;
2326493b4a8cSFedor Uporov 
2327493b4a8cSFedor Uporov 	/*
2328493b4a8cSFedor Uporov 	 * Retrieve the BSD compatible list values.
232904660064SFedor Uporov 	 * The Linux / FUSE attribute list format isn't the same
233004660064SFedor Uporov 	 * as FreeBSD's format. So we need to transform it into
233104660064SFedor Uporov 	 * FreeBSD's format before giving it to the user.
233204660064SFedor Uporov 	 */
2333493b4a8cSFedor Uporov 	bsd_list = malloc(linux_list_len, M_TEMP, M_WAITOK);
2334493b4a8cSFedor Uporov 	err = fuse_xattrlist_convert(prefix, linux_list, linux_list_len,
233504660064SFedor Uporov 	    bsd_list, &bsd_list_len);
233604660064SFedor Uporov 	if (err != 0)
233704660064SFedor Uporov 		goto out;
233804660064SFedor Uporov 
2339493b4a8cSFedor Uporov 	if (ap->a_size != NULL)
2340493b4a8cSFedor Uporov 		*ap->a_size = bsd_list_len;
2341493b4a8cSFedor Uporov 
2342493b4a8cSFedor Uporov 	if (uio != NULL)
234304660064SFedor Uporov 		err = uiomove(bsd_list, bsd_list_len, uio);
234404660064SFedor Uporov 
234504660064SFedor Uporov out:
234604660064SFedor Uporov 	free(bsd_list, M_TEMP);
234704660064SFedor Uporov 	fdisp_destroy(&fdi);
234804660064SFedor Uporov 	return (err);
234904660064SFedor Uporov }
235004660064SFedor Uporov 
235104660064SFedor Uporov /*
235204660064SFedor Uporov     struct vop_deleteextattr_args {
235304660064SFedor Uporov 	struct vop_generic_args a_gen;
235404660064SFedor Uporov 	struct vnode *a_vp;
235504660064SFedor Uporov 	int a_attrnamespace;
235604660064SFedor Uporov 	const char *a_name;
235704660064SFedor Uporov 	struct ucred *a_cred;
235804660064SFedor Uporov 	struct thread *a_td;
235904660064SFedor Uporov     };
236004660064SFedor Uporov */
236104660064SFedor Uporov static int
236204660064SFedor Uporov fuse_vnop_deleteextattr(struct vop_deleteextattr_args *ap)
236304660064SFedor Uporov {
236404660064SFedor Uporov 	struct vnode *vp = ap->a_vp;
236528f4f623SFedor Uporov 	struct fuse_dispatcher fdi;
236604660064SFedor Uporov 	struct mount *mp = vnode_mount(vp);
236728f4f623SFedor Uporov 	struct thread *td = ap->a_td;
236828f4f623SFedor Uporov 	struct ucred *cred = ap->a_cred;
236904660064SFedor Uporov 	char *prefix;
237004660064SFedor Uporov 	size_t len;
237104660064SFedor Uporov 	char *attr_str;
237204660064SFedor Uporov 	int err;
237304660064SFedor Uporov 
237404660064SFedor Uporov 	fuse_trace_printf_vnop();
237504660064SFedor Uporov 
237604660064SFedor Uporov 	if (fuse_isdeadfs(vp))
237728f4f623SFedor Uporov 		return (ENXIO);
237804660064SFedor Uporov 
237904660064SFedor Uporov 	/* Default to looking for user attributes. */
238004660064SFedor Uporov 	if (ap->a_attrnamespace == EXTATTR_NAMESPACE_SYSTEM)
238104660064SFedor Uporov 		prefix = EXTATTR_NAMESPACE_SYSTEM_STRING;
238204660064SFedor Uporov 	else
238304660064SFedor Uporov 		prefix = EXTATTR_NAMESPACE_USER_STRING;
238404660064SFedor Uporov 
238504660064SFedor Uporov 	len = strlen(prefix) + sizeof(extattr_namespace_separator) +
238604660064SFedor Uporov 	    strlen(ap->a_name) + 1;
238704660064SFedor Uporov 
238804660064SFedor Uporov 	fdisp_init(&fdi, len);
238904660064SFedor Uporov 	fdisp_make_vp(&fdi, FUSE_REMOVEXATTR, vp, td, cred);
239004660064SFedor Uporov 
239104660064SFedor Uporov 	attr_str = fdi.indata;
239204660064SFedor Uporov 	snprintf(attr_str, len, "%s%c%s", prefix, extattr_namespace_separator,
239304660064SFedor Uporov 	    ap->a_name);
239404660064SFedor Uporov 
239504660064SFedor Uporov 	err = fdisp_wait_answ(&fdi);
239604660064SFedor Uporov 	if (err != 0) {
239704660064SFedor Uporov 		if (err == ENOSYS)
239804660064SFedor Uporov 			fsess_set_notimpl(mp, FUSE_REMOVEXATTR);
239904660064SFedor Uporov 		debug_printf("removexattr: got err=%d from daemon\n", err);
240004660064SFedor Uporov 	}
240104660064SFedor Uporov 
240204660064SFedor Uporov 	fdisp_destroy(&fdi);
240304660064SFedor Uporov 	return (err);
240404660064SFedor Uporov }
240504660064SFedor Uporov 
24065fe58019SAttilio Rao /*
24075fe58019SAttilio Rao     struct vnop_print_args {
24085fe58019SAttilio Rao 	struct vnode *a_vp;
24095fe58019SAttilio Rao     };
24105fe58019SAttilio Rao */
24115fe58019SAttilio Rao static int
24125fe58019SAttilio Rao fuse_vnop_print(struct vop_print_args *ap)
24135fe58019SAttilio Rao {
24145fe58019SAttilio Rao 	struct fuse_vnode_data *fvdat = VTOFUD(ap->a_vp);
24155fe58019SAttilio Rao 
24165fe58019SAttilio Rao 	printf("nodeid: %ju, parent nodeid: %ju, nlookup: %ju, flag: %#x\n",
24175fe58019SAttilio Rao 	    (uintmax_t)VTOILLU(ap->a_vp), (uintmax_t)fvdat->parent_nid,
24185fe58019SAttilio Rao 	    (uintmax_t)fvdat->nlookup,
24195fe58019SAttilio Rao 	    fvdat->flag);
24205fe58019SAttilio Rao 
24215fe58019SAttilio Rao 	return 0;
24225fe58019SAttilio Rao }
2423